Author: nephro
Date: Fri Jun 10 17:17:17 2011
New Revision: 49827

URL: http://svn.gna.org/viewcvs/wesnoth?rev=49827&view=rev
Log:
More aspect exposure. @Crab_, please, leave feedback on this, I am unsure about 
several parts of this patch

Modified:
    trunk/data/ai/scenarios/scenario-lua-ai.cfg
    trunk/src/ai/lua/core.cpp
    trunk/src/ai/lua/lua_object.hpp
    trunk/src/ai/registry.cpp
    trunk/src/scripting/lua.cpp
    trunk/src/scripting/lua_api.hpp

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=49827&r1=49826&r2=49827&view=diff
==============================================================================
--- trunk/data/ai/scenarios/scenario-lua-ai.cfg (original)
+++ trunk/data/ai/scenarios/scenario-lua-ai.cfg Fri Jun 10 17:17:17 2011
@@ -247,9 +247,19 @@
                [aspect]
                        id=recruitment_pattern
                        engine=lua
-                       value="'fighter','fighter'"
-               [/aspect]
-               
+                       value=<< {'fighter','scout'} >>
+               [/aspect]
+
+               [aspect]
+                       id=leader_goal
+                       engine=lua
+                       value=<< {x=14, y=10} >>
+               [/aspect]
+               [aspect]
+                       id=avoid
+                       engine=lua
+                       value=<< { x = '2-4', y = '23-25'} >>
+               [/aspect]
             version=10710
             [engine]
                 name="lua"
@@ -288,21 +298,22 @@
 
 
 function my_ai:do_moves()
-
-       wesnoth.message('Passive leader: ', tostring(ai.get_passive_leader()))
-       wesnoth.message('Passive leader shares keep: ', 
tostring(ai.get_passive_leader_shares_keep()))
-       wesnoth.message('Simple targeting: ', 
tostring(ai.get_simple_targeting()))
-       wesnoth.message('Scout village targ.: ' , 
ai.get_scout_village_targeting())
-       wesnoth.message('Recruit. ignore bc: ' , 
tostring(ai.get_recruitment_ignore_bad_combat()))
-       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())
-       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)
+       --wesnoth.message('Passive leader: ', tostring(ai.get_passive_leader()))
+       --wesnoth.message('Passive leader shares keep: ', 
tostring(ai.get_passive_leader_shares_keep()))
+       --wesnoth.message('Simple targeting: ', 
tostring(ai.get_simple_targeting()))
+       --wesnoth.message('Scout village targ.: ' , 
ai.get_scout_village_targeting())
+       --wesnoth.message('Recruit. ignore bc: ' , 
tostring(ai.get_recruitment_ignore_bad_combat()))
+       --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())
+       --local rp = ai.get_recruitment_pattern() -- rp is used like a simple 
array
+       --wesnoth.message('pattern'.. rp[1] .. rp[2])
+       --wesnoth.message('TOOD: ' .. wesnoth.get_time_of_day().id)
+       wesnoth.message('Leader goal' .. ai.get_leader_goal().x .. ' ' .. 
ai.get_leader_goal().y) -- fixed
+       wesnoth.message('Avoid ex. ' .. #ai.get_avoid() .. ' ' 
..ai.get_avoid()[1]["x"] .. " " .. ai.get_avoid()[1]["y"]) -- TOFIX
        
        my_leader = wesnoth.get_units({canrecruit = true, side = ai.side})[1]
        x,y = ai.suitable_keep(my_leader)

Modified: trunk/src/ai/lua/core.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/core.cpp?rev=49827&r1=49826&r2=49827&view=diff
==============================================================================
--- trunk/src/ai/lua/core.cpp (original)
+++ trunk/src/ai/lua/core.cpp Fri Jun 10 17:17:17 2011
@@ -43,9 +43,11 @@
 #include "../../play_controller.hpp"
 #include "../../resources.hpp"
 #include "../../terrain_translation.hpp"
+#include "../../terrain_filter.hpp"
 #include "../../unit.hpp"
 #include "../actions.hpp"
 #include "../composite/engine_lua.hpp"
+#include <lua/llimits.h>
 
 static lg::log_domain log_ai_engine_lua("ai/engine/lua");
 #define LOG_LUA LOG_STREAM(info, log_ai_engine_lua)
@@ -263,10 +265,34 @@
        return 1;
 }
 
