stratagus-cvs
[Top][All Lists]
Advanced

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

[Stratagus-CVS] stratagus doc/ChangeLog.html src/action/action_...


From: ludovic pollet
Subject: [Stratagus-CVS] stratagus doc/ChangeLog.html src/action/action_...
Date: Fri, 14 Nov 2003 12:35:54 -0500

CVSROOT:        /cvsroot/stratagus
Module name:    stratagus
Branch:         
Changes by:     ludovic pollet <address@hidden> 03/11/14 12:35:53

Modified files:
        doc            : ChangeLog.html 
        src/action     : action_move.c 
        src/ai         : ai_local.h new_ai.c 
        src/include    : ai.h 
        src/pathfinder : astar.c 

Log message:
        AI tries to move its own units when blocked (from Pollet Ludovic).

Patches:
Index: stratagus/doc/ChangeLog.html
diff -u stratagus/doc/ChangeLog.html:1.578 stratagus/doc/ChangeLog.html:1.579
--- stratagus/doc/ChangeLog.html:1.578  Fri Nov 14 12:01:06 2003
+++ stratagus/doc/ChangeLog.html        Fri Nov 14 12:35:49 2003
@@ -2,7 +2,7 @@
 <html>
 <head>
 <!--
-----   $Id: ChangeLog.html,v 1.578 2003/11/14 17:01:06 pludov Exp $
+----   $Id: ChangeLog.html,v 1.579 2003/11/14 17:35:49 pludov Exp $
 
 ----   (c) Copyright 1998-2003 by Lutz Sammer
 
@@ -36,6 +36,7 @@
 <li>Future 2.00 Release<p>
     <ul>
     <li>++
+    <li>AI tries to move its own units when blocked (from Pollet Ludovic).
     <li>Replay log are now generated for saved/loaded games as well (from 
Pollet Ludovic).
     <li>Fixed Bug #6466: Can't change between Units/Tile with left mouse after 
click menu in editor (from Russell Smith).
     <li>Fixed Bug #6522: Set Harvest/Mining button doesn't work (from Jimmy 
Salmon).
Index: stratagus/src/action/action_move.c
diff -u stratagus/src/action/action_move.c:1.72 
stratagus/src/action/action_move.c:1.73
--- stratagus/src/action/action_move.c:1.72     Mon Nov 10 22:06:52 2003
+++ stratagus/src/action/action_move.c  Fri Nov 14 12:35:50 2003
@@ -21,7 +21,7 @@
 //     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //     GNU General Public License for more details.
 //
-//     $Id: action_move.c,v 1.72 2003/11/11 03:06:52 n0body Exp $
+//     $Id: action_move.c,v 1.73 2003/11/14 17:35:50 pludov Exp $
 
 //@{
 
@@ -44,6 +44,7 @@
 #include "sound.h"
 #include "interface.h"
 #include "map.h"
