1 /*****************************************************************************
2  Freeciv - Copyright (C) 2005 - The Freeciv Project
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2, or (at your option)
6    any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 *****************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17 
18 /* common */
19 #include "achievements.h"
20 #include "actions.h"
21 #include "citizens.h"
22 #include "culture.h"
23 #include "game.h"
24 #include "government.h"
25 #include "improvement.h"
26 #include "map.h"
27 #include "movement.h"
28 #include "nation.h"
29 #include "research.h"
30 #include "tech.h"
31 #include "terrain.h"
32 #include "tile.h"
33 #include "unitlist.h"
34 #include "unittype.h"
35 
36 /* common/scriptcore */
37 #include "luascript.h"
38 
39 #include "api_game_methods.h"
40 
41 /*****************************************************************************
42   Return the current turn.
43 *****************************************************************************/
api_methods_game_turn(lua_State * L)44 int api_methods_game_turn(lua_State *L)
45 {
46   LUASCRIPT_CHECK_STATE(L, 0);
47 
48   return game.info.turn;
49 }
50 
51 /*****************************************************************************
52   Return TRUE if pbuilding is a wonder.
53 *****************************************************************************/
api_methods_building_type_is_wonder(lua_State * L,Building_Type * pbuilding)54 bool api_methods_building_type_is_wonder(lua_State *L,
55                                          Building_Type *pbuilding)
56 {
57   LUASCRIPT_CHECK_STATE(L, FALSE);
58   LUASCRIPT_CHECK_SELF(L, pbuilding, FALSE);
59 
60   return is_wonder(pbuilding);
61 }
62 
63 /*****************************************************************************
64   Return TRUE if pbuilding is a great wonder.
65 *****************************************************************************/
api_methods_building_type_is_great_wonder(lua_State * L,Building_Type * pbuilding)66 bool api_methods_building_type_is_great_wonder(lua_State *L,
67                                                Building_Type *pbuilding)
68 {
69   LUASCRIPT_CHECK_STATE(L, FALSE);
70   LUASCRIPT_CHECK_SELF(L, pbuilding, FALSE);
71 
72   return is_great_wonder(pbuilding);
73 }
74 
75 /*****************************************************************************
76   Return TRUE if pbuilding is a small wonder.
77 *****************************************************************************/
api_methods_building_type_is_small_wonder(lua_State * L,Building_Type * pbuilding)78 bool api_methods_building_type_is_small_wonder(lua_State *L,
79                                                Building_Type *pbuilding)
80 {
81   LUASCRIPT_CHECK_STATE(L, FALSE);
82   LUASCRIPT_CHECK_SELF(L, pbuilding, FALSE);
83 
84   return is_small_wonder(pbuilding);
85 }
86 
87 /*****************************************************************************
88   Return TRUE if pbuilding is a building.
89 *****************************************************************************/
api_methods_building_type_is_improvement(lua_State * L,Building_Type * pbuilding)90 bool api_methods_building_type_is_improvement(lua_State *L,
91                                               Building_Type *pbuilding)
92 {
93   LUASCRIPT_CHECK_STATE(L, FALSE);
94   LUASCRIPT_CHECK_SELF(L, pbuilding, FALSE);
95 
96   return is_improvement(pbuilding);
97 }
98 
99 /*****************************************************************************
100   Return rule name for Building_Type
101 *****************************************************************************/
api_methods_building_type_rule_name(lua_State * L,Building_Type * pbuilding)102 const char *api_methods_building_type_rule_name(lua_State *L,
103                                                 Building_Type *pbuilding)
104 {
105   LUASCRIPT_CHECK_STATE(L, NULL);
106   LUASCRIPT_CHECK_SELF(L, pbuilding, NULL);
107 
108   return improvement_rule_name(pbuilding);
109 }
110 
111 /*****************************************************************************
112   Return translated name for Building_Type
113 *****************************************************************************/
114 const char
api_methods_building_type_name_translation(lua_State * L,Building_Type * pbuilding)115   *api_methods_building_type_name_translation(lua_State *L,
116                                               Building_Type *pbuilding)
117 {
118   LUASCRIPT_CHECK_STATE(L, NULL);
119   LUASCRIPT_CHECK_SELF(L, pbuilding, NULL);
120 
121   return improvement_name_translation(pbuilding);
122 }
123 
124 /*****************************************************************************
125   Return TRUE iff city has building
126 *****************************************************************************/
api_methods_city_has_building(lua_State * L,City * pcity,Building_Type * building)127 bool api_methods_city_has_building(lua_State *L, City *pcity,
128                                    Building_Type *building)
129 {
130   LUASCRIPT_CHECK_STATE(L, FALSE);
131   LUASCRIPT_CHECK_SELF(L, pcity, FALSE);
132   LUASCRIPT_CHECK_ARG_NIL(L, building, 3, Building_Type, FALSE);
133 
134   return city_has_building(pcity, building);
135 }
136 
137 /*****************************************************************************
138   Return the square raduis of the city map.
139 *****************************************************************************/
api_methods_city_map_sq_radius(lua_State * L,City * pcity)140 int api_methods_city_map_sq_radius(lua_State *L, City *pcity)
141 {
142   LUASCRIPT_CHECK_STATE(L, 0);
143   LUASCRIPT_CHECK_SELF(L, pcity, 0);
144 
145   return city_map_radius_sq_get(pcity);
146 }
147 
148 /**************************************************************************
149   Return the size of the city.
150 **************************************************************************/
api_methods_city_size_get(lua_State * L,City * pcity)151 int api_methods_city_size_get(lua_State *L, City *pcity)
152 {
153   LUASCRIPT_CHECK_STATE(L, 1);
154   LUASCRIPT_CHECK_SELF(L, pcity, 1);
155 
156   return city_size_get(pcity);
157 }
158 
159 /**************************************************************************
160   Return the tile of the city.
161 **************************************************************************/
api_methods_city_tile_get(lua_State * L,City * pcity)162 Tile *api_methods_city_tile_get(lua_State *L, City *pcity)
163 {
164   LUASCRIPT_CHECK_STATE(L, NULL);
165   LUASCRIPT_CHECK_SELF(L, pcity, NULL);
166 
167   return pcity->tile;
168 }
169 
170 /**************************************************************************
171   How much city inspires partisans for a player.
172 **************************************************************************/
api_methods_city_inspire_partisans(lua_State * L,City * self,Player * inspirer)173 int api_methods_city_inspire_partisans(lua_State *L, City *self, Player *inspirer)
174 {
175   bool inspired = FALSE;
176 
177   if (!game.info.citizen_nationality) {
178     if (self->original == inspirer) {
179       inspired = TRUE;
180     }
181   } else {
182     if (game.info.citizen_partisans_pct > 0) {
183       if (is_server()) {
184         int own = citizens_nation_get(self, inspirer->slot);
185         int total = 0;
186 
187         /* Not citizens_foreign_iterate() as city has already changed hands.
188          * old owner would be considered foreign and new owner not. */
189         citizens_iterate(self, pslot, nat) {
190           total += nat;
191         } citizens_iterate_end;
192 
193         if ((own * 100 / total) >= game.info.citizen_partisans_pct) {
194           inspired = TRUE;
195         }
196       }
197       /* else is_client() -> don't consider inspired by default. */
198     } else if (self->original == inspirer) {
199       inspired = TRUE;
200     }
201   }
202 
203   if (inspired) {
204     /* Cannot use get_city_bonus() as it would use city's current owner
205      * instead of inspirer. */
206     return get_target_bonus_effects(NULL, inspirer, NULL, self, NULL,
207                                     city_tile(self), NULL, NULL, NULL,
208                                     NULL, EFT_INSPIRE_PARTISANS);
209   }
210 
211   return 0;
212 }
213 
214 /**************************************************************************
215   How much culture city has?
216 **************************************************************************/
api_methods_city_culture_get(lua_State * L,City * pcity)217 int api_methods_city_culture_get(lua_State *L, City *pcity)
218 {
219   LUASCRIPT_CHECK_STATE(L, 0);
220   LUASCRIPT_CHECK_SELF(L, pcity, 0);
221 
222   return city_culture(pcity);
223 }
224 
225 /*****************************************************************************
226   Return TRUE iff city happy
227 *****************************************************************************/
api_methods_is_city_happy(lua_State * L,City * pcity)228 bool api_methods_is_city_happy(lua_State *L, City *pcity)
229 {
230   LUASCRIPT_CHECK_STATE(L, FALSE);
231   LUASCRIPT_CHECK_SELF(L, pcity, FALSE);
232 
233   /* Note: if clients ever have virtual cities or sth, needs amending */
234   return is_server() ? city_happy(pcity) : pcity->client.happy;
235 }
236 
237 /*****************************************************************************
238   Return TRUE iff city is unhappy
239 *****************************************************************************/
api_methods_is_city_unhappy(lua_State * L,City * pcity)240 bool api_methods_is_city_unhappy(lua_State *L, City *pcity)
241 {
242   LUASCRIPT_CHECK_STATE(L, FALSE);
243   LUASCRIPT_CHECK_SELF(L, pcity, FALSE);
244 
245   /* Note: if clients ever have virtual cities or sth, needs amending */
246   return is_server() ? city_unhappy(pcity) : pcity->client.unhappy;
247 }
248 
249 /*****************************************************************************
250   Return TRUE iff city is celebrating
251 *****************************************************************************/
api_methods_is_city_celebrating(lua_State * L,City * pcity)252 bool api_methods_is_city_celebrating(lua_State *L, City *pcity)
253 {
254   LUASCRIPT_CHECK_STATE(L, FALSE);
255   LUASCRIPT_CHECK_SELF(L, pcity, FALSE);
256 
257   return city_celebrating(pcity);
258 }
259 
260 /*****************************************************************************
261   Return TRUE iff city is government center
262 *****************************************************************************/
api_methods_is_gov_center(lua_State * L,City * pcity)263 bool api_methods_is_gov_center(lua_State *L, City *pcity)
264 {
265   LUASCRIPT_CHECK_STATE(L, FALSE);
266   LUASCRIPT_CHECK_SELF(L, pcity, FALSE);
267 
268   return is_gov_center(pcity);
269 }
270 
271 /*****************************************************************************
272   Return TRUE if city is capital
273 *****************************************************************************/
api_methods_is_capital(lua_State * L,City * pcity)274 bool api_methods_is_capital(lua_State *L, City *pcity)
275 {
276   LUASCRIPT_CHECK_STATE(L, FALSE);
277   LUASCRIPT_CHECK_SELF(L, pcity, FALSE);
278 
279   return is_capital(pcity);
280 }
281 
282 /*****************************************************************************
283    Return rule name for Government
284 *****************************************************************************/
api_methods_government_rule_name(lua_State * L,Government * pgovernment)285 const char *api_methods_government_rule_name(lua_State *L,
286                                              Government *pgovernment)
287 {
288   LUASCRIPT_CHECK_STATE(L, NULL);
289   LUASCRIPT_CHECK_SELF(L, pgovernment, NULL);
290 
291   return government_rule_name(pgovernment);
292 }
293 
294 /*****************************************************************************
295   Return translated name for Government
296 *****************************************************************************/
api_methods_government_name_translation(lua_State * L,Government * pgovernment)297 const char *api_methods_government_name_translation(lua_State *L,
298                                                     Government *pgovernment)
299 {
300   LUASCRIPT_CHECK_STATE(L, NULL);
301   LUASCRIPT_CHECK_SELF(L, pgovernment, NULL);
302 
303   return government_name_translation(pgovernment);
304 }
305 
306 /*****************************************************************************
307   Return rule name for Nation_Type
308 *****************************************************************************/
api_methods_nation_type_rule_name(lua_State * L,Nation_Type * pnation)309 const char *api_methods_nation_type_rule_name(lua_State *L,
310                                               Nation_Type *pnation)
311 {
312   LUASCRIPT_CHECK_STATE(L, NULL);
313   LUASCRIPT_CHECK_SELF(L, pnation, NULL);
314 
315   return nation_rule_name(pnation);
316 }
317 
318 /*****************************************************************************
319   Return translated adjective for Nation_Type
320 *****************************************************************************/
api_methods_nation_type_name_translation(lua_State * L,Nation_Type * pnation)321 const char *api_methods_nation_type_name_translation(lua_State *L,
322                                                      Nation_Type *pnation)
323 {
324   LUASCRIPT_CHECK_STATE(L, NULL);
325   LUASCRIPT_CHECK_SELF(L, pnation, NULL);
326 
327   return nation_adjective_translation(pnation);
328 }
329 
330 /*****************************************************************************
331   Return translated plural noun for Nation_Type
332 *****************************************************************************/
api_methods_nation_type_plural_translation(lua_State * L,Nation_Type * pnation)333 const char *api_methods_nation_type_plural_translation(lua_State *L,
334                                                        Nation_Type *pnation)
335 {
336   LUASCRIPT_CHECK_STATE(L, NULL);
337   LUASCRIPT_CHECK_SELF(L, pnation, NULL);
338 
339   return nation_plural_translation(pnation);
340 }
341 
342 /*****************************************************************************
343   Return TRUE iff player has wonder
344 *****************************************************************************/
api_methods_player_has_wonder(lua_State * L,Player * pplayer,Building_Type * building)345 bool api_methods_player_has_wonder(lua_State *L, Player *pplayer,
346                                    Building_Type *building)
347 {
348   LUASCRIPT_CHECK_STATE(L, FALSE);
349   LUASCRIPT_CHECK_SELF(L, pplayer, FALSE);
350   LUASCRIPT_CHECK_ARG_NIL(L, building, 3, Building_Type, FALSE);
351 
352   return wonder_is_built(pplayer, building);
353 }
354 
355 /*****************************************************************************
356   Return player number
357 *****************************************************************************/
api_methods_player_number(lua_State * L,Player * pplayer)358 int api_methods_player_number(lua_State *L, Player *pplayer)
359 {
360   LUASCRIPT_CHECK_STATE(L, -1);
361   LUASCRIPT_CHECK_SELF(L, pplayer, -1);
362 
363   return player_number(pplayer);
364 }
365 
366 /*****************************************************************************
367   Return the number of cities pplayer has.
368 *****************************************************************************/
api_methods_player_num_cities(lua_State * L,Player * pplayer)369 int api_methods_player_num_cities(lua_State *L, Player *pplayer)
370 {
371   LUASCRIPT_CHECK_STATE(L, 0);
372   LUASCRIPT_CHECK_SELF(L, pplayer, 0);
373 
374   return city_list_size(pplayer->cities);
375 }
376 
377 /*****************************************************************************
378   Return the number of units pplayer has.
379 *****************************************************************************/
api_methods_player_num_units(lua_State * L,Player * pplayer)380 int api_methods_player_num_units(lua_State *L, Player *pplayer)
381 {
382   LUASCRIPT_CHECK_STATE(L, 0);
383   LUASCRIPT_CHECK_SELF(L, pplayer, 0);
384 
385   return unit_list_size(pplayer->units);
386 }
387 
388 /*****************************************************************************
389   Return gold for Player
390 *****************************************************************************/
api_methods_player_gold(lua_State * L,Player * pplayer)391 int api_methods_player_gold(lua_State *L, Player *pplayer)
392 {
393   LUASCRIPT_CHECK_STATE(L, 0);
394   LUASCRIPT_CHECK_SELF(L, pplayer, 0);
395 
396   return pplayer->economic.gold;
397 }
398 
399 /*****************************************************************************
400   Return TRUE if Player knows advance ptech.
401 *****************************************************************************/
api_methods_player_knows_tech(lua_State * L,Player * pplayer,Tech_Type * ptech)402 bool api_methods_player_knows_tech(lua_State *L, Player *pplayer,
403                                    Tech_Type *ptech)
404 {
405   LUASCRIPT_CHECK_STATE(L, FALSE);
406   LUASCRIPT_CHECK_SELF(L, pplayer, FALSE);
407   LUASCRIPT_CHECK_ARG_NIL(L, ptech, 3, Tech_Type, FALSE);
408 
409   return research_invention_state(research_get(pplayer),
410                                   advance_number(ptech)) == TECH_KNOWN;
411 }
412 
413 /**********************************************************************//**
414   Return Lua list of all players
415 **************************************************************************/
api_methods_private_list_players(lua_State * L)416 lua_Object api_methods_private_list_players(lua_State *L)
417 {
418   lua_Object result = 0;
419   int i = 0;
420 
421   LUASCRIPT_CHECK_STATE(L, 0);
422   lua_createtable(L, player_count(), 0);
423   result = lua_gettop(L);
424   players_iterate(pplayer) {
425     tolua_pushfieldusertype(L, result, ++i, pplayer, "Player");
426   } players_iterate_end;
427   return result;
428 }
429 
430 /**************************************************************************
431   How much culture player has?
432 **************************************************************************/
api_methods_player_culture_get(lua_State * L,Player * pplayer)433 int api_methods_player_culture_get(lua_State *L, Player *pplayer)
434 {
435   LUASCRIPT_CHECK_STATE(L, 0);
436   LUASCRIPT_CHECK_SELF(L, pplayer, 0);
437 
438   return player_culture(pplayer);
439 }
440 
441 /*****************************************************************************
442   Return TRUE if players share research.
443 *****************************************************************************/
api_methods_player_shares_research(lua_State * L,Player * pplayer,Player * aplayer)444 bool api_methods_player_shares_research(lua_State *L, Player *pplayer,
445                                         Player *aplayer)
446 {
447   LUASCRIPT_CHECK_STATE(L, FALSE);
448   LUASCRIPT_CHECK_SELF(L, pplayer, FALSE);
449   LUASCRIPT_CHECK_ARG_NIL(L, aplayer, 3, Player, FALSE);
450 
451   return research_get(pplayer) == research_get(aplayer);
452 }
453 
454 /*****************************************************************************
455   Return name of the research group player belongs to.
456 *****************************************************************************/
api_methods_research_rule_name(lua_State * L,Player * pplayer)457 const char *api_methods_research_rule_name(lua_State *L, Player *pplayer)
458 {
459   LUASCRIPT_CHECK_STATE(L, NULL);
460   LUASCRIPT_CHECK_SELF(L, pplayer, NULL);
461 
462   return research_rule_name(research_get(pplayer));
463 }
464 
465 /*****************************************************************************
466   Return name of the research group player belongs to.
467 *****************************************************************************/
api_methods_research_name_translation(lua_State * L,Player * pplayer)468 const char *api_methods_research_name_translation(lua_State *L, Player *pplayer)
469 {
470   static char buf[MAX_LEN_MSG];
471 
472   LUASCRIPT_CHECK_STATE(L, NULL);
473   LUASCRIPT_CHECK_SELF(L, pplayer, NULL);
474 
475   (void) research_pretty_name(research_get(pplayer), buf, ARRAY_SIZE(buf));
476 
477   return buf;
478 }
479 
480 /*****************************************************************************
481   Return list head for unit list for Player
482 *****************************************************************************/
api_methods_private_player_unit_list_head(lua_State * L,Player * pplayer)483 Unit_List_Link *api_methods_private_player_unit_list_head(lua_State *L,
484                                                           Player *pplayer)
485 {
486   LUASCRIPT_CHECK_STATE(L, NULL);
487   LUASCRIPT_CHECK_SELF(L, pplayer, NULL);
488   return unit_list_head(pplayer->units);
489 }
490 
491 /*****************************************************************************
492   Return list head for city list for Player
493 *****************************************************************************/
api_methods_private_player_city_list_head(lua_State * L,Player * pplayer)494 City_List_Link *api_methods_private_player_city_list_head(lua_State *L,
495                                                           Player *pplayer)
496 {
497   LUASCRIPT_CHECK_STATE(L, NULL);
498   LUASCRIPT_CHECK_SELF(L, pplayer, NULL);
499 
500   return city_list_head(pplayer->cities);
501 }
502 
503 /*****************************************************************************
504   Return rule name for Tech_Type
505 *****************************************************************************/
api_methods_tech_type_rule_name(lua_State * L,Tech_Type * ptech)506 const char *api_methods_tech_type_rule_name(lua_State *L, Tech_Type *ptech)
507 {
508   LUASCRIPT_CHECK_STATE(L, NULL);
509   LUASCRIPT_CHECK_SELF(L, ptech, NULL);
510 
511   return advance_rule_name(ptech);
512 }
513 
514 /*****************************************************************************
515   Return translated name for Tech_Type
516 *****************************************************************************/
api_methods_tech_type_name_translation(lua_State * L,Tech_Type * ptech)517 const char *api_methods_tech_type_name_translation(lua_State *L,
518                                                    Tech_Type *ptech)
519 {
520   LUASCRIPT_CHECK_STATE(L, NULL);
521   LUASCRIPT_CHECK_SELF(L, ptech, NULL);
522 
523   return advance_name_translation(ptech);
524 }
525 
526 /*****************************************************************************
527   Return rule name for Terrain
528 *****************************************************************************/
api_methods_terrain_rule_name(lua_State * L,Terrain * pterrain)529 const char *api_methods_terrain_rule_name(lua_State *L, Terrain *pterrain)
530 {
531   LUASCRIPT_CHECK_STATE(L, NULL);
532   LUASCRIPT_CHECK_SELF(L, pterrain, NULL);
533 
534   return terrain_rule_name(pterrain);
535 }
536 
537 /*****************************************************************************
538   Return translated name for Terrain
539 *****************************************************************************/
api_methods_terrain_name_translation(lua_State * L,Terrain * pterrain)540 const char *api_methods_terrain_name_translation(lua_State *L,
541                                                  Terrain *pterrain)
542 {
543   LUASCRIPT_CHECK_STATE(L, NULL);
544   LUASCRIPT_CHECK_SELF(L, pterrain, NULL);
545 
546   return terrain_name_translation(pterrain);
547 }
548 
549 /*****************************************************************************
550   Return name of the terrain's class
551 *****************************************************************************/
api_methods_terrain_class_name(lua_State * L,Terrain * pterrain)552 const char *api_methods_terrain_class_name(lua_State *L, Terrain *pterrain)
553 {
554   LUASCRIPT_CHECK_STATE(L, NULL);
555   LUASCRIPT_CHECK_SELF(L, pterrain, NULL);
556 
557   return terrain_class_name(terrain_type_terrain_class(pterrain));
558 }
559 
560 /*****************************************************************************
561   Return rule name for Disaster
562 *****************************************************************************/
api_methods_disaster_rule_name(lua_State * L,Disaster * pdis)563 const char *api_methods_disaster_rule_name(lua_State *L, Disaster *pdis)
564 {
565   LUASCRIPT_CHECK_STATE(L, NULL);
566   LUASCRIPT_CHECK_SELF(L, pdis, NULL);
567 
568   return disaster_rule_name(pdis);
569 }
570 
571 /*****************************************************************************
572   Return translated name for Disaster
573 *****************************************************************************/
api_methods_disaster_name_translation(lua_State * L,Disaster * pdis)574 const char *api_methods_disaster_name_translation(lua_State *L,
575                                                   Disaster *pdis)
576 {
577   LUASCRIPT_CHECK_STATE(L, NULL);
578   LUASCRIPT_CHECK_SELF(L, pdis, NULL);
579 
580   return disaster_name_translation(pdis);
581 }
582 
583 /*****************************************************************************
584   Return rule name for Achievement
585 *****************************************************************************/
api_methods_achievement_rule_name(lua_State * L,Achievement * pach)586 const char *api_methods_achievement_rule_name(lua_State *L, Achievement *pach)
587 {
588   LUASCRIPT_CHECK_STATE(L, NULL);
589   LUASCRIPT_CHECK_SELF(L, pach, NULL);
590 
591   return achievement_rule_name(pach);
592 }
593 
594 /*****************************************************************************
595   Return translated name for Achievement
596 *****************************************************************************/
api_methods_achievement_name_translation(lua_State * L,Achievement * pach)597 const char *api_methods_achievement_name_translation(lua_State *L,
598                                                      Achievement *pach)
599 {
600   LUASCRIPT_CHECK_STATE(L, NULL);
601   LUASCRIPT_CHECK_SELF(L, pach, NULL);
602 
603   return achievement_name_translation(pach);
604 }
605 
606 /*****************************************************************************
607   Return rule name for Action
608 *****************************************************************************/
api_methods_action_rule_name(lua_State * L,Action * pact)609 const char *api_methods_action_rule_name(lua_State *L, Action *pact)
610 {
611   LUASCRIPT_CHECK_STATE(L, NULL);
612   LUASCRIPT_CHECK_SELF(L, pact, NULL);
613 
614   return action_id_rule_name(pact->id);
615 }
616 
617 /*****************************************************************************
618   Return translated name for Action
619 *****************************************************************************/
api_methods_action_name_translation(lua_State * L,Action * pact)620 const char *api_methods_action_name_translation(lua_State *L, Action *pact)
621 {
622   LUASCRIPT_CHECK_STATE(L, NULL);
623   LUASCRIPT_CHECK_SELF(L, pact, NULL);
624 
625   return action_id_name_translation(pact->id);
626 }
627 
628 /*****************************************************************************
629   Return the native x coordinate of the tile.
630 *****************************************************************************/
api_methods_tile_nat_x(lua_State * L,Tile * ptile)631 int api_methods_tile_nat_x(lua_State *L, Tile *ptile)
632 {
633   LUASCRIPT_CHECK_STATE(L, -1);
634   LUASCRIPT_CHECK_SELF(L, ptile, -1);
635 
636   return index_to_native_pos_x(tile_index(ptile));
637 }
638 
639 /*****************************************************************************
640   Return the native y coordinate of the tile.
641 *****************************************************************************/
api_methods_tile_nat_y(lua_State * L,Tile * ptile)642 int api_methods_tile_nat_y(lua_State *L, Tile *ptile)
643 {
644   LUASCRIPT_CHECK_STATE(L, -1);
645   LUASCRIPT_CHECK_SELF(L, ptile, -1);
646 
647   return index_to_native_pos_y(tile_index(ptile));
648 }
649 
650 /*****************************************************************************
651   Return the map x coordinate of the tile.
652 *****************************************************************************/
api_methods_tile_map_x(lua_State * L,Tile * ptile)653 int api_methods_tile_map_x(lua_State *L, Tile *ptile)
654 {
655   LUASCRIPT_CHECK_STATE(L, -1);
656   LUASCRIPT_CHECK_SELF(L, ptile, -1);
657 
658   return index_to_map_pos_x(tile_index(ptile));
659 }
660 
661 /*****************************************************************************
662   Return the map y coordinate of the tile.
663 *****************************************************************************/
api_methods_tile_map_y(lua_State * L,Tile * ptile)664 int api_methods_tile_map_y(lua_State *L, Tile *ptile)
665 {
666   LUASCRIPT_CHECK_STATE(L, -1);
667   LUASCRIPT_CHECK_SELF(L, ptile, -1);
668 
669   return index_to_map_pos_y(tile_index(ptile));
670 }
671 
672 /*****************************************************************************
673   Return City on ptile, else NULL
674 *****************************************************************************/
api_methods_tile_city(lua_State * L,Tile * ptile)675 City *api_methods_tile_city(lua_State *L, Tile *ptile)
676 {
677   LUASCRIPT_CHECK_STATE(L, NULL);
678   LUASCRIPT_CHECK_SELF(L, ptile, NULL);
679 
680   return tile_city(ptile);
681 }
682 
683 /*****************************************************************************
684   Return TRUE if there is a city inside the maximum city radius from ptile.
685 *****************************************************************************/
api_methods_tile_city_exists_within_max_city_map(lua_State * L,Tile * ptile,bool may_be_on_center)686 bool api_methods_tile_city_exists_within_max_city_map(lua_State *L,
687                                                       Tile *ptile,
688                                                       bool may_be_on_center)
689 {
690   LUASCRIPT_CHECK_STATE(L, FALSE);
691   LUASCRIPT_CHECK_SELF(L, ptile, FALSE);
692 
693   return city_exists_within_max_city_map(ptile, may_be_on_center);
694 }
695 
696 /*****************************************************************************
697   Return TRUE if there is a extra with rule name name on ptile.
698   If no name is specified return true if there is a extra on ptile.
699 *****************************************************************************/
api_methods_tile_has_extra(lua_State * L,Tile * ptile,const char * name)700 bool api_methods_tile_has_extra(lua_State *L, Tile *ptile, const char *name)
701 {
702   LUASCRIPT_CHECK_STATE(L, FALSE);
703   LUASCRIPT_CHECK_SELF(L, ptile, FALSE);
704 
705   if (!name) {
706     extra_type_iterate(pextra) {
707       if (tile_has_extra(ptile, pextra)) {
708         return TRUE;
709       }
710     } extra_type_iterate_end;
711 
712     return FALSE;
713   } else {
714     struct extra_type *pextra;
715 
716     pextra = extra_type_by_rule_name(name);
717 
718     return (NULL != pextra && tile_has_extra(ptile, pextra));
719   }
720 }
721 
722 /*****************************************************************************
723   Return TRUE if there is a base with rule name name on ptile.
724   If no name is specified return true if there is any base on ptile.
725 *****************************************************************************/
api_methods_tile_has_base(lua_State * L,Tile * ptile,const char * name)726 bool api_methods_tile_has_base(lua_State *L, Tile *ptile, const char *name)
727 {
728   LUASCRIPT_CHECK_STATE(L, FALSE);
729   LUASCRIPT_CHECK_SELF(L, ptile, FALSE);
730 
731   if (!name) {
732     extra_type_by_cause_iterate(EC_BASE, pextra) {
733       if (tile_has_extra(ptile, pextra)) {
734         return TRUE;
735       }
736     } extra_type_by_cause_iterate_end;
737 
738     return FALSE;
739   } else {
740     struct extra_type *pextra;
741 
742     pextra = extra_type_by_rule_name(name);
743 
744     return (NULL != pextra && is_extra_caused_by(pextra, EC_BASE)
745             && tile_has_extra(ptile, pextra));
746   }
747 }
748 
749 /*****************************************************************************
750   Return TRUE if there is a road with rule name name on ptile.
751   If no name is specified return true if there is any road on ptile.
752 *****************************************************************************/
api_methods_tile_has_road(lua_State * L,Tile * ptile,const char * name)753 bool api_methods_tile_has_road(lua_State *L, Tile *ptile, const char *name)
754 {
755   LUASCRIPT_CHECK_STATE(L, FALSE);
756   LUASCRIPT_CHECK_SELF(L, ptile, FALSE);
757 
758   if (!name) {
759     extra_type_by_cause_iterate(EC_ROAD, pextra) {
760       if (tile_has_extra(ptile, pextra)) {
761         return TRUE;
762       }
763     } extra_type_by_cause_iterate_end;
764 
765     return FALSE;
766   } else {
767     struct extra_type *pextra;
768 
769     pextra = extra_type_by_rule_name(name);
770 
771     return (NULL != pextra && is_extra_caused_by(pextra, EC_ROAD)
772             && tile_has_extra(ptile, pextra));
773   }
774 }
775 
776 /*****************************************************************************
777   Return number of units on tile
778 *****************************************************************************/
api_methods_tile_num_units(lua_State * L,Tile * ptile)779 int api_methods_tile_num_units(lua_State *L, Tile *ptile)
780 {
781   LUASCRIPT_CHECK_STATE(L, 0);
782   LUASCRIPT_CHECK_SELF(L, ptile, 0);
783 
784   return unit_list_size(ptile->units);
785 }
786 
787 /*****************************************************************************
788   Return list head for unit list for Tile
789 *****************************************************************************/
api_methods_private_tile_unit_list_head(lua_State * L,Tile * ptile)790 Unit_List_Link *api_methods_private_tile_unit_list_head(lua_State *L,
791                                                         Tile *ptile)
792 {
793   LUASCRIPT_CHECK_STATE(L, NULL);
794   LUASCRIPT_CHECK_SELF(L, ptile, NULL);
795 
796   return unit_list_head(ptile->units);
797 }
798 
799 /*****************************************************************************
800   Return nth tile iteration index (for internal use)
801   Will return the next index, or an index < 0 when done
802 *****************************************************************************/
api_methods_private_tile_next_outward_index(lua_State * L,Tile * pstart,int tindex,int max_dist)803 int api_methods_private_tile_next_outward_index(lua_State *L, Tile *pstart,
804                                                 int tindex, int max_dist)
805 {
806   int dx, dy;
807   int newx, newy;
808   int startx, starty;
809 
810   LUASCRIPT_CHECK_STATE(L, 0);
811   LUASCRIPT_CHECK_SELF(L, pstart, 0);
812 
813   if (tindex < 0) {
814     return 0;
815   }
816 
817   index_to_map_pos(&startx, &starty, tile_index(pstart));
818 
819   tindex++;
820   while (tindex < game.map.num_iterate_outwards_indices) {
821     if (game.map.iterate_outwards_indices[tindex].dist > max_dist) {
822       return -1;
823     }
824     dx = game.map.iterate_outwards_indices[tindex].dx;
825     dy = game.map.iterate_outwards_indices[tindex].dy;
826     newx = dx + startx;
827     newy = dy + starty;
828     if (!normalize_map_pos(&newx, &newy)) {
829       tindex++;
830       continue;
831     }
832     return tindex;
833   }
834   return -1;
835 }
836 
837 /*****************************************************************************
838   Return tile for nth iteration index (for internal use)
839 *****************************************************************************/
api_methods_private_tile_for_outward_index(lua_State * L,Tile * pstart,int tindex)840 Tile *api_methods_private_tile_for_outward_index(lua_State *L, Tile *pstart,
841                                                  int tindex)
842 {
843   int newx, newy;
844 
845   LUASCRIPT_CHECK_STATE(L, NULL);
846   LUASCRIPT_CHECK_SELF(L, pstart, NULL);
847   LUASCRIPT_CHECK_ARG(L,
848                       tindex >= 0 && tindex < game.map.num_iterate_outwards_indices,
849                       3, "index out of bounds", NULL);
850 
851   index_to_map_pos(&newx, &newy, tile_index(pstart));
852   newx += game.map.iterate_outwards_indices[tindex].dx;
853   newy += game.map.iterate_outwards_indices[tindex].dy;
854 
855   if (!normalize_map_pos(&newx, &newy)) {
856     return NULL;
857   }
858   return map_pos_to_tile(newx, newy);
859 }
860 
861 /*****************************************************************************
862   Return squared distance between tiles 1 and 2
863 *****************************************************************************/
api_methods_tile_sq_distance(lua_State * L,Tile * ptile1,Tile * ptile2)864 int api_methods_tile_sq_distance(lua_State *L, Tile *ptile1, Tile *ptile2)
865 {
866   LUASCRIPT_CHECK_STATE(L, 0);
867   LUASCRIPT_CHECK_SELF(L, ptile1, 0);
868   LUASCRIPT_CHECK_ARG_NIL(L, ptile2, 3, Tile, 0);
869 
870   return sq_map_distance(ptile1, ptile2);
871 }
872 
873 /*****************************************************************************
874   Can punit found a city on its tile?
875 *****************************************************************************/
api_methods_unit_city_can_be_built_here(lua_State * L,Unit * punit)876 bool api_methods_unit_city_can_be_built_here(lua_State *L, Unit *punit)
877 {
878   LUASCRIPT_CHECK_STATE(L, FALSE);
879   LUASCRIPT_CHECK_SELF(L, punit, FALSE);
880 
881   return city_can_be_built_here(unit_tile(punit), punit);
882 }
883 
884 /**************************************************************************
885   Return the tile of the unit.
886 **************************************************************************/
api_methods_unit_tile_get(lua_State * L,Unit * punit)887 Tile *api_methods_unit_tile_get(lua_State *L, Unit *punit)
888 {
889   LUASCRIPT_CHECK_STATE(L, NULL);
890   LUASCRIPT_CHECK_SELF(L, punit, NULL);
891 
892   return unit_tile(punit);
893 }
894 
895 /*****************************************************************************
896   Get unit orientation
897 *****************************************************************************/
api_methods_unit_orientation_get(lua_State * L,Unit * punit)898 const Direction *api_methods_unit_orientation_get(lua_State *L, Unit *punit)
899 {
900   LUASCRIPT_CHECK_STATE(L, NULL);
901   LUASCRIPT_CHECK_ARG_NIL(L, punit, 2, Unit, NULL);
902 
903   return luascript_dir(punit->facing);
904 }
905 
906 /*****************************************************************************
907   Return Unit that transports punit, if any.
908 *****************************************************************************/
api_methods_unit_transporter(lua_State * L,Unit * punit)909 Unit *api_methods_unit_transporter(lua_State *L, Unit *punit)
910 {
911   LUASCRIPT_CHECK_STATE(L, NULL);
912   LUASCRIPT_CHECK_SELF(L, punit, NULL);
913 
914   return punit->transporter;
915 }
916 
917 /*****************************************************************************
918   Return list head for cargo list for Unit
919 *****************************************************************************/
api_methods_private_unit_cargo_list_head(lua_State * L,Unit * punit)920 Unit_List_Link *api_methods_private_unit_cargo_list_head(lua_State *L,
921                                                          Unit *punit)
922 {
923   LUASCRIPT_CHECK_STATE(L, NULL);
924   LUASCRIPT_CHECK_SELF(L, punit, NULL);
925   return unit_list_head(punit->transporting);
926 }
927 
928 /*****************************************************************************
929   Return TRUE if punit_type has flag.
930 *****************************************************************************/
api_methods_unit_type_has_flag(lua_State * L,Unit_Type * punit_type,const char * flag)931 bool api_methods_unit_type_has_flag(lua_State *L, Unit_Type *punit_type,
932                                     const char *flag)
933 {
934   enum unit_type_flag_id id;
935 
936   LUASCRIPT_CHECK_STATE(L, FALSE);
937   LUASCRIPT_CHECK_SELF(L, punit_type, FALSE);
938   LUASCRIPT_CHECK_ARG_NIL(L, flag, 3, string, FALSE);
939 
940   id = unit_type_flag_id_by_name(flag, fc_strcasecmp);
941   if (unit_type_flag_id_is_valid(id)) {
942     return utype_has_flag(punit_type, id);
943   } else {
944     luascript_error(L, "Unit type flag \"%s\" does not exist", flag);
945     return FALSE;
946   }
947 }
948 
949 /*****************************************************************************
950   Return TRUE if punit_type has role.
951 *****************************************************************************/
api_methods_unit_type_has_role(lua_State * L,Unit_Type * punit_type,const char * role)952 bool api_methods_unit_type_has_role(lua_State *L, Unit_Type *punit_type,
953                                     const char *role)
954 {
955   enum unit_role_id id;
956 
957   LUASCRIPT_CHECK_STATE(L, FALSE);
958   LUASCRIPT_CHECK_SELF(L, punit_type, FALSE);
959   LUASCRIPT_CHECK_ARG_NIL(L, role, 3, string, FALSE);
960 
961   id = unit_role_id_by_name(role, fc_strcasecmp);
962   if (unit_role_id_is_valid(id)) {
963     return utype_has_role(punit_type, id);
964   } else {
965     luascript_error(L, "Unit role \"%s\" does not exist", role);
966     return FALSE;
967   }
968 }
969 
970 /*****************************************************************************
971   Return TRUE iff the unit type can exist on the tile.
972 *****************************************************************************/
api_methods_unit_type_can_exist_at_tile(lua_State * L,Unit_Type * punit_type,Tile * ptile)973 bool api_methods_unit_type_can_exist_at_tile(lua_State *L,
974                                              Unit_Type *punit_type,
975                                              Tile *ptile)
976 {
977   LUASCRIPT_CHECK_STATE(L, FALSE);
978   LUASCRIPT_CHECK_SELF(L, punit_type, FALSE);
979   LUASCRIPT_CHECK_ARG_NIL(L, ptile, 3, Tile, FALSE);
980 
981   return can_exist_at_tile(punit_type, ptile);
982 }
983 
984 /*****************************************************************************
985   Return rule name for Unit_Type
986 *****************************************************************************/
api_methods_unit_type_rule_name(lua_State * L,Unit_Type * punit_type)987 const char *api_methods_unit_type_rule_name(lua_State *L,
988                                             Unit_Type *punit_type)
989 {
990   LUASCRIPT_CHECK_STATE(L, NULL);
991   LUASCRIPT_CHECK_SELF(L, punit_type, NULL);
992 
993   return utype_rule_name(punit_type);
994 }
995 
996 /*****************************************************************************
997   Return translated name for Unit_Type
998 *****************************************************************************/
api_methods_unit_type_name_translation(lua_State * L,Unit_Type * punit_type)999 const char *api_methods_unit_type_name_translation(lua_State *L,
1000                                                    Unit_Type *punit_type)
1001 {
1002   LUASCRIPT_CHECK_STATE(L, NULL);
1003   LUASCRIPT_CHECK_SELF(L, punit_type, NULL);
1004 
1005   return utype_name_translation(punit_type);
1006 }
1007 
1008 
1009 /*****************************************************************************
1010   Return Unit for list link
1011 *****************************************************************************/
api_methods_unit_list_link_data(lua_State * L,Unit_List_Link * ul_link)1012 Unit *api_methods_unit_list_link_data(lua_State *L,
1013                                       Unit_List_Link *ul_link)
1014 {
1015   LUASCRIPT_CHECK_STATE(L, NULL);
1016 
1017   return unit_list_link_data(ul_link);
1018 }
1019 
1020 /*****************************************************************************
1021   Return next list link or NULL when link is the last link
1022 *****************************************************************************/
api_methods_unit_list_next_link(lua_State * L,Unit_List_Link * ul_link)1023 Unit_List_Link *api_methods_unit_list_next_link(lua_State *L,
1024                                                 Unit_List_Link *ul_link)
1025 {
1026   LUASCRIPT_CHECK_STATE(L, NULL);
1027 
1028   return unit_list_link_next(ul_link);
1029 }
1030 
1031 /*****************************************************************************
1032   Return City for list link
1033 *****************************************************************************/
api_methods_city_list_link_data(lua_State * L,City_List_Link * cl_link)1034 City *api_methods_city_list_link_data(lua_State *L,
1035                                       City_List_Link *cl_link)
1036 {
1037   LUASCRIPT_CHECK_STATE(L, NULL);
1038 
1039   return city_list_link_data(cl_link);
1040 }
1041 
1042 /*****************************************************************************
1043   Return next list link or NULL when link is the last link
1044 *****************************************************************************/
api_methods_city_list_next_link(lua_State * L,City_List_Link * cl_link)1045 City_List_Link *api_methods_city_list_next_link(lua_State *L,
1046                                                 City_List_Link *cl_link)
1047 {
1048   LUASCRIPT_CHECK_STATE(L, NULL);
1049 
1050   return city_list_link_next(cl_link);
1051 }
1052