/* This file was generated by scramble.py. */
#line 1 "src/tile.py"
#include "tile.h"
#line 18
static int next_id = 1;
static LandHash * by_name;
static LandArray * by_id;
#line 22
int tile_by_name(char const * name) {
    Tile * t = land_hash_get(by_name, name);
    if (t) {
#line 24
        return t->id;
    }
#line 25
    return 0;
}
void tile_make(Tile * self) {
    land_image_set_rgba_data(self->pic, self->rgba);
    land_free(self->rgba);
    if (self->n && self->i < self->n) {
        self->frames [self->i++] = self->pic;
    }
#line 32
    self->rgba = NULL;
}
void tile_frame(Tile * self) {
    if (! self->rgba) {
        self->pic = land_image_new(self->w, self->h);
        self->rgba = land_calloc(self->w * self->h * 4);
    }
}
#line 39
Tile * tile_by_id(int id) {
    Tile * t = land_array_get_nth(by_id, id);
    return t;
}
Tile * tile_create(char const * name, int w, int h) {
    if (! by_name) {
        by_name = land_hash_new();
        by_id = land_array_new();
        land_array_add(by_id, NULL);
    }
    int id = tile_by_name(name);
    Tile * self = tile_by_id(id);
    if (! self) {
        land_alloc(self);
        self->id = next_id++;
        land_hash_insert(by_name, name, self);
        land_array_add(by_id, self);
    }
#line 56
    self->w = w;
    self->h = h;
    self->i = 0;
    tile_frame(self);
    return self;
}
void tile_frames(Tile * self, int n) {
    self->n = n;
    self->frames = land_calloc(n * sizeof (* self->frames));
}
void tile_paste(Tile * self, char const * name) {
    int tid = tile_by_name(name);
    Tile * from = tile_by_id(tid);
    if (! from) {
#line 69
        return ;
    }
    memmove(self->rgba, from->rgba, self->w * self->h * 4);
}
void tile_cut(Tile * self, Stencil * stencil) {
    int w = self->w;
    int h = self->h;
    for (int y = 0; y < h; y += 1) {
        for (int x = 0; x < w; x += 1) {
            uint8_t * p = rgba_p(self->rgba, x, y, w);
            float a = stencil->mask [x + stencil->w * y];
            rgba_alpha(a, p);
        }
    }
}
void tile_render(Tile * self, float x, float y, int m) {
    /* 01 02 04
     * 08    10
     * 20 40 80
     * 0
     * 1 2 3
     * 4 5 6
     * 7 8 9
     * x x x
     * x x x x x x
     * x x x x x x
     * x x x x x x
     */
#line 101
    if (! self->frames) {
        land_image_draw(self->pic, x, y);
#line 102
        return ;
    }
    int f = 0;
    if (M(0x5a, 0x00)) {
#line 105
        f = 0;
    }
#line 106
    else if (M(0x0a, 0x00)) {
#line 106
        f = 1;
    }
#line 107
    else if (M(0x12, 0x00)) {
#line 107
        f = 3;
    }
#line 108
    else if (M(0x48, 0x00)) {
#line 108
        f = 7;
    }
#line 109
    else if (M(0x50, 0x00)) {
#line 109
        f = 9;
    }
#line 110
    else if (M(0x02, 0x00)) {
#line 110
        f = 2;
    }
#line 111
    else if (M(0x08, 0x00)) {
#line 111
        f = 4;
    }
#line 112
    else if (M(0x10, 0x00)) {
#line 112
        f = 6;
    }
#line 113
    else if (M(0x40, 0x00)) {
#line 113
        f = 8;
    }
#line 114
    else if (M(0x5a, 0x5a)) {
#line 114
        f = 5;
    }
#line 115
    land_image_draw(self->frames [f], x, y);
}
/* This file was generated by scramble.py. */
