File: clientutils.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (176 lines) | stat: -rw-r--r-- 5,775 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/********************************************************************** 
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "astring.h"

/* common */
#include "fc_types.h"
#include "tile.h"

#include "clientutils.h"

/* This module contains functions that would belong to the client,
 * except that in case of freeciv-web, server does handle these
 * for the web client. */


/****************************************************************************
  Creates the activity progress text for the given tile.
****************************************************************************/
const char *concat_tile_activity_text(struct tile *ptile)
{
  int activity_total[ACTIVITY_LAST];
  int activity_units[ACTIVITY_LAST];
  int extra_total[MAX_EXTRA_TYPES];
  int extra_units[MAX_EXTRA_TYPES];
  int rmextra_total[MAX_EXTRA_TYPES];
  int rmextra_units[MAX_EXTRA_TYPES];
  int num_activities = 0;
  int remains, turns;
  static struct astring str = ASTRING_INIT;

  astr_clear(&str);

  memset(activity_total, 0, sizeof(activity_total));
  memset(activity_units, 0, sizeof(activity_units));
  memset(extra_total, 0, sizeof(extra_total));
  memset(extra_units, 0, sizeof(extra_units));
  memset(rmextra_total, 0, sizeof(rmextra_total));
  memset(rmextra_units, 0, sizeof(rmextra_units));

  unit_list_iterate(ptile->units, punit) {
    if (is_clean_activity(punit->activity)) {
      int eidx = extra_index(punit->activity_target);

      rmextra_total[eidx] += punit->activity_count;
      rmextra_total[eidx] += get_activity_rate_this_turn(punit);
      rmextra_units[eidx] += get_activity_rate(punit);
    } else if (is_build_activity(punit->activity, ptile)) {
      int eidx = extra_index(punit->activity_target);

      extra_total[eidx] += punit->activity_count;
      extra_total[eidx] += get_activity_rate_this_turn(punit);
      extra_units[eidx] += get_activity_rate(punit);
    } else {
      activity_total[punit->activity] += punit->activity_count;
      activity_total[punit->activity] += get_activity_rate_this_turn(punit);
      activity_units[punit->activity] += get_activity_rate(punit);
    }
  } unit_list_iterate_end;

  activity_type_iterate(i) {
    if (is_build_activity(i, ptile)) {
      enum extra_cause cause = EC_NONE;

      switch(i) {
      case ACTIVITY_GEN_ROAD:
        cause = EC_ROAD;
        break;
      case ACTIVITY_BASE:
        cause = EC_BASE;
        break;
      case ACTIVITY_IRRIGATE:
        cause = EC_IRRIGATION;
        break;
      case ACTIVITY_MINE:
        cause = EC_MINE;
        break;
      default:
        fc_assert(cause != EC_NONE);
        break;
      };

      if (cause != EC_NONE) {
        extra_type_by_cause_iterate(cause, ep) {
          int ei = extra_index(ep);

          if (extra_units[ei] > 0) {
            remains = tile_activity_time(i, ptile, ep) - extra_total[ei];
            if (remains > 0) {
              turns = 1 + (remains + extra_units[ei] - 1) / extra_units[ei];
            } else {
              /* extra will be finished this turn */
              turns = 1;
            }
            if (num_activities > 0) {
              astr_add(&str, "/");
            }
            astr_add(&str, "%s(%d)", extra_name_translation(ep), turns);
            num_activities++;
          }
        } extra_type_by_cause_iterate_end;
      }
    } else if (is_clean_activity(i)) {
      enum extra_rmcause rmcause = ERM_NONE;

      switch(i) {
      case ACTIVITY_PILLAGE:
        rmcause = ERM_PILLAGE;
        break;
      case ACTIVITY_POLLUTION:
        rmcause = ERM_CLEANPOLLUTION;
        break;
      case ACTIVITY_FALLOUT:
        rmcause = ERM_CLEANFALLOUT;
        break;
      default:
        fc_assert(rmcause != ERM_NONE);
        break;
      };

      if (rmcause != ERM_NONE) {
        extra_type_by_rmcause_iterate(rmcause, ep) {
          int ei = extra_index(ep);

          if (rmextra_units[ei] > 0) {
            remains = tile_activity_time(i, ptile, ep) - rmextra_total[ei];
            if (remains > 0) {
              turns = 1 + (remains + rmextra_units[ei] - 1) / rmextra_units[ei];
            } else {
              /* extra will be removed this turn */
              turns = 1;
            }
            if (num_activities > 0) {
              astr_add(&str, "/");
            }
            astr_add(&str, rmcause == ERM_PILLAGE ? _("Pillage %s(%d)") : _("Clean %s(%d)"),
                     extra_name_translation(ep), turns);
            num_activities++;
          }
        } extra_type_by_rmcause_iterate_end;
      }
    } else if (is_tile_activity(i)) {
      if (activity_units[i] > 0) {
        remains = tile_activity_time(i, ptile, NULL) - activity_total[i];
        if (remains > 0) {
          turns = 1 + (remains + activity_units[i] - 1) / activity_units[i];
        } else {
          /* activity will be finished this turn */
          turns = 1;
        }
        if (num_activities > 0) {
          astr_add(&str, "/");
        }
        astr_add(&str, "%s(%d)", get_activity_text(i), turns);
        num_activities++;
      }
    }
  } activity_type_iterate_end;

  return astr_str(&str);
}