Tanks now take diagonal paths when efficient.
authordsc <david.schoonover@gmail.com>
Fri, 24 Dec 2010 05:34:21 +0000 (21:34 -0800)
committerdsc <david.schoonover@gmail.com>
Fri, 24 Dec 2010 05:34:21 +0000 (21:34 -0800)
src/tanks/map/level.cjs
src/tanks/map/pathmap.cjs
src/tanks/thing/tank.cjs

index fc4f88b..3e7025a 100644 (file)
@@ -43,8 +43,8 @@ Rect.subclass('Level', {
         
         E = 
         game.addThing(new Tank(2), 0,5);
-        // game.addThing(new Tank(2), 1,0);
-        // game.addThing(new Tank(2), 8,1);
+        game.addThing(new Tank(2), 1,0);
+        game.addThing(new Tank(2), 8,1);
         
         I = game.addThing(new Item(), 8,8);
     },
index a522cbb..2e439f9 100644 (file)
@@ -14,8 +14,9 @@ var Y          = require('Y').Y
 
 
 ,   SQRT_TWO = Math.sqrt(2)
+
 ,   PASSABLE = 1 // Does not obstruct other objects
-,   BLOCKING = 2 // Objstructs other blockers
+,   BLOCKING = 2 // Obstructs other blockers
 ,   ZONE     = 3 // Does not obstruct other objects, but still collides with them
 ,
 
@@ -111,19 +112,29 @@ Y.subclass('Square', new Vec(0,0), {
         
         for (ix=-1; ix<2; ix++) {
             for (iy=-1; iy<2; iy++) {
-                // if (ix === 0 && iy === 0)
-                if ( (ix === 0 && iy === 0) || (abs(ix) === 1 && abs(iy) === 1) ) // skipping diagonals
+                // Skip self
+                if (ix === 0 && iy === 0)
                     continue;
+                
+                cost = 1;
                 x1 = x + ix*SIZE;  y1 = y + iy*SIZE;
                 x2 = x1+SIZE;      y2 = y1+SIZE;
+                
+                // filter squares outside bounds
                 if (    pm.x1 >= x1 || pm.y1 >= y1 ||
                         pm.x2 <= x2 || pm.y2 <= y2 )
                     continue;
-                // TODO: handle diagonals: need to ensure we don't try to move through a corner
-                // cost = ( abs(ix) === 1 && abs(iy) === 1 ) ? SQRT_TWO : 1;
-                cost = 1;
+                
+                // Diagonal square
+                if ( abs(ix) === 1 && abs(iy) === 1 ) {
+                    cost = SQRT_TWO;
+                    
+                    // Ensure we don't cut a blocked corner
+                    if ( pm._getSquare(x1,y).blocked || pm._getSquare(x,y1).blocked )
+                        continue;
+                }
+                
                 sq = pm._getSquare(x1,y1);
-                // if ( !sq.blocked ) neighbors.push([ sq, cost*SIZE ]);
                 neighbors.push([ sq, cost*SIZE ]);
             }
         }
index a110d75..cc38b24 100644 (file)
@@ -80,13 +80,12 @@ Thing.subclass('Tank', function(Tank){
     
     this['act'] =
     function act(elapsed, now){
+        var ai = this.ai;
         this.elapsed = elapsed;
         this.now = now;
         
-        var ai = this.ai;
-        
-        this.continueMove();
-        return;
+        // this.continueMove();
+        // return;
         
         // Check to see if we should obey our last decision, and not recalc
         if (this.forceCurrentMove && this.forceCurrentMove() && this.currentMoveLimit > now) {
@@ -160,7 +159,7 @@ Thing.subclass('Tank', function(Tank){
     
     this['continueMove'] =
     function continueMove(){
-        if ( !this.currentMove ){ //|| this.ai.path.ready ){
+        if ( !this.currentMove || this.ai.path.ready ){
             var target = this.findNearEnemies(10000).shift();
             if (target)
                 this.calculatePath(target.loc);