-//static int cfun_ai_get_avoid(lua_State *L)
-//{
-//     return 1;
-//}
+static int cfun_ai_get_avoid(lua_State *L)
+{
+       std::set<map_location> locs;
+       terrain_filter avoid = get_readonly_context(L).get_avoid();     
+       avoid.get_locations(locs, true); // is it true here?
+       
+       int sz = locs.size();
+       lua_createtable(L, sz, 0); // create a table that we'll use as an array 
+       
+       std::set<map_location>::iterator it = locs.begin();     
+       for (int i = 0; it != locs.end(); ++it, ++i)
+       {
+               lua_pushinteger(L, i + 1); // Index for the map location
+               lua_createtable(L, 2, 0); // Table for a single map location
+               
+               lua_pushstring(L, "x");         
+               lua_pushinteger(L, it->x);              
+               lua_settable(L, -3);
+               
+               lua_pushstring(L, "y");
+               lua_pushinteger(L, it->y);
+               lua_settable(L, -3);            
+               
+               lua_settable(L, -3);
+       }
+       
+       return 1;
+}
 
 static int cfun_ai_get_caution(lua_State *L)
 {
@@ -286,6 +312,13 @@
 {
        double leader_aggression = 
get_readonly_context(L).get_leader_aggression();
        lua_pushnumber(L, leader_aggression);
+       return 1;
+}
+
+static int cfun_ai_get_leader_goal(lua_State *L)
+{
+       config goal = get_readonly_context(L).get_leader_goal();
+       luaW_pushconfig(L, goal); 
        return 1;
 }
 
@@ -402,10 +435,12 @@
                { "attack",                     &cfun_ai_execute_attack         
        },
                // Aspects
                { "get_aggression",             &cfun_ai_get_aggression         
        },
+               { "get_avoid",                  &cfun_ai_get_avoid              
        },
                { "get_attack_depth",           &cfun_ai_get_attack_depth       
        }, // { "get_", &cfun_ai_get_}, little template # TODELETE
                { "get_caution",                &cfun_ai_get_caution            
        },
                { "get_grouping",               &cfun_ai_get_grouping           
        },
                { "get_leader_aggression",      &cfun_ai_get_leader_aggression  
        },
+               { "get_leader_goal",            &cfun_ai_get_leader_goal        
        },
                { "get_leader_value",           &cfun_ai_get_leader_value       
        },
                { "get_number_of_possible_recruits_to_force_recruit", 
&cfun_ai_get_number_of_possible_recruits_to_force_recruit},
                { "get_passive_leader",         &cfun_ai_get_passive_leader     
        },

Modified: trunk/src/ai/lua/lua_object.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/lua_object.hpp?rev=49827&r1=49826&r2=49827&view=diff
==============================================================================
--- trunk/src/ai/lua/lua_object.hpp (original)
+++ trunk/src/ai/lua/lua_object.hpp Fri Jun 10 17:17:17 2011
@@ -27,6 +27,10 @@
 #include <boost/shared_ptr.hpp>
 
 #include "lua/lualib.h"
+#include "../../scripting/lua_api.hpp"
+#include "config.hpp"
+#include "terrain_filter.hpp"
+#include "resources.hpp"
 
 namespace ai {
 
@@ -99,18 +103,36 @@
 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)) 
+       int l = lua_objlen(L, n);
+       for (int i = 1; i < l + 1; ++i) 
        {
-               --top; // Don't take nils from the top of the stack
+               lua_pushinteger(L, i);
+               lua_gettable(L, n);
+               std::string  s = lua_tostring(L, -1);
+               lua_settop(L, n);
+               v->push_back(s);
        }
        
-       for (int i = n; i <= top; ++i) 
-       {
-               v->push_back(lua_tostring(L, i));
-       }
        return v;
+}
+
+template <>
+inline boost::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int 
n)
+{
+       boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
+       luaW_toconfig(L, n, *cfg);
+       return cfg;
+}
+
+template <>
+inline boost::shared_ptr<terrain_filter> 
lua_object<terrain_filter>::to_type(lua_State *L, int n)
+{
+       // To Crab_: Is this part ok? I tested it, works fine
+       boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
+       boost::shared_ptr<vconfig> vcfg = boost::shared_ptr<vconfig>(new 
vconfig(*cfg));
+       luaW_tovconfig(L, n, *vcfg);
+       boost::shared_ptr<terrain_filter> tf = 
boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, *resources::units));
+       return tf;
 }
 
 } // end of namespace ai

Modified: trunk/src/ai/registry.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/registry.cpp?rev=49827&r1=49826&r2=49827&view=diff
==============================================================================
--- trunk/src/ai/registry.cpp (original)
+++ trunk/src/ai/registry.cpp Fri Jun 10 17:17:17 2011
@@ -364,6 +364,9 @@
 
 static register_lua_aspect_factory< lua_aspect<int> >
        attack_depth__lua_aspect_factory("attack_depth*lua_aspect");
+       
+static register_lua_aspect_factory< lua_aspect<terrain_filter> >
+       avoid__lua_aspect_factory("avoid*lua_aspect");
 
 static register_lua_aspect_factory< lua_aspect<double> >
        caution__lua_aspect_factory("caution*lua_aspect");
@@ -373,6 +376,9 @@
 
 static register_lua_aspect_factory< lua_aspect<double> >
        leader_aggression__lua_aspect_factory("leader_aggression*lua_aspect");
+       
+static register_lua_aspect_factory< lua_aspect<config> >
+       leader_goal__lua_aspect_factory("leader_goal*lua_aspect");
 
 static register_lua_aspect_factory< lua_aspect<double> >
        leader_value__lua_aspect_factory("leader_value*lua_aspect");

Modified: trunk/src/scripting/lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=49827&r1=49826&r2=49827&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Fri Jun 10 17:17:17 2011
@@ -377,7 +377,7 @@
  * Gets an optional vconfig from either a table or a userdata.
  * @return false in case of failure.
  */
-static bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
+bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
 {
        switch (lua_type(L, index))
        {

Modified: trunk/src/scripting/lua_api.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua_api.hpp?rev=49827&r1=49826&r2=49827&view=diff
==============================================================================
--- trunk/src/scripting/lua_api.hpp (original)
+++ trunk/src/scripting/lua_api.hpp Fri Jun 10 17:17:17 2011
@@ -20,12 +20,14 @@
 
 struct lua_State;
 class config;
+class vconfig;
 class unit;
 
 bool luaW_pcall(lua_State *L , int nArgs, int nRets, bool allow_wml_error = 
false);
 unit *luaW_tounit(lua_State *L, int index, bool only_on_map = false);
 void luaW_pushconfig(lua_State *L, config const &cfg);
 bool luaW_toconfig(lua_State *L, int index, config &cfg, int tstring_meta = 0);
+bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg);
 
 /**
  * Storage for a unit, either owned by the Lua code (#ptr != 0), on a


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

Reply via email to