--- deliantra/server/random_maps/expand2x.C 2006/09/10 16:06:37 1.3 +++ deliantra/server/random_maps/expand2x.C 2009/11/07 18:32:45 1.9 @@ -1,70 +1,40 @@ +/* + * This file is part of Deliantra, the Roguelike Realtime MMORPG. + * + * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) + * + * Deliantra is free software: you can redistribute it and/or modify it under + * the terms of the Affero GNU General Public License as published by the + * Free Software Foundation, either version 3 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 Affero GNU General Public License + * and the GNU General Public License along with this program. If not, see + * . + * + * The authors can be reached via e-mail to + */ /* * Expands a layout by 2x in each dimension. * H. S. Teoh * -------------------------------------------------------------------------- - * $Id: expand2x.C,v 1.3 2006/09/10 16:06:37 root Exp $ + * $Id: expand2x.C,v 1.9 2009/11/07 18:32:45 root Exp $ * * ALGORITHM * * ... (TBW) */ -#include /* just in case */ -#include /* use compiler to do sanity check */ - - -/* PROTOTYPES */ - -static void expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize); -static void expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize); -static void expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize); - - -/* FUNCTIONS */ - -char ** -expand2x (char **layout, int xsize, int ysize) -{ - int i, j; - int nxsize = xsize * 2 - 1; - int nysize = ysize * 2 - 1; - - /* Allocate new layout */ - char **newlayout = (char **) calloc (sizeof (char *), nxsize); - - for (i = 0; i < nxsize; i++) - { - newlayout[i] = (char *) calloc (sizeof (char), nysize); - } - - for (i = 0; i < xsize; i++) - { - for (j = 0; j < ysize; j++) - { - switch (layout[i][j]) - { - case '#': - expand_wall (newlayout, i, j, layout, xsize, ysize); - break; - case 'D': - expand_door (newlayout, i, j, layout, xsize, ysize); - break; - default: - expand_misc (newlayout, i, j, layout, xsize, ysize); - } - } - } - - /* Dump old layout */ - for (i = 0; i < xsize; i++) - { - free (layout[i]); - } - free (layout); - - return newlayout; -} +#include "global.h" +#include "random_map.h" +#include "rproto.h" /* Copy the old tile X into the new one at location (i*2, j*2) and * fill up the rest of the 2x2 result with \0: @@ -72,7 +42,7 @@ * \0 \0 */ static void -expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize) +expand_misc (Layout newlayout, int i, int j, Layout layout) { newlayout[i * 2][j * 2] = layout[i][j]; /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that @@ -87,18 +57,19 @@ * and the possible combinations thereof. */ static int -calc_pattern (char ch, char **layout, int i, int j, int xsize, int ysize) +calc_pattern (char ch, Layout layout, int i, int j) { int pattern = 0; - if (i + 1 < xsize && layout[i + 1][j] == ch) + if (i + 1 < layout->w && layout[i + 1][j] == ch) pattern |= 1; - if (j + 1 < ysize) + if (j + 1 < layout->h) { if (layout[i][j + 1] == ch) pattern |= 2; - if (i + 1 < xsize && layout[i + 1][j + 1] == ch) + + if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) pattern |= 4; } @@ -110,28 +81,27 @@ * walls. */ static void -expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize) +expand_wall (Layout newlayout, int i, int j, Layout layout) { - int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); - int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); + int wall_pattern = calc_pattern ('#', layout, i, j); + int door_pattern = calc_pattern ('D', layout, i, j); int both_pattern = wall_pattern | door_pattern; newlayout[i * 2][j * 2] = '#'; - if (i + 1 < xsize) + + if (i + 1 < layout->w) { if (both_pattern & 1) { /* join walls/doors to the right */ - /* newlayout[i*2+1][j*2] = '#'; */ newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; } } - if (j + 1 < ysize) + if (j + 1 < layout->h) { if (both_pattern & 2) { /* join walls/doors to the bottom */ - /* newlayout[i*2][j*2+1] = '#'; */ newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; } @@ -149,38 +119,49 @@ * that it doesn't know how to correctly expand. */ static void -expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize) +expand_door (Layout newlayout, int i, int j, Layout layout) { - int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); - int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); + int wall_pattern = calc_pattern ('#', layout, i, j); + int door_pattern = calc_pattern ('D', layout, i, j); int join_pattern; /* Doors "like" to connect to walls more than other doors. If there is * a wall and another door, this door will connect to the wall and * disconnect from the other door. */ if (wall_pattern & 3) - { - join_pattern = wall_pattern; - } + join_pattern = wall_pattern; else - { - join_pattern = door_pattern; - } + join_pattern = door_pattern; newlayout[i * 2][j * 2] = 'D'; - if (i + 1 < xsize) - { - if (join_pattern & 1) - { /* there is a door/wall to the right */ - newlayout[i * 2 + 1][j * 2] = 'D'; - } - } - if (j + 1 < ysize) - { - if (join_pattern & 2) - { /* there is a door/wall below */ - newlayout[i * 2][j * 2 + 1] = 'D'; + if (i + 1 < layout->w) + if (join_pattern & 1) + /* there is a door/wall to the right */ + newlayout[i * 2 + 1][j * 2] = 'D'; + + if (j + 1 < layout->h) + if (join_pattern & 2) + /* there is a door/wall below */ + newlayout[i * 2][j * 2 + 1] = 'D'; +} + +void +expand2x (Layout layout) +{ + Layout newlayout (layout->w * 2 - 1, layout->h * 2 - 1); + newlayout->clear (); + + for (int i = 0; i < layout->w; i++) + for (int j = 0; j < layout->h; j++) + switch (layout[i][j]) + { + case '#': expand_wall (newlayout, i, j, layout); break; + case 'D': expand_door (newlayout, i, j, layout); break; + default: expand_misc (newlayout, i, j, layout); break; } - } + + layout.swap (newlayout); + newlayout.free (); } +