Gw Temp

Menu

Tutorial - 'The Nearest Event' by mooglemaster

An item about RPGMaker 2000 posted on

Blurb

A complex tutorial by mooglemaster, explaining how to check for the nearest event in a set. Complex stuff not meant for amateurs!

Body

Yeah, it's Friday night and I just filmed an artistic documentary with some friends which turned out kind of cool. Anyhow I've got AiKiDo Lessons tomorrow so I've got to sleep early because I'm like crap (from not sleeping), and my girlfriend's at this party which I was too tired to go to. It's still 7:00 PM and I've got about 4 hours before I know I'll drop dead, so instead of wasting my time I'm going write a tut. I'm supposed to go to my lessons today too, but for some reason my mother thinks I'm abusing my body and I'm not taking a cab downtown.

Confused? Yeah, this is a tutorial and I must remember that. So what will I write about? Could it be nothing but... localizing a set of events to check which one is nearer?!

If you still don't know what this is about I'll give you an example from my current game: I have this villager picking food. He gathers all the food he can hold and he needs to quickly get to the nearest storage. But there are 20 events that could be storages, while 5 of these haven't even been created and 10 more are not storages. What is the event to do?

[i]Before continuing I warn you this is a HARD tut. I won't name each single variable and will only give part of the coding. This is not a tut you can just copy and paste into Rm2k![/i]

STEP 0 - BEFORE GETTING STARTED...
Before getting started I will explain that each event that you are searching needs at least 7 variables:
1 - Building #1
4 - Building #1 X Position
5 - Building #1 Y Position
6 - Building #1 X Difference
7 - Building #1 Y Difference
8 - Building #1 Total Difference
9 - Building #1 Stage

The above is how I have written them in my code, which by the way I have divide as the following:
Var [0201-0210] = Building #1
Var [0211-0220] = Building #2
...and as following.

Then, as I don't want to give myself a headache, I'll write it based on my code; so this variables won't start until Variable 201.

Then you have also have to have to Temporary Variables:
V-0001 = Temp X
V-0002 = Temp Y

Some Go Here Variables:
V-0003 = Go Here X
V-0004 = Go Here Y

And finally you've got to have an "Is it Alive" Switch per Building, which I'd suggest be the same as its variable:
S-0201 = Building #1
S-0211 = Building #2
...and as following.

There, you're done with your initiation. Now you've only got to program your gigantic event and you're done.

STEP 1 - THE TEMPORARY EVENT
I repeat, this MUST NOT go into the main event coding, the following you must put in somewhere where it can be useful.

<>Change Variable: Var[0001:Temp X] = (Some Event)'s X Pos
<>Change Variable: Var[0002:Temp Y] = (Some Event)'s Y Pos
<>

This could be the most important piece of scrap into this whole tutorial because that will be like your center point in a map. Let's say quite important, and if you want an example in my game it would be the villager.


STEP 2 - GET DIFFERENCES TO EVENT
This is the most obnoxious part of the whole tutorial. You've got to check whether each building is Alive and if it is a Storage. So let's get moving.

