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

Freeciv-3.3

Loading...
Searching...
No Matches
citizens.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 "rand.h"
21
22/* common */
23#include "city.h"
24#include "game.h"
25#include "player.h"
26
27#include "citizens.h"
28
29/*************************************************************************/
32 void citizens_init(struct city *pcity)
33{
34 fc_assert_ret(pcity);
35
36 if (!game.info.citizen_nationality) {
37 return;
38 }
39
40 /* For the nationality of the citizens the informations for all player
41 * slots are allocated as once. Considering a size of citizens (= char)
42 * this results in an allocation of 2 * 128 * 1 bytes for the citizens
43 * per nation as well as the timer for a nationality change. */
44 if (pcity->nationality == NULL) {
45 /* Allocate the memory*/
46 pcity->nationality = fc_calloc(MAX_NUM_PLAYER_SLOTS,
47 sizeof(*pcity->nationality));
48 } else {
49 /* Reset the nationality information. */
50 memset(pcity->nationality, 0,
51 MAX_NUM_PLAYER_SLOTS * sizeof(*pcity->nationality));
52 }
53}
54
55/*************************************************************************/
58 void citizens_free(struct city *pcity)
59{
60 fc_assert_ret(pcity);
61
62 if (pcity->nationality) {
63 free(pcity->nationality);
64 pcity->nationality = NULL;
65 }
66}
67
68/*************************************************************************/
74 citizens citizens_nation_get(const struct city *pcity,
75 const struct player_slot *pslot)
76{
77 if (!game.info.citizen_nationality) {
78 return 0;
79 }
80
81 fc_assert_ret_val(pslot != NULL, 0);
82 fc_assert_ret_val(pcity != NULL, 0);
83 fc_assert_ret_val(pcity->nationality != NULL, 0);
84
85 return *(pcity->nationality + player_slot_index(pslot));
86}
87
88/*************************************************************************/
91 citizens citizens_nation_foreign(const struct city *pcity)
92{
93 return citizens_count(pcity)
94 - citizens_nation_get(pcity, city_owner(pcity)->slot);
95}
96
97/*************************************************************************/
104 void citizens_nation_add(struct city *pcity, const struct player_slot *pslot,
105 int add)
106{
107 citizens nationality = citizens_nation_get(pcity, pslot);
108
109 if (!game.info.citizen_nationality) {
110 return;
111 }
112
113 fc_assert_ret(pslot != NULL);
114 fc_assert_ret(pcity != NULL);
115 fc_assert_ret(pcity->nationality != NULL);
116
117 fc_assert_ret(MAX_CITY_SIZE - nationality >= add);
118 fc_assert_ret(nationality >= -add);
119
120 citizens_nation_set(pcity, pslot, nationality + add);
121}
122
123/*************************************************************************/
130 void citizens_nation_move(struct city *pcity,
131 const struct player_slot *pslot_from,
132 const struct player_slot *pslot_to,
133 int move)
134{
135 citizens_nation_add(pcity, pslot_from, -move);
136 citizens_nation_add(pcity, pslot_to, move);
137}
138
139/*************************************************************************/
145 void citizens_nation_set(struct city *pcity, const struct player_slot *pslot,
146 citizens count)
147{
148 if (!game.info.citizen_nationality) {
149 return;
150 }
151
152 fc_assert_ret(pslot != NULL);
153 fc_assert_ret(pcity != NULL);
154 fc_assert_ret(pcity->nationality != NULL);
155
156 *(pcity->nationality + player_slot_index(pslot)) = count;
157}
158
159/*************************************************************************/
162 citizens citizens_count(const struct city *pcity)
163{
164 /* Use int here to check for an possible overflow at the end. */
165 int count = 0;
166
167 if (!game.info.citizen_nationality) {
168 return city_size_get(pcity);
169 }
170
171 citizens_iterate(pcity, pslot, nationality) {
172 /* If the citizens of a nation is greater than 0 there should be a player
173 * for this nation. This test should only be done on the server as the
174 * client does not has the knowledge about all players all the time. */
175 fc_assert_ret_val(!is_server() || player_slot_get_player(pslot) != NULL,
176 city_size_get(pcity));
177
178 count += nationality;
179 } citizens_iterate_end;
180
181 fc_assert_ret_val(count >= 0 && count <= MAX_CITY_SIZE,
182 city_size_get(pcity));
183
184 return (citizens)count;
185}
186
187/*************************************************************************/
190 struct player_slot *citizens_random(const struct city *pcity)
191{
192 int total = citizens_count(pcity);
193 int chocen = fc_rand(total);
194
195 citizens_iterate(pcity, pslot, nationality) {
196 chocen -= nationality;
197 if (chocen <= 0) {
198 return pslot;
199 }
200 } citizens_iterate_end;
201
202 fc_assert(FALSE);
203
204 return NULL;
205}
citizens_random
struct player_slot * citizens_random(const struct city *pcity)
Definition citizens.c:190
citizens_free
void citizens_free(struct city *pcity)
Definition citizens.c:58
citizens_nation_move
void citizens_nation_move(struct city *pcity, const struct player_slot *pslot_from, const struct player_slot *pslot_to, int move)
Definition citizens.c:130
citizens_nation_add
void citizens_nation_add(struct city *pcity, const struct player_slot *pslot, int add)
Definition citizens.c:104
citizens_nation_get
citizens citizens_nation_get(const struct city *pcity, const struct player_slot *pslot)
Definition citizens.c:74
citizens_nation_set
void citizens_nation_set(struct city *pcity, const struct player_slot *pslot, citizens count)
Definition citizens.c:145
citizens_nation_foreign
citizens citizens_nation_foreign(const struct city *pcity)
Definition citizens.c:91
citizens_count
citizens citizens_count(const struct city *pcity)
Definition citizens.c:162
citizens_init
void citizens_init(struct city *pcity)
Definition citizens.c:32
citizens.h
citizens_iterate_end
#define citizens_iterate_end
Definition citizens.h:54
citizens_iterate
#define citizens_iterate(_pcity, _pslot, _nationality)
Definition citizens.h:48
city.h
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
MAX_CITY_SIZE
#define MAX_CITY_SIZE
Definition city.h:106
incite_cost
char * incite_cost
Definition comments.c:74
is_server
static bool is_server(void)
Definition fc_interface.h:69
citizens
unsigned char citizens
Definition fc_types.h:392
MAX_NUM_PLAYER_SLOTS
#define MAX_NUM_PLAYER_SLOTS
Definition fc_types.h:32
game
struct civ_game game
Definition game.c:62
game.h
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
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
player_slot_index
int player_slot_index(const struct player_slot *pslot)
Definition player.c:426
player_slot_get_player
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
player.h
rand.h
fc_rand
#define fc_rand(_size)
Definition rand.h:56
city
Definition city.h:320
city::nationality
citizens * nationality
Definition city.h:341
civ_game::info
struct packet_game_info info
Definition game.h:89
move
Definition widget_window.c:39
packet_game_info::citizen_nationality
bool citizen_nationality
Definition packets_gen.h:146
player_slot
Definition player.c:46
FALSE
#define FALSE
Definition support.h:47