/* This file was generated by scramble.py. */
#line 1 "src/map.py"
#include "map.h"
#line 3
#include "game.h"
#line 15
static int brick;
static int brick2;
static int earth;
static int ladder;
#line 20
Map * map_create(int w, int h, int tw, int th) {
    Map * self;
#line 21
    land_alloc(self);
    self->w = w;
    self->h = h;
    self->tw = tw;
    self->th = th;
    self->tiles = land_calloc(w * h * sizeof (* self->tiles));
    self->crowded = land_calloc(w * h * sizeof (* self->crowded));
    return self;
}
int map_tx(float x) {
    Map * map = game_global ()->map;
    return imod(floor(x / map->tw), map->w);
}
int map_ty(float y) {
    Map * map = game_global ()->map;
    return imod(floor(y / map->th), map->h);
}
int map_get(Map * self, int x, int y) {
    return self->tiles [imod(x, self->w) + self->w * imod(y, self->h)];
}
void map_put(Map * self, int x, int y, int t) {
    self->tiles [imod(x, self->w) + self->w * imod(y, self->h)] = t;
    self->uptodate = 0;
}
#line 47
void map_crowd(Map * self, float x, float y) {
    CROWDED++;
}
void map_uncrowd(Map * self, float x, float y) {
    CROWDED--;
}
int map_crowded(Map * self, float x, float y) {
    return CROWDED;
}
static void hole(Map * self, int x, int y) {
    for (int j = - 1; j < 2; j += 1) {
        for (int i = - 1; i < 2; i += 1) {
            if (map_get(self, x + i, y + j) == earth) {
                map_put(self, x + i, y + j, 0);
            }
        }
    }
}
#line 62
void map_generate(Map * self) {
    brick = tile_by_name("brick");
    brick2 = tile_by_name("brick2");
    earth = tile_by_name("earth");
    ladder = tile_by_name("ladder");
#line 68
    for (int y = 0; y < self->h; y += 1) {
        for (int x = 0; x < self->w; x += 1) {
            map_put(self, x, y, earth);
        }
    }
#line 72
    self->start_x = self->w / 2;
    self->start_y = self->h / 2;
    int x = self->start_x;
    int y = self->start_y;
    int dx [] = {1, 0, - 1, 0, 1, - 1};
    int dy [] = {0, 1, 0, - 1, 0, 0};
#line 79
    for (int u = 0; u < 30; u += 1) {
        for (int t = 0; t < 30; t += 1) {
            hole(self, x, y);
            int d = land_rand(0, 5);
            x = imod(x + dx [d] * 3, self->w);
            y = imod(y + dy [d] * 3, self->h);
        }
#line 85
        hole(self, x, y);
        for (int t = 0; t < 10; t += 1) {
            y++;
            for (int i = - 1; i < 2; i += 1) {
                map_put(self, x + i, y, ladder);
            }
        }
    }
#line 91
    x = self->start_x;
    y = self->start_y;
    for (int j = 0; j < 6 + 6; j += 1) {
        for (int i = 0; i < 5 + 3; i += 1) {
            map_put(self, x + i - 3, y + j - 5, j < 3 ? brick2 : brick);
        }
    }
}
#line 98
void map_render(Map * self, Camera * cam) {
    /* cam.x = 23.5
     * |
     * 0     10     20     30     40     50
     * tx = floor(23.5 / 10) = 2
     * sx = 2 * 10 - 23.5 = -3.5
     */
#line 107
    int tx = floor(cam->x / self->tw);
    int ty = floor(cam->y / self->th);
    float sx = tx * self->tw - cam->x;
    float sy = ty * self->th - cam->y;
#line 112
    while (sy < cam->h) {
        float lx = sx;
        int ltx = tx;
        while (sx < cam->w) {
            int cc = map_get(self, tx, ty);
            int m = 0;
            int b = 1;
            for (int j = 0; j < 3; j += 1) {
                for (int i = 0; i < 3; i += 1) {
                    if (i == 1 && j == 1) {
                        continue;
                    }
#line 123
                    int c = map_get(self, tx - 1 + i, ty - 1 + j);
                    if (c == cc) {
                        m += b;
                    }
#line 126
                    b *= 2;
                }
            }
#line 127
            Tile * t = tile_by_id(cc);
            if (t) {
                tile_render(t, sx, sy, m);
            }
#line 130
            int c = map_crowded(self, tx * self->tw, ty * self->th);
            if (c != 0) {
                land_font_set(game_global ()->small);
                land_color(1, 1, 1, 1);
                land_text_pos(sx, sy);
            }
            //land_print("%d", c)
#line 136
            tx++;
            sx += self->tw;
        }
#line 138
        sx = lx;
        tx = ltx;
        ty++;
        sy += self->th;
    }
}
#line 143
void map_update_overview_tile(Map * map, Tile * tile) {
    if (map->uptodate) {
#line 144
        return ;
    }
    if (game_global ()->time - map->update_time < 60) {
#line 146
        return ;
    }
    int w = tile->w;
    int h = tile->h;
#line 151
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            uint8_t * p = rgba_p(tile->rgba, x, y, w);
            int tid = map_get(map, x, y);
            if (tid == brick) {
#line 155
                rgba_color(1, 0.7, 0.4, p);
            }
#line 156
            else if (tid == brick2) {
#line 156
                rgba_color(1, 1, 1, p);
            }
#line 157
            else if (tid == earth) {
                int tid2 = map_get(map, x, y - 1);
                if (tid2 == 0) {
#line 159
                    rgba_color(0, 1, 0, p);
                }
#line 159
                else {
                    rgba_color(0.8, 0.5, 0.4, p);
                }
            }
#line 161
            else if (tid == ladder) {
#line 161
                rgba_color(0.8, 0.7, 0, p);
            }
#line 161
            else {
                rgba_color(0, 0, 0, p);
            }
        }
    }
#line 164
    land_image_set_rgba_data(tile->pic, tile->rgba);
    map->uptodate = 1;
    map->update_time = game_global ()->time;
}
//land_free(tile.rgba)
Tile * map_create_overview_tile(Map * map) {
    Tile * tile = tile_create("map", map->w, map->h);
    map_update_overview_tile(map, tile);
#line 173
    return tile;
}
/* This file was generated by scramble.py. */
