-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltm.c
306 lines (247 loc) · 8.07 KB
/
ltm.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
/*
* ltm.c: Rog-O-Matic XIV (CMU) Fri Dec 28 20:37:04 1984 - mlm
* Copyright (C) 1985 by A. Appel, G. Jacobson, L. Hamey, and M. Mauldin
*
* This file contains functions for maintaining a database or "long
* term memory"
*/
# include <curses.h>
# include <math.h>
# include "types.h"
# include "globals.h"
# include "install.h"
static int nosave = 0; /* True ==> dont write ltm back out */
static char ltmnam[100]; /* Long term memory file name */
/*
* mapcharacter: Read a character help message
*/
mapcharacter (ch, str)
char ch, *str;
{
dwait (D_CONTROL, "mapcharacter called: '%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 (monster)
char *monster;
{ register 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, "Overflowed monster array");
strcpy (monhist[nextmon].m_name, monster); /* 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 (monster)
char *monster;
{ register 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.
*/
saveltm (score)
int score;
{ register int m;
register FILE *ltmfil;
if (nextmon < 1 || nosave) return;
dwait (D_CONTROL, "Saveltm called, writing file '%s'", ltmnam);
/* Disable interrupts and open the file for writing */
critical ();
/* Only write out the new results if we can get write access */
if (lock_file (LOCKFILE, MAXLOCK))
{ if ((ltmfil = wopen (ltmnam, "w")) == NULL)
{ dwait (D_WARNING, "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_file (LOCKFILE);
}
/* Re-enable interrupts */
uncritical ();
}
/*
* restoreltm: Read the long term memory file.
*/
restoreltm ()
{
sprintf (ltmnam, "%s/ltm%d", RGMDIR, version);
dwait (D_CONTROL, "Restoreltm called, 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 ();
/* Only read the long term memory if we can get access */
if (lock_file (LOCKFILE, MAXLOCK))
{ if (fexists (ltmnam))
readltm ();
else
{ dwait (D_CONTROL | D_SAY,
"Starting long term memory file '%s'...", ltmnam);
ltm.gamecnt = ltm.gamesum = ltm.timeswritten = 0;
ltm.inittime = time (0);
}
unlock_file (LOCKFILE);
}
else
{ saynow ("Warning: could not lock long term memory file!");
nosave = 1;
}
uncritical ();
}
/*
* readltm: Read in the long term memory file for this version of Rogue
* into storage. Be careful about serializing access to the file.
*/
readltm ()
{ char buf[BUFSIZ];
register FILE *ltmfil;
if ((ltmfil = fopen (ltmnam, "r")) == NULL)
{ nosave = 1;
dwait (D_WARNING | D_SAY,
"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.
*/
parsemonster (monster)
char *monster;
{ register char *attrs;
char *index();
register 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.
*/
clearltm (ltm)
register ltmrec *ltm;
{ register int i;
for (i=0; i<MAXMON; i++)
{ ltm[i].m_name[0] = '\0';
clearprob (<m[i].wehit);
clearprob (<m[i].theyhit);
clearprob (<m[i].arrowhit);
clearstat (<m[i].htokill);
clearstat (<m[i].damage);
clearstat (<m[i].atokill);
}
}
/*
* dumpmonstertable: Format and print the monster table on the screen
*/
dumpmonstertable ()
{ register 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.
*/
analyzeltm ()
{ register 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);
}
}