allow [modify_ai] to work with stages and goals
This commit is contained in:
parent
9b43193936
commit
e24fdb0657
8 changed files with 180 additions and 65 deletions
|
@ -50,7 +50,7 @@ void ai_composite::on_create()
|
|||
|
||||
// init the composite ai stages
|
||||
foreach(const config &cfg_element, cfg_.child_range("stage")){
|
||||
engine::parse_stage_from_config(*this,cfg_element,std::back_inserter(stages_));
|
||||
add_stage(-1,cfg_element);
|
||||
}
|
||||
|
||||
config cfg;
|
||||
|
@ -62,11 +62,44 @@ void ai_composite::on_create()
|
|||
|
||||
}
|
||||
|
||||
|
||||
ai_composite::~ai_composite()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool ai_composite::add_stage(int pos, const config &cfg)
|
||||
{
|
||||
if (pos<0) {
|
||||
pos = stages_.size();
|
||||
}
|
||||
std::vector< stage_ptr > stages;
|
||||
engine::parse_stage_from_config(*this,cfg,std::back_inserter(stages));
|
||||
int j=0;
|
||||
foreach (stage_ptr b, stages ){
|
||||
stages_.insert(stages_.begin()+pos+j,b);
|
||||
j++;
|
||||
}
|
||||
return (j>0);
|
||||
}
|
||||
|
||||
|
||||
bool ai_composite::add_goal(int pos, const config &cfg)
|
||||
{
|
||||
if (pos<0) {
|
||||
pos = get_goals().size();
|
||||
}
|
||||
std::vector< goal_ptr > goals;
|
||||
engine::parse_goal_from_config(*this,cfg,std::back_inserter(goals));
|
||||
int j=0;
|
||||
foreach (goal_ptr b, goals ){
|
||||
get_goals().insert(get_goals().begin()+pos+j,b);
|
||||
j++;
|
||||
}
|
||||
return (j>0);
|
||||
}
|
||||
|
||||
|
||||
void ai_composite::play_turn(){
|
||||
foreach(stage_ptr &s, stages_){
|
||||
s->play_stage();
|
||||
|
@ -126,42 +159,28 @@ config ai_composite::to_config() const
|
|||
|
||||
component* ai_composite::get_child(const path_element &child)
|
||||
{
|
||||
//@todo 1.7.4 implement
|
||||
if (child.property=="aspect") {
|
||||
//ASPECT
|
||||
if (child.id.empty()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
aspect_map::const_iterator a = get_aspects().find(child.id);
|
||||
if (a==get_aspects().end()){
|
||||
return NULL;
|
||||
} else {
|
||||
if (a!=get_aspects().end()){
|
||||
return &*a->second;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
} else if (child.property=="stage") {
|
||||
//STAGE
|
||||
if (child.id.empty()) {
|
||||
if ( (child.position<0) || (child.position>=static_cast<int>(stages_.size())) ) {
|
||||
return NULL;
|
||||
} else {
|
||||
//@todo 1.7.4 implement
|
||||
return NULL;
|
||||
//return &stages_.at(pos);
|
||||
}
|
||||
}
|
||||
//foreach(const stage_ptr &s, stages_) {
|
||||
//if (s->get_id()==child.id) {
|
||||
//@todo 1.7.4 implement
|
||||
//return &*s;
|
||||
//}
|
||||
//}
|
||||
//std::vector< stage_ptr >::iterator i = std::find_if(stages_.begin(),stages_.end(),path_element_matches< stage_ptr >(child));
|
||||
//if (i!=stages_.end()){
|
||||
// return &*(*i);
|
||||
//}
|
||||
return NULL;
|
||||
} else if (child.property=="engine") {
|
||||
//ENGINE
|
||||
//@todo 1.7.5 implement
|
||||
return NULL;
|
||||
} else if (child.property=="goal") {
|
||||
//GOAL
|
||||
//std::vector< goal_ptr >::iterator i = std::find_if(get_goals().begin(),get_goals().end(),path_element_matches< goal_ptr >(child));
|
||||
//if (i!=get_goals().end()){
|
||||
// return &*(*i);
|
||||
//}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -172,21 +191,81 @@ component* ai_composite::get_child(const path_element &child)
|
|||
|
||||
bool ai_composite::add_child(const path_element &child, const config &cfg)
|
||||
{
|
||||
//@todo 1.7.4 implement
|
||||
if (child.property=="aspect") {
|
||||
//ASPECT
|
||||
return false;//adding aspects directly is not supported - aspect['foo'].facet should be added instead
|
||||
} else if (child.property=="stage") {
|
||||
std::vector< stage_ptr >::iterator i = std::find_if(stages_.begin(),stages_.end(),path_element_matches< stage_ptr >(child));
|
||||
return add_stage(i-stages_.begin(),cfg);
|
||||
} else if (child.property=="engine") {
|
||||
//ENGINE
|
||||
//@todo 1.7.5 implement
|
||||
return false;
|
||||
} else if (child.property=="goal") {
|
||||
std::vector< goal_ptr >::iterator i = std::find_if(get_goals().begin(),get_goals().end(),path_element_matches< goal_ptr >(child));
|
||||
return add_goal(i-get_goals().begin(),cfg);
|
||||
}
|
||||
|
||||
//OOPS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool ai_composite::change_child(const path_element &child, const config &cfg)
|
||||
{
|
||||
//@todo 1.7.4 implement
|
||||
if (child.property=="aspect") {
|
||||
//ASPECT
|
||||
return false;//changing aspects directly is not supported - aspect['foo'].facet should be changed instead
|
||||
} else if (child.property=="stage") {
|
||||
std::vector< stage_ptr >::iterator i = std::find_if(stages_.begin(),stages_.end(),path_element_matches< stage_ptr >(child));
|
||||
if (i!=stages_.end()) {
|
||||
//@todo 1.7.4 implement
|
||||
//return (*i)->redeploy(cfg);
|
||||
}
|
||||
return false;
|
||||
|
||||
} else if (child.property=="engine") {
|
||||
//ENGINE
|
||||
//@todo 1.7.5 implement
|
||||
return false;
|
||||
} else if (child.property=="goal") {
|
||||
std::vector< goal_ptr >::iterator i = std::find_if(get_goals().begin(),get_goals().end(),path_element_matches< goal_ptr >(child));
|
||||
if (i!=get_goals().end()) {
|
||||
return (*i)->redeploy(cfg);
|
||||
}
|
||||
}
|
||||
|
||||
//OOPS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool ai_composite::delete_child(const path_element &child)
|
||||
{
|
||||
//@todo 1.7.4 implement
|
||||
if (child.property=="aspect") {
|
||||
//ASPECT
|
||||
return false;//deleting aspects directly is not supported - aspect['foo'].facet should be deleted instead
|
||||
} else if (child.property=="stage") {
|
||||
std::vector< stage_ptr >::iterator i = std::find_if(stages_.begin(),stages_.end(),path_element_matches< stage_ptr >(child));
|
||||
if (i!=stages_.end()) {
|
||||
stages_.erase(i);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} else if (child.property=="engine") {
|
||||
//ENGINE
|
||||
//@todo 1.7.5 implement
|
||||
return false;
|
||||
} else if (child.property=="goal") {
|
||||
std::vector< goal_ptr >::iterator i = std::find_if(get_goals().begin(),get_goals().end(),path_element_matches< goal_ptr >(child));
|
||||
if (i!=get_goals().end()) {
|
||||
get_goals().erase(i);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//OOPS
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -90,6 +90,12 @@ public:
|
|||
void switch_side(side_number side);
|
||||
|
||||
|
||||
virtual bool add_goal(int pos, const config &cfg);
|
||||
|
||||
|
||||
virtual bool add_stage(int pos, const config &cfg);
|
||||
|
||||
|
||||
void on_create();
|
||||
|
||||
/**
|
||||
|
|
|
@ -711,32 +711,6 @@ protected:
|
|||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
class path_element_matches{
|
||||
public:
|
||||
path_element_matches(const path_element &element)
|
||||
: count_(0), element_(element)
|
||||
{
|
||||
}
|
||||
virtual ~path_element_matches(){}
|
||||
|
||||
bool operator()(const T& t)
|
||||
{
|
||||
if ( (!element_.id.empty()) && (element_.id == t->get_id()) ) {
|
||||
return true;
|
||||
}
|
||||
if (count_ == element_.position) {
|
||||
return true;
|
||||
}
|
||||
count_++;
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
int count_;
|
||||
path_element element_;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class composite_aspect : public typesafe_aspect<T> {
|
||||
public:
|
||||
|
@ -825,7 +799,7 @@ public:
|
|||
return false;
|
||||
}
|
||||
typename std::vector< boost::shared_ptr< typesafe_aspect<T> > >::iterator i = std::find_if(facets_.begin(),facets_.end(),path_element_matches< boost::shared_ptr< typesafe_aspect<T> > >(child));
|
||||
LOG_STREAM(debug, aspect::log()) << "adding a new child facet to composite aspect["<<this->get_id()<<"]"<< std::endl;
|
||||
LOG_STREAM(info, aspect::log()) << "adding a new child facet to composite aspect["<<this->get_id()<<"]"<< std::endl;
|
||||
return add_facet(i-facets_.begin(),cfg);
|
||||
}
|
||||
|
||||
|
|
|
@ -60,6 +60,35 @@ public:
|
|||
static bool delete_component(component *root, const std::string &path);
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
class path_element_matches{
|
||||
public:
|
||||
path_element_matches(const path_element &element)
|
||||
: count_(0), element_(element)
|
||||
{
|
||||
}
|
||||
virtual ~path_element_matches(){}
|
||||
|
||||
bool operator()(const T& t)
|
||||
{
|
||||
if ( (!element_.id.empty()) && (element_.id == t->get_id()) ) {
|
||||
return true;
|
||||
}
|
||||
if (count_ == element_.position) {
|
||||
return true;
|
||||
}
|
||||
count_++;
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
int count_;
|
||||
path_element element_;
|
||||
};
|
||||
|
||||
|
||||
} //end of namespace ai
|
||||
|
||||
|
||||
|
|
|
@ -34,20 +34,20 @@ static lg::log_domain log_ai_composite_goal("ai/composite/goal");
|
|||
goal::goal(readonly_context &/*context*/, const config &cfg)
|
||||
: cfg_(cfg), value_()
|
||||
{
|
||||
if (cfg.has_attribute("value")) {
|
||||
try {
|
||||
value_ = boost::lexical_cast<double>(cfg["value"]);
|
||||
} catch (boost::bad_lexical_cast){
|
||||
ERR_AI_COMPOSITE_GOAL << "bad value of goal"<<std::endl;
|
||||
value_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void goal::on_create()
|
||||
{
|
||||
if (cfg_.has_attribute("value")) {
|
||||
try {
|
||||
value_ = boost::lexical_cast<double>(cfg_["value"]);
|
||||
} catch (boost::bad_lexical_cast){
|
||||
ERR_AI_COMPOSITE_GOAL << "bad value of goal"<<std::endl;
|
||||
value_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -73,4 +73,17 @@ config goal::to_config() const
|
|||
}
|
||||
|
||||
|
||||
const std::string& goal::get_id() const
|
||||
{
|
||||
return cfg_["id"];
|
||||
}
|
||||
|
||||
|
||||
bool goal::redeploy(const config &cfg)
|
||||
{
|
||||
cfg_ = cfg;
|
||||
on_create();
|
||||
return true;
|
||||
}
|
||||
|
||||
} //end of namespace ai
|
||||
|
|
|
@ -50,6 +50,12 @@ public:
|
|||
void on_create();
|
||||
|
||||
|
||||
const std::string& get_id() const;
|
||||
|
||||
|
||||
bool redeploy(const config &cfg);
|
||||
|
||||
|
||||
double value()
|
||||
{
|
||||
return value_;
|
||||
|
|
|
@ -66,9 +66,16 @@ config stage::to_config() const
|
|||
config cfg;
|
||||
cfg["engine"] = cfg_["engine"];
|
||||
cfg["name"] = cfg_["name"];
|
||||
cfg["id"] = cfg_["id"];
|
||||
return cfg;
|
||||
}
|
||||
|
||||
|
||||
const std::string& stage::get_id() const
|
||||
{
|
||||
return cfg_["id"];
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
// COMPOSITE AI IDLE STAGE
|
||||
// =======================================================================
|
||||
|
|
|
@ -77,6 +77,7 @@ public:
|
|||
virtual config to_config() const;
|
||||
|
||||
|
||||
virtual const std::string& get_id() const;
|
||||
protected:
|
||||
/**
|
||||
* Play the turn - implementation
|
||||
|
|
Loading…
Add table
Reference in a new issue