--- deliantra/server/random_maps/layout.C 2010/07/03 03:09:27 1.15
+++ deliantra/server/random_maps/layout.C 2018/11/17 23:40:02 1.34
@@ -1,31 +1,32 @@
/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
- *
- * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
- * Copyright (©) Crossfire Development Team (restored, original file without copyright notice)
- *
+ *
+ * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
+ * Copyright (©) 2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ * Copyright (©) 1994-2004 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
*/
#include
-#include
+#include
#include
-void
+ecb_noinline void
layout::alloc (int w, int h)
{
assert (sizeof (cell) == 1);
@@ -76,14 +77,23 @@
sfree ((char *)data, size);
}
-void
+ecb_noinline void
layout::fill (char fill)
{
//memset (data [0], fill, w * h); // only when contiguous :/
fill_rect (0, 0, w, h, fill);
}
-void
+ecb_noinline void
+layout::replace (char from, char to)
+{
+ for (int x = 0; x < w; ++x)
+ for (int y = 0; y < h; ++y)
+ if (data [x][y] == from)
+ data [x][y] = to;
+}
+
+ecb_noinline void
layout::rect (int x1, int y1, int x2, int y2, char fill)
{
--x2;
@@ -95,32 +105,33 @@
data [x1][y1] = data [x1][y2 - 1] = fill;
}
-void
+ecb_noinline void
layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
{
for (; x1 < x2; ++x1)
memset (data [x1] + y1, fill, y2 - y1);
}
-void layout::border (char fill)
+void
+layout::border (char fill)
{
rect (0, 0, w, h, fill);
}
-void
+ecb_noinline void
layout::fill_rand (int percent)
{
percent = lerp (percent, 0, 100, 0, 256);
- for (int x = w - 1; --x > 0; )
- for (int y = h - 1; --y > 0; )
+ for (int x = 0; x < w; ++x)
+ for (int y = 0; y < h; ++y)
data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
}
/////////////////////////////////////////////////////////////////////////////
// erode by cellular automata
-void
+ecb_noinline void
layout::erode_1_2 (int c1, int c2, int repeat)
{
layout neu (w, h);
@@ -196,7 +207,7 @@
typedef fixed_stack pointlist;
-static void noinline
+ecb_noinline static void
push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
{
if (dist [x][y])
@@ -222,23 +233,25 @@
}
}
-static inline void
-make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d)
+static void inline
+make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
{
for (;;)
{
point neigh[4];
int ncnt = 0;
- if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
- if (x < dist.w - 1 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
- if (y > 0 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
- if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
+ d += perturb > 1;
+
+ if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
+ if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
+ if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
+ if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
if (!ncnt)
return;
- point &p = neigh [rmg_rndm (ncnt)];
+ point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
seeds.push (p);
@@ -265,14 +278,16 @@
// isolation remover, works on a "distance" map
// the map must be initialised with 0 == rooms, 255 = walls
-static void noinline
-isolation_remover (layout &dist)
+ecb_noinline static void
+isolation_remover (layout &dist, unsigned int perturb = 2)
{
// dist contains
// 0 == invisited rooms
// 1 == visited rooms
// 2+ shortest distance to random near room
+ clamp_it (perturb, 0, 2);
+
// phase 1, find seed
int cnt = 0;
int x, y;
@@ -300,11 +315,17 @@
// phase 2, while we have seeds, if
// seed is empty, floodfill, else grow
+ int rem_index = 0; // used to remove "somewhat ordered"
+
while (seeds.size)
{
coroapi::cede_to_tick ();
- point p = seeds.remove (rmg_rndm (seeds.size));
+ int i = perturb
+ ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1)
+ : rem_index ++ % seeds.size;
+
+ point p = seeds.remove (i);
x = p.x;
y = p.y;
@@ -313,7 +334,7 @@
{
// found new isolated area, make tunnel
push_flood_fill (dist, seeds, x, y);
- make_tunnel (dist, seeds, x, y, 255);
+ make_tunnel (dist, seeds, x, y, 254, perturb);
}
else
{
@@ -329,7 +350,7 @@
}
void
-layout::isolation_remover ()
+layout::isolation_remover (int perturb)
{
layout dist (w - 2, h - 2); // map without border
@@ -337,7 +358,7 @@
for (int y = 1; y < h - 1; ++y)
dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
- ::isolation_remover (dist);
+ ::isolation_remover (dist, perturb);
// now copy the tunnels over
for (int x = 1; x < w - 1; ++x)
@@ -348,46 +369,6 @@
/////////////////////////////////////////////////////////////////////////////
-// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
-void
-layout::gen_cave (int subtype)
-{
- switch (subtype)
- {
- // a rough cave
- case 0:
- fill_rand (rmg_rndm (85, 97));
- break;
-
- // corridors
- case 1:
- fill_rand (rmg_rndm (5, 40));
- erode_1_2 (5, 2, 10);
- erode_1_2 (5, -1, 10);
- erode_1_2 (5, 2, 1);
- break;
-
- // somewhat open, roundish
- case 2:
- fill_rand (45);
- erode_1_2 (5, 0, 5);
- erode_1_2 (5, 1, 1);
- break;
-
- // wide open, some room-like structures
- case 3:
- fill_rand (45);
- erode_1_2 (5, 2, 4);
- erode_1_2 (5, -1, 3);
- break;
- }
-
- border ();
- isolation_remover ();
-}
-
-/////////////////////////////////////////////////////////////////////////////
-
//+GPL
/* puts doors at appropriate locations in a maze. */
@@ -395,10 +376,10 @@
layout::doorify ()
{
int ndoors = w * h / 60; /* reasonable number of doors. */
- int doorlocs = 0; /* # of available doorlocations */
- uint16 *doorlist_x = salloc (w * h);
- uint16 *doorlist_y = salloc (w * h);
+ coroapi::cede_to_tick ();
+
+ fixed_stack doorloc (w * h);
/* make a list of possible door locations */
for (int i = 1; i < w - 1; i++)
@@ -407,34 +388,21 @@
int sindex = surround_flag (*this, i, j);
if (sindex == 3 || sindex == 12) /* these are possible door sindex */
- {
- doorlist_x [doorlocs] = i;
- doorlist_y [doorlocs] = j;
- doorlocs++;
- }
+ doorloc.push (point (i, j));
}
- while (ndoors > 0 && doorlocs > 0)
+ while (ndoors && doorloc.size)
{
- int di = rmg_rndm (doorlocs);
- int i = doorlist_x [di];
- int j = doorlist_y [di];
- int sindex = surround_flag (*this, i, j);
+ point p = doorloc.remove (rmg_rndm (doorloc.size));
+
+ int sindex = surround_flag (*this, p.x, p.y);
if (sindex == 3 || sindex == 12) /* these are possible door sindex */
{
- data [i][j] = 'D';
- ndoors--;
+ data [p.x][p.y] = 'D';
+ --ndoors;
}
-
- /* reduce the size of the list */
- doorlocs--;
- doorlist_x[di] = doorlist_x [doorlocs];
- doorlist_y[di] = doorlist_y [doorlocs];
}
-
- sfree (doorlist_x, w * h);
- sfree (doorlist_y, w * h);
}
/* takes a map and makes it symmetric: adjusts Xsize and
@@ -502,6 +470,8 @@
void
layout::rotate (int rotation)
{
+ coroapi::cede_to_tick ();
+
switch (rotation & 3)
{
case 2: /* a reflection */
@@ -566,7 +536,7 @@
* 4 match on (i+1, j+1)
* and the possible combinations thereof.
*/
-static int noinline
+ecb_noinline static int
calc_pattern (char ch, layout &maze, int i, int j)
{
int pattern = 0;
@@ -663,12 +633,14 @@
new_layout.clear ();
+ coroapi::cede_to_tick ();
+
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
switch (data [i][j])
{
- case '#': expand_wall (new_layout, i, j, *this); break;
- case 'D': expand_door (new_layout, i, j, *this); break;
+ case '#': expand_wall (new_layout, i, j, *this); break;
+ case 'D': expand_door (new_layout, i, j, *this); break;
default: expand_misc (new_layout, i, j, *this); break;
}
@@ -754,7 +726,7 @@
}
int
-make_wall (char **maze, int x, int y, int dir)
+make_wall (layout &maze, int x, int y, int dir)
{
maze[x][y] = 'D'; /* mark a door */
@@ -782,6 +754,8 @@
{
int tries = w * h / 30;
+ coroapi::cede_to_tick ();
+
for (int ti = 0; ti < tries; ti++)
{
/* starting location for looking at creating a door */
@@ -813,27 +787,88 @@
}
}
+//-GPL
+
/////////////////////////////////////////////////////////////////////////////
-static void
-gen_mixed_ (layout &maze, random_map_params *RP, int dir)
+// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
+void
+layout::gen_cave (int subtype)
{
- if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
- dir = 2; // stop recursion randomly
+ switch (subtype)
+ {
+ // a rough cave
+ case 0:
+ fill_rand (rmg_rndm (85, 97));
+ break;
+
+ // corridors
+ case 1:
+ fill_rand (rmg_rndm (5, 40));
+ erode_1_2 (5, 2, 10);
+ erode_1_2 (5, -1, 10);
+ erode_1_2 (5, 2, 1);
+ break;
+
+ // somewhat open, some room-like structures
+ case 2:
+ fill_rand (45);
+ erode_1_2 (5, 2, 4);
+ erode_1_2 (5, -1, 3);
+ break;
- if (dir == 0 && maze.w > 16)
+ // wide open, roundish
+ case 3:
+ fill_rand (45);
+ erode_1_2 (5, 0, 5);
+ erode_1_2 (5, 1, 1);
+ break;
+ }
+
+ border ();
+ isolation_remover (1);
+}
+
+void
+layout::gen_castle ()
+{
+ fill ('#');
+
+ for (int n = w * h / 30 + 1; n--; )
+ {
+ int rw = rmg_rndm (6, 10);
+ int rh = rmg_rndm (6, 10);
+
+ if (rw > w || rh > h)
+ continue;
+
+ int rx = rmg_rndm (0, w - rw);
+ int ry = rmg_rndm (0, h - rh);
+
+ rect (rx, ry, rx + rw, ry + rh, '#');
+ fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
+ }
+
+ border ();
+ isolation_remover (0);
+}
+
+static void
+gen_mixed_ (layout &maze, random_map_params *RP)
+{
+ if (maze.w > maze.h && maze.w > 16)
{
int m = rmg_rndm (8, maze.w - 8);
- layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir);
- layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
+ layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
+ layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
}
- else if (dir == 1 && maze.h > 16)
+ else if (maze.h > 16)
{
int m = rmg_rndm (8, maze.h - 8);
- layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir);
- layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
+ layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
+ layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
}
else
{
@@ -844,19 +879,29 @@
maze.generate (RP);
}
+
+ coroapi::cede_to_tick ();
}
+// recursive subdivision with random sublayouts
static void
gen_mixed (layout &maze, random_map_params *RP)
{
random_map_params &rp = *new random_map_params (RP);
- gen_mixed_ (maze, &rp, rmg_rndm (2));
+ gen_mixed_ (maze, &rp);
delete &rp;
maze.border ();
- maze.isolation_remover ();
+
+ // exits currently do not work so well, as they
+ // are currently often found together, so nuke entrances
+ maze.replace ('<', ' ');
+
+ maze.isolation_remover (0);
}
+//+GPL
+
/* function selects the maze function and gives it whatever
arguments it needs. */
void
@@ -925,6 +970,14 @@
break;
+ case LAYOUT_CASTLE:
+ gen_castle ();
+
+ if (rmg_rndm (2))
+ doorify ();
+
+ break;
+
case LAYOUT_MULTIPLE:
gen_mixed (*this, RP);
break;
@@ -937,18 +990,40 @@
//-GPL
#if 0
+static void
+gen_village (layout &maze)
+{
+ maze.clear ();
+ maze.border ();
+
+ for (int n = maze.w * maze.h / 200 + 1; n--; )
+ {
+ int rw = rmg_rndm (6, 10);
+ int rh = rmg_rndm (6, 10);
+
+ int rx = rmg_rndm (2, maze.w - rw - 2);
+ int ry = rmg_rndm (2, maze.h - rh - 2);
+
+ maze.rect (rx, ry, rx + rw, ry + rh, '#');
+ }
+
+ maze.border ();
+ maze.isolation_remover (2);
+}
+
static struct demo
{
demo ()
{
rmg_rndm.seed (time (0));
+ extern void hack();hack ();
for(int i=1;i<100;i++)
{
- layout maze (40, 25);
- maze.fill_rand (85);
+ layout maze (40, 30);
+ maze.fill_rand (99);
maze.border ();
- maze.isolation_remover ();
+ maze.isolation_remover (2);
maze.print ();
}