-
Notifications
You must be signed in to change notification settings - Fork 7
/
history.c
368 lines (327 loc) · 9.4 KB
/
history.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
history: logging of game events
*/
#include "history.h"
#include <stdlib.h>
#include "bits.h"
#include "date.h"
#include "saving.h"
char *hist_alloc(history *hist)
{
size_t n=hist->nents++;
if(hist->nents>hist->nalloc)
{
size_t ns=hist->nalloc+1024;
char (*new)[HIST_LINE]=realloc(hist->ents, ns*sizeof(char [HIST_LINE]));
if(!new)
{
hist->nents--;
return(NULL);
}
hist->nalloc=ns;
hist->ents=new;
}
return(hist->ents[n]);
}
int hist_append(history *hist, const char line[HIST_LINE])
{
char *p=hist_alloc(hist);
if(!p) return(1);
memcpy(p, line, HIST_LINE);
return(0);
}
int hist_clear(history *hist)
{
if(!hist) return(1);
free(hist->ents);
hist->ents=NULL;
hist->nents=0;
hist->nalloc=0;
return(0);
}
int hist_save(history hist, FILE *out)
{
if(!out) return(1);
#ifdef WINDOWS
fprintf(out, "History:%lu\n", (unsigned long)hist.nents);
#else
fprintf(out, "History:%zu\n", hist.nents);
#endif
for(size_t i=0;i<hist.nents;i++)
{
char *line=hist.ents[i];
if(strchr(line, '\n')||strchr(line, '\\'))
{
while(*line)
{
switch(*line)
{
case '\n':
fputs("\\n", out);
break;
case '\\':
fputs("\\\\", out);
break;
default:
fputc(*line, out);
}
line++;
}
fputc('\n', out);
}
else
fprintf(out, "%s\n", line);
}
return(0);
}
int hist_load(FILE *in, size_t nents, history *hist)
{
char line[HIST_LINE];
if(!in) return(1);
if(!hist) return(1);
int rc=hist_clear(hist);
if(rc) return rc;
for(size_t i=0;i<nents;i++)
{
if(!fgets(line, HIST_LINE, in))
return(1);
size_t l=strlen(line);
if(line[l-1]=='\n') line[l-1]=0;
if(strchr(line, '\\'))
{
char line2[HIST_LINE];
char *d_in=line, *d_out=line2;
while(*d_in)
{
if(*d_in=='\\')
switch(*++d_in)
{
case 'n':
*d_out++='\n';
break;
default:
*d_out++=*d_in;
}
else
*d_out+=*d_in;
d_in++;
}
if(hist_append(hist, line2))
return(3);
}
else if(hist_append(hist, line))
return(3);
}
return(0);
}
int ev_append(history *hist, date d, harris_time t, const char *ev)
{
char *p=hist_alloc(hist);
if(!p) return(1);
size_t i=0;
i+=writedate(d, p+i);
p[i++]=' ';
i+=sprintf(p+i, "%02u:%02u ", t.hour, t.minute);
strncpy(p+i, ev, HIST_LINE-i);
return(0);
}
int eva_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, const char *ev)
{
char buf[HIST_LINE];
size_t i=sprintf(buf, "A ");
pacid(id, buf+i);i+=8;
buf[i++]=' ';
buf[i++]=ftr?'F':'B';
i+=sprintf(buf+i, "%d ", type);
strncpy(buf+i, ev, HIST_LINE-i);
return(ev_append(hist, d, t, buf));
}
int ct_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, unsigned int mark)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "CT %u", mark);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int na_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, unsigned int nid)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "NA %u", nid);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int pf_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type)
{
return(eva_append(hist, d, t, id, ftr, type, "PF"));
}
int ra_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, unsigned int tid)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "RA %u", tid);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int hi_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, unsigned int tid, unsigned int bmb)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "HI %u %u", tid, bmb);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int dmac_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, double ddmg, double cdmg, acid src)
{
char buf[HIST_LINE];
size_t i=snprintf(buf, 72, "DM "FLOAT" "FLOAT" AC ", CAST_FLOAT ddmg, CAST_FLOAT cdmg);
pacid(src, buf+i);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int dmfk_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, double ddmg, double cdmg, unsigned int fid)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "DM "FLOAT" "FLOAT" FK %u", CAST_FLOAT ddmg, CAST_FLOAT cdmg, fid);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int dmtf_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, double ddmg, double cdmg, unsigned int tid)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "DM "FLOAT" "FLOAT" TF %u", CAST_FLOAT ddmg, CAST_FLOAT cdmg, tid);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int fa_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type, unsigned int fa)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "FA %u", fa);
return(eva_append(hist, d, t, id, ftr, type, buf));
}
int cr_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type)
{
return(eva_append(hist, d, t, id, ftr, type, "CR"));
}
int sc_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type)
{
return(eva_append(hist, d, t, id, ftr, type, "SC"));
}
int ob_append(history *hist, date d, harris_time t, acid id, bool ftr, unsigned int type)
{
return(eva_append(hist, d, t, id, ftr, type, "OB"));
}
int evt_append(history *hist, date d, harris_time t, unsigned int tid, const char *ev)
{
char buf[HIST_LINE];
size_t i=snprintf(buf, HIST_LINE, "T %u ", tid);
strncpy(buf+i, ev, HIST_LINE-i);
return(ev_append(hist, d, t, buf));
}
int tdm_append(history *hist, date d, harris_time t, unsigned int tid, double ddmg, double cdmg)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "DM "FLOAT" "FLOAT, CAST_FLOAT ddmg, CAST_FLOAT cdmg);
return(evt_append(hist, d, t, tid, buf));
}
int tfk_append(history *hist, date d, harris_time t, unsigned int tid, double dflk, double cflk)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "FK "FLOAT" "FLOAT, CAST_FLOAT dflk, CAST_FLOAT cflk);
return(evt_append(hist, d, t, tid, buf));
}
int tsh_append(history *hist, date d, harris_time t, unsigned int tid)
{
return(evt_append(hist, d, t, tid, "SH"));
}
int evm_append(history *hist, date d, harris_time t, const char *ev)
{
char buf[HIST_LINE];
size_t i=sprintf(buf, "M ");
strncpy(buf+i, ev, HIST_LINE-i);
return(ev_append(hist, d, t, buf));
}
int ca_append(history *hist, date d, harris_time t, unsigned int cshr, unsigned int cash)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "CA %u %u", cshr, cash);
return(evm_append(hist, d, t, buf));
}
int co_append(history *hist, date d, harris_time t, double confid)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "CO "FLOAT, CAST_FLOAT confid);
return(evm_append(hist, d, t, buf));
}
int mo_append(history *hist, date d, harris_time t, double morale)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "MO "FLOAT, CAST_FLOAT morale);
return(evm_append(hist, d, t, buf));
}
int gp_append(history *hist, date d, harris_time t, unsigned int iclass, double gprod, double dprod)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "GP %u "FLOAT" "FLOAT, iclass, CAST_FLOAT gprod, CAST_FLOAT dprod);
return(evm_append(hist, d, t, buf));
}
int tp_append(history *hist, date d, harris_time t, enum t_class cls, bool ignore)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "TP %u %u", cls, ignore?1:0);
return(evm_append(hist, d, t, buf));
}
int ip_append(history *hist, date d, harris_time t, enum i_class cls, bool ignore)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "IP %u %u", cls, ignore?1:0);
return(evm_append(hist, d, t, buf));
}
int pg_append(history *hist, date d, harris_time t, unsigned int event)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "PG %u", event);
return(evm_append(hist, d, t, buf));
}
int evc_append(history *hist, date d, harris_time t, cmid id, enum cclass cls, const char *ev)
{
char buf[HIST_LINE];
size_t i=sprintf(buf, "C ");
pcmid(id, buf+i);i+=16;
buf[i++]=' ';
buf[i++]=cclasses[cls].letter;
buf[i++]=' ';
strncpy(buf+i, ev, HIST_LINE-i);
return(ev_append(hist, d, t, buf));
}
int ge_append(history *hist, date d, harris_time t, cmid id, enum cclass cls, unsigned int lrate)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "GE %u", lrate);
return(evc_append(hist, d, t, id, cls, buf));
}
int sk_append(history *hist, date d, harris_time t, cmid id, enum cclass cls, unsigned int skill)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "SK %u", skill);
return(evc_append(hist, d, t, id, cls, buf));
}
int st_append(history *hist, date d, harris_time t, cmid id, enum cclass cls, enum cstatus status)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "ST %c", cstatuses[status][0]);
return(evc_append(hist, d, t, id, cls, buf));
}
int op_append(history *hist, date d, harris_time t, cmid id, enum cclass cls, unsigned int tops)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "OP %u", tops);
return(evc_append(hist, d, t, id, cls, buf));
}
int de_append(history *hist, date d, harris_time t, cmid id, enum cclass cls)
{
return(evc_append(hist, d, t, id, cls, "DE"));
}
int pw_append(history *hist, date d, harris_time t, cmid id, enum cclass cls)
{
return(evc_append(hist, d, t, id, cls, "PW"));
}
int ex_append(history *hist, date d, harris_time t, cmid id, enum cclass cls, unsigned int rt)
{
char buf[HIST_LINE];
snprintf(buf, HIST_LINE, "EX %u", rt);
return(evc_append(hist, d, t, id, cls, buf));
}