#include "simulations.h"
#include "raytracer.h"
#include "altera_ps2/ps2_keyboard.h"
#include <stdio.h>

// x - depth, y - left, z - top

const int cam_y = 128;

void Delay() {
    int i;
    for (i = 0; i < 3000000; ++i) {}
}

const int car_height = 5;
const int car_length = 8;
const int car_width = 5;
const int car_depth = 10;
int car_color;

void DrawCar(int car_left) {
    // car back
    write_rect(1,
               car_depth, car_left, 0,
               car_depth, car_left + car_width, car_height,
               car_color);
    // car top
    write_rect(2,
               car_depth, car_left, car_height,
               car_depth + car_length, car_left + car_width, car_height,
               car_color);
    // car side
    if (car_left < cam_y) {
        write_rect(3,
                   car_depth, car_left + car_width, 0,
                   car_depth + car_length, car_left + car_width, car_height,
                   car_color);
    } else {
        write_rect(3,
                   car_depth, car_left, 0,
                   car_depth + car_length, car_left, car_height,
                   car_color);
    }
}

/**
 * Returns true (1) if there's any overlap between the range of numbers from a1
 * to a2 and from b1 to b2. Eg: Overlap(1, 3, 2, 4) -> true and 
 * Overlap(1, 3, 4, 6) -> false.
 **/
int Overlap(a1, a2, b1, b2) {
    if (a1 < b1) {
        if (a2 > b1) {
            return 1;
        } else {
            return 0;
        }
    } else {
        if (a1 < b2) {
            return 1;
        } else {
            return 0;
        }
    }
}

void DontCrash() {
    IOWR_16DIRECT(RAY_TRACER_AVALON_MODULE_INST_BASE, CAM_Y, cam_y);
    IOWR_16DIRECT(RAY_TRACER_AVALON_MODULE_INST_BASE, CAM_Z, 10);
    car_color = rgb_to_color(0, 0, 3);
    write_rect(0, 0, 0, 0, 254, 254, 0, rgb_to_color(1, 1, 0)); // floor
    int car_left = 0; // This value only here to prevent warning
    int obstacle_left = 0; // This value only here to prevent warning
    int obstacle_depth = 0; // This value only here to prevent warning
    alt_u8 key;
    int y_overlap, x_overlap;
    int alive = 0;
    printf("Welcome to Don't Crash!\n");
    printf("Use A and D to move the car side to side.\n");
    printf("Don't crash into the obstacles!\n");
    printf("Press space to get started.\n");
    while (DONT_CRASH == simulation) {
        // Get input
        key = LatestKey();
        if (!alive) {
            if (key == KB_SPACE) {
                alive = 1;
                car_left = 125;
                obstacle_left = 110;
                obstacle_depth = 245;
            }
            continue;
        }
        // Update world
        switch (key) {
            case KB_A_DOWN:
                car_left--;
                break;
            case KB_D_DOWN:
                car_left++;
                break;
        }
        obstacle_depth--;
        if (obstacle_depth <= 0) {
            obstacle_depth = 230;
            obstacle_left = car_left;
        }
        // Draw to screen
        DrawCar(car_left);
        write_rect(4, obstacle_depth, obstacle_left, 0,
                   obstacle_depth, obstacle_left + 5, 10, rgb_to_color(3, 3, 3));
        // Check for end game
        x_overlap = Overlap(obstacle_depth, obstacle_depth,
                            car_depth, car_depth + car_length);
        y_overlap = Overlap(obstacle_left, obstacle_left + 5,
                            car_left, car_left + car_width);
        if (x_overlap && y_overlap) {
            printf("You crashed! Game over.\n");
            printf("Press space to restart or a number to switch to another game.\n");
            alive = 0;
        }
        // Call scheduler so other processes can run
        Delay();
    }
}
