Author: nephro
Date: Sat Aug  6 18:06:24 2011
New Revision: 50622

URL: http://svn.gna.org/viewcvs/wesnoth?rev=50622&view=rev
Log:
Lua backed goals now available(docs to be updated tomorrow)  //some minor fixes

Modified:
    trunk/data/ai/scenarios/scenario-lua-ai.cfg
    trunk/src/ai/composite/engine_lua.cpp
    trunk/src/ai/composite/engine_lua.hpp
    trunk/src/ai/composite/goal.cpp
    trunk/src/ai/composite/goal.hpp
    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=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/data/ai/scenarios/scenario-lua-ai.cfg (original)
+++ trunk/data/ai/scenarios/scenario-lua-ai.cfg Sat Aug  6 18:06:24 2011
@@ -290,6 +290,17 @@
                                x,y=42,20
                        [/criteria]
                [/goal]
+               [goal]
+                       name=lua_goal
+                       value=6
+                       engine=lua
+                       code = <<
+                               local t = {}
+                               t[1] = {["value"]=2.3, ["type"]=3, 
["loc"]={["x"]=5, ["y"]=6}}
+                               t[2] = {["value"]=2.4, ["type"]=4, 
["loc"]={["x"]=4, ["y"]=16}}
+                               return t
+                       >>
+               [/goal]
                [aspect]
                        id=aggression
                        engine=lua
@@ -311,10 +322,15 @@
 
 
 function my_ai:stage_hello()
-       wesnoth.message('hello from stage!')
+       --wesnoth.message('hello from stage!')
        --wesnoth.message('PERSISTANCE: ' .. tostring(self.data["pers"]) .. 
self.data["stringg"])
        local tg = ai.get_targets()
-       wesnoth.message(tg[3].type .. " " .. tg[3].value .. " " .. tg[3].loc.x 
.. " " ..tg[3].loc.y)
+       for k,v in pairs(tg) do
+               if v.type==3 or v.type==4 then
+                       wesnoth.message(tg[k].type .. " " .. tg[k].value .. " " 
.. tg[k].loc.x .. " " ..tg[k].loc.y)
+               end
+       end
+       
 end
 
 function my_ai:candidate_action_evaluation_hello()
@@ -325,6 +341,7 @@
 function my_ai:candidate_action_execution_hello(cfg)
       -- wesnoth.message('hello from candidate action execution!')
        --wesnoth.message('test value is ') -- .. cfg.test) -- ERROR: cfg.test 
is nil here
+       
        self:do_moves()
 end
 

Modified: trunk/src/ai/composite/engine_lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/engine_lua.cpp?rev=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/src/ai/composite/engine_lua.cpp (original)
+++ trunk/src/ai/composite/engine_lua.cpp Sat Aug  6 18:06:24 2011
@@ -20,6 +20,7 @@
 
 #include "ai.hpp"
 #include "engine_lua.hpp"
+#include "goal.hpp"
 #include "rca.hpp"
 #include "stage.hpp"
 #include "aspect.hpp"
@@ -258,6 +259,24 @@
        *b = new_aspect;
 }
 
+void engine_lua::do_parse_goal_from_config(const config &cfg, 
std::back_insert_iterator<std::vector< goal_ptr > > b )
+{
+       goal_factory::factory_map::iterator f = 
goal_factory::get_list().find(cfg["name"]);
+       if (f == goal_factory::get_list().end()){
+               ERR_AI_LUA << "side "<<ai_.get_side()<< " : UNKNOWN 
goal["<<cfg["name"]<<"]"<< std::endl;
+               DBG_AI_LUA << "config snippet contains: " << std::endl << cfg 
<< std::endl;
+               return;
+       }
+       goal_ptr new_goal = f->second->get_new_instance(ai_,cfg);
+       new_goal->on_create(lua_ai_context_);
+       if (!new_goal) {
+               ERR_AI_LUA << "side "<<ai_.get_side()<< " : UNABLE TO CREATE 
goal["<<cfg["name"]<<"]"<< std::endl;
+               DBG_AI_LUA << "config snippet contains: " << std::endl << cfg 
<< std::endl;
+               return;
+       }
+       *b = new_goal;
+}
+
 
 std::string engine_lua::evaluate(const std::string &/*str*/)
 {

Modified: trunk/src/ai/composite/engine_lua.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/engine_lua.hpp?rev=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/src/ai/composite/engine_lua.hpp (original)
+++ trunk/src/ai/composite/engine_lua.hpp Sat Aug  6 18:06:24 2011
@@ -50,7 +50,9 @@
         * Taka a config (with engine=lua in it)
         * and parse several (usually, 1) aspects out of it
         */
-       void do_parse_aspect_from_config( const config &cfg, const std::string 
&id, std::back_insert_iterator<std::vector< aspect_ptr > > b );
+       virtual void do_parse_aspect_from_config( const config &cfg, const 
std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b );
+       
+       virtual void do_parse_goal_from_config(const config &cfg, 
std::back_insert_iterator<std::vector< goal_ptr > > b );
 
        virtual std::string evaluate(const std::string &str);
 

Modified: trunk/src/ai/composite/goal.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/goal.cpp?rev=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/src/ai/composite/goal.cpp (original)
+++ trunk/src/ai/composite/goal.cpp Sat Aug  6 18:06:24 2011
@@ -19,11 +19,14 @@
  */
 
 #include "goal.hpp"
+#include "../lua/core.hpp"
 #include "../manager.hpp"
 #include "../../log.hpp"
+#include "../lua/lua_object.hpp"
 #include "../../gamestatus.hpp"
 #include "../../foreach.hpp"
 #include "../../resources.hpp"
+#include "../../scripting/lua.hpp"
 #include "../../terrain_filter.hpp"
 #include "../../unit.hpp"
 #include "../../unit_map.hpp"
@@ -48,6 +51,10 @@
 
 
 void goal::on_create()
+{
+}
+
+void goal::on_create(boost::shared_ptr<ai::lua_ai_context>)
 {
 }
 
@@ -291,5 +298,38 @@
 {
 }
 
+lua_goal::lua_goal(readonly_context &context, const config &cfg)
+: goal(context, cfg) 
+{
+       if (cfg.has_attribute("code")) {
+               code_ = cfg["code"].str();
+       }
+       else 
+       {
+               // report failure
+       }
+}
+
+void lua_goal::on_create(boost::shared_ptr<ai::lua_ai_context> l_ctx)
+{
+       handler_ = 
boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(),
 *l_ctx));
