Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / clientutils.c
blobfd98c356c54a43991f95928624101c11ea9ec63a
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "astring.h"
21 /* common */
22 #include "fc_types.h"
23 #include "tile.h"
25 #include "clientutils.h"
27 /* This module contains functions that would belong to the client,
28 * except that in case of freeciv-web, server does handle these
29 * for the web client. */
32 /****************************************************************************
33 Creates the activity progress text for the given tile.
34 ****************************************************************************/
35 const char *concat_tile_activity_text(struct tile *ptile)
37 int activity_total[ACTIVITY_LAST];
38 int activity_units[ACTIVITY_LAST];
39 int extra_total[MAX_EXTRA_TYPES];
40 int extra_units[MAX_EXTRA_TYPES];
41 int rmextra_total[MAX_EXTRA_TYPES];
42 int rmextra_units[MAX_EXTRA_TYPES];
43 int num_activities = 0;
44 int remains, turns;
45 static struct astring str = ASTRING_INIT;
47 astr_clear(&str);
49 memset(activity_total, 0, sizeof(activity_total));
50 memset(activity_units, 0, sizeof(activity_units));
51 memset(extra_total, 0, sizeof(extra_total));
52 memset(extra_units, 0, sizeof(extra_units));
53 memset(rmextra_total, 0, sizeof(rmextra_total));
54 memset(rmextra_units, 0, sizeof(rmextra_units));
56 unit_list_iterate(ptile->units, punit) {
57 if (is_clean_activity(punit->activity)) {
58 int eidx = extra_index(punit->activity_target);
60 rmextra_total[eidx] += punit->activity_count;
61 rmextra_total[eidx] += get_activity_rate_this_turn(punit);
62 rmextra_units[eidx] += get_activity_rate(punit);
63 } else if (is_build_activity(punit->activity, ptile)) {
64 int eidx = extra_index(punit->activity_target);
66 extra_total[eidx] += punit->activity_count;
67 extra_total[eidx] += get_activity_rate_this_turn(punit);
68 extra_units[eidx] += get_activity_rate(punit);
69 } else {
70 activity_total[punit->activity] += punit->activity_count;
71 activity_total[punit->activity] += get_activity_rate_this_turn(punit);
72 activity_units[punit->activity] += get_activity_rate(punit);
74 } unit_list_iterate_end;
76 activity_type_iterate(i) {
77 if (is_build_activity(i, ptile)) {
78 enum extra_cause cause = EC_NONE;
80 switch(i) {
81 case ACTIVITY_GEN_ROAD:
82 cause = EC_ROAD;
83 break;
84 case ACTIVITY_BASE:
85 cause = EC_BASE;
86 break;
87 case ACTIVITY_IRRIGATE:
88 cause = EC_IRRIGATION;
89 break;
90 case ACTIVITY_MINE:
91 cause = EC_MINE;
92 break;
93 default:
94 fc_assert(cause != EC_NONE);
95 break;
98 if (cause != EC_NONE) {
99 extra_type_by_cause_iterate(cause, ep) {
100 int ei = extra_index(ep);
102 if (extra_units[ei] > 0) {
103 remains = tile_activity_time(i, ptile, ep) - extra_total[ei];
104 if (remains > 0) {
105 turns = 1 + (remains + extra_units[ei] - 1) / extra_units[ei];
106 } else {
107 /* extra will be finished this turn */
108 turns = 1;
110 if (num_activities > 0) {
111 astr_add(&str, "/");
113 astr_add(&str, "%s(%d)", extra_name_translation(ep), turns);
114 num_activities++;
116 } extra_type_by_cause_iterate_end;
118 } else if (is_clean_activity(i)) {
119 enum extra_rmcause rmcause = ERM_NONE;
121 switch(i) {
122 case ACTIVITY_PILLAGE:
123 rmcause = ERM_PILLAGE;
124 break;
125 case ACTIVITY_POLLUTION:
126 rmcause = ERM_CLEANPOLLUTION;
127 break;
128 case ACTIVITY_FALLOUT:
129 rmcause = ERM_CLEANFALLOUT;
130 break;
131 default:
132 fc_assert(rmcause != ERM_NONE);
133 break;
136 if (rmcause != ERM_NONE) {
137 extra_type_by_rmcause_iterate(rmcause, ep) {
138 int ei = extra_index(ep);
140 if (rmextra_units[ei] > 0) {
141 remains = tile_activity_time(i, ptile, ep) - rmextra_total[ei];
142 if (remains > 0) {
143 turns = 1 + (remains + rmextra_units[ei] - 1) / rmextra_units[ei];
144 } else {
145 /* extra will be removed this turn */
146 turns = 1;
148 if (num_activities > 0) {
149 astr_add(&str, "/");
151 astr_add(&str, rmcause == ERM_PILLAGE ? _("Pillage %s(%d)") : _("Clean %s(%d)"),
152 extra_name_translation(ep), turns);
153 num_activities++;
155 } extra_type_by_rmcause_iterate_end;
157 } else if (is_tile_activity(i)) {
158 if (activity_units[i] > 0) {
159 remains = tile_activity_time(i, ptile, NULL) - activity_total[i];
160 if (remains > 0) {
161 turns = 1 + (remains + activity_units[i] - 1) / activity_units[i];
162 } else {
163 /* activity will be finished this turn */
164 turns = 1;
166 if (num_activities > 0) {
167 astr_add(&str, "/");
169 astr_add(&str, "%s(%d)", get_activity_text(i), turns);
170 num_activities++;
173 } activity_type_iterate_end;
175 return astr_str(&str);