Freeciv-3.1
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
34{
35 int index;
37 bv_extras extras;
38};
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/**********************************************************************/
91
92/**********************************************************************/
96{
97 return &(texai_world.map);
98}
99
100/**********************************************************************/
104{
106}
107
108/**********************************************************************/
111void texai_tile_info(struct tile *ptile)
112{
113 if (texai_thread_running()) {
114 struct texai_tile_info_msg *info = fc_malloc(sizeof(struct texai_tile_info_msg));
115
116 info->index = tile_index(ptile);
117 info->terrain = ptile->terrain;
118 info->extras = ptile->extras;
119
120 texai_send_msg(TEXAI_MSG_TILE_INFO, NULL, info);
121 }
122}
123
124/**********************************************************************/
127void texai_tile_info_recv(void *data)
128{
129 struct texai_tile_info_msg *info = (struct texai_tile_info_msg *)data;
130
131 if (texai_world.map.tiles != NULL) {
132 struct tile *ptile;
133
134 ptile = index_to_tile(&(texai_world.map), info->index);
135 ptile->terrain = info->terrain;
136 ptile->extras = info->extras;
137 }
138
139 free(info);
140}
141
142/**********************************************************************/
145static void texai_city_update(struct city *pcity, enum texaimsgtype msgtype)
146{
147 if (texai_thread_running()) {
148 struct texai_city_info_msg *info
149 = fc_malloc(sizeof(struct texai_city_info_msg));
150
151 info->id = pcity->id;
152 info->owner = player_number(city_owner(pcity));
153 info->tindex = tile_index(city_tile(pcity));
154
155 texai_send_msg(msgtype, NULL, info);
156 }
157}
158
159/**********************************************************************/
162void texai_city_created(struct city *pcity)
163{
164 texai_city_update(pcity, TEXAI_MSG_CITY_CREATED);
165}
166
167/**********************************************************************/
170void texai_city_changed(struct city *pcity)
171{
172 texai_city_update(pcity, TEXAI_MSG_CITY_CHANGED);
173}
174
175/**********************************************************************/
178void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
179{
180 struct texai_city_info_msg *info = (struct texai_city_info_msg *)data;
181 struct city *pcity;
182 struct player *pplayer = player_by_number(info->owner);
183
184 if (msgtype == TEXAI_MSG_CITY_CREATED) {
185 struct tile *ptile;
186 struct texai_plr *plr_data;
187
188 if (idex_lookup_city(&texai_world, info->id) != NULL) {
189 return;
190 }
191
192 ptile = index_to_tile(&(texai_world.map), info->tindex);
193
194 pcity = create_city_virtual(pplayer, ptile, "");
195 adv_city_alloc(pcity);
196 pcity->id = info->id;
197
199 plr_data = player_ai_data(pplayer, texai_get_self());
200 city_list_prepend(plr_data->cities, pcity);
201 tile_set_worked(ptile, pcity);
202 } else {
203 pcity = idex_lookup_city(&texai_world, info->id);
204
205 if (pcity != NULL) {
206 pcity->owner = pplayer;
207 } else {
208 log_error("Tex: requested change on city id %d that's not known.",
209 info->id);
210 }
211 }
212
213 free(info);
214}
215
216/**********************************************************************/
219struct city *texai_map_city(int city_id)
220{
221 return idex_lookup_city(&texai_world, city_id);
222}
223
224/**********************************************************************/
227void texai_city_destroyed(struct city *pcity)
228{
229 if (texai_thread_running()) {
230 struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
231
232 info->id = pcity->id;
233
234 texai_send_msg(TEXAI_MSG_CITY_DESTROYED, NULL, info);
235 }
236}
237
238/**********************************************************************/
242{
243 struct texai_id_msg *info = (struct texai_id_msg *)data;
244 struct city *pcity = idex_lookup_city(&texai_world, info->id);
245
246 if (pcity != NULL) {
247 struct texai_plr *plr_data;
248
249 adv_city_free(pcity);
250 tile_set_worked(city_tile(pcity), NULL);
251 plr_data = player_ai_data(city_owner(pcity), texai_get_self());
252 city_list_remove(plr_data->cities, pcity);
255 } else {
256 log_error("Tex: requested removal of city id %d that's not known.",
257 info->id);
258 }
259
260 free(info);
261}
262
263/**********************************************************************/
266static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
267{
268 if (texai_thread_running()) {
269 struct texai_unit_info_msg *info
270 = fc_malloc(sizeof(struct texai_unit_info_msg));
271
272 info->id = punit->id;
276
277 texai_send_msg(msgtype, NULL, info);
278 }
279}
280
281/**********************************************************************/
285{
286 texai_unit_update(punit, TEXAI_MSG_UNIT_CREATED);
287}
288
289/**********************************************************************/
293{
294 texai_unit_update(punit, TEXAI_MSG_UNIT_CHANGED);
295}
296
297/**********************************************************************/
300void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
301{
302 struct texai_unit_info_msg *info = (struct texai_unit_info_msg *)data;
303 struct unit *punit;
304 struct player *pplayer = player_by_number(info->owner);
305 struct unit_type *type = utype_by_number(info->type);
306 struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
307
308 if (msgtype == TEXAI_MSG_UNIT_CREATED) {
309 struct texai_plr *plr_data;
310
311 if (idex_lookup_unit(&texai_world, info->id) != NULL) {
312 return;
313 }
314
315 plr_data = player_ai_data(pplayer, texai_get_self());
316
317 punit = unit_virtual_create(pplayer, NULL, type, 0);
318 punit->id = info->id;
319
321 unit_list_prepend(ptile->units, punit);
322 unit_list_prepend(plr_data->units, punit);
323
324 unit_tile_set(punit, ptile);
325 } else {
326 fc_assert(msgtype == TEXAI_MSG_UNIT_CHANGED);
327
329
330 punit->utype = type;
331 }
332
333 free(info);
334}
335
336/**********************************************************************/
340{
341 if (texai_thread_running()) {
342 struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
343
344 info->id = punit->id;
345
346 texai_send_msg(TEXAI_MSG_UNIT_DESTROYED, NULL, info);
347 }
348}
349
350/**********************************************************************/
354{
355 struct texai_id_msg *info = (struct texai_id_msg *)data;
356 struct unit *punit = idex_lookup_unit(&texai_world, info->id);
357
358 if (punit != NULL) {
359 struct texai_plr *plr_data = player_ai_data(punit->owner,
361
362 unit_list_remove(punit->tile->units, punit);
363 unit_list_remove(plr_data->units, punit);
366 } else {
367 log_error("Tex: requested removal of unit id %d that's not known.",
368 info->id);
369 }
370
371 free(info);
372}
373
374/**********************************************************************/
378{
379 if (texai_thread_running()) {
380 struct texai_unit_move_msg *info = fc_malloc(sizeof(struct texai_unit_move_msg));
381
382 info->id = punit->id;
384
385 texai_send_msg(TEXAI_MSG_UNIT_MOVED, NULL, info);
386 }
387}
388
389/**********************************************************************/
392void texai_unit_moved_recv(void *data)
393{
394 struct texai_unit_move_msg *info = (struct texai_unit_move_msg *)data;
395 struct unit *punit = idex_lookup_unit(&texai_world, info->id);
396 struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
397
398 if (punit != NULL) {
399 unit_list_remove(punit->tile->units, punit);
400 unit_list_prepend(ptile->units, punit);
401
402 unit_tile_set(punit, ptile);
403 } else {
404 log_error("Tex: requested moving of unit id %d that's not known.",
405 info->id);
406 }
407
408 free(info);
409}
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3343
void destroy_city_virtual(struct city *pcity)
Definition city.c:3419
#define city_tile(_pcity_)
Definition city.h:544
#define city_owner(_pcity_)
Definition city.h:543
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:73
GType type
Definition repodlgs.c:1312
void idex_free(struct world *iworld)
Definition idex.c:53
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:81
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:132
void idex_unregister_city(struct world *iworld, struct city *pcity)
Definition idex.c:96
void idex_init(struct world *iworld)
Definition idex.c:44
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:145
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Definition idex.c:114
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:66
void adv_city_free(struct city *pcity)
Definition infracache.c:501
void adv_city_alloc(struct city *pcity)
Definition infracache.c:488
#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:528
void map_allocate(struct civ_map *amap)
Definition map.c:491
void map_init_topology(struct civ_map *nmap)
Definition map.c:301
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:454
void map_init(struct civ_map *imap, bool server_side)
Definition map.c:157
#define fc_malloc(sz)
Definition mem.h:34
void * player_ai_data(const struct player *pplayer, const struct ai_type *ai)
Definition player.c:1904
struct player * player_by_number(const int player_id)
Definition player.c:840
int player_number(const struct player *pplayer)
Definition player.c:828
Definition city.h:309
int id
Definition city.h:315
struct player * owner
Definition city.h:312
struct tile * tiles
Definition map_types.h:80
struct unit_list * units
Definition texaiplayer.h:45
struct city_list * cities
Definition texaiplayer.h:46
struct terrain * terrain
Definition texaiworld.c:36
Definition tile.h:49
bv_extras extras
Definition tile.h:54
struct unit_list * units
Definition tile.h:57
struct terrain * terrain
Definition tile.h:56
Definition unit.h:138
int id
Definition unit.h:145
struct tile * tile
Definition unit.h:140
const struct unit_type * utype
Definition unit.h:139
struct player * owner
Definition unit.h:143
struct civ_map map
#define TRUE
Definition support.h:46
struct ai_type * texai_get_self(void)
Definition texai.c:60
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:145
void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
Definition texaiworld.c:178
struct civ_map * texai_map_get(void)
Definition texaiworld.c:95
void texai_city_created(struct city *pcity)
Definition texaiworld.c:162
void texai_city_destruction_recv(void *data)
Definition texaiworld.c:241
void texai_city_destroyed(struct city *pcity)
Definition texaiworld.c:227
void texai_unit_destroyed(struct unit *punit)
Definition texaiworld.c:339
static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
Definition texaiworld.c:266
void texai_tile_info_recv(void *data)
Definition texaiworld.c:127
void texai_map_close(void)
Definition texaiworld.c:103
void texai_unit_changed(struct unit *punit)
Definition texaiworld.c:292
void texai_world_init(void)
Definition texaiworld.c:69
void texai_tile_info(struct tile *ptile)
Definition texaiworld.c:111
void texai_city_changed(struct city *pcity)
Definition texaiworld.c:170
void texai_unit_move_seen(struct unit *punit)
Definition texaiworld.c:377
void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
Definition texaiworld.c:300
void texai_unit_created(struct unit *punit)
Definition texaiworld.c:284
struct city * texai_map_city(int city_id)
Definition texaiworld.c:219
void texai_unit_moved_recv(void *data)
Definition texaiworld.c:392
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:353
void tile_set_worked(struct tile *ptile, struct city *pcity)
Definition tile.c:106
#define tile_index(_pt_)
Definition tile.h:87
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
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
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