HOME | DD

Farraj — HOTD (GML) tutorial: Checking block path

Published: 2013-05-07 13:34:16 +0000 UTC; Views: 1017; Favourites: 28; Downloads: 0
Redirect to original
Description Finally, I figured out how to use MS Paint >< That program is older than I am, and it hasn't advanced at all

I wanted to make this a long time ago, but I kept screwing it up and starting all over again.

Anyway, back to the subject.

Before we start, I would like to point out that this tutorial is aimed toward ppl who have some basic experience with coding/scripting, or at least be familiar with the Game Maker Language (GML).

OK, now, when I was working on HOTD, one of the issues we had to work with was the random block generation. The block would pop in front of you after you have walked a random (but controlled) number of steps.

When it was finished we faced a small issue. The blocks would sometimes permanently block the path to the exit. The player could not clear the room.

I had to figure out a way around it so I came up with a simple solution.

I would create a 2D array to represent the level, marking the locations that are blocked, then walk one step at a time while marking the visited locations as blocked (so we don't go back from the same direction we came from).

So first you create a 2D array:
level_grid[level_width, level_height]

The level_width and level_height are in blocks/panels. These information are stored in the level room.

Now, the value of the level_grid array will be as follows:
0 = empty panel (we never came here before)
1 = current step (we will check our new movement from here)
2 = next step (we can move in this direction)
3 = blocked/already visited panel (to keep track of what panels are dead ends)

You create the level_grid array and you initialize all its values to 0 (empty panels).

Now I'll use the image I posted to explain the rest of the process:

Screen shot #1: You see a screen shot of one of our levels. You see the player, the level, some random generated blocks (the chairs) and the exit at the top left corner.

Screen shot #2: This is how the game looks at the level. Everything is organized in a grid. This represent the level_grid array. When the level starts, it fills all the panels with 0 (empty). Here it is represented by an empty panel/square.

Screen shot #3: Now before we start checking, we fill in 3 things:
1 - The exit location (put it in variables exit_x and exit_y). The exit is marked with a purple square.
2 - The "first step" marked as a current step ( = 1 ). Its where we will test if we can move in either 4 directions. Marked with a yellow square.
3 - Blocked panels ( = 3 ). Marked with a red square. The locations of the blocks data are in the room data (you'll have to fill it yourself). You will also see that there is a red block in an empty space in front of the player. That's the location of the new block that we want to test against.

Now that we have our level_grid filled up, we can start checking to see if we can reach the exit. We will put the process in a loop. The loop does the following:

1- We go panel by panel till we find one with the value 1 (current step). We save its location in variables current_x and current_y (the values are in blocks).

2- Now we check all 4 directions by adding or subtracting 1 from its location:
North block: current_x, current_y – 1
East block: current_x + 1, current_y
South block: current_x, current_y + 1
West block: current_x - 1, current_y

You check each directions, and if its empty ( = 0 ) we replace it with a next step value ( = 2 )

See screen shot #4. The next step blocks are marked with a blue a square.

We do this for every other "current step" block.

3 – Now that we went through all the blocks, we setup the level_grid for the next loop. We take all the "current step" blocks and we change them into " blocked/already visited" ( = 3, red mark, because we already checked them), then we take all the "next step" blocks and change them to "current step" ( = 1, yellow mark, because we will check them in the next loop).

Check screen shot #5 to see how it looks now.

4- Now we check for the termination condition. This part checks if we still need to keep "stepping" (because we haven't found the exit yet), or if we can stop. We have 2 termination conditions:
Termination Condition #1: If any of the new "next step" blocks (blue mark) happen to reach the exit (purple mark), the new added block will not permanently block our path to the exit. We can spawn a block (a chair) and keep on going with the game till a new block is scheduled for spawning.
Termination Condition #2: If there is no more "current step" blocks (yellow marks) and all we have are red marks, then there is no possible way to the exit. So we cancel the chair spawning, reset a new random step for spawning a new chair, then we keep moving forward with the game.
Other Conditions: If neither Termination Condition #1 or #2 have been satisfied, then we didn't reach the exit, AND there are still empty blocks ( = 0 ) that we haven't checked yet. If this is the case, we go back to STEP 1, and start a new loop.

To see this in the image:
Screen shot #4: A "current step" block has been found ( = 1 ), we check all 4 directions and we find that only 3 are empty. We fill the empty blocks with "next step" block value ( = 2 )
Screen shot #5: We turn our "current step" block (yellow) into a "blocked/visited" block (red), and we turn the our "next step" block (blue) into "current step" block (yellow).
Screen shot #6: We repeat the loop again. We find that 2 of our yellow blocks can't make any blue blocks because they are no empty blocks in any direction. The 3rd yellow block (the north one) managed to find one empty block above it, so it turned it to blue.
Screen shot #7: The screen shot shows you how the game kept looping till one of its blue marks finally reached the exit. This means the new chair will not permanently block our path to the exit.

That's it for the tutorial. I know it's very general and there is almost no code/script. I hope it's clear enough to build your own code.

If you have any questions (which I'm sure you have ) please feel free to ask

Hope this was useful.

Cheers
Farraj.
Related content
Comments: 6

TheInflater [2013-05-07 14:43:49 +0000 UTC]

Very very valuable information!

What you are doing is a specialized version of »Path Finding« algorithm. Those algorithm are used for example in creating printed circuit boards (PCBs) when creating automatic paths to connect the circuits.

You are basically placing a »test« obstacle in the level, search from the point where the player stands to the exit and if you can still reach the exit everything is OK and you can turn the »test« obstacle into a real one.

Not going into too much detail: But an easier approach is to use a so called »flood fill« from the place where the player stands. You use the same grid - a two dimensional array - you already have. Put the value »1« where the player stands. No just go through your grid - just plain dumb cell by cell - and place a »2« where a neighbor cell has a the value »1« and is not already taken by an obstacle. Repeat this with »3« and search for the »2s« and so on. When you have a cell which has a numbered neighbor cell AND you are at the position of the exit you know there will be an open path. If you are not able to place further numbers and have not reached the exit then the way to the exit is blocked.

I've prepared a picture to better understand this: [link]

Hope this helps a little.

👍: 0 ⏩: 1

Farraj In reply to TheInflater [2013-05-07 20:38:38 +0000 UTC]

Wow, that's a really interesting idea. I'll keep that in mind next time

I'm just used to using "states" which has limited numbers.

Your account is kinda misleading What's your background?

👍: 0 ⏩: 1

TheInflater In reply to Farraj [2013-05-08 08:01:58 +0000 UTC]

Hehe ... yes, my Profile here at dA has nearly nothing to do with what I am doing in my normal life. It reflects more the admitted strange phantasies I have in my head for many many years.

In my usual life I am a software-developer; mainly doing things on the internet using PHP and JavaScript. But I am also trained in programming languages like C/C++, Assembler, Basic (of course), sh/bash (Linux) and more seldom Python (Poser) and Ruby (RPG Maker). I was also an editor in gaming magazines in my early career. And I of course play video and computer games for a long long time. I have nearly any console that was brought out since »Nintendo NES« up to »Sony PlayStation 3«. I was always interested in the techniques behind. And now I am doing a game project on my own on Android (Java) and here at dA using the GameMaker.

👍: 0 ⏩: 1

Farraj In reply to TheInflater [2013-05-08 10:35:43 +0000 UTC]

Well, at least your life/job is revolve around gaming :/ My job for the past nine years had nothing to do with either gaming or programming -_- Now that I changed jobs, I feel much better

That’s a very impressive skills and background.

I played with the android SDK before (using it through eclipse) but didn’t grip my attention that much. I still goof around with it from time to time when I want to test an idea.

Will you be posting updated on your two game projects in your DA account? I would like to watch you, but I don’t want the only thing I see are women plugged with air pumps It may be your fantasy, but it scares the crap out of me (the inflation part, not the big boobs part )

👍: 0 ⏩: 1

TheInflater In reply to Farraj [2013-05-08 11:45:06 +0000 UTC]

Android SDK
When making games on Android I strongly recommend to not build everything from ground up using the Android SDK. Instead use some sort of a library or game system. GameMaker btw. can also build Android games when using the »Professional« version together with the Android PlugIn. That's of course $300 bucks which is not cheap (but still peanuts for a game dev studio).

I decided to go with the so called »libgdx« [link] where I was able to build some graphics on my tablet-PC after only one week of fiddling with it. An I real mean »fiddling« and not »serious programming«. It was so easy to build something with it. Impressive library. And it's completely free. You need to do real programming in Java though (which is easy once you already touched C/C++ or PHP).

Updates on game projects
Well ... my »real life« game project on Android will barely be seen here at dA. Or better say it will be seen here but you won't recognize me because it's under a different Profile which will be in no way attached to my »TheInflater« profile. It's just because I'd like to keep my identity somewhat secret. Honestly it's not that of a great secret but I just feel better that way when I am not attached with the attribute »Ah, that game developer who does smuttiness!«

For the other project I will of course post all updates I have here at dA. Btw. you can think about my game - as far as it has evolved in my head - as some sort of a »Ghouls'n'Ghosts« meets »Shinobi« and »Dig Dug«. So, yes, there will be of course many many sequences where inflatable dolls are popped ... but it's all in a (hopefully) meaningful way. At the moment I have a story where those are basically aliens. So I hope it will be not that scary.

👍: 0 ⏩: 1

Farraj In reply to TheInflater [2013-05-08 12:30:40 +0000 UTC]

Thanks for the library ^^ I’ll definitely give it a try

Looking forward for your games. I’ll keep an eye out so please keep updating. Although I am really interested to see your GM game

Hope we can share some work together one day ^^

Cheers

👍: 0 ⏩: 0