diff ai/ai.d @ 24:441eb7672404

impleneted steer to avoid
author zzzzrrr <mason.green@gmail.com>
date Fri, 27 Mar 2009 16:05:24 -0400
parents e79347dd38a3
children 88cca12cc8b9
line wrap: on
line diff
--- a/ai/ai.d	Thu Mar 26 20:35:58 2009 -0400
+++ b/ai/ai.d	Fri Mar 27 16:05:24 2009 -0400
@@ -30,8 +30,9 @@
  */
 module openmelee.ai.ai;
 
+import tango.util.container.LinkedList : LinkedList;
 import tango.io.Stdout : Stdout;
-import tango.math.Math : atan2, abs, PI;
+import tango.math.Math : atan2, abs, PI, isNaN;
 
 import blaze.common.bzMath: bzVec2, bzClamp;
 import blaze.bzWorld : bzWorld;
@@ -39,40 +40,57 @@
 import openmelee.ai.steer : Steer;
 import openmelee.ships.ship : Ship;
 
+alias LinkedList!(Ship) ObjectList;
+
+struct Threat {
+    Ship target;
+    bzVec2 steering;
+    float distance = 0.0f;
+    float collisionTime = 0.0f;
+    float minSeparation = float.max;
+    bzVec2 relativePos;
+    bzVec2 relativeVel;
+}
+
 class AI {
 
-
 	Steer steer;
 	Ship ship;
 	float maxPredictionTime = 0.1f;
-    bzWorld m_world;
 	bzVec2 st;
     
-    bool avoidTurn;
+    bool avoidRight;
+    bool avoidLeft;
     
-	this(Ship ship, bzWorld world) {
-        m_world = world;
+	this(Ship ship, ObjectList objectList) {
 		this.ship = ship;
-		steer = new Steer(ship);
+		steer = new Steer(ship, objectList);
 	}
 	
 	void move(Ship enemy) {
-	    
-        st = bzVec2.zeroVect;
+	   
+        Threat threat;
         
         // 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);
+        steer.collisionThreat(threat);
+        st = threat.steering;
         
         if(st == bzVec2.zeroVect) {
-            avoidTurn = false;
-            st = steer.steerForPursuit(enemy.state, maxPredictionTime);
+            if(avoidLeft || avoidRight) {
+                avoidLeft = avoidRight = false;
+                ship.rBody.angularVelocity = 0.0f;
+            }
+            st = steer.targetEnemy(enemy.state, maxPredictionTime);
             chase(enemy);
+            return;
         } else {
+            ship.state.turn = false;
             avoid();
+            return;
         }
+        
+        assert(0);
     }
     
     void chase(Ship enemy) {
@@ -80,10 +98,12 @@
         ship.state.target = st;
         st = ship.rBody.localPoint(st);
         float x = st.x;
-		float y = st.y;
+        float y = st.y;
         st = -bzVec2(y,-x);
+        // Because ship's heading is 90 off rigid body's heading
+        //st = st.rotate(-90);
         float angle = atan2(st.x, st.y);
-		
+        
 		if(abs(angle) > 0.05) {
             if(!ship.state.turn) {
                 if(st.x >= 0) {
@@ -108,18 +128,25 @@
     void avoid() {
         
         st = ship.rBody.localPoint(st);
+        // Because ship's heading is 90 off rigid body's heading
+        //st = st.rotate(-90);
         float x = st.x;
-		float y = st.y;
+        float y = st.y;
         st = -bzVec2(y,-x);
         float angle = atan2(st.x, st.y);
         
-        if(!avoidTurn) {
-            if(st.x >= 0) {
+        if(st.x <= 0) {
+            if(!avoidRight) {
                 ship.turnRight();
-            } else {
+                avoidRight = true;
+                avoidLeft = false;
+            }
+        } else {
+            if(!avoidLeft) {
                 ship.turnLeft();
+                avoidLeft = true;
+                avoidRight = false;
             }
-            avoidTurn = true;
         }
                 
         ship.thrust();