Author: crab
Date: Thu Mar 24 20:59:50 2011
New Revision: 48991

URL: http://svn.gna.org/viewcvs/wesnoth?rev=48991&view=rev
Log:
patch #2599 by nephro: lua_ai routine:  ai.get_aggression()

Modified:
    trunk/data/ai/scenarios/scenario-lua-ai.cfg
    trunk/data/core/about.cfg
    trunk/src/ai/lua/core.cpp

Modified: trunk/data/ai/scenarios/scenario-lua-ai.cfg
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/ai/scenarios/scenario-lua-ai.cfg?rev=48991&r1=48990&r2=48991&view=diff
==============================================================================
--- trunk/data/ai/scenarios/scenario-lua-ai.cfg (original)
+++ trunk/data/ai/scenarios/scenario-lua-ai.cfg Thu Mar 24 20:59:50 2011
@@ -232,6 +232,7 @@
 
 
 function my_ai:do_moves()
+          wesnoth.message('Aggression: ' .. ai.get_aggression())
        my_leader = wesnoth.get_units({canrecruit = true, side = ai.side})[1]
        --! move (partial move)
        ai.move(my_leader,13, 22)

Modified: trunk/data/core/about.cfg
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/core/about.cfg?rev=48991&r1=48990&r2=48991&view=diff
==============================================================================
--- trunk/data/core/about.cfg (original)
+++ trunk/data/core/about.cfg Thu Mar 24 20:59:50 2011
@@ -911,6 +911,9 @@
         name= "Derek (Gambit)"
     [/entry]
     [entry]
+        name= "Dmitry K. (nephro)"
+    [/entry]
+    [entry]
         name = "Doug Rosvick (dlr365)"
         wikiuser = "dlr365"
     [/entry]

Modified: trunk/src/ai/lua/core.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/core.cpp?rev=48991&r1=48990&r2=48991&view=diff
==============================================================================
--- trunk/src/ai/lua/core.cpp (original)
+++ trunk/src/ai/lua/core.cpp Thu Mar 24 20:59:50 2011
@@ -61,6 +61,15 @@
        lua_rawset(L, LUA_REGISTRYINDEX);
 }
 
+static ai::engine_lua &get_engine(lua_State *L)
+{
+       return *((ai::engine_lua*)lua_touserdata(L, lua_upvalueindex(1)));
+}
+
+static ai::readonly_context &get_readonly_context(lua_State *L)
+{
+       return get_engine(L).get_readonly_context();
+}
 
 static int transform_ai_action(lua_State *L, ai::action_result_ptr 
action_result)
 {
@@ -104,7 +113,7 @@
                return luaL_typerror(L, index, "location (unit/integers)");
        }
 
-       int side = 
((ai::engine_lua*)lua_touserdata(L,lua_upvalueindex(1)))->get_readonly_context().get_side();
+       int side = get_readonly_context(L).get_side();
        map_location from, to;
        if (!to_map_location(L, index, from)) goto error_call_destructors;
        if (!to_map_location(L, index, to)) goto error_call_destructors;
@@ -130,7 +139,7 @@
                return luaL_typerror(L, index, "location (unit/integers)");
        }
 
-       ai::readonly_context &context = 
((ai::engine_lua*)lua_touserdata(L,lua_upvalueindex(1)))->get_readonly_context();
+       ai::readonly_context &context = get_readonly_context(L);
 
        int side = context.get_side();
        map_location attacker, defender;
@@ -160,7 +169,7 @@
                return luaL_typerror(L, index, "location (unit/integers)");
        }
 
-       int side = 
((ai::engine_lua*)lua_touserdata(L,lua_upvalueindex(1)))->get_readonly_context().get_side();
+       int side = get_readonly_context(L).get_side();
        map_location loc;
        if (!to_map_location(L, index, loc)) goto error_call_destructors;
 
@@ -186,7 +195,7 @@
 static int cfun_ai_execute_recruit(lua_State *L)
 {
        const char *unit_name = luaL_checkstring(L, 1);
-       int side = 
((ai::engine_lua*)lua_touserdata(L,lua_upvalueindex(1)))->get_readonly_context().get_side();
+       int side = get_readonly_context(L).get_side();
        map_location where;
        if (!lua_isnoneornil(L, 2)) {
                where.x = lua_tonumber(L, 2) - 1;
@@ -200,7 +209,7 @@
 static int cfun_ai_execute_recall(lua_State *L)
 {
        const char *unit_id = luaL_checkstring(L, 1);
-       int side = 
((ai::engine_lua*)lua_touserdata(L,lua_upvalueindex(1)))->get_readonly_context().get_side();
+       int side = get_readonly_context(L).get_side();
        map_location where;
        if (!lua_isnoneornil(L, 2)) {
                where.x = lua_tonumber(L, 2) - 1;
@@ -211,6 +220,13 @@
        return transform_ai_action(L,recall_result);
 }
 
+static int cfun_ai_get_aggression(lua_State *L)
+{
+       double aggression = get_readonly_context(L).get_aggression();
+       lua_pushnumber(L, aggression);
+       return 1;
+}
+
 lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, 
ai::engine_lua *engine)
 {
        int res_ai = luaL_loadstring(L, code);//stack size is now 1 [ -1: 
ai_context]
@@ -230,6 +246,7 @@
 
        static luaL_reg const callbacks[] = {
                { "attack",           &cfun_ai_execute_attack           },
+               { "get_aggression",   &cfun_ai_get_aggression           },
                { "move",             &cfun_ai_execute_move_partial     },
                { "move_full",        &cfun_ai_execute_move_full        },
                { "recall",           &cfun_ai_execute_recall           },


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to