首先是需要一个生成 地牢文件 maze.txt 生成代码
代码来自
一种 Roguelike 地牢生成算法 | indienova 独立游戏
C++ Example of Dungeon-Building Algorithm - RogueBasin
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <ctime>
//https://www.roguebasin.com/index.php?title=C%2B%2B_Example_of_Dungeon-Building_Algorithm
//https://indienova.com/indie-game-development/roguelike-dungeon-building-algorithm/
//https://indienova.com/indie-game-development/rooms-and-mazes-a-procedural-dungeon-generator/
class Dungeon
{
int xmax;
int ymax;
int xsize;
int ysize;
int objects;
int chanceRoom;
int chanceCorridor;
int* dungeon_map;
long oldseed;
enum
{
tileUnused = 0,
tileDirtWall=1,
tileDirtFloor=2,
tileStoneWall=3,
tileCorridor=4,
tileDoor=5,
tileUpStairs=6,
tileDownStairs=7,
tileChest=8
};
std::string msgXSize;
std::string msgYSize;
std::string msgMaxObjects;
std::string msgNumObjects;
std::string msgHelp;
std::string msgDetailedHelp;
void setCell(int x, int y, int celltype)
{
dungeon_map[x + xsize * y] = celltype;
}
int getCell(int x, int y)
{
return dungeon_map[x + xsize * y];
}
int getRand(int min, int max)
{
time_t seed;
seed = time(NULL) + oldseed;
oldseed = seed;
srand(seed);
int n = max - min + 1;
int i = rand() % n;
if(i < 0)
i = -i;
return min + i;
}
bool makeCorridor(int x, int y, int lenght, int direction)
{
int len = getRand(2, lenght);
int floor = tileCorridor;
int dir = 0;
if(direction > 0 && direction < 4) dir = direction;
int xtemp = 0;
int ytemp = 0;
switch(dir)
{
case 0:
{
if(x < 0 || x > xsize) return false;
else xtemp = x;
for(ytemp = y; ytemp > (y-len); ytemp--)
{
if(ytemp < 0 || ytemp > ysize) return false;
if(getCell(xtemp, ytemp) != tileUnused) return false;
}
for(ytemp = y; ytemp > (y - len); ytemp--)
{
setCell(xtemp, ytemp, floor);
}
break;
}
case 1:
{
if(y < 0 || y > ysize) return false;
else ytemp = y;
for(xtemp = x; xtemp < (x + len); xtemp++)
{
if(xtemp < 0 || xtemp > xsize) return false;
if(getCell(xtemp, ytemp) != tileUnused) return false;
}
for(xtemp = x; xtemp < (x + len); xtemp++)
{
setCell(xtemp, ytemp, floor);
}
break;
}
case 2:
{
if(x < 0 || x > xsize) return false;
else xtemp = x;
for(ytemp = y; ytemp < (y + len); ytemp++)
{
if(ytemp < 0 || ytemp > ysize) return false;
if(getCell(xtemp, ytemp) != tileUnused) return false;
}
for (ytemp = y; ytemp < (y+len); ytemp++){
setCell(xtemp, ytemp, floor);
}
break;
}
case 3:
{
if (ytemp < 0 || ytemp > ysize) return false;
else ytemp = y;
for (xtemp = x; xtemp > (x-len); xtemp--){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
for (xtemp = x; xtemp > (x-len); xtemp--){
setCell(xtemp, ytemp, floor);
}
break;
}
}
//woot, we're still here! let's tell the other guys we're done!!
return true;
}
bool makeRoom(int x, int y, int xlength, int ylength, int direction){
//define the dimensions of the room, it should be at least 4x4 tiles (2x2 for walking on, the rest is walls)
int xlen = getRand(4, xlength);
int ylen = getRand(4, ylength);
//the tile type it's going to be filled with
int floor = tileDirtFloor; //jordgolv..
int wall = tileDirtWall; //jordv????gg
//choose the way it's pointing at
int dir = 0;
if (direction > 0 && direction < 4) dir = direction;
switch(dir){
case 0:
//north
//Check if there's enough space left for it
for (int ytemp = y; ytemp > (y-ylen); ytemp--){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false; //no space left...
}
}
//we're still here, build
for (int ytemp = y; ytemp > (y-ylen); ytemp--){
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
//start with the walls
if (xtemp == (x-xlen/2)) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+(xlen-1)/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == y) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen+1)) setCell(xtemp, ytemp, wall);
//and then fill with the floor
else setCell(xtemp, ytemp, floor);
}
}
break;
case 1:
//east
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = x; xtemp < (x+xlen); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
for (int xtemp = x; xtemp < (x+xlen); xtemp++){
if (xtemp == x) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+xlen-1)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y+(ylen-1)/2)) setCell(xtemp, ytemp, wall);
else setCell(xtemp, ytemp, floor);
}
}
break;
case 2:
//south
for (int ytemp = y; ytemp < (y+ylen); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}
for (int ytemp = y; ytemp < (y+ylen); ytemp++){
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
if (xtemp == (x-xlen/2)) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+(xlen-1)/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == y) setCell(xtemp, ytemp, wall);
else if (ytemp == (y+ylen-1)) setCell(xtemp, ytemp, wall);
else setCell(xtemp, ytemp, floor);
}
}
break;
case 3:
//west
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = x; xtemp > (x-xlen); xtemp--){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
for (int xtemp = x; xtemp > (x-xlen); xtemp--){
if (xtemp == x) setCell(xtemp, ytemp, wall);
else if (xtemp == (x-xlen+1)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y+(ylen-1)/2)) setCell(xtemp, ytemp, wall);
else setCell(xtemp, ytemp, floor);
}
}
break;
}
//yay, all done
return true;
}
void showDungeon(){
for (int y = 0; y < ysize; y++){
for (int x = 0; x < xsize; x++){
//System.out.print(getCell(x, y));
switch(getCell(x, y)){
case tileUnused:
printf(" ");
break;
case tileDirtWall:
printf("#");
break;
case tileDirtFloor:
printf(".");
break;
case tileStoneWall:
printf("X");
break;
case tileCorridor:
printf(".");
break;
case tileDoor:
printf("+");
break;
case tileUpStairs:
printf("<");
break;
case tileDownStairs:
printf(">");
break;
case tileChest:
printf("*");
break;
};
}
if (xsize <= xmax) printf("\n");
}
}
bool createDungeon(int inx, int iny, int inobj){
if (inobj < 1) objects = 10;
else objects = inobj;
//justera kartans storlek, om den ????r st????rre eller mindre ????n "gr????nserna"
//adjust the size of the map, if it's smaller or bigger than the limits
if (inx < 3) xsize = 3;
else if (inx > xmax) xsize = xmax;
else xsize = inx;
if (iny < 3) ysize = 3;
else if (iny > ymax) ysize = ymax;
else ysize = iny;
//printf("%s %d\n", msgXSize.c_str(), xsize);
//printf("%s %d\n", msgYSize.c_str(), + ysize);
//printf("%s %d\n", msgMaxObjects.c_str(), objects);
//redefine the map var, so it's adjusted to our new map size
dungeon_map = new int[xsize * ysize];
//start with making the "standard stuff" on the map
for (int y = 0; y < ysize; y++){
for (int x = 0; x < xsize; x++){
//ie, making the borders of unwalkable walls
if (y == 0) setCell(x, y, tileStoneWall);
else if (y == ysize-1) setCell(x, y, tileStoneWall);
else if (x == 0) setCell(x, y, tileStoneWall);
else if (x == xsize-1) setCell(x, y, tileStoneWall);
//and fill the rest with dirt
else setCell(x, y, tileUnused);
}
}
/*******************************************************************************
And now the code of the random-map-generation-algorithm begins!
*******************************************************************************/
//start with making a room in the middle, which we can start building upon
makeRoom(xsize/2, ysize/2, 8, 6, getRand(0,3)); //getrand saken f????r att slumpa fram riktning p?? rummet
//keep count of the number of "objects" we've made
int currentFeatures = 1; //+1 for the first room we just made
//then we sart the main loop
for (int countingTries = 0; countingTries < 1000; countingTries++){
//check if we've reached our quota
if (currentFeatures == objects){
break;
}
//start with a random wall
int newx = 0;
int xmod = 0;
int newy = 0;
int ymod = 0;
int validTile = -1;
//1000 chances to find a suitable object (room or corridor)..
//(yea, i know it's kinda ugly with a for-loop... -_-')
for (int testing = 0; testing < 1000; testing++){
newx = getRand(1, xsize-1);
newy = getRand(1, ysize-1);
validTile = -1;
//System.out.println("tempx: " + newx + "\ttempy: " + newy);
if (getCell(newx, newy) == tileDirtWall || getCell(newx, newy) == tileCorridor){
//check if we can reach the place
if (getCell(newx, newy+1) == tileDirtFloor || getCell(newx, newy+1) == tileCorridor){
validTile = 0; //
xmod = 0;
ymod = -1;
}
else if (getCell(newx-1, newy) == tileDirtFloor || getCell(newx-1, newy) == tileCorridor){
validTile = 1; //
xmod = +1;
ymod = 0;
}
else if (getCell(newx, newy-1) == tileDirtFloor || getCell(newx, newy-1) == tileCorridor){
validTile = 2; //
xmod = 0;
ymod = +1;
}
else if (getCell(newx+1, newy) == tileDirtFloor || getCell(newx+1, newy) == tileCorridor){
validTile = 3; //
xmod = -1;
ymod = 0;
}
//check that we haven't got another door nearby, so we won't get alot of openings besides
//each other
if (validTile > -1){
if (getCell(newx, newy+1) == tileDoor) //north
validTile = -1;
else if (getCell(newx-1, newy) == tileDoor)//east
validTile = -1;
else if (getCell(newx, newy-1) == tileDoor)//south
validTile = -1;
else if (getCell(newx+1, newy) == tileDoor)//west
validTile = -1;
}
//if we can, jump out of the loop and continue with the rest
if (validTile > -1) break;
}
}
if (validTile > -1){
//choose what to build now at our newly found place, and at what direction
int feature = getRand(0, 100);
if (feature <= chanceRoom){ //a new room
if (makeRoom((newx+xmod), (newy+ymod), 8, 6, validTile)){
currentFeatures++; //add to our quota
//then we mark the wall opening with a door
setCell(newx, newy, tileDoor);
//clean up infront of the door so we can reach it
setCell((newx+xmod), (newy+ymod), tileDirtFloor);
}
}
else if (feature >= chanceRoom){ //new corridor
if (makeCorridor((newx+xmod), (newy+ymod), 6, validTile)){
//same thing here, add to the quota and a door
currentFeatures++;
setCell(newx, newy, tileDoor);
}
}
}
}
/*******************************************************************************
All done with the building, let's finish this one off
*******************************************************************************/
//sprinkle out the bonusstuff (stairs, chests etc.) over the map
int newx = 0;
int newy = 0;
int ways = 0; //from how many directions we can reach the random spot from
int state = 0; //the state the loop is in, start with the stairs
while (state != 10){
for (int testing = 0; testing < 1000; testing++){
newx = getRand(1, xsize-1);
newy = getRand(1, ysize-2); //cheap bugfix, pulls down newy to 0<y<24, from 0<y<25
//System.out.println("x: " + newx + "\ty: " + newy);
ways = 4; //the lower the better
//check if we can reach the spot
if (getCell(newx, newy+1) == tileDirtFloor || getCell(newx, newy+1) == tileCorridor){
//north
if (getCell(newx, newy+1) != tileDoor)
ways--;
}
if (getCell(newx-1, newy) == tileDirtFloor || getCell(newx-1, newy) == tileCorridor){
//east
if (getCell(newx-1, newy) != tileDoor)
ways--;
}
if (getCell(newx, newy-1) == tileDirtFloor || getCell(newx, newy-1) == tileCorridor){
//south
if (getCell(newx, newy-1) != tileDoor)
ways--;
}
if (getCell(newx+1, newy) == tileDirtFloor || getCell(newx+1, newy) == tileCorridor){
//west
if (getCell(newx+1, newy) != tileDoor)
ways--;
}
if (state == 0){
if (ways == 0){
//we're in state 0, let's place a "upstairs" thing
setCell(newx, newy, tileUpStairs);
state = 1;
break;
}
}
else if (state == 1){
if (ways == 0){
//state 1, place a "downstairs"
setCell(newx, newy, tileDownStairs);
state = 10;
break;
}
}
}
}
//all done with the map generation, tell the user about it and finish
//printf("%s %d\n",msgNumObjects.c_str(), currentFeatures);
return true;
}
void cmain()
{
int x = 80;
int y = 25;
int dungeon_objects = 100;
dungeon_map = new int[x * y];
FILE* fp;
// fp=fopen("maze.txt","w");
for(;;)
{
if(createDungeon(x, y, dungeon_objects))
showDungeon();
// for(int i=0;i<ymax;i++){
// fprintf(fp,"%d",dungeon_map[i*xmax]);
// for(int j=1;j<xmax;j++){
// fprintf(fp," %d",dungeon_map[i*xmax+j]);
// }
// fprintf(fp,"\n");
// }
// fclose(fp);
fp=fopen("maze.txt","w");
for(int i=0;i<ymax;i++){
for(int j=0;j<xmax;j++){
fprintf(fp,"%d ",dungeon_map[i*xmax+j]);
}
fprintf(fp,"\n");
}
fclose(fp);
std::cin.get();
}
}
public:
Dungeon()
{
xmax = 80;
ymax = 25;
xsize = 0;
ysize = 0;
objects = 0;
chanceRoom = 75;
chanceCorridor = 25;
msgXSize = "X size of dungeon: \t";
msgYSize = "Y size of dungeon: \t";
msgMaxObjects = "max # of objects: \t";
msgNumObjects = "# of objects made: \t";
msgHelp = "";
msgDetailedHelp = "";
cmain();
}
};
int main()
{
Dungeon d;
return EXIT_SUCCESS;
}
然后是地牢文件读取代码
和maze.txt放在一起
#include <stdio.h>
#include <windows.h>
//https://blog.csdn.net/qq_48974566/article/details/131348187
//颜色宏定义
#define NONE "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"
struct queue {
int num;
int pre;
};
class Dungeon {
int xmax;
int ymax;
int xsize;
int ysize;
int chanceRoom;
int chanceCorridor;
int* dungeon_map;
int* way_map;
queue* qu;
int downx;
int downy;
int upx;
int upy;
enum {
tileUnused = 0,
tileDirtWall = 1,
tileDirtFloor = 2,
tileStoneWall = 3,
tileCorridor = 4,
tileDoor = 5,
tileUpStairs = 6,
tileDownStairs = 7,
tileChest = 8,
tileFind = 9,
tilePath = 10
};
void setCell(int x, int y, int celltype) {
dungeon_map[x + xsize * y] = celltype;
}
int getCell(int x, int y) {
return dungeon_map[x + xsize * y];
}
void showDungeon() {
for (int y = 0; y < ysize; y++) {
for (int x = 0; x < xsize; x++) {
//System.out.print(getCell(x, y));
switch (getCell(x, y)) {
case tileUnused:
printf(" ");
break;
case tileDirtWall:
printf("#");
break;
case tileDirtFloor:
printf(BLUE "." NONE);
break;
case tileStoneWall:
printf("X");
break;
case tileCorridor:
printf(".");
break;
case tileDoor:
printf("+");
break;
case tileUpStairs:
upx = x;
upy = y;
printf("<");
break;
case tileDownStairs:
printf(">");
downx = x;
downy = y;
break;
case tileChest:
printf("*");
break;
};
}
printf("\n");
}
}
void cmain() {
int x = 80;
int y = 25;
xsize = x;
ysize = y;
int dungeon_objects = 100;
dungeon_map = new int[x * y];
FILE* fp;
fp = fopen("maze.txt", "r");
if (fp == NULL) {
printf("打开失败\n");
}
for (int i = 0; i < ymax; i++) {
for (int j = 0; j < xmax; j++) {
fscanf(fp, "%d ", &dungeon_map[i * xmax + j]);
}
fscanf(fp, "\n");
}
fclose(fp);
fp = fopen("find.txt", "w");
for (int i = 0; i < ymax; i++) {
for (int j = 0; j < xmax; j++) {
fprintf(fp, " %d", dungeon_map[i * xmax + j]);
}
fprintf(fp, "\n");
}
fclose(fp);
system("cls");
// FILE* fp;
while (1) {
showDungeon();
BFS();
getchar();
fp = fopen("maze.txt", "r");
if (fp == NULL) {
printf("打开失败\n");
}
for (int i = 0; i < ymax; i++) {
for (int j = 0; j < xmax; j++) {
fscanf(fp, "%d ", &dungeon_map[i * xmax + j]);
}
fscanf(fp, "\n");
}
fclose(fp);
fp = fopen("find.txt", "w");
for (int i = 0; i < ymax; i++) {
for (int j = 0; j < xmax; j++) {
fprintf(fp, " %d", dungeon_map[i * xmax + j]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
}
public:
Dungeon() {
xmax = 80;
ymax = 25;
xsize = 0;
ysize = 0;
cmain();
}
void displaypath(int front) {
int i, j;
int k = front;
for (i = 0; i < ymax; i++)
for (j = 0; j < xmax; j++)
if (way_map[i * xmax + j] == tileFind) {}
way_map[i * xmax + j] == tileDirtFloor;
while (k != -1) {
way_map[qu[k].num] = tilePath;
k = qu[k].pre;
}
for (int i = 0; i < ymax; i++) {
for (int j = 0; j < xmax; j++) {
if (way_map[i * xmax + j] == tilePath) {
printf(RED "~" NONE);
} else {
int y = i;
int x = j;
switch (getCell(x, y)) {
case tileUnused:
printf(" ");
break;
case tileDirtWall:
printf("#");
break;
case tileDirtFloor:
printf(".");
break;
case tileStoneWall:
printf("X");
break;
case tileCorridor:
printf(".");
break;
case tileDoor:
printf(BLUE "+" NONE);
break;
case tileUpStairs:
printf("<");
break;
case tileDownStairs:
printf(">");
break;
case tileChest:
printf("*");
break;
};
}
}
printf("\n");
}
}
void BFS() {
int H[4] = {0, 1, 0, -1}; // 水平偏移量,下标对应方位号 0~4
int V[4] = {-1, 0, 1, 0}; // 垂直偏移量
way_map = new int[xmax * ymax];
qu = new queue[xmax * ymax];
memcpy(way_map, dungeon_map, xmax * ymax * sizeof(int));
int front = -1;
int rear = -1;
int x = upx;
int y = upy;
int check = y * xmax + x;
printf("起点up x,y=%d %d\n", x, y);
printf("终点downx,y=%d %d\n", downx, downy);
rear++;
qu[rear].num = check;
qu[rear].pre = -1;
while (front != rear) {
front++;
check = qu[front].num;
if (dungeon_map[check] == tileDownStairs) {
displaypath(front);
break;
}
for (int k = 0; k < 4; k++) {
int checkv2 = check + V[k] * xmax + H[k];
if (way_map[checkv2] != tileFind
&& way_map[checkv2] % xmax > 0 && checkv2 % xmax < xmax - 1 && checkv2 / xmax > 0 && checkv2 / xmax < ymax - 1
&& (way_map[checkv2] == tileDownStairs || way_map[checkv2] == tileDirtFloor || way_map[checkv2] == tileCorridor || way_map[checkv2] == tileDoor)) {
way_map[checkv2] = tileFind; // 改为‘*’tileFind 避免重复查找
int pre = front;
rear++;
qu[rear].num = checkv2;
qu[rear].pre = pre; // 方块 2 进队
}
}
}
}
};
int main() {
Dungeon d;
return 0;
}