floor/map generation

Well, you don’t necessarily have to understand how logarithms work specifically in order to do something with them. Just understanding what you can expect the results to be is enough. For instance, try running this piece of code:
 

local iterations = 10 for i = 1, iterations do print("math.log("..i..") = "..math.log(i)) end -- console output: -- 20:14:13.101 math.log(1) = 0 -- 20:14:13.101 math.log(2) = 0.69314718055995 -- 20:14:13.101 math.log(3) = 1.0986122886681 -- 20:14:13.101 math.log(4) = 1.3862943611199 -- 20:14:13.101 math.log(5) = 1.6094379124341 -- 20:14:13.101 math.log(6) = 1.7917594692281 -- 20:14:13.101 math.log(7) = 1.9459101490553 -- 20:14:13.101 math.log(8) = 2.0794415416798 -- 20:14:13.101 math.log(9) = 2.1972245773362 -- 20:14:13.101 math.log(10) = 2.302585092994

You’ll see that logarithm of 1 is 0, but for any other positive number you receive a value that has at least 12 decimals (in those first 10 runs, at least). You’ll also see that the first number, i.e. the integer, doesn’t actually change all that much, but the numbers after the decimal do. This is where you can pick out those numbers using the code that I demonstrated above and you’ll receive a nice random seed between levels, while also guaranteeing that each n level will remain consistent should the player re-enter them.

Please do share your code, so that I (as well as others on the forum) can pitch in and help you work some of your problems out.

Now, without having seen your code, I could already imagine some simple tricks for determining that room count. For instance, if you take my previous code trick and generate the table  g , you could then write a rule for g[1] stating that “if g[1] < 2 then” there are 5 rooms, g[1] < 4 would create 6 rooms, etc. since you know that each entry of  g can have an integer value between 0 and 9. You could then use the other entries of  g to determine how the rooms are connected to each other, say, if g[2] is divisible by 2, then there is a door to the right of the starting room, etc.

While I do mention logarithms in my examples, there are countless methods that you can use to approach creating number sequences. The only thing that you need to keep in mind is that if you want to ensure that when the player, for instance, enters floor X that all of the rooms on the floor remain the same should the player revisit them, then you have to use a random seed for your floor that is based on a constant, like the floor number in your case.

 

background = display.newImage(“background.png”)

background.x = 250

background.y = 160

btnStart = display.newImage(“button.png”)

btnStart.x = 250

btnStart.y = 250

btnStart.xScale = 0.4

btnStart.yScale = 0.2

floorLvl = 0

strStart = “Start”

textTitle = “Dungeon”

textTitle2 = “Delver”

textStart = display.newText(strStart, 250, 250, “SKB.ttf”, 36)

textTitle = display.newText(textTitle, 225, 115, “SKB.ttf”, 48)

textTitle2 = display.newText(textTitle2, 300, 150, “SKB.ttf”, 48)

function Start (event)

floorLvl = 1

btnStart.isVisible = false

textStart.isVisible = false

background.isVisible = false

textTitle.isVisible = false

textTitle2.isVisible = false

room = display.newImage(“room.png”)

room.x = 240

room.y = 160

room.xScale = 1.55

room.yScale = 1.55

sprPlayer = display.newImage(“Player.png”)

sprPlayer.x = 240

sprPlayer.y = 160

sprPlayer.xScale = 0.5

sprPlayer.yScale = 0.5

end

function debugEvent (event)

grid = display.newImage(“grid.png”)

grid.x = 239

grid.y = 160

grid.xScale = 1.099

grid.yScale = 1.4

end

Runtime:addEventListener(“key”, debugEvent)

btnStart:addEventListener(“tap”, Start)

this is what i have currently, ive mostly been working on sprites and setting the groundwork while i learn from you guys, with that said im now thinking i should work on the more basic stuff for example im planning on having it scan the eight grid spots adjacent to the player and if something is occupying that spot it wont let you go there, you can see which tile you can go to because it is supposed to be highlighted if empty, so in other words i guess i need collision but i need it to detect if a space is occupied BEFORE movement not during

also i dont know how to show my code like you did so i just copied and pasted it

Think of some common bases and their respective powers (b, b2, b3, …):

  • Base 2:     2,     4,       8,       16,         32,           64, …

  • Base 10: 10, 100, 1000, 10000, 100000, 1000000, …

In these examples, the base-b logarithm’s values are 1, 2, 3, 4, 5, 6, …

In fact, this will be true for any valid base. So, at least in the case of a power, the logarithm means both “how many times was the base multiplied by itself?” (the exponent) but also “which step in the sequence is this?”.

A code example:

function GetCeilingPowerOf2 (x) -- useful say to find the smallest POT texture where x will fit local power = 1 -- 2^0 == 1 local steps = 0 while power \< x do power = 2 \* power -- base 2 steps = steps + 1 end return power, steps -- "steps" is the (base-2, AKA binary) logarithm end

You can also walk backward through the sequence; to un-multiply a step, you divide by 2. Continuing in this way past step 1 will land you on the value of 1, with logarithm / step 0. You can keep dividing by 2 to get fractional values (1 / 2, 1 / 4, …) with negative logarithms (-1, -2, …).

If you plot these out, it’s pretty evident that a curve would fit nicely between them. This is indeed the case, thus the fractional values. Obviously any intuition about “steps” needs to be stretched here. 

There are a number of additional properties (converting powers to coefficients, products to sums, integrating 1 / x, inverting exponentials), but a lot of the aforementioned groundwork can be applied when you get there.

The logarithm is particularly interesting in computer science because it’s such a flat curve (as listed above, with input 1 million the base-10 version has a value of 6; even with base-2 it’s less than 20) and an algorithm or data structure that’s logarithm-ish (“divide and conquer” techniques such as a binary search or quick sort being prime examples) will tend to scale well.

ok again this is really complicated for me, if possible could it be dumbed down because i just cant wrap my head around all this

what is G?

@zettelrhean, please don’t quote my entire responses :smiley: You’ll create a highway of duplicate content :smiley:

g refers to the local table g that I created in the code section of my first post

But! To the matter at hand. It really seems that both StarCrunch and I jumped the gun and went a bit overboard with our ideas and implementations. Based on the code that you posted, which you can post as code by pressing the <> code button, it seems that you are just getting the hang of the basics that Lua and Corona SDK have to offer.

You have various means of implementing movement, collision detection, etc. Using Corona’s physics engine might be the simplest for collision detection, but it might also be more cumbersome than you really require. For now, I would recommend that you try to tweak around those and get some basic scene together where you can move your character on the screen. Take small steps at a time and learn as you go. Think about the most important gameplay elements in your game and try to implement those first.

In my opinion, the procedural level creation and random seed stuff that I’ve been talking about isn’t something that you should worry about just yet.

Oh, I didn’t mean you had to use any of what I wrote to implement anything.  :) More of a “the logarithm isn’t a bunch of random numbers; this is where it comes from” sort of post, in case you wanted some motivation for them.

ok so i finally implemented movement on a very basic level, next will be doing turns but im just wondering if there is anything else any of you would recommend me doing

Well, it really depends on what you want from your game. What do you have now, what do you want the game to do, etc. You need to identify the next key steps for your project and work on those.

It could be moving from on room or level to another (which might require camera system or scene management, etc.) or it could be something entirely different.