stratagus-cvs
[Top][All Lists]
Advanced

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

[Stratagus-CVS] stratagus/src/ai ai_local.h ai_plan.c ai_resour...


From: ludovic p
Subject: [Stratagus-CVS] stratagus/src/ai ai_local.h ai_plan.c ai_resour...
Date: Wed, 29 Oct 2003 18:11:56 -0500

CVSROOT:        /cvsroot/stratagus
Module name:    stratagus
Branch:         
Changes by:     ludovic p <address@hidden>      03/10/29 18:11:55

Modified files:
        src/ai         : ai_local.h ai_plan.c ai_resource.c ccl_ai.c 
                         new_ai.c 

Log message:
        AI will now try to explore the map if it can't find a ressource
        ( fix bug #6216 )

Patches:
Index: stratagus/src/ai/ai_local.h
diff -u stratagus/src/ai/ai_local.h:1.38 stratagus/src/ai/ai_local.h:1.39
--- stratagus/src/ai/ai_local.h:1.38    Sun Oct 26 10:34:58 2003
+++ stratagus/src/ai/ai_local.h Wed Oct 29 18:11:53 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai_local.h,v 1.38 2003/10/26 15:34:58 pludov Exp $
+//      $Id: ai_local.h,v 1.39 2003/10/29 23:11:53 pludov Exp $
 
 #ifndef __AI_LOCAL_H__
 #define __AI_LOCAL_H__
@@ -239,6 +239,15 @@
     AiActionEvaluation*        Next;           /// Next in linked list
 };
 
+typedef struct _ai_exploration_request_ AiExplorationRequest;
+
+struct _ai_exploration_request_ {
+    int                        X;              /// x pos on map
+    int                        Y;              /// y pos on map
+    int                        Mask;           /// mask ( ex: MapFieldLandUnit 
)
+    AiExplorationRequest*Next;         /// Next in linked list
+};
+
 /**
 **     AI variables.
 */
@@ -276,6 +285,9 @@
 
     int                        NeedFood;       /// Flag need food
 
+    AiExplorationRequest*FirstExplorationRequest;/// Requests for exploration
+    unsigned int       LastExplorationGameCycle;/// When did the last explore 
occur ?
+
     /// number of elements in UnitTypeRequests
     int                        UnitTypeRequestsCount;
     /// unit-types to build/train requested and priority list
@@ -380,7 +392,9 @@
 extern void AiAddResearchRequest(Upgrade * upgrade);
     /// Periodic called resource manager handler
 extern void AiResourceManager(void);
-    /// Count the number of builder unit available for the given unittype
+    /// Ask the ai to explore around x,y
+extern void AiExplore(int x, int y, int exploreMask);
+    /// Count the number of builder unit available for the given unittype 
 extern int AiCountUnitBuilders(UnitType * type);
 
 //
@@ -436,6 +450,8 @@
 extern int AiFindWall(AiForce * force);
     /// Plan the an attack
 extern int AiPlanAttack(AiForce * force);
+    /// Send explorers around the map
+extern void AiSendExplorers(void);
 
 //
 //      Scripts
Index: stratagus/src/ai/ai_plan.c
diff -u stratagus/src/ai/ai_plan.c:1.19 stratagus/src/ai/ai_plan.c:1.20
--- stratagus/src/ai/ai_plan.c:1.19     Sun Oct 26 10:34:58 2003
+++ stratagus/src/ai/ai_plan.c  Wed Oct 29 18:11:54 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai_plan.c,v 1.19 2003/10/26 15:34:58 pludov Exp $
+//      $Id: ai_plan.c,v 1.20 2003/10/29 23:11:54 pludov Exp $
 
 //@{
 
@@ -595,6 +595,141 @@
        return 1;
     }
     return 0;
