stratagus-cvs
[Top][All Lists]
Advanced

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

[Stratagus-CVS] stratagus/src include/missile.h include/script....


From: address@hidden
Subject: [Stratagus-CVS] stratagus/src include/missile.h include/script....
Date: 19 Jan 2004 06:41:04 +1100

CVSROOT:        /home/strat
Module name:    stratagus
Changes by:      <address@hidden>       04/01/19 06:41:02

Modified files:
        src/include    : missile.h script.h spells.h stratagus.h 
        src/missile    : missile.c script_missile.c 
        src/stratagus  : script.c script_spell.c spells.c 
        src/unit       : unit.c 

Log message:
        META_LUA work. Simpler mechanism.
        Finally units run away when hit.

Patches:
Index: stratagus/src/include/missile.h
diff -u stratagus/src/include/missile.h:1.81 
stratagus/src/include/missile.h:1.82
--- stratagus/src/include/missile.h:1.81        Sat Jan 17 06:22:15 2004
+++ stratagus/src/include/missile.h     Mon Jan 19 06:40:58 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: missile.h,v 1.81 2004/01/16 19:22:15 wizzard Exp $
+//      $Id: missile.h,v 1.82 2004/01/18 19:40:58 nobody_ Exp $
 
 #ifndef __MISSILE_H__
 #define __MISSILE_H__
@@ -396,8 +396,6 @@
 
        /// Base structure of missile-types
 struct _missile_type_ {
-       const void* OType;  /// object type (future extensions)
-
        char* Ident;          /// missile name
        char* File;           /// missile sprite file
        int   Transparency;   /// Missile transparency possible value is 50 
(later 25 and 75)
@@ -483,10 +481,10 @@
 --  Variables
 ----------------------------------------------------------------------------*/
 
-extern char** MissileTypeWcNames;  /// Mapping wc-number 2 symbol
-
-extern MissileType* MissileTypes;  /// All missile-types
+extern MissileType** MissileTypes;  /// All missile-types
+extern int NumMissileTypes;        /// Number of missile-types
 
+extern char** MissileTypeWcNames;  /// Mapping wc-number 2 symbol
 extern const char* MissileClassNames[];  /// Missile class names
 
 extern BurningBuildingFrame* BurningBuildingFrames;  /// Burning building 
frames
@@ -559,6 +557,11 @@
 FuncController MissileActionWhirlwind;
 FuncController MissileActionFlameShield;
 FuncController MissileActionDeathCoil;
+
+#ifdef META_LUA
+       /// Initialize Spell scripting.
+extern void ScriptSpellInit(void);
+#endif
 
 //@}
 
Index: stratagus/src/include/script.h
diff -u stratagus/src/include/script.h:1.52 stratagus/src/include/script.h:1.53
--- stratagus/src/include/script.h:1.52 Sun Jan 18 09:23:25 2004
+++ stratagus/src/include/script.h      Mon Jan 19 06:40:58 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: script.h,v 1.52 2004/01/17 22:23:25 nobody_ Exp $
+//      $Id: script.h,v 1.53 2004/01/18 19:40:58 nobody_ Exp $
 
 #ifndef __SCRIPT_H__
 #define __SCRIPT_H__
@@ -87,15 +87,42 @@
 #ifdef META_LUA
 
 /*----------------------------------------------------------------------------
---  Functions.
+--  Functions and data structures.
 ----------------------------------------------------------------------------*/
 
+       /// Script get/set function prototype. The values is on the lua stack.
+typedef int ScriptGetSetFunction(void* object, const char* key, lua_State* l);
+
+       /// Structure for a script proxy. Don't mess with this outside of 
scripting
+typedef struct {
+       void* Object;                          /// The actual Object
+       ScriptGetSetFunction* GetFunction;        /// Get function
+       ScriptGetSetFunction* SetFunction;        /// Set function
+} ScriptProxy;
+
+       /// Userdata Constructor.
+extern void ScriptDoCreateUserdata(lua_State* l, void* Object,
+               ScriptGetSetFunction* GetFunction, ScriptGetSetFunction* 
SetFunction);
        /// Really dumb set function that always goes into an error.
 extern int ScriptSetValueBlock(lua_State* l);
 
 /*----------------------------------------------------------------------------
 --  Quick macros for meta lua. Use them in well-formed get/set functions.
 ----------------------------------------------------------------------------*/
+
+       /// Macro to call ScriptDoCreateUserdata w/o casts.
+#define ScriptCreateUserdata(l, o, g, s) (ScriptDoCreateUserdata(l, o, \
+               (ScriptGetSetFunction*)(g), (ScriptGetSetFunction*)(s)))
+
+    /// Quick way to fail in a function. You can use _C_ like in DebugLevelx
+#ifdef DEBUG
+#define LuaError(l, args) \
+       { lua_pushfstring(l, args); lua_error(l); return 0; }
+#else
+       /// Save on memory.
+#define LuaError(l, args) \
+       { lua_pushstring(l, "Lua error"); lua_error(l); return 0; }
+#endif
 
 //
 //     Pushing 0 as a string to lua is ok. strdup-ing 0 is not.
