Freeciv-3.3: /fast/freeciv/nightly/main/server/advisors/advchoice.c Source File

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/**********************************************************************/
31 void 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/**********************************************************************/
46 void 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/**********************************************************************/
59 struct adv_choice *adv_new_choice(void)
60{
61 struct adv_choice *choice = fc_malloc(sizeof(*choice));
62
63 adv_init_choice(choice);
64
65 return choice;
66}
67
68/**********************************************************************/
71 void 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/**********************************************************************/
85 struct adv_choice *adv_better_choice(struct adv_choice *first,
86 struct adv_choice *second)
87{
88 if (second->want > first->want) {
89 return second;
90 } else {
91 return first;
92 }
93}
94
95/**********************************************************************/
98 struct adv_choice *adv_better_choice_free(struct adv_choice *first,
99 struct adv_choice *second)
100{
101 if (second->want > first->want) {
102 adv_free_choice(first);
103
104 return second;
105 } else {
106 adv_free_choice(second);
107
108 return first;
109 }
110}
111
112/**********************************************************************/
115 bool is_unit_choice_type(enum choice_type type)
116{
117 return type == CT_CIVILIAN || type == CT_ATTACKER || type == CT_DEFENDER;
118}
119
120/**********************************************************************/
124 const 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 */
adv_deinit_choice
void adv_deinit_choice(struct adv_choice *choice)
Definition advchoice.c:46
adv_init_choice
void adv_init_choice(struct adv_choice *choice)
Definition advchoice.c:31
adv_better_choice_free
struct adv_choice * adv_better_choice_free(struct adv_choice *first, struct adv_choice *second)
Definition advchoice.c:98
adv_free_choice
void adv_free_choice(struct adv_choice *choice)
Definition advchoice.c:71
adv_better_choice
struct adv_choice * adv_better_choice(struct adv_choice *first, struct adv_choice *second)
Definition advchoice.c:85
adv_new_choice
struct adv_choice * adv_new_choice(void)
Definition advchoice.c:59
is_unit_choice_type
bool is_unit_choice_type(enum choice_type type)
Definition advchoice.c:115
adv_choice_rule_name
const char * adv_choice_rule_name(const struct adv_choice *choice)
Definition advchoice.c:124
advchoice.h
adv_choice_set_use
#define adv_choice_set_use(_choice, _use)
Definition advchoice.h:85
adv_choice_log_int
#define adv_choice_log_int(_choice, _loc1, _loc2)
Definition advchoice.h:88
adv_choice_copy
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition advchoice.h:79
choice_type
choice_type
Definition advchoice.h:36
CT_CIVILIAN
@ CT_CIVILIAN
Definition advchoice.h:39
CT_LAST
@ CT_LAST
Definition advchoice.h:42
CT_DEFENDER
@ CT_DEFENDER
Definition advchoice.h:41
CT_ATTACKER
@ CT_ATTACKER
Definition advchoice.h:40
CT_NONE
@ CT_NONE
Definition advchoice.h:37
CT_BUILDING
@ CT_BUILDING
Definition advchoice.h:38
adv_choice_get_use
static const char * adv_choice_get_use(const struct adv_choice *choice)
Definition advchoice.h:89
adv_choice_log_info
#define adv_choice_log_info(_choice, _loc1, _loc2)
Definition advchoice.h:87
incite_cost
char * incite_cost
Definition comments.c:74
ADV_WANT_PRINTF
#define ADV_WANT_PRINTF
Definition fc_types.h:1366
type
GType type
Definition repodlgs.c:1313
improvement_rule_name
const char * improvement_rule_name(const struct impr_type *pimprove)
Definition improvement.c:252
improvement.h
name
const char * name
Definition inputfile.c:127
log_base
#define log_base(level, message,...)
Definition log.h:94
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
requirements.h
adv_choice
Definition advchoice.h:45
adv_choice::type
enum choice_type type
Definition advchoice.h:46
adv_choice::want
adv_want want
Definition advchoice.h:48
adv_choice::value
universals_u value
Definition advchoice.h:47
adv_choice::need_boat
bool need_boat
Definition advchoice.h:49
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
support.h
FALSE
#define FALSE
Definition support.h:47
universals_u::utype
const struct unit_type * utype
Definition fc_types.h:728
universals_u::building
const struct impr_type * building
Definition fc_types.h:721
utype_rule_name
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1578
unittype.h