Freeciv-3.1
Loading...
Searching...
No Matches
api_game_find.c
Go to the documentation of this file.
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/**********************************************************************/
31Player *api_find_player_by_name(lua_State *L, const char *name)
32{
33 LUASCRIPT_CHECK_STATE(L, NULL);
34
35 return player_by_name(name);
36}
37
38/**********************************************************************/
41Player *api_find_player(lua_State *L, int player_id)
42{
43 LUASCRIPT_CHECK_STATE(L, NULL);
44
45 return player_by_number(player_id);
46}
47
48/**********************************************************************/
51City *api_find_city(lua_State *L, Player *pplayer, int city_id)
52{
53 LUASCRIPT_CHECK_STATE(L, NULL);
54
55 if (pplayer) {
56 return player_city_by_number(pplayer, city_id);
57 } else {
58 return idex_lookup_city(&wld, city_id);
59 }
60}
61
62/**********************************************************************/
65Unit *api_find_unit(lua_State *L, Player *pplayer, int unit_id)
66{
67 LUASCRIPT_CHECK_STATE(L, NULL);
68
69 if (pplayer) {
70 return player_unit_by_number(pplayer, unit_id);
71 } else {
72 return idex_lookup_unit(&wld, unit_id);
73 }
74}
75
76/**********************************************************************/
79Unit *api_find_transport_unit(lua_State *L, Player *pplayer,
80 Unit_Type *ptype, Tile *ptile)
81{
82 LUASCRIPT_CHECK_STATE(L, NULL);
83 LUASCRIPT_CHECK_ARG_NIL(L, pplayer, 2, Player, NULL);
84 LUASCRIPT_CHECK_ARG_NIL(L, ptype, 3, Unit_Type, NULL);
85 LUASCRIPT_CHECK_ARG_NIL(L, ptile, 4, Tile, NULL);
86
87 {
88 struct unit *ptransport;
89 struct unit *pvirt = unit_virtual_create(pplayer, NULL, ptype, 0);
90 unit_tile_set(pvirt, ptile);
91 pvirt->homecity = 0;
92 ptransport = transporter_for_unit(pvirt);
94 return ptransport;
95 }
96}
97
98/**********************************************************************/
102Unit_Type *api_find_role_unit_type(lua_State *L, const char *role_name,
103 Player *pplayer)
104{
105 int role_or_flag;
106
107 LUASCRIPT_CHECK_STATE(L, NULL);
108 LUASCRIPT_CHECK_ARG_NIL(L, role_name, 2, string, NULL);
109
110 role_or_flag = unit_role_id_by_name(role_name, fc_strcasecmp);
111
112 if (!unit_role_id_is_valid(role_or_flag)) {
113 role_or_flag = unit_type_flag_id_by_name(role_name, fc_strcasecmp);
114 if (!unit_type_flag_id_is_valid(role_or_flag)) {
115 return NULL;
116 }
117 }
118
119 if (pplayer) {
120 return best_role_unit_for_player(pplayer, role_or_flag);
121 } else if (num_role_units(role_or_flag) > 0) {
122 return get_role_unit(role_or_flag, 0);
123 } else {
124 return NULL;
125 }
126}
127
128/**********************************************************************/
131Tile *api_find_tile(lua_State *L, int nat_x, int nat_y)
132{
133 LUASCRIPT_CHECK_STATE(L, NULL);
134
135 return native_pos_to_tile(&(wld.map), nat_x, nat_y);
136}
137
138/**********************************************************************/
141Tile *api_find_tile_by_index(lua_State *L, int tindex)
142{
143 LUASCRIPT_CHECK_STATE(L, NULL);
144
145 return index_to_tile(&(wld.map), tindex);
146}
147
148/**********************************************************************/
151Government *api_find_government(lua_State *L, int government_id)
152{
153 LUASCRIPT_CHECK_STATE(L, NULL);
154
155 return government_by_number(government_id);
156}
157
158/**********************************************************************/
162 const char *name_orig)
163{
164 LUASCRIPT_CHECK_STATE(L, NULL);
165 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
166
167 return government_by_rule_name(name_orig);
168}
169
170/**********************************************************************/
173Nation_Type *api_find_nation_type(lua_State *L, int nation_type_id)
174{
175 LUASCRIPT_CHECK_STATE(L, NULL);
176
177 return nation_by_number(nation_type_id);
178}
179
180/**********************************************************************/
184 const char *name_orig)
185{
186 LUASCRIPT_CHECK_STATE(L, NULL);
187 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
188
189 return nation_by_rule_name(name_orig);
190}
191
192/**********************************************************************/
195Action *api_find_action(lua_State *L, action_id act_id)
196{
197 LUASCRIPT_CHECK_STATE(L, NULL);
198
199 return action_by_number(act_id);
200}
201
202/**********************************************************************/
205Action *api_find_action_by_name(lua_State *L, const char *name_orig)
206{
207
208 LUASCRIPT_CHECK_STATE(L, NULL);
209 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
210
211 return action_by_rule_name(name_orig);
212}
213
214/**********************************************************************/
217Building_Type *api_find_building_type(lua_State *L, int building_type_id)
218{
219 LUASCRIPT_CHECK_STATE(L, NULL);
220
221 return improvement_by_number(building_type_id);
222}
223
224/**********************************************************************/
228 const char *name_orig)
229{
230 LUASCRIPT_CHECK_STATE(L, NULL);
231 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
232
233 return improvement_by_rule_name(name_orig);
234}
235
236/**********************************************************************/
239Unit_Type *api_find_unit_type(lua_State *L, int unit_type_id)
240{
241 LUASCRIPT_CHECK_STATE(L, NULL);
242
243 return utype_by_number(unit_type_id);
244}
245
246/**********************************************************************/
249Unit_Type *api_find_unit_type_by_name(lua_State *L, const char *name_orig)
250{
251 LUASCRIPT_CHECK_STATE(L, NULL);
252 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
253
254 return unit_type_by_rule_name(name_orig);
255}
256
257/**********************************************************************/
260Tech_Type *api_find_tech_type(lua_State *L, int tech_type_id)
261{
262 LUASCRIPT_CHECK_STATE(L, NULL);
263
264 return advance_by_number(tech_type_id);
265}
266
267/**********************************************************************/
270Tech_Type *api_find_tech_type_by_name(lua_State *L, const char *name_orig)
271{
272 LUASCRIPT_CHECK_STATE(L, NULL);
273 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
274
275 return advance_by_rule_name(name_orig);
276}
277
278/**********************************************************************/
281Terrain *api_find_terrain(lua_State *L, int terrain_id)
282{
283 LUASCRIPT_CHECK_STATE(L, NULL);
284
285 return terrain_by_number(terrain_id);
286}
287
288/**********************************************************************/
291Terrain *api_find_terrain_by_name(lua_State *L, const char *name_orig)
292{
293 LUASCRIPT_CHECK_STATE(L, NULL);
294 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
295
296 return terrain_by_rule_name(name_orig);
297}
298
299/**********************************************************************/
302Achievement *api_find_achievement(lua_State *L, int achievement_id)
303{
304 LUASCRIPT_CHECK_STATE(L, NULL);
305
306 return achievement_by_number(achievement_id);
307}
308
309/**********************************************************************/
312Achievement *api_find_achievement_by_name(lua_State *L, const char *name_orig)
313{
314 LUASCRIPT_CHECK_STATE(L, NULL);
315 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
316
317 return achievement_by_rule_name(name_orig);
318}
319
320/**********************************************************************/
323Disaster *api_find_disaster(lua_State *L, int disaster_id)
324{
325 LUASCRIPT_CHECK_STATE(L, NULL);
326
327 return disaster_by_number(disaster_id);
328}
329
330/**********************************************************************/
333Disaster *api_find_disaster_by_name(lua_State *L, const char *name_orig)
334{
335 LUASCRIPT_CHECK_STATE(L, NULL);
336 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
337
338 return disaster_by_rule_name(name_orig);
339}
340
341/**********************************************************************/
344const Direction *api_find_direction(lua_State *L, int id)
345{
346 LUASCRIPT_CHECK_STATE(L, NULL);
347
348 return luascript_dir((enum direction8) id);
349}
350
351/**********************************************************************/
354Action *api_find_action_type_by_id(lua_State *L, int id)
355{
356 LUASCRIPT_CHECK_STATE(L, NULL);
357
358 return action_by_number(id);
359}
360
361/**********************************************************************/
364Action *api_find_action_type_by_name(lua_State *L, const char *name)
365{
366 LUASCRIPT_CHECK_STATE(L, NULL);
367 LUASCRIPT_CHECK_ARG_NIL(L, name, 2, string, NULL);
368
370}
371
372/**********************************************************************/
376{
377 static char *p = "";
378
379 LUASCRIPT_CHECK_STATE(L, NULL);
380
381 return p;
382}
struct achievement * achievement_by_number(int id)
struct achievement * achievement_by_rule_name(const char *name)
struct action * action_by_rule_name(const char *name)
Definition actions.c:1708
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
Nation_Type * api_find_nation_type(lua_State *L, int nation_type_id)
Tile * api_find_tile(lua_State *L, int nat_x, int nat_y)
Unit_Type * api_find_unit_type(lua_State *L, int unit_type_id)
Government * api_find_government_by_name(lua_State *L, const char *name_orig)
Player * api_find_player(lua_State *L, int player_id)
Achievement * api_find_achievement_by_name(lua_State *L, const char *name_orig)
Achievement * api_find_achievement(lua_State *L, int achievement_id)
Unit_Type * api_find_role_unit_type(lua_State *L, const char *role_name, Player *pplayer)
const Direction * api_find_direction(lua_State *L, int id)
Action * api_find_action_type_by_name(lua_State *L, const char *name)
Tile * api_find_tile_by_index(lua_State *L, int tindex)
Building_Type * api_find_building_type_by_name(lua_State *L, const char *name_orig)
Nation_Type * api_find_nation_type_by_name(lua_State *L, const char *name_orig)
Disaster * api_find_disaster(lua_State *L, int disaster_id)
Tech_Type * api_find_tech_type_by_name(lua_State *L, const char *name_orig)
Government * api_find_government(lua_State *L, int government_id)
Disaster * api_find_disaster_by_name(lua_State *L, const char *name_orig)
Building_Type * api_find_building_type(lua_State *L, int building_type_id)
Action * api_find_action_type_by_id(lua_State *L, int id)
Action * api_find_action_by_name(lua_State *L, const char *name_orig)
Nonexistent * api_find_nonexistent(lua_State *L)
Player * api_find_player_by_name(lua_State *L, const char *name)
Unit * api_find_transport_unit(lua_State *L, Player *pplayer, Unit_Type *ptype, Tile *ptile)
Tech_Type * api_find_tech_type(lua_State *L, int tech_type_id)
City * api_find_city(lua_State *L, Player *pplayer, int city_id)
Unit * api_find_unit(lua_State *L, Player *pplayer, int unit_id)
Action * api_find_action(lua_State *L, action_id act_id)
Terrain * api_find_terrain(lua_State *L, int terrain_id)
Terrain * api_find_terrain_by_name(lua_State *L, const char *name_orig)
Unit_Type * api_find_unit_type_by_name(lua_State *L, const char *name_orig)
struct disaster_type * disaster_by_rule_name(const char *name)
Definition disaster.c:114
struct disaster_type * disaster_by_number(Disaster_type_id id)
Definition disaster.c:87
int action_id
Definition fc_types.h:359
struct world wld
Definition game.c:58
struct government * government_by_number(const Government_type_id gov)
Definition government.c:102
struct government * government_by_rule_name(const char *name)
Definition government.c:54
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:132
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:145
struct impr_type * improvement_by_number(const Impr_type_id id)
struct impr_type * improvement_by_rule_name(const char *name)
const char * name
Definition inputfile.c:127
const Direction * luascript_dir(enum direction8 dir)
Definition luascript.c:789
#define LUASCRIPT_CHECK_STATE(L,...)
Definition luascript.h:117
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition luascript.h:138
enum direction8 Direction
void Nonexistent
#define nat_x
#define nat_y
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:454
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:441
struct nation_type * nation_by_number(const Nation_type_id nation)
Definition nation.c:474
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:120
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1205
struct player * player_by_number(const int player_id)
Definition player.c:840
struct player * player_by_name(const char *name)
Definition player.c:872
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1179
Definition city.h:309
Definition tile.h:49
Definition unit.h:138
int homecity
Definition unit.h:146
struct civ_map map
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:107
struct advance * advance_by_rule_name(const char *name)
Definition tech.c:200
struct terrain * terrain_by_rule_name(const char *name)
Definition terrain.c:174
struct terrain * terrain_by_number(const Terrain_type_id type)
Definition terrain.c:144
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1617
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1713
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1281
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1896
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1819
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2301
int num_role_units(int role)
Definition unittype.c:2251
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2346
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112