Index: stratagus/src/include/spells.h
diff -u stratagus/src/include/spells.h:1.46 stratagus/src/include/spells.h:1.47
--- stratagus/src/include/spells.h:1.46 Sun Jan 18 09:23:25 2004
+++ stratagus/src/include/spells.h      Mon Jan 19 06:40:58 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: spells.h,v 1.46 2004/01/17 22:23:25 nobody_ Exp $
+//     $Id: spells.h,v 1.47 2004/01/18 19:40:58 nobody_ Exp $
 
 #ifndef __SPELLS_H__
 #define __SPELLS_H__
@@ -344,14 +344,8 @@
 SpellFunc CastSpawnMissile;
 
 #ifdef META_LUA
-
        /// Initialize Spell scripting.
 extern void ScriptSpellInit(void);
-    /// Register the spell-table user-data.
-extern void ScriptSpellTableCreateUserdata(lua_State* l);
-       /// Register a spell user-data.
-extern void ScriptSpellCreateUserdata(lua_State* l, SpellType* spell);
-
 #endif
 
 //@}
Index: stratagus/src/include/stratagus.h
diff -u stratagus/src/include/stratagus.h:1.38 
stratagus/src/include/stratagus.h:1.39
--- stratagus/src/include/stratagus.h:1.38      Sun Jan 18 11:52:18 2004
+++ stratagus/src/include/stratagus.h   Mon Jan 19 06:40:58 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: stratagus.h,v 1.38 2004/01/18 00:52:18 nobody_ Exp $
+//      $Id: stratagus.h,v 1.39 2004/01/18 19:40:58 nobody_ Exp $
 
 #ifndef __STRATAGUS_H__
 #define __STRATAGUS_H__
Index: stratagus/src/missile/missile.c
diff -u stratagus/src/missile/missile.c:1.128 
stratagus/src/missile/missile.c:1.129
--- stratagus/src/missile/missile.c:1.128       Sat Jan 17 18:26:07 2004
+++ stratagus/src/missile/missile.c     Mon Jan 19 06:40:59 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: missile.c,v 1.128 2004/01/17 07:26:07 jsalmon3 Exp $
+//      $Id: missile.c,v 1.129 2004/01/18 19:40:59 nobody_ Exp $
 
 //@{
 
@@ -107,11 +107,7 @@
 */
 global char** MissileTypeWcNames;
 
-/**
-**  Defined missile-types.
-*/
-global MissileType* MissileTypes;
-
+global MissileType** MissileTypes;              /// Missile types.
 global int NumMissileTypes;                     /// number of missile-types 
made.
 
 #ifdef DEBUG
@@ -175,8 +171,8 @@
 #ifndef DYNAMIC_LOAD
        int i;
 
-       for (i = 0; MissileTypes[i].OType; ++i) {
-               LoadMissileSprite(&MissileTypes[i]);
+       for (i = 0; i < NumMissileTypes; ++i) {
+               LoadMissileSprite(MissileTypes[i]);
        }
 #endif
 }
@@ -219,30 +215,19 @@
        MissileType* mtype;
        int i;
 
-       //
-       // Allocate new memory. (+2 for start end empty last entry.)
-       //
-       mtype = calloc(NumMissileTypes + 2, sizeof(MissileType));
-       if (!mtype) {
-               fprintf(stderr, "Out of memory\n");
-               ExitFatal(-1);
-       }
-       memcpy(mtype, MissileTypes, sizeof(MissileType) * NumMissileTypes);
-       if (MissileTypes) {
-               free(MissileTypes);
-       }
-       MissileTypes = mtype;
-       mtype = MissileTypes + NumMissileTypes++;
-       mtype->OType = MissileTypeType;
+       MissileTypes = realloc(MissileTypes, (NumMissileTypes + 1) * 
sizeof(MissileType *));
+       mtype = MissileTypes[NumMissileTypes++] = (MissileType 
*)malloc(sizeof(MissileType));
+       memset(mtype, 0, sizeof(MissileType));
+
+       // Defaults.
        mtype->Ident = ident;
-       //
+       mtype->CanHitOwner = 0;
+       mtype->FriendlyFire = 0;
+
        // Rehash.
-       //
        for (i = 0; i < NumMissileTypes; ++i) {
-               *(MissileType**)hash_add(MissileTypeHash, 
MissileTypes[i].Ident) = &MissileTypes[i];
+               *(MissileType**)hash_add(MissileTypeHash, 
MissileTypes[i]->Ident) = MissileTypes[i];
        }
