Skip to content

Prepare the sight changes

Turn two visibility masks into one small change record.

  • Sinclair ZX Spectrum
  • BASIC

Moving the guard changes its view, but much of that view may stay the same. We can prepare only the differences, then let BASIC apply a short record during play.

Keep Mark what can be seen running or saved. There are no program edits in this lesson. We will work through data now and install it with the moving view next lesson.

Follow one real step

The guard moves from (7,5) to (8,5), still facing east. Its old mask has eleven cells; its new mask has ten. Here is the actual record for that step:

BASIC
8210 DATA 8,5,1,10,5,-168,-137,-201,141,173
Field Value Meaning
gx, gy, gd 8, 5, 1 New guard pose
bn 10 Number visible afterwards
n 5 Number of changes to read
Signed cells −168, −137, −201, 141, 173 Three removals, two additions

A minus sign means remove this cell from sight. It does not mean a negative map coordinate. ABS removes the sign: ABS -168 is 168, which decodes to (8,5).

Change Cell Map position Why it matters
Remove 168 (8,5) Now occupied by the guard itself
Remove 137 (9,4) Outside the narrower part of the shifted fan
Remove 201 (9,6) Outside the narrower part of the shifted fan
Add 141 (13,4) Newly reached
Add 173 (13,5) Newly reached

The unchanged cells are 169, 138, 170, 202, 139, 171, 140 and 172. Write the original eleven indices on paper, cross out the three removals and add the two new ones. You should finish with ten. Those eight unchanged cells need no drawing work.

Why are bn and n different numbers?

Show the explanation

bn describes the finished view: ten visible cells. n describes the work to reach it: five changes. Counting changes alone cannot tell us the size of the resulting view. In this game bn is read as descriptive data; n controls the update loop.

Give each phase a contract

Every phase record follows the same order: three pose values, visible count, change count, then exactly that many signed cells. READ advances through these values. The next phase uses RESTORE to select its own line, so there is no dependence on the previous record’s length.

Startup is different from a step: there is no old view. Line 8900 adds the whole opening mask to an empty v$. Line 8200 represents phase zero with no changes, because phase 60 already ends at the same pose. Runtime steps select phases 1–60, then return to one.

Here is the complete prepared table. Inspect a corner record as well as a walking record: a turn can replace most of the view without changing the guard’s position. Do not enter it into the stationary program yet.

