Freeciv21
Develop your civilization from humble roots to a global empire
advchoice.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // utility
15 #include "support.h"
16 
17 // common
18 #include "improvement.h"
19 #include "requirements.h"
20 #include "unittype.h"
21 
22 #include "advchoice.h"
23 
27 void adv_init_choice(struct adv_choice *choice)
28 {
29  choice->value.utype = nullptr;
30  choice->want = 0;
31  choice->type = CT_NONE;
32  choice->need_boat = false;
33 #ifdef ADV_CHOICE_TRACK
34  choice->use = nullptr;
35  choice->log_if_chosen = FALSE;
36 #endif // ADV_CHOICE_TRACK
37 }
38 
42 void adv_deinit_choice(struct adv_choice *choice)
43 {
44 #ifdef ADV_CHOICE_TRACK
45  if (choice->use != nullptr) {
46  free(choice->use);
47  choice->use = nullptr;
48  }
49 #endif // ADV_CHOICE_TRACK
50 }
51 
56 {
57  struct adv_choice *choice = new adv_choice();
58 
59  adv_init_choice(choice);
60 
61  return choice;
62 }
63 
67 void adv_free_choice(struct adv_choice *choice)
68 {
69 #ifdef ADV_CHOICE_TRACK
70  if (choice->use != nullptr) {
71  free(choice->use);
72  }
73 #endif // ADV_CHOICE_TRACK
74  delete choice;
75 }
76 
81 struct adv_choice *adv_better_choice(struct adv_choice *first,
82  struct adv_choice *second)
83 {
84  if (second->want > first->want) {
85  return second;
86  } else {
87  return first;
88  }
89 }
90 
95  struct adv_choice *second)
96 {
97  if (second->want > first->want) {
98  adv_free_choice(first);
99 
100  return second;
101  } else {
102  adv_free_choice(second);
103 
104  return first;
105  }
106 }
107 
112 {
113  return type == CT_CIVILIAN || type == CT_ATTACKER || type == CT_DEFENDER;
114 }
115 
116 #ifdef ADV_CHOICE_TRACK
120 void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
121 {
122  if (dest != src) {
123  dest->type = src->type;
124  dest->value = src->value;
125  dest->want = src->want;
126  dest->need_boat = src->need_boat;
127  if (dest->use != nullptr) {
128  free(dest->use);
129  }
130  if (src->use != nullptr) {
131  dest->use = fc_strdup(src->use);
132  } else {
133  dest->use = nullptr;
134  }
135  dest->log_if_chosen = src->log_if_chosen;
136  }
137 }
138 
142 void adv_choice_set_use(struct adv_choice *choice, const char *use)
143 {
144  if (choice->use != nullptr) {
145  free(choice->use);
146  }
147  choice->use = fc_strdup(use);
148 }
149 
153 void adv_choice_log_info(struct adv_choice *choice, const char *loc1,
154  const char *loc2)
155 {
156  const char *use;
157  const char *name;
158 
159  if (choice->use != nullptr) {
160  use = choice->use;
161  } else {
162  use = "<unknown>";
163  }
164 
165  if (choice->type == CT_BUILDING) {
167  } else if (choice->type == CT_NONE) {
168  name = "None";
169  } else {
170  name = utype_rule_name(choice->value.utype);
171  }
172 
173  if (loc2 != nullptr) {
174  log_base(ADV_CHOICE_QtMsgType,
175  "Choice at \"%s:%s\": %s, "
176  "want " ADV_WANT_PRINTF " as %s (%d)",
177  loc1, loc2, name, choice->want, use, choice->type);
178  } else {
179  log_base(ADV_CHOICE_QtMsgType,
180  "Choice at \"%s\": %s, "
181  "want " ADV_WANT_PRINTF " as %s (%d)",
182  loc1, name, choice->want, use, choice->type);
183  }
184 }
185 
189 const char *adv_choice_get_use(const struct adv_choice *choice)
190 {
191  if (choice->use == nullptr) {
192  return "(unset)";
193  }
194 
195  return choice->use;
196 }
197 
198 #endif // ADV_CHOICE_TRACK
void adv_deinit_choice(struct adv_choice *choice)
Clear choice without freeing it itself.
Definition: advchoice.cpp:42
void adv_init_choice(struct adv_choice *choice)
Sets the values of the choice to initial values.
Definition: advchoice.cpp:27
struct adv_choice * adv_new_choice()
Dynamically allocate a new choice.
Definition: advchoice.cpp:55
void adv_free_choice(struct adv_choice *choice)
Free dynamically allocated choice.
Definition: advchoice.cpp:67
struct adv_choice * adv_better_choice(struct adv_choice *first, struct adv_choice *second)
Return better one of the choices given.
Definition: advchoice.cpp:81
struct adv_choice * adv_better_choice_free(struct adv_choice *first, struct adv_choice *second)
Return better one of the choices given, and free the other.
Definition: advchoice.cpp:94
bool is_unit_choice_type(enum choice_type type)
Does choice type refer to unit.
Definition: advchoice.cpp:111
#define adv_choice_set_use(_choice, _use)
Definition: advchoice.h:69
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition: advchoice.h:62
static const char * adv_choice_get_use(const struct adv_choice *choice)
Definition: advchoice.h:71
choice_type
Definition: advchoice.h:22
@ CT_CIVILIAN
Definition: advchoice.h:25
@ CT_DEFENDER
Definition: advchoice.h:27
@ CT_ATTACKER
Definition: advchoice.h:26
@ CT_NONE
Definition: advchoice.h:23
@ CT_BUILDING
Definition: advchoice.h:24
#define adv_choice_log_info(_choice, _loc1, _loc2)
Definition: advchoice.h:70
#define ADV_WANT_PRINTF
Definition: fc_types.h:1145
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
const char * name
Definition: inputfile.cpp:118
#define log_base(level, message,...)
Definition: log.h:41
enum choice_type type
Definition: advchoice.h:32
adv_want want
Definition: advchoice.h:34
universals_u value
Definition: advchoice.h:33
bool need_boat
Definition: advchoice.h:35
#define fc_strdup(str)
Definition: support.h:111
const struct unit_type * utype
Definition: fc_types.h:585
const struct impr_type * building
Definition: fc_types.h:579
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274