/
actions.cpp
1154 lines (984 loc) · 33.2 KB
/
actions.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 - 2015 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.
*/
/**
* Managing the AI-Game interaction - AI actions and their results
* @file
*/
/**
* A small explanation about what's going on here:
* Each action has access to two game_info objects
* First is 'info' - real information
* Second is 'subjective info' - AIs perception of what's going on
* So, when we check_before action, we use 'subjective info' and don't
* touch real 'info' at all.
* But when we actually want to execute an action, we firstly check
* 'subjective info' and then (if subjective check is ok) do the same
* check on real 'info'. There's a caveat: if we fail an action based
* on real 'info', then we NEED to update AIs knowledge to avoid the ai
* doing the same thing again.
* So far the use of 'subjective info' is stubbed out.
*/
#include "actions.hpp"
#include "manager.hpp"
#include "simulated_actions.hpp"
#include "../actions/attack.hpp"
#include "../actions/create.hpp"
#include "../attack_prediction.hpp"
#include "../game_preferences.hpp"
#include "../log.hpp"
#include "../mouse_handler_base.hpp"
#include "../pathfind/teleport.hpp"
#include "../play_controller.hpp"
#include "../recall_list_manager.hpp"
#include "../replay_helper.hpp"
#include "../resources.hpp"
#include "../synced_context.hpp"
#include "../team.hpp"
#include "../unit.hpp"
#include "../unit_ptr.hpp"
namespace ai {
static lg::log_domain log_ai_actions("ai/actions");
#define DBG_AI_ACTIONS LOG_STREAM(debug, log_ai_actions)
#define LOG_AI_ACTIONS LOG_STREAM(info, log_ai_actions)
#define WRN_AI_ACTIONS LOG_STREAM(warn, log_ai_actions)
#define ERR_AI_ACTIONS LOG_STREAM(err, log_ai_actions)
// =======================================================================
// AI ACTIONS
// =======================================================================
action_result::action_result( side_number side )
: return_value_checked_(true),side_(side),status_(AI_ACTION_SUCCESS),is_execution_(false),is_gamestate_changed_(false)
{
}
action_result::~action_result()
{
if (!return_value_checked_) {
ERR_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl;
}
}
void action_result::check_after()
{
do_check_after();
}
void action_result::check_before()
{
do_check_before();
}
void action_result::execute()
{
is_execution_ = true;
init_for_execution();
check_before();
if (is_success()){
try {
do_execute();
} catch (end_level_exception&) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
if (is_success()){
check_after();
}
if(resources::controller->is_end_turn()) {
throw ai_end_turn_exception();
}
is_execution_ = false;
}
void action_result::init_for_execution()
{
return_value_checked_ = false;
is_gamestate_changed_ = false;
status_ = action_result::AI_ACTION_SUCCESS;
do_init_for_execution();
}
bool action_result::is_gamestate_changed() const
{
return is_gamestate_changed_;
}
bool action_result::is_ok()
{
return_value_checked_ = true;
return is_success();
}
void action_result::set_error(int error_code, bool log_as_error){
status_ = error_code;
if (is_execution()) {
if (log_as_error) {
ERR_AI_ACTIONS << "Error #"<<error_code<<" ("<<actions::get_error_name(error_code)<<") in "<< do_describe();
} else {
LOG_AI_ACTIONS << "Error #"<<error_code<<" ("<<actions::get_error_name(error_code)<<") in "<< do_describe();
}
} else {
LOG_AI_ACTIONS << "Error #"<<error_code<<" ("<<actions::get_error_name(error_code)<<") when checking "<< do_describe();
}
}
void action_result::set_gamestate_changed()
{
is_gamestate_changed_ = true;
}
int action_result::get_status() const
{
return status_;
}
bool action_result::is_success() const
{
return (status_ == action_result::AI_ACTION_SUCCESS);
}
bool action_result::is_execution() const
{
return is_execution_;
}
game_info& action_result::get_info() const
{
return manager::get_active_ai_info_for_side(get_side());
}
team& action_result::get_my_team() const
{
return (*resources::teams)[side_-1];
}
// attack_result
attack_result::attack_result( side_number side, const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect& advancements)
: action_result(side), attacker_loc_(attacker_loc), defender_loc_(defender_loc), attacker_weapon_(attacker_weapon), aggression_(aggression), advancements_(advancements){
}
void attack_result::do_check_before()
{
LOG_AI_ACTIONS << " check_before " << *this << std::endl;
const unit_map::const_iterator attacker = resources::units->find(attacker_loc_);
const unit_map::const_iterator defender = resources::units->find(defender_loc_);
if(attacker==resources::units->end())
{
LOG_AI_ACTIONS << "attempt to attack without attacker\n";
set_error(E_EMPTY_ATTACKER);
return;
}
if (defender==resources::units->end())
{
LOG_AI_ACTIONS << "attempt to attack without defender\n";
set_error(E_EMPTY_DEFENDER);
return;
}
if(attacker->incapacitated()) {
LOG_AI_ACTIONS << "attempt to attack with unit that is petrified\n";
set_error(E_INCAPACITATED_ATTACKER);
return;
}
if(defender->incapacitated()) {
LOG_AI_ACTIONS << "attempt to attack unit that is petrified\n";
set_error(E_INCAPACITATED_DEFENDER);
return;
}
if(!attacker->attacks_left()) {
LOG_AI_ACTIONS << "attempt to attack with no attacks left\n";
set_error(E_NO_ATTACKS_LEFT);
return;
}
if(attacker->side()!=get_side()) {
LOG_AI_ACTIONS << "attempt to attack with not own unit\n";
set_error(E_NOT_OWN_ATTACKER);
return;
}
if(!get_my_team().is_enemy(defender->side())) {
LOG_AI_ACTIONS << "attempt to attack unit that is not enemy\n";
set_error(E_NOT_ENEMY_DEFENDER);
return;
}
if (attacker_weapon_!=-1) {
if ((attacker_weapon_<0)||(attacker_weapon_ >= static_cast<int>(attacker->attacks().size()))) {
LOG_AI_ACTIONS << "invalid weapon selection for the attacker\n";
set_error(E_WRONG_ATTACKER_WEAPON);
return;
}
}
if (!tiles_adjacent(attacker_loc_,defender_loc_)) {
LOG_AI_ACTIONS << "attacker and defender not adjacent\n";
set_error(E_ATTACKER_AND_DEFENDER_NOT_ADJACENT);
return;
}
}
void attack_result::do_check_after()
{
}
std::string attack_result::do_describe() const
{
std::stringstream s;
s << "attack by side ";
s << get_side();
s << " from location "<<attacker_loc_;
s << " to location "<<defender_loc_;
s << " using weapon "<< attacker_weapon_;
s << " with aggression "<< aggression_;
s <<std::endl;
return s.str();
}
void attack_result::do_execute()
{
LOG_AI_ACTIONS << "start of execution of: "<< *this << std::endl;
// Stop the user from issuing any commands while the unit is attacking
const events::command_disabler disable_commands;
//@note: yes, this is a decision done here. It's that way because we want to allow a simpler attack 'with whatever weapon is considered best', and because we want to allow the defender to pick it's weapon. That's why aggression is needed. a cleaner solution is needed.
battle_context bc(*resources::units, attacker_loc_,
defender_loc_, attacker_weapon_, -1, aggression_);
int attacker_weapon = bc.get_attacker_stats().attack_num;
int defender_weapon = bc.get_defender_stats().attack_num;
if(attacker_weapon < 0) {
set_error(E_UNABLE_TO_CHOOSE_ATTACKER_WEAPON);
return;
}
const unit_map::const_iterator a_ = resources::units->find(attacker_loc_);
const unit_map::const_iterator d_ = resources::units->find(defender_loc_);
if(resources::simulation_){
bool gamestate_changed = simulated_attack(attacker_loc_, defender_loc_, bc.get_attacker_combatant().average_hp(), bc.get_defender_combatant().average_hp());
sim_gamestate_changed(this, gamestate_changed);
return;
}
//to get rid of an unused member variable warning, FIXME: find a way to 'ask' the ai which advancement should be chosen from synced_commands.cpp .
if(synced_context::get_synced_state() != synced_context::SYNCED) //RAII block for set_scontext_synced
{
//we don't use synced_context::run_in_synced_context because that wouldn't allow us to pass advancements_
recorder.add_synced_command("attack", replay_helper::get_attack(attacker_loc_, defender_loc_, attacker_weapon, defender_weapon, a_->type_id(),
d_->type_id(), a_->level(), d_->level(), resources::tod_manager->turn(),
resources::tod_manager->get_time_of_day()));
set_scontext_synced sync;
attack_unit_and_advance(attacker_loc_, defender_loc_, attacker_weapon, defender_weapon, true, advancements_);
resources::controller->check_victory();
sync.do_final_checkup();
}
else
{
attack_unit_and_advance(attacker_loc_, defender_loc_, attacker_weapon, defender_weapon, true, advancements_);
}
set_gamestate_changed();
//start of ugly hack. @todo 1.9 rework that via extended event system
//until event system is reworked, we note the attack this way
get_info().recent_attacks.insert(defender_loc_);
//end of ugly hack
try {
manager::raise_gamestate_changed();
} catch (...) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
void attack_result::do_init_for_execution()
{
}
// move_result
move_result::move_result(side_number side, const map_location& from,
const map_location& to, bool remove_movement, bool unreach_is_ok)
: action_result(side)
, from_(from)
, to_(to)
, remove_movement_(remove_movement)
, route_()
, unit_location_(from)
, unreach_is_ok_(unreach_is_ok)
, has_ambusher_(false)
, has_interrupted_teleport_(false)
{
}
const unit *move_result::get_unit()
{
unit_map::const_iterator un = resources::units->find(from_);
if (un==resources::units->end()){
set_error(E_NO_UNIT);
return NULL;
}
const unit *u = &*un;
if (u->side() != get_side()) {
set_error(E_NOT_OWN_UNIT);
return NULL;
}
if (u->incapacitated()) {
set_error(E_INCAPACITATED_UNIT);
return NULL;
}
return u;
}
bool move_result::test_route(const unit &un)
{
if (from_== to_) {
if (!remove_movement_ || (un.movement_left() == 0) ) {
set_error(E_EMPTY_MOVE);
return false;
}
return true;
}
if (un.movement_left() == 0 ) {
set_error(E_EMPTY_MOVE);
return false;
}
if (!to_.valid()) {
set_error(E_NO_ROUTE);
return false;
}
team &my_team = get_my_team();
const pathfind::shortest_path_calculator calc(un, my_team, *resources::teams, resources::gameboard->map());
//allowed teleports
pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(un, my_team, true);///@todo 1.9: see_all -> false
//do an A*-search
route_ = boost::shared_ptr<pathfind::plain_route>( new pathfind::plain_route(pathfind::a_star_search(un.get_location(), to_, 10000.0, &calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports)));
if (route_->steps.empty()) {
set_error(E_NO_ROUTE);
return false;
}
return true;
}
void move_result::do_check_before()
{
LOG_AI_ACTIONS << " check_before " << *this << std::endl;
const unit *u = get_unit();
if (!u) {
return;
}
if (!test_route(*u)) {
return;
}
}
const map_location& move_result::get_unit_location() const
{
return unit_location_;
}
void move_result::do_check_after()
{
if (has_ambusher_) {
set_error(E_AMBUSHED,false);
return;
}
if (has_interrupted_teleport_) {
set_error(E_FAILED_TELEPORT);
return;
}
///@todo 1.9 add 'new units spotted' failure mode
if (!unreach_is_ok_ && unit_location_!=to_) {
set_error(E_NOT_REACHED_DESTINATION);
return;
}
}
std::string move_result::do_describe() const
{
std::stringstream s;
if (remove_movement_){
s << "full move by side ";
} else {
s << "partial move by side ";
}
s << get_side();
s << " from location "<<from_;
s << " to location "<<to_;
s <<std::endl;
return s.str();
}
void move_result::do_execute()
{
LOG_AI_ACTIONS << "start of execution of: "<< *this << std::endl;
assert(is_success());
if(resources::simulation_){
bool gamestate_changed = false;
if(from_ != to_){
int step = route_->steps.size();
gamestate_changed = simulated_move(get_side(), from_, to_, step, unit_location_);
} else {
assert(remove_movement_);
}
unit_map::const_iterator un = resources::units->find(unit_location_);
if(remove_movement_ && un->movement_left() > 0 && unit_location_ == to_){
gamestate_changed = simulated_stopunit(unit_location_, true, false);
}
sim_gamestate_changed(this, gamestate_changed);
return;
}
::actions::move_unit_spectator move_spectator(*resources::units);
move_spectator.set_unit(resources::units->find(from_));
if (from_ != to_) {
size_t num_steps = ::actions::move_unit_and_record(
/*std::vector<map_location> steps*/ route_->steps,
/*::actions::undo_list* undo_stack*/ NULL,
/*bool continue_move*/ true, ///@todo 1.9 set to false after implemeting interrupt awareness
/*bool show_move*/ preferences::show_ai_moves(),
/*bool* interrupted*/ NULL,
/*::actions::move_unit_spectator* move_spectator*/ &move_spectator);
if ( num_steps > 0 ) {
set_gamestate_changed();
} else if ( move_spectator.get_ambusher().valid() ) {
// Unlikely, but some types of strange WML (or bad pathfinding)
// could cause an ambusher to be found without moving.
set_gamestate_changed();
}
} else {
assert(remove_movement_);
}
if (move_spectator.get_unit().valid()){
unit_location_ = move_spectator.get_unit()->get_location();
if (remove_movement_ && move_spectator.get_unit()->movement_left() > 0 && unit_location_ == to_)
{
stopunit_result_ptr stopunit_res = actions::execute_stopunit_action(get_side(),true,unit_location_,true,false);
if (!stopunit_res->is_ok()) {
set_error(stopunit_res->get_status());
}
if (stopunit_res->is_gamestate_changed()) {
set_gamestate_changed();
}
}
} else {
unit_location_ = map_location();
}
has_ambusher_ = move_spectator.get_ambusher().valid();
has_interrupted_teleport_ = move_spectator.get_failed_teleport().valid();
if (is_gamestate_changed()) {
try {
manager::raise_gamestate_changed();
} catch (...) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
}
void move_result::do_init_for_execution()
{
}
// recall_result
recall_result::recall_result(side_number side,
const std::string& unit_id, const map_location& where, const map_location& from)
: action_result(side)
, unit_id_(unit_id)
, where_(where)
, recall_location_(where)
, recall_from_(from)
, location_checked_(false)
{
}
unit_const_ptr recall_result::get_recall_unit(const team &my_team)
{
unit_const_ptr rec = my_team.recall_list().find_if_matches_id(unit_id_);
if (!rec) {
set_error(E_NOT_AVAILABLE_FOR_RECALLING);
}
return rec;
}
bool recall_result::test_enough_gold(const team &my_team)
{
if (my_team.gold() < my_team.recall_cost() ) {
set_error(E_NO_GOLD);
return false;
}
return true;
}
void recall_result::do_check_before()
{
LOG_AI_ACTIONS << " check_before " << *this << std::endl;
const team& my_team = get_my_team();
const bool location_specified = recall_location_.valid();
//Enough gold?
if (!test_enough_gold(my_team)) {
return;
}
//Unit available for recalling?
const unit_const_ptr & to_recall = get_recall_unit(my_team);
if ( !to_recall ) {
return;
}
// Leader available for recalling?
switch ( ::actions::check_recall_location(get_side(), recall_location_,
recall_from_, *to_recall) )
{
case ::actions::RECRUIT_NO_LEADER:
case ::actions::RECRUIT_NO_ABLE_LEADER:
set_error(E_NO_LEADER);
return;
case ::actions::RECRUIT_NO_KEEP_LEADER:
set_error(E_LEADER_NOT_ON_KEEP);
return;
case ::actions::RECRUIT_NO_VACANCY:
set_error(E_BAD_RECALL_LOCATION);
return;
case ::actions::RECRUIT_ALTERNATE_LOCATION:
if ( location_specified ) {
set_error(E_BAD_RECALL_LOCATION);
return;
}
// No break. If the location was not specified, this counts as "OK".
case ::actions::RECRUIT_OK:
location_checked_ = true;
}
}
void recall_result::do_check_after()
{
if (!resources::gameboard->map().on_board(recall_location_)){
set_error(AI_ACTION_FAILURE);
return;
}
unit_map::const_iterator unit = resources::units->find(recall_location_);
if (unit==resources::units->end()){
set_error(AI_ACTION_FAILURE);
return;
}
if (unit->side() != get_side()){
set_error(AI_ACTION_FAILURE);
return;
}
}
std::string recall_result::do_describe() const
{
std::stringstream s;
s << "recall by side ";
s << get_side();
s << " of unit id ["<<unit_id_;
if (where_ != map_location::null_location()){
s << "] on location "<<where_;
} else {
s << "] on any suitable location";
}
s <<std::endl;
return s.str();
}
void recall_result::do_execute()
{
LOG_AI_ACTIONS << "start of execution of: " << *this << std::endl;
assert(is_success());
const events::command_disabler disable_commands;
// Assert that recall_location_ has been validated.
// This should be implied by is_success() once check_before() has been
// called, so this is a guard against future breakage.
assert(location_checked_);
if(resources::simulation_){
bool gamestate_changed = simulated_recall(get_side(), unit_id_, recall_location_);
sim_gamestate_changed(this, gamestate_changed);
return;
}
// Do the actual recalling.
// We ignore possible errors (=unit doesn't exist on the recall list)
// because that was the previous behavior.
synced_context::run_in_synced_context_if_not_already("recall",
replay_helper::get_recall(unit_id_, recall_location_, recall_from_),
false,
preferences::show_ai_moves(),
synced_context::ignore_error_function);
set_gamestate_changed();
try {
manager::raise_gamestate_changed();
} catch (...) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
void recall_result::do_init_for_execution()
{
}
// recruit_result
recruit_result::recruit_result(side_number side,
const std::string& unit_name, const map_location& where, const map_location& from)
: action_result(side)
, unit_name_(unit_name)
, where_(where)
, recruit_location_(where)
, recruit_from_(from)
, location_checked_(false)
{
}
const unit_type *recruit_result::get_unit_type_known(const std::string &recruit)
{
const unit_type *type = unit_types.find(recruit);
if (!type) {
set_error(E_UNKNOWN_OR_DUMMY_UNIT_TYPE);
return NULL;
}
return type;
}
bool recruit_result::test_enough_gold(const team &my_team, const unit_type &type)
{
if (my_team.gold() < type.cost()) {
set_error(E_NO_GOLD);
return false;
}
return true;
}
void recruit_result::do_check_before()
{
LOG_AI_ACTIONS << " check_before " << *this << std::endl;
const team& my_team = get_my_team();
const bool location_specified = recruit_location_.valid();
//Unit type known ?
const unit_type *s_type = get_unit_type_known(unit_name_);
if (!s_type) {
return;
}
//Enough gold?
if (!test_enough_gold(my_team, *s_type)) {
return;
}
// Leader available for recruiting?
switch ( ::actions::check_recruit_location(get_side(), recruit_location_,
recruit_from_, unit_name_) )
{
case ::actions::RECRUIT_NO_LEADER:
case ::actions::RECRUIT_NO_ABLE_LEADER:
set_error(E_NO_LEADER);
return;
case ::actions::RECRUIT_NO_KEEP_LEADER:
set_error(E_LEADER_NOT_ON_KEEP);
return;
case ::actions::RECRUIT_NO_VACANCY:
set_error(E_BAD_RECRUIT_LOCATION);
return;
case ::actions::RECRUIT_ALTERNATE_LOCATION:
if ( location_specified ) {
set_error(E_BAD_RECRUIT_LOCATION);
return;
}
// No break. If the location was not specified, this counts as "OK".
case ::actions::RECRUIT_OK:
location_checked_ = true;
}
}
void recruit_result::do_check_after()
{
if (!resources::gameboard->map().on_board(recruit_location_)) {
set_error(AI_ACTION_FAILURE);
return;
}
unit_map::const_iterator unit = resources::units->find(recruit_location_);
if (unit==resources::units->end()) {
set_error(AI_ACTION_FAILURE);
return;
}
if (unit->side() != get_side()) {
set_error(AI_ACTION_FAILURE);
return;
}
}
std::string recruit_result::do_describe() const
{
std::stringstream s;
s << "recruitment by side ";
s << get_side();
s << " of unit type ["<<unit_name_;
if (where_ != map_location::null_location()){
s << "] on location "<<where_;
} else {
s << "] on any suitable location";
}
s <<std::endl;
return s.str();
}
void recruit_result::do_execute()
{
LOG_AI_ACTIONS << "start of execution of: " << *this << std::endl;
assert(is_success());
const unit_type *u = unit_types.find(unit_name_);
const events::command_disabler disable_commands;
// Assert that recruit_location_ has been validated.
// This should be implied by is_success() once check_before() has been
// called, so this is a guard against future breakage.
assert(location_checked_ && u != NULL);
if(resources::simulation_){
bool gamestate_changed = simulated_recruit(get_side(), u, recruit_location_);
sim_gamestate_changed(this, gamestate_changed);
return;
}
synced_context::run_in_synced_context_if_not_already("recruit", replay_helper::get_recruit(u->id(), recruit_location_, recruit_from_), false, preferences::show_ai_moves());
//TODO: should we do something to pass use_undo = false in replays and ai moves ?
//::actions::recruit_unit(*u, get_side(), recruit_location_, recruit_from_,
// preferences::show_ai_moves(), false);
set_gamestate_changed();
try {
manager::raise_gamestate_changed();
} catch (...) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
void recruit_result::do_init_for_execution()
{
}
// stopunit_result
stopunit_result::stopunit_result( side_number side, const map_location& unit_location, bool remove_movement, bool remove_attacks)
: action_result(side), unit_location_(unit_location), remove_movement_(remove_movement), remove_attacks_(remove_attacks)
{
}
const unit *stopunit_result::get_unit()
{
unit_map::const_iterator un = resources::units->find(unit_location_);
if (un==resources::units->end()){
set_error(E_NO_UNIT);
return NULL;
}
const unit *u = &*un;
if (u->side() != get_side()) {
set_error(E_NOT_OWN_UNIT);
return NULL;
}
if (u->incapacitated()) {
set_error(E_INCAPACITATED_UNIT);
return NULL;
}
return u;
}
void stopunit_result::do_check_before()
{
LOG_AI_ACTIONS << " check_before " << *this << std::endl;
if (!get_unit()) {
return;
}
}
void stopunit_result::do_check_after()
{
unit_map::const_iterator un = resources::units->find(unit_location_);
if (un==resources::units->end()){
set_error(AI_ACTION_FAILURE);
return;
}
if (remove_movement_ && un->movement_left() != 0) {
set_error(AI_ACTION_FAILURE);
return;
}
if (remove_attacks_ && un->attacks_left() != 0) {
set_error(AI_ACTION_FAILURE);
return;
}
}
std::string stopunit_result::do_describe() const
{
std::stringstream s;
s <<" stopunit by side ";
s << get_side();
if (remove_movement_){
s << " : remove movement ";
}
if (remove_movement_ && remove_attacks_){
s << "and ";
}
if (remove_attacks_){
s << " remove attacks ";
}
s << "from unit on location "<<unit_location_;
s <<std::endl;
return s.str();
}
void stopunit_result::do_execute()
{
LOG_AI_ACTIONS << "start of execution of: " << *this << std::endl;
assert(is_success());
unit_map::iterator un = resources::units->find(unit_location_);
if(resources::simulation_){
bool gamestate_changed = simulated_stopunit(unit_location_, remove_movement_, remove_attacks_);
sim_gamestate_changed(this, gamestate_changed);
return;
}
try {
if (remove_movement_){
un->remove_movement_ai();
set_gamestate_changed();
manager::raise_gamestate_changed();
}
if (remove_attacks_){
un->remove_attacks_ai();
set_gamestate_changed();
manager::raise_gamestate_changed();//to be on the safe side
}
} catch (...) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
void stopunit_result::do_init_for_execution()
{
}
// synced_command_result
synced_command_result::synced_command_result( side_number side, const std::string& lua_code, const map_location& location )
: action_result(side), lua_code_(lua_code), location_(location)
{
}
void synced_command_result::do_check_before()
{
LOG_AI_ACTIONS << " check_before " << *this << std::endl;
}
void synced_command_result::do_check_after()
{
}
std::string synced_command_result::do_describe() const
{
std::stringstream s;
s <<" synced_command by side ";
s << get_side();
s <<std::endl;
return s.str();
}
void synced_command_result::do_execute()
{
if(resources::simulation_){
bool gamestate_changed = simulated_synced_command();
sim_gamestate_changed(this, gamestate_changed);
return;
}
LOG_AI_ACTIONS << "start of execution of: " << *this << std::endl;
assert(is_success());
std::stringstream s;
if (location_ != map_location::null_location()){
s << "local x1 = " << location_.x << " local y1 = " << location_.y << " ";
}
s << lua_code_;
synced_context::run_in_synced_context_if_not_already("lua_ai", replay_helper::get_lua_ai(s.str()));
try {
set_gamestate_changed();
manager::raise_gamestate_changed();
} catch (...) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
}
void synced_command_result::do_init_for_execution()
{
}
// =======================================================================
// STATELESS INTERFACE TO AI ACTIONS
// =======================================================================
attack_result_ptr actions::execute_attack_action( side_number side,
bool execute,
const map_location& attacker_loc,
const map_location& defender_loc,
int attacker_weapon,
double aggression,
const unit_advancements_aspect& advancements)
{