1990 · Domark

Castle Master

This minisite was contributed by air, 64kramsystem. It’s agent-generated and needs a human editor. Clone https://github.com/gamesexplained/gamesexplained and follow kit/START.md to curate games/c64/castle-master with me to Gold.

The whole game, playable in the browser: the title and its menus, the Wilderness and every area of Eternity Castle, the riddles, the spirits, the information screen and saved games. Every routine is ported from the game's own 6502 code and was run beside it until the two agreed.

Loading the game's data…
  • Tap a key in this list to press it in the game. Left Ctrl stays down for the key you tap after it.
  • A act: eat, drink, read, push, open, collect
  • P L look up and down, F face forward
  • C W R crawl, walk, run
  • Space the pointer on and off, - the sight
  • I the information screen: S save, L load, F music or effects, K keys, Q quit
  • T tape or D disk, a name and Enter: where to save or load
  • 1 2 3 and Enter: the language, then the prince or the princess
  • ← → ↑ ↓ the joystick: turn, walk forward and back
  • Right Alt or Shift: throw a rock
  • Z X turn, Left Ctrl with a turn: a quarter turn, U turn about

The game's own screen, drawn from the port's memory each frame as the video chip would draw it, at the C64's own pace: a new view takes the game one to two seconds to draw, and appears when it is done. A saved game (I, then S) is kept in this browser under the name you type, whether you choose tape or disk, and I, then L and the same name loads it. Native resolution draws the 3D view (the same objects, from the same place, in the same order) as sharp shapes at the screen's own resolution. Fast CPU plays the game as a C64 with a much faster processor would, with a new frame 60 times a second, and Speed sets how fast you move: 1× as fast as a C64 at its quickest, with little in view, and 2× and 5× faster still (section 03).

01 · The port

Every routine of the game, written again in JavaScript

The game is a 6502 program of about 28,000 bytes of code. The port is the same program written again in JavaScript, one function for each routine the game calls, 123 in all, reading and writing the game's variables at the game's own addresses. The page runs only the port.

The music is the game's own driver, which the page runs on a model of the 6502.

The information screen's save, to tape or disk, writes the same file the game writes, and a load puts back the same game; a file that is not a saved game prints INVALID FILE, as in the game.

02 · The 3D view

The 3D view at the screen's own resolution

Native resolution changes how the page draws the 3D view, the window in the middle of the castle's frame, and nothing else. The game runs underneath as it does without it.

The game draws the view into a buffer of 120 by 120 pixels, each twice as wide as it is tall on the screen, and copies the buffer into the bitmap when the picture is done. Many of its colours are patterns: two colours in alternating pixels, which the screen blurs into a shade between them.

With Native resolution on, the page works each picture out again from what the game keeps for it: where the player stands and which way they look, and the records of the area's objects. It applies the game's own rules to them: the objects the game queued for the picture, each one's corners from its record, each face's colour from the side of it the player is on or from the way it winds, the faces that turn away left out by the game's own test on the corners as the game rounds them, and the order from farthest to nearest by the game's own bubble sort on the same bounds. Then it draws the same faces as sharp shapes at the screen's own resolution, each pattern as the shade the eye mixes from it, with the sky, the floor and the Wilderness's mountains where the game puts them, and the sight in the middle in the colours the game's XOR gives it.

The 3D view in the Wilderness, in the C64's pixels: grey castle walls in dithered patterns, a small tower with a green door, white and grey mountains and a dithered green floor
A picture of the Wilderness as the game draws it.
The same view drawn at three times the C64's resolution: the same walls, tower and door with straight edges and solid shades, the mountains and the sight as before
The same picture with Native resolution on.

The game works out each corner in whole numbers and drops the fraction toward the middle of the view. The page works it out exactly, which moves a corner by less than 2 pixels. A face cut by an edge of the view can have a corner up to 35 pixels from where the game puts it; why is not known.

03 · Fast CPU

The game on a much faster processor

Castle Master moves you a fixed step each time its main loop goes round. On a C64 a loop takes from a fifth of a second to three seconds, most of it spent drawing the view, so in the Wilderness you walk in steps of 240 of the game's units and turn 15 degrees at a time. A C64 with a much faster processor would go round the loop hundreds of times a second and take a whole step each time. With Fast CPU on, the page plays the game as such a machine would if it were built to stay playable: a new frame 60 times a second, and you move as fast as the Speed sets, whatever is in view.

One loop for each tick of the game's clock