+#include "ai.h"
 
 /*----------------------------------------------------------------------------
 --     Variables
@@ -102,6 +103,9 @@
            case PF_UNREACHABLE:        // Can't reach, stop
                unit->Reset = unit->Wait = 1;
                unit->Moving = 0;
+               if (unit->Player->AiEnabled) {
+                   AiCanNotMove(unit);
+               }
                return d;
            case PF_REACHED:            // Reached goal, stop
                unit->Reset = unit->Wait = 1;
Index: stratagus/src/ai/ai_local.h
diff -u stratagus/src/ai/ai_local.h:1.42 stratagus/src/ai/ai_local.h:1.43
--- stratagus/src/ai/ai_local.h:1.42    Wed Nov 12 15:35:41 2003
+++ stratagus/src/ai/ai_local.h Fri Nov 14 12:35:51 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai_local.h,v 1.42 2003/11/12 20:35:41 jsalmon3 Exp $
+//      $Id: ai_local.h,v 1.43 2003/11/14 17:35:51 pludov Exp $
 
 #ifndef __AI_LOCAL_H__
 #define __AI_LOCAL_H__
@@ -297,6 +297,7 @@
     AiExplorationRequest*FirstExplorationRequest;/// Requests for exploration
     unsigned int       LastExplorationGameCycle;/// When did the last explore 
occur ?
 
+    unsigned int       LastCanNotMoveGameCycle;
     /// number of elements in UnitTypeRequests
     int                        UnitTypeRequestsCount;
     /// unit-types to build/train requested and priority list
Index: stratagus/src/ai/new_ai.c
diff -u stratagus/src/ai/new_ai.c:1.87 stratagus/src/ai/new_ai.c:1.88
--- stratagus/src/ai/new_ai.c:1.87      Wed Nov 12 15:35:41 2003
+++ stratagus/src/ai/new_ai.c   Fri Nov 14 12:35:51 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: new_ai.c,v 1.87 2003/11/12 20:35:41 jsalmon3 Exp $
+//      $Id: new_ai.c,v 1.88 2003/11/14 17:35:51 pludov Exp $
 
 
 //@{
@@ -149,6 +149,9 @@
 #include "unit.h"
 #include "ccl.h"
 #include "ccl_helpers.h"
+#include "actions.h"
+#include "map.h"
+
 #if defined(DEBUG) && defined(TIMEIT)
 #include "rdtsc.h"
 #endif
@@ -779,7 +782,7 @@
 {
     CLprintf(file, "\n;;; -----------------------------------------\n");
     CLprintf(file,
-       ";;; MODULE: AI $Id: new_ai.c,v 1.87 2003/11/12 20:35:41 jsalmon3 Exp 
$\n\n");
+       ";;; MODULE: AI $Id: new_ai.c,v 1.88 2003/11/14 17:35:51 pludov Exp 
$\n\n");
 
     SaveAiTypesWcName(file);
     SaveAiHelper(file);
@@ -1331,6 +1334,134 @@
     DebugCheck(unit->Player->Type == PlayerPerson);
 
     AiReduceMadeInBuilded(unit->Player->Ai, what);
+}
+
+/**
+**     Called if an unit can't move. Try to move unit in the way
+**
+**     @param unit     Pointer to unit what builds the building.
+**     @param what     Pointer to unit-type.
+*/
+global void AiCanNotMove(Unit * unit)
+{
+    static int 
dirs[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};    
+    int ux0,uy0,ux1,uy1;
+    int bx0,by0,bx1,by1;
+    int x,y;
+    int trycount,i;
+    Unit * blocker;
+    UnitType * unittype;
+    UnitType * blockertype;
+    Unit * movableunits[16];
+    int movablepos[16][2];
+    int movablenb;
+
+    AiPlayer = unit->Player->Ai;
+
+    // No more than 1 move per cycle ( avoid stressing the pathfinder )
+    if (GameCycle == AiPlayer->LastCanNotMoveGameCycle) {
+       return;
+    }
+
+    unittype = unit->Type;
+
+    ux0 = unit->X;
+    uy0 = unit->Y;
+    ux1 = ux0 + unittype->TileWidth - 1;
+    uy1 = uy0 + unittype->TileHeight - 1;
+
+    movablenb = 0;
+
+    DebugLevel2Fn("AiCanNotMove : %s at %d %d\n" _C_ unittype->Ident _C_ ux0 
_C_ uy0);
+
+    // Try to make some unit moves around it
+    for (i = 0; i < NumUnits; ++i) {
+       blocker = Units[i];
+
+       if (UnitUnusable(blocker)) {
+           continue;
+       }
+
+       if (!UnitIdle(blocker)) {
+           continue;
+       }
+
+       if (blocker->Player != unit->Player) {
+           // Not allied
+           if (!(blocker->Player->Allied & (1 << unit->Player->Player))) {
+               continue;
+           }
+       }
+
+       blockertype = blocker->Type;
+
+       if (blockertype->UnitType != unittype->UnitType) {
+           continue;
+       }
+
+       
+       if (!blockertype->_Speed || blockertype->Building) {
+           continue;
+       }
+
+       bx0 = blocker->X;
+       by0 = blocker->Y;
+       bx1 = bx0;
+       by1 = by0;
+
+       // Check for collision
+#define int_min(a,b)  ((a)<(b)?(a):(b))
+#define int_max(a,b)  ((a)>(b)?(a):(b))
+       if (!((ux0 == bx1 + 1 || ux1 == bx0 - 1) && 
+               (int_max(by0, uy0) <= int_min(by1, uy1)))
+           && !((uy0 == by1 + 1 || uy1 == by0 - 1) && 
+               (int_max(bx0, ux0) <= int_min(bx1, ux1)))) {
+          continue;
+       }
+#undef int_min
+#undef int_max
+
+       if (unit == blocker) {
+           continue;
+       }
+
+       // Move blocker in a rand dir
+       i = SyncRand() & 7;
+       trycount = 8;
+       while (trycount > 0) {
+           i = (i + 1) &7;
+           trycount--;
+           
+           x = blocker->X + dirs[i][0];
+           y = blocker->Y + dirs[i][1];
+
+           // Out of the map => no !
+           if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+               continue;
+           }
+           // move to blocker ? => no !
+           if (x == ux0 && y == uy0) {
+               continue;
+           }
+
+           movableunits[movablenb] = blocker;
+           movablepos[movablenb][0] = x;
+           movablepos[movablenb][1] = y;
+
+           movablenb++;
+           trycount = 0;
+       }
+       if (movablenb >= 16) {
+           break;
+       }
+    }
+
+    // Don't move more than 1 unit.
+    if (movablenb) {
+       i = SyncRand() % movablenb;
+       CommandMove(movableunits[i], movablepos[i][0], movablepos[i][1], 
FlushCommands);
+       AiPlayer->LastCanNotMoveGameCycle = GameCycle;
+    }
 }
 
 /**
Index: stratagus/src/include/ai.h
diff -u stratagus/src/include/ai.h:1.30 stratagus/src/include/ai.h:1.31
--- stratagus/src/include/ai.h:1.30     Fri Oct 31 04:14:47 2003
+++ stratagus/src/include/ai.h  Fri Nov 14 12:35:52 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: ai.h,v 1.30 2003/10/31 09:14:47 pludov Exp $
+//     $Id: ai.h,v 1.31 2003/11/14 17:35:52 pludov Exp $
 
 #ifndef __AI_H__
 #define __AI_H__
@@ -77,6 +77,8 @@
 extern void AiCanNotBuild(Unit* unit,const UnitType* what);
     /// Called if AI unit can't reach building place
 extern void AiCanNotReach(Unit* unit,const UnitType* what);
+    /// Called if an AI unit can't move
+extern void AiCanNotMove(Unit * unit);
     /// Called if AI unit has completed training
 extern void AiTrainingComplete(Unit* unit,Unit* what);
     /// Called if AI unit has completed upgrade to
Index: stratagus/src/pathfinder/astar.c
diff -u stratagus/src/pathfinder/astar.c:1.54 
stratagus/src/pathfinder/astar.c:1.55
--- stratagus/src/pathfinder/astar.c:1.54       Tue Nov 11 10:32:22 2003
+++ stratagus/src/pathfinder/astar.c    Fri Nov 14 12:35:52 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: astar.c,v 1.54 2003/11/11 15:32:22 pludov Exp $
+//     $Id: astar.c,v 1.55 2003/11/14 17:35:52 pludov Exp $
 
 //@{
 
@@ -532,6 +532,18 @@
            UnitNumber(unit) _C_
            unit->X _C_ unit->Y _C_ x _C_ y);
 
+    if (abs(gx - unit->X) <= 1 && abs(gy - unit->Y) <= 1 && maxrange == 0) {
+       // Simplest case, move to adj cell
+       if (gx == unit->X && gy == unit->Y) {
+           return PF_REACHED;
+       }
+
+       if (path) {
+           path[0] = XY2Heading[gx - unit->X + 1][gy - unit->Y + 1];
+       }
+       return 1;
+    }
+
     OpenSetSize=0;
     num_in_close=0;
     mask=UnitMovementMask(unit);
@@ -748,7 +760,8 @@
     if( unit->Data.Move.Length <= 0 || 
        ( unit->Goal && (unit->Goal->X != unit->Orders[0].X
            || unit->Goal->Y != unit->Orders[0].Y)) ) {
-        result=NewPath(unit);
+       result=NewPath(unit);
+       
         if( result==PF_UNREACHABLE ) {
            unit->Data.Move.Length=0;
            return result;




reply via email to

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