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 ai/ai_local.h ai/script_a...


From: address@hidden
Subject: [Stratagus-CVS] stratagus/src ai/ai.c ai/ai_local.h ai/script_a...
Date: 8 Feb 2004 12:11:59 +1100

CVSROOT:        /home/strat
Module name:    stratagus
Changes by:      <address@hidden>       04/02/08 12:11:58

Modified files:
        src/ai         : ai.c ai_local.h script_ai.c 
        src/game       : savegame.c 
        src/include    : unit.h 
        src/network    : commands.c 
        src/stratagus  : spells.c 
        src/unit       : script_unit.c unit.c 

Log message:
        Apply Jarod's ai and lua savegame patch. Save unit allocator.
        Replays with save/load and ai still go out of sync

Patches:
Index: stratagus/src/ai/ai.c
diff -u stratagus/src/ai/ai.c:1.114 stratagus/src/ai/ai.c:1.115
--- stratagus/src/ai/ai.c:1.114 Mon Feb  2 06:00:24 2004
+++ stratagus/src/ai/ai.c       Sun Feb  8 12:11:51 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai.c,v 1.114 2004/02/01 19:00:24 nobody_ Exp $
+//      $Id: ai.c,v 1.115 2004/02/08 01:11:51 nobody_ Exp $
 
 
 //@{
@@ -368,14 +368,16 @@
        //  Dump table wc2 race numbers -> internal symbol.
        //
        if ((cp = AiTypeWcNames)) {
-               CLprintf(file, "(define-ai-wc-names");
+               CLprintf(file, "DefineAiWcNames(");
 
-               i = 90;
+               if (*cp) {
+                       i = CLprintf(file, "\n \"%s\"", *cp++);
+               }
                while (*cp) {
                        if (i + strlen(*cp) > 79) {
                                i = CLprintf(file, "\n ");
                        }
-                       i += CLprintf(file, " '%s", *cp++);
+                       i += CLprintf(file, ", \"%s\"", *cp++);
                }
                CLprintf(file, ")\n\n");
        }
@@ -397,15 +399,17 @@
        int i;
        int j;
        int f;
+       int max;
 
