move formula ai features of unit to a unit formula manager
This use of handle-body idiom saves *alot* of includes, since a huge number of objects include unit indirectly, and make no use of the ai formula features.
This commit is contained in:
parent
1796b4d7a0
commit
a60d5b0018
13 changed files with 183 additions and 96 deletions
|
@ -982,6 +982,8 @@
|
|||
<Unit filename="../../src/unit_animation.hpp" />
|
||||
<Unit filename="../../src/unit_display.cpp" />
|
||||
<Unit filename="../../src/unit_display.hpp" />
|
||||
<Unit filename="../../src/unit_formula_manager.cpp" />
|
||||
<Unit filename="../../src/unit_formula_manager.hpp" />
|
||||
<Unit filename="../../src/unit_frame.cpp" />
|
||||
<Unit filename="../../src/unit_frame.hpp" />
|
||||
<Unit filename="../../src/unit_helper.cpp" />
|
||||
|
|
|
@ -1017,6 +1017,8 @@
|
|||
<Unit filename="..\..\src\unit_animation.hpp" />
|
||||
<Unit filename="..\..\src\unit_display.cpp" />
|
||||
<Unit filename="..\..\src\unit_display.hpp" />
|
||||
<Unit filename="..\..\src\unit_formula_manager.cpp" />
|
||||
<Unit filename="..\..\src\unit_formula_manager.hpp" />
|
||||
<Unit filename="..\..\src\unit_frame.cpp" />
|
||||
<Unit filename="..\..\src\unit_frame.hpp" />
|
||||
<Unit filename="..\..\src\unit_helper.cpp" />
|
||||
|
|
|
@ -21052,6 +21052,14 @@
|
|||
RelativePath="..\..\src\unit_display.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\unit_formula_manager.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\unit_formula_manager.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\unit_frame.cpp"
|
||||
>
|
||||
|
|
|
@ -886,6 +886,7 @@ set(wesnoth-main_SRC
|
|||
unit_abilities.cpp
|
||||
unit_animation.cpp
|
||||
unit_display.cpp
|
||||
unit_formula_manager.cpp
|
||||
unit_frame.cpp
|
||||
unit_helper.cpp
|
||||
unit_id.cpp
|
||||
|
|
|
@ -519,6 +519,7 @@ wesnoth_sources = Split("""
|
|||
unit_abilities.cpp
|
||||
unit_animation.cpp
|
||||
unit_display.cpp
|
||||
unit_formula_manager.cpp
|
||||
unit_frame.cpp
|
||||
unit_helper.cpp
|
||||
unit_id.cpp
|
||||
|
|
|
@ -39,6 +39,8 @@
|
|||
#include "../../resources.hpp"
|
||||
#include "../../terrain_filter.hpp"
|
||||
#include "../../tod_manager.hpp"
|
||||
#include "../../unit.hpp"
|
||||
#include "../../unit_formula_manager.hpp"
|
||||
#include "../../pathfind/pathfind.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
@ -455,7 +457,7 @@ variant formula_ai::execute_variant(const variant& var, ai_context &ai_, bool co
|
|||
|
||||
if( status == 0 ){
|
||||
LOG_AI << "Setting unit variable: " << set_unit_var_command->key() << " -> " << set_unit_var_command->value().to_debug_string() << "\n";
|
||||
unit->add_formula_var(set_unit_var_command->key(), set_unit_var_command->value());
|
||||
unit->formula_manager().add_formula_var(set_unit_var_command->key(), set_unit_var_command->value());
|
||||
made_moves.push_back(action);
|
||||
} else {
|
||||
ERR_AI << "ERROR #" << status << " while executing 'set_unit_var' formula function" << std::endl;
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#include "../../formula_function.hpp"
|
||||
#include "../../log.hpp"
|
||||
#include "../../resources.hpp"
|
||||
#include "../../unit.hpp"
|
||||
#include "../../unit_formula_manager.hpp"
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
static lg::log_domain log_formula_ai("ai/stage/unit_formulas");
|
||||
|
@ -58,11 +60,11 @@ bool stage_unit_formulas::do_play_stage()
|
|||
for(unit_map::unit_iterator i = units_.begin() ; i != units_.end() ; ++i)
|
||||
{
|
||||
if (i->side() == get_side()) {
|
||||
if (i->has_formula() || i->has_loop_formula()) {
|
||||
if (i->formula_manager().has_formula() || i->formula_manager().has_loop_formula()) {
|
||||
int priority = 0;
|
||||
if (i->has_priority_formula()) {
|
||||
if (i->formula_manager().has_priority_formula()) {
|
||||
try {
|
||||
game_logic::const_formula_ptr priority_formula(fai_.create_optional_formula(i->get_priority_formula()));
|
||||
game_logic::const_formula_ptr priority_formula(fai_.create_optional_formula(i->formula_manager().get_priority_formula()));
|
||||
if (priority_formula) {
|
||||
game_logic::map_formula_callable callable(&fai_);
|
||||
callable.add_ref();
|
||||
|
@ -94,9 +96,9 @@ bool stage_unit_formulas::do_play_stage()
|
|||
|
||||
if( i.valid() ) {
|
||||
|
||||
if (i->has_formula()) {
|
||||
if (i->formula_manager().has_formula()) {
|
||||
try {
|
||||
game_logic::const_formula_ptr formula(fai_.create_optional_formula(i->get_formula()));
|
||||
game_logic::const_formula_ptr formula(fai_.create_optional_formula(i->formula_manager().get_formula()));
|
||||
if (formula) {
|
||||
game_logic::map_formula_callable callable(&fai_);
|
||||
callable.add_ref();
|
||||
|
@ -116,10 +118,10 @@ bool stage_unit_formulas::do_play_stage()
|
|||
}
|
||||
|
||||
if( i.valid() ) {
|
||||
if (i->has_loop_formula())
|
||||
if (i->formula_manager().has_loop_formula())
|
||||
{
|
||||
try {
|
||||
game_logic::const_formula_ptr loop_formula(fai_.create_optional_formula(i->get_loop_formula()));
|
||||
game_logic::const_formula_ptr loop_formula(fai_.create_optional_formula(i->formula_manager().get_loop_formula()));
|
||||
if (loop_formula) {
|
||||
game_logic::map_formula_callable callable(&fai_);
|
||||
callable.add_ref();
|
||||
|
|
|
@ -12,6 +12,8 @@
|
|||
*/
|
||||
|
||||
#include "callable_objects.hpp"
|
||||
#include "unit.hpp"
|
||||
#include "unit_formula_manager.hpp"
|
||||
|
||||
template <typename T, typename K>
|
||||
variant convert_map( const std::map<T, K>& input_map ) {
|
||||
|
@ -224,8 +226,8 @@ variant unit_callable::get_value(const std::string& key) const
|
|||
} else if(key == "cost") {
|
||||
return variant(u_.cost());
|
||||
} else if(key == "vars") {
|
||||
if(u_.formula_vars()) {
|
||||
return variant(u_.formula_vars().get());
|
||||
if(u_.formula_manager().formula_vars()) {
|
||||
return variant(u_.formula_manager().formula_vars().get());
|
||||
} else {
|
||||
return variant();
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#ifndef CALLABLE_OBJECTS_HPP_INCLUDED
|
||||
#define CALLABLE_OBJECTS_HPP_INCLUDED
|
||||
|
||||
|
||||
#include "formula_callable.hpp"
|
||||
#include "map.hpp"
|
||||
#include "team.hpp"
|
||||
|
||||
|
|
83
src/unit.cpp
83
src/unit.cpp
|
@ -20,24 +20,22 @@
|
|||
#include "unit.hpp"
|
||||
|
||||
#include "actions/move.hpp"
|
||||
#include "callable_objects.hpp"
|
||||
#include "formula.hpp"
|
||||
#include "formula_string_utils.hpp"
|
||||
#include "game_display.hpp"
|
||||
#include "game_events/handlers.hpp"
|
||||
#include "game_preferences.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "halo.hpp"
|
||||
#include "log.hpp"
|
||||
#include "play_controller.hpp"
|
||||
#include "random_new.hpp"
|
||||
#include "resources.hpp"
|
||||
#include "unit_id.hpp"
|
||||
#include "unit_abilities.hpp"
|
||||
#include "terrain_filter.hpp"
|
||||
#include "formula_string_utils.hpp"
|
||||
#include "random_new.hpp"
|
||||
#include "resources.hpp"
|
||||
#include "unit_formula_manager.hpp"
|
||||
#include "scripting/lua.hpp"
|
||||
#include "side_filter.hpp"
|
||||
#include "play_controller.hpp"
|
||||
#include "terrain_filter.hpp"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
@ -151,10 +149,7 @@ unit::unit(const unit& o):
|
|||
|
||||
alpha_(o.alpha_),
|
||||
|
||||
unit_formula_(o.unit_formula_),
|
||||
unit_loop_formula_(o.unit_loop_formula_),
|
||||
unit_priority_formula_(o.unit_priority_formula_),
|
||||
formula_vars_(o.formula_vars_ ? new game_logic::map_formula_callable(*o.formula_vars_) : o.formula_vars_),
|
||||
formula_man_(new unit_formula_manager(o.formula_manager())),
|
||||
|
||||
movement_(o.movement_),
|
||||
max_movement_(o.max_movement_),
|
||||
|
@ -237,10 +232,7 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg) :
|
|||
side_(0),
|
||||
gender_(generate_gender(*type_, cfg)),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
unit_priority_formula_(),
|
||||
formula_vars_(),
|
||||
formula_man_(new unit_formula_manager()),
|
||||
movement_(0),
|
||||
max_movement_(0),
|
||||
vision_(-1),
|
||||
|
@ -386,22 +378,7 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg) :
|
|||
|
||||
if (const config &ai = cfg.child("ai"))
|
||||
{
|
||||
unit_formula_ = ai["formula"].str();
|
||||
unit_loop_formula_ = ai["loop_formula"].str();
|
||||
unit_priority_formula_ = ai["priority"].str();
|
||||
|
||||
if (const config &ai_vars = ai.child("vars"))
|
||||
{
|
||||
formula_vars_ = new game_logic::map_formula_callable;
|
||||
|
||||
variant var;
|
||||
BOOST_FOREACH(const config::attribute &i, ai_vars.attribute_range()) {
|
||||
var.serialize_from_string(i.second);
|
||||
formula_vars_->add(i.first, var);
|
||||
}
|
||||
} else {
|
||||
formula_vars_ = game_logic::map_formula_callable_ptr();
|
||||
}
|
||||
formula_man_->read(ai);
|
||||
}
|
||||
|
||||
//don't use the unit_type's attacks if this config has its own defined
|
||||
|
@ -560,10 +537,7 @@ unit::unit(const unit_type &u_type, int side, bool real_unit,
|
|||
gender_(gender != unit_race::NUM_GENDERS ?
|
||||
gender : generate_gender(u_type, real_unit)),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
unit_priority_formula_(),
|
||||
formula_vars_(),
|
||||
formula_man_(new unit_formula_manager()),
|
||||
movement_(0),
|
||||
max_movement_(0),
|
||||
vision_(-1),
|
||||
|
@ -1643,9 +1617,7 @@ bool unit::internal_matches_filter(const vconfig& cfg, const map_location& loc,
|
|||
}
|
||||
config::attribute_value cfg_formula = cfg["formula"];
|
||||
if (!cfg_formula.blank()) {
|
||||
const unit_callable callable(loc,*this);
|
||||
const game_logic::formula form(cfg_formula);
|
||||
if(!form.evaluate(callable).as_bool()) {///@todo use formula_ai
|
||||
if (!formula_man_->matches_filter(cfg_formula, loc, *this)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1683,36 +1655,8 @@ void unit::write(config& cfg) const
|
|||
|
||||
//support for unit formulas in [ai] and unit-specific variables in [ai] [vars]
|
||||
|
||||
if ( has_formula() || has_loop_formula() || (formula_vars_ && formula_vars_->empty() == false) ) {
|
||||
formula_man_->write(cfg);
|
||||
|
||||
config &ai = cfg.add_child("ai");
|
||||
|
||||
if (has_formula())
|
||||
ai["formula"] = unit_formula_;
|
||||
|
||||
if (has_loop_formula())
|
||||
ai["loop_formula"] = unit_loop_formula_;
|
||||
|
||||
if (has_priority_formula())
|
||||
ai["priority"] = unit_priority_formula_;
|
||||
|
||||
|
||||
if (formula_vars_ && formula_vars_->empty() == false)
|
||||
{
|
||||
config &ai_vars = ai.add_child("vars");
|
||||
|
||||
std::string str;
|
||||
for(game_logic::map_formula_callable::const_iterator i = formula_vars_->begin(); i != formula_vars_->end(); ++i)
|
||||
{
|
||||
i->second.serialize_to_string(str);
|
||||
if (!str.empty())
|
||||
{
|
||||
ai_vars[i->first] = str;
|
||||
str.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cfg["gender"] = gender_string(gender_);
|
||||
cfg["variation"] = variation_;
|
||||
|
@ -1780,11 +1724,6 @@ void unit::write(config& cfg) const
|
|||
|
||||
}
|
||||
|
||||
void unit::add_formula_var(std::string str, variant var) {
|
||||
if(!formula_vars_) formula_vars_ = new game_logic::map_formula_callable;
|
||||
formula_vars_->add(str, var);
|
||||
}
|
||||
|
||||
const surface unit::still_image(bool scaled) const
|
||||
{
|
||||
image::locator image_loc;
|
||||
|
|
16
src/unit.hpp
16
src/unit.hpp
|
@ -20,7 +20,6 @@
|
|||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
|
||||
#include "formula_callable.hpp"
|
||||
#include "unit_animation.hpp"
|
||||
#include "unit_types.hpp"
|
||||
#include "unit_map.hpp"
|
||||
|
@ -28,6 +27,7 @@
|
|||
class display;
|
||||
class gamemap;
|
||||
class team;
|
||||
class unit_formula_manager;
|
||||
class vconfig;
|
||||
|
||||
/// The things contained within a unit_ability_list.
|
||||
|
@ -370,14 +370,7 @@ public:
|
|||
std::vector<std::string> get_ability_list() const;
|
||||
bool has_ability_type(const std::string& ability) const;
|
||||
|
||||
const game_logic::map_formula_callable_ptr& formula_vars() const { return formula_vars_; }
|
||||
void add_formula_var(std::string str, variant var);
|
||||
bool has_formula() const { return !unit_formula_.empty(); }
|
||||
bool has_loop_formula() const { return !unit_loop_formula_.empty(); }
|
||||
bool has_priority_formula() const { return !unit_priority_formula_.empty(); }
|
||||
const std::string& get_formula() const { return unit_formula_; }
|
||||
const std::string& get_loop_formula() const { return unit_loop_formula_; }
|
||||
const std::string& get_priority_formula() const { return unit_priority_formula_; }
|
||||
unit_formula_manager & formula_manager() const { return *formula_man_; }
|
||||
|
||||
void backup_state();
|
||||
void apply_modifications();
|
||||
|
@ -451,10 +444,7 @@ private:
|
|||
|
||||
fixed_t alpha_;
|
||||
|
||||
std::string unit_formula_;
|
||||
std::string unit_loop_formula_;
|
||||
std::string unit_priority_formula_;
|
||||
game_logic::map_formula_callable_ptr formula_vars_;
|
||||
boost::scoped_ptr<unit_formula_manager> formula_man_;
|
||||
|
||||
int movement_;
|
||||
int max_movement_;
|
||||
|
|
80
src/unit_formula_manager.cpp
Normal file
80
src/unit_formula_manager.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include "unit_formula_manager.hpp"
|
||||
|
||||
#include "callable_objects.hpp"
|
||||
#include "config.hpp"
|
||||
#include "formula.hpp"
|
||||
#include "formula_string_utils.hpp"
|
||||
#include "map_location.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
bool unit_formula_manager::matches_filter(const std::string & cfg_formula, const map_location & loc, const unit & me)
|
||||
{
|
||||
const unit_callable callable(loc,me);
|
||||
const game_logic::formula form(cfg_formula);
|
||||
if(!form.evaluate(callable).as_bool()) {///@todo use formula_ai
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void unit_formula_manager::add_formula_var(std::string str, variant var)
|
||||
{
|
||||
if(!formula_vars_) formula_vars_ = new game_logic::map_formula_callable;
|
||||
formula_vars_->add(str, var);
|
||||
}
|
||||
|
||||
void unit_formula_manager::read(const config & ai)
|
||||
{
|
||||
unit_formula_ = ai["formula"].str();
|
||||
unit_loop_formula_ = ai["loop_formula"].str();
|
||||
unit_priority_formula_ = ai["priority"].str();
|
||||
|
||||
if (const config &ai_vars = ai.child("vars"))
|
||||
{
|
||||
formula_vars_ = new game_logic::map_formula_callable;
|
||||
|
||||
variant var;
|
||||
BOOST_FOREACH(const config::attribute &i, ai_vars.attribute_range()) {
|
||||
var.serialize_from_string(i.second);
|
||||
formula_vars_->add(i.first, var);
|
||||
}
|
||||
} else {
|
||||
formula_vars_ = game_logic::map_formula_callable_ptr();
|
||||
}
|
||||
}
|
||||
|
||||
void unit_formula_manager::write(config & cfg)
|
||||
{
|
||||
if ( has_formula() || has_loop_formula() || (formula_vars_ && formula_vars_->empty() == false) ) {
|
||||
|
||||
config &ai = cfg.add_child("ai");
|
||||
|
||||
if (has_formula())
|
||||
ai["formula"] = unit_formula_;
|
||||
|
||||
if (has_loop_formula())
|
||||
ai["loop_formula"] = unit_loop_formula_;
|
||||
|
||||
if (has_priority_formula())
|
||||
ai["priority"] = unit_priority_formula_;
|
||||
|
||||
|
||||
if (formula_vars_ && formula_vars_->empty() == false)
|
||||
{
|
||||
config &ai_vars = ai.add_child("vars");
|
||||
|
||||
std::string str;
|
||||
for(game_logic::map_formula_callable::const_iterator i = formula_vars_->begin(); i != formula_vars_->end(); ++i)
|
||||
{
|
||||
i->second.serialize_to_string(str);
|
||||
if (!str.empty())
|
||||
{
|
||||
ai_vars[i->first] = str;
|
||||
str.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
58
src/unit_formula_manager.hpp
Normal file
58
src/unit_formula_manager.hpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UNIT_FORMULA_MANAGER_HPP
|
||||
#define UNIT_FORMULA_MANAGER_HPP
|
||||
|
||||
#include "formula_callable.hpp" //map_formula_callable_ptr is an intrusive ptr, requires full header to compile
|
||||
|
||||
#include<string>
|
||||
|
||||
class config;
|
||||
struct map_location;
|
||||
class unit;
|
||||
|
||||
class unit_formula_manager {
|
||||
public:
|
||||
unit_formula_manager() {}
|
||||
unit_formula_manager(const unit_formula_manager & o) :
|
||||
unit_formula_(o.unit_formula_),
|
||||
unit_loop_formula_(o.unit_loop_formula_),
|
||||
unit_priority_formula_(o.unit_priority_formula_),
|
||||
formula_vars_(o.formula_vars_ ? new game_logic::map_formula_callable(*o.formula_vars_) : o.formula_vars_)
|
||||
{}
|
||||
|
||||
const game_logic::map_formula_callable_ptr& formula_vars() const { return formula_vars_; }
|
||||
void add_formula_var(std::string str, variant var);
|
||||
bool has_formula() const { return !unit_formula_.empty(); }
|
||||
bool has_loop_formula() const { return !unit_loop_formula_.empty(); }
|
||||
bool has_priority_formula() const { return !unit_priority_formula_.empty(); }
|
||||
const std::string& get_formula() const { return unit_formula_; }
|
||||
const std::string& get_loop_formula() const { return unit_loop_formula_; }
|
||||
const std::string& get_priority_formula() const { return unit_priority_formula_; }
|
||||
|
||||
bool matches_filter( const std::string & cfg_formula, const map_location & loc, const unit & me);
|
||||
|
||||
void read(const config & ai);
|
||||
void write(config & cfg);
|
||||
|
||||
private:
|
||||
std::string unit_formula_;
|
||||
std::string unit_loop_formula_;
|
||||
std::string unit_priority_formula_;
|
||||
game_logic::map_formula_callable_ptr formula_vars_;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue