Repository navigation
Expand file tree
/
Copy pathltm.c
More file actions
386 lines (317 loc) · 9.17 KB
/
Copy pathltm.c
File metadata and controls
386 lines (317 loc) · 9.17 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic 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 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic 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 Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* ltm.c:
*
* This file contains functions for maintaining a database or "long
* term memory"
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <setjmp.h>
#include "have_strlcat.h"
#include "have_strlcpy.h"
#include "strl.h"
#include "modern_curses.h"
#include "types.h"
#include "config.h"
#include "globals.h"
#include "install.h"
/* static declarations */
static bool nosave = false; /* True ==> dont write ltm back out */
static char ltmnam[TY_BUF + 1]; /* Long term memory file name, +1 for paranoia */
static void readltm(void);
static void parsemonster(char *monster);
static void clearltm(ltmrec *ltmarr);
/*
* mapcharacter: Read a character help message
*/
void
mapcharacter(char ch, char *str)
{
dwait(D_CONTROL, __func__, "'%c' ==> '%s'", ch, str);
/* Ancient versions of Rogue had no wands or staves */
if (ch == '/' && stlmatch(str, "unknown")) {
version = RV36A;
/* Dont map any unknown character */
} else if (stlmatch(str, "unknown")) {
;
/* If it is a monster, set its array index */
} else if (ch >= 'a' && ch <= 'z') {
monindex[ch - 'a' + 1] = addmonhist(str);
}
}
/*
* addmonhist: Return the monster index of a given monster name in the
* history array. Create an entry if none exists.
*/
int
addmonhist(char *monster)
{
int m;
/* Search for the monsters entry in the table */
for (m = 0; m < nextmon; m++) {
if (streq(monster, monhist[m].m_name)) {
return (m);
}
}
if (nextmon >= MAXMON) { /* Check for overflow */
dwait(D_FATAL, __func__, "Overflowed monster array");
}
strlcpy(monhist[nextmon].m_name, monster, sizeof(monhist[nextmon].m_name)); /* Copy in the name */
return (nextmon++); /* Return the index */
}
/*
* findmonster: Return the monster index of a given monster name in the
* history array. Return -1 if the monster is not in the table.
*/
int
findmonster(char *monster)
{
int m;
/* Search for the monsters entry in the table */
for (m = 0; m < nextmon; m++) {
if (streq(monster, monhist[m].m_name)) {
return (m);
}
}
return (-1);
}
/*
* saveltm: Write the new monster information out to the long term memory
* file for this version of Rogue. Be careful about serializing
* access to the output file.
*/
void
saveltm(int score)
{
int m;
FILE *ltmfil;
int lock_fd;
if (nextmon < 1 || nosave) {
return;
}
dwait(D_CONTROL, __func__, "writing file: %s", ltmnam);
/* Disable interrupts and open the file for writing */
critical();
/* lock */
lock_fd = lock_file(__func__, NULL, lock_path);
/* Only write out the new results if we can get write access */
if ((ltmfil = wopen(ltmnam, "w")) == NULL) {
dwait(D_WARNING, __func__, "Can't write long term memory file: %s", ltmnam);
} else {
/* Write the ltm file header */
fprintf(ltmfil, "Count %d, sum %d, start %d, saved %d\n", ltm.gamecnt + 1, ltm.gamesum + score, ltm.inittime,
ltm.timeswritten + 1);
/* Now write a line for each monster */
for (m = 0; m < nextmon; m++) {
fprintf(ltmfil, "%s|", monhist[m].m_name);
writeprob(ltmfil, &monhist[m].wehit);
fprintf(ltmfil, "|");
writeprob(ltmfil, &monhist[m].theyhit);
fprintf(ltmfil, "|");
writeprob(ltmfil, &monhist[m].arrowhit);
fprintf(ltmfil, "|");
writestat(ltmfil, &monhist[m].htokill);
fprintf(ltmfil, "|");
writestat(ltmfil, &monhist[m].damage);
fprintf(ltmfil, "|");
writestat(ltmfil, &monhist[m].atokill);
fprintf(ltmfil, "|\n");
}
/* Close the file and unlock it */
fclose(ltmfil);
/* unlock */
unlock_file(__func__, lock_fd);
}
/* Re-enable interrupts */
uncritical();
}
/*
* restoreltm: Read the long term memory file.
*/
void
restoreltm(void)
{
int lock_fd;
memset(ltmnam, 0, sizeof(ltmnam));
snprintf(ltmnam, sizeof(ltmnam) - 1, "%s/ltm%d", rgmdir, version);
dwait(D_CONTROL, __func__, "reading file: %s", ltmnam);
clearltm(monhist); /* Clear the original sums */
nextmon = 0; /* Zero the list of monsters */
monindex[0] = addmonhist("it"); /* Monster 0 is "it" */
/* Disable interrupts and open the file for reading */
critical();
/* lock */
lock_fd = lock_file(__func__, NULL, lock_path);
/* Only read the long term memory if we can get access */
if (fexists(ltmnam)) {
readltm();
} else {
dwait(D_CONTROL | D_SAY, __func__, "Starting long term memory file: %s", ltmnam);
ltm.gamecnt = ltm.gamesum = ltm.timeswritten = 0;
ltm.inittime = time(0);
}
/* unlock */
unlock_file(__func__, lock_fd);
uncritical();
}
/*
* readltm: Read in the long term memory file for this version of Rogue
* into storage. Be careful about serializing access to the file.
*/
static void
readltm(void)
{
char buf[BUFSIZ];
FILE *ltmfil;
if ((ltmfil = fopen(ltmnam, "r")) == NULL) {
nosave = true;
dwait(D_WARNING | D_SAY, __func__, "Could not read long term memory file: %s", ltmnam);
} else {
/* Read the ltm file header */
if (fgets(buf, BUFSIZ, ltmfil)) {
sscanf(buf, "Count %d, sum %d, start %d, saved %d", <m.gamecnt, <m.gamesum, <m.inittime, <m.timeswritten);
}
/* Read each monster line */
while (fgets(buf, BUFSIZ, ltmfil)) {
parsemonster(buf);
}
fclose(ltmfil);
}
}
/*
* parsemonster: parse one line from the ltm file.
*/
static void
parsemonster(char *monster)
{
char *attrs;
int m;
/* Separate the monster name from the attributes */
if ((attrs = index(monster, '|')) == NULL) {
return;
}
*attrs++ = '\0';
/* Find the monsters entry in long term memory */
m = addmonhist(monster);
/* Now parse the probabilities and statistics */
parseprob(attrs, &monhist[m].wehit);
SKIPTO('|', attrs);
parseprob(attrs, &monhist[m].theyhit);
SKIPTO('|', attrs);
parseprob(attrs, &monhist[m].arrowhit);
SKIPTO('|', attrs);
parsestat(attrs, &monhist[m].htokill);
SKIPTO('|', attrs);
parsestat(attrs, &monhist[m].damage);
SKIPTO('|', attrs);
parsestat(attrs, &monhist[m].atokill);
SKIPTO('|', attrs);
}
/*
* clearltm: Clear a whole long term memory array.
*/
static void
clearltm(ltmrec *ltmarr)
{
int i;
for (i = 0; i < MAXMON; i++) {
ltmarr[i].m_name[0] = '\0';
clearprob(<marr[i].wehit);
clearprob(<marr[i].theyhit);
clearprob(<marr[i].arrowhit);
clearstat(<marr[i].htokill);
clearstat(<marr[i].damage);
clearstat(<marr[i].atokill);
}
}
/*
* dumpmonstertable: Format and print the monster table on the screen
*/
void
dumpmonstertable(void)
{
int m;
char monc;
clear();
mvprintw(0, 0, "Monster table:");
analyzeltm();
for (m = 0, monc = 'A'; m < 26; m++, monc++) {
if (m < 13) {
at(m + 2, 0);
} else {
at(m - 11, 40);
}
printw("%c: %s", monc, monname(monc));
if (monhist[monindex[m + 1]].damage.count > 0) {
printw(" (%d,%d)", monatt[m].expdam, monatt[m].maxdam);
} else {
printw(" <%d>", monatt[m].maxdam);
}
if (monhist[monindex[m + 1]].atokill.count > 0) {
printw(" [%d]", monatt[m].mtokill);
}
}
pauserogue();
}
/*
* analyzeltm: Set the monatt array based on current long term memory.
*/
void
analyzeltm(void)
{
int m, i;
double avg_dam = 0.6 * Level + 3, max_dam = 7.0 + Level, avg_arr = 4.0;
double phit, mean_dam, stdev_dam, three_dev;
/* Loop through each monster in this game (not whole ltm file) */
for (i = 0; i < 26; i++) {
m = monindex[i + 1];
/* Calculate expected and maximum damage done by monster */
if (monhist[m].damage.count > 3) {
mean_dam = mean(&monhist[m].damage);
stdev_dam = stdev(&monhist[m].damage);
max_dam = monhist[m].damage.high;
avg_dam = mean_dam * prob(&monhist[m].theyhit);
three_dev = mean_dam + 3 * stdev_dam;
if (max_dam > three_dev && monhist[m].damage.count > 10) {
max_dam = mean_dam + stdev_dam;
monhist[m].damage.high = max_dam;
}
} else if (monhist[m].damage.high > 0.0) {
max_dam = monhist[m].damage.high;
}
/* Calculate average arrows fired to killed monster */
if (monhist[m].atokill.count > 2) {
phit = prob(&monhist[m].arrowhit);
phit = max(phit, 0.1);
avg_arr = mean(&monhist[m].atokill) / phit;
}
/* Now store the information in the monster tables */
monatt[i].expdam = ceil(avg_dam * 10);
monatt[i].maxdam = ceil(max_dam);
monatt[i].mtokill = ceil(avg_arr);
}
}