Freeciv-3.2: /fast/freeciv/nightly/S3_2/ai/tex/texaiworld.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
texaiworld.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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 "world_object.h"
22
23/* server/advisors */
24#include "infracache.h"
25
26/* ai/tex */
27#include "texaiplayer.h"
28
29#include "texaiworld.h"
30
31 static struct world texai_world;
32
33 struct texai_tile_info_msg
34{
35 int index;
36 struct terrain *terrain;
37 bv_extras extras;
38};
39
40 struct texai_city_info_msg
41{
42 int id;
43 int owner;
44 int tindex;
45};
46
47 struct texai_id_msg
48{
49 int id;
50};
51
52 struct texai_unit_info_msg
53{
54 int id;
55 int owner;
56 int tindex;
57 int type;
58};
59
60 struct texai_unit_move_msg
61{
62 int id;
63 int tindex;
64};
65
66/**********************************************************************/
69 void texai_world_init(void)
70{
71 idex_init(&texai_world);
72}
73
74/**********************************************************************/
77 void texai_world_close(void)
78{
79 idex_free(&texai_world);
80}
81
82/**********************************************************************/
85 void texai_map_init(void)
86{
87 map_init(&(texai_world.map), TRUE);
88 map_allocate(&(texai_world.map));
89}
90
91/**********************************************************************/
94 struct civ_map *texai_map_get(void)
95{
96 return &(texai_world.map);
97}
98
99/**********************************************************************/
102 void texai_map_close(void)
103{
104 map_free(&(texai_world.map));
105}
106
107/**********************************************************************/
110 void texai_tile_info(struct tile *ptile)
111{
112 if (texai_thread_running()) {
113 struct texai_tile_info_msg *info = fc_malloc(sizeof(struct texai_tile_info_msg));
114
115 info->index = tile_index(ptile);
116 info->terrain = ptile->terrain;
117 info->extras = ptile->extras;
118
119 texai_send_msg(TEXAI_MSG_TILE_INFO, NULL, info);
120 }
121}
122
123/**********************************************************************/
126 void texai_tile_info_recv(void *data)
127{
128 struct texai_tile_info_msg *info = (struct texai_tile_info_msg *)data;
129
130 if (texai_world.map.tiles != NULL) {
131 struct tile *ptile;
132
133 ptile = index_to_tile(&(texai_world.map), info->index);
134 ptile->terrain = info->terrain;
135 ptile->extras = info->extras;
136 }
137
138 free(info);
139}
140
141/**********************************************************************/
144 static void texai_city_update(struct city *pcity, enum texaimsgtype msgtype)
145{
146 if (texai_thread_running()) {
147 struct texai_city_info_msg *info
148 = fc_malloc(sizeof(struct texai_city_info_msg));
149
150 info->id = pcity->id;
151 info->owner = player_number(city_owner(pcity));
152 info->tindex = tile_index(city_tile(pcity));
153
154 texai_send_msg(msgtype, NULL, info);
155 }
156}
157
158/**********************************************************************/
161 void texai_city_created(struct city *pcity)
162{
163 texai_city_update(pcity, TEXAI_MSG_CITY_CREATED);
164}
165
166/**********************************************************************/
169 void texai_city_changed(struct city *pcity)
170{
171 texai_city_update(pcity, TEXAI_MSG_CITY_CHANGED);
172}
173
174/**********************************************************************/
177 void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
178{
179 struct texai_city_info_msg *info = (struct texai_city_info_msg *)data;
180 struct city *pcity;
181 struct player *pplayer = player_by_number(info->owner);
182
183 if (msgtype == TEXAI_MSG_CITY_CREATED) {
184 struct tile *ptile;
185 struct texai_plr *plr_data;
186
187 if (idex_lookup_city(&texai_world, info->id) != NULL) {
188 return;
189 }
190
191 ptile = index_to_tile(&(texai_world.map), info->tindex);
192
193 pcity = create_city_virtual(pplayer, ptile, "");
194 adv_city_alloc(pcity);
195 pcity->id = info->id;
196
197 idex_register_city(&texai_world, pcity);
198 plr_data = player_ai_data(pplayer, texai_get_self());
199 city_list_prepend(plr_data->cities, pcity);
200 tile_set_worked(ptile, pcity);
201 } else {
202 pcity = idex_lookup_city(&texai_world, info->id);
203
204 if (pcity != NULL) {
205 pcity->owner = pplayer;
206 } else {
207 log_error("Tex: requested change on city id %d that's not known.",
208 info->id);
209 }
210 }
211
212 free(info);
213}
214
215/**********************************************************************/
218 struct city *texai_map_city(int city_id)
219{
220 return idex_lookup_city(&texai_world, city_id);
221}
222
223/**********************************************************************/
226 void texai_city_destroyed(struct city *pcity)
227{
228 if (texai_thread_running()) {
229 struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
230
231 info->id = pcity->id;
232
233 texai_send_msg(TEXAI_MSG_CITY_DESTROYED, NULL, info);
234 }
235}
236
237/**********************************************************************/
240 void texai_city_destruction_recv(void *data)
241{
242 struct texai_id_msg *info = (struct texai_id_msg *)data;
243 struct city *pcity = idex_lookup_city(&texai_world, info->id);
244
245 if (pcity != NULL) {
246 struct texai_plr *plr_data;
247
248 adv_city_free(pcity);
249 tile_set_worked(city_tile(pcity), NULL);
250 plr_data = player_ai_data(city_owner(pcity), texai_get_self());
251 city_list_remove(plr_data->cities, pcity);
252 idex_unregister_city(&texai_world, pcity);
253 destroy_city_virtual(pcity);
254 } else {
255 log_error("Tex: requested removal of city id %d that's not known.",
256 info->id);
257 }
258
259 free(info);
260}
261
262/**********************************************************************/
265 static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
266{
267 if (texai_thread_running()) {
268 struct texai_unit_info_msg *info
269 = fc_malloc(sizeof(struct texai_unit_info_msg));
270
271 info->id = punit->id;
272 info->owner = player_number(unit_owner(punit));
273 info->tindex = tile_index(unit_tile(punit));
274 info->type = utype_number(unit_type_get(punit));
275
276 texai_send_msg(msgtype, NULL, info);
277 }
278}
279
280/**********************************************************************/
283 void texai_unit_created(struct unit *punit)
284{
285 texai_unit_update(punit, TEXAI_MSG_UNIT_CREATED);
286}
287
288/**********************************************************************/
291 void texai_unit_changed(struct unit *punit)
292{
293 texai_unit_update(punit, TEXAI_MSG_UNIT_CHANGED);
294}
295
296/**********************************************************************/
299 void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
300{
301 struct texai_unit_info_msg *info = (struct texai_unit_info_msg *)data;
302 struct unit *punit;
303 struct player *pplayer = player_by_number(info->owner);
304 struct unit_type *type = utype_by_number(info->type);
305 struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
306
307 if (msgtype == TEXAI_MSG_UNIT_CREATED) {
308 struct texai_plr *plr_data;
309
310 if (idex_lookup_unit(&texai_world, info->id) != NULL) {
311 return;
312 }
313
314 plr_data = player_ai_data(pplayer, texai_get_self());
315
316 punit = unit_virtual_create(pplayer, NULL, type, 0);
317 punit->id = info->id;
318
319 idex_register_unit(&texai_world, punit);
320 unit_list_prepend(ptile->units, punit);
321 unit_list_prepend(plr_data->units, punit);
322
323 unit_tile_set(punit, ptile);
324 } else {
325 fc_assert(msgtype == TEXAI_MSG_UNIT_CHANGED);
326
327 punit = idex_lookup_unit(&texai_world, info->id);
328
329 punit->utype = type;
330 }
331
332 free(info);
333}
334
335/**********************************************************************/
338 void texai_unit_destroyed(struct unit *punit)
339{
340 if (texai_thread_running()) {
341 struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
342
343 info->id = punit->id;
344
345 texai_send_msg(TEXAI_MSG_UNIT_DESTROYED, NULL, info);
346 }
347}
348
349/**********************************************************************/
352 void texai_unit_destruction_recv(void *data)
353{
354 struct texai_id_msg *info = (struct texai_id_msg *)data;
355 struct unit *punit = idex_lookup_unit(&texai_world, info->id);
356
357 if (punit != NULL) {
358 struct texai_plr *plr_data = player_ai_data(punit->owner,
359 texai_get_self());
360
361 unit_list_remove(punit->tile->units, punit);
362 unit_list_remove(plr_data->units, punit);
363 idex_unregister_unit(&texai_world, punit);
364 unit_virtual_destroy(punit);
365 } else {
366 log_error("Tex: requested removal of unit id %d that's not known.",
367 info->id);
368 }
369
370 free(info);
371}
372
373/**********************************************************************/
376 void texai_unit_move_seen(struct unit *punit)
377{
378 if (texai_thread_running()) {
379 struct texai_unit_move_msg *info = fc_malloc(sizeof(struct texai_unit_move_msg));
380
381 info->id = punit->id;
382 info->tindex = tile_index(unit_tile(punit));
383
384 texai_send_msg(TEXAI_MSG_UNIT_MOVED, NULL, info);
385 }
386}
387
388/**********************************************************************/
391 void texai_unit_moved_recv(void *data)
392{
393 struct texai_unit_move_msg *info = (struct texai_unit_move_msg *)data;
394 struct unit *punit = idex_lookup_unit(&texai_world, info->id);
395 struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
396
397 if (punit != NULL) {
398 unit_list_remove(punit->tile->units, punit);
399 unit_list_prepend(ptile->units, punit);
400
401 unit_tile_set(punit, ptile);
402 } else {
403 log_error("Tex: requested moving of unit id %d that's not known.",
404 info->id);
405 }
406
407 free(info);
408}
create_city_virtual
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3418
destroy_city_virtual
void destroy_city_virtual(struct city *pcity)
Definition city.c:3504
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_owner
#define city_owner(_pcity_)
Definition city.h:563
incite_cost
char * incite_cost
Definition comments.c:74
punit
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
type
GType type
Definition repodlgs.c:1313
idex_free
void idex_free(struct world *iworld)
Definition idex.c:54
idex_register_unit
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:82
idex_lookup_city
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:133
idex_unregister_city
void idex_unregister_city(struct world *iworld, struct city *pcity)
Definition idex.c:97
idex_init
void idex_init(struct world *iworld)
Definition idex.c:45
idex_lookup_unit
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:146
idex_unregister_unit
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Definition idex.c:115
idex_register_city
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:67
idex.h
adv_city_free
void adv_city_free(struct city *pcity)
Definition infracache.c:501
adv_city_alloc
void adv_city_alloc(struct city *pcity)
Definition infracache.c:488
infracache.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_error
#define log_error(message,...)
Definition log.h:103
map_free
void map_free(struct civ_map *fmap)
Definition map.c:530
map_allocate
void map_allocate(struct civ_map *amap)
Definition map.c:493
index_to_tile
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
map_init
void map_init(struct civ_map *imap, bool server_side)
Definition map.c:157
map.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
player_ai_data
void * player_ai_data(const struct player *pplayer, const struct ai_type *ai)
Definition player.c:1940
player_by_number
struct player * player_by_number(const int player_id)
Definition player.c:849
player_number
int player_number(const struct player *pplayer)
Definition player.c:837
city
Definition city.h:320
city::id
int id
Definition city.h:326
city::owner
struct player * owner
Definition city.h:323
civ_map
Definition map_types.h:71
civ_map::tiles
struct tile * tiles
Definition map_types.h:83
player
Definition player.h:247
terrain
Definition terrain.h:182
texai_city_info_msg
Definition texaiworld.c:41
texai_city_info_msg::id
int id
Definition texaiworld.c:42
texai_city_info_msg::owner
int owner
Definition texaiworld.c:43
texai_city_info_msg::tindex
int tindex
Definition texaiworld.c:44
texai_id_msg
Definition texaiworld.c:48
texai_id_msg::id
int id
Definition texaiworld.c:49
texai_plr
Definition texaiplayer.h:43
texai_tile_info_msg
Definition texaiworld.c:34
texai_tile_info_msg::terrain
struct terrain * terrain
Definition texaiworld.c:36
texai_tile_info_msg::index
int index
Definition texaiworld.c:35
texai_tile_info_msg::extras
bv_extras extras
Definition texaiworld.c:37
texai_unit_info_msg
Definition texaiworld.c:53
texai_unit_info_msg::owner
int owner
Definition texaiworld.c:55
texai_unit_info_msg::tindex
int tindex
Definition texaiworld.c:56
texai_unit_info_msg::type
int type
Definition texaiworld.c:57
texai_unit_info_msg::id
int id
Definition texaiworld.c:54
texai_unit_move_msg
Definition texaiworld.c:61
texai_unit_move_msg::tindex
int tindex
Definition texaiworld.c:63
texai_unit_move_msg::id
int id
Definition texaiworld.c:62
tile
Definition tile.h:50
tile::extras
bv_extras extras
Definition tile.h:55
tile::units
struct unit_list * units
Definition tile.h:58
tile::terrain
struct terrain * terrain
Definition tile.h:57
unit_type
Definition unittype.h:500
unit
Definition unit.h:138
unit::id
int id
Definition unit.h:145
unit::tile
struct tile * tile
Definition unit.h:140
unit::utype
const struct unit_type * utype
Definition unit.h:139
unit::owner
struct player * owner
Definition unit.h:143
world
Definition world_object.h:37
world::map
struct civ_map map
Definition world_object.h:38
TRUE
#define TRUE
Definition support.h:46
texai_get_self
struct ai_type * texai_get_self(void)
Definition texai.c:60
texai_send_msg
void texai_send_msg(enum texaimsgtype type, struct player *pplayer, void *data)
Definition texaimsg.c:26
texai_thread_running
bool texai_thread_running(void)
Definition texaiplayer.c:435
texaiplayer.h
texai_city_update
static void texai_city_update(struct city *pcity, enum texaimsgtype msgtype)
Definition texaiworld.c:144
texai_city_info_recv
void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
Definition texaiworld.c:177
texai_map_get
struct civ_map * texai_map_get(void)
Definition texaiworld.c:94
texai_city_created
void texai_city_created(struct city *pcity)
Definition texaiworld.c:161
texai_city_destruction_recv
void texai_city_destruction_recv(void *data)
Definition texaiworld.c:240
texai_city_destroyed
void texai_city_destroyed(struct city *pcity)
Definition texaiworld.c:226
texai_unit_destroyed
void texai_unit_destroyed(struct unit *punit)
Definition texaiworld.c:338
texai_unit_update
static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
Definition texaiworld.c:265
texai_tile_info_recv
void texai_tile_info_recv(void *data)
Definition texaiworld.c:126
texai_map_close
void texai_map_close(void)
Definition texaiworld.c:102
texai_unit_changed
void texai_unit_changed(struct unit *punit)
Definition texaiworld.c:291
texai_world_init
void texai_world_init(void)
Definition texaiworld.c:69
texai_tile_info
void texai_tile_info(struct tile *ptile)
Definition texaiworld.c:110
texai_city_changed
void texai_city_changed(struct city *pcity)
Definition texaiworld.c:169
texai_unit_move_seen
void texai_unit_move_seen(struct unit *punit)
Definition texaiworld.c:376
texai_unit_info_recv
void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
Definition texaiworld.c:299
texai_unit_created
void texai_unit_created(struct unit *punit)
Definition texaiworld.c:283
texai_map_city
struct city * texai_map_city(int city_id)
Definition texaiworld.c:218
texai_unit_moved_recv
void texai_unit_moved_recv(void *data)
Definition texaiworld.c:391
texai_map_init
void texai_map_init(void)
Definition texaiworld.c:85
texai_world
static struct world texai_world
Definition texaiworld.c:31
texai_world_close
void texai_world_close(void)
Definition texaiworld.c:77
texai_unit_destruction_recv
void texai_unit_destruction_recv(void *data)
Definition texaiworld.c:352
texaiworld.h
tile_set_worked
void tile_set_worked(struct tile *ptile, struct city *pcity)
Definition tile.c:106
tile_index
#define tile_index(_pt_)
Definition tile.h:88
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:1620
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1725
unit_tile_set
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1252
unit_tile
#define unit_tile(_pu)
Definition unit.h:390
unit_owner
#define unit_owner(_pu)
Definition unit.h:389
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
utype_by_number
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112
utype_number
Unit_type_id utype_number(const struct unit_type *punittype)
Definition unittype.c:100
world_object.h