/
tolua_game.pkg
821 lines (707 loc) · 24.1 KB
/
tolua_game.pkg
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
/*****************************************************************************
Freeciv - Copyright (C) 2005 - The Freeciv Project
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, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*****************************************************************************/
/*****************************************************************************
ADVERTISEMENT: do not attempt to change the name of the API functions.
They may be in use in Lua scripts in savefiles, so once released, the
name and signature cannot change shape even in new major versions of
Freeciv, until the relevant save format version can no longer be loaded.
If you really like to change a function name, be sure to keep also the
old one running.
*****************************************************************************/
$#ifdef HAVE_CONFIG_H
$#include <fc_config.h>
$#endif
/* common/scriptcore */
$#include "api_common_utilities.h"
$#include "api_game_effects.h"
$#include "api_game_find.h"
$#include "api_game_methods.h"
$#include "luascript_types.h"
/* Classes. */
struct Player {
const char *name;
Nation_Type *nation;
Government *government;
bool is_alive;
};
struct City {
const char *name;
Player *owner;
Player *original;
const int id;
};
struct Counter
{
};
struct Connection {
const int id;
};
struct Unit {
Unit_Type *utype;
Player *owner;
/* This used to be @ homecity_id, but it does not work with toluaxx. */
int homecity;
int veteran;
const int id;
};
struct Tile {
Terrain *terrain;
Player *owner;
const int index @ id;
};
struct Government {
const int item_number @ id;
};
struct Nation_Type {
const int item_number @ id;
};
struct Building_Type {
int build_cost;
const int item_number @ id;
};
struct Unit_Type {
int build_cost;
Unit_Type *obsoleted_by;
const int item_number @ id;
};
struct Tech_Type {
const int item_number @ id;
const int cost @ cost_base;
};
struct Terrain {
const int item_number @ id;
};
struct Disaster {
const int id;
};
struct Achievement {
const int id;
};
struct Action {
const int id;
};
struct Direction {
/* nothing here, it's enum */
};
struct Unit_List_Link {
};
struct City_List_Link {
};
/* Declaring all fields as const, readonly */
struct Game_Info {
const int base_tech_cost;
const int min_tech_cost;
const int tech_leak_pct;
const bool tech_steal_allow_holes;
const bool tech_trade_allow_holes;
const bool tech_trade_loss_allow_holes;
const bool tech_parasite_allow_holes;
const bool tech_loss_allow_holes;
const int sciencebox;
const char tech_cost_style @ tech_cost_style_id;
const char tech_leakage @ tech_leakage_style_id;
};
/* Module Game */
$#define game_info_substructure game.info
module game {
extern Game_Info game_info_substructure @ info;
int api_methods_game_turn
@ current_turn (lua_State *L);
/* Deprecated. Use current_turn() to get the real turn number */
int api_methods_game_turn_deprecated
@ turn (lua_State *L);
int api_methods_game_year
@ current_year (lua_State *L);
int api_methods_game_year_fragment
@ current_fragment (lua_State *L);
const char *api_methods_game_year_text
@ current_year_text (lua_State *L);
const char *api_methods_game_rulesetdir
@ rulesetdir (lua_State *L);
const char *api_methods_game_ruleset_name
@ ruleset_name (lua_State *L);
}
module Counter {
const char *api_methods_counter_rule_name @ rule_name (lua_State *L, Counter *c);
const char *api_methods_counter_name_translation @ name_translation (lua_State *L, Counter *c);
int api_methods_counter_city_get @ get (lua_State *L, Counter *c, City *city);
}
/* Module Player. */
module Player {
module properties {
int api_methods_player_number
@ id (lua_State *L, Player *self);
int api_methods_player_bulbs
@ bulbs (lua_State *L, Player *pplayer);
}
const char *api_methods_player_controlling_gui
@ controlling_gui (lua_State *L, Player *self);
int api_methods_player_num_cities
@ num_cities (lua_State *L, Player *self);
int api_methods_player_num_units
@ num_units (lua_State *L, Player *self);
int api_methods_player_future
@ num_future_techs (lua_State *L, Player *pplayer);
bool api_methods_player_has_wonder
@ has_wonder (lua_State *L, Player *self, Building_Type *building);
int api_methods_player_gold
@ gold (lua_State *L, Player *self);
int api_methods_player_infrapoints
@ infrapoints (lua_State *L, Player *self);
bool api_methods_player_knows_tech
@ knows_tech (lua_State *L, Player *self, Tech_Type *ptech);
bool api_method_player_can_research
@ can_research (lua_State *L, Player *pplayer, Tech_Type *ptech);
int api_methods_player_tech_cost
@ tech_cost (lua_State *L, Player *pplayer, Tech_Type *ptech);
lua_Object api_methods_player_researching
@ researching (lua_State *L, Player *pplayer);
int api_methods_player_research_cost
@ researching_cost (lua_State *L, Player *pplayer);
bool api_methods_player_shares_research
@ shares_research (lua_State *L, Player *self, Player *other);
const char *api_methods_research_rule_name
@ research_rule_name (lua_State *L, Player *self);
const char *api_methods_research_name_translation
@ research_name_translation (lua_State *L, Player *self);
int api_methods_player_culture_get
@ culture(lua_State *L, Player *self);
bool api_methods_player_has_flag
@ has_flag (lua_State *L, Player *self, const char *flag);
Unit_Type *api_methods_player_can_upgrade
@ can_upgrade(lua_State *L, Player *pplayer, Unit_Type *utype);
bool api_methods_player_can_build_impr_direct
@ can_build_direct (lua_State *L, Player *pplayer, Building_Type *itype);
bool api_methods_player_can_build_unit_direct
@ can_build_direct (lua_State *L, Player *pplayer, Unit_Type *utype);
}
module methods_private {
lua_Object api_methods_private_list_players
@ list_players (lua_State *L);
module Player {
Unit_List_Link *api_methods_private_player_unit_list_head
@ unit_list_head (lua_State *L, Player *self);
City_List_Link *api_methods_private_player_city_list_head
@ city_list_head (lua_State *L, Player *self);
}
}
$[
-- Player methods
function Player:is_human()
return not self.has_flag(self, "AI");
end
function Player:exists()
return true
end
$]
/* Module City. */
module City {
module properties {
int api_methods_city_size_get
@ size(lua_State *L, City *self);
Tile *api_methods_city_tile_get
@ tile(lua_State *L, City *self);
}
bool api_methods_city_has_building
@ has_building(lua_State *L, City *self, Building_Type *building);
int api_methods_city_map_sq_radius
@ map_sq_radius(lua_State *L, City *self);
int api_methods_city_inspire_partisans
@ inspire_partisans(lua_State *L, City *self, Player *inspirer);
int api_methods_city_culture_get
@ culture(lua_State *L, City *self);
bool api_methods_is_city_happy
@ is_happy(lua_State *L, City *self);
bool api_methods_is_city_unhappy
@ is_unhappy(lua_State *L, City *self);
bool api_methods_is_city_celebrating
@ is_celebrating(lua_State *L, City *self);
bool api_methods_is_gov_center
@ is_gov_center(lua_State *L, City *self);
bool api_methods_is_capital
@ is_capital(lua_State *L, City *self);
bool api_methods_is_primary_capital
@ is_primary_capital(lua_State *L, City *self);
const char *api_methods_city_link
@ link_text(lua_State *L, City *self);
const char *api_methods_city_tile_link
@ tile_link_text(lua_State *L, City *self);
}
$[
-- City methods.
function City:exists()
return true
end
$]
/* Module Unit. */
module Unit {
module properties {
Tile *api_methods_unit_tile_get
@ tile(lua_State *L, Unit *self);
}
Unit *api_methods_unit_transporter
@ transporter (lua_State *L, Unit *self);
bool api_methods_unit_city_can_be_built_here
@ is_on_possible_city_tile (lua_State *L, Unit *self);
bool api_methods_unit_can_upgrade
@ can_upgrade (lua_State *L, Unit *punit, bool is_free = TRUE);
const char *api_methods_unit_transform_problem
@ transform_problem (lua_State *L, Unit *punit, Unit_Type *ptype);
Direction *api_methods_unit_orientation_get
@ facing(lua_State *L, Unit *self);
const char *api_methods_unit_link
@ link_text(lua_State *L, Unit *self);
const char *api_methods_unit_tile_link
@ tile_link_text(lua_State *L, Unit *self);
}
module methods_private {
module Unit {
Unit_List_Link *api_methods_private_unit_cargo_list_head
@ cargo_list_head (lua_State *L, Unit *self);
}
}
$[
-- Unit methods.
function Unit:exists()
return true
end
function Unit:get_homecity()
return find.city(self.owner, self.homecity)
end
$]
/* Module Tile. */
module Tile {
module properties {
int api_methods_tile_nat_x
@ nat_x (lua_State *L, Tile *self);
int api_methods_tile_nat_y
@ nat_y (lua_State *L, Tile *self);
int api_methods_tile_map_x
@ x (lua_State *L, Tile *self);
int api_methods_tile_map_y
@ y (lua_State *L, Tile *self);
}
City *api_methods_tile_city
@ city (lua_State *L, Tile *self);
bool api_methods_tile_city_exists_within_max_city_map
@ city_exists_within_max_city_map (lua_State *L, Tile *self, bool center);
bool api_methods_tile_has_extra
@ has_extra(lua_State *L, Tile *self, const char *name);
bool api_methods_tile_has_base
@ has_base(lua_State *L, Tile *self, const char *name);
bool api_methods_tile_has_road
@ has_road(lua_State *L, Tile *self, const char *name);
Player *api_methods_tile_extra_owner
@ extra_owner(lua_State *L, Tile *self, const char *extra_name);
bool api_methods_enemy_tile
@ is_enemy(lua_State *L, Tile *self, Player *against);
int api_methods_tile_num_units
@ num_units (lua_State *L, Tile *self);
int api_methods_tile_sq_distance
@ sq_distance (lua_State *L, Tile *self, Tile *other);
const char *api_methods_tile_link
@ link_text(lua_State *L, Tile *self);
}
$[
-- ***************************************************************************
-- Deprecated. New one is Tile:city_exists_within_max_city_map().
-- ***************************************************************************
function Tile:city_exists_within_city_radius(center)
log.deprecation_warning("city_exists_within_city_radius()", "city_exists_within_max_city_map()",
"2.3");
return self:city_exists_within_max_city_map(center)
end
$]
module methods_private {
module Tile {
int api_methods_private_tile_next_outward_index
@ next_outward_index (lua_State *L, Tile *pcenter, int tindex,
int max_dist);
Tile *api_methods_private_tile_for_outward_index
@ tile_for_outward_index (lua_State *L, Tile *pcenter, int tindex);
Unit_List_Link *api_methods_private_tile_unit_list_head
@ unit_list_head (lua_State *L, Tile *self);
}
}
/* Module Government. */
module Government {
const char *api_methods_government_rule_name
@ rule_name (lua_State *L, Government *self);
const char *api_methods_government_name_translation
@ name_translation (lua_State *L, Government *self);
}
/* Module Nation_Type. */
module Nation_Type {
const char *api_methods_nation_type_rule_name
@ rule_name (lua_State *L, Nation_Type *self);
const char *api_methods_nation_type_name_translation
@ name_translation (lua_State *L, Nation_Type *self);
const char *api_methods_nation_type_plural_translation
@ plural_translation (lua_State *L, Nation_Type *self);
}
/* Module Building_Type. */
module Building_Type {
bool api_methods_building_type_is_wonder
@ is_wonder (lua_State *L, Building_Type *self);
bool api_methods_building_type_is_great_wonder
@ is_great_wonder (lua_State *L, Building_Type *self);
bool api_methods_building_type_is_small_wonder
@ is_small_wonder (lua_State *L, Building_Type *self);
bool api_methods_building_type_is_improvement
@ is_improvement (lua_State *L, Building_Type *self);
const char *api_methods_building_type_rule_name
@ rule_name (lua_State *L, Building_Type *self);
const char *api_methods_building_type_name_translation
@ name_translation (lua_State *L, Building_Type *self);
}
$[
-- Building_Type methods.
function Building_Type:build_shield_cost()
return self.build_cost
end
$]
/* Module Unit_Type. */
module Unit_Type {
bool api_methods_unit_type_has_flag
@ has_flag (lua_State *L, Unit_Type *self, const char *flag);
bool api_methods_unit_type_has_role
@ has_role (lua_State *L, Unit_Type *self, const char *role);
const char *api_methods_unit_type_rule_name
@ rule_name (lua_State *L, Unit_Type *self);
const char *api_methods_unit_type_name_translation
@ name_translation (lua_State *L, Unit_Type *self);
bool api_methods_unit_type_can_exist_at_tile
@ can_exist_at_tile(lua_State *L, Unit_Type *self, Tile *ptile);
}
$[
-- Unit_Type methods.
function Unit_Type:build_shield_cost()
return self.build_cost
end
$]
/* Module Tech_Type. */
module Tech_Type {
const char *api_methods_tech_type_rule_name
@ rule_name (lua_State *L, Tech_Type *self);
const char *api_methods_tech_type_name_translation
@ name_translation (lua_State *L, Tech_Type *self);
}
$[
local ptcost = Player.tech_cost
-- Unpersonalized tech cost method (without "Tech_Cost_Factor" effect)
function Tech_Type:cost()
return ptcost(nil, self)
end
$]
/* Module Terrain. */
module Terrain {
const char *api_methods_terrain_rule_name
@ rule_name (lua_State *L, Terrain *self);
const char *api_methods_terrain_name_translation
@ name_translation (lua_State *L, Terrain *self);
const char *api_methods_terrain_class_name
@ class_name (lua_State *L, Terrain *self);
}
/* Module Disaster. */
module Disaster {
const char *api_methods_disaster_rule_name
@ rule_name (lua_State *L, Disaster *self);
const char *api_methods_disaster_name_translation
@ name_translation (lua_State *L, Disaster *self);
}
/* Module Achievement. */
module Achievement {
const char *api_methods_achievement_rule_name
@ rule_name (lua_State *L, Achievement *self);
const char *api_methods_achievement_name_translation
@ name_translation (lua_State *L, Achievement *self);
}
/* Module Action. */
module Action {
const char *api_methods_action_rule_name
@ rule_name (lua_State *L, Action *self);
const char *api_methods_action_name_translation
@ name_translation (lua_State *L, Action *self);
const char *api_methods_action_target_kind
@ target_kind (lua_State *L, Action *self);
}
/* Module Unit_List_Link. */
module Unit_List_Link {
Unit *api_methods_unit_list_link_data
@ data (lua_State *L, Unit_List_Link *self);
Unit_List_Link *api_methods_unit_list_next_link
@ next (lua_State *L, Unit_List_Link *self);
}
/* Module City_List_Link. */
module City_List_Link {
City *api_methods_city_list_link_data
@ data (lua_State *L, City_List_Link *self);
City_List_Link *api_methods_city_list_next_link
@ next (lua_State *L, City_List_Link *self);
}
/* Module find. */
module find {
Counter *api_find_counter_by_name
@ counter (lua_State *L, const char *name);
Counter *api_find_counter
@ counter (lua_State *L, int counter_id);
Player *api_find_player_by_name
@ player (lua_State *L, const char *name);
Player *api_find_player
@ player (lua_State *L, int player_id);
City *api_find_city
@ city (lua_State *L, Player *pplayer, int city_id);
Unit *api_find_unit
@ unit (lua_State *L, Player *pplayer, int unit_id);
Unit *api_find_transport_unit
@ transport_unit (lua_State *L, Player *pplayer, Unit_Type *ptype,
Tile *ptile);
Tile *api_find_tile
@ tile (lua_State *L, int nat_x, int nat_y);
Tile *api_find_tile_by_index
@ tile (lua_State *L, int tindex);
/* NOTE: For overloading to work correctly, the string function
* must be before the integer function for each case below. */
Government *api_find_government_by_name
@ government (lua_State *L, const char *name_orig);
Government *api_find_government
@ government (lua_State *L, int government_id);
Nation_Type *api_find_nation_type_by_name
@ nation_type (lua_State *L, const char *name_orig);
Nation_Type *api_find_nation_type
@ nation_type (lua_State *L, int nation_type_id);
Action *api_find_action_by_name
@ action (lua_State *L, const char *name_orig);
Action *api_find_action
@ action (lua_State *L, int action_type_id);
Building_Type *api_find_building_type_by_name
@ building_type (lua_State *L, const char *name_orig);
Building_Type *api_find_building_type
@ building_type (lua_State *L, int building_type_id);
Unit_Type *api_find_unit_type_by_name
@ unit_type (lua_State *L, const char *name_orig);
Unit_Type *api_find_unit_type
@ unit_type (lua_State *L, int unit_type_id);
Unit_Type *api_find_role_unit_type
@ role_unit_type (lua_State *L, const char *role_name, Player *pplayer);
Tech_Type *api_find_tech_type_by_name
@ tech_type (lua_State *L, const char *name_orig);
Tech_Type *api_find_tech_type
@ tech_type (lua_State *L, int tech_type_id);
Terrain *api_find_terrain_by_name
@ terrain (lua_State *L, const char *name_orig);
Terrain *api_find_terrain
@ terrain (lua_State *L, int terrain_id);
Achievement *api_find_achievement_by_name
@ achievement (lua_State *L, const char *name_orig);
Achievement *api_find_achievement
@ achievement (lua_State *L, int achievement_id);
Disaster *api_find_disaster_by_name
@ achievement (lua_State *L, const char *name_orig);
Disaster *api_find_disaster
@ disaster (lua_State *L, int disaster_id);
Direction *api_utilities_str2dir
@ direction (lua_State *L, const char *str);
Direction *api_find_direction
@ direction (lua_State *L, int id);
Action *api_find_action_type_by_name
@ action (lua_State *L, const char *name);
Action *api_find_action_type_by_id
@ action (lua_State *L, int id);
Nonexistent *api_find_nonexistent
@ nonexistent (lua_State *L);
}
$[
local game_info = game.info
function find.game_info()
return game_info
end
$]
module E {
/* Notify events module is exported by api_specenum */
}
/* Effects module */
module effects {
int api_effects_world_bonus
@ world_bonus (lua_State *L, const char *effect_type);
int api_effects_player_bonus
@ player_bonus (lua_State *L, Player *pplayer, const char *effect_type);
int api_effects_city_bonus
@ city_bonus (lua_State *L, City *pcity, const char *effect_type);
int api_effects_unit_bonus
@ unit_bonus(lua_State *L, Unit *punit, Player *other_player,
const char *effect_type);
int api_effects_tile_bonus
@ tile_bonus(lua_State *L, Tile *ptile, City *pcity,
const char *output_id, const char *effect_type);
int api_effects_unit_vs_tile_bonus
@ unit_vs_tile_bonus(lua_State *L, Unit *punit, Tile *ptile,
const char *effect_type);
}
/* Direction module */
module Direction {
module properties {
int api_utilities_direction_id
@ id (lua_State *L, Direction dir);
const char *api_utilities_dir2str
@ name (lua_State *L, Direction dir);
bool api_utilities_direction_is_cardinal
@ cardinal(lua_State *L, Direction self);
}
}
module direction {
Direction *api_utilities_str2dir
@ str2dir(lua_State *L, const char *str);
Direction *api_utilities_dir_ccw
@ next_ccw(lua_State *L, Direction self);
Direction *api_utilities_dir_cw
@ next_cw(lua_State *L, Direction self);
Direction *api_utilities_opposite_dir
@ opposite(lua_State *L, Direction self);
}
$[
-- **************************************************************************
-- Convert direction describing text to direction
-- **************************************************************************
function str2direction(str)
return direction.str2dir(str)
end
-- Object-oriented way to call direction functions
Direction.properties.next_ccw = direction.next_ccw
Direction.properties.next_cw = direction.next_cw
Direction.properties.opposite = direction.opposite
$]
module Game_Info {
}
$[
-- Getting enums from game.info as rule names
-- The tables are defined and filled in api_game_specenum.c
local tcs, tls = TECH_COST, TECH_LEAKAGE
-- Remove the tables from _G, likely no more use for them
TECH_COST, TECH_LEAKAGE = nil, nil
Game_Info.properties = {
tech_cost_style = function(self)
return tcs[self.tech_cost_style_id];
end,
tech_leakage_style = function(self)
return tls[self.tech_leakage_style_id];
end
}
-- ***************************************************************************
-- Player and Tile: cities_iterate and units_iterate methods
-- ***************************************************************************
do
local private = methods_private
-- Iterate over the values of 'array' in order:
-- array[1], array[2], array[3], etc.
local function value_iterator(array)
local i = 0
local function iterator()
i = i + 1
return array[i]
end
return iterator
end
-- use a copy of the list for safe iteration
local function safe_iterate_list(link)
local objs = {}
while link do
objs[#objs + 1] = link:data()
link = link:next()
end
return value_iterator(objs)
end
-- Safe iteration over all units that belong to Player
function Player:units_iterate()
return safe_iterate_list(private.Player.unit_list_head(self))
end
-- Safe iteration over all cities that belong to Player
function Player:cities_iterate()
return safe_iterate_list(private.Player.city_list_head(self))
end
-- Safe iteration over the units on Tile
function Tile:units_iterate()
return safe_iterate_list(private.Tile.unit_list_head(self))
end
-- Safe iteration over the units transported by Unit
function Unit:cargo_iterate()
return safe_iterate_list(private.Unit.cargo_list_head(self))
end
end
-- ***************************************************************************
-- Tile: square_iterate, circle_iterate
-- ***************************************************************************
do
local next_outward_index = methods_private.Tile.next_outward_index
local tile_for_outward_index = methods_private.Tile.tile_for_outward_index
-- iterate over tiles at distance 'radius'
function Tile:square_iterate(radius)
local index = -1
local function iterator()
index = next_outward_index(self, index, radius)
if index < 0 then
return nil
else
return tile_for_outward_index(self, index)
end
end
return iterator
end
-- iterate over tiles at squared distance 'sq_radius'
function Tile:circle_iterate(sq_radius)
local cr_radius = math.floor(math.sqrt(sq_radius))
local sq_iter = self:square_iterate(cr_radius)
local function iterator()
local tile = nil
repeat
tile = sq_iter()
until not tile or self:sq_distance(tile) <= sq_radius
return tile
end
return iterator
end
end
$]
$[
-- ***************************************************************************
-- Iteration constructs for game-global objects
-- ***************************************************************************
do
-- iterate over the values returned by lookup
-- until nil is returned:
-- lookup(0), lookup(1), lookup(2), etc
local function index_iterate(lookup)
local index = -1
local function iterator()
index = index + 1
return lookup(index)
end
return iterator
end
-- Iterate over all players of the game (note: indices may have gaps)
local listp = methods_private.list_players
function players_iterate()
local plist = listp()
local index = 0
return function()
index = index + 1
return plist[index]
end
end
-- Iterate over all tiles of the game
function whole_map_iterate()
return index_iterate(find.tile)
end
-- NOTE: Identical further definitions can be made for
-- governments, tech_types, building_types etc
end
$]