Freeciv-3.4
Loading...
Searching...
No Matches
advchoice.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 "support.h"
20
21/* common */
22#include "improvement.h"
23#include "requirements.h"
24#include "unittype.h"
25
26#include "advchoice.h"
27
28/**********************************************************************/
31void adv_init_choice(struct adv_choice *choice)
32{
33 choice->value.utype = nullptr;
34 choice->want = 0;
35 choice->type = CT_NONE;
36 choice->need_boat = FALSE;
37
38#ifdef ADV_CHOICE_TRACK
39 choice->use = nullptr;
40 choice->log_if_chosen = FALSE;
41#endif /* ADV_CHOICE_TRACK */
42
43}
44
45/**********************************************************************/
48void adv_deinit_choice(struct adv_choice *choice)
49{
50#ifdef ADV_CHOICE_TRACK
51 if (choice->use != nullptr) {
52 free(choice->use);
53 choice->use = nullptr;
54 }
55#endif /* ADV_CHOICE_TRACK */
56}
57
58/**********************************************************************/
62{
63 struct adv_choice *choice = fc_malloc(sizeof(*choice));
64
65 adv_init_choice(choice);
66
67 return choice;
68}
69
70/**********************************************************************/
73void adv_free_choice(struct adv_choice *choice)
74{
75#ifdef ADV_CHOICE_TRACK
76 if (choice->use != nullptr) {
77 free(choice->use);
78 }
79#endif /* ADV_CHOICE_TRACK */
80
81 free(choice);
82}
83
84/**********************************************************************/
89 struct adv_choice *second)
90{
91 if (second->want > first->want) {
92 return second;
93 } else {
94 return first;
95 }
96}
97
98/**********************************************************************/
102 struct adv_choice *second)
103{
104 if (second->want > first->want) {
105 adv_free_choice(first);
106
107 return second;
108 } else {
110
111 return first;
112 }
113}
114
115/**********************************************************************/
119{
120 return type == CT_CIVILIAN || type == CT_ATTACKER || type == CT_DEFENDER;
121}
122
123/**********************************************************************/
127const char *adv_choice_rule_name(const struct adv_choice *choice)
128{
129 switch (choice->type) {
130 case CT_BUILDING:
131 return improvement_rule_name(choice->value.building);
132 case CT_NONE:
133 return "None";
134 case CT_CIVILIAN:
135 case CT_ATTACKER:
136 case CT_DEFENDER:
137 return utype_rule_name(choice->value.utype);
138 case CT_LAST:
139 break;
140 }
141
142 return "(unknown)";
143}
144
145#ifdef ADV_CHOICE_TRACK
146/**********************************************************************/
149void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
150{
151 if (dest != src) {
152 dest->type = src->type;
153 dest->value = src->value;
154 dest->want = src->want;
155 dest->need_boat = src->need_boat;
156 if (dest->use != nullptr) {
157 free(dest->use);
158 }
159 if (src->use != nullptr) {
160 dest->use = fc_strdup(src->use);
161 } else {
162 dest->use = nullptr;
163 }
164 dest->log_if_chosen = src->log_if_chosen;
165 }
166}
167
168/**********************************************************************/
171void adv_choice_set_use(struct adv_choice *choice, const char *use)
172{
173 if (choice->use != nullptr) {
174 free(choice->use);
175 }
176 choice->use = fc_strdup(use);
177}
178
179/**********************************************************************/
182void adv_choice_log_info(struct adv_choice *choice,
183 const char *loc1, const char *loc2)
184{
185 const char *use;
186 const char *name;
187
188 if (choice->use != nullptr) {
189 use = choice->use;
190 } else {
191 use = "<unknown>";
192 }
193
194 name = adv_choice_rule_name(choice);
195
196 if (loc2 != nullptr) {
197 log_base(ADV_CHOICE_LOG_LEVEL, "Choice at \"%s:%s\": %s, "
198 "want " ADV_WANT_PRINTF " as %s (%d)",
199 loc1, loc2, name, choice->want, use, choice->type);
200 } else {
201 log_base(ADV_CHOICE_LOG_LEVEL, "Choice at \"%s\": %s, "
202 "want " ADV_WANT_PRINTF " as %s (%d)",
203 loc1, name, choice->want, use, choice->type);
204 }
205}
206
207/**********************************************************************/
210void adv_choice_log_int(struct adv_choice *choice,
211 const char *loc1, int loc2)
212{
213 char buf[20];
214
215 fc_snprintf(buf, sizeof(buf), "%d", loc2);
216 adv_choice_log_info(choice, loc1, buf);
217}
218
219/**********************************************************************/
222const char *adv_choice_get_use(const struct adv_choice *choice)
223{
224 if (choice->use == nullptr) {
225 return "(unset)";
226 }
227
228 return choice->use;
229}
230
231#endif /* ADV_CHOICE_TRACK */
void adv_deinit_choice(struct adv_choice *choice)
Definition advchoice.c:48
void adv_init_choice(struct adv_choice *choice)
Definition advchoice.c:31
struct adv_choice * adv_better_choice_free(struct adv_choice *first, struct adv_choice *second)
Definition advchoice.c:101
void adv_free_choice(struct adv_choice *choice)
Definition advchoice.c:73
struct adv_choice * adv_better_choice(struct adv_choice *first, struct adv_choice *second)
Definition advchoice.c:88
struct adv_choice * adv_new_choice(void)
Definition advchoice.c:61
bool is_unit_choice_type(enum choice_type type)
Definition advchoice.c:118
const char * adv_choice_rule_name(const struct adv_choice *choice)
Definition advchoice.c:127
#define adv_choice_set_use(_choice, _use)
Definition advchoice.h:85
#define adv_choice_log_int(_choice, _loc1, _loc2)
Definition advchoice.h:88
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition advchoice.h:79
choice_type
Definition advchoice.h:36
@ CT_CIVILIAN
Definition advchoice.h:39
@ CT_LAST
Definition advchoice.h:42
@ CT_DEFENDER
Definition advchoice.h:41
@ CT_ATTACKER
Definition advchoice.h:40
@ CT_NONE
Definition advchoice.h:37
@ CT_BUILDING
Definition advchoice.h:38
static const char * adv_choice_get_use(const struct adv_choice *choice)
Definition advchoice.h:89
#define adv_choice_log_info(_choice, _loc1, _loc2)
Definition advchoice.h:87
char * incite_cost
Definition comments.c:77
#define ADV_WANT_PRINTF
Definition fc_types.h:1051
GType type
Definition repodlgs.c:1313
const char * improvement_rule_name(const struct impr_type *pimprove)
const char * name
Definition inputfile.c:127
#define log_base(level, message,...)
Definition log.h:95
#define fc_strdup(str)
Definition mem.h:43
#define fc_malloc(sz)
Definition mem.h:34
enum choice_type type
Definition advchoice.h:46
adv_want want
Definition advchoice.h:48
universals_u value
Definition advchoice.h:47
bool need_boat
Definition advchoice.h:49
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:925
#define FALSE
Definition support.h:47
const struct unit_type * utype
Definition fc_types.h:536
const struct impr_type * building
Definition fc_types.h:529
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1604