+}
+
+/**
+**     Respond to ExplorationRequests
+*/
+void AiSendExplorers(void)
+{
+    AiExplorationRequest* request;
+    int requestcount;
+    int requestid;
+    int centerx,centery;
+    int x,y;
+    int i;
+    int targetok;
+    int ray;
+    int trycount;
+    int outtrycount;
+    
+    Unit** unit;
+    UnitType * type;
+    Unit* bestunit;
+    int distance;
+    int bestdistance;
+    int flyeronly;
+    
+    // Count requests...
+    request = AiPlayer->FirstExplorationRequest;
+    requestcount = 0;
+
+    while (request) {
+       requestcount ++;
+       
+       request = request->Next;
+    }
+
+    // Nothing => abort
+    if (! requestcount) {
+       return;
+    }
+
+    outtrycount = 0;
+    do {
+       bestunit = 0;
+       outtrycount++;
+
+       // Choose a request
+       requestid = SyncRand() % requestcount;
+
+       request = AiPlayer->FirstExplorationRequest;
+       while (requestid) {
+           request = request->Next;
+           requestid--;
+       }
+       // Choose a target, "near"
+       centerx = request->X;
+       centery = request->Y;
+       ray = 3;
+       trycount = 0;
+
+       do {
+           targetok = 0;
+
+           x = centerx + SyncRand() % (2 * ray + 1) - ray;
+           y = centery + SyncRand() % (2 * ray + 1) - ray;
+
+           if (x >= 0 && y >= 0 && x < TheMap.Width && y < TheMap.Height) {
+               targetok = !IsMapFieldExplored(AiPlayer->Player, x, y);
+           }
+
+           ray = 3 * ray / 2;
+           trycount ++;
+       } while (trycount < 8 && ! targetok);
+
+       if (! targetok) {
+           continue;
+       }
+
+       // We have an unexplored tile in sight (x,y)
+
+       // Find an idle unit, responding to the mask
+       flyeronly = 0;
+       bestdistance = -1;
+
+       unit = AiPlayer->Player->Units;
+       for (i = AiPlayer->Player->TotalNumUnits; i > 0; unit++) {
+           i--;
+
+           if (!UnitIdle((*unit))) {
+               continue;
+           }
+           
+           if ((*unit)->X == -1 || (*unit)->Y == -1) {
+               continue;
+           }
+
+           type = (*unit)->Type;
+
+           if (type->Building) {
+               continue;
+           }
+           
+           if (type->UnitType != UnitTypeFly) {
+               if (flyeronly) {
+                   continue;
+               }
+               if ((request->Mask & MapFieldLandUnit) && (type->UnitType != 
UnitTypeLand)) {
+                   continue;
+               }
+               if ((request->Mask & MapFieldSeaUnit) && (type->UnitType != 
UnitTypeNaval)) {
+                   continue;
+               }
+           } else {
+               flyeronly = 1;
+           }
+
+           distance = ((*unit)->X - x) * ((*unit)->X - x) + ((*unit)->Y - y) * 
((*unit)->Y - y);
+           if (bestdistance == -1 || distance <= bestdistance || 
+                   (bestunit->Type->UnitType != UnitTypeFly && type->UnitType 
== UnitTypeFly)) {
+               bestdistance = distance;
+               bestunit = (*unit);
+           }
+       }
+    } while(outtrycount <= 4 && !bestunit);
+    
+    if (bestunit) {
+       CommandMove(bestunit, x, y, FlushCommands);
+       AiPlayer->LastExplorationGameCycle=GameCycle;
+    }
+
+    // Remove all requests
+    while (AiPlayer->FirstExplorationRequest) {
+       request = AiPlayer->FirstExplorationRequest->Next;
+       free(AiPlayer->FirstExplorationRequest);
+       AiPlayer->FirstExplorationRequest = request;
+    }
 }
 
 //@}
