Skip to content

Commit

Permalink
Add get_team() function to get a team_callable from gamestate formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Jun 16, 2018
1 parent d6c9b67 commit ad43efb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/formula/function_gamestate.cpp
Expand Up @@ -242,6 +242,33 @@ DEFINE_WFL_FUNCTION(enemy_of, 2, 2)
return variant(resources::gameboard->get_team(self).is_enemy(other) ? 1 : 0);
}

DEFINE_WFL_FUNCTION(get_team, 1, 1)
{
variant which = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "get_team"));
if(which.is_int()) {
int i = which.as_int();
if(i > 0 && i <= resources::gameboard->teams().size();) {
return std::make_shared<team_callable>(resources::gameboard->get_team(i));
}
} else if(which.is_string()) {
std::string team_name = which.as_string();
std::vector<variant> sides;
for(auto team : resources::gameboard->teams()) {
auto teams = utils::split(team.team_name());
if(std::find(teams.begin(), teams.end(), team_name) != teams.end()) {
sides.emplace_back(std::make_shared<team_callable>(team));
}
}
return variant(sides);
} else if(auto u = which.try_convert<unit_callable>()) {
int i = u->get_unit().side();
return std::make_shared<team_callable>(resources::gameboard->get_team(i));
} else if(which.try_convert<team_callable>()) {
return which;
}
return variant();
}

} // namespace gamestate

gamestate_function_symbol_table::gamestate_function_symbol_table(std::shared_ptr<function_symbol_table> parent) : function_symbol_table(parent) {
Expand All @@ -255,6 +282,7 @@ gamestate_function_symbol_table::gamestate_function_symbol_table(std::shared_ptr
DECLARE_WFL_FUNCTION(adjacent_locs); // This is deliberately duplicated here; this form excludes off-map locations, while the core form does not
DECLARE_WFL_FUNCTION(locations_in_radius);
DECLARE_WFL_FUNCTION(enemy_of);
DECLARE_WFL_FUNCTION(get_team);
}

}
14 changes: 8 additions & 6 deletions src/formula/variant.cpp
Expand Up @@ -676,26 +676,28 @@ variant variant::execute_variant(const variant& var)
continue;
}

if(auto action = vars.top().try_convert<action_callable>()) {
auto& top = vars.top();

if(auto action = top.try_convert<action_callable>()) {
variant res = action->execute_self(*this);
if(res.is_int() && res.as_bool()) {
made_moves.push_back(vars.top());
made_moves.push_back(top);
}
} else if(vars.top().is_string() && vars.top().as_string() == "continue") {
} else if(top.is_string() && top.as_string() == "continue") {
// if(infinite_loop_guardian_.continue_check()) {
made_moves.push_back(vars.top());
made_moves.push_back(top);
// } else {
//too many calls in a row - possible infinite loop
// ERR_SF << "ERROR #5001 while executing 'continue' formula keyword" << std::endl;

// if(safe_call)
// error = variant(new game_logic::safe_call_result(nullptr, 5001));
// }
} else if(vars.top().is_string() && (vars.top().as_string() == "end_turn" || vars.top().as_string() == "end")) {
} else if(top.is_string() && (top.as_string() == "end_turn" || top.as_string() == "end")) {
break;
} else {
//this information is unneeded when evaluating formulas from commandline
ERR_SF << "UNRECOGNIZED MOVE: " << vars.top().to_debug_string() << std::endl;
ERR_SF << "UNRECOGNIZED MOVE: " << top.to_debug_string() << std::endl;
}

vars.pop();
Expand Down

0 comments on commit ad43efb

Please sign in to comment.