forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet_type.c
275 lines (223 loc) · 5.69 KB
/
planet_type.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "universe.h"
#include "common.h"
#include "log.h"
#include "planet_type.h"
#include "port.h"
#include "parseconfig.h"
#include "ptrlist.h"
#include "stringtree.h"
const char planet_life_desc[PLANET_LIFE_NUM][PLANET_LIFE_DESC_LEN] = {
"Toxic to life",
"Barren, no life",
"Dead planet",
"Single-cellular life",
"Bacterial life",
"Simple lifeforms",
"Resistant plantlife",
"Complex plantlife",
"Animal life",
"Intelligent humanoid life"
};
char planet_life_names[PLANET_LIFE_NUM][PLANET_LIFE_NAME_LEN] = {
"TOXIC",
"BARREN",
"DEAD",
"SINGLECELL",
"BACTERIA",
"SIMPLE",
"RESISTANT",
"COMPLEX",
"ANIMAL",
"INTELLIGENT",
};
char planet_zone_names[PLANET_ZONE_NUM][PLANET_ZONE_NAME_LEN] = {
"HOT",
"ECO",
"COLD",
};
static void planet_type_init(struct planet_type * const type)
{
memset(type, 0, sizeof(*type));
ptrlist_init(&type->port_types);
INIT_LIST_HEAD(&type->list);
}
void planet_type_free(struct planet_type * const type)
{
free(type->name);
free(type->desc);
free(type->surface);
free(type->atmo);
ptrlist_free(&type->port_types);
}
static int set_atmosphere(struct planet_type *type, struct config *conf)
{
if (!conf->str)
return -1;
type->atmo = strdup(conf->str);
if (!type->atmo)
return -1;
return 0;
}
static int set_port_types(struct planet_type *type, struct config *conf)
{
struct config *child;
struct port_type *port;
list_for_each_entry(child, &conf->children, list) {
port = st_lookup_string(&univ.port_type_names, child->key);
if (!port)
return -1;
ptrlist_push(&type->port_types, port);
}
return 0;
}
static int set_description(struct planet_type *type, struct config *conf)
{
if (!conf->str)
return -1;
type->desc = strdup(conf->str);
if (!type->desc)
return -1;
return 0;
}
static int set_life(enum planet_life *life, char *value)
{
int i;
struct list_head life_root = LIST_HEAD_INIT(life_root);
int lifes[PLANET_LIFE_NUM];
if (!value)
return -1;
for (i = 0; i < ARRAY_SIZE(lifes); i++)
lifes[i] = i;
for (i = 0; i < ARRAY_SIZE(lifes); i++)
if (st_add_string(&life_root, planet_life_names[i], &lifes[i]))
return -1;
int *j = st_lookup_string(&life_root, value);
if (!j)
return -1;
*life = *j;
st_destroy(&life_root, ST_DONT_FREE_DATA);
return 0;
}
static int set_maximum_diameter(struct planet_type *type, struct config *conf)
{
type->maxdia = limit_long_to_uint(conf->l);
return 0;
}
static int set_maximum_life(struct planet_type *type, struct config *conf)
{
return set_life(&type->maxlife, conf->str);
}
static int set_minimum_diameter(struct planet_type *type, struct config *conf)
{
type->mindia = limit_long_to_uint(conf->l);
return 0;
}
static int set_minimum_life(struct planet_type *type, struct config *conf)
{
return set_life(&type->minlife, conf->str);
}
static int set_name(struct planet_type *type, struct config *conf)
{
if (!conf->str)
return -1;
type->name = strdup(conf->str);
if (!type->name)
return -1;
return 0;
}
static int set_surface(struct planet_type *type, struct config *conf)
{
if (!conf->str)
return -1;
type->surface = strdup(conf->str);
if (!type->surface)
return -1;
return 0;
}
static int set_zones(struct planet_type *type, struct config *conf)
{
struct config *child;
unsigned int i;
struct list_head zone_root = LIST_HEAD_INIT(zone_root);
int zones[PLANET_ZONE_NUM];
for (i = 0; i < ARRAY_SIZE(zones); i++)
zones[i] = i;
for (i = 0; i < ARRAY_SIZE(zones); i++)
if (st_add_string(&zone_root, planet_zone_names[i], &zones[i]))
return -1;
list_for_each_entry(child, &conf->children, list) {
int *j = st_lookup_string(&zone_root, child->key);
if (!j)
return -1;
type->zones[*j] = 1;
}
st_destroy(&zone_root, ST_DONT_FREE_DATA);
return 0;
}
static int build_command_tree(struct list_head *root)
{
if (st_add_string(root, "atmosphere", set_atmosphere))
return -1;
if (st_add_string(root, "ports", set_port_types))
return -1;
if (st_add_string(root, "description", set_description))
return -1;
if (st_add_string(root, "maxdiameter", set_maximum_diameter))
return -1;
if (st_add_string(root, "maxlife", set_maximum_life))
return -1;
if (st_add_string(root, "mindiameter", set_minimum_diameter))
return -1;
if (st_add_string(root, "minlife", set_minimum_life))
return -1;
if (st_add_string(root, "name", set_name))
return -1;
if (st_add_string(root, "surface", set_surface))
return -1;
if (st_add_string(root, "zones", set_zones))
return -1;
return 0;
}
int load_planets_from_file(const char * const file, struct universe * const universe)
{
struct list_head conf_root = LIST_HEAD_INIT(conf_root);
struct list_head cmd_root = LIST_HEAD_INIT(cmd_root);
struct config *conf, *child;
struct planet_type *pl_type;
int (*func)(struct planet_type*, struct config*);
assert(file);
if (build_command_tree(&cmd_root))
return -1;
if (parse_config_file(file, &conf_root))
return -1;
list_for_each_entry(conf, &conf_root, list) {
pl_type = malloc(sizeof(*pl_type));
if (!pl_type)
goto err;
planet_type_init(pl_type);
pl_type->c = conf->key[0];
list_for_each_entry(child, &conf->children, list) {
func = st_lookup_string(&cmd_root, child->key);
if (!func) {
log_printfn(LOG_CONFIG, "unknown planet type key: \"%s\"", child->key);
continue;
}
if (func(pl_type, child)) {
log_printfn(LOG_CONFIG, "syntax error or out of memory when processing planet type key \"%s\"", child->key);
continue;
}
}
list_add_tail(&pl_type->list, &universe->planet_types);
}
destroy_config(&conf_root);
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return 0;
err:
destroy_config(&conf_root);
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return -1;
}