Freeciv-3.3
Loading...
Searching...
No Matches
cma_fec.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2001 - R. Falke
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/***********************************************************************
15 This is the common file for all front-end (Front End Common) for the
16 citizen management agent (CMA).
17***********************************************************************/
18
19#ifdef HAVE_CONFIG_H
20#include <fc_config.h>
21#endif
22
23#include <string.h>
24
25/* utility */
26#include "fciconv.h"
27#include "fcintl.h"
28#include "log.h"
29#include "mem.h"
30#include "support.h"
31
32/* common */
33#include "game.h"
34#include "specialist.h"
35
36/* client */
37#include "attribute.h"
38
39/* client/agents */
40#include "agents.h"
41
42#include "cma_fec.h"
43
44
45#define RESULT_COLUMNS 10
46#define BUFFER_SIZE 100
47#define MAX_LEN_PRESET_NAME 80
48
49struct cma_preset {
50 char *descr;
52};
53
54#define SPECLIST_TAG preset
55#define SPECLIST_TYPE struct cma_preset
56#include "speclist.h"
57
58#define preset_list_iterate(presetlist, ppreset) \
59 TYPED_LIST_ITERATE(struct cma_preset, presetlist, ppreset)
60#define preset_list_iterate_end LIST_ITERATE_END
61
62static struct preset_list *preset_list = NULL;
63
64/**********************************************************************/
68static void city_remove(int city_id)
69{
71}
72
73/**********************************************************************/
76void cmafec_init(void)
77{
78 struct agent self;
79
80 if (preset_list == NULL) {
82 }
83
84 memset(&self, 0, sizeof(self));
85 strcpy(self.name, "CMA");
86 self.level = 1;
87 self.city_callbacks[CB_REMOVE] = city_remove;
89}
90
91/**********************************************************************/
94void cmafec_free(void)
95{
96 while (cmafec_preset_num() > 0) {
98 }
100}
101
102/**********************************************************************/
106 const struct cm_parameter *const parameter)
107{
109}
110
111/**********************************************************************/
116{
117 struct cm_parameter parameter;
118
119 /* our fe_parameter could be stale. our agents parameter is up to date */
120 if (cma_is_city_under_agent(pcity, &parameter)) {
121 cm_copy_parameter(dest, &parameter);
123 } else {
124 /* Create a dummy parameter to return. */
125 cm_init_parameter(dest);
127 /* We haven't seen this city before; store the dummy. */
129 }
130 }
131}
132
133/**********************************************************************/
137{
138 struct cma_preset *ppreset = fc_malloc(sizeof(struct cma_preset));
139
140 if (preset_list == NULL) {
142 }
143
144 cm_copy_parameter(&ppreset->parameter, pparam);
148}
149
150/**********************************************************************/
154{
155 struct cma_preset *ppreset;
156
157 fc_assert_ret(idx >= 0 && idx < cmafec_preset_num());
158
161
162 free(ppreset->descr);
163 free(ppreset);
164}
165
166/**********************************************************************/
170{
171 struct cma_preset *ppreset;
172
173 fc_assert_ret_val(idx >= 0 && idx < cmafec_preset_num(), NULL);
174
176 return ppreset->descr;
177}
178
179/**********************************************************************/
183{
184 struct cma_preset *ppreset;
185
186 fc_assert_ret_val(idx >= 0 && idx < cmafec_preset_num(), NULL);
187
189 return &ppreset->parameter;
190}
191
192/**********************************************************************/
197 *const parameter)
198{
199 int i;
200
201 for (i = 0; i < preset_list_size(preset_list); i++) {
203 if (cm_are_parameter_equal(&ppreset->parameter, parameter)) {
204 return i;
205 }
206 }
207 return -1;
208}
209
210/**********************************************************************/
214{
216}
217
218/**********************************************************************/
221const char *cmafec_get_short_descr_of_city(const struct city *pcity)
222{
223 struct cm_parameter parameter;
224
225 if (!cma_is_city_under_agent(pcity, &parameter)) {
226 return Q_("?cma:none");
227 } else {
228 return cmafec_get_short_descr(&parameter);
229 }
230}
231
232/**********************************************************************/
236const char *cmafec_get_short_descr(const struct cm_parameter *const
237 parameter)
238{
239 int idx = cmafec_preset_get_index_of_parameter(parameter);
240
241 if (idx == -1) {
242 return Q_("?cma:custom");
243 } else {
244 return cmafec_preset_get_descr(idx);
245 }
246}
247
248/**********************************************************************/
251static const char *get_city_growth_string(struct city *pcity, int surplus)
252{
253 int stock, cost, turns;
254 static char buffer[50];
255
256 if (surplus == 0) {
257 fc_snprintf(buffer, sizeof(buffer), _("never"));
258 return buffer;
259 }
260
261 stock = pcity->food_stock;
263
264 stock += surplus;
265
266 if (stock >= cost) {
267 turns = 1;
268 } else if (surplus > 0) {
269 turns = ((cost - stock - 1) / surplus) + 1 + 1;
270 } else {
271 if (stock < 0) {
272 turns = -1;
273 } else {
274 turns = (stock / surplus);
275 }
276 }
277 fc_snprintf(buffer, sizeof(buffer), PL_("%d turn", "%d turns", turns),
278 turns);
279 return buffer;
280}
281
282/**********************************************************************/
285static const char *get_prod_complete_string(struct city *pcity, int surplus)
286{
287 int stock, cost, turns;
288 static char buffer[50];
289
290 if (surplus <= 0) {
291 fc_snprintf(buffer, sizeof(buffer), _("never"));
292 return buffer;
293 }
294
297 (pcity->production.value.building), sizeof(buffer));
298 return buffer;
299 }
300 stock = pcity->shield_stock + surplus;
302
303 if (stock >= cost) {
304 turns = 1;
305 } else if (surplus > 0) {
306 turns = ((cost - stock - 1) / surplus) + 1 + 1;
307 } else {
308 if (stock < 0) {
309 turns = -1;
310 } else {
311 turns = (stock / surplus);
312 }
313 }
314 fc_snprintf(buffer, sizeof(buffer), PL_("%d turn", "%d turns", turns),
315 turns);
316 return buffer;
317}
318
319/**********************************************************************/
323 const struct cm_result *result,
324 const struct cm_parameter *const
325 parameter)
326{
327 int j;
330 static char buffer[600];
331 int slen;
332
333 /* TRANS: "W" is worker citizens, as opposed to specialists;
334 * %s will represent the specialist types, for instance "E/S/T" */
335 fc_snprintf(citizen_types, BUFFER_SIZE, _("People (W/%s)"),
337
338 if (!result->found_a_valid) {
339 for (j = 0; j < RESULT_COLUMNS; j++)
340 fc_snprintf(buf[j], BUFFER_SIZE, "---");
341 } else {
343 fc_snprintf(buf[o], BUFFER_SIZE, "%+3d", result->surplus[o]);
345
346 fc_snprintf(buf[6], BUFFER_SIZE, "%d/%s%s",
349 /* TRANS: preserve leading space */
350 result->happy ? _(" happy") : "");
351
352 fc_snprintf(buf[7], BUFFER_SIZE, "%s",
354 fc_snprintf(buf[8], BUFFER_SIZE, "%s",
356 fc_snprintf(buf[9], BUFFER_SIZE, "%s",
357 cmafec_get_short_descr(parameter));
358 }
359
361 fc_snprintf(buffer, sizeof(buffer),
362 _("Name: %s\n"
363 "Food: %10s Gold: %10s\n"
364 "Production: %10s Luxury: %10s\n"
365 "Trade: %10s Science: %10s\n"
366 "\n"
367 "%*s%s: %s\n"
368 " City grows: %s\n"
369 "Production completed: %s"),
372 MAX(0, slen), "",
374 buf[6], buf[7], buf[8]);
375
376 log_debug("\n%s", buffer);
377
378 return buffer;
379}
380
381/**********************************************************************/
385{
386 int i;
387 struct cm_parameter parameters[] = {
388 { /* very happy */
389 .minimal_surplus = {0, 0, 0, -20, 0, 0},
390 .require_happy = FALSE,
391 .allow_disorder = FALSE,
392 .allow_specialists = TRUE,
393 .factor = {10, 5, 0, 4, 0, 4},
394 .happy_factor = 25
395 },
396 { /* prefer food */
397 .minimal_surplus = {-20, 0, 0, -20, 0, 0},
398 .require_happy = FALSE,
399 .allow_disorder = FALSE,
400 .allow_specialists = TRUE,
401 .factor = {25, 5, 0, 4, 0, 4},
402 .happy_factor = 0
403 },
404 { /* prefer prod */
405 .minimal_surplus = {0, -20, 0, -20, 0, 0},
406 .require_happy = FALSE,
407 .allow_disorder = FALSE,
408 .allow_specialists = TRUE,
409 .factor = {10, 25, 0, 4, 0, 4},
410 .happy_factor = 0
411 },
412 { /* prefer gold */
413 .minimal_surplus = {0, 0, 0, -20, 0, 0},
414 .require_happy = FALSE,
415 .allow_disorder = FALSE,
416 .allow_specialists = TRUE,
417 .factor = {10, 5, 0, 25, 0, 4},
418 .happy_factor = 0
419 },
420 { /* prefer science */
421 .minimal_surplus = {0, 0, 0, -20, 0, 0},
422 .require_happy = FALSE,
423 .allow_disorder = FALSE,
424 .allow_specialists = TRUE,
425 .factor = {10, 5, 0, 4, 0, 25},
426 .happy_factor = 0
427 }
428 };
429 const char *names[ARRAY_SIZE(parameters)] = {
430 N_("?cma:Very happy"),
431 N_("?cma:Prefer food"),
432 N_("?cma:Prefer production"),
433 N_("?cma:Prefer gold"),
434 N_("?cma:Prefer science")
435 };
436
437 for (i = ARRAY_SIZE(parameters) - 1; i >= 0; i--) {
438 cmafec_preset_add(Q_(names[i]), &parameters[i]);
439 }
440}
void register_agent(const struct agent *agent)
Definition agents.c:382
@ CB_REMOVE
Definition agents.h:37
void attr_city_set(enum attr_city what, int city_id, size_t data_length, const void *const data)
Definition attribute.c:489
@ ATTR_CITY_CMAFE_PARAMETER
Definition attribute.h:39
int city_production_build_shield_cost(const struct city *pcity)
Definition city.c:737
int city_granary_size(int city_size)
Definition city.c:2132
bool city_production_is_genus(const struct city *pcity, enum impr_genus_id genus)
Definition city.c:717
static citizens city_size_get(const struct city *pcity)
Definition city.h:566
#define output_type_iterate(output)
Definition city.h:842
#define output_type_iterate_end
Definition city.h:848
static struct ai_type * self
Definition classicai.c:46
void cm_copy_parameter(struct cm_parameter *dest, const struct cm_parameter *const src)
Definition cm.c:2174
void cm_init_parameter(struct cm_parameter *dest)
Definition cm.c:2183
int cm_result_specialists(const struct cm_result *result)
Definition cm.c:2238
bool cm_are_parameter_equal(const struct cm_parameter *const p1, const struct cm_parameter *const p2)
Definition cm.c:2141
bool cma_is_city_under_agent(const struct city *pcity, struct cm_parameter *parameter)
Definition cma_core.c:552
bool cma_get_parameter(enum attr_city attr, int city_id, struct cm_parameter *parameter)
Definition cma_core.c:575
void cma_set_parameter(enum attr_city attr, int city_id, const struct cm_parameter *parameter)
Definition cma_core.c:624
const char * cmafec_get_short_descr(const struct cm_parameter *const parameter)
Definition cma_fec.c:236
void cmafec_set_fe_parameter(struct city *pcity, const struct cm_parameter *const parameter)
Definition cma_fec.c:105
void cmafec_free(void)
Definition cma_fec.c:94
static struct preset_list * preset_list
Definition cma_fec.c:62
#define RESULT_COLUMNS
Definition cma_fec.c:45
char * cmafec_preset_get_descr(int idx)
Definition cma_fec.c:169
const char * cmafec_get_result_descr(struct city *pcity, const struct cm_result *result, const struct cm_parameter *const parameter)
Definition cma_fec.c:322
#define BUFFER_SIZE
Definition cma_fec.c:46
const char * cmafec_get_short_descr_of_city(const struct city *pcity)
Definition cma_fec.c:221
static const char * get_city_growth_string(struct city *pcity, int surplus)
Definition cma_fec.c:251
const struct cm_parameter * cmafec_preset_get_parameter(int idx)
Definition cma_fec.c:182
void cmafec_preset_add(const char *descr_name, struct cm_parameter *pparam)
Definition cma_fec.c:136
int cmafec_preset_get_index_of_parameter(const struct cm_parameter *const parameter)
Definition cma_fec.c:196
static void city_remove(int city_id)
Definition cma_fec.c:68
static const char * get_prod_complete_string(struct city *pcity, int surplus)
Definition cma_fec.c:285
void cmafec_preset_remove(int idx)
Definition cma_fec.c:153
void cmafec_init(void)
Definition cma_fec.c:76
#define MAX_LEN_PRESET_NAME
Definition cma_fec.c:47
int cmafec_preset_num(void)
Definition cma_fec.c:213
void create_default_cma_presets(void)
Definition cma_fec.c:384
void cmafec_get_fe_parameter(struct city *pcity, struct cm_parameter *dest)
Definition cma_fec.c:115
char * incite_cost
Definition comments.c:76
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 const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
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:74
@ O_SHIELD
Definition fc_types.h:101
@ O_FOOD
Definition fc_types.h:101
@ O_TRADE
Definition fc_types.h:101
@ O_SCIENCE
Definition fc_types.h:101
@ O_LUXURY
Definition fc_types.h:101
@ O_GOLD
Definition fc_types.h:101
size_t get_internal_string_length(const char *text)
Definition fciconv.c:396
#define Q_(String)
Definition fcintl.h:70
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
#define N_(String)
Definition fcintl.h:69
const char * improvement_name_translation(const struct impr_type *pimprove)
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_debug(message,...)
Definition log.h:116
#define fc_malloc(sz)
Definition mem.h:34
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MAX(x, y)
Definition shared.h:54
const char * specialists_abbreviation_string(void)
Definition specialist.c:174
const char * specialists_string(const citizens *specialist_list)
Definition specialist.c:199
Definition agents.h:40
char name[MAX_LEN_NAME]
Definition ai.h:51
Definition city.h:317
int minimal_surplus[O_LAST]
Definition cm.h:41
Definition cm.h:52
bool found_a_valid
Definition cm.h:54
int surplus[O_LAST]
Definition cm.h:56
bool happy
Definition cm.h:54
citizens specialists[SP_MAX]
Definition cm.h:60
struct cm_parameter parameter
Definition cma_fec.c:51
char * descr
Definition cma_fec.c:50
int id
Definition unit.h:147
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47