const c64 = @import("c64");

extern module Terrain = terrain_rules {
    fn cost(terrain: u8) u8;
}

extern module FixedMath = fixed_math {
    fn bias(value: u8) u8;
}

memory {
    region frames: c64 at 0xc800 size 0x0400;
    region window_ram: c64 at 0x6000 size 0x1000;
    region disk_ram: c64 at 0x9000 size 0x0800;
    region cache: reu at 0x030000 size 0x3000;
    stack in frames;
}

module_loader {
    window main in window_ram;
    disk_staging in disk_ram;
    reu_cache in cache;
    capacity = 3;
    current {
        file = "survey";
    }
    dependency terrain_rules {
        file = "terrain";
        requires = .{ .fixed_math };
    }
    dependency fixed_math {
        file = "fixedmath";
        requires = .{};
    }
}

module graph_survey abi 1 {
    export limit;
    dependency terrain_rules;
    dependency fixed_math;
}

fn limit() callconv(.stack) u8 {
    return 8;
}

fn showTerrain() void {
    if (Terrain.acquireGraph()) {
        if (Terrain.cost(3)) |cost| {
            c64.console.print("TERRAIN COST {x}\n", .{cost});
        }
        Terrain.releaseGraph();
    } else {
        c64.console.print("TERRAIN UNAVAILABLE\n", .{});
    }
}

export fn main() void {
    c64.console.clear();
    if (FixedMath.acquire()) {
        showTerrain();
        if (FixedMath.bias(4)) |bonus| {
            c64.console.print("BONUS {x}\n", .{bonus});
        }
        FixedMath.release();
        if (FixedMath.bias(4) == null) {
            c64.console.print("RULES RELEASED\n", .{});
        }
    } else {
        c64.console.print("SCORING UNAVAILABLE\n", .{});
    }
}
