-
Notifications
You must be signed in to change notification settings - Fork 74
/
commodities.c
250 lines (216 loc) · 6.09 KB
/
commodities.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
/*
Copyright (C) 2014 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "commodities.h"
#include "string-utils.h"
#include "nonuniform_random_sampler.h"
static int parse_error(char *filename, char *line, int ln, char *bad_word)
{
if (bad_word)
printf("Error at line %s:%d at %s\n", filename, ln, bad_word);
else
printf("Error at line %s:%d\n", filename, ln);
return -1;
}
static int parse_float_field(char *filename, char *line, int ln, float *value, char **saveptr)
{
char *c;
char word[100];
int rc;
c = strtok_r(NULL, ",", saveptr);
if (!c)
return parse_error(filename, line, ln, NULL);
strcpy(word, c);
clean_spaces(word);
rc = sscanf(word, "%f", value);
if (rc != 1)
return parse_error(filename, line, ln, word);
return 0;
}
static int parse_int_field(char *filename, char *line, int ln, int *value, char **saveptr)
{
char *c;
char word[100];
int rc;
c = strtok_r(NULL, ",", saveptr);
if (!c)
return parse_error(filename, line, ln, NULL);
strcpy(word, c);
clean_spaces(word);
rc = sscanf(word, "%d", value);
if (rc != 1)
return parse_error(filename, line, ln, word);
return 0;
}
static int parse_line(char *filename, char *line, int ln, struct commodity *c)
{
char *x, *saveptr;
char word[100];
int rc;
if (line[0] == '#')
return 1;
clean_spaces(line);
if (strcmp(line, "") == 0)
return 1;
/* Name */
x = strtok_r(line, ",", &saveptr);
if (!x)
return parse_error(filename, line, ln, NULL);
strcpy(word, x);
clean_spaces(word);
uppercase(word);
strcpy(c->name, word);
/* unit */
x = strtok_r(NULL, ",", &saveptr);
if (!x)
return parse_error(filename, line, ln, NULL);
strcpy(word, x);
clean_spaces(word);
uppercase(word);
strcpy(c->unit, word);
rc = parse_float_field(filename, line, ln, &c->base_price, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->volatility, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->legality, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->econ_sensitivity, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->govt_sensitivity, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->tech_sensitivity, &saveptr);
if (rc)
return rc;
rc = parse_int_field(filename, line, ln, &c->odds, &saveptr);
if (rc)
return rc;
return 0;
}
#define MAX_COMMODITIES 100
struct commodity *read_commodities(char *filename, int *ncommodities)
{
FILE *f;
char line[1000];
char *l;
struct commodity c, *clist;
int rc, ln = 0;
int n = 0;
n = 0;
clist = malloc(sizeof(*clist) * MAX_COMMODITIES);
memset(clist, 0, sizeof(*clist) * MAX_COMMODITIES);
f = fopen(filename, "r");
if (!f)
return NULL;
while (!feof(f)) {
l = fgets(line, 1000, f);
if (!l)
break;
line[strlen(line) - 1] = '\0';
ln++;
rc = parse_line(filename, line, ln, &c);
switch (rc) {
case 0:
clist[n] = c;
n++;
continue;
case 1: /* comment */
continue;
case -1:
return NULL;
}
}
*ncommodities = n;
return clist;
}
/* economy, tech_level, government will be between 0.0 and 1.0 indicating the
* "wealthiness", "techiness", and "government stability" of the planet,
* respectively.
*/
float commodity_calculate_price(struct commodity *c,
float economy, float tech_level, float government)
{
float econ_price_factor, tech_level_factor, government_factor, price;
econ_price_factor = 2.0f * (economy - 0.5f) * c->econ_sensitivity;
tech_level_factor = 2.0f * (tech_level - 0.5f) * c->tech_sensitivity;
government_factor = 2.0f * (government - 0.5f) * c->govt_sensitivity;
price = c->base_price +
c->base_price * econ_price_factor +
c->base_price * tech_level_factor +
c->base_price * government_factor;
/* TODO: add some random variability, but not enough to swamp. */
return price;
}
int add_commodity(struct commodity **c, int *ncommodities, const char *name, const char *unit,
float base_price, float volatility, float legality,
float econ_sensitivity, float govt_sensitivity, float tech_sensitivity, int odds)
{
struct commodity *newc;
int n = *ncommodities + 1;
newc = realloc(*c, n * sizeof(newc[0]));
if (!newc)
return -1;
*c = newc;
newc = &(*c)[n - 1];
strncpy(newc->name, name, sizeof(newc->name));
strncpy(newc->unit, unit, sizeof(newc->name));
newc->base_price = base_price;
newc->volatility = volatility;
newc->legality = legality;
newc->econ_sensitivity = econ_sensitivity;
newc->govt_sensitivity = govt_sensitivity;
newc->tech_sensitivity = tech_sensitivity;
newc->odds = odds;
*ncommodities = n;
return n - 1;
}
#ifdef TESTCOMMODITIES
static void test_price(struct commodity *c, float e, float g, float t)
{
float p;
p = commodity_calculate_price(c, e, t, g);
printf("%s %0.2f %0.2f %0.2f %3.2f\n", c->name, e, t, g, p);
}
int main(int argc, char *argv[])
{
struct commodity *c;
float e, g, t;
int i, ncommodities;
if (argc < 2) {
fprintf(stderr, "usage: test-commodities commodities-file\n");
return 1;
}
c = read_commodities(argv[1], &ncommodities);
if (!c) {
fprintf(stderr, "Failed to read commodities file %s\n", argv[1]);
return 1;
}
for (i = 0; i < ncommodities; i++)
for (e = -1.0; e <= 1.0; e += 0.1)
for (g = -1.0; g <= 1.0; g += 0.1)
for (t = -1.0; t <= 1.0; t += 0.1)
test_price(&c[i], e, g, t);
return 0;
}
#endif