/* This file was generated by scramble.py. */
#line 1 "src/create.py"
#include "create.h"
static int _kind(Dungeon * self, int x, int y);
static void _walker(Dungeon * self, int x, int y);
static int _closest_shortcut(int n, int * sx, int * sy, int x, int y);
static void _shortcuts(Dungeon * self);
static int _cmp_size(void const * a, void const * b);
#line 4
void dungeon_place(Dungeon * self, int x, int y, int k) {
    self->board [y * board_w + x].kind = k;
}
static int _kind(Dungeon * self, int x, int y) {
    return self->board [y * board_w + x].kind;
}
void dungeon_straight(Dungeon * self, int x, int y, int d, int n) {
    int x2 = x, y2 = y;
    for (int i = 0; i < n; i += 1) {
        dungeon_move(& x2, & y2, d);
        dungeon_place(self, x2, y2, 1);
        self->ex = x2;
        self->ey = y2;
    }
}
#line 18
static void _walker(Dungeon * self, int x, int y) {
    self->floors = 0;
    int path [board_w * board_h];
    int n = 0;
    while (1) {
        while (1) {
            int dirs [6];
            land_shuffle(dirs, 6);
            int found = 6;
            for (int i = 0; i < 6; i += 1) {
                int d = dirs [i];
                int x2 = x, y2 = y;
                dungeon_move(& x2, & y2, d);
                if (_kind(self, x2, y2) != 2) {
                    continue;
                }
                if (x2 == board_w - 3) {
                    dungeon_place(self, x2, y2, 1);
                    self->ex = x2;
                    self->ey = y2;
#line 37
                    return ;
                }
#line 40
                int w = count_kinds(self, x2, y2, 2);
                if (w == 5) {
                    found = d;
                    break;
                }
            }
#line 44
            if (found == 6) {
                break;
            }
            dungeon_move(& x, & y, found);
            dungeon_place(self, x, y, 1);
            self->floors += 1;
            Tile * tile = dungeon_tile(self, x, y);
            path [n++] = board_w * y + x;
            tile->far = n;
        }
#line 53
        if (n == 0) {
#line 53
            return ;
        }
        n--;
        x = path [n] % board_w;
        y = (int)(path [n] / board_w);
    }
}
#line 59
static int _closest_shortcut(int n, int * sx, int * sy, int x, int y) {
    int d = 100;
    for (int i = 0; i < n; i += 1) {
        int dx = abs(sx [i] - x);
        int dy = abs(sy [i] - y);
        if (dx + dy < d) {
            d = dx + dy;
        }
    }
#line 66
    return d;
}
static void _shortcuts(Dungeon * self) {
    int n = 0;
    int sx [20];
    int sy [20];
    Kind * wallk [] = {game->wall, game->wall2, game->wall3, game->wall4, game->wall5};
    for (int y = 0; y < board_h; y += 1) {
        for (int x = 0; x < board_w; x += 1) {
            Tile * tile = dungeon_tile(self, x, y);
            if (tile->kind == 0 || tile->kind == 2) {
                tile->kid = wallk [land_rand(0, 4)]->kid;
            }
#line 78
            if (tile->kind != 1) {
#line 78
                continue;
            }
#line 79
            int r = land_rand(0, 15);
            if (r > 4) {
#line 80
                r = 0;
            }
#line 81
            tile->kid = game->ground [r]->kid;
#line 83
            if (tile->far % (4 + self->level) == 2) {
                int ldirs [] = {0, 5, 1, 2, 4, 3};
                for (int d = 0; d < 6; d += 1) {
                    Tile * wall = dungeon_tile_dir(self, x, y, ldirs [d]);
                    if (wall->kind == 2 || wall->kind == 0) {
                        wall->kid = game->wall_torch->kid;
                        break;
                    }
                }
            }
#line 91
            for (int d = 0; d < 6; d += 1) {
                int x2 = x, y2 = y;
                dungeon_move(& x2, & y2, d);
                Tile * tile2 = dungeon_tile(self, x2, y2);
                if (tile2->kind == 2) {
                    int x3 = x2, y3 = y2;
                    dungeon_move(& x3, & y3, d);
                    Tile * tile3 = dungeon_tile(self, x3, y3);
                    if (tile3->kind == 1 && tile3->far >= tile->far + 5) {
                        if (n < 20 && _closest_shortcut(n, sx, sy, x2, y2) > 5) {
                            sx [n] = x2;
                            sy [n] = y2;
                            n++;
                            tile2->kind = 1;
                            tile2->kid = game->ground [0]->kid;
                        }
                    }
                }
            }
        }
    }
}
#line 107
static int _cmp_size(void const * a, void const * b) {
    Dungeon * da = * (Dungeon * *) a;
    Dungeon * db = * (Dungeon * *) b;
    if (da->floors < db->floors) {
#line 110
        return - 1;
    }
#line 111
    if (da->floors > db->floors) {
#line 111
        return 1;
    }
#line 112
    return 0;
}
void dungeon_create(Dungeon * (* dlevels)) {
    LandArray * levels = land_array_new();
    for (int l = 0; l < 100; l += 1) {
        Dungeon * level;
#line 117
        land_alloc(level);
        dungeon_clear(level);
        level->sx = 2;
        level->sy = 16;
        dungeon_place(level, level->sx, level->sy, 1);
        _walker(level, level->sx, level->sy);
        land_array_add(levels, level);
    }
#line 124
    land_array_sort(levels, _cmp_size);
#line 126
    for (int i = 0; i < 9; i += 1) {
        Dungeon * level = land_array_get(levels, i * 99 / 9);
        dlevels [1 + i] = level;
        level->level = 1 + i;
        dungeon_create_level(level);
    }
}
#line 139
void dungeon_create_level(Dungeon * self) {
    if (self->creatures) {
        land_array_destroy_with_free(self->creatures);
    }
#line 142
    self->creatures = land_array_new();
    land_array_add(self->creatures, NULL);
#line 145
    _shortcuts(self);
#line 147
    int path [200];
    int n = dungeon_path(self, self->sx, self->sy, self->ex, self->ey, 200, path);
    int x = 2;
    int y = 16;
    for (int i = 0; i < n; i += 1) {
        dungeon_move(& x, & y, path [i]);
        Tile * tile = dungeon_tile(self, x, y);
        tile->path_step = 1 + i;
    }
    dungeon_light(self);
#line 158
    for (int i = 0; i < self->level; i += 1) {
        if (self->level >= 4) {
#line 159
            dungeon_add_quest(self, 4, 0);
        }
#line 160
        else if (self->level >= 3) {
#line 160
            dungeon_add_quest(self, 3, 0);
        }
#line 161
        else if (self->level >= 2) {
#line 161
            dungeon_add_quest(self, 2, 0);
        }
#line 161
        else {
            dungeon_add_quest(self, 1, 0);
        }
    }
#line 164
    int l = self->level;
    if (l == 1) {
#line 165
        RAT;
#line 165
        RAT;
    }
#line 166
    if (l == 2) {
#line 166
        TRO;
    }
#line 167
    if (l == 3) {
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
#line 167
        RAT;
    }
#line 168
    if (l == 4) {
#line 168
        TR2;
#line 168
        TRO;
#line 168
        TRO;
#line 168
        RAT;
#line 168
        RAT;
    }
#line 169
    if (l == 5) {
#line 169
        DEM;
#line 169
        RAT;
#line 169
        RAT;
#line 169
        RAT;
#line 169
        TRO;
#line 169
        TRO;
#line 169
        TRO;
#line 169
        RAT;
#line 169
        RAT;
    }
#line 170
    if (l == 6) {
#line 170
        DEM;
#line 170
        RAT;
#line 170
        DEM;
#line 170
        RAT;
#line 170
        TRO;
#line 170
        RAT;
#line 170
        TRO;
#line 170
        RAT;
#line 170
        TR2;
#line 170
        TR2;
    }
#line 171
    if (l == 7) {
#line 171
        DRA;
    }
#line 172
    if (l == 8) {
#line 172
        RAT;
#line 172
        RAT;
#line 172
        RAT;
#line 172
        TRO;
#line 172
        TR2;
#line 172
        TR2;
#line 172
        DEM;
#line 172
        DEM;
#line 172
        DEM;
#line 172
        DRA;
    }
#line 173
    if (l == 9) {
#line 173
        TRO;
#line 173
        TRO;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        TR2;
#line 173
        DRA;
#line 173
        DRA;
#line 173
        DEM;
#line 173
        RAT;
#line 173
        RAT;
#line 173
        RAT;
#line 173
        RAT;
    }
    self->ticks = 0;
}
/* This file was generated by scramble.py. */