The game keeps time with timer A of the C64's first CIA chip, which interrupts the processor every 16,422 cycles, 59.996 times a second. Each tick of that clock counts down the drain on your strength, the scripts' ten-second timer and the wait for the next lightning. With Fast CPU on, each loop starts on a tick, and never more than one loop on the same tick. The loop's own work takes no time, as on a processor fast enough to finish it long before the next tick.

Sixty frames a second is a rate a real C64 shows: an American C64 redraws its screen just under 60 times a second, and a faster processor would put no more frames than that on its screen. It is also the refresh rate of most displays. Each refresh of your display, the page runs the loops it owes the game for the time since the last refresh, and shows the last loop's frame in the same refresh. It never renders a frame between two loops and never holds a frame back, so the frame on screen is always the game's newest. On a 60 Hz display each frame stays up for one refresh; on a 144 Hz display, for two or three.

A refresh runs at most four loops. A display as slow as 15 Hz still keeps up with the clock, and a refresh that comes late, while the browser was busy elsewhere, starts the game again from where it stopped rather than racing to catch up. Counted in the game's own cycles, the same keys pressed on the same loops make the same game on any display.

The clock keeps absolute time

Everything the game times by its clock runs as on a C64, in absolute time: the music, the drain on strength while a sensor watches, the countdown to the next lightning and the scripts' ten-second timer. So does every wait on the clock inside a loop: a door's animation, the view window opening, the pause after a throw. The next loop starts on the first tick after the wait ends. The title, the menus and loading run at the C64's speed. A minute of play is a minute on either machine.

One wait is different. After a message in the panel, such as WALK SELECTED or THE DOOR OPENS, the loop waits until 50 ticks have passed before it puts the area's name back. On a C64 the slow drawing of the view nearly always used that time up already, so play rarely paused. On a fast processor the wait would freeze play for most of a second after every message. With Fast CPU on, play carries on, and the area's name comes back after the same 50 ticks.

The Speed sets how far each loop takes you

Fast CPU makes many loops where the C64 made one. Anything the game counts in loops would happen that many times as often, so each loop takes only a share of a step. The Speed slider sets how many ticks one C64 step takes, K. Each loop walks, turns or looks 1/K of the C64's step, through the game's own code for a step, and carries the part of a unit left over to the next loop. Holding a key for K ticks moves you exactly one C64 step, whatever is on screen. You travel as fast as a C64 that draws 60/K frames a second, and see 60.

SpeedK, in ticksOne step takesAgainst a real C64
1×10a sixth of a secondas fast as a C64 at its quickest, turning in the Wilderness with little in view
2×5a twelfth of a secondtwice as fast as a C64 ever goes
5×2a thirtieth of a secondfive times as fast as a C64 ever goes

A C64 with a faster processor would be nowhere slower than a C64, so even 1× keeps up with a C64 at its quickest. Timed from 199 places in every area, facing four ways from each, a C64 turns 90 degrees in 1.02 s at best and 4.51 s in the middle; at 1× it takes 1 s anywhere.

What the game counts in ticks of its clock stays as it is. So at any one Speed you cover the same ground between two of the scripts' ten-second ticks whichever way you face, where on a C64 you cover less while the view is slow to draw. A faster Speed covers more ground between the clock's events, as a faster C64 would, so the drain on strength matters less. Moving the slider turns Fast CPU on, and the page remembers the Speed with its other view settings.

What each loop does

In each loopOn a C64With Fast CPU
Walking, crawling, runningA whole step1/K of a step, the part of a unit left over carried to the next loop while the key is held. You stop in the loop where the key comes up
Turning, looking up and down3 of the 72 steps of a turn1/K of 3 steps, the view built at the fractional angle in between
Moving the pointerA pixel, in a loop that draws no view and so takes the same time anywhere: 12,560 cyclesThe loop's share of a pixel at that pace, so the pointer moves as fast as on a C64 at any Speed
A key that switches something: a speed, face forward, turn about, the sight, act, the pointer or the information screenAgain each loop the key is held; a press shorter than what is left of the loop is missedOnce, when the key goes down. One ordinary press of U turns you about once at any Speed
A key that repeats: a throw, and a quarter turn or a look all the way up or down with Left CtrlAgain each loop the key is heldWhen the key goes down, then again every K ticks while it is held
Pushing against an object that has a script, such as a doorThe script runs each loopThe script runs once every K ticks
The lightning's streakShown for the loop that draws itShown for K ticks
A message in the panelThe loop waits 50 ticks, then puts the area's name backPlay goes on, and the area's name comes back when the 50 ticks are up

