Freeciv21
Develop your civilization from humble roots to a global empire
api_game_find.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 // utility
12 #include "fcintl.h"
13 
14 // common
15 #include "idex.h"
16 #include "map.h"
17 
18 /* common/scriptcore */
19 #include "luascript.h"
20 
21 #include "api_game_find.h"
22 
26 Player *api_find_player(lua_State *L, int player_id)
27 {
28  LUASCRIPT_CHECK_STATE(L, nullptr);
29 
30  return player_by_number(player_id);
31 }
32 
36 City *api_find_city(lua_State *L, Player *pplayer, int city_id)
37 {
38  LUASCRIPT_CHECK_STATE(L, nullptr);
39 
40  if (pplayer) {
41  return player_city_by_number(pplayer, city_id);
42  } else {
43  return idex_lookup_city(&wld, city_id);
44  }
45 }
46 
50 Unit *api_find_unit(lua_State *L, Player *pplayer, int unit_id)
51 {
52  LUASCRIPT_CHECK_STATE(L, nullptr);
53 
54  if (pplayer) {
55  return player_unit_by_number(pplayer, unit_id);
56  } else {
57  return idex_lookup_unit(&wld, unit_id);
58  }
59 }
60 
64 Unit *api_find_transport_unit(lua_State *L, Player *pplayer,
65  Unit_Type *ptype, Tile *ptile)
66 {
67  LUASCRIPT_CHECK_STATE(L, nullptr);
68  LUASCRIPT_CHECK_ARG_NIL(L, pplayer, 2, Player, nullptr);
69  LUASCRIPT_CHECK_ARG_NIL(L, ptype, 3, Unit_Type, nullptr);
70  LUASCRIPT_CHECK_ARG_NIL(L, ptile, 4, Tile, nullptr);
71 
72  {
73  struct unit *ptransport;
74  struct unit *pvirt = unit_virtual_create(pplayer, nullptr, ptype, 0);
75  unit_tile_set(pvirt, ptile);
76  pvirt->homecity = 0;
77  ptransport = transporter_for_unit(pvirt);
78  unit_virtual_destroy(pvirt);
79  return ptransport;
80  }
81 }
82 
87 Unit_Type *api_find_role_unit_type(lua_State *L, const char *role_name,
88  Player *pplayer)
89 {
90  int role_or_flag;
91 
92  LUASCRIPT_CHECK_STATE(L, nullptr);
93  LUASCRIPT_CHECK_ARG_NIL(L, role_name, 2, string, nullptr);
94 
95  role_or_flag = unit_role_id_by_name(role_name, fc_strcasecmp);
96 
97  if (!unit_role_id_is_valid(unit_role_id(role_or_flag))) {
98  role_or_flag = unit_type_flag_id_by_name(role_name, fc_strcasecmp);
99  if (!unit_type_flag_id_is_valid(unit_type_flag_id(role_or_flag))) {
100  return nullptr;
101  }
102  }
103 
104  if (pplayer) {
105  return best_role_unit_for_player(pplayer, role_or_flag);
106  } else if (num_role_units(role_or_flag) > 0) {
107  return get_role_unit(role_or_flag, 0);
108  } else {
109  return nullptr;
110  }
111 }
112 
116 Tile *api_find_tile(lua_State *L, int nat_x, int nat_y)
117 {
118  LUASCRIPT_CHECK_STATE(L, nullptr);
119 
120  return native_pos_to_tile(&(wld.map), nat_x, nat_y);
121 }
122 
126 Tile *api_find_tile_by_index(lua_State *L, int tindex)
127 {
128  LUASCRIPT_CHECK_STATE(L, nullptr);
129 
130  return index_to_tile(&(wld.map), tindex);
131 }
132 
136 Government *api_find_government(lua_State *L, int government_id)
137 {
138  LUASCRIPT_CHECK_STATE(L, nullptr);
139 
140  return government_by_number(government_id);
141 }
142 
146 Government *api_find_government_by_name(lua_State *L, const char *name_orig)
147 {
148  LUASCRIPT_CHECK_STATE(L, nullptr);
149  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
150 
151  return government_by_rule_name(name_orig);
152 }
153 
157 Nation_Type *api_find_nation_type(lua_State *L, int nation_type_id)
158 {
159  LUASCRIPT_CHECK_STATE(L, nullptr);
160 
161  return nation_by_number(nation_type_id);
162 }
163 
168  const char *name_orig)
169 {
170  LUASCRIPT_CHECK_STATE(L, nullptr);
171  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
172 
173  return nation_by_rule_name(name_orig);
174 }
175 
179 Action *api_find_action(lua_State *L, action_id act_id)
180 {
181  LUASCRIPT_CHECK_STATE(L, nullptr);
182 
183  return action_by_number(act_id);
184 }
185 
189 Action *api_find_action_by_name(lua_State *L, const char *name_orig)
190 {
191  LUASCRIPT_CHECK_STATE(L, nullptr);
192  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
193 
194  return action_by_rule_name(name_orig);
195 }
196 
200 Building_Type *api_find_building_type(lua_State *L, int building_type_id)
201 {
202  LUASCRIPT_CHECK_STATE(L, nullptr);
203 
204  return improvement_by_number(building_type_id);
205 }
206 
211  const char *name_orig)
212 {
213  LUASCRIPT_CHECK_STATE(L, nullptr);
214  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
215 
216  return improvement_by_rule_name(name_orig);
217 }
218 
222 Unit_Type *api_find_unit_type(lua_State *L, int unit_type_id)
223 {
224  LUASCRIPT_CHECK_STATE(L, nullptr);
225 
226  return utype_by_number(unit_type_id);
227 }
228 
232 Unit_Type *api_find_unit_type_by_name(lua_State *L, const char *name_orig)
233 {
234  LUASCRIPT_CHECK_STATE(L, nullptr);
235  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
236 
237  return unit_type_by_rule_name(name_orig);
238 }
239 
243 Tech_Type *api_find_tech_type(lua_State *L, int tech_type_id)
244 {
245  LUASCRIPT_CHECK_STATE(L, nullptr);
246 
247  return advance_by_number(tech_type_id);
248 }
249 
253 Tech_Type *api_find_tech_type_by_name(lua_State *L, const char *name_orig)
254 {
255  LUASCRIPT_CHECK_STATE(L, nullptr);
256  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
257 
258  return advance_by_rule_name(name_orig);
259 }
260 
264 Terrain *api_find_terrain(lua_State *L, int terrain_id)
265 {
266  LUASCRIPT_CHECK_STATE(L, nullptr);
267 
268  return terrain_by_number(terrain_id);
269 }
270 
274 Terrain *api_find_terrain_by_name(lua_State *L, const char *name_orig)
275 {
276  LUASCRIPT_CHECK_STATE(L, nullptr);
277  LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, nullptr);
278 
279  return terrain_by_rule_name(name_orig);
280 }
281 
286 {
287  static const char *p = "";
288 
289  LUASCRIPT_CHECK_STATE(L, nullptr);
290 
291  return (Nonexistent *) p;
292 }
struct action * action_by_rule_name(const char *name)
Return the action with the given name.
Definition: actions.cpp:1169
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
Building_Type * api_find_building_type_by_name(lua_State *L, const char *name_orig)
Return the improvement type with the given name_orig.
Unit_Type * api_find_role_unit_type(lua_State *L, const char *role_name, Player *pplayer)
Return a unit type for given role or flag.
Unit_Type * api_find_unit_type_by_name(lua_State *L, const char *name_orig)
Return the unit type with the given name_orig.
Nation_Type * api_find_nation_type_by_name(lua_State *L, const char *name_orig)
Return the nation type with the given name_orig.
Terrain * api_find_terrain(lua_State *L, int terrain_id)
Return the terrain with the given terrain_id index.
Terrain * api_find_terrain_by_name(lua_State *L, const char *name_orig)
Return the terrain with the given name_orig.
Action * api_find_action_by_name(lua_State *L, const char *name_orig)
Return the action with the given name_orig.
Tile * api_find_tile_by_index(lua_State *L, int tindex)
Return the tile at the given index.
Building_Type * api_find_building_type(lua_State *L, int building_type_id)
Return the improvement type with the given impr_type_id index.
Nation_Type * api_find_nation_type(lua_State *L, int nation_type_id)
Return the nation type with the given nation_type_id index.
Government * api_find_government_by_name(lua_State *L, const char *name_orig)
Return the governmet with the given name_orig.
Unit * api_find_unit(lua_State *L, Player *pplayer, int unit_id)
Return a player unit with the given unit_id.
Government * api_find_government(lua_State *L, int government_id)
Return the government with the given Government_type_id index.
Unit * api_find_transport_unit(lua_State *L, Player *pplayer, Unit_Type *ptype, Tile *ptile)
Return a unit that can transport ptype at a given ptile.
Tech_Type * api_find_tech_type(lua_State *L, int tech_type_id)
Return the tech type with the given tech_type_id index.
Tech_Type * api_find_tech_type_by_name(lua_State *L, const char *name_orig)
Return the tech type with the given name_orig.
Player * api_find_player(lua_State *L, int player_id)
Return a player with the given player_id.
City * api_find_city(lua_State *L, Player *pplayer, int city_id)
Return a player city with the given city_id.
Action * api_find_action(lua_State *L, action_id act_id)
Return the action type with the given action_id number.
Unit_Type * api_find_unit_type(lua_State *L, int unit_type_id)
Return the unit type with the given unit_type_id index.
Tile * api_find_tile(lua_State *L, int nat_x, int nat_y)
Return the tile at the given native coordinates.
Nonexistent * api_find_nonexistent(lua_State *L)
Return a dummy pointer.
int action_id
Definition: fc_types.h:306
struct world wld
Definition: game.cpp:48
struct government * government_by_number(const Government_type_id gov)
Return the government with the given index.
Definition: government.cpp:96
struct government * government_by_rule_name(const char *name)
Returns the government that has the given (untranslated) rule name.
Definition: government.cpp:48
struct unit * idex_lookup_unit(struct world *iworld, int id)
Lookup unit with given id.
Definition: idex.cpp:152
struct city * idex_lookup_city(struct world *iworld, int id)
Lookup city with given id.
Definition: idex.cpp:139
struct impr_type * improvement_by_rule_name(const char *name)
Does a linear search of improvement_types[].name.vernacular Returns nullptr when none match.
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
#define LUASCRIPT_CHECK_STATE(L,...)
Definition: luascript.h:110
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition: luascript.h:131
void Nonexistent
#define nat_x
#define nat_y
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Return the tile for the given native position.
Definition: map.cpp:416
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Return the tile for the given index position.
Definition: map.cpp:429
struct nation_type * nation_by_number(const Nation_type_id nation)
Return the nation with the given index.
Definition: nation.cpp:450
struct nation_type * nation_by_rule_name(const char *name)
Returns the nation that has the given (untranslated) rule name (adjective).
Definition: nation.cpp:98
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
If the specified player owns the unit with the specified id, return pointer to the unit struct.
Definition: player.cpp:1139
struct player * player_by_number(const int player_id)
Return struct player pointer for the given player index.
Definition: player.cpp:768
struct city * player_city_by_number(const struct player *pplayer, int city_id)
If the specified player owns the city with the specified id, return pointer to the city struct.
Definition: player.cpp:1113
Definition: tech.h:113
Definition: city.h:291
Definition: player.h:231
Definition: tile.h:42
Definition: unit.h:134
int homecity
Definition: unit.h:142
struct civ_map map
Definition: world_object.h:21
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
struct advance * advance_by_rule_name(const char *name)
Does a linear search of advances[].name.vernacular Returns nullptr when none match.
Definition: tech.cpp:180
struct advance * advance_by_number(const Tech_type_id atype)
Return the advance for the given advance index.
Definition: tech.cpp:94
struct terrain * terrain_by_number(const Terrain_type_id type)
Return the terrain for the given terrain index.
Definition: terrain.cpp:128
struct terrain * terrain_by_rule_name(const char *name)
Return the terrain type matching the name, or T_UNKNOWN if none matches.
Definition: terrain.cpp:140
struct unit * transporter_for_unit(const struct unit *pcargo)
Find the best transporter at the given location for the unit.
Definition: unit.cpp:1779
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
Definition: unit.cpp:1200
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Return "best" unit the player can build, with given role/flag.
Definition: unittype.cpp:1950
int num_role_units(int role)
How many unit types have specified role/flag.
Definition: unittype.cpp:1866
struct unit_type * unit_type_by_rule_name(const char *name)
Returns the unit type that has the given (untranslated) rule name.
Definition: unittype.cpp:1444
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
Definition: unittype.cpp:1900