00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ca_testing_recruitment.hpp"
00022 #include "../actions.hpp"
00023 #include "../manager.hpp"
00024 #include "../composite/engine.hpp"
00025 #include "../composite/rca.hpp"
00026 #include "../composite/stage.hpp"
00027 #include "../../gamestatus.hpp"
00028 #include "../../foreach.hpp"
00029 #include "../../log.hpp"
00030 #include "../../map.hpp"
00031 #include "../../resources.hpp"
00032 #include "../../team.hpp"
00033 #include "../../wml_exception.hpp"
00034 #include "../../pathfind/pathfind.hpp"
00035
00036
00037 #include <numeric>
00038 #include <string>
00039 #include <vector>
00040 #include <algorithm>
00041 #include <iostream>
00042 #include <map>
00043
00044
00045 namespace ai {
00046
00047 namespace testing_ai_default {
00048
00049 static lg::log_domain log_ai_ca_testing_recruitment("ai/ca/testing_recruitment");
00050 #define DBG_AI LOG_STREAM(debug, log_ai_ca_testing_recruitment)
00051 #define LOG_AI LOG_STREAM(info, log_ai_ca_testing_recruitment)
00052 #define WRN_AI LOG_STREAM(warn, log_ai_ca_testing_recruitment)
00053 #define ERR_AI LOG_STREAM(err, log_ai_ca_testing_recruitment)
00054
00055
00056 testing_recruitment_phase::testing_recruitment_phase( rca_context &context, const config &cfg )
00057 : candidate_action(context,cfg)
00058 {
00059 }
00060
00061
00062 testing_recruitment_phase::~testing_recruitment_phase()
00063 {
00064 }
00065
00066
00067 double testing_recruitment_phase::evaluate()
00068 {
00069 const unit_map::const_iterator leader = resources::units->find_leader(get_side());
00070 if(leader == resources::units->end()) {
00071 return BAD_SCORE;
00072 }
00073 if (!resources::game_map->is_keep(leader->get_location())) {
00074 return BAD_SCORE;
00075 }
00076
00077 std::set<map_location> checked_hexes;
00078 checked_hexes.insert(leader->get_location());
00079 if (count_free_hexes_in_castle(leader->get_location(), checked_hexes)==0) {
00080 return BAD_SCORE;
00081 }
00082 return get_score();
00083 }
00084
00085
00086 class potential_recruit
00087 {
00088 public:
00089 bool operator==(const std::string &i) const
00090 {
00091 return id() == i;
00092 }
00093
00094 potential_recruit(int cost, int max_qty, double quality, int side, const unit_type *type)
00095 : cost_(cost), max_qty_(max_qty), quality_(quality), side_(side), type_(type)
00096 {}
00097
00098 const std::string& id() const
00099 {
00100 return type_->id();
00101 }
00102 int cost() const
00103 {
00104 return cost_;
00105 }
00106 double quality() const
00107 {
00108 return quality_;
00109 }
00110 void set_quality(double quality)
00111 {
00112 quality_ = quality;
00113 }
00114 int max_qty() const
00115 {
00116 return max_qty_;
00117 }
00118 int side() const
00119 {
00120 return side_;
00121 }
00122 const unit_type *type() const
00123 {
00124 return type_;
00125 }
00126 private:
00127 int cost_;
00128 int max_qty_;
00129 double quality_;
00130 int side_;
00131 const unit_type *type_;
00132 };
00133
00134 class potential_recruit_converter
00135 {
00136 public:
00137 potential_recruit_converter(int max_qty, int side )
00138 : max_qty_ (max_qty), side_(side)
00139 {
00140 }
00141 potential_recruit operator()(const std::string &id)
00142 {
00143 const unit_type *type = unit_types.find(id);
00144 return potential_recruit(type->cost(),max_qty_,0,side_,type);
00145 }
00146 private:
00147 int max_qty_;
00148 int side_;
00149 };
00150
00151 class fake_team
00152 {
00153 public:
00154 fake_team(const team &target)
00155 : target_(&target)
00156 , gold_(0)
00157 , extra_units_()
00158 , recruit_list_()
00159 {
00160 reset();
00161 }
00162
00163 void reset()
00164 {
00165 extra_units_.clear();
00166 gold_ = target_->gold();
00167 recruit_list_.clear();
00168 std::transform(target_->recruits().begin(),target_->recruits().end(),std::back_inserter(recruit_list_), potential_recruit_converter(999,side()));
00169 }
00170
00171 int gold()
00172 {
00173 return gold_;
00174 }
00175
00176 void set_gold(int gold)
00177 {
00178 gold_ = gold;
00179 }
00180
00181 void fake_recruit(const potential_recruit &r)
00182 {
00183 if (gold()<r.cost()) {
00184 ERR_AI << "ERROR: cannot fake recruit "<<r.id()<<", not enough gold" << std::endl;
00185 return;
00186 }
00187 if (get_current_qty(r)>=r.max_qty()) {
00188 ERR_AI << "ERROR: cannot fake recruit "<<r.id()<<", too many in the field" << std::endl;
00189 return;
00190 }
00191 DBG_AI << " Fake recruiting [" << r.id() << "] for side [" << side() << "]"<< std::endl;
00192 spend_gold(r.cost());
00193 extra_units_.push_back(r);
00194 }
00195
00196 std::vector<potential_recruit>& extra_units()
00197 {
00198 return extra_units_;
00199 }
00200
00201 int get_current_qty(const potential_recruit &r) const
00202 {
00203 return get_current_qty(r.id());
00204 }
00205
00206 int get_current_qty(const std::string &name) const
00207 {
00208 int counter = 0;
00209 foreach(unit &un, *resources::units){
00210 if(un.side() == side() && un.type_id() == name)
00211 {
00212 counter++;
00213 }
00214 }
00215 return std::count(extra_units_.begin(), extra_units_.end(), name) + counter;
00216 }
00217
00218 void spend_gold(int gold)
00219 {
00220 gold_-=gold;
00221 }
00222
00223 int side() const
00224 {
00225 return target_->side();
00226 }
00227
00228 bool is_enemy(int side) const
00229 {
00230 return target_->is_enemy(side);
00231 }
00232
00233 std::vector<potential_recruit>& recruit_list()
00234 {
00235 return recruit_list_;
00236 }
00237
00238 const std::vector<potential_recruit> &recruit_list() const
00239 {
00240 return recruit_list_;
00241 }
00242
00243 private:
00244 const team *target_;
00245 int gold_;
00246 std::vector<potential_recruit> extra_units_;
00247 std::vector<potential_recruit> recruit_list_;
00248 };
00249
00250 static int average_resistance_against(const unit_type& a, const unit_type& b)
00251 {
00252 gamemap &map_ = *resources::game_map;
00253
00254 int weighting_sum = 0, defense = 0;
00255 const std::map<t_translation::t_terrain, size_t>& terrain =
00256 map_.get_weighted_terrain_frequencies();
00257
00258 for (std::map<t_translation::t_terrain, size_t>::const_iterator j = terrain.begin(),
00259 j_end = terrain.end(); j != j_end; ++j)
00260 {
00261
00262 if (a.movement_type().movement_cost(map_, j->first) < unit_movement_type::UNREACHABLE) {
00263 defense += a.movement_type().defense_modifier(map_, j->first) * j->second;
00264 weighting_sum += j->second;
00265 }
00266 }
00267
00268 if (weighting_sum == 0) {
00269
00270
00271
00272 for (std::map<t_translation::t_terrain, size_t>::const_iterator jj = terrain.begin(),
00273 jj_end = terrain.end(); jj != jj_end; ++jj)
00274 {
00275 defense += a.movement_type().defense_modifier(map_, jj->first) * jj->second;
00276 weighting_sum += jj->second;
00277 }
00278 }
00279
00280 if(weighting_sum != 0) {
00281 defense /= weighting_sum;
00282 } else {
00283 LOG_AI << "The weighting sum is 0 and is ignored.\n";
00284 }
00285
00286
00287
00288 int sum = 0, weight_sum = 0;
00289
00290
00291 bool steadfast = a.has_ability_by_id("steadfast");
00292 bool living = !a.not_living();
00293 const std::vector<attack_type>& attacks = b.attacks();
00294 for (std::vector<attack_type>::const_iterator i = attacks.begin(),
00295 i_end = attacks.end(); i != i_end; ++i)
00296 {
00297 int resistance = a.movement_type().resistance_against(*i);
00298
00299 if (steadfast && resistance < 100)
00300 resistance = std::max<int>(resistance * 2 - 100, 50);
00301
00302 int cth = i->get_special_bool("chance_to_hit", true) ? 70 : defense;
00303 int weight = i->damage() * i->num_attacks();
00304
00305 if (living && cth != 0 && i->get_special_bool("poison", true)) {
00306
00307 int prob = 100;
00308 for (int j = 0; j < i->num_attacks(); ++j)
00309 prob = prob * (100 - cth);
00310
00311 weight += game_config::poison_amount * (100 - prob) / 100;
00312 }
00313 sum += cth * resistance * weight * weight;
00314 weight_sum += weight;
00315 }
00316
00317
00318 sum /= std::max<int>(1,std::min<int>(a.hitpoints(),1000));
00319
00320
00321
00322
00323
00324 if (weight_sum == 0) {
00325 return sum;
00326 }
00327 return sum/weight_sum;
00328 }
00329
00330
00331 static int compare_unit_types(const unit_type& a, const unit_type& b)
00332 {
00333 const int a_effectiveness_vs_b = average_resistance_against(b,a);
00334 const int b_effectiveness_vs_a = average_resistance_against(a,b);
00335
00336 DBG_AI << " comparison of '" << a.id() << " vs " << b.id() << ": "
00337 << a_effectiveness_vs_b << " - " << b_effectiveness_vs_a << " = "
00338 << (a_effectiveness_vs_b - b_effectiveness_vs_a) << '\n';
00339 return a_effectiveness_vs_b - b_effectiveness_vs_a;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 struct potential_recruit_sorter
00397 {
00398 potential_recruit_sorter():max_cost(0), max_quality(0), quality_factor(0)
00399 {
00400 }
00401 potential_recruit_sorter(int max_cost_, double max_quality_, double quality_factor_) : max_cost(max_cost_), max_quality(max_quality_), quality_factor(quality_factor_)
00402 {
00403 }
00404 bool operator()(const potential_recruit *a, const potential_recruit *b)
00405 {
00406 return (a->quality() * quality_factor - a->cost()
00407 * (1.0 - quality_factor) * max_quality
00408 / static_cast<double>(max_cost))
00409 > (b->quality() * quality_factor - b->cost() * (1.0 - quality_factor)
00410 * max_quality / static_cast<double>(max_cost));
00411 }
00412
00413 int max_cost;
00414 double max_quality;
00415 double quality_factor;
00416 };
00417 static std::vector<potential_recruit> ai_choose_best_recruits(fake_team &t, int max_units_to_recruit, double quality_factor, bool counter_recruit)
00418 {
00419 LOG_AI << "Running simple "<< (counter_recruit ? "counter-":"")<< "recruit selection algorithm for side "<< t.side() << std::endl;
00420 std::vector<potential_recruit> recruits;
00421 const std::vector<potential_recruit> &recruit_list = t.recruit_list();
00422 if(recruit_list.empty())
00423 {
00424 return recruits;
00425 }
00426
00427 std::map<std::string, int> current_units;
00428 foreach (const potential_recruit &i, t.extra_units())
00429 {
00430 current_units[(i.id())]++;
00431 }
00432 foreach (const unit &i, *resources::units)
00433 {
00434 if (i.side()==t.side())
00435 {
00436 current_units[(i.type_id())]++;
00437 }
00438 }
00439 int gold = t.gold();
00440 double max_quality = recruit_list[0].quality();
00441 int max_cost = recruit_list[0].cost();
00442 std::vector<const potential_recruit*> sorted = std::vector<const potential_recruit*>();
00443 foreach(const potential_recruit &i, recruit_list)
00444 {
00445 if(i.cost() > max_cost)
00446 {
00447 max_cost = i.cost();
00448 }
00449 if(i.quality() > max_quality)
00450 {
00451 max_quality = i.quality();
00452 }
00453
00454 LOG_AI <<"IN "<< (counter_recruit ? "COUNTER-" : "") <<"RECRUIT: side=["<<t.side()<<"] unit=["<<i.id()<<"] quality=["<<i.quality()<<"] cost=["<<i.cost()<<"]"<< std::endl;
00455 sorted.push_back(&i);
00456 }
00457 potential_recruit_sorter sorter(max_cost, max_quality, quality_factor);
00458 std::sort(sorted.begin(), sorted.end(), sorter);
00459 int recruited = 0;
00460 foreach(const potential_recruit *i, sorted)
00461 {
00462 if(recruited < max_units_to_recruit)
00463 {
00464 int possible_amount = static_cast<int>(gold / i->cost());
00465 if(possible_amount > max_units_to_recruit - recruited)
00466 {
00467 possible_amount = max_units_to_recruit - recruited;
00468 }
00469 if(possible_amount > i->max_qty() - current_units[i->id()])
00470 {
00471 possible_amount = i->max_qty() - current_units[i->id()];
00472 }
00473 for(int j = 0; j < possible_amount; j++)
00474 {
00475 recruits.push_back(*i);
00476 }
00477 gold -= possible_amount * i->cost();
00478 recruited += possible_amount;
00479 }
00480 else
00481 {
00482 break;
00483 }
00484 }
00485 LOG_AI << "Finished simple recruit selection algorithm for side "<< t.side() << std::endl;
00486 return recruits;
00487 }
00488 static void ai_choose_recruits(fake_team &t, int max_units_to_recruit, double quality_factor, bool counter_recruit)
00489 {
00490 std::vector<potential_recruit> recruits = ai_choose_best_recruits(t, max_units_to_recruit, quality_factor, counter_recruit);
00491 foreach(potential_recruit &i, recruits) {
00492 t.fake_recruit(i);
00493 }
00494
00495 }
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 static void get_recruit_qualities(std::vector<potential_recruit> &recruit_list, fake_team &t, std::vector<fake_team> &fake_teams)
00553 {
00554
00555 typedef std::map<const unit_type*, std::vector<double> > unit_map;
00556 unit_map enemies;
00557 foreach(unit &un, *resources::units){
00558 if(t.is_enemy(un.side()) && !un.can_recruit()){
00559 enemies[un.type()].push_back(
00560 static_cast<double>(un.hitpoints())
00561 / static_cast<double>(un.max_hitpoints()));
00562 }
00563 }
00564 DBG_AI << "before extra_units of fake_teams: enemies.size() = " << enemies.size() << std::endl;
00565 foreach(fake_team &tmp_t, fake_teams)
00566 {
00567 if (t.is_enemy(tmp_t.side())) {
00568 foreach(potential_recruit &rec, tmp_t.extra_units())
00569 {
00570 enemies[rec.type()].push_back(1.0);
00571 }
00572 }
00573 }
00574 DBG_AI << "after extra_units of fake_teams: enemies.size() = " << enemies.size() << std::endl;
00575
00576 foreach(potential_recruit &rec, recruit_list) {
00577 double score = 0;
00578 double weighting = 0;
00579 foreach(unit_map::value_type &enemy, enemies) {
00580 double hitpoints_sum = std::accumulate(enemy.second.begin(),enemy.second.end(),0);
00581 score += compare_unit_types(*rec.type(), *enemy.first) * hitpoints_sum;
00582 weighting += hitpoints_sum;
00583 }
00584 if (weighting!=0) {
00585 score /= weighting;
00586 }
00587 rec.set_quality(score);
00588 LOG_AI <<"side=["<<t.side()<<"] unit=["<<rec.id()<<"] quality=["<<rec.quality()<<"] cost=["<<rec.cost()<<"]"<< std::endl;
00589 }
00590
00591 }
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 static void get_recruit_qualities(fake_team &t, std::vector<fake_team> &fake_teams)
00608 {
00609 get_recruit_qualities(t.recruit_list(),t,fake_teams);
00610 }
00611
00612 static void get_recruit_quality(potential_recruit &rec, fake_team &t, std::vector<fake_team> &fake_teams)
00613 {
00614 std::vector<potential_recruit> recruits;
00615 recruits.push_back(rec);
00616 get_recruit_qualities(recruits,t,fake_teams);
00617 rec.set_quality(recruits[0].quality());
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 void testing_recruitment_phase::do_recruit(int max_units_to_recruit, double quality_factor)
01018 {
01019 std::vector<fake_team> tmp_fake_teams;
01020 std::vector<fake_team> fake_teams;
01021 std::copy(resources::teams->begin(), resources::teams->end(), std::back_inserter(tmp_fake_teams));
01022 fake_team *ai_t = 0;
01023 for(int i = get_side() - 1
01024 ; static_cast<unsigned int>(i) < tmp_fake_teams.size()
01025 ; i++)
01026 {
01027 fake_teams.push_back(tmp_fake_teams[i]);
01028
01029 }
01030 for(int i = 0; i < get_side() - 1; i++)
01031 {
01032 fake_teams.push_back(tmp_fake_teams[i]);
01033 }
01034 ai_t = &fake_teams[0];
01035 if(ai_t->recruit_list().empty())
01036 {
01037 return;
01038 }
01039 for(int recruited_amount = 0; recruited_amount < max_units_to_recruit; recruited_amount++)
01040 {
01041
01042 foreach(fake_team &t, fake_teams)
01043 {
01044 t.reset();
01045 }
01046
01047 std::vector<potential_recruit> ai_recruit_list = ai_t->recruit_list();
01048
01049 foreach(potential_recruit &recruit_type, ai_recruit_list)
01050 {
01051 foreach(fake_team &t, fake_teams)
01052 {
01053 t.reset();
01054 }
01055
01056 if(ai_t->gold() < recruit_type.cost())
01057 {
01058 continue;
01059 }
01060 if(ai_t->get_current_qty(recruit_type) >= recruit_type.max_qty())
01061 {
01062 continue;
01063 }
01064 LOG_AI << "Pretend that we recruited: " << recruit_type.id() << std::endl;
01065 ai_t->fake_recruit(recruit_type);
01066 foreach(fake_team &t, fake_teams)
01067 {
01068 if(ai_t->side() == t.side())
01069 {
01070 continue;
01071 }
01072 LOG_AI << "evaluating reaction of fake_team " << t.side() << std::endl;
01073
01074
01075 int enemy_max_units = 5;
01076
01077 double enemy_quality_factor = 1.0;
01078
01079
01080 get_recruit_qualities(t, fake_teams);
01081
01082 ai_choose_recruits(t, enemy_max_units, enemy_quality_factor, true);
01083
01084 }
01085 get_recruit_quality(recruit_type,*ai_t, fake_teams);
01086 }
01087 ai_t->recruit_list() = ai_recruit_list;
01088
01089 std::vector<potential_recruit> recruit_result = ai_choose_best_recruits(*ai_t, 1, quality_factor,false);
01090 if(recruit_result.empty())
01091 {
01092 std::cout << "recruit_result = empty" << std::endl;
01093 break;
01094 }
01095 const potential_recruit &recruit_unit = recruit_result[0];
01096 std::cout << "recruit: " << recruit_unit.id() << std::endl;
01097 if(ai_t->gold() >= recruit_unit.cost())
01098 {
01099 recruit_result_ptr recruit_action = check_recruit_action(recruit_unit.id());
01100 if (recruit_action->is_ok())
01101 {
01102 recruit_action->execute();
01103 if (!recruit_action->is_ok()){
01104 ERR_AI << "recruit failed" << std::endl;
01105 }
01106 } else {
01107 return;
01108 }
01109 }
01110 else
01111 {
01112 std::cout << "gold not ok" << std::endl;
01113 }
01114 }
01115 }
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140 void testing_recruitment_phase::execute()
01141 {
01142 std::cout << "execute floris' recruitment algorithm" << std::endl;
01143 int max_units_to_recruit = 1;
01144 double quality_factor = 1.0;
01145 do_recruit(max_units_to_recruit, quality_factor);
01146 }
01147
01148 }
01149
01150 }