-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
game_lua_kernel.cpp
4830 lines (4280 loc) · 140 KB
/
game_lua_kernel.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 - 2016 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
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 a Lua interpreter, to be embedded in WML.
*
* @note Naming conventions:
* - intf_ functions are exported in the wesnoth domain,
* - impl_ functions are hidden inside metatables,
* - cfun_ functions are closures,
* - luaW_ functions are helpers in Lua style.
*/
#include "scripting/game_lua_kernel.hpp"
#include "global.hpp"
#include "actions/attack.hpp" // for battle_context_unit_stats, etc
#include "actions/vision.hpp" // for clear_shroud
#include "ai/composite/ai.hpp" // for ai_composite
#include "ai/composite/component.hpp" // for component, etc
#include "ai/composite/contexts.hpp" // for ai_context
#include "ai/composite/engine_lua.hpp" // for engine_lua
#include "ai/composite/rca.hpp" // for candidate_action
#include "ai/composite/stage.hpp" // for stage
#include "ai/configuration.hpp" // for configuration
#include "ai/lua/core.hpp" // for lua_ai_context, etc
#include "ai/manager.hpp" // for manager, holder
#include "attack_prediction.hpp" // for combatant
#include "chat_events.hpp" // for chat_handler, etc
#include "config.hpp" // for config, etc
#include "display_chat_manager.hpp" // for clear_chat_messages
#include "utils/foreach.tpp"
#include "formatter.hpp"
#include "game_board.hpp" // for game_board
#include "game_classification.hpp" // for game_classification, etc
#include "game_config.hpp" // for debug, base_income, etc
#include "game_config_manager.hpp" // for game_config_manager
#include "game_data.hpp" // for game_data, etc
#include "game_display.hpp" // for game_display
#include "game_errors.hpp" // for game_error
#include "game_events/conditional_wml.hpp" // for conditional_passed
#include "game_events/entity_location.hpp"
#include "game_events/manager.hpp" // for add_event_handler
#include "game_events/pump.hpp" // for queued_event
#include "game_preferences.hpp" // for encountered_units
#include "help/help.hpp"
#include "image.hpp" // for get_image, locator
#include "log.hpp" // for LOG_STREAM, logger, etc
#include "make_enum.hpp" // for operator<<
#include "map.hpp" // for gamemap
#include "map_label.hpp"
#include "map_location.hpp" // for map_location
#include "mouse_events.hpp" // for mouse_handler
#include "mp_game_settings.hpp" // for mp_game_settings
#include "pathfind/pathfind.hpp" // for full_cost_map, plain_route, etc
#include "pathfind/teleport.hpp" // for get_teleport_locations, etc
#include "play_controller.hpp" // for play_controller
#include "recall_list_manager.hpp" // for recall_list_manager
#include "replay.hpp" // for get_user_choice, etc
#include "reports.hpp" // for register_generator, etc
#include "resources.hpp"
#include "scripting/lua_api.hpp" // for luaW_toboolean, etc
#include "scripting/lua_common.hpp"
#include "scripting/lua_cpp_function.hpp"
#include "scripting/lua_gui2.hpp" // for show_gamestate_inspector
#include "scripting/lua_pathfind_cost_calculator.hpp"
#include "scripting/lua_race.hpp"
#include "scripting/lua_team.hpp"
#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc
#include "scripting/lua_unit_type.hpp"
#include "scripting/push_check.hpp"
#include "sdl/utils.hpp" // for surface
#include "side_filter.hpp" // for side_filter
#include "sound.hpp" // for commit_music_changes, etc
#include "synced_context.hpp" // for synced_context, etc
#include "synced_user_choice.hpp"
#include "team.hpp" // for team, village_owner
#include "terrain.hpp" // for terrain_type
#include "terrain_filter.hpp" // for terrain_filter
#include "terrain_translation.hpp" // for read_terrain_code, etc
#include "terrain_type_data.hpp"
#include "time_of_day.hpp" // for time_of_day, tod_color
#include "tod_manager.hpp" // for tod_manager
#include "tstring.hpp" // for t_string, operator+
#include "unit.hpp" // for unit, intrusive_ptr_add_ref, etc
#include "unit_animation_component.hpp" // for unit_animation_component
#include "unit_display.hpp"
#include "unit_filter.hpp"
#include "unit_map.hpp" // for unit_map, etc
#include "unit_ptr.hpp" // for unit_const_ptr, unit_ptr
#include "unit_types.hpp" // for unit_type_data, unit_types, etc
#include "util.hpp" // for lexical_cast
#include "variable.hpp" // for vconfig, etc
#include "variable_info.hpp"
#include "wml_exception.hpp"
#include <boost/bind.hpp> // for bind_t, bind
#include <boost/foreach.hpp> // for auto_any_base, etc
#include <boost/intrusive_ptr.hpp> // for intrusive_ptr
#include <boost/optional.hpp>
#include <boost/range/algorithm/copy.hpp> // boost::copy
#include <boost/range/adaptors.hpp> // boost::adaptors::filtered
#include <boost/tuple/tuple.hpp> // for tuple
#include <cassert> // for assert
#include <cstring> // for strcmp, NULL
#include <iterator> // for distance, advance
#include <map> // for map, map<>::value_type, etc
#include <new> // for operator new
#include <set> // for set
#include <sstream> // for operator<<, basic_ostream, etc
#include <utility> // for pair
#include <vector> // for vector, etc
#include "SDL_timer.h" // for SDL_GetTicks
#include "SDL_video.h" // for SDL_Color, SDL_Surface
#include "lua/lauxlib.h" // for luaL_checkinteger, etc
#include "lua/lua.h" // for lua_setfield, etc
class CVideo;
#ifdef DEBUG_LUA
#include "scripting/debug_lua.hpp"
#endif
// Suppress uninitialized variables warnings, because of boost::optional constructors in this file which apparently confuses gcc
#if defined(__GNUC__) && !defined(__clang__) // we shouldn't need this for clang, but for gcc and tdm-gcc we probably do
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6 ) // "GCC diagnostic ignored" is apparently not available at version 4.5.2
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#endif
static lg::log_domain log_scripting_lua("scripting/lua");
#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
#define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
#define ERR_LUA LOG_STREAM(err, log_scripting_lua)
std::vector<config> game_lua_kernel::preload_scripts;
config game_lua_kernel::preload_config;
struct map_locker
{
map_locker(game_lua_kernel* kernel) : kernel_(kernel)
{
++kernel_->map_locked_;
}
~map_locker()
{
--kernel_->map_locked_;
}
game_lua_kernel* kernel_;
};
void game_lua_kernel::extract_preload_scripts(config const &game_config)
{
game_lua_kernel::preload_scripts.clear();
BOOST_FOREACH(config const &cfg, game_config.child_range("lua")) {
game_lua_kernel::preload_scripts.push_back(cfg);
}
game_lua_kernel::preload_config = game_config.child("game_config");
}
void game_lua_kernel::log_error(char const * msg, char const * context)
{
lua_kernel_base::log_error(msg, context);
lua_chat(context, msg);
}
void game_lua_kernel::lua_chat(std::string const &caption, std::string const &msg)
{
if (game_display_) {
game_display_->get_chat_manager().add_chat_message(time(NULL), caption, 0, msg,
events::chat_handler::MESSAGE_PUBLIC, false);
}
}
/**
* Gets a vector of sides from side= attribute in a given config node.
* Promotes consistent behavior.
*/
std::vector<int> game_lua_kernel::get_sides_vector(const vconfig& cfg)
{
const config::attribute_value sides = cfg["side"];
const vconfig &ssf = cfg.child("filter_side");
if (!ssf.null()) {
if(!sides.empty()) { WRN_LUA << "ignoring duplicate side filter information (inline side=)" << std::endl; }
side_filter filter(ssf, &game_state_);
return filter.get_teams();
}
side_filter filter(sides.str(), &game_state_);
return filter.get_teams();
}
namespace {
/**
* Temporary entry to a queued_event stack
*/
struct queued_event_context
{
typedef game_events::queued_event qe;
std::stack<qe const *> & stack_;
queued_event_context(qe const *new_qe, std::stack<qe const*> & stack)
: stack_(stack)
{
stack_.push(new_qe);
}
~queued_event_context()
{
stack_.pop();
}
};
}//unnamed namespace for queued_event_context
/**
* Destroys a unit object before it is collected (__gc metamethod).
*/
static int impl_unit_collect(lua_State *L)
{
lua_unit *u = static_cast<lua_unit *>(lua_touserdata(L, 1));
u->lua_unit::~lua_unit();
return 0;
}
/**
* Checks two lua proxy units for equality. (__eq metamethod)
*/
static int impl_unit_equality(lua_State* L)
{
unit& left = luaW_checkunit(L, 1);
unit& right = luaW_checkunit(L, 2);
const bool equal = left.underlying_id() == right.underlying_id();
lua_pushboolean(L, equal);
return 1;
}
/**
* Gets some data on a unit (__index metamethod).
* - Arg 1: full userdata containing the unit id.
* - Arg 2: string containing the name of the property.
* - Ret 1: something containing the attribute.
*/
static int impl_unit_get(lua_State *L)
{
lua_unit *lu = static_cast<lua_unit *>(lua_touserdata(L, 1));
char const *m = luaL_checkstring(L, 2);
const unit* pu = lu->get();
if (strcmp(m, "valid") == 0)
{
if (!pu) return 0;
if (lu->on_map())
lua_pushstring(L, "map");
else if (lu->on_recall_list())
lua_pushstring(L, "recall");
else
lua_pushstring(L, "private");
return 1;
}
if (!pu) return luaL_argerror(L, 1, "unknown unit");
unit const &u = *pu;
// Find the corresponding attribute.
return_int_attrib("x", u.get_location().x + 1);
return_int_attrib("y", u.get_location().y + 1);
if (strcmp(m, "loc") == 0) {
lua_pushinteger(L, u.get_location().x + 1);
lua_pushinteger(L, u.get_location().y + 1);
return 2;
}
return_int_attrib("side", u.side());
return_string_attrib("id", u.id());
return_string_attrib("type", u.type_id());
return_string_attrib("image_mods", u.effect_image_mods());
return_int_attrib("hitpoints", u.hitpoints());
return_int_attrib("max_hitpoints", u.max_hitpoints());
return_int_attrib("experience", u.experience());
return_int_attrib("max_experience", u.max_experience());
return_int_attrib("recall_cost", u.recall_cost());
return_int_attrib("moves", u.movement_left());
return_int_attrib("max_moves", u.total_movement());
return_int_attrib("max_attacks", u.max_attacks());
return_int_attrib("attacks_left", u.attacks_left());
return_tstring_attrib("name", u.name());
return_bool_attrib("canrecruit", u.can_recruit());
return_int_attrib("level", u.level());
return_vector_string_attrib("extra_recruit", u.recruits());
return_vector_string_attrib("advances_to", u.advances_to());
if (strcmp(m, "alignment") == 0) {
lua_push(L, u.alignment());
return 1;
}
if (strcmp(m, "upkeep") == 0) {
unit::t_upkeep upkeep = u.upkeep_raw();
if(boost::get<unit::upkeep_full>(&upkeep) != NULL){
lua_pushstring(L, "full");
}
if(boost::get<unit::upkeep_loyal>(&upkeep) != NULL){
lua_pushstring(L, "loyal");
}
else {
lua_push(L, boost::get<int>(upkeep));
}
return 1;
}
if (strcmp(m, "advancements") == 0) {
lua_push(L, u.modification_advancements());
return 1;
}
if (strcmp(m, "overlays") == 0) {
lua_push(L, u.overlays());
return 1;
}
if (strcmp(m, "status") == 0) {
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1);
lua_pushlightuserdata(L
, ustatusKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
if (strcmp(m, "variables") == 0) {
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1);
lua_pushlightuserdata(L
, unitvarKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
if (strcmp(m, "attacks") == 0) {
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
// hack: store the unit at -1 becasue we want positive indexes to refers to the attacks.
lua_rawseti(L, -2, -1);
lua_pushlightuserdata(L, uattacksKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
return_bool_attrib("hidden", u.get_hidden());
return_bool_attrib("petrified", u.incapacitated());
return_bool_attrib("resting", u.resting());
return_string_attrib("role", u.get_role());
return_string_attrib("race", u.race()->id());
return_string_attrib("gender", gender_string(u.gender()));
return_string_attrib("variation", u.variation());
return_bool_attrib("zoc", u.get_emit_zoc());
return_string_attrib("facing", map_location::write_direction(u.facing()));
return_string_attrib("portrait", u.big_profile() == u.absolute_image() ? u.absolute_image() + u.image_mods() : u.big_profile());
return_cfg_attrib("__cfg", u.write(cfg); u.get_location().write(cfg));
return lua_kernel_base::get_lua_kernel<game_lua_kernel>(L).return_unit_method(L, m);
}
/**
* Sets some data on a unit (__newindex metamethod).
* - Arg 1: full userdata containing the unit id.
* - Arg 2: string containing the name of the property.
* - Arg 3: something containing the attribute.
*/
static int impl_unit_set(lua_State *L)
{
lua_unit *lu = static_cast<lua_unit *>(lua_touserdata(L, 1));
char const *m = luaL_checkstring(L, 2);
unit* pu = lu->get();
if (!pu) return luaL_argerror(L, 1, "unknown unit");
unit &u = *pu;
// Find the corresponding attribute.
//modify_int_attrib_check_range("side", u.set_side(value), 1, static_cast<int>(teams().size())); TODO: Figure out if this is a good idea, to refer to teams() and make this depend on having a gamestate
modify_int_attrib("side", u.set_side(value));
modify_int_attrib("moves", u.set_movement(value));
modify_int_attrib("hitpoints", u.set_hitpoints(value));
modify_int_attrib("experience", u.set_experience(value));
modify_int_attrib("recall_cost", u.set_recall_cost(value));
modify_int_attrib("attacks_left", u.set_attacks(value));
modify_int_attrib("level", u.set_level(value));
modify_bool_attrib("resting", u.set_resting(value));
modify_tstring_attrib("name", u.set_name(value));
modify_string_attrib("role", u.set_role(value));
modify_string_attrib("facing", u.set_facing(map_location::parse_direction(value)));
modify_bool_attrib("hidden", u.set_hidden(value));
modify_bool_attrib("zoc", u.set_emit_zoc(value));
modify_bool_attrib("canrecruit", u.set_can_recruit(value));
modify_vector_string_attrib("extra_recruit", u.set_recruits(vector));
modify_vector_string_attrib("advances_to", u.set_advances_to(vector));
if (strcmp(m, "alignment") == 0) {
u.set_alignment(lua_check<unit_type::ALIGNMENT>(L, 3));
return 0;
}
if (strcmp(m, "advancements") == 0) {
u.set_advancements(lua_check<std::vector<config> >(L, 3));
return 0;
}
if (strcmp(m, "upkeep") == 0) {
if(lua_isnumber(L, 3)) {
u.set_upkeep(luaL_checkint(L, 3));
return 0;
}
const char* v = luaL_checkstring(L, 3);
if(strcmp(m, "loyal") == 0) {
u.set_upkeep(unit::upkeep_loyal());
}
else if(strcmp(m, "full") == 0) {
u.set_upkeep(unit::upkeep_full());
}
else {
std::string err_msg = "unknown upkeep value of unit: ";
err_msg += v;
return luaL_argerror(L, 2, err_msg.c_str());
}
return 0;
}
if (!lu->on_map()) {
map_location loc = u.get_location();
modify_int_attrib("x", loc.x = value - 1; u.set_location(loc));
modify_int_attrib("y", loc.y = value - 1; u.set_location(loc));
}
std::string err_msg = "unknown modifiable property of unit: ";
err_msg += m;
return luaL_argerror(L, 2, err_msg.c_str());
}
/**
* Gets the status of a unit (__index metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* - Arg 2: string containing the name of the status.
* - Ret 1: boolean.
*/
static int impl_unit_status_get(lua_State *L)
{
if (!lua_istable(L, 1))
return luaL_typerror(L, 1, "unit status");
lua_rawgeti(L, 1, 1);
const unit* u = luaW_tounit(L, -1);
if (!u) return luaL_argerror(L, 1, "unknown unit");
char const *m = luaL_checkstring(L, 2);
lua_pushboolean(L, u->get_state(m));
return 1;
}
/**
* Sets the status of a unit (__newindex metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* - Arg 2: string containing the name of the status.
* - Arg 3: boolean.
*/
static int impl_unit_status_set(lua_State *L)
{
if (!lua_istable(L, 1))
return luaL_typerror(L, 1, "unit status");
lua_rawgeti(L, 1, 1);
unit* u = luaW_tounit(L, -1);
if (!u) return luaL_argerror(L, 1, "unknown unit");
char const *m = luaL_checkstring(L, 2);
u->set_state(m, luaW_toboolean(L, 3));
return 0;
}
/**
* Gets the variable of a unit (__index metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* - Arg 2: string containing the name of the status.
* - Ret 1: boolean.
*/
static int impl_unit_variables_get(lua_State *L)
{
if (!lua_istable(L, 1))
return luaL_typerror(L, 1, "unit variables");
lua_rawgeti(L, 1, 1);
const unit* u = luaW_tounit(L, -1);
if (!u) return luaL_argerror(L, 1, "unknown unit");
char const *m = luaL_checkstring(L, 2);
return_cfgref_attrib("__cfg", u->variables());
variable_access_const v(m, u->variables());
return LuaW_pushvariable(L, v) ? 1 : 0;
}
/**
* Gets the attacks of a unit (__index metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* - Arg 2: index (int) or id (string) identifying a particular attack.
* - Ret 1: the unit's attacks.
*/
static int impl_unit_attacks_get(lua_State *L)
{
if (!lua_istable(L, 1)) {
return luaL_typerror(L, 1, "unit attacks");
}
lua_rawgeti(L, 1, -1);
const unit* u = luaW_tounit(L, -1);
if (!u) {
return luaL_argerror(L, 1, "unknown unit");
}
const attack_type* attack = NULL;
const std::vector<attack_type>& attacks = u->attacks();
if(!lua_isnumber(L,2)) {
std::string attack_id = luaL_checkstring(L, 2);
BOOST_FOREACH(const attack_type& at, attacks) {
if(at.id() == attack_id) {
attack = &at;
break;
}
}
if (attack == NULL) {
//return nil on invalid index, just like lua tables do.
return 0;
}
}
else
{
//
size_t index = luaL_checkinteger(L, 2) - 1;
if (index >= attacks.size()) {
//return nil on invalid index, just like lua tables do.
return 0;
}
attack = &attacks[index];
}
// stack { lua_unit }, id/index, lua_unit
lua_createtable(L, 2, 0);
// stack { lua_unit }, id/index, lua_unit, table
lua_pushvalue(L, -2);
// stack { lua_unit }, id/index, lua_unit, table, lua_unit
lua_rawseti(L, -2, 1);
// stack { lua_unit }, id/index, lua_unit, table
lua_pushstring(L, attack->id().c_str());
// stack { lua_unit }, id/index, lua_unit, table, attack id
lua_rawseti(L, -2, 2);
// stack { lua_unit }, id/index, lua_unit, table
lua_pushlightuserdata(L, uattackKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
/**
* Counts the attacks of a unit (__len metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* - Ret 1: size of unit attacks vector.
*/
static int impl_unit_attacks_len(lua_State *L)
{
if (!lua_istable(L, 1)) {
return luaL_typerror(L, 1, "unit attacks");
}
lua_rawgeti(L, 1, -1);
const unit* u = luaW_tounit(L, -1);
if (!u) {
return luaL_argerror(L, 1, "unknown unit");
}
lua_pushinteger(L, u->attacks().size());
return 1;
}
/**
* Gets a propoerty of a units attack (__index metamethod).
* - Arg 1: table containing the userdata containing the unit id. and a string identyfying the attack.
* - Arg 2: string
* - Ret 1:
*/
static int impl_unit_attack_get(lua_State *L)
{
if (!lua_istable(L, 1)) {
return luaL_typerror(L, 1, "unit attack");
}
lua_rawgeti(L, 1, 1);
const unit* u = luaW_tounit(L, -1);
if (!u) {
return luaL_argerror(L, 1, "unknown unit");
}
lua_rawgeti(L, 1, 2);
std::string attack_id = luaL_checkstring(L, -1);
char const *m = luaL_checkstring(L, 2);
BOOST_FOREACH(const attack_type& attack, u->attacks())
{
if(attack.id() == attack_id)
{
return_string_attrib("description", attack.name());
return_string_attrib("name", attack.id());
return_string_attrib("type", attack.type());
return_string_attrib("icon", attack.icon());
return_string_attrib("range", attack.range());
// "min_range"
// "max_range"
return_int_attrib("damage", attack.damage());
return_int_attrib("number", attack.num_attacks());
return_int_attrib("attack_weight", attack.attack_weight());
return_int_attrib("defense_weight", attack.defense_weight());
return_int_attrib("accuracy", attack.accuracy());
return_int_attrib("movement_used", attack.movement_used());
return_int_attrib("parry", attack.parry());
return_cfgref_attrib("specials", attack.specials());
std::string err_msg = "unknown property of attack: ";
err_msg += m;
return luaL_argerror(L, 2, err_msg.c_str());
}
}
return luaL_argerror(L, 1, "invalid attack id");
}
/**
* Gets a propoerty of a units attack (__index metamethod).
* - Arg 1: table containing the userdata containing the unit id. and a string identyfying the attack.
* - Arg 2: string
* - Ret 1:
*/
static int impl_unit_attack_set(lua_State *L)
{
if (!lua_istable(L, 1)) {
return luaL_typerror(L, 1, "unit attack");
}
lua_rawgeti(L, 1, 1);
unit* u = luaW_tounit(L, -1);
if (!u) {
return luaL_argerror(L, 1, "unknown unit");
}
lua_rawgeti(L, 1, 2);
std::string attack_id = luaL_checkstring(L, -1);
char const *m = luaL_checkstring(L, 2);
BOOST_FOREACH(attack_type& attack, u->attacks())
{
if(attack.id() == attack_id)
{
modify_tstring_attrib("description", attack.set_name(value));
// modify_string_attrib("name", attack.set_id(value));
modify_string_attrib("type", attack.set_type(value));
modify_string_attrib("icon", attack.set_icon(value));
modify_string_attrib("range", attack.set_range(value));
// "min_range"
// "max_range"
modify_int_attrib("damage", attack.set_damage(value));
modify_int_attrib("number", attack.set_num_attacks(value));
modify_int_attrib("attack_weight", attack.set_attack_weight(value));
modify_int_attrib("defense_weight", attack.set_defense_weight(value));
modify_int_attrib("accuracy", attack.set_accuracy(value));
modify_int_attrib("movement_used", attack.set_movement_used(value));
modify_int_attrib("parry", attack.set_parry(value));
if (strcmp(m, "specials") == 0) { \
attack.set_specials(luaW_checkconfig(L, 3));
return 0;
}
return_cfgref_attrib("specials", attack.specials());
std::string err_msg = "unknown modifyable property of attack: ";
err_msg += m;
return luaL_argerror(L, 2, err_msg.c_str());
}
}
return luaL_argerror(L, 1, "invalid attack id");
}
/**
* Sets the variable of a unit (__newindex metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* - Arg 2: string containing the name of the status.
* - Arg 3: scalar.
*/
static int impl_unit_variables_set(lua_State *L)
{
if (!lua_istable(L, 1))
return luaL_typerror(L, 1, "unit variables");
lua_rawgeti(L, 1, 1);
unit* u = luaW_tounit(L, -1);
if (!u) return luaL_argerror(L, 1, "unknown unit");
char const *m = luaL_checkstring(L, 2);
if (strcmp(m, "__cfg") == 0) {
u->variables() = luaW_checkconfig(L, 3);
return 0;
}
variable_access_create v(m, u->variables());
LuaW_checkvariable(L, v, 3);
return 0;
}
/**
* Gets currently viewing side.
* - Ret 1: integer specifying the currently viewing side
* - Ret 2: Bool whether the vision is not limited to that team, this can for example be true during replays.
*/
static int intf_get_viewing_side(lua_State *L)
{
if(const display* disp = display::get_singleton()) {
lua_pushinteger(L, disp->viewing_side());
lua_pushboolean(L, disp->show_everything());
return 2;
}
else {
return 0;
}
}
int game_lua_kernel::intf_animate_unit(lua_State *L)
{
// if (game_display_)
{
events::command_disabler disable_commands;
unit_display::wml_animation(luaW_checkvconfig(L, 1), get_event_info().loc1);
}
return 0;
}
int game_lua_kernel::intf_gamestate_inspector(lua_State *L)
{
if (game_display_) {
return lua_gui2::show_gamestate_inspector(game_display_->video(), luaW_checkvconfig(L, 1));
}
return 0;
}
/**
* Gets the unit at the given location or with the given id.
* - Arg 1: table containing location either at indices 1,2 or at keys x,y.
* OR
* - Arg 1: string ID
* OR
* - Args 1, 2: location.
* - Ret 1: full userdata with __index pointing to impl_unit_get and
* __newindex pointing to impl_unit_set.
*/
int game_lua_kernel::intf_get_unit(lua_State *L)
{
int x, y;
if (lua_isnoneornil(L, 2)) {
if (lua_istable(L, 1)) {
lua_rawgeti(L, 1, 1);
if (lua_isnoneornil(L, -1)) {
lua_getfield(L, 1, "x");
x = luaL_checkinteger(L, -1) - 1;
lua_getfield(L, 1, "y");
y = luaL_checkinteger(L, -1) - 1;
} else {
x = luaL_checkinteger(L, -1) - 1;
lua_rawgeti(L, 1, 2);
y = luaL_checkinteger(L, -1) - 1;
}
} else if(lua_isstring(L, 1)) {
std::string id = luaL_checkstring(L, 1);
BOOST_FOREACH(const unit& u, units()) {
if(u.id() == id) {
new(lua_newuserdata(L, sizeof(lua_unit))) lua_unit(u.underlying_id());
lua_pushlightuserdata(L, getunitKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
}
return 0;
} else {
return luaL_argerror(L, 1, "expected string, location table, or integer");
}
} else {
x = luaL_checkinteger(L, 1) - 1;
y = luaL_checkinteger(L, 2) - 1;
}
unit_map::const_iterator ui = units().find(map_location(x, y));
if (!ui.valid()) return 0;
new(lua_newuserdata(L, sizeof(lua_unit))) lua_unit(ui->underlying_id());
lua_pushlightuserdata(L
, getunitKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
/**
* Gets the unit displayed in the sidebar.
* - Ret 1: full userdata with __index pointing to impl_unit_get and
* __newindex pointing to impl_unit_set.
*/
int game_lua_kernel::intf_get_displayed_unit(lua_State *L)
{
if (!game_display_) {
return 0;
}
unit_map::const_iterator ui = board().find_visible_unit(
game_display_->displayed_unit_hex(),
teams()[game_display_->viewing_team()],
game_display_->show_everything());
if (!ui.valid()) return 0;
new(lua_newuserdata(L, sizeof(lua_unit))) lua_unit(ui->underlying_id());
lua_pushlightuserdata(L
, getunitKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
/**
* Gets all the units matching a given filter.
* - Arg 1: optional table containing a filter
* - Ret 1: table containing full userdata with __index pointing to
* impl_unit_get and __newindex pointing to impl_unit_set.
*/
int game_lua_kernel::intf_get_units(lua_State *L)
{
vconfig filter = luaW_checkvconfig(L, 1, true);
// Go through all the units while keeping the following stack:
// 1: metatable, 2: return table, 3: userdata, 4: metatable copy
lua_settop(L, 0);
lua_pushlightuserdata(L
, getunitKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_newtable(L);
int i = 1;
// note that if filter is null, this yields a null filter matching everything (and doing no work)
filter_context & fc = game_state_;
BOOST_FOREACH ( const unit * ui, unit_filter(filter, &fc).all_matches_on_map()) {
new(lua_newuserdata(L, sizeof(lua_unit))) lua_unit(ui->underlying_id());
lua_pushvalue(L, 1);
lua_setmetatable(L, 3);
lua_rawseti(L, 2, i);
++i;
}
return 1;
}
/**
* Matches a unit against the given filter.
* - Arg 1: full userdata.
* - Arg 2: table containing a filter
* - Ret 1: boolean.
*/
int game_lua_kernel::intf_match_unit(lua_State *L)
{
if (!luaW_hasmetatable(L, 1, getunitKey))
return luaL_typerror(L, 1, "unit");
lua_unit *lu = static_cast<lua_unit *>(lua_touserdata(L, 1));
unit* u = lu->get();
if (!u) return luaL_argerror(L, 1, "unit not found");
vconfig filter = luaW_checkvconfig(L, 2, true);
if (filter.null()) {
lua_pushboolean(L, true);
return 1;
}
filter_context & fc = game_state_;
if (int side = lu->on_recall_list()) {
if (!lua_isnoneornil(L, 3)) {
WRN_LUA << "wesnoth.match_unit called with 3rd argument, but unit to match was on recall list. ";
WRN_LUA << "Thus the 3rd argument is ignored.\n";
}
team &t = (teams())[side - 1];
scoped_recall_unit auto_store("this_unit",
t.save_id(), t.recall_list().find_index(u->id()));
lua_pushboolean(L, unit_filter(filter, &fc).matches(*u, map_location()));
return 1;
}
if (!lua_isnoneornil(L, 3)) {
lua_unit *lu_adj = static_cast<lua_unit *>(lua_touserdata(L, 1));
unit* u_adj = lu_adj->get();
if (!u_adj) {
return luaL_argerror(L, 3, "unit not found");
}
lua_pushboolean(L, unit_filter(filter, &fc).matches(*u, *u_adj));
} else {
lua_pushboolean(L, unit_filter(filter, &fc).matches(*u));
}
return 1;
}
/**
* Gets the numeric ids of all the units matching a given filter on the recall lists.
* - Arg 1: optional table containing a filter
* - Ret 1: table containing full userdata with __index pointing to
* impl_unit_get and __newindex pointing to impl_unit_set.
*/
int game_lua_kernel::intf_get_recall_units(lua_State *L)
{
vconfig filter = luaW_checkvconfig(L, 1, true);
// Go through all the units while keeping the following stack:
// 1: metatable, 2: return table, 3: userdata, 4: metatable copy
lua_settop(L, 0);
lua_pushlightuserdata(L
, getunitKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_newtable(L);
int i = 1, s = 1;
filter_context & fc = game_state_;
const unit_filter ufilt(filter, &fc);
BOOST_FOREACH(team &t, teams())
{
BOOST_FOREACH(unit_ptr & u, t.recall_list())
{
if (!filter.null()) {
scoped_recall_unit auto_store("this_unit",
t.save_id(), t.recall_list().find_index(u->id()));
if (!ufilt( *u, map_location() ))
continue;
}
new(lua_newuserdata(L, sizeof(lua_unit))) lua_unit(s, u->underlying_id());
lua_pushvalue(L, 1);
lua_setmetatable(L, 3);
lua_rawseti(L, 2, i);
++i;
}
++s;
}
return 1;
}
/**
* Fires an event.
* - Arg 1: string containing the event name.
* - Arg 2,3: optional first location.
* - Arg 4,5: optional second location.
* - Arg 6: optional WML table used as the [weapon] tag.
* - Arg 7: optional WML table used as the [second_weapon] tag.
* - Ret 1: boolean indicating whether the event was processed or not.
*/
int game_lua_kernel::intf_fire_event(lua_State *L)
{
char const *m = luaL_checkstring(L, 1);
int pos = 2;
map_location l1, l2;
config data;
if (lua_isnumber(L, 2)) {
l1.x = lua_tointeger(L, 2) - 1;
l1.y = luaL_checkinteger(L, 3) - 1;
if (lua_isnumber(L, 4)) {
l2.x = lua_tointeger(L, 4) - 1;
l2.y = luaL_checkinteger(L, 5) - 1;
pos = 6;
} else pos = 4;
}
if (!lua_isnoneornil(L, pos)) {
data.add_child("first", luaW_checkconfig(L, pos));
}
++pos;
if (!lua_isnoneornil(L, pos)) {
data.add_child("second", luaW_checkconfig(L, pos));
}
bool b = play_controller_.pump().fire(m, l1, l2, data);
lua_pushboolean(L, b);
return 1;
}
/**
* Fires a wml menu item.
* - Arg 1: id of the item. it is not possible to fire items that don't have ids with this function.
* - Arg 2,3: optional first location.
* - Ret 1: boolean, true indicating that the event was fired successfully
*
* NOTE: This is not an "official" feature, it may currently cause assertion failures if used with
* menu items which have "needs_select". It is not supported right now to use it this way.
* The purpose of this function right now is to make it possible to have automated sanity tests for
* the wml menu items system.
*/
int game_lua_kernel::intf_fire_wml_menu_item(lua_State *L)
{
char const *m = luaL_checkstring(L, 1);