Freeciv-3.3: /fast/freeciv/nightly/main/common/scriptcore/api_game_find.c Source File

Freeciv-3.3

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 "counters.h"
20#include "idex.h"
21#include "map.h"
22#include "movement.h"
23
24/* common/scriptcore */
25#include "luascript.h"
26
27#include "api_game_find.h"
28
29/**********************************************************************/
32 Player *api_find_player_by_name(lua_State *L, const char *name)
33{
34 LUASCRIPT_CHECK_STATE(L, NULL);
35
36 return player_by_name(name);
37}
38
39/**********************************************************************/
42 Player *api_find_player(lua_State *L, int player_id)
43{
44 LUASCRIPT_CHECK_STATE(L, NULL);
45
46 return player_by_number(player_id);
47}
48
49/**********************************************************************/
52 City *api_find_city(lua_State *L, Player *pplayer, int city_id)
53{
54 LUASCRIPT_CHECK_STATE(L, NULL);
55
56 if (pplayer) {
57 return player_city_by_number(pplayer, city_id);
58 } else {
59 return idex_lookup_city(&wld, city_id);
60 }
61}
62
63/**********************************************************************/
66 Unit *api_find_unit(lua_State *L, Player *pplayer, int unit_id)
67{
68 LUASCRIPT_CHECK_STATE(L, NULL);
69
70 if (pplayer) {
71 return player_unit_by_number(pplayer, unit_id);
72 } else {
73 return idex_lookup_unit(&wld, unit_id);
74 }
75}
76
77/**********************************************************************/
80 Unit *api_find_transport_unit(lua_State *L, Player *pplayer,
81 Unit_Type *ptype, Tile *ptile)
82{
83 LUASCRIPT_CHECK_STATE(L, NULL);
84 LUASCRIPT_CHECK_ARG_NIL(L, pplayer, 2, Player, NULL);
85 LUASCRIPT_CHECK_ARG_NIL(L, ptype, 3, Unit_Type, NULL);
86 LUASCRIPT_CHECK_ARG_NIL(L, ptile, 4, Tile, NULL);
87
88 {
89 struct unit *ptransport;
90 struct unit *pvirt = unit_virtual_create(pplayer, NULL, ptype, 0);
91 unit_tile_set(pvirt, ptile);
92 pvirt->homecity = 0;
93 ptransport = transporter_for_unit(pvirt);
94 unit_virtual_destroy(pvirt);
95 return ptransport;
96 }
97}
98
99/**********************************************************************/
103 Unit_Type *api_find_role_unit_type(lua_State *L, const char *role_name,
104 Player *pplayer)
105{
106 int role_or_flag;
107
108 LUASCRIPT_CHECK_STATE(L, NULL);
109 LUASCRIPT_CHECK_ARG_NIL(L, role_name, 2, string, NULL);
110
111 role_or_flag = unit_role_id_by_name(role_name, fc_strcasecmp);
112
113 if (!unit_role_id_is_valid(role_or_flag)) {
114 role_or_flag = unit_type_flag_id_by_name(role_name, fc_strcasecmp);
115 if (!unit_type_flag_id_is_valid(role_or_flag)) {
116 return NULL;
117 }
118 }
119
120 if (pplayer) {
121 return best_role_unit_for_player(pplayer, role_or_flag);
122 } else if (num_role_units(role_or_flag) > 0) {
123 return get_role_unit(role_or_flag, 0);
124 } else {
125 return NULL;
126 }
127}
128
129/**********************************************************************/
132 Tile *api_find_tile(lua_State *L, int nat_x, int nat_y)
133{
134 LUASCRIPT_CHECK_STATE(L, NULL);
135
136 return native_pos_to_tile(&(wld.map), nat_x, nat_y);
137}
138
139/**********************************************************************/
142 Tile *api_find_tile_by_index(lua_State *L, int tindex)
143{
144 LUASCRIPT_CHECK_STATE(L, NULL);
145
146 return index_to_tile(&(wld.map), tindex);
147}
148
149/**********************************************************************/
152 Government *api_find_government(lua_State *L, int government_id)
153{
154 LUASCRIPT_CHECK_STATE(L, NULL);
155
156 return government_by_number(government_id);
157}
158
159/**********************************************************************/
162 Government *api_find_government_by_name(lua_State *L,
163 const char *name_orig)
164{
165 LUASCRIPT_CHECK_STATE(L, NULL);
166 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
167
168 return government_by_rule_name(name_orig);
169}
170
171/**********************************************************************/
174 Nation_Type *api_find_nation_type(lua_State *L, int nation_type_id)
175{
176 LUASCRIPT_CHECK_STATE(L, NULL);
177
178 return nation_by_number(nation_type_id);
179}
180
181/**********************************************************************/
184 Nation_Type *api_find_nation_type_by_name(lua_State *L,
185 const char *name_orig)
186{
187 LUASCRIPT_CHECK_STATE(L, NULL);
188 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
189
190 return nation_by_rule_name(name_orig);
191}
192
193/**********************************************************************/
196 Action *api_find_action(lua_State *L, action_id act_id)
197{
198 LUASCRIPT_CHECK_STATE(L, NULL);
199
200 return action_by_number(act_id);
201}
202
203/**********************************************************************/
206 Action *api_find_action_by_name(lua_State *L, const char *name_orig)
207{
208
209 LUASCRIPT_CHECK_STATE(L, NULL);
210 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
211
212 return action_by_rule_name(name_orig);
213}
214
215/**********************************************************************/
218 Building_Type *api_find_building_type(lua_State *L, int building_type_id)
219{
220 LUASCRIPT_CHECK_STATE(L, NULL);
221
222 return improvement_by_number(building_type_id);
223}
224
225/**********************************************************************/
228 Building_Type *api_find_building_type_by_name(lua_State *L,
229 const char *name_orig)
230{
231 LUASCRIPT_CHECK_STATE(L, NULL);
232 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
233
234 return improvement_by_rule_name(name_orig);
235}
236
237/**********************************************************************/
240 Unit_Type *api_find_unit_type(lua_State *L, int unit_type_id)
241{
242 LUASCRIPT_CHECK_STATE(L, NULL);
243
244 return utype_by_number(unit_type_id);
245}
246
247/**********************************************************************/
250 Unit_Type *api_find_unit_type_by_name(lua_State *L, const char *name_orig)
251{
252 LUASCRIPT_CHECK_STATE(L, NULL);
253 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
254
255 return unit_type_by_rule_name(name_orig);
256}
257
258/**********************************************************************/
261 Tech_Type *api_find_tech_type(lua_State *L, int tech_type_id)
262{
263 LUASCRIPT_CHECK_STATE(L, NULL);
264
265 return advance_by_number(tech_type_id);
266}
267
268/**********************************************************************/
271 Tech_Type *api_find_tech_type_by_name(lua_State *L, const char *name_orig)
272{
273 LUASCRIPT_CHECK_STATE(L, NULL);
274 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
275
276 return advance_by_rule_name(name_orig);
277}
278
279/**********************************************************************/
282 Terrain *api_find_terrain(lua_State *L, int terrain_id)
283{
284 LUASCRIPT_CHECK_STATE(L, NULL);
285
286 return terrain_by_number(terrain_id);
287}
288
289/**********************************************************************/
292 Terrain *api_find_terrain_by_name(lua_State *L, const char *name_orig)
293{
294 LUASCRIPT_CHECK_STATE(L, NULL);
295 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
296
297 return terrain_by_rule_name(name_orig);
298}
299
300/**********************************************************************/
303 Achievement *api_find_achievement(lua_State *L, int achievement_id)
304{
305 LUASCRIPT_CHECK_STATE(L, NULL);
306
307 return achievement_by_number(achievement_id);
308}
309
310/**********************************************************************/
313 Achievement *api_find_achievement_by_name(lua_State *L, const char *name_orig)
314{
315 LUASCRIPT_CHECK_STATE(L, NULL);
316 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
317
318 return achievement_by_rule_name(name_orig);
319}
320
321/**********************************************************************/
324 Disaster *api_find_disaster(lua_State *L, int disaster_id)
325{
326 LUASCRIPT_CHECK_STATE(L, NULL);
327
328 return disaster_by_number(disaster_id);
329}
330
331/**********************************************************************/
334 Disaster *api_find_disaster_by_name(lua_State *L, const char *name_orig)
335{
336 LUASCRIPT_CHECK_STATE(L, NULL);
337 LUASCRIPT_CHECK_ARG_NIL(L, name_orig, 2, string, NULL);
338
339 return disaster_by_rule_name(name_orig);
340}
341
342/**********************************************************************/
345 const Direction *api_find_direction(lua_State *L, int id)
346{
347 LUASCRIPT_CHECK_STATE(L, NULL);
348
349 return luascript_dir((enum direction8) id);
350}
351
352/**********************************************************************/
355 Action *api_find_action_type_by_id(lua_State *L, int id)
356{
357 LUASCRIPT_CHECK_STATE(L, NULL);
358
359 return action_by_number(id);
360}
361
362/**********************************************************************/
365 Action *api_find_action_type_by_name(lua_State *L, const char *name)
366{
367 LUASCRIPT_CHECK_STATE(L, NULL);
368 LUASCRIPT_CHECK_ARG_NIL(L, name, 2, string, NULL);
369
370 return action_by_rule_name(name);
371}
372
373/**********************************************************************/
376 Counter *api_find_counter(lua_State *L, int counter_id)
377{
378 LUASCRIPT_CHECK_STATE(L, NULL);
379
380 return counter_by_id(counter_id);
381}
382
383/**********************************************************************/
386 Counter *api_find_counter_by_name(lua_State *L, const char *name)
387{
388 LUASCRIPT_CHECK_STATE(L, NULL);
389 LUASCRIPT_CHECK_ARG_NIL(L, name, 2, string, NULL);
390
391 return counter_by_rule_name(name);
392}
393
394/**********************************************************************/
397 Nonexistent *api_find_nonexistent(lua_State *L)
398{
399 static char *p = "";
400
401 LUASCRIPT_CHECK_STATE(L, NULL);
402
403 return p;
404}
achievement_by_number
struct achievement * achievement_by_number(int id)
Definition achievements.c:96
achievement_by_rule_name
struct achievement * achievement_by_rule_name(const char *name)
Definition achievements.c:123
action_by_rule_name
struct action * action_by_rule_name(const char *name)
Definition actions.c:1044
action_by_number
static struct action * action_by_number(action_id act_id)
Definition actions.h:390
api_find_nation_type
Nation_Type * api_find_nation_type(lua_State *L, int nation_type_id)
Definition api_game_find.c:174
api_find_tile
Tile * api_find_tile(lua_State *L, int nat_x, int nat_y)
Definition api_game_find.c:132
api_find_unit_type
Unit_Type * api_find_unit_type(lua_State *L, int unit_type_id)
Definition api_game_find.c:240
api_find_government_by_name
Government * api_find_government_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:162
api_find_player
Player * api_find_player(lua_State *L, int player_id)
Definition api_game_find.c:42
api_find_achievement_by_name
Achievement * api_find_achievement_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:313
api_find_achievement
Achievement * api_find_achievement(lua_State *L, int achievement_id)
Definition api_game_find.c:303
api_find_role_unit_type
Unit_Type * api_find_role_unit_type(lua_State *L, const char *role_name, Player *pplayer)
Definition api_game_find.c:103
api_find_direction
const Direction * api_find_direction(lua_State *L, int id)
Definition api_game_find.c:345
api_find_action_type_by_name
Action * api_find_action_type_by_name(lua_State *L, const char *name)
Definition api_game_find.c:365
api_find_tile_by_index
Tile * api_find_tile_by_index(lua_State *L, int tindex)
Definition api_game_find.c:142
api_find_building_type_by_name
Building_Type * api_find_building_type_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:228
api_find_nation_type_by_name
Nation_Type * api_find_nation_type_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:184
api_find_disaster
Disaster * api_find_disaster(lua_State *L, int disaster_id)
Definition api_game_find.c:324
api_find_tech_type_by_name
Tech_Type * api_find_tech_type_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:271
api_find_government
Government * api_find_government(lua_State *L, int government_id)
Definition api_game_find.c:152
api_find_disaster_by_name
Disaster * api_find_disaster_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:334
api_find_counter
Counter * api_find_counter(lua_State *L, int counter_id)
Definition api_game_find.c:376
api_find_building_type
Building_Type * api_find_building_type(lua_State *L, int building_type_id)
Definition api_game_find.c:218
api_find_action_type_by_id
Action * api_find_action_type_by_id(lua_State *L, int id)
Definition api_game_find.c:355
api_find_action_by_name
Action * api_find_action_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:206
api_find_nonexistent
Nonexistent * api_find_nonexistent(lua_State *L)
Definition api_game_find.c:397
api_find_player_by_name
Player * api_find_player_by_name(lua_State *L, const char *name)
Definition api_game_find.c:32
api_find_transport_unit
Unit * api_find_transport_unit(lua_State *L, Player *pplayer, Unit_Type *ptype, Tile *ptile)
Definition api_game_find.c:80
api_find_tech_type
Tech_Type * api_find_tech_type(lua_State *L, int tech_type_id)
Definition api_game_find.c:261
api_find_city
City * api_find_city(lua_State *L, Player *pplayer, int city_id)
Definition api_game_find.c:52
api_find_unit
Unit * api_find_unit(lua_State *L, Player *pplayer, int unit_id)
Definition api_game_find.c:66
api_find_action
Action * api_find_action(lua_State *L, action_id act_id)
Definition api_game_find.c:196
api_find_terrain
Terrain * api_find_terrain(lua_State *L, int terrain_id)
Definition api_game_find.c:282
api_find_terrain_by_name
Terrain * api_find_terrain_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:292
api_find_counter_by_name
Counter * api_find_counter_by_name(lua_State *L, const char *name)
Definition api_game_find.c:386
api_find_unit_type_by_name
Unit_Type * api_find_unit_type_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:250
api_game_find.h
incite_cost
char * incite_cost
Definition comments.c:74
counter_by_rule_name
struct counter * counter_by_rule_name(const char *name)
Definition counters.c:115
counter_by_id
struct counter * counter_by_id(int id)
Definition counters.c:82
counter_id
int counter_id(struct counter *pcount)
Definition counters.c:105
counters.h
disaster_by_rule_name
struct disaster_type * disaster_by_rule_name(const char *name)
Definition disaster.c:114
disaster_by_number
struct disaster_type * disaster_by_number(Disaster_type_id id)
Definition disaster.c:87
action_id
int action_id
Definition fc_types.h:393
wld
struct world wld
Definition game.c:63
government_by_number
struct government * government_by_number(const Government_type_id gov)
Definition government.c:103
government_by_rule_name
struct government * government_by_rule_name(const char *name)
Definition government.c:55
idex_lookup_city
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:133
idex_lookup_unit
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:146
idex.h
improvement_by_number
struct impr_type * improvement_by_number(const Impr_type_id id)
Definition improvement.c:194
improvement_by_rule_name
struct impr_type * improvement_by_rule_name(const char *name)
Definition improvement.c:367
name
const char * name
Definition inputfile.c:127
luascript_dir
const Direction * luascript_dir(enum direction8 dir)
Definition luascript.c:790
luascript.h
LUASCRIPT_CHECK_STATE
#define LUASCRIPT_CHECK_STATE(L,...)
Definition luascript.h:117
LUASCRIPT_CHECK_ARG_NIL
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition luascript.h:138
Direction
enum direction8 Direction
Definition luascript_types.h:59
Nonexistent
void Nonexistent
Definition luascript_types.h:65
nat_x
#define nat_x
nat_y
#define nat_y
index_to_tile
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:462
native_pos_to_tile
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:449
map.h
movement.h
nation_by_number
struct nation_type * nation_by_number(const Nation_type_id nation)
Definition nation.c:475
nation_by_rule_name
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:121
player_unit_by_number
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1229
player_by_number
struct player * player_by_number(const int player_id)
Definition player.c:849
player_by_name
struct player * player_by_name(const char *name)
Definition player.c:881
player_city_by_number
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1203
achievement
Definition achievements.h:26
action
Definition actions.h:106
advance
Definition tech.h:124
city
Definition city.h:320
counter
Definition counters.h:27
disaster_type
Definition disaster.h:48
government
Definition government.h:52
impr_type
Definition improvement.h:50
nation_type
Definition nation.h:98
player
Definition player.h:249
terrain
Definition terrain.h:75
tile
Definition tile.h:50
unit_type
Definition unittype.h:507
unit
Definition unit.h:140
world::map
struct civ_map map
Definition world_object.h:38
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
advance_by_number
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:107
advance_by_rule_name
struct advance * advance_by_rule_name(const char *name)
Definition tech.c:200
terrain_by_rule_name
struct terrain * terrain_by_rule_name(const char *name)
Definition terrain.c:186
terrain_by_number
struct terrain * terrain_by_number(const Terrain_type_id type)
Definition terrain.c:156
unit_virtual_create
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1657
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1762
unit_tile_set
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1281
transporter_for_unit
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1945
unit_type_by_rule_name
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1767
get_role_unit
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2253
num_role_units
int num_role_units(int role)
Definition unittype.c:2203
best_role_unit_for_player
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2297
utype_by_number
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112