Author: nephro
Date: Fri Aug  5 15:25:35 2011
New Revision: 50611

URL: http://svn.gna.org/viewcvs/wesnoth?rev=50611&view=rev
Log:
Exposed the target list to Lua // minor fixes

Modified:
    trunk/data/ai/scenarios/scenario-lua-ai.cfg
    trunk/src/ai/composite/engine.cpp
    trunk/src/ai/composite/engine.hpp
    trunk/src/ai/composite/engine_lua.cpp
    trunk/src/ai/composite/engine_lua.hpp
    trunk/src/ai/contexts.hpp
    trunk/src/ai/lua/core.cpp
    trunk/src/ai/manager.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=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/data/ai/scenarios/scenario-lua-ai.cfg (original)
+++ trunk/data/ai/scenarios/scenario-lua-ai.cfg Fri Aug  5 15:25:35 2011
@@ -282,11 +282,19 @@
 
         [ai]
             version=10710
-            [aspect]
-                id=aggression
-                engine=lua
-                code = << return 0.23 >>
-            [/aspect]
+               [goal]
+                       name=protect_location
+                       value=5
+                       protect_radius=16
+                       [criteria] #NOTE: this is a SLF, because we're 
protecting a location
+                               x,y=42,20
+                       [/criteria]
+               [/goal]
+               [aspect]
+                       id=aggression
+                       engine=lua
+                       code = << return 0.23 >>
+               [/aspect]
             [engine]
                 name="lua"
                 code= <<
@@ -304,7 +312,9 @@
 
 function my_ai:stage_hello()
        wesnoth.message('hello from stage!')
-       wesnoth.message('PERSISTANCE: ' .. tostring(self.data["pers"]) .. 
self.data["stringg"])
+       --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)
 end
 
 function my_ai:candidate_action_evaluation_hello()

Modified: trunk/src/ai/composite/engine.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/engine.cpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/composite/engine.cpp (original)
+++ trunk/src/ai/composite/engine.cpp Fri Aug  5 15:25:35 2011
@@ -119,9 +119,14 @@
        return "evaluate command is not implemented by this engine";
 }
 
-void engine::set_ai_context(ai_context * /*context*/)
+void engine::set_ai_context(ai_context_ptr ai_context)
 {
-       //do nothing
+       ai_context_ = ai_context;
+}
+
+ai_context_ptr engine::get_ai_context()
+{
+       return ai_context_;
 }
 
 config engine::to_config() const

Modified: trunk/src/ai/composite/engine.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/engine.hpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/composite/engine.hpp (original)
+++ trunk/src/ai/composite/engine.hpp Fri Aug  5 15:25:35 2011
@@ -84,9 +84,9 @@
        /**
         * set ai context (which is not available during early initialization)
         */
-       virtual void set_ai_context(ai_context *context);
+       virtual void set_ai_context(ai_context_ptr context);
 
-
+       virtual ai_context_ptr get_ai_context();
        /**
         * serialize
         */
@@ -104,6 +104,7 @@
 
 protected:
        readonly_context &ai_;
+       ai_context_ptr ai_context_;
 
        /** name of the engine which has created this engine*/
        std::string engine_;

Modified: trunk/src/ai/composite/engine_lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/engine_lua.cpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/composite/engine_lua.cpp (original)
+++ trunk/src/ai/composite/engine_lua.cpp Fri Aug  5 15:25:35 2011
@@ -266,12 +266,6 @@
        return "";
 }
 
