From 57aa197583c05f4a91d04a50c115bfd414d5e5b4 Mon Sep 17 00:00:00 2001 From: Dmitry Kovalenko Date: Fri, 1 Jun 2012 12:57:14 +0000 Subject: [PATCH] LuaAI: (1) Lua CA_wrapper slightly refactored, in preparation for adding CA definitons using external files (2) --debug_lua command line argument added. Unused at the moment, but will be used for Lua debug mechanisms --- data/ai/scenarios/scenario-lua-ai.cfg | 2 +- src/ai/composite/engine_lua.cpp | 54 ++++++++++++++++++--------- src/commandline_options.cpp | 4 ++ src/commandline_options.hpp | 2 + src/game.cpp | 3 ++ src/game_config.cpp | 2 +- src/game_config.hpp | 2 +- src/scripting/lua.cpp | 1 + 8 files changed, 49 insertions(+), 21 deletions(-) diff --git a/data/ai/scenarios/scenario-lua-ai.cfg b/data/ai/scenarios/scenario-lua-ai.cfg index 37bd696bdcb..ce9566f619d 100644 --- a/data/ai/scenarios/scenario-lua-ai.cfg +++ b/data/ai/scenarios/scenario-lua-ai.cfg @@ -296,7 +296,7 @@ end function my_ai:do_moves() - + wesnoth.message("ldbg", tostring(wesnoth.game_config.debug_lua)) my_leader = wesnoth.get_units({side = 1, can_recruit=yes})[1].name diff --git a/src/ai/composite/engine_lua.cpp b/src/ai/composite/engine_lua.cpp index 7eeee53390a..3a080fa2d49 100644 --- a/src/ai/composite/engine_lua.cpp +++ b/src/ai/composite/engine_lua.cpp @@ -53,18 +53,16 @@ static lg::log_domain log_ai_engine_lua("ai/engine/lua"); typedef boost::shared_ptr< lua_object > lua_int_obj; -class lua_candidate_action_wrapper : public candidate_action { -public: - lua_candidate_action_wrapper( rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx) - : candidate_action(context,cfg),evaluation_(cfg["evaluation"]),evaluation_action_handler_(), - execution_(cfg["execution"]),execution_action_handler_(),serialized_evaluation_state_() +class lua_candidate_action_wrapper_base : public candidate_action { + +public: + lua_candidate_action_wrapper_base( rca_context &context, const config &cfg) + : candidate_action(context, cfg),evaluation_action_handler_(),execution_action_handler_(),serialized_evaluation_state_() { - evaluation_action_handler_ = boost::shared_ptr(resources::lua_kernel->create_lua_ai_action_handler(evaluation_.c_str(),lua_ai_ctx)); - execution_action_handler_ = boost::shared_ptr(resources::lua_kernel->create_lua_ai_action_handler(execution_.c_str(),lua_ai_ctx)); + // do nothing } - virtual ~lua_candidate_action_wrapper() {} - + virtual ~lua_candidate_action_wrapper_base() {} virtual double evaluate() { @@ -88,22 +86,42 @@ public: execution_action_handler_->handle(serialized_evaluation_state_, false, l_obj); } } + + virtual config to_config() const { + config cfg = candidate_action::to_config(); + cfg.add_child("state",serialized_evaluation_state_); + return cfg; + } + +protected: + boost::shared_ptr evaluation_action_handler_; + boost::shared_ptr execution_action_handler_; + config serialized_evaluation_state_; +}; + +class lua_candidate_action_wrapper : public lua_candidate_action_wrapper_base { + +public: + lua_candidate_action_wrapper( rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx) + : lua_candidate_action_wrapper_base(context,cfg),evaluation_(cfg["evaluation"]),execution_(cfg["execution"]) + { + evaluation_action_handler_ = boost::shared_ptr(resources::lua_kernel->create_lua_ai_action_handler(evaluation_.c_str(),lua_ai_ctx)); + execution_action_handler_ = boost::shared_ptr(resources::lua_kernel->create_lua_ai_action_handler(execution_.c_str(),lua_ai_ctx)); + } + + virtual ~lua_candidate_action_wrapper() {} virtual config to_config() const { - config cfg = candidate_action::to_config(); + config cfg = lua_candidate_action_wrapper_base::to_config(); cfg["evaluation"] = evaluation_; - cfg["execution"] = execution_; - cfg.add_child("state",serialized_evaluation_state_); + cfg["execution"] = execution_; return cfg; } private: - std::string evaluation_; - boost::shared_ptr evaluation_action_handler_; + std::string evaluation_; std::string execution_; - boost::shared_ptr execution_action_handler_; - config serialized_evaluation_state_; }; class lua_sticky_candidate_action_wrapper : public lua_candidate_action_wrapper { @@ -120,7 +138,7 @@ public: { if (resources::units->find(bound_unit_->underlying_id()).valid()) { - return lua_candidate_action_wrapper::evaluate(); + return lua_candidate_action_wrapper_base::evaluate(); } else { @@ -131,7 +149,7 @@ public: virtual void execute() { - lua_candidate_action_wrapper::execute(); + lua_candidate_action_wrapper_base::execute(); this->disable(); // we do not want to execute the same sticky CA twice -> will be moved out to Lua later } private: diff --git a/src/commandline_options.cpp b/src/commandline_options.cpp index 8718e7ad1a7..3de1824bcef 100644 --- a/src/commandline_options.cpp +++ b/src/commandline_options.cpp @@ -51,6 +51,7 @@ commandline_options::commandline_options ( int argc, char** argv ) : config_dir(), data_dir(), debug(false), + debug_lua(false), #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS debug_dot_domain(), debug_dot_level(), @@ -127,6 +128,7 @@ commandline_options::commandline_options ( int argc, char** argv ) : ("config-path", "prints the path of the user config directory and exits.") ("data-dir", po::value(), "overrides the data directory with the one specified.") ("debug,d", "enables additional command mode options in-game.") + ("debug_lua", "enables some Lua debugging mechanisms") #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS ("debug-dot-level", po::value(), "sets the level of the debug dot files. should be a comma separated list of levels. These files are used for debugging the widgets especially the for the layout engine. When enabled the engine will produce dot files which can be converted to images with the dot tool. Available levels: size (generate the size info of the widget), state (generate the state info of the widget).") ("debug-dot-domain", po::value(), "sets the domain of the debug dot files. should be a comma separated list of domains. See --debug-dot-level for more info. Available domains: show (generate the data when the dialog is about to be shown), layout (generate the data during the layout phase - might result in multiple files). The data can also be generated when the F12 is pressed in a dialog.") @@ -255,6 +257,8 @@ commandline_options::commandline_options ( int argc, char** argv ) : data_dir = vm["data-dir"].as(); if (vm.count("debug")) debug = true; + if (vm.count("debug_lua")) + debug_lua = true; #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS if (vm.count("debug-dot-domain")) { debug_dot_domain = vm["debug-dot-domain"].as(); diff --git a/src/commandline_options.hpp b/src/commandline_options.hpp index cd32ebc11fb..11c260b919e 100644 --- a/src/commandline_options.hpp +++ b/src/commandline_options.hpp @@ -49,6 +49,8 @@ public: boost::optional data_dir; /// True if --debug was given on the command line. Enables debug mode. bool debug; + /// True if --debug_lua was given in the commandline. Enables some Lua debugging mechanisms. + bool debug_lua; #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS /// Non-empty if --debug-dot-domain was given on the command line. boost::optional debug_dot_domain; diff --git a/src/game.cpp b/src/game.cpp index f2ac12b3926..5e033f6802a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -138,6 +138,9 @@ static int process_command_args(const commandline_options& cmdline_opts) { // don't update font as we already updating it in game ctor //font_manager_.update_font_path(); } + if(cmdline_opts.debug_lua) { + game_config::debug_lua = true; + } if(cmdline_opts.gunzip) { const std::string input_file(*cmdline_opts.gunzip); if(!is_gzip_file(input_file)) { diff --git a/src/game_config.cpp b/src/game_config.cpp index 58919e02bfb..a9f3153a3c2 100644 --- a/src/game_config.cpp +++ b/src/game_config.cpp @@ -52,7 +52,7 @@ namespace game_config const std::string revision = VERSION; #endif std::string wesnoth_program_dir; - bool debug = false, editor = false, ignore_replay_errors = false, mp_debug = false, exit_at_end = false, new_syntax = false, no_delay = false, small_gui = false, disable_autosave = false; + bool debug = false, debug_lua = false, editor = false, ignore_replay_errors = false, mp_debug = false, exit_at_end = false, new_syntax = false, no_delay = false, small_gui = false, disable_autosave = false; int cache_compression_level = 6; diff --git a/src/game_config.hpp b/src/game_config.hpp index f49666a1fa8..55d284bdfe3 100644 --- a/src/game_config.hpp +++ b/src/game_config.hpp @@ -51,7 +51,7 @@ namespace game_config /** Default percentage gold carried over to the next scenario. */ extern const int gold_carryover_percentage; - extern bool debug, editor, ignore_replay_errors, mp_debug, exit_at_end, new_syntax, no_delay, small_gui, disable_autosave; + extern bool debug, debug_lua, editor, ignore_replay_errors, mp_debug, exit_at_end, new_syntax, no_delay, small_gui, disable_autosave; extern int cache_compression_level; diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 737930cc92a..ca283cd4fb0 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1890,6 +1890,7 @@ static int impl_game_config_get(lua_State *L) return_int_attrib("last_turn", resources::tod_manager->number_of_turns()); return_string_attrib("version", game_config::version); return_bool_attrib("debug", game_config::debug); + return_bool_attrib("debug_lua", game_config::debug_lua); return_bool_attrib("mp_debug", game_config::mp_debug); return 0; }