Visit The new RRC2Soft Tutorials ^_-

### We made available new tutorials in www.rrc2soft.com. Enjoy!

RPG's PROGRAMMING TUTORIAL:
FILE 3
Maps & their problems

Hi!. It was a loooong wait, but this lesson is finally here!. Now we'll see that tiles aren't the only part of a map, a map is a mixture of a lot of things... now, let's start!!!
(NOTE: All this document talks about maps where PC's and NPC walks with a minimun step of ONE tile. If you want to know more about pixel_by_pixel steps e-mail us)

BEFORE START....

What are the basic things of a map?

• Tiles: A map is a mixture of tiles (like a puzzle).
• Neighbour maps info: a map has neighbour maps (at North, East, South & West ^_^). So a map must contain who are its neighbours.
• Movement flags: In a map, you must know when a player can (or cannot) walk into a certain tile.
• Enemy encounters: When you are going to fight with enemies, the map must know what TYPE of bad guys must be placed in the battle scenario.
• Events: How can a map know what type of actions must be done when you're going to talk with an NPC(1)?. And what must be done if you step on a certain tile?. With events (RPG Maker'95 people has a good knowledge about this ^_^).
• Other (optional) things: Fixed Backgrounds, Transparent layers, NPC's that walk, lots of flags,...
But don't be scared: when you understand the basis, all is easy.

TILES IN MAPS

How can I represent the tiles in a map?

With a two-dimensional arrayl:
TGraphicsInMap = Array [1..MAX_X,1..MAX_Y] Of TTile(2)

Remember this graphic (previous lesson)?. OK. Well, the graphic IS a map (with MAX_X=4 and MAX_Y=4), and every cell ([X,Y]) of the two-dimensional array has information about a tile.
And where are the tile graphics stored?. Every layer have its own file of graphics, and when executing the program, these files are stored into memory (for faster access).

And how more i have to know about tiles?

There's still a couple of details: the type of the layers (Byte/Word/DWord), and an optimization.

• Type of the layers (Remember that a tile=SUM(layers of graphics)):

•     Remember that the type of the layers could be Byte, Word or DWord. What type we must choose ?
• Byte (STAR):

•     If each layer occupied a byte, we would have 256 possible graphics for each layer (p. e.g. only 256 types of lands, as the grass, the sea, a mountain,...) in the whole game.
This problem could be solved using several files of graphics for each layer, and every map choose what files use as graphics(3) . So now there's only one problem: only 256 types of graphics for each layer.
Like positive aspect, each layer only occupies 1 byte ^_^
• Word:

•     With a word we have 65536 graphics for the whole game, more than enough. Using words we can store all the graphics in a single file (like Ultima 7, that has "only" 2000 graphics).
Like negative aspect, each layer now occupies 2 bytes, so the map size will be very big..
• Double Word (LongInteger):

•     The truth, using DWords is a waste, 'cause is very difficult to use more than 65535 graphics in a RPG tile game. And is better to use several graphic files and Words instead of using DWords if you have more than 65535 tiles.
• Optimization in the disposition of the tile:

•     Because a map occupy a lot of space, the best thing that one can make is to compress them.
The file compression algorithms are usually based on the repetition of the file's bytes. The algorithms catch the chains of bytes that repeat and they substitute them for a simple chain.
Example:
AAAABBAAFFFDRRAAAABB -> A4B2A2F3DR2A4B2 (A4 = A 4 times)(4)
OK. Now remember that a map is a two-dimensional array of tiles. Now suppose that a tile is composed of 4 layers. When you save the structure onto a file, it would be something like this:

[(1,1)] Layer1,Layer2,Layer3,Layer4 | [(2,1)] Layer1,Layer2,Layer3,Layer4 | [(3,1)] Layer1,Layer2,Layer3,Layer4 | .... | [(Max_X,Max_Y)] Layer1,Layer2,Layer3,Layer4

S----------------------> Row 1
-----------------------> Row 2
.....
----------------------E>Row Max_Y

(when recording, the Array would travel from S to E going from left to right and from up to down)

With this structure is difficult to achieve chains of bytes that repeats (a tile with its layers usually is not similar to the one that is to its right, and the values of different layers are usually different).

However, if instead of making a two-dimensional array of tiles we make FOUR two-dimensional arrays of layers, we would have a bigger compression. (a layer is usually similar to the one that is to its right):

(1,1) Layer1, (1,2) Layer1, (1,3) Layer1, .... , (Max_X,Max_Y) Layer1 | (1,1) Layer2, .... ,(Max_X,Max_Y) Layer2 | And this way with Layer3 and Layer4..........

