-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapi_game_methods.cpp
1212 lines (1010 loc) · 28.5 KB
/
api_game_methods.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) 1996-2020 Freeciv21 and Freeciv contributors. This file is
part of Freeciv21. Freeciv21 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 3 of the
License, or (at your option) any later version. You should have received
a copy of the GNU General Public License along with Freeciv21. If not,
see https://www.gnu.org/licenses/.
*/
// utility
#include "deprecations.h"
#include "fcintl.h"
// common
#include "achievements.h"
#include "actions.h"
#include "calendar.h"
#include "citizens.h"
#include "culture.h"
#include "featured_text.h"
#include "game.h"
#include "government.h"
#include "improvement.h"
#include "map.h"
#include "movement.h"
#include "nation.h"
#include "research.h"
#include "tech.h"
#include "terrain.h"
#include "tile.h"
#include "unitlist.h"
#include "unittype.h"
/* common/scriptcore */
#include "luascript.h"
#include "api_game_methods.h"
/**
Return the current turn.
*/
int api_methods_game_turn(lua_State *L)
{
LUASCRIPT_CHECK_STATE(L, 0);
return game.info.turn;
}
/**
Return the current year.
*/
int api_methods_game_year(lua_State *L)
{
LUASCRIPT_CHECK_STATE(L, 0);
return game.info.year;
}
/**
Return the current year fragment.
*/
int api_methods_game_year_fragment(lua_State *L)
{
LUASCRIPT_CHECK_STATE(L, 0);
return game.info.fragment_count;
}
/**
Return the current year fragment.
*/
const char *api_methods_game_year_text(lua_State *L)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
return calendar_text();
}
/**
Return the current turn, as if real turns started from 0.
*/
int api_methods_game_turn_deprecated(lua_State *L)
{
LUASCRIPT_CHECK_STATE(L, 0);
qCWarning(deprecations_category,
"Deprecated: lua construct \"game:turn\", deprecated "
"since \"3.0\", used. "
"Use \"game:current_turn\" instead.");
if (game.info.turn > 0) {
return game.info.turn - 1;
}
return game.info.turn;
}
/**
Return name of the current ruleset.
*/
const char *api_methods_game_rulesetdir(lua_State *L)
{
Q_UNUSED(L)
return game.server.rulesetdir;
}
/**
Return name of the current ruleset.
*/
const char *api_methods_game_ruleset_name(lua_State *L)
{
Q_UNUSED(L)
return game.control.name;
}
/**
Return TRUE if pbuilding is a wonder.
*/
bool api_methods_building_type_is_wonder(lua_State *L,
Building_Type *pbuilding)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pbuilding, false);
return is_wonder(pbuilding);
}
/**
Return TRUE if pbuilding is a great wonder.
*/
bool api_methods_building_type_is_great_wonder(lua_State *L,
Building_Type *pbuilding)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pbuilding, false);
return is_great_wonder(pbuilding);
}
/**
Return TRUE if pbuilding is a small wonder.
*/
bool api_methods_building_type_is_small_wonder(lua_State *L,
Building_Type *pbuilding)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pbuilding, false);
return is_small_wonder(pbuilding);
}
/**
Return TRUE if pbuilding is a building.
*/
bool api_methods_building_type_is_improvement(lua_State *L,
Building_Type *pbuilding)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pbuilding, false);
return is_improvement(pbuilding);
}
/**
Return rule name for Building_Type
*/
const char *api_methods_building_type_rule_name(lua_State *L,
Building_Type *pbuilding)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pbuilding, nullptr);
return improvement_rule_name(pbuilding);
}
/**
Return translated name for Building_Type
*/
const char *
api_methods_building_type_name_translation(lua_State *L,
Building_Type *pbuilding)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pbuilding, nullptr);
return improvement_name_translation(pbuilding);
}
/**
Return TRUE iff city has building
*/
bool api_methods_city_has_building(lua_State *L, City *pcity,
Building_Type *building)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pcity, false);
LUASCRIPT_CHECK_ARG_NIL(L, building, 3, Building_Type, false);
return city_has_building(pcity, building);
}
/**
Return the square raduis of the city map.
*/
int api_methods_city_map_sq_radius(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pcity, 0);
return city_map_radius_sq_get(pcity);
}
/**
Return the size of the city.
*/
int api_methods_city_size_get(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, 1);
LUASCRIPT_CHECK_SELF(L, pcity, 1);
return city_size_get(pcity);
}
/**
Return the tile of the city.
*/
Tile *api_methods_city_tile_get(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pcity, nullptr);
return pcity->tile;
}
/**
How much city inspires partisans for a player.
*/
int api_methods_city_inspire_partisans(lua_State *L, City *self,
Player *inspirer)
{
Q_UNUSED(L)
bool inspired = false;
if (!game.info.citizen_nationality) {
if (self->original == inspirer) {
inspired = true;
}
} else {
if (game.info.citizen_partisans_pct > 0) {
if (is_server()) {
int own = citizens_nation_get(self, inspirer->slot);
int total = 0;
/* Not citizens_foreign_iterate() as city has already changed hands.
* old owner would be considered foreign and new owner not. */
citizens_iterate(self, pslot, nat) { total += nat; }
citizens_iterate_end;
if (total != 0
&& ((own * 100 / total) >= game.info.citizen_partisans_pct)) {
inspired = true;
}
}
// else is_client() -> don't consider inspired by default.
} else if (self->original == inspirer) {
inspired = true;
}
}
if (inspired) {
/* Cannot use get_city_bonus() as it would use city's current owner
* instead of inspirer. */
return get_target_bonus_effects(
nullptr, inspirer, nullptr, self, nullptr, city_tile(self), nullptr,
nullptr, nullptr, nullptr, nullptr, EFT_INSPIRE_PARTISANS);
}
return 0;
}
/**
How much culture city has?
*/
int api_methods_city_culture_get(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pcity, 0);
return city_culture(pcity);
}
/**
Return TRUE iff city happy
*/
bool api_methods_is_city_happy(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pcity, false);
// Note: if clients ever have virtual cities or sth, needs amending
return is_server() ? city_happy(pcity) : pcity->client.happy;
}
/**
Return TRUE iff city is unhappy
*/
bool api_methods_is_city_unhappy(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pcity, false);
// Note: if clients ever have virtual cities or sth, needs amending
return is_server() ? city_unhappy(pcity) : pcity->client.unhappy;
}
/**
Return TRUE iff city is celebrating
*/
bool api_methods_is_city_celebrating(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pcity, false);
return city_celebrating(pcity);
}
/**
Return TRUE iff city is government center
*/
bool api_methods_is_gov_center(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pcity, false);
return is_gov_center(pcity);
}
/**
Return TRUE if city is capital
*/
bool api_methods_is_capital(lua_State *L, City *pcity)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pcity, false);
return is_capital(pcity);
}
/**
Return rule name for Government
*/
const char *api_methods_government_rule_name(lua_State *L,
Government *pgovernment)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pgovernment, nullptr);
return government_rule_name(pgovernment);
}
/**
Return translated name for Government
*/
const char *api_methods_government_name_translation(lua_State *L,
Government *pgovernment)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pgovernment, nullptr);
return government_name_translation(pgovernment);
}
/**
Return rule name for Nation_Type
*/
const char *api_methods_nation_type_rule_name(lua_State *L,
Nation_Type *pnation)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pnation, nullptr);
return nation_rule_name(pnation);
}
/**
Return translated adjective for Nation_Type
*/
const char *api_methods_nation_type_name_translation(lua_State *L,
Nation_Type *pnation)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pnation, nullptr);
return nation_adjective_translation(pnation);
}
/**
Return translated plural noun for Nation_Type
*/
const char *api_methods_nation_type_plural_translation(lua_State *L,
Nation_Type *pnation)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pnation, nullptr);
return nation_plural_translation(pnation);
}
/**
Return gui type string of the controlling connection.
*/
const char *api_methods_player_controlling_gui(lua_State *L, Player *pplayer)
{
static bool warned = false;
if (!warned) {
qWarning() << "player:controlling_gui is deprecated";
warned = true;
}
return "Qt";
}
/**
Return TRUE iff player has wonder
*/
bool api_methods_player_has_wonder(lua_State *L, Player *pplayer,
Building_Type *building)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pplayer, false);
LUASCRIPT_CHECK_ARG_NIL(L, building, 3, Building_Type, false);
return wonder_is_built(pplayer, building);
}
/**
Return player number
*/
int api_methods_player_number(lua_State *L, Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, -1);
LUASCRIPT_CHECK_SELF(L, pplayer, -1);
return player_number(pplayer);
}
/**
Return the number of cities pplayer has.
*/
int api_methods_player_num_cities(lua_State *L, Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pplayer, 0);
return city_list_size(pplayer->cities);
}
/**
Return the number of units pplayer has.
*/
int api_methods_player_num_units(lua_State *L, Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pplayer, 0);
return unit_list_size(pplayer->units);
}
/**
Return gold for Player
*/
int api_methods_player_gold(lua_State *L, Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pplayer, 0);
return pplayer->economic.gold;
}
/**
Return TRUE if Player knows advance ptech.
*/
bool api_methods_player_knows_tech(lua_State *L, Player *pplayer,
Tech_Type *ptech)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pplayer, false);
LUASCRIPT_CHECK_ARG_NIL(L, ptech, 3, Tech_Type, false);
return research_invention_state(research_get(pplayer),
advance_number(ptech))
== TECH_KNOWN;
}
/**
How much culture player has?
*/
int api_methods_player_culture_get(lua_State *L, Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pplayer, 0);
return player_culture(pplayer);
}
/**
Does player have flag set?
*/
bool api_methods_player_has_flag(lua_State *L, Player *pplayer,
const char *flag)
{
enum plr_flag_id flag_val;
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pplayer, 0);
flag_val = plr_flag_id_by_name(flag, fc_strcasecmp);
if (plr_flag_id_is_valid(flag_val)) {
return player_has_flag(pplayer, flag_val);
}
return false;
}
/**
Return TRUE if players share research.
*/
bool api_methods_player_shares_research(lua_State *L, Player *pplayer,
Player *aplayer)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, pplayer, false);
LUASCRIPT_CHECK_ARG_NIL(L, aplayer, 3, Player, false);
return research_get(pplayer) == research_get(aplayer);
}
/**
Return name of the research group player belongs to.
*/
const char *api_methods_research_rule_name(lua_State *L, Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pplayer, nullptr);
return research_rule_name(research_get(pplayer));
}
/**
Return name of the research group player belongs to.
*/
const char *api_methods_research_name_translation(lua_State *L,
Player *pplayer)
{
static char buf[MAX_LEN_MSG];
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pplayer, nullptr);
(void) research_pretty_name(research_get(pplayer), buf, ARRAY_SIZE(buf));
return buf;
}
/**
Return list head for unit list for Player
*/
Unit_List_Link *api_methods_private_player_unit_list_head(lua_State *L,
Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pplayer, nullptr);
return unit_list_head(pplayer->units);
}
/**
Return list head for city list for Player
*/
City_List_Link *api_methods_private_player_city_list_head(lua_State *L,
Player *pplayer)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pplayer, nullptr);
return city_list_head(pplayer->cities);
}
/**
Return rule name for Tech_Type
*/
const char *api_methods_tech_type_rule_name(lua_State *L, Tech_Type *ptech)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, ptech, nullptr);
return advance_rule_name(ptech);
}
/**
Return translated name for Tech_Type
*/
const char *api_methods_tech_type_name_translation(lua_State *L,
Tech_Type *ptech)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, ptech, nullptr);
return advance_name_translation(ptech);
}
/**
Return rule name for Terrain
*/
const char *api_methods_terrain_rule_name(lua_State *L, Terrain *pterrain)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pterrain, nullptr);
return terrain_rule_name(pterrain);
}
/**
Return translated name for Terrain
*/
const char *api_methods_terrain_name_translation(lua_State *L,
Terrain *pterrain)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pterrain, nullptr);
return terrain_name_translation(pterrain);
}
/**
Return name of the terrain's class
*/
const char *api_methods_terrain_class_name(lua_State *L, Terrain *pterrain)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pterrain, nullptr);
return terrain_class_name(terrain_type_terrain_class(pterrain));
}
/**
Return rule name for Disaster
*/
const char *api_methods_disaster_rule_name(lua_State *L, Disaster *pdis)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pdis, nullptr);
return disaster_rule_name(pdis);
}
/**
Return translated name for Disaster
*/
const char *api_methods_disaster_name_translation(lua_State *L,
Disaster *pdis)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pdis, nullptr);
return disaster_name_translation(pdis);
}
/**
Return rule name for Achievement
*/
const char *api_methods_achievement_rule_name(lua_State *L,
Achievement *pach)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pach, nullptr);
return achievement_rule_name(pach);
}
/**
Return translated name for Achievement
*/
const char *api_methods_achievement_name_translation(lua_State *L,
Achievement *pach)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pach, nullptr);
return achievement_name_translation(pach);
}
/**
Return rule name for Action
*/
const char *api_methods_action_rule_name(lua_State *L, Action *pact)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pact, nullptr);
return action_id_rule_name(pact->id);
}
/**
Return translated name for Action
*/
const char *api_methods_action_name_translation(lua_State *L, Action *pact)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pact, nullptr);
return qUtf8Printable(action_id_name_translation(pact->id));
}
/**
Return the native x coordinate of the tile.
*/
int api_methods_tile_nat_x(lua_State *L, Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, -1);
LUASCRIPT_CHECK_SELF(L, ptile, -1);
return index_to_native_pos_x(tile_index(ptile));
}
/**
Return the native y coordinate of the tile.
*/
int api_methods_tile_nat_y(lua_State *L, Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, -1);
LUASCRIPT_CHECK_SELF(L, ptile, -1);
return index_to_native_pos_y(tile_index(ptile));
}
/**
Return the map x coordinate of the tile.
*/
int api_methods_tile_map_x(lua_State *L, Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, -1);
LUASCRIPT_CHECK_SELF(L, ptile, -1);
return index_to_map_pos_x(tile_index(ptile));
}
/**
Return the map y coordinate of the tile.
*/
int api_methods_tile_map_y(lua_State *L, Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, -1);
LUASCRIPT_CHECK_SELF(L, ptile, -1);
return index_to_map_pos_y(tile_index(ptile));
}
/**
Return City on ptile, else nullptr
*/
City *api_methods_tile_city(lua_State *L, Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, ptile, nullptr);
return tile_city(ptile);
}
/**
Return TRUE if there is a city inside the maximum city radius from ptile.
*/
bool api_methods_tile_city_exists_within_max_city_map(lua_State *L,
Tile *ptile,
bool may_be_on_center)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, ptile, false);
return city_exists_within_max_city_map(ptile, may_be_on_center);
}
/**
Return TRUE if there is a extra with rule name name on ptile.
If no name is specified return true if there is a extra on ptile.
*/
bool api_methods_tile_has_extra(lua_State *L, Tile *ptile, const char *name)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, ptile, false);
if (!name) {
extra_type_iterate(pextra)
{
if (tile_has_extra(ptile, pextra)) {
return true;
}
}
extra_type_iterate_end;
return false;
} else {
struct extra_type *pextra;
pextra = extra_type_by_rule_name(name);
return (nullptr != pextra && tile_has_extra(ptile, pextra));
}
}
/**
Return TRUE if there is a base with rule name name on ptile.
If no name is specified return true if there is any base on ptile.
*/
bool api_methods_tile_has_base(lua_State *L, Tile *ptile, const char *name)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, ptile, false);
if (!name) {
extra_type_by_cause_iterate(EC_BASE, pextra)
{
if (tile_has_extra(ptile, pextra)) {
return true;
}
}
extra_type_by_cause_iterate_end;
return false;
} else {
struct extra_type *pextra;
pextra = extra_type_by_rule_name(name);
return (nullptr != pextra && is_extra_caused_by(pextra, EC_BASE)
&& tile_has_extra(ptile, pextra));
}
}
/**
Return TRUE if there is a road with rule name name on ptile.
If no name is specified return true if there is any road on ptile.
*/
bool api_methods_tile_has_road(lua_State *L, Tile *ptile, const char *name)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, ptile, false);
if (!name) {
extra_type_by_cause_iterate(EC_ROAD, pextra)
{
if (tile_has_extra(ptile, pextra)) {
return true;
}
}
extra_type_by_cause_iterate_end;
return false;
} else {
struct extra_type *pextra;
pextra = extra_type_by_rule_name(name);
return (nullptr != pextra && is_extra_caused_by(pextra, EC_ROAD)
&& tile_has_extra(ptile, pextra));
}
}
/**
Is tile occupied by enemies
*/
bool api_methods_enemy_tile(lua_State *L, Tile *ptile, Player *against)
{
struct city *pcity;
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, ptile, false);
if (is_non_allied_unit_tile(ptile, against)) {
return true;
}
pcity = tile_city(ptile);
return pcity != nullptr && !pplayers_allied(against, city_owner(pcity));
}
/**
Return number of units on tile
*/
int api_methods_tile_num_units(lua_State *L, Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, ptile, 0);
return unit_list_size(ptile->units);
}
/**
Return list head for unit list for Tile
*/
Unit_List_Link *api_methods_private_tile_unit_list_head(lua_State *L,
Tile *ptile)
{
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, ptile, nullptr);
return unit_list_head(ptile->units);
}
/**
Return nth tile iteration index (for internal use)
Will return the next index, or an index < 0 when done
*/
int api_methods_private_tile_next_outward_index(lua_State *L, Tile *pstart,
int tindex, int max_dist)
{
int dx, dy;
int newx, newy;
int startx, starty;
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, pstart, 0);
if (tindex < 0) {
return 0;
}
index_to_map_pos(&startx, &starty, tile_index(pstart));
tindex++;
while (tindex < wld.map.num_iterate_outwards_indices) {
if (wld.map.iterate_outwards_indices[tindex].dist > max_dist) {
return -1;
}
dx = wld.map.iterate_outwards_indices[tindex].dx;
dy = wld.map.iterate_outwards_indices[tindex].dy;
newx = dx + startx;
newy = dy + starty;
if (!normalize_map_pos(&(wld.map), &newx, &newy)) {
tindex++;
continue;
}
return tindex;
}
return -1;
}
/**
Return tile for nth iteration index (for internal use)
*/
Tile *api_methods_private_tile_for_outward_index(lua_State *L, Tile *pstart,
int tindex)
{
int newx, newy;
LUASCRIPT_CHECK_STATE(L, nullptr);
LUASCRIPT_CHECK_SELF(L, pstart, nullptr);
LUASCRIPT_CHECK_ARG(
L, tindex >= 0 && tindex < wld.map.num_iterate_outwards_indices, 3,
"index out of bounds", nullptr);
index_to_map_pos(&newx, &newy, tile_index(pstart));
newx += wld.map.iterate_outwards_indices[tindex].dx;
newy += wld.map.iterate_outwards_indices[tindex].dy;
if (!normalize_map_pos(&(wld.map), &newx, &newy)) {
return nullptr;
}
return map_pos_to_tile(&(wld.map), newx, newy);
}
/**
Return squared distance between tiles 1 and 2
*/
int api_methods_tile_sq_distance(lua_State *L, Tile *ptile1, Tile *ptile2)
{
LUASCRIPT_CHECK_STATE(L, 0);
LUASCRIPT_CHECK_SELF(L, ptile1, 0);
LUASCRIPT_CHECK_ARG_NIL(L, ptile2, 3, Tile, 0);
return sq_map_distance(ptile1, ptile2);
}
/**
Can punit found a city on its tile?
*/
bool api_methods_unit_city_can_be_built_here(lua_State *L, Unit *punit)
{
LUASCRIPT_CHECK_STATE(L, false);
LUASCRIPT_CHECK_SELF(L, punit, false);
return city_can_be_built_here(unit_tile(punit), punit);
}
/**
Return the tile of the unit.
*/