stratagus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Stratagus-CVS] stratagus/src network/network.c pathfinder/asta...


From: Russell Smith
Subject: [Stratagus-CVS] stratagus/src network/network.c pathfinder/asta...
Date: Fri, 05 Dec 2003 23:32:41 -0500

CVSROOT:        /cvsroot/stratagus
Module name:    stratagus
Branch:         
Changes by:     Russell Smith <address@hidden>  03/12/05 23:32:41

Modified files:
        src/network    : network.c 
        src/pathfinder : astar.c ccl_pathfinder.c pathfinder.c 

Log message:
        Remove FIXME's and Cleanup code

Patches:
Index: stratagus/src/network/network.c
diff -u stratagus/src/network/network.c:1.122 
stratagus/src/network/network.c:1.123
--- stratagus/src/network/network.c:1.122       Sun Nov  2 16:12:54 2003
+++ stratagus/src/network/network.c     Fri Dec  5 23:32:40 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: network.c,v 1.122 2003/11/02 21:12:54 mr-russ Exp $
+//     $Id: network.c,v 1.123 2003/12/06 04:32:40 mr-russ Exp $
 
 //@{
 
@@ -208,9 +208,6 @@
 **     @todo FIXME: continue docu
 */
 
-// FIXME: should split the next into small modules!
-// FIXME: I (Johns) leave this for other people (this means you!)
-
 //----------------------------------------------------------------------------
 //     Includes
 //----------------------------------------------------------------------------
@@ -304,7 +301,6 @@
 **     @param buf      Buffer of outgoing message.
 **     @param len      Buffer length.
 **
