]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Add dungeon room generation
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 10 Jun 2021 19:54:06 +0000 (21:54 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 10 Jun 2021 19:54:06 +0000 (21:54 +0200)
plugins/game/game.c

index ea6a665eb49e24270830005cef4b5701dc4f5479..b87d5b4c4f8707fe8f3309316a9ccee5f0011a55 100644 (file)
@@ -258,20 +258,54 @@ static bool check_direction(int x, int y, enum direction dir)
                return is_solid(x, y + 1) && is_solid(x, y - 1) && (is_solid(x + 1, y) || rand() % 3 > 1) && (is_solid(x - 1, y) || rand() % 3 > 1);
 }
 
-static void generate_corridor(int lx, int ly, enum direction ldir)
+static void mapgen_set_air(int x, int y)
 {
-       if (is_outside(lx, ly))
+       if (is_outside(x, y))
                return;
-
-       map[lx][ly] = (struct node) {&air};
-
+       if (map[x][y].material == &air)
+               return;
+       map[x][y] = (struct node) {&air};
        for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
                struct generator_function *func = ptr->element;
 
-               if (! func->chance || rand() % func->chance == 0) {
-                       func->callback(lx, ly);
+               if (rand() % func->chance == 0)
+                       func->callback(x, y);
+       }
+}
+
+static void generate_room(int origin_x, int origin_y)
+{
+       int left = 5 + rand() % 15;
+       int right = 5 + rand() % 15;
+
+       int up = 0;
+       int down = 0;
+
+       for (int x = -left; x <= right; x++) {
+               if (x < 0) {
+                       up += rand() % 2;
+                       down += rand() % 2;
+               } else {
+                       up -= rand() % 2;
+                       down -= rand() % 2;
                }
+
+               for (int y = -up; y <= down; y++)
+                       mapgen_set_air(origin_x + x, origin_y + y);
        }
+}
+
+static void generate_corridor(int lx, int ly, enum direction ldir)
+{
+       if (is_outside(lx, ly))
+               return;
+
+       if (rand() % 200 == 0) {
+               generate_room(lx, ly);
+               return;
+       }
+
+       mapgen_set_air(lx, ly);
 
        int x, y;