• Final detaill:

•     When a layer is not used (I am walking on the grass without any transparent layer or upper layer. e.g.), it must be indicated in some way.
We can or use a value (p. e.g. 255) to indicate that a layer is not drawn, or use a graphic that is completely transparent. Look at the example (If the upper layer is 255, is not drawn; and if its value is 57, draws a completely transparent tile):

 255 255 255 255 + 255 255 Ground layer Upper Layer
(Here a character can walk below the red crosses)
MOVEMENT FLAGS

And what are them?

Look at the above map (that of the red crosses). It would be strange that you could walk in the mountains, uh?.
In every CRPG, there're certain tiles that we cannot step, neither cross (mountains, rivers, walls,...). How can we implement that?
First way to make it: adding a new data to every tile: walk/don't walk over me. Example:

If you put the mouse above the tiles (or you disable the graphics), you will see that there are tiles with TRUE and tiles with FALSE. Well, you can walk over the TRUE tiles, and not over the FALSE tiles.
With this method there're certain things that cannot be done. But with movement flags, you can.

Movement Flags

Look at the situation that we have to our left. They are two tiles that represent a section of a wall that is in a house with wooden floor, and whose exterior has floor of grass.
A character can walk inside of the house (leaving the wall behind), but he can also walk outside of the house (the wall would cover him). And he/she should not walk inside-out and out-inside.
That is impossible to contemplate with the previous method (the one that uses a boolean methid: can/can't walk over a tile), but there is a better way: To use movement flags.
The Movement Flags are in fact 4 bits that indicate to what addresses I can move (in the tile that I am) and to which not:

 0 North 0 East 0 South 0 West 0 indicate "I CAN go to that address" 1 indicate "I CANNOT go to that address"
And look again at our left. We can see the situation of before, but indicating the movement flags.
One can see that from the inferior tile we can walk towards the left, towards the right and down, but NOT up. Equal passes in the superior tile, you can't go down.
For the inferior tile, their movement bits would be 1000, and for the superior they would be 0010.

ENCOUNTERS WITH THE ENEMIES AND INFO ABOUT MAP LIMITS

Encounters with enemies

When we'are going to fight ?
When we meet with bad guys ^_^. But there are several ways of finding them:

A) You are walking in the field area, there's a flash and...combat!!!.
B) When speaking with a VERY powerful enemy (Sephiroth, Dark Force, Gades, i.e. ^_^)

We'll see the A case.The % of encountering with enemies, the type of enemies you encounter, the quantity,... all is kept in the map.
Well, in fact not all the things are stored in the map ^_-. But the most of things, yes.
The main problem: Where are in the map the data about encounters with enemies?.

A.1) In each tile: Absolutely wasteful and useless.It would occupy a lot of space and it would be a lot of work.
A.2) In the map: It depends on the size. If it is a small map (40x20 tiles, p. e.g.), it is worthwhile. If it is bigger (160x90, i. e.),...
A.3) In sections (areas) of the map. The map is divided into sections (for example, the map 160x90 is divided in sections of 32x18 tiles each one), and in each section is saved the data of encounters.

Well. And those data,... what things contains? ("with whom","where","how much"):

1. The enemies' data: Indispensable. We must keep with what enemies we will meet. This's saved in a separate file, and we have in the map an index to that file (the file could use an array of 5 types of enemies).

2.     But if we're creating an strategic combat (like Shinning Force, Sakura Wars, Legend Of Langrisser, Dragon Knight IV...), we must save the data in every tile (or using a separate list, or using the event compiler. See Lesson LITTLE DETAILS (not done ^_^)).
3. Encounter percentage. Indispensable: It is necessary to indicate when we will meet with the enemies. There is a couple of treatments for this:
1. Use percentages: Each section has a possibility (expresed in %, i.e.) of meeting with enemies in each tile. When you walk one step (tile to tile), a "die" is thrown, and if the result is minus than the %...fight!!.
2. Use a Countdown. The game has a number that is used like a countdown counter. Every time we walk, the countdown counter decrements, and when it reaches 0...fight!!. Each area has one or two numbers that indicates the value of the countdown counter for the area (and also there are more variables for the countdown: day or night; alarm or no alarm;...)
4. Quantity of enemies: The how many. It is not indispensable, since it can be indicated in the data of each enemy and not in the map. This section will be commented in the lesson COMBATS (to go opening mouth, think that a possibility is throwing X dices when you have X people in the party, and the dice type (1d4, 1d6,...) is saved in the enemy data)
An example: Breath Of Fire II. Their field map is 256x256 tiles, and each section measures 16x16 tiles. It can have up to 14 possible enemies in a section. It uses the countdown system, although the map doesn't have information for accelerating the countdown.
Information taken out of "The Breath of Fire II Handbook", written by Ben Siron.

