Author: nephro
Date: Wed Jun  8 12:09:29 2011
New Revision: 49792

URL: http://svn.gna.org/viewcvs/wesnoth?rev=49792&view=rev
Log:
Exposed recruitment pattern aspect to Lua

Modified:
    trunk/data/ai/scenarios/scenario-lua-ai.cfg
    trunk/src/ai/composite/aspect.hpp
    trunk/src/ai/lua/core.cpp
    trunk/src/ai/lua/lua_object.hpp
    trunk/src/ai/registry.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=49792&r1=49791&r2=49792&view=diff
==============================================================================
--- trunk/data/ai/scenarios/scenario-lua-ai.cfg (original)
+++ trunk/data/ai/scenarios/scenario-lua-ai.cfg Wed Jun  8 12:09:29 2011
@@ -244,6 +244,12 @@
                        engine=lua
                        value="'defensive'"
                [/aspect]
+               [aspect]
+                       id=recruitment_pattern
+                       engine=lua
+                       value="'fighter','fighter'"
+               [/aspect]
+               
             version=10710
             [engine]
                 name="lua"
@@ -291,10 +297,12 @@
        wesnoth.message('Recruit. ignore bm: ' , 
tostring(ai.get_recruitment_ignore_bad_movement()))
        wesnoth.message('nofprtfr: ' , 
ai.get_number_of_possible_recruits_to_force_recruit())
        wesnoth.message('Grouping: ' .. ai.get_grouping())
-wesnoth.message('Support villages: ' .. tostring(ai.get_support_villages()))
-wesnoth.message('Village value: ' .. ai.get_village_value())
-wesnoth.message('Villages per scout: ' .. ai.get_villages_per_scout())
-wesnoth.message('TOOD: ' .. wesnoth.get_time_of_day().id)
+       wesnoth.message('Support villages: ' .. 
tostring(ai.get_support_villages()))
+       wesnoth.message('Village value: ' .. ai.get_village_value())
+       wesnoth.message('Villages per scout: ' .. ai.get_villages_per_scout())
+       local rp = ai.get_recruitment_pattern() -- rp is used like a simple 
array
+       wesnoth.message('pattern'.. #rp)
+       wesnoth.message('TOOD: ' .. wesnoth.get_time_of_day().id)
        
        my_leader = wesnoth.get_units({canrecruit = true, side = ai.side})[1]
        x,y = ai.suitable_keep(my_leader)

Modified: trunk/src/ai/composite/aspect.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/aspect.hpp?rev=49792&r1=49791&r2=49792&view=diff
==============================================================================
--- trunk/src/ai/composite/aspect.hpp (original)
+++ trunk/src/ai/composite/aspect.hpp Wed Jun  8 12:09:29 2011
@@ -410,7 +410,7 @@
                        {
                                value = "true";
                        }
-                       value = "return (" + value + ")";
+                       value = "return " + value;
                } 
                else if (cfg.has_attribute("code")) 
                {       

Modified: trunk/src/ai/lua/core.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/core.cpp?rev=49792&r1=49791&r2=49792&view=diff
==============================================================================
--- trunk/src/ai/lua/core.cpp (original)
+++ trunk/src/ai/lua/core.cpp Wed Jun  8 12:09:29 2011
@@ -331,6 +331,20 @@
        return 1;
 }
 
+static int cfun_ai_get_recruitment_pattern(lua_State *L)
+{
+       std::vector<std::string> recruiting = 
get_readonly_context(L).get_recruitment_pattern();
+       int size = recruiting.size();
+       lua_createtable(L, size, 0); // create an exmpty table with predefined 
size
+       for (int i = 0; i < size; ++i)
+       {
+               lua_pushinteger(L, i + 1); // Indexing in Lua starts from 1
+               lua_pushstring(L, recruiting[i].c_str());
+               lua_settable(L, -3);
+       }
+       return 1;
+}
+
 static int cfun_ai_get_scout_village_targeting(lua_State *L)
 {
        double scout_village_targeting = 
get_readonly_context(L).get_scout_village_targeting();
@@ -398,6 +412,7 @@
                { "get_passive_leader_shares_keep", 
&cfun_ai_get_passive_leader_shares_keep},
                { "get_recruitment_ignore_bad_combat", 
&cfun_ai_get_recruitment_ignore_bad_combat},
                { "get_recruitment_ignore_bad_movement", 
&cfun_ai_get_recruitment_ignore_bad_movement},
+               { "get_recruitment_pattern",    
&cfun_ai_get_recruitment_pattern        },
                { "get_scout_village_targeting", 
&cfun_ai_get_scout_village_targeting   },
                { "get_simple_targeting",       &cfun_ai_get_simple_targeting   
        },
                { "get_support_villages",       &cfun_ai_get_support_villages   
        },
@@ -498,9 +513,9 @@
                luaW_pushconfig(L, cfg);
                luaW_pcall(L, 2, 0, true);
        }
-       else if (luaW_pcall(L, 1, 2, true))
-       {
-               l_obj->store(L, initial_top + 1);
+       else if (luaW_pcall(L, 1, 5, true)) // @note for Crab: how much nrets 
should we actually have here
+       {                                   // there were 2 initially, but 
aspects like recruitment pattern
+               l_obj->store(L, initial_top + 1); // return a lot of results
        }
 
        lua_settop(L, initial_top);//empty stack

Modified: trunk/src/ai/lua/lua_object.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/lua_object.hpp?rev=49792&r1=49791&r2=49792&view=diff
==============================================================================
--- trunk/src/ai/lua/lua_object.hpp (original)
+++ trunk/src/ai/lua/lua_object.hpp Wed Jun  8 12:09:29 2011
@@ -23,6 +23,7 @@
 #define LUA_OBJECT_HPP_INCLUDED
 
 #include <string>
+#include <vector>
 #include <boost/shared_ptr.hpp>
 
 #include "lua/lualib.h"
@@ -94,6 +95,24 @@
        return boost::shared_ptr<int>(new int(lua_tonumber(L, n)));
 }
 
+template <>
+inline boost::shared_ptr< std::vector<std::string> > lua_object< 
std::vector<std::string> >::to_type(lua_State *L, int n)
+{
+       boost::shared_ptr< std::vector<std::string> > v = boost::shared_ptr< 
std::vector<std::string> >(new std::vector<std::string>());
+       int top = lua_gettop(L);
+       
+       while (lua_isnil(L, top)) 
+       {
+               --top; // Don't take nils from the top of the stack
+       }
+       
+       for (int i = n; i <= top; ++i) 
+       {
+               v->push_back(lua_tostring(L, i));
+       }
+       return v;
+}
+
 } // end of namespace ai
 
 

Modified: trunk/src/ai/registry.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/registry.cpp?rev=49792&r1=49791&r2=49792&view=diff
==============================================================================
--- trunk/src/ai/registry.cpp (original)
+++ trunk/src/ai/registry.cpp Wed Jun  8 12:09:29 2011
@@ -406,6 +406,9 @@
 
 static register_lua_aspect_factory< lua_aspect<int> >
        villages_per_scout__lua_aspect_factory("villages_per_scout*lua_aspect");
+       
+static register_lua_aspect_factory< lua_aspect< std::vector<std::string> > >
+       
recruitment_pattern__lua_aspect_factory("recruitment_pattern*lua_aspect");
 
 void registry::init()
 {


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

Reply via email to