-       mtype->CanHitOwner = 0;  // defaults
-       mtype->FriendlyFire = 0;
 
        return mtype;
 }
@@ -1320,7 +1305,7 @@
 
        DebugCheck(file == NULL);
        CLprintf(file, "\n;;; -----------------------------------------\n");
-       CLprintf(file, ";;; MODULE: missile-types $Id: missile.c,v 1.128 
2004/01/17 07:26:07 jsalmon3 Exp $\n\n");
+       CLprintf(file, ";;; MODULE: missile-types $Id: missile.c,v 1.129 
2004/01/18 19:40:59 nobody_ Exp $\n\n");
 
        //
        // Original number to internal missile-type name.
@@ -1442,7 +1427,7 @@
        Missile* const* missiles;
 
        CLprintf(file,"\n--- -----------------------------------------\n");
-       CLprintf(file,"--- MODULE: missiles $Id: missile.c,v 1.128 2004/01/17 
07:26:07 jsalmon3 Exp $\n\n");
+       CLprintf(file,"--- MODULE: missiles $Id: missile.c,v 1.129 2004/01/18 
19:40:59 nobody_ Exp $\n\n");
 
        for (missiles = GlobalMissiles; *missiles; ++missiles) {
                SaveMissile(*missiles, file);
@@ -1457,12 +1442,15 @@
 */
 global void InitMissileTypes(void)
 {
+       int i;
        MissileType* mtype;
 
        if (!MissileTypes) {
                return;
        }
-       for (mtype = MissileTypes; mtype->OType; ++mtype) {
+       for (i = 0; i < NumMissileTypes; ++i) {
+               mtype = MissileTypes[i];
+
                //
                // Add missile names to hash table
                //
@@ -1491,12 +1479,15 @@
 */
 global void CleanMissileTypes(void)
 {
+       int i;
        MissileType* mtype;
 
        if (!MissileTypes) {
                return;
        }
-       for (mtype = MissileTypes; mtype->OType; ++mtype) {
+
+       for (i = 0; i < NumMissileTypes; ++i) {
+               mtype = MissileTypes[i];
                hash_del(MissileTypeHash, mtype->Ident);
 
                free(mtype->Ident);
Index: stratagus/src/missile/script_missile.c
diff -u stratagus/src/missile/script_missile.c:1.57 
stratagus/src/missile/script_missile.c:1.58
--- stratagus/src/missile/script_missile.c:1.57 Sun Jan 18 11:52:18 2004
+++ stratagus/src/missile/script_missile.c      Mon Jan 19 06:40:59 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: script_missile.c,v 1.57 2004/01/18 00:52:18 nobody_ Exp $
+//      $Id: script_missile.c,v 1.58 2004/01/18 19:40:59 nobody_ Exp $
 
 //@{
 
@@ -426,9 +426,107 @@
 }
 
 #ifdef META_LUA
+
+       /// Get func for SpellType 
+local int ScriptMissileTypeGet(MissileType* missiletype, const char* key, 
lua_State* l);
+       /// Set func for SpellType 
+local int ScriptMissileTypeSet(MissileType* missiletype, const char* key, 
lua_State* l);
+
+/**
+**  Create a new missile Type
+**
+**     @param l    Lua state
+*/
+local int ScriptMissileTypesCreate(lua_State* l)
+{
+       LuaError(l, "Function not implemented.\n");
+}
+
+/**
+**     Get function for a missile type userdata.
+**
+**     @param missiletype      Pointer to the missile type.
+**     @param key      Key string.
+*/
+local int ScriptMissileTypeGet(MissileType* missiletype, const char* key, 
lua_State* l)
+{
+       LuaError(l, "Function not implemented.\n");
+}
+
+/**
+**     Set function for a missile type userdata.
+**
+**     @param missiletype      Pointer to the missile type.
+**     @param key      Key string.
+*/
+local int ScriptMissileTypeSet(MissileType* missiletype, const char* key, 
lua_State* l)
+{
+       LuaError(l, "Function not implemented.\n");
+}
+
 /**
-**  Register
+**     Get function for the big spell namespace.
 */
+local int ScriptMissileTypesNamespaceGet(lua_State* l)
+{
+       int i;
+       const char* key;
+       MissileType* mtype;
+
+       //  Index with number
+       if (lua_isnumber(l, 2)) {
+               i = LuaToNumber(l, 2);
+               DebugLevel3Fn("(%d)\n" _C_ i);
+               if (i < 0 || i >= NumMissileTypes) {
+                       LuaError(l, "Missile index out of range");
+               }
+               ScriptCreateUserdata(l, MissileTypes[i], ScriptMissileTypeGet, 
ScriptMissileTypeSet);
+               return 1;
+       }
+
+       //  Index with string.
+       key = LuaToString(l, 2);
+
+       META_GET_INT("n", NumMissileTypes);
+
+       if ((mtype = MissileTypeByIdent(key))) {
+               ScriptCreateUserdata(l, mtype, ScriptMissileTypeGet, 
ScriptMissileTypeSet);
+               return 1;
+       }
+
+       LuaError(l, "Spell \"%s\" doesn't exist.\n" _C_ key);
+}
+
+/**
+**     Initialize missile scripting. The main table is at -1
+**
+**     @param l   The lua state.
+*/
+global void ScriptMissileInit(void)
+{
+       // Create Stratagus.Missiles namespace.
+       // No userdata, there's no data. And no finalizer
+       lua_pushstring(Lua, "MissileTypes");
+       lua_newtable(Lua);
+
+       // Generate the metatable
+       lua_newtable(Lua);
+       lua_pushstring(Lua, "__index");
+       lua_pushcfunction(Lua, ScriptMissileTypesNamespaceGet);
+       lua_settable(Lua, -3);
+       lua_pushstring(Lua, "__newindex");
+       lua_pushcfunction(Lua, ScriptSetValueBlock); // Read-Only
+       lua_settable(Lua, -3);
+       lua_setmetatable(Lua, -2);
+
+       // Add functions.
+       lua_pushstring(Lua, "Create");
+       lua_pushcfunction(Lua, ScriptMissileTypesCreate);
+       lua_rawset(Lua, -3);
+
+       lua_rawset(Lua, -3);
+}
+
 #endif
 
 //@}
Index: stratagus/src/stratagus/script.c
diff -u stratagus/src/stratagus/script.c:1.158 
stratagus/src/stratagus/script.c:1.159
--- stratagus/src/stratagus/script.c:1.158      Sun Jan 18 09:23:26 2004
+++ stratagus/src/stratagus/script.c    Mon Jan 19 06:41:00 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: script.c,v 1.158 2004/01/17 22:23:26 nobody_ Exp $
+//      $Id: script.c,v 1.159 2004/01/18 19:41:00 nobody_ Exp $
 
 //@{
 
@@ -950,6 +950,127 @@
 
 #ifdef META_LUA
 
+/**
+**  Generic Get Function for a script proxy. Delegate the call to the object's
+**  own Get function.
+**
+**     @param l            The lua state.
+*/
+local int ScriptGet(lua_State* l)
+{
+       ScriptProxy* sp;
+       const char* key;
+
+       sp = (ScriptProxy*)lua_touserdata(l, -2);
+       key = LuaToString(l, -1);
+       DebugCheck((!sp) || (!key) || (!sp->Object) || (!sp->GetFunction));
+       DebugLevel3Fn("%p->(%s)\n" _C_ sp _C_ key);
+
+       return sp->GetFunction(sp->Object, key, l);
+}
+
+/**
+**  Generic Set Function for a script proxy. Delegate the call to the object's
+**  own Set function.
+**
+**     @param l            The lua state.
+*/
+local int ScriptSet(lua_State* l)
+{
+       ScriptProxy* sp;
+       const char* key;
+
+       sp = (ScriptProxy*)lua_touserdata(l, -3);
+       key = LuaToString(l, -2);
+       DebugCheck((!sp) || (!key) || (!sp->Object) || (!sp->SetFunction));
+       DebugLevel3Fn("%p->(%s)\n" _C_ sp _C_ key);
+
+       return sp->SetFunction(sp->Object, key, l);
+}
+
+/**
+**  Garbage collector function for normal userdata. This is only used inside
+**     scripting, getting called when lua decides to collect the proxy for a C
+**  structure.
+**
+**  @param l            The lua state.
+**
+**     @see ScriptCreateUserdata for garbage details.
+*/
+local int ScriptCollectUserdata(lua_State* l)
+{
+       ScriptProxy* sp;
+       char key[20];
+
+       sp = (ScriptProxy*)lua_touserdata(l, -1);
+       DebugLevel3Fn("Collecting ScriptProxy at %p for obj at %p.\n" _C_ sp 
_C_ sp->Object);
+
+       // Remove the key from the table.
+       lua_pushstring(l, "StratagusReferences");
+       lua_gettable(l, LUA_REGISTRYINDEX);
+       sprintf(key, "%p%p%p", sp->Object, sp->GetFunction, sp->SetFunction);
+       lua_pushstring(l, key);
+       lua_pushnil(l);
+       lua_settable(l, -3);
+       lua_remove(l, -1);
+       return 0;
+}
+
+/**
+**  Create a lua proxy for a C structure. This will not always create new
+**  userdata, but use an old one for the same structure. The userdata is 
pushed on
+**  the lua stack anyway.
+**
+**  @param l            The lua state
+**  @param Object       The Object to create userdata for.
+**  @param GetFunction  The function called to "Get" a value from the 
structure.
+**     @param SetFunction  The function called to "Set" a value from the 
structure.
+**
+**     @notes The Object is sent to the get/set functions, otherwise it is not 
touched.
+**     @notes Internals.  All lua proxies are kept inside a weak table inside 
the registry.
+**  That table is indexed by the pointer to the object, get and set funcs. 
When This
+**     function is called it searches for an already existant userdata, and 
returns it if found.
+**     Otherwise it create a new userdata, and sets it's metatable. A garbage 
collection
+**  proc is called to remove it from the table.
+*/
+global void ScriptDoCreateUserdata(lua_State* l, void* Object,
+               ScriptGetSetFunction* GetFunction, ScriptGetSetFunction* 
SetFunction)
+{
+       char key[40];
+       ScriptProxy* sp;
+
+       // FIXME: FASTER?
+       sprintf(key, "%p%p%p", Object, GetFunction, SetFunction);
+
+       lua_pushstring(l, "StratagusReferences");
+       lua_gettable(l, LUA_REGISTRYINDEX);
+       lua_pushstring(l, key);
+       lua_gettable(l, -2);
+
+       if (lua_isnil(l, -1)) {
+               lua_remove(l, -1);
+               // Create userdata.
+               sp = (ScriptProxy*)lua_newuserdata(l, sizeof(ScriptProxy));
+               sp->Object = Object;
+               sp->GetFunction = GetFunction;
+               sp->SetFunction = SetFunction;
+               // Get the standard metatable
+               lua_pushstring(l, "StratagusStandardMetatable");
+               lua_gettable(l, LUA_REGISTRYINDEX);
+               lua_setmetatable(l, -2);
+               // Add it to the reference table
+               lua_pushstring(l, key);
+               lua_pushvalue(l, -2);
+               lua_settable(l, -4);
+               // Remove StratagusReferences reference
+               lua_remove(l, -2);
+               DebugLevel3Fn("Creating ScriptProxy at %p for obj at %p.\n" _C_ 
lua_touserdata(l, -1) _C_ Object);
+       } else {
+               lua_remove(l, -2);
+               DebugLevel3Fn("Reusing ScriptProxy at %p for obj at %p.\n" _C_ 
lua_touserdata(l, -1) _C_ Object);
+       }
+}
+
 global int ScriptSetValueBlock(lua_State* l)
 {
        lua_pushstring(l, "Structure is read-only, sorry.\n");
@@ -1008,7 +1129,7 @@
 {
        lua_pushstring(Lua, "Stratagus");
 
-       /* Generate a weak table in the registry */
+       // Generate a weak table in the registry
        lua_pushstring(Lua, "StratagusReferences");
        lua_newtable(Lua);
        lua_newtable(Lua);
@@ -1017,8 +1138,22 @@
        lua_settable(Lua, -3);
        lua_setmetatable(Lua, -2);
        lua_settable(Lua, LUA_REGISTRYINDEX);
+
+       // Generate a standard metatable
+       lua_pushstring(Lua, "StratagusStandardMetatable");
+       lua_newtable(Lua);
+       lua_pushstring(Lua, "__index");
+       lua_pushcfunction(Lua, ScriptGet);
+       lua_settable(Lua, -3);
+       lua_pushstring(Lua, "__newindex");
+       lua_pushcfunction(Lua, ScriptSet);
+       lua_settable(Lua, -3);
+       lua_pushstring(Lua, "__gc");
+       lua_pushcfunction(Lua, ScriptCollectUserdata);
+       lua_settable(Lua, -3);
+       lua_settable(Lua, LUA_REGISTRYINDEX);
        
-       /* This is the main table, and the metatable for Stratagus. */
+       // This is the main table, and the metatable for Stratagus.
        lua_newtable(Lua);
        lua_newtable(Lua);
        lua_pushstring(Lua, "__index");
@@ -1194,7 +1329,7 @@
        }
 
        fprintf(fd, "--- -----------------------------------------\n");
-       fprintf(fd, "--- $Id: script.c,v 1.158 2004/01/17 22:23:26 nobody_ Exp 
$\n");
+       fprintf(fd, "--- $Id: script.c,v 1.159 2004/01/18 19:41:00 nobody_ Exp 
$\n");
 
        fprintf(fd, "SetVideoResolution(%d, %d)\n", VideoWidth, VideoHeight);
        fprintf(fd, "SetGroupKeys(\"");
@@ -1230,7 +1365,7 @@
        }
 
        fprintf(fd, "--- -----------------------------------------\n");
-       fprintf(fd, "--- $Id: script.c,v 1.158 2004/01/17 22:23:26 nobody_ Exp 
$\n");
+       fprintf(fd, "--- $Id: script.c,v 1.159 2004/01/18 19:41:00 nobody_ Exp 
$\n");
 
        // Global options
        if (OriginalFogOfWar) {
@@ -1333,7 +1468,7 @@
        extern SCM oblistvar;
 
        CLprintf(file, "\n;;; -----------------------------------------\n");
-       CLprintf(file, ";;; MODULE: CCL $Id: script.c,v 1.158 2004/01/17 
22:23:26 nobody_ Exp $\n\n");
+       CLprintf(file, ";;; MODULE: CCL $Id: script.c,v 1.159 2004/01/18 
19:41:00 nobody_ Exp $\n\n");
 
        for (list = oblistvar; gh_list_p(list); list = gh_cdr(list)) {
                SCM sym;
Index: stratagus/src/stratagus/script_spell.c
diff -u stratagus/src/stratagus/script_spell.c:1.37 
stratagus/src/stratagus/script_spell.c:1.38
--- stratagus/src/stratagus/script_spell.c:1.37 Sun Jan 18 11:52:19 2004
+++ stratagus/src/stratagus/script_spell.c      Mon Jan 19 06:41:00 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: script_spell.c,v 1.37 2004/01/18 00:52:19 nobody_ Exp $
+//     $Id: script_spell.c,v 1.38 2004/01/18 19:41:00 nobody_ Exp $
 //@{
 
 /*----------------------------------------------------------------------------
@@ -1013,34 +1013,31 @@
 
 #ifdef META_LUA
 
+       /// Get func for SpellType 
+local int ScriptSpellGet(SpellType* spell, const char* key, lua_State* l);
+       /// Set func for SpellType 
+local int ScriptSpellSet(SpellType* spell, const char* key, lua_State* l);
+
 //
 //     Functions directly acessible from lua. Placed in the stratagus 
namespace.
 //
 
 /**
 **  Create a new spell
-**
-**     @param l    Lua state, contains one string, the spell name.
-**
-**     @return The spell UserData in the stack.
 */
-local int ScriptCreateSpell(lua_State* l)
+local int ScriptSpellCreate(lua_State* l)
 {
        const char* name;
        SpellType* spell;
 
        if (lua_gettop(l) != 1) {
-               lua_pushstring(l, "incorrect argument");
-               lua_error(l);
-               return 0;
+               LuaError(l, "Incorrect number of arguments");
        }
        name = LuaToString(l, 1);
 
        spell = SpellTypeByIdent(name);
        if (spell != NULL) {
-               lua_pushstring(l, "Spell allready exists");
-               lua_error(l);
-               return 0;
+               LuaError(l, "Spell allready exists");
        } else {
                SpellTypeTable = realloc(SpellTypeTable, (1 + SpellTypeCount) * 
sizeof(SpellType*));
                spell = SpellTypeTable[SpellTypeCount++] = 
malloc(sizeof(SpellType));
@@ -1048,7 +1045,7 @@
                spell->Ident = SpellTypeCount - 1;
                spell->IdentName = strdup(name);
                spell->DependencyId = -1;
-               ScriptSpellCreateUserdata(l, spell);
+               ScriptCreateUserdata(l, spell, ScriptSpellGet, ScriptSpellSet);
                return 1;
        }
 }
@@ -1056,18 +1053,11 @@
 /**
 **     Get function for a spell userdata.
 **
-**     @param l        The Lua State.
+**     @param spell    Pointer to the spell.
+**     @param key      Key string.
 */
-local int ScriptSpellGetValue(lua_State* l)
+local int ScriptSpellGet(SpellType* spell, const char* key, lua_State* l)
 {
-       const SpellType* spell;
-       const char* key;
-
-       spell = *((SpellType**)lua_touserdata(l, -2));
-       key = lua_tostring(l, -1);
-       DebugCheck((!spell) || (!key));
-       DebugLevel3Fn("%p->(%s)\n" _C_ spell _C_ key);
-
        META_GET_STRING("DisplayName", spell->Name);
        META_GET_STRING("Ident", spell->IdentName);
        META_GET_INT("ManaCost", spell->ManaCost);
@@ -1091,27 +1081,19 @@
                DebugCheck(1);
        }
 
-       lua_pushfstring(l, "Field \"%s\" is innexistent or write-only (yes, we 
have those).\n", key);
-       lua_error(l);
-       return 0;
+       LuaError(l, "Field \"%s\" is innexistent or write-only (yes, we have 
those).\n" _C_ key);
 }
 
 /**
-**     Get function for a spell userdata.
+**     Set function for a spell userdata.
 **
-**     @param l        The Lua State.
+**     @param spell    Pointer to the spell.
+**     @param key      Key string.
 */
-local int ScriptSpellSetValue(lua_State* l)
+local int ScriptSpellSet(SpellType* spell, const char* key, lua_State* l)
 {
-       SpellType* spell;
-       const char* key;
        const char* val;
 
-       spell = *((SpellType**)lua_touserdata(l, -3));
-       key = LuaToString(l, -2);
-       DebugCheck((!spell) || (!key));
-       DebugLevel3Fn("%p->(%s)\n" _C_ spell _C_ key);
-
        META_SET_STRING("DisplayName", spell->Name);
        META_SET_INT("ManaCost", spell->ManaCost);
        META_SET_INT("Range", spell->Range);
@@ -1130,94 +1112,16 @@
                        spell->Target = TargetUnit;
                        return 0;
                }
-
-               lua_pushfstring(l, "Enum field \"%s\" can't receive value 
\"%s\"", key, val);
-               lua_error(l);
-               return 0;
+               LuaError(l, "Enum field \"%s\" can't receive value \"%s\"" _C_ 
key _C_ val);
        }
 
-       lua_pushfstring(l, "Field \"%s\" is innexistent or read-only.\n", key);
-       lua_error(l);
-       return 0;
-}
-
-/**
-**     Garbage collection for a spell
-**
-**     @param l         The lua state.
-*/
-global int ScriptSpellGCollect(lua_State* l)
-{
-       SpellType* spell;
-       char s[20];
-
-       spell = *((SpellType**)lua_touserdata(l, -1));
-       DebugLevel3Fn("Collecting ScriptData for a %s at %p.\n" _C_ "SpellType" 
_C_ spell->ScriptData);
-       lua_pushstring(l, "StratagusReferences");
-       lua_gettable(l, LUA_REGISTRYINDEX);
-       sprintf(s, "%p", spell->ScriptData); // FIXME: 64-bit.
-       lua_pushstring(l, s);
-       lua_pushnil(l);
-       lua_settable(l, -3);
-       lua_settop(l, -2);
-       spell->ScriptData = 0;
-       return 0;
-}
-
-/**
-**  Create a lua table for a spell.
-**
-**     @param l      The lua state.
-**     @param spell  Point to the spell.
-*/
-global void ScriptSpellCreateUserdata(lua_State* l, SpellType* spell)
-{
-       char s[20];
-       SpellType** sp;
-
-       if (spell->ScriptData) {
-               lua_pushstring(l, "StratagusReferences");
-               lua_gettable(l, LUA_REGISTRYINDEX);
-               sprintf(s, "%p", spell->ScriptData); // FIXME: 64-bit.
-               lua_pushstring(l, s);
-               lua_gettable(l, -2);
-               lua_remove(l, -2);
-               DebugLevel3Fn("Reusing ScriptData for a %s at %p.\n" _C_ 
"SpellType" _C_ lua_touserdata(l, -1));
-       } else {
-               // Create userdata.
-               sp = (SpellType**)lua_newuserdata(l, sizeof(SpellType));
-               *sp = spell;
-               spell->ScriptData = sp;
-               // Generate the metatable
-               lua_newtable(l);
-               lua_pushstring(l, "__index");
-               lua_pushcfunction(l, ScriptSpellGetValue);
-               lua_settable(l, -3);
-               lua_pushstring(l, "__newindex");
-               lua_pushcfunction(l, ScriptSpellSetValue);
-               lua_settable(l, -3);
-               lua_pushstring(l, "__gc");
-               lua_pushcfunction(l, ScriptSpellGCollect);
-               lua_settable(l, -3);
-               lua_setmetatable(l, -2);
-               // Add to weak ref table.
-               lua_pushstring(l, "StratagusReferences");
-               lua_gettable(l, LUA_REGISTRYINDEX);
-               sprintf(s, "%p", spell->ScriptData); // FIXME: 64-bit.
-               lua_pushstring(l, s);
-               lua_pushvalue(l, -3);
-               lua_settable(l, -3);
-               lua_remove(l, -1);
-               DebugLevel3Fn("Creating ScriptData for a %s at %p.\n" _C_ 
"SpellType" _C_ lua_touserdata(l, -1));
-       }
+       LuaError(l, "Field \"%s\" is innexistent or read-only.\n" _C_ key);
 }
 
 /**
-**     Get function for the big spell list.
-**
-**     @param l        The Lua State.
+**     Get function for the big spell namespace.
 */
-local int ScriptSpellNamespaceGetValue(lua_State* l)
+local int ScriptSpellNamespaceGet(lua_State* l)
 {
        int i;
        const char* key;
@@ -1228,11 +1132,9 @@
                i = LuaToNumber(l, 2);
                DebugLevel3Fn("(%d)\n" _C_ i);
                if (i < 0 || i >= SpellTypeCount) {
-                       lua_pushstring(l, "Spell index out of range");
-                       lua_error(l);
-                       return 0;
+                       LuaError(l, "Spell index out of range");
                }
-               ScriptSpellCreateUserdata(l, SpellTypeTable[i]);
+               ScriptCreateUserdata(l, SpellTypeTable[i], ScriptSpellGet, 
ScriptSpellSet);
                return 1;
        }
 
@@ -1243,21 +1145,16 @@
 
        META_GET_INT("n", SpellTypeCount);
 
-       spell = SpellTypeByIdent(key);
-       if (spell) {
-               ScriptSpellCreateUserdata(l, spell);
+       if ((spell = SpellTypeByIdent(key))) {
+               ScriptCreateUserdata(l, spell, ScriptSpellGet, ScriptSpellSet);
                return 1;
        }
 
-       lua_pushfstring(l, "Spell \"%s\" doesn't exist.\n", key);
-       lua_error(l);
-       return 0;
+       LuaError(l, "Spell \"%s\" doesn't exist.\n" _C_ key);
 }
 
 /**
 **     Initialize spell scripting. The main table is at -1
-**
-**     @param l   The lua state.
 */
 global void ScriptSpellInit(void)
 {
@@ -1269,7 +1166,7 @@
        // Generate the metatable
        lua_newtable(Lua);
        lua_pushstring(Lua, "__index");
-       lua_pushcfunction(Lua, ScriptSpellNamespaceGetValue);
+       lua_pushcfunction(Lua, ScriptSpellNamespaceGet);
        lua_settable(Lua, -3);
        lua_pushstring(Lua, "__newindex");
        lua_pushcfunction(Lua, ScriptSetValueBlock); // Read-Only
@@ -1278,7 +1175,7 @@
 
        // Add functions.
        lua_pushstring(Lua, "Create");
-       lua_pushcfunction(Lua, ScriptCreateSpell);
+       lua_pushcfunction(Lua, ScriptSpellCreate);
        lua_rawset(Lua, -3);
 
        lua_rawset(Lua, -3);
Index: stratagus/src/stratagus/spells.c
diff -u stratagus/src/stratagus/spells.c:1.138 
stratagus/src/stratagus/spells.c:1.139
--- stratagus/src/stratagus/spells.c:1.138      Sun Jan 18 09:23:26 2004
+++ stratagus/src/stratagus/spells.c    Mon Jan 19 06:41:00 2004
@@ -27,7 +27,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//     $Id: spells.c,v 1.138 2004/01/17 22:23:26 nobody_ Exp $
+//     $Id: spells.c,v 1.139 2004/01/18 19:41:00 nobody_ Exp $
 
 /*
 **             And when we cast our final spell
@@ -1279,6 +1279,7 @@
                if (spell->SoundWhenCast.Name) {
                        free(spell->SoundWhenCast.Name);
                }
+               free(spell);
                // FIXME: missile free somewhere else, right?
        }
        free(SpellTypeTable);
Index: stratagus/src/unit/unit.c
diff -u stratagus/src/unit/unit.c:1.364 stratagus/src/unit/unit.c:1.365
--- stratagus/src/unit/unit.c:1.364     Sun Jan 18 03:58:01 2004
+++ stratagus/src/unit/unit.c   Mon Jan 19 06:41:01 2004
@@ -26,7 +26,7 @@
 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //      02111-1307, USA.
 //
-//      $Id: unit.c,v 1.364 2004/01/17 16:58:01 nobody_ Exp $
+//      $Id: unit.c,v 1.365 2004/01/18 19:41:01 nobody_ Exp $
 
 //@{
 
@@ -3107,10 +3107,6 @@
        }
 
        //
-       //              FIXME: call others for help.
-       //
-
-       //
        //              Unit is working?
        //
        if (target->Orders[0].Action != UnitActionStill) {
@@ -3145,7 +3141,15 @@
        //              FIXME: Can't attack run away.
        //
        if (!type->Building) {
-               DebugLevel0Fn("FIXME: run away!\n");
+               int x;
+               int y;
+
+               x = target->X - attacker->X;
+               y = target->Y - attacker->Y;
+               x = target->X + (x * 5) / isqrt(x * x + y * y);
+               y = target->Y + (y * 5) / isqrt(x * x + y * y);
+               CommandStopUnit(target);
+               CommandMove(target, x + (SyncRand() & 3), y + (SyncRand() & 3), 
0);
        }
 }
 
@@ -3786,7 +3790,7 @@
        int j;
 
        CLprintf(file, "\n--- -----------------------------------------\n");
-       CLprintf(file, "--- MODULE: units $Id: unit.c,v 1.364 2004/01/17 
16:58:01 nobody_ Exp $\n\n");
+       CLprintf(file, "--- MODULE: units $Id: unit.c,v 1.365 2004/01/18 
19:41:01 nobody_ Exp $\n\n");
 
 #if 0
        //




reply via email to

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