Freeciv-3.2
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
41static struct tile *previous_tiles = NULL;
42static struct unit_list *previous_units;
43
44/**********************************************************************/
47static 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/**********************************************************************/
60static void sha_unit_change(int id)
61{
62 struct unit *punit = game_unit_by_number(id);
64
65 log_debug("sha got unit: %d", id);
66
68 *pold_unit = *punit;
69}
70
71/**********************************************************************/
74static void sha_unit_new(int id)
75{
76 struct unit *punit = game_unit_by_number(id);
78
79 log_debug("sha got unit: %d", id);
80
81 *pold_unit = *punit;
83}
84
85/**********************************************************************/
88static void sha_unit_remove(int id)
89{
91
92 log_debug("sha got unit: %d", id);
93
96 /* list pointers were struct copied, cannot unit_virtual_destroy() */
97 memset(pold_unit, 0, sizeof(*pold_unit)); /* ensure no pointers remain */
99}
100
101/**********************************************************************/
105{
106 struct agent self;
107
110
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;
125}
126
127/**********************************************************************/
134
135/**************************************************************************
136 Public interface
137**************************************************************************/
138
139/**********************************************************************/
142struct tile *sha_tile_recall(struct tile *ptile)
143{
144 return &previous_tiles[tile_index(ptile)];
145}
146
147/**********************************************************************/
150struct unit *sha_unit_recall(int id)
151{
152 return unit_list_find(previous_units, id);
153}
void register_agent(const struct agent *agent)
Definition agents.c:381
@ CB_NEW
Definition agents.h:37
@ CB_REMOVE
Definition agents.h:37
@ CB_CHANGE
Definition agents.h:37
#define LAST_AGENT_LEVEL
Definition agents.h:32
static struct ai_type * self
Definition classicai.c:45
char * incite_cost
Definition comments.c:75
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 * game_unit_by_number(int id)
Definition game.c:116
#define fc_assert_ret(condition)
Definition log.h:191
#define log_debug(message,...)
Definition log.h:115
#define MAP_INDEX_SIZE
Definition map.h:137
#define fc_malloc(sz)
Definition mem.h:34
void simple_historian_init(void)
Definition sha.c:104
static void sha_unit_remove(int id)
Definition sha.c:88
static struct unit_list * previous_units
Definition sha.c:42
struct unit * sha_unit_recall(int id)
Definition sha.c:150
static struct tile * previous_tiles
Definition sha.c:41
static void sha_unit_change(int id)
Definition sha.c:60
struct tile * sha_tile_recall(struct tile *ptile)
Definition sha.c:142
void simple_historian_done(void)
Definition sha.c:130
static void sha_unit_new(int id)
Definition sha.c:74
static void sha_tile_update(struct tile *ptile)
Definition sha.c:47
Definition agents.h:40
char name[MAX_LEN_NAME]
Definition ai.h:51
Definition tile.h:50
Definition unit.h:138
#define sz_strlcpy(dest, src)
Definition support.h:195
#define tile_index(_pt_)
Definition tile.h:88
#define TILE_XY(ptile)
Definition tile.h:43
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1624
#define unit_owner(_pu)
Definition unit.h:396
struct unit * unit_list_find(const struct unit_list *punitlist, int unit_id)
Definition unitlist.c:30