Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

163 строки
5.1KB

  1. /* World map storage and the seeded map generator (R2). */
  2. #include <string.h>
  3. #include <c64.h>
  4. #include "map.h"
  5. /* Generation runs once per game; static locals make cc65 code much faster here. */
  6. #pragma static-locals (on)
  7. #pragma bss-name (push, "MAP")
  8. unsigned char map[MAP_W * MAP_H];
  9. #pragma bss-name (pop)
  10. unsigned map_seed;
  11. unsigned char map_start_x, map_start_y;
  12. unsigned char map_dwarf_x[DWARF_COUNT];
  13. unsigned char map_dwarf_y[DWARF_COUNT];
  14. /* Generation tuning. */
  15. #define VEIN_COUNT 56u
  16. #define CAVERN_MIN 8u
  17. #define CAVERN_TRIES 40u
  18. #define START_KEEPOUT_X 24u /* caverns stay this far from the map centre ... */
  19. #define START_KEEPOUT_Y 18u /* ... in x or in y, so the starting cave stays separate */
  20. /* 16-bit xorshift (7, 9, 8): full period 65535, never returns 0 from a non-zero state. */
  21. static unsigned rng;
  22. static unsigned rnd(void)
  23. {
  24. rng ^= rng << 7;
  25. rng ^= rng >> 9;
  26. rng ^= rng << 8;
  27. return rng;
  28. }
  29. /* Eight directions, clockwise from north. */
  30. static const signed char dir_dx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  31. static const signed char dir_dy[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
  32. unsigned map_random_seed(void)
  33. {
  34. unsigned s;
  35. s = (unsigned)CIA1.ta_lo | ((unsigned)CIA1.ta_hi << 8);
  36. s ^= (unsigned)VIC.rasterline << 5;
  37. s ^= (unsigned)(*(unsigned char *)0xA2) << 8; /* jiffy clock, low byte */
  38. s ^= *(unsigned char *)0xA1; /* jiffy clock, middle byte */
  39. return s ? s : 1u;
  40. }
  41. /* Hard-rock veins: wandering lines, sometimes two tiles wide. Rows/columns 0 and 127 stay soft rock. */
  42. static void make_veins(void)
  43. {
  44. unsigned char v, len, x, y, dir, thick;
  45. unsigned r;
  46. for (v = 0; v < VEIN_COUNT; ++v) {
  47. r = rnd();
  48. x = (unsigned char)(r & 0x7F);
  49. y = (unsigned char)((r >> 7) & 0x7F);
  50. dir = (unsigned char)((r >> 14) | ((r & 0x80) ? 4 : 0));
  51. thick = (unsigned char)(rnd() & 1);
  52. len = (unsigned char)(24u + (rnd() & 63u));
  53. while (len--) {
  54. if (x == 0 || x >= 127 || y == 0 || y >= 127) {
  55. break;
  56. }
  57. *MAP_PTR(x, y) = TILE_HARD;
  58. if (thick && x < 126) {
  59. *MAP_PTR(x + 1, y) = TILE_HARD;
  60. }
  61. r = rnd();
  62. if ((r & 3) == 0) {
  63. dir = (unsigned char)((dir + ((r & 4) ? 1 : 7)) & 7);
  64. }
  65. x = (unsigned char)(x + dir_dx[dir]);
  66. y = (unsigned char)(y + dir_dy[dir]);
  67. }
  68. }
  69. }
  70. /* Carve a 2x2 block of floor with its top-left at (x, y). Caller keeps x, y in 2..124. */
  71. static void carve2x2(unsigned char x, unsigned char y)
  72. {
  73. unsigned char *p = MAP_PTR(x, y);
  74. p[0] = TILE_FLOOR;
  75. p[1] = TILE_FLOOR;
  76. p[MAP_W] = TILE_FLOOR;
  77. p[MAP_W + 1] = TILE_FLOOR;
  78. }
  79. /* Caverns: random walks that carve 2x2 floor, kept away from the map centre. */
  80. static void make_caverns(void)
  81. {
  82. unsigned char made = 0, tries, count, x, y, dir;
  83. unsigned steps;
  84. unsigned r;
  85. count = (unsigned char)(CAVERN_MIN + (rnd() & 3u));
  86. for (tries = 0; tries < CAVERN_TRIES && made < count; ++tries) {
  87. r = rnd();
  88. /* 8..119 in each axis */
  89. x = (unsigned char)(8u + (((r & 0x7Fu) * 7u) >> 3));
  90. y = (unsigned char)(8u + ((((r >> 7) & 0x7Fu) * 7u) >> 3));
  91. if ((unsigned char)(x > 64 ? x - 64 : 64 - x) < START_KEEPOUT_X &&
  92. (unsigned char)(y > 64 ? y - 64 : 64 - y) < START_KEEPOUT_Y) {
  93. continue;
  94. }
  95. ++made;
  96. steps = 100u + (rnd() & 127u);
  97. while (steps--) {
  98. carve2x2(x, y);
  99. r = rnd();
  100. dir = (unsigned char)((r & 3u) << 1); /* N, E, S or W */
  101. x = (unsigned char)(x + dir_dx[dir]);
  102. y = (unsigned char)(y + dir_dy[dir]);
  103. if (x < 2) x = 2; else if (x > 124) x = 124;
  104. if (y < 2) y = 2; else if (y > 124) y = 124;
  105. }
  106. }
  107. }
  108. /* Half-widths of the starting cave, one per row from cy-4 to cy+4. */
  109. static const unsigned char start_half[9] = { 3, 5, 6, 7, 7, 7, 6, 5, 3 };
  110. /* Starting cave: a rough ellipse about 15x9 near the centre, carved last so it is always open. */
  111. static void make_start_cave(void)
  112. {
  113. unsigned char row, half, x, y, cx, cy;
  114. unsigned char *p;
  115. cx = (unsigned char)(60u + (rnd() & 7u));
  116. cy = (unsigned char)(60u + (rnd() & 7u));
  117. for (row = 0; row < 9; ++row) {
  118. half = (unsigned char)(start_half[row] + (rnd() & 1u));
  119. y = (unsigned char)(cy - 4 + row);
  120. p = MAP_PTR(cx - half, y);
  121. for (x = 0; x <= (unsigned char)(half * 2); ++x) {
  122. p[x] = TILE_FLOOR;
  123. }
  124. }
  125. map_start_x = cx;
  126. map_start_y = cy;
  127. /* Rows cy-1..cy+1 are at least 7 tiles wide each side, so these are always floor (R2.4). */
  128. map_dwarf_x[0] = (unsigned char)(cx - 2); map_dwarf_y[0] = cy;
  129. map_dwarf_x[1] = cx; map_dwarf_y[1] = (unsigned char)(cy - 1);
  130. map_dwarf_x[2] = (unsigned char)(cx + 2); map_dwarf_y[2] = (unsigned char)(cy + 1);
  131. }
  132. void map_generate(unsigned seed)
  133. {
  134. if (seed == 0) {
  135. seed = 1;
  136. }
  137. map_seed = seed;
  138. rng = seed;
  139. memset(map, TILE_SOFT, sizeof map);
  140. make_veins();
  141. make_caverns();
  142. make_start_cave();
  143. }

Powered by TurnKey Linux.