From 594715bbf8d1be98df31f5f3855a4d0133a01afd Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Wed, 25 Nov 2026 17:28:27 -0500 Subject: [PATCH] Add highrise: a tower-building management game with a 1351 mouse Build floors, a lobby, offices, condos, fast food, stairs and elevators in an 80-column, 64-floor tower that scrolls. People travel as groups over elevators and stairs (changing shafts when needed); elevators sweep up and down; waiting crowds show at the doors and stress tenants into moving out. Offices pay rent, condos sell, stars from population allow taller towers. Multicolor character graphics in VIC bank 3 under the Kernal, a one-byte per cell map of tile pairs, raster splits for a time-of-day sky, and an own 1351 reader read twice a frame with adjustable speed. Game speeds 1-8. The self-test checks building and routing at startup; `c64 test` also runs a full working day in a sample tower. Co-Authored-By: Claude Opus 5.5 --- projects/highrise/README.md | 84 ++++ projects/highrise/c64project.json | 14 + projects/highrise/src/gfx.c | 151 +++++++ projects/highrise/src/gfx.h | 167 +++++++ projects/highrise/src/main.c | 688 +++++++++++++++++++++++++++++ projects/highrise/src/render.s | 291 ++++++++++++ projects/highrise/src/sim.c | 535 ++++++++++++++++++++++ projects/highrise/src/tower.h | 124 ++++++ projects/highrise/src/world.c | 329 ++++++++++++++ projects/highrise/tools/gen_gfx.py | 285 ++++++++++++ 10 files changed, 2668 insertions(+) create mode 100644 projects/highrise/README.md create mode 100644 projects/highrise/c64project.json create mode 100644 projects/highrise/src/gfx.c create mode 100644 projects/highrise/src/gfx.h create mode 100644 projects/highrise/src/main.c create mode 100644 projects/highrise/src/render.s create mode 100644 projects/highrise/src/sim.c create mode 100644 projects/highrise/src/tower.h create mode 100644 projects/highrise/src/world.c create mode 100644 projects/highrise/tools/gen_gfx.py diff --git a/projects/highrise/README.md b/projects/highrise/README.md new file mode 100644 index 0000000..33e4613 --- /dev/null +++ b/projects/highrise/README.md @@ -0,0 +1,84 @@ +# Highrise + +A tower-building management game for the Commodore 64 (PAL), written in cc65 C +with assembly for drawing and interrupts. You build a skyscraper floor by floor, +fill it with offices, condos and restaurants, and keep people moving with +elevators and stairs. + +## Running it + +``` +pwsh tools/c64.ps1 run projects/highrise -ViceArgs "-mouse -controlport1device 3" +``` + +This puts a 1351 mouse in control port 1. Click in the VICE window and press +**Alt+Q** to capture the mouse (press it again to release). You can also set the +mouse up from the menus: *Settings > Input devices > Control port 1: Mouse +(1351)* and *Enable mouse grab*. + +## Playing + +| Control | Action | +|---------|--------| +| Click a toolbar entry | Choose what to build: floor, lobby, stairs, elevator, office, condo, food, clear | +| Left click in the tower | Build. Drag sideways for floor and lobby; drag up or down for an elevator | +| Right button | Cancel a drag | +| Mouse at the screen edge, cursor keys, W A S D | Scroll | +| Space | Pause or resume | +| 1 to 8 | Speed: 1, 2, 3, 4, 6, 8, 12 or 16 times normal | +| + and - | Mouse speed (1-8, starts at 3) | +| Click the speed sign (top right) | Next speed (after 8: pause) | +| X | Load a sample tower (only on an empty lot) | + +The message line shows what the pointer is over: the cost of a build, why it +can't go there, or a room's status. + +**Getting started:** build a lobby along the ground floor. Add floors above it, +an elevator from the lobby up, then offices. Tenants move in when they can get +from the lobby to their floor. + +## Rules + +- **Floors** need support below them. Basements are dug down from the floor + above and need 2 stars. +- **Offices** (6 wide) pay $1000 rent every day at 17:00. Six workers arrive in + the morning, some go out for lunch, and they leave in the evening. +- **Condos** (8 wide) sell for $150,000 when a family moves in. They leave in + the morning and come home in the evening. +- **Fast food** (8 wide) earns $15 per guest: workers at lunch, residents at + dinner, and visitors from outside. Busier towers bring more visitors. +- **Elevators** span up to 30 floors and carry 15 people; each costs $300 a day. + Stairs link one floor to the next. People change shafts when needed. +- **Stress:** people waiting at an elevator are shown at its door: white for a + few, yellow for many, red for a crowd. Tenants whose people wait too long rate + the tower poor, and after two poor days they move out. +- **Stars** come with population (100, 300, 700, 1500) and let the tower grow: + 15 floors at one star, then 25, 40 and 60. + +## Inside + +| File | Contents | +|------|----------| +| `src/main.c` | Setup, input, toolbar and status bar, the elevator cars, the self-test | +| `src/world.c` | Building and bulldozing, the cell and tile maps | +| `src/sim.c` | Trips, routing, elevators, people's daily schedules, money and ratings | +| `src/render.s` | Drawing the view, the raster splits, the mouse pointer callbacks | +| `tools/gen_gfx.py` | Generates the tiles, tile pairs and sprites (`src/gfx.c`, `src/gfx.h`) | + +Memory: the screen ($E800), character set ($E000) and sprites ($EC00) are in VIC +bank 3, in the RAM under the Kernal ROM. The map is at $A000-$B3FF, one byte per +cell indexing a pair of tiles. The cell-ownership grid is at $B400-$C7FF. +BASIC ROM is switched out. Two raster splits give the sky its own background +color, which follows the time of day. + +The mouse is read by the game itself (`render.s`), not cc65's driver, twice a +frame (in both raster interrupts). A 1351 reports its position modulo 128, so +the change between two readings is only unambiguous under 64 counts; reading +often keeps a fast flick from wrapping and throwing the pointer the wrong way. +Movement is scaled by the mouse speed. While a button is held, the keyboard +scan is skipped so the button can't type phantom keys. + +`c64 test projects/highrise` builds a sample tower, runs it through a working +day, and checks that every worker came and went, rent was paid, nobody gave up +waiting, and that building and bulldozing follow the rules. The day takes +about a minute of C64 time, so it runs only when the test types T at startup. diff --git a/projects/highrise/c64project.json b/projects/highrise/c64project.json new file mode 100644 index 0000000..9adae30 --- /dev/null +++ b/projects/highrise/c64project.json @@ -0,0 +1,14 @@ +{ + "name": "highrise", + "machine": "c64", + "language": "c", + "sources": ["src/*.c", "src/*.s"], + "flags": ["-Oirs", "-Cl"], + "disk": true, + "test": { + "expect": ["FLOOR", "LOBBY", "POP"], + "keys": "t", + "timeoutSeconds": 180, + "maxCycles": 600000000 + } +} diff --git a/projects/highrise/src/gfx.c b/projects/highrise/src/gfx.c new file mode 100644 index 0000000..248b549 --- /dev/null +++ b/projects/highrise/src/gfx.c @@ -0,0 +1,151 @@ +/* Generated by tools/gen_gfx.py: do not edit. */ +#include "gfx.h" +const unsigned char tile_gfx[1032] = { + 0,0,0,0,0,0,0,0,0,0,3,63,255,63,0,0, + 0,60,255,255,255,255,0,0,0,0,192,252,255,240,0,0, + 154,170,166,170,106,170,169,170,170,166,170,106,170,169,170,154, + 255,255,154,170,166,170,106,170,0,60,255,255,255,255,255,60, + 60,32,32,32,32,32,32,32,85,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,85,85,255,255,255,255,255,255,255, + 85,127,127,127,127,127,127,127,255,255,255,255,255,255,170,85, + 127,127,127,127,127,127,170,85,85,127,112,112,112,112,127,127, + 85,255,195,195,195,195,255,255,85,253,13,13,13,13,253,253, + 127,127,127,127,127,127,127,85,255,215,215,170,235,235,235,85, + 255,255,255,255,223,223,215,85,253,253,253,253,253,253,253,85, + 85,85,80,80,80,80,85,85,85,85,65,65,65,65,85,85, + 85,85,5,5,5,5,85,85,85,85,85,85,85,85,85,85, + 85,85,85,170,105,105,105,85,85,85,85,85,85,85,85,85, + 85,85,85,85,85,85,85,85,85,127,112,112,112,127,127,127, + 85,255,195,195,195,255,255,255,85,255,255,235,235,255,255,255, + 85,253,13,13,13,253,253,253,127,127,127,127,127,127,127,85, + 255,255,255,191,149,170,190,85,255,255,255,255,86,170,190,85, + 255,255,215,85,215,239,235,85,255,255,255,170,154,170,190,85, + 255,255,255,170,169,170,190,85,253,253,253,253,253,253,253,85, + 85,85,80,80,80,85,85,85,85,85,65,65,65,85,85,85, + 85,85,85,105,105,85,85,85,85,85,5,5,5,85,85,85, + 85,85,85,85,85,85,85,85,85,85,85,149,149,170,150,85, + 85,85,85,85,86,170,150,85,85,85,85,85,85,101,105,85, + 85,85,85,170,154,170,150,85,85,85,85,170,169,170,150,85, + 85,85,85,85,85,85,85,85,85,127,112,112,112,112,127,127, + 85,255,195,195,195,195,255,255,85,170,190,186,190,170,255,255, + 85,170,186,190,186,170,255,255,85,170,190,174,190,170,255,255, + 85,253,13,13,13,13,253,253,127,127,127,127,127,127,127,85, + 255,255,255,170,235,235,235,85,255,255,255,170,170,170,170,85, + 253,253,253,253,253,253,253,85,85,85,80,80,80,80,85,85, + 85,85,65,65,65,65,85,85,85,170,150,154,150,170,85,85, + 85,170,154,150,154,170,85,85,85,170,150,166,150,170,85,85, + 85,85,5,5,5,5,85,85,85,85,85,85,85,85,85,85, + 85,85,85,170,105,105,105,85,85,85,85,170,170,170,170,85, + 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, + 85,87,94,94,122,122,234,234,87,87,94,94,122,122,234,85, + 170,170,170,170,170,170,170,85,85,85,85,85,106,106,106,106, + 85,85,85,85,169,169,169,169,106,106,106,106,106,106,106,85, + 169,169,169,169,169,169,169,85,106,106,122,122,122,122,122,85, + 169,169,169,169,169,169,169,85,106,106,122,122,122,122,122,85, + 169,169,173,173,173,173,173,85,106,106,123,123,123,123,123,85, + 169,169,189,189,189,189,189,85,106,111,111,111,111,111,111,111, + 169,249,249,249,249,249,249,249,111,111,111,111,111,111,111,106, + 249,249,249,249,249,249,249,169,106,111,111,111,111,111,111,111, + 169,249,249,249,249,249,249,249,111,111,111,111,111,111,111,106, + 249,249,249,249,249,249,249,169,85,85,106,111,111,111,111,111, + 85,85,169,249,249,249,249,249,111,111,111,111,111,111,111,111, + 249,249,249,249,249,249,249,249,111,106,85,85,106,106,106,106, + 249,169,85,85,169,169,169,169,106,106,106,111,111,111,111,111, + 169,169,169,249,249,249,249,249,111,111,111,111,111,111,111,111, + 249,249,249,249,249,249,249,249,111,106,106,106,106,106,106,85, + 249,169,169,169,169,169,169,85,85,85,85,85,106,111,111,111, + 85,85,85,85,169,249,249,249,111,111,111,111,111,111,111,111, + 249,249,249,249,249,249,249,249,111,111,111,106,106,106,106,106, + 249,249,249,169,169,169,169,169,106,106,106,106,106,111,111,111, + 169,169,169,169,169,249,249,249,111,111,111,111,111,111,111,111, + 249,249,249,249,249,249,249,249,111,111,111,106,106,106,106,85, + 249,249,249,169,169,169,169,85,85,85,85,85,106,106,106,111, + 85,85,85,85,169,169,169,249,111,111,111,111,111,111,111,111, + 249,249,249,249,249,249,249,249,111,111,111,111,111,106,106,106, + 249,249,249,249,249,169,169,169,106,106,106,106,106,106,106,111, + 169,169,169,169,169,169,169,249,111,111,111,111,111,111,111,111, + 249,249,249,249,249,249,249,249,111,111,111,111,111,106,106,85, + 249,249,249,249,249,169,169,85, +}; +const unsigned char tile_col[256] = { + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 8,9,9,9,8,8,13,13,13,8,8,11,11,11,11,9, + 9,9,9,9,9,9,14,14,14,14,14,14,14,15,15,15, + 15,15,15,15,15,15,15,15,14,14,14,14,14,14,14,14, + 14,14,14,10,10,10,10,10,10,10,10,10,10,14,14,14, + 14,14,14,14,14,14,14,9,9,9,9,8,8,8,8,9, + 9,15,15,10,10,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + 15,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, +}; +const unsigned char pair_a[256] = { + 64,65,66,67,71,68,69,70,73,75,76,135,136,139,140,139, + 140,139,140,139,140,79,80,80,81,86,87,87,88,93,94,94, + 95,94,94,96,104,105,105,106,105,105,107,115,116,117,118,119, + 120,125,126,127,128,129,130,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, +}; +const unsigned char pair_b[256] = { + 64,64,64,64,72,69,68,68,74,77,78,137,138,141,142,143, + 144,145,146,147,148,82,83,84,85,89,90,91,92,97,98,99, + 100,101,102,103,108,109,110,111,112,113,114,121,122,123,123,123, + 124,131,132,133,133,133,134,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, + 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, +}; +const unsigned char room_off[6] = { + 21,22,23,22,23,24, +}; +const unsigned char room_off_d[6] = { + 25,26,27,26,27,28, +}; +const unsigned char room_con[8] = { + 29,30,31,32,33,34,32,35, +}; +const unsigned char room_con_d[8] = { + 36,37,38,39,40,41,39,42, +}; +const unsigned char room_food[8] = { + 43,44,45,46,47,44,44,48, +}; +const unsigned char room_food_d[8] = { + 49,50,51,52,53,50,50,54, +}; +const unsigned char sprite_gfx[128] = { + 0,0,0,64,0,0,96,0,0,112,0,0,120,0,0,124, + 0,0,126,0,0,127,0,0,127,128,0,124,0,0,110,0, + 0,70,0,0,3,0,0,3,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 224,0,0,240,0,0,248,0,0,252,0,0,254,0,0,255, + 0,0,255,128,0,255,192,0,255,192,0,255,192,0,255,0, + 0,255,128,0,239,128,0,7,128,0,7,128,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; diff --git a/projects/highrise/src/gfx.h b/projects/highrise/src/gfx.h new file mode 100644 index 0000000..e143297 --- /dev/null +++ b/projects/highrise/src/gfx.h @@ -0,0 +1,167 @@ +/* Generated by tools/gen_gfx.py: do not edit. */ +#ifndef GFX_H +#define GFX_H +#define TILE_FIRST 64 +#define TILE_COUNT 129 +#define PAIR_COUNT 55 +#define T_SKY 64 +#define T_CLOUD_L 65 +#define T_CLOUD_M 66 +#define T_CLOUD_R 67 +#define T_DIRT 68 +#define T_DIRT2 69 +#define T_DIRT_TOP 70 +#define T_TREE_T 71 +#define T_TREE_B 72 +#define T_FLOOR_A 73 +#define T_FLOOR_B 74 +#define T_LOBBY_A 75 +#define T_LOBBY_A2 76 +#define T_LOBBY_B 77 +#define T_LOBBY_B2 78 +#define T_OFF_AL 79 +#define T_OFF_AM 80 +#define T_OFF_AR 81 +#define T_OFF_BL 82 +#define T_OFF_B1 83 +#define T_OFF_B2 84 +#define T_OFF_BR 85 +#define T_OFF_AL_D 86 +#define T_OFF_AM_D 87 +#define T_OFF_AR_D 88 +#define T_OFF_BL_D 89 +#define T_OFF_B1_D 90 +#define T_OFF_B2_D 91 +#define T_OFF_BR_D 92 +#define T_CON_AL 93 +#define T_CON_AW 94 +#define T_CON_AX 95 +#define T_CON_AR 96 +#define T_CON_BL 97 +#define T_CON_BED1 98 +#define T_CON_BED2 99 +#define T_CON_PLANT 100 +#define T_CON_SOFA1 101 +#define T_CON_SOFA2 102 +#define T_CON_BR 103 +#define T_CON_AL_D 104 +#define T_CON_AW_D 105 +#define T_CON_AX_D 106 +#define T_CON_AR_D 107 +#define T_CON_BL_D 108 +#define T_CON_BED1_D 109 +#define T_CON_BED2_D 110 +#define T_CON_PLANT_D 111 +#define T_CON_SOFA1_D 112 +#define T_CON_SOFA2_D 113 +#define T_CON_BR_D 114 +#define T_FOOD_AL 115 +#define T_FOOD_AW 116 +#define T_FOOD_S1 117 +#define T_FOOD_S2 118 +#define T_FOOD_S3 119 +#define T_FOOD_AR 120 +#define T_FOOD_BL 121 +#define T_FOOD_T 122 +#define T_FOOD_C 123 +#define T_FOOD_BR 124 +#define T_FOOD_AL_D 125 +#define T_FOOD_AW_D 126 +#define T_FOOD_S1_D 127 +#define T_FOOD_S2_D 128 +#define T_FOOD_S3_D 129 +#define T_FOOD_AR_D 130 +#define T_FOOD_BL_D 131 +#define T_FOOD_T_D 132 +#define T_FOOD_C_D 133 +#define T_FOOD_BR_D 134 +#define T_ST_AL 135 +#define T_ST_AR 136 +#define T_ST_BL 137 +#define T_ST_BR 138 +#define T_SH_AL 139 +#define T_SH_AR 140 +#define T_SH_BL0 141 +#define T_SH_BR0 142 +#define T_SH_BL1 143 +#define T_SH_BR1 144 +#define T_SH_BL2 145 +#define T_SH_BR2 146 +#define T_SH_BL3 147 +#define T_SH_BR3 148 +#define T_CAR0_0_0L 149 +#define T_CAR0_0_0R 150 +#define T_CAR0_0_1L 151 +#define T_CAR0_0_1R 152 +#define T_CAR0_1_0L 153 +#define T_CAR0_1_0R 154 +#define T_CAR0_1_1L 155 +#define T_CAR0_1_1R 156 +#define T_CAR2_0_0L 157 +#define T_CAR2_0_0R 158 +#define T_CAR2_0_1L 159 +#define T_CAR2_0_1R 160 +#define T_CAR2_0_2L 161 +#define T_CAR2_0_2R 162 +#define T_CAR2_1_0L 163 +#define T_CAR2_1_0R 164 +#define T_CAR2_1_1L 165 +#define T_CAR2_1_1R 166 +#define T_CAR2_1_2L 167 +#define T_CAR2_1_2R 168 +#define T_CAR4_0_0L 169 +#define T_CAR4_0_0R 170 +#define T_CAR4_0_1L 171 +#define T_CAR4_0_1R 172 +#define T_CAR4_0_2L 173 +#define T_CAR4_0_2R 174 +#define T_CAR4_1_0L 175 +#define T_CAR4_1_0R 176 +#define T_CAR4_1_1L 177 +#define T_CAR4_1_1R 178 +#define T_CAR4_1_2L 179 +#define T_CAR4_1_2R 180 +#define T_CAR6_0_0L 181 +#define T_CAR6_0_0R 182 +#define T_CAR6_0_1L 183 +#define T_CAR6_0_1R 184 +#define T_CAR6_0_2L 185 +#define T_CAR6_0_2R 186 +#define T_CAR6_1_0L 187 +#define T_CAR6_1_0R 188 +#define T_CAR6_1_1L 189 +#define T_CAR6_1_1R 190 +#define T_CAR6_1_2L 191 +#define T_CAR6_1_2R 192 +#define P_SKY 0 +#define P_CLOUD_L 1 +#define P_CLOUD_M 2 +#define P_CLOUD_R 3 +#define P_TREE 4 +#define P_DIRT 5 +#define P_DIRT2 6 +#define P_DIRT_TOP 7 +#define P_FLOOR 8 +#define P_LOBBY 9 +#define P_LOBBY2 10 +#define P_ST_L 11 +#define P_ST_R 12 +#define P_SH_L0 13 +#define P_SH_R0 14 +#define P_SH_L1 15 +#define P_SH_R1 16 +#define P_SH_L2 17 +#define P_SH_R2 18 +#define P_SH_L3 19 +#define P_SH_R3 20 +extern const unsigned char tile_gfx[1032]; +extern const unsigned char tile_col[256]; +extern const unsigned char pair_a[256], pair_b[256]; +extern const unsigned char room_off[6]; +extern const unsigned char room_off_d[6]; +extern const unsigned char room_con[8]; +extern const unsigned char room_con_d[8]; +extern const unsigned char room_food[8]; +extern const unsigned char room_food_d[8]; +extern const unsigned char sprite_gfx[128]; +#endif diff --git a/projects/highrise/src/main.c b/projects/highrise/src/main.c new file mode 100644 index 0000000..f15c5a8 --- /dev/null +++ b/projects/highrise/src/main.c @@ -0,0 +1,688 @@ +/* + * Highrise - build and run a skyscraper. Commodore 64, cc65, PAL. + * + * c64 build projects/highrise c64 test projects/highrise + * c64 run projects/highrise (then plug in the mouse: see README.md) + * + * Build floors and a lobby, add offices, condos and fast food, and connect + * them with elevators and stairs. Tenants move in when people can reach their + * floor from the lobby; offices pay rent every day, condos sell once. People + * who wait too long at the elevators get stressed, and stressed tenants move + * out. More people earn stars, and stars let the tower grow taller. + * + * Controls: 1351 mouse in port 1. Click a tool in the toolbar, then click in + * the tower to build (drag sideways for floors and lobby, up or down for an + * elevator). Right button cancels. Mouse at the screen edge or cursor keys + * scroll; space pauses; 1-8 set the speed; + and - set the mouse speed. + */ +#include +#include +#include +#include +#include "c64test.h" +#include "tower.h" + +/* render.s reads the mouse in the frame interrupt */ +extern volatile unsigned mouse_x; /* 0-319 */ +extern volatile unsigned char mouse_y; /* 0-199 */ +extern volatile unsigned char mouse_btn; +extern unsigned char mouse_sens; /* 1-8: quarter steps */ +#define BTN_LEFT 0x10 +#define BTN_RIGHT 0x01 +extern volatile unsigned char frame_count; + +#define SPRITE_MEM ((unsigned char *)0xEC00) +#define SPRITE_PTRS (SCREEN + 0x3F8) +#define SPR_BLOCK 176 /* $EC00 in bank 3 */ +#define PREVIEW_MEM (SPRITE_MEM + 128) /* blocks 178-180 */ + +#define ROW_STATUS 0 +#define ROW_MSG 1 +#define ROW_TOOLS 22 +#define ROW_COSTS 23 +#define ROW_HELP 24 + +static const char *const tool_name[TOOL_COUNT] = { + "floor", "lobby", "stair", "elev ", "offce", "condo", "food ", "clear" }; +static const char *const tool_cost[TOOL_COUNT] = { + " $500", "$2000", "$5000", " $18K", " $40K", " $80K", " $50K", " " }; +/* sim ticks per frame for speeds 0 (paused) to 8. A busy tower can't always + keep up with the top speeds: the C64 then simply runs flat out. */ +static const unsigned char speed_mul[9] = { 0, 1, 2, 3, 4, 6, 8, 12, 16 }; + +static unsigned char tool = TOOL_FLOOR, speed = 1, paused_speed = 1; +static unsigned char msg_time; /* frames an event message stays */ +static unsigned char hover_f, hover_x, in_view; +static unsigned char drag, drag_f, drag_x; +static unsigned char preview_w; /* width in columns drawn into the sprites */ + +/* ---- text on the panels (rows 0-1 and 22-24, hi-res characters) */ + +static void put_str(unsigned char row, unsigned char col, const char *s, unsigned char color) +{ + unsigned char c, *p = SCREEN + row * 40 + col, *q = COLRAM + row * 40 + col; + + while ((c = *s++)) { + if (c >= 0x40 && c < 0x60) c -= 0x40; /* PETSCII letters -> screen codes */ + else if (c >= 0xC1 && c <= 0xDA) c -= 0xC0; + *p++ = c; + *q++ = color; + } +} + +static void clear_row(unsigned char row) +{ + memset(SCREEN + row * 40, 32, 40); +} + +static char numbuf[12]; + +static void put_num(unsigned char row, unsigned char col, unsigned long v, unsigned char width, + unsigned char color) +{ + unsigned char len; + + ultoa(v, numbuf, 10); + len = strlen(numbuf); + while (len < width) { put_str(row, col++, " ", color); --width; } + put_str(row, col, numbuf, color); +} + +static void floor_name(unsigned char f, char *out) +{ + if (f < GROUND) { out[0] = 'b'; out[1] = '0' + GROUND - f; out[2] = 0; } + else { itoa(f - GROUND + 1, out, 10); strcat(out, "f"); } +} + +static void message(const char *text, unsigned char f, unsigned char color) +{ + char fl[6]; + + clear_row(ROW_MSG); + put_str(ROW_MSG, 0, text, color); + if (f != 255) { + floor_name(f, fl); + put_str(ROW_MSG, 40 - strlen(fl), fl, color); + } +} + +static void event_message(const char *text, unsigned char f) +{ + message(text, f, COLOR_YELLOW); + msg_time = 150; +} + +static void draw_toolbar(void) +{ + unsigned char i; + + /* panel text must use colors 0-7: 8-15 would make it multicolor */ + for (i = 0; i < TOOL_COUNT; ++i) { + put_str(ROW_TOOLS, i * 5, tool_name[i], i == tool ? COLOR_YELLOW : (i & 1) ? COLOR_WHITE : COLOR_CYAN); + put_str(ROW_COSTS, i * 5, tool_cost[i], i == tool ? COLOR_YELLOW : (i & 1) ? COLOR_GREEN : COLOR_PURPLE); + } +} + +static unsigned shown_min = 0xFFFF; +static signed long shown_money = -1; +static unsigned shown_pop = 0xFFFF; +static unsigned char shown_stars, shown_speed = 255; + +static void draw_status(void) +{ + unsigned char i; + char buf[3]; + + if (money != shown_money) { + shown_money = money; + put_str(ROW_STATUS, 0, money < 0 ? "-$" : " $", money < 0 ? COLOR_RED : COLOR_GREEN); + put_num(ROW_STATUS, 2, money < 0 ? -money : money, 9, money < 0 ? COLOR_RED : COLOR_GREEN); + } + if (pop != shown_pop) { + shown_pop = pop; + put_str(ROW_STATUS, 12, "pop", COLOR_CYAN); + put_num(ROW_STATUS, 15, pop, 5, COLOR_WHITE); + } + if (stars != shown_stars) { + shown_stars = stars; + for (i = 0; i < 5; ++i) put_str(ROW_STATUS, 21 + i, "*", i < stars ? COLOR_YELLOW : COLOR_BLUE); + } + if (clock_min != shown_min) { + shown_min = clock_min; + put_str(ROW_STATUS, 27, "d", COLOR_CYAN); + put_num(ROW_STATUS, 28, day, 3, COLOR_WHITE); + buf[0] = '0' + clock_min / 600; buf[1] = '0' + (clock_min / 60) % 10; buf[2] = 0; + put_str(ROW_STATUS, 32, buf, COLOR_WHITE); + put_str(ROW_STATUS, 34, ":", COLOR_WHITE); + buf[0] = '0' + (clock_min % 60) / 10; buf[1] = '0' + clock_min % 10; + put_str(ROW_STATUS, 35, buf, COLOR_WHITE); + } + if (speed != shown_speed) { + shown_speed = speed; + buf[0] = '>'; buf[1] = '0' + speed; buf[2] = 0; + put_str(ROW_STATUS, 38, speed ? buf : "ii", speed ? COLOR_GREEN : COLOR_RED); + } +} + +/* sky by time of day */ +static void set_sky(void) +{ + unsigned m = clock_min; + sky_color = m < 300 || m >= 1170 ? COLOR_BLACK + : m < 390 || m >= 1080 ? COLOR_BLUE : COLOR_LIGHTBLUE; +} + +/* ---- view */ + +static void scroll_to(signed char df, signed char dx) +{ + signed int f = view_f + df, x = view_x + dx; + + if (f < VIEW_FLOORS - 1) f = VIEW_FLOORS - 1; + if (f > FLOORS - 1) f = FLOORS - 1; + if (x < 0) x = 0; + if (x > WORLD_W - VIEW_COLS) x = WORLD_W - VIEW_COLS; + if (f == view_f && x == view_x) return; + view_f = f; + view_x = x; + draw_view(); + memset(s_drawn, 0xFF, sizeof s_drawn); /* every car needs drawing */ +} + +/* draw a tile on screen only (cars), if visible */ +static void scr_put(unsigned char mr, unsigned char x, unsigned char t) +{ + unsigned char sr = mr - TROW(view_f), sc = x - view_x; + unsigned o; + + if (sr < VIEW_ROWS && sc < VIEW_COLS) { + o = (unsigned)(VIEW_ROW + sr) * 40 + sc; + SCREEN[o] = t; + COLRAM[o] = tile_col[t]; + } +} + +static void scr_restore(unsigned char mr, unsigned char x) /* mr: tile row */ +{ + unsigned char p = MAP[(unsigned)(mr >> 1) * WORLD_W + x]; + scr_put(mr, x, (mr & 1) ? pair_b[p] : pair_a[p]); +} + +static const unsigned char car_base[4][2] = { + { T_CAR0_0_0L, T_CAR0_1_0L }, { T_CAR2_0_0L, T_CAR2_1_0L }, + { T_CAR4_0_0L, T_CAR4_1_0L }, { T_CAR6_0_0L, T_CAR6_1_0L } }; + +static void draw_cars(void) +{ + unsigned char s, mr, k, rows, base, x; + unsigned y, old; + + for (s = 0; s < MAX_SHAFTS; ++s) { + if (s_kind[s] != SK_ELEV) continue; + y = (unsigned)(FLOORS - 1 - s_lo[s]) * 16 - s_pos[s]; /* car's top, in map pixels */ + if (y == s_drawn[s]) continue; + x = s_x[s]; + old = s_drawn[s]; + if (old != NOT_DRAWN) { + old &= 0x7FFF; + for (mr = old >> 3; mr <= (old + 15) >> 3; ++mr) { + scr_restore(mr, x); + scr_restore(mr, x + 1); + } + } + mr = y >> 3; + rows = (y & 7) ? 3 : 2; + base = car_base[(y & 7) >> 1][mr & 1]; + for (k = 0; k < rows; ++k, ++mr, base += 2) { + scr_put(mr, x, base); + scr_put(mr, x + 1, base + 1); + } + s_drawn[s] = y; + } +} + +/* ---- build preview: a rectangle in sprites 2-4 */ + +/* Sprite memory is under the Kernal ROM: writes reach it, but reads would + return ROM bytes, so the shape is built here and copied over. Boxes wider + than 9 columns use double-width sprites (each shape pixel covers two). */ +static unsigned char preview_buf[192]; + +static void preview_shape(unsigned char w) +{ + unsigned char line, px, wpx, *p; + + if (w == preview_w) return; + preview_w = w; + wpx = w > 9 ? w * 4 : w * 8; + memset(preview_buf, 0, sizeof preview_buf); + for (line = 0; line < 16; ++line) + for (px = 0; px < wpx; ++px) + if (line == 0 || line == 15 || px == 0 || px == wpx - 1) { + p = preview_buf + (px / 24) * 64 + line * 3 + (px % 24) / 8; + *p |= 0x80 >> (px & 7); + } + memcpy(PREVIEW_MEM, preview_buf, sizeof preview_buf); +} + +/* $D010 and $D015 are shared with the mouse pointer, which the IRQ moves: + change them with interrupts off, or a stale copy can move or hide it. */ +static void preview_hide(void) +{ + __asm__("sei"); + VIC.spr_ena &= 0xE3; + __asm__("cli"); +} + +static void preview_show(unsigned char f, unsigned char x0, unsigned char w, unsigned char ok) +{ + unsigned char i, n, ena = 0, hi = 0, step, color = ok ? COLOR_GREEN : COLOR_RED; + signed int sx; + unsigned char sy; + + if (w > 18) w = 18; /* the message line gives the full size */ + preview_shape(w); + step = w > 9 ? 48 : 24; /* screen pixels per sprite */ + VIC.spr_exp_x = w > 9 ? 0x1C : 0; + sy = (unsigned char)((VIEW_ROW + (view_f - f) * 2) * 8 + 50); + n = (w * 8 + step - 1) / step; + for (i = 0; i < n; ++i) { + sx = (signed int)(x0 - view_x) * 8 + 24 + i * step; + if (sx < 0 || sx > 343) continue; + (&VIC.spr0_x)[(2 + i) * 2] = (unsigned char)sx; + (&VIC.spr0_y)[(2 + i) * 2] = sy; + if (sx > 255) hi |= 4 << i; + (&VIC.spr0_color)[2 + i] = color; + ena |= 4 << i; + } + __asm__("sei"); + VIC.spr_hi_x = (VIC.spr_hi_x & 0xE3) | hi; + VIC.spr_ena = (VIC.spr_ena & 0xE3) | ena; + __asm__("cli"); +} + +/* ---- what the current tool would do at the pointer */ + +static unsigned char tool_room_w(void) +{ + return tool == TOOL_OFFICE ? 6 : (tool == TOOL_CONDO || tool == TOOL_FOOD) ? 8 : 2; +} + +static unsigned char place_x(void) /* left column of the thing to build */ +{ + unsigned char w; + + if (tool == TOOL_FLOOR || tool == TOOL_LOBBY || tool == TOOL_CLEAR) return hover_x; + if (tool == TOOL_STAIRS || tool == TOOL_ELEV) return drag && tool == TOOL_ELEV ? drag_x : hover_x; + w = tool_room_w(); + return hover_x < w / 2 ? 0 : hover_x - w / 2; +} + +static void describe(void) +{ + unsigned char o = cellobj[hover_f][hover_x], r, s; + static const char *const eval_name[3] = { "good", "fair", "poor" }; + + if (o >= C_SHAFT) { + s = o - C_SHAFT; + if (s_kind[s] == SK_STAIRS) { message("stairs", hover_f, COLOR_WHITE); return; } + message("elevator waiting", hover_f, COLOR_WHITE); + put_num(ROW_MSG, 18, s_wn[s][hover_f - s_lo[s]], 3, COLOR_WHITE); + put_str(ROW_MSG, 22, "riding", COLOR_WHITE); + put_num(ROW_MSG, 29, s_load[s], 2, COLOR_WHITE); + } else if (o >= C_ROOM) { + r = o - C_ROOM; + message(r_type[r] == RT_OFFICE ? "office" : r_type[r] == RT_CONDO ? "condo" : "food", + hover_f, COLOR_WHITE); + if (r_type[r] == RT_FOOD) { + put_str(ROW_MSG, 7, "guests today", COLOR_WHITE); + put_num(ROW_MSG, 20, r_done[r], 3, COLOR_WHITE); + } else if (!r_occ[r]) { + put_str(ROW_MSG, 7, "for rent - needs a way up", COLOR_RED); + } else { + put_str(ROW_MSG, 7, "in", COLOR_WHITE); + put_num(ROW_MSG, 10, r_in[r], 1, COLOR_WHITE); + put_str(ROW_MSG, 12, eval_name[r_eval[r]], r_eval[r] == 2 ? COLOR_RED : COLOR_GREEN); + } + } else { + message(o == C_LOBBY ? "lobby" : o == C_FLOOR ? "empty floor" : "", hover_f, COLOR_CYAN); + } +} + +static void update_hover(void) +{ + unsigned char x0, w, f1 = hover_f, ok, o; + long cost; + static unsigned char last_f, last_x, last_tool, last_drag, last_in, refresh; + + /* redraw only when something changed, or every half second (live counts) */ + if (hover_f == last_f && hover_x == last_x && tool == last_tool && drag == last_drag + && in_view == last_in && ++refresh < 25) return; + refresh = 0; + last_f = hover_f; last_x = hover_x; last_tool = tool; last_drag = drag; last_in = in_view; + if (!in_view) { preview_hide(); return; } + x0 = place_x(); + if (tool == TOOL_CLEAR) { + o = cellobj[hover_f][hover_x]; + w = 1; + if (o >= C_SHAFT) { x0 = s_x[o - C_SHAFT]; w = 2; } + else if (o >= C_ROOM) { x0 = r_x[o - C_ROOM]; w = room_width(r_type[o - C_ROOM]); } + preview_show(hover_f, x0, w, o != C_EMPTY); + if (!msg_time) describe(); + return; + } + if (drag && (tool == TOOL_FLOOR || tool == TOOL_LOBBY)) { + cost = build_cost(tool, drag_f, drag_x, hover_x, drag_f); + x0 = drag_x < hover_x ? drag_x : hover_x; + w = (drag_x < hover_x ? hover_x - drag_x : drag_x - hover_x) + 1; + preview_show(drag_f, x0, w, cost >= 0 && cost <= money); + } else { + if (drag && tool == TOOL_ELEV) f1 = drag_f; + cost = build_cost(tool, hover_f, x0, x0, f1); + w = (tool == TOOL_FLOOR || tool == TOOL_LOBBY) ? 1 : tool_room_w(); + ok = cost > 0 && cost <= money; + preview_show(hover_f, x0, w, ok); + } + if (msg_time) return; + if (cost < 0) message(build_error, hover_f, COLOR_RED); + else if (cost == 0) describe(); + else { + message("cost $", hover_f, cost <= money ? COLOR_GREEN : COLOR_RED); + put_num(ROW_MSG, 6, cost, 1, cost <= money ? COLOR_GREEN : COLOR_RED); + } +} + +static void do_build(void) +{ + unsigned char ok, x0 = place_x(); + + if (tool == TOOL_CLEAR) ok = clear_at(hover_f, hover_x); + else if (tool == TOOL_FLOOR || tool == TOOL_LOBBY) ok = build(tool, drag_f, drag_x, hover_x, drag_f); + else if (tool == TOOL_ELEV) ok = build(tool, hover_f, drag_x, drag_x, drag_f); + else ok = build(tool, hover_f, x0, x0, hover_f); + if (ok) { + memset(s_drawn, 0xFF, sizeof s_drawn); + draw_view(); + } else { + message(build_error, hover_f, COLOR_RED); + msg_time = 100; + } +} + +/* ---- input */ + +static unsigned char build_sample(void); +static void day_test(void); +static void new_game(void); +static unsigned char running; /* main loop passes, up to 255 */ +static unsigned char buttons_was, edge_wait; + +static void handle_input(void) +{ + unsigned mx; + unsigned char my, b, col, row, key, pressed; + + __asm__("sei"); /* a consistent position */ + mx = mouse_x; + my = mouse_y; + b = mouse_btn; + __asm__("cli"); + pressed = b & ~buttons_was; + buttons_was = b; + col = mx >> 3; + row = my >> 3; + if (col > 39) col = 39; + if (row > 24) row = 24; + + in_view = row >= VIEW_ROW && row < VIEW_ROW + VIEW_ROWS; + if (in_view) { + hover_f = view_f - (row - VIEW_ROW) / 2; + hover_x = view_x + col; + if (drag && tool == TOOL_ELEV) hover_x = drag_x; /* elevators drag straight */ + } + + if (b & BTN_RIGHT) drag = 0; + if (pressed & BTN_LEFT) { + if (row == ROW_TOOLS || row == ROW_COSTS) { + tool = col / 5; + draw_toolbar(); + } else if (row == ROW_STATUS && col >= 37) { + speed = speed == 8 ? 0 : speed + 1; + } else if (in_view) { + drag = 1; drag_f = hover_f; drag_x = hover_x; + } + } + if (drag && !(b & BTN_LEFT)) { + if (in_view) do_build(); + drag = 0; + } + + /* scrolling: pointer against the screen's edge, or cursor keys / WASD */ + if (edge_wait) --edge_wait; + else if (mx <= 1) { scroll_to(0, -1); edge_wait = 3; } + else if (mx >= 318) { scroll_to(0, 1); edge_wait = 3; } + else if (my <= 1) { scroll_to(1, 0); edge_wait = 8; } + else if (my >= 198) { scroll_to(-1, 0); edge_wait = 8; } + + key = cbm_k_getin(); + switch (key) { + case CH_CURS_UP: case 'w': scroll_to(1, 0); break; + case CH_CURS_DOWN: case 's': scroll_to(-1, 0); break; + case CH_CURS_LEFT: case 'a': scroll_to(0, -4); break; + case CH_CURS_RIGHT: case 'd': scroll_to(0, 4); break; + case ' ': /* pause, and back to the same speed */ + if (speed) { paused_speed = speed; speed = 0; } else speed = paused_speed; + break; + case 't': /* `c64 test` types this at once */ + if (running < 50) { + day_test(); + c64test_pass(); + new_game(); + } + break; + case 'x': /* a sample tower, on an empty lot */ + if (!room_count) { + money += 5000000L; + stars = 3; update_stars(); + build_sample(); + for (key = 0; key < room_count; ++key) if (r_type[key] != RT_FOOD) occupy(key); + money = 500000L; + clock_min = 8 * 60; + memset(s_drawn, 0xFF, sizeof s_drawn); + draw_view(); + event_message("sample tower", 255); + } + break; + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': speed = key - '0'; break; + case '+': case '-': /* mouse speed */ + if (key == '+' && mouse_sens < 8) ++mouse_sens; + if (key == '-' && mouse_sens > 1) --mouse_sens; + event_message("mouse speed", 255); + put_num(ROW_MSG, 12, mouse_sens, 1, COLOR_YELLOW); + put_str(ROW_MSG, 14, "(+ faster, - slower)", COLOR_YELLOW); + break; + } +} + +/* ---- setup */ + +static void video_init(void) +{ + unsigned char *src, *dst; + unsigned i; + + /* character ROM's upper-case font -> $E000 (chars 0-63), tiles after it */ + __asm__("sei"); + *(unsigned char *)1 = 0x32; /* char ROM in, BASIC out */ + src = (unsigned char *)0xD000; + dst = (unsigned char *)0xE000; + for (i = 0; i < 512; ++i) dst[i] = src[i]; + *(unsigned char *)1 = 0x36; /* I/O back in; BASIC stays out */ + __asm__("cli"); + memcpy((unsigned char *)0xE200, tile_gfx, sizeof tile_gfx); + memset(SPRITE_MEM, 0, 320); + memcpy(SPRITE_MEM, sprite_gfx, sizeof sprite_gfx); + + memset(SCREEN, 32, 1000); + memset(COLRAM, COLOR_WHITE, 1000); + CIA2.pra &= 0xFC; /* VIC bank 3: $C000-$FFFF */ + VIC.addr = 0xA8; /* screen $E800, chars $E000 */ + VIC.ctrl2 = 0x18; /* multicolor text, 40 columns */ + VIC.bordercolor = COLOR_BLACK; + VIC.bgcolor0 = COLOR_BLACK; + VIC.bgcolor1 = COLOR_GRAY1; /* structure */ + VIC.bgcolor2 = COLOR_BROWN; /* wood and earth */ + + for (i = 0; i < 5; ++i) SPRITE_PTRS[i] = SPR_BLOCK + i; + VIC.spr0_color = COLOR_WHITE; + VIC.spr1_color = COLOR_BLACK; + VIC.spr_mcolor = 0; + VIC.spr_exp_x = VIC.spr_exp_y = 0; + VIC.spr_bg_prio = 0; +} + +static void new_game(void) +{ + world_reset(); + sim_reset(); + view_f = GROUND + VIEW_FLOORS - 3; /* ground near the bottom of the view */ + view_x = 20; + set_sky(); + draw_view(); + memset(s_drawn, 0xFF, sizeof s_drawn); + clear_row(ROW_STATUS); clear_row(ROW_MSG); + clear_row(ROW_TOOLS); clear_row(ROW_COSTS); clear_row(ROW_HELP); + shown_money = -1; shown_pop = 0xFFFF; shown_stars = 0; shown_speed = 255; shown_min = 0xFFFF; + draw_toolbar(); + put_str(ROW_HELP, 0, "build a lobby, floors and an elevator", COLOR_CYAN); + draw_status(); +} + +/* ---- self-test: build a small tower and run it for a day, no player */ + +/* A small tower: lobby, six floors, an elevator, stairs, offices, a + restaurant and a condo. Returns 0, or the step that failed. Used by the + self-test and by the X key (a sample to look at). */ +static unsigned char sample_offices; + +static unsigned char build_sample(void) +{ + unsigned char f; + + sample_offices = 0; + if (!build(TOOL_LOBBY, GROUND, 20, 59, GROUND)) return 2; + if (cellobj[GROUND][40] != C_LOBBY || MAP[(unsigned)PROW(GROUND) * WORLD_W + 40] != P_LOBBY2) return 3; + for (f = GROUND + 1; f <= GROUND + 6; ++f) + if (!build(TOOL_FLOOR, f, 20, 59, f)) return 4; + if (build(TOOL_FLOOR, GROUND + 8, 20, 20, GROUND + 8)) return 5; /* no support */ + if (!build(TOOL_ELEV, GROUND + 6, 38, 38, GROUND)) return 6; + if (cellobj[GROUND + 3][39] != C_SHAFT || s_lo[0] != GROUND || s_hi[0] != GROUND + 6) return 7; + if (!build(TOOL_STAIRS, GROUND + 1, 58, 58, GROUND + 1)) return 8; + for (f = GROUND + 1; f <= GROUND + 6; ++f) { + if (!build(TOOL_OFFICE, f, 20, 20, f) || !build(TOOL_OFFICE, f, 26, 26, f) + || !build(TOOL_OFFICE, f, 32, 32, f)) return 9; + sample_offices += 3; + if (f == GROUND + 1) { if (!build(TOOL_FOOD, f, 40, 40, f)) return 10; } + else if (f == GROUND + 6) { if (!build(TOOL_CONDO, f, 40, 40, f)) return 11; } + else { if (!build(TOOL_OFFICE, f, 40, 40, f)) return 12; ++sample_offices; } + } + return 0; +} + +static void self_test(void) +{ + unsigned char f, r, leg, offices; + + world_reset(); + sim_reset(); + money = 5000000L; + stars = 3; + update_stars(); + c64test_check(max_floor == GROUND + 39, 1); + f = build_sample(); + c64test_check(f == 0, f); + offices = sample_offices; + c64test_check(!build(TOOL_OFFICE, GROUND + 2, 22, 22, GROUND + 2), 13); /* occupied */ + + c64test_check(plan(GROUND, GROUND + 3, &leg) == 0 && leg == GROUND + 3, 14); + c64test_check(plan(GROUND, GROUND + 8, &leg) == 0 && leg == GROUND + 6, 15); /* gets closer */ + c64test_check(reachable(GROUND, GROUND + 6) && !reachable(GROUND, GROUND + 7), 16); + + for (r = 0; r < room_count; ++r) if (r_type[r] != RT_FOOD) occupy(r); + c64test_check(pop == offices * 6 + 3, 17); + + /* bulldozing */ + c64test_check(clear_at(GROUND + 3, 21) && cellobj[GROUND + 3][21] == C_FLOOR, 24); + c64test_check(!clear_at(GROUND + 3, 21 + 30), 25); /* floors rest on it */ + c64test_check(clear_at(GROUND + 2, 39) && !reachable(GROUND, GROUND + 3), 26); +} + +/* The long check: a working day in the sample tower. About 40 seconds of C64 + time, so it runs only when `c64 test` types T in the first second. */ +static void day_test(void) +{ + unsigned char r, offices; + unsigned noon_in = 0; + signed long before_rent = 0; + + world_reset(); + sim_reset(); + money = 5000000L; + stars = 3; + update_stars(); + c64test_check(build_sample() == 0, 30); + offices = sample_offices; + for (r = 0; r < room_count; ++r) if (r_type[r] != RT_FOOD) occupy(r); + draw_view(); + + /* one working day: 06:00 to 23:00 */ + while (clock_min < 23 * 60) { + sim_tick(); + if (clock_min == 12 * 60 && !noon_in) + for (r = 0; r < room_count; ++r) if (r_type[r] == RT_OFFICE) noon_in += r_in[r] + r_trav[r]; + if (clock_min == 16 * 60 + 59) before_rent = money; + } + *(unsigned *)0x02F0 = noon_in; + *(unsigned long *)0x02F2 = stat_arrived; + *(unsigned long *)0x02F6 = stat_failed; + *(unsigned char *)0x02FA = trips_active(); + c64test_check(noon_in >= offices * 5, 18); /* most workers came in */ + c64test_check(money >= before_rent + offices * 1000L, 19); + c64test_check(stat_failed == 0, 20); + c64test_check(trips_active() == 0 && s_load[0] == 0, 21); /* everyone got home */ + for (r = 0; r < room_count; ++r) { + if (r_type[r] == RT_OFFICE) c64test_check(r_in[r] == 0 && r_trav[r] == 0, 22); + if (r_type[r] == RT_FOOD) c64test_check(r_done[r] > 10, 23); + } +} + +int main(void) +{ + unsigned char last, frames, ticks; + + *(unsigned char *)1 = 0x36; /* BASIC ROM out: the map is at $A000 */ + video_init(); + self_test(); + + sim_message = event_message; + new_game(); + irq_install(); /* also starts the mouse */ + VIC.spr_ena = 0x03; /* pointer: sprites 0 and 1 */ + + last = frame_count; + for (;;) { + if (running < 255) ++running; + handle_input(); + frames = frame_count - last; + last += frames; + if (frames > 4) frames = 4; + for (ticks = frames * speed_mul[speed]; ticks; --ticks) sim_tick(); + set_sky(); + draw_cars(); + update_hover(); + draw_status(); + if (msg_time) msg_time = msg_time > frames ? msg_time - frames : 0; + } + return 0; +} diff --git a/projects/highrise/src/render.s b/projects/highrise/src/render.s new file mode 100644 index 0000000..717a1d4 --- /dev/null +++ b/projects/highrise/src/render.s @@ -0,0 +1,291 @@ +; Highrise: drawing the tower view, the raster splits, and the mouse pointer. +; +; Screen $E800 and charset $E000 sit in RAM under the Kernal ROM (VIC bank 3): +; writes reach the RAM, but reads would see ROM, so nothing here reads them. +; +; The IRQ replaces the CIA timer interrupt with two raster interrupts: +; line 65: wait for line 67 (first line of row 2), $D021 = sky color +; line 225: wait for line 227 (first line of row 22), $D021 = panel color, +; count the frame, read the mouse, then run the Kernal handler +; (keyboard scan) unless a mouse button is down. +; The mouse is also read in the top interrupt: twice a frame. +; So the status bar and toolbar keep a fixed background while the sky behind +; the tower changes with the time of day. The color store lands before the +; badline's DMA starts on those lines, so the change is clean. + + .export _draw_view, _irq_install, _sky_color, _frame_count + .export _mouse_x, _mouse_y, _mouse_btn, _mouse_sens + .import _view_f, _view_x, _tile_col, _pair_a, _pair_b + .importzp ptr1, ptr2, ptr3, tmp1, tmp2 + +MAP = $A000 +SCREEN = $E800 +COLRAM = $D800 +FLOORS = 64 +PANEL = 0 ; panel background: black + + .bss +_sky_color: .res 1 +_frame_count: .res 1 +oldirq: .res 2 +_mouse_x: .res 2 ; pointer, 0-319 +_mouse_y: .res 1 ; pointer, 0-199 +_mouse_btn: .res 1 ; bit 4 left, bit 0 right +_mouse_sens: .res 1 ; speed in quarter steps, 1-8 +qx: .res 2 ; pointer in quarter pixels, 0-1279 +qy: .res 2 ; 0-799 +oldpx: .res 1 ; last pot readings used +oldpy: .res 1 +delta: .res 1 + + .rodata +mul80lo: + .repeat FLOORS, I + .byte <(I*80) + .endrepeat +mul80hi: + .repeat FLOORS, I + .byte >(I*80 + MAP) + .endrepeat + + .code + +; void draw_view(void): draw 10 floors x 40 columns into screen rows 2-21. +; The map holds a pair id per cell: its top tile (pair_a) goes on the +; floor's first row and its bottom tile (pair_b) on the second, each with its +; color from tile_col. +.macro ROWPASS table + ldy #39 +: lda (ptr1),y + tax + lda table,x + sta (ptr2),y + tax + lda _tile_col,x + sta (ptr3),y + dey + bpl :- + lda ptr2 + clc + adc #40 + sta ptr2 + sta ptr3 + bcc :+ + inc ptr2+1 + inc ptr3+1 +: +.endmacro + +_draw_view: + lda #FLOORS-1 + sec + sbc _view_f + sta tmp1 ; map row of the top floor in view + lda #<(SCREEN+80) + sta ptr2 + lda #>(SCREEN+80) + sta ptr2+1 + lda #<(COLRAM+80) + sta ptr3 + lda #>(COLRAM+80) + sta ptr3+1 + lda #10 + sta tmp2 +@floor: ldx tmp1 + lda mul80lo,x + clc + adc _view_x + sta ptr1 + lda mul80hi,x + adc #0 + sta ptr1+1 + ROWPASS _pair_a + ROWPASS _pair_b + inc tmp1 + dec tmp2 + bne @floor + rts + +; void irq_install(void): call after mouse_install() +_irq_install: + sei + lda $0314 + sta oldirq + lda $0315 + sta oldirq+1 + lda #irq + sta $0315 + lda #$7F ; CIA 1: no timer interrupt + sta $DC0D + lda $DC0D + lda #<(160*4) ; pointer starts mid-screen + sta qx + lda #>(160*4) + sta qx+1 + lda #<(100*4) + sta qy + lda #>(100*4) + sta qy+1 + lda #3 + sta _mouse_sens + lda $D419 + sta oldpx + lda $D41A + sta oldpy + jsr place + lda #65 + sta $D012 + lda $D011 + and #$7F + sta $D011 + lda #$01 + sta $D01A + sta $D019 + cli + rts + +irq: lda $D019 + and #$01 + bne @raster + jmp $EA81 ; not ours: restore registers and return +@raster: + sta $D019 ; acknowledge (A = 1) + lda $D012 + bmi @bottom + lda _sky_color ; top split + ldx #67 +: cpx $D012 + bne :- + sta $D021 + lda #225 + sta $D012 + jsr mouse + jmp $EA81 +@bottom: + lda #PANEL + ldx #227 +: cpx $D012 + bne :- + sta $D021 + lda #65 + sta $D012 + inc _frame_count + jsr mouse + lda _mouse_btn ; a held button pulls keyboard lines low: + bne :+ ; skip the keyboard scan so it types nothing + jmp (oldirq) +: jmp $EA81 + +; ---- 1351 mouse in port 1, read twice a frame (in both raster interrupts). +; Each pot holds the mouse's position modulo 128 (bit 0 is noise on a real +; 1351). The change since the last reading, taken as -64..63, is only right +; if the mouse moved less than 64 counts in between, so the pots are read +; often: a fast flick at once a frame could wrap and throw the pointer the +; wrong way. The change is halved (dropping the noise bit, keeping the odd +; count for next time) and times mouse_sens/4 moves the pointer. + +; AXIS pot, old, q, limit, invert +.macro AXIS pot, old, q, limit, invert + .local skip, pos, add, clamp_hi + lda pot + sec + sbc old + and #$7F + cmp #$40 + bcc pos + ora #$80 ; -64..-1 +pos: cmp #$80 + ror a ; halve, keeping the sign + beq skip ; under 2 counts: wait for more + sta delta + asl a ; old += 2 * delta: an odd count carries over + clc + adc old + sta old + .if invert + lda delta ; moving the mouse up raises the count + eor #$FF + clc + adc #1 + sta delta + .endif + ldy _mouse_sens ; q += delta * sens +add: lda delta + clc + adc q + sta q + lda delta + and #$80 + beq :+ + lda #$FF +: adc q+1 + sta q+1 + dey + bne add + lda q+1 ; clamp to 0..limit-1 + bpl clamp_hi + lda #0 + sta q + sta q+1 + beq skip +clamp_hi: + lda q + cmp #limit + bcc skip + lda #<(limit-1) + sta q + lda #>(limit-1) + sta q+1 +skip: +.endmacro + +mouse: AXIS $D419, oldpx, qx, 1280, 0 + AXIS $D41A, oldpy, qy, 800, 1 + lda #$00 ; keyboard columns to inputs while reading, + sta $DC02 ; so a held key can't look like a button + lda $DC01 + dec $DC02 ; back to outputs ($FF) + eor #$FF + and #$11 + sta _mouse_btn + ; fall through + +; pointer = q / 4; sprites 0 and 1 there (the arrow's tip is 1 pixel in) +place: lda qx+1 + lsr a + sta _mouse_x+1 + lda qx + ror a + lsr _mouse_x+1 + ror a + sta _mouse_x + clc + adc #23 + sta $D000 + sta $D002 + lda _mouse_x+1 + adc #0 + beq :+ + lda $D010 + ora #$03 + bne :++ +: lda $D010 + and #$FC +: sta $D010 + lda qy+1 ; qy < 800, so qy / 4 fits a byte + sta delta + lda qy + lsr delta + ror a + lsr delta + ror a + sta _mouse_y + clc + adc #49 + sta $D001 + sta $D003 + rts diff --git a/projects/highrise/src/sim.c b/projects/highrise/src/sim.c new file mode 100644 index 0000000..dd7fa2b --- /dev/null +++ b/projects/highrise/src/sim.c @@ -0,0 +1,535 @@ +/* Highrise: the simulation. People move as trips (groups of 1-3) between + * floors; each trip goes leg by leg over elevators and stairs, changing + * shafts when no single one serves both floors. Elevators run a classic + * up-down sweep: keep going while there are calls ahead, then turn. + */ +#include +#include "tower.h" + +unsigned clock_min; +unsigned day; +unsigned long stat_arrived, stat_failed; +void (*sim_message)(const char *text, unsigned char f); + +unsigned char t_n[MAX_TRIPS]; /* people in the group; 0 = free slot */ +static unsigned char t_room[MAX_TRIPS], t_kind[MAX_TRIPS], t_state[MAX_TRIPS]; +static unsigned char t_cur[MAX_TRIPS], t_dest[MAX_TRIPS], t_leg[MAX_TRIPS]; +static unsigned char t_shaft[MAX_TRIPS], t_timer[MAX_TRIPS], t_wait[MAX_TRIPS]; +static unsigned char t_food[MAX_TRIPS], t_legs[MAX_TRIPS], t_x[MAX_TRIPS]; +static unsigned char tick_in_min, next_slot; +static unsigned char foods[MAX_ROOMS], food_count; +static unsigned char s_wake[MAX_SHAFTS]; /* an idle car has been called */ +static unsigned char elevs[MAX_SHAFTS], elev_count; +unsigned char shafts_changed = 1; /* set by world.c: rebuild elevs */ + +static const unsigned char bit8[8] = { 1, 2, 4, 8, 16, 32, 64, 128 }; +#define BIT(a, f) ((a)[(f) >> 3] & bit8[(f) & 7]) +#define SETB(a, f) ((a)[(f) >> 3] |= bit8[(f) & 7]) +#define CLRB(a, f) ((a)[(f) >> 3] &= (unsigned char)~bit8[(f) & 7]) +#define SERVES(f, s) (((unsigned char *)&floor_shafts[f])[(s) >> 3] & bit8[(s) & 7]) + +static unsigned char rnd_state = 0x5A; +static unsigned char rand8(void) +{ + rnd_state ^= rnd_state << 3; + rnd_state ^= rnd_state >> 5; + rnd_state ^= rnd_state << 1; + return rnd_state; +} + +static void say(const char *text, unsigned char f) +{ + if (sim_message) sim_message(text, f); +} + +void sim_reset(void) +{ + memset(t_n, 0, sizeof t_n); + clock_min = 6 * 60; + day = 1; + tick_in_min = 0; + stat_arrived = stat_failed = 0; +} + +unsigned char trips_active(void) +{ + unsigned char t, n = 0; + for (t = 0; t < MAX_TRIPS; ++t) if (t_n[t]) ++n; + return n; +} + +/* ---- routing */ + +/* First leg of a route from one floor to another: returns the shaft to take + (255 if there is no way) and the floor it goes to in *leg. Prefers one + shaft serving both floors, then a change between two shafts, then any + shaft that gets closer. */ +unsigned char plan(unsigned char from, unsigned char to, unsigned char *leg) +{ + unsigned char s, s2, best = 255, bestw = 255, lo, hi, t, d, bestd = 255; + + for (s = 0; s < MAX_SHAFTS; ++s) /* one shaft all the way */ + if (SERVES(from, s) && SERVES(to, s)) { + d = s_kind[s] == SK_ELEV ? s_wn[s][from - s_lo[s]] : 8; + if (d < bestw) { bestw = d; best = s; } + } + if (best != 255) { *leg = to; return best; } + + for (s = 0; s < MAX_SHAFTS; ++s) { /* change once */ + if (!SERVES(from, s)) continue; + for (s2 = 0; s2 < MAX_SHAFTS; ++s2) { + if (s2 == s || !SERVES(to, s2)) continue; + lo = s_lo[s] > s_lo[s2] ? s_lo[s] : s_lo[s2]; + hi = s_hi[s] < s_hi[s2] ? s_hi[s] : s_hi[s2]; + if (lo > hi) continue; + t = to < lo ? lo : to > hi ? hi : to; /* change floor nearest the goal */ + if (t == from) continue; + d = t > to ? t - to : to - t; + d += t > from ? t - from : from - t; + if (d < bestd) { bestd = d; best = s; *leg = t; } + } + } + if (best != 255) return best; + + for (s = 0; s < MAX_SHAFTS; ++s) { /* get closer, then think again */ + if (!SERVES(from, s)) continue; + t = to < s_lo[s] ? s_lo[s] : to > s_hi[s] ? s_hi[s] : to; + if (t == from) continue; + d = t > to ? t - to : to - t; + if (d < bestd) { bestd = d; best = s; *leg = t; } + } + return best; +} + +unsigned char reachable(unsigned char from, unsigned char to) +{ + unsigned char legs, leg; + + for (legs = 0; legs < 6 && from != to; ++legs) { + if (plan(from, to, &leg) == 255) return 0; + from = leg; + } + return from == to; +} + +/* ---- waiting at elevator doors */ + +static void waiting_changed(unsigned char s, unsigned char f) +{ + unsigned char i = f - s_lo[s], n = s_wn[s][i], v; + + v = n == 0 ? 0 : n < 5 ? 1 : n < 12 ? 2 : 3; + if (v != s_wv[s][i]) { + s_wv[s][i] = v; + draw_shaft_door(s, f); + } +} + +static void unwait(unsigned char t) +{ + unsigned char s = t_shaft[t], i = t_cur[t] - s_lo[s]; + + s_wn[s][i] -= t_n[t]; + waiting_changed(s, t_cur[t]); +} + +/* ---- trips */ + +static void account(unsigned char t, unsigned char extra) +{ + unsigned char r = t_room[t]; + if (r != NOBODY && r_type[r]) { + r_stress[r] += t_wait[t] + extra; + if (r_done[r] < 255) ++r_done[r]; + } +} + +static void fail_trip(unsigned char t) +{ + unsigned char r = t_room[t], n = t_n[t]; + + stat_failed += n; + account(t, 60); + if (r != NOBODY && r_type[r]) { + r_trav[r] -= n; + if (t_kind[t] == K_TO_FOOD || t_kind[t] == K_BACK) r_in[r] += n; /* went home */ + } + t_n[t] = 0; +} + +static void start_leg(unsigned char t); + +static void arrive_final(unsigned char t) +{ + unsigned char r = t_room[t], n = t_n[t], fd; + + stat_arrived += n; + switch (t_kind[t]) { + case K_TO_FOOD: + fd = t_food[t]; + if (r_type[fd] == RT_FOOD) { + money += 15L * n; + if (r_done[fd] < 255) ++r_done[fd]; + } + t_kind[t] = K_BACK; + t_dest[t] = r == NOBODY ? GROUND : r_floor[r]; + t_state[t] = S_EAT; + t_timer[t] = 20 + (rand8() & 15); + t_legs[t] = 0; + return; + case K_TO_ROOM: + case K_BACK: + if (r != NOBODY && r_type[r]) { r_in[r] += n; r_trav[r] -= n; } + break; + case K_LEAVE: + if (r != NOBODY && r_type[r]) r_trav[r] -= n; + break; + } + account(t, 0); + t_n[t] = 0; +} + +static void arrive_leg(unsigned char t) +{ + t_cur[t] = t_leg[t]; + t_x[t] = s_x[t_shaft[t]]; + if (t_cur[t] == t_dest[t]) arrive_final(t); + else start_leg(t); +} + +static void start_leg(unsigned char t) +{ + unsigned char s, leg, d; + + if (t_cur[t] == t_dest[t]) { arrive_final(t); return; } + s = plan(t_cur[t], t_dest[t], &leg); + if (s == 255 || ++t_legs[t] > 6) { fail_trip(t); return; } + t_shaft[t] = s; + t_leg[t] = leg; + t_state[t] = S_WALK; + d = t_x[t] > s_x[s] ? t_x[t] - s_x[s] : s_x[s] - t_x[t]; + t_timer[t] = 1 + (d >> 3); +} + +static void walk_done(unsigned char t) +{ + unsigned char s = t_shaft[t], f = t_cur[t]; + + if (s_kind[s] == SK_STAIRS) { + t_state[t] = S_STAIR; + t_timer[t] = 3; + return; + } + t_state[t] = S_WAIT; + s_wn[s][f - s_lo[s]] += t_n[t]; + if (t_leg[t] > f) SETB(s_up[s], f); else SETB(s_dn[s], f); + s_wake[s] = 1; + waiting_changed(s, f); +} + +/* Start a trip; 0 if the trip pool is full. Moves people out of the room. */ +static unsigned char spawn(unsigned char r, unsigned char kind, unsigned char from, + unsigned char to, unsigned char n, unsigned char food) +{ + unsigned char t, i; + + for (i = 0; i < MAX_TRIPS; ++i) { + t = (next_slot + i) & (MAX_TRIPS - 1); + if (!t_n[t]) break; + } + if (i == MAX_TRIPS) return 0; + next_slot = t + 1; + if (r != NOBODY) { + if (kind != K_TO_ROOM) r_in[r] -= n; + r_trav[r] += n; + t_x[t] = r_x[r] + 3; + } else { + t_x[t] = 255; /* from the street: walking time below */ + } + t_n[t] = n; t_room[t] = r; t_kind[t] = kind; t_cur[t] = from; t_dest[t] = to; + t_food[t] = food; t_wait[t] = 0; t_legs[t] = 0; + start_leg(t); + if (t_n[t] && t_x[t] == 255) t_timer[t] = 2; + return 1; +} + +void cancel_trips_room(unsigned char r) +{ + unsigned char t; + + for (t = 0; t < MAX_TRIPS; ++t) { + if (!t_n[t]) continue; + if (t_room[t] == r) { + if (t_state[t] == S_WAIT) unwait(t); + if (t_state[t] == S_RIDE) s_load[t_shaft[t]] -= t_n[t]; + t_n[t] = 0; + } else if (t_food[t] == r && t_kind[t] == K_TO_FOOD) { + t_kind[t] = K_BACK; /* the restaurant is gone: go back */ + t_dest[t] = t_room[t] == NOBODY ? GROUND : r_floor[t_room[t]]; + } + } + r_in[r] = r_trav[r] = 0; +} + +void cancel_trips_shaft(unsigned char s) +{ + unsigned char t; + + for (t = 0; t < MAX_TRIPS; ++t) { + if (!t_n[t] || t_shaft[t] != s) continue; + if (t_state[t] == S_RIDE) t_cur[t] = s_lo[s] + ((s_pos[s] + 8) >> 4); + if (t_state[t] >= S_WALK && t_state[t] <= S_STAIR) start_leg(t); + } +} + +/* ---- elevators */ + +/* Requests (calls or destinations) as one mask, rebuilt when they change. */ +static unsigned char req[8]; +static void requests(unsigned char s) +{ + unsigned char i, *a = s_dst[s], *b = s_up[s], *c = s_dn[s]; + for (i = 0; i < 8; ++i) req[i] = a[i] | b[i] | c[i]; +} + +static const unsigned char above_mask[8] = { 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00 }; +static const unsigned char below_mask[8] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F }; + +/* Any request beyond floor f in direction dir? (byte at a time) */ +static unsigned char ahead(unsigned char s, unsigned char f, signed char dir) +{ + unsigned char k = f >> 3, i; + + requests(s); + if (dir > 0) { + if (req[k] & above_mask[f & 7]) return 1; + for (i = k + 1; i < 8; ++i) if (req[i]) return 1; + } else { + if (req[k] & below_mask[f & 7]) return 1; + for (i = 0; i < k; ++i) if (req[i]) return 1; + } + return 0; +} + +static void stop_at(unsigned char s, unsigned char f) +{ + unsigned char t, n, boarded = 0, up = 0, dn = 0; + signed char dir = s_dir[s]; + + for (t = 0; t < MAX_TRIPS; ++t) /* off */ + if (t_n[t] && t_state[t] == S_RIDE && t_shaft[t] == s && t_leg[t] == f) { + s_load[s] -= t_n[t]; + arrive_leg(t); + } + CLRB(s_dst[s], f); + + if (dir > 0 && !ahead(s, f, 1)) dir = ahead(s, f, -1) ? -1 : 0; + else if (dir < 0 && !ahead(s, f, -1)) dir = ahead(s, f, 1) ? 1 : 0; + if (dir == 0) dir = BIT(s_up[s], f) ? 1 : BIT(s_dn[s], f) ? -1 : 0; + + for (t = 0; t < MAX_TRIPS; ++t) { /* on */ + if (!t_n[t] || t_state[t] != S_WAIT || t_shaft[t] != s || t_cur[t] != f) continue; + n = t_n[t]; + if ((t_leg[t] > f ? 1 : -1) == dir && s_load[s] + n <= CAR_CAP) { + unwait(t); + t_state[t] = S_RIDE; + s_load[s] += n; + SETB(s_dst[s], t_leg[t]); + boarded += n; + } else if (t_leg[t] > f) up = 1; + else dn = 1; + } + if (up) SETB(s_up[s], f); else CLRB(s_up[s], f); + if (dn) SETB(s_dn[s], f); else CLRB(s_dn[s], f); + s_dir[s] = dir; + s_doors[s] = 12 + boarded; +} + +static void elev_tick(unsigned char s) +{ + unsigned char f, g, best, bd, d; + signed char dir; + + if (s_doors[s]) { + if (--s_doors[s] == 0 && s_dir[s]) { + f = s_lo[s] + (s_pos[s] >> 4); + if (!ahead(s, f, s_dir[s])) s_dir[s] = 0; + } + return; + } + dir = s_dir[s]; + if (dir == 0) { /* idle: go to the nearest request */ + if (!s_wake[s]) return; /* asleep until someone calls */ + requests(s); + if (!(req[0] | req[1] | req[2] | req[3] | req[4] | req[5] | req[6] | req[7])) { + s_wake[s] = 0; + return; + } + f = s_lo[s] + (s_pos[s] >> 4); + best = 255; bd = 255; + for (g = s_lo[s]; g <= s_hi[s]; ++g) + if (BIT(req, g)) { + d = g > f ? g - f : f - g; + if (d < bd) { bd = d; best = g; } + } + if (best == 255) return; + if (best == f) { stop_at(s, f); return; } + s_dir[s] = best > f ? 1 : -1; + return; + } + if (dir > 0) s_pos[s] += 2; else s_pos[s] -= 2; + if (s_pos[s] & 15) return; + f = s_lo[s] + (s_pos[s] >> 4); /* level with a floor */ + if (BIT(s_dst[s], f) || (dir > 0 && BIT(s_up[s], f)) || (dir < 0 && BIT(s_dn[s], f))) { + stop_at(s, f); + } else if (!ahead(s, f, dir)) { + if (BIT(s_up[s], f) || BIT(s_dn[s], f)) stop_at(s, f); + else s_dir[s] = 0; + } +} + +/* ---- people's days */ + +static unsigned char pick_food(void) +{ + return food_count ? foods[rand8() % food_count] : 255; +} + +static void room_minute(unsigned char r) +{ + unsigned char f = r_floor[r], n, k, fd, want; + unsigned m = clock_min; + + switch (r_type[r]) { + case RT_OFFICE: + if (!r_occ[r]) break; + k = 6 - r_in[r] - r_trav[r]; /* not here yet */ + if (k && m >= 450 && m < 600 && rand8() < (m < 570 ? 24 : 90)) { + n = k > 1 ? 1 + (rand8() & 1) : 1; + spawn(r, K_TO_ROOM, GROUND, f, n, 0); + } + if (m >= 720 && m < 780 && r_in[r] && rand8() < 10 && (fd = pick_food()) != 255) + spawn(r, K_TO_FOOD, f, r_floor[fd], 1, fd); + if (m == 1020) money += 1000; /* daily rent */ + if (r_in[r] && m >= 1020 && (m >= 1110 || rand8() < 28)) { + n = r_in[r] > 3 ? 3 : r_in[r]; + spawn(r, K_LEAVE, f, GROUND, n, 0); + } + break; + case RT_CONDO: + if (!r_occ[r]) break; + if (m >= 420 && m < 540 && r_in[r] > 1 && rand8() < 16) + spawn(r, K_LEAVE, f, GROUND, 1, 0); + k = 3 - r_in[r] - r_trav[r]; + if (k && m >= 1020 && m < 1260 && rand8() < (m < 1200 ? 16 : 90)) + spawn(r, K_TO_ROOM, GROUND, f, 1, 0); + if (m >= 1110 && m < 1200 && r_in[r] && rand8() < 6 && (fd = pick_food()) != 255) + spawn(r, K_TO_FOOD, f, r_floor[fd], 1, fd); + break; + case RT_FOOD: + if (((m >= 690 && m < 840) || (m >= 1050 && m < 1200)) && rand8() < 4 + (pop > 900 ? 116 : pop >> 3)) + spawn(NOBODY, K_TO_FOOD, GROUND, f, 1 + (rand8() & 1), r); + break; + } + + /* empty offices and condos fill when people can get to them */ + if (!r_occ[r] && m >= 360 && m < 1200 && rand8() < 3 && reachable(GROUND, f)) occupy(r); + + want = r_type[r] == RT_FOOD ? (m >= 600 && m < 1260) : (r_occ[r] && r_in[r]); + if (want != r_lit[r]) { + r_lit[r] = want; + draw_room(r); + } +} + +void occupy(unsigned char r) +{ + r_occ[r] = 1; + r_bad[r] = 0; + r_stress[r] = 0; + r_done[r] = 0; + if (r_type[r] == RT_CONDO) { + money += 150000L; + say("condo sold for $150000", r_floor[r]); + } else { + say("new office tenant", r_floor[r]); + } + update_stars(); +} + +static void new_day(void) +{ + unsigned char r, s; + unsigned avg; + + ++day; + for (r = 0; r < room_count; ++r) { + if (!r_type[r]) continue; + if (r_type[r] != RT_FOOD && r_occ[r]) { + avg = r_done[r] ? r_stress[r] / r_done[r] : 0; + r_eval[r] = avg < 15 ? 0 : avg < 40 ? 1 : 2; + if (r_eval[r] == 2 && ++r_bad[r] >= 2) { + cancel_trips_room(r); + r_occ[r] = 0; + r_bad[r] = 0; + say(r_type[r] == RT_OFFICE ? "office tenant moved out" : "condo owners moved out", r_floor[r]); + } else if (r_eval[r] != 2) { + r_bad[r] = 0; + } + } + r_stress[r] = 0; + r_done[r] = 0; + } + for (s = 0; s < MAX_SHAFTS; ++s) + if (s_kind[s] == SK_ELEV) money -= 300; /* upkeep */ + update_stars(); +} + +/* A minute's work is spread over its 20 ticks: each tick handles every 20th + room and trip, so a big tower doesn't stall one frame. */ +static void minute_part(unsigned char part) +{ + unsigned char r, t; + + if (part == 0) { + if (++clock_min == 1440) { clock_min = 0; new_day(); } + food_count = 0; + for (r = 0; r < room_count; ++r) + if (r_type[r] == RT_FOOD) foods[food_count++] = r; + } + for (r = part; r < room_count; r += TICKS_PER_MIN) + if (r_type[r]) room_minute(r); + + for (t = part; t < MAX_TRIPS; t += TICKS_PER_MIN) { + if (!t_n[t]) continue; + switch (t_state[t]) { + case S_WALK: + if (--t_timer[t] == 0) walk_done(t); + break; + case S_WAIT: + if (++t_wait[t] >= 180) { unwait(t); fail_trip(t); } /* gave up */ + break; + case S_STAIR: + if (--t_timer[t] == 0) arrive_leg(t); + break; + case S_EAT: + if (--t_timer[t] == 0) start_leg(t); + break; + } + } +} + +void sim_tick(void) +{ + unsigned char i; + + if (shafts_changed) { + shafts_changed = 0; + elev_count = 0; + for (i = 0; i < MAX_SHAFTS; ++i) + if (s_kind[i] == SK_ELEV) { elevs[elev_count++] = i; s_wake[i] = 1; } + } + for (i = 0; i < elev_count; ++i) elev_tick(elevs[i]); + minute_part(tick_in_min); + if (++tick_in_min >= TICKS_PER_MIN) tick_in_min = 0; +} diff --git a/projects/highrise/src/tower.h b/projects/highrise/src/tower.h new file mode 100644 index 0000000..a533fde --- /dev/null +++ b/projects/highrise/src/tower.h @@ -0,0 +1,124 @@ +/* Highrise: shared definitions. */ +#ifndef TOWER_H +#define TOWER_H + +#include "gfx.h" + +/* ---- world geometry */ +#define WORLD_W 80 /* columns (two screens) */ +#define FLOORS 64 /* 0-3 basements B4-B1, 4 = ground, 5-63 = 2F-60F */ +#define GROUND 4 +/* The map holds one byte per cell: a pair id (gfx.h P_*), whose top and bottom + tiles are pair_a[] and pair_b[]. Row 0 is floor 63. $A000-$B3FF. */ +#define MAP ((unsigned char *)0xA000) +#define PROW(f) ((unsigned char)(FLOORS - 1 - (f))) /* map row of floor f */ +#define TROW(f) ((unsigned char)((FLOORS - 1 - (f)) * 2)) /* tile row of its top half */ +/* what occupies each cell (C_*): 64 x 80 bytes at $B400-$C7FF */ +#define cellobj ((unsigned char (*)[WORLD_W])0xB400) + +/* ---- screen: bank 3, charset $E000, screen $E800 (RAM under the Kernal) */ +#define SCREEN ((unsigned char *)0xE800) +#define COLRAM ((unsigned char *)0xD800) +#define VIEW_ROW 2 /* first screen row of the tower view */ +#define VIEW_ROWS 20 +#define VIEW_FLOORS 10 +#define VIEW_COLS 40 + +/* ---- what occupies a cell (cellobj) */ +#define C_EMPTY 0 +#define C_FLOOR 1 +#define C_LOBBY 2 +#define C_ROOM 3 /* + room index (0-199) */ +#define C_SHAFT 208 /* + shaft index (0-31) */ +#define MAX_ROOMS 200 +#define MAX_SHAFTS 32 + +/* ---- tools (toolbar order) */ +enum { TOOL_FLOOR, TOOL_LOBBY, TOOL_STAIRS, TOOL_ELEV, TOOL_OFFICE, TOOL_CONDO, + TOOL_FOOD, TOOL_CLEAR, TOOL_COUNT }; + +/* ---- room types */ +#define RT_OFFICE 1 +#define RT_CONDO 2 +#define RT_FOOD 3 + +/* ---- shaft kinds */ +#define SK_ELEV 1 +#define SK_STAIRS 2 + +/* ---- trips: a group of people going somewhere */ +#define MAX_TRIPS 128 +#define K_TO_ROOM 1 /* arrive at their own room */ +#define K_LEAVE 2 /* go to the lobby and leave the tower */ +#define K_TO_FOOD 3 /* go eat, then come back */ +#define K_BACK 4 /* return from eating */ +#define S_WALK 1 +#define S_WAIT 2 +#define S_RIDE 3 +#define S_STAIR 4 +#define S_EAT 5 +#define NOBODY 255 /* trip owner for visitors from outside */ + +#define CAR_CAP 15 +#define TICKS_PER_MIN 20 /* a sim tick is one frame at speed 1 */ + +/* ---- world.c */ +extern unsigned char view_f, view_x; /* floor at the top of the view, first column */ +extern signed long money; +extern unsigned char stars; +extern unsigned pop; +extern unsigned char max_floor; /* highest floor allowed at this rating */ + +extern unsigned char r_type[MAX_ROOMS], r_floor[MAX_ROOMS], r_x[MAX_ROOMS]; +extern unsigned char r_occ[MAX_ROOMS], r_in[MAX_ROOMS], r_trav[MAX_ROOMS]; +extern unsigned char r_lit[MAX_ROOMS], r_eval[MAX_ROOMS], r_bad[MAX_ROOMS]; +extern unsigned char r_done[MAX_ROOMS]; +extern unsigned r_stress[MAX_ROOMS]; +extern unsigned char room_count; /* highest used index + 1 */ + +extern unsigned char s_kind[MAX_SHAFTS], s_x[MAX_SHAFTS], s_lo[MAX_SHAFTS], s_hi[MAX_SHAFTS]; +extern unsigned s_pos[MAX_SHAFTS]; /* car height in pixels above floor s_lo */ +extern signed char s_dir[MAX_SHAFTS]; +extern unsigned char s_doors[MAX_SHAFTS], s_load[MAX_SHAFTS]; +extern unsigned char s_dst[MAX_SHAFTS][8], s_up[MAX_SHAFTS][8], s_dn[MAX_SHAFTS][8]; +extern unsigned char s_wn[MAX_SHAFTS][32]; /* people waiting, per floor - s_lo */ +extern unsigned char s_wv[MAX_SHAFTS][32]; /* door tile variant shown */ +extern unsigned s_drawn[MAX_SHAFTS]; /* car's screen-drawn y, or NOT_DRAWN */ +extern unsigned long floor_shafts[FLOORS]; /* bit s: shaft s serves this floor */ +#define NOT_DRAWN 0xFFFF + +void world_reset(void); +unsigned char room_width(unsigned char type); +void put_pair(unsigned char f, unsigned char x, unsigned char p); +void draw_room(unsigned char r); +void draw_shaft_door(unsigned char s, unsigned char f); +unsigned char build(unsigned char tool, unsigned char f, unsigned char x0, unsigned char x1, + unsigned char f1); +long build_cost(unsigned char tool, unsigned char f, unsigned char x0, unsigned char x1, + unsigned char f1); +extern const char *build_error; +unsigned char clear_at(unsigned char f, unsigned char x); +void update_stars(void); + +/* ---- sim.c */ +extern unsigned clock_min; /* minute of the day, 0-1439 */ +extern unsigned day; +extern unsigned char t_n[MAX_TRIPS]; +extern unsigned long stat_arrived, stat_failed; +extern unsigned char shafts_changed; +void sim_reset(void); +void sim_tick(void); +unsigned char plan(unsigned char from, unsigned char to, unsigned char *leg); +unsigned char reachable(unsigned char from, unsigned char to); +void occupy(unsigned char r); +void cancel_trips_room(unsigned char r); +void cancel_trips_shaft(unsigned char s); +unsigned char trips_active(void); +extern void (*sim_message)(const char *text, unsigned char f); + +/* ---- render.s */ +extern unsigned char sky_color; +void draw_view(void); /* uses view_f, view_x */ +void irq_install(void); + +#endif diff --git a/projects/highrise/src/world.c b/projects/highrise/src/world.c new file mode 100644 index 0000000..41a2659 --- /dev/null +++ b/projects/highrise/src/world.c @@ -0,0 +1,329 @@ +/* Highrise: the tower's cells, tiles, building and bulldozing. */ +#include +#include "tower.h" + +unsigned char view_f, view_x; +signed long money; +unsigned char stars; +unsigned pop; +unsigned char max_floor; +const char *build_error; + +unsigned char r_type[MAX_ROOMS], r_floor[MAX_ROOMS], r_x[MAX_ROOMS]; +unsigned char r_occ[MAX_ROOMS], r_in[MAX_ROOMS], r_trav[MAX_ROOMS]; +unsigned char r_lit[MAX_ROOMS], r_eval[MAX_ROOMS], r_bad[MAX_ROOMS]; +unsigned char r_done[MAX_ROOMS]; +unsigned r_stress[MAX_ROOMS]; +unsigned char room_count; + +unsigned char s_kind[MAX_SHAFTS], s_x[MAX_SHAFTS], s_lo[MAX_SHAFTS], s_hi[MAX_SHAFTS]; +unsigned s_pos[MAX_SHAFTS]; +signed char s_dir[MAX_SHAFTS]; +unsigned char s_doors[MAX_SHAFTS], s_load[MAX_SHAFTS]; +unsigned char s_dst[MAX_SHAFTS][8], s_up[MAX_SHAFTS][8], s_dn[MAX_SHAFTS][8]; +unsigned char s_wn[MAX_SHAFTS][32]; +unsigned char s_wv[MAX_SHAFTS][32]; +unsigned s_drawn[MAX_SHAFTS]; +unsigned long floor_shafts[FLOORS]; + +unsigned char room_width(unsigned char type) +{ + return type == RT_OFFICE ? 6 : 8; +} + +/* Set one map cell, and draw it if it is in view. */ +void put_pair(unsigned char f, unsigned char x, unsigned char p) +{ + unsigned char d, sc, t; + unsigned o; + + MAP[(unsigned)PROW(f) * WORLD_W + x] = p; + d = view_f - f; + sc = x - view_x; + if (d < VIEW_FLOORS && sc < VIEW_COLS) { + o = (unsigned)(VIEW_ROW + d * 2) * 40 + sc; + t = pair_a[p]; + SCREEN[o] = t; + COLRAM[o] = tile_col[t]; + t = pair_b[p]; + SCREEN[o + 40] = t; + COLRAM[o + 40] = tile_col[t]; + } +} + +/* The scenery in a cell when nothing is built there. */ +static unsigned char bg_pair(unsigned char f, unsigned char x) +{ + unsigned char k; + + if (f >= GROUND) { + if (f == GROUND && (x == 22 || x == 25 || x == 53 || x == 57)) return P_TREE; + if (f >= GROUND + 6) { /* clouds, in a few places */ + k = (unsigned char)(x + f * 23) % 29; + if (k < 3) return P_CLOUD_L + k; + } + return P_SKY; + } + if (f == GROUND - 1) return P_DIRT_TOP; + return ((x + f) & 1) ? P_DIRT : P_DIRT2; +} + +/* Redraw one cell from what occupies it. */ +static void draw_cell(unsigned char f, unsigned char x) +{ + unsigned char o = cellobj[f][x], p, i, s; + + if (o == C_EMPTY) { + p = bg_pair(f, x); + } else if (o == C_FLOOR) { + p = P_FLOOR; + } else if (o == C_LOBBY) { + p = (x & 3) ? P_LOBBY : P_LOBBY2; + } else if (o >= C_SHAFT) { + s = o - C_SHAFT; + i = x - s_x[s]; + if (s_kind[s] == SK_STAIRS) p = i ? P_ST_R : P_ST_L; + else p = (i ? P_SH_R0 : P_SH_L0) + 2 * s_wv[s][f - s_lo[s]]; + s_drawn[s] |= 0x8000; /* car must be redrawn over it */ + } else { + draw_room(o - C_ROOM); + return; + } + put_pair(f, x, p); +} + +void draw_room(unsigned char r) +{ + const unsigned char *a; + unsigned char i, w = room_width(r_type[r]), x = r_x[r], f = r_floor[r]; + + switch (r_type[r]) { + case RT_OFFICE: a = r_lit[r] ? room_off : room_off_d; break; + case RT_CONDO: a = r_lit[r] ? room_con : room_con_d; break; + default: a = r_lit[r] ? room_food : room_food_d; break; + } + for (i = 0; i < w; ++i) put_pair(f, x + i, a[i]); +} + +void draw_shaft_door(unsigned char s, unsigned char f) +{ + unsigned char v = s_wv[s][f - s_lo[s]], x = s_x[s]; + + put_pair(f, x, P_SH_L0 + 2 * v); + put_pair(f, x + 1, P_SH_R0 + 2 * v); + s_drawn[s] |= 0x8000; +} + +void world_reset(void) +{ + unsigned char f, x; + + memset(cellobj, C_EMPTY, FLOORS * WORLD_W); + memset(r_type, 0, sizeof r_type); + memset(s_kind, 0, sizeof s_kind); + shafts_changed = 1; + memset(floor_shafts, 0, sizeof floor_shafts); + room_count = 0; + for (f = 0; f < FLOORS; ++f) + for (x = 0; x < WORLD_W; ++x) + MAP[(unsigned)PROW(f) * WORLD_W + x] = bg_pair(f, x); + money = 500000L; + stars = 1; + pop = 0; + max_floor = GROUND + 14; +} + +/* ---- building */ + +static const unsigned char max_by_stars[6] = { 0, GROUND + 14, GROUND + 24, GROUND + 39, FLOORS - 1, FLOORS - 1 }; + +void update_stars(void) +{ + unsigned char r, s; + + pop = 0; + for (r = 0; r < room_count; ++r) + if (r_type[r] && r_occ[r]) pop += r_type[r] == RT_OFFICE ? 6 : r_type[r] == RT_CONDO ? 3 : 0; + s = pop >= 1500 ? 5 : pop >= 700 ? 4 : pop >= 300 ? 3 : pop >= 100 ? 2 : 1; + if (s > stars) { /* a rating, once earned, stays */ + stars = s; + if (sim_message) sim_message("the tower earned a new star", 255); + } + max_floor = max_by_stars[stars]; +} + +static unsigned char walkable(unsigned char f, unsigned char x) +{ + unsigned char o = cellobj[f][x]; + return o == C_FLOOR || o == C_LOBBY; +} + +static unsigned char lo_x, hi_x, lo_f, hi_f; + +static void order(unsigned char f, unsigned char x0, unsigned char x1, unsigned char f1) +{ + if (x0 <= x1) { lo_x = x0; hi_x = x1; } else { lo_x = x1; hi_x = x0; } + if (f <= f1) { lo_f = f; hi_f = f1; } else { lo_f = f1; hi_f = f; } +} + +/* Cost of a build (0 if nothing to do), or -1 with build_error set. */ +long build_cost(unsigned char tool, unsigned char f, unsigned char x0, unsigned char x1, + unsigned char f1) +{ + unsigned char x, n = 0, w, ff; + + order(f, x0, x1, f1); + if (tool != TOOL_ELEV) { lo_f = hi_f = f; } + if (hi_f > max_floor) { build_error = "more stars needed to build that high"; return -1; } + if (lo_f < GROUND && stars < 2) { build_error = "basements need 2 stars"; return -1; } + switch (tool) { + case TOOL_FLOOR: + case TOOL_LOBBY: + if (tool == TOOL_LOBBY && f != GROUND) { build_error = "the lobby goes on the ground floor"; return -1; } + for (x = lo_x; x <= hi_x; ++x) { + if (cellobj[f][x] == C_EMPTY) { + if (f > GROUND && cellobj[f - 1][x] == C_EMPTY) { build_error = "a floor needs support below"; return -1; } + if (f < GROUND && cellobj[f + 1][x] == C_EMPTY) { build_error = "dig down from the floor above"; return -1; } + ++n; + } else if (tool == TOOL_LOBBY && cellobj[f][x] == C_FLOOR) { + ++n; + } else if (cellobj[f][x] != (tool == TOOL_LOBBY ? C_LOBBY : C_FLOOR)) { + build_error = "something is in the way"; return -1; + } + } + if (tool == TOOL_LOBBY) return 2000L * n; + return (f < GROUND ? 1000L : 500L) * n; + case TOOL_OFFICE: + case TOOL_CONDO: + case TOOL_FOOD: + w = room_width(tool == TOOL_OFFICE ? RT_OFFICE : tool == TOOL_CONDO ? RT_CONDO : RT_FOOD); + if (x0 + w > WORLD_W) { build_error = "no room at the edge"; return -1; } + if (tool == TOOL_CONDO && f <= GROUND) { build_error = "condos go above the ground floor"; return -1; } + for (x = x0; x < x0 + w; ++x) + if (cellobj[f][x] != C_FLOOR) { build_error = "rooms need empty floor"; return -1; } + return tool == TOOL_OFFICE ? 40000L : tool == TOOL_CONDO ? 80000L : 50000L; + case TOOL_STAIRS: + if (x0 + 2 > WORLD_W || f + 1 >= FLOORS) { build_error = "no room"; return -1; } + if (f + 1 > max_floor) { build_error = "more stars needed to build that high"; return -1; } + for (x = x0; x < x0 + 2; ++x) + if (!walkable(f, x) || !walkable(f + 1, x)) { build_error = "stairs need floor here and above"; return -1; } + return 5000L; + case TOOL_ELEV: + if (x0 + 2 > WORLD_W) { build_error = "no room"; return -1; } + if (hi_f == lo_f) { build_error = "drag up or down to span floors"; return -1; } + if (hi_f - lo_f > 29) { build_error = "an elevator spans 30 floors at most"; return -1; } + for (ff = lo_f; ff <= hi_f; ++ff) + for (x = x0; x < x0 + 2; ++x) + if (!walkable(ff, x)) { build_error = "elevators need floor on every level"; return -1; } + return 15000L + 1500L * (hi_f - lo_f + 1); + } + return 0; +} + +static unsigned char new_shaft(unsigned char kind, unsigned char x, unsigned char lo, unsigned char hi) +{ + unsigned char s, f; + + for (s = 0; s < MAX_SHAFTS && s_kind[s]; ++s) ; + if (s == MAX_SHAFTS) { build_error = "too many elevators and stairs"; return 255; } + s_kind[s] = kind; s_x[s] = x; s_lo[s] = lo; s_hi[s] = hi; + s_pos[s] = 0; s_dir[s] = 0; s_doors[s] = 0; s_load[s] = 0; + memset(s_dst[s], 0, 8); memset(s_up[s], 0, 8); memset(s_dn[s], 0, 8); + memset(s_wn[s], 0, 32); memset(s_wv[s], 0, 32); + s_drawn[s] = NOT_DRAWN; + shafts_changed = 1; + for (f = lo; f <= hi; ++f) { + floor_shafts[f] |= 1UL << s; + if (kind == SK_ELEV || f == lo) { + cellobj[f][x] = cellobj[f][x + 1] = C_SHAFT + s; + draw_cell(f, x); draw_cell(f, x + 1); + } + } + return s; +} + +/* Build; returns 1 on success, else 0 with build_error set. */ +unsigned char build(unsigned char tool, unsigned char f, unsigned char x0, unsigned char x1, + unsigned char f1) +{ + long cost = build_cost(tool, f, x0, x1, f1); + unsigned char x, r, t; + + if (cost < 0) return 0; + if (cost == 0) { build_error = "already built"; return 0; } + if (cost > money) { build_error = "not enough money"; return 0; } + switch (tool) { + case TOOL_FLOOR: + case TOOL_LOBBY: + for (x = lo_x; x <= hi_x; ++x) { + cellobj[f][x] = tool == TOOL_LOBBY ? C_LOBBY : C_FLOOR; + draw_cell(f, x); + } + break; + case TOOL_STAIRS: + if (new_shaft(SK_STAIRS, x0, f, f + 1) == 255) return 0; + break; + case TOOL_ELEV: + if (new_shaft(SK_ELEV, x0, lo_f, hi_f) == 255) return 0; + break; + default: + for (r = 0; r < MAX_ROOMS && r_type[r]; ++r) ; + if (r == MAX_ROOMS) { build_error = "too many rooms"; return 0; } + t = tool == TOOL_OFFICE ? RT_OFFICE : tool == TOOL_CONDO ? RT_CONDO : RT_FOOD; + r_type[r] = t; r_floor[r] = f; r_x[r] = x0; + r_occ[r] = t == RT_FOOD; r_in[r] = 0; r_trav[r] = 0; r_lit[r] = 0; + r_eval[r] = 0; r_bad[r] = 0; r_done[r] = 0; r_stress[r] = 0; + if (r >= room_count) room_count = r + 1; + for (x = x0; x < x0 + room_width(t); ++x) cellobj[f][x] = C_ROOM + r; + draw_room(r); + break; + } + money -= cost; + return 1; +} + +/* Bulldoze what is at (f, x). Returns 1 if something went. */ +unsigned char clear_at(unsigned char f, unsigned char x) +{ + unsigned char o = cellobj[f][x], i, s, ff, w; + + if (o == C_EMPTY) { build_error = "nothing here"; return 0; } + if (o >= C_SHAFT) { + s = o - C_SHAFT; + for (ff = s_lo[s]; ff <= s_hi[s]; ++ff) floor_shafts[ff] &= ~(1UL << s); + cancel_trips_shaft(s); /* re-route without this shaft */ + for (ff = s_lo[s]; ff <= s_hi[s]; ++ff) { + for (i = 0; i < 2; ++i) + if (cellobj[ff][s_x[s] + i] == o) { + cellobj[ff][s_x[s] + i] = ff == GROUND ? C_LOBBY : C_FLOOR; + draw_cell(ff, s_x[s] + i); + } + } + s_kind[s] = 0; + shafts_changed = 1; + return 1; + } + if (o >= C_ROOM) { + i = o - C_ROOM; + cancel_trips_room(i); + w = room_width(r_type[i]); + r_type[i] = 0; + for (x = r_x[i]; x < r_x[i] + w; ++x) { + cellobj[f][x] = C_FLOOR; + draw_cell(f, x); + } + update_stars(); + return 1; + } + /* bare floor or lobby: only if nothing rests on it */ + if (f >= GROUND && f + 1 < FLOORS && cellobj[f + 1][x] != C_EMPTY) { build_error = "floors above rest on it"; return 0; } + if (f <= GROUND && f > 0 && cellobj[f - 1][x] != C_EMPTY && f - 1 < GROUND) { + build_error = "dig out the floors below first"; return 0; + } + if (f > 0 && cellobj[f - 1][x] >= C_SHAFT && s_kind[cellobj[f - 1][x] - C_SHAFT] == SK_STAIRS) { + build_error = "stairs lead up here"; return 0; + } + cellobj[f][x] = C_EMPTY; + draw_cell(f, x); + return 1; +} diff --git a/projects/highrise/tools/gen_gfx.py b/projects/highrise/tools/gen_gfx.py new file mode 100644 index 0000000..82ff3c8 --- /dev/null +++ b/projects/highrise/tools/gen_gfx.py @@ -0,0 +1,285 @@ +"""Graphics for Highrise: writes src/gfx.c and src/gfx.h. + +Characters 0-63 are the C64's own upper-case font (copied from character ROM +at startup); this script defines the tiles from 64 up and the sprites. + +Tiles are multicolor characters, 4 double-wide pixels x 8 lines: + '.' = $D021 (sky: changes with the time of day; windows show it) + 'd' = $D022 dark grey (structure) 'l' = $D023 brown (wood, earth) + 'c' = the tile's own color (0-7) from color RAM + + python projects/highrise/tools/gen_gfx.py +""" +import os + +FIRST = 64 +tiles = [] # (name, rows[8], color) +names = {} + + +def add(name, rows, color=0): + assert len(rows) == 8 and all(len(r) == 4 for r in rows), name + names[name] = FIRST + len(tiles) + tiles.append((name, rows, color)) + + +def mirror(rows): + return [r[::-1] for r in rows] + + +def dark(rows): + """Lights off: walls go dark grey; windows still show the sky.""" + return [r.replace('c', 'd') for r in rows] + + +SLAB_A = 'dddd' +# ---- outdoors +add('SKY', ['....'] * 8) +add('CLOUD_L', ['....', '....', '...c', '.ccc', 'cccc', '.ccc', '....', '....'], 1) +add('CLOUD_M', ['....', '.cc.', 'cccc', 'cccc', 'cccc', 'cccc', '....', '....'], 1) +add('CLOUD_R', ['....', '....', 'c...', 'ccc.', 'cccc', 'cc..', '....', '....'], 1) +add('DIRT', ['ldll', 'llll', 'lldl', 'llll', 'dlll', 'llll', 'llld', 'llll']) +add('DIRT2', ['llll', 'lldl', 'llll', 'dlll', 'llll', 'llld', 'llll', 'ldll']) +add('DIRT_TOP', ['cccc', 'cccc', 'ldll', 'llll', 'lldl', 'llll', 'dlll', 'llll'], 5) +add('TREE_T', ['....', '.cc.', 'cccc', 'cccc', 'cccc', 'cccc', 'cccc', '.cc.'], 5) +add('TREE_B', ['.cc.', '.l..', '.l..', '.l..', '.l..', '.l..', '.l..', '.l..'], 5) + +# ---- structure +add('FLOOR_A', [SLAB_A] + ['cccc'] * 7, 0) +add('FLOOR_B', ['cccc'] * 7 + ['dddd'], 0) +add('LOBBY_A', [SLAB_A] + ['cccc'] * 7, 3) +add('LOBBY_A2', [SLAB_A] + ['dccc'] * 7, 3) +add('LOBBY_B', ['cccc'] * 6 + ['llll', 'dddd'], 3) +add('LOBBY_B2', ['dccc'] * 6 + ['llll', 'dddd'], 3) + +# ---- rooms: each has a lit and a dark version +def room(prefix, a_tiles, b_tiles, color): + for lit in (True, False): + suffix = '' if lit else '_D' + for n, rows in a_tiles + b_tiles: + add(prefix + n + suffix, rows if lit else dark(rows), color if lit else 6) + + +WIN_L = ['dddd', 'dccc', 'dc..', 'dc..', 'dc..', 'dc..', 'dccc', 'dccc'] +WIN_M = ['dddd', 'cccc', 'c..c', 'c..c', 'c..c', 'c..c', 'cccc', 'cccc'] +WALL_L = ['dccc'] * 7 + ['dddd'] +WALL_R = ['cccd'] * 7 + ['dddd'] +room('OFF_', [ + ('AL', WIN_L), ('AM', WIN_M), ('AR', mirror(WIN_L)), +], [ + ('BL', WALL_L), + ('B1', ['cccc', 'cddc', 'cddc', 'llll', 'cllc', 'cllc', 'cllc', 'dddd']), # desk + screen + ('B2', ['cccc', 'cccc', 'cccc', 'cccc', 'cdcc', 'cdcc', 'cddc', 'dddd']), # chair + ('BR', WALL_R), +], 1) +room('CON_', [ + ('AL', ['dddd', 'dccc', 'dc..', 'dc..', 'dc..', 'dccc', 'dccc', 'dccc']), + ('AW', ['dddd', 'cccc', 'c..c', 'c..c', 'c..c', 'cccc', 'cccc', 'cccc']), + ('AX', ['dddd', 'cccc', 'cccc', 'cllc', 'cllc', 'cccc', 'cccc', 'cccc']), # picture + ('AR', ['dddd', 'cccd', '..cd', '..cd', '..cd', 'cccd', 'cccd', 'cccd']), +], [ + ('BL', WALL_L), + ('BED1', ['cccc', 'cccc', 'cccc', 'lccc', 'lddd', 'llll', 'lccl', 'dddd']), + ('BED2', ['cccc', 'cccc', 'cccc', 'cccc', 'dddl', 'llll', 'lccl', 'dddd']), + ('PLANT', ['cccc', 'cccc', 'cddc', 'dddd', 'cddc', 'clcc', 'cllc', 'dddd']), + ('SOFA1', ['cccc', 'cccc', 'cccc', 'llll', 'ldll', 'llll', 'lccl', 'dddd']), + ('SOFA2', ['cccc', 'cccc', 'cccc', 'llll', 'llld', 'llll', 'lccl', 'dddd']), + ('BR', WALL_R), +], 7) +room('FOOD_', [ + ('AL', WIN_L), ('AW', WIN_M), + ('S1', ['dddd', 'llll', 'lccl', 'lcll', 'lccl', 'llll', 'cccc', 'cccc']), + ('S2', ['dddd', 'llll', 'lcll', 'lccl', 'lcll', 'llll', 'cccc', 'cccc']), + ('S3', ['dddd', 'llll', 'lccl', 'llcl', 'lccl', 'llll', 'cccc', 'cccc']), + ('AR', mirror(WIN_L)), +], [ + ('BL', WALL_L), + ('T', ['cccc', 'cccc', 'cccc', 'llll', 'cllc', 'cllc', 'cllc', 'dddd']), + ('C', ['cccc', 'cccc', 'cccc', 'llll', 'llll', 'llll', 'llll', 'dddd']), + ('BR', WALL_R), +], 2) + +# ---- stairs: a flight rising to the right across two columns and both rows +grid = [] +for y in range(16): + x0 = (15 - y) // 2 + row = ''.join('c' if x == x0 else ('l' if x > x0 else 'd') for x in range(8)) + grid.append(row) +grid[0] = 'dddddddd' +grid[15] = 'dddddddd' +add('ST_AL', [r[:4] for r in grid[:8]], 1) +add('ST_AR', [r[4:] for r in grid[:8]], 1) +add('ST_BL', [r[:4] for r in grid[8:]], 1) +add('ST_BR', [r[4:] for r in grid[8:]], 1) + +# ---- elevator shaft: dark column with a door on each floor +SHAFT = ['dddddddd'] * 4 + ['dlllllld'] * 11 + ['dddddddd'] +add('SH_AL', [r[:4] for r in SHAFT[:8]]) +add('SH_AR', [r[4:] for r in SHAFT[:8]]) +FIG_COLORS = [0, 1, 7, 2] # nobody / a few / many / a crowd +FIG_AT = [[], [1], [1, 6], [1, 3, 5, 6]] +for v in range(4): + rows = [list(r) for r in SHAFT[8:]] + for px in FIG_AT[v]: + for y in (2, 3, 4, 5, 6): + rows[y][px] = 'c' + rows = [''.join(r) for r in rows] + add('SH_BL%d' % v, [r[:4] for r in rows], FIG_COLORS[v]) + add('SH_BR%d' % v, [r[4:] for r in rows], FIG_COLORS[v]) + +# ---- elevator car over the shaft, in 2-pixel steps. The car's top line is +# at pixel offset o (0, 2, 4, 6) within a character row; that row is the top +# (A, parity 0) or bottom (B, parity 1) row of a floor. +CAR = ['dlllllld'] + ['dlccccld'] * 14 + ['dlllllld'] +for o in (0, 2, 4, 6): + for parity in (0, 1): + nrows = 2 if o == 0 else 3 + for k in range(nrows): + cell = [] + for line in range(8): + world = k * 8 + line # line within the covered rows + carline = world - o + if 0 <= carline < 16: + cell.append(CAR[carline]) + else: + srow = ((parity + k) % 2) * 8 + line # shaft line beneath + cell.append(SHAFT[srow]) + add('CAR%d_%d_%dL' % (o, parity, k), [r[:4] for r in cell], 7) + add('CAR%d_%d_%dR' % (o, parity, k), [r[4:] for r in cell], 7) + +assert FIRST + len(tiles) <= 256, len(tiles) + +# ---- sprites: mouse pointer (white fill over a black outline) +ARROW = [ + 'X..........', + 'XX.........', + 'XXX........', + 'XXXX.......', + 'XXXXX......', + 'XXXXXX.....', + 'XXXXXXX....', + 'XXXXXXXX...', + 'XXXXX......', + 'XX.XXX.....', + 'X...XX.....', + '.....XX....', + '.....XX....', +] +fill = [[0] * 24 for _ in range(21)] +for y, r in enumerate(ARROW): + for x, ch in enumerate(r): + if ch == 'X': + fill[y + 1][x + 1] = 1 +outline = [[0] * 24 for _ in range(21)] +for y in range(21): + for x in range(24): + if any(0 <= y + dy < 21 and 0 <= x + dx < 24 and fill[y + dy][x + dx] + for dy in (-1, 0, 1) for dx in (-1, 0, 1)): + outline[y][x] = 1 + + +def sprite_bytes(g): + out = [] + for row in g: + for b in range(3): + v = 0 + for i in range(8): + v = (v << 1) | row[b * 8 + i] + out.append(v) + return out + [0] + + +sprites = sprite_bytes(fill) + sprite_bytes(outline) + +# ---- cell pairs: the map stores one byte per cell, an index into these +# (top tile, bottom tile) pairs, so a floor's two rows cost one byte. +pairs = [] +pair_names = {} + + +def pair(name, a, b): + pair_names[name] = len(pairs) + pairs.append((names[a], names[b])) + + +pair('SKY', 'SKY', 'SKY') +pair('CLOUD_L', 'CLOUD_L', 'SKY') +pair('CLOUD_M', 'CLOUD_M', 'SKY') +pair('CLOUD_R', 'CLOUD_R', 'SKY') +pair('TREE', 'TREE_T', 'TREE_B') +pair('DIRT', 'DIRT', 'DIRT2') +pair('DIRT2', 'DIRT2', 'DIRT') +pair('DIRT_TOP', 'DIRT_TOP', 'DIRT') +pair('FLOOR', 'FLOOR_A', 'FLOOR_B') +pair('LOBBY', 'LOBBY_A', 'LOBBY_B') +pair('LOBBY2', 'LOBBY_A2', 'LOBBY_B2') +pair('ST_L', 'ST_AL', 'ST_BL') +pair('ST_R', 'ST_AR', 'ST_BR') +for v in range(4): + pair('SH_L%d' % v, 'SH_AL', 'SH_BL%d' % v) + pair('SH_R%d' % v, 'SH_AR', 'SH_BR%d' % v) +ROOMS = { + 'OFF': (['AL', 'AM', 'AM', 'AM', 'AM', 'AR'], ['BL', 'B1', 'B2', 'B1', 'B2', 'BR']), + 'CON': (['AL', 'AW', 'AW', 'AX', 'AW', 'AW', 'AX', 'AR'], + ['BL', 'BED1', 'BED2', 'PLANT', 'SOFA1', 'SOFA2', 'PLANT', 'BR']), + 'FOOD': (['AL', 'AW', 'S1', 'S2', 'S3', 'AW', 'AW', 'AR'], ['BL', 'T', 'C', 'C', 'C', 'T', 'T', 'BR']), +} +room_cols = {} +for room_name, (a_row, b_row) in ROOMS.items(): + for suffix in ('', '_D'): + ids = [] + for a, b in zip(a_row, b_row): + key = '%s_%s_%s%s' % (room_name, a, b, suffix) + if key not in pair_names: + pair(key, '%s_%s%s' % (room_name, a, suffix), '%s_%s%s' % (room_name, b, suffix)) + ids.append(pair_names[key]) + room_cols[room_name + suffix] = ids +assert len(pairs) <= 256 + +here = os.path.dirname(os.path.abspath(__file__)) +src = os.path.join(here, '..', 'src') +data = [] +for name, rows, color in tiles: + for r in rows: + v = 0 + for ch in r: + v = (v << 2) | '.dlc'.index(ch) + data.append(v) +colors = [1] * 64 + [8 | c for _, _, c in tiles] + [8] * (256 - FIRST - len(tiles)) +NL = chr(10) + +with open(os.path.join(src, 'gfx.h'), 'w', newline=NL) as fh: + out = ['/* Generated by tools/gen_gfx.py: do not edit. */', '#ifndef GFX_H', '#define GFX_H', + '#define TILE_FIRST %d' % FIRST, '#define TILE_COUNT %d' % len(tiles), + '#define PAIR_COUNT %d' % len(pairs)] + out += ['#define T_%s %d' % (name, names[name]) for name, _, _ in tiles] + out += ['#define P_%s %d' % (name, i) for name, i in pair_names.items() + if not any(name.startswith(r + '_') for r in ROOMS)] + out += ['extern const unsigned char tile_gfx[%d];' % len(data), + 'extern const unsigned char tile_col[256];', + 'extern const unsigned char pair_a[256], pair_b[256];'] + out += ['extern const unsigned char room_%s[%d];' % (k.lower(), len(v)) for k, v in room_cols.items()] + out += ['extern const unsigned char sprite_gfx[%d];' % len(sprites), '#endif'] + fh.write(NL.join(out) + NL) + + +def arr(name, vals, n=16): + lines = ['const unsigned char %s[%d] = {' % (name, len(vals))] + for i in range(0, len(vals), n): + lines.append(' ' + ','.join(str(v) for v in vals[i:i + n]) + ',') + lines.append('};') + return lines + + +with open(os.path.join(src, 'gfx.c'), 'w', newline=NL) as fh: + out = ['/* Generated by tools/gen_gfx.py: do not edit. */', '#include "gfx.h"'] + out += arr('tile_gfx', data) + out += arr('tile_col', colors) + out += arr('pair_a', [a for a, b in pairs] + [names['SKY']] * (256 - len(pairs))) + out += arr('pair_b', [b for a, b in pairs] + [names['SKY']] * (256 - len(pairs))) + for k, ids in room_cols.items(): + out += arr('room_' + k.lower(), ids) + out += arr('sprite_gfx', sprites) + fh.write(NL.join(out) + NL) +print('%d tiles (chars %d-%d), %d pairs, %d sprite bytes' + % (len(tiles), FIRST, FIRST + len(tiles) - 1, len(pairs), len(sprites)))