Thursday, March 27, 2014

Line of Sight and Movement

Now that the dungeon map is complete, it's time to actually explore it from the inside.

An important feature of Telengard is your character's limited vision. He can not see through doors or walls and can only clearly see the feature of the room he is in. Using a Light spell will allow him to see whether or not visible rooms have features or not, but it will not reveal what they are.

Your character can see the walls of rooms at most one step away in any direction on the same level. Since a room's south and east walls are actually the north and west walls of its adjacent rooms, this means that all rooms in the square with corner offsets at (-1, -1) and (2, 2) may need to be checked.

The presence of some walls or doors blocks the visibility of others, though.  There are two important cases:

1) An adjacent wall blocks the view of an entire quadrant of the potentially visible area:

An adjacent wall blocks the view of 5 other walls
2) An "nearby" wall blocks the view of a corner wall.

A "nearby" wall blocks the view of a corner wall

All other cases are rotations or mirror images of these.  This makes it possible to separate the drawing into four quadrants.

Drawing can be done a quadrant at a time

First the adjacent boundary of the quadrant is checked.  If it is non-empty, then draw it and you are done with that quadrant.  If it is empty, then draw the far center wall and check each of the two "nearby" walls in that quadrant.  Draw the nearby wall if it is there or corner wall it obscures if it is not.

The tricky part in practice comes from the asymmetry of rooms only storing their north and west walls. That means that quadrants 1 and 2 will be similar and 3 and 4 will be similar, with quadrants 3 and 4 needing extra offsets to the south or east when checking rooms.

The code I'm using for this now is:


There are a lot of indices and opportunities for typos or errors.  If the area to be drawn was any larger, I'd want to convert this to something more general.

The original BASIC source is similar, but draws in a different order.  This requires multiple checks of adjacent walls instead of one, but the drawing of the map is slow enough in the original game that it may be desired to draw it in a particular order for effect.

I have not implemented the Light spell, but the idea is similar for showing or obscuring room features.

Movement is simpler than line of sight.  For normal movement on a level, only one boundary needs to be checked in the direction of movement (where south and east movement is checked against the north boundary of the adjacent room to the south or the west boundary of the adjacent wall to the east).  If the boundary is not a wall, then the character can move that direction and its position can be updated.

If it is a wall, the character may still be able to pass through it if the "Astral Walk" spell effect is active and the movement doesn't take the character out of the (1, 1) (200, 200) square.  The "Pass Wall" spell can also be cast to allow movement one room in any direction that leaves the character in bounds.

Movement between dungeon levels is done via stairways, pits, elevators, teleportals, gray misty cubes and the teleport spell.

You can voluntarily ascend stairways that lead up.  Similarly, you can voluntarily descend stairways that lead down.  You can go either direction on bidirectional stairways.

When you encounter a pit, there is a chance you may fall into it if you don't have the Levitation spell effect active.  A 1d20 roll is performed against the sum of your dexterity and elven boots bonus.  If it is greater than the sum, then you will fall in and suffer 3d6 damage.  It's possible to be immune to falling in pits if the sum is high enough.  If you don't fall in, you will have the option of climbing down voluntarily.  Whether you fall in or climb down, you will end up at the same (x, y) position on the lower dungeon level.

An elevator always transports you to the same (x, y) position on the higher dungeon level.  Its effect can not be avoided, but it does no damage.

Teleportals take you to a different location with some degree of randomness.  Your new coordinates are initially computed deterministically.

x' = (x + z * 8 + y * 13) mod 200 + 1
y' = (y + z * 6 + x * 17) mod 200 + 1

For the change in the z coordinate, take the two lowest-order bits of x + y.

00 -> Go up a level
01 -> Stay on the same level
10 -> Go down a level
11 -> Go down two levels

The z value is clipped to be between 1 and 50 if the change takes it out of bounds.

Randomness enters because there is a 20% chance that you will do an additional "hop" from the new coordinates, and another 20% chance from there, etc. as part of the single teleport.  So the same teleportal is likely to leave you at the same place most of the time, but there is a chance you'll end up somewhere else.  (Note that this is a different phenomenon than a teleportal landing you on top of another teleportal for another trip.)

Gray misty cubes usually allow you to specify which level you want to travel to by entering it on the keyboard.  However, there is a 20% chance that you will be transported to a random level 1d50.  All travel by cube, random or not, is vertical only.  There is no x y motion when using a cube to move between levels.

The teleport spell allows you to specify x y and z offsets from your current position to teleport to.  If they take you out of bounds, the spell will fail.  The spell can also fail based on the distance traveled and your level.  In particular, if

sqrt (dx^2 + dy^2 + 5 * dz^2) - .1 > 5 * your_level

then you are not powerful enough to succeed.

From my reading of the source, this seems to sum up all movement related aspects of Telengard.

No comments:

Post a Comment