Freeciv-3.3: /fast/freeciv/nightly/main/common/vision.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
vision.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#include "mem.h"
21#include "shared.h"
22
23/* common */
24#include "game.h"
25#include "player.h"
26#include "tile.h"
27#include "vision.h"
28
29
30/************************************************************************/
33 struct vision *vision_new(struct player *pplayer, struct tile *ptile)
34{
35 struct vision *vision = fc_malloc(sizeof(*vision));
36
37 vision->player = pplayer;
38 vision->tile = ptile;
39 vision->can_reveal_tiles = TRUE;
40 vision->radius_sq[V_MAIN] = -1;
41 vision->radius_sq[V_INVIS] = -1;
42 vision->radius_sq[V_SUBSURFACE] = -1;
43
44 return vision;
45}
46
47/************************************************************************/
50 void vision_free(struct vision *vision)
51{
52 fc_assert(-1 == vision->radius_sq[V_MAIN]);
53 fc_assert(-1 == vision->radius_sq[V_INVIS]);
54 fc_assert(-1 == vision->radius_sq[V_SUBSURFACE]);
55 free(vision);
56}
57
58/************************************************************************/
62 bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles)
63{
64 bool was = vision->can_reveal_tiles;
65
66 vision->can_reveal_tiles = reveal_tiles;
67
68 return was;
69}
70
71/************************************************************************/
74 void vision_site_destroy(struct vision_site *psite)
75{
76 if (psite->name != NULL) {
77 free(psite->name);
78 }
79
80 free(psite);
81}
82
83/************************************************************************/
86 struct vision_site *vision_site_new(int identity, struct tile *location,
87 struct player *owner)
88{
89 struct vision_site *psite = fc_calloc(1, sizeof(*psite));
90
91 psite->identity = identity;
92 psite->location = location;
93 psite->owner = owner;
94
95 return psite;
96}
97
98/************************************************************************/
101 struct vision_site *vision_site_new_from_city(const struct city *pcity)
102{
103 struct vision_site *psite =
104 vision_site_new(pcity->id, city_tile(pcity), city_owner(pcity));
105
106 vision_site_size_set(psite, city_size_get(pcity));
107 psite->name = fc_strdup(city_name_get(pcity));
108
109 return psite;
110}
111
112/************************************************************************/
115 void vision_site_update_from_city(struct vision_site *psite,
116 const struct city *pcity)
117{
118 /* Should be same identity and location */
119 fc_assert_ret(psite->identity == pcity->id);
120 fc_assert_ret(psite->location == pcity->tile);
121
122 psite->owner = city_owner(pcity);
123
124 vision_site_size_set(psite, city_size_get(pcity));
125
126 if (psite->name != NULL) {
127 free(psite->name);
128 }
129 psite->name = fc_strdup(city_name_get(pcity));
130}
131
132/************************************************************************/
135 struct vision_site *vision_site_copy(const struct vision_site *psite)
136{
137 /* Unlike in _new(), we don't need to use zeroing calloc() here
138 * as we overwrite everything. */
139 struct vision_site *copy = fc_malloc(sizeof(*psite));
140
141 /* Copy everything. */
142 *copy = *psite;
143
144 /* Then allocate things that aren't shared */
145 if (psite->name != NULL) {
146 copy->name = fc_strdup(psite->name);
147 }
148
149 return copy;
150}
151
152/************************************************************************/
155 citizens vision_site_size_get(const struct vision_site *psite)
156{
157 fc_assert_ret_val(psite != NULL, 0);
158
159 return psite->size;
160}
161
162/************************************************************************/
165 void vision_site_size_set(struct vision_site *psite, citizens size)
166{
167 fc_assert_ret(psite != NULL);
168
169 psite->size = size;
170}
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_size_get
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
city_owner
#define city_owner(_pcity_)
Definition city.h:563
incite_cost
char * incite_cost
Definition comments.c:74
citizens
unsigned char citizens
Definition fc_types.h:392
game.h
owner
struct city * owner
Definition citydlg.c:220
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
player.h
shared.h
size
size_t size
Definition specvec.h:72
city
Definition city.h:320
city::id
int id
Definition city.h:326
city::tile
struct tile * tile
Definition city.h:322
player
Definition player.h:249
tile
Definition tile.h:50
tile::owner
struct player * owner
Definition tile.h:60
vision_site
Definition vision.h:109
vision_site::name
char * name
Definition vision.h:110
vision_site::location
struct tile * location
Definition vision.h:111
vision_site::identity
int identity
Definition vision.h:114
vision
Definition vision.h:85
vision::tile
struct tile * tile
Definition vision.h:88
vision::can_reveal_tiles
bool can_reveal_tiles
Definition vision.h:89
vision::radius_sq
v_radius_t radius_sq
Definition vision.h:92
vision::player
struct player * player
Definition vision.h:87
TRUE
#define TRUE
Definition support.h:46
tile.h
vision_site_update_from_city
void vision_site_update_from_city(struct vision_site *psite, const struct city *pcity)
Definition vision.c:115
vision_site_size_set
void vision_site_size_set(struct vision_site *psite, citizens size)
Definition vision.c:165
vision_site_size_get
citizens vision_site_size_get(const struct vision_site *psite)
Definition vision.c:155
vision_new
struct vision * vision_new(struct player *pplayer, struct tile *ptile)
Definition vision.c:33
vision_reveal_tiles
bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles)
Definition vision.c:62
vision_free
void vision_free(struct vision *vision)
Definition vision.c:50
vision_site_new_from_city
struct vision_site * vision_site_new_from_city(const struct city *pcity)
Definition vision.c:101
vision_site_copy
struct vision_site * vision_site_copy(const struct vision_site *psite)
Definition vision.c:135
vision_site_new
struct vision_site * vision_site_new(int identity, struct tile *location, struct player *owner)
Definition vision.c:86
vision_site_destroy
void vision_site_destroy(struct vision_site *psite)
Definition vision.c:74
vision.h