-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
core.cpp
1174 lines (991 loc) · 34.7 KB
/
core.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) 2010 - 2017 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
* Provides core classes for the Lua AI.
*
*/
#include <cassert>
#include <cstring>
#include "ai/lua/core.hpp"
#include "ai/composite/aspect.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "scripting/lua_unit.hpp"
#include "scripting/push_check.hpp"
#include "ai/lua/lua_object.hpp" // (Nephro)
#include "attack_prediction.hpp"
#include "game_display.hpp"
#include "log.hpp"
#include "map/map.hpp"
#include "pathfind/pathfind.hpp"
#include "play_controller.hpp"
#include "resources.hpp"
#include "terrain/translation.hpp"
#include "terrain/filter.hpp"
#include "units/unit.hpp"
#include "ai/actions.hpp"
#include "ai/lua/engine_lua.hpp"
#include "ai/composite/contexts.hpp"
#include "ai/default/aspect_attacks.hpp"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include "lua/llimits.h"
static lg::log_domain log_ai_engine_lua("ai/engine/lua");
#define LOG_LUA LOG_STREAM(info, log_ai_engine_lua)
#define WRN_LUA LOG_STREAM(warn, log_ai_engine_lua)
#define ERR_LUA LOG_STREAM(err, log_ai_engine_lua)
static char const aisKey[] = "ai contexts";
namespace ai {
static void push_attack_analysis(lua_State *L, const attack_analysis&);
void lua_ai_context::init(lua_State *L)
{
// Create the ai elements table.
lua_newtable(L);
lua_setfield(L, LUA_REGISTRYINDEX, aisKey);
}
void lua_ai_context::get_arguments(config &cfg) const
{
int top = lua_gettop(L);
lua_getfield(L, LUA_REGISTRYINDEX, aisKey);
lua_rawgeti(L, -1, num_);
lua_getfield(L, -1, "params");
luaW_toconfig(L, -1, cfg);
lua_settop(L, top);
}
void lua_ai_context::set_arguments(const config &cfg)
{
int top = lua_gettop(L);
lua_getfield(L, LUA_REGISTRYINDEX, aisKey);
lua_rawgeti(L, -1, num_);
luaW_pushconfig(L, cfg);
lua_setfield(L, -2, "params");
lua_settop(L, top);
}
void lua_ai_context::get_persistent_data(config &cfg) const
{
int top = lua_gettop(L);
lua_getfield(L, LUA_REGISTRYINDEX, aisKey);
lua_rawgeti(L, -1, num_);
lua_getfield(L, -1, "data");
luaW_toconfig(L, -1, cfg);
lua_settop(L, top);
}
void lua_ai_context::set_persistent_data(const config &cfg)
{
int top = lua_gettop(L);
lua_getfield(L, LUA_REGISTRYINDEX, aisKey);
lua_rawgeti(L, -1, num_);
luaW_pushconfig(L, cfg);
lua_setfield(L, -2, "data");
lua_settop(L, top);
}
static ai::engine_lua &get_engine(lua_State *L)
{
return *(static_cast<ai::engine_lua*>(
lua_touserdata(L, lua_upvalueindex(1))));
}
static ai::readonly_context &get_readonly_context(lua_State *L)
{
return get_engine(L).get_readonly_context();
}
void lua_ai_context::push_ai_table()
{
lua_ai_load ctx(*this, false);
}
static int transform_ai_action(lua_State *L, ai::action_result_ptr action_result)
{
lua_newtable(L);
lua_pushboolean(L,action_result->is_ok());
lua_setfield(L, -2, "ok");
lua_pushboolean(L,action_result->is_gamestate_changed());
lua_setfield(L, -2, "gamestate_changed");
lua_pushinteger(L,action_result->get_status());
lua_setfield(L, -2, "status");
lua_pushstring(L, actions::get_error_name(action_result->get_status()).c_str());
lua_setfield(L, -2, "result");
return 1;
}
static int cfun_ai_get_suitable_keep(lua_State *L)
{
int index = 1;
ai::readonly_context &context = get_readonly_context(L);
unit* leader = nullptr;
if (lua_isuserdata(L, index))
{
leader = luaW_tounit(L, index);
if (!leader) return luaL_argerror(L, 1, "unknown unit");
}
else return luaW_type_error(L, 1, "unit");
const map_location loc = leader->get_location();
const pathfind::paths leader_paths(*leader, false, true, context.current_team());
const map_location &res = context.suitable_keep(loc,leader_paths);
if (!res.valid()) {
return 0;
}
else {
lua_pushnumber(L, res.wml_x());
lua_pushnumber(L, res.wml_y());
return 2;
}
}
static int ai_move(lua_State *L, bool exec, bool remove_movement)
{
int side = get_readonly_context(L).get_side();
map_location from = luaW_checklocation(L, 1);
map_location to = luaW_checklocation(L, 2);
bool unreach_is_ok = false;
if (lua_isboolean(L, 3)) {
unreach_is_ok = luaW_toboolean(L, 3);
}
ai::move_result_ptr move_result = ai::actions::execute_move_action(side,exec,from,to,remove_movement, unreach_is_ok);
return transform_ai_action(L,move_result);
}
static int cfun_ai_execute_move_full(lua_State *L)
{
return ai_move(L, true, true);
}
static int cfun_ai_execute_move_partial(lua_State *L)
{
return ai_move(L, true, false);
}
static int cfun_ai_check_move(lua_State *L)
{
return ai_move(L, false, false);
}
static int ai_attack(lua_State *L, bool exec)
{
ai::readonly_context &context = get_readonly_context(L);
int side = context.get_side();
map_location attacker = luaW_checklocation(L, 1);
map_location defender = luaW_checklocation(L, 2);
int attacker_weapon = -1;//-1 means 'select what is best'
double aggression = context.get_aggression();//use the aggression from the context
if (!lua_isnoneornil(L, 3)) {
attacker_weapon = lua_tointeger(L, 3);
if (attacker_weapon != -1) {
attacker_weapon--; // Done for consistency of the Lua style
}
}
//TODO: Right now, aggression is used by the attack execution functions to determine the weapon to be used.
// If a decision is made to expand the function that determines the weapon, this block must be refactored
// to parse aggression if a single int is on the stack, or create a table of parameters, if a table is on the
// stack.
if (!lua_isnoneornil(L, 4) && lua_isnumber(L,4)) {
aggression = lua_tonumber(L, 4);
}
unit_advancements_aspect advancements = context.get_advancements();
ai::attack_result_ptr attack_result = ai::actions::execute_attack_action(side,exec,attacker,defender,attacker_weapon,aggression,advancements);
return transform_ai_action(L,attack_result);
}
static int cfun_ai_execute_attack(lua_State *L)
{
return ai_attack(L, true);
}
static int cfun_ai_check_attack(lua_State *L)
{
return ai_attack(L, false);
}
static int ai_stopunit_select(lua_State *L, bool exec, bool remove_movement, bool remove_attacks)
{
int side = get_readonly_context(L).get_side();
map_location loc = luaW_checklocation(L, 1);
ai::stopunit_result_ptr stopunit_result = ai::actions::execute_stopunit_action(side,exec,loc,remove_movement,remove_attacks);
return transform_ai_action(L,stopunit_result);
}
static int cfun_ai_execute_stopunit_moves(lua_State *L)
{
return ai_stopunit_select(L, true, true, false);
}
static int cfun_ai_execute_stopunit_attacks(lua_State *L)
{
return ai_stopunit_select(L, true, false, true);
}
static int cfun_ai_execute_stopunit_all(lua_State *L)
{
return ai_stopunit_select(L, true, true, true);
}
static int cfun_ai_check_stopunit(lua_State *L)
{
return ai_stopunit_select(L, false, true, true);
}
static int ai_synced_command(lua_State *L, bool exec)
{
const char *lua_code = luaL_checkstring(L, 1);
int side = get_readonly_context(L).get_side();
map_location location;
if (!lua_isnoneornil(L, 2)) {
location.set_wml_x(lua_tonumber(L, 2));
location.set_wml_y(lua_tonumber(L, 3));
}
ai::synced_command_result_ptr synced_command_result = ai::actions::execute_synced_command_action(side,exec,std::string(lua_code),location);
return transform_ai_action(L,synced_command_result);
}
static int cfun_ai_execute_synced_command(lua_State *L)
{
return ai_synced_command(L, true);
}
static int cfun_ai_check_synced_command(lua_State *L)
{
return ai_synced_command(L, false);
}
static int ai_recruit(lua_State *L, bool exec)
{
const char *unit_name = luaL_checkstring(L, 1);
int side = get_readonly_context(L).get_side();
map_location where;
if (!lua_isnoneornil(L, 2)) {
where.set_wml_x(lua_tonumber(L, 2));
where.set_wml_y(lua_tonumber(L, 3));
}
//TODO fendrin: talk to Crab about the from argument.
map_location from = map_location::null_location();
ai::recruit_result_ptr recruit_result = ai::actions::execute_recruit_action(side,exec,std::string(unit_name),where,from);
return transform_ai_action(L,recruit_result);
}
static int cfun_ai_execute_recruit(lua_State *L)
{
return ai_recruit(L, true);
}
static int cfun_ai_check_recruit(lua_State *L)
{
return ai_recruit(L, false);
}
static int ai_recall(lua_State *L, bool exec)
{
const char *unit_id = luaL_checkstring(L, 1);
int side = get_readonly_context(L).get_side();
map_location where;
if (!lua_isnoneornil(L, 2)) {
where.set_wml_x(lua_tonumber(L, 2));
where.set_wml_y(lua_tonumber(L, 3));
}
//TODO fendrin: talk to Crab about the from argument.
map_location from = map_location::null_location();
ai::recall_result_ptr recall_result = ai::actions::execute_recall_action(side,exec,std::string(unit_id),where,from);
return transform_ai_action(L,recall_result);
}
static int cfun_ai_execute_recall(lua_State *L)
{
return ai_recall(L, true);
}
static int cfun_ai_check_recall(lua_State *L)
{
return ai_recall(L, false);
}
static int cfun_ai_fallback_human(lua_State* L)
{
throw fallback_ai_to_human_exception();
}
// Goals and targets
static int cfun_ai_get_targets(lua_State *L)
{
move_map enemy_dst_src = get_readonly_context(L).get_enemy_dstsrc();
std::vector<target> targets = get_engine(L).get_ai_context()->find_targets(enemy_dst_src);
int i = 1;
lua_createtable(L, 0, 0);
for (std::vector<target>::iterator it = targets.begin(); it != targets.end(); ++it)
{
lua_pushinteger(L, i);
//to factor out
lua_createtable(L, 3, 0);
lua_pushstring(L, "type");
lua_pushstring(L, it->type.to_string().c_str());
lua_rawset(L, -3);
lua_pushstring(L, "loc");
luaW_pushlocation(L, it->loc);
lua_rawset(L, -3);
lua_pushstring(L, "value");
lua_pushnumber(L, it->value);
lua_rawset(L, -3);
lua_rawset(L, -3);
++i;
}
return 1;
}
#define DEPRECATED_ASPECT_MESSAGE(name) WRN_LUA << "ai.get_" name "() is deprecated, use ai.aspects." name " instead\n"
// Aspect section
static int cfun_ai_get_aggression(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("aggression");
double aggression = get_readonly_context(L).get_aggression();
lua_pushnumber(L, aggression);
return 1;
}
static int cfun_ai_get_attack_depth(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("attack_depth");
int attack_depth = get_readonly_context(L).get_attack_depth();
lua_pushnumber(L, attack_depth);
return 1;
}
static int cfun_ai_get_attacks(lua_State *L)
{
// Unlike the other aspect fetchers, this one is not deprecated, because ai.aspects.attacks returns the viable units but this returns a full attack analysis
const ai::attacks_vector& attacks = get_readonly_context(L).get_attacks();
lua_createtable(L, attacks.size(), 0);
int table_index = lua_gettop(L);
ai::attacks_vector::const_iterator it = attacks.begin();
for (int i = 1; it != attacks.end(); ++it, ++i)
{
push_attack_analysis(L, *it);
lua_rawseti(L, table_index, i);
}
return 1;
}
static int cfun_ai_get_avoid(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("avoid");
std::set<map_location> locs;
terrain_filter avoid = get_readonly_context(L).get_avoid();
avoid.get_locations(locs);
lua_push(L, locs);
return 1;
}
static int cfun_ai_get_caution(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("caution");
double caution = get_readonly_context(L).get_caution();
lua_pushnumber(L, caution);
return 1;
}
static int cfun_ai_get_grouping(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("grouping");
std::string grouping = get_readonly_context(L).get_grouping();
lua_pushstring(L, grouping.c_str());
return 1;
}
static int cfun_ai_get_leader_aggression(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("leader_aggression");
double leader_aggression = get_readonly_context(L).get_leader_aggression();
lua_pushnumber(L, leader_aggression);
return 1;
}
static int cfun_ai_get_leader_goal(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("leader_goal");
config goal = get_readonly_context(L).get_leader_goal();
luaW_pushconfig(L, goal);
return 1;
}
static int cfun_ai_get_leader_ignores_keep(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("leader_ignores_keep");
bool leader_ignores_keep = get_readonly_context(L).get_leader_ignores_keep();
lua_pushboolean(L, leader_ignores_keep);
return 1;
}
static int cfun_ai_get_leader_value(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("leader_value");
double leader_value = get_readonly_context(L).get_leader_value();
lua_pushnumber(L, leader_value);
return 1;
}
static int cfun_ai_get_passive_leader(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("passive_leader");
bool passive_leader = get_readonly_context(L).get_passive_leader();
lua_pushboolean(L, passive_leader);
return 1;
}
static int cfun_ai_get_passive_leader_shares_keep(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("passive_leader_shares_keep");
bool passive_leader_shares_keep = get_readonly_context(L).get_passive_leader_shares_keep();
lua_pushboolean(L, passive_leader_shares_keep);
return 1;
}
static int cfun_ai_get_recruitment_pattern(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("recruitment_pattern");
std::vector<std::string> recruiting = get_readonly_context(L).get_recruitment_pattern();
int size = recruiting.size();
lua_createtable(L, size, 0); // create an empty table with predefined size
for (int i = 0; i < size; ++i)
{
lua_pushinteger(L, i + 1); // Indexing in Lua starts from 1
lua_pushstring(L, recruiting[i].c_str());
lua_settable(L, -3);
}
return 1;
}
static int cfun_ai_get_scout_village_targeting(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("scout_village_targeting");
double scout_village_targeting = get_readonly_context(L).get_scout_village_targeting();
lua_pushnumber(L, scout_village_targeting);
return 1;
}
static int cfun_ai_get_simple_targeting(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("simple_targeting");
bool simple_targeting = get_readonly_context(L).get_simple_targeting();
lua_pushboolean(L, simple_targeting);
return 1;
}
static int cfun_ai_get_support_villages(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("support_villages");
bool support_villages = get_readonly_context(L).get_support_villages();
lua_pushboolean(L, support_villages);
return 1;
}
static int cfun_ai_get_village_value(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("village_value");
double village_value = get_readonly_context(L).get_village_value();
lua_pushnumber(L, village_value);
return 1;
}
static int cfun_ai_get_villages_per_scout(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("villages_per_scout");
int villages_per_scout = get_readonly_context(L).get_villages_per_scout();
lua_pushnumber(L, villages_per_scout);
return 1;
}
// End of aspect section
static int cfun_attack_rating(lua_State *L)
{
int top = lua_gettop(L);
// the attack_analysis table should be on top of the stack
lua_getfield(L, -1, "att_ptr"); // [-2: attack_analysis; -1: pointer to attack_analysis object in c++]
// now the pointer to our attack_analysis C++ object is on top
const attack_analysis* aa_ptr = static_cast< attack_analysis * >(lua_touserdata(L, -1));
//[-2: attack_analysis; -1: pointer to attack_analysis object in c++]
double aggression = get_readonly_context(L).get_aggression();
double rating = aa_ptr->rating(aggression, get_readonly_context(L));
lua_settop(L, top);
lua_pushnumber(L, rating);
return 1;
}
static void push_movements(lua_State *L, const std::vector< std::pair < map_location, map_location > > & moves)
{
lua_createtable(L, moves.size(), 0);
int table_index = lua_gettop(L);
std::vector< std::pair < map_location, map_location > >::const_iterator move = moves.begin();
for (int i = 1; move != moves.end(); ++move, ++i)
{
lua_createtable(L, 2, 0); // Creating a table for a pair of map_location's
lua_pushstring(L, "src");
luaW_pushlocation(L, move->first);
lua_rawset(L, -3);
lua_pushstring(L, "dst");
luaW_pushlocation(L, move->second);
lua_rawset(L, -3);
lua_rawseti(L, table_index, i); // setting the pair as an element of the movements table
}
}
static void push_attack_analysis(lua_State *L, const attack_analysis& aa)
{
lua_newtable(L);
// Pushing a pointer to the current object
lua_pushstring(L, "att_ptr");
lua_pushlightuserdata(L, const_cast<attack_analysis*>(&aa));
lua_rawset(L, -3);
// Registering callback function for the rating method
lua_pushstring(L, "rating");
lua_pushlightuserdata(L, &get_engine(L));
lua_pushcclosure(L, &cfun_attack_rating, 1);
lua_rawset(L, -3);
lua_pushstring(L, "movements");
push_movements(L, aa.movements);
lua_rawset(L, -3);
lua_pushstring(L, "target");
luaW_pushlocation(L, aa.target);
lua_rawset(L, -3);
lua_pushstring(L, "target_value");
lua_pushnumber(L, aa.target_value);
lua_rawset(L, -3);
lua_pushstring(L, "avg_losses");
lua_pushnumber(L, aa.avg_losses);
lua_rawset(L, -3);
lua_pushstring(L, "chance_to_kill");
lua_pushnumber(L, aa.chance_to_kill);
lua_rawset(L, -3);
lua_pushstring(L, "avg_damage_inflicted");
lua_pushnumber(L, aa.avg_damage_inflicted);
lua_rawset(L, -3);
lua_pushstring(L, "target_starting_damage");
lua_pushinteger(L, aa.target_starting_damage);
lua_rawset(L, -3);
lua_pushstring(L, "avg_damage_taken");
lua_pushnumber(L, aa.avg_damage_taken);
lua_rawset(L, -3);
lua_pushstring(L, "resources_used");
lua_pushnumber(L, aa.resources_used);
lua_rawset(L, -3);
lua_pushstring(L, "terrain_quality");
lua_pushnumber(L, aa.alternative_terrain_quality);
lua_rawset(L, -3);
lua_pushstring(L, "alternative_terrain_quality");
lua_pushnumber(L, aa.alternative_terrain_quality);
lua_rawset(L, -3);
lua_pushstring(L, "vulnerability");
lua_pushnumber(L, aa.vulnerability);
lua_rawset(L, -3);
lua_pushstring(L, "support");
lua_pushnumber(L, aa.support);
lua_rawset(L, -3);
lua_pushstring(L, "leader_threat");
lua_pushboolean(L, aa.leader_threat);
lua_rawset(L, -3);
lua_pushstring(L, "uses_leader");
lua_pushboolean(L, aa.uses_leader);
lua_rawset(L, -3);
lua_pushstring(L, "is_surrounded");
lua_pushboolean(L, aa.is_surrounded);
lua_rawset(L, -3);
}
static void push_move_map(lua_State *L, const move_map& m)
{
lua_createtable(L, 0, 0); // the main table
if (m.empty())
{
return;
}
move_map::const_iterator it = m.begin();
int index = 1;
std::hash<map_location> lhash;
do
{
map_location key = it->first;
lua_pushinteger(L, lhash(key));
lua_createtable(L, 0, 0);
while (key == it->first) {
luaW_pushlocation(L, it->second);
lua_rawseti(L, -2, index);
++index;
++it;
}
lua_settable(L, -3);
index = 1;
} while (it != m.end());
}
static int cfun_ai_get_dstsrc(lua_State *L)
{
move_map dst_src = get_readonly_context(L).get_dstsrc();
get_readonly_context(L).set_dst_src_valid_lua();
push_move_map(L, dst_src);
return 1;
}
static int cfun_ai_get_srcdst(lua_State *L)
{
move_map src_dst = get_readonly_context(L).get_srcdst();
get_readonly_context(L).set_src_dst_valid_lua();
push_move_map(L, src_dst);
return 1;
}
static int cfun_ai_get_enemy_dstsrc(lua_State *L)
{
move_map enemy_dst_src = get_readonly_context(L).get_enemy_dstsrc();
get_readonly_context(L).set_dst_src_enemy_valid_lua();
push_move_map(L, enemy_dst_src);
return 1;
}
static int cfun_ai_get_enemy_srcdst(lua_State *L)
{
move_map enemy_src_dst = get_readonly_context(L).get_enemy_srcdst();
get_readonly_context(L).set_src_dst_enemy_valid_lua();
push_move_map(L, enemy_src_dst);
return 1;
}
static int cfun_ai_is_dst_src_valid(lua_State *L)
{
bool valid = get_readonly_context(L).is_dst_src_valid_lua();
lua_pushboolean(L, valid);
return 1;
}
static int cfun_ai_is_dst_src_enemy_valid(lua_State *L)
{
bool valid = get_readonly_context(L).is_dst_src_enemy_valid_lua();
lua_pushboolean(L, valid);
return 1;
}
static int cfun_ai_is_src_dst_valid(lua_State *L)
{
bool valid = get_readonly_context(L).is_src_dst_valid_lua();
lua_pushboolean(L, valid);
return 1;
}
static int cfun_ai_is_src_dst_enemy_valid(lua_State *L)
{
bool valid = get_readonly_context(L).is_src_dst_enemy_valid_lua();
lua_pushboolean(L, valid);
return 1;
}
static int cfun_ai_recalculate_move_maps(lua_State *L)
{
get_readonly_context(L).recalculate_move_maps();
return 1;
}
static int cfun_ai_recalculate_move_maps_enemy(lua_State *L)
{
get_readonly_context(L).recalculate_move_maps_enemy();
return 1;
}
template<typename T>
typesafe_aspect<T>* try_aspect_as(aspect_ptr p)
{
return std::dynamic_pointer_cast<typesafe_aspect<T> >(p).get();
}
static int impl_ai_aspect_get(lua_State* L)
{
const aspect_map& aspects = get_engine(L).get_readonly_context().get_aspects();
aspect_map::const_iterator iter = aspects.find(luaL_checkstring(L, 2));
if(iter == aspects.end()) {
return 0;
}
typedef std::vector<std::string> string_list;
if(typesafe_aspect<bool>* aspect_as_bool = try_aspect_as<bool>(iter->second)) {
lua_pushboolean(L, aspect_as_bool->get());
} else if(typesafe_aspect<int>* aspect_as_int = try_aspect_as<int>(iter->second)) {
lua_pushinteger(L, aspect_as_int->get());
} else if(typesafe_aspect<double>* aspect_as_double = try_aspect_as<double>(iter->second)) {
lua_pushnumber(L, aspect_as_double->get());
} else if(typesafe_aspect<config>* aspect_as_config = try_aspect_as<config>(iter->second)) {
luaW_pushconfig(L, aspect_as_config->get());
} else if(typesafe_aspect<string_list>* aspect_as_string_list = try_aspect_as<string_list>(iter->second)) {
lua_push(L, aspect_as_string_list->get());
} else if(typesafe_aspect<terrain_filter>* aspect_as_terrain_filter = try_aspect_as<terrain_filter>(iter->second)) {
std::set<map_location> result;
aspect_as_terrain_filter->get().get_locations(result);
lua_push(L, result);
} else if(typesafe_aspect<attacks_vector>* aspect_as_attacks_vector = try_aspect_as<attacks_vector>(iter->second)) {
using ai_default_rca::aspect_attacks_base;
aspect_attacks_base* real_aspect = dynamic_cast<aspect_attacks_base*>(aspect_as_attacks_vector);
while(real_aspect == nullptr) {
// It's probably a composite aspect, so find the active facet
composite_aspect<attacks_vector>& composite = dynamic_cast<composite_aspect<attacks_vector>&>(*aspect_as_attacks_vector);
aspect_as_attacks_vector = &dynamic_cast<typesafe_aspect<attacks_vector>&>(composite.find_active());
real_aspect = dynamic_cast<aspect_attacks_base*>(aspect_as_attacks_vector);
}
int my_side = get_engine(L).get_readonly_context().get_side();
std::vector<unit_const_ptr> attackers, enemies;
for(unit_map::const_iterator u = resources::gameboard->units().begin(); u != resources::gameboard->units().end(); ++u) {
if(!u.valid()) {
continue;
}
if(u->side() == my_side && real_aspect->is_allowed_attacker(*u)) {
attackers.push_back(u.get_shared_ptr());
} else if(u->side() != my_side && real_aspect->is_allowed_enemy(*u)) {
enemies.push_back(u.get_shared_ptr());
}
}
lua_createtable(L, 0, 2);
lua_createtable(L, attackers.size(), 0);
for(size_t i = 0; i < attackers.size(); i++) {
luaW_pushunit(L, attackers[i]->underlying_id());
lua_rawseti(L, -2, i + 1);
}
lua_setfield(L, -2, "own");
lua_createtable(L, enemies.size(), 0);
for(size_t i = 0; i < enemies.size(); i++) {
luaW_pushunit(L, enemies[i]->underlying_id());
lua_rawseti(L, -2, i + 1);
}
lua_setfield(L, -2, "enemy");
return 1;
} else if(typesafe_aspect<unit_advancements_aspect>* aspect_as_unit_advancements_aspects = try_aspect_as<unit_advancements_aspect>(iter->second)) {
const unit_advancements_aspect& val = aspect_as_unit_advancements_aspects->get();
int my_side = get_engine(L).get_readonly_context().get_side();
lua_newtable(L);
std::hash<map_location> lhash;
for (unit_map::const_iterator u = resources::gameboard->units().begin(); u != resources::gameboard->units().end(); ++u) {
if (!u.valid() || u->side() != my_side) {
continue;
}
lua_pushinteger(L, lhash(u->get_location()));
lua_push(L, val.get_advancements(u));
lua_settable(L, -3);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int impl_ai_aspect_set(lua_State* L)
{
lua_pushstring(L, "attempted to write to the ai.aspects table, which is read-only");
return lua_error(L);
}
static int impl_ai_get(lua_State* L)
{
if(!lua_isstring(L,2)) {
return 0;
}
ai::engine_lua& engine = get_engine(L);
std::string m = lua_tostring(L,2);
if(m == "side") {
lua_pushinteger(L, engine.get_readonly_context().get_side());
return 1;
}
if(m == "aspects") {
lua_newtable(L); // [-1: Aspects table]
lua_newtable(L); // [-1: Aspects metatable -2: Aspects table]
lua_pushlightuserdata(L, &engine); // [-1: Engine -2: Aspects mt -3: Aspects table]
lua_pushcclosure(L, &impl_ai_aspect_get, 1); // [-1: Metafunction -2: Aspects mt -3: Aspects table]
lua_setfield(L, -2, "__index"); // [-1: Aspects metatable -2: Aspects table]
lua_pushcfunction(L, &impl_ai_aspect_set); // [-1: Metafunction -2: Aspects mt -3: Aspects table]
lua_setfield(L, -2, "__newindex"); // [-1: Aspects metatable -2: Aspects table]
lua_setmetatable(L, -2); // [-1: Aspects table]
return 1;
}
static luaL_Reg const callbacks[] = {
// Move maps
{ "get_new_dst_src", &cfun_ai_get_dstsrc },
{ "get_new_src_dst", &cfun_ai_get_srcdst },
{ "get_new_enemy_dst_src", &cfun_ai_get_enemy_dstsrc },
{ "get_new_enemy_src_dst", &cfun_ai_get_enemy_srcdst },
{ "recalculate_move_maps", &cfun_ai_recalculate_move_maps },
{ "recalculate_enemy_move_maps", &cfun_ai_recalculate_move_maps_enemy },
// End of move maps
// Goals and targets
{ "get_targets", &cfun_ai_get_targets },
// End of G & T
// Aspects
{ "get_aggression", &cfun_ai_get_aggression },
{ "get_avoid", &cfun_ai_get_avoid },
{ "get_attack_depth", &cfun_ai_get_attack_depth },
{ "get_attacks", &cfun_ai_get_attacks },
{ "get_caution", &cfun_ai_get_caution },
{ "get_grouping", &cfun_ai_get_grouping },
{ "get_leader_aggression", &cfun_ai_get_leader_aggression },
{ "get_leader_goal", &cfun_ai_get_leader_goal },
{ "get_leader_ignores_keep", &cfun_ai_get_leader_ignores_keep },
{ "get_leader_value", &cfun_ai_get_leader_value },
{ "get_passive_leader", &cfun_ai_get_passive_leader },
{ "get_passive_leader_shares_keep", &cfun_ai_get_passive_leader_shares_keep },
{ "get_recruitment_pattern", &cfun_ai_get_recruitment_pattern },
{ "get_scout_village_targeting", &cfun_ai_get_scout_village_targeting },
{ "get_simple_targeting", &cfun_ai_get_simple_targeting },
{ "get_support_villages", &cfun_ai_get_support_villages },
{ "get_village_value", &cfun_ai_get_village_value },
{ "get_villages_per_scout", &cfun_ai_get_villages_per_scout },
// End of aspects
// Validation/cache functions
{ "is_dst_src_valid", &cfun_ai_is_dst_src_valid },
{ "is_enemy_dst_src_valid", &cfun_ai_is_dst_src_enemy_valid },
{ "is_src_dst_valid", &cfun_ai_is_src_dst_valid },
{ "is_enemy_src_dst_valid", &cfun_ai_is_src_dst_enemy_valid },
// End of validation functions
{ "suitable_keep", &cfun_ai_get_suitable_keep },
{ "check_recall", &cfun_ai_check_recall },
{ "check_move", &cfun_ai_check_move },
{ "check_stopunit", &cfun_ai_check_stopunit },
{ "check_synced_command", &cfun_ai_check_synced_command },
{ "check_attack", &cfun_ai_check_attack },
{ "check_recruit", &cfun_ai_check_recruit },
//{ "",},
//{ "",},
{ nullptr, nullptr } };
for (const luaL_Reg* p = callbacks; p->name; ++p) {
if(m == p->name) {
lua_pushlightuserdata(L, &engine); // [-1: engine ...]
lua_pushcclosure(L, p->func, 1); // [-1: function ...]
// Store the function so that __index doesn't need to be called next time
lua_pushstring(L, p->name); // [-1: name -2: function ...]
lua_pushvalue(L, -2); // [-1: function -2: name -3: function ...]
lua_rawset(L, 1); // [-1: function ...]
return 1;
}
}
lua_pushstring(L, "read_only");
lua_rawget(L, 1);
bool read_only = luaW_toboolean(L, -1);
lua_pop(L, 1);
if(read_only) {
return 0;
}
static luaL_Reg const mutating_callbacks[] = {
{ "attack", &cfun_ai_execute_attack },
{ "move", &cfun_ai_execute_move_partial },
{ "move_full", &cfun_ai_execute_move_full },
{ "recall", &cfun_ai_execute_recall },
{ "recruit", &cfun_ai_execute_recruit },
{ "stopunit_all", &cfun_ai_execute_stopunit_all },
{ "stopunit_attacks", &cfun_ai_execute_stopunit_attacks },
{ "stopunit_moves", &cfun_ai_execute_stopunit_moves },
{ "synced_command", &cfun_ai_execute_synced_command },
{ "fallback_human", &cfun_ai_fallback_human},
{ nullptr, nullptr } };
for (const luaL_Reg* p = mutating_callbacks; p->name; ++p) {
if(m == p->name) {
lua_pushlightuserdata(L, &engine);
lua_pushcclosure(L, p->func, 1);
return 1;
}
}
return 0;
}
static void generate_and_push_ai_table(lua_State* L, ai::engine_lua* engine) {
//push data table here
lua_newtable(L); // [-1: ai table]
lua_newtable(L); // [-1: metatable -2: ai table]
lua_pushlightuserdata(L, engine); // [-1: engine -2: metatable -3: ai table]
lua_pushcclosure(L, &impl_ai_get, 1); // [-1: metafunc -2: metatable -3: ai table]
lua_setfield(L, -2, "__index"); // [-1: metatable -2: ai table]
lua_setmetatable(L, -2); // [-1: ai table]
}