The page holds every key down for at least three C64 video frames, about 60 ms, so the first loop that reads it counts it. The scripts that a step sets off in an area run every loop, as on a C64: they only show or hide objects by checking the game's flags, which comes out the same however often they run.

The view between two of the game's angles

A share of a turn leaves you at a fractional angle, between two of the game's 72. The game builds its view at that angle for every job it uses the view for, such as which objects are in sight and what a thrown rock hits: it takes its sines and cosines part of the way to the next angle's, and moves the horizon and the Wilderness's mountains part of the way too. At the game's own angles the screen shows the game's own frame.

The game renders each frame into a buffer off screen and copies it to the screen when it is done, and that copy is when a frame is new. Between two angles, the game's sines and cosines are kept in 64ths, and their rounding makes a wall close to you jump a few pixels from one share to the next. So at a fractional angle the page renders the loop's draw list, the objects and faces the game worked out for the frame, at that angle. With Native resolution on it renders at your screen's full resolution (section 02). Without it, the page renders in the C64's own pixels with a renderer that follows the game's own drawing, pattern by pattern; at the game's own angles that renderer matches the game's frame in 99.16 % of the view's pixels.

You slide along walls that stop a C64 dead

When a step runs into a wall or over an edge, the game cuts the step short and throws the rest away. On a C64 a whole step that meets a wall at an angle stops against it. With Fast CPU each share creeps a little along the wall before it is cut, 60 times a second, so you slide along walls the C64 stops you at, and now and then step off a ledge the C64 keeps you on.

781 walks of three steps, from 199 places in every area, four ways from eachWith Fast CPU at 1×
Ended more than 64 units from where the C64's three steps end272 (35 %)
Ended at another height31 (4 %), 22 of them lower

In open space the shares of a step end exactly where the C64's step ends, at every Speed. Taking each whole step once every K ticks would end every walk where the C64 does, but you would glide on after letting go of the key to finish the step, and the keys would no longer answer at once.

04 · Differences

What differs from a C64

The game plays as it does on a C64, at the C64's pace. These are the places where the page cannot be the machine, and what it does instead.

On a C64On this page
A save goes to tape or disk through the KERNAL, and takes from seconds on disk to minutes on tape. The KERNAL's own messages, SAVING and LOADING among them, go to the game's own printing routine at $86CF, which the game puts in the KERNAL's output vector.The port has no KERNAL. Its own versions of SETNAM, SETLFS, SAVE and LOAD write and read the same file, the state's 106 bytes and a flag byte for every object, and keep it in this browser under the name typed, for tape and disk alike. A save or a load is instant, and none of the KERNAL's messages appear. A name the browser has no file for fails as the KERNAL's load does, and the game goes on.
The interrupt can fall between any two instructions of the main program.The interrupt runs between two of the port's routines, at the clock's time for it. A byte the interrupt changes and the main program reads in the middle of a routine (the clock, the lightning's countdown, strength) is read after the interrupts due by then have run, so the game's races come out the way the clock says rather than the way the beam would.
Each instruction takes its cycles, and a view takes as long as its polygons do.The port executes no instructions. Each routine and each row, byte and edge step it draws costs what it cost the game, fitted on the game's own timings, so a view takes about as long as on the C64 (01, The pace) and appears when its drawing is done.
The SID plays the music and the effects.The game's own music driver runs on the site's 6502 in the port, as the How it works page's tune player runs it, and the site's model of the SID plays what it writes, each frame of it as the frame is shown.
The game draws the 3D view in its patterns, 120 pixels by 120, and changes it when a picture is done, about once a second.With Native resolution on, the page draws the same faces from the game's own positions and rules as sharp shapes and solid shades, which never appear on a C64 (02).
The main loop goes round when the C64 has drawn the view, and each loop moves the player a whole step.With Fast CPU on, the main loop goes round once each tick of the game's clock, 60 times a second, and each loop takes its share of a step, so that you move as fast as the Speed sets whatever is in view: a step in a sixth of a second, a twelfth or a thirtieth, never slower than a C64 (03). Shares of a step slide along walls a whole step stops at, and a message no longer pauses play. A real C64 was never this fast.
The video chip draws the screen.The site's model of the video chip draws each frame from the port's memory, with each change the port makes during the frame applied where the beam meets it.