diff 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 diff
--- a/ai/ai.d	Wed Mar 25 11:28:25 2009 -0400
+++ b/ai/ai.d	Wed Mar 25 14:44:47 2009 -0400
@@ -34,6 +34,7 @@
 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;
@@ -44,8 +45,11 @@
 	Steer steer;
 	Ship ship;
 	float maxPredictionTime = 0.1f;
-	
-	this(Ship ship) {
+    bzWorld m_world;
+	bzVec2 st;
+    
+	this(Ship ship, bzWorld world) {
+        m_world = world;
 		this.ship = ship;
 		steer = new Steer(ship);
 	}
@@ -53,11 +57,20 @@
 	void move(Ship enemy) {
 	    
         // Elementary steering AI 
+	    steer.update();
+        st = steer.steerToAvoidObstacles(5, m_world.bodyList);
         
-	    steer.update();
-	    bzVec2 st;
-	    st = steer.steerForPursuit(enemy.state, maxPredictionTime);
-	    ship.state.target = st;
+        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;
@@ -84,5 +97,26 @@
 			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();
+		}
+    }
 
 }