-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathlua_api.c
More file actions
2263 lines (1929 loc) · 71.3 KB
/
lua_api.c
File metadata and controls
2263 lines (1929 loc) · 71.3 KB
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
#include "pre_inc.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "bflib_basics.h"
#include "bflib_sndlib.h"
#include "globals.h"
#include "thing_data.h"
#include "creature_states.h"
#include "creature_states_pray.h"
#include "gui_msgs.h"
#include "gui_soundmsgs.h"
#include "thing_navigate.h"
#include "map_data.h"
#include "game_legacy.h"
#include "player_utils.h"
#include "lvl_script_lib.h"
#include "room_library.h"
#include "room_util.h"
#include "keeperfx.hpp"
#include "power_specials.h"
#include "thing_creature.h"
#include "thing_effects.h"
#include "magic_powers.h"
#include "lua_api.h"
#include "lua_base.h"
#include "lua_params.h"
#include "lua_api_lens.h"
#include "post_inc.h"
/**********************************************/
/***************************************************************************************************/
/************ Api Functions ******************************************************************/
/***************************************************************************************************/
//Setup Commands
static int lua_Set_generate_speed(lua_State *L)
{
GameTurnDelta interval = luaL_checkinteger(L,1);
PlayerNumber player_idx = (luaL_isPlayer(L,2)) ? luaL_checkPlayerSingle(L,2) : ALL_PLAYERS;
struct PlayerInfo* player;
if (player_idx == ALL_PLAYERS)
{
for (PlayerNumber plyr_idx = 0; plyr_idx < PLAYERS_COUNT; plyr_idx++)
{
player = get_player(plyr_idx);
if (!player_invalid(player))
{
player->generate_speed = saturate_set_unsigned(interval, 16);
}
}
}
else
{
player = get_player(player_idx);
if (!player_invalid(player))
{
player->generate_speed = saturate_set_unsigned(interval, 16);
}
}
update_dungeon_generation_speeds();
return 0;
}
static int lua_Computer_player(lua_State *L)
{
PlayerNumber player_idx = luaL_checkPlayerSingle(L,1);
if (lua_isnumber(L, 2))
{
long attitude = luaL_checkint(L,2);
script_support_setup_player_as_computer_keeper(player_idx, attitude);
return 0;
}
const char* comp_model = luaL_checkstring(L,2);
if(strcasecmp(comp_model,"ROAMING") == 0)
{
struct PlayerInfo* player = get_player(player_idx);
player->player_type = PT_Roaming;
player->allocflags |= PlaF_Allocated;
player->allocflags |= PlaF_CompCtrl;
player->id_number = player_idx;
return 0;
}
else
{
luaL_error(L,"invalid Computer_player param '%s'",comp_model);
return 0;
}
}
static int lua_Ally_players(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
PlayerNumber player_idx = luaL_checkPlayerSingle(L,2);
uchar state = luaL_checkinteger(L, 3);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
set_ally_with_player(i, player_idx, (state & 1) ? true : false);
set_ally_with_player(player_idx, i, (state & 1) ? true : false);
set_player_ally_locked(i, player_idx, (state & 2) ? true : false);
set_player_ally_locked(player_idx, i, (state & 2) ? true : false);
}
return 0;
}
static int lua_Start_money(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
GoldAmount gold_val = luaL_checkinteger(L, 2);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
if (gold_val > SENSIBLE_GOLD)
{
gold_val = SENSIBLE_GOLD;
SCRPTWRNLOG("Gold added to player reduced to %d", SENSIBLE_GOLD);
}
player_add_offmap_gold(i, gold_val);
}
return 0;
}
static int lua_Max_creatures(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long max_amount = luaL_checkinteger(L, 2);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
SYNCDBG(4,"Setting player %d max attracted creatures to %d.",(int)i,(int)max_amount);
struct Dungeon* dungeon = get_dungeon(i);
if (dungeon_invalid(dungeon))
continue;
dungeon->max_creatures_attracted = max_amount;
}
return 0;
}
static int lua_Add_creature_to_pool(lua_State *L)
{
long crtr_model = luaL_checkNamedCommand(L,1,creature_desc);
long amount = luaL_checkinteger(L, 2);
add_creature_to_pool(crtr_model, amount);
return 0;
}
static int lua_Creature_available(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long cr_kind = luaL_checkNamedCommand(L,2,creature_desc);
TbBool can_be_attracted = lua_toboolean(L, 3);
long amount_forced = luaL_checkinteger(L, 4);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
if (!set_creature_available(i,cr_kind,can_be_attracted,amount_forced))
WARNLOG("Setting creature %s availability for player %d failed.",creature_code_name(cr_kind),(int)i);
}
return 0;
}
static int lua_Dead_creatures_return_to_pool(lua_State *L)
{
TbBool return_to_pool = lua_toboolean(L, 3);
set_flag_value(game.mode_flags, MFlg_DeadBackToPool, return_to_pool);
return 0;
}
static int lua_Room_available(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long rkind = luaL_checkNamedCommand(L,2,room_desc);
TbBool can_be_available = lua_tointeger(L, 3);
TbBool is_available = lua_toboolean(L, 4);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
set_room_available(i,rkind,can_be_available,is_available);
}
return 0;
}
static int lua_Magic_available(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long power = luaL_checkNamedCommand(L,2,power_desc);
TbBool can_be_available = lua_toboolean(L, 3);
TbBool is_available = lua_toboolean(L, 4);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
set_power_available(i,power,can_be_available,is_available);
}
return 0;
}
static int lua_Door_available(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long door_type = luaL_checkNamedCommand(L,2,door_desc);
TbBool can_be_available = lua_toboolean(L, 3);
long number_available = luaL_checkinteger(L, 4);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
set_door_buildable_and_add_to_amount(i, door_type, can_be_available, number_available);
}
return 0;
}
static int lua_Trap_available(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long trap_type = luaL_checkNamedCommand(L,2,trap_desc);
TbBool can_be_available = lua_toboolean(L, 3);
long number_available = luaL_checkinteger(L, 4);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
set_trap_buildable_and_add_to_amount(i, trap_type, can_be_available, number_available);
}
return 0;
}
//Script flow control
static int lua_Win_game(lua_State *L)
{
struct PlayerRange player_range;
if(lua_isnone(L,1))
{
player_range.start_idx = 0;
player_range.end_idx = PLAYERS_COUNT;
}
else
player_range = luaL_checkPlayerRange(L, 1);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
struct PlayerInfo *player = get_player(i);
set_player_as_won_level(player);
}
return 0;
}
static int lua_Lose_game(lua_State *L)
{
struct PlayerRange player_range;
if(lua_isnone(L,1))
{
player_range.start_idx = 0;
player_range.end_idx = PLAYERS_COUNT;
}
else
player_range = luaL_checkPlayerRange(L, 1);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
struct PlayerInfo *player = get_player(i);
set_player_as_lost_level(player);
}
return 0;
}
static int lua_Count_creatures_at_action_point(lua_State *L)
{
ActionPointId ap_idx = luaL_checkActionPoint(L, 1);
struct PlayerRange player_range = luaL_checkPlayerRange(L, 2);
long crtr_model = luaL_checkCreature_or_creature_wildcard(L,3);
long sum = 0;
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
sum += count_player_creatures_of_model_in_action_point(i, crtr_model, ap_idx);
}
lua_pushinteger(L, sum);
return 1;
}
static int lua_Set_timer(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long timr_id = luaL_checkNamedCommand(L,2,timer_desc);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
restart_script_timer(i, timr_id);
}
return 0;
}
static int lua_Add_to_timer(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
long timr_id = luaL_checkNamedCommand(L,2,timer_desc);
long amount = luaL_checkinteger(L, 3);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
add_to_script_timer(i, timr_id, amount);
}
return 0;
}
static int lua_Display_timer(lua_State *L)
{
PlayerNumber player_id = luaL_checkPlayerSingle(L, 1);
long timr_id = luaL_checkNamedCommand(L,2,timer_desc);
long display = luaL_checkinteger(L, 3);
game.script_timer_player = player_id;
game.script_timer_id = timr_id;
game.script_timer_limit = 0;
game.timer_real = display;
game.flags_gui |= GGUI_ScriptTimer;
return 0;
}
static int lua_Hide_timer(lua_State *L)
{
game.flags_gui &= ~GGUI_ScriptTimer;
return 0;
}
static int lua_Bonus_level_time(lua_State *L)
{
GameTurn turns = luaL_checkinteger(L, 1);
TbBool clocktime = lua_toboolean(L, 2);
if (turns > 0)
{
game.bonus_time = game.play_gameturn + turns;
set_flag(game.flags_gui, GGUI_CountdownTimer);
}
else
{
game.bonus_time = 0;
clear_flag(game.flags_gui, GGUI_CountdownTimer);
}
game.timer_real = clocktime;
return 0;
}
static int lua_Add_bonus_time(lua_State *L)
{
GameTurnDelta turns = luaL_checkinteger(L, 1);
game.bonus_time += turns;
return 0;
}
static int lua_Reset_action_point(lua_State *L)
{
ActionPointId apt_idx = luaL_checkActionPoint(L, 1);
PlayerNumber player_range = luaL_checkPlayerRangeId(L, 2);
action_point_reset_idx(apt_idx, player_range);
return 0;
}
static int lua_Set_next_level(lua_State *L)
{
LevelNumber lvnum = luaL_checkinteger(L, 1);
if(!is_level_in_current_campaign(lvnum))
{
return luaL_argerror(L, 1, lua_pushfstring(L, "Level '%d' not part of current campaign", lvnum));
}
intralvl.next_level = lvnum;
return 0;
}
//Adding New Creatures and Parties to the Level
static int lua_Add_creature_to_level(lua_State *L)
{
PlayerNumber plr_idx = luaL_checkPlayerSingle(L, 1);
long crtr_id = luaL_checkNamedCommand(L,2,creature_desc);
TbMapLocation location = luaL_checkLocation(L, 3);
long crtr_level = luaL_checkinteger(L, 4);
long carried_gold = luaL_checkinteger(L, 5);
long spawn_type;
if(!lua_isnoneornil(L, 6))
{
spawn_type = luaL_checkNamedCommand(L, 6,spawn_type_desc);
}
else
{
spawn_type = SpwnT_Default;
}
if ((crtr_level < 1) || (crtr_level > CREATURE_MAX_LEVEL))
{
SCRPTERRLOG("Invalid CREATURE LEVEL parameter");
return 0;
}
// Recognize place where party is created
if (location == 0)
return 0;
lua_pushThing(L,script_create_new_creature(plr_idx, crtr_id, location, carried_gold, crtr_level-1,spawn_type));
return 1;
}
static int lua_Add_tunneller_to_level(lua_State *L)
{
PlayerNumber plr_id = luaL_checkPlayerSingle(L,1);
TbMapLocation spawn_location = luaL_checkLocation(L,2);
TbMapLocation head_for = luaL_checkHeadingLocation(L,3); // checks 2 params
long level = luaL_checkinteger(L,5);
long gold_held = luaL_checkinteger(L,6);
struct Thing* thing = script_process_new_tunneler(plr_id, spawn_location, head_for, level-1, gold_held);
lua_pushThing(L, thing);
return 1;
}
static int lua_Create_party(lua_State *L)
{
const char* party_name = luaL_checklstring(L, 1,NULL);
create_party(party_name);
return 0;
}
static int lua_Add_to_party(lua_State *L)
{
long party_id = luaL_checkParty(L, 1);
long crtr_id = luaL_checkNamedCommand(L,2,creature_desc);
long experience = luaL_checklong(L, 3);
long gold = luaL_checklong(L, 4);
long objective_id = luaL_checkNamedCommand(L, 5,hero_objective_desc);
long countdown = luaL_checklong (L, 6);
if ((experience < 1) || (experience > CREATURE_MAX_LEVEL))
{
SCRPTERRLOG("Invalid Creature Level parameter; %ld not in range (%d,%d)",experience,1,CREATURE_MAX_LEVEL);
return 0;
}
add_member_to_party(party_id, crtr_id, experience, gold, objective_id, countdown, -1);
return 0;
}
static int lua_Delete_from_party(lua_State *L)
{
long party_id = luaL_checkParty(L, 1);
const char* creature = lua_tostring(L, 2);
long experience = lua_tointeger(L, 3);
long creature_id = get_rid(creature_desc, creature);
if (creature_id == -1)
{
SCRPTERRLOG("Unknown creature, '%s'", creature);
return 0;
}
delete_member_from_party(party_id, creature_id, experience);
return 0;
}
static int lua_Add_tunneller_party_to_level(lua_State *L)
{
PlayerNumber owner = luaL_checkPlayerSingle(L, 1);
long prty_id = luaL_checkParty(L, 2);
TbMapLocation spawn_location = luaL_checkLocation(L, 3);
TbMapLocation head_for = luaL_checkHeadingLocation(L,4); // checks 2 params
long crtr_level = luaL_checkCrtLevel(L, 6);
GoldAmount carried_gold = luaL_checkinteger(L, 7);
struct Party* party = &game.script.creature_partys[prty_id];
if (party->members_num >= GROUP_MEMBERS_COUNT-1)
{
SCRPTERRLOG("Party too big for ADD_TUNNELLER (Max %d members)", GROUP_MEMBERS_COUNT-1);
return 0;
}
struct Thing* leadtng = script_process_new_tunneller_party(owner, prty_id, spawn_location, head_for, crtr_level, carried_gold);
if (thing_is_invalid(leadtng))
{
return 0;
}
lua_pushPartyTable(L, leadtng);
return 1;
}
static int lua_Add_party_to_level(lua_State *L)
{
PlayerNumber owner = luaL_checkPlayerSingle(L, 1);
long prty_id = luaL_checkParty(L, 2);
TbMapLocation location = luaL_checkLocation(L, 3);
// Recognize place where party is created
if (location == 0)
return 0;
struct Party* party = &game.script.creature_partys[prty_id];
struct Thing* leadtng = script_process_new_party(party, owner, location, 1);
if (thing_is_invalid(leadtng))
{
return 0;
}
lua_pushPartyTable(L, leadtng);
return 1;
}
//Displaying information and affecting interface
static int lua_Display_objective(lua_State *L)
{
long msg_id = luaL_checkinteger(L, 1);
TbMapLocation zoom_location = luaL_optLocation(L,2);
set_general_objective(msg_id,zoom_location,0,0);
return 0;
}
static int lua_Display_objective_with_pos(lua_State *L)
{
long msg_id = luaL_checkinteger(L, 1);
long stl_x = luaL_checkstl_x(L, 2);
long stl_y = luaL_checkstl_y(L, 3);
set_general_objective(msg_id,0,stl_x,stl_y);
return 0;
}
static int lua_Display_information(lua_State *L)
{
long msg_id = luaL_checkinteger(L, 1);
TbMapLocation zoom_location = luaL_optLocation(L,2);
set_general_information(msg_id,zoom_location,0,0);
return 0;
}
static int lua_Display_information_with_pos(lua_State *L)
{
long msg_id = luaL_checkinteger(L, 1);
long stl_x = luaL_checkstl_x(L, 2);
long stl_y = luaL_checkstl_y(L, 3);
set_general_objective(msg_id,0,stl_x,stl_y);
return 0;
}
static int lua_Quick_objective(lua_State *L)
{
const char *msg_text = lua_tostring(L, 1);
TbMapLocation target = luaL_optLocation(L, 2);
process_objective(msg_text, target, 0, 0);
return 0;
}
static int lua_Quick_information(lua_State *L)
{
long slot = luaL_checkIntMinMax(L, 1, 0,QUICK_MESSAGES_COUNT-1);
const char *msg_text = lua_tostring(L, 2);
TbMapLocation target = luaL_optLocation(L, 3);
snprintf(game.quick_messages[slot], MESSAGE_TEXT_LEN, "%s", msg_text);
set_quick_information(slot, target, 0, 0);
return 0;
}
static int lua_Quick_objective_with_pos(lua_State *L)
{
const char *msg_text = lua_tostring(L, 1);
MapSubtlCoord stl_x = luaL_checkstl_x(L, 2);
MapSubtlCoord stl_y = luaL_checkstl_y(L, 3);
process_objective(msg_text, 0, stl_x, stl_y);
return 0;
}
static int lua_Quick_information_with_pos(lua_State *L)
{
long slot = luaL_checkIntMinMax(L, 1, 0,QUICK_MESSAGES_COUNT-1);
const char *msg_text = lua_tostring(L, 2);
MapSubtlCoord stl_x = luaL_checkstl_x(L, 3);
MapSubtlCoord stl_y = luaL_checkstl_y(L, 4);
snprintf(game.quick_messages[slot], MESSAGE_TEXT_LEN, "%s", msg_text);
set_quick_information(slot, 0, stl_x, stl_y);
return 0;
}
static int lua_Display_message(lua_State *L)
{
int msg_id = luaL_checkinteger(L, 1);
const char *msg = get_string(msg_id);
char id;
char type;
luaL_checkMessageIcon(L, 1, &type, &id);
message_add(type,id, msg);
return 0;
}
static int lua_Quick_message(lua_State *L)
{
const char *msg = lua_tostring(L, 1);
char id;
char type;
luaL_checkMessageIcon(L, 2, &type, &id);
message_add(type,id, msg);
return 0;
}
static int lua_Clear_message(lua_State* L)
{
char count = luaL_optCheckinteger(L, 1);
if ((count <= 0) || (count > GUI_MESSAGES_COUNT))
{
count = GUI_MESSAGES_COUNT;
}
for (int k = game.active_messages_count - 1; k >= (game.active_messages_count - count); k--)
{
game.messages[k].expiration_turn = game.play_gameturn;
}
return 0;
}
static int lua_Heart_lost_objective(lua_State *L)
{
long message_id = luaL_checkinteger(L, 1);
TbMapLocation target = luaL_checkLocation(L, 2);
game.heart_lost_display_message = true;
game.heart_lost_quick_message = false;
game.heart_lost_message_id = message_id;
game.heart_lost_message_target = target;
return 0;
}
static int lua_Heart_lost_quick_objective(lua_State *L)
{
long slot = luaL_checkIntMinMax(L, 1, 0,QUICK_MESSAGES_COUNT-1);
const char *msg = lua_tostring(L, 2);
TbMapLocation target = luaL_checkLocation(L, 3);
snprintf(game.quick_messages[slot], MESSAGE_TEXT_LEN, "%s", msg);
game.heart_lost_display_message = true;
game.heart_lost_quick_message = true;
game.heart_lost_message_id = slot;
game.heart_lost_message_target = target;
return 0;
}
static int lua_Play_message(lua_State *L)
{
PlayerNumber player_idx = luaL_checkPlayerSingle(L, 1);
long msgtype_id = luaL_checkNamedCommand(L, 2, msgtype_desc);
TbBool param_is_string;
const char* filename = NULL;
long msg_id = 0;
if (lua_isnumber(L, 3)) {
param_is_string = false;
msg_id = luaL_checkinteger(L, 3);
} else {
param_is_string = true;
filename = luaL_checkstring(L, 3);
}
if (player_idx == my_player_number)
{
script_play_message(param_is_string,msgtype_id,msg_id,filename);
}
return 0;
}
static int lua_Tutorial_flash_button(lua_State *L)
{
long button = -1;
if (lua_isnumber(L, 1))
{
button = luaL_checkinteger(L, 1);
}
else
{
const char* str = luaL_checkstring(L, 1);
static const struct NamedCommand *desc[4] = {room_desc, power_desc, trap_desc, door_desc};
static const short btn_group[4] = {GID_ROOM_PANE, GID_POWER_PANE, GID_TRAP_PANE, GID_DOOR_PANE};
for (int i = 0; i < 4; i++)
{
short id = get_rid(desc[i], str);
if (id >= 0)
{
button = get_button_designation(btn_group[i], id);
break;
}
}
}
if (button >= 0)
{
GameTurnDelta gameturns = luaL_checkinteger(L, 2);
gui_set_button_flashing(button,gameturns);
}
return 0;
}
static int lua_Display_countdown(lua_State *L)
{
PlayerNumber player = luaL_checkPlayerSingle(L, 1);
int timer = luaL_checkNamedCommand(L,2,timer_desc);
int target = luaL_checkinteger(L,3);
int clocktime = lua_toboolean(L,4);
game.script_timer_player = player;
game.script_timer_id = timer;
game.script_timer_limit = target;
game.timer_real = clocktime;
game.flags_gui |= GGUI_ScriptTimer;
return 0;
}
static int lua_Display_variable(lua_State *L)
{
PlayerNumber player = luaL_checkPlayerSingle(L, 1);
int variable = luaL_checkinteger(L,2);
int target = luaL_checkinteger(L,3);
game.script_variable_player = player;
game.script_value_type = variable;
game.script_value_id = target;
game.flags_gui |= GGUI_Variable;
return 0;
}
static int lua_Hide_variable(lua_State *L)
{
game.flags_gui &= ~GGUI_Variable;
return 0;
}
//Manipulating Map
static int lua_Reveal_map_location(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
TbMapLocation target = luaL_checkLocation(L, 2);
MapSubtlDelta range = luaL_checkinteger(L, 3);
SYNCDBG(0, "Revealing location type %u", target);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
MapSubtlCoord x = 0;
MapSubtlCoord y = 0;
find_map_location_coords(target, &x, &y, i, __func__);
if ((x == 0) && (y == 0))
{
WARNLOG("Can't decode location %u", target);
return 0;
}
if (range == -1)
{
struct CompoundCoordFilterParam iter_param;
iter_param.plyr_idx = i;
slabs_fill_iterate_from_slab(subtile_slab(x), subtile_slab(y), slabs_reveal_slab_and_corners, &iter_param);
} else
player_reveal_map_area(i, x, y,range,range);
}
return 0;
}
static int lua_Reveal_map_rect(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
MapSubtlCoord stl_x = luaL_checkstl_x(L, 2);
MapSubtlCoord stl_y = luaL_checkstl_y(L, 3);
MapSubtlDelta width = lua_tointeger(L,4);
MapSubtlDelta height = lua_tointeger(L,5);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
player_reveal_map_area(i,stl_x,stl_y,width,height);
}
return 0;
}
static int lua_Conceal_map_rect(lua_State *L)
{
struct PlayerRange player_range = luaL_checkPlayerRange(L, 1);
MapSubtlCoord stl_x = luaL_checkstl_x(L, 2);
MapSubtlCoord stl_y = luaL_checkstl_y(L, 3);
MapSubtlDelta width = lua_tointeger(L,4);
MapSubtlDelta height = lua_tointeger(L,5);
TbBool conceal_all = lua_toboolean(L,6);
for (PlayerNumber i = player_range.start_idx; i < player_range.end_idx; i++)
{
player_conceal_map_area(i,stl_x,stl_y,width,height,conceal_all);
}
return 0;
}
static int lua_Set_door(lua_State *L)
{
long doorAction = luaL_checkNamedCommand(L,1,locked_desc);
MapSlabCoord slb_x = luaL_checkslb_x(L,2);
MapSlabCoord slb_y = luaL_checkslb_y(L,3);
struct Thing* doortng = get_door_for_position(slab_subtile_center(slb_x), slab_subtile_center(slb_y));
if (!thing_is_invalid(doortng))
{
switch (doorAction)
{
case 0:
unlock_door(doortng);
break;
case 1:
lock_door(doortng);
break;
}
}
return 0;
}
static int lua_Add_heart_health(lua_State *L)
{
PlayerNumber plyr_idx = luaL_checkPlayerSingle(L,1);
HitPoints healthdelta = lua_tointeger(L,2);
TbBool warn_on_damage = lua_toboolean(L,3);
add_heart_health(plyr_idx,healthdelta,warn_on_damage);
return 0;
}
static int lua_Add_object_to_level(lua_State *L)
{
long obj_id = luaL_checkNamedCommand(L,1,object_desc);
TbMapLocation location = luaL_checkLocation(L, 2);
long arg = lua_tointeger(L,3);
PlayerNumber plr_idx = luaL_optPlayerSingle(L, 4);
short angle = lua_tointeger(L, 5);
struct Coord3d pos;
if (!get_coords_at_location(&pos, location,true))
{
return 0;
}
lua_pushThing(L,script_process_new_object(obj_id, pos.x.stl.num, pos.y.stl.num, arg, plr_idx,angle));
return 1;
}
static int lua_Add_object_to_level_at_pos(lua_State *L)
{
long obj_id = luaL_checkNamedCommand(L,1,object_desc);
MapSubtlCoord stl_x = luaL_checkstl_x(L, 2);
MapSubtlCoord stl_y = luaL_checkstl_y(L, 3);
long arg = lua_tointeger(L,4);
PlayerNumber plr_idx = luaL_optPlayerSingle(L, 5);
short angle = lua_tointeger(L, 6);
lua_pushThing(L,script_process_new_object(obj_id, stl_x, stl_y, arg, plr_idx,angle));
return 1;
}
static int lua_Add_effect_generator_to_level(lua_State *L)
{
ThingModel gen_id = luaL_checkNamedCommand(L,1,effectgen_desc);
TbMapLocation location = luaL_checkLocation(L, 2);
long range = luaL_checkinteger(L, 3);
lua_pushThing(L,script_process_new_effectgen(gen_id, location, range));
return 1;
}
static int lua_Place_door(lua_State *L)
{
PlayerNumber plyridx = luaL_checkPlayerSingle(L,1);
long doorkind = luaL_checkNamedCommand(L,2,door_desc);
MapSlabCoord slb_x = luaL_checkslb_x(L,3);
MapSlabCoord slb_y = luaL_checkslb_y(L,4);
TbBool locked = lua_toboolean(L,5);
TbBool free = lua_toboolean(L,6);
script_place_door(plyridx, doorkind, slb_x, slb_y, locked, free);
return 0;
}
static int lua_Place_trap(lua_State *L)
{
PlayerNumber plyridx = luaL_checkPlayerSingle(L,1);
long trapkind = luaL_checkNamedCommand(L,2,trap_desc);
MapSubtlCoord stl_x = luaL_checkstl_x(L,3);
MapSubtlCoord stl_y = luaL_checkstl_y(L,4);
TbBool free = lua_toboolean(L,5);
script_place_trap(plyridx, trapkind, stl_x, stl_y, free);
return 0;
}
//Manipulating Configs
static void set_configuration(lua_State *L, const struct NamedFieldSet* named_fields_set, const char* function_name)
{
if (lua_gettop(L) < 2)
{
luaL_error(L, "Not enough arguments for %s", function_name);
return;
}
if (!lua_isstring(L, 1))
{
luaL_error(L, "First argument must be a string for %s", function_name);
return;
}
if (!lua_isstring(L, 2))
{
luaL_error(L, "Second argument must be a string for %s", function_name);
return;
}
const char* id_str = lua_tostring(L, 1);
const char* property = lua_tostring(L, 2);
short id = get_id(named_fields_set->names, id_str);
if (id == -1)
{
char error_msg[256];
snprintf(error_msg, sizeof(error_msg), "Unknown %s, '%s'", named_fields_set->block_basename, id_str);
luaL_argerror(L, 1, error_msg);
return;
}
if (id > named_fields_set->max_count)
{
char error_msg[256];
snprintf(error_msg, sizeof(error_msg), "'%s%d' is out of range",named_fields_set->block_basename, id);
luaL_argerror(L, 1, error_msg);
return;
}
long property_id = get_named_field_id(named_fields_set->named_fields, property);
if (property_id == -1)
{
char error_msg[256];
snprintf(error_msg, sizeof(error_msg), "Expected a valid property name, got '%s'",property);
luaL_argerror(L, 1, error_msg);
return;
}
const struct NamedField* field = &named_fields_set->named_fields[property_id];
char concatenated_values[MAX_TEXT_LENGTH];
if (field->argnum == -1)
{
concatenated_values[0] = '\0';
for (int i = 3; lua_tostring(L, i) != NULL; i++) {
strncat(concatenated_values, lua_tostring(L, i), sizeof(concatenated_values) - strlen(concatenated_values) - 1);
if (lua_tostring(L, i + 1) != NULL) {
strncat(concatenated_values, " ", sizeof(concatenated_values) - strlen(concatenated_values) - 1);
}
}
int64_t value = parse_named_field_value(field, concatenated_values,named_fields_set,id,function_name,ccf_DuringLevel);
assign_named_field_value(&named_fields_set->named_fields[property_id],value,named_fields_set,id,function_name,ccf_DuringLevel);
}
else
{
int i = 0;
while (lua_tostring(L, i + 3) != NULL)
{
if( named_fields_set->named_fields[property_id + i].name == NULL ||
(strcmp(named_fields_set->named_fields[property_id + i].name, named_fields_set->named_fields[property_id].name) != 0))
{
char error_msg[256];
snprintf(error_msg, sizeof(error_msg), "more values then expected for property: '%s' '%s'", property, lua_tostring(L, i + 3));
luaL_argerror(L, 1, error_msg);
return;
}
int64_t value = parse_named_field_value(&named_fields_set->named_fields[property_id + i], lua_tostring(L, i + 3),named_fields_set,id,function_name,ccf_DuringLevel);
assign_named_field_value(&named_fields_set->named_fields[property_id + i],value,named_fields_set,id,function_name,ccf_DuringLevel);