/* ** ClanLib SDK ** Copyright (c) 1997-2005 The ClanLib Team ** ** This software is provided 'as-is', without any express or implied ** warranty. In no event will the authors be held liable for any damages ** arising from the use of this software. ** ** Permission is granted to anyone to use this software for any purpose, ** including commercial applications, and to alter it and redistribute it ** freely, subject to the following restrictions: ** ** 1. The origin of this software must not be misrepresented; you must not ** claim that you wrote the original software. If you use this software ** in a product, an acknowledgment in the product documentation would be ** appreciated but is not required. ** 2. Altered source versions must be plainly marked as such, and must not be ** misrepresented as being the original software. ** 3. This notice may not be removed or altered from any source distribution. ** ** Note: Some of the libraries ClanLib may link to may have additional ** requirements or restrictions. ** ** File Author(s): ** ** Magnus Norddahl ** (if your name is missing here, please add it) */ #include "precomp.h" #include "map.h" ///////////////////////////////////////////////////////////////////////////// // Map construction: Map::Map(CL_ResourceManager *resources) : width(0), height(0), tiles(0), eggs_left(0) { tile_images = CL_Sprite("Game/spr_maptiles", resources); } Map::~Map() { delete[] tiles; } ///////////////////////////////////////////////////////////////////////////// // Map attributes: Map::TileType Map::get_tile_type(int x, int y) { int tile = tiles[x + y*width]; switch (tile) { case 0: return tile_empty; case 1: return tile_egg; case 2: return tile_powerup; case 3: return tile_trail; default: return tile_wall; } } ///////////////////////////////////////////////////////////////////////////// // Map operations: void Map::generate_level(int map_width, int map_height, int num_connections) { // Clean up any possible earlier map: delete[] tiles; // Allocate map array and setup variables: width = map_width; height = map_height; tiles = new int[width * height]; eggs_left = 0; // Random generate map: int x, y, center_x, center_y, num_tiles, i; // 1. clear map with walls. for (y=0; y=0; pos2--) { if (find_neighbour( tunnel_x[tun][pos2], tunnel_y[tun][pos2], &tunnel_x[tun][pos], &tunnel_y[tun][pos])) { total_dead_end = false; break; } } // If we moved into a total dead end, this tunnel can move no // further. if (total_dead_end) { tunnel_x[tun][pos] = tunnel_x[tun][pos-1]; tunnel_y[tun][pos] = tunnel_y[tun][pos-1]; filled_level++; } } // Mark newly found tile as egg: tiles[tunnel_x[tun][pos] + tunnel_y[tun][pos]*width] = 1; } // All four tunnels reached a dead end. We're done tunneling. if (filled_level == 4) break; } // Clean up. for (i=0; i<4; i++) { delete[] tunnel_x[i]; delete[] tunnel_y[i]; } // 4. connect gaps random in the tunnels: for (i=0; i0 && tiles[(x-1) + y*width] > 3) new_image |= 1; // left flag if (x 3) new_image |= 2; // right flag if (y>0 && tiles[x + (y-1)*width] > 3) new_image |= 4; // up flag if (y 3) new_image |= 8; // down flag tiles[x + y*width] = new_image+4; } } // Place powerups in each corner of map: tiles[1 + 1*width] = 2; tiles[(width-2) + 1*width] = 2; tiles[1 + (height-2)*width] = 2; tiles[(width-2) + (height-2)*width] = 2; // Count the number of eggs eggs_left = 0; for (y=0; y1 && tiles[(x-2) + y*width] >= 4) num_dirs++; if (y>1 && tiles[x + (y-2)*width] >= 4) num_dirs++; if (x= 4) num_dirs++; if (y= 4) num_dirs++; int sel = rand()%(num_dirs+1); if (x>1 && tiles[(x-2)+y*width] >= 4 && (sel--) == 0) { tiles[(x-1)+y*width] = 1; *xx = x-2; return true; } if (y>1 && tiles[x+(y-2)*width] >= 4 && (sel--) == 0) { tiles[x+(y-1)*width] = 1; *yy = y-2; return true; } if (x= 4 && (sel--) == 0) { tiles[(x+1)+y*width] = 1; *xx = x+2; return true; } if (y= 4 && (sel--) == 0) { tiles[x+(y+1)*width] = 1; *yy = y+2; return true; } return false; }