Mercurial > projects > openmelee
view ai/ai.d @ 23:e79347dd38a3
steering
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Thu, 26 Mar 2009 20:35:58 -0400 |
parents | 4fce5596d1f6 |
children | 441eb7672404 |
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; bool avoidTurn; this(Ship ship, bzWorld world) { m_world = world; this.ship = ship; steer = new Steer(ship); } void move(Ship enemy) { st = bzVec2.zeroVect; // Elementary steering AI steer.update(); //st = steer.steerToAvoidObstacles(0.25, m_world.bodyList); //st = steer.avoid(10.0, m_world.bodyList); //st = steer.getSteering(m_world.bodyList); if(st == bzVec2.zeroVect) { avoidTurn = false; 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(!avoidTurn) { if(st.x >= 0) { ship.turnRight(); } else { ship.turnLeft(); } avoidTurn = true; } ship.thrust(); } }