/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992 Frank Tore Johansen
*
* 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
/* Put in the walls and autojoin them. */
/* given a maze and a coordinate, tell me which squares up/down/right/left
are occupied. */
int
surround_flag (const layout &maze, int i, int j)
{
/* 1 = wall to left,
2 = wall to right,
4 = wall above
8 = wall below */
int surround_index = 0;
if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
return surround_index;
}
/* like surround_flag, but only walls count. */
int
surround_flag2 (const layout &maze, int i, int j)
{
/* 1 = wall to left,
2 = wall to right,
4 = wall above
8 = wall below */
int surround_index = 0;
if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
return surround_index;
}
/* like surround_flag, except it checks a map, not a maze.
* Since this is part of the random map code, presumption
* is that this is not a tiled map.
* What is considered blocking and not is somewhat hard coded.
*/
int
surround_flag3 (maptile *map, int i, int j)
{
/*
* 1 = blocked to left,
* 2 = blocked to right,
* 4 = blocked above
* 8 = blocked below
*/
int surround_index = 0;
// don't forget to update the mapspace!
if (i > 0) map->at (i - 1, j ).update ();
if (i < map->width - 1) map->at (i + 1, j ).update ();
if (j > 0) map->at (i , j - 1).update ();
if (j < map->height - 1) map->at (i , j + 1).update ();
if (i > 0 && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK)) surround_index |= 1;
if (i < map->width - 1 && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & MOVE_WALK)) surround_index |= 2;
if (j > 0 && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & MOVE_WALK)) surround_index |= 4;
if (j < map->height - 1 && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK)) surround_index |= 8;
return surround_index;
}
/* like surround_flag2, except it checks a map, not a maze. */
static int
surround_flag4 (maptile *map, int i, int j)
{
/* 1 = blocked to left,
2 = blocked to right,
4 = blocked above
8 = blocked below */
int surround_index = 0;
if (i > 0 && wall_blocked (map, i - 1, j)) surround_index |= 1;
if (i < map->width - 1 && wall_blocked (map, i + 1, j)) surround_index |= 2;
if (j > 0 && wall_blocked (map, i, j - 1)) surround_index |= 4;
if (j < map->height - 1 && wall_blocked (map, i, j + 1)) surround_index |= 8;
return surround_index;
}
/* picks the right wall type for this square, to make it look nice,
and have everything nicely joined. It uses the maze. */
static object *
pick_joined_wall (object *the_wall, const layout &maze, int i, int j, random_map_params *RP)
{
int l;
char wall_name[1024];
archetype *wall_arch = 0;
assign (wall_name, the_wall->arch->archname);
/* conventionally, walls are named like this:
wallname_wallcode, where wallcode indicates
a joinedness, and wallname is the wall.
this code depends on the convention for
finding the right wall. */
/* extract the wall name, which is the text up to the leading _ */
for (l = 0; l < 64; l++)
{
if (wall_name[l] == '_')
{
wall_name[l] = 0;
break;
}
}
strcat (wall_name, "_");
strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
wall_arch = archetype::find (wall_name);
return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
}
// checks whether the layout has a "reachable" space at a given point, i.e.
// not a wall that is completely surrounded by walls.
static bool inline
is_visible (layout &maze, int i, int j)
{
for (int dx = -1; dx <= 1; ++dx)
for (int dy = -1; dy <= 1; ++dy)
{
int x = i + dx;
int y = j + dy;
if (IN_RANGE_EXC (x, 0, maze.w)
&& IN_RANGE_EXC (y, 0, maze.h)
&& maze [x][y] != '#')
return true;
}
return false;
}
/* takes a map and a maze, and puts walls in the map (picked from
w_style) at '#' marks. */
void
make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP)
{
object *the_wall;
/* get the style map */
if (!strcmp (w_style, "none"))
return;
maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty);
if (!style_map)
return;
maptile *mining_map = m_style && *m_style
? find_style ("/styles/miningstyles", m_style, RP->difficulty)
: 0;
if ((the_wall = style_map->pick_random_object (rmg_rndm)))
{
// first pass, remove all "unreachable" (in-rock) walls
// and replace them by "blocked" spaces.
layout clean_maze (maze);
if (archetype *blocked = archetype::find ("blocked"))
for (int x = 0; x < maze.w; ++x)
for (int y = 0; y < maze.h; ++y)
if (maze [x][y] == '#' && !is_visible (maze, x, y))
{
clean_maze [x][y] = 'X';
// erase everything on this space ad replace it by blocked
mapspace &ms = map->at (x, y);
while (ms.top)
ms.top->destroy ();
map->insert (blocked->instance (), x, y, 0, INS_NO_MERGE | INS_NO_WALK_ON);
}
// second pass, add in walls
int joinedwalls = 0;
assign (RP->wall_name, the_wall->arch->archname);
if (char *cp = strchr (RP->wall_name, '_'))
{
*cp = 0;
joinedwalls = 1;
}
for (int i = 0; i < RP->Xsize; i++)
for (int j = 0; j < RP->Ysize; j++)
if (clean_maze [i][j] == '#')
{
object *thiswall = joinedwalls ? pick_joined_wall (the_wall, clean_maze, i, j, RP)
: the_wall->arch->instance ();
thiswall->move_block = MOVE_ALL;
map->insert (thiswall, i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
if (mining_map)
if (object *vein = mining_map->at (rmg_rndm (mining_map->width), rmg_rndm (mining_map->height)).bot)
map->insert (vein->clone (), i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
}
}
}
/* this takes a map, and changes an existing wall to match what's blocked
* around it, counting only doors and walls as blocked. If insert_flag is
* 1, it will go ahead and insert the wall into the map. If not, it
* will only return the wall which would belong there, and doesn't
* remove anything. It depends on the
* global, previously-set variable, "wall_name"
*/
object *
retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
{
int l;
object *the_wall = 0;
object *new_wall = 0;
archetype *wall_arch = 0;
/* first find the wall */
for (the_wall = GET_MAP_OB (the_map, i, j); the_wall; the_wall = the_wall->above)
if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER)
break;
/* if what we found is a door, don't remove it, set the_wall to NULL to
* signal that later.
*/
if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
{
the_wall = NULL;
/* if we're not supposed to insert a new wall where there wasn't one,
* we've gotta leave.
*/
if (insert_flag == 0)
return 0;
}
else if (the_wall == NULL)
return NULL;
/* canonicalize the wall name */
for (l = 0; l < 64; l++)
{
if (RP->wall_name[l] == '_')
{
RP->wall_name[l] = 0;
break;
}
}
strcat (RP->wall_name, "_");
strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
wall_arch = archetype::find (RP->wall_name);
if (!wall_arch)
{
new_wall = wall_arch->instance ();
new_wall->x = i;
new_wall->y = j;
if (the_wall->map)
the_wall->destroy ();
the_wall->move_block = MOVE_ALL;
insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON);
}
return new_wall;
}