Freeciv21
Develop your civilization from humble roots to a global empire
texaiworld.c
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <fc_config.h>
15 
16 /* common */
17 #include "idex.h"
18 #include "map.h"
19 #include "world_object.h"
20 
21 /* server/advisors */
22 #include "infracache.h"
23 
24 /* ai/tex */
25 #include "texaiplayer.h"
26 
27 #include "texaiworld.h"
28 
29 static struct world texai_world;
30 
32  int index;
33  struct terrain *terrain;
34  bv_extras extras;
35 };
36 
38  int id;
39  int owner;
40  int tindex;
41 };
42 
43 struct texai_id_msg {
44  int id;
45 };
46 
48  int id;
49  int owner;
50  int tindex;
51  int type;
52 };
53 
55  int id;
56  int tindex;
57 };
58 
59 /**********************************************************************/
63 
64 /**********************************************************************/
68 
69 /**********************************************************************/
72 void texai_map_init(void)
73 {
74  map_init(&(texai_world.map), true);
76 }
77 
78 /**********************************************************************/
81 struct civ_map *texai_map_get(void) { return &(texai_world.map); }
82 
83 /**********************************************************************/
87 
88 /**********************************************************************/
91 void texai_tile_info(struct tile *ptile)
92 {
93  if (texai_thread_running()) {
94  struct texai_tile_info_msg *info =
95  fc_malloc(sizeof(struct texai_tile_info_msg));
96 
97  info->index = tile_index(ptile);
98  info->terrain = ptile->terrain;
99  info->extras = ptile->extras;
100 
101  texai_send_msg(TEXAI_MSG_TILE_INFO, NULL, info);
102  }
103 }
104 
105 /**********************************************************************/
108 void texai_tile_info_recv(void *data)
109 {
110  struct texai_tile_info_msg *info = (struct texai_tile_info_msg *) data;
111 
112  if (texai_world.map.tiles != NULL) {
113  struct tile *ptile;
114 
115  ptile = index_to_tile(&(texai_world.map), info->index);
116  ptile->terrain = info->terrain;
117  ptile->extras = info->extras;
118  }
119 
120  free(info);
121 }
122 
123 /**********************************************************************/
126 static void texai_city_update(struct city *pcity, enum texaimsgtype msgtype)
127 {
128  if (texai_thread_running()) {
129  struct texai_city_info_msg *info =
130  fc_malloc(sizeof(struct texai_city_info_msg));
131 
132  info->id = pcity->id;
133  info->owner = player_number(city_owner(pcity));
134  info->tindex = tile_index(city_tile(pcity));
135 
136  texai_send_msg(msgtype, NULL, info);
137  }
138 }
139 
140 /**********************************************************************/
143 void texai_city_created(struct city *pcity)
144 {
145  texai_city_update(pcity, TEXAI_MSG_CITY_CREATED);
146 }
147 
148 /**********************************************************************/
151 void texai_city_changed(struct city *pcity)
152 {
153  texai_city_update(pcity, TEXAI_MSG_CITY_CHANGED);
154 }
155 
156 /**********************************************************************/
159 void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
160 {
161  struct texai_city_info_msg *info = (struct texai_city_info_msg *) data;
162  struct city *pcity;
163  struct player *pplayer = player_by_number(info->owner);
164 
165  if (msgtype == TEXAI_MSG_CITY_CREATED) {
166  struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
167 
168  pcity = create_city_virtual(pplayer, ptile, "");
169  adv_city_alloc(pcity);
170  pcity->id = info->id;
171 
173  tile_set_worked(ptile, pcity);
174  } else {
175  pcity = idex_lookup_city(&texai_world, info->id);
176 
177  if (pcity != NULL) {
178  pcity->owner = pplayer;
179  } else {
180  qCritical("Tex: requested change on city id %d that's not known.",
181  info->id);
182  }
183  }
184 }
185 
186 /**********************************************************************/
189 struct city *texai_map_city(int city_id)
190 {
191  return idex_lookup_city(&texai_world, city_id);
192 }
193 
194 /**********************************************************************/
197 void texai_city_destroyed(struct city *pcity)
198 {
199  if (texai_thread_running()) {
200  struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
201 
202  info->id = pcity->id;
203 
204  texai_send_msg(TEXAI_MSG_CITY_DESTROYED, NULL, info);
205  }
206 }
207 
208 /**********************************************************************/
212 {
213  struct texai_id_msg *info = (struct texai_id_msg *) data;
214  struct city *pcity = idex_lookup_city(&texai_world, info->id);
215 
216  if (pcity != NULL) {
217  adv_city_free(pcity);
218  tile_set_worked(city_tile(pcity), NULL);
220  destroy_city_virtual(pcity);
221  } else {
222  qCritical("Tex: requested removal of city id %d that's not known.",
223  info->id);
224  }
225 }
226 
227 /**********************************************************************/
230 static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
231 {
232  if (texai_thread_running()) {
233  struct texai_unit_info_msg *info =
234  fc_malloc(sizeof(struct texai_unit_info_msg));
235 
236  info->id = punit->id;
237  info->owner = player_number(unit_owner(punit));
238  info->tindex = tile_index(unit_tile(punit));
239  info->type = utype_number(unit_type_get(punit));
240 
241  texai_send_msg(msgtype, NULL, info);
242  }
243 }
244 
245 /**********************************************************************/
248 void texai_unit_created(struct unit *punit)
249 {
250  texai_unit_update(punit, TEXAI_MSG_UNIT_CREATED);
251 }
252 
253 /**********************************************************************/
256 void texai_unit_changed(struct unit *punit)
257 {
258  texai_unit_update(punit, TEXAI_MSG_UNIT_CHANGED);
259 }
260 
261 /**********************************************************************/
264 void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
265 {
266  struct texai_unit_info_msg *info = (struct texai_unit_info_msg *) data;
267  struct unit *punit;
268  struct player *pplayer = player_by_number(info->owner);
269  struct unit_type *type = utype_by_number(info->type);
270  struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
271 
272  if (msgtype == TEXAI_MSG_UNIT_CREATED) {
273  struct texai_plr *plr_data = player_ai_data(pplayer, texai_get_self());
274 
275  punit = unit_virtual_create(pplayer, NULL, type, 0);
276  punit->id = info->id;
277 
279  unit_list_prepend(ptile->units, punit);
280  unit_list_prepend(plr_data->units, punit);
281  } else if (msgtype == TEXAI_MSG_UNIT_MOVED) {
282  struct tile *old_tile;
283 
284  punit = idex_lookup_unit(&texai_world, info->id);
285 
286  old_tile = punit->tile;
287  if (old_tile != ptile) {
288  unit_list_remove(old_tile->units, punit);
289  unit_list_prepend(ptile->units, punit);
290  }
291  } else {
292  fc_assert(msgtype == TEXAI_MSG_UNIT_CHANGED);
293 
294  punit = idex_lookup_unit(&texai_world, info->id);
295 
296  punit->utype = type;
297  }
298 
299  unit_tile_set(punit, ptile);
300 }
301 
302 /**********************************************************************/
305 void texai_unit_destroyed(struct unit *punit)
306 {
307  if (texai_thread_running()) {
308  struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
309 
310  info->id = punit->id;
311 
312  texai_send_msg(TEXAI_MSG_UNIT_DESTROYED, NULL, info);
313  }
314 }
315 
316 /**********************************************************************/
320 {
321  struct texai_id_msg *info = (struct texai_id_msg *) data;
322  struct unit *punit = idex_lookup_unit(&texai_world, info->id);
323 
324  if (punit != NULL) {
325  struct texai_plr *plr_data =
327 
328  unit_list_remove(punit->tile->units, punit);
329  unit_list_remove(plr_data->units, punit);
331  unit_virtual_destroy(punit);
332  } else {
333  qCritical("Tex: requested removal of unit id %d that's not known.",
334  info->id);
335  }
336 }
337 
338 /**********************************************************************/
341 void texai_unit_move_seen(struct unit *punit)
342 {
343  if (texai_thread_running()) {
344  struct texai_unit_move_msg *info =
345  fc_malloc(sizeof(struct texai_unit_move_msg));
346 
347  info->id = punit->id;
348  info->tindex = tile_index(unit_tile(punit));
349 
350  texai_send_msg(TEXAI_MSG_UNIT_MOVED, NULL, info);
351  }
352 }
353 
354 /**********************************************************************/
357 void texai_unit_moved_recv(void *data)
358 {
359  struct texai_unit_move_msg *info = (struct texai_unit_move_msg *) data;
360  struct unit *punit = idex_lookup_unit(&texai_world, info->id);
361  struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
362 
363  if (punit != NULL) {
364  unit_list_remove(punit->tile->units, punit);
365  unit_list_prepend(ptile->units, punit);
366 
367  unit_tile_set(punit, ptile);
368  } else {
369  qCritical("Tex: requested moving of unit id %d that's not known.",
370  info->id);
371  }
372 }
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
void destroy_city_virtual(struct city *pcity)
Removes the virtual skeleton of a city.
Definition: city.cpp:3421
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Create virtual skeleton for a city.
Definition: city.cpp:3346
void idex_free(struct world *iworld)
Free the hashs.
Definition: idex.cpp:49
void idex_register_unit(struct world *iworld, struct unit *punit)
Register a unit into idex, with current punit->id.
Definition: idex.cpp:79
struct unit * idex_lookup_unit(struct world *iworld, int id)
Lookup unit with given id.
Definition: idex.cpp:152
void idex_unregister_city(struct world *iworld, struct city *pcity)
Remove a city from idex, with current pcity->id.
Definition: idex.cpp:97
void idex_init(struct world *iworld)
idex = ident index: a lookup table for quick mapping of unit and city id values to unit and city poin...
Definition: idex.cpp:40
struct city * idex_lookup_city(struct world *iworld, int id)
Lookup city with given id.
Definition: idex.cpp:139
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Remove a unit from idex, with current punit->id.
Definition: idex.cpp:118
void idex_register_city(struct world *iworld, struct city *pcity)
Register a city into idex, with current pcity->id.
Definition: idex.cpp:61
void adv_city_free(struct city *pcity)
Free advisors related city data.
Definition: infracache.cpp:510
void adv_city_alloc(struct city *pcity)
Allocate advisors related city data.
Definition: infracache.cpp:498
#define fc_assert(condition)
Definition: log.h:89
void map_free(struct civ_map *fmap)
Frees the allocated memory of the map.
Definition: map.cpp:498
void map_allocate(struct civ_map *amap)
Allocate space for map, and initialise the tiles.
Definition: map.cpp:462
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Return the tile for the given index position.
Definition: map.cpp:429
void map_init(struct civ_map *imap, bool server_side)
Put some sensible values into the map structure.
Definition: map.cpp:129
void * player_ai_data(const struct player *pplayer, const struct ai_type *ai)
Return pointer to ai data of given player and ai type.
Definition: player.cpp:1838
struct player * player_by_number(const int player_id)
Return struct player pointer for the given player index.
Definition: player.cpp:768
int player_number(const struct player *pplayer)
Return the player index/number/id.
Definition: player.cpp:756
Definition: city.h:291
int id
Definition: city.h:296
struct player * owner
Definition: city.h:294
struct tile * tiles
Definition: map_types.h:76
Definition: player.h:231
struct unit_list * units
Definition: texaiplayer.h:43
struct terrain * terrain
Definition: texaiworld.c:33
bv_extras extras
Definition: texaiworld.c:34
Definition: tile.h:42
bv_extras extras
Definition: tile.h:47
struct unit_list * units
Definition: tile.h:50
struct terrain * terrain
Definition: tile.h:49
Definition: unit.h:134
int id
Definition: unit.h:141
struct tile * tile
Definition: unit.h:136
const struct unit_type * utype
Definition: unit.h:135
struct player * owner
Definition: unit.h:139
struct civ_map map
Definition: world_object.h:21
#define fc_malloc(sz)
Definition: support.h:58
struct ai_type * texai_get_self(void)
Get pointer to ai type of the tex ai.
Definition: texai.c:58
void texai_send_msg(enum texaimsgtype type, struct player *pplayer, void *data)
Construct and send message to player thread.
Definition: texaimsg.c:24
bool texai_thread_running(void)
Return whether player thread is running.
Definition: texaiplayer.c:424
static void texai_city_update(struct city *pcity, enum texaimsgtype msgtype)
Send city information to the thread.
Definition: texaiworld.c:126
void texai_city_info_recv(void *data, enum texaimsgtype msgtype)
Receive city update to the thread.
Definition: texaiworld.c:159
void texai_city_created(struct city *pcity)
New city has been added to the main map.
Definition: texaiworld.c:143
void texai_city_destruction_recv(void *data)
Receive city destruction to the thread.
Definition: texaiworld.c:211
void texai_city_destroyed(struct city *pcity)
City has been removed from the main map.
Definition: texaiworld.c:197
void texai_unit_destroyed(struct unit *punit)
Unit has been removed from the main map.
Definition: texaiworld.c:305
static void texai_unit_update(struct unit *punit, enum texaimsgtype msgtype)
Send unit information to the thread.
Definition: texaiworld.c:230
struct civ_map * texai_map_get(void)
Return tex worldmap.
Definition: texaiworld.c:81
void texai_tile_info_recv(void *data)
Receive tile update to the thread.
Definition: texaiworld.c:108
void texai_map_close(void)
Free resources allocated for texai world map.
Definition: texaiworld.c:86
void texai_unit_changed(struct unit *punit)
Unit (potentially) changed in main map.
Definition: texaiworld.c:256
void texai_world_init(void)
Initialize world object for texai.
Definition: texaiworld.c:62
void texai_tile_info(struct tile *ptile)
Tile info updated on main map.
Definition: texaiworld.c:91
void texai_city_changed(struct city *pcity)
City on main map has (potentially) changed.
Definition: texaiworld.c:151
void texai_unit_move_seen(struct unit *punit)
Unit has moved in the main map.
Definition: texaiworld.c:341
void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
Receive unit update to the thread.
Definition: texaiworld.c:264
void texai_unit_created(struct unit *punit)
New unit has been added to the main map.
Definition: texaiworld.c:248
void texai_unit_moved_recv(void *data)
Receive unit move to the thread.
Definition: texaiworld.c:357
void texai_map_init(void)
Initialize world map for texai.
Definition: texaiworld.c:72
static struct world texai_world
Definition: texaiworld.c:29
void texai_world_close(void)
Free resources allocated for texai world object.
Definition: texaiworld.c:67
void texai_unit_destruction_recv(void *data)
Receive unit destruction to the thread.
Definition: texaiworld.c:319
struct city * texai_map_city(int city_id)
Get city from the tex map.
Definition: texaiworld.c:189
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
Definition: tile.cpp:96
#define tile_index(_pt_)
Definition: tile.h:70
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
Definition: unit.cpp:1200
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
Unit_type_id utype_number(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:91