/* This file was generated by scramble.py. */
#line 1 "src/game.py"
#include "game.h"
#line 5
#include "names.h"
#include "brick.h"
#include "earth.h"
#include "monster.h"
#include "ladder.h"
#include "cloud.h"
#include "music.h"
#line 14
static LandRunner * _runner;
#include "ctype.h"
#include "title.h"
#line 63
static Game * game;
#line 65
Game * game_global(void) {
    return game;
}
void start(int kids) {
    map_generate(game->map);
    game->kids_count = kids;
    game->kids = land_calloc(game->kids_count * sizeof (* game->kids));
    for (int i = 0; i < game->kids_count; i += 1) {
        Tile * tile = girl_create_kid(i);
        game->kids [i] = player_create(tile->id);
        game->kids [i]->name = land_strdup(name_pick(tile->female));
        player_place_at_start(game->kids [i], (i % 5) - 2, (i / 5) + 1, game->map);
#line 78
        kid_command(game->kids [i], Studying, 0);
        game->kids [i]->kid = i;
    }
    game->player = player_create(tile_by_name("girl"));
    game->player->ladders = 25;
    player_place_at_start(game->player, 0, 0, game->map);
#line 85
    game->monster = player_create(tile_by_name("monster"));
    player_place_at_start(game->monster, 64, 64, game->map);
    game->monster->ladders = 100;
    game->title = 0;
}
void init(LandRunner * runner) {
    land_alloc(game);
    game->title = 1;
    game->music = 1;
    game->small = land_font_load("data/galaxy.ttf", 14);
    game->large = land_font_load("data/galaxy.ttf", 32);
    game->ring = land_sound_load("data/alarm.ogg");
    game->ding = land_sound_load("data/ding.ogg");
    game->knock = land_sound_load("data/knock.ogg");
    game->toc = land_sound_load("data/toc.ogg");
    game->classroom = land_sound_load("data/classroom.ogg");
    game->laugh = land_sound_load("data/laugh.ogg");
    game->cry = land_sound_load("data/cry.ogg");
    game->command = land_strdup("");
    game->camera = camera_new(land_display_width(), land_display_height());
    game->map = map_create(128, 128, 128, 128);
    game->brick = brick_orange_create();
    brick_white_create();
    game->ladder = ladder_create();
    game->pams = parameters_new();
    girl_create(game->pams);
    cloud_create();
    sparkle_create();
    tear_create();
    monster_create();
    game->earth = earth_create();
    game->overview = map_create_overview_tile(game->map);
}
void enter(LandRunner * runner) {
#line 118
    ;
}
//static def param(float *p, minv, maxv, d):
//    if land_key(LandKeyLeftShift) or land_key(LandKeyRightShift):
//        d = -d
//    *p += d
//    if *p < minv:
//        *p = maxv
//    if *p > maxv:
//        *p = minv
//    girl_create(game.pams)
#line 131
void tick(LandRunner * runner) {
    if (game->music) {
#line 134
        if (game->title) {
            music_play_tune_if_not(music_tune3);
        }
#line 135
        else {
#line 137
            if (game->kids [0]->state == Studying || game->kids [0]->state == Crazy) {
                music_play_tune_if_not(music_tune4);
            }
#line 138
            else {
#line 140
                if (music_current() == music_tune1) {
                    if (music_repeats() >= 1) {
                        music_play_tune_if_not(music_tune3);
                    }
                }
#line 143
                else if (music_current() == music_tune2) {
                    if (music_repeats() >= 1) {
                        music_play_tune_if_not(music_tune3);
                    }
                }
#line 145
                else {
#line 147
                    music_play_tune_if_not(music_tune3);
                }
            }
        }
#line 149
        music_tick();
    }
#line 150
    if (land_closebutton()) {
#line 150
        land_quit();
    }
#line 151
    if (land_key_pressed(LandKeyEscape)) {
        if (game->title) {
#line 152
            land_quit();
        }
#line 152
        else {
            game->title = 1;
        }
    }
#line 154
    int kx = 0;
    int ky = 0;
    int ka = 0;
    if (land_key(LandKeyRight)) {
#line 157
        kx++;
    }
#line 158
    if (land_key(LandKeyLeft)) {
#line 158
        kx--;
    }
#line 159
    if (land_key(LandKeyUp)) {
#line 159
        ky--;
    }
#line 160
    if (land_key(LandKeyDown)) {
#line 160
        ky++;
    }
#line 161
    if (land_key_pressed(LandKeyLeftShift) || land_key_pressed(LandKeyRightShift)) {
#line 161
        ka = 2;
    }
#line 162
    else if (land_key(LandKeyLeftShift) || land_key(LandKeyRightShift)) {
#line 162
        ka = 1;
    }
    while (! land_keybuffer_empty()) {
        int k, u;
        land_keybuffer_next(& k, & u);
        if (u >= 32 && u < 127) {
            if (game->time > game->letter_time) {
                game->command [0] = 0;
            }
#line 170
            int s = land_utf8_count(game->command);
            game->command = land_utf8_realloc_insert(game->command, s, u);
            game->letter_time = game->time + 60;
            game->command_time = game->time + 180;
        }
    }
#line 175
    if (game->time > game->command_time) {
        game->command [0] = 0;
    }
    if (game->title) {
        title_tick();
#line 181
        if (land_equals(game->command, "easy")) {
#line 181
            start(20);
        }
#line 182
        if (land_equals(game->command, "medium")) {
#line 182
            start(25);
        }
#line 183
        if (land_equals(game->command, "hard")) {
#line 183
            start(30);
        }
#line 184
        if (land_equals(game->command, "continue")) {
            if (game->player) {
                game->title = 0;
            }
        }
#line 188
        game->time++;
#line 188
        return ;
    }
#line 191
    player_input(game->player, kx, ky, 0);
    game->camera->x = game->player->x - game->camera->w / 2 + game->map->tw / 2;
    game->camera->y = game->player->y + 128 - game->camera->h / 2 + game->map->th / 2;
#line 195
    if (land_key_pressed(LandKeyFunction + 1)) {
        game->music = ! game->music;
    }
    particle_move_all();
#line 200
    if (kx == 0) {
        int lx = map_tx(game->player->x + 64);
        int ly = map_ty(game->player->y + 256 - 10);
        if (ka == 2) {
            player_ladder(game->player);
            game->lx = lx;
            game->ly = ly;
        }
#line 207
        else if (ka) {
            if (lx != game->lx || ly != game->ly) {
                player_ladder(game->player);
                game->lx = lx;
                game->ly = ly;
            }
        }
    }
#line 211
    else {
#line 213
        if (ka) {
            player_dig(game->player, kx);
        }
    }
#line 216
    player_input(game->monster, 0, 0, 2);
#line 218
    if (game->inclass == game->kids_count && game->kids [0]->state == Sitting) {
        if (! game->won) {
            game->won = 1;
            for (int i = 0; i < game->kids_count; i += 1) {
                Player * kid = game->kids [i];
                kid->state = Dancing;
                kid->attention_span = 10;
            }
        }
    }
#line 226
    game->inclass = 0;
    game->noisy = 0;
    int mini = - 1;
    float mind = 0;
    bool command_successful = 0;
    for (int i = 0; i < game->kids_count; i += 1) {
        Player * kid = game->kids [i];
        if (kid->state == Sitting || kid->state == Studying || kid->state == Dancing) {
            game->inclass++;
        }
#line 235
        if (kid->state == Sitting) {
            float v = 1 - kid->attention_span / 3600.0;
            if (v < 0) {
                v = 0;
            }
#line 239
            game->noisy += v;
        }
#line 240
        char * c = game->command;
        bool named = 0;
        float d = kid_distance(kid, NULL, NULL);
        kid->seen = 0;
        if (kid->state != Sitting) {
            if (mini == - 1 || d < mind) {
                mind = d;
                mini = i;
            }
        }
#line 248
        if (d < 1280) {
            kid->seen_x = kid->x;
            kid->seen_y = kid->y;
            kid->seen = 1;
        }
#line 252
        if (d < 500 && game->command [0]) {
            int f = game->command [0];
            game->command [0] = toupper(f);
            if (land_starts_with(game->command, kid->name)) {
                c = game->command + strlen(kid->name) + 1;
                named = 1;
            }
#line 257
            else {
#line 259
                game->command [0] = f;
            }
#line 260
            if (land_equals(c, "follow")) {
                kid_command(kid, Following, named);
                command_successful = 1;
            }
#line 263
            if (land_equals(c, "stop")) {
                kid_command(kid, Stopping, named);
                command_successful = 1;
            }
#line 266
            if (land_equals(c, "sit")) {
                kid_command(kid, Sitting, named);
                command_successful = 1;
            }
#line 269
            if (land_equals(c, "quiet")) {
                if (kid->state == Sitting) {
                    kid->attention_span = 3600;
                    command_successful = 1;
                }
            }
        }
#line 273
        player_input(kid, 0, 0, 1);
#line 275
        if (i == 0) {
            if (kid->state == Studying && kid->attention_span == 1) {
                land_sound_loop(game->ring, 2, 0, 1);
            }
#line 278
            if (kid->state == Crazy && kid->attention_span == 1) {
                land_sound_stop(game->ring);
            }
        }
    }
#line 281
    if (command_successful) {
        game->command [0] = 0;
    }
    if (land_equals(game->command, "where are you?")) {
        if (mini != - 1) {
            Player * kid = game->kids [mini];
            kid->seen_x = kid->x;
            kid->seen_y = kid->y;
            kid->seen = 1;
        }
#line 290
        game->command [0] = 0;
    }
    if (game->noisy < 5 && game->time < game->noisy_time) {
        land_sound_stop(game->classroom);
        game->noisy_time = game->time;
    }
#line 295
    if (game->noisy >= 5 && game->time > game->noisy_time) {
        game->noisy_time = game->time + land_sound_seconds(game->classroom) * 60;
        float v = game->noisy / 10.0;
        if (v > 1) {
            v = 1;
        }
#line 300
        land_sound_play(game->classroom, v, 0, 1);
    }
    //if land_key_pressed('h'): param(&game.pams.hairlength, 0, 2, 0.1)
    //if land_key_pressed('g'): param(&game.pams.hairg, -1, 1, 0.1)
    //if land_key_pressed('t'): param(&game.pams.hairtop, 0.2, 0.8, 0.1)
    //if land_key_pressed('e'): param(&game.pams.eyedistance, 0.3, 0.5, 0.05)
    //if land_key_pressed('w'): param(&game.pams.headw, 0.5, 1.2, 0.1)
    //if land_key_pressed('r'): param(&game.pams.radius, 30, 50, 1)
    //if land_key_pressed('1'): param(&game.pams.forehead, 0.1, 1, 0.01)
    //if land_key_pressed('2'): param(&game.pams.jaw, 0.1, 1, 0.01)
    //if land_key_pressed('3'): param(&game.pams.chin, 0.1, 1, 0.01)
    //if land_key_pressed('4'): param(&game.pams.foreheadg, 0, 2, 0.1)
    //if land_key_pressed('5'): param(&game.pams.jawg, -2, 1, 0.1)
    //if land_key_pressed('6'): param(&game.pams.ching, -3, 1, 0.1)
#line 315
    game->time++;
}
static void draw_player(Player * p) {
    Tile * t = tile_by_id(p->tid);
    int f = (p->frame >> 2) & 7;
    if (f > 4) {
        // 0 1 2 3 4 3 2 1
        f = 8 - f;
    }
#line 323
    if (p->falling) {
        f = 5;
    }
#line 325
    if (p->state == Crying) {
        f = 6;
    }
#line 327
    float x = camera_x(game->camera, p->x);
    float y = camera_y(game->camera, p->y);
#line 330
    if (gravity == 1) {
        land_image_draw(t->frames [f], x, y);
    }
#line 331
    else {
#line 333
        land_image_draw_rotated(t->frames [f], x + 128, y + 256 + 120, pi);
    }
    if (p->name) {
        float nx = x + 64;
        float ny = y + 256 - t->tall - 20;
        land_font_set(game->small);
        land_text_pos(nx, ny);
        land_color_set(t->color);
        land_print_center("%s", p->name);
#line 343
        if (p->attention_span < 600) {
            land_color(1, 0, 0, 1);
            land_line(nx - 40, ny - 5, nx - 40 + p->attention_span * 80 / 600, ny - 5);
        }
    }
#line 345
    else {
#line 347
        land_font_set(game->large);
        land_text_pos(x + 64, y + 48);
        land_color_set(t->color);
        land_print_center("%s", game->command);
    }
}
#line 352
void draw(LandRunner * runner) {
    land_clear(0, 0, 0, 1);
    if (game->title) {
        title_draw();
#line 355
        return ;
    }
#line 358
    map_update_overview_tile(game->map, game->overview);
    map_render(game->map, game->camera);
    draw_player(game->player);
#line 362
    for (int i = 0; i < game->kids_count; i += 1) {
        draw_player(game->kids [i]);
    }
    draw_player(game->monster);
#line 367
    particle_draw_all(game->camera);
#line 369
    Player * kid0 = game->kids [0];
    land_font_set(game->large);
    land_text_pos(game->camera->w / 2, game->camera->h / 3);
    if (kid0->state == Studying) {
        land_color(0, 0, 0, 1);
        if (kid0->attention_span > 120) {
            land_print_center("Ms Teacher is teaching her class.");
        }
#line 375
        else {
#line 377
            land_print_center("But then...");
        }
    }
#line 378
    else if (kid0->state == Crazy) {
        float fade = 180 - kid0->attention_span;
        if (fade > 0) {
            float f = fade / 180.0;
            land_color(0, 0, 0, f);
            land_filled_rectangle(0, 0, game->camera->w, game->camera->h);
        }
#line 384
        land_color(1, 0.2, 0.4, 1);
        land_print_center("Behave during recess!");
    }
#line 385
    else {
#line 387
        float x = game->camera->w - game->overview->w + 0.5;
        float y = 0.5;
        land_image_draw(game->overview->pic, x, y);
#line 391
        for (int i = 0; i < game->kids_count; i += 1) {
            Player * kid = game->kids [i];
            if (kid->state == Sitting) {
                continue;
            }
#line 395
            int p = (game->time / 6) & 1;
            if (kid->seen) {
#line 396
                land_color(1, p, p, 1);
            }
#line 396
            else {
                land_color(1, 0, 0, 1);
            }
#line 398
            float kx = x + kid->seen_x / 128;
            float ky = y + kid->seen_y / 128;
            land_circle(kx - 3, ky - 3, kx + 3, ky + 3);
        }
        land_color(1, 1, 1, 1);
        land_circle(x + game->player->x / 128 - 3, y + game->player->y / 128 - 3, x + game->player->x / 128 + 3, y + game->player->y / 128 + 3);
#line 407
        land_color(0, 0, 1, 1);
        land_circle(x + game->monster->x / 128 - 3, y + game->monster->y / 128 - 3, x + game->monster->x / 128 + 3, y + game->monster->y / 128 + 3);
    }
#line 412
    land_color(1, 1, 1, 1);
    land_font_set(game->small);
    land_text_pos(0, 0);
    float p = 100 * game->noisy / 10;
    if (p > 100) {
#line 416
        p = 100;
    }
#line 417
    land_print("kids in class: %2d/%d noise level: %.0f %%", game->inclass, game->kids_count, p);
#line 420
    land_image_draw_scaled(game->ladder->pic, 240, 0, 0.125, 0.125);
    land_text_pos(240 + 16, 0);
    land_print("%d", game->player->ladders);
#line 424
    if (game->won) {
        land_font_set(game->large);
        land_text_pos(game->camera->w / 2, game->camera->h / 3);
        land_color(1, 0, 0, 1);
        land_print_center("Yay, all back in class!");
    }
}
#line 430
void leave(LandRunner * runner) {
#line 430
    ;
}
#line 433
void destroy(LandRunner * runner) {
#line 433
    ;
}
#line 436
LandRunner * game_runner(void) {
    if (_runner) {
#line 437
        return _runner;
    }
#line 438
    _runner = land_runner_new("game", init, enter, tick, draw, leave, destroy);
    land_runner_register(_runner);
    return _runner;
}
/* This file was generated by scramble.py. */
