From 08b11139a107bea6f651c22d8944dae0085e5773 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sat, 27 Feb 2016 03:35:56 -0500 Subject: [PATCH] Log useful error messages (and don't crash) when creating a goal with the wrong engine --- src/ai/composite/engine_default.cpp | 2 +- src/ai/composite/engine_lua.cpp | 2 +- src/ai/composite/goal.cpp | 33 +++++++++++++++++++++++++++-- src/ai/composite/goal.hpp | 4 +++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/ai/composite/engine_default.cpp b/src/ai/composite/engine_default.cpp index 20083837636f..23bf4c9add0a 100644 --- a/src/ai/composite/engine_default.cpp +++ b/src/ai/composite/engine_default.cpp @@ -106,7 +106,7 @@ void engine_cpp::do_parse_goal_from_config(const config &cfg, std::back_insert_i return; } goal_ptr new_goal = f->second->get_new_instance(ai_,cfg); - if (!new_goal) { + if (!new_goal || !new_goal->ok()) { ERR_AI_ENGINE_CPP << "side "<second->get_new_instance(ai_,cfg); new_goal->on_create(lua_ai_context_); - if (!new_goal) { + if (!new_goal || !new_goal->ok()) { ERR_AI_LUA << "side "<) { + unrecognized(); +} + +void goal::unrecognized() +{ + ERR_AI_GOAL << "side " << get_side() << " : " << " tried to create goal with name=[" << cfg_["name"] << "], but the [" << cfg_["engine"] << "] engine did not recognize that type of goal. " << std::endl; + ok_ = false; } @@ -105,6 +113,12 @@ bool goal::redeploy(const config &cfg) } +bool goal::ok() const +{ + return ok_; +} + + bool goal::active() const { return is_active(cfg_["time_of_day"],cfg_["turns"]); @@ -114,6 +128,11 @@ bool goal::active() const void target_unit_goal::on_create() { goal::on_create(); + if (cfg_["engine"] != "cpp") { + unrecognized(); + value_ = 0; + return; + } if (const config::attribute_value *v = cfg_.get("value")) { try { value_ = boost::lexical_cast(*v); @@ -156,6 +175,11 @@ target_unit_goal::target_unit_goal(readonly_context &context, const config &cfg) void target_location_goal::on_create() { goal::on_create(); + if (cfg_["engine"] != "cpp") { + unrecognized(); + value_ = 0; + return; + } if (cfg_.has_attribute("value")) { try { value_ = boost::lexical_cast(cfg_["value"]); @@ -200,6 +224,11 @@ target_location_goal::target_location_goal(readonly_context &context, const conf void protect_goal::on_create() { goal::on_create(); + if (cfg_["engine"] != "cpp") { + unrecognized(); + value_ = 0; + return; + } if (const config::attribute_value *v = cfg_.get("value")) { try { value_ = boost::lexical_cast(*v); @@ -319,7 +348,7 @@ lua_goal::lua_goal(readonly_context &context, const config &cfg) } else { - // report failure + ERR_AI_GOAL << "side " << get_side() << " : Error creating Lua goal (missing code= key)" << std::endl; } } diff --git a/src/ai/composite/goal.hpp b/src/ai/composite/goal.hpp index fec2d9533cd5..57feed936d7a 100644 --- a/src/ai/composite/goal.hpp +++ b/src/ai/composite/goal.hpp @@ -69,6 +69,7 @@ class goal : public readonly_context_proxy, public component { bool active() const; + bool ok() const; virtual std::string get_id() const; virtual std::string get_name() const; @@ -78,8 +79,9 @@ class goal : public readonly_context_proxy, public component { protected: + void unrecognized(); config cfg_; - + bool ok_; };