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
|
/*-
* Copyright (c) 2002 by Edwin de Jong <mauddib@gmx.net>.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
*/
#define NONE 0x0000
#define LT 0x1000
#define RT 0x0001
#define RB 0x0010
#define LB 0x0100
#define ALL 0x1111
#define BLOCK_EMPTY ' '
#define BLOCK_DOT_1 '`'
#define BLOCK_DOT_2 '.'
#define BLOCK_WALL '#'
#define BLOCK_GHOST_ONLY '='
#define BLOCK_WALL_TL '\''
#define BLOCK_WALL_TR '`'
#define BLOCK_WALL_BR ','
#define BLOCK_WALL_BL '_'
#define BLOCK_WALL_HO '-'
#define BLOCK_WALL_VE '|'
/* This is more or less the standard pacman level (without the left-right
tunnel. */
static const lev_t stdlevel = {
"########################################",
"########################################",
"#######````````````##````````````#######",
"#######`####`#####`##`#####`####`#######",
"#######`####`#####`##`#####`####`#######",
"#######`####`#####`##`#####`####`#######",
"#######``````````````````````````#######",
"#######`####`##`########`##`####`#######",
"#######`####`##`########`##`####`#######",
"#######``````##````##````##``````#######",
"############`#####`##`#####`############",
"############`#####`##`#####`############",
"############`##``````````##`############",
"############`##`###==###`##`############",
"############`##`########`##`############",
"############````########````############",
"############`##`########`##`############",
"############`##`########`##`############",
"############`##``````````##`############",
"############`##`########`##`############",
"############`##`########`##`############",
"#######````````````##````````````#######",
"#######`####`#####`##`#####`####`#######",
"#######`####`#####`##`#####`####`#######",
"#######```##````````````````##```#######",
"#########`##`##`########`##`##`#########",
"#########`##`##`########`##`##`#########",
"#######``````##````##````##``````#######",
"#######`##########`##`##########`#######",
"#######`##########`##`##########`#######",
"#######``````````````````````````#######",
"########################################"};
#define TILEWIDTH 5U
#define TILEHEIGHT 5U
#define TILES_COUNT 11U
#define GO_UP 0x0001U
#define GO_LEFT 0x0002U
#define GO_RIGHT 0x0004U
#define GO_DOWN 0x0008U
/* This are tiles which can be places to create a level. */
static struct {
char block[TILEWIDTH * TILEHEIGHT + 1];
unsigned dirvec[4];
unsigned ndirs;
unsigned simular_to;
} tiles[TILES_COUNT] = {
/*
* ' ' == dont care == BLOCK_EMPTY
* '#' == set wall, and not clear == BLOCK_WALL
* '`' == clear == BLOCK_DOT_1
* middle position is always set as cleardef
*/
{ " # "
" # "
" ``` "
" # "
" # ",
{ GO_LEFT, GO_RIGHT, 0, 0 }, 2,
(unsigned) (1 << 0 | 1 << 6 | 1 << 8 | 1 << 10) },
{ " "
" ` "
"##`##"
" ` "
" ",
{ GO_UP, GO_DOWN, 0, 0 }, 2,
(unsigned) (1 << 1 | 1 << 7 | 1 << 9 | 1 << 10) },
{ " ##"
"##`##"
"##`` "
"#### "
"#### ",
{ GO_UP, GO_RIGHT, 0, 0 }, 2,
(unsigned) (1 << 2 | 1 << 6 | 1 << 7 | 1 << 10) },
{ "#### "
"#### "
"##`` "
"##`##"
" ##",
{ GO_RIGHT, GO_DOWN, 0, 0 }, 2,
(unsigned) (1 << 3 | 1 << 7 | 1 << 8 | 1 << 10) },
{ " ###"
" ###"
" ``##"
"##` "
"## ",
{ GO_LEFT, GO_DOWN, 0, 0 }, 2,
(unsigned) (1 << 4 | 1 << 8 | 1 << 9 | 1 << 10) },
{ "## "
"##`##"
" ``##"
" ####"
" ####",
{ GO_LEFT, GO_UP, 0, 0 }, 2,
(unsigned) (1 << 5 | 1 << 6 | 1 << 9 | 1 << 10) },
{ "##`##"
"##`##"
"`````"
" ### "
" ### ",
{ GO_LEFT, GO_UP, GO_RIGHT, 0 }, 3,
(unsigned) 1 << 6 },
{ " `##"
"##`##"
"##```"
"##`##"
" `##",
{ GO_UP, GO_RIGHT, GO_DOWN, 0}, 3,
(unsigned) (1 << 7) },
{ " ### "
" ### "
"`````"
"##`##"
"##`##",
{ GO_LEFT, GO_RIGHT, GO_DOWN, 0}, 3,
(unsigned) (1 << 8) },
{ "##` "
"##`##"
"```##"
"##`##"
"##` ",
{ GO_UP, GO_DOWN, GO_LEFT, 0 }, 3,
(unsigned) (1 << 9) },
{ "##`##"
"##`##"
"`````"
"##`##"
"##`##",
{ GO_UP, GO_DOWN, GO_LEFT, GO_RIGHT }, 4,
(unsigned) (1 << 10) }
};
/* probability array for each of the tiles */
#define MAXTILEPROB 22
static const unsigned tileprob[MAXTILEPROB] =
{ 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 10 };
/* Sets a block in the level to a certain state. */
static void
setblockto(lev_t *level, const unsigned x, const unsigned y,
const char c)
{
if (!(x < LEVWIDTH && y < LEVHEIGHT)) return;
(*level)[y][x] = c;
}
/* check if a block is set */
static int
checkset(lev_t *level, const unsigned x, const unsigned y)
{
if (!(x < LEVWIDTH && y < LEVHEIGHT) ||
(*level)[y][x] == BLOCK_WALL ||
(*level)[y][x] == BLOCK_GHOST_ONLY)
return True;
return False;
}
/* Check if a block is not set */
static int
checksetout(lev_t *level, const unsigned x, const unsigned y)
{
if (!(x < LEVWIDTH && y < LEVHEIGHT) ||
checkset(level, x, y) != 0)
return True;
return False;
}
/* Check if a block cannot be set */
static int
checkunsetdef(lev_t *level, const unsigned x, const unsigned y)
{
if (!(x < LEVWIDTH && y < LEVHEIGHT))
return False;
if ((*level)[y][x] == BLOCK_DOT_1) return True;
return False;
}
/* Initializes a level to empty state. */
static void
clearlevel(lev_t *level)
{
unsigned x, y;
for (y = 0; y < LEVHEIGHT ; y++)
for (x = 0 ; x < LEVWIDTH ; x++)
(*level)[y][x] = BLOCK_EMPTY;
}
/* Changes a level from the level creation structure ((array to array) to
array. */
static void
copylevel(char *dest, lev_t *level)
{
unsigned x, y;
for (y = 0; y < LEVHEIGHT ; y++)
for (x = 0; x < LEVWIDTH ; x++)
dest[y * LEVWIDTH + x] = (*level)[y][x];
}
/* Creates a jail to work around, so we can finish it later. */
static void
createjail(lev_t *level, const unsigned width,
const unsigned height)
{
unsigned x, y, xstart, xend, ystart, yend;
if (LEVWIDTH < width || LEVHEIGHT < height) return;
xstart = LEVWIDTH/2 - width/2;
xend = LEVWIDTH/2 + width/2;
ystart = LEVHEIGHT/2 - height/2;
yend = LEVHEIGHT/2 + height/2;
for (y = ystart - 1; y < yend + 1; y++)
for (x = xstart - 1; x < xend + 1; x++)
setblockto(level, x, y, BLOCK_DOT_1);
for (y = ystart; y < yend; y++)
for (x = xstart; x < xend; x++)
setblockto(level, x, y, BLOCK_WALL);
}
/* Finishes a jail so it is empty and the ghostpass is on top. */
static void
finishjail(lev_t *level, const unsigned width,
const unsigned height)
{
unsigned x, y, xstart, xend, ystart, yend;
xstart = LEVWIDTH/2 - width/2;
xend = LEVWIDTH/2 + width/2;
ystart = LEVHEIGHT/2 - height/2;
yend = LEVHEIGHT/2 + height/2;
for (y = ystart + 1; y < yend - 1 ; y++)
for (x = xstart + 1; x < xend - 1; x++)
setblockto(level, x, y, BLOCK_EMPTY);
for (x = xstart - 1; x < xend + 1; x++) {
setblockto(level, x, ystart - 1, BLOCK_EMPTY);
setblockto(level, x, yend, BLOCK_EMPTY);
}
for (y = ystart - 1; y < yend + 1; y++) {
setblockto(level, xstart - 1, y, BLOCK_EMPTY);
setblockto(level, xend, y, BLOCK_EMPTY);
}
setblockto(level, xstart + width/2 - 1, ystart, BLOCK_GHOST_ONLY);
setblockto(level, xstart + width/2, ystart, BLOCK_GHOST_ONLY);
}
/* Tries to set a block at a certain position. Returns true if possible,
and leaves level in new state (plus block), or False if not possible,
and leaves level in unpredictable state. */
static int
tryset(lev_t *level, const unsigned xpos, const unsigned ypos,
const char *block)
{
register unsigned x, y;
register char locchar;
int xstart, ystart;
unsigned xend, yend;
if ((*level)[ypos][xpos] == BLOCK_DOT_1) return False;
xstart = xpos - 2;
ystart = ypos - 2;
for (y = 0 ; y < TILEHEIGHT ; y++)
for (x = 0 ; x < TILEWIDTH ; x++) {
locchar = block[y * TILEWIDTH + x];
if (locchar == BLOCK_EMPTY)
continue;
if (locchar == BLOCK_DOT_1 &&
(xstart + x < 1 ||
xstart + x >= LEVWIDTH - 1 ||
ystart + y < 1 ||
ystart + y >= LEVHEIGHT - 1 ||
checkset(level, xstart + x, ystart + y) != 0))
return False;
else if (locchar == BLOCK_WALL &&
(xstart + x > 1 &&
xstart + x < LEVWIDTH &&
ystart + y > 1 &&
ystart + y < LEVHEIGHT - 1) &&
checkunsetdef(level,
(unsigned)(xstart + x),
(unsigned)(ystart + y)) != 0)
return False;
}
/* and set the block in place */
xend = (xstart + TILEWIDTH < LEVWIDTH - 1) ?
TILEWIDTH : LEVWIDTH - xstart - 2;
yend = (ystart + TILEHEIGHT < LEVHEIGHT - 1) ?
TILEHEIGHT : LEVHEIGHT - ystart - 2;
for (y = (ystart < 1) ? (unsigned)(1 - ystart) : 0U ;
y < yend ; y++)
for (x = (xstart < 1) ?
(unsigned)(1 - xstart) : 0U ;
x < xend ; x++) {
locchar = block[y * TILEWIDTH + x];
if ((locchar == BLOCK_WALL) &&
((*level)[ystart + y][xstart + x] == BLOCK_EMPTY)) {
(*level)[ystart + y][xstart + x] = BLOCK_WALL;
(*level)[ystart + y]
[LEVWIDTH - (xstart + x + 1)] =
BLOCK_WALL;
}
}
(*level)[ypos][xpos] = BLOCK_DOT_1;
(*level)[ypos][LEVWIDTH-xpos-1] = BLOCK_DOT_1;
return True;
}
/* Tries certain combinations of blocks in the level recursively. */
static unsigned
nextstep(lev_t *level, const unsigned x, const unsigned y,
unsigned dirvec[], unsigned ndirs)
{
unsigned dirpos, curdir, inc = 0;
int ret = 0;
while (ndirs > 0) {
ndirs--;
if (ndirs == 0) {
curdir = dirvec[0];
}
else {
dirpos = NRAND(ndirs);
curdir = dirvec[dirpos];
/* nope, no bufoverflow, but ndirs - 1 + 1 */
dirvec[dirpos] = dirvec[ndirs];
dirvec[ndirs] = curdir;
}
switch (curdir) {
case GO_UP:
if (y < 1 ||
(ret = creatlevelblock(level, x, y - 1))
== 0)
return 0;
break;
case GO_RIGHT:
if (x > LEVWIDTH - 2 ||
(ret = creatlevelblock(level, x + 1, y))
== 0)
return 0;
break;
case GO_DOWN:
if (y > LEVHEIGHT - 2 ||
(ret = creatlevelblock(level, x, y + 1))
== 0)
return 0;
break;
case GO_LEFT:
if (x < 1 ||
(ret = creatlevelblock(level, x - 1, y))
== 0)
return 0;
}
if (ret != -1)
inc += (unsigned)ret;
}
if (inc == 0) inc = 1;
return inc;
}
static int
creatlevelblock(lev_t *level, const unsigned x, const unsigned y)
{
unsigned tried = GETNB(TILES_COUNT);
unsigned tilenr;
unsigned ret;
lev_t savedlev;
if (!((x < LEVWIDTH) && (y < LEVHEIGHT)))
return 0;
if (checkunsetdef(level, x, y) != 0)
return -1;
if (x == 0)
tried &= ~(1<<0);
else if (x == 1)
tried &= ~(1<<4 | 1<<5 | 1<<6 | 1<<8 | 1<<9 | 1<<10);
else if (x == LEVWIDTH-1)
tried &= ~(1<<0);
else if (x == LEVWIDTH-2)
tried &= ~(1<<2 | 1<<3 | 1<<6 | 1<<7 | 1<<8 | 1<<10);
if (y == 1)
tried &= ~(1<<2 | 1<<5 | 1<<6 | 1<<7 | 1<<9 | 1<<10);
else if (y == 0)
tried &= ~(1<<1);
else if (y == LEVHEIGHT-1)
tried &= ~(1<<1);
else if (y == LEVHEIGHT-2)
tried &= ~(1<<3 | 1<<4 | 1<<7 | 1<<8 | 1<<9 | 1<<10);
/* make a copy of the current level, so we can go back on the stack */
(void) memcpy(&savedlev, level, sizeof(lev_t));
/* while there are still some blocks left to try */
while (tried != 0x00) {
tilenr = tileprob[NRAND(MAXTILEPROB)];
if (!TESTNB(tried, tilenr))
continue;
if (tryset(level, x, y, tiles[tilenr].block) != 0) {
if ((ret = nextstep(level, x, y, tiles[tilenr].dirvec,
tiles[tilenr].ndirs)) != 0) {
return ret + 1;
}
(void) memcpy(level, &savedlev, sizeof(lev_t));
}
tried &= ~(tiles[tilenr].simular_to);
}
return 0;
}
/* Fills up all empty space so there is wall everywhere. */
static void
filllevel(lev_t *level)
{
unsigned x, y;
for (y = 0; y < LEVHEIGHT; y++)
for (x = 0; x < LEVWIDTH; x++)
if ((*level)[y][x] == BLOCK_EMPTY)
(*level)[y][x] = BLOCK_WALL;
}
/* Changes a level from a simple wall/nowall to a wall with rounded corners
and such. Stupid algorithm, could be done better! */
static void
frmtlevel(lev_t *level)
{
lev_t frmtlev;
register unsigned x, y;
register unsigned poscond;
register unsigned poscond2;
clearlevel(&frmtlev);
for (y = 0; y < LEVHEIGHT; y++)
for (x = 0; x < LEVWIDTH; x++) {
if (checkset(level, x, y) == 0) {
frmtlev[y][x] = BLOCK_DOT_2;
continue;
}
if ((*level)[y][x] == BLOCK_GHOST_ONLY) {
frmtlev[y][x] = BLOCK_GHOST_ONLY;
continue;
}
poscond =
(checksetout(level, x - 1, y - 1) != 0 ?
0x01U : 0U) |
(checksetout(level, x + 1, y - 1) != 0 ?
0x02U : 0U) |
(checksetout(level, x + 1, y + 1) != 0 ?
0x04U : 0U) |
(checksetout(level, x - 1, y + 1) != 0 ?
0x08U : 0U);
poscond2 =
(checksetout(level, x - 1, y) != 0 ?
0x01U : 0) |
(checksetout(level, x, y - 1) != 0 ?
0x02U : 0) |
(checksetout(level, x + 1, y) != 0 ?
0x04U : 0) |
(checksetout(level, x, y + 1) != 0 ?
0x08U : 0);
switch (poscond) {
/* completely filled */
case 0x01U | 0x02U | 0x04U | 0x08U:
frmtlev[y][x] = BLOCK_EMPTY; continue;
/* left to top corner */
case 0x01U:
frmtlev[y][x] = BLOCK_WALL_TL; continue;
/* top to right corner */
case 0x02U:
frmtlev[y][x] = BLOCK_WALL_TR; continue;
/* right to bottom corner */
case 0x04U:
frmtlev[y][x] = BLOCK_WALL_BR; continue;
/* bottom to left corner */
case 0x08U:
frmtlev[y][x] = BLOCK_WALL_BL; continue;
}
switch (poscond2) {
case 0x01U | 0x04U:
case 0x01U | 0x04U | 0x08U:
case 0x01U | 0x04U | 0x02U:
frmtlev[y][x] = BLOCK_WALL_HO; continue;
case 0x02U | 0x08U:
case 0x02U | 0x08U | 0x01U:
case 0x02U | 0x08U | 0x04U:
frmtlev[y][x] = BLOCK_WALL_VE; continue;
case 0x01U | 0x02U:
frmtlev[y][x] = BLOCK_WALL_TL; continue;
case 0x02U | 0x04U:
frmtlev[y][x] = BLOCK_WALL_TR; continue;
case 0x04U | 0x08U:
frmtlev[y][x] = BLOCK_WALL_BR; continue;
case 0x08U | 0x01U:
frmtlev[y][x] = BLOCK_WALL_BL; continue;
}
switch (poscond) {
case 0x02U | 0x04U | 0x08U:
frmtlev[y][x] = BLOCK_WALL_TL; continue;
case 0x01U | 0x04U | 0x08U:
frmtlev[y][x] = BLOCK_WALL_TR; continue;
case 0x01U | 0x02U | 0x08U:
frmtlev[y][x] = BLOCK_WALL_BR; continue;
case 0x01U | 0x02U | 0x04U:
frmtlev[y][x] = BLOCK_WALL_BL; continue;
}
frmtlev[y][x] = BLOCK_EMPTY;
}
(void) memcpy((lev_t *)level, (lev_t *)&frmtlev, sizeof(lev_t));
}
/* Counts the number of dots in the level, and returns that number. */
static unsigned
countdots(ModeInfo * mi)
{
pacmangamestruct *pp = &pacmangames[MI_SCREEN(mi)];
unsigned i, count = 0;
for (i = 0 ; i < LEVWIDTH*LEVHEIGHT ; i++)
if (pp->level[i] == BLOCK_DOT_2) count++;
return count;
}
/* Creates a new level, and places that in the pacmangamestruct. */
static int
createnewlevel(ModeInfo * mi)
{
pacmangamestruct *pp = &pacmangames[MI_SCREEN(mi)];
lev_t *level;
unsigned dirvec[1] = { GO_UP };
unsigned ret = 0, i = 0;
if ((level = (lev_t *)calloc(1, sizeof(lev_t))) == NULL)
return i;
if (NRAND(2) == 0) {
do {
clearlevel(level);
createjail(level, JAILWIDTH, JAILHEIGHT);
if ((ret = nextstep(level, LEVWIDTH/2 - 1,
LEVHEIGHT/2 - JAILHEIGHT/2 - 3,
dirvec, 1)) == 0) {
(void) free((void *) level);
return i;
}
} while (ret * 100 < (LEVWIDTH * LEVHEIGHT * MINDOTPERC));
filllevel(level);
frmtlevel(level);
finishjail(level, JAILWIDTH, JAILHEIGHT);
} else {
(void) memcpy(level, stdlevel, sizeof(lev_t));
frmtlevel(level);
i = 1;
}
copylevel(pp->level, level);
pp->dotsleft = countdots(mi);
(void) free((void *) level);
return i;
}
/* Checks if a position is allowable for ghosts/pacs to enter. */
static int
check_pos(pacmangamestruct *pp, int y, int x, int ghostpass)
{
if ((pp->level[y*LEVWIDTH + x] == BLOCK_DOT_2) ||
(pp->level[y*LEVWIDTH + x] == BLOCK_EMPTY) ||
((pp->level[y*LEVWIDTH + x] == BLOCK_GHOST_ONLY) && ghostpass)) {
return 1;
}
return 0;
}
/* Checks if there is a dot on the specified position in the level. */
static int
check_dot(pacmangamestruct *pp, unsigned int x, unsigned int y)
{
if (x >= LEVWIDTH || y >= LEVHEIGHT) return 0;
if (pp->level[y * LEVWIDTH + x] == BLOCK_DOT_2) return 1;
return 0;
}
|