|
- /* World map storage and the seeded map generator (R2). */
- #include <string.h>
- #include <c64.h>
- #include "map.h"
-
- /* Generation runs once per game; static locals make cc65 code much faster here. */
- #pragma static-locals (on)
-
- #pragma bss-name (push, "MAP")
- unsigned char map[MAP_W * MAP_H];
- #pragma bss-name (pop)
-
- unsigned map_seed;
- unsigned char map_start_x, map_start_y;
- unsigned char map_dwarf_x[DWARF_COUNT];
- unsigned char map_dwarf_y[DWARF_COUNT];
-
- /* Generation tuning. */
- #define VEIN_COUNT 56u
- #define CAVERN_MIN 8u
- #define CAVERN_TRIES 40u
- #define START_KEEPOUT_X 24u /* caverns stay this far from the map centre ... */
- #define START_KEEPOUT_Y 18u /* ... in x or in y, so the starting cave stays separate */
-
- /* 16-bit xorshift (7, 9, 8): full period 65535, never returns 0 from a non-zero state. */
- static unsigned rng;
-
- static unsigned rnd(void)
- {
- rng ^= rng << 7;
- rng ^= rng >> 9;
- rng ^= rng << 8;
- return rng;
- }
-
- /* Eight directions, clockwise from north. */
- static const signed char dir_dx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
- static const signed char dir_dy[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
-
- unsigned map_random_seed(void)
- {
- unsigned s;
- s = (unsigned)CIA1.ta_lo | ((unsigned)CIA1.ta_hi << 8);
- s ^= (unsigned)VIC.rasterline << 5;
- s ^= (unsigned)(*(unsigned char *)0xA2) << 8; /* jiffy clock, low byte */
- s ^= *(unsigned char *)0xA1; /* jiffy clock, middle byte */
- return s ? s : 1u;
- }
-
- /* Hard-rock veins: wandering lines, sometimes two tiles wide. Rows/columns 0 and 127 stay soft rock. */
- static void make_veins(void)
- {
- unsigned char v, len, x, y, dir, thick;
- unsigned r;
-
- for (v = 0; v < VEIN_COUNT; ++v) {
- r = rnd();
- x = (unsigned char)(r & 0x7F);
- y = (unsigned char)((r >> 7) & 0x7F);
- dir = (unsigned char)((r >> 14) | ((r & 0x80) ? 4 : 0));
- thick = (unsigned char)(rnd() & 1);
- len = (unsigned char)(24u + (rnd() & 63u));
- while (len--) {
- if (x == 0 || x >= 127 || y == 0 || y >= 127) {
- break;
- }
- *MAP_PTR(x, y) = TILE_HARD;
- if (thick && x < 126) {
- *MAP_PTR(x + 1, y) = TILE_HARD;
- }
- r = rnd();
- if ((r & 3) == 0) {
- dir = (unsigned char)((dir + ((r & 4) ? 1 : 7)) & 7);
- }
- x = (unsigned char)(x + dir_dx[dir]);
- y = (unsigned char)(y + dir_dy[dir]);
- }
- }
- }
-
- /* Carve a 2x2 block of floor with its top-left at (x, y). Caller keeps x, y in 2..124. */
- static void carve2x2(unsigned char x, unsigned char y)
- {
- unsigned char *p = MAP_PTR(x, y);
- p[0] = TILE_FLOOR;
- p[1] = TILE_FLOOR;
- p[MAP_W] = TILE_FLOOR;
- p[MAP_W + 1] = TILE_FLOOR;
- }
-
- /* Caverns: random walks that carve 2x2 floor, kept away from the map centre. */
- static void make_caverns(void)
- {
- unsigned char made = 0, tries, count, x, y, dir;
- unsigned steps;
- unsigned r;
-
- count = (unsigned char)(CAVERN_MIN + (rnd() & 3u));
- for (tries = 0; tries < CAVERN_TRIES && made < count; ++tries) {
- r = rnd();
- /* 8..119 in each axis */
- x = (unsigned char)(8u + (((r & 0x7Fu) * 7u) >> 3));
- y = (unsigned char)(8u + ((((r >> 7) & 0x7Fu) * 7u) >> 3));
- if ((unsigned char)(x > 64 ? x - 64 : 64 - x) < START_KEEPOUT_X &&
- (unsigned char)(y > 64 ? y - 64 : 64 - y) < START_KEEPOUT_Y) {
- continue;
- }
- ++made;
- steps = 100u + (rnd() & 127u);
- while (steps--) {
- carve2x2(x, y);
- r = rnd();
- dir = (unsigned char)((r & 3u) << 1); /* N, E, S or W */
- x = (unsigned char)(x + dir_dx[dir]);
- y = (unsigned char)(y + dir_dy[dir]);
- if (x < 2) x = 2; else if (x > 124) x = 124;
- if (y < 2) y = 2; else if (y > 124) y = 124;
- }
- }
- }
-
- /* Half-widths of the starting cave, one per row from cy-4 to cy+4. */
- static const unsigned char start_half[9] = { 3, 5, 6, 7, 7, 7, 6, 5, 3 };
-
- /* Starting cave: a rough ellipse about 15x9 near the centre, carved last so it is always open. */
- static void make_start_cave(void)
- {
- unsigned char row, half, x, y, cx, cy;
- unsigned char *p;
-
- cx = (unsigned char)(60u + (rnd() & 7u));
- cy = (unsigned char)(60u + (rnd() & 7u));
- for (row = 0; row < 9; ++row) {
- half = (unsigned char)(start_half[row] + (rnd() & 1u));
- y = (unsigned char)(cy - 4 + row);
- p = MAP_PTR(cx - half, y);
- for (x = 0; x <= (unsigned char)(half * 2); ++x) {
- p[x] = TILE_FLOOR;
- }
- }
- map_start_x = cx;
- map_start_y = cy;
-
- /* Rows cy-1..cy+1 are at least 7 tiles wide each side, so these are always floor (R2.4). */
- map_dwarf_x[0] = (unsigned char)(cx - 2); map_dwarf_y[0] = cy;
- map_dwarf_x[1] = cx; map_dwarf_y[1] = (unsigned char)(cy - 1);
- map_dwarf_x[2] = (unsigned char)(cx + 2); map_dwarf_y[2] = (unsigned char)(cy + 1);
- }
-
- void map_generate(unsigned seed)
- {
- if (seed == 0) {
- seed = 1;
- }
- map_seed = seed;
- rng = seed;
-
- memset(map, TILE_SOFT, sizeof map);
- make_veins();
- make_caverns();
- make_start_cave();
- }
|