mail[Wesnoth-commits] r44312 - in /trunk: data/lua/compatibility-1.8.lua src/scripting/lua.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by guillaume . melquiond on July 18, 2010 - 19:38:
Author: silene
Date: Sun Jul 18 19:38:35 2010
New Revision: 44312

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44312&view=rev
Log:
Removed wesnoth.get_unit_type* functions from engine.
Implemented unit_type proxies in an associative table.
Added backward-compatible functions.

Modified:
    trunk/data/lua/compatibility-1.8.lua
    trunk/src/scripting/lua.cpp

Modified: trunk/data/lua/compatibility-1.8.lua
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/lua/compatibility-1.8.lua?rev=44312&r1=44311&r2=44312&view=diff
==============================================================================
--- trunk/data/lua/compatibility-1.8.lua (original)
+++ trunk/data/lua/compatibility-1.8.lua Sun Jul 18 19:38:35 2010
@@ -8,3 +8,16 @@
 function wesnoth.get_side_count(i)
        return #wesnoth.sides
 end
+
+function wesnoth.get_unit_type_ids()
+       local t = {}
+       for k,v in pairs(wesnoth.unit_types) do
+               table.insert(t, k)
+       end
+       table.sort(t)
+       return t
+end
+
+function wesnoth.get_unit_type(t)
+       return wesnoth.unit_types[t]
+end

Modified: trunk/src/scripting/lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=44312&r1=44311&r2=44312&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Sun Jul 18 19:38:35 2010
@@ -729,42 +729,6 @@
        return_int_attrib("level", ut.level());
        return_cfgref_attrib("__cfg", ut.get_cfg());
        return 0;
-}
-
-/**
- * Gets the unit type corresponding to an id.
- * - Arg 1: string containing the unit type id.
- * - Ret 1: table with an "id" field and with __index pointing to 
lua_unit_type_get.
- */
-static int intf_get_unit_type(lua_State *L)
-{
-       char const *m = luaL_checkstring(L, 1);
-       if (!unit_types.unit_type_exists(m)) return 0;
-
-       lua_createtable(L, 0, 1);
-       lua_pushvalue(L, 1);
-       lua_setfield(L, -2, "id");
-       lua_pushlightuserdata(L, (void *)&gettypeKey);
-       lua_rawget(L, LUA_REGISTRYINDEX);
-       lua_setmetatable(L, -2);
-       return 1;
-}
-
-/**
- * Gets the ids of all the unit types.
- * - Ret 1: table containing the ids.
- */
-static int intf_get_unit_type_ids(lua_State *L)
-{
-       lua_newtable(L);
-       int i = 1;
-       foreach (const unit_type_data::unit_type_map::value_type &ut, 
unit_types.types())
-       {
-               lua_pushstring(L, ut.first.c_str());
-               lua_rawseti(L, -2, i);
-               ++i;
-       }
-       return 1;
 }
 
 /**
@@ -2255,8 +2219,6 @@
                { "get_selected_tile",        &intf_get_selected_tile        
},
                { "get_terrain",              &intf_get_terrain              
},
                { "get_terrain_info",         &intf_get_terrain_info         
},
-               { "get_unit_type",            &intf_get_unit_type            
},
-               { "get_unit_type_ids",        &intf_get_unit_type_ids        
},
                { "get_units",                &intf_get_units                
},
                { "get_variable",             &intf_get_variable             
},
                { "get_village_owner",        &intf_get_village_owner        
},
@@ -2418,10 +2380,10 @@
 void LuaKernel::initialize()
 {
        lua_State *L = mState;
+       lua_getglobal(L, "wesnoth");
 
        // Create the sides table.
        std::vector<team> &teams = *resources::teams;
-       lua_getglobal(L, "wesnoth");
        lua_pushlightuserdata(L, (void *)&getsideKey);
        lua_rawget(L, LUA_REGISTRYINDEX);
        lua_createtable(L, teams.size(), 0);
@@ -2435,6 +2397,22 @@
                lua_rawseti(L, -2, i + 1);
        }
        lua_setfield(L, -3, "sides");
+       lua_pop(L, 1);
+
+       // Create the unit_types table.
+       lua_pushlightuserdata(L, (void *)&gettypeKey);
+       lua_rawget(L, LUA_REGISTRYINDEX);
+       lua_newtable(L);
+       foreach (const unit_type_data::unit_type_map::value_type &ut, 
unit_types.types())
+       {
+               lua_createtable(L, 0, 1);
+               lua_pushstring(L, ut.first.c_str());
+               lua_setfield(L, -2, "id");
+               lua_pushvalue(L, -3);
+               lua_setmetatable(L, -2);
+               lua_setfield(L, -2, ut.first.c_str());
+       }
+       lua_setfield(L, -3, "unit_types");
        lua_pop(L, 2);
 
        // Execute the preload scripts.




Related Messages


Powered by MHonArc, Updated Sun Jul 18 20:20:29 2010