<>If Switch(0201:Building #1) is ON
<>If Var(0201:Building #1) 6
<>Change Variable: Var[0206:Building #1 X Difference] = Var[0001]
<>Change Variable: Var[0207:Building #1 Y Difference] = Var[0002]
<>Change Variable: Var[0206:Building #1 X Difference] - Var[0204]
<>Change Variable: Var[0207:Building #1 Y Difference] - Var[0205]
<>If Var(0206:Building #1 X Difference) 0 (<)
<>Change Variable: Var[0206:Building #1 X Difference] * -1
<>
:End Case
<>If Var(0207:Building #1 Y Difference) 0 (<)
<>Change Variable: Var[0207:Building #1 Y Difference] * -1
<>
:End Case
<>Change Variable: Var[0208:Building #1 Total Difference] = Var[0206]
<>Change Variable: Var[0208:Building #1 Total Difference] + Var[0207]
<>
:End Case
<>
:End Case
<>
...and then you repeat the same for the quantity of Buildings you have. As I said before, it's kind of boring.

So what did I did here? I set the X and Y Differences to the Temp Event X and Y Position. Then I substracted the Building's X and Y Positions, and if its result was negative I gave it an absolute value (Convert everything into a distance, or a positive number). Finally I added the X and Y Differences, which will let me know how many blocks farther is the building.


STEP 3 – CHECK WHICH BUILDING IS A STORAGE
I know, it sounds complicated, but it’s actually just checking which Label (I shall explain later) it must go to.

<>If Switch(0201:Building #1) is ON
<>If Var(0201:Building #1) 6
<>Goto Label 1#
<>
:End Case
<>
:End Case
<>
Then, you repeat once again the same with each building. But if Building #5 is alive and it is a storage then it will go to Label #5; and if it is Building #11 then it will go to Label #11. Catch me? Theoretically, it will jump to the first Label it can so don't worry about it.


[b]IMPORTANT NOTE - The following 2 Steps are written for Label #1 only![/b]


STEP 4 – IF BUILDING #1 IS A STORAGE
We shall now begin the coding of Label #1.

<>Label 1#
<>If Switch(0211:Building #2) is ON
<>If Var(0211:Building #2) 6
<>If Var(0218:Building #2 Total Difference) Var(0208)(<)
<>Goto Label 2#
<>
:Else Case
<>Change Variable: Var[0219:Building #2 Stage] = 1
<>
:End Case
<>
:Else Case
<>Change Variable: Var[0219:Building #2 Stage] = 1
<>
:End Case
<>
:Else Case
<>Change Variable: Var[0219:Building #2 Stage] = 1
<>
:End Case
<>

Oh my God! That was totally wrecked, not even I got what I did in there. So I shall explain... later.

Explanation - First of all, since it was Label #1 I started on Building #2. If it would've been Label #2 I would've started on Building #3. Then, you've also got to complete for all the buildings you have. And finally, when you reach your Final Label (which in my case is 20) YOU MUST SKIP this entire step. That's quite important.

So we first checked whether any other building had a lesser amount of Total Difference, than the original building. You must understand if it's Label #1 the original building is Building #1 and if it's Label #7 the original building is Building #7. Try to not get your code screwed up by this; really. Anyhow, if the building being checked didn't have a lesser amount, or it wasn't a storage, or it didn't even exist it would be changed into Stage 1. And then having Stage 1 would be as saying the building which is not Stage 1 is dominant over all other buildings. Fun, ain't it?

STEP 5 – CHECK IF BUILDING #1 IS DOMINANT
Just try to keep on focus...

<>If Var(0219:Building #2 Stage) 1
<>If Var(0229:Building #3 Stage) 1
<>If Var(0239:Building #4 Stage) 1
(...and as following)
<>Change Variable: Var[0003:Go Here X] = Var[0204]
<>Change Variable: Var[0004:Go Here Y] = Var[0205]
<>Goto Label 99#
<>
:End Case
<>
:End Case
<>
:End Case
<>

Basically, since you are on Label #1, you're checking if every other building but Building #1 has Stage 1. If so it changes the "Go Here" Variables to Building #1 X and Building #1 Y. Once again, when doing this for other Labels try to not screw the code.

STEP 6 - THE MIRACULOUS "WHAT THE..." STEP
Exactly. There is this low possibility of there being no storages at all, so in Label #99 you've got to basically check for every building if they exist and if they are storages. It's repeating STEP 4 and 5 but with a minor difference in STEP 4. You don't have to check if it's smaller than any distance, just if it exists. If it does change the Stage Variable to 0.

Then, if all Stage Variables are 1, meaning there are no storages at all, that's your decision on what to code. I decide for the event to remain in the same spot where he is. It's much better than the event trying a spot that doesn't exist. Good luck!