/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Survey the gold deposit. Link gime, input and bcontrol. */
#include <coco.h>
#include <vcc/gime.h>
#include <vcc/input.h>

enum { SKY, WHITE, CYAN, GOLD, MAGENTA, ROCK, SHADOW };
enum { LAUNCH_Y = 66, DEPOSIT_X = 216, DEPOSIT_Y = 152, MAX_CHARGE = 16 };

typedef struct {
    unsigned shipX, probeX;
    byte probeY;  /* Zero when no probe is in flight. */
} Flight;

/* The craft and probe fly through clear sky, so a hidden page can erase
   its previous sprites without saving any of the scenery beneath them. */
static void scenery(void)
{
    static const byte horizon[] = {172, 170, 168, 171, 174, 175, 179, 180, 178, 173,
                                   170, 166, 162, 165, 172, 176, 180, 177, 174, 171};
    unsigned i;
    for (i = 0; i < 20; ++i) {
        unsigned x = i * 16;
        unsigned top = horizon[i];
        gime_fill_rect(x, top, 16, 192 - top, ROCK);
        gime_fill_rect(x, top, 16, 1, MAGENTA);
        if (i % 3 == 0) gime_fill_rect(x + 6, top + 6, 7, 2, SHADOW);
        gime_plot(x + 4, 20 + i * 7 % 23, WHITE);
    }
    gime_fill_rect(264, 15, 8, 2, GOLD);  /* Crescent over the landing site. */
    gime_fill_rect(261, 17, 3, 7, GOLD);
    gime_fill_rect(264, 24, 8, 2, GOLD);
    gime_fill_rect(DEPOSIT_X + 2, DEPOSIT_Y + 3, 4, 11, CYAN);
    gime_fill_rect(DEPOSIT_X + 20, DEPOSIT_Y + 3, 4, 22, CYAN);
    gime_fill_rect(7, 7, 52, 8, WHITE);  /* Charge gauge surround. */
}

static void drawShip(unsigned x)
{
    gime_fill_rect(x + 6, 49, 8, 2, WHITE);
    gime_fill_rect(x + 4, 51, 12, 6, CYAN);
    gime_fill_rect(x + 8, 52, 5, 3, SHADOW);
    gime_fill_rect(x, 57, 20, 4, WHITE);
    gime_fill_rect(x + 2, 61, 3, 3, GOLD);
    gime_fill_rect(x + 15, 61, 3, 3, GOLD);
}

int main(void)
{
    static const byte colors[] = {0x00, 0x3f, 0x1b, 0x34, 0x29, 0x08, 0x01};
    VccInput stick;
    Flight now = {144, 0, 0}, old[2];
    unsigned launched = 0;
    byte ready = 0, charge = 0, limit = 0, charging = 0, found = 0;
    byte page = 0, freshPages = 2, i;

    if (!gime_open()) {
        printf("USE COCO 3 BASIC WITH 512K OR MORE\n");
        return 1;
    }
    for (i = 0; i < sizeof(colors); ++i) gime_set_palette(i, colors[i]);
    input_init(&stick, INPUT_RIGHT);

    while (!isKeyPressed(KEY_PROBE_Q, KEY_BIT_Q)) {
        input_sample(&stick);            /* One sample for this entire update. */
        if (stick.dx < 0 && now.shipX >= 2) now.shipX -= 2;
        if (stick.dx > 0 && now.shipX < GIME_WIDTH - 20) now.shipX += 2;

        if (stick.pressed & INPUT_PRIMARY) {
            ++launched;                  /* Start one new probe at the craft. */
            now.probeX = now.shipX + 9;
            now.probeY = LAUNCH_Y;
            limit = LAUNCH_Y + 24 + 4 * ready;
            charge = 0;
            charging = 1;
        } else if (now.probeY) {
            if (now.probeY >= limit) now.probeY = 0;
            else now.probeY += 2;
        }
        if (charging && (stick.held & INPUT_PRIMARY) && charge < MAX_CHARGE)
            ++charge;                    /* Build energy for the NEXT probe. */
        if (charging && (stick.released & INPUT_PRIMARY)) {
            ready = charge;              /* Lock in the range for the next press. */
            charging = 0;
        }
        if (now.probeY + 3 >= DEPOSIT_Y && now.probeX >= DEPOSIT_X &&
            now.probeX < DEPOSIT_X + 26) {
            found = 1;
            now.probeY = 0;
        }

        if (freshPages) {
            scenery();
            --freshPages;
        } else {
            gime_fill_rect(old[page].shipX, 49, 20, 15, SKY);
            if (old[page].probeY)
                gime_fill_rect(old[page].probeX, old[page].probeY, 2, 4, SKY);
        }
        drawShip(now.shipX);
        if (now.probeY) gime_fill_rect(now.probeX, now.probeY, 2, 4, CYAN);
        gime_fill_rect(DEPOSIT_X, DEPOSIT_Y, 26, 3, found ? CYAN : GOLD);
        gime_fill_rect(9, 9, 48, 4, SHADOW);
        gime_fill_rect(9, 9, 3 * (charging ? charge : ready), 4, charging ? GOLD : CYAN);
        old[page] = now;
        if (!gime_present()) {
            gime_close();
            printf("DISPLAY TIMING UNAVAILABLE\n");
            return 1;
        }
        page ^= 1;
    }
    gime_close();
    printf("PROBES LAUNCHED: %u\n", launched);
    if (found) printf("DEPOSIT SURVEYED\n");
    return 0;
}