Index: stratagus/src/ai/ai_resource.c
diff -u stratagus/src/ai/ai_resource.c:1.74 stratagus/src/ai/ai_resource.c:1.75
--- stratagus/src/ai/ai_resource.c:1.74 Tue Oct 28 17:41:12 2003
+++ stratagus/src/ai/ai_resource.c      Wed Oct 29 18:11:54 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai_resource.c,v 1.74 2003/10/28 22:41:12 n0body Exp $
+//      $Id: ai_resource.c,v 1.75 2003/10/29 23:11:54 pludov Exp $
 
 //@{
 
@@ -797,6 +797,8 @@
     //  These will hold the coordinates of the forest.
     int forestx;
     int foresty;
+    int i;
+    int exploremask;
     //  This will hold the resulting gather destination.
     Unit *dest;
 
@@ -811,15 +813,38 @@
            CommandResourceLoc(unit, forestx, foresty, FlushCommands);
            return 1;
        }
+       // Ask the AI to explore...
+       AiExplore(unit->X, unit->Y, MapFieldLandUnit);
     } else {
        //
-       //      Find a resource to ravest from.
+       //      Find a resource to harvest from.
        //
        if ((dest = FindResource(unit, unit->X, unit->Y, 1000, resource))) {
            CommandResource(unit, dest, FlushCommands);
            return 1;
        }
+       exploremask = 0;
+       for (i = 0; i <= UnitTypeMax; i++) {        
+           if (UnitTypes[i] && UnitTypes[i]->GivesResource == resource) {
+               switch (UnitTypes[i]->UnitType) {
+               case UnitTypeLand:
+                   exploremask |= MapFieldLandUnit;
+                   break;
+               case UnitTypeFly:
+                   exploremask |= MapFieldAirUnit;
+                   break;
+               case UnitTypeNaval:
+                   exploremask |= MapFieldSeaUnit;
+                   break;
+               default:
+                   DebugCheck(1);
+               }
+           }
+       }
+       // Ask the AI to explore
+       AiExplore(unit->X, unit->Y, exploremask);
     }
+    
     //  Failed.
     return 0;
 }
@@ -1266,6 +1291,29 @@
     (*queue)->Type = type;
     (*queue)->Want = count;
     (*queue)->Made = 0;
