-
Notifications
You must be signed in to change notification settings - Fork 0
/
utl_util.c
378 lines (339 loc) · 8.07 KB
/
utl_util.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
369
370
371
372
373
374
375
376
377
378
/*
* udd utl_util.c file - generic util functions
*
* 03-Sep-89, Chuck Cranor
*
* V5.1C (see VERSION file for history)
*
*/
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
#include "defs.h"
int roll(a, b)
{
int tot = 0;
if (b < 1)
return(0);
while (a-- > 0)
tot += (random() % b) + 1;
return(tot);
}
void utl_inilvl()
{
int lcv1, lcv2;
int ptr = 0;
dlvl_loadum(u.c[18], u.c[15]);
for (lcv1 = 0 ; lcv1 < 22 ; lcv1++)
for (lcv2 = 0 ; lcv2 < 22 ; lcv2++)
u.l[lcv1][lcv2] = 0;
for (lcv1 = 1; lcv1 <= 20; lcv1++) {
u.l[lcv1][21] = 1;
u.l[21][lcv1] = 4;
}
u.l[21][21] = 0;
for (lcv1 = 1 ; lcv1 < 21 ; lcv1++)
for (lcv2 = 1 ; lcv2 < 21 ; lcv2++)
u.l[lcv1][lcv2] = dd.dmap[ptr++] + utl_populate();
}
int utl_populate()
{
if (rnd() > 0.4)
return(0);
if (rnd() <= 0.5) {
if (rnd() > 0.5)
return(256);
if (rnd() > 0.05)
return(768);
return(1792);
}
if (rnd() > 0.2)
return(512);
return(1536);
}
int utl_winner()
{
int fd;
if ((fd = open(FIL_ORB, O_CREAT|O_APPEND|O_RDWR, 0644)) < 0) {
printf("\r\nCan't open ORB file %s!\r\n", FIL_ORB);
perror("open");
printf("\rPlease report this error. In the meantime stay\r\n");
printf("mortal for a little while longer.\r\n");
return(NOPE);
}
write(fd, "\t", 1);
if (write(fd, u.n[0], strlen(u.n[0])) < 0) {
printf("Error writing to ORB file!\r\n");
perror("write");
printf("\rGet help...\r\n");
close(fd);
return(NOPE);
}
write(fd, "\r\n", 2);
close(fd);
/* try to remove him from the file */
chr_save(NOPE);
if (chr_load(u.n[0], NOLOCK) != NOPE)
chr_lck_nuke(NUKE);
return(YEP);
}
int utl_escape()
{
int i;
printf("\r\n\n\nYou made it out of the dungeon!\r\n");
printf("\n\n\n\n\n\n");
if (u.c[50] == 1) { /* winner??? */
printf("You have the orb with you\007!\007!\007!\007\r\n");
printf("You have been declared a national hero\007!\007!\r\n");
printf("The gods will now give you immortality!\r\n");
u.c[64] = DGN_NEWADV;
if (u.c[61] != 0 || utl_winner() == YEP)
return(YEP);
}
u.c[11] = u.c[10];
u.c[9] += u.c[21];
u.c[13] += u.c[12];
for (i = 31; i <= 36 ; i++)
u.c[i] = u.c[i - 6];
utl_chklvl();
printf("\n\r\n");
utl_status();
ask:
printf("\r\nPress <CR> to reenter the dungeon, <LF> to leave: ");
i = getchar();
printf("\r\n");
if (i < 0)
i = 10;
u.c[15] = u.c[16] = u.c[17] = 0;
u.c[64] = DGN_NEWADV;
if (i == 13)
return(NOPE);
if (i != 10) {
printf("Don't you listen?\r\n");
goto ask;
}
return(chr_save(YEP));
}
int utl_death()
{
int i;
while (u.c[7] == 1 && u.c[34] > 0) { /* can he save himself? */
printf("RAISE DEAD!!!!\r\n");
sleep(2);
u.c[34]--;
u.c[4]--;
if (u.c[4] == 0 || roll(1,10) > u.c[4])
printf("It failed!! [%ld spell%s left]\r\n", u.c[34],
(u.c[34] == 1) ? "" : "s");
else {
u.c[11] = roll(1,u.c[10]);
printf("You're alive with %ld hit points, and a constitution of %ld.\r\n",
u.c[11], u.c[4]);
u.c[64] = DGN_PROMPT;
return(NOPE);
}
}
printf("\r\n\n\nAnother%s mighty %s bites the dust.\r\n",
(u.c[8] < 4) ? " not so" : "", class[u.c[7]]);
printf("\n\n\n\n");
for (i = 0 ; i < 65 ; i++)
u.c[i] = 0;
u.c[57] = 1;
if (chr_save(YEP) == NOPE) {
printf("utl_death: Internal error! This shouldn't happen,\r\n");
printf(" but since you're dead it doesn't really matter!\r\n");
}
printf("Do you wish to (R)eturn to %s, (T)ry again, or (E)xit : ", ddd);
i = getchar();
if (islower(i))
i = toupper(i);
if (i == 'T') {
printf("Try again\r\n");
u.c[64] = SWB_CREATE;
return(YEP);
}
if (i == 'R') {
printf("Return to %s\r\n", ddd);
u.c[64] = XXX_NORM;
return(YEP);
}
printf("Exit\r\n");
printf("Fare well, brave sire.\r\n");
unix_exit(0);
return(0);
}
void utl_stat()
{
int i;
printf("%s\r\n", u.n[0]);
for (i = 0 ; i < 6 ; i++)
printf("%.3s %02ld\r\n", st + 3*i, u.c[i+1]);
}
void utl_status()
{
printf("\r\nLevel\t\t%ld\r\n", u.c[8]);
printf("Experience\t%ld\r\n", u.c[9]);
printf("Gold found\t%ld\r\n", u.c[12]);
printf("Hit points\t%ld\r\n\n", u.c[11]);
if (u.c[31]+u.c[32]+u.c[33]+u.c[34] != 0)
printf("Spells: %ld %ld %ld %ld\r\n", u.c[31], u.c[32], u.c[33], u.c[34]);
else
printf("Spells: <none>\r\n");
printf("\n");
}
void utl_eqp()
{
printf("\r\nEquipment:\r\n\n");
if (u.c[22] >= 0)
printf("%s%s +%ld\n\r", (u.c[22] > 0) ? "Magic " : "",
wep[u.c[7]], u.c[22]);
if (u.c[23] >= 0)
printf("%s%s Armor +%ld\n\r", (u.c[23] > 0) ? "Magic " : "",
arm[u.c[7]], u.c[23]);
if (u.c[24] >= 0)
printf("%sShield +%ld\n\r", (u.c[24] > 0) ? "Magic " : "", u.c[24]);
if (u.c[51] > 0)
printf("Ring of regeneration +%ld\r\n", u.c[51]);
if (u.c[52] > 0)
printf("Elven cloak +%ld\r\n", u.c[52]);
if (u.c[53] > 0)
printf("Elven boots +%ld\r\n", u.c[53]);
if (u.c[50] == 1)
printf("\r\n\007The ORB\r\n");
printf("\r\n");
}
void utl_dtrp()
{
if (u.c[41] > 0 && u.i[8] == 1 && rnd() > 0.05)
printf("You detect traps: ");
}
int utl_exp(lvl) /* find exp for a level */
int lvl;
{
double z, z1, z2;
int lcv;
if (lvl > 10) {
z2 = lvl - 10.0;
z2 = pow(z2, (double) 1.5);
z = utl_exp(10) + utl_exp(10) * z2;
return((int) z);
}
if (u.c[7] == 0)
z = 2000.0;
else if (u.c[7] == 2)
z = 2500.0;
else z = 1500.0;
for (lcv = 3 ; lcv <= lvl; lcv++)
z *= 2.0;
if (lvl == 1)
z = 0.0;
return((int) z);
}
int utl_chklvl()
{
int i1;
if (u.c[9] < utl_exp(u.c[8])) {
printf("You went down a level\007!\r\n");
i1 = roll(1, 4 + 2*(2 - u.c[7]));
if (u.c[4] > 14)
i1 = i1 - 14 + u.c[4];
printf("You lost %d hit point%s.\r\n", i1, (i1 == 1) ? "" : "s");
u.c[10] -= i1;
u.c[11] -= i1;
u.c[8] -= 1;
if (u.c[8] < 1 || u.c[11] < 1) {
printf("You died!\r\n");
return(utl_death());
}
utl_sprog();
if (chr_save(NOPE) != YEP)
printf("[Strange, I can't check point your character!]\n\r");
return(NOPE);
}
if (u.c[9] < utl_exp(u.c[8]+1))
return(NOPE);
printf("You went up a level\007!\r\n");
i1 = roll(1, 4 + 2*(2 - u.c[7]));
if (u.c[4] > 14)
i1 = i1 - 14 + u.c[4];
printf("You gain %d hit point%s.\r\n", i1, (i1 == 1) ? "" : "s");
u.c[10] += i1;
u.c[11] += i1;
u.c[8] += 1;
if (u.c[9] >= utl_exp(u.c[8] + 1))
u.c[9] = utl_exp(u.c[8] + 1) - 1;
utl_sprog();
if (chr_save(NOPE) != YEP)
printf("[Strange, I can't check point your character!]\n\r");
return(NOPE);
}
void utl_sprog()
{
int lcv, tmp;
if (u.c[7] == 0)
return;
for (lcv = 1 ; lcv < 5 ; lcv++) {
if (u.c[7] == 1)
tmp = u.c[8] - (lcv + 1.0) / 0.75 + 1;
else
tmp = u.c[8] - lcv / 0.8 + 1;
if (tmp < 0)
tmp = 0;
if (lcv == 1)
tmp += 2;
u.c[30+lcv] = u.c[30+lcv] + tmp - u.c[24+lcv];
u.c[24+lcv] = tmp;
}
}
void utl_prtspl(c7, lvl)
int c7, lvl;
{
int i = 0;
int mx = 6;
int lcv;
printf("\r\n");
if (c7 == 1)
mx = i = 4;
i += lvl;
for (lcv = 1 ; lcv <= mx ; lcv++)
printf("%d. %s\r\n", lcv, sp[i][lcv]);
}
void utl_adj_st()
{
int i1, i2, i3;
i1 = roll(1,2);
i2 = roll(1,6);
i3 = 1;
while (rnd() + u.c[15] * 0.02 > 0.9)
i3++;
if (i1 == 1)
while (u.c[i2] - i3 < 1)
i3--;
printf("Your %.3s just went %s by %d.\r\n", st + ((i2 - 1) * 3),
(i1 == 1) ? "down" : "up", i3);
if (i1 == 1)
i3 = -i3;
u.c[i2] += i3;
printf("It is now %ld.\r\n", u.c[i2]);
if (chr_save(NOPE) != YEP)
printf("[Strange, I can't check point your character!]\n\r");
}
int utl_adj_ex()
{
int i1, i2;
i1 = roll(1,2);
i2 = (rnd() * 500 + 2) * u.c[15];
printf("You just %s %d experience points.\r\n",
(i1 == 1) ? "lost" : "gained", i2);
if (i1 == 1)
i2 = -i2;
u.c[9] += i2;
if (chr_save(NOPE) != YEP)
printf("[Strange, I can't check point your character!]\n\r");
return(utl_chklvl());
}