Freeciv-3.2
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/*************************************************************************/
32void citizens_init(struct city *pcity)
33{
34 fc_assert_ret(pcity);
35
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*/
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/*************************************************************************/
58void 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/*************************************************************************/
74citizens citizens_nation_get(const struct city *pcity,
75 const struct player_slot *pslot)
76{
78 return 0;
79 }
80
81 fc_assert_ret_val(pslot != NULL, 0);
82 fc_assert_ret_val(pcity != NULL, 0);
84
85 return *(pcity->nationality + player_slot_index(pslot));
86}
87
88/*************************************************************************/
92{
93 return citizens_count(pcity)
94 - citizens_nation_get(pcity, city_owner(pcity)->slot);
95}
96
97/*************************************************************************/
104void citizens_nation_add(struct city *pcity, const struct player_slot *pslot,
105 int add)
106{
107 citizens nationality = citizens_nation_get(pcity, pslot);
108
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/*************************************************************************/
130void citizens_nation_move(struct city *pcity,
131 const struct player_slot *pslot_from,
132 const struct player_slot *pslot_to,
133 int move)
134{
137}
138
139/*************************************************************************/
145void citizens_nation_set(struct city *pcity, const struct player_slot *pslot,
146 citizens count)
147{
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/*************************************************************************/
162citizens 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
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. */
176 city_size_get(pcity));
177
178 count += nationality;
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/*************************************************************************/
190struct 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 }
201
203
204 return NULL;
205}
struct player_slot * citizens_random(const struct city *pcity)
Definition citizens.c:190
void citizens_free(struct city *pcity)
Definition citizens.c:58
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
void citizens_nation_add(struct city *pcity, const struct player_slot *pslot, int add)
Definition citizens.c:104
citizens citizens_nation_get(const struct city *pcity, const struct player_slot *pslot)
Definition citizens.c:74
void citizens_nation_set(struct city *pcity, const struct player_slot *pslot, citizens count)
Definition citizens.c:145
citizens citizens_nation_foreign(const struct city *pcity)
Definition citizens.c:91
citizens citizens_count(const struct city *pcity)
Definition citizens.c:162
void citizens_init(struct city *pcity)
Definition citizens.c:32
#define citizens_iterate_end
Definition citizens.h:54
#define citizens_iterate(_pcity, _pslot, _nationality)
Definition citizens.h:48
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define city_owner(_pcity_)
Definition city.h:563
#define MAX_CITY_SIZE
Definition city.h:106
char * incite_cost
Definition comments.c:75
static bool is_server(void)
unsigned char citizens
Definition fc_types.h:388
#define MAX_NUM_PLAYER_SLOTS
Definition fc_types.h:32
struct civ_game game
Definition game.c:62
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_calloc(n, esz)
Definition mem.h:38
int player_slot_index(const struct player_slot *pslot)
Definition player.c:426
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
#define fc_rand(_size)
Definition rand.h:56
Definition city.h:320
citizens * nationality
Definition city.h:341
struct packet_game_info info
Definition game.h:89
#define FALSE
Definition support.h:47