Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/scriptcore/api_game_find.c Source File

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/**********************************************************************/
31 Player *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/**********************************************************************/
41 Player *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/**********************************************************************/
51 City *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/**********************************************************************/
65 Unit *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/**********************************************************************/
79 Unit *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);
93 unit_virtual_destroy(pvirt);
94 return ptransport;
95 }
96}
97
98/**********************************************************************/
102 Unit_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/**********************************************************************/
131 Tile *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/**********************************************************************/
141 Tile *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/**********************************************************************/
151 Government *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/**********************************************************************/
161 Government *api_find_government_by_name(lua_State *L,
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/**********************************************************************/
173 Nation_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/**********************************************************************/
183 Nation_Type *api_find_nation_type_by_name(lua_State *L,
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/**********************************************************************/
195 Action *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/**********************************************************************/
205 Action *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/**********************************************************************/
217 Building_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/**********************************************************************/
227 Building_Type *api_find_building_type_by_name(lua_State *L,
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/**********************************************************************/
239 Unit_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/**********************************************************************/
249 Unit_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/**********************************************************************/
260 Tech_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/**********************************************************************/
270 Tech_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/**********************************************************************/
281 Terrain *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/**********************************************************************/
291 Terrain *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/**********************************************************************/
302 Achievement *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/**********************************************************************/
312 Achievement *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/**********************************************************************/
323 Disaster *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/**********************************************************************/
333 Disaster *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/**********************************************************************/
344 const 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/**********************************************************************/
354 Action *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/**********************************************************************/
364 Action *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
369 return action_by_rule_name(name);
370}
371
372/**********************************************************************/
375 Nonexistent *api_find_nonexistent(lua_State *L)
376{
377 static char *p = "";
378
379 LUASCRIPT_CHECK_STATE(L, NULL);
380
381 return p;
382}
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:1708
action_by_number
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
api_find_nation_type
Nation_Type * api_find_nation_type(lua_State *L, int nation_type_id)
Definition api_game_find.c:173
api_find_tile
Tile * api_find_tile(lua_State *L, int nat_x, int nat_y)
Definition api_game_find.c:131
api_find_unit_type
Unit_Type * api_find_unit_type(lua_State *L, int unit_type_id)
Definition api_game_find.c:239
api_find_government_by_name
Government * api_find_government_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:161
api_find_player
Player * api_find_player(lua_State *L, int player_id)
Definition api_game_find.c:41
api_find_achievement_by_name
Achievement * api_find_achievement_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:312
api_find_achievement
Achievement * api_find_achievement(lua_State *L, int achievement_id)
Definition api_game_find.c:302
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:102
api_find_direction
const Direction * api_find_direction(lua_State *L, int id)
Definition api_game_find.c:344
api_find_action_type_by_name
Action * api_find_action_type_by_name(lua_State *L, const char *name)
Definition api_game_find.c:364
api_find_tile_by_index
Tile * api_find_tile_by_index(lua_State *L, int tindex)
Definition api_game_find.c:141
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:227
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:183
api_find_disaster
Disaster * api_find_disaster(lua_State *L, int disaster_id)
Definition api_game_find.c:323
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:270
api_find_government
Government * api_find_government(lua_State *L, int government_id)
Definition api_game_find.c:151
api_find_disaster_by_name
Disaster * api_find_disaster_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:333
api_find_building_type
Building_Type * api_find_building_type(lua_State *L, int building_type_id)
Definition api_game_find.c:217
api_find_action_type_by_id
Action * api_find_action_type_by_id(lua_State *L, int id)
Definition api_game_find.c:354
api_find_action_by_name
Action * api_find_action_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:205
api_find_nonexistent
Nonexistent * api_find_nonexistent(lua_State *L)
Definition api_game_find.c:375
api_find_player_by_name
Player * api_find_player_by_name(lua_State *L, const char *name)
Definition api_game_find.c:31
api_find_transport_unit
Unit * api_find_transport_unit(lua_State *L, Player *pplayer, Unit_Type *ptype, Tile *ptile)
Definition api_game_find.c:79
api_find_tech_type
Tech_Type * api_find_tech_type(lua_State *L, int tech_type_id)
Definition api_game_find.c:260
api_find_city
City * api_find_city(lua_State *L, Player *pplayer, int city_id)
Definition api_game_find.c:51
api_find_unit
Unit * api_find_unit(lua_State *L, Player *pplayer, int unit_id)
Definition api_game_find.c:65
api_find_action
Action * api_find_action(lua_State *L, action_id act_id)
Definition api_game_find.c:195
api_find_terrain
Terrain * api_find_terrain(lua_State *L, int terrain_id)
Definition api_game_find.c:281
api_find_terrain_by_name
Terrain * api_find_terrain_by_name(lua_State *L, const char *name_orig)
Definition api_game_find.c:291
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:249
api_game_find.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:359
wld
struct world wld
Definition game.c:58
government_by_number
struct government * government_by_number(const Government_type_id gov)
Definition government.c:102
government_by_rule_name
struct government * government_by_rule_name(const char *name)
Definition government.c:54
idex_lookup_city
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:132
idex_lookup_unit
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:145
idex.h
improvement_by_number
struct impr_type * improvement_by_number(const Impr_type_id id)
Definition improvement.c:192
improvement_by_rule_name
struct impr_type * improvement_by_rule_name(const char *name)
Definition improvement.c:365
name
const char * name
Definition inputfile.c:127
luascript_dir
const Direction * luascript_dir(enum direction8 dir)
Definition luascript.c:789
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:57
Nonexistent
void Nonexistent
Definition luascript_types.h:62
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:454
native_pos_to_tile
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:441
map.h
movement.h
nation_by_number
struct nation_type * nation_by_number(const Nation_type_id nation)
Definition nation.c:474
nation_by_rule_name
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:120
player_unit_by_number
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1205
player_by_number
struct player * player_by_number(const int player_id)
Definition player.c:840
player_by_name
struct player * player_by_name(const char *name)
Definition player.c:872
player_city_by_number
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1179
achievement
Definition achievements.h:26
action
Definition actions.h:379
advance
Definition tech.h:124
city
Definition city.h:309
disaster_type
Definition disaster.h:46
government
Definition government.h:52
impr_type
Definition improvement.h:68
nation_type
Definition nation.h:97
player
Definition player.h:249
terrain
Definition terrain.h:176
tile
Definition tile.h:49
unit_type
Definition unittype.h:481
unit
Definition unit.h:138
unit::homecity
int homecity
Definition unit.h:146
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:189
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:174
terrain_by_number
struct terrain * terrain_by_number(const Terrain_type_id type)
Definition terrain.c:144
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:1619
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1715
unit_tile_set
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1282
transporter_for_unit
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1898
unit_type_by_rule_name
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1819
get_role_unit
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2301
num_role_units
int num_role_units(int role)
Definition unittype.c:2251
best_role_unit_for_player
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2345
utype_by_number
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112