Skip to content

Commit

Permalink
Log useful error messages (and don't crash) when creating a goal with…
Browse files Browse the repository at this point in the history
… the wrong engine
  • Loading branch information
CelticMinstrel authored and mattsc committed Mar 22, 2016
1 parent 852a255 commit 08b1113
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ai/composite/engine_default.cpp
Expand Up @@ -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 "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]"<< std::endl;
DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
return;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/composite/engine_lua.cpp
Expand Up @@ -351,7 +351,7 @@ void engine_lua::do_parse_goal_from_config(const config &cfg, std::back_insert_i
}
goal_ptr new_goal = f->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 "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]"<< std::endl;
DBG_AI_LUA << "config snippet contains: " << std::endl << cfg << std::endl;
return;
Expand Down
33 changes: 31 additions & 2 deletions src/ai/composite/goal.cpp
Expand Up @@ -49,7 +49,7 @@ static lg::log_domain log_ai_goal("ai/goal");
#define ERR_AI_GOAL LOG_STREAM(err, log_ai_goal)

goal::goal(readonly_context &context, const config &cfg)
: readonly_context_proxy(), cfg_(cfg)
: readonly_context_proxy(), cfg_(cfg), ok_(true)
{
init_readonly_context_proxy(context);
}
Expand All @@ -58,10 +58,18 @@ goal::goal(readonly_context &context, const config &cfg)

void goal::on_create()
{
LOG_AI_GOAL << "side " << get_side() << " : " << " created goal with name=[" << cfg_["name"] << "]" << std::endl;
}

void goal::on_create(boost::shared_ptr<ai::lua_ai_context>)
{
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;
}


Expand Down Expand Up @@ -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"]);
Expand All @@ -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<double>(*v);
Expand Down Expand Up @@ -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<double>(cfg_["value"]);
Expand Down Expand Up @@ -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<double>(*v);
Expand Down Expand Up @@ -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;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/ai/composite/goal.hpp
Expand Up @@ -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;
Expand All @@ -78,8 +79,9 @@ class goal : public readonly_context_proxy, public component {


protected:
void unrecognized();
config cfg_;

bool ok_;

};

Expand Down

0 comments on commit 08b1113

Please sign in to comment.