/
ca_testing_recruitment.cpp
1151 lines (1062 loc) · 35.6 KB
/
ca_testing_recruitment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2009 - 2014 by Yurii Chernyi <terraninfo@terraninfo.net>
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.
*/
/**
* @file
* Experimental recruitment phase by Floris Kint
*/
#include "ca_testing_recruitment.hpp"
#include "../actions.hpp"
#include "../manager.hpp"
#include "../composite/engine.hpp"
#include "../composite/rca.hpp"
#include "../composite/stage.hpp"
#include "../../gamestatus.hpp"
#include "../../log.hpp"
#include "../../map.hpp"
#include "../../resources.hpp"
#include "../../team.hpp"
#include "../../wml_exception.hpp"
#include "../../pathfind/pathfind.hpp"
#include <boost/foreach.hpp>
#include <numeric>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
namespace ai {
namespace testing_ai_default {
static lg::log_domain log_aitesting("aitesting");
#define LOG_AIT LOG_STREAM(info, log_aitesting)
//If necessary, this define can be replaced with `#define LOG_AIT std::cout` to restore previous behavior
static lg::log_domain log_ai_ca_testing_recruitment("ai/ca/testing_recruitment");
#define DBG_AI LOG_STREAM(debug, log_ai_ca_testing_recruitment)
#define LOG_AI LOG_STREAM(info, log_ai_ca_testing_recruitment)
#define WRN_AI LOG_STREAM(warn, log_ai_ca_testing_recruitment)
#define ERR_AI LOG_STREAM(err, log_ai_ca_testing_recruitment)
testing_recruitment_phase::testing_recruitment_phase( rca_context &context, const config &cfg )
: candidate_action(context,cfg)
{
}
testing_recruitment_phase::~testing_recruitment_phase()
{
}
double testing_recruitment_phase::evaluate()
{
const unit_map::const_iterator leader = resources::units->find_leader(get_side());
if(leader == resources::units->end()) {
return BAD_SCORE;
}
if (!resources::game_map->is_keep(leader->get_location())) {
return BAD_SCORE;
}
std::set<map_location> checked_hexes;
checked_hexes.insert(leader->get_location());
if (count_free_hexes_in_castle(leader->get_location(), checked_hexes)==0) {
return BAD_SCORE;
}
return get_score();
}
class potential_recruit
{
public:
bool operator==(const std::string &i) const
{
return id() == i;
}
potential_recruit(int cost, int max_qty, double quality, int side, const unit_type *type)
: cost_(cost), max_qty_(max_qty), quality_(quality), side_(side), type_(type)
{}
const std::string& id() const
{
return type_->id();
}
int cost() const
{
return cost_;
}
double quality() const
{
return quality_;
}
void set_quality(double quality)
{
quality_ = quality;
}
int max_qty() const
{
return max_qty_;
}
int side() const
{
return side_;
}
const unit_type *type() const
{
return type_;
}
private:
int cost_;
int max_qty_;
double quality_;
int side_;
const unit_type *type_;
};
class potential_recruit_converter
{
public:
potential_recruit_converter(int max_qty, int side )
: max_qty_ (max_qty), side_(side)
{
}
potential_recruit operator()(const std::string &id)
{
const unit_type *type = unit_types.find(id);
assert(type);
return potential_recruit(type->cost(),max_qty_,0,side_,type);
}
private:
int max_qty_;
int side_;
};
class fake_team
{
public:
fake_team(const team &target)
: target_(&target)
, gold_(0)
, extra_units_()
, recruit_list_()
{
reset();
}
void reset()
{
extra_units_.clear();
gold_ = target_->gold();
recruit_list_.clear();
std::transform(target_->recruits().begin(),target_->recruits().end(),std::back_inserter(recruit_list_), potential_recruit_converter(999,side()));//999 - unlimited recruits of this type
}
int gold()
{
return gold_;
}
void set_gold(int gold)
{
gold_ = gold;
}
void fake_recruit(const potential_recruit &r)
{
if (gold()<r.cost()) {
ERR_AI << "ERROR: cannot fake recruit "<<r.id()<<", not enough gold" << std::endl;
return;
}
if (get_current_qty(r)>=r.max_qty()) {
ERR_AI << "ERROR: cannot fake recruit "<<r.id()<<", too many in the field" << std::endl;
return;
}
DBG_AI << " Fake recruiting [" << r.id() << "] for side [" << side() << "]"<< std::endl;
spend_gold(r.cost());
extra_units_.push_back(r);//@todo: give an id if needed
}
std::vector<potential_recruit>& extra_units()
{
return extra_units_;
}
int get_current_qty(const potential_recruit &r) const
{
return get_current_qty(r.id());//@todo: fix
}
int get_current_qty(const std::string &name) const
{
int counter = 0;
BOOST_FOREACH(unit &un, *resources::units){
if(un.side() == side() && un.type().base_id() == name) // @todo: is base_id good?
{
counter++;
}
}
return std::count(extra_units_.begin(), extra_units_.end(), name) + counter;//@todo: fix
}
void spend_gold(int gold)
{
gold_-=gold;
}
int side() const
{
return target_->side();
}
bool is_enemy(int side) const
{
return target_->is_enemy(side);
}
std::vector<potential_recruit>& recruit_list()
{
return recruit_list_;
}
const std::vector<potential_recruit> &recruit_list() const
{
return recruit_list_;
}
private:
const team *target_;
int gold_;
std::vector<potential_recruit> extra_units_;
std::vector<potential_recruit> recruit_list_;
};
static int average_resistance_against(const unit_type& a, const unit_type& b)
{
int weighting_sum = 0, defense = 0;
const std::map<t_translation::t_terrain, size_t>& terrain =
resources::game_map->get_weighted_terrain_frequencies();
for (std::map<t_translation::t_terrain, size_t>::const_iterator j = terrain.begin(),
j_end = terrain.end(); j != j_end; ++j)
{
// Use only reachable tiles when computing the average defense.
if (a.movement_type().movement_cost(j->first) < movetype::UNREACHABLE) {
defense += a.movement_type().defense_modifier(j->first) * j->second;
weighting_sum += j->second;
}
}
if (weighting_sum == 0) {
// This unit can't move on this map, so just get the average weighted
// of all available terrains. This still is a kind of silly
// since the opponent probably can't recruit this unit and it's a static unit.
for (std::map<t_translation::t_terrain, size_t>::const_iterator jj = terrain.begin(),
jj_end = terrain.end(); jj != jj_end; ++jj)
{
defense += a.movement_type().defense_modifier(jj->first) * jj->second;
weighting_sum += jj->second;
}
}
if(weighting_sum != 0) {
defense /= weighting_sum;
} else {
LOG_AI << "The weighting sum is 0 and is ignored.\n";
}
//LOG_AI << "average defense of '" << a.id() << "': " << defense << "\n";
int sum = 0, weight_sum = 0;
// calculation of the average damage taken
bool steadfast = a.has_ability_by_id("steadfast");
bool poisonable = !a.musthave_status("unpoisonable");
const std::vector<attack_type>& attacks = b.attacks();
for (std::vector<attack_type>::const_iterator i = attacks.begin(),
i_end = attacks.end(); i != i_end; ++i)
{
int resistance = a.movement_type().resistance_against(*i);
// Apply steadfast resistance modifier.
if (steadfast && resistance < 100)
resistance = std::max<int>(resistance * 2 - 100, 50);
// Do not look for filters or values, simply assume 70% if CTH is customized.
int cth = i->get_special_bool("chance_to_hit", true) ? 70 : defense;
int weight = i->damage() * i->num_attacks();
// if cth == 0 the division will do 0/0 so don't execute this part
if (poisonable && cth != 0 && i->get_special_bool("poison", true)) {
// Compute the probability of not poisoning the unit.
int prob = 100;
for (int j = 0; j < i->num_attacks(); ++j)
prob = prob * (100 - cth);
// Assume poison works one turn.
weight += game_config::poison_amount * (100 - prob) / 100;
}
sum += cth * resistance * weight * weight; // average damage * weight
weight_sum += weight;
}
// normalize by HP
sum /= std::max<int>(1,std::min<int>(a.hitpoints(),1000)); // avoid values really out of range
// Catch division by zero here if the attacking unit
// has zero attacks and/or zero damage.
// If it has no attack at all, the ai shouldn't prefer
// that unit anyway.
if (weight_sum == 0) {
return sum;
}
return sum/weight_sum;
}
static int compare_unit_types(const unit_type& a, const unit_type& b)
{
const int a_effectiveness_vs_b = average_resistance_against(b,a);
const int b_effectiveness_vs_a = average_resistance_against(a,b);
DBG_AI << " comparison of '" << a.id() << " vs " << b.id() << ": "
<< 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;
}
/*static double get_unit_quality(const unit_type &info, fake_team &t, std::vector<fake_team> & fake_teams)
{
const int hitpoints_const = 100;
double score = 0;
double total_weight = 0;
BOOST_FOREACH(const unit &enemy_unit, *resources::units)
{
if(enemy_unit.can_recruit() || !t.is_enemy(enemy_unit.side()))
{
continue;
}
const unit_type *enemy_info = unit_types.find(enemy_unit.type_id());
double weight = enemy_unit.hitpoints() * hitpoints_const / enemy_unit.max_hitpoints();
total_weight += weight;
VALIDATE(enemy_info, "Unknown unit type : " + enemy_unit.type_id() + " while updating recruit quality.");
score += compare_unit_types(info, *enemy_info) * weight;
}
BOOST_FOREACH(fake_team &enemy_team, fake_teams)
{
if(!t.is_enemy(enemy_team.side()))
{
continue;
}
BOOST_FOREACH(const potential_recruit &enemy_unit, enemy_team.extra_units())
{
const unit_type *enemy_info = enemy_unit.type();
VALIDATE(enemy_info, "Unknown unit type : " + enemy_unit.id() + " while updating recruit quality.");
total_weight += hitpoints_const;
score += compare_unit_types(info, *enemy_info) * hitpoints_const;
}
}
if(total_weight != 0)
{
return score / total_weight;
}
else
{
return score;
}
}*/
/*static void update_recruit_qualities(fake_team &t, std::vector<fake_team> &fake_teams)
{
BOOST_FOREACH( potential_recruit &recruit, t.recruit_list() )
{
double score = get_unit_quality(*recruit.type(),t,fake_teams);
recruit.set_quality(score);
}
}*/
struct potential_recruit_sorter
{
potential_recruit_sorter():max_cost(0), max_quality(0), quality_factor(0)
{
}
potential_recruit_sorter(int max_cost_, double max_quality_, double quality_factor_) : max_cost(max_cost_), max_quality(max_quality_), quality_factor(quality_factor_)
{
}
bool operator()(const potential_recruit *a, const potential_recruit *b)
{
return (a->quality() * quality_factor - a->cost()
* (1.0 - quality_factor) * max_quality
/ static_cast<double>(max_cost))
> (b->quality() * quality_factor - b->cost() * (1.0 - quality_factor)
* max_quality / static_cast<double>(max_cost));
}
int max_cost;
double max_quality;
double quality_factor;
};
static std::vector<potential_recruit> ai_choose_best_recruits(fake_team &t, int max_units_to_recruit, double quality_factor, bool counter_recruit)
{
LOG_AI << "Running simple "<< (counter_recruit ? "counter-":"")<< "recruit selection algorithm for side "<< t.side() << std::endl;
std::vector<potential_recruit> recruits;
const std::vector<potential_recruit> &recruit_list = t.recruit_list();
if(recruit_list.empty())
{
return recruits;
}
std::map<std::string, int> current_units;
BOOST_FOREACH(const potential_recruit &i, t.extra_units())
{
current_units[(i.id())]++;
}
BOOST_FOREACH(const unit &i, *resources::units)
{
if (i.side()==t.side())
{
current_units[i.type().base_id()]++;
}
}
int gold = t.gold();
double max_quality = recruit_list[0].quality();
int max_cost = recruit_list[0].cost();
std::vector<const potential_recruit*> sorted = std::vector<const potential_recruit*>();
BOOST_FOREACH(const potential_recruit &i, recruit_list)
{
if(i.cost() > max_cost)
{
max_cost = i.cost();
}
if(i.quality() > max_quality)
{
max_quality = i.quality();
}
LOG_AI <<"IN "<< (counter_recruit ? "COUNTER-" : "") <<"RECRUIT: side=["<<t.side()<<"] unit=["<<i.id()<<"] quality=["<<i.quality()<<"] cost=["<<i.cost()<<"]"<< std::endl;
sorted.push_back(&i);
}
potential_recruit_sorter sorter(max_cost, max_quality, quality_factor);
std::sort(sorted.begin(), sorted.end(), sorter);
int recruited = 0;
BOOST_FOREACH(const potential_recruit *i, sorted)
{
if(recruited < max_units_to_recruit)
{
int possible_amount = gold / i->cost();
if(possible_amount > max_units_to_recruit - recruited)
{
possible_amount = max_units_to_recruit - recruited;
}
if(possible_amount > i->max_qty() - current_units[i->id()])
{
possible_amount = i->max_qty() - current_units[i->id()];
}
for(int j = 0; j < possible_amount; j++)
{
recruits.push_back(*i);
}
gold -= possible_amount * i->cost();
recruited += possible_amount;
}
else
{
break;
}
}
LOG_AI << "Finished simple recruit selection algorithm for side "<< t.side() << std::endl;
return recruits;
}
static void ai_choose_recruits(fake_team &t, int max_units_to_recruit, double quality_factor, bool counter_recruit)
{
std::vector<potential_recruit> recruits = ai_choose_best_recruits(t, max_units_to_recruit, quality_factor, counter_recruit);
BOOST_FOREACH(potential_recruit &i, recruits) {
t.fake_recruit(i);
}
}
/*struct unit_data{
unit_data(const std::string _id, const unit_type *_type):id(_id), type(*_type)
{}
const std::string id;
const unit_type &type;
};*/
//class defender_pair_type{
//public:
// //defender_pair_type(const unit_data *_defender):defender(_defender){}
// defender_pair_type(const unit_type *_defender):defender(_defender){}
//// ~defender_pair_type(){
//// delete defender;
//// for(unsigned int i = 0; i < enemies.size(); i++){
//// delete enemies[i];
//// }
//// }
//// const unit_data* defender;
//// std::vector<unit_data*> enemies;
//// void add_enemy(unit_data *data){
//// enemies.push_back(data);
//// }
// const unit_type *defender;
// std::vector<const unit_type*> enemies;
// void add_enemy(const unit_type *type){
// enemies.push_back(type);
// }
//};
//class enemy_pair_type{
//public:
//// enemy_pair_type(const unit_data *_enemy):enemy(_enemy){
//// score = 0;
////
//// }
//// ~enemy_pair_type(){
//// delete enemy;
//// for(unsigned int i = 0; i < defenders.size(); i++){
//// delete defenders[i];
//// }
//// }
//// void add_defender(unit_data *data){
//// defenders.push_back(data);
//// }
//// const unit_data* enemy;
//// std::vector<unit_data*> defenders;
// enemy_pair_type(const unit_type *_enemy):enemy(_enemy){
// score = 0;
// }
// void add_defender(const unit_type *defender){
// defenders.push_back(defender);
// }
// const unit_type *enemy;
// std::vector<const unit_type*> defenders;
// double score;
//};
static void get_recruit_qualities(std::vector<potential_recruit> &recruit_list, fake_team &t, std::vector<fake_team> &fake_teams)
{
//DBG_AI << "start of get_recruit_qualities" << std::endl;
typedef std::map<const unit_type*, std::vector<double> > unit_map;
unit_map enemies;
BOOST_FOREACH(unit &un, *resources::units){
if(t.is_enemy(un.side()) && !un.can_recruit()){
enemies[&un.type()].push_back(
static_cast<double>(un.hitpoints())
/ static_cast<double>(un.max_hitpoints()));
}
}
DBG_AI << "before extra_units of fake_teams: enemies.size() = " << enemies.size() << std::endl;
BOOST_FOREACH(fake_team &tmp_t, fake_teams)
{
if (t.is_enemy(tmp_t.side())) {
BOOST_FOREACH(potential_recruit &rec, tmp_t.extra_units())
{
enemies[rec.type()].push_back(1.0);
}
}
}
DBG_AI << "after extra_units of fake_teams: enemies.size() = " << enemies.size() << std::endl;
BOOST_FOREACH(potential_recruit &rec, recruit_list) {
double score = 0;
double weighting = 0;
BOOST_FOREACH(unit_map::value_type &enemy, enemies) {
double hitpoints_sum = std::accumulate(enemy.second.begin(),enemy.second.end(),0);
score += compare_unit_types(*rec.type(), *enemy.first) * hitpoints_sum;
weighting += hitpoints_sum;
}
if (weighting!=0) {
score /= weighting;
}
rec.set_quality(score);
LOG_AI <<"side=["<<t.side()<<"] unit=["<<rec.id()<<"] quality=["<<rec.quality()<<"] cost=["<<rec.cost()<<"]"<< std::endl;
}
}
/*
class unit_type_health{
public:
unit_type_health(const unit_type *type, double health):type_(type), health_(health){
}
double health(){return health_;}
const unit_type* type(){return type_;}
protected:
const unit_type* type_;
double health_;
};
*/
static void get_recruit_qualities(fake_team &t, std::vector<fake_team> &fake_teams)
{
get_recruit_qualities(t.recruit_list(),t,fake_teams);
}
static void get_recruit_quality(potential_recruit &rec, fake_team &t, std::vector<fake_team> &fake_teams)
{
std::vector<potential_recruit> recruits;
recruits.push_back(rec);
get_recruit_qualities(recruits,t,fake_teams);
rec.set_quality(recruits[0].quality());
}
/*static double get_combat_score2(fake_team &t, std::vector<fake_team> &fake_teams)
{
std::vector<defender_pair_type*> defenders;
std::vector<enemy_pair_type*> enemies;
BOOST_FOREACH(unit &un, *resources::units)
{
if(t.is_enemy(un.side()))
{
// const unit_type &enemy_type = *un.type();
//enemy_pair_type *pair = new enemy_pair_type(new unit_data(un.id(), un.type()));
enemy_pair_type *pair = new enemy_pair_type(un.type());
BOOST_FOREACH(unit &defender, *resources::units)
{
if(!t.is_enemy(defender.side())){
//int score = compare_unit_types(*defender.type(), enemy_type);
//if(score >= 0)
//{
//pair->add_defender(new unit_data(defender.id(), defender.type()));
pair->add_defender(defender.type());
//}
}
}
BOOST_FOREACH(fake_team &tmp_t, fake_teams){
if(!t.is_enemy(tmp_t.side())){
BOOST_FOREACH(potential_recruit &rec, tmp_t.extra_units()){
// int score = compare_unit_types(*rec.type(), enemy_type);
// if(score >= 0){
//pair->add_defender(new unit_data(rec.id(), rec.type()));
pair->add_defender(rec.type());
// }
}
}
}
enemies.push_back(pair);
}
else
{
//const unit_type &defender_type = *un.type();
//defender_pair_type *pair = new defender_pair_type(new unit_data(un.id(), un.type()));
defender_pair_type *pair = new defender_pair_type(un.type());
BOOST_FOREACH(unit &enemy, *resources::units)
{
//int score = compare_unit_types(defender_type, *enemy.type());
//if(score >= 0)
//{
//pair->add_enemy(new unit_data(enemy.id(), enemy.type()));
pair->add_enemy(enemy.type());
//}
}
BOOST_FOREACH(fake_team &tmp_t, fake_teams){
if(t.is_enemy(tmp_t.side())){
BOOST_FOREACH(potential_recruit &rec, tmp_t.extra_units()){
//int score = compare_unit_types(defender_type, *rec.type());
//if(score >= 0){
//pair->add_enemy(new unit_data(rec.id(), rec.type()));
pair->add_enemy(rec.type());
//}
}
}
}
defenders.push_back(pair);
}
}
BOOST_FOREACH(fake_team &tmp_t, fake_teams)
{
BOOST_FOREACH(potential_recruit &rec, tmp_t.extra_units()){
if(t.is_enemy(tmp_t.side())){
//const unit_type &enemy_type = *rec.type();
//enemy_pair_type *pair = new enemy_pair_type(new unit_data(rec.id(), rec.type()));
enemy_pair_type *pair = new enemy_pair_type(rec.type());
BOOST_FOREACH(unit &defender, *resources::units){
if(t.is_enemy(defender.side())){
continue;
}
//int score = compare_unit_types(*defender.type(), enemy_type);
//if(score >= 0){
//pair->add_defender(new unit_data(defender.id(), defender.type()));
pair->add_defender(defender.type());
//}
}
//HIER
BOOST_FOREACH(fake_team &sub_t, fake_teams){
if(t.is_enemy(sub_t.side())){
continue;
}
BOOST_FOREACH(potential_recruit &sub_rec, sub_t.extra_units()){
//int score = compare_unit_types(*sub_rec.type(), enemy_type);
//if(score >= 0){
//pair->add_defender(new unit_data(sub_rec.id(), sub_rec.type()));
pair->add_defender(sub_rec.type());
//}
}
}
enemies.push_back(pair);
}else{
//const unit_type &defender_type = *rec.type();
//defender_pair_type *pair = new defender_pair_type(new unit_data(rec.id(), rec.type()));
defender_pair_type *pair = new defender_pair_type(rec.type());
BOOST_FOREACH(unit &enemy, *resources::units)
{
if(!t.is_enemy(enemy.side())){
continue;
}
// int score = compare_unit_types(defender_type, *enemy.type());
// if(score >= 0){
//pair->add_enemy(new unit_data(rec.id(), rec.type()));
pair->add_enemy(rec.type());
// }
}
BOOST_FOREACH(fake_team &sub_t, fake_teams){
if(!t.is_enemy(sub_t.side())){
continue;
}
BOOST_FOREACH(potential_recruit &sub_rec, sub_t.extra_units()){
// int score = compare_unit_types(defender_type, *sub_rec.type());
// if(score >= 0){
//pair->add_enemy(new unit_data(sub_rec.id(), sub_rec.type()));
pair->add_enemy(sub_rec.type());
// }
}
}
defenders.push_back(pair);
}
}
}
double min_score = 0;
double max_score = 0;
BOOST_FOREACH(enemy_pair_type *pair, enemies)
{
// if(pair->defenders.size() == 0)
// {
// pair->score = -10000;
// }else{
//BOOST_FOREACH(unit_data *defender, pair->defenders)
BOOST_FOREACH(unit_type *defender, pair->defenders)
{
unsigned int defender_enemies = 0;
BOOST_FOREACH(defender_pair_type *defender_p, defenders)
{
//if(defender->id == defender_p->defender->id){
if(defender->type_name()() == defender_p->defender->type_name()){
defender_enemies = defender_p->enemies.size();
break;
}
}
//double tmpscore =(compare_unit_types(defender->type, pair->enemy->type) / ((defender_enemies != 0) ? defender_enemies : 1 ));
double tmpscore =(compare_unit_types(defender, pair->enemy) / ((defender_enemies != 0) ? defender_enemies : 1 ));
if(tmpscore > 0){
pair->score += tmpscore;
LOG_AIT << defender->type_name() << " resistance against " << pair->enemy->type_name() << " is: " << tmpscore << ", new score = " << pair->score << std::endl;
}
}
if(pair->score > max_score)
max_score = pair->score;
if(pair->score < min_score)
min_score = pair->score;
LOG_AIT << pair->enemy->id << " resistance = " << pair->score << std::endl;
//}
}
double score = 0;
score -= max_score - min_score;
BOOST_FOREACH(enemy_pair_type *pair, enemies)
{
score += pair->score;
}
for(unsigned int i = 0; i < enemies.size(); i++)
{
delete enemies[i];
}
for(unsigned int i = 0; i < defenders.size(); i++)
{
delete defenders[i];
}
return score;
}*/
/*static double get_combat_score(fake_team &t, std::vector<fake_team> &fake_teams)
{
typedef std::map<const unit_type*, std::vector<double> > unit_map;
unit_map enemies;
unit_map defenders;
BOOST_FOREACH(unit &un, *resources::units){
if(t.is_enemy(un.side())){
enemies[un.type()].push_back((double)un.hitpoints() / (double)un.max_hitpoints());
}else{
defenders[un.type()].push_back(un.hitpoints() / un.max_hitpoints());
}
}
BOOST_FOREACH(fake_team &tmp_t, fake_teams)
{
BOOST_FOREACH(potential_recruit &rec, tmp_t.extra_units())
{
if(t.is_enemy(tmp_t.side())){
enemies[rec.type()].push_back(1.0);
}else{
defenders[rec.type()].push_back(1.0);
}
}
}
double result = 0;
BOOST_FOREACH(unit_map::value_type &defender, defenders)
{
double defenders_score = 0;
BOOST_FOREACH(unit_map::value_type &enemy, enemies)
{
double hitpoints_sum = 0;
BOOST_FOREACH(double i, enemy.second)
{
hitpoints_sum += i;
}
defenders_score += compare_unit_types(*defender.first, *enemy.first) / ((hitpoints_sum == 0)?1:hitpoints_sum);
}
double hitpoints_sum = 0;
BOOST_FOREACH(double i, defender.second)
{
hitpoints_sum += i;
}
defenders_score *= hitpoints_sum;
result += defenders_score;
}
return result;
// vector<defender_pair> defenders;
// vector<enemy_pair> enemies;
// //BOOST_FOREACH(unit &un, *resources::units)
// //{
// // if(t.is_enemy(un.side()))
// // {
// // enemies.push_back(un.id());
// // }else{
// // defenders.push_back(un.id());
// // }
// //}
// BOOST_FOREACH(unit &un, *resources::units)
// {
// if(t.is_enemy(un.side()))
// {
// const unit_type &enemy_type = un.type();
// enemy_pair pair;
// pair.enemy = new unit_data(un.id(), enemy_type);
// BOOST_FOREACH(unit &defender, *resources::units)
// {
// int score = compare_unit_types(defender.type(), enemy_type);
// if(score >= 0)
// {
// pair.defenders.push_back(new unit_data(defender.id(), defender.type()));
// }
// }
// defenders.push_back(pair);
// }
// else
// {
// const unit_type &defender_type = un.type();
// defender_pair pair;
// pair.defender = new unit_data(defender.id(), defender_type);
// BOOST_FOREACH(unit &enemy, *resources::units)
// {
// int score = compare_unit_types(defender_type, enemy.type());
// if(score >= 0)
// {
// pair.enemies.push_back(new unit_data(enemy.id(), enemy.type()));
// //pair.score += score;
// }
// }
// enemies.push_back(pair);
// }
// }
// BOOST_FOREACH(fake_team &tmp_t, fake_teams)
// {
// if(t.is_enemy(tmp_t.side())){
// BOOST_FOREACH(potential_recruit &rec, tmp_t.extra_units())
// {
//
// }
// }else{
//
// }
//
// }
// //std::vector<unit> no_defense_enemies;
// BOOST_FOREACH(enemy_pair &pair, enemies)
// {
// if(pair.defenders.size() == 0)
// {
// //no_defense_enemies.push_back(pair.enemy);
//
// pair.score = 0;
// }else{
// BOOST_FOREACH(unit_data &defender, pair.defenders)
// {
// //unit_type &defender = defender_data.type;
// unsigned int defender_enemies = 0;
// BOOST_FOREACH(defender_pair &defender_p, defenders)
// {
// if(defender.id == defender_p.defender.id){
// defender_enemies = defender_p.enemies.size();
// break;
// }
// }
//
// pair.score += (compare_unit_types(defender.type, pair.enemy.type) / ((defender_enemies != 0) ? defender_enemies : 1 ));
// }
// }
// }
// //enemy_pair *worst_pair = &enemies[0];
// double total_score = 0;
// BOOST_FOREACH(enemy_pair &pair, enemies)
// {
// total_score += pair.score;
// }
// return worst_pair->enemy;
}*/
//static void check_worst_defense(fake_team &t)
//{
// //std::vector<unit_pair> pairs;
// //unit &worst_def_unit;
// vector<defender_pair> defenders;
// vector<enemy_pair> enemies;
// /*BOOST_FOREACH(unit &un, *resources::units)
// {
// if(t.is_enemy(un.side()))
// {
// enemies.push_back(un.id());
// }else{
// defenders.push_back(un.id());
// }
// }*/
// BOOST_FOREACH(unit &un, *resources::units)
// {
// if(t.is_enemy(un.side()))
// {
// const unit_type &enemy_type = un.type();
// enemy_pair pair;
// pair.enemy = un;
// BOOST_FOREACH(unit &defender, *resources::units)
// {
// int score = compare_unit_types(defender.type(), enemy_type);
// if(score >= 0)
// {
// pair.defenders.push_back(defender);
// }
// }
// defenders.push_back(pair);
// }
// else
// {
// const unit_type &defender_type = un.type();
// defender_pair pair;
// pair.defender = un;
// BOOST_FOREACH(unit &enemy, *resources::units)
// {
// int score = compare_unit_types(defender_type, enemy.type());
// if(score >= 0)
// {
// pair.enemies.push_back(enemy);
// //pair.score += score;
// }
// }
// enemies.push_back(pair);
// }
// }
// //std::vector<unit> no_defense_enemies;
// BOOST_FOREACH(enemy_pair &pair, enemies)
// {
// if(pair.defenders.size() == 0)
// {
// //no_defense_enemies.push_back(pair.enemy);
// return pair.enemy;
// //pair.score = 0;
// }else{
// BOOST_FOREACH(unit &defender, pair.defenders)
// {
// unsigned int defender_enemies = 0;
// BOOST_FOREACH(defender_pair &defender_p, defenders)
// {
// if(defender.id() == defender_p.defender.id()){
// defender_enemies = defender_p.enemies.size();
// break;