Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/agents/sha.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
sha.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2004 - A. Gorshenev
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#include "support.h"
21
22/* common */
23#include "game.h"
24#include "map.h"
25#include "unitlist.h"
26
27/* client/agents */
28#include "agents.h"
29
30#include "sha.h"
31
32
33/**************************************************************************
34 This is the simple historian agent.
35 It just saves the last states of all tiles and units.
36 The trick is just to call this agent the last of all
37 so it still keeps old values whereas all other agents
38 already got the new ones.
39**************************************************************************/
40
41 static struct tile *previous_tiles = NULL;
42 static struct unit_list *previous_units;
43
44/**********************************************************************/
47 static void sha_tile_update(struct tile *ptile)
48{
49 log_debug("sha got tile: %d ~= (%d, %d)",
50 tile_index(ptile), TILE_XY(ptile));
51
52#if 0
53 previous_tiles[tile_index(ptile)] = *ptile;
54#endif
55}
56
57/**********************************************************************/
60 static void sha_unit_change(int id)
61{
62 struct unit *punit = game_unit_by_number(id);
63 struct unit *pold_unit = unit_list_find(previous_units, id);
64
65 log_debug("sha got unit: %d", id);
66
67 fc_assert_ret(NULL != pold_unit);
68 *pold_unit = *punit;
69}
70
71/**********************************************************************/
74 static void sha_unit_new(int id)
75{
76 struct unit *punit = game_unit_by_number(id);
77 struct unit *pold_unit = unit_virtual_create(unit_owner(punit), NULL, 0, 0);
78
79 log_debug("sha got unit: %d", id);
80
81 *pold_unit = *punit;
82 unit_list_prepend(previous_units, pold_unit);
83}
84
85/**********************************************************************/
88 static void sha_unit_remove(int id)
89{
90 struct unit *pold_unit = unit_list_find(previous_units, id);;
91
92 log_debug("sha got unit: %d", id);
93
94 fc_assert_ret(NULL != pold_unit);
95 unit_list_remove(previous_units, pold_unit);
96 /* list pointers were struct copied, cannot unit_virtual_destroy() */
97 memset(pold_unit, 0, sizeof(*pold_unit)); /* ensure no pointers remain */
98 free(pold_unit);
99}
100
101/**********************************************************************/
104 void simple_historian_init(void)
105{
106 struct agent self;
107
108 previous_tiles = fc_malloc(MAP_INDEX_SIZE * sizeof(*previous_tiles));
109 memset(previous_tiles, 0, MAP_INDEX_SIZE * sizeof(*previous_tiles));
110
111 previous_units = unit_list_new();
112
113 memset(&self, 0, sizeof(self));
114 sz_strlcpy(self.name, "Simple Historian");
115
116 self.level = LAST_AGENT_LEVEL;
117
118 self.unit_callbacks[CB_REMOVE] = sha_unit_remove;
119 self.unit_callbacks[CB_CHANGE] = sha_unit_change;
120 self.unit_callbacks[CB_NEW] = sha_unit_new;
121 self.tile_callbacks[CB_REMOVE] = sha_tile_update;
122 self.tile_callbacks[CB_CHANGE] = sha_tile_update;
123 self.tile_callbacks[CB_NEW] = sha_tile_update;
124 register_agent(&self);
125}
126
127/**********************************************************************/
130 void simple_historian_done(void)
131{
132 unit_list_destroy(previous_units);
133}
134
135/**************************************************************************
136 Public interface
137**************************************************************************/
138
139/**********************************************************************/
142 struct tile *sha_tile_recall(struct tile *ptile)
143{
144 return &previous_tiles[tile_index(ptile)];
145}
146
147/**********************************************************************/
150 struct unit *sha_unit_recall(int id)
151{
152 return unit_list_find(previous_units, id);
153}
register_agent
void register_agent(const struct agent *agent)
Definition agents.c:378
agents.h
CB_NEW
@ CB_NEW
Definition agents.h:37
CB_REMOVE
@ CB_REMOVE
Definition agents.h:37
CB_CHANGE
@ CB_CHANGE
Definition agents.h:37
LAST_AGENT_LEVEL
#define LAST_AGENT_LEVEL
Definition agents.h:32
self
static struct ai_type * self
Definition classicai.c:46
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:73
game_unit_by_number
struct unit * game_unit_by_number(int id)
Definition game.c:111
game.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
log_debug
#define log_debug(message,...)
Definition log.h:115
map.h
MAP_INDEX_SIZE
#define MAP_INDEX_SIZE
Definition map.h:131
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
simple_historian_init
void simple_historian_init(void)
Definition sha.c:104
sha_unit_remove
static void sha_unit_remove(int id)
Definition sha.c:88
previous_units
static struct unit_list * previous_units
Definition sha.c:42
sha_unit_recall
struct unit * sha_unit_recall(int id)
Definition sha.c:150
previous_tiles
static struct tile * previous_tiles
Definition sha.c:41
sha_unit_change
static void sha_unit_change(int id)
Definition sha.c:60
sha_tile_recall
struct tile * sha_tile_recall(struct tile *ptile)
Definition sha.c:142
simple_historian_done
void simple_historian_done(void)
Definition sha.c:130
sha_unit_new
static void sha_unit_new(int id)
Definition sha.c:74
sha_tile_update
static void sha_tile_update(struct tile *ptile)
Definition sha.c:47
sha.h
agent
Definition agents.h:40
ai_type::name
char name[MAX_LEN_NAME]
Definition ai.h:51
tile
Definition tile.h:49
unit
Definition unit.h:138
support.h
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:161
tile_index
#define tile_index(_pt_)
Definition tile.h:87
TILE_XY
#define TILE_XY(ptile)
Definition tile.h:42
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:1613
unit_owner
#define unit_owner(_pu)
Definition unit.h:387
unit_list_find
struct unit * unit_list_find(const struct unit_list *punitlist, int unit_id)
Definition unitlist.c:30
unitlist.h