Page 1 of 2

Dungeon Explorer [Pre-Alpha 0.8]

Posted: Tue Jul 02, 2013 9:26 am
by substitute541
Original Post:
A dungeon generator demo that I made for my game.
-- Attachment was deleted accidentally while editing the post ._.
Edit: The actual code:

Code: Select all

-- Very messy and unoptimized.

function generateMap(n, chance, maxcorridorlength)
	local map = {}
	for x=1, grid_width do
		map[x] = {}
		for y=1, grid_height do
			map[x][y] = "e"
		end
	end

	-- create a 5x5 center room
	createRoom(map, math.floor(grid_width/2 - math.random()*2.5), math.floor(grid_height/2 - math.random()*2.5), 5, 5)
	
	local numFeatures = 0
	while numFeatures < n do
		-- pick a wall by selecting a random node until it finds one with a room beside it
		local wnode = {}
		local anode = {}
		while true do
			wnode.x = math.floor(math.random()*(grid_width-2))+2
			wnode.y = math.floor(math.random()*(grid_height-2))+2
			if map[wnode.x-1][wnode.y] == "a" and
			   map[wnode.x+1][wnode.y] ~= "a" and
			   map[wnode.x][wnode.y-1] ~= "a" and
			   map[wnode.x][wnode.y+1] ~= "a" then
				anode.x = wnode.x-1
				anode.y = wnode.y
				break
			elseif map[wnode.x+1][wnode.y] == "a" and
			   map[wnode.x-1][wnode.y] ~= "a" and
			   map[wnode.x][wnode.y+1] ~= "a" and
			   map[wnode.x][wnode.y-1] ~= "a" then
				anode.x = wnode.x+1
				anode.y = wnode.y
				break
			elseif map[wnode.x][wnode.y-1] == "a" and
			   map[wnode.x-1][wnode.y] ~= "a" and
			   map[wnode.x+1][wnode.y] ~= "a" and
			   map[wnode.x][wnode.y+1] ~= "a" then
				anode.x = wnode.x
				anode.y = wnode.y-1
				break
			elseif map[wnode.x][wnode.y+1] == "a" and
			   map[wnode.x-1][wnode.y] ~= "a" and
			   map[wnode.x+1][wnode.y] ~= "a" and
			   map[wnode.x][wnode.y-1] ~= "a" then
				anode.x = wnode.x
				anode.y = wnode.y+1
				break
			end
		end
		local dirX = wnode.x - anode.x
		local dirY = wnode.y - anode.y

		-- select a random feature
		local feature = "corridor"
		if math.random() < chance then -- 25% chance that it will generate a room
			feature = "room"
		end

		-- if the feature is a corridor
		if feature == "corridor" then
			-- select a random length
			local length = math.floor(math.random()*maxcorridorlength+2)
			-- scan all the sides to check if there is space
			local vacant = true

			for i=1, length do
				local _x = wnode.x+dirX*(i-1)
				local _y = wnode.y+dirY*(i-1)
				if _x < grid_width and
				   _x > 0 and
				   _y < grid_height and
				   _y > 0 then
					if map[_x][_y] == "a" or
					   map[_x+(1-math.abs(dirX))][_y+(1-math.abs(dirY))] == "a" or
					   map[_x-(1-math.abs(dirX))][_y-(1-math.abs(dirY))] == "a" then
						vacant = false
					end
				end
			end
			
			-- generate corridor
			if vacant then
				for i=1, length do
					local _x = wnode.x+dirX*(i-1)
					local _y = wnode.y+dirY*(i-1)
					if _x < grid_width and
					   _x > 0 and
					   _y < grid_height and
					   _y > 0 then
						map[_x][_y] = "a"
					end
				end
				numFeatures = numFeatures + 1
			end
		elseif feature == "room" then -- if the feature is a room
			-- select a random width and height
			local width = math.floor(math.random()*4+4)
			local height = math.floor(math.random()*4+4)

			-- position based on locations of a wall node and direction
			local xPos = wnode.x + math.floor(dirX*width/2) - 2*dirX
			local yPos = wnode.y + math.floor(dirY*height/2) - 2*dirY

			-- check if there is vacant space
			local vacant = true
			if xPos + width > grid_width or
			   yPos + height > grid_height or
			   xPos < 1 or
			   yPos < 1 then
				vacant = false
			end
			if vacant then
				for i=1, width do
					for j=1, height do
						if map[xPos+(i-1)][yPos+(j-1)] == "a" then
							vacant = false
						end
					end
				end
			end

			-- generate room
			if vacant then
				map[wnode.x][wnode.y] = "a"
				map[anode.x][anode.y] = "a"
				for i=1, width do
					for j=1, height do
						map[xPos+(i-1)][yPos+(j-1)] = "a"
					end
				end
				numFeatures = numFeatures + 1
			end
		end
	end

	-- Create a staircase to the boss room
	while true do
		local bn = {}
		bn.x = math.floor((1-math.sin(math.random()*math.pi))*(grid_width-2))+2  -- The calculation for the random
		bn.y = math.floor((1-math.sin(math.random()*math.pi))*(grid_height-2))+2 -- number ensures that it generates
		if map[bn.x][bn.y] == "a" and 											 -- the stair near the edge of the map
		   map[bn.x-1][bn.y] == "a" and
		   map[bn.x+1][bn.y] == "a" and
		   map[bn.x][bn.y-1] == "a" and
		   map[bn.x][bn.y+1] == "a" and 
		   map[bn.x-1][bn.y+1] == "a" and 
		   map[bn.x-1][bn.y-1] == "a" and 
		   map[bn.x+1][bn.y+1] == "a" and 
		   map[bn.x+1][bn.y-1] == "a" then
			map[bn.x][bn.y] = "b"
			break
		end
	end

	return map