-       for (t = 0; t < (upgrade ? UpgradeMax : NumUnitTypes); ++t) {
+       max = (upgrade ? UpgradeMax : NumUnitTypes);
+       for (t = 0; t < max; ++t) {
                // Look if that unit-type can build something
                for (f = i = 0; i < n; ++i) {
                        if (table[i]) {
                                for (j = 0; j < table[i]->Count; ++j) {
                                        if (table[i]->Table[j]->Slot == t) {
                                                if (!f) {
-                                                       CLprintf(file, "\n  
(list '%s '%s\n     ", name,
+                                                       CLprintf(file, "\n  
{\"%s\", \"%s\"\n   ", name,
                                                                
UnitTypes[t]->Ident);
                                                        f = 4;
                                                }
@@ -413,19 +417,19 @@
                                                        if (f + 
strlen(Upgrades[i].Ident) > 78) {
                                                                f = 
CLprintf(file, "\n  ");
                                                        }
-                                                       f += CLprintf(file, 
"'%s ", Upgrades[i].Ident);
+                                                       f += CLprintf(file, ", 
\"%s\"", Upgrades[i].Ident);
                                                } else {
                                                        if (f + 
strlen(UnitTypes[i]->Ident) > 78) {
                                                                f = 
CLprintf(file, "\n  ");
                                                        }
-                                                       f += CLprintf(file, 
"'%s ", UnitTypes[i]->Ident);
+                                                       f += CLprintf(file, ", 
\"%s\"", UnitTypes[i]->Ident);
                                                }
                                        }
                                }
                        }
                }
                if (f) {
-                       CLprintf(file, ")");
+                       CLprintf(file, "},");
                }
        }
 }
@@ -447,19 +451,25 @@
 
        for (i = 0; i < n; ++i) {
                if (table[i]) {
-                       CLprintf(file, "\n  (list '%s '%s\n     ", name, 
UnitTypes[i]->Ident);
+                       CLprintf(file, "\n  {\"%s\", \"%s\"\n   ", name, 
UnitTypes[i]->Ident);
                        f = 4;
                        for (j = 0; j < table[i]->Count; ++j) {
                                if (f + strlen(table[i]->Table[j]->Ident) > 78) 
{
                                        f = CLprintf(file, "\n  ");
                                }
-                               f += CLprintf(file, "'%s ", 
table[i]->Table[j]->Ident);
+                               f += CLprintf(file, ", \"%s\"", 
table[i]->Table[j]->Ident);
+                       }
+                       if (i == n - 1) {
+                               CLprintf(file, "}");
+                       } else {
+                               CLprintf(file, "},");
                        }
-                       CLprintf(file, ")");
                }
        }
 }
 
+#if 0 // Not used. Same as SaveAiUnitLimitTable with Defautresource instead of 
food
+
 /**
 **  Save AI helper sub table.
 **
@@ -501,6 +511,8 @@
        }
 }
 
+#endif
+
 /**
 **  Save AI helper sub table.
 **
@@ -524,20 +536,20 @@
                                for (j = 0; j < table[i]->Count; ++j) {
                                        if (table[i]->Table[j]->Slot == t) {
                                                if (!f) {
-                                                       CLprintf(file, "\n  
(list '%s '%s\n     ", name,
+                                                       CLprintf(file, "\n  
{\"%s\", \"%s\"\n   ", name,
                                                                
UnitTypes[t]->Ident);
                                                        f = 4;
                                                }
                                                if (f + strlen("food") > 78) {
                                                        f = CLprintf(file, "\n  
");
                                                }
-                                               f += CLprintf(file, "'%s ", 
"food");
+                                               f += CLprintf(file, ",\"%s\" ", 
"food");
                                        }
                                }
                        }
                }
                if (f) {
-                       CLprintf(file, ")");
+                       CLprintf(file, "},");
                }
        }
 }
@@ -546,10 +558,11 @@
 **  Save AI helper table.
 **
 **  @param file  Output file.
+**  @todo manage correctly ","
 */
 local void SaveAiHelper(CLFile* file)
 {
-       CLprintf(file, "(define-ai-helper");
+       CLprintf(file, "DefineAiHelper(");
        //
        //  Save build table
        //
@@ -597,27 +610,13 @@
 */
 local void SaveAiType(CLFile* file, const AiType* aitype)
 {
-#if 0
-       SCM list;
-
        if (aitype->Next) {
                SaveAiType(file, aitype->Next);
        }
        DebugLevel3Fn("%s,%s,%s\n" _C_ aitype->Name _C_ aitype->Race _C_ 
aitype->Class);
-       CLprintf(file, "(define-ai \"%s\" '%s '%s\n",
-               aitype->Name, aitype->Race ? aitype->Race : "*", aitype->Class);
-
-       CLprintf(file, "  '(");
-       //  Print the script a little formated
-       list = aitype->Script;
-       while (!gh_null_p(list)) {
-               CLprintf(file, "\n      ");
-               lprin1CL(gh_car(list), file);
-               list = gh_cdr(list);
-       }
-       CLprintf(file, " ))\n\n");
-#else
-#endif
+       CLprintf(file, "DefineAi(\"%s\", \"%s\", \"%s\", %s)\n\n",
+               aitype->Name, aitype->Race ? aitype->Race : "*",
+               aitype->Class, aitype->FunctionName);
 }
 
 /**
@@ -820,15 +819,14 @@
 {
        CLprintf(file, "\n--- -----------------------------------------\n");
        CLprintf(file,
-               "--- MODULE: AI $Id: ai.c,v 1.114 2004/02/01 19:00:24 nobody_ 
Exp $\n\n");
-#if 0
+               "--- MODULE: AI $Id: ai.c,v 1.115 2004/02/08 01:11:51 nobody_ 
Exp $\n\n");
+
        SaveAiTypesWcName(file);
        SaveAiHelper(file);
        SaveAiTypes(file);
-#endif
        SaveAiPlayers(file);
 
-       DebugLevel0Fn("FIXME: Saving AI isn't supported\n");
+       DebugLevel0Fn("FIXME: Saving lua function definition isn't 
supported\n");
 }
 
 /**
@@ -998,6 +996,7 @@
                free(aitype->Race);
                free(aitype->Class);
                free(aitype->Script);
+               free(aitype->FunctionName);
 
                temp = aitype->Next;
                free(aitype);
Index: stratagus/src/ai/ai_local.h
diff -u stratagus/src/ai/ai_local.h:1.55 stratagus/src/ai/ai_local.h:1.56
--- stratagus/src/ai/ai_local.h:1.55    Sat Jan 17 02:17:21 2004
+++ stratagus/src/ai/ai_local.h Sun Feb  8 12:11:52 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: ai_local.h,v 1.55 2004/01/16 15:17:21 wizzard Exp $
+//      $Id: ai_local.h,v 1.56 2004/02/08 01:11:52 nobody_ Exp $
 
 #ifndef __AI_LOCAL_H__
 #define __AI_LOCAL_H__
@@ -68,6 +68,7 @@
 #endif
 
        char* Script; /// Main script
+       char* FunctionName;     /// Name of the function
 };
 
 /**
Index: stratagus/src/ai/script_ai.c
diff -u stratagus/src/ai/script_ai.c:1.100 stratagus/src/ai/script_ai.c:1.101
--- stratagus/src/ai/script_ai.c:1.100  Mon Jan 19 09:36:24 2004
+++ stratagus/src/ai/script_ai.c        Sun Feb  8 12:11:52 2004
@@ -27,7 +27,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: script_ai.c,v 1.100 2004/01/18 22:36:24 nobody_ Exp $
+//      $Id: script_ai.c,v 1.101 2004/02/08 01:11:52 nobody_ Exp $
 
 //@{
 
@@ -360,6 +360,21 @@
        lua_rawset(l, 5);
        lua_pop(l, 1);
 
+// Get name of function
+       lua_pushstring(l, "debug");
+       lua_gettable(l, LUA_GLOBALSINDEX);
+       DebugCheck(lua_isnil(l, -1));
+       lua_pushstring(l, "getinfo");
+       lua_gettable(l, -2);
+       DebugCheck(!lua_isfunction(l, -1));
+       lua_pushvalue(l, 4);
+       lua_call(l, 1, 1);
+       lua_pushstring(l, "name");
+       lua_gettable(l, -2);
+       aitype->FunctionName = strdup(lua_tostring(l, -1));
+       lua_pop(l, 2); // FIXME : check if this value is correct.
+       // We can have opcode of this function with string.dump(function)
+       // Problems are for sub functions...
        return 0;
 }
 
Index: stratagus/src/game/savegame.c
diff -u stratagus/src/game/savegame.c:1.44 stratagus/src/game/savegame.c:1.45
--- stratagus/src/game/savegame.c:1.44  Sun Feb  1 09:04:04 2004
+++ stratagus/src/game/savegame.c       Sun Feb  8 12:11:53 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: savegame.c,v 1.44 2004/01/31 22:04:04 wizzard Exp $
+//      $Id: savegame.c,v 1.45 2004/02/08 01:11:53 nobody_ Exp $
 
 //@{
 
@@ -36,6 +36,7 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <time.h>
 
 #include "stratagus.h"
@@ -70,6 +71,156 @@
 ----------------------------------------------------------------------------*/
 
 /**
+**     For saving lua state (table, number, string, bool, not function).
+**
+**     @param l        lua_State to save.
+**     @param is_root  non-null for the main call, 0 for recursif call.
+**
+**     @return NULL if nothing could be saved.
+**             else a string that could be executed in lua to restore lua state
+**     @todo do the output prettier (adjust indentation, newline)
+*/
+local char* SaveGlobal(lua_State *l, int is_root)
+{
+       int type_key;
+       int type_value;
+       const char *sep;
+       const char *key;
+       char *value;
+       char *res;
+       int first;
+       char *tmp;
+       int b;
+
+//     DebugCheck(is_root && lua_gettop(l));
+       first = 1;
+       res = NULL;
+       if (is_root) {
+               lua_pushstring(l, "_G");// global table in lua.
+               lua_gettable(l, LUA_GLOBALSINDEX);
+       }
+       sep = (is_root) ? "" : ", ";
+       DebugCheck(!lua_istable(l, -1));
+       lua_pushnil(l);
+       while (lua_next(l, -2)) {
+               type_key = lua_type(l, -2);
+               type_value = lua_type(l, -1);
+               key = (type_key == LUA_TSTRING) ? lua_tostring(l, -2) : "";
+               if (!strcmp(key, "_G") || (is_root
+                       && (!strcmp(key, "assert") || !strcmp(key, "gcinfo") || 
!strcmp(key, "getfenv")
+                       || !strcmp(key, "unpack") || !strcmp(key, "tostring") 
|| !strcmp(key, "tonumber")
+                       || !strcmp(key, "setmetatable") || !strcmp(key, 
"require") || !strcmp(key, "pcall")
+                       || !strcmp(key, "rawequal") || !strcmp(key, 
"collectgarbage") || !strcmp(key, "type")
+                       || !strcmp(key, "getmetatable") || !strcmp(key, "next") 
|| !strcmp(key, "print")
+                       || !strcmp(key, "xpcall") || !strcmp(key, "rawset") || 
!strcmp(key, "setfenv")
+                       || !strcmp(key, "rawget") || !strcmp(key, "newproxy") 
|| !strcmp(key, "ipairs")
+                       || !strcmp(key, "loadstring") || !strcmp(key, "dofile") 
|| !strcmp(key, "_TRACEBACK")
+                       || !strcmp(key, "_VERSION") || !strcmp(key, "pairs") || 
!strcmp(key, "__pow")
+                       || !strcmp(key, "error") || !strcmp(key, "loadfile") || 
!strcmp(key, "arg")
+                       || !strcmp(key, "_LOADED") || !strcmp(key, "loadlib") 
|| !strcmp(key, "string")
+                       || !strcmp(key, "os") || !strcmp(key, "io") || 
!strcmp(key, "debug")
+                       || !strcmp(key, "coroutine")
+               // other string to protected ?
+#if META_LUA
+                       || !strcmp(key, "Stratagus") // do not save stratagus 
table. or should be ?
+#endif
+               ))) {
+                       lua_pop(l, 1); // pop the value
+                       continue;
+               }
+               switch (type_value) {
+                       case LUA_TNIL:
+                               value = strdup("nil");
+                               break;
+                       case LUA_TNUMBER:
+                               value = strdup(lua_tostring(l, -1)); // let lua 
do the conversion
+                               break;
+                       case LUA_TBOOLEAN:
+                               b = lua_toboolean(l, -1);
+                               value = strdup(b ? "true" : "false"); // let 
lua do the conversion
+                               break;
+                       case LUA_TSTRING:
+                               value = strdcat3("\"", lua_tostring(l, -1), 
"\"");
+                               break;
+                       case LUA_TTABLE:
+                               lua_pushvalue(l, -1);
+                               tmp = SaveGlobal(l, 0); 
+                               value = NULL;
+                               if (tmp != NULL) {
+                                       value = strdcat3("{", tmp, "}");
+                                       free(tmp);
+                               }
+                               break;
+                       case LUA_TFUNCTION:
+                       // Could be done with string.dump(function)
+                       // and debug.getinfo(function).name (coulb be nil for 
anonymous function)
+                       // But not usefull yet.
+                               value = NULL;
+                               break;
+                       case LUA_TUSERDATA:
+                       case LUA_TTHREAD:
+                       case LUA_TLIGHTUSERDATA:
+                       case LUA_TNONE:
+                       default : // no other cases
+                               value = NULL;
+                               break;
+               }
+               lua_pop(l, 1); /* pop the value */
+
+               // Check the validity of the key (only [a-zA-z_])
+               if (type_key == LUA_TSTRING) {
+                       int i;
+
+                       for (i = 0; key[i]; i++) {
+                               if (!(('a' <= key[i] && 'z' >= key[i])
+                                       || ('A' <= key[i] && 'Z' >= key[i])
+                                       || ('0' <= key[i] && key[i] >= '9') || 
key[i] == '_')) {
+                                       free(value);
+                                       value = NULL;
+                                       break;
+                               }
+                       }
+               }
+               if (value == NULL) {
+                       if (!is_root) {
+                               lua_pop(l, 2); // pop the key and the table
+                               return NULL;
+                       }
+                       continue;
+               }
+               if (type_key == LUA_TSTRING && !strcmp(key, value)) {
+                       continue;
+               }
+               if (first) {
+                       first = 0;
+                       if (type_key == LUA_TSTRING) {
+                               res = strdcat3(key, "=", value);
+                               free(value);
+                       } else {
+                               res = value;
+                       }
+               } else {
+                       if (type_key == LUA_TSTRING) {
+                               tmp = value;
+                               value = strdcat3(key, "=", value);
+                               free(tmp);
+                               tmp = res;
+                               res = strdcat3(res, sep, value);
+                               free(tmp);
+                       } else {
+                               res = strdcat3(res, sep, value);
+                       }
+               }
+               tmp = res;
+               res = strdcat3("", res, "\n");
+               free(tmp);
+       }
+       lua_pop(l, 1); // pop the table
+//     DebugCheck(is_root && lua_gettop(l));
+       return res;
+}
+
+/**
 **  Save a game to file.
 **
 **  @param filename  File name to be stored.
@@ -101,7 +252,7 @@
        CLprintf(file, "SaveGame({\n");
        CLprintf(file, "---  \"comment\", \"Generated by Stratagus Version " 
VERSION "\",\n");
        CLprintf(file, "---  \"comment\", \"Visit http://Stratagus.Org for more 
informations\",\n");
-       CLprintf(file, "---  \"comment\", \"$Id: savegame.c,v 1.44 2004/01/31 
22:04:04 wizzard Exp $\",\n");
+       CLprintf(file, "---  \"comment\", \"$Id: savegame.c,v 1.45 2004/02/08 
01:11:53 nobody_ Exp $\",\n");
        CLprintf(file, "---  \"type\",    \"%s\",\n", "single-player");
        CLprintf(file, "---  \"date\",    \"%s\",\n", s);
        CLprintf(file, "---  \"map\",     \"%s\",\n", TheMap.Description);
@@ -162,7 +313,10 @@
        SaveObjectives(file);
        SaveReplayList(file);
        // FIXME: find all state information which must be saved.
-
+       s = SaveGlobal(Lua, 1);
+       if (s != NULL) {
+               CLprintf(file, "-- Lua state\n\n %s\n", s);
+       }
        CLclose(file);
 }
 
Index: stratagus/src/include/unit.h
diff -u stratagus/src/include/unit.h:1.256 stratagus/src/include/unit.h:1.257
--- stratagus/src/include/unit.h:1.256  Sat Feb  7 01:02:17 2004
+++ stratagus/src/include/unit.h        Sun Feb  8 12:11:54 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: unit.h,v 1.256 2004/02/06 14:02:17 nobody_ Exp $
+//      $Id: unit.h,v 1.257 2004/02/08 01:11:54 nobody_ Exp $
 
 #ifndef __UNIT_H__
 #define __UNIT_H__
@@ -748,6 +748,8 @@
 extern int    NumSelected;                 /// how many units selected
 extern int    NumTeamSelected[PlayerMax];  /// Number of Units a team member 
has selected
 
+extern Unit*  ReleasedHead;                               /// Head of the 
released unit list.
+extern Unit*  ReleasedTail;                               /// Tail of the 
released unit list.
 
 /*----------------------------------------------------------------------------
 --             Functions
@@ -1070,7 +1072,7 @@
 extern void CclParseOrder(lua_State* l, Order* order);
        /// register CCL units features
 extern void UnitCclRegister(void);
-
+       
 //@}
 
 #endif         // !__UNIT_H__
Index: stratagus/src/network/commands.c
diff -u stratagus/src/network/commands.c:1.103 
stratagus/src/network/commands.c:1.104
--- stratagus/src/network/commands.c:1.103      Sat Jan 31 20:26:13 2004
+++ stratagus/src/network/commands.c    Sun Feb  8 12:11:55 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: commands.c,v 1.103 2004/01/31 09:26:13 nobody_ Exp $
+//      $Id: commands.c,v 1.104 2004/02/08 01:11:55 nobody_ Exp $
 
 //@{
 
@@ -173,7 +173,7 @@
 
        replay->Comment1 = strdup("Generated by Stratagus Version " VERSION "");
        replay->Comment2 = strdup("Visit http://Stratagus.Org for more 
information");
-       replay->Comment3 = strdup("$Id: commands.c,v 1.103 2004/01/31 09:26:13 
nobody_ Exp $");
+       replay->Comment3 = strdup("$Id: commands.c,v 1.104 2004/02/08 01:11:55 
nobody_ Exp $");
 
        if (GameSettings.NetGameType == SettingsSinglePlayerGame) {
                replay->Type = ReplaySinglePlayer;
@@ -828,6 +828,7 @@
                } else {
                        NotifyPlayer(ThisPlayer, NotifyYellow, 0, 0, "Replay 
got out of sync (%lu) !", GameCycle);
                        DebugLevel0("OUT OF SYNC %u != %u\n" _C_ SyncRandSeed 
_C_ ReplayStep->SyncRandSeed);
+                       DebugCheck(1);
                        // ReplayStep = 0;
                        // NextLogCycle = ~0UL;
                        // return;
Index: stratagus/src/stratagus/spells.c
diff -u stratagus/src/stratagus/spells.c:1.145 
stratagus/src/stratagus/spells.c:1.146
--- stratagus/src/stratagus/spells.c:1.145      Mon Feb  2 23:09:33 2004
+++ stratagus/src/stratagus/spells.c    Sun Feb  8 12:11:56 2004
@@ -27,7 +27,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: spells.c,v 1.145 2004/02/02 12:09:33 nobody_ Exp $
+//     $Id: spells.c,v 1.146 2004/02/08 01:11:56 nobody_ Exp $
 
 /*
 **             And when we cast our final spell
@@ -197,6 +197,7 @@
        if (portal) {
                // FIXME: if cop is already defined --> move it, but it doesn't 
work?
                RemoveUnit(portal, NULL);
+               UnitCacheRemove(portal);
                PlaceUnit(portal, x, y);
        } else {
                portal = MakeUnitAndPlace(x, y, ptype, 
&Players[PlayerNumNeutral]);
Index: stratagus/src/unit/script_unit.c
diff -u stratagus/src/unit/script_unit.c:1.105 
stratagus/src/unit/script_unit.c:1.106
--- stratagus/src/unit/script_unit.c:1.105      Sun Feb  1 09:25:02 2004
+++ stratagus/src/unit/script_unit.c    Sun Feb  8 12:11:57 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: script_unit.c,v 1.105 2004/01/31 22:25:02 nobody_ Exp $
+//      $Id: script_unit.c,v 1.106 2004/02/08 01:11:57 nobody_ Exp $
 
 //@{
 
@@ -1509,7 +1509,48 @@
 #undef SLOT_LEN
 }
 
-// FIXME: write the missing access functions
+/**
+**     Load the unit allocator state.
+**     We need to do this in order to make sure that the game allocates units 
in the exact same way.
+*/
+local int CclUnitAllocQueue(lua_State* l)
+{
+       int i;
+       int args;
+       const char* key;
+       Unit* unit;
+
+       ReleasedHead = ReleasedTail = 0;
+       args = lua_gettop(l);
+       for (i = 1; i <= args; ++i) {
+               unit = malloc(sizeof(Unit));
+
+               lua_pushnil(l);
+               while (lua_next(l, i)) {
+                       key = LuaToString(l, -2);
+                       if (!strcmp(key, "Slot")) {
+                               unit->Slot = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                       } else if (!strcmp(key, "FreeCycle")) {
+                               unit->Refs = LuaToNumber(l, -1);
+                               lua_pop(l, 1);
+                       } else {
+                               lua_pushfstring(l, "Wrong key %s", key);
+                               lua_error(l);
+                               return 0;
+                       }
+               }
+
+               unit->Next = 0;
+               if (ReleasedHead) {
+                       ReleasedTail->Next = unit;
+                       ReleasedTail = unit;
+               } else {
+                       ReleasedHead = ReleasedTail = unit;
+               }
+       }
+       return 0;
+}
 
 /**
 **             Register CCL features for unit.
@@ -1539,6 +1580,7 @@
        lua_register(Lua, "SetUnitUnholyArmor", CclSetUnitUnholyArmor);
 
        lua_register(Lua, "SlotUsage", CclSlotUsage);
+       lua_register(Lua, "UnitAllocQueue", CclUnitAllocQueue);
 }
 
 //@}
Index: stratagus/src/unit/unit.c
diff -u stratagus/src/unit/unit.c:1.389 stratagus/src/unit/unit.c:1.390
--- stratagus/src/unit/unit.c:1.389     Sat Feb  7 01:02:18 2004
+++ stratagus/src/unit/unit.c   Sun Feb  8 12:11:57 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: unit.c,v 1.389 2004/02/06 14:02:18 nobody_ Exp $
+//      $Id: unit.c,v 1.390 2004/02/08 01:11:57 nobody_ Exp $
 
 //@{
 
@@ -71,8 +71,8 @@
 
 global Unit* UnitSlots[MAX_UNIT_SLOTS];                /// All possible units
 global Unit** UnitSlotFree;                            /// First free unit slot
-local Unit* ReleasedHead;                              /// List of released 
units.
-local Unit** ReleasedTail;                             /// List tail of 
released units.
+global Unit* ReleasedHead;                             /// List of released 
units.
+global Unit* ReleasedTail;                             /// List tail of 
released units.
 
 global Unit* Units[MAX_UNIT_SLOTS];            /// Array of used slots
 global int NumUnits;                                           /// Number of 
slots used
@@ -107,7 +107,7 @@
        } while (--slot > UnitSlots);
        UnitSlotFree = slot;
 
-       ReleasedTail = &ReleasedHead;                           // list of 
unfreed units.
+       ReleasedTail = ReleasedHead = 0; // list of unfreed units.
        NumUnits = 0;
 }
 
@@ -208,7 +208,6 @@
        //              on the way. We must wait a little time before we could 
free the
        //              memory.
        //
-       *ReleasedTail = unit;
        UnitCacheRemove(unit);
        //
        //              Remove the unit from the global units table.
@@ -219,8 +218,14 @@
        *unit->UnitSlot = temp;
        Units[NumUnits] = NULL;
 
-       unit->Next = 0;
-       ReleasedTail = &unit->Next;
+       if (ReleasedHead) {
+               ReleasedTail->Next = unit;
+               ReleasedTail = unit;
+               unit->Next = 0;
+       } else {
+               ReleasedHead = ReleasedTail = unit;
+               unit->Next = 0;
+       }
        unit->Refs = GameCycle + (NetworkMaxLag << 1);  // could be reuse after 
this time
        DebugLevel2Fn("%lu:No more references, only wait for network lag, unit 
%d\n" _C_
                GameCycle _C_ UnitNumber(unit));
@@ -252,11 +257,11 @@
        if (ReleasedHead && (unsigned)ReleasedHead->Refs < GameCycle) {
                unit = ReleasedHead;
                ReleasedHead = unit->Next;
-               if (ReleasedTail == &unit->Next) {              // last element
-                       ReleasedTail = &ReleasedHead;
+               if (ReleasedHead == 0) {                // last element
+                       DebugLevel0Fn("Released unit queue emptied\n");
+                       ReleasedTail = ReleasedHead = 0;
                }
-               DebugLevel2Fn("%lu:Release %p %d\n" _C_ GameCycle _C_ unit _C_
-                       UnitNumber(unit));
+               DebugLevel0Fn("%lu:Release %p %d\n" _C_ GameCycle _C_ unit _C_ 
unit->Slot);
                slot = UnitSlots + unit->Slot;
                memset(unit, 0, sizeof(*unit));
                // FIXME: can release here more slots, reducing memory needs.
@@ -3730,6 +3735,7 @@
 global void SaveUnits(CLFile* file)
 {
        Unit** table;
+       Unit* unit;
        int i;
        unsigned char SlotUsage[MAX_UNIT_SLOTS / 8 + 1];
        int InRun;
@@ -3737,7 +3743,7 @@
        int j;
 
        CLprintf(file, "\n--- -----------------------------------------\n");
-       CLprintf(file, "--- MODULE: units $Id: unit.c,v 1.389 2004/02/06 
14:02:18 nobody_ Exp $\n\n");
+       CLprintf(file, "--- MODULE: units $Id: unit.c,v 1.390 2004/02/08 
01:11:57 nobody_ Exp $\n\n");
 
 #if 0
        //
@@ -3793,11 +3799,20 @@
        }
 #endif
 
-       CLprintf (file, ")\n");
+       CLprintf(file, ")\n");
 
        for (table = Units; table < &Units[NumUnits]; ++table) {
                SaveUnit(*table, file);
        }
+
+       //  Save the Unit allocator state, sadly. I don't want to do this!
+       CLprintf(file, "\nUnitAllocQueue(\n");
+       for (unit = ReleasedHead; unit; unit = unit->Next) {
+               CLprintf(file, "\t{Slot = %d, FreeCycle = %d}%c \n", 
unit->Slot, unit->Refs,
+                               (unit->Next ? ',' : ' '));
+               DebugLevel0Fn("{Slot = %d, FreeCycle = %d}\n" _C_ unit->Slot 
_C_ unit->Refs);
+       }
+       CLprintf(file, ")\n");
 }
 
 /*----------------------------------------------------------------------------




reply via email to

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