File: map.C

package info (click to toggle)
xkobo 1.11-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 580 kB
  • ctags: 628
  • sloc: cpp: 4,869; ansic: 4,403; makefile: 45; sh: 15
file content (145 lines) | stat: -rw-r--r-- 3,882 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
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

/*
 * XKOBO, a video-oriented game
 * Copyright (C) 1995,1996  Akira Higuchi
 *     a-higuti@math.hokudai.ac.jp
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 */

#include "config.h"
#include "map.h"
#include "random.h"

void _map::init()
{
    int i, j;
    for (i=0; i<sx; i++)
      for (j=0; j<sy; j++)
        pos(i, j) = SPACE;
}

void _map::make_maze(int x, int y, int difx, int dify)
{
    int i,j;
    int vx, vy;
    
    /* clear */
    for (i=x-difx; i<=x+difx; i++)
      for (j=y-dify; j<=y+dify; j++)
        pos(i,j) = SPACE;
    pos(x, y) = CORE;
    
    /* push initial sites */
    site_max = 0;
    
    if (rand_num.get(8) < 128){
        this->maze_push(x-1, y  );
        this->maze_push(x+1, y  );
    }
    else{
        this->maze_push(x  , y-1);
        this->maze_push(x  , y+1);
    }
    
    
    for (;;){
        /* get one */
        if (this->maze_pop()) break;
        vx = sitex[site_max];
        vy = sitey[site_max];
        
        int dirs[4];
        for (i=0; i<4; i++) dirs[i] = 0;
        int dirs_max = 0;
        if (this->maze_judge(x, y, difx, dify, vx+2, vy+0))
          dirs[dirs_max++] = 1;
        if (this->maze_judge(x, y, difx, dify, vx+0, vy+2))
          dirs[dirs_max++] = 2;
        if (this->maze_judge(x, y, difx, dify, vx-2, vy+0))
          dirs[dirs_max++] = 3;
        if (this->maze_judge(x, y, difx, dify, vx+0, vy-2))
          dirs[dirs_max++] = 4;
        if (dirs_max == 0) continue; /* there are no places to go */
        i = rand_num.get() % dirs_max;
        this->maze_move_and_push(vx, vy, dirs[i]);
        this->maze_push(vx, vy);
    }
}

int _map::maze_pop()
{
    if (site_max == 0) return 1;
    int i = rand_num.get() % site_max;
    site_max--;
    if (i != site_max){
        int tmpx = sitex[site_max];
        int tmpy = sitey[site_max];
        sitex[site_max] = sitex[i];
        sitey[site_max] = sitey[i];
        sitex[i] = tmpx;
        sitey[i] = tmpy;
    }
    return 0;
}

void _map::maze_push(int x, int y)
{
    sitex[site_max]   = x;
    sitey[site_max++] = y;
    pos(x, y) = WALL;
}

void _map::maze_move_and_push(int x, int y, int d)
{
    int x1 = x;
    int y1 = y;
    switch (d){
      case 1:{x1 += 2; break;}
      case 2:{y1 += 2; break;}
      case 3:{x1 -= 2; break;}
      case 4:{y1 -= 2; break;}
    }
    this->maze_push(x1, y1);
    pos((x+x1)/2, (y+y1)/2) = WALL;
}

int _map::maze_judge(int cx, int cy, int dx, int dy, int x, int y)
{
    if ((x < cx-dx) || (x > cx+dx) || (y < cy-dy) || (y > cy+dy)) return 0;
    if (pos(x, y) == WALL) return 0;
    return 1;
}

void _map::convert(int ratio)
{
    int i, j;
    int p = 0;
    for (i=0; i<sx; i++)
      for (j=0; j<sy; j++){
          p = 0;
          if (pos(i,j) != WALL) continue;
          if ((j > 0   ) && (pos(i  , j-1) != SPACE)) p |= U_MASK;
          if ((i < sx-1) && (pos(i+1, j  ) != SPACE)) p |= R_MASK;
          if ((j < sy-1) && (pos(i  , j+1) != SPACE)) p |= D_MASK;
          if ((i > 0   ) && (pos(i-1, j  ) != SPACE)) p |= L_MASK;
          if ((p==U_MASK) || (p==R_MASK) || (p==D_MASK) || (p==L_MASK)){
              if (rand_num.get(8) < ratio)
                p |= HARD;
          }
          pos(i,j) = p;
      }
}