+}
+
+void lua_goal::add_targets(std::back_insert_iterator< std::vector< target > > 
target_list)
+{
+       boost::shared_ptr< lua_object< std::vector < target > > > l_obj 
+               = boost::shared_ptr< lua_object< std::vector < target > > >(new 
lua_object< std::vector < target > >());
+       config c = config();
+       handler_->handle(c, true, l_obj);
+       std::vector < target > targets = *(l_obj->get());
+       
+       foreach (target tg, targets)
+       {
+               *target_list = tg;
+       }
+       
+}
+
+
 
 } //end of namespace ai

Modified: trunk/src/ai/composite/goal.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/goal.hpp?rev=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/src/ai/composite/goal.hpp (original)
+++ trunk/src/ai/composite/goal.hpp Sat Aug  6 18:06:24 2011
@@ -38,6 +38,9 @@
 class terrain_filter;
 
 namespace ai {
+       
+class lua_ai_context;
+class lua_ai_action_handler;
 
 class goal : public readonly_context_proxy, public component {
 public:
@@ -54,6 +57,7 @@
 
 
        virtual void on_create();
+       virtual void on_create(boost::shared_ptr<ai::lua_ai_context>);
 
 
        bool active() const;
@@ -160,6 +164,17 @@
        : protect_goal(context,cfg,true,true)
        {
        }
+};
+
+class lua_goal : public goal {
+public:
+       lua_goal(readonly_context& context, const config& cfg);
+       virtual void add_targets(std::back_insert_iterator< std::vector< target 
> > target_list);
+       void on_create(boost::shared_ptr<ai::lua_ai_context>);
+       
+private:
+       std::string code_;
+       boost::shared_ptr<lua_ai_action_handler> handler_;
 };
 
 

Modified: trunk/src/ai/lua/lua_object.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/lua_object.hpp?rev=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/src/ai/lua/lua_object.hpp (original)
+++ trunk/src/ai/lua/lua_object.hpp Sat Aug  6 18:06:24 2011
@@ -29,10 +29,12 @@
 #include "lua/lualib.h"
 #include "../../scripting/lua_api.hpp"
 #include "config.hpp"
+#include "../default/contexts.hpp"
 #include "terrain_filter.hpp"
 #include "resources.hpp"
 
 namespace ai {
+       
 
 class lua_object_base {
 
@@ -136,6 +138,50 @@
        return tf;
 }
 
+template <>
+inline boost::shared_ptr<std::vector<target> > lua_object< std::vector<target> 
>::to_type(lua_State *L, int n)
+{
+       boost::shared_ptr<std::vector<target> > targets = 
boost::shared_ptr<std::vector<target> >(new std::vector<target>());
+       std::back_insert_iterator< std::vector<target> > tg(*targets);
+       int l = lua_objlen(L, n);
+       
+       for (int i = 1; i <= l; ++i)
+       {
+               lua_rawgeti(L, n, i); // st n + 1  TABLE @ N    table @ n + 1 
+               
+               lua_pushstring(L, "loc"); // st n + 2
+               lua_rawget(L, -2); // st n + 2 
+               
+               lua_pushstring(L, "x"); // st n + 3
+               lua_rawget(L, -2); // st n + 3
+               int x = lua_tointeger(L, -1); // st n + 3 
+               lua_pop(L, 1); // st n + 2
+               
+               lua_pushstring(L, "y"); // st n + 3
+               lua_rawget(L, -2); // st n + 3
+               int y = lua_tointeger(L, -1); // st n + 3               
+               
+               lua_pop(L, 2); // st n + 1
+               
+               lua_pushstring(L, "type"); // st n + 2
+               lua_rawget(L, -2);  // st n + 2
+               target::TYPE type = (target::TYPE)lua_tointeger(L, -1);  // st 
n + 2
+               lua_pop(L, 1); // st n + 1
+               
+               
+               lua_pushstring(L, "value");
+               lua_rawget(L, -2);
+               int value = lua_tonumber(L, -1);
+               
+               map_location ml(x - 1, y - 1);
+               
+               *tg = target(ml, value, type);
+       }
+       
+       lua_settop(L, n);
+       return targets;
+} 
+
 } // end of namespace ai
 
 

Modified: trunk/src/ai/registry.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/registry.cpp?rev=50622&r1=50621&r2=50622&view=diff
==============================================================================
--- trunk/src/ai/registry.cpp (original)
+++ trunk/src/ai/registry.cpp Sat Aug  6 18:06:24 2011
@@ -157,6 +157,9 @@
 
 static register_goal_factory<protect_unit_goal>
        goal_factory_protect_unit("protect_unit");
+       
+static register_goal_factory<lua_goal>
+       goal_factory_lua_goal("lua_goal");
 
 
 // =======================================================================


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

Reply via email to