summaryrefslogtreecommitdiffstats
path: root/hacks/pacman_level.c
blob: f26b3bfe120b0e0681f7e833a57cfef0398e5869 (plain) (blame)
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*-
 * 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.
 */

#include <assert.h>
#include "pacman.h"
#include "pacman_level.h"


#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	'|'
#define BLOCK_DOT_BONUS 'o'

/* This is more or less the standard pacman level (without the left-right
   tunnel. */
static const lev_t stdlevel = {
    "########################################",
    "########################################",
    "#######````````````##````````````#######",
    "#######`####`#####`##`#####`####`#######",
    "#######`####`#####`##`#####`####`#######",
    "#######`####`#####`##`#####`####`#######",
    "#######``````````````````````````#######",
    "#######`####`##`########`##`####`#######",
    "#######`####`##`########`##`####`#######",
    "#######``````##````##````##``````#######",
    "############`#####`##`#####`############",
    "############`#####`##`#####`############",
    "############`##``````````##`############",
    "############`##`###==###`##`############",
    "############`##`########`##`############",
    "############````########````############",
    "############`##`########`##`############",
    "############`##`########`##`############",
    "############`##``````````##`############",
    "############`##`########`##`############",
    "############`##`########`##`############",
    "#######````````````##````````````#######",
    "#######`####`#####`##`#####`####`#######",
    "#######`####`#####`##`#####`####`#######",
    "#######```##````````````````##```#######",
    "#########`##`##`########`##`##`#########",
    "#########`##`##`########`##`##`#########",
    "#######``````##````##````##``````#######",
    "#######`##########`##`##########`#######",
    "#######`##########`##`##########`#######",
    "#######``````````````````````````#######",
    "########################################"
};

#define TILES_COUNT 11U

#define GO_UP 0x0001U
#define GO_LEFT 0x0002U
#define GO_RIGHT 0x0004U
#define GO_DOWN 0x0008U

static const struct tiles def_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 };


static int creatlevelblock (pacmangamestruct *pp, 
                            lev_t * level, const unsigned x,
                            const unsigned y);


enum
{ TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };

/* 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);
}

void
pacman_get_jail_opening ( int *x, int *y)
{
    int xstart = LEVWIDTH / 2  - JAILWIDTH / 2;
    int ystart = LEVHEIGHT / 2 - JAILHEIGHT / 2;
    *x = xstart + JAILWIDTH / 2;
    *y = ystart;
}

/* 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 (pacmangamestruct *pp, 
          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 (pp, level, x, y - 1))
                == 0)
                return 0;
            break;
        case GO_RIGHT:
            if (x > LEVWIDTH - 2 || (ret = creatlevelblock (pp, level, x + 1, y))
                == 0)
                return 0;
            break;
        case GO_DOWN:
            if (y > LEVHEIGHT - 2 || (ret = creatlevelblock (pp, level, x, y + 1))
                == 0)
                return 0;
            break;
        case GO_LEFT:
            if (x < 1 || (ret = creatlevelblock (pp, level, x - 1, y))
                == 0)
                return 0;
        }
        if (ret != -1)
            inc += (unsigned) ret;
    }
    if (inc == 0)
        inc = 1;
    return inc;
}

static int
creatlevelblock (pacmangamestruct *pp, 
                 lev_t * level, const unsigned x, const unsigned y)
{
    unsigned tried = GETNB (TILES_COUNT);
    unsigned tilenr;
    unsigned ret;
    lev_t savedlev;

    if (!pp->tiles) {
        pp->tiles = (struct tiles *) malloc (sizeof (def_tiles));
        memcpy (pp->tiles, def_tiles, sizeof (def_tiles));
    }

    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, pp->tiles[tilenr].block) != 0) {
            if ((ret = nextstep (pp, level, x, y, pp->tiles[tilenr].dirvec,
                                 pp->tiles[tilenr].ndirs)) != 0) {
                return ret + 1;
            }
            (void) memcpy (level, &savedlev, sizeof (lev_t));
        }
        tried &= ~(pp->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;
}


/* Check to see if the x and y value are in the corners.
 * we could probable compute these values once and avoid
 * go through the loops every time.
 */
static void
top_left (lev_t * level, unsigned int *passed_x, unsigned int *passed_y)
{
    int x, y;
    for (y = 0; y < LEVHEIGHT; y++)
        for (x = 0; x < LEVWIDTH; x++) {
            if (checkset (level, x, y) == 0) {
                *passed_x = x;
                *passed_y = y;
		return;
            }
        }
}


static void
bottom_left (lev_t * level, unsigned int *passed_x, unsigned int *passed_y)
{
    int x, y;
    for (y = LEVHEIGHT; y > -1; y--)
        for (x = 0; x < LEVWIDTH; x++) {
            if (checkset (level, x, y) == 0) {
                *passed_x = x;
                *passed_y = y;
                return;
            }
        }
}

