mapgen.c (2174B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define POS(x) ((x < 0) ? 0 : x) 7 #define RANDINT(lower, upper) (lower + (rand() % (upper - lower))) 8 9 char *map; 10 char *npcs = "EAWKLP"; 11 char *special = "TCH"; 12 13 void 14 mk_square(int x, int y, int w) { 15 x = POS(x - w/2); 16 y = POS(y - w/2); 17 18 for (int i = y; (i - y) < w && i < 1000; i++) { 19 for (int j = x; (j - x) < w && j < 1000; j++) { 20 if (map[j + (i * 1000)] == '#') { 21 if (rand() % 17 == 0) 22 map[j + (i * 1000)] = '.'; 23 else 24 map[j + (i * 1000)] = ' '; 25 } 26 } 27 } 28 } 29 30 void 31 mk_line(int x1, int y1, int x2, int y2, int w) { 32 float m = (float)((float)(y2 - y1) / (float)(x2 - x1)); 33 float c = y1 - (m * x1); 34 35 int dir = (x1 < x2) ? 1 : -1; 36 37 for (int i = x1; i != x2; i += dir) { 38 mk_square(i, (m * i + c), 10); 39 } 40 } 41 42 void 43 place_npc(int x, int y) { 44 if (!*npcs) return; 45 map[x + y * 1000] = *npcs; 46 npcs++; 47 } 48 49 void 50 mk_map(int x, int y, int w) { 51 int x2, y2; 52 if (w < 30) return; 53 54 mk_square(x, y, w); 55 56 srand(time(NULL)); 57 for (int i = 0; i < 4; i++) { 58 x2 = (rand() % (w * 3)); 59 y2 = (rand() % (w * 3)); 60 x2 = (rand() % 2) ? x2 : -x2; 61 y2 = (rand() % 2) ? y2 : -y2; 62 mk_line(x, y, x + x2, y + y2, 1); 63 mk_map(x + x2, y + y2, w / 2); 64 place_npc(x + x2, y + y2); 65 } 66 } 67 68 void 69 place_special1() { 70 int x, y; 71 do { 72 x = RANDINT(300, 700); 73 y = RANDINT(500, 800); 74 } while (map[x + y * 1000] != ' '); 75 76 map[x + y * 1000] = 'T'; 77 } 78 79 void 80 place_special2() { 81 int x, y; 82 do { 83 x = RANDINT(200, 500); 84 y = RANDINT(300, 700); 85 } while (map[x + y * 1000] != ' '); 86 87 map[x + y * 1000] = 'C'; 88 } 89 90 void 91 place_special3() { 92 int x, y; 93 do { 94 x = RANDINT(500, 800); 95 y = RANDINT(500, 800); 96 } while (map[x + y * 1000] != ' '); 97 98 map[x + y * 1000] = 'H'; 99 } 100 101 void 102 print_map() { 103 printf(".globl map\n.data\nmap:\n.byte "); 104 for (int i = 0; i < 1000; i++) { 105 for (int j = 0; j < 1000; j++) 106 printf("'%c'%s", map[j + (i * 1000)], (j == 999) ? "" : ", "); 107 printf("\n%s", (i == 999) ? "" : ".byte "); 108 } 109 } 110 111 112 int 113 main() { 114 map = malloc(1000 * 1000); 115 memset(map, '#', 1000 * 1000); 116 mk_map(500, 500, 100); 117 118 place_special1(); 119 place_special2(); 120 place_special3(); 121 122 print_map(); 123 }