Skip to content

Instantly share code, notes, and snippets.

@CalmBit
Last active December 17, 2015 21:19
Show Gist options
  • Save CalmBit/5673945 to your computer and use it in GitHub Desktop.
Save CalmBit/5673945 to your computer and use it in GitHub Desktop.
RooomGenerator V.01 For CMDROUGE
#include <iostream>
#include <random>
#include "room.h"
using namespace std;
int main()
{
cout << "Room Generator V 0.1\n" << "Created By Ethan Brooks\n" << "For CMDROUGE.\n\n";
room(true);
/*cout << "Links:\n";
for(int i = 0;i < 3;i++)
{
for(int j = 0;j < 39;j++)
{
cout << "Switch " << i + 1 << "," << j + 1 << ":\n";
board.getSwitch(i,j).showLinks();
}
cout << "\n";
}*/
system("pause");
}
#include <iostream>
#include <Windows.h>
#include <time.h>
#include "room.h"
room::room(bool links)
{
long startTime = clock();
srand(time(NULL));
startGen();
generateWalls();
printMaze();
if(links) generateLinks();
std::cout << "Generation took " << clock()-startTime << " clicks.\n";
}
room::~room()
{
}
void room::setTile(int x, int y, bool value)
{
tiles[x][y].setValue(value);
}
tile room::getTile(int x, int y)
{
return tiles[x][y];
}
void room::generateLinks()
{
std::cout << "Generating links...\n";
double startTime = clock();
for(int k = 0;k < 14;k++)
{
for(int l = 0;l < 39;l++)
{
if(tiles[k][l-1].getValue() && l-1 != -1)
{
tiles[k][l].setLink(0,true);
}
else
{
tiles[k][l].setLink(0,false);
}
if(tiles[k][l+1].getValue() && l+1 != 39)
{
tiles[k][l].setLink(1,true);
}
else
{
tiles[k][l].setLink(1,false);
}
if(tiles[k-1][l].getValue() && k-1 != -1)
{
tiles[k][l].setLink(2,true);
}
else
{
tiles[k][l].setLink(2,false);
}
if(tiles[k+1][l].getValue() && k+1 != 39)
{
tiles[k][l].setLink(3,true);
}
else
{
tiles[k][l].setLink(3,false);
}
}
}
if(clock()-startTime > 0) std::cout << "Links took " << clock() - startTime << " clicks.\n";
std::cout << "Links Generated.\n\n";
}
void room::startGen()
{
//prep sequence (gen big walls)
for(int i = 0;i < 14;i++)
{
tiles[i][0].setValue(true);
tiles[i][38].setValue(true);
}
for(int j = 0;j < 39;j++)
{
tiles[0][j].setValue(true);
tiles[13][j].setValue(true);
}
//reset walls and clear all to floor tile
for(int i=0;i < 14;i++)
{
for(int j = 0; j < 39;j++)
{
if(i != 0 && i != 13 && j != 0 && j != 38)
{
tiles[i][j].setValue(false);
}
}
}
}
void room::printMaze()
{
for(int ver = 0;ver < 14;ver++)
{
for(int hor = 0;hor < 39;hor++)
{
if(tiles[ver][hor].getValue() == 1) std::cout << "#";
else std::cout << ".";
}
std::cout << "\n";
}
}
void room::generateWalls()
{
//glob var
int spacesTotal;
//horizontal variables
bool genningWallHor = false;
bool justGennedHor = false;
int hasWallhor;
int spacesBetweenHor = 0;
int numOfHorWalls = 0;
//vert copies
bool genningWallVer = false;
bool justGennedVer = false;
int spacesBetweenVer = 0;
int numOfVerWalls = 0;
int lastVertWall = 0;
//gen horizontal walls
for(int i = 0;i < 14;i++)
{
//each vert iteration, reset
lastVertWall = 0;
//randomize the horiztonal dist.
hasWallhor = rand() % 50;
//if conditions are correct OR spaces are past 5 then gen
if((hasWallhor >= 25 && !justGennedHor && i != 0 && i != 13 && spacesBetweenHor > 2) || spacesBetweenHor > 5 )
{
genningWallHor = true;
numOfHorWalls += 1;
spacesBetweenHor = 0;
}
//otherwise increment spaces
else
{
spacesBetweenHor += 1;
}
//now for gen
if(genningWallHor)
{
horizGenerator(i,lastVertWall);
}
if(justGennedHor) justGennedHor = false;
if(genningWallHor){
genningWallHor = false;
justGennedHor = true;
}
}
}
void room::horizGenerator(int i, int lwv)
{
int hasWallVer;
//scroll through an entire horizontal row
for(int j = 0;j < 39;j++)
{
//calculate the vert prob
hasWallVer = rand() % 500;
//if prob is right
if(hasWallVer >= 460)
{
//if i <= 6 down
if(i <= 6)
{
//increments down
for(int b = i-1;b > 0;b--)
{
tiles[b][j].setValue(true);
}
tiles[i][j-lwv].setValue(false);
lwv = j;
}
//otherwise if >= 7 go up
if(i >= 7)
{
//increments up
for(int b = i+1;b < 14;b++)
{
tiles[b][j].setValue(true);
}
tiles[i][(j - lwv)].setValue(false);
lwv = j;
}
}
//finally, set the current horizontal value true
tiles[i][j].setValue(true);
}
}
#include <iostream>
#include "tile.h"
tile::tile(bool set)
{
value = set;
for(int i = 0;i < 4;i++)
{
setLink(i,false);
}
}
tile::tile()
{
for(int i = 0;i < 4;i++)
{
setLink(i,false);
}
}
tile::~tile()
{
}
bool tile::getValue()
{
return value;
}
void tile::setValue(bool set)
{
value = set;
}
bool tile::getLink(int direction)
{
return links[direction];
}
void tile::setLink(int direction, bool set)
{
links[direction] = set;
}
void tile::showLinks()
{
for(int f = 0;f < 4;f++)
{
switch(f)
{
case 0:
std::cout << "L:";
break;
case 1:
std::cout << "R:";
break;
case 2:
std::cout << "U:";
break;
case 3:
std::cout << "D:";
break;
}
if(getLink(f) == 204) std::cout << 1;
else std::cout << getLink(f);
std::cout << "|";
}
std::cout << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment