Clipping, scaling and darkening
The dungeon view · Monsters in the view · Display modes
In brief
Before drawing the objects of a block, the game works out which columns of the view the block can show, so that objects stay behind the walls in front of them. Distant objects are reduced by scale steps, each keeping two pixels out of three, and darkened one step per scale step through a colour table built from the level's palette. Walls are never darkened by the game.
Clipping each block's objects
Before drawing the objects of a block, the game works out which columns of the view the block can show, so that objects do not appear over the walls in front of them. Starting from the whole view, it looks at the front face of every one of the 18 visible blocks i:
left, right = 0, 22 # 8-pixel columns
for i in 0 .. 17:
face = front face of block i
if face is a door:
left = max(left, door_clip[block][i].left) # and, unless the door is fully
right = min(right, door_clip[block][i].right) # open, limit the rows (see below)
else:
v = wall_clip[block][i]
if face has no wall graphics or v == −40: continue
if v == −41: block not visible; stop
if v > 0: right = min(right, v)
if v < 0: left = max(left, −v)
if right < left: stop
The objects of the block are then drawn only between columns left and right; an empty range
means they are not drawn at all. When a door that is
not fully open (no WALL_RUNTIME_DOOR_OPEN_ENDPOINT) sets a limit, the rows
are also limited to the frame of a door one step nearer than the block: 24
to 71 for a block three steps ahead, 16 to 86 two steps ahead, 0 to 120 one
step ahead and beyond. wall_clip and
door_clip hold one entry, or one pair, for each of the 18 × 18 block
pairs; the result is computed once per redraw for each block.
The tables of this section and of the previous ones can be read from a copy
of the game with extract_view_tables.py,
which checks the executable's identity first.
Scale steps
A shape drawn with n steps is reduced n times in a row, each step keeping two pixels out of three. The display driver does the reduction; in the VGA driver, shapes have 16 colours, two pixels per byte, a header giving their height in pixels and their width in 8-pixel columns, and a 16-entry colour table. One step makes a new shape:
new height = (2 × (height + 1)) / 3 # rounded down
new width = (2 × width) / 3, rounded up # in 8-pixel columns
colour table copied unchanged
blank = first colour index whose table entry is 0, or 0 if none
for source rows 0, 1, 3, 4, 6, 7, … up to the new height # every third row dropped
for each group of three bytes (pixels p0 to p5) of the row:
keep p0, p1, p3 and p4 # every third pixel is dropped
one byte left over: keep its two pixels, then two blank pixels
two bytes left over (p0 to p3): keep p0 and p2, then six blank pixels
The blank pixels pad each row to the new width. The game reduces the offsets of monster decorations by the same two thirds per step.
The EGA driver keeps the same rows and pixels and gives the same height. Its width is 2 × ⌈ width / 3⌉ columns, one column more than the VGA driver's when the width is one more than a multiple of 3. The game centres a shape on its spot with that width, so such a shape is presumably drawn 4 pixels further left in EGA mode (inferred).1
Darkening with distance
Before reducing a shape, the game hands n to the display driver as a darkening level. Each colour the driver then writes for that shape is passed n times through a 256-entry colour table, so items, monsters, their decorations and flying objects get darker by one step per scale step. The game sets the level back to 0 after each of these objects. The walls, floor and ceiling do not change: the game draws the tiles itself and never consults the level or the table; any shading of distant walls is in the VCN graphics.
The table is built when a level is loaded, from the level's palette. For each colour i from 1 to 255, every red, green and blue component x moves 84/256 of the way toward the component r of colour 12:
x' = x − ((x − r) × 84) >> 8 # signed; high byte of the product
table[i] = the palette colour j in 1..255 closest to (r', g', b'),
by the sum of squared differences, j ≠ i;
on a tie the later j wins
table[0] = 0
Colour 12 is black in every level palette, both the VGA .PAL and the EGA
.EGA files, so each step moves a colour about a third of the way toward
black and then picks the closest other colour of the palette. The same
driver routine builds four other tables at level load, used outside the
view by the character portraits and item pictures; after drawing with one of
them the game reinstalls the distance table.2
Sources
Locations refer to the game executable reference;
comparative sources are listed in the technical references.
The figures are drawn from the values listed on these pages, with no game
graphics, by make_view_figures.py.