{
"id": "268",
"prompt": "Orbital station-keeping: a satellite circles a planet under a toy central pull ((center - pos)/128, velocity integrated first so the orbit is stable) - but thin atmosphere bleeds off 1/512 of its velocity every frame, so the orbit slowly decays. Each tap is a prograde burn adding 1/6 to the velocity vector. Fall inside the planet and you DEORBIT; drift past the dotted ring and you ESCAPE - both are message screens that a tap relaunches. Show altitude using an integer octagonal distance approximation (max + min/2 of the axis offsets), since there is no sqrt.",
"solution_rl": "fn iabs(v: i32) -> i32 {\n if v < 0 { -v } else { v }\n}\n\nfn frame(t: i32) {\n let tab = [0, 25, 49, 71, 90, 106, 117, 124, 127, 124, 117, 106, 90, 71, 49, 25, 0, -25, -49, -71, -90, -106, -117, -124, -127, -124, -117, -106, -90, -71, -49, -25];\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let cx: i32 = w / 2;\n let cy: i32 = h / 2;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(2, (cx + 150) * 16);\n host::display::state_set(3, cy * 16);\n host::display::state_set(4, 0);\n host::display::state_set(5, 218);\n host::display::state_set(6, 0);\n }\n let down: i32 = host::display::pointer_down();\n let prev: i32 = host::display::state_get(7);\n host::display::state_set(7, down);\n let tap: bool = down == 1 && prev == 0;\n let mut x: i32 = host::display::state_get(2);\n let mut y: i32 = host::display::state_get(3);\n let mut vx: i32 = host::display::state_get(4);\n let mut vy: i32 = host::display::state_get(5);\n let status: i32 = host::display::state_get(6);\n if status == 0 {\n if tap {\n vx = vx + vx / 6;\n vy = vy + vy / 6;\n }\n vx = vx + (cx * 16 - x) / 128;\n vy = vy + (cy * 16 - y) / 128;\n vx = vx - vx / 512;\n vy = vy - vy / 512;\n x = x + vx;\n y = y + vy;\n host::display::state_set(2, x);\n host::display::state_set(3, y);\n host::display::state_set(4, vx);\n host::display::state_set(5, vy);\n let mut a: i32 = iabs(x / 16 - cx);\n let mut b: i32 = iabs(y / 16 - cy);\n if a < b {\n let tmp: i32 = a;\n a = b;\n b = tmp;\n }\n let d: i32 = a + b / 2;\n if d < 40 { host::display::state_set(6, 1); }\n if d > 235 { host::display::state_set(6, 2); }\n host::display::clear(0x04040a);\n let mut i: i32 = 0;\n while i < 16 {\n let ang: i32 = i * 2;\n host::display::fill_rect(cx + tab[ang] * 235 / 127 - 1, cy + tab[(ang + 8) & 31] * 235 / 127 - 1, 2, 2, 0x282838);\n i = i + 1;\n }\n host::display::fill_rect(cx - 32, cy - 32, 64, 64, 0x3060a0);\n host::display::fill_rect(x / 16 - 4, y / 16 - 4, 8, 8, 0xe0e0e0);\n host::display::draw_string(8, 8, \"ALT\", 0x707070, 1);\n let mut alt: i32 = d - 40;\n if alt < 0 { alt = 0; }\n host::display::draw_number(50, 8, alt, 0xffffff, 1);\n host::display::draw_string(8, 24, \"TAP = BOOST\", 0x505050, 1);\n } else {\n host::display::clear(0x04040a);\n if status == 1 {\n host::display::draw_string(cx - 90, cy - 20, \"DEORBITED\", 0xff5040, 3);\n } else {\n host::display::draw_string(cx - 80, cy - 20, \"ESCAPED\", 0x60c0ff, 3);\n }\n host::display::draw_string(cx - 70, cy + 30, \"TAP TO RELAUNCH\", 0x808080, 1);\n if tap { host::display::state_set(0, 0); }\n }\n host::display::present();\n}\n",
"tags": [
"simulation",
"orbits",
"state",
"pointer"
]
}