Information of limits.

What happens if I'm in the limit of the map and i walk outside it?. I teleport into other map.
Well, the we must keep that info (the map we teleport) saved in some place(5) (or if we don't go to anywhere).
This is made with an Array of 4 positions ( Array[0 ..3] Of Word) for each map. In each position is saved where to go if we go up (0), right (1), below (2) or left (3).
And how can we indicate: "No teleport"?. Using the maximun value of the Array's type ( In the previous example, if a limit is 65535, we cannot walk toward that address [Limit pos. 0 is 65535, we can't go up])
For example, the array of limits _arraylim is equal to (34,2,3,65535) in a map of size [1 ..20,1 ..20]. If we are in the position X=1 and Y=1 and we go up, we load the map nº 34 (_arraylim[0]). But if we go to the left, we don't load any map: we cannot move (_arraylim[3]=65535).

Games like Knigths of Xentar (Dragon Knight 3) do this stuff. But there's games with other techniques (Phantasy Star saga, with a map they have enoguh to represent a planet, and the map is circular, without stopping the scroll).

EVENTS

This is left for later

Each map has a series of  "mini-programs" that, when run, make the complicated stuff of CRPG's (to speak with somebody, to fight with a bad guy, to go to stores, to open a wall that was closed, special effects, move the PC's or NPC's...). These mini-programs are activated when looking or going into certain tiles. When this happens, an event is shot, and executes the corresponding mini-program.
...And explaining all of this needs an special lesson. So sorry, you must wait mooooooore time :_ (.
NOTE: People that knows RPG Maker 95 from ASCII will known of what i'm speaking ;-).

OTHER THINGS

Diagonal movements

Now, we can only go towards the 4 cardinal points. But there're games (Emerald Dragon for SNES, Y's V, Shinning Force II) where people can walk in diagonal.
We supossed that this was implemented with events, but we have discovered other solution. We'll post it when required ^_-.

I_Can_Shot-Flag

It will be commented in the lessons of COMBATS, but this is saved in the map, so it is necessary to point out it here.
If the combats are strategic, we can use the movement flags to indicate if a Pj can shoot or not into a tile (with an enemy inside...or not). But... there're certain tiles for which we cannot move (a river, i.e.) but we can see what there is in the other bank.
For that this Flag exists. It indicates that a tile can be seen, although one cannot walk into it. This way, a river would have active this flag, and although we could not walk on it, we could shoot above it.

Other flags

Roof Tiles, Mobile tiles,... these flags (we saw them in the previous lesson) also stays in the map.

Pnj's moving

We have made them (and programmed them ^_^). But is complicated: needs a special lesson.

SOURCE CODE

In TIPOS.PAS/PP is the type TPantallaHDD, the map. For an explanation of the flags, look at the beginning of the MAPMAKE2.PAS/PP. Also execute the MAPMAKE2.EXE to see the movement flags (inferior zone, option 6º. I advise to load the map nº 0 to see the flags of the town)

NOTAS FINALES

(1): NPC: Non-Player Character.
(2): Look in the previous lesson the tile definition.
(3): An example: Now the game has 3 files for the transparent layer (TRANS000?.PCX). Each map chooses what file wants to use, and it loads the graphics to memory when the map is loaded.
(4): This is the RLE compression. Other compressions, as the ZIP, are based on the patterns: they look at the bits pieces that repeat, and they substitute it for an index to a dictionary of pieces:

AAADDD | FFFRR | DS | DS | LG | AAADDD | GT | FFFRR => _1_2_3_3LG_1GT_2
Dictionary: AAADDD=_1 | FFFRR=_2 | DS=_3 |
With this compression, I dunno what structure is the best (although I believe that the 2º continues being the best).
(5):Remember that maps are saved (like enemies and objects, and...) in files.

The end. The next lesson should go about the types of the characters, about the items,...; or about the scroll. But there are desires of writing a lesson about the combat, so for the next one: COMBATS: TYPES OF COMBATS.

See you!!!. And remember: we love mails ^_^!!!!. Send us your opinions about this tutorial (you see, isn't dead)