-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmisc.c
675 lines (555 loc) · 15.3 KB
/
misc.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include <stdio.h>
#include "z-rand.h"
#include "h-define.h"
#include "h-type.h"
#include "defines.h"
#include "externs.h"
#include "wadstructs.h"
/*
* Global array for looping through the "keypad directions"
*/
s16b ddd[9] =
{ 2, 8, 6, 4, 3, 1, 9, 7, 5 };
/*
* Global arrays for converting "keypad direction" into offsets
*/
s16b ddx[10] =
{ 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 };
s16b ddy[10] =
{ 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 };
/*
* Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]"
*/
s16b ddx_ddd[9] =
{ 0, 0, 1, -1, 1, -1, 1, -1, 0 };
s16b ddy_ddd[9] =
{ 1, -1, 0, 0, 1, 1, -1, -1, 0 };
s16b cave_o_idx[DUNGEON_HGT][DUNGEON_WID];
byte cave_info[DUNGEON_HGT][256];
byte cave_feat[DUNGEON_HGT][DUNGEON_WID];
s16b cave_m_idx[DUNGEON_HGT][DUNGEON_WID];
s16b feeling; /* Most recent feeling */
s16b rating; /* Level's current rating */
int playerpos_x, playerpos_y;
void add_thing(Thing* t);
s16b player_place(int y, int x)
{
Thing t;
printf("player at %i %i\n", y, x);
t.x = x * 64 + 32;
t.y = y * 64 + 32;
t.angle = rand_int(360);
t.type = 1;
t.options = 0x7;
add_thing(&t);
}
void place_trap(int y, int x)
{
printf("trap at %i %i\n", y, x);
cave_set_feat(y, x, FEAT_TRAP_HEAD);
cave_sector_map[y][x]=++sector_counter;
}
#define CHAINSAW 0x07d5
#define SHOTGUN 0x07d1
#define DBL_SHOTGUN 0x0052
#define CHAINGUN 0x07d2
#define ROCKET_LAUNCHER 0x07d3
#define PLASMA_GUN 0x07d4
#define BFG9000 0x07d6
#define AMMO_CLIP 0x07d7
#define SHOTGUN_SHELLS 0x07d8
#define ONE_ROCKET 0x07da
#define SMALL_CELLS 0x07ff
#define AMMO_BOX 0x0800
#define SHELLS_BOX 0x0801
#define ROCKETS_BOX 0x07fe
#define CELL_BOX 0x0011
#define BACKPACK 0x0008
#define STIMPACK 0x07db
#define MEDIKIT 0x07dc
#define HEALTH_POTION 0x07de
#define SPIRIT_ARMOR 0x07df
#define GREEN_ARMOR 0x07e2
#define BLUE_ARMOR 0x07e3
#define MEGASPHERE 0x0053
#define SOULSPHERE 0x07dd
#define INVULNERABILITY 0x07e6
#define BERZERK_PACK 0x07e7
#define INVISIBILITY 0x07e8
#define RADIATION_SUIT 0x07e9
#define COMPUTER_MAP 0x07ea
#define LIGHT_AMP 0x07fd
void place_object(int y, int x, bool good, bool great)
{
Thing t;
short int fun_items[13] = {CHAINSAW, SHOTGUN, DBL_SHOTGUN, CHAINGUN, ROCKET_LAUNCHER,
AMMO_BOX, SHELLS_BOX, ROCKETS_BOX, BACKPACK, MEDIKIT,
GREEN_ARMOR, BLUE_ARMOR, STIMPACK};
t.x = x * 64 + 32;
t.y = y * 64 + 32;
t.angle = rand_int(360);
t.type = fun_items[rand_int(13)];
printf("object of type %i at (%i, %i)\n", t.type, x, y);
t.options = 0x7;
add_thing(&t);
}
void place_gold(int y, int x)
{
printf("money at %i %i\n", y, x);
}
/* monster thing types */
#define FORMER_HUMAN 0x0bbc
#define WOLFENSTEIN_SS 0x0054
#define SERGEANT 0x0009
#define HEAVY_WEP_DUDE 0x0041
#define IMP 0x0bb9
#define DEMON 0x0bba
#define SPECTRE 0x003a
#define LOST_SOUL 0x0bbe
#define CACODEMON 0x0bbd
#define HELL_KNIGHT 0x0045
#define BARON_OF_HELL 0x0bbb
#define ARACHNOTRON 0x0044
#define PAIN_ELEMENTAL 0x0047
#define REVENANT 0x0042
#define MANCUBUS 0x0043
#define ARCH_VILE 0x0040
#define SPIDER_BOSS 0x0007
#define CYBER_DEMON 0x0010
bool place_monster(int y, int x, bool slp, bool grp)
{
Thing t;
int fun_monsters[11] = {FORMER_HUMAN,
SERGEANT,
IMP,
DEMON,
SPECTRE,
LOST_SOUL,
CACODEMON,
HELL_KNIGHT,
BARON_OF_HELL,
REVENANT,
PAIN_ELEMENTAL};
printf("monster at %i %i\n", y, x);
t.x = x * 64 + 32;
t.y = y * 64 + 32;
t.angle = rand_int(360)-1;
t.type = fun_monsters[rand_int(11)];
t.options = 0x7;
cave_m_idx[y][x]=t.type + 1;
add_thing(&t);
}
bool place_monster_aux(int y, int x, int r_idx, bool slp, bool grp)
{
printf("monster type %i at %i %i\n", r_idx, y, x);
}
/*
* Change the "feat" flag for a grid, and notice/redraw the grid
*/
void cave_set_feat(int y, int x, int feat)
{
/* Change the feature */
cave_feat[y][x] = feat;
/* Handle "wall/door" grids */
if (feat >= FEAT_DOOR_HEAD)
{
cave_info[y][x] |= (CAVE_WALL);
}
/* Handle "floor"/etc grids */
else
{
cave_info[y][x] &= ~(CAVE_WALL);
}
}
/*
* Approximate Distance between two points.
*
* When either the X or Y component dwarfs the other component,
* this function is almost perfect, and otherwise, it tends to
* over-estimate about one grid per fifteen grids of distance.
*
* Algorithm: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2
*/
sint distance(int y1, int x1, int y2, int x2)
{
int ay, ax;
/* Find the absolute y/x distance components */
ay = (y1 > y2) ? (y1 - y2) : (y2 - y1);
ax = (x1 > x2) ? (x1 - x2) : (x2 - x1);
/* Hack -- approximate the distance */
return ((ay > ax) ? (ay + (ax>>1)) : (ax + (ay>>1)));
}
/*
* Standard "find me a location" function
*
* Obtains a legal location within the given distance of the initial
* location, and with "los()" from the source to destination location.
*
* This function is often called from inside a loop which searches for
* locations while increasing the "d" distance.
*
* Currently the "m" parameter is unused.
*/
void scatter(int *yp, int *xp, int y, int x, int d, int m)
{
int nx, ny;
/* Unused */
m = m;
/* Pick a location */
while (TRUE)
{
/* Pick a new location */
ny = rand_spread(y, d);
nx = rand_spread(x, d);
/* Ignore annoying locations */
if (!in_bounds_fully(y, x)) continue;
/* Ignore "excessively distant" locations */
if ((d > 1) && (distance(y, x, ny, nx) > d)) continue;
/* Require "line of sight" */
if (los(y, x, ny, nx)) break;
}
/* Save the location */
(*yp) = ny;
(*xp) = nx;
}
/*
* A simple, fast, integer-based line-of-sight algorithm. By Joseph Hall,
* 4116 Brewster Drive, Raleigh NC 27606. Email to [email protected].
*
* This function returns TRUE if a "line of sight" can be traced from the
* center of the grid (x1,y1) to the center of the grid (x2,y2), with all
* of the grids along this path (except for the endpoints) being non-wall
* grids. Actually, the "chess knight move" situation is handled by some
* special case code which allows the grid diagonally next to the player
* to be obstructed, because this yields better gameplay semantics. This
* algorithm is totally reflexive, except for "knight move" situations.
*
* Because this function uses (short) ints for all calculations, overflow
* may occur if dx and dy exceed 90.
*
* Once all the degenerate cases are eliminated, we determine the "slope"
* ("m"), and we use special "fixed point" mathematics in which we use a
* special "fractional component" for one of the two location components
* ("qy" or "qx"), which, along with the slope itself, are "scaled" by a
* scale factor equal to "abs(dy*dx*2)" to keep the math simple. Then we
* simply travel from start to finish along the longer axis, starting at
* the border between the first and second tiles (where the y offset is
* thus half the slope), using slope and the fractional component to see
* when motion along the shorter axis is necessary. Since we assume that
* vision is not blocked by "brushing" the corner of any grid, we must do
* some special checks to avoid testing grids which are "brushed" but not
* actually "entered".
*
* Angband three different "line of sight" type concepts, including this
* function (which is used almost nowhere), the "project()" method (which
* is used for determining the paths of projectables and spells and such),
* and the "update_view()" concept (which is used to determine which grids
* are "viewable" by the player, which is used for many things, such as
* determining which grids are illuminated by the player's torch, and which
* grids and monsters can be "seen" by the player, etc).
*/
bool los(int y1, int x1, int y2, int x2)
{
/* Delta */
int dx, dy;
/* Absolute */
int ax, ay;
/* Signs */
int sx, sy;
/* Fractions */
int qx, qy;
/* Scanners */
int tx, ty;
/* Scale factors */
int f1, f2;
/* Slope, or 1/Slope, of LOS */
int m;
/* Extract the offset */
dy = y2 - y1;
dx = x2 - x1;
/* Extract the absolute offset */
ay = ABS(dy);
ax = ABS(dx);
/* Handle adjacent (or identical) grids */
if ((ax < 2) && (ay < 2)) return (TRUE);
/* Directly South/North */
if (!dx)
{
/* South -- check for walls */
if (dy > 0)
{
for (ty = y1 + 1; ty < y2; ty++)
{
if (!cave_floor_bold(ty, x1)) return (FALSE);
}
}
/* North -- check for walls */
else
{
for (ty = y1 - 1; ty > y2; ty--)
{
if (!cave_floor_bold(ty, x1)) return (FALSE);
}
}
/* Assume los */
return (TRUE);
}
/* Directly East/West */
if (!dy)
{
/* East -- check for walls */
if (dx > 0)
{
for (tx = x1 + 1; tx < x2; tx++)
{
if (!cave_floor_bold(y1, tx)) return (FALSE);
}
}
/* West -- check for walls */
else
{
for (tx = x1 - 1; tx > x2; tx--)
{
if (!cave_floor_bold(y1, tx)) return (FALSE);
}
}
/* Assume los */
return (TRUE);
}
/* Extract some signs */
sx = (dx < 0) ? -1 : 1;
sy = (dy < 0) ? -1 : 1;
/* Vertical "knights" */
if (ax == 1)
{
if (ay == 2)
{
if (cave_floor_bold(y1 + sy, x1)) return (TRUE);
}
}
/* Horizontal "knights" */
else if (ay == 1)
{
if (ax == 2)
{
if (cave_floor_bold(y1, x1 + sx)) return (TRUE);
}
}
/* Calculate scale factor div 2 */
f2 = (ax * ay);
/* Calculate scale factor */
f1 = f2 << 1;
/* Travel horizontally */
if (ax >= ay)
{
/* Let m = dy / dx * 2 * (dy * dx) = 2 * dy * dy */
qy = ay * ay;
m = qy << 1;
tx = x1 + sx;
/* Consider the special case where slope == 1. */
if (qy == f2)
{
ty = y1 + sy;
qy -= f1;
}
else
{
ty = y1;
}
/* Note (below) the case (qy == f2), where */
/* the LOS exactly meets the corner of a tile. */
while (x2 - tx)
{
if (!cave_floor_bold(ty, tx)) return (FALSE);
qy += m;
if (qy < f2)
{
tx += sx;
}
else if (qy > f2)
{
ty += sy;
if (!cave_floor_bold(ty, tx)) return (FALSE);
qy -= f1;
tx += sx;
}
else
{
ty += sy;
qy -= f1;
tx += sx;
}
}
}
/* Travel vertically */
else
{
/* Let m = dx / dy * 2 * (dx * dy) = 2 * dx * dx */
qx = ax * ax;
m = qx << 1;
ty = y1 + sy;
if (qx == f2)
{
tx = x1 + sx;
qx -= f1;
}
else
{
tx = x1;
}
/* Note (below) the case (qx == f2), where */
/* the LOS exactly meets the corner of a tile. */
while (y2 - ty)
{
if (!cave_floor_bold(ty, tx)) return (FALSE);
qx += m;
if (qx < f2)
{
ty += sy;
}
else if (qx > f2)
{
tx += sx;
if (!cave_floor_bold(ty, tx)) return (FALSE);
qx -= f1;
ty += sy;
}
else
{
tx += sx;
qx -= f1;
ty += sy;
}
}
}
/* Assume los */
return (TRUE);
}
s16b get_mon_num(int level)
{
return rand_int(10);
}
/*
* Attempt to allocate a random monster in the dungeon.
*
* Place the monster at least "dis" distance from the player.
*
* Use "slp" to choose the initial "sleep" status
*
* Use "monster_level" for the monster level
*/
bool alloc_monster(int dis, int slp)
{
int y, x;
/* Find a legal, distant, unoccupied, space */
while (1)
{
/* Pick a location */
y = rand_int(DUNGEON_HGT);
x = rand_int(DUNGEON_WID);
/* Require "naked" floor grid */
if (!cave_naked_bold(y, x)) continue;
else break;
}
/* Attempt to place the monster, allow groups */
if (place_monster(y, x, slp, TRUE)) return (TRUE);
/* Nope */
return (FALSE);
}
/*
* The number of entries in the "Rand_normal_table"
*/
#define RANDNOR_NUM 256
/*
* The standard deviation of the "Rand_normal_table"
*/
#define RANDNOR_STD 64
/*
* The normal distribution table for the "Rand_normal()" function (below)
*/
static s16b Rand_normal_table[RANDNOR_NUM] =
{
206, 613, 1022, 1430, 1838, 2245, 2652, 3058,
3463, 3867, 4271, 4673, 5075, 5475, 5874, 6271,
6667, 7061, 7454, 7845, 8234, 8621, 9006, 9389,
9770, 10148, 10524, 10898, 11269, 11638, 12004, 12367,
12727, 13085, 13440, 13792, 14140, 14486, 14828, 15168,
15504, 15836, 16166, 16492, 16814, 17133, 17449, 17761,
18069, 18374, 18675, 18972, 19266, 19556, 19842, 20124,
20403, 20678, 20949, 21216, 21479, 21738, 21994, 22245,
22493, 22737, 22977, 23213, 23446, 23674, 23899, 24120,
24336, 24550, 24759, 24965, 25166, 25365, 25559, 25750,
25937, 26120, 26300, 26476, 26649, 26818, 26983, 27146,
27304, 27460, 27612, 27760, 27906, 28048, 28187, 28323,
28455, 28585, 28711, 28835, 28955, 29073, 29188, 29299,
29409, 29515, 29619, 29720, 29818, 29914, 30007, 30098,
30186, 30272, 30356, 30437, 30516, 30593, 30668, 30740,
30810, 30879, 30945, 31010, 31072, 31133, 31192, 31249,
31304, 31358, 31410, 31460, 31509, 31556, 31601, 31646,
31688, 31730, 31770, 31808, 31846, 31882, 31917, 31950,
31983, 32014, 32044, 32074, 32102, 32129, 32155, 32180,
32205, 32228, 32251, 32273, 32294, 32314, 32333, 32352,
32370, 32387, 32404, 32420, 32435, 32450, 32464, 32477,
32490, 32503, 32515, 32526, 32537, 32548, 32558, 32568,
32577, 32586, 32595, 32603, 32611, 32618, 32625, 32632,
32639, 32645, 32651, 32657, 32662, 32667, 32672, 32677,
32682, 32686, 32690, 32694, 32698, 32702, 32705, 32708,
32711, 32714, 32717, 32720, 32722, 32725, 32727, 32729,
32731, 32733, 32735, 32737, 32739, 32740, 32742, 32743,
32745, 32746, 32747, 32748, 32749, 32750, 32751, 32752,
32753, 32754, 32755, 32756, 32757, 32757, 32758, 32758,
32759, 32760, 32760, 32761, 32761, 32761, 32762, 32762,
32763, 32763, 32763, 32764, 32764, 32764, 32764, 32765,
32765, 32765, 32765, 32766, 32766, 32766, 32766, 32767,
};
/*
* Generate a random integer number of NORMAL distribution
*
* The table above is used to generate a psuedo-normal distribution,
* in a manner which is much faster than calling a transcendental
* function to calculate a true normal distribution.
*
* Basically, entry 64*N in the table above represents the number of
* times out of 32767 that a random variable with normal distribution
* will fall within N standard deviations of the mean. That is, about
* 68 percent of the time for N=1 and 95 percent of the time for N=2.
*
* The table above contains a "faked" final entry which allows us to
* pretend that all values in a normal distribution are strictly less
* than four standard deviations away from the mean. This results in
* "conservative" distribution of approximately 1/32768 values.
*
* Note that the binary search takes up to 16 quick iterations.
*/
s16b Rand_normal(int mean, int stand)
{
s16b tmp;
s16b offset;
s16b low = 0;
s16b high = RANDNOR_NUM;
/* Paranoia */
if (stand < 1) return (mean);
/* Roll for probability */
tmp = rand_int(32768);
/* Binary Search */
while (low < high)
{
int mid = (low + high) >> 1;
/* Move right if forced */
if (Rand_normal_table[mid] < tmp)
{
low = mid + 1;
}
/* Move left otherwise */
else
{
high = mid;
}
}
/* Convert the index into an offset */
offset = (long)stand * (long)low / RANDNOR_STD;
/* One half should be negative */
if (rand_int(100) < 50) return (mean - offset);
/* One half should be positive */
return (mean + offset);
}