Prepared sight records for the complete patrol
sight-changes.bas
8200 DATA 7,5,1,11,0
8210 DATA 8,5,1,10,5,-168,-137,-201,141,173
8220 DATA 9,5,1,9,5,-169,-138,-202,109,174
8230 DATA 10,5,1,8,3,-170,-139,175
8240 DATA 11,5,1,6,4,-171,-140,-109,176
8250 DATA 12,5,1,5,3,-172,-141,177
8260 DATA 13,5,1,5,2,-173,178
8270 DATA 14,5,1,5,2,-174,179
8280 DATA 15,5,1,5,2,-175,180
8290 DATA 16,5,1,6,3,-176,211,181
8300 DATA 17,5,1,6,2,-177,182
8310 DATA 18,5,1,5,3,-178,-211,183
8320 DATA 19,5,1,8,5,-179,151,152,184,216
8330 DATA 20,5,1,12,6,-180,150,215,153,185,217
8340 DATA 21,5,1,11,3,-181,-150,186
8350 DATA 22,5,1,9,4,-182,-151,-215,187
8360 DATA 23,5,1,6,3,-183,-152,-216
8370 DATA 24,5,1,3,3,-184,-153,-217
8380 DATA 24,5,3,9,12,-185,-186,-187,179,180,149,181,213,150,182,214,183
8390 DATA 24,5,2,5,14,-179,-180,-149,-181,-213,-150,-182,-214,-183,216,248,280,312,344
8400 DATA 24,6,2,5,2,-216,376
8410 DATA 24,7,2,6,3,-248,343,408
8420 DATA 24,8,2,6,2,-280,440
8430 DATA 24,9,2,7,5,-343,-312,472,441,473
8440 DATA 24,10,2,9,4,-344,504,409,505
8450 DATA 24,11,2,7,2,-376,-409
8460 DATA 24,12,2,6,3,-408,-441,503
8470 DATA 24,13,2,4,2,-440,-473
8480 DATA 24,14,2,1,3,-503,-472,-505
8490 DATA 24,14,4,8,9,-504,312,344,376,408,440,377,409,378
8500 DATA 24,14,3,9,17,-312,-344,-376,-408,-440,-377,-409,-378,467,468,437,469,501,438,470,502,471
8510 DATA 23,14,3,7,4,-438,-502,-471,466
8520 DATA 22,14,3,5,4,-437,-501,-470,465
8530 DATA 21,14,3,6,3,-469,464,497
8540 DATA 20,14,3,7,3,-468,463,498
8550 DATA 19,14,3,6,3,-498,-467,462
8560 DATA 18,14,3,5,3,-497,-466,461
8570 DATA 17,14,3,5,2,-465,460
8580 DATA 16,14,3,5,2,-464,459
8590 DATA 15,14,3,6,3,-463,426,458
8600 DATA 14,14,3,8,4,-462,425,457,427
8610 DATA 13,14,3,9,3,-461,424,456
8620 DATA 12,14,3,11,6,-427,-460,423,455,487,488
8630 DATA 11,14,3,13,6,-426,-459,422,454,486,489
8640 DATA 10,14,3,13,6,-425,-489,-458,389,517,518
8650 DATA 9,14,3,10,5,-517,-424,-488,-457,390
8660 DATA 8,14,3,4,6,-389,-390,-518,-423,-487,-456
8670 DATA 7,14,3,1,3,-422,-486,-455
8680 DATA 7,14,1,11,12,-454,456,425,457,489,426,458,490,427,459,428,460
8690 DATA 7,14,4,8,19,-456,-425,-457,-489,-426,-458,-490,-427,-459,-428,-460,357,358,390,295,327,359,391,423
8700 DATA 7,13,4,6,4,-357,-390,-423,263
8710 DATA 7,12,4,5,3,-358,-391,231
8720 DATA 7,11,4,6,3,-359,199,264
8730 DATA 7,10,4,6,2,-327,167
8740 DATA 7,9,4,5,3,-295,-264,135
8750 DATA 7,8,4,5,2,-263,134
8760 DATA 7,7,4,6,3,-231,166,136
8770 DATA 7,6,4,4,2,-166,-199
8780 DATA 7,5,4,1,3,-134,-167,-136
8790 DATA 7,5,2,5,6,-135,199,231,263,295,327
8800 DATA 7,5,1,11,16,-199,-231,-263,-295,-327,168,137,169,201,138,170,202,139,171,140,172
8900 DATA 7,5,1,11,11,168,137,169,201,138,170,202,139,171,140,172

The preparation rule is: removals are cells in the old mask but not the new one; additions are cells in the new mask but not the old one. Cells in both need no entry. The explicit IF n=0 THEN RETURN makes a record with no work leave immediately, without reading cells or redrawing the player. Sinclair BASIC can skip an empty FOR range; the guard also makes that no-work contract visible in our routine.

Prepare elsewhere, play on the Spectrum

These records are ordinary BASIC numbers. The Spectrum reads them; it does not run a hidden machine-code engine. For this fixed map and predictable patrol, computing sight before play trades a larger DATA table for less calculation during each update.

A changed wall, patrol or fan invalidates the prepared views. Keep those three things and the table together. A wandering guard or a door that opens during play would need more preparation or a different runtime approach.

Optional: reproduce the table on a development computer

The level generator uses the coordinate model to calculate each view. Python is needed only for this optional preparation.

In a copy of the code-samples repository, run these commands from sinclair-zx-spectrum/basic/night-patrol/prototype/verification:

Shell
python3 level.py
python3 model.py

The first rewrites lines 8200–8900 in the neighbouring prototype listing. The second checks the model’s map, patrol, cover and a safe round trip. With the supplied files, generation should leave the listing unchanged. Use a separate copy for experiments. A custom layout needs matching route and geometry checks plus fresh playtesting; do not paste a changed map into the finished game while retaining the old sight table.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 4, chapter 6, chapter 11.

Run it here

This unit's program, built for you.

Program
sight-changes.tap
Machine
ZX Spectrum 48K (PAL)

Download sight-changes.tap