+}
+
+/**
+**     Mark that a zone is requiring exploration.
+**
+**     @param x        X pos of the zone
+**     @param y        Y pos of the zone
+**     @param mask     Mask to explore ( land/sea/air )
+*/
+global void AiExplore(int x, int y, int mask)
+{
+    AiExplorationRequest * req;
+
+    // Alloc a new struct,
+    req = (AiExplorationRequest*) malloc(sizeof(AiExplorationRequest));
+
+    // Link into the exploration requests list
+    req->X = x;
+    req->Y = y;
+    req->Mask = mask;
+
+    req->Next = AiPlayer->FirstExplorationRequest;
+    AiPlayer->FirstExplorationRequest = req;
 }
 
 /**
Index: stratagus/src/ai/ccl_ai.c
diff -u stratagus/src/ai/ccl_ai.c:1.73 stratagus/src/ai/ccl_ai.c:1.74
--- stratagus/src/ai/ccl_ai.c:1.73      Tue Oct 28 17:41:13 2003
+++ stratagus/src/ai/ccl_ai.c   Wed Oct 29 18:11:55 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ccl_ai.c,v 1.73 2003/10/28 22:41:13 n0body Exp $
+//      $Id: ccl_ai.c,v 1.74 2003/10/29 23:11:55 pludov Exp $
 
 //@{
 
@@ -81,6 +81,19 @@
     }
 };
 
+static IOStructDef AiExplorationRequestStructDef = {
+    "AiExplorationRequest",
+    sizeof (AiExplorationRequest),
+    -1,
+    {
+       {"`next",               NULL,           &((AiExplorationRequest *) 
0)->Next,    NULL},  
+       {"gamecycle",           &IOInt,         &((AiExplorationRequest *) 
0)->Mask,    NULL},
+       {"map-x",               &IOInt,         &((AiExplorationRequest *) 
0)->X,       NULL},
+       {"map-y",               &IOInt,         &((AiExplorationRequest *) 
0)->Y,       NULL},
+       {0, 0, 0, 0}
+    }
+};
+
 /// Description of the AiRunningScript structure
 static IOStructDef AiRunningScriptStructDef = {
     "AiRunningScript",
@@ -248,6 +261,8 @@
        {"collect",             &IORessourceArray,&((PlayerAi *) 0)->Collect,   
        0},
        {"neededmask",          &IORessourceMask,&((PlayerAi *) 0)->Reserve,    
        0},
        {"need-food",           &IOBool,        &((PlayerAi *) 0)->NeedFood,    
        0},
+       {"exploration-requests",&IOLinkedList,  &((PlayerAi *) 
0)->FirstExplorationRequest,&AiExplorationRequestStructDef},
+       {"last-exploration",    &IOInt,         &((PlayerAi *) 
0)->LastExplorationGameCycle,0},
        {"unit-type-requests",  &IOTable,       0,                              
        &UnitTypeRequestsTableDef},
        {"upgrade-to-requests", &IOTable,       0,                              
        &UpgradeToRequestsTableDef},
        {"research-requests",   &IOTable,       0,                              
        &ResearchRequestsTableDef},
@@ -1748,7 +1763,8 @@
     for (i = 1; i < AI_MAX_RUNNING_SCRIPTS; i++) {
        AiPlayer->Scripts[i].Script = NIL;
        AiPlayer->Scripts[i].SleepCycles = 0;
-       snprintf(AiPlayer->Scripts[i].ident, 10, "Empty");
+       AiEraseForce(AiPlayer->Scripts[i].ownForce);
+       snprintf(AiPlayer->Scripts[i].ident, 10, "Empty");      
     }
     return SCM_BOOL_T;
 }
Index: stratagus/src/ai/new_ai.c
diff -u stratagus/src/ai/new_ai.c:1.80 stratagus/src/ai/new_ai.c:1.81
--- stratagus/src/ai/new_ai.c:1.80      Tue Oct 28 17:41:13 2003
+++ stratagus/src/ai/new_ai.c   Wed Oct 29 18:11:55 2003
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: new_ai.c,v 1.80 2003/10/28 22:41:13 n0body Exp $
+//      $Id: new_ai.c,v 1.81 2003/10/29 23:11:55 pludov Exp $
 
 
 //@{
@@ -744,7 +744,7 @@
 {
     CLprintf(file, "\n;;; -----------------------------------------\n");
     CLprintf(file,
-       ";;; MODULE: AI $Id: new_ai.c,v 1.80 2003/10/28 22:41:13 n0body Exp 
$\n\n");
+       ";;; MODULE: AI $Id: new_ai.c,v 1.81 2003/10/29 23:11:55 pludov Exp 
$\n\n");
 
     SaveAiTypesWcName(file);
     SaveAiHelper(file);
@@ -875,6 +875,7 @@
     void *temp;
     AiType *aitype;
     AiBuildQueue *queue;
+    AiExplorationRequest* request;
     char **cp;
 
     for (p = 0; p < PlayerMax; ++p) {
@@ -915,6 +916,15 @@
                free(queue);
            }
 
+           //
+           // Free ExplorationRequest list
+           //
+           while (pai->FirstExplorationRequest) {
+               request = pai->FirstExplorationRequest->Next;
+               free(pai->FirstExplorationRequest);
+               pai->FirstExplorationRequest = request;
+           }
+
            free(pai);
            Players[p].Ai = NULL;
        }
@@ -1378,6 +1388,12 @@
     if (AiPlayer->AutoAttack) {
        AiPeriodicAttack();
     }
+
+    // At most 1 explorer each 5 seconds
+    if (GameCycle > AiPlayer->LastExplorationGameCycle + 5 * 
CYCLES_PER_SECOND) {
+       AiSendExplorers();
+    }
+
 #ifdef TIMEIT
     ev = rdtsc();
     sx = (ev - sv);




reply via email to

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