/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Land on the gold pad. Link gime, bgraph and bcontrol. */
#include <vcc/gime.h>
#include <Primitive.h>
#include <Joystick.h>

enum { SPACE, WHITE, CYAN, GOLD, FLAME, BLUE, VIOLET };
enum { CRAFT_W = 20, CRAFT_H = 16, PAD_LEFT = 220, PAD_RIGHT = 270 };
typedef struct { unsigned x, y; } Position;

/* BGraph supplies the line algorithm. Send each pixel to the hidden page. */
static void pixel(word x, byte y, byte color, void *unused)
{
    (void) unused;
    gime_plot(x, y, color);
}

static void landscape(void)
{
    static const byte ridge[] = {183, 167, 177, 161, 176, 170, 184, 165, 177, 168};
    unsigned i;  /* Star and ridge x coordinates extend beyond 255. */
    /* gime_open cleared both pages to SPACE before the first frame. */
    for (i = 0; i < 22; ++i)
        gime_plot(7 + i * 14, 8 + (i * 13) % 34, WHITE);
    /* The crescent and stars sit above the flight area. */
    gime_fill_rect(258, 18, 12, 2, GOLD);
    gime_fill_rect(254, 20, 6, 10, GOLD);
    gime_fill_rect(258, 30, 12, 2, GOLD);
    gime_fill_rect(0, 184, GIME_WIDTH, GIME_HEIGHT - 184, BLUE);
    for (i = 0; i < 9; ++i)
        Primitive_line(i * 35, ridge[i], (i + 1) * 35, ridge[i + 1], VIOLET, pixel, 0);
    gime_fill_rect(PAD_LEFT, 160, PAD_RIGHT - PAD_LEFT, 2, GOLD);
    gime_fill_rect(PAD_LEFT + 3, 162, 2, 12, CYAN);
    gime_fill_rect(PAD_RIGHT - 5, 162, 2, 12, CYAN);
}

static void drawCraft(unsigned x, unsigned y)
{
    gime_fill_rect(x + 7, y, 6, 2, WHITE);
    gime_fill_rect(x + 5, y + 2, 10, 5, CYAN);
    gime_fill_rect(x + 8, y + 3, 4, 3, BLUE);
    gime_fill_rect(x + 2, y + 7, 16, 4, WHITE);
    gime_fill_rect(x + 3, y + 11, 2, 4, GOLD);
    gime_fill_rect(x + 15, y + 11, 2, 4, GOLD);
    gime_fill_rect(x, y + 15, 5, 1, WHITE);
    gime_fill_rect(x + 15, y + 15, 5, 1, WHITE);
    gime_fill_rect(x + 8, y + 11, 4, 3, FLAME);
}

static int direction(byte axis, byte lowProbe, byte lowBit, byte highProbe, byte highBit)
{
    /* When a key is held, it takes precedence over this joystick axis. */
    if (isKeyPressed(lowProbe, lowBit))
        return -2;
    if (isKeyPressed(highProbe, highBit))
        return 2;
    Joystick_selectAnalogMuxInputs(axis);
    if (!Joystick_compareAxis(16))
        return -2;
    return Joystick_compareAxis(48) ? 2 : 0;
}

static unsigned move(int value, int delta, int minimum, int maximum)
{
    value += delta;
    if (value < minimum) return minimum;
    if (value > maximum) return maximum;
    return value;
}

int main(void)
{
    Position craft = {144, 82}, previous[2];
    byte page = 0, initialized = 0, landed = 0, presented = 1;
    static const byte colors[] = {0x00, 0x3F, 0x1B, 0x34, 0x26, 0x08, 0x29};
    byte i;
    if (!gime_open())
    {
        printf("USE A SUPPORTED COCO 3 BASIC SETUP\n");
        return 1;
    }
    for (i = 0; i < sizeof(colors); ++i)
        gime_set_palette(i, colors[i]);
    Joystick_disableSound();

    while (!isKeyPressed(KEY_PROBE_Q, KEY_BIT_Q))
    {
        if (initialized < 2)
        {
            landscape();        /* Each physical page gets the scenery once. */
            ++initialized;
        }
        else
            gime_fill_rect(previous[page].x, previous[page].y, CRAFT_W, CRAFT_H, SPACE);

        craft.x = move(craft.x, direction(0, KEY_PROBE_A, KEY_BIT_A, KEY_PROBE_D, KEY_BIT_D), 0, GIME_WIDTH - CRAFT_W);
        craft.y = move(craft.y, direction(1, KEY_PROBE_W, KEY_BIT_W, KEY_PROBE_S, KEY_BIT_S), 44, 160 - CRAFT_H);
        drawCraft(craft.x, craft.y);
        previous[page] = craft; /* Where to erase when this page comes around again. */
        presented = gime_present();
        if (!presented)
            break;
        page ^= 1;
        if (craft.y == 160 - CRAFT_H && craft.x >= PAD_LEFT && craft.x + CRAFT_W <= PAD_RIGHT)
        {
            landed = 1;
            break;
        }
    }

    Joystick_selectAnalogMuxInputs(0);
    Joystick_enableSound();
    gime_close();
    if (!presented)
    {
        printf("DISPLAY TIMING UNAVAILABLE\n");
        return 1;
    }
    printf("%s\n", landed ? "ON THE PAD!" : "FLIGHT ENDED");
    return 0;
}