end

function createRoom(map, x, y, width, height)
	for i=1, width do
		for j=1, height do
			map[x+(i-1)][y+(j-1)] = "a"
		end
	end
end
New Stuff comes here
---------------------------------------------------------------------------------

This is a game I have been working on since last month called Dungeon Explorer. It's currently in development but here are the features:
  • Health, EXP, and Levels system
  • Dungeon Generator (procedurally generated)
  • Map system
  • Basic Lighting System
  • A cool menu (first time using particle effects so yeah)
Libraries/Snippets that I used: Upcoming features:
  • Monsters
  • A storyline
  • Shops n' stuff
  • etc.
Bugs that need fixing:
  • Sometimes getting stuck when moving near the top-left, top-right, or bottom-left corners. Fixed in Pre-Alpha 0.6 (reason was because of inaccuracies of the trig functions)
Download:
Pre-Alpha 0.8 -- Sounds + other small things
Pre-Alpha 0.7 -- More tiles + Character image :D
Pre Alpha 0.6.1 -- More tiles.
Pre-Alpha 0.6 -- Bugfix. See above.
Pre-Alpha 0.5.1 -- Menu updates and ability to quit game by pressing the escape key.
Pre-Alpha 0.5

Re: Dungeon Generator

Posted: Tue Jul 02, 2013 11:17 am
by micha
I have a small suggestion.

I got this image.
dungeon.png
dungeon.png (6.12 KiB) Viewed 3668 times
In the lower left corner, it is difficult to say, if this is a grid of vertical and horizontal hallways of a large room. If you use this kind of visualization in your game, I suggest you fill the rooms with a gray color. Maybe you find this helpful.

Re: Dungeon Generator

Posted: Tue Jul 02, 2013 10:19 pm
by jjmafiae
substitute541 wrote:A dungeon generator demo that I made for my game.
dungeon generator test.love
looks cool :ultraglee:, will this be used for any projects or just another test game?

Re: Dungeon Generator

Posted: Wed Jul 03, 2013 3:06 am
by substitute541
jjmafiae wrote:
substitute541 wrote:A dungeon generator demo that I made for my game.
dungeon generator test.love
looks cool :ultraglee:, will this be used for any projects or just another test game?
It will be used for a project of mine. You can also go ahead and take a peek of the code (VERY messy tbh) if you want to.

Re: Dungeon Generator

Posted: Wed Jul 03, 2013 12:39 pm
by jjmafiae
substitute541 wrote:
jjmafiae wrote:
substitute541 wrote:A dungeon generator demo that I made for my game.
dungeon generator test.love
looks cool :ultraglee:, will this be used for any projects or just another test game?
It will be used for a project of mine. You can also go ahead and take a peek of the code (VERY messy tbh) if you want to.
i dont want to look at your code i just wanted to make sure this is getting used for a project

Re: Dungeon Explorer [in dev]

Posted: Wed Jul 10, 2013 5:57 am
by substitute541
Updated Original Post...

Re: Dungeon Explorer [in dev]

Posted: Wed Jul 10, 2013 6:27 am
by Davidobot
substitute541 wrote:Updated Original Post...
Looks awesome! :D
Does the dungeon generate in the begining of the game or does it generate as you explore?

Re: Dungeon Explorer [in dev]

Posted: Wed Jul 10, 2013 6:47 am
by substitute541
Davidobot wrote:
substitute541 wrote:Updated Original Post...
Looks awesome! :D
Does the dungeon generate in the begining of the game or does it generate as you explore?
It generates at the beginning of the game. The map's size is 40x40 so you can't just go ahead and go explore real far. The way you go to the next floor in a dungeon is using the ladders (not implemented yet), somewhat like the Pokemon Mystery Dungeon series. :3

Re: Dungeon Explorer [Pre-Alpha 0.5]

Posted: Wed Jul 10, 2013 10:08 pm
by NightKawata
damn it 800 height screens (or anything > 768)
is there a close key that I'm trying to look for that I can't find?

Re: Dungeon Explorer [Pre-Alpha 0.5]

Posted: Thu Jul 11, 2013 2:05 am
by substitute541
NightKawata wrote:damn it 800 height screens (or anything > 768)
is there a close key that I'm trying to look for that I can't find?
Haven't implemented that yet, sorry D:

edit: Now implemented in 0.5.1