view ai/ai.d @ 19:08ddf9e71b88

steer to avoid
author zzzzrrr <mason.green@gmail.com>
date Wed, 25 Mar 2009 14:44:47 -0400
parents 7f74e064dad5
children 6efd0830715b
line wrap: on
line source

/*
 * Copyright (c) 2009, Mason Green (zzzzrrr)
 * http://www.dsource.org/projects/openmelee
 * 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * * Neither the name of the polygonal nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
module openmelee.ai.ai;

import tango.io.Stdout : Stdout;
import tango.math.Math : atan2, abs, PI;

import blaze.common.bzMath: bzVec2, bzClamp;
import blaze.bzWorld : bzWorld;

import openmelee.ai.steer : Steer;
import openmelee.ships.ship : Ship;

class AI {


	Steer steer;
	Ship ship;
	float maxPredictionTime = 0.1f;
    bzWorld m_world;
	bzVec2 st;
    
	this(Ship ship, bzWorld world) {
        m_world = world;
		this.ship = ship;
		steer = new Steer(ship);
	}
	
	void move(Ship enemy) {
	    
        // Elementary steering AI 
	    steer.update();
        st = steer.steerToAvoidObstacles(5, m_world.bodyList);
        
        if(st == bzVec2.zeroVect) {
            st = steer.steerForPursuit(enemy.state, maxPredictionTime);
            chase(enemy);
        } else {
            avoid();
        }
    }
    
    void chase(Ship enemy) {
        
        ship.state.target = st;
        st = ship.rBody.localPoint(st);
        float x = st.x;
		float y = st.y;
        st = -bzVec2(y,-x);
        float angle = atan2(st.x, st.y);
		
		if(abs(angle) > 0.05) {
            if(!ship.state.turn) {
                if(st.x >= 0) {
                    ship.turnRight();
                    ship.state.turn = true;
                } else {
                    ship.state.turn = true;
                    ship.turnLeft();
                }
           }
		} else {
			ship.rBody.angularVelocity = 0.0f;
			ship.state.turn = false;
		}
		
		float range = (ship.state.position - enemy.state.position).length; 
		if(range > 2 && abs(angle) < PI/4) {
			ship.thrust();
		}
    }
    
    void avoid() {
        
        st = ship.rBody.localPoint(st);
        float x = st.x;
		float y = st.y;
        st = -bzVec2(y,-x);
        float angle = atan2(st.x, st.y);
        
        if(st.x >= 0) {
            ship.turnRight();
        } else {
            ship.turnLeft();
        }
       
        ship.state.turn = true;
        
        if(abs(angle) > PI/4) {
			ship.thrust();
		}
    }

}