-**     @todo FIXME: should support multicast and proxy clients/server.
 */
 global void NetworkBroadcast(const void* buf, int len)
 {
@@ -607,10 +603,6 @@
     NetworkCommandQueue* ncq;
     NetworkExtendedCommand* nec;
 
-    //
-    // FIXME: look if we can ignore this command.
-    //         Duplicate commands can be ignored.
-    //
     ncq = malloc(sizeof(NetworkCommandQueue));
     dl_insert_first(CommandsIn, ncq->List);
 
Index: stratagus/src/pathfinder/astar.c
diff -u stratagus/src/pathfinder/astar.c:1.56 
stratagus/src/pathfinder/astar.c:1.57
--- stratagus/src/pathfinder/astar.c:1.56       Wed Nov 19 21:22:30 2003
+++ stratagus/src/pathfinder/astar.c    Fri Dec  5 23:32:41 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: astar.c,v 1.56 2003/11/20 02:22:30 pludov Exp $
+//     $Id: astar.c,v 1.57 2003/12/06 04:32:41 mr-russ Exp $
 
 //@{
 
@@ -77,9 +77,9 @@
 global const int Heading2Y[9] = { -1,-1, 0,+1,+1,+1, 0,-1, 0 };
 global const int XY2Heading[3][3] = { {7,6,5},{0,0,4},{1,2,3}};
 /// cost matrix
-local Node *AStarMatrix;
+local Node* AStarMatrix;
 /// a list of close nodes, helps to speed up the matrix cleaning
-local int *CloseSet;
+local int* CloseSet;
 local int Threshold;
 local int OpenSetMaxSize;
 local int AStarMatrixSize;
@@ -87,10 +87,10 @@
 #define MAX_OPEN_SET_RATIO 8   // 10,16 to small
 
 /// see pathfinder.h
-global int AStarFixedUnitCrossingCost=MaxMapWidth*MaxMapHeight;
-global int AStarMovingUnitCrossingCost=5;
-global int AStarKnowUnknown=0;
-global int AStarUnknownTerrainCost=2;
+global int AStarFixedUnitCrossingCost = MaxMapWidth * MaxMapHeight;
+global int AStarMovingUnitCrossingCost = 5;
+global int AStarKnowUnknown = 0;
+global int AStarUnknownTerrainCost = 2;
 
 /**
 **     The Open set is handled by a Heap stored in a table
@@ -99,7 +99,7 @@
 */
 
 /// The set of Open nodes
-local Open *OpenSet;
+local Open* OpenSet;
 /// The size of the open node set
 local int OpenSetSize;
 
@@ -108,13 +108,13 @@
 */
 global void InitAStar(void)
 {
-    if( !AStarMatrix ) {
-       AStarMatrixSize=sizeof(Node)*TheMap.Width*TheMap.Height;
-       AStarMatrix=(Node *)calloc(TheMap.Width*TheMap.Height,sizeof(Node));
-       Threshold=TheMap.Width*TheMap.Height/MAX_CLOSE_SET_RATIO;
-       CloseSet=(int *)malloc(sizeof(int)*Threshold);
-       OpenSetMaxSize=TheMap.Width*TheMap.Height/MAX_OPEN_SET_RATIO;
-       OpenSet=(Open *)malloc(sizeof(Open)*OpenSetMaxSize);
+    if (!AStarMatrix) {
+       AStarMatrixSize = sizeof(Node) * TheMap.Width * TheMap.Height;
+       AStarMatrix = (Node*)calloc(TheMap.Width * TheMap.Height, sizeof(Node));
+       Threshold = TheMap.Width * TheMap.Height / MAX_CLOSE_SET_RATIO;
+       CloseSet = (int*)malloc(sizeof(int) * Threshold);
+       OpenSetMaxSize = TheMap.Width * TheMap.Height / MAX_OPEN_SET_RATIO;
+       OpenSet = (Open*)malloc(sizeof(Open) * OpenSetMaxSize);
     }
 }
 
@@ -123,9 +123,9 @@
 */
 global void FreeAStar(void)
 {
-    if( AStarMatrix ) {
+    if (AStarMatrix) {
        free(AStarMatrix);
-       AStarMatrix=NULL;
+       AStarMatrix = NULL;
        free(CloseSet);
        free(OpenSet);
     }
@@ -136,7 +136,7 @@
 */
 local void AStarPrepare(void)
 {
-    memset(AStarMatrix,0,AStarMatrixSize);
+    memset(AStarMatrix, 0, AStarMatrixSize);
 }
 
 /**
@@ -146,12 +146,12 @@
 {
     int i;
 
-    if( num_in_close>=Threshold ) {
+    if (num_in_close >= Threshold) {
        AStarPrepare();
     } else {
-       for( i=0; i<num_in_close; ++i ) {
-         AStarMatrix[CloseSet[i]].CostFromStart=0;
-         AStarMatrix[CloseSet[i]].InGoal=0;
+       for (i = 0; i < num_in_close; ++i) {
+         AStarMatrix[CloseSet[i]].CostFromStart = 0;
+         AStarMatrix[CloseSet[i]].InGoal = 0;
        }
     }
 }
@@ -180,22 +180,22 @@
     int end;
     Open swap;
 
-    if( --OpenSetSize ) {
-       OpenSet[pos]=OpenSet[OpenSetSize];
+    if (--OpenSetSize) {
+       OpenSet[pos] = OpenSet[OpenSetSize];
        // now we exchange the new root with its smallest child until the
        // order is correct
-       i=0;
-       end=(OpenSetSize>>1)-1;
-       while( i<=end ) {
-           j=(i<<1)+1;
-           if( j<OpenSetSize-1 && OpenSet[j].Costs>=OpenSet[j+1].Costs ) {
+       i = 0;
+       end = (OpenSetSize >> 1) - 1;
+       while (i <= end) {
+           j = (i << 1) + 1;
+           if (j < OpenSetSize - 1 && OpenSet[j].Costs >= OpenSet[j + 
1].Costs) {
                ++j;
            }
-           if( OpenSet[i].Costs>OpenSet[j].Costs ) {
-               swap=OpenSet[i];
-               OpenSet[i]=OpenSet[j];
-               OpenSet[j]=swap;
-               i=j;
+           if(OpenSet[i].Costs > OpenSet[j].Costs) {
+               swap = OpenSet[i];
+               OpenSet[i] = OpenSet[j];
+               OpenSet[j] = swap;
+               i = j;
            } else {
                break;
            }
@@ -207,30 +207,30 @@
 **     Add a new node to the open set (and update the heap structure)
 **     Returns Pathfinder failed
 */
-local int AStarAddNode(int x,int y,int o,int costs)
+local int AStarAddNode(int x, int y, int o, int costs)
 {
     int i;
     int j;
     Open swap;
 
-    i=OpenSetSize;
-    if( OpenSetSize>=OpenSetMaxSize ) {
+    i = OpenSetSize;
+    if (OpenSetSize >= OpenSetMaxSize) {
        fprintf(stderr, "A* internal error: raise Open Set Max Size "
-               "(current value %d)\n",OpenSetMaxSize);
+               "(current value %d)\n", OpenSetMaxSize);
        return PF_FAILED;
     }
-    OpenSet[i].X=x;
-    OpenSet[i].Y=y;
-    OpenSet[i].O=o;
-    OpenSet[i].Costs=costs;
-    OpenSetSize++;
-    while( i>0 ) {
-       j=(i-1)>>1;
-       if( OpenSet[i].Costs<OpenSet[j].Costs ) {
-           swap=OpenSet[i];
-           OpenSet[i]=OpenSet[j];
-           OpenSet[j]=swap;
-           i=j;
+    OpenSet[i].X = x;
+    OpenSet[i].Y = y;
+    OpenSet[i].O = o;
+    OpenSet[i].Costs = costs;
+    ++OpenSetSize;
+    while (i > 0) {
+       j = (i - 1) >> 1;
+       if (OpenSet[i].Costs < OpenSet[j].Costs) {
+           swap = OpenSet[i];
+           OpenSet[i] = OpenSet[j];
+           OpenSet[j] = swap;
+           i = j;
        } else {
            break;
        }
@@ -243,22 +243,22 @@
 **     Change the cost associated to an open node. The new cost MUST BE LOWER
 **     than the old one in the current heap based implementation.
 */
-local void AStarReplaceNode(int pos,int costs)
+local void AStarReplaceNode(int pos, int costs)
 {
     int i;
     int j;
     Open swap;
 
-    i=pos;
-    OpenSet[pos].Costs=costs;
+    i = pos;
+    OpenSet[pos].Costs = costs;
     // we need to go up, as the cost can only decrease
-    while( i>0 ) {
-       j=(i-1)>>1;
-       if( OpenSet[i].Costs<OpenSet[j].Costs ) {
-           swap=OpenSet[i];
-           OpenSet[i]=OpenSet[j];
-           OpenSet[j]=swap;
-           i=j;
+    while (i > 0) {
+       j = (i - 1) >> 1;
+       if (OpenSet[i].Costs < OpenSet[j].Costs) {
+           swap = OpenSet[i];
+           OpenSet[i] = OpenSet[j];
+           OpenSet[j] = swap;
+           i = j;
        } else {
            break;
        }
@@ -274,8 +274,8 @@
 {
     int i;
 
-    for( i=0; i<OpenSetSize; ++i ) {
-       if( OpenSet[i].O==eo ) {
+    for (i = 0; i < OpenSetSize; ++i) {
+       if (OpenSet[i].O == eo) {
            return i;
        }
     }
@@ -288,45 +288,45 @@
 **      0 -> no induced cost, except move
 **     >0 -> costly tile
 */
-local int CostMoveTo(Unit* unit, int ex,int ey,int mask,int current_cost) {
+local int CostMoveTo(Unit* unit, int ex, int ey, int mask, int current_cost) {
     int j;
     int cost;
     Unit* goal;
 
-    cost=0;
+    cost = 0;
 
     // Doesn't cost anything to move to ourselves :)
     // Used when marking goals mainly.  Could cause speed problems
-    if( unit->X == ex && unit->Y == ey ) {
+    if (unit->X == ex && unit->Y == ey) {
        return 0;
     }
-    j=TheMap.Fields[ex+ey*TheMap.Width].Flags&mask;
+    j = TheMap.Fields[ex + ey * TheMap.Width].Flags & mask;
     if( j && (AStarKnowUnknown
-           || IsMapFieldExplored(unit->Player,ex,ey)) ) {
-       if( j&~(MapFieldLandUnit|MapFieldAirUnit|MapFieldSeaUnit) ) {
+           || IsMapFieldExplored(unit->Player, ex, ey)) ) {
+       if( j & ~(MapFieldLandUnit | MapFieldAirUnit | MapFieldSeaUnit) ) {
            // we can't cross fixed units and other unpassable things
            return -1;
        }
-       goal=UnitCacheOnXY(ex,ey,unit->Type->UnitType);
-       if( !goal ) {
+       goal = UnitCacheOnXY(ex, ey, unit->Type->UnitType);
+       if (!goal) {
            // Shouldn't happen, mask says there is something on this tile
-           DebugCheck( 1 );
+           DebugCheck(1);
            return -1;
        }
-       if( goal->Moving ) {
+       if (goal->Moving)  {
            // moving unit are crossable
-           cost+=AStarMovingUnitCrossingCost;
+           cost += AStarMovingUnitCrossingCost;
        } else {
            // for non moving unit Always Fail
            // FIXME: Need support for moving a fixed unit to add cost
            return -1;
-           cost+=AStarFixedUnitCrossingCost;
+           cost += AStarFixedUnitCrossingCost;
        }
     }
     // Add cost of crossing unknown tiles if required
-    if( !AStarKnowUnknown && !IsMapFieldExplored(unit->Player,ex,ey) ) {
+    if (!AStarKnowUnknown && !IsMapFieldExplored(unit->Player, ex, ey) ) {
        // Tend against unknown tiles.
-       cost+=AStarUnknownTerrainCost;
+       cost += AStarUnknownTerrainCost;
     }
     return cost;
 }
@@ -349,11 +349,11 @@
     int filler;
     int range;
 
-    goal_reachable=0;
+    goal_reachable = 0;
 
-    if( minrange == 0 && maxrange == 0 && gw == 0 && gh == 0 ) {
-       if( CostMoveTo(unit,gx,gy,mask,AStarFixedUnitCrossingCost)>=0 ) {
-           AStarMatrix[gx+gy*TheMap.Width].InGoal=1;
+    if (minrange == 0 && maxrange == 0 && gw == 0 && gh == 0) {
+       if (CostMoveTo(unit, gx, gy, mask, AStarFixedUnitCrossingCost) >= 0) {
+           AStarMatrix[gx + gy * TheMap.Width].InGoal = 1;
            return 1;
        } else {
            return 0;
@@ -382,44 +382,44 @@
        gh--;
     }
     // Mark top, bottom, left, right
-    for(range=minrange; range <= maxrange; range++) {
+    for (range = minrange; range <= maxrange; ++range) {
        // Mark Top and Bottom of Goal
-       for(x=gx;x<=gx+gw;x++) {
-           if( x >= 0 && x < TheMap.Width) {
-               if( gy-range >= 0 && 
CostMoveTo(unit,x,gy-range,mask,AStarFixedUnitCrossingCost)>=0 ) {
-                   AStarMatrix[(gy-range)*TheMap.Width+x].InGoal=1;
-                   goal_reachable=1;
-                   if( *num_in_close<Threshold ) {
-                       CloseSet[(*num_in_close)++]=(gy-range)*TheMap.Width+x;
+       for (x = gx; x <= gx + gw; ++x) {
+           if (x >= 0 && x < TheMap.Width) {
+               if (gy - range >= 0 && CostMoveTo(unit, x, gy - range, mask, 
AStarFixedUnitCrossingCost) >= 0) {
+                   AStarMatrix[(gy - range) * TheMap.Width + x].InGoal = 1;
+                   goal_reachable = 1;
+                   if (*num_in_close < Threshold) {
+                       CloseSet[(*num_in_close)++] = (gy - range) * 
TheMap.Width + x;
                    }                                               
                }
-               if( gy+range+gh < TheMap.Height && 
-                       
CostMoveTo(unit,x,gy+gh+range,mask,AStarFixedUnitCrossingCost)>=0 ) {
-                   AStarMatrix[(gy+range+gh)*TheMap.Width+x].InGoal=1;
-                   if( *num_in_close<Threshold ) {
-                       
CloseSet[(*num_in_close)++]=(gy+range+gh)*TheMap.Width+x;
+               if (gy + range + gh < TheMap.Height && 
+                       CostMoveTo(unit, x, gy + gh + range, mask, 
AStarFixedUnitCrossingCost) >= 0) {
+                   AStarMatrix[(gy + range + gh) * TheMap.Width + x].InGoal = 
1;
+                   if (*num_in_close < Threshold) {
+                       CloseSet[(*num_in_close)++] = (gy + range + gh) * 
TheMap.Width + x;
                    }
-                   goal_reachable=1;
+                   goal_reachable = 1;
                }
            }
        }
 
-       for(y=gy;y<=gy+gh;y++) {
-           if( y >= 0 && y < TheMap.Height) {
-               if( gx-range >=0 && 
CostMoveTo(unit,gx-range,y,mask,AStarFixedUnitCrossingCost)>=0 ) {
-                   AStarMatrix[y*TheMap.Width+gx-range].InGoal=1;
-                   if( *num_in_close<Threshold ) {
-                       CloseSet[(*num_in_close)++]=y*TheMap.Width+gx-range;
+       for (y = gy; y <= gy + gh; ++y) {
+           if (y >= 0 && y < TheMap.Height) {
+               if (gx - range >= 0 && CostMoveTo(unit, gx - range, y, mask, 
AStarFixedUnitCrossingCost) >= 0) {
+                   AStarMatrix[y * TheMap.Width + gx - range].InGoal = 1;
+                   if (*num_in_close < Threshold) {
+                       CloseSet[(*num_in_close)++] = y * TheMap.Width + gx - 
range;
                    }
-                   goal_reachable=1;
+                   goal_reachable = 1;
                }
-               if( gx+gw+range < TheMap.Width && 
-                       
CostMoveTo(unit,gx+gw+range,y,mask,AStarFixedUnitCrossingCost)>=0 ) {
-                   AStarMatrix[y*TheMap.Width+gx+gw+range].InGoal=1;
-                   if( *num_in_close<Threshold ) {
-                       CloseSet[(*num_in_close)++]=y*TheMap.Width+gx+gw+range;
+               if (gx + gw + range < TheMap.Width && 
+                       CostMoveTo(unit, gx + gw + range, y, mask, 
AStarFixedUnitCrossingCost) >= 0) {
+                   AStarMatrix[y * TheMap.Width + gx + gw + range].InGoal = 1;
+                   if (*num_in_close < Threshold) {
+                       CloseSet[(*num_in_close)++] = y * TheMap.Width + gx + 
gw + range;
                    }
-                   goal_reachable=1;
+                   goal_reachable = 1;
                }
            }
        }
@@ -430,77 +430,77 @@
 
     // Mark Edges of goal
 
-    steps=VisionLookup[minrange];
+    steps = VisionLookup[minrange];
 
-    while( VisionTable[0][steps] <= maxrange ) {
+    while (VisionTable[0][steps] <= maxrange) {
        // 0 - Top right Quadrant
-       cx[0] = gx+gw;
-       cy[0] = gy-VisionTable[0][steps];
+       cx[0] = gx + gw;
+       cy[0] = gy - VisionTable[0][steps];
        // 1 - Top left Quadrant
        cx[1] = gx;
-       cy[1] = gy-VisionTable[0][steps];
+       cy[1] = gy - VisionTable[0][steps];
        // 2 - Bottom Left Quadrant
        cx[2] = gx;
-       cy[2] = gy+VisionTable[0][steps]+gh;
+       cy[2] = gy + VisionTable[0][steps]+gh;
        // 3 - Bottom Right Quadrant
-       cx[3] = gx+gw;
-       cy[3] = gy+VisionTable[0][steps]+gh;
+       cx[3] = gx + gw;
+       cy[3] = gy + VisionTable[0][steps]+gh;
 
-       steps++;  // Move past blank marker
-       while( VisionTable[1][steps] != 0 || VisionTable[2][steps] != 0 ) {
+       ++steps;  // Move past blank marker
+       while (VisionTable[1][steps] != 0 || VisionTable[2][steps] != 0) {
            // Loop through for repeat cycle
-           cycle=0;
-           while( cycle++ < VisionTable[0][steps] ) {
+           cycle = 0;
+           while (cycle++ < VisionTable[0][steps]) {
                // If we travelled on an angle, mark down as well.
-               if( VisionTable[1][steps] == VisionTable[2][steps] ) {
+               if (VisionTable[1][steps] == VisionTable[2][steps]) {
                    // do down
                    quad = 0;
-                   while( quad < 4 ) {
-                       if( quad < 2 ) {
-                           filler=1;
+                   while (quad < 4) {
+                       if (quad < 2) {
+                           filler = 1;
                        } else {
-                           filler=-1;
+                           filler = -1;
                        }
-                       if( cx[quad] >= 0 && cx[quad] < TheMap.Width && 
cy[quad]+filler >= 0 && 
+                       if (cx[quad] >= 0 && cx[quad] < TheMap.Width && 
cy[quad]+filler >= 0 && 
                            cy[quad]+filler < TheMap.Height &&
-                           
CostMoveTo(unit,cx[quad],cy[quad]+filler,mask,AStarFixedUnitCrossingCost)>=0 ) {
-                           eo=(cy[quad]+filler)*TheMap.Width+cx[quad];
-                           AStarMatrix[eo].InGoal=1;
-                           if( *num_in_close<Threshold ) {
-                               CloseSet[(*num_in_close)++]=eo;
+                           CostMoveTo(unit, cx[quad], cy[quad] + filler, mask, 
AStarFixedUnitCrossingCost) >= 0) {
+                           eo = (cy[quad] + filler) * TheMap.Width + cx[quad];
+                           AStarMatrix[eo].InGoal = 1;
+                           if (*num_in_close < Threshold) {
+                               CloseSet[(*num_in_close)++] = eo;
                            }
-                           goal_reachable=1;
+                           goal_reachable = 1;
                        }
-                       quad++;
+                       ++quad;
                    }
                }
                
-               cx[0]+=VisionTable[1][steps];
-               cy[0]+=VisionTable[2][steps];
-               cx[1]-=VisionTable[1][steps];
-               cy[1]+=VisionTable[2][steps];
-               cx[2]-=VisionTable[1][steps];
-               cy[2]-=VisionTable[2][steps];
-               cx[3]+=VisionTable[1][steps];
-               cy[3]-=VisionTable[2][steps];
+               cx[0] += VisionTable[1][steps];
+               cy[0] += VisionTable[2][steps];
+               cx[1] -= VisionTable[1][steps];
+               cy[1] += VisionTable[2][steps];
+               cx[2] -= VisionTable[1][steps];
+               cy[2] -= VisionTable[2][steps];
+               cx[3] += VisionTable[1][steps];
+               cy[3] -= VisionTable[2][steps];
            
                // Mark Actually Goal curve change
                quad = 0;
-               while( quad < 4 ) {
-                   if( cx[quad] >= 0 && cx[quad] < TheMap.Width && cy[quad] >= 
0 &&
+               while (quad < 4) {
+                   if (cx[quad] >= 0 && cx[quad] < TheMap.Width && cy[quad] >= 
0 &&
                        cy[quad] < TheMap.Height &&
-                       
CostMoveTo(unit,cx[quad],cy[quad],mask,AStarFixedUnitCrossingCost)>=0 ) {
-                       eo=cy[quad]*TheMap.Width+cx[quad];
-                       AStarMatrix[eo].InGoal=1;
-                       if( *num_in_close<Threshold ) {
-                           CloseSet[(*num_in_close)++]=eo;
+                       CostMoveTo(unit, cx[quad], cy[quad], mask, 
AStarFixedUnitCrossingCost) >= 0) {
+                       eo = cy[quad] * TheMap.Width + cx[quad];
+                       AStarMatrix[eo].InGoal = 1;
+                       if (*num_in_close < Threshold) {
+                           CloseSet[(*num_in_close)++] = eo;
                        }
-                       goal_reachable=1;
+                       goal_reachable = 1;
                    }
-                   quad++;
+                   ++quad;
                }
            }
-           steps++;
+           ++steps;
        }
     }
     return goal_reachable;
Index: stratagus/src/pathfinder/ccl_pathfinder.c
diff -u stratagus/src/pathfinder/ccl_pathfinder.c:1.26 
stratagus/src/pathfinder/ccl_pathfinder.c:1.27
--- stratagus/src/pathfinder/ccl_pathfinder.c:1.26      Tue Dec  2 19:32:33 2003
+++ stratagus/src/pathfinder/ccl_pathfinder.c   Fri Dec  5 23:32:41 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: ccl_pathfinder.c,v 1.26 2003/12/03 00:32:33 jsalmon3 Exp $
+//     $Id: ccl_pathfinder.c,v 1.27 2003/12/06 04:32:41 mr-russ Exp $
 
 //@{
 
@@ -63,42 +63,42 @@
     SCM value;
     int i;
 
-    while( !gh_null_p(list) ) {
-       value=gh_car(list);
-       list=gh_cdr(list);
-       if( gh_eq_p(value,gh_symbol2scm("fixed-unit-cost")) ) {
-           i=gh_scm2int(gh_car(list));
-           list=gh_cdr(list);
-           if( i <=3 ) {
+    while (!gh_null_p(list)) {
+       value = gh_car(list);
+       list = gh_cdr(list);
+       if (gh_eq_p(value, gh_symbol2scm("fixed-unit-cost"))) {
+           i = gh_scm2int(gh_car(list));
+           list = gh_cdr(list);
+           if (i <= 3) {
                PrintFunction();
                fprintf(stdout,"Fixed unit crossing cost must be strictly > 
3\n");
            } else {
-               AStarFixedUnitCrossingCost=i;
+               AStarFixedUnitCrossingCost = i;
            }
-       } else if( gh_eq_p(value,gh_symbol2scm("moving-unit-cost")) ) {
-           i=gh_scm2int(gh_car(list));
-           list=gh_cdr(list);
-           if( i<=3) {
+       } else if (gh_eq_p(value, gh_symbol2scm("moving-unit-cost"))) {
+           i = gh_scm2int(gh_car(list));
+           list = gh_cdr(list);
+           if (i <= 3) {
                PrintFunction();
                fprintf(stdout,"Moving unit crossing cost must be strictly > 
3\n");
            } else {
-               AStarMovingUnitCrossingCost=i;
+               AStarMovingUnitCrossingCost = i;
            }
-       } else if( gh_eq_p(value,gh_symbol2scm("know-unseen-terrain")) ) {
-           AStarKnowUnknown=1;
-       } else if( gh_eq_p(value,gh_symbol2scm("dont-know-unseen-terrain")) ) {
-           AStarKnowUnknown=0;
-       } else if( gh_eq_p(value,gh_symbol2scm("unseen-terrain-cost")) ) {
-           i=gh_scm2int(gh_car(list));
-           if( i < 0 ) {
+       } else if (gh_eq_p(value, gh_symbol2scm("know-unseen-terrain"))) {
+           AStarKnowUnknown = 1;
+       } else if (gh_eq_p(value, gh_symbol2scm("dont-know-unseen-terrain"))) {
+           AStarKnowUnknown = 0;
+       } else if (gh_eq_p(value, gh_symbol2scm("unseen-terrain-cost"))) {
+           i = gh_scm2int(gh_car(list));
+           if (i < 0) {
                PrintFunction();
                fprintf(stdout,"Unseen Terrain Cost must be non-negative\n");
            } else {
-               AStarUnknownTerrainCost=i;
+               AStarUnknownTerrainCost = i;
            }
-           list=gh_cdr(list);
+           list = gh_cdr(list);
        } else {
-           errl("Unsupported tag",value);
+           errl("Unsupported tag", value);
        }
     }
 
@@ -244,16 +244,16 @@
 global void PathfinderCclRegister(void)
 {
 #if defined(USE_GUILE) || defined(USE_SIOD)
-    gh_new_procedureN("a-star",CclAStar);
+    gh_new_procedureN("a-star", CclAStar);
 #ifdef MAP_REGIONS
-    gh_new_procedure0_0("debug-regions",CclDebugRegions);
+    gh_new_procedure0_0("debug-regions", CclDebugRegions);
 #endif // MAP_REGIONS
     gh_new_procedure1_0 ("pf-show-regids!", CclPfHierShowRegIds);
     gh_new_procedure1_0 ("pf-show-groupids!", CclPfHierShowGroupIds);
 #elif defined(USE_LUA)
-    lua_register(Lua, "AStar",CclAStar);
+    lua_register(Lua, "AStar", CclAStar);
 #ifdef MAP_REGIONS
-    lua_register(Lua, "DebugRegions",CclDebugRegions);
+    lua_register(Lua, "DebugRegions", CclDebugRegions);
 #endif // MAP_REGIONS
     lua_register(Lua, "PfShowRegids", CclPfHierShowRegIds);
     lua_register(Lua, "PfShowGroupids", CclPfHierShowGroupIds);
Index: stratagus/src/pathfinder/pathfinder.c
diff -u stratagus/src/pathfinder/pathfinder.c:1.60 
stratagus/src/pathfinder/pathfinder.c:1.61
--- stratagus/src/pathfinder/pathfinder.c:1.60  Wed Nov 19 21:22:30 2003
+++ stratagus/src/pathfinder/pathfinder.c       Fri Dec  5 23:32:41 2003
@@ -28,7 +28,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: pathfinder.c,v 1.60 2003/11/20 02:22:30 pludov Exp $
+//     $Id: pathfinder.c,v 1.61 2003/12/06 04:32:41 mr-russ Exp $
 
 //@{
 
@@ -101,24 +101,24 @@
     unsigned h;
     unsigned e;
 
-    w=TheMap.Width+2;
-    h=TheMap.Height;
+    w = TheMap.Width + 2;
+    h = TheMap.Height;
 
-    i=w+w+1;
-    memset(matrix,98,i);                // +1 for ships!
-    memset(matrix+i,0,w*h);             // initialize matrix
-
-    for( e=i+w*h; i<e; ) {              // mark left and right border
-       matrix[i]=98;
-       i+=w;
-       matrix[i-1]=98;
+    i = w + w + 1;
+    memset(matrix, 98, i);                // +1 for ships!
+    memset(matrix + i, 0, w * h);             // initialize matrix
+
+    for (e = i + w * h; i < e;) {              // mark left and right border
+       matrix[i] = 98;
+       i += w;
+       matrix[i - 1] = 98;
     }
-    memset(matrix+i,98,w+1);            // +1 for ships!
+    memset(matrix + i, 98, w + 1);            // +1 for ships!
 }
                                                                        
 local void InitLocalMatrix(void)
 {
-    memset(LocalMatrix,0,TheMap.Width*TheMap.Height*sizeof(int));              
// initialize matrix
+    memset(LocalMatrix, 0, TheMap.Width * TheMap.Height * sizeof(int));        
        // initialize matrix
 }
 
 /**
@@ -137,7 +137,7 @@
 {
     unsigned char* matrix;
 
-    matrix=malloc((TheMap.Width+2)*(TheMap.Height+3)+2);
+    matrix = malloc((TheMap.Width + 2) * (TheMap.Height + 3) + 2);
     InitMatrix(matrix);
 
     return matrix;
@@ -155,7 +155,7 @@
 **
 **     @returns        depth, -1 unreachable
 */
-local int CheckPlaceInMatrix(int gx,int gy,int gw,int gh,int range,unsigned 
int* matrix)
+local int CheckPlaceInMatrix(int gx, int gy, int gw, int gh, int range, 
unsigned int* matrix)
 {
     int cx[4]; 
     int cy[4];
@@ -166,30 +166,30 @@
     int quad;
     int filler;
 
-    if( range == 0 && gw == 0 && gh == 0 ) {
-       return matrix[gx+gy*TheMap.Width];
+    if (range == 0 && gw == 0 && gh == 0) {
+       return matrix[gx + gy * TheMap.Width];
     }
 
     // Mark top, bottom, left, right
 
     // Mark Top and Bottom of Goal
-    for(x=gx;x<=gx+gw;x++) {
-       if( x >= 0 && x < TheMap.Width) {
-           if( gy-range >= 0 && matrix[(gy-range)*TheMap.Width+x] ) {
+    for (x = gx;x <= gx + gw;++x) {
+       if (x >= 0 && x < TheMap.Width) {
+           if ( gy-range >= 0 && matrix[(gy - range) * TheMap.Width + x]) {
                return 1;
            }
-           if( gy+range+gh < TheMap.Height && 
matrix[(gy+range+gh)*TheMap.Width+x] ) {
+           if (gy + range + gh < TheMap.Height && matrix[(gy + range + gh) * 
TheMap.Width + x]) {
                return 1;
            }
        }
     }
 
-    for(y=gy;y<=gy+gh;y++) {
-       if( y >= 0 && y < TheMap.Height) {
-           if( gx-range >=0 && matrix[y*TheMap.Width+gx-range] ) {
+    for (y = gy;y <= gy + gh;++y) {
+       if (y >= 0 && y < TheMap.Height) {
+           if (gx - range >= 0 && matrix[y * TheMap.Width + gx - range]) {
                return 1;
            }
-           if( gx+gw+range < TheMap.Width && 
matrix[y*TheMap.Width+gx+gw+range] ) {
+           if (gx + gw + range < TheMap.Width && matrix[y * TheMap.Width + gx 
+ gw + range]) {
                return 1;
            }
        }
@@ -199,68 +199,68 @@
 
     // Mark Edges of goal
    
-    steps=0;
+    steps = 0;
     // Find place to start. (for marking curves)
     while(VisionTable[0][steps] != range && VisionTable[1][steps] == 0 && 
VisionTable[2][steps] == 0) {
        steps++;
     }
     // 0 - Top right Quadrant
-    cx[0] = gx+gw;
-    cy[0] = gy-VisionTable[0][steps];
+    cx[0] = gx + gw;
+    cy[0] = gy - VisionTable[0][steps];
     // 1 - Top left Quadrant
     cx[1] = gx;
-    cy[1] = gy-VisionTable[0][steps];
+    cy[1] = gy - VisionTable[0][steps];
     // 2 - Bottom Left Quadrant
     cx[2] = gx;
-    cy[2] = gy+VisionTable[0][steps]+gh;
+    cy[2] = gy + VisionTable[0][steps]+gh;
     // 3 - Bottom Right Quadrant
-    cx[3] = gx+gw;
-    cy[3] = gy+VisionTable[0][steps]+gh;
+    cx[3] = gx + gw;
+    cy[3] = gy + VisionTable[0][steps]+gh;
     
-    steps++;  // Move past blank marker
-    while(VisionTable[1][steps] != 0 || VisionTable[2][steps] != 0 ) {
+    ++steps;  // Move past blank marker
+    while (VisionTable[1][steps] != 0 || VisionTable[2][steps] != 0) {
        // Loop through for repeat cycle
-       cycle=0;
-       while( cycle++ < VisionTable[0][steps] ) {
+       cycle = 0;
+       while (cycle++ < VisionTable[0][steps]) {
            // If we travelled on an angle, mark down as well.
-           if( VisionTable[1][steps] == VisionTable[2][steps] ) {
+           if (VisionTable[1][steps] == VisionTable[2][steps]) {
                // do down
                quad = 0;
-               while( quad < 4 ) {
-                   if( quad < 2 ) {
-                       filler=1;
+               while (quad < 4) {
+                   if (quad < 2) {
+                       filler = 1;
                    } else {
-                       filler=-1;
+                       filler = -1;
                    }
-                   if( cx[quad] >= 0 && cx[quad] < TheMap.Width && 
cy[quad]+filler >= 0 && 
-                       cy[quad]+filler < TheMap.Height && 
matrix[(cy[quad]+filler)*TheMap.Width+cx[quad]] ) {
+                   if (cx[quad] >= 0 && cx[quad] < TheMap.Width && cy[quad] + 
filler >= 0 && 
+                       cy[quad] + filler < TheMap.Height && matrix[(cy[quad] + 
filler) * TheMap.Width + cx[quad]]) {
                        return 1;
                    }
-                   quad++;
+                   ++quad;
                }
            }
                
-           cx[0]+=VisionTable[1][steps];
-           cy[0]+=VisionTable[2][steps];
-           cx[1]-=VisionTable[1][steps];
-           cy[1]+=VisionTable[2][steps];
-           cx[2]-=VisionTable[1][steps];
-           cy[2]-=VisionTable[2][steps];
-           cx[3]+=VisionTable[1][steps];
-           cy[3]-=VisionTable[2][steps];
+           cx[0] += VisionTable[1][steps];
+           cy[0] += VisionTable[2][steps];
+           cx[1] -= VisionTable[1][steps];
+           cy[1] += VisionTable[2][steps];
+           cx[2] -= VisionTable[1][steps];
+           cy[2] -= VisionTable[2][steps];
+           cx[3] += VisionTable[1][steps];
+           cy[3] -= VisionTable[2][steps];
            
            // Mark Actually Goal curve change
            quad = 0;
-           while( quad < 4 ) {
-               if( cx[quad] >= 0 && cx[quad] < TheMap.Width && cy[quad] >= 0 &&
+           while (quad < 4) {
+               if (cx[quad] >= 0 && cx[quad] < TheMap.Width && cy[quad] >= 0 &&
                    cy[quad] < TheMap.Height &&
-                   matrix[cy[quad]*TheMap.Width+cx[quad]] ) {
+                   matrix[cy[quad] * TheMap.Width + cx[quad]] ) {
                    return 1;
                }
-               quad++;
+               ++quad;
            }
        }
-       steps++;
+       ++steps;
     }
     return 0;
 }
@@ -275,7 +275,7 @@
 **     @param matrix   Matrix for calculation.
 **
 */
-local void FillMatrix(Unit* unit,unsigned int* matrix)
+local void FillMatrix(Unit* unit, unsigned int* matrix)
 {
     struct {
        unsigned short X;
@@ -296,67 +296,67 @@
     int size;
     unsigned int* m;
 
-    size=4*(TheMap.Width+TheMap.Height)*sizeof(*points);
-    points=malloc(size);
-    size=4*(TheMap.Width+TheMap.Height);
+    size = 4 * (TheMap.Width + TheMap.Height) * sizeof(*points);
+    points = malloc(size);
+    size = 4 * (TheMap.Width + TheMap.Height);
 
-    mask=UnitMovementMask(unit);
+    mask = UnitMovementMask(unit);
     // Ignore all possible mobile units.
     // FIXME: bad? mask&=~(MapFieldLandUnit|MapFieldAirUnit|MapFieldSeaUnit);
 
-    points[0].X=x=unit->X;
-    points[0].Y=y=unit->Y;
-    points[0].depth=1;
-    rp=0;
-    matrix[x+y*TheMap.Width]=depth=1;                  // mark start point
-    ep=wp=1;                                   // start with one point
-    n=2;
+    points[0].X = x = unit->X;
+    points[0].Y = y = unit->Y;
+    points[0].depth = 1;
+    rp = 0;
+    matrix[x + y * TheMap.Width] = depth = 1;          // mark start point
+    ep = wp = 1;                                       // start with one point
+    n = 2;
 
     //
     // Pop a point from stack, push all neightbors which could be entered.
     //
-    for( ;; ) {
-       while( rp!=ep ) {
-           rx=points[rp].X;
-           ry=points[rp].Y;
-           depth=points[rp].depth;
-           for( j=0; j<8; ++j ) {              // mark all neighbors
-               x=rx+Heading2X[j];
-               y=ry+Heading2Y[j];
-               if( x < 0 || y<0 || x >= TheMap.Width || y >= TheMap.Height) {  
// already checked
-                   // We Have been here before
+    for (;;) {
+       while (rp != ep) {
+           rx = points[rp].X;
+           ry = points[rp].Y;
+           depth = points[rp].depth;
+           for (j = 0; j < 8; ++j) {           // mark all neighbors
+               x = rx + Heading2X[j];
+               y = ry + Heading2Y[j];
+               if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+                   // Outside the map
                    continue;
                }
-               m=matrix+x+y*TheMap.Width;
-               if( *m ) {
+               m = matrix + x + y * TheMap.Width;
+               if (*m) {
                    continue;
                }
-               if( CanMoveToMask(x,y,mask) ) { // reachable
-                   *m=depth+1;
-                   points[wp].X=x;             // push the point
-                   points[wp].Y=y;
-                   points[wp].depth=depth+1;
-                   if( ++wp>=size ) {          // round about
-                       wp=0;
+               if (CanMoveToMask(x, y, mask)) {        // reachable
+                   *m = depth + 1;
+                   points[wp].X = x;           // push the point
+                   points[wp].Y = y;
+                   points[wp].depth = depth + 1;
+                   if (++wp >= size) {         // round about
+                       wp = 0;
                    }
                } else {                        // unreachable
-                   *m=0;
+                   *m = 0;
                }
            }
 
            // Loop for 
-           if( ++rp>=size ) {                  // round about
-               rp=0;
+           if (++rp >= size) {                 // round about
+               rp = 0;
            }
        }
 
        //
        //      Continue with next frame.
        //
-       if( rp==wp ) {                  // unreachable, no more points available
+       if (rp == wp) {                 // unreachable, no more points available
            break;
        }
-       ep=wp;
+       ep = wp;
     }
 
     free(points);
@@ -381,7 +381,7 @@
 **
 **     @return         Distance to place.
 */
-global int PlaceReachable(Unit* src,int x,int y,int w,int h,int minrange 
__attribute__((unused)),int range)
+global int PlaceReachable(Unit* src, int x, int y, int w, int h, int minrange 
__attribute__((unused)), int range)
 {
     int depth;
     static unsigned long LastGameCycle;
@@ -392,10 +392,10 @@
     //
     //  Setup movement.
     //
-    if( src->Type->MovementMask != mask || LastGameCycle != GameCycle
-       || LocalMatrix[src->X+src->Y*TheMap.Width] == 0 ) {
+    if (src->Type->MovementMask != mask || LastGameCycle != GameCycle
+       || LocalMatrix[src->X + src->Y * TheMap.Width] == 0) {
        InitLocalMatrix();
-       FillMatrix(src,LocalMatrix);
+       FillMatrix(src, LocalMatrix);
        LastGameCycle = GameCycle;
        mask = src->Type->MovementMask;
     }
@@ -403,7 +403,7 @@
     //
     //  Find a path to the place.
     //
-    if( (depth=CheckPlaceInMatrix(x,y,w,h,range,LocalMatrix)) < 0 ) {
+    if ((depth=CheckPlaceInMatrix(x, y, w, h, range, LocalMatrix)) < 0) {
        DebugLevel1("Can't move to destination, not route to goal\n");
        return 0;
     }
@@ -422,7 +422,7 @@
 **
 **     @return         Distance to place.
 */
-global int UnitReachable(Unit* src,Unit* dst,int range)
+global int UnitReachable(Unit* src, Unit* dst, int range)
 {
     int depth;
 
@@ -433,8 +433,8 @@
     //
     // Find a path to the goal.
     //
-    
depth=PlaceReachable(src,dst->X,dst->Y,dst->Type->TileWidth,dst->Type->TileHeight,0,range);
-    if( depth <= 0 ) {
+    depth=PlaceReachable(src, dst->X, dst->Y, dst->Type->TileWidth, 
dst->Type->TileHeight, 0, range);
+    if (depth <= 0) {
        DebugLevel3("NO WAY\n");
        return 0;
     }
@@ -471,41 +471,41 @@
     int maxrange;
     char* path;
 
-    if( unit->Orders[0].Goal ) {
-       gw=unit->Orders[0].Goal->Type->TileWidth;
-       gh=unit->Orders[0].Goal->Type->TileHeight;
-       gx=unit->Orders[0].Goal->X;
-       gy=unit->Orders[0].Goal->Y;
-       maxrange=unit->Orders[0].Range;
-       minrange=unit->Orders[0].MinRange;
+    if (unit->Orders[0].Goal) {
+       gw = unit->Orders[0].Goal->Type->TileWidth;
+       gh = unit->Orders[0].Goal->Type->TileHeight;
+       gx = unit->Orders[0].Goal->X;
+       gy = unit->Orders[0].Goal->Y;
+       maxrange = unit->Orders[0].Range;
+       minrange = unit->Orders[0].MinRange;
     } else {
        // Take care of non square goals :)
        // If goal is non square, range states a non-existant goal rather
        // than a tile.
        gw = unit->Orders[0].Width;
        gh = unit->Orders[0].Height;
-       maxrange=unit->Orders[0].Range;
-       minrange=unit->Orders[0].MinRange;
-       gx=unit->Orders[0].X;
-       gy=unit->Orders[0].Y;
+       maxrange = unit->Orders[0].Range;
+       minrange = unit->Orders[0].MinRange;
+       gx = unit->Orders[0].X;
+       gy = unit->Orders[0].Y;
     }
     path = unit->Data.Move.Path;
-    i=AStarFindPath(unit,gx,gy,gw,gh,minrange,maxrange,path);
-    if( i == PF_FAILED ) {
+    i = AStarFindPath(unit,gx,gy,gw,gh,minrange,maxrange,path);
+    if (i == PF_FAILED) {
        i = PF_UNREACHABLE;
     }
 
     // Update path if it was requested. Otherwise we may only want
     // to know if there exists a path.
 
-    if( path != NULL ) {
-       if( i >= MAX_PATH_LENGTH ) {
-           unit->Data.Move.Length=MAX_PATH_LENGTH;
+    if (path != NULL) {
+       if (i >= MAX_PATH_LENGTH) {
+           unit->Data.Move.Length = MAX_PATH_LENGTH;
        } else {
-           unit->Data.Move.Length=i;
+           unit->Data.Move.Length = i;
        }
-       if( unit->Data.Move.Length == 0) {
-           unit->Data.Move.Length++;
+       if (unit->Data.Move.Length == 0) {
+           ++unit->Data.Move.Length;
        }
     }
     return i;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]