static void
top_right (lev_t * level, unsigned int *passed_x, unsigned int *passed_y)
{
    int x, y;

    for (y = 0; y < LEVHEIGHT; y++)
        for (x = LEVWIDTH; x >= 0; x--) {
            if (checkset (level, x, y) == 0) {
                *passed_x = x;
                *passed_y = y;
		return;
            }
        }
}

static void
bottom_right (lev_t * level, unsigned int *passed_x, unsigned int *passed_y)
{
    int x, y;

    for (y = LEVHEIGHT; y >= 0; y--)
        for (x = LEVWIDTH; x >= 0; x--) {
            if (checkset (level, x, y) == 0) {
                *passed_x = x;
                *passed_y = y;
		return;
            }
        }
}

static void
init_bonus_dots (pacmangamestruct *pp, lev_t * level)
{
    unsigned int x = 0, y = 0;
    top_left (level, &x, &y);
    pp->bonus_dots[TOP_LEFT].x = x;
    pp->bonus_dots[TOP_LEFT].y = y;
    pp->bonus_dots[TOP_LEFT].eaten = False;
    top_right (level, &x, &y);
    pp->bonus_dots[TOP_RIGHT].x = x;
    pp->bonus_dots[TOP_RIGHT].y = y;
    pp->bonus_dots[TOP_RIGHT].eaten = False;
    bottom_left (level, &x, &y);
    pp->bonus_dots[BOTTOM_LEFT].x = x;
    pp->bonus_dots[BOTTOM_LEFT].y = y;
    pp->bonus_dots[BOTTOM_LEFT].eaten = False;
    bottom_right (level, &x, &y);
    pp->bonus_dots[BOTTOM_RIGHT].x = x;
    pp->bonus_dots[BOTTOM_RIGHT].y = y;
    pp->bonus_dots[BOTTOM_RIGHT].eaten = False;
}

int
pacman_is_bonus_dot (pacmangamestruct *pp, int x, int y, int *idx)
{
    int ret = False;
    int i;
    for (i = 0; i < NUM_BONUS_DOTS; i++) {
/*     fprintf(stderr,"is bonus: passed x (%d, %d) bonus (%d, %d)\n",x,y,bonus_dots[i].x, bonus_dots[i].y); */
        if (x == pp->bonus_dots[i].x && y == pp->bonus_dots[i].y) {
            ret = True;
            *idx = i;
            break;
        }
    }
    return ret;
}

static void
check_bonus_idx (int idx)
{
    assert (0 <= idx && idx < NUM_BONUS_DOTS);
}

int
pacman_bonus_dot_eaten (pacmangamestruct *pp, int idx)
{
    check_bonus_idx (idx);
    return pp->bonus_dots[idx].eaten;
}

void
pacman_eat_bonus_dot (pacmangamestruct *pp, int idx)
{
    check_bonus_idx (idx);
    pp->bonus_dots[idx].eaten = True;
}

void
pacman_bonus_dot_pos (pacmangamestruct *pp, int idx, int *x, int *y)
{
    check_bonus_idx (idx);
    *x = pp->bonus_dots[idx].x;
    *y = pp->bonus_dots[idx].y;
}

/* 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 (pacmangamestruct *pp, lev_t * level)
{
    lev_t frmtlev;
    int x, y;
    int idx;
    register unsigned poscond;
    register unsigned poscond2;

    clearlevel (&frmtlev);
    init_bonus_dots (pp, level);
    for (y = 0; y < LEVHEIGHT; y++)
        for (x = 0; x < LEVWIDTH; x++) {

            if (checkset (level, x, y) == 0) {
                if (pacman_is_bonus_dot (pp, x, y, &idx)) {
                    frmtlev[y][x] = BLOCK_DOT_BONUS;
                }
                else {
                    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 (pacmangamestruct *pp)
{
    unsigned i, count = 0;

    for (i = 0; i < LEVWIDTH * LEVHEIGHT; i++)
      if (pp->level[i] == BLOCK_DOT_2 || pp->level[i] == BLOCK_DOT_BONUS)
            count++;

    return count;
}

/* Creates a new level, and places that in the pacmangamestruct. */
int
pacman_createnewlevel (pacmangamestruct *pp)
{
    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 (pp, 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 (pp, level);
        finishjail (level, JAILWIDTH, JAILHEIGHT);
    }
    else {
        (void) memcpy (level, stdlevel, sizeof (lev_t));
        frmtlevel (pp, level);
        i = 1;
    }
    copylevel (pp->level, level);
    pp->dotsleft = countdots (pp);

    (void) free ((void *) level);
    return i;
}

/* Checks if a position is allowable for ghosts/pacs to enter. */
int
pacman_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_DOT_BONUS) ||
        ((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. */
int
pacman_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;
}