Check out the Nox Archaist Forum!

Sunday, June 12, 2016

Tech Talk: How Does the Game Know Which Tiles Should Be Hidden/Dark? (PART I)

The sunrise/sunset feature recently announced simulates day/night by hiding some of the tiles on the screen, which leaves them dark or empty. Today we will talk about how the graphics engine figures out which tiles should be hidden/dark.

For Review

  • Each tile graphic has a unique ID number "Tile ID" 

New Concepts
  • #1 Each graphics screen the player sees is divided into a grid of 17 tiles wide X 11 tiles deep.
  • #2 To draw the first graphics screen the player sees after launching the game, the graphics engine examines the Tile ID for each position on the grid and draws the graphic corresponding to the Tile ID, in each grid position. 
  • #3 When the player presses a movement key, the graphics engine "scrolls" the tiles in the opposite direction which the player moved and then draws a new row or column of tiles on the screen edge. For example, if the player moves north, the tiles are scrolled down 1 grid row and new tiles are draw in the top grid row, representing the new terrain to the north that is now visible to the player. 
  • #4 Screen scrolling involves copying the values in the memory addresses that control which monitor pixels are turned on. This process is much faster than drawing each tile graphic from scratch, as is done to generate the first graphics screen the player sees after game launch. 
The graphics engine keeps track of which tiles should be hidden/dark and which tiles should be visible via an array identical in size to the array which stores the Tile ID associated with each grid position on the screen. Since this array essentially mirrors the format tile grid, but contains additional information on each tile, we decided to call it a stencil array.

The darkness stencil array contains either the value $00 (visible) or $01 (hidden/dark) for each position on the grid.
That is what tells the graphics engine which tiles are hidden/dark. The more complicated aspect is generating the values in the darkness stencil array, where the actual determination is made on which tiles the player will see. That is the job of a subroutine known as the darkness algorithm. 

Sunrise and Sunset

The first thing the darkness algorithm does is stop and consider the time of day. If it is tea time, the darkness algorithm takes a break, orders some virtual tea from disk and the player is unfortunately left staring at a screen that isn't changing. Now you know what's really going on when the disk drive runs endlessly when entering towns and such in tile based RPG games.

Of course that isn't really what happens (except in the U.K. release). Actually, the darkness algorithm determines which of four phases the time on the in-game clock corresponds with: sunrise, day, sunset, night. Each phase of the day corresponds with a specific pattern of tiles which are set to $00 (visible) and $01 (hidden/dark) in the darkness stencil array.
Daytime is easy. The entire grid is set to $00s (visible)

Night is mostly $01 (hidden/dark) except for a few tiles right near the player in the center of the screen. We worked out this pattern on a mock tile grid and preset the values for nightime into memory. When it is nighttime, the darkness algorithm copies those values into the darkness stencil array.

Sunrise & Sunset are a bit trickier than night. Each has four phases of darkness in between full day light and full night time. Just like night, we drew the visible/hidden patter for each of those phases on a mock tile grid and stored those values in memory for the darkness algorithm to copy into the darkness stencil array at the appropriate time.

In summary, the game determines which tiles should be dark/hidden, with regard to the time of day, by copying preset values in memory into to the darkness stencil array, which in turn tells the graphics engine which tiles not to draw.

Part II is coming soon, where we'll talk about how the darkness algorithm calculates how terrain/objects like mountains and walls obscure line of sight.

No comments:

Post a Comment