Freeciv-3.3
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
31static struct world texai_world;
32
39
41{
42 int id;
43 int owner;
44 int tindex;
45};
46
48{
49 int id;
50};
51
53{
54 int id;
55 int owner;
56 int tindex;
57 int type;
58};
59
61{
62 int id;
63 int tindex;
64};
65
66/**********************************************************************/
70{
72}
73
74/**********************************************************************/
78{
80}
81
82/**********************************************************************/
90
91/**********************************************************************/
95{
96 return &(texai_world.map);
97}
98
99/**********************************************************************/
103{
105}
106
107/**********************************************************************/
110void 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
120 }
121}
122
123/**********************************************************************/
126void 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/**********************************************************************/
144static 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
155 }
156}
157
158/**********************************************************************/
161void texai_city_created(struct city *pcity)
162{
164}
165
166/**********************************************************************/
169void texai_city_changed(struct city *pcity)
170{
172}
173
174/**********************************************************************/
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
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
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/**********************************************************************/
218struct city *texai_map_city(int city_id)
219{
220 return idex_lookup_city(&texai_world, city_id);
221}
222
223/**********************************************************************/
226void 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
234 }
235}
236
237/**********************************************************************/
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);
251 city_list_remove(plr_data->cities, 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/**********************************************************************/
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;
275
277 }
278}
279
280/**********************************************************************/
287
288/**********************************************************************/
295
296/**********************************************************************/
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
308 struct texai_plr *plr_data;
309
310 if (idex_lookup_unit(&texai_world, info->id) != NULL) {
311 return;
312 }
313
315
316 punit = unit_virtual_create(pplayer, NULL, type, 0);
317 punit->id = info->id;
318
322
323 unit_tile_set(punit, ptile);
324 } else {
326
328
329 punit->utype = type;
330 }
331
332 free(info);
333}
334
335/**********************************************************************/
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
346 }
347}
348
349/**********************************************************************/
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) {
360
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/**********************************************************************/
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;
383
385 }
386}
387
388/**********************************************************************/
391void 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) {
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}
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3426
void destroy_city_virtual(struct city *pcity)
Definition city.c:3512
#define city_tile(_pcity_)
Definition city.h:564
#define city_owner(_pcity_)
Definition city.h:563
char * incite_cost
Definition comments.c:74
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
GType type
Definition repodlgs.c:1313
void idex_free(struct world *iworld)
Definition idex.c:54
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:82
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:133
void idex_unregister_city(struct world *iworld, struct city *pcity)
Definition idex.c:97
void idex_init(struct world *iworld)
Definition idex.c:45
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:146
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Definition idex.c:115
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:67
void adv_city_free(struct city *pcity)
Definition infracache.c:502
void adv_city_alloc(struct city *pcity)
Definition infracache.c:489
#define fc_assert(condition)
Definition log.h:176
#define log_error(message,...)
Definition log.h:103
void map_free(struct civ_map *fmap)
Definition map.c:536
void map_allocate(struct civ_map *amap)
Definition map.c:499
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:462
void map_init(struct civ_map *imap, bool server_side)
Definition map.c:156
#define fc_malloc(sz)
Definition mem.h:34
void * player_ai_data(const struct player *pplayer, const struct ai_type *ai)
Definition player.c:1946
struct player * player_by_number(const int player_id)
Definition player.c:849
int player_number(const struct player *pplayer)
Definition player.c:837
Definition city.h:320
int id
Definition city.h:326
struct player * owner
Definition city.h:323
struct tile * tiles
Definition map_types.h:84
struct terrain * terrain
Definition texaiworld.c:36
Definition tile.h:50
bv_extras extras
Definition tile.h:55
struct unit_list * units
Definition tile.h:58
struct terrain * terrain
Definition tile.h:57
Definition unit.h:140
int id
Definition unit.h:147
struct tile * tile
Definition unit.h:142
const struct unit_type * utype
Definition unit.h:141
struct player * owner
Definition unit.h:145
struct civ_map map
#define TRUE
Definition support.h:46
struct ai_type * texai_get_self(void)
Definition texai.c:61
void texai_send_msg(enum texaimsgtype type, struct player *pplayer, void *data)
Definition texaimsg.c:26
bool texai_thread_running(void)
static void texai_city_update(struct city *pcity, enum texaimsgtype msgtype)
Definition texaiworld.c:144
void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
Definition texaiworld.c:177
struct civ_map * texai_map_get(void)
Definition texaiworld.c:94
void texai_city_created(struct city *pcity)
Definition texaiworld.c:161
void texai_city_destruction_recv(void *data)
Definition texaiworld.c:240
void texai_city_destroyed(struct city *pcity)
Definition texaiworld.c:226
void texai_unit_destroyed(struct unit *punit)
Definition texaiworld.c:338
static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
Definition texaiworld.c:265
void texai_tile_info_recv(void *data)
Definition texaiworld.c:126
void texai_map_close(void)
Definition texaiworld.c:102
void texai_unit_changed(struct unit *punit)
Definition texaiworld.c:291
void texai_world_init(void)
Definition texaiworld.c:69
void texai_tile_info(struct tile *ptile)
Definition texaiworld.c:110
void texai_city_changed(struct city *pcity)
Definition texaiworld.c:169
void texai_unit_move_seen(struct unit *punit)
Definition texaiworld.c:376
void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
Definition texaiworld.c:299
void texai_unit_created(struct unit *punit)
Definition texaiworld.c:283
struct city * texai_map_city(int city_id)
Definition texaiworld.c:218
void texai_unit_moved_recv(void *data)
Definition texaiworld.c:391
void texai_map_init(void)
Definition texaiworld.c:85
static struct world texai_world
Definition texaiworld.c:31
void texai_world_close(void)
Definition texaiworld.c:77
void texai_unit_destruction_recv(void *data)
Definition texaiworld.c:352
void tile_set_worked(struct tile *ptile, struct city *pcity)
Definition tile.c:106
#define tile_index(_pt_)
Definition tile.h:89
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1662
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1767
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1285
#define unit_tile(_pu)
Definition unit.h:397
#define unit_owner(_pu)
Definition unit.h:396
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112
Unit_type_id utype_number(const struct unit_type *punittype)
Definition unittype.c:100