stratagus-cvs
[Top][All Lists]
Advanced

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

[Stratagus-CVS] stratagus/src/ai ai.c ccl_ai.c


From: address@hidden
Subject: [Stratagus-CVS] stratagus/src/ai ai.c ccl_ai.c
Date: 4 Jan 2004 08:37:10 +1100

CVSROOT:        /home/strat
Module name:    stratagus
Changes by:      <address@hidden>       04/01/04 08:37:09

Modified files:
        src/ai         : ai.c ccl_ai.c 

Log message:
        Load/Save DefineAiPlayer

Patches:
Index: stratagus/src/ai/ai.c
diff -u stratagus/src/ai/ai.c:1.106 stratagus/src/ai/ai.c:1.107
--- stratagus/src/ai/ai.c:1.106 Sat Jan  3 11:26:19 2004
+++ stratagus/src/ai/ai.c       Sun Jan  4 08:37:08 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai.c,v 1.106 2004/01/03 00:26:19 jsalmon3 Exp $
+//      $Id: ai.c,v 1.107 2004/01/03 21:37:08 jsalmon3 Exp $
 
 
 //@{
@@ -641,18 +641,134 @@
 **  @param plynr  Player number.
 **  @param ai     Player AI.
 */
-local void SaveAiPlayer(CLFile* file, unsigned plynr, PlayerAi* ai)
+local void SaveAiPlayer(CLFile* file, int plynr, PlayerAi* ai)
 {
-#if 0
-       IOOutFile = file;
-       IOLoadingMode = 0;
-       IOTabLevel = 1;
-
-       CLprintf(IOOutFile, "(define-ai-player '");
-       IOPlayerAiFullPtr(SCM_UNSPECIFIED, &ai, 0);
-       CLprintf(IOOutFile, ")\n");
-#else
-#endif
+       int i;
+       const AiBuildQueue* queue;
+
+       CLprintf(file, "DefineAiPlayer(%d,\n", plynr);
+       CLprintf(file, "  \"ai-type\", \"%s\",\n",ai->AiType->Name);
+
+       CLprintf(file, "  \"script\", \"%s\",\n", ai->Script);
+       CLprintf(file, "  \"script-debug\", %s,\n", ai->ScriptDebug ? "true" : 
"false");
+       CLprintf(file, "  \"sleep-cycles\", %lu,\n", ai->SleepCycles);
+
+       //
+       //  All forces
+       //
+       for (i = 0; i < AI_MAX_ATTACKING_FORCES; ++i) {
+               const AiUnitType* aut;
+               const AiUnit* aiunit;
+
+               CLprintf(file, "  \"force\", {%d, %s%s%s", i,
+                       ai->Force[i].Completed ? "\"complete\"," : 
"\"recruit\",",
+                       ai->Force[i].Attacking ? " \"attack\"," : "",
+                       ai->Force[i].Defending ? " \"defend\"," : "");
+
+               CLprintf(file, " \"role\", ");
+               switch (ai->Force[i].Role) {
+                       case AiForceRoleAttack:
+                               CLprintf(file, "\"attack\",");
+                               break;
+                       case AiForceRoleDefend:
+                               CLprintf(file, "\"defend\",");
+                               break;
+                       default:
+                               CLprintf(file, "\"unknown-%d\",", 
ai->Force[i].Role);
+                               break;
+               }
+
+               CLprintf(file, "\n    \"types\", { ");
+               for (aut = ai->Force[i].UnitTypes; aut; aut = aut->Next) {
+                       CLprintf(file, "%d, \"%s\", ", aut->Want, 
aut->Type->Ident);
+               }
+               CLprintf(file, "},\n    \"units\", {");
+               for (aiunit = ai->Force[i].Units; aiunit; aiunit = 
aiunit->Next) {
+                       CLprintf(file, " %d, \"%s\",", UnitNumber(aiunit->Unit),
+                               aiunit->Unit->Type->Ident);
+               }
+               CLprintf(file, "},\n    \"state\", %d, \"goalx\", %d, 
\"goaly\", %d, \"must-transport\", %d,",
+                       ai->Force[i].State, ai->Force[i].GoalX, 
ai->Force[i].GoalY, ai->Force[i].MustTransport);
+               CLprintf(file, "},\n");
+       }
+
+       CLprintf(file, "  \"reserve\", {");
+       for (i = 0; i < MaxCosts; ++i) {
+               CLprintf(file, "\"%s\", %d, ", DefaultResourceNames[i], 
ai->Reserve[i]);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file, "  \"used\", {");
+       for (i = 0; i < MaxCosts; ++i) {
+               CLprintf(file, "\"%s\", %d, ", DefaultResourceNames[i], 
ai->Used[i]);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file, "  \"needed\", {");
+       for (i = 0; i < MaxCosts; ++i) {
+               CLprintf(file, "\"%s\", %d, ", DefaultResourceNames[i], 
ai->Needed[i]);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file, "  \"collect\", {");
+       for (i = 0; i < MaxCosts; ++i) {
+               CLprintf(file, "\"%s\", %d, ", DefaultResourceNames[i], 
ai->Collect[i]);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file,"  \"need-mask\", {");
+       for (i = 0; i < MaxCosts; ++i) {
+               if (ai->NeededMask & (1 << i)) {
+                       CLprintf(file, "\"%s\", ", DefaultResourceNames[i]);
+               }
+       }
+       CLprintf(file, "},\n");
+       if (ai->NeedSupply) {
+               CLprintf(file, "  \"need-supply\",\n");
+       }
+
+       //
+       //  Requests
+       //
+       // FIXME: FirstExplorationRequest
+       // FIXME: TransportRequests
+       CLprintf(file, "  \"unit-type\", {");
+       for (i = 0; i < ai->UnitTypeRequestsCount; ++i) {
+               CLprintf(file, "\"%s\", ", 
ai->UnitTypeRequests[i].Table[0]->Ident);
+               CLprintf(file, "%d, ", ai->UnitTypeRequests[i].Count);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file, "  \"upgrade\", {");
+       for (i = 0; i < ai->UpgradeToRequestsCount; ++i) {
+               CLprintf(file, "\"%s\", ", ai->UpgradeToRequests[i]->Ident);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file, "  \"research\", {");
+       for (i = 0; i < ai->ResearchRequestsCount; ++i) {
+               CLprintf(file, "\"%s\", ", ai->ResearchRequests[i]->Ident);
+       }
+       CLprintf(file, "},\n");
+
+       //
+       //  Building queue
+       //
+       CLprintf(file, "  \"building\", {");
+       for (queue = ai->UnitTypeBuilded; queue; queue = queue->Next) {
+               CLprintf(file, "\"%s\", %d, %d, ", queue->Type->Ident, 
queue->Made, queue->Want);
+       }
+       CLprintf(file, "},\n");
+
+       CLprintf(file, "  \"repair-building\", %u,\n", ai->LastRepairBuilding);
+
+       CLprintf(file, "  \"repair-workers\", {");
+       for (i = 0; i < UnitMax; ++i) {
+               if (ai->TriedRepairWorkers[i]) {
+                       CLprintf(file, "%d, %d, ", i, 
ai->TriedRepairWorkers[i]);
+               }
+       }
+       CLprintf(file,"})\n\n");
 }
 
 /**
@@ -662,7 +778,7 @@
 */
 local void SaveAiPlayers(CLFile* file)
 {
-       unsigned p;
+       int p;
 
        for (p = 0; p < PlayerMax; ++p) {
                if (Players[p].Ai) {
@@ -680,7 +796,7 @@
 {
        CLprintf(file, "\n--- -----------------------------------------\n");
        CLprintf(file,
-               "--- MODULE: AI $Id: ai.c,v 1.106 2004/01/03 00:26:19 jsalmon3 
Exp $\n\n");
+               "--- MODULE: AI $Id: ai.c,v 1.107 2004/01/03 21:37:08 jsalmon3 
Exp $\n\n");
 #if 0
        SaveAiTypesWcName(file);
        SaveAiHelper(file);
Index: stratagus/src/ai/ccl_ai.c
diff -u stratagus/src/ai/ccl_ai.c:1.95 stratagus/src/ai/ccl_ai.c:1.96
--- stratagus/src/ai/ccl_ai.c:1.95      Fri Jan  2 08:24:03 2004
+++ stratagus/src/ai/ccl_ai.c   Sun Jan  4 08:37:08 2004
@@ -27,7 +27,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ccl_ai.c,v 1.95 2004/01/01 21:24:03 jsalmon3 Exp $
+//      $Id: ccl_ai.c,v 1.96 2004/01/03 21:37:08 jsalmon3 Exp $
 
 //@{
 
@@ -808,6 +808,7 @@
                AiPlayer->Force[force].Role = AiForceRoleDefend;
        } else {
                lua_pushfstring(l, "Unknown force role", flag);
+               lua_error(l);
        }
 
        lua_pushboolean(l, 0);
@@ -1176,37 +1177,383 @@
 }
 
 /**
-**  Define an AI player.
+**     Define an AI player.
 **
-**  @param list  List of the AI Player.
+**     @param list     List of the AI Player.
 */
-#if 0
-local SCM CclDefineAiPlayer(SCM list)
+local int CclDefineAiPlayer(lua_State* l)
 {
-#if 0
-       SCM value;
-       SCM sublist;
-       char* str;
-#endif
+       const char* value;
        int i;
-       PlayerAi *ai;
+       PlayerAi* ai;
+       int args;
+       int j;
+       int subargs;
+       int k;
 
-       IOLoadingMode = 1;
+       args = lua_gettop(l);
+       j = 0;
 
-       IOPlayerAiFullPtr(gh_car(list), &ai, 0);
+       i = LuaToNumber(l, j + 1);
+       ++j;
 
-       i = ai->Player->Player;
        DebugCheck(i < 0 || i > PlayerMax);
-       Players[i].Ai = ai;
+       DebugLevel0Fn("%p %d\n" _C_ Players[i].Ai _C_ Players[i].AiEnabled );
+       // FIXME: lose this:
+       // DebugCheck(Players[i].Ai || !Players[i].AiEnabled);
+
+       ai = Players[i].Ai = calloc(1, sizeof(PlayerAi));
+       ai->Player = &Players[i];
+
+       //
+       //      Parse the list: (still everything could be changed!)
+       //
+       for (; j < args; ++j) {
+               value = LuaToString(l, j + 1);
+               ++j;
+
+               if (!strcmp(value, "ai-type")) {
+                       AiType* ait;
+
+                       value = LuaToString(l, j + 1);
+                       for (ait = AiTypes; ait; ait = ait->Next) {
+                               if (!strcmp(ait->Name, value)) {
+                                       break;
+                               }
+                       }
+                       if (!ait) {
+                               lua_pushfstring(l, "ai-type not found: %s", 
value);
+                       }
+                       ai->AiType = ait;
+                       ai->Script = ait->Script;
+               } else if (!strcmp(value, "script")) {
+                       ai->Script = strdup(LuaToString(l, j + 1));
+               } else if (!strcmp(value, "script-debug")) {
+                       ai->ScriptDebug = LuaToBoolean(l, j + 1);
+               } else if (!strcmp(value, "sleep-cycles")) {
+                       ai->SleepCycles = LuaToNumber(l, j + 1);
+               } else if (!strcmp(value, "force")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       k = 0;
+                       lua_rawgeti(l, j + 1, k + 1);
+                       i = LuaToNumber(l, -1);
+                       lua_pop(l, 1);
+                       ++k;
+                       for (; k < subargs; ++k) {
+                               lua_rawgeti(l, j + 1, k + 1);
+                               value = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               if (!strcmp(value, "complete")) {
+                                       ai->Force[i].Completed = 1;
+                                       --k;
+                               } else if (!strcmp(value, "recruit")) {
+                                       ai->Force[i].Completed = 0;
+                                       --k;
+                               } else if (!strcmp(value, "attack")) {
+                                       ai->Force[i].Attacking = 1;
+                                       --k;
+                               } else if (!strcmp(value, "defend")) {
+                                       ai->Force[i].Defending = 1;
+                                       --k;
+                               } else if (!strcmp(value, "role")) {
+                                       lua_rawgeti(l, j + 1, k + 1);
+                                       value = LuaToString(l, -1);
+                                       lua_pop(l, 1);
+                                       if (!strcmp(value, "attack")) {
+                                               ai->Force[i].Role = 
AiForceRoleAttack;
+                                       } else if (!strcmp(value, "defend")) {
+                                               ai->Force[i].Role = 
AiForceRoleDefend;
+                                       } else {
+                                               lua_pushfstring(l, "Unsupported 
force tag: %s", value);
+                                               lua_error(l);
+                                       }
+                               } else if (!strcmp(value, "types")) {
+                                       AiUnitType** queue;
+                                       int subsubargs;
+                                       int subk;
+
+                                       lua_rawgeti(l, j + 1, k + 1);
+                                       if (!lua_istable(l, -1)) {
+                                               lua_pushstring(l, "incorrect 
argument");
+                                               lua_error(l);
+                                       }
+                                       subsubargs = luaL_getn(l, -1);
+                                       queue = &ai->Force[i].UnitTypes;
+                                       for (subk = 0; subk < subsubargs; 
++subk) {
+                                               int num;
+                                               const char* ident;
+
+                                               lua_rawgeti(l, -1, subk + 1);
+                                               num = LuaToNumber(l, -1);
+                                               lua_pop(l, 1);
+                                               ++subk;
+                                               lua_rawgeti(l, -1, subk + 1);
+                                               ident = LuaToString(l, -1);
+                                               lua_pop(l, 1);
+                                               *queue = 
malloc(sizeof(AiUnitType));
+                                               (*queue)->Next = NULL;
+                                               (*queue)->Want = num;
+                                               (*queue)->Type = 
UnitTypeByIdent(ident);
+                                               queue = &(*queue)->Next;
+                                       }
+                                       lua_pop(l, 1);
+                               } else if (!strcmp(value, "units")) {
+                                       AiUnit** queue;
+                                       int subsubargs;
+                                       int subk;
+
+                                       lua_rawgeti(l, j + 1, k + 1);
+                                       if (!lua_istable(l, -1)) {
+                                               lua_pushstring(l, "incorrect 
argument");
+                                               lua_error(l);
+                                       }
+                                       subsubargs = luaL_getn(l, -1);
+                                       queue = &ai->Force[i].Units;
+                                       for (subk = 0; subk < subsubargs; 
++subk) {
+                                               int num;
+                                               const char* ident;
+
+                                               lua_rawgeti(l, -1, subk + 1);
+                                               num = LuaToNumber(l, -1);
+                                               lua_pop(l, 1);
+                                               ++subk;
+                                               lua_rawgeti(l, -1, subk + 1);
+                                               ident = LuaToString(l, -1);
+                                               lua_pop(l, 1);
+                                               *queue = malloc(sizeof(AiUnit));
+                                               (*queue)->Next = NULL;
+                                               (*queue)->Unit = UnitSlots[num];
+                                               queue = &(*queue)->Next;
+                                       }
+                                       lua_pop(l, 1);
+                               }
+                       }
+               } else if (!strcmp(value, "reserve")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       for (k = 0; k < subargs; ++k) {
+                               const char* type;
+                               int num;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               type = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               num = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ai->Reserve[DefaultResourceNumber(type)] = num;
+                       }
+               } else if (!strcmp(value, "used")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       for (k = 0; k < subargs; ++k) {
+                               const char* type;
+                               int num;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               type = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               num = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ai->Used[DefaultResourceNumber(type)] = num;
+                       }
+               } else if (!strcmp(value, "needed")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       for (k = 0; k < subargs; ++k) {
+                               const char* type;
+                               int num;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               type = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               num = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ai->Needed[DefaultResourceNumber(type)] = num;
+                       }
+               } else if (!strcmp(value, "collect")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       for (k = 0; k < subargs; ++k) {
+                               const char* type;
+                               int num;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               type = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               num = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ai->Collect[DefaultResourceNumber(type)] = num;
+                       }
+               } else if (!strcmp(value, "need-mask")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       for (k = 0; k < subargs; ++k) {
+                               const char* type;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               type = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ai->NeededMask |= (1 << 
DefaultResourceNumber(type));
+                       }
+               } else if (!strcmp(value, "need-supply")) {
+                       ai->NeedSupply = 1;
+                       --j;
+               } else if (!strcmp(value, "unit-type")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       i = 0;
+                       if (subargs) {
+                               ai->UnitTypeRequests = malloc(subargs / 2 * 
sizeof(AiUnitTypeTable));
+                       }
+                       for (k = 0; k < subargs; ++k) {
+                               const char* ident;
+                               int count;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               ident = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               count = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ai->UnitTypeRequests[i].Table[0] = 
UnitTypeByIdent(ident);
+                               ai->UnitTypeRequests[i].Count = count;
+                               ++i;
+                       }
+                       ai->UnitTypeRequestsCount = i;
+               } else if (!strcmp(value, "upgrade")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       i = 0;
+                       if (subargs) {
+                               ai->UpgradeToRequests = malloc(subargs * 
sizeof(UnitType*));
+                       }
+                       for (k = 0; k < subargs; ++k) {
+                               const char* ident;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               ident = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ai->UpgradeToRequests[i] = 
UnitTypeByIdent(ident);
+                               ++i;
+                       }
+                       ai->UpgradeToRequestsCount = i;
+               } else if (!strcmp(value, "research")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       i = 0;
+                       if (subargs) {
+                               ai->ResearchRequests = malloc(subargs * 
sizeof(Upgrade*));
+                       }
+                       for (k = 0; k < subargs; ++k) {
+                               const char* ident;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               ident = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ai->ResearchRequests[i] = UpgradeByIdent(ident);
+                               ++i;
+                       }
+                       ai->ResearchRequestsCount = i;
+               } else if (!strcmp(value, "building")) {
+                       AiBuildQueue** queue;
+
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       queue = &ai->UnitTypeBuilded;
+                       for (k = 0; k < subargs; ++k) {
+                               const char* ident;
+                               int made;
+                               int want;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               ident = LuaToString(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               made = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               want = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               *queue = malloc(sizeof(AiBuildQueue));
+                               (*queue)->Next = NULL;
+                               (*queue)->Type = UnitTypeByIdent(ident);
+                               (*queue)->Want = want;
+                               (*queue)->Made = made;
+                               queue = &(*queue)->Next;
+                       }
+               } else if (!strcmp(value, "repair-building")) {
+                       ai->LastRepairBuilding = LuaToNumber(l, j + 1);
+               } else if (!strcmp(value, "repair-workers")) {
+                       if (!lua_istable(l, j + 1)) {
+                               lua_pushstring(l, "incorrect argument");
+                               lua_error(l);
+                       }
+                       subargs = luaL_getn(l, j + 1);
+                       for (k = 0; k < subargs; ++k) {
+                               int num;
+                               int workers;
+
+                               lua_rawgeti(l, j + 1, k + 1);
+                               num = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ++k;
+                               lua_rawgeti(l, j + 1, k + 1);
+                               workers = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                               ai->TriedRepairWorkers[num] = workers;
+                               ++i;
+                       }
+               } else {
+                       lua_pushfstring(l, "Unsupported tag: %s", value);
+                       lua_error(l);
+               }
+       }
 
-       return SCM_UNSPECIFIED;
-}
-#else
-local int CclDefineAiPlayer(lua_State* l)
-{
        return 0;
 }
-#endif
 
 /**
 **  Register CCL features for unit-type.




reply via email to

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