File: snake.c

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (113 lines) | stat: -rw-r--r-- 3,045 bytes parent folder | download | duplicates (4)
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
/*
 * Crossfire -- cooperative multi-player graphical RPG and adventure game
 *
 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
 * Copyright (c) 1992 Frank Tore Johansen
 *
 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
 * welcome to redistribute it under certain conditions. For details, please
 * see COPYING and LICENSE.
 *
 * The authors can be reached via e-mail at <crossfire@metalforge.org>.
 */

/**
 * @file
 * Snake-like layout generator.
 * @author peterm@langmuir.eecs.berkeley.edu
 */

#include "global.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/**
 * Generate a snake-like layout.
 * @param xsize
 * @param ysize
 * layout size.
 * @return
 * generated layout.
 */
char **make_snake_layout(int xsize, int ysize, int _unused_option, int _unused_layers)
{
    int i, j;

    (void)_unused_option;
    (void)_unused_layers;

    /* allocate that array, set it up */
    char **maze = (char **)calloc(sizeof(char *), xsize);
    for (i = 0; i < xsize; i++) {
        maze[i] = (char *)calloc(sizeof(char), ysize);
    }

    /* write the outer walls */
    for (i = 0; i < xsize; i++) {
        maze[i][0] = maze[i][ysize-1] = '#';
    }
    for (j = 0; j < ysize; j++) {
        maze[0][j] = maze[xsize-1][j] = '#';
    }

    /* Bail out if the size is too small to make a snake. */
    if (xsize < 8 || ysize < 8) {
        return maze;
    }

    /* decide snake orientation--vertical or horizontal , and
       make the walls and place the doors. */

    if (RANDOM()%2) { /* vertical orientation */
        int n_walls = RANDOM()%((xsize-5)/3)+1;
        int spacing = xsize/(n_walls+1);
        int orientation = 1;

        for (i = spacing; i < xsize-3; i += spacing) {
            if (orientation) {
                for (j = 1; j < ysize-2; j++) {
                    maze[i][j] = '#';
                }
                maze[i][j] = 'D';
            } else {
                for (j = 2; j < ysize; j++) {
                    maze[i][j] = '#';
                }
                maze[i][1] = 'D';
            }
            orientation ^= 1; /* toggle the value of orientation */
        }
    } else { /* horizontal orientation */
        int n_walls = RANDOM()%((ysize-5)/3)+1;
        int spacing = ysize/(n_walls+1);
        int orientation = 1;

        for (i = spacing; i < ysize-3; i += spacing) {
            if (orientation) {
                for (j = 1; j < xsize-2; j++) {
                    maze[j][i] = '#';
                }
                maze[j][i] = 'D';
            } else {
                for (j = 2; j < xsize; j++) {
                    maze[j][i] = '#';
                }
                maze[1][i] = 'D';
            }
            orientation ^= 1; /* toggle the value of orientation */
        }
    }

    /* place the exit up/down */
    if (RANDOM()%2) {
        maze[1][1] = '<';
        maze[xsize-2][ysize-2] = '>';
    } else {
        maze[1][1] = '>';
        maze[xsize-2][ysize-2] = '<';
    }

    return maze;
}