Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

2 Players: Tanks drive the way the arrows point

Mason3637ddbd289ef6c
diff --git a/games/twoplayer/src/main.c b/games/twoplayer/src/main.c
index c1fe1437..8870aacc 100644
--- a/games/twoplayer/src/main.c
+++ b/games/twoplayer/src/main.c
@@ -478,12 +478,21 @@ static void tanks_step(void) {
for (int p = 0; p < 2; p++) {
if (dead[p]) continue;
int v = 0, turn = 0;
- if (is_cpu(p)) tanks_cpu(&v, &turn);
- else {
- v = diry(p) < 0 ? 70 : diry(p) * -50, turn = dirx(p) * 8;
+ if (is_cpu(p)) {
+ tanks_cpu(&v, &turn);
+ ta[p] = (ta[p] + turn * ms) & 0x3FFF; /* 8 a ms: half a turn a second */
+ } else {
+ /* the arrows point where to go (diagonals too): the tank turns that way, at half a turn a second, and drives
+ once it faces within an eighth of a turn of it */
+ static const int8_t WANT[9] = {40, 48, 56, 32, -1, 0, 24, 16, 8}; /* in 64ths of a turn, y down */
+ int w = WANT[(diry(p) + 1) * 3 + dirx(p) + 1];
+ if (w >= 0) {
+ int d = (((w << 8) - ta[p] + 0x2000) & 0x3FFF) - 0x2000, step = 8 * ms;
+ ta[p] = (ta[p] + (d > step ? step : d < -step ? -step : d)) & 0x3FFF;
+ v = (d < 0 ? -d : d) <= 0x800 ? 70 : 0;
+ }
if (acted(p)) fire(p);
}
- ta[p] = (ta[p] + turn * ms) & 0x3FFF; /* 8 a ms: half a turn a second */
int x = tx_[p], y = ty_[p], dx = cos64(ta[p] >> 8) * v * ms / 496, dy = sin64(ta[p] >> 8) * v * ms / 496;
if (!tank_stuck(p, x + dx, y + dy)) tx_[p] += dx, ty_[p] += dy;
else if (!tank_stuck(p, x + dx, y)) tx_[p] += dx;
diff --git a/games/twoplayer/tests/play.c b/games/twoplayer/tests/play.c
index 8eef5914..f60df42c 100644
--- a/games/twoplayer/tests/play.c
+++ b/games/twoplayer/tests/play.c
@@ -123,7 +123,12 @@ static void think(int level, int *p, int *t) {
SWAP(tx_[0], tx_[1]); SWAP(ty_[0], ty_[1]); SWAP(ta[0], ta[1]); SWAP(dead[0], dead[1]); SWAP(dead_t[0], dead_t[1]);
SWAP(ai_aim, ai_aim1); SWAP(ai_t, ai_t1); SWAP(ai_shot, ai_shot1);
for (int i = 0; i < SHELLS; i++) sh[i].own = sh[i].own < 0 ? -1 : 1 - sh[i].own;
- *p = (v > 0 ? B_UP : 0) | (turn > 0 ? B_RIGHT : turn < 0 ? B_LEFT : 0);
+ /* the arrows point where to go: ahead to drive, an eighth of a turn aside to drive while turning, a quarter
+ to turn on the spot */
+ static const uint8_t DIR8[8] = {B_RIGHT, B_RIGHT | B_DOWN, B_DOWN, B_DOWN | B_LEFT, B_LEFT, B_LEFT | B_UP, B_UP,
+ B_UP | B_RIGHT};
+ int k = v > 0 ? 8 : 16, a = (ta[0] >> 8) + (turn > 0 ? k : turn < 0 ? -k : 0);
+ *p = v > 0 || turn ? DIR8[((a + 4) & 63) / 8] : 0;
} else if (game == CYCLES) {
static const uint8_t BIT[4] = {B_RIGHT, B_DOWN, B_LEFT, B_UP};
uint32_t next = bot.next;