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
|
/*
* Linux snipes, a text-based maze-oriented game for linux.
* Copyright (C) 1997 Jeremy Boulton.
*
* 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.
*
* Jeremy Boulton is reachable via electronic mail at
* boultonj@ugcs.caltech.edu.
*/
/* walls.c */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <curses.h>
#include "snipes.h"
#include "mazegen.h"
#include "chars.h"
#include "collision.h"
#include "screen.h"
#define X_MAX 2000
#define Y_MAX 2000
#define TOP_WALL_MASK 0x8
#define LEFT_WALL_MASK 0x4
#define RIGHT_WALL_MASK 0x2
#define BOTTOM_WALL_MASK 0x1
chtype corner_char[]={
BLANK_CHAR, // 0
VERTICAL_WALL_CHAR, // 1
HORIZONTAL_WALL_CHAR, // 2
UPPER_LEFT_CORNER_CHAR, // 3
HORIZONTAL_WALL_CHAR, // 4
UPPER_RIGHT_CORNER_CHAR, // 5
HORIZONTAL_WALL_CHAR, // 6
HORIZONTAL_T_DOWN_CHAR, // 7
VERTICAL_WALL_CHAR, // 8
VERTICAL_WALL_CHAR, // 9
LOWER_LEFT_CORNER_CHAR, // 10
VERTICAL_T_RIGHT_CHAR, // 11
LOWER_RIGHT_CORNER_CHAR, // 12
VERTICAL_T_LEFT_CHAR, // 13
HORIZONTAL_T_UP_CHAR, // 14
FOUR_WAY_INTERSECTION // 15
};
/* WRAP macro for enforcing the range 0 <= t <= m. Assumes that t will
* always less than (m+1) out of range.
*/
#define WRAP(t,m) ( (t>m)?t-m-1:((t<0)?t+m+1:t) )
/* 2 more wrapping macros. The range is 0 <= t <= m. t should be
* in range to start with.
*/
#define INCWRAP(t,m) ( (t==m)?0:t+1 )
#define DECWRAP(t,m) ( (t==0)?m:t-1 )
/*
* Cell width is the wall-to-wall width:
*
* |--- 12 ---|
* | |
* +----------+
*
* and addressing of the map begins at (0,0) in the upper
* left hand corner. Use normalize_x_coord() and
* normalize_y_coord() to make sure that (x,y) references
* are not outsize of the bounds of the maze. i.e., you
* use these to handle wrap-around.
*
*/
typedef struct wall_info {
char *wall_array;
int cell_width;
int cell_height;
long max_x_cell;
long max_y_cell;
long max_x;
long max_y;
} wall_info;
long normalize_x_coord( wall_info *w, long x );
long normalize_y_coord( wall_info *w, long y );
int init_walls( wall_info *w, int x, int y, int width, int height );
int close_walls( wall_info *w );
void draw_walls( wall_info *w, screen_coords *sc );
long normalize_x_coord( wall_info *w, long x )
{
return WRAP(x, w->max_x);
}
long normalize_y_coord( wall_info *w, long y )
{
return WRAP(y, w->max_y);
}
long next_x_cell( wall_info *w, long xcell )
{
return INCWRAP( xcell, w->max_x_cell );
}
long next_y_cell( wall_info *w, long ycell )
{
return INCWRAP( ycell, w->max_y_cell );
}
long prev_x_cell( wall_info *w, long xcell )
{
return DECWRAP( xcell, w->max_x_cell );
}
long prev_y_cell( wall_info *w, long ycell )
{
return DECWRAP( ycell, w->max_y_cell );
}
/* Functions for building the maze. */
/* set_wall_segment( wall_info *wi, int x, int y, int type )
* Record for both display and collision detection purposes
* that there is a wall of a particular corner type at the
* given location.
*
* x, y: maze coordinate at which to add wall
* Note: assumed to be in the correct range.
* type: wall (corner) type number
*/
void set_wall_segment( wall_info *wi, int x, int y, int type )
{
*(wi->wall_array+y*(wi->max_x+1)+x) = type;
if( type )
collision_map_place_obj( x, y, 1, 1, MAZE_OBJECT_WALL );
else
collision_map_remove_obj( x, y, 1, 1, MAZE_OBJECT_WALL );
}
/* check_wall_segment( wall_info *wi, int x, int y )
* x, y: maze coordinate at which to add wall
* Note: assumed to be in the correct range.
* Returns: (bool) wall exists at that location.
*/
int check_wall_segment( wall_info *wi, int x, int y )
{
return *(wi->wall_array+y*(wi->max_x+1)+x);
}
/* add_h_wall( void *wi, long y, long x1, long x2 )
* x1, x2, y: maze block coordinates
* This function is called by the maze generator.
*/
void add_h_wall( void *wi, long y, long x1, long x2 )
{
wall_info *w=wi;
long i,x;
for( x=x1; x!=x2; x=next_x_cell(w,x) )
for( i=0; i<w->cell_width; i++ )
set_wall_segment( w, x*w->cell_width+i, y*w->cell_height, 2 );
}
/* add_v_wall( void *wi, long x, long y1, long y2 )
* y1, y2, x: maze block coordinates
* This function is called by the maze generator.
*/
void add_v_wall( void *wi, long x, long y1, long y2 )
{
wall_info *w=wi;
long i,y;
for( y=y1; y!=y2; y=next_y_cell(w,y) )
for( i=0; i<w->cell_height; i++ )
set_wall_segment( w, x*w->cell_width, y*w->cell_height+i, 1 );
}
int init_walls( wall_info *w, int x, int y, int width, int height )
{
char temp;
long i,j,cx,cy;
maze *mymaze;
if( x < 0 || x > X_MAX || y < 0 || y > Y_MAX )
return 1;
if( (w->wall_array = (char *)malloc( x*y*(width-1)*(height-1) )) == NULL )
return 1;
memset( w->wall_array, 0, x*y*(width-1)*(height-1)*sizeof(char) );
w->cell_width=width-1;
w->cell_height=height-1;
w->max_x_cell=x-1;
w->max_y_cell=y-1;
w->max_x=x*( width-1)-1;
w->max_y=y*(height-1)-1;
mymaze = maze_init( w, (voidfunc)add_h_wall, (voidfunc)add_v_wall, x, y );
maze_finish( mymaze );
/* Set corner characters now that all the walls are drawn. */
for( cx=0; cx<=w->max_x_cell; cx++ )
for( cy=0; cy<=w->max_y_cell; cy++ ) {
i = cx*w->cell_width;
j = cy*w->cell_height;
temp = 0;
if( check_wall_segment( w, normalize_x_coord(w,i-1), j ) )
temp |= LEFT_WALL_MASK;
if( check_wall_segment( w, normalize_x_coord(w,i+1), j ) )
temp |= RIGHT_WALL_MASK;
if( check_wall_segment( w, i, normalize_y_coord(w,j-1) ) )
temp |= TOP_WALL_MASK;
if( check_wall_segment( w, i, normalize_y_coord(w,j+1) ) )
temp |= BOTTOM_WALL_MASK;
set_wall_segment( w, i, j, temp );
}
return 0;
}
int close_walls( wall_info *w )
{
free( w->wall_array );
return 0;
}
void draw_walls( wall_info *w, screen_coords *sc )
{
long x, y;
long final_x, final_y;
long start2_x, start2_y;
long final2_x, final2_y;
int sx, sy;
int index;
char *t;
screen_setnormalattr();
start2_x = final2_x = final_x = normalize_x_coord( w, sc->maze_x2+1 );
start2_y = final2_y = final_y = normalize_y_coord( w, sc->maze_y2+1 );
if( final_x < sc->maze_x1 ) {
start2_x = 0;
final_x = w->max_x+1;
}
if( final_y < sc->maze_y1 ) {
start2_y = 0;
final_y = w->max_y+1;
}
sy = sc->screen_y1;
for( y=sc->maze_y1; y!=final_y; y++, sy++ ) {
sx = sc->screen_x1;
t = w->wall_array+y*(w->max_x+1);
for( x=sc->maze_x1; x!=final_x; x++, sx++ ) {
index = *(t+x);
if( corner_char[index] != BLANK_CHAR )
screen_mvaddch( sy, sx, corner_char[index] );
}
for( x=start2_x; x!=final2_x; x++, sx++ ) {
index = *(t+x);
if( corner_char[index] != BLANK_CHAR )
screen_mvaddch( sy, sx, corner_char[index] );
}
}
for( y=start2_y; y!=final2_y; y++, sy++ ) {
sx = sc->screen_x1;
t = w->wall_array+y*(w->max_x+1);
for( x=sc->maze_x1; x!=final_x; x++, sx++ ) {
index = *(t+x);
if( corner_char[index] != BLANK_CHAR )
screen_mvaddch( sy, sx, corner_char[index] );
}
for( x=start2_x; x!=final2_x; x++, sx++ ) {
index = *(t+x);
if( corner_char[index] != BLANK_CHAR )
screen_mvaddch( sy, sx, corner_char[index] );
}
}
}
|