Remove obsolete class from FormulaAI

This class appears to stem from the days when FormulaAI was implemented as a separate AI algorithm,
rather than as an engine of the composite AI. Apart from one function which didn't access anything in
the class (and has been moved elsewhere), the class was entirely unused.
This commit is contained in:
Celtic Minstrel 2016-04-02 03:05:35 -04:00
parent adb0abfb6d
commit 104ccf6e36
4 changed files with 22 additions and 116 deletions

View file

@ -77,9 +77,26 @@ using namespace game_logic;
namespace ai {
game_logic::candidate_action_ptr formula_ai::load_candidate_action_from_config(const config& cfg)
using ca_ptr = game_logic::candidate_action_ptr;
ca_ptr formula_ai::load_candidate_action_from_config(const config& rc_action)
{
return candidate_action_manager_.load_candidate_action_from_config(cfg,this,&function_table_);
ca_ptr new_ca;
const t_string &name = rc_action["name"];
try {
const t_string &type = rc_action["type"];
if( type == "movement") {
new_ca = ca_ptr(new move_candidate_action(name, type, rc_action, &function_table_));
} else if( type == "attack") {
new_ca = ca_ptr(new attack_candidate_action(name, type, rc_action, &function_table_));
} else {
ERR_AI << "Unknown candidate action type: " << type << std::endl;
}
} catch(formula_error& e) {
handle_exception(e, "Error while registering candidate action '" + name + "'");
}
return new_ca;
}
int formula_ai::get_recursion_count() const{
@ -97,8 +114,7 @@ formula_ai::formula_ai(readonly_context &context, const config &cfg)
keeps_cache_(),
infinite_loop_guardian_(),
vars_(),
function_table_(*this),
candidate_action_manager_()
function_table_(*this)
{
add_ref();
init_readonly_context_proxy(context);
@ -956,13 +972,13 @@ void formula_ai::on_create(){
}
void formula_ai::evaluate_candidate_action(game_logic::candidate_action_ptr fai_ca)
void formula_ai::evaluate_candidate_action(ca_ptr fai_ca)
{
fai_ca->evaluate(this,*resources::units);
}
bool formula_ai::execute_candidate_action(game_logic::candidate_action_ptr fai_ca)
bool formula_ai::execute_candidate_action(ca_ptr fai_ca)
{
game_logic::map_formula_callable callable(this);
callable.add_ref();

View file

@ -163,7 +163,6 @@ private:
gamestate_change_observer infinite_loop_guardian_;
game_logic::map_formula_callable vars_;
game_logic::ai_function_symbol_table function_table_;
game_logic::candidate_action_manager candidate_action_manager_;
friend class ai_default;
};

View file

@ -30,57 +30,6 @@ static lg::log_domain log_formula_ai("ai/engine/fai");
namespace game_logic {
void candidate_action_manager::load_config(const config& cfg, ai::formula_ai* ai, function_symbol_table* function_table)
{
// register candidate actions
BOOST_FOREACH(const config &rc_action, cfg.child_range("register_candidate_action"))
{
candidate_action_ptr new_ca = load_candidate_action_from_config(rc_action,ai,function_table);
if (new_ca) {
candidate_actions_.push_back(new_ca);
}
}
}
candidate_action_ptr candidate_action_manager::load_candidate_action_from_config(const config& rc_action, ai::formula_ai* ai, function_symbol_table* function_table)
{
candidate_action_ptr new_ca;
const t_string &name = rc_action["name"];
try {
const t_string &type = rc_action["type"];
if( type == "movement") {
new_ca = candidate_action_ptr(new move_candidate_action(name, type, rc_action, function_table ));
} else if( type == "attack") {
new_ca = candidate_action_ptr(new attack_candidate_action(name, type, rc_action, function_table ));
} else {
ERR_AI << "Unknown candidate action type: " << type << std::endl;
}
} catch(formula_error& e) {
ai->handle_exception(e, "Error while registering candidate action '" + name + "'");
}
return new_ca;
}
bool candidate_action_manager::evaluate_candidate_actions(ai::formula_ai* ai, unit_map& units)
{
evaluated_candidate_actions_.clear();
BOOST_FOREACH(candidate_action_ptr cm, candidate_actions_)
{
cm->evaluate(ai, units);
evaluated_candidate_actions_.insert(cm);
}
if( evaluated_candidate_actions_.empty() ||
(*evaluated_candidate_actions_.begin())->get_score() <= 0 ) //@note ai::candidate_action::BAD_SCORE )
return false;
return true;
}
base_candidate_action::base_candidate_action(const std::string& name,
const std::string& type, const config& cfg,
function_symbol_table* function_table) :

View file

@ -20,8 +20,6 @@
#ifndef AI_FORMULA_CANDIDATES_HPP_INCLUDED
#define AI_FORMULA_CANDIDATES_HPP_INCLUDED
#include "formula/function.hpp"
#include <set>
class unit_map;
@ -72,62 +70,6 @@ protected:
int score_;
};
struct candidate_action_compare {
bool operator() (const candidate_action_ptr laction,
const candidate_action_ptr raction) const
{
return laction->get_score() > raction->get_score();
}
};
typedef std::set<game_logic::candidate_action_ptr, game_logic::candidate_action_compare> candidate_action_set;
//this class is responsible for managing candidate actions
class candidate_action_manager {
public:
candidate_action_manager()
: evaluated_candidate_actions_()
, candidate_actions_()
{}
//register candidate actions from config
void load_config(const config& cfg, ai::formula_ai* ai, function_symbol_table* function_table);
//register a single candidate action from config
candidate_action_ptr load_candidate_action_from_config(const config& cfg, ai::formula_ai* ai, function_symbol_table* function_table);
//evaluate candidate action, return true if we have candidate action that have score > 0
bool evaluate_candidate_actions(ai::formula_ai* ai, unit_map& units);
const_formula_ptr get_best_action_formula() const {
if( evaluated_candidate_actions_.empty() )
return game_logic::formula_ptr();
return (*evaluated_candidate_actions_.begin())->get_action();
}
//calls same method from best candidate action
void update_callable_map(game_logic::map_formula_callable& callable){
if( evaluated_candidate_actions_.empty() )
return;
(*evaluated_candidate_actions_.begin())->update_callable_map(callable);
}
void register_candidate_action(candidate_action_ptr& candidate_action){
candidate_actions_.push_back(candidate_action);
}
bool has_candidate_actions() const { return !candidate_actions_.empty(); }
void clear() {
candidate_actions_.clear();
evaluated_candidate_actions_.clear();
}
private:
game_logic::candidate_action_set evaluated_candidate_actions_;
std::vector<candidate_action_ptr> candidate_actions_;
};
class candidate_action_with_filters : public base_candidate_action {
public: