Freeciv-3.3
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 = NULL;
34 choice->want = 0;
35 choice->type = CT_NONE;
36 choice->need_boat = FALSE;
37#ifdef ADV_CHOICE_TRACK
38 choice->use = NULL;
39 choice->log_if_chosen = FALSE;
40#endif /* ADV_CHOICE_TRACK */
41}
42
43/**********************************************************************/
46void adv_deinit_choice(struct adv_choice *choice)
47{
48#ifdef ADV_CHOICE_TRACK
49 if (choice->use != NULL) {
50 free(choice->use);
51 choice->use = NULL;
52 }
53#endif /* ADV_CHOICE_TRACK */
54}
55
56/**********************************************************************/
60{
61 struct adv_choice *choice = fc_malloc(sizeof(*choice));
62
63 adv_init_choice(choice);
64
65 return choice;
66}
67
68/**********************************************************************/
71void adv_free_choice(struct adv_choice *choice)
72{
73#ifdef ADV_CHOICE_TRACK
74 if (choice->use != NULL) {
75 free(choice->use);
76 }
77#endif /* ADV_CHOICE_TRACK */
78 free(choice);
79}
80
81/**********************************************************************/
86 struct adv_choice *second)
87{
88 if (second->want > first->want) {
89 return second;
90 } else {
91 return first;
92 }
93}
94
95/**********************************************************************/
99 struct adv_choice *second)
100{
101 if (second->want > first->want) {
102 adv_free_choice(first);
103
104 return second;
105 } else {
107
108 return first;
109 }
110}
111
112/**********************************************************************/
116{
117 return type == CT_CIVILIAN || type == CT_ATTACKER || type == CT_DEFENDER;
118}
119
120/**********************************************************************/
124const char *adv_choice_rule_name(const struct adv_choice *choice)
125{
126 switch (choice->type) {
127 case CT_BUILDING:
128 return improvement_rule_name(choice->value.building);
129 case CT_NONE:
130 return "None";
131 case CT_CIVILIAN:
132 case CT_ATTACKER:
133 case CT_DEFENDER:
134 return utype_rule_name(choice->value.utype);
135 case CT_LAST:
136 break;
137 }
138
139 return "(unknown)";
140}
141
142#ifdef ADV_CHOICE_TRACK
143/**********************************************************************/
146void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
147{
148 if (dest != src) {
149 dest->type = src->type;
150 dest->value = src->value;
151 dest->want = src->want;
152 dest->need_boat = src->need_boat;
153 if (dest->use != NULL) {
154 free(dest->use);
155 }
156 if (src->use != NULL) {
157 dest->use = fc_strdup(src->use);
158 } else {
159 dest->use = NULL;
160 }
161 dest->log_if_chosen = src->log_if_chosen;
162 }
163}
164
165/**********************************************************************/
168void adv_choice_set_use(struct adv_choice *choice, const char *use)
169{
170 if (choice->use != NULL) {
171 free(choice->use);
172 }
173 choice->use = fc_strdup(use);
174}
175
176/**********************************************************************/
179void adv_choice_log_info(struct adv_choice *choice,
180 const char *loc1, const char *loc2)
181{
182 const char *use;
183 const char *name;
184
185 if (choice->use != NULL) {
186 use = choice->use;
187 } else {
188 use = "<unknown>";
189 }
190
191 name = adv_choice_rule_name(choice);
192
193 if (loc2 != NULL) {
194 log_base(ADV_CHOICE_LOG_LEVEL, "Choice at \"%s:%s\": %s, "
195 "want " ADV_WANT_PRINTF " as %s (%d)",
196 loc1, loc2, name, choice->want, use, choice->type);
197 } else {
198 log_base(ADV_CHOICE_LOG_LEVEL, "Choice at \"%s\": %s, "
199 "want " ADV_WANT_PRINTF " as %s (%d)",
200 loc1, name, choice->want, use, choice->type);
201 }
202}
203
204/**********************************************************************/
207void adv_choice_log_int(struct adv_choice *choice,
208 const char *loc1, int loc2)
209{
210 char buf[20];
211
212 fc_snprintf(buf, sizeof(buf), "%d", loc2);
213 adv_choice_log_info(choice, loc1, buf);
214}
215
216/**********************************************************************/
219const char *adv_choice_get_use(const struct adv_choice *choice)
220{
221 if (choice->use == NULL) {
222 return "(unset)";
223 }
224
225 return choice->use;
226}
227
228#endif /* ADV_CHOICE_TRACK */
void adv_deinit_choice(struct adv_choice *choice)
Definition advchoice.c:46
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:98
void adv_free_choice(struct adv_choice *choice)
Definition advchoice.c:71
struct adv_choice * adv_better_choice(struct adv_choice *first, struct adv_choice *second)
Definition advchoice.c:85
struct adv_choice * adv_new_choice(void)
Definition advchoice.c:59
bool is_unit_choice_type(enum choice_type type)
Definition advchoice.c:115
const char * adv_choice_rule_name(const struct adv_choice *choice)
Definition advchoice.c:124
#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:76
#define ADV_WANT_PRINTF
Definition fc_types.h:1064
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:960
#define FALSE
Definition support.h:47
const struct unit_type * utype
Definition fc_types.h:553
const struct impr_type * building
Definition fc_types.h:546
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1584