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 "idex.h"
20 #include "map.h"
21 #include "movement.h"
22 
23 /* common/scriptcore */
24 #include "luascript.h"
25 
26 #include "api_game_find.h"
27 
28 /*****************************************************************************
29   Return a player with the given player_id.
30 *****************************************************************************/
api_find_player(lua_State * L,int player_id)31 Player *api_find_player(lua_State *L, int player_id)
32 {
33   LUASCRIPT_CHECK_STATE(L, NULL);
34 
35   return player_by_number(player_id);
36 }
37 
38 /*****************************************************************************
39   Return a player city with the given city_id.
40 *****************************************************************************/
api_find_city(lua_State * L,Player * pplayer,int city_id)41 City *api_find_city(lua_State *L, Player *pplayer, int city_id)
42 {
43   LUASCRIPT_CHECK_STATE(L, NULL);
44 
45   if (pplayer) {
46     return player_city_by_number(pplayer, city_id);
47   } else {
48     return idex_lookup_city(city_id);
49   }
50 }
51 
52 /*****************************************************************************
53   Return a player unit with the given unit_id.
54 *****************************************************************************/
api_find_unit(lua_State * L,Player * pplayer,int unit_id)55 Unit *api_find_unit(lua_State *L, Player *pplayer, int unit_id)
56 {
57   LUASCRIPT_CHECK_STATE(L, NULL);
58 
59   if (pplayer) {
60     return player_unit_by_number(pplayer, unit_id);
61   } else {
62     return idex_lookup_unit(unit_id);
63   }
64 }
65 
66 /*****************************************************************************
67   Return a unit that can transport ptype at a given ptile.
68 *****************************************************************************/
api_find_transport_unit(lua_State * L,Player * pplayer,Unit_Type * ptype,Tile * ptile)69 Unit *api_find_transport_unit(lua_State *L, Player *pplayer, Unit_Type *ptype,
70                               Tile *ptile)
71 {
72   LUASCRIPT_CHECK_STATE(L, NULL);
73   LUASCRIPT_CHECK_ARG_NIL(L, pplayer, 2, Player, NULL);
74   LUASCRIPT_CHECK_ARG_NIL(L, ptype, 3, Unit_Type, NULL);
75   LUASCRIPT_CHECK_ARG_NIL(L, ptile, 4, Tile, NULL);
76 
77   {
78     struct unit *ptransport;
79     struct unit *pvirt = unit_virtual_create(pplayer, NULL, ptype, 0);
80     unit_tile_set(pvirt, ptile);
81     pvirt->homecity = 0;
82     ptransport = transporter_for_unit(pvirt);
83     unit_virtual_destroy(pvirt);
84     return ptransport;
85   }
86 }
87 
88 /*****************************************************************************
89   Return a unit type for given role or flag.
90   (Prior to 2.6.0, this worked only for roles.)
91 *****************************************************************************/
api_find_role_unit_type(lua_State * L,const char * role_name,Player * pplayer)92 Unit_Type *api_find_role_unit_type(lua_State *L, const char *role_name,
93                                    Player *pplayer)
94 {
95   int role_or_flag;
96 
97   LUASCRIPT_CHECK_STATE(L, NULL);
98   LUASCRIPT_CHECK_ARG_NIL(L, role_name, 2, string, NULL);
99 
100   role_or_flag = unit_role_id_by_name(role_name, fc_strcasecmp);
101 
102   if (!unit_role_id_is_valid(role_or_flag)) {
103     role_or_flag = unit_type_flag_id_by_name(role_name, fc_strcasecmp);
104     if (!unit_type_flag_id_is_valid(role_or_flag)) {
105       return NULL;
106     }
107   }
108 
109   if (pplayer) {
110     return best_role_unit_for_player(pplayer, role_or_flag);
111   } else if (num_role_units(role_or_flag) > 0) {
112     return get_role_unit(role_or_flag, 0);
113   } else {
114     return NULL;
115   }
116 }
117 
118 /*****************************************************************************
119   Return the tile at the given native coordinates.
120 *****************************************************************************/
api_find_tile(lua_State * L,int nat_x,int nat_y)121 Tile *api_find_tile(lua_State *L, int nat_x, int nat_y)
122 {
123   LUASCRIPT_CHECK_STATE(L, NULL);
124 
125   return native_pos_to_tile(nat_x, nat_y);
126 }
127 
128 /*****************************************************************************
129   Return the tile at the given index.
130 *****************************************************************************/
api_find_tile_by_index(lua_State * L,int tindex)131 Tile *api_find_tile_by_index(lua_State *L, int tindex)
132 {
133   LUASCRIPT_CHECK_STATE(L, NULL);
134 
135   return index_to_tile(tindex);
136 }
137 
138 /*****************************************************************************
139   Return the government with the given Government_type_id index.
140 *****************************************************************************/
api_find_government(lua_State * L,int government_id)141 Government *api_find_government(lua_State *L, int government_id)
142 {
143   LUASCRIPT_CHECK_STATE(L, NULL);
144 
145   return government_by_number(government_id);
146 }
147 
148 /*****************************************************************************
149   Return the governmet with the given name_orig.
150 *****************************************************************************/
api_find_government_by_name(lua_State * L,const char * name_orig)151 Government *api_find_government_by_name(lua_State *L, const char *name_orig)
152 {
153   LUASCRIPT_CHECK_STATE(L, NULL);
154   LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
155 
156   return government_by_rule_name(name_orig);
157 }
158 
159 /*****************************************************************************
160   Return the nation type with the given nation_type_id index.
161 *****************************************************************************/
api_find_nation_type(lua_State * L,int nation_type_id)162 Nation_Type *api_find_nation_type(lua_State *L, int nation_type_id)
163 {
164   LUASCRIPT_CHECK_STATE(L, NULL);
165 
166   return nation_by_number(nation_type_id);
167 }
168 
169 /*****************************************************************************
170   Return the nation type with the given name_orig.
171 *****************************************************************************/
api_find_nation_type_by_name(lua_State * L,const char * name_orig)172 Nation_Type *api_find_nation_type_by_name(lua_State *L, const char *name_orig)
173 {
174   LUASCRIPT_CHECK_STATE(L, NULL);
175   LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
176 
177   return nation_by_rule_name(name_orig);
178 }
179 
180 /*****************************************************************************
181   Return the improvement type with the given impr_type_id index.
182 *****************************************************************************/
api_find_building_type(lua_State * L,int building_type_id)183 Building_Type *api_find_building_type(lua_State *L, int building_type_id)
184 {
185   LUASCRIPT_CHECK_STATE(L, NULL);
186 
187   return improvement_by_number(building_type_id);
188 }
189 
190 /*****************************************************************************
191   Return the improvement type with the given name_orig.
192 *****************************************************************************/
api_find_building_type_by_name(lua_State * L,const char * name_orig)193 Building_Type *api_find_building_type_by_name(lua_State *L,
194                                               const char *name_orig)
195 {
196   LUASCRIPT_CHECK_STATE(L, NULL);
197   LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
198 
199   return improvement_by_rule_name(name_orig);
200 }
201 
202 /*****************************************************************************
203   Return the unit type with the given unit_type_id index.
204 *****************************************************************************/
api_find_unit_type(lua_State * L,int unit_type_id)205 Unit_Type *api_find_unit_type(lua_State *L, int unit_type_id)
206 {
207   LUASCRIPT_CHECK_STATE(L, NULL);
208 
209   return utype_by_number(unit_type_id);
210 }
211 
212 /*****************************************************************************
213   Return the unit type with the given name_orig.
214 *****************************************************************************/
api_find_unit_type_by_name(lua_State * L,const char * name_orig)215 Unit_Type *api_find_unit_type_by_name(lua_State *L, const char *name_orig)
216 {
217   LUASCRIPT_CHECK_STATE(L, NULL);
218   LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
219 
220   return unit_type_by_rule_name(name_orig);
221 }
222 
223 /*****************************************************************************
224   Return the tech type with the given tech_type_id index.
225 *****************************************************************************/
api_find_tech_type(lua_State * L,int tech_type_id)226 Tech_Type *api_find_tech_type(lua_State *L, int tech_type_id)
227 {
228   LUASCRIPT_CHECK_STATE(L, NULL);
229 
230   return advance_by_number(tech_type_id);
231 }
232 
233 /*****************************************************************************
234   Return the tech type with the given name_orig.
235 *****************************************************************************/
api_find_tech_type_by_name(lua_State * L,const char * name_orig)236 Tech_Type *api_find_tech_type_by_name(lua_State *L, const char *name_orig)
237 {
238   LUASCRIPT_CHECK_STATE(L, NULL);
239   LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
240 
241   return advance_by_rule_name(name_orig);
242 }
243 
244 /*****************************************************************************
245   Return the terrain with the given terrain_id index.
246 *****************************************************************************/
api_find_terrain(lua_State * L,int terrain_id)247 Terrain *api_find_terrain(lua_State *L, int terrain_id)
248 {
249   LUASCRIPT_CHECK_STATE(L, NULL);
250 
251   return terrain_by_number(terrain_id);
252 }
253 
254 /*****************************************************************************
255   Return the terrain with the given name_orig.
256 *****************************************************************************/
api_find_terrain_by_name(lua_State * L,const char * name_orig)257 Terrain *api_find_terrain_by_name(lua_State *L, const char *name_orig)
258 {
259   LUASCRIPT_CHECK_STATE(L, NULL);
260   LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
261 
262   return terrain_by_rule_name(name_orig);
263 }
264 
265 /*****************************************************************************
266   Return a dummy pointer.
267 *****************************************************************************/
api_find_nonexistent(lua_State * L)268 Nonexistent *api_find_nonexistent(lua_State *L)
269 {
270   static char *p = "";
271 
272   LUASCRIPT_CHECK_STATE(L, NULL);
273 
274   return p;
275 }
276