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

Freeciv-3.1

Loading...
Searching...
No Matches
repodlgs_common.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 "fcintl.h"
20#include "log.h"
21#include "mem.h" /* free() */
22#include "support.h" /* fc_snprintf() */
23
24/* common */
25#include "game.h"
26#include "government.h"
27#include "unitlist.h"
28
29/* client/include */
30#include "repodlgs_g.h"
31
32/* client */
33#include "client_main.h"
34#include "connectdlg_common.h" /* is_server_running() */
35#include "control.h"
36#include "options.h"
37#include <packhand_gen.h> /* <> so looked from the build directory first. */
38
39#include "repodlgs_common.h"
40
41
42/************************************************************************/
46 void get_economy_report_data(struct improvement_entry *entries,
47 int *num_entries_used, int *total_cost,
48 int *total_income)
49{
50 *num_entries_used = 0;
51 *total_cost = 0;
52 *total_income = 0;
53
54 if (NULL == client.conn.playing) {
55 return;
56 }
57
58 improvement_iterate(pimprove) {
59 if (is_improvement(pimprove)) {
60 int count = 0, redundant = 0, cost = 0;
61 city_list_iterate(client.conn.playing->cities, pcity) {
62 if (city_has_building(pcity, pimprove)) {
63 count++;
64 cost += city_improvement_upkeep(pcity, pimprove);
65 if (is_improvement_redundant(pcity, pimprove)) {
66 redundant++;
67 }
68 }
69 }
70 city_list_iterate_end;
71
72 if (count == 0) {
73 continue;
74 }
75
76 entries[*num_entries_used].type = pimprove;
77 entries[*num_entries_used].count = count;
78 entries[*num_entries_used].redundant = redundant;
79 entries[*num_entries_used].total_cost = cost;
80 entries[*num_entries_used].cost = cost / count;
81 (*num_entries_used)++;
82
83 /* Currently there is no building expense under anarchy. It's
84 * not a good idea to hard-code this in the client, but what
85 * else can we do? */
86 if (government_of_player(client.conn.playing) !=
87 game.government_during_revolution) {
88 *total_cost += cost;
89 }
90 }
91 } improvement_iterate_end;
92
93 city_list_iterate(client.conn.playing->cities, pcity) {
94 *total_income += pcity->prod[O_GOLD];
95 if (city_production_has_flag(pcity, IF_GOLD)) {
96 *total_income += MAX(0, pcity->surplus[O_SHIELD]);
97 }
98 } city_list_iterate_end;
99}
100
101/************************************************************************/
105 void get_economy_report_units_data(struct unit_entry *entries,
106 int *num_entries_used, int *total_cost)
107{
108 int count, cost, partial_cost;
109
110 *num_entries_used = 0;
111 *total_cost = 0;
112
113 if (NULL == client.conn.playing) {
114 return;
115 }
116
117 unit_type_iterate(unittype) {
118 cost = utype_upkeep_cost(unittype, client.conn.playing, O_GOLD);
119
120 if (cost == 0) {
121 /* Short-circuit all of the following checks. */
122 continue;
123 }
124
125 count = 0;
126 partial_cost = 0;
127
128 city_list_iterate(client.conn.playing->cities, pcity) {
129 unit_list_iterate(pcity->units_supported, punit) {
130 if (unit_type_get(punit) == unittype) {
131 count++;
132 partial_cost += punit->upkeep[O_GOLD];
133 }
134
135 } unit_list_iterate_end;
136 } city_list_iterate_end;
137
138 if (count == 0) {
139 continue;
140 }
141
142 (*total_cost) += partial_cost;
143
144 entries[*num_entries_used].type = unittype;
145 entries[*num_entries_used].count = count;
146 entries[*num_entries_used].cost = cost;
147 entries[*num_entries_used].total_cost = partial_cost;
148 (*num_entries_used)++;
149
150 } unit_type_iterate_end;
151}
152
153/************************************************************************/
160 void sell_all_improvements(const struct impr_type *pimprove, bool redundant_only,
161 char *message, size_t message_sz)
162{
163 int count = 0, gold = 0;
164
165 if (!can_client_issue_orders()) {
166 fc_snprintf(message, message_sz, _("You cannot sell improvements."));
167 return;
168 }
169
170 city_list_iterate(client.conn.playing->cities, pcity) {
171 if (!pcity->did_sell && city_has_building(pcity, pimprove)
172 && (!redundant_only
173 || is_improvement_redundant(pcity, pimprove))) {
174 count++;
175 gold += impr_sell_gold(pimprove);
176 city_sell_improvement(pcity, improvement_number(pimprove));
177 }
178 } city_list_iterate_end;
179
180 if (count > 0) {
181 /* FIXME: plurality of count is ignored! */
182 /* TRANS: "Sold 3 Harbor for 90 gold." (Pluralisation is in gold --
183 * second %d -- not in buildings.) */
184 fc_snprintf(message, message_sz, PL_("Sold %d %s for %d gold.",
185 "Sold %d %s for %d gold.", gold),
186 count, improvement_name_translation(pimprove), gold);
187 } else {
188 fc_snprintf(message, message_sz, _("No %s could be sold."),
189 improvement_name_translation(pimprove));
190 }
191}
192
193/************************************************************************/
200 void disband_all_units(const struct unit_type *punittype, bool in_cities_only,
201 char *message, size_t message_sz)
202{
203 int count = 0;
204
205 if (!can_client_issue_orders()) {
206 /* TRANS: Obscure observer error. */
207 fc_snprintf(message, message_sz, _("You cannot disband units."));
208 return;
209 }
210
211 if (!utype_can_do_action(punittype, ACTION_DISBAND_UNIT)) {
212 fc_snprintf(message, message_sz, _("%s cannot be disbanded."),
213 utype_name_translation(punittype));
214 return;
215 }
216
217 city_list_iterate(client.conn.playing->cities, pcity) {
218 /* Only supported units are disbanded. Units with no homecity have no
219 * cost and are not disbanded. */
220 unit_list_iterate(pcity->units_supported, punit) {
221 struct city *incity = tile_city(unit_tile(punit));
222
223 if (unit_type_get(punit) == punittype
224 && (!in_cities_only
225 || (incity && city_owner(incity) == client.conn.playing))) {
226 count++;
227 request_unit_disband(punit);
228 }
229 } unit_list_iterate_end;
230 } city_list_iterate_end;
231
232 if (count > 0) {
233 fc_snprintf(message, message_sz, _("Disbanded %d %s."),
234 count, utype_name_translation(punittype));
235 } else {
236 fc_snprintf(message, message_sz, _("No %s could be disbanded."),
237 utype_name_translation(punittype));
238 }
239}
entries
struct @124::my_agent entries[MAX_AGENTS]
city_has_building
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1212
city_improvement_upkeep
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1227
city_production_has_flag
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:712
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:488
city_owner
#define city_owner(_pcity_)
Definition city.h:543
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:490
city_sell_improvement
int city_sell_improvement(struct city *pcity, Impr_type_id sell_id)
Definition citydlg_common.c:1517
client
struct civclient client
Definition client_main.c:149
can_client_issue_orders
bool can_client_issue_orders(void)
Definition client_main.c:1265
client_main.h
connectdlg_common.h
request_unit_disband
void request_unit_disband(struct unit *punit)
Definition control.c:1975
control.h
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
cost
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 int cost
Definition dialogs_g.h:73
O_SHIELD
@ O_SHIELD
Definition fc_types.h:91
O_GOLD
@ O_GOLD
Definition fc_types.h:91
fcintl.h
PL_
#define PL_(String1, String2, n)
Definition fcintl.h:71
_
#define _(String)
Definition fcintl.h:67
game
struct civ_game game
Definition game.c:57
game.h
government_of_player
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
government.h
impr_sell_gold
int impr_sell_gold(const struct impr_type *pimprove)
Definition improvement.c:332
is_improvement_redundant
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
Definition improvement.c:636
is_improvement
bool is_improvement(const struct impr_type *pimprove)
Definition improvement.c:783
improvement_number
Impr_type_id improvement_number(const struct impr_type *pimprove)
Definition improvement.c:182
improvement_name_translation
const char * improvement_name_translation(const struct impr_type *pimprove)
Definition improvement.c:241
improvement_iterate_end
#define improvement_iterate_end
Definition improvement.h:211
improvement_iterate
#define improvement_iterate(_p)
Definition improvement.h:205
log.h
mem.h
options.h
packhand_gen.h
get_economy_report_units_data
void get_economy_report_units_data(struct unit_entry *entries, int *num_entries_used, int *total_cost)
Definition repodlgs_common.c:105
get_economy_report_data
void get_economy_report_data(struct improvement_entry *entries, int *num_entries_used, int *total_cost, int *total_income)
Definition repodlgs_common.c:46
sell_all_improvements
void sell_all_improvements(const struct impr_type *pimprove, bool redundant_only, char *message, size_t message_sz)
Definition repodlgs_common.c:160
disband_all_units
void disband_all_units(const struct unit_type *punittype, bool in_cities_only, char *message, size_t message_sz)
Definition repodlgs_common.c:200
repodlgs_common.h
repodlgs_g.h
MAX
#define MAX(x, y)
Definition shared.h:54
city
Definition city.h:309
civ_game::government_during_revolution
struct government * government_during_revolution
Definition game.h:94
civclient::conn
struct connection conn
Definition client_main.h:96
connection::playing
struct player * playing
Definition connection.h:156
impr_type
Definition improvement.h:68
improvement_entry
Definition repodlgs_common.h:28
message
Definition messagewin_common.h:29
player::cities
struct city_list * cities
Definition player.h:281
unit_entry
Definition repodlgs_common.h:34
unit_type
Definition unittype.h:481
unit::upkeep
int upkeep[O_LAST]
Definition unit.h:148
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
support.h
tile_city
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
unit_tile
#define unit_tile(_pu)
Definition unit.h:388
unitlist.h
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
utype_upkeep_cost
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Definition unittype.c:132
utype_name_translation
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1612
utype_can_do_action
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
unit_type_iterate
#define unit_type_iterate(_p)
Definition unittype.h:838
unit_type_iterate_end
#define unit_type_iterate_end
Definition unittype.h:845