mail[Wesnoth-commits] r44301 - in /trunk/src: game.cpp play_controller.cpp playcampaign.cpp scripting/lua.cpp scripting/lua.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by guillaume . melquiond on July 18, 2010 - 17:21:
Author: silene
Date: Sun Jul 18 17:21:41 2010
New Revision: 44301

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44301&view=rev
Log:
Modified handling of [lua] tags at toplevel, so that they are no longer 
converted to preload events. Instead, they are extracted and executed at 
preload time.

Modified:
    trunk/src/game.cpp
    trunk/src/play_controller.cpp
    trunk/src/playcampaign.cpp
    trunk/src/scripting/lua.cpp
    trunk/src/scripting/lua.hpp

Modified: trunk/src/game.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game.cpp?rev=44301&r1=44300&r2=44301&view=diff
==============================================================================
--- trunk/src/game.cpp (original)
+++ trunk/src/game.cpp Sun Jul 18 17:21:41 2010
@@ -67,6 +67,7 @@
 #include "addon/manager.hpp"
 #include "replay.hpp"
 #include "savegame.hpp"
+#include "scripting/lua.hpp"
 #include "sound.hpp"
 #include "statistics.hpp"
 #include "thread.hpp"
@@ -1571,6 +1572,10 @@
                                
gui2::show_error_message(disp().video(),msg.str());
                        }
                }
+
+               // Extract the Lua scripts at toplevel.
+               lua::extract_preload_scripts(game_config_);
+               game_config_.clear_children("lua");
 
                game_config_.merge_children("units");
                game_config_.append(core_terrain_rules);

Modified: trunk/src/play_controller.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/play_controller.cpp?rev=44301&r1=44300&r2=44301&view=diff
==============================================================================
--- trunk/src/play_controller.cpp (original)
+++ trunk/src/play_controller.cpp Sun Jul 18 17:21:41 2010
@@ -42,6 +42,7 @@
 #include "ai/manager.hpp"
 #include "ai/testing.hpp"
 #include "whiteboard/manager.hpp"
+#include "scripting/lua.hpp"
 
 static lg::log_domain log_engine("engine");
 #define LOG_NG LOG_STREAM(info, log_engine)
@@ -467,6 +468,7 @@
 
 void play_controller::fire_prestart(bool execute){
        // Run initialization scripts, even if loading from a snapshot.
+       resources::lua_kernel->initialize();
        game_events::fire("preload");
 
        // pre-start events must be executed before any GUI operation,

Modified: trunk/src/playcampaign.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playcampaign.cpp?rev=44301&r1=44300&r2=44301&view=diff
==============================================================================
--- trunk/src/playcampaign.cpp (original)
+++ trunk/src/playcampaign.cpp Sun Jul 18 17:21:41 2010
@@ -65,18 +65,6 @@
 
 } // end anon namespace
 
-/** Marks all the toplevel [lua] tag as preload events. */
-static void preload_lua_tags(const config &game_config, config &target)
-{
-       foreach (const config &cfg, game_config.child_range("lua"))
-       {
-               config &ev = target.add_child("event");
-               ev["name"] = "preload";
-               ev["first_time_only"] = false;
-               ev.add_child("lua", cfg);
-       }
-}
-
 void play_replay(display& disp, game_state& gamestate, const config& 
game_config,
                CVideo& video)
 {
@@ -93,21 +81,6 @@
                assert(scenario);
                gamestate.starting_pos = scenario;
        }
-
-       // Detect replays stored by the server, since they are missing core 
lua tags.
-       if (const config &core = game_config.child("lua"))
-       {
-               bool found_core = false;
-               foreach (const config &cfg, 
gamestate.starting_pos.child_range("lua")) {
-                       if (cfg["code"] == core["code"]) {
-                               found_core = true;
-                               break;
-                       }
-               }
-               if (!found_core)
-                       preload_lua_tags(game_config, gamestate.starting_pos);
-       }
-
        starting_pos = gamestate.starting_pos;
 
        //for replays, use the variables specified in starting_pos
@@ -245,7 +218,6 @@
                // Otherwise this is the start of a campaign scenario.
                if(gamestate.starting_pos["id"].empty() == false) {
                        LOG_G << "loading starting position...\n";
-                       preload_lua_tags(game_config, gamestate.starting_pos);
                        starting_pos = gamestate.starting_pos;
                        scenario = &starting_pos;
                } else {
@@ -254,7 +226,6 @@
                        scenario = &game_config.find_child(type, "id", 
gamestate.classification().scenario);
                        if (*scenario) {
                                starting_pos = *scenario;
-                               preload_lua_tags(game_config, starting_pos);
                                config temp(starting_pos);
                                write_players(gamestate, temp, false, true);
                                gamestate.starting_pos = temp;
@@ -343,7 +314,6 @@
                                static config scenario2;
                                scenario2 = 
random_generate_scenario((*scenario)["scenario_generation"], 
scenario->child("generator"));
                                //level_ = scenario;
-                               preload_lua_tags(game_config, scenario2);
                                //merge carryover information into the newly 
generated scenario
                                config temp(scenario2);
                                write_players(gamestate, temp, false, true);
@@ -492,7 +462,6 @@
                        else
                        {
                                starting_pos = *scenario;
-                               preload_lua_tags(game_config, starting_pos);
                                scenario = &starting_pos;
                        }
 
@@ -529,7 +498,6 @@
                                        static config scenario2;
                                        scenario2 = 
random_generate_scenario((*scenario)["scenario_generation"], 
scenario->child("generator"));
                                        //level_ = scenario;
-                                       preload_lua_tags(game_config, 
scenario2);
                                        gamestate.starting_pos = scenario2;
                                        scenario = &scenario2;
                                }

Modified: trunk/src/scripting/lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=44301&r1=44300&r2=44301&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Sun Jul 18 17:21:41 2010
@@ -61,6 +61,16 @@
 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
 
 namespace lua {
+
+static std::vector<config> preload_scripts;
+
+void extract_preload_scripts(config const &game_config)
+{
+       preload_scripts.clear();
+       foreach (config const &cfg, game_config.child_range("lua")) {
+               preload_scripts.push_back(cfg);
+       }
+}
 
 /**
  * Stack storing the queued_event objects needed for calling WML actions.
@@ -2445,6 +2455,13 @@
        lua_settop(L, 0);
 }
 
+void LuaKernel::initialize()
+{
+       foreach (const config &cfg, preload_scripts) {
+               execute(cfg["code"].str().c_str(), 0, 0);
+       }
+}
+
 LuaKernel::~LuaKernel()
 {
        lua_close(mState);

Modified: trunk/src/scripting/lua.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.hpp?rev=44301&r1=44300&r2=44301&view=diff
==============================================================================
--- trunk/src/scripting/lua.hpp (original)
+++ trunk/src/scripting/lua.hpp Sun Jul 18 17:21:41 2010
@@ -27,6 +27,8 @@
 
 namespace lua {
 
+void extract_preload_scripts(config const &);
+
 class LuaKernel
 {
        lua_State *mState;
@@ -34,6 +36,7 @@
 public:
        LuaKernel();
        ~LuaKernel();
+       void initialize();
        void run_event(vconfig const &, game_events::queued_event const &);
        bool run_filter(char const *name, unit const &u);
        /** Runs a plain script. */




Related Messages


Powered by MHonArc, Updated Sun Jul 18 17:40:14 2010