-
-void engine_lua::set_ai_context(ai_context * /*context*/)
-{
-       //this function is called when the ai is fully initialized
-}
-
 config engine_lua::to_config() const
 {
        config cfg = engine::to_config();

Modified: trunk/src/ai/composite/engine_lua.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/composite/engine_lua.hpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/composite/engine_lua.hpp (original)
+++ trunk/src/ai/composite/engine_lua.hpp Fri Aug  5 15:25:35 2011
@@ -59,12 +59,12 @@
         */
        virtual config to_config() const;
 
-       /**
-        * Method to inject AI context into the engine.
-        * The context includes all that in necessary for the AI -
-        * , like access to game state and movement/attack routines.
-        */
-       virtual void set_ai_context(ai_context *context);
+//     /**
+//      * Method to inject AI context into the engine.
+//      * The context includes all that in necessary for the AI -
+//      * , like access to game state and movement/attack routines.
+//      */
+//     virtual void set_ai_context(ai_context *context);
 
 private:
 
@@ -72,7 +72,7 @@
         * The underlying lua code
         */
        std::string code_;
-
+       
        //There is one lua engine per AI. So, it can hold state
        boost::shared_ptr<lua_ai_context> lua_ai_context_;
 

Modified: trunk/src/ai/contexts.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/contexts.hpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/contexts.hpp (original)
+++ trunk/src/ai/contexts.hpp Fri Aug  5 15:25:35 2011
@@ -49,6 +49,9 @@
 
 class interface;
 class ai_context;
+
+typedef ai_context* ai_context_ptr;
+
 
 // recursion counter
 class recursion_counter {

Modified: trunk/src/ai/lua/core.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/lua/core.cpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/lua/core.cpp (original)
+++ trunk/src/ai/lua/core.cpp Fri Aug  5 15:25:35 2011
@@ -47,6 +47,7 @@
 #include "../../unit.hpp"
 #include "../actions.hpp"
 #include "../composite/engine_lua.hpp"
+#include "../composite/contexts.hpp"
 #include <lua/llimits.h>
 
 static lg::log_domain log_ai_engine_lua("ai/engine/lua");
@@ -57,6 +58,8 @@
 
 namespace ai {
 
+static void push_map_location(lua_State *L, const map_location& ml);
+       
 void lua_ai_context::init(lua_State *L)
 {
        // Create the ai elements table.
@@ -277,6 +280,42 @@
        return transform_ai_action(L,recall_result);
 }
 
+// Goals and targets
+
+
+static int cfun_ai_get_targets(lua_State *L)
+{
+       move_map enemy_dst_src = get_readonly_context(L).get_enemy_dstsrc();
+       std::vector<target> targets = 
get_engine(L).get_ai_context()->find_targets(enemy_dst_src);
+       int i = 1;
+       
+       lua_createtable(L, 0, 0);
+       for (std::vector<target>::iterator it = targets.begin(); it != 
targets.end(); it++)
+       {
+               lua_pushinteger(L, i);
+               
+               //to factor out
+               lua_createtable(L, 3, 0);
+               
+               
+               lua_pushstring(L, "type");
+               lua_pushnumber(L, it->type);
+               lua_rawset(L, -3);
+               
+               lua_pushstring(L, "loc");
+               push_map_location(L, it->loc);
+               lua_rawset(L, -3);
+               
+               lua_pushstring(L, "value");
+               lua_pushnumber(L, it->value);
+               lua_rawset(L, -3);
+               
+               lua_rawset(L, -3);
+               ++i;
+       }
+       return 1;
+}
+
 // Aspect section
 static int cfun_ai_get_aggression(lua_State *L)
 {
@@ -446,11 +485,11 @@
        lua_createtable(L, 2, 0);
 
        lua_pushstring(L, "x");
-       lua_pushinteger(L, ml.x);
+       lua_pushinteger(L, ml.x + 1);
        lua_rawset(L, -3);
 
        lua_pushstring(L, "y");
-       lua_pushinteger(L, ml.y);
+       lua_pushinteger(L, ml.y + 1);
        lua_rawset(L, -3);
 }
 
@@ -538,6 +577,9 @@
                { "get_enemy_dstsrc",           &cfun_ai_get_enemy_dstsrc       
        },
                { "get_enemy_srcdst",           &cfun_ai_get_enemy_srcdst       
        },
                // End of move maps
+               // Goals and targets
+               { "get_targets",                &cfun_ai_get_targets            
        },
+               // End of G & T
                // Aspects
                { "get_aggression",             &cfun_ai_get_aggression         
        },
                { "get_avoid",                  &cfun_ai_get_avoid              
        },

Modified: trunk/src/ai/manager.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/manager.cpp?rev=50611&r1=50610&r2=50611&view=diff
==============================================================================
--- trunk/src/ai/manager.cpp (original)
+++ trunk/src/ai/manager.cpp Fri Aug  5 15:25:35 2011
@@ -37,6 +37,7 @@
 #include <map>
 #include <stack>
 #include <vector>
+#include "composite/engine.hpp"
 
 namespace ai {
 
@@ -95,7 +96,13 @@
                        modify_ai(mod_ai);
                }
                cfg_.clear_children("modify_ai");
-
+               
+               std::vector<engine_ptr> engines = ai_->get_engines();
+               for (std::vector<engine_ptr>::iterator it = engines.begin(); it 
!= engines.end(); ++it)
+               {
+                       (*it)->set_ai_context(&(ai_->get_ai_context()));
+               }
+               
        } else {
                ERR_AI_MANAGER << describe_ai()<<"AI lazy initialization 
error!" << std::endl;
        }


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

Reply via email to