mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-21 19:07:16 +00:00
Removed AI that has been obsolete and broken for several years.
This commit is contained in:
parent
552ee91564
commit
99a3190fca
@ -180,7 +180,6 @@ set(wesnoth-main_SRC
|
||||
ai/default/contexts.cpp
|
||||
ai/default/move.cpp
|
||||
ai/default/village.cpp
|
||||
ai/dfool/ai.cpp
|
||||
ai/formula/ai.cpp
|
||||
ai/formula/callable_objects.cpp
|
||||
ai/formula/candidates.cpp
|
||||
|
@ -60,7 +60,6 @@ wesnoth_source = \
|
||||
ai/default/contexts.cpp \
|
||||
ai/default/move.cpp \
|
||||
ai/default/village.cpp \
|
||||
ai/dfool/ai.cpp \
|
||||
ai/formula/ai.cpp \
|
||||
ai/formula/callable_objects.cpp \
|
||||
ai/formula/candidates.cpp \
|
||||
|
@ -165,7 +165,6 @@ wesnoth_sources = Split("""
|
||||
ai/default/contexts.cpp
|
||||
ai/default/move.cpp
|
||||
ai/default/village.cpp
|
||||
ai/dfool/ai.cpp
|
||||
ai/formula/ai.cpp
|
||||
ai/formula/callable_objects.cpp
|
||||
ai/formula/candidates.cpp
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
#include "ai.hpp"
|
||||
|
||||
#include "../dfool/ai.hpp"
|
||||
#include "../actions.hpp"
|
||||
#include "../manager.hpp"
|
||||
#include "../formula/ai.hpp"
|
||||
|
@ -1,688 +0,0 @@
|
||||
/* $Id$ */
|
||||
/*
|
||||
Copyright (C) 2007 - 2009
|
||||
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 version 2
|
||||
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.
|
||||
*/
|
||||
|
||||
/** @file ai/dfool/ai.cpp */
|
||||
|
||||
#include "../../global.hpp"
|
||||
|
||||
#include "ai.hpp"
|
||||
#include "../actions.hpp"
|
||||
|
||||
#include "../../foreach.hpp"
|
||||
#include "../../log.hpp"
|
||||
#include "../../formula_string_utils.hpp"
|
||||
|
||||
namespace ai {
|
||||
|
||||
namespace dfool {
|
||||
|
||||
static lg::log_domain log_ai("ai/dfool");
|
||||
#define LOG_AI LOG_STREAM(info, log_ai)
|
||||
|
||||
void dfool_ai::play_turn(){
|
||||
int team_num = get_side();
|
||||
const config& parms = cfg_;
|
||||
|
||||
LOG_AI << "dfool side:" << team_num << " of " << current_team().nteams() << '\n';
|
||||
|
||||
config side_filter;
|
||||
char buf[80];
|
||||
snprintf(buf, sizeof(buf), "%d", team_num);
|
||||
side_filter["side"]=buf;
|
||||
|
||||
LOG_AI << "dfool sees:\n";
|
||||
|
||||
// for(unit_map::iterator ua = get_info().units.begin(); ua != get_info().units.end(); ++ua) {
|
||||
// std::string t = ua->second.get_ai_special();
|
||||
// LOG_STREAM(info, ai)<<"ua:"<<ua->second.underlying_id()<<"\t"<<t<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<ua->first.x<<","<<ua->first.y<<std::endl;
|
||||
// }
|
||||
|
||||
unit_list all = all_units();
|
||||
unit_list my_units=filter_units(side_filter, all,get_info().units);
|
||||
unit_list v_units=visible_units();
|
||||
|
||||
// LOG_STREAM(info, ai)<<"My Units"<<std::endl;
|
||||
// for(unit_list::iterator ui = all.begin(); ui != all.end(); ++ui) {
|
||||
// LOG_STREAM(info, ai)<<"\t....."<<*ui<<"........"<<std::endl;
|
||||
// unit_map::iterator u = unit(*ui,get_info().units);
|
||||
// if(u!=get_info().units.end()){
|
||||
// LOG_STREAM(info, ai)<<"\t"<<u->second.name()<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t"<<u->second.underlying_id()<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->second.get_ai_special()<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->first.x<<","<<u->first.y<<std::endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
LOG_AI << "Visible Units\n";
|
||||
for(unit_list::iterator ui = v_units.begin(); ui != v_units.end(); ++ui) {
|
||||
unit_map::iterator u = unit(*ui,get_info().units);
|
||||
if(u!=get_info().units.end()){
|
||||
// LOG_STREAM(info, ai)<<"\t"<<u->second.name()<<std::endl;
|
||||
LOG_AI << "\t\t" << u->second.underlying_id() << '\n';
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->second.get_ai_special()<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->first.x<<","<<u->first.y<<std::endl;
|
||||
|
||||
unit_memory_.add_unit_sighting(u->second, u->first, get_info().tod_manager_.turn());
|
||||
}
|
||||
}
|
||||
|
||||
foreach (const config &o, parms.child_range("order"))
|
||||
{
|
||||
std::string order_id = o["id"];
|
||||
std::string number = o["number"];
|
||||
size_t num=atoi(number.c_str());
|
||||
|
||||
LOG_AI << "dfool order:" << order_id << '\n';
|
||||
|
||||
// First find units where AI_SPECIAL matches order id
|
||||
config order_filter;
|
||||
order_filter["ai_special"]=order_id;
|
||||
unit_list order_units = filter_units(order_filter,my_units,get_info().units);
|
||||
if(num > order_units.size()){
|
||||
// Need to populate orders
|
||||
// Find units that match any filter.
|
||||
// If no filters, then accept all units.
|
||||
config::const_child_itors filters = o.child_range("filter");
|
||||
if (filters.first != filters.second)
|
||||
{
|
||||
foreach (const config &ff, filters)
|
||||
{
|
||||
// LOG_STREAM(info, ai)<<"dfool filter:"<<std::endl;
|
||||
unit_list filtered_units=filter_units(ff,my_units,get_info().units);
|
||||
|
||||
/** @todo 2.0: add sorting */
|
||||
|
||||
for(unit_list::iterator i = filtered_units.begin(); i != filtered_units.end() && (num > order_units.size()); ++i) {
|
||||
unit_map::iterator ui=unit(*i,get_info().units);
|
||||
if(ui!=get_info().units.end()){
|
||||
std::string ais=ui->second.get_ai_special();
|
||||
|
||||
// LOG_STREAM(info, ai)<<"\t match: "<<ui->second.underlying_id()<<"\t"<<ais<<":::"<<std::endl;
|
||||
|
||||
bool used=(ais.size() > 0);
|
||||
if(used){
|
||||
// LOG_STREAM(info, ai)<<"\t\talready assigned: "<<ui->second.underlying_id()<<"\t"<<ais<<std::endl;
|
||||
}else{
|
||||
ui->second.assign_ai_special(order_id);
|
||||
order_units.push_back(*i);
|
||||
|
||||
// LOG_STREAM(info, ai)<<"\t\tmatching: "<<ui->second.underlying_id()<<" to order: "<<order_id<<"\t"<<ui->second.get_ai_special()<<std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
order_units=my_units;
|
||||
}
|
||||
}
|
||||
|
||||
// Execute commands
|
||||
foreach (size_t ou, order_units)
|
||||
{
|
||||
bool com_break = false;
|
||||
|
||||
foreach (const config &com, o.child_range("command"))
|
||||
{
|
||||
unit_map::iterator u = unit(ou, get_info().units);
|
||||
|
||||
bool found=true;
|
||||
if(u!=get_info().units.end()){
|
||||
foreach (const config &ff, com.child_range("filter"))
|
||||
{
|
||||
LOG_AI << "ff:" << com["type"] << ' '
|
||||
<< ff["type"] << ' ' << ff["x"] << ',' << ff["y"] << '\n';
|
||||
LOG_AI << "ff?" << u->second.type_id() << " "
|
||||
<< u->first.x << ',' << u->first.y << '\n';
|
||||
if (!u->second.matches_filter(vconfig(ff), u->first)) {
|
||||
found=false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(found){
|
||||
std::string type = com["type"];
|
||||
std::string e = com["test"];
|
||||
std::map<std::string, evaluator*> function_map;
|
||||
arithmetic_evaluator eval(get_info().game_state_,&function_map);
|
||||
distance_evaluator dist(get_info().game_state_,&function_map);
|
||||
function_map["eval"]=&eval;
|
||||
function_map["distance"]=&dist;
|
||||
LOG_AI << "eval: " << type << ':' << e << " = " << eval.value(e) << '\n';
|
||||
|
||||
LOG_AI << "\tcommand: " << type << '\n';
|
||||
if(type=="moveto"){
|
||||
moveto(com,u);
|
||||
}
|
||||
if(type=="set_order"){
|
||||
std::string set_id = com["id"];
|
||||
std::string a=(u->second.get_ai_special());
|
||||
LOG_AI << "\t\t" << u->second.underlying_id() << '\t' << a << "->" << set_id << '\n';
|
||||
(u->second.assign_ai_special(set_id));
|
||||
a=(u->second.get_ai_special());
|
||||
LOG_AI << "\t\t" << u->second.underlying_id() << '\t' << a << " =?= " << set_id << '\n';
|
||||
}
|
||||
if(type=="break"){
|
||||
com_break=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//unit_memory_.write(ai_mem);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::string dfool_ai::describe_self()
|
||||
{
|
||||
return "[dfool_ai]";
|
||||
}
|
||||
|
||||
unit_list dfool_ai::filter_units(const config& filter, unit_list& ul, unit_map& um)
|
||||
{
|
||||
// LOG_STREAM(info, ai)<<"filter1:"<<std::endl;
|
||||
unit_list filtered_units_;
|
||||
for(unit_list::const_iterator i = ul.begin(); i != ul.end(); ++i) {
|
||||
// LOG_STREAM(info, ai)<<"filter2:"<<std::endl;
|
||||
unit_map::iterator j = unit(*i,um);
|
||||
// LOG_STREAM(info, ai)<<"j:"<<j->second.underlying_id()<<":"<<j->first.x<<","<<j->first.y<<std::endl;
|
||||
assert(j->second.underlying_id() >0);
|
||||
// LOG_STREAM(info, ai)<<"filter3:"<<std::endl;
|
||||
if (j->second.matches_filter(vconfig(filter), j->first)) {
|
||||
// LOG_STREAM(info, ai)<<"filter4:"<<std::endl;
|
||||
filtered_units_.push_back(*i);
|
||||
}
|
||||
|
||||
}
|
||||
// LOG_STREAM(info, ai)<<"filter:"<<std::endl;
|
||||
return filtered_units_;
|
||||
}
|
||||
|
||||
unit_list dfool_ai::visible_units()
|
||||
{
|
||||
unit_list visible_units;
|
||||
bool no_fog=current_team().uses_shroud() == false && current_team().uses_fog() == false;
|
||||
|
||||
const unit_map& um=get_info().units;
|
||||
for(unit_map::const_iterator i = um.begin(); i != um.end(); ++i) {
|
||||
bool hidden_by_fog = current_team().fogged(i->first);
|
||||
bool hidden = i->second.invisible(i->first, um, get_info().teams);
|
||||
if((no_fog || !hidden_by_fog) && !hidden) {
|
||||
visible_units.push_back(i->second.underlying_id());
|
||||
}
|
||||
}
|
||||
|
||||
LOG_AI << "number of visible units: " << visible_units.size() << '\n';
|
||||
return visible_units;
|
||||
}
|
||||
|
||||
unit_list dfool_ai::all_units(){
|
||||
unit_list all;
|
||||
const unit_map& um=get_info().units;
|
||||
for(unit_map::const_iterator i = um.begin(); i != um.end(); ++i) {
|
||||
// LOG_STREAM(info, ai)<<"all:"<<i->second.underlying_id()<<std::endl;
|
||||
all.push_back(i->second.underlying_id());
|
||||
}
|
||||
return(all);
|
||||
}
|
||||
|
||||
bool dfool_ai::moveto(const config &o, unit_map::const_iterator m)
|
||||
{
|
||||
map_location target(atoi(o["target_x"].c_str()) - 1, atoi(o["target_y"].c_str()) - 1);
|
||||
LOG_AI << "\tmoving to:(" << target.x << ',' << target.y << ")\n";
|
||||
bool gamestate_changed = false;
|
||||
if(m->second.movement_left()){
|
||||
moves_map possible_moves;
|
||||
move_map srcdst, dstsrc;
|
||||
unit_map known_units;
|
||||
|
||||
|
||||
// unit_memory_.known_map(known_units, get_info().tod_manager_.turn());
|
||||
unit_memory_.known_map(known_units, 0);
|
||||
|
||||
LOG_AI << "known units:\n";
|
||||
for(unit_map::const_iterator uu = known_units.begin();uu!=known_units.end();uu++){
|
||||
LOG_AI << '\t' << uu->second.underlying_id() << ' ' << uu->first << '\n';
|
||||
}
|
||||
|
||||
calculate_moves(known_units,possible_moves,srcdst,dstsrc,false,false,NULL,true);
|
||||
|
||||
int closest_distance = -1;
|
||||
std::pair<map_location,map_location> closest_move;
|
||||
|
||||
/** @todo 2.0 This undoubtedly could be done more cleanly */
|
||||
for(move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {
|
||||
// Must restrict move_map to only unit that is moving.
|
||||
if(i->second==m->first){
|
||||
const int distance = distance_between(target,i->first);
|
||||
// int distance=10000;
|
||||
// std::cout<<"got here\n";
|
||||
// const shortest_path_calculator calc(m->second, current_team(),known_units,get_info().teams,get_info().map);
|
||||
//std::cout<<"got here2\n";
|
||||
//paths::route route = a_star_search(m->first, target, 1000.0, &calc,
|
||||
//get_info().map.x(), get_info().map.y());
|
||||
// std::cout<<"got here3\n";
|
||||
|
||||
// distance = route_turns_to_complete(m->second, get_info().map, route, known_units, get_info().teams) + distance_between(target,i->first)/100;
|
||||
|
||||
if(closest_distance == -1 || distance < closest_distance) {
|
||||
closest_distance = distance;
|
||||
closest_move = *i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG_AI << "\tmoving : " << m->second.underlying_id() << " from ("
|
||||
<< closest_move.first.x << ',' << closest_move.first.y << ") to ("
|
||||
<< target.x << ',' << target.y << ")\n";
|
||||
LOG_AI << "\tdistance: " << closest_distance << '\n';
|
||||
|
||||
if(closest_distance != -1) {
|
||||
move_result_ptr move_res = execute_move_action(closest_move.second,closest_move.first,false);
|
||||
gamestate_changed |= move_res->is_gamestate_changed();
|
||||
if (move_res->is_ok()) {
|
||||
LOG_AI << "error in move" << std::endl;
|
||||
}
|
||||
if(move_res->get_unit_location() != closest_move.second)
|
||||
return(gamestate_changed); // Something unexpected happened
|
||||
}
|
||||
}
|
||||
return(gamestate_changed);
|
||||
}
|
||||
|
||||
unit_map::iterator dfool_ai::unit(size_t unit_id, unit_map& um){
|
||||
// LOG_STREAM(info, ai)<<"unit start:"<<unit_id<<std::endl;
|
||||
for(unit_map::iterator u = um.begin(); u != um.end(); u++){
|
||||
if(unit_id == u->second.underlying_id()){
|
||||
// LOG_STREAM(info, ai)<<"unit:"<<unit_id<<" , "<< u->second.underlying_id()<<std::endl;
|
||||
return u;
|
||||
}
|
||||
}
|
||||
// LOG_STREAM(info, ai)<<"nounit:"<<std::endl;
|
||||
return(um.end());
|
||||
}
|
||||
|
||||
unit_memory::unit_memory(const config& cfg) :
|
||||
units_(),
|
||||
ids_(),
|
||||
turns_(),
|
||||
locations_()
|
||||
{
|
||||
foreach (const config &mem, cfg.child_range("unit_memory"))
|
||||
{
|
||||
config unit_cfg = mem.child_or_empty("unit");
|
||||
|
||||
unit u(unit_cfg);
|
||||
|
||||
int t = atoi(mem["turn"].c_str());
|
||||
map_location l(atoi(mem["x"].c_str()) - 1, atoi(mem["y"].c_str()) - 1);
|
||||
add_unit_sighting(u,l,t);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::add_unit_sighting(const unit& u, const map_location& l, size_t t){
|
||||
size_t unit_id= u.underlying_id();
|
||||
// Check if this unit has already been seen
|
||||
size_t i,j;
|
||||
for(i=0; i < ids_.size();i++){
|
||||
if(unit_id == ids_[i]){break;}
|
||||
}
|
||||
|
||||
if(i == ids_.size()){
|
||||
// Unit has not been seen
|
||||
units_.push_back(u);
|
||||
ids_.push_back(unit_id);
|
||||
turns_.push_back(t);
|
||||
locations_.push_back(l);
|
||||
}else{
|
||||
// Update unit info
|
||||
units_[i]=u;
|
||||
turns_[i]=t;
|
||||
locations_[i]=l;
|
||||
}
|
||||
|
||||
// Remove units that are co-located units
|
||||
std::set<size_t> remove_list;
|
||||
for(j=0; j < ids_.size();j++){
|
||||
if(j!=i && locations_[j] == locations_[i]){
|
||||
remove_list.insert(j);
|
||||
}
|
||||
}
|
||||
for(std::set<size_t>::const_iterator k=remove_list.begin();k!=remove_list.end();k++){
|
||||
remove_unit_sighting(ids_[*k]);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::remove_unit_sighting(size_t id){
|
||||
size_t i;
|
||||
for(i=0;i<ids_.size();i++){
|
||||
if(id == ids_[i]){break;}
|
||||
}
|
||||
|
||||
if(i == ids_.size()){
|
||||
// Unit not in memory
|
||||
}else{
|
||||
// Remove unit info
|
||||
units_.erase(units_.begin()+i);
|
||||
ids_.erase(ids_.begin()+i);
|
||||
locations_.erase(locations_.begin()+i);
|
||||
turns_.erase(turns_.begin()+i);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::write(config& temp){
|
||||
// std::cout<<"ai_write:\n";
|
||||
for(size_t i = 0; i < units_.size(); i++){
|
||||
config element;
|
||||
write_element(i, element);
|
||||
temp.add_child("unit_memory",element);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::write_element(int i, config &temp){
|
||||
config temp_unit;
|
||||
std::stringstream ts,xs,ys;
|
||||
ts << turns_[i];;
|
||||
temp["turn"] = ts.str();
|
||||
xs << locations_[i].x;
|
||||
temp["x"] = xs.str();
|
||||
ys << locations_[i].y;
|
||||
temp["y"] = ys.str();
|
||||
units_[i].write(temp_unit);
|
||||
temp.add_child("unit",temp_unit);
|
||||
// std::cout<<"ai write: "<<temp_unit["id"]<<"\n";
|
||||
}
|
||||
|
||||
void unit_memory::known_map(unit_map& u, size_t turn){
|
||||
size_t i;
|
||||
std::map<map_location,size_t> turn_used;
|
||||
for(i=0;i<units_.size();i++){
|
||||
map_location l = locations_[i];
|
||||
size_t t = turn_used[l];
|
||||
if(turns_[i] >= turn && turns_[i] >= t){
|
||||
// std::cout<<"turn_used: "<< t <<"\n";
|
||||
// std::cout<<"turn: "<< turns_[i] <<"\n";
|
||||
turn_used[l] = t;
|
||||
if(t != 0){
|
||||
u.replace(l, units_[i]);
|
||||
}else{
|
||||
std::cout<<"id: "<< ids_[i] <<"\n";
|
||||
|
||||
u.add(l, units_[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string evaluator::value(const std::string& val_string){
|
||||
std::string temp_string = val_string;
|
||||
|
||||
std::vector<std::string> p = utils::paranthetical_split(val_string,0,"(",")");
|
||||
|
||||
// Find function calls designated by @ and evaluate values inside ()
|
||||
std::string func;
|
||||
std::stringstream tot;
|
||||
std::cout<<"got here eval:"<<val_string<<"\n";
|
||||
bool function=false;
|
||||
for(size_t i=0;i!=p.size();i++){
|
||||
std::stringstream ptemp;
|
||||
if(i%2){
|
||||
if(function){
|
||||
std::cout<<"function: "<<func<<"\n";
|
||||
std::map<std::string, evaluator*>::iterator fmi =
|
||||
function_map_->find(func);
|
||||
if(fmi != function_map_->end()){ // evaluate function
|
||||
std::cout<<"function ::: "<<func<<" ::: "<<fmi->first<<"\n";
|
||||
evaluator& f=*(fmi->second);
|
||||
ptemp<<f.value(p[i]);
|
||||
p[i]=ptemp.str();
|
||||
}else{ // error
|
||||
LOG_AI << "error: evaluator function undefined: " << func << '\n';
|
||||
p[i]="ERR";
|
||||
}
|
||||
}else if(p[i].size()>0 ){
|
||||
p[i]="("+p[i]+")";
|
||||
}
|
||||
function=false;
|
||||
}else{
|
||||
std::string t=p[i];
|
||||
std::cout<<"got here: t :"<<t<<"\n";
|
||||
std::vector<std::string> temp=utils::split(t,'@');
|
||||
std::cout<<"got here: temp :"<<temp.size()<<"\n";
|
||||
|
||||
if(std::find(t.begin(),t.end(),'@') != t.end()){
|
||||
function=true;
|
||||
}
|
||||
|
||||
if(temp.size()>2){
|
||||
LOG_AI << "evaluator syntax error:\n\t" << val_string << '\n';
|
||||
}
|
||||
std::cout<<"eval size:"<<temp.size()<<"\n";
|
||||
|
||||
if(function && temp.size()>0){
|
||||
std::cout<<"temp "<<temp[0]<<"\n";
|
||||
if(temp.size()==2){
|
||||
p[i]=temp[0];
|
||||
func=temp[1];
|
||||
}else{
|
||||
std::cout<<"got lost::"<<temp[0]<<"\n";
|
||||
p[i]="";
|
||||
func=temp[0];
|
||||
}
|
||||
}else{
|
||||
p[i]=temp[0];
|
||||
}
|
||||
}
|
||||
tot<<p[i];
|
||||
}
|
||||
return(utils::interpolate_variables_into_string(tot.str(),state));
|
||||
}
|
||||
|
||||
std::string arithmetic_evaluator::value(const std::string& val_string){
|
||||
std::string temp = evaluator::value(val_string); // calculate WML variables
|
||||
std::list<std::string> tokens = parse_tokens(temp);
|
||||
LOG_AI << "tokens:\n";
|
||||
for(std::list<std::string>::const_iterator i=tokens.begin();i!=tokens.end();i++){
|
||||
LOG_AI << '\t' << *i << '\n';
|
||||
}
|
||||
if(tokens.size()){
|
||||
LOG_AI << "got here tokenless\n";
|
||||
temp=evaluate_tokens(tokens);
|
||||
}
|
||||
LOG_AI << "temp:" << temp << '\n';
|
||||
return temp;
|
||||
}
|
||||
|
||||
std::string arithmetic_evaluator::evaluate_tokens(std::list<std::string> &tlist){
|
||||
std::vector<std::string> op_priority;
|
||||
op_priority.push_back("^");
|
||||
op_priority.push_back("*/%");
|
||||
op_priority.push_back("+-");
|
||||
double temp=0;
|
||||
LOG_AI << "got here token\n";
|
||||
for(size_t i=0;i<op_priority.size();i++){
|
||||
tlist.remove("");
|
||||
for(std::list<std::string>::iterator token = tlist.begin();token!=tlist.end();token++){
|
||||
for(size_t j=0;j<op_priority[i].size();j++){
|
||||
std::string t;
|
||||
t+=op_priority[i][j];
|
||||
if((*token) == t){
|
||||
std::list<std::string>::iterator a=token;
|
||||
std::list<std::string>::iterator b=token;
|
||||
std::cout<<"got here token1\n";
|
||||
a--;
|
||||
b++;
|
||||
std::cout<<"atb:"<<*token<<"\n";
|
||||
// std::cout<<"atb:"<<*a<<*token<<*b<<"\n";
|
||||
if((*token)[0]=='*'){
|
||||
temp= atof((*a).c_str()) * atof((*b).c_str());
|
||||
}
|
||||
if((*token)[0]=='/'){
|
||||
temp= atof((*a).c_str()) / atof((*b).c_str());
|
||||
}
|
||||
if((*token)[0]=='%'){
|
||||
temp= std::fmod(atof((*a).c_str()), atof((*b).c_str()));
|
||||
}
|
||||
if((*token)[0]=='+'){
|
||||
temp= atof((*a).c_str()) + atof((*b).c_str());
|
||||
}
|
||||
if((*token)[0]=='-'){
|
||||
temp= atof((*a).c_str()) - atof((*b).c_str());
|
||||
}
|
||||
if((*token)[0]=='^'){
|
||||
temp= std::pow(atof((*a).c_str()),atof((*b).c_str()));
|
||||
}
|
||||
std::cout<<"got here token2:"<<temp<<"\n";
|
||||
std::stringstream r;
|
||||
r<<temp;
|
||||
*a="";
|
||||
*token="";
|
||||
*b=r.str();
|
||||
token++;
|
||||
// tlist.erase(a);
|
||||
// tlist.erase(b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tlist.remove("");
|
||||
return(*(tlist.begin()));
|
||||
}
|
||||
|
||||
std::list<std::string> arithmetic_evaluator::parse_tokens(const std::string& s){
|
||||
std::list<std::string> ret;
|
||||
std::string temp;
|
||||
std::string str="";
|
||||
std::string operators = "^*/%+-";
|
||||
std::string digits = "0123456789";
|
||||
char dpoint='.';
|
||||
std::string parenthesis="()";
|
||||
int count=0;
|
||||
size_t i;
|
||||
|
||||
for(i=0;i!=s.size();i++){ // strip out spaces
|
||||
if(s[i]!=' '){
|
||||
str+=s[i];
|
||||
}
|
||||
}
|
||||
|
||||
i=0;
|
||||
while(i!=str.size()){
|
||||
std::cout<<"i:"<<i<<"\n";
|
||||
if(0==count){
|
||||
ret.push_back("");
|
||||
}
|
||||
char c=str[i];
|
||||
bool dpfound=false;
|
||||
bool found=false;
|
||||
for(size_t j=0;j!=digits.size();j++){
|
||||
if(c==digits[j]){
|
||||
found=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found && !dpfound && c==dpoint){ // check for decimal point
|
||||
dpfound=true;
|
||||
found=true;
|
||||
}
|
||||
|
||||
if(found || (0==count && c=='-')){
|
||||
ret.back()+=c;
|
||||
count++;
|
||||
}else if(count){
|
||||
count=0;
|
||||
for(size_t j=0;j!=operators.size();j++){
|
||||
if(c==operators[j]){
|
||||
found=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found){
|
||||
ret.push_back("");
|
||||
ret.back()+=c;
|
||||
}else{
|
||||
std::cout<<"error in arithmetic operator:"<<c<<":\n\t"<<s<<"\n";
|
||||
return(ret);
|
||||
}
|
||||
}else if(c==parenthesis[0]){
|
||||
std::vector<std::string> temp=utils::paranthetical_split(str.substr(i,str.size()-i),0);
|
||||
if(temp.size()%2!=0){
|
||||
std::cout<<"temp"<<temp[1]<<":"<<i<<"\n";
|
||||
ret.back()+=value(temp[1]);
|
||||
i+=temp[1].size()+2;
|
||||
std::cout<<"\t"<<ret.back()<<"\n";
|
||||
std::cout<<"temp 2:"<<i<<"\n";
|
||||
count=0;
|
||||
c=str[i];
|
||||
for(size_t j=0;j!=operators.size();j++){
|
||||
if(c==operators[j]){
|
||||
found=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found){
|
||||
ret.push_back("");
|
||||
ret.back()+=c;
|
||||
}else if(i!=str.size()){
|
||||
std::cout<<"error in arithmetic operator:"<<str[i]<<":\n\t"<<s<<"\n\t"<<str.substr(0,i)<<"\n\t"<<str.substr(i,str.size()-i)<<"\n";
|
||||
return(ret);
|
||||
}
|
||||
}else{
|
||||
std::cout<<"error in parenthetical expression:\n\t"<<s<<"\n";
|
||||
}
|
||||
}else if(i!=str.size()){
|
||||
std::cout<<"error in arithmetic expression:\n\t"<<s<<":\n\t"<<str.substr(0,i)<<"\n\t:"<<s[i]<<":\n";
|
||||
std::cout<<i<<";"<<str.size()<<"\n";
|
||||
return(ret);
|
||||
}
|
||||
if(i!=str.size()){
|
||||
i++;
|
||||
}
|
||||
}
|
||||
std::cout<<"got here 5\n";
|
||||
return(ret);
|
||||
}
|
||||
|
||||
std::string distance_evaluator::value(const std::string& val_string){
|
||||
std::cout<<"got distance:"<<val_string<<"\n";
|
||||
std::vector<std::string> vals = utils::split(val_string,',');
|
||||
if(vals.size()<4){
|
||||
std::cout<<"error in distance parameters\n";
|
||||
return("ERR");
|
||||
}
|
||||
std::vector<int> coord;
|
||||
for(size_t i=0;i!=4;i++){
|
||||
vals[i] = arithmetic_evaluator::value(vals[i]);
|
||||
std::cout<<"vals["<<i<<"]:"<<vals[i]<<"\n";
|
||||
coord.push_back(atoi(vals[i].c_str()));
|
||||
}
|
||||
std::cout<<"coords:"<<coord[0]<<","<<coord[1]<<"\n";
|
||||
std::cout<<"coords:"<<coord[2]<<","<<coord[3]<<"\n";
|
||||
map_location a(coord[0]-1,coord[1]-1);
|
||||
map_location b(coord[2]-1,coord[3]-1);
|
||||
std::cout<<"a "<<a.x<<","<<a.y<<"\n";
|
||||
std::cout<<"b "<<b.x<<","<<b.y<<"\n";
|
||||
int distance = distance_between(a,b);
|
||||
std::stringstream t;
|
||||
t<<distance;
|
||||
return(t.str());
|
||||
}
|
||||
} // end of namespace dfool
|
||||
|
||||
} // end of namespace ai
|
@ -1,159 +0,0 @@
|
||||
/* $Id$ */
|
||||
/*
|
||||
Copyright (C) 2007 - 2009
|
||||
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 version 2
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ai/dfool/ai.hpp
|
||||
* This ai uses for its decisions only units it has "seen".
|
||||
*/
|
||||
|
||||
#ifndef AI_DFOOL_AI_HPP_INCLUDED
|
||||
#define AI_DFOOL_AI_HPP_INCLUDED
|
||||
|
||||
#include "../../global.hpp"
|
||||
|
||||
#include "../interface.hpp"
|
||||
#include "../contexts.hpp"
|
||||
|
||||
#include "../../map_location.hpp"
|
||||
#include "../../unit_map.hpp"
|
||||
#include "../../unit.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
//silence "inherits via dominance" warnings
|
||||
#pragma warning(disable:4250)
|
||||
#endif
|
||||
|
||||
|
||||
namespace ai {
|
||||
|
||||
namespace dfool {
|
||||
typedef std::vector<size_t> unit_list;
|
||||
|
||||
// class target {
|
||||
// public:
|
||||
// target(config& command, unit_history u_hist, info& ai_info);
|
||||
// double value(location loc, unit& u, unit_history u_hist, info ai_info);
|
||||
// private:
|
||||
// config unit_filter_;
|
||||
// config terrain_filter_;
|
||||
// std::string hex_val_;
|
||||
// std::string number;
|
||||
// std::string id;
|
||||
// };
|
||||
|
||||
class unit_memory{
|
||||
public:
|
||||
unit_memory(const config& cfg);
|
||||
void add_unit_sighting(const unit& u, const map_location& l, size_t t);
|
||||
void remove_unit_sighting(size_t id);
|
||||
//void purge(int turn = -1); // Clean outdated entries
|
||||
void write(config& temp);
|
||||
// Create a map based upon units seen since turn
|
||||
void known_map(unit_map& units, size_t turn=0);
|
||||
private:
|
||||
void write_element(int i, config& temp);
|
||||
// Could replace these with a single vector of memory elements
|
||||
std::vector<unit> units_;
|
||||
unit_list ids_;
|
||||
std::vector<size_t> turns_;
|
||||
std::vector<map_location> locations_;
|
||||
};
|
||||
|
||||
class evaluator{
|
||||
public:
|
||||
evaluator(const game_state& s, std::map<std::string, evaluator*>* m):function_map_(m),state(s){};
|
||||
virtual ~evaluator(){};
|
||||
virtual std::string value(const std::string& s);
|
||||
private:
|
||||
std::map<std::string, evaluator*>* function_map_;
|
||||
const game_state& state;
|
||||
};
|
||||
|
||||
class arithmetic_evaluator : public evaluator {
|
||||
public:
|
||||
arithmetic_evaluator(const game_state& s, std::map<std::string, evaluator*>* m):evaluator(s,m){};
|
||||
std::string value(const std::string& s);
|
||||
private:
|
||||
std::list<std::string> parse_tokens(const std::string&);
|
||||
std::string evaluate_tokens(std::list<std::string>&);
|
||||
};
|
||||
|
||||
class distance_evaluator : public arithmetic_evaluator {
|
||||
public:
|
||||
distance_evaluator(const game_state& s, std::map<std::string, evaluator*>* m):arithmetic_evaluator(s,m){};
|
||||
std::string value(const std::string& s);
|
||||
private:
|
||||
std::list<std::string> parse_tokens(const std::string&);
|
||||
std::string evaluate_tokens(std::list<std::string>&);
|
||||
};
|
||||
|
||||
/**
|
||||
* An ai that keeps track of what it has "seen",
|
||||
* does not target units that it has not "seen",
|
||||
* and does not make decisions based on unseen units.
|
||||
*/
|
||||
class dfool_ai : public default_ai_context_proxy, public interface {
|
||||
public:
|
||||
dfool_ai(default_ai_context &context, const config &cfg)
|
||||
: cfg_(cfg), recursion_counter_(context.get_recursion_count()), unit_memory_(config())
|
||||
{
|
||||
init_default_ai_context_proxy(context);
|
||||
}
|
||||
void play_turn();
|
||||
virtual std::string describe_self();
|
||||
virtual int get_recursion_count() const{
|
||||
return recursion_counter_.get_count();
|
||||
}
|
||||
virtual void new_turn()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
virtual config to_config() const{
|
||||
return config();
|
||||
}
|
||||
|
||||
private:
|
||||
const config &cfg_;
|
||||
recursion_counter recursion_counter_;
|
||||
// std::map<std::string,target> target_map_;
|
||||
unit_list all_units();
|
||||
unit_list visible_units();
|
||||
void switch_side(side_number /*side*/)
|
||||
{}
|
||||
unit_list my_units();
|
||||
unit_list filter_units(const config& filter,unit_list& ul, unit_map& um);
|
||||
bool moveto(const config &o, unit_map::const_iterator m);
|
||||
unit_map::iterator unit(size_t unit_id, unit_map& um);
|
||||
|
||||
unit_memory unit_memory_;
|
||||
|
||||
};
|
||||
|
||||
} // end of namespace dfool
|
||||
|
||||
} // end of namespace ai
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
@ -22,7 +22,6 @@
|
||||
#include "configuration.hpp"
|
||||
#include "contexts.hpp"
|
||||
#include "default/ai.hpp"
|
||||
#include "dfool/ai.hpp"
|
||||
#include "manager.hpp"
|
||||
#include "formula/ai.hpp"
|
||||
#include "registry.hpp"
|
||||
@ -44,7 +43,6 @@ const std::string manager::AI_TYPE_COMPOSITE_AI = "composite_ai";
|
||||
const std::string manager::AI_TYPE_SAMPLE_AI = "sample_ai";
|
||||
const std::string manager::AI_TYPE_IDLE_AI = "idle_ai";
|
||||
const std::string manager::AI_TYPE_FORMULA_AI = "formula_ai";
|
||||
const std::string manager::AI_TYPE_DFOOL_AI = "dfool_ai";
|
||||
const std::string manager::AI_TYPE_AI2 = "ai2";
|
||||
const std::string manager::AI_TYPE_DEFAULT = "default";
|
||||
|
||||
|
@ -24,7 +24,6 @@
|
||||
#include "composite/engine_fai.hpp"
|
||||
#include "composite/goal.hpp"
|
||||
#include "default/ai.hpp"
|
||||
#include "dfool/ai.hpp"
|
||||
#include "formula/ai.hpp"
|
||||
#include "registry.hpp"
|
||||
#include "testing/aspect_attacks.hpp"
|
||||
@ -41,7 +40,6 @@ static register_ai_factory<ai_default> ai_factory_default("");
|
||||
static register_ai_factory<ai_default> ai_default_ai_factory("default_ai");
|
||||
static register_ai_factory<ai2> ai2_ai_factory("ai2");
|
||||
static register_ai_factory<idle_ai> ai_idle_ai_factory("idle_ai");
|
||||
static register_ai_factory<dfool::dfool_ai> ai_dfool_ai_factory("dfool_ai");
|
||||
//static register_ai_factory<formula_ai> ai_formula_ai_factory("formula_ai");
|
||||
static register_ai_factory<ai_composite> ai_composite_ai_factory("composite_ai");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user