Freeciv-3.3
Loading...
Searching...
No Matches
texaiplayer.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/* utility */
19#include "log.h"
20
21/* common */
22#include "ai.h"
23#include "city.h"
24#include "game.h"
25#include "map.h"
26#include "unit.h"
27
28/* server/advisors */
29#include "advchoice.h"
30#include "infracache.h"
31
32/* ai/default */
33#include "daimilitary.h"
34#include "daiplayer.h"
35
36/* ai/tex */
37#include "texaicity.h"
38#include "texaiworld.h"
39
40#include "texaiplayer.h"
41
42/* What level of operation we should abort because
43 * of received messages. Lower is more critical;
44 * TEXAI_ABORT_EXIT means that whole thread should exit,
45 * TEXAI_ABORT_NONE means that we can continue what we were doing */
52
54
63
69
70/**********************************************************************/
79
80/**********************************************************************/
83static void texai_thread_start(void *arg)
84{
85 bool finished = FALSE;
86 struct ai_type *texai = arg;
87
88 log_debug("New AI thread launched");
89
91 if (!map_is_empty()) {
93 }
94
95 /* Just wait until we are signaled to shutdown */
97 while (!finished) {
99
101 finished = TRUE;
102 }
103 }
105
107
108 log_debug("AI thread exiting");
109}
110
111/**********************************************************************/
118
119/**********************************************************************/
123{
124 whole_map_iterate(&(wld.map), ptile) {
125 texai_tile_info(ptile);
127}
128
129/**********************************************************************/
132static void texai_map_alloc_recv(void)
133{
135}
136
137/**********************************************************************/
144
145/**********************************************************************/
148static void texai_map_free_recv(void)
149{
151}
152
153/**********************************************************************/
156struct unit_list *texai_player_units(struct player *pplayer)
157{
158 struct texai_plr *plr_data = player_ai_data(pplayer, texai_get_self());
159
160 return plr_data->units;
161}
162
163/**********************************************************************/
167{
169
172 struct texai_msg *msg;
174
178
179 log_debug("Plr thr got %s", texaimsgtype_name(msg->type));
180
181 switch (msg->type) {
184
186
187 /* Use _safe iterate in case the main thread
188 * destroyes cities while we are iterating through these. */
190 struct adv_choice *choice;
192 = fc_malloc(sizeof(struct texai_build_choice_req));
193 struct city *tex_city = texai_map_city(pcity->id);
194
197
198 if (tex_city != NULL) {
200 msg->plr, tex_city,
202 choice_req->city_id = tex_city->id;
203 adv_choice_copy(&(choice_req->choice), choice);
204 adv_free_choice(choice);
206 }
207
208 /* Release mutex for a second in case main thread
209 * wants to do something to city list. */
211
212 /* Recursive message check in case phase is finished. */
216 break;
217 }
220
222
223 break;
226 break;
229 break;
232 texai_unit_info_recv(msg->data, msg->type);
233 break;
236 break;
239 texai_city_info_recv(msg->data, msg->type);
240 break;
243 break;
246 break;
249 break;
252 break;
255 break;
256 default:
257 log_error("Illegal message type %s (%d) for tex ai!",
258 texaimsgtype_name(msg->type), msg->type);
259 break;
260 }
261
262 if (new_abort < ret_abort) {
264 }
265
266 FC_FREE(msg);
267
269 }
271
272 return ret_abort;
273}
274
275/**********************************************************************/
278void texai_player_alloc(struct ai_type *ait, struct player *pplayer)
279{
280 struct texai_plr *player_data = fc_calloc(1, sizeof(struct texai_plr));
281
282 player_set_ai_data(pplayer, ait, player_data);
283
284 /* Default AI */
285 dai_data_init(ait, pplayer);
286
287 player_data->units = unit_list_new();
288 player_data->cities = city_list_new();
289}
290
291/**********************************************************************/
294void texai_player_free(struct ai_type *ait, struct player *pplayer)
295{
296 struct texai_plr *player_data = player_ai_data(pplayer, ait);
297
298 /* Default AI */
299 dai_data_close(ait, pplayer);
300
301 if (player_data != NULL) {
302 player_set_ai_data(pplayer, ait, NULL);
306 }
307}
308
309/**********************************************************************/
339
340/**********************************************************************/
343void texai_control_lost(struct ai_type *ait, struct player *pplayer)
344{
346
347 log_debug("%s no longer under tex AI (%d)", pplayer->name,
349
350 if (exthrai.num_players <= 0) {
352
355
360 }
361}
362
363/**********************************************************************/
366void texai_refresh(struct ai_type *ait, struct player *pplayer)
367{
371 struct texai_req *req;
372
375
377
378 log_debug("Plr thr sent %s", texaireqtype_name(req->type));
379
380 switch (req->type) {
383 break;
385 {
387 = (struct texai_build_choice_req *)(req->data);
388 struct city *pcity = game_city_by_number(choice_req->city_id);
389
390 if (pcity != NULL && city_owner(pcity) == req->plr) {
391 adv_choice_copy(&(def_ai_city_data(pcity, ait)->choice),
392 &(choice_req->choice));
394 }
395 }
396 break;
398 req->plr->ai_phase_done = TRUE;
399 break;
400 }
401
402 FC_FREE(req);
403
405 }
407 }
408}
409
410/**********************************************************************/
421
422/**********************************************************************/
431
432/**********************************************************************/
436{
437 return exthrai.thread_running;
438}
void adv_free_choice(struct adv_choice *choice)
Definition advchoice.c:71
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition advchoice.h:79
#define city_list_iterate_safe(citylist, _city)
Definition city.h:519
#define city_list_iterate(citylist, pcity)
Definition city.h:505
#define city_owner(_pcity_)
Definition city.h:560
#define city_list_iterate_end
Definition city.h:507
#define city_list_iterate_safe_end
Definition city.h:541
char * incite_cost
Definition comments.c:76
void dai_data_init(struct ai_type *ait, struct player *pplayer)
Definition daidata.c:59
void dai_data_close(struct ai_type *ait, struct player *pplayer)
Definition daidata.c:104
struct adv_choice * military_advisor_choose_build(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct city *pcity, player_unit_list_getter ul_cb)
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition daiplayer.h:42
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
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 int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
void fc_thread_cond_destroy(fc_thread_cond *cond)
Definition fcthread.c:385
void fc_thread_cond_signal(fc_thread_cond *cond)
Definition fcthread.c:397
void fc_thread_cond_wait(fc_thread_cond *cond, fc_mutex *mutex)
Definition fcthread.c:391
void fc_thread_cond_init(fc_thread_cond *cond)
Definition fcthread.c:379
void fc_mutex_allocate(fc_mutex *mutex)
int fc_thread_start(fc_thread *thread, void(*function)(void *arg), void *arg)
void fc_mutex_init(fc_mutex *mutex)
void fc_mutex_release(fc_mutex *mutex)
void fc_thread_wait(fc_thread *thread)
void fc_mutex_destroy(fc_mutex *mutex)
struct civ_game game
Definition game.c:61
struct world wld
Definition game.c:62
struct city * game_city_by_number(int id)
Definition game.c:106
void initialize_infrastructure_cache(struct player *pplayer)
Definition infracache.c:250
#define log_debug(message,...)
Definition log.h:116
#define log_error(message,...)
Definition log.h:104
bool map_is_empty(void)
Definition map.c:148
#define whole_map_iterate(_map, _tile)
Definition map.h:573
#define whole_map_iterate_end
Definition map.h:582
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#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
void player_set_ai_data(struct player *pplayer, const struct ai_type *ai, void *data)
Definition player.c:1954
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
Definition ai.h:50
Definition city.h:317
struct civ_game::@32::@36::@41 mutexes
fc_mutex city_list
Definition game.h:278
struct civ_game::@32::@36 server
struct city_list * cities
Definition player.h:281
char name[MAX_LEN_NAME]
Definition player.h:251
bool ai_phase_done
Definition player.h:264
struct adv_choice choice
Definition texaiplayer.c:67
struct player * plr
Definition texaimsg.h:57
enum texaimsgtype type
Definition texaimsg.h:56
void * data
Definition texaimsg.h:58
fc_mutex mutex
Definition texaiplayer.h:33
fc_thread_cond thr_cond
Definition texaiplayer.h:32
struct texaimsg_list * msglist
Definition texaiplayer.h:34
void * data
Definition texaimsg.h:65
enum texaireqtype type
Definition texaimsg.h:63
struct player * plr
Definition texaimsg.h:64
struct texaireq_list * reqlist
Definition texaiplayer.h:39
bool thread_running
Definition texaiplayer.c:60
struct texai_reqs reqs_from
Definition texaiplayer.c:59
int num_players
Definition texaiplayer.c:57
struct texai_msgs msgs_to
Definition texaiplayer.c:58
fc_thread ait
Definition texaiplayer.c:61
int id
Definition unit.h:147
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct ai_type * texai_get_self(void)
Definition texai.c:61
void texai_city_worker_requests_create(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition texaicity.c:72
void texai_city_worker_wants(struct ai_type *ait, struct player *pplayer, struct city *pcity)
Definition texaicity.c:93
void texai_req_worker_task_rcv(struct texai_req *req)
Definition texaicity.c:533
void texai_send_msg(enum texaimsgtype type, struct player *pplayer, void *data)
Definition texaimsg.c:26
void texai_send_req(enum texaireqtype type, struct player *pplayer, void *data)
Definition texaimsg.c:48
void texai_player_alloc(struct ai_type *ait, struct player *pplayer)
struct texai_thr exthrai
void texai_control_gained(struct ai_type *ait, struct player *pplayer)
void texai_player_free(struct ai_type *ait, struct player *pplayer)
struct unit_list * texai_player_units(struct player *pplayer)
bool texai_thread_running(void)
void texai_control_lost(struct ai_type *ait, struct player *pplayer)
void texai_req_from_thr(struct texai_req *req)
static void texai_thread_start(void *arg)
Definition texaiplayer.c:83
static void texai_map_free_recv(void)
texai_abort_msg_class
Definition texaiplayer.c:47
@ TEXAI_ABORT_NONE
Definition texaiplayer.c:50
@ TEXAI_ABORT_PHASE_END
Definition texaiplayer.c:49
@ TEXAI_ABORT_EXIT
Definition texaiplayer.c:48
static enum texai_abort_msg_class texai_check_messages(struct ai_type *ait)
void texai_refresh(struct ai_type *ait, struct player *pplayer)
void texai_init_threading(void)
Definition texaiplayer.c:73
void texai_msg_to_thr(struct texai_msg *msg)
void texai_map_alloc(void)
void texai_whole_map_copy(void)
static void texai_map_alloc_recv(void)
void texai_map_free(void)
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_tile_info_recv(void *data)
Definition texaiworld.c:127
void texai_map_close(void)
Definition texaiworld.c:103
void texai_world_init(void)
Definition texaiworld.c:69
void texai_tile_info(struct tile *ptile)
Definition texaiworld.c:111
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
void texai_world_close(void)
Definition texaiworld.c:77
void texai_unit_destruction_recv(void *data)
Definition texaiworld.c:353
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33