Switched AI to the new logging system
This commit is contained in:
parent
27af651cca
commit
eb621ce3a2
4 changed files with 115 additions and 105 deletions
138
src/ai.cpp
138
src/ai.cpp
|
@ -31,6 +31,8 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#define LOG_AI lg::info(lg::ai)
|
||||
|
||||
///a trivial ai that sits around doing absolutely nothing
|
||||
class idle_ai : public ai_interface {
|
||||
public:
|
||||
|
@ -186,7 +188,7 @@ ai_interface* create_ai(const std::string& name, ai_interface::info& info)
|
|||
else if(name == "idle_ai")
|
||||
return new idle_ai(info);
|
||||
else if(name != "")
|
||||
std::cerr << "ERROR: AI not found: '" << name << "'\n";
|
||||
lg::err(lg::ai) << "AI not found: '" << name << "'\n";
|
||||
|
||||
return new ai(info);
|
||||
}
|
||||
|
@ -204,8 +206,8 @@ bool ai::recruit_usage(const std::string& usage)
|
|||
|
||||
const int min_gold = 0;
|
||||
|
||||
log_scope("recruiting troops");
|
||||
std::cerr << "recruiting " << usage << "\n";
|
||||
log_scope2(ai, "recruiting troops");
|
||||
LOG_AI << "recruiting " << usage << "\n";
|
||||
|
||||
std::vector<std::string> options;
|
||||
|
||||
|
@ -220,7 +222,7 @@ bool ai::recruit_usage(const std::string& usage)
|
|||
if(i->second.usage() == usage && recruits.count(name)
|
||||
&& current_team().gold() - i->second.cost() > min_gold
|
||||
&& not_recommended_units_.count(name) == 0) {
|
||||
std::cerr << "recommending '" << name << "'\n";
|
||||
LOG_AI << "recommending '" << name << "'\n";
|
||||
options.push_back(name);
|
||||
}
|
||||
}
|
||||
|
@ -231,7 +233,7 @@ bool ai::recruit_usage(const std::string& usage)
|
|||
return recruit(options[option]);
|
||||
}
|
||||
|
||||
std::cerr << "no available units to recruit that come under the price\n";
|
||||
LOG_AI << "no available units to recruit that come under the price\n";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -335,7 +337,8 @@ void ai_interface::sync_network()
|
|||
|
||||
gamemap::location ai_interface::move_unit(location from, location to, std::map<location,paths>& possible_moves)
|
||||
{
|
||||
std::cerr << "ai_interface::move_unit " << (from.x+1) << "," << (from.y+1) << " -> " << (to.x+1) << "," << (to.y+1) << "\n";
|
||||
LOG_AI << "ai_interface::move_unit " << (from.x + 1) << "," << (from.y + 1)
|
||||
<< " -> " << (to.x + 1) << "," << (to.y + 1) << "\n";
|
||||
//stop the user from issuing any commands while the unit is moving
|
||||
const command_disabler disable_commands(&info_.disp);
|
||||
|
||||
|
@ -344,17 +347,16 @@ gamemap::location ai_interface::move_unit(location from, location to, std::map<l
|
|||
info_.disp.select_hex(from);
|
||||
info_.disp.update_display();
|
||||
|
||||
log_scope("move_unit");
|
||||
log_scope2(ai, "move_unit");
|
||||
unit_map::iterator u_it = info_.units.find(from);
|
||||
if(u_it == info_.units.end()) {
|
||||
std::cerr << "Could not find unit at " << from.x << ", "
|
||||
<< from.y << "\n";
|
||||
lg::err(lg::ai) << "Could not find unit at " << from.x << ", " << from.y << "\n";
|
||||
assert(false);
|
||||
return location();
|
||||
}
|
||||
|
||||
if(from == to) {
|
||||
std::cerr << "moving unit at " << (from.x+1) << "," << (from.y+1) << " on spot. resetting moves\n";
|
||||
LOG_AI << "moving unit at " << (from.x+1) << "," << (from.y+1) << " on spot. resetting moves\n";
|
||||
u_it->second.set_movement(0);
|
||||
return to;
|
||||
}
|
||||
|
@ -452,11 +454,13 @@ bool ai::multistep_move_possible(location from, location to, location via, std::
|
|||
const unit_map::const_iterator i = units_.find(from);
|
||||
if(i != units_.end()) {
|
||||
if(from != via && to != via && units_.count(via) == 0) {
|
||||
std::cerr << "when seeing if leader can move from " << (from.x+1) << "," << (from.y+1) << " -> " << (to.x+1) << "," << (to.y+1) << " seeing if can detour to keep at " << (via.x+1) << "," << (via.y+1) << "\n";
|
||||
LOG_AI << "when seeing if leader can move from "
|
||||
<< (from.x + 1) << "," << (from.y + 1) << " -> " << (to.x + 1) << "," << (to.y + 1)
|
||||
<< " seeing if can detour to keep at " << (via.x + 1) << "," << (via.y + 1) << "\n";
|
||||
const std::map<location,paths>::const_iterator moves = possible_moves.find(from);
|
||||
if(moves != possible_moves.end()) {
|
||||
|
||||
std::cerr << "found leader moves..\n";
|
||||
LOG_AI << "found leader moves..\n";
|
||||
|
||||
int move_left = 0;
|
||||
|
||||
|
@ -465,17 +469,17 @@ bool ai::multistep_move_possible(location from, location to, location via, std::
|
|||
const paths::routes_map::const_iterator itor = moves->second.routes.find(via);
|
||||
if(itor != moves->second.routes.end()) {
|
||||
move_left = itor->second.move_left;
|
||||
std::cerr << "can make it to keep with " << move_left << " movement left\n";
|
||||
LOG_AI << "can make it to keep with " << move_left << " movement left\n";
|
||||
unit temp_unit(i->second);
|
||||
temp_unit.set_movement(move_left);
|
||||
const temporary_unit_placer unit_placer(units_,via,temp_unit);
|
||||
const paths unit_paths(map_,state_,gameinfo_,units_,via,teams_,false,false);
|
||||
|
||||
std::cerr << "found " << unit_paths.routes.size() << " moves for temp leader\n";
|
||||
LOG_AI << "found " << unit_paths.routes.size() << " moves for temp leader\n";
|
||||
|
||||
//see if this leader could make it back to the keep
|
||||
if(unit_paths.routes.count(to) != 0) {
|
||||
std::cerr << "can make it back to the keep\n";
|
||||
LOG_AI << "can make it back to the keep\n";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -725,7 +729,7 @@ void ai::play_turn()
|
|||
|
||||
void ai::do_move()
|
||||
{
|
||||
log_scope("doing ai move");
|
||||
log_scope2(ai, "doing ai move");
|
||||
|
||||
invalidate_defensive_position_cache();
|
||||
|
||||
|
@ -750,10 +754,10 @@ void ai::do_move()
|
|||
|
||||
std::vector<attack_analysis> analysis;
|
||||
|
||||
AI_DIAGNOSTIC("combat phase");
|
||||
LOG_AI << "combat phase\n";
|
||||
|
||||
if(consider_combat_) {
|
||||
std::cerr << "combat...\n";
|
||||
LOG_AI << "combat...\n";
|
||||
consider_combat_ = do_combat(possible_moves,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc);
|
||||
if(consider_combat_) {
|
||||
do_move();
|
||||
|
@ -763,27 +767,27 @@ void ai::do_move()
|
|||
|
||||
move_leader_to_goals(enemy_srcdst,enemy_dstsrc);
|
||||
|
||||
AI_DIAGNOSTIC("get villages phase");
|
||||
LOG_AI << "get villages phase\n";
|
||||
|
||||
std::cerr << "villages...\n";
|
||||
LOG_AI << "villages...\n";
|
||||
const bool got_village = get_villages(possible_moves,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc,leader);
|
||||
if(got_village) {
|
||||
do_move();
|
||||
return;
|
||||
}
|
||||
|
||||
AI_DIAGNOSTIC("healing phase");
|
||||
LOG_AI << "healing phase\n";
|
||||
|
||||
std::cerr << "healing...\n";
|
||||
LOG_AI << "healing...\n";
|
||||
const bool healed_unit = get_healing(possible_moves,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc);
|
||||
if(healed_unit) {
|
||||
do_move();
|
||||
return;
|
||||
}
|
||||
|
||||
AI_DIAGNOSTIC("retreat phase");
|
||||
LOG_AI << "retreat phase\n";
|
||||
|
||||
std::cerr << "retreating...\n";
|
||||
LOG_AI << "retreating...\n";
|
||||
const bool retreated_unit = retreat_units(possible_moves,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc,leader);
|
||||
if(retreated_unit) {
|
||||
do_move();
|
||||
|
@ -796,18 +800,18 @@ void ai::do_move()
|
|||
|
||||
find_threats();
|
||||
|
||||
AI_DIAGNOSTIC("move/targetting phase");
|
||||
LOG_AI << "move/targetting phase\n";
|
||||
|
||||
const bool met_invisible_unit = move_to_targets(possible_moves,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc,leader);
|
||||
if(met_invisible_unit) {
|
||||
std::cerr << "met_invisible_unit\n";
|
||||
LOG_AI << "met_invisible_unit\n";
|
||||
do_move();
|
||||
return;
|
||||
}
|
||||
|
||||
std::cerr << "done move to targets\n";
|
||||
LOG_AI << "done move to targets\n";
|
||||
|
||||
AI_DIAGNOSTIC("leader/recruitment phase");
|
||||
LOG_AI << "leader/recruitment phase\n";
|
||||
|
||||
//recruitment phase and leader movement phase
|
||||
if(leader != units_.end()) {
|
||||
|
@ -823,8 +827,6 @@ void ai::do_move()
|
|||
}
|
||||
}
|
||||
|
||||
AI_DIAGNOSTIC("");
|
||||
|
||||
recorder.end_turn();
|
||||
sync_network();
|
||||
}
|
||||
|
@ -836,7 +838,7 @@ bool ai::do_combat(std::map<gamemap::location,paths>& possible_moves, const move
|
|||
std::vector<attack_analysis> analysis = analyze_targets(srcdst,dstsrc,enemy_srcdst,enemy_dstsrc);
|
||||
|
||||
int time_taken = SDL_GetTicks() - ticks;
|
||||
std::cerr << "took " << time_taken << " ticks for " << analysis.size() << " positions. Analyzing...\n";
|
||||
LOG_AI << "took " << time_taken << " ticks for " << analysis.size() << " positions. Analyzing...\n";
|
||||
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
|
@ -847,7 +849,7 @@ bool ai::do_combat(std::map<gamemap::location,paths>& possible_moves, const move
|
|||
if(num_sims > 40)
|
||||
num_sims = 40;
|
||||
|
||||
std::cerr << "simulations: " << num_sims << "\n";
|
||||
LOG_AI << "simulations: " << num_sims << "\n";
|
||||
|
||||
const int max_positions = 30000;
|
||||
const int skip_num = analysis.size()/max_positions;
|
||||
|
@ -859,7 +861,7 @@ bool ai::do_combat(std::map<gamemap::location,paths>& possible_moves, const move
|
|||
continue;
|
||||
|
||||
const double rating = it->rating(current_team().aggression(),*this);
|
||||
std::cerr << "attack option rated at " << rating << " (" << current_team().aggression() << ")\n";
|
||||
LOG_AI << "attack option rated at " << rating << " (" << current_team().aggression() << ")\n";
|
||||
if(rating > choice_rating) {
|
||||
choice_it = it;
|
||||
choice_rating = rating;
|
||||
|
@ -867,7 +869,7 @@ bool ai::do_combat(std::map<gamemap::location,paths>& possible_moves, const move
|
|||
}
|
||||
|
||||
time_taken = SDL_GetTicks() - ticks;
|
||||
std::cerr << "analysis took " << time_taken << " ticks\n";
|
||||
LOG_AI << "analysis took " << time_taken << " ticks\n";
|
||||
|
||||
if(choice_rating > 0.0) {
|
||||
location from = choice_it->movements[0].first;
|
||||
|
@ -879,8 +881,10 @@ bool ai::do_combat(std::map<gamemap::location,paths>& possible_moves, const move
|
|||
|
||||
const location arrived_at = move_unit(from,to,possible_moves);
|
||||
if(arrived_at != to) {
|
||||
std::cerr << "unit moving to attack has ended up unexpectedly at " << (arrived_at.x+1) << "," << (arrived_at.y+1) << " when moving to "
|
||||
<< (to.x+1) << "," << (to.y+1) << " moved from " << (from.x+1) << "," << (from.y+1) << "\n";
|
||||
lg::warn(lg::ai) << "unit moving to attack has ended up unexpectedly at "
|
||||
<< (arrived_at.x + 1) << "," << (arrived_at.y + 1) << " when moving to "
|
||||
<< (to.x + 1) << "," << (to.y + 1) << " moved from "
|
||||
<< (from.x + 1) << "," << (from.y + 1) << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -906,7 +910,7 @@ void ai_interface::attack_enemy(const location& u, const location& target, int w
|
|||
|
||||
if(info_.units.count(u) && info_.units.count(target)) {
|
||||
if(info_.units.find(target)->second.stone()) {
|
||||
std::cerr << "ERROR: attempt to attack unit that is turned to stone\n";
|
||||
lg::err(lg::ai) << "attempt to attack unit that is turned to stone\n";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -980,7 +984,7 @@ std::vector<std::pair<gamemap::location,gamemap::location> > ai::get_village_com
|
|||
|
||||
bool ai::get_villages(std::map<gamemap::location,paths>& possible_moves, const move_map& srcdst, const move_map& dstsrc, const move_map& enemy_srcdst, const move_map& enemy_dstsrc, unit_map::const_iterator leader)
|
||||
{
|
||||
std::cerr << "deciding which villages we want...\n";
|
||||
LOG_AI << "deciding which villages we want...\n";
|
||||
|
||||
location start_pos;
|
||||
|
||||
|
@ -1052,10 +1056,10 @@ bool ai::get_villages(std::map<gamemap::location,paths>& possible_moves, const m
|
|||
|
||||
std::set<location> taken_villages, moved_units;
|
||||
const int ticks = SDL_GetTicks();
|
||||
std::cerr << "get_villages()..." << village_moves.size() << "\n";
|
||||
LOG_AI << "get_villages()..." << village_moves.size() << "\n";
|
||||
const std::vector<std::pair<location,location> >& moves = get_village_combinations(possible_moves,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc,leader,
|
||||
taken_villages,moved_units,village_moves,village_moves.begin());
|
||||
std::cerr << "get_villages() done: " << (SDL_GetTicks() - ticks) << "\n";
|
||||
LOG_AI << "get_villages() done: " << (SDL_GetTicks() - ticks) << "\n";
|
||||
|
||||
//move all the units to get villages, however move the leader last, so that the castle will be cleared
|
||||
//if it wants to stop to recruit along the way.
|
||||
|
@ -1235,7 +1239,8 @@ bool ai::retreat_units(std::map<gamemap::location,paths>& possible_moves, const
|
|||
}
|
||||
|
||||
if(best_pos.valid()) {
|
||||
std::cerr << "retreating '" << i->second.type().name() << "' " << i->first.x << "," << i->first.y << " -> " << best_pos.x << "," << best_pos.y << "\n";
|
||||
LOG_AI << "retreating '" << i->second.type().name() << "' " << i->first.x << ","
|
||||
<< i->first.y << " -> " << best_pos.x << "," << best_pos.y << "\n";
|
||||
move_unit(i->first,best_pos,possible_moves);
|
||||
return true;
|
||||
}
|
||||
|
@ -1248,7 +1253,7 @@ bool ai::retreat_units(std::map<gamemap::location,paths>& possible_moves, const
|
|||
|
||||
bool ai::move_to_targets(std::map<gamemap::location,paths>& possible_moves, move_map& srcdst, move_map& dstsrc, const move_map& enemy_srcdst, const move_map& enemy_dstsrc, unit_map::const_iterator leader)
|
||||
{
|
||||
std::cerr << "finding targets...\n";
|
||||
LOG_AI << "finding targets...\n";
|
||||
std::vector<target> targets;
|
||||
for(;;) {
|
||||
if(targets.empty()) {
|
||||
|
@ -1260,7 +1265,7 @@ bool ai::move_to_targets(std::map<gamemap::location,paths>& possible_moves, move
|
|||
}
|
||||
}
|
||||
|
||||
std::cerr << "choosing move...\n";
|
||||
LOG_AI << "choosing move...\n";
|
||||
std::pair<location,location> move = choose_move(targets,srcdst,dstsrc,enemy_srcdst,enemy_dstsrc);
|
||||
for(std::vector<target>::const_iterator ittg = targets.begin(); ittg != targets.end(); ++ittg) {
|
||||
assert(map_.on_board(ittg->loc));
|
||||
|
@ -1274,7 +1279,7 @@ bool ai::move_to_targets(std::map<gamemap::location,paths>& possible_moves, move
|
|||
return true;
|
||||
}
|
||||
|
||||
std::cerr << "move: " << move.first.x << ", " << move.first.y << " - " << move.second.x << ", " << move.second.y << "\n";
|
||||
LOG_AI << "move: " << move.first.x << ", " << move.first.y << " - " << move.second.x << ", " << move.second.y << "\n";
|
||||
|
||||
//search to see if there are any enemy units next
|
||||
//to the tile which really should be attacked once the move is done
|
||||
|
@ -1303,7 +1308,7 @@ bool ai::move_to_targets(std::map<gamemap::location,paths>& possible_moves, move
|
|||
//we didn't arrive at our intended destination. We return true, meaning that
|
||||
//the AI algorithm should be recalculated from the start.
|
||||
if(arrived_at != move.second) {
|
||||
std::cerr << "didn't arrive at destination\n";
|
||||
lg::warn(lg::ai) << "didn't arrive at destination\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1335,7 +1340,7 @@ int ai::average_resistance_against(const unit_type& a, const unit_type& b) const
|
|||
|
||||
defense /= weighting_sum;
|
||||
|
||||
std::cerr << "average defense of '" << a.name() << "': " << defense << "\n";
|
||||
LOG_AI << "average defense of '" << a.name() << "': " << defense << "\n";
|
||||
|
||||
int sum = 0, weight_sum = 0;
|
||||
|
||||
|
@ -1356,7 +1361,7 @@ int ai::compare_unit_types(const unit_type& a, const unit_type& b) const
|
|||
const int a_effectiveness_vs_b = average_resistance_against(b,a);
|
||||
const int b_effectiveness_vs_a = average_resistance_against(a,b);
|
||||
|
||||
std::cerr << "comparison of '" << a.name() << " vs " << b.name() << ": "
|
||||
LOG_AI << "comparison of '" << a.name() << " vs " << b.name() << ": "
|
||||
<< a_effectiveness_vs_b << " - " << b_effectiveness_vs_a << " = "
|
||||
<< (a_effectiveness_vs_b - b_effectiveness_vs_a) << "\n";
|
||||
return a_effectiveness_vs_b - b_effectiveness_vs_a;
|
||||
|
@ -1368,7 +1373,7 @@ void ai::analyze_potential_recruit_combat()
|
|||
return;
|
||||
}
|
||||
|
||||
log_scope("analyze_potential_recruit_combat()");
|
||||
log_scope2(ai, "analyze_potential_recruit_combat()");
|
||||
|
||||
//records the best combat analysis for each usage type
|
||||
std::map<std::string,int> best_usage;
|
||||
|
@ -1396,7 +1401,7 @@ void ai::analyze_potential_recruit_combat()
|
|||
score /= weighting;
|
||||
}
|
||||
|
||||
std::cerr << "combat score of '" << *i << "': " << score << "\n";
|
||||
LOG_AI << "combat score of '" << *i << "': " << score << "\n";
|
||||
unit_combat_scores_[*i] = score;
|
||||
|
||||
if(best_usage.count(info->second.usage()) == 0 || score > best_usage[info->second.usage()]) {
|
||||
|
@ -1413,7 +1418,7 @@ void ai::analyze_potential_recruit_combat()
|
|||
}
|
||||
|
||||
if(unit_combat_scores_[*i] + 1000 < best_usage[info->second.usage()]) {
|
||||
std::cerr << "recommending not to use '" << *i << "' because of poor combat performance "
|
||||
LOG_AI << "recommending not to use '" << *i << "' because of poor combat performance "
|
||||
<< unit_combat_scores_[*i] << "/" << best_usage[info->second.usage()] << "\n";
|
||||
not_recommended_units_.insert(*i);
|
||||
}
|
||||
|
@ -1451,7 +1456,7 @@ void ai::analyze_potential_recruit_movements()
|
|||
return;
|
||||
}
|
||||
|
||||
log_scope("analyze_potential_recruit_movements()");
|
||||
log_scope2(ai, "analyze_potential_recruit_movements()");
|
||||
|
||||
const int max_targets = 5;
|
||||
|
||||
|
@ -1464,7 +1469,7 @@ void ai::analyze_potential_recruit_movements()
|
|||
|
||||
const std::set<std::string>& recruits = current_team().recruits();
|
||||
|
||||
std::cerr << "targets: " << targets.size() << "\n";
|
||||
LOG_AI << "targets: " << targets.size() << "\n";
|
||||
|
||||
std::map<std::string,int> best_scores;
|
||||
|
||||
|
@ -1484,14 +1489,14 @@ void ai::analyze_potential_recruit_movements()
|
|||
|
||||
const shortest_path_calculator calc(temp_unit,current_team(),units,teams_,map_,state_);
|
||||
for(std::vector<target>::const_iterator t = targets.begin(); t != targets.end(); ++t) {
|
||||
std::cerr << "analyzing '" << *i << "' getting to target...\n";
|
||||
LOG_AI << "analyzing '" << *i << "' getting to target...\n";
|
||||
const paths::route& route = a_star_search(start,t->loc,100.0,calc);
|
||||
if(route.steps.empty() == false) {
|
||||
std::cerr << "made it: " << route.move_left << "\n";
|
||||
LOG_AI << "made it: " << route.move_left << "\n";
|
||||
cost += route.move_left;
|
||||
++targets_reached;
|
||||
} else {
|
||||
std::cerr << "failed\n";
|
||||
LOG_AI << "failed\n";
|
||||
++targets_missed;
|
||||
}
|
||||
}
|
||||
|
@ -1521,10 +1526,10 @@ void ai::analyze_potential_recruit_movements()
|
|||
if(best_score > 0) {
|
||||
j->second = (j->second*10)/best_score;
|
||||
if(j->second > 15) {
|
||||
std::cerr << "recommending against recruiting '" << j->first << "' (score: " << j->second << ")\n";
|
||||
LOG_AI << "recommending against recruiting '" << j->first << "' (score: " << j->second << ")\n";
|
||||
not_recommended_units_.insert(j->first);
|
||||
} else {
|
||||
std::cerr << "recommending recruit of '" << j->first << "' (score: " << j->second << ")\n";
|
||||
LOG_AI << "recommending recruit of '" << j->first << "' (score: " << j->second << ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1580,7 +1585,7 @@ void ai::do_recruitment()
|
|||
//get scouts depending on how many neutral villages there are
|
||||
int scouts_wanted = villages_per_scout > 0 ? neutral_villages/villages_per_scout : 0;
|
||||
|
||||
std::cerr << "scouts_wanted: " << neutral_villages << "/" << villages_per_scout << " = " << scouts_wanted << "\n";
|
||||
LOG_AI << "scouts_wanted: " << neutral_villages << "/" << villages_per_scout << " = " << scouts_wanted << "\n";
|
||||
|
||||
std::map<std::string,int> unit_types;
|
||||
|
||||
|
@ -1590,7 +1595,7 @@ void ai::do_recruitment()
|
|||
}
|
||||
}
|
||||
|
||||
std::cerr << "we have " << unit_types["scout"] << " scouts already and we want " << scouts_wanted << " in total\n";
|
||||
LOG_AI << "we have " << unit_types["scout"] << " scouts already and we want " << scouts_wanted << " in total\n";
|
||||
|
||||
while(unit_types["scout"] < scouts_wanted) {
|
||||
if(recruit_usage("scout") == false)
|
||||
|
@ -1616,7 +1621,7 @@ void ai::move_leader_to_goals(const move_map& enemy_srcdst, const move_map& enem
|
|||
const config* const goal = current_team().ai_parameters().child("leader_goal");
|
||||
|
||||
if(goal == NULL) {
|
||||
AI_LOG("No goal found");
|
||||
LOG_AI << "No goal found\n";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1624,17 +1629,17 @@ void ai::move_leader_to_goals(const move_map& enemy_srcdst, const move_map& enem
|
|||
|
||||
const unit_map::iterator leader = find_leader(units_,team_num_);
|
||||
if(leader == units_.end() || leader->second.incapacitated()) {
|
||||
AI_LOG("leader not found");
|
||||
LOG_AI << "leader not found\n";
|
||||
return;
|
||||
}
|
||||
|
||||
AI_LOG("Doing recruitment before goals");
|
||||
LOG_AI << "Doing recruitment before goals\n";
|
||||
|
||||
do_recruitment();
|
||||
|
||||
const paths::route route = a_star_search(leader->first,dst,1000.0,shortest_path_calculator(leader->second,current_team(),units_,teams_,map_,state_));
|
||||
if(route.steps.empty()) {
|
||||
AI_LOG("route empty");
|
||||
LOG_AI << "route empty";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1651,7 +1656,7 @@ void ai::move_leader_to_goals(const move_map& enemy_srcdst, const move_map& enem
|
|||
}
|
||||
|
||||
if(loc.valid()) {
|
||||
AI_LOG("Moving leader to goal");
|
||||
LOG_AI << "Moving leader to goal\n";
|
||||
move_unit(leader->first,loc,possible_moves);
|
||||
}
|
||||
}
|
||||
|
@ -1705,7 +1710,7 @@ void ai::move_leader_to_keep(const move_map& enemy_dstsrc)
|
|||
|
||||
void ai::move_leader_after_recruit(const move_map& enemy_dstsrc)
|
||||
{
|
||||
std::cerr << "moving leader after recruit...\n";
|
||||
LOG_AI << "moving leader after recruit...\n";
|
||||
|
||||
const unit_map::iterator leader = find_leader(units_,team_num_);
|
||||
if(leader == units_.end() || leader->second.incapacitated()) {
|
||||
|
@ -1739,7 +1744,8 @@ void ai::move_leader_after_recruit(const move_map& enemy_dstsrc)
|
|||
|
||||
//if this location is in range of the village, then we consider it
|
||||
if(current_loc.valid()) {
|
||||
AI_LOG("considering movement to " + str_cast(current_loc.x+1) + "," + str_cast(current_loc.y+1));
|
||||
LOG_AI << "considering movement to " << str_cast(current_loc.x + 1)
|
||||
<< "," << str_cast(current_loc.y+1);
|
||||
unit_map temp_units;
|
||||
temp_units.insert(std::pair<location,unit>(current_loc,leader->second));
|
||||
const paths p(map_,state_,gameinfo_,temp_units,current_loc,teams_,false,false);
|
||||
|
|
|
@ -16,9 +16,6 @@
|
|||
#include "actions.hpp"
|
||||
#include "ai_interface.hpp"
|
||||
|
||||
#define AI_DIAGNOSTIC(MSG) if(game_config::debug) { diagnostic(MSG); } std::cerr << "AI_DIAGNOSTIC: " << MSG << "\n";
|
||||
#define AI_LOG(MSG) if(game_config::debug) { log_message(MSG); } std::cerr << "AI_LOG: " << MSG << "\n";
|
||||
|
||||
class ai : public ai_interface {
|
||||
public:
|
||||
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
|
||||
#define LOG_AI lg::info(lg::ai)
|
||||
|
||||
const int max_positions = 10000;
|
||||
|
||||
//analyze possibility of attacking target on 'loc'
|
||||
|
@ -53,7 +55,7 @@ void ai::do_attack_analysis(
|
|||
|
||||
const size_t max_positions = 1000;
|
||||
if(result.size() > max_positions && !cur_analysis.movements.empty()) {
|
||||
std::cerr << "cut analysis short with number of positions\n";
|
||||
LOG_AI << "cut analysis short with number of positions\n";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -253,7 +255,7 @@ int ai::choose_weapon(const location& att, const location& def,
|
|||
cur_stats = cache_itor->stats;
|
||||
|
||||
if(!(size_t(cache_itor->weapon) < itor->second.attacks().size())) {
|
||||
std::cerr << "cached illegal weapon: " << cache_itor->weapon
|
||||
lg::err(lg::ai) << "cached illegal weapon: " << cache_itor->weapon
|
||||
<< "/" << itor->second.attacks().size() << "\n";
|
||||
}
|
||||
|
||||
|
@ -264,7 +266,7 @@ int ai::choose_weapon(const location& att, const location& def,
|
|||
++cache_misses;
|
||||
|
||||
if((cache_misses%100) == 0) {
|
||||
std::cerr << "cache_stats: " << cache_hits << ":" << cache_misses << " " << weapon_choice_cache.size() << "\n";
|
||||
LOG_AI << "cache_stats: " << cache_hits << ":" << cache_misses << " " << weapon_choice_cache.size() << "\n";
|
||||
}
|
||||
|
||||
int current_choice = -1;
|
||||
|
@ -543,7 +545,7 @@ double ai::attack_analysis::rating(double aggression, ai& ai_obj) const
|
|||
//only use the leader if we do a serious amount of damage
|
||||
//compared to how much they do to us.
|
||||
if(uses_leader && aggression > -4.0) {
|
||||
std::cerr << "uses leader..\n";
|
||||
LOG_AI << "uses leader..\n";
|
||||
aggression = -4.0;
|
||||
}
|
||||
|
||||
|
@ -556,7 +558,8 @@ double ai::attack_analysis::rating(double aggression, ai& ai_obj) const
|
|||
|
||||
const double exposure_mod = uses_leader ? 2.0 : ai_obj.current_team().caution();
|
||||
const double exposure = exposure_mod*resources_used*(terrain_quality - alternative_terrain_quality)*vulnerability/maximum<double>(0.01,support);
|
||||
std::cerr << "attack option has base value " << value << " with exposure " << exposure << ": " << vulnerability << "/" << support << " = " << (vulnerability/support) << "\n";
|
||||
LOG_AI << "attack option has base value " << value << " with exposure " << exposure << ": "
|
||||
<< vulnerability << "/" << support << " = " << (vulnerability/support) << "\n";
|
||||
if(uses_leader) {
|
||||
ai_obj.log_message("attack option has value " + str_cast(value) + " with exposure " + str_cast(exposure) + ": " + str_cast(vulnerability) + "/" + str_cast(support));
|
||||
}
|
||||
|
@ -589,9 +592,11 @@ double ai::attack_analysis::rating(double aggression, ai& ai_obj) const
|
|||
value *= 5.0;
|
||||
}
|
||||
|
||||
std::cerr << "attack on " << (target.x+1) << "," << (target.y+1) << ": attackers: " << movements.size() << " value: " << value
|
||||
<< " chance to kill: " << chance_to_kill << " damage inflicted: " << avg_damage_inflicted << " damage taken: " << avg_damage_taken
|
||||
<< " vulnerability: " << vulnerability << " support: " << support << " quality: " << terrain_quality << " alternative quality: " << alternative_terrain_quality << "\n";
|
||||
LOG_AI << "attack on " << (target.x+1) << "," << (target.y+1) << ": attackers: " << movements.size()
|
||||
<< " value: " << value << " chance to kill: " << chance_to_kill << " damage inflicted: "
|
||||
<< avg_damage_inflicted << " damage taken: " << avg_damage_taken << " vulnerability: "
|
||||
<< vulnerability << " support: " << support << " quality: " << terrain_quality
|
||||
<< " alternative quality: " << alternative_terrain_quality << "\n";
|
||||
|
||||
return value;
|
||||
}
|
||||
|
@ -601,7 +606,7 @@ std::vector<ai::attack_analysis> ai::analyze_targets(
|
|||
const move_map& enemy_srcdst, const move_map& enemy_dstsrc
|
||||
)
|
||||
{
|
||||
log_scope("analyzing targets...");
|
||||
log_scope2(ai, "analyzing targets...");
|
||||
|
||||
weapon_choice_cache.clear();
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#define LOG_AI lg::info(lg::ai)
|
||||
|
||||
struct move_cost_calculator
|
||||
{
|
||||
move_cost_calculator(const unit& u, const gamemap& map,
|
||||
|
@ -79,7 +81,7 @@ private:
|
|||
|
||||
std::vector<ai::target> ai::find_targets(unit_map::const_iterator leader, const move_map& enemy_srcdst, const move_map& enemy_dstsrc)
|
||||
{
|
||||
log_scope("finding targets...");
|
||||
log_scope2(ai, "finding targets...");
|
||||
|
||||
const bool has_leader = leader != units_.end();
|
||||
|
||||
|
@ -148,7 +150,7 @@ std::vector<ai::target> ai::find_targets(unit_map::const_iterator leader, const
|
|||
for(std::vector<team::target>::iterator j = team_targets.begin();
|
||||
j != team_targets.end(); ++j) {
|
||||
if(u->second.matches_filter(j->criteria)) {
|
||||
std::cerr << "found explicit target..." << j->value << "\n";
|
||||
LOG_AI << "found explicit target..." << j->value << "\n";
|
||||
targets.push_back(target(u->first,j->value,target::EXPLICIT));
|
||||
}
|
||||
}
|
||||
|
@ -174,7 +176,7 @@ std::vector<ai::target> ai::find_targets(unit_map::const_iterator leader, const
|
|||
|
||||
assert(new_values.size() == targets.size());
|
||||
for(size_t n = 0; n != new_values.size(); ++n) {
|
||||
std::cerr << "target value: " << targets[n].value << " -> " << new_values[n] << "\n";
|
||||
LOG_AI << "target value: " << targets[n].value << " -> " << new_values[n] << "\n";
|
||||
targets[n].value = new_values[n];
|
||||
}
|
||||
|
||||
|
@ -243,7 +245,7 @@ bool ai::move_group(const location& dst, const std::vector<location>& route, con
|
|||
return false;
|
||||
}
|
||||
|
||||
AI_DIAGNOSTIC("group has " + str_cast(units.size()) + " members");
|
||||
LOG_AI << "group has " << units.size() << " members\n";
|
||||
|
||||
location next;
|
||||
|
||||
|
@ -322,11 +324,11 @@ bool ai::move_group(const location& dst, const std::vector<location>& route, con
|
|||
if(n != direction && ((n+3)%6) != direction && map_.on_board(adj[n]) &&
|
||||
units_.count(adj[n]) == 0 && std::count(preferred_moves.begin(),preferred_moves.end(),adj[n]) == 0) {
|
||||
preferred_moves.push_front(adj[n]);
|
||||
AI_LOG("added moves: " + str_cast(adj[n].x+1) + "," + str_cast(adj[n].y+1));
|
||||
LOG_AI << "added moves: " << adj[n].x + 1 << "," << adj[n].y + 1 << "\n";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
AI_DIAGNOSTIC("Could not move group member to any of " + str_cast(preferred_moves.size()) + " locations");
|
||||
LOG_AI << "Could not move group member to any of " << preferred_moves.size() << " locations\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -374,7 +376,7 @@ double ai::compare_groups(const std::set<location>& our_group, const std::set<lo
|
|||
|
||||
std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<target>& targets, const move_map& srcdst, const move_map& dstsrc, const move_map& enemy_srcdst, const move_map& enemy_dstsrc)
|
||||
{
|
||||
log_scope("choosing move");
|
||||
log_scope2(ai, "choosing move");
|
||||
|
||||
user_interact();
|
||||
|
||||
|
@ -398,13 +400,13 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
}
|
||||
|
||||
if(u == units_.end()) {
|
||||
std::cerr << "no eligible units found\n";
|
||||
LOG_AI << "no eligible units found\n";
|
||||
return std::pair<location,location>();
|
||||
}
|
||||
|
||||
//guardian units stay put
|
||||
if(u->second.is_guardian()) {
|
||||
std::cerr << u->second.type().name() << " is guardian, staying still\n";
|
||||
LOG_AI << u->second.type().name() << " is guardian, staying still\n";
|
||||
return std::pair<location,location>(u->first,u->first);
|
||||
}
|
||||
|
||||
|
@ -467,7 +469,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
|
||||
|
||||
if(best_target == targets.end()) {
|
||||
std::cerr << "no eligible targets found\n";
|
||||
LOG_AI << "no eligible targets found\n";
|
||||
return std::pair<location,location>();
|
||||
}
|
||||
|
||||
|
@ -476,7 +478,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
const bool dumb_ai = current_team().ai_parameters()["simple_targetting"] == "yes";
|
||||
|
||||
if(dumb_ai == false) {
|
||||
std::cerr << "complex targetting...\n";
|
||||
LOG_AI << "complex targetting...\n";
|
||||
//now see if any other unit can put a better bid forward
|
||||
for(++u; u != units_.end(); ++u) {
|
||||
if(u->second.side() != team_num_ || u->second.can_recruit() ||
|
||||
|
@ -520,12 +522,12 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
}
|
||||
}
|
||||
|
||||
std::cerr << "done complex targetting...\n";
|
||||
LOG_AI << "done complex targetting...\n";
|
||||
} else {
|
||||
u = units_.end();
|
||||
}
|
||||
|
||||
std::cerr << "best unit: " << (best->first.x+1) << "," << (best->first.y+1) << "\n";
|
||||
LOG_AI << "best unit: " << (best->first.x+1) << "," << (best->first.y+1) << "\n";
|
||||
|
||||
assert(best_target >= targets.begin() && best_target < targets.end());
|
||||
|
||||
|
@ -536,13 +538,13 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
//if our target is a position to support, then we
|
||||
//see if we can move to a position in support of this target
|
||||
if(best_target->type == target::SUPPORT) {
|
||||
std::cerr << "support...\n";
|
||||
LOG_AI << "support...\n";
|
||||
|
||||
std::vector<location> locs;
|
||||
access_points(srcdst,best->first,best_target->loc,locs);
|
||||
|
||||
if(locs.empty() == false) {
|
||||
AI_LOG("supporting unit at " + str_cast(best_target->loc.x+1) + "," + str_cast(best_target->loc.y+1));
|
||||
LOG_AI << "supporting unit at " << best_target->loc.x + 1 << "," << best_target->loc.y + 1 << "\n";
|
||||
location best_loc;
|
||||
int best_defense = 0;
|
||||
double best_vulnerability = 0.0;
|
||||
|
@ -561,7 +563,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
}
|
||||
}
|
||||
|
||||
std::cerr << "returning support...\n";
|
||||
LOG_AI << "returning support...\n";
|
||||
return std::pair<location,location>(best->first,best_loc);
|
||||
}
|
||||
}
|
||||
|
@ -574,7 +576,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
bool dangerous = false;
|
||||
|
||||
if(current_team().ai_parameters()["grouping"] != "no") {
|
||||
std::cerr << "grouping...\n";
|
||||
LOG_AI << "grouping...\n";
|
||||
const unit_map::const_iterator unit_at_target = units_.find(best_target->loc);
|
||||
int movement = best->second.movement_left();
|
||||
|
||||
|
@ -597,11 +599,11 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
}
|
||||
}
|
||||
|
||||
std::cerr << "done grouping...\n";
|
||||
LOG_AI << "done grouping...\n";
|
||||
}
|
||||
|
||||
if(dangerous) {
|
||||
AI_LOG("dangerous path");
|
||||
LOG_AI << "dangerous path\n";
|
||||
std::set<location> group, enemies;
|
||||
const location dst = form_group(best_route.steps,dstsrc,srcdst,group);
|
||||
enemies_along_path(best_route.steps,enemy_dstsrc,enemy_srcdst,enemies);
|
||||
|
@ -609,20 +611,20 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
const double our_strength = compare_groups(group,enemies,best_route.steps);
|
||||
|
||||
if(our_strength > 0.5 + current_team().caution()) {
|
||||
AI_DIAGNOSTIC("moving group");
|
||||
LOG_AI << "moving group\n";
|
||||
const bool res = move_group(dst,best_route.steps,group);
|
||||
AI_DIAGNOSTIC("");
|
||||
if(res) {
|
||||
return std::pair<location,location>(location(1,1),location());
|
||||
} else {
|
||||
AI_LOG("group didn't move " + str_cast(group.size()));
|
||||
LOG_AI << "group didn't move " << group.size() << "\n";
|
||||
|
||||
//the group didn't move, so end the first unit in the group's turn, to prevent an infinite loop
|
||||
return std::pair<location,location>(best->first,best->first);
|
||||
|
||||
}
|
||||
} else {
|
||||
AI_LOG("massing to attack " + str_cast(best_target->loc.x+1) + "," + str_cast(best_target->loc.y+1) + " " + str_cast(our_strength));
|
||||
LOG_AI << "massing to attack " << best_target->loc.x + 1 << "," << best_target->loc.y + 1
|
||||
<< " " << our_strength << "\n";
|
||||
|
||||
const double value = best_target->value;
|
||||
const location target_loc = best_target->loc;
|
||||
|
@ -697,11 +699,11 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
targets.erase(best_target);
|
||||
}
|
||||
|
||||
AI_LOG("Moving to " + str_cast(its.first->first.x+1) + "," + str_cast(its.first->first.y+1));
|
||||
LOG_AI << "Moving to " << its.first->first.x + 1 << "," << its.first->first.y + 1 << "\n";
|
||||
|
||||
return std::pair<location,location>(its.first->second,its.first->first);
|
||||
} else {
|
||||
AI_LOG("dangerous!");
|
||||
LOG_AI << "dangerous!\n";
|
||||
is_dangerous = true;
|
||||
}
|
||||
}
|
||||
|
@ -711,7 +713,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
}
|
||||
|
||||
if(best != units_.end()) {
|
||||
AI_LOG("Could not make good move, staying still");
|
||||
LOG_AI << "Could not make good move, staying still\n";
|
||||
|
||||
//this sounds like the road ahead might be dangerous, and that's why we don't advance.
|
||||
//create this as a target, attempting to rally units around
|
||||
|
@ -719,7 +721,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<targe
|
|||
return std::pair<location,location>(best->first,best->first);
|
||||
}
|
||||
|
||||
AI_LOG("Could not find anywhere to move!");
|
||||
LOG_AI << "Could not find anywhere to move!\n";
|
||||
return std::pair<location,location>();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue