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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Undertale: battles (FIGHT, ACT, ITEM, MERCY, the bullet board), the Ruins' monsters, menus and saves
Mason Chen9e8652d3d548
diff --git a/games/undertale/src/battle.c b/games/undertale/src/battle.cindex e5d1c077..e6118f21 100644--- a/games/undertale/src/battle.c+++ b/games/undertale/src/battle.c@@ -1,9 +1,733 @@-/* Battles (battle.h). */+/* Battles: the turns, the menus, FIGHT's bar, the box, the end (battle.h). */+#include <stdio.h>+#include <string.h>#include "battle.h"#include "game.h"+#include "gfx.h"+#include "items.h"+#include "res.h"+#include "save.h"+#include "text.h"+#include "menu.h"+#include "monsters.h"+#include "world.h"++void flowey_battle_start(void); /* flowey.c */+void flowey_battle_update(uint32_t keys, uint32_t pressed);+void flowey_battle_draw(void);++Battle bt;static bool over = true;-void battle_start(int id) { (void)id; over = true; }+static int heart_x, heart_y; /* the SOUL's start, from Frisk */+static int prev_mode;++enum {+ P_INTRO, P_MENU, P_TARGET, P_ACTS, P_ITEMS, P_MERCY, P_BAR, P_STRIKE, P_RESULT, P_TALK, P_BOX_IN, P_ATTACK,+ P_BOX_OUT, P_WON, P_FLEE, P_DEATH, P_END+};+enum { B_FIGHT, B_ACT, B_ITEM, B_MERCY };++static const int16_t button_x[4] = {16, 92, 172, 250};+#define BUTTON_Y 216++/* ---------------------------------------------------------------- the box */+static void box_to(int x0, int y0, int x1, int y1) { bt.tx0 = (int16_t)x0, bt.ty0 = (int16_t)y0, bt.tx1 = (int16_t)x1, bt.ty1 = (int16_t)y1; }+static void box_menu(void) { box_to(BOX_X0, BOX_Y0, BOX_X1, BOX_Y1); }+void board_set(int w, int h) {+ int cx = 160, y1 = BOX_Y1;+ box_to(cx - w / 2, y1 - h, cx + (w + 1) / 2, y1);+}+void board_set_rect(int x0, int y0, int x1, int y1) { box_to(x0, y0, x1, y1); }+int board_cx(void) { return (bt.x0 + bt.x1) / 2; }+int board_cy(void) { return (bt.y0 + bt.y1) / 2; }+static bool box_settled(void) { return bt.x0 == bt.tx0 && bt.y0 == bt.ty0 && bt.x1 == bt.tx1 && bt.y1 == bt.ty1; }+static int16_t approach(int16_t v, int16_t t, int step) {+ if (v < t) return (int16_t)(v + step > t ? t : v + step);+ if (v > t) return (int16_t)(v - step < t ? t : v - step);+ return v;+}+static void box_step(void) {+ bt.x0 = approach(bt.x0, bt.tx0, 12), bt.x1 = approach(bt.x1, bt.tx1, 12);+ bt.y0 = approach(bt.y0, bt.ty0, 8), bt.y1 = approach(bt.y1, bt.ty1, 8);+}++/* ---------------------------------------------------------------- texts */+static void say(const char *s) {+ if (s != bt.text) snprintf(bt.text, sizeof bt.text, "%s", s);+ writer_start(&bt.w, bt.text, 26, 133, FONT_MAIN);+ bt.w.line_h = 16;+}++int count_alive(void) {+ int n = 0;+ for (int i = 0; i < bt.nmon; i++) n += bt.mon[i].alive && !bt.mon[i].gone;+ return n;+}++static const char *flavor_text(void) {+ /* a monster's own, else one of its neutral texts, low HP's or spareable's */+ int alive[MAX_MON], n = 0;+ for (int i = 0; i < bt.nmon; i++)+ if (bt.mon[i].alive && !bt.mon[i].gone) alive[n++] = i;+ if (!n) return "";+ Monster *m = &bt.mon[alive[rnd_n(n)]];+ const MonDef *d = m->def;+ if (d->flavor) {+ const char *s = d->flavor(&bt, m);+ if (s) return s;+ }+ if (m->spare && d->spareable) return d->spareable;+ if (m->hp < m->maxhp / 3 && d->low_hp) return d->low_hp;+ int k = 0;+ while (k < 6 && d->neutral[k]) k++;+ return k ? d->neutral[rnd_n(k)] : "";+}++/* ---------------------------------------------------------------- the start and the end */+void battle_start(int form) {+ memset(&bt, 0, sizeof bt);+ bt.form = form;+ bt.atk = -1;+ form_setup(form, &bt);+ for (int i = 0; i < bt.nmon; i++) {+ Monster *m = &bt.mon[i];+ const MonDef *d = m->def;+ if (!m->maxhp) m->hp = m->maxhp = d->hp;+ if (!m->at) m->at = d->at;+ if (!m->df) m->df = d->df;+ if (!m->exp) m->exp = d->exp;+ if (!m->gold) m->gold = d->gold;+ m->alive = true;+ m->spare = d->flags & MF_SPARE_START;+ }+ bt.x0 = bt.tx0 = BOX_X0, bt.y0 = bt.ty0 = BOX_Y0, bt.x1 = bt.tx1 = BOX_X1, bt.y1 = bt.ty1 = BOX_Y1;+ bt.speed = 2 * FX;+ bt.hp_shown = sv.hp;+ heart_x = pl.x + 10 - cam_x, heart_y = pl.y + 17 - cam_y;+ prev_mode = g_mode;+ g_mode = MODE_BATTLE;+ bt.phase = P_INTRO;+ bt.t = 0;+ say(form_text(form));+ bt.w.finished = true; /* (typed once the intro is over) */+ if (form == FORM_FLOWEY) flowey_battle_start();+ over = false;+ bullets_clear();+}+bool battle_over(void) { return over; }-int battle_result(void) { return BR_SPARED; }-void battle_update(uint32_t keys, uint32_t pressed) { (void)keys, (void)pressed; }-void battle_draw(void) {}+int battle_result(void) { return bt.result; }++static void battle_end(int result) {+ bt.result = result;+ bt.phase = P_END;+ over = true;+ bullets_clear();+ if (result == BR_DIED) {+ menu_gameover_open();+ return;+ }+ g_mode = MODE_WORLD;+ world_camera();+}++/* ---------------------------------------------------------------- FIGHT */+static int fight_damage(Monster *m, int dist) {+ /* the game's: (AT + weapon - DEF + a little luck), times how near the middle the bar was stopped */+ int at = sv.at + weapon_at();+ float base = (float)(at - m->df + rnd_n(3));+ float stretch = dist <= 12 ? 2.2f : (1.0f - (float)dist / 273.0f) * 2.0f;+ int dmg = (int)(base * stretch + 0.5f);+ if (m->def->flags & MF_ONE_HIT) dmg = m->hp;+ return dmg < 0 ? 0 : dmg;+}++static void strike(void) {+ Monster *m = &bt.mon[bt.target];+ /* the bar's distance from the middle, in the original's pixels */+ int x = bt.bar_x / FX; /* 0..281 across the target */+ int dist = abs_i(x - 140) * 2;+ int dmg = bt.bar_x < 0 ? -1 : fight_damage(m, dist);+ bt.strike_dmg = dmg;+ bt.phase = P_STRIKE;+ bt.t = 0;+}++static void after_player_turn(void);++static void strike_land(void) {+ Monster *m = &bt.mon[bt.target];+ int dmg = bt.strike_dmg;+ m->dmg_shown = (int16_t)dmg, m->dmg_t = 45;+ m->bar_shown = m->hp;+ if (dmg <= 0 && (m->def->flags & MF_MISS_IS_SPARE) && m->def->on_spare) m->def->on_spare(&bt, m);+ if (dmg > 0) {+ if (m->def->on_hit && m->def->on_hit(&bt, m, dmg)) return;+ m->hp = (int16_t)(m->hp - dmg);+ m->hurt = 30, m->shake = 8;+ if (m->hp <= 0) {+ m->hp = 0;+ m->killed = true;+ }+ }+}++/* ---------------------------------------------------------------- the turns */+static void begin_menu(void) {+ bt.phase = P_MENU;+ box_menu();+ say(flavor_text());+ bt.turn++;+}++static void begin_talk(void) {+ bt.phase = P_TALK;+ bt.t = 0;+ for (int i = 0; i < bt.nmon; i++) {+ Monster *m = &bt.mon[i];+ m->say = NULL;+ if (!m->alive || m->gone) continue;+ if (m->def->talk) m->say = m->def->talk(&bt, m);+ else {+ int k = 0;+ while (k < 6 && m->def->quotes[k]) k++;+ if (k) m->say = m->def->quotes[rnd_n(k)];+ }+ }+ bt.talk_i = -1;+ for (int i = 0; i < bt.nmon; i++)+ if (bt.mon[i].say) {+ bt.talk_i = i;+ break;+ }+ if (bt.talk_i >= 0) {+ writer_start(&bt.bw, bt.mon[bt.talk_i].say, 0, 0, FONT_SMALL);+ bt.bw.line_h = 9;+ }+}+static bool all_gone(void);+static void win(void);++static void begin_attack(void) {+ /* the attack: one of the monsters' (at random among those alive) */+ int who[MAX_MON], n = 0;+ for (int i = 0; i < bt.nmon; i++)+ if (bt.mon[i].alive && !bt.mon[i].gone && bt.mon[i].def->attack) who[n++] = i;+ if (!n) {+ begin_menu();+ return;+ }+ int i = who[rnd_n(n)];+ bt.atk_owner = i;+ bt.atk = bt.mon[i].def->attack(&bt, &bt.mon[i]);+ if (bt.atk < 0) {+ begin_menu();+ return;+ }+ bt.atk_len = attack_length(bt.atk);+ bt.atk_t = 0;+ board_set(BOARD_W, BOARD_H);+ attack_run(&bt, bt.atk, -1); /* (t -1: the attack sets its board) */+ bt.phase = P_BOX_IN;+ bullets_clear();+}++static void win(void) {+ int exp = 0, gold = 0;+ for (int i = 0; i < bt.nmon; i++) {+ if (bt.mon[i].killed) exp += bt.mon[i].exp, gold += bt.mon[i].gold;+ else if (bt.mon[i].gone) gold += bt.mon[i].spare_gold;+ }+ gold += bt.gold_won;+ sv.gold = (uint16_t)min_i(sv.gold + gold, 9999);+ bool up = gain_exp(exp);+ bt.exp_won = exp, bt.gold_won = gold;+ char s[200];+ snprintf(s, sizeof s, "* YOU WON!\n* You earned %d EXP and %d gold.%s", exp, gold, up ? "\n* Your LOVE increased." : "");+ say(s);+ bt.phase = P_WON;+ box_menu();+ bullets_clear();+}++static bool all_gone(void) { return count_alive() == 0; }++static void after_player_turn(void) {+ if (all_gone()) {+ bool any_killed = false;+ for (int i = 0; i < bt.nmon; i++) any_killed |= bt.mon[i].killed;+ if (any_killed || bt.gold_won) win();+ else {+ bt.result = BR_SPARED;+ win();+ }+ return;+ }+ begin_talk();+}++static void do_spare(void) {+ bool any = false;+ bt.last_action = 3;+ for (int i = 0; i < bt.nmon; i++) {+ Monster *m = &bt.mon[i];+ if (m->alive && !m->gone && m->spare) m->gone = true, m->fade = 1, any = true;+ else if (m->alive && !m->gone && m->def->on_spare) m->def->on_spare(&bt, m);+ }+ (void)any;+ bt.text[0] = 0;+ if (all_gone()) {+ win();+ return;+ }+ after_player_turn();+}++static void do_flee(void) {+ bool can = !bt.no_flee;+ bt.last_action = 4;+ for (int i = 0; i < bt.nmon; i++)+ if (bt.mon[i].alive && !bt.mon[i].gone && (bt.mon[i].def->flags & MF_BOSS)) can = false;+ if (!can) {+ after_player_turn();+ return;+ }+ bool ok = sv.armor == ITEM_BANDAGE || rnd_n(100) < 50 + 10 * bt.turn;+ if (!ok) {+ after_player_turn();+ return;+ }+ static const char *const lines[] = {"* Escaped...", "* I'm outta here.", "* I've got better to do.", "* Don't slow me down."};+ say(lines[rnd_n(4)]);+ bt.phase = P_FLEE;+ bt.t = 0;+}++/* ---------------------------------------------------------------- input */+static int alive_index(int k) { /* the k-th monster still there */+ for (int i = 0; i < bt.nmon; i++)+ if (bt.mon[i].alive && !bt.mon[i].gone && k-- == 0) return i;+ return -1;+}++static void menu_input(uint32_t pressed) {+ switch (bt.phase) {+ case P_MENU:+ if (pressed & K_LEFT) bt.button = (bt.button + 3) & 3;+ if (pressed & K_RIGHT) bt.button = (bt.button + 1) & 3;+ if (pressed & K_Z) {+ bt.target = 0, bt.act = 0, bt.item = 0, bt.mercy = 0, bt.page = 0;+ if (bt.button == B_FIGHT || bt.button == B_ACT) bt.phase = P_TARGET;+ else if (bt.button == B_ITEM) {+ if (item_count()) bt.phase = P_ITEMS;+ } else bt.phase = P_MERCY;+ }+ break;+ case P_TARGET: {+ int n = count_alive();+ if (pressed & K_UP) bt.target = (bt.target + n - 1) % n;+ if (pressed & K_DOWN) bt.target = (bt.target + 1) % n;+ if (pressed & K_X) bt.phase = P_MENU;+ if (pressed & K_Z) {+ if (bt.button == B_FIGHT) {+ bt.last_action = 0;+ bt.target = alive_index(bt.target);+ bt.phase = P_BAR;+ bt.bar_dir = (sv.weapon == ITEM_TOY_KNIFE && rnd_n(2)) ? -1 : 1;+ bt.bar_x = bt.bar_dir > 0 ? 0 : 281 * FX;+ bt.t = 0;+ } else {+ bt.phase = P_ACTS;+ bt.act = 0;+ }+ }+ break;+ }+ case P_ACTS: {+ int m = alive_index(bt.target);+ int n = 1 + bt.mon[m].def->nacts;+ if (pressed & K_LEFT && bt.act % 2) bt.act--;+ if (pressed & K_RIGHT && !(bt.act % 2) && bt.act + 1 < n) bt.act++;+ if (pressed & K_UP && bt.act >= 2) bt.act -= 2;+ if (pressed & K_DOWN && bt.act + 2 < n) bt.act += 2;+ if (pressed & K_X) bt.phase = P_TARGET;+ if (pressed & K_Z) {+ Monster *mo = &bt.mon[m];+ bt.target = m;+ bt.last_action = 1;+ if (bt.act == 0) {+ snprintf(bt.text, sizeof bt.text, "* %s - ATK %d DEF %d\n* %s", mo->def->name, mo->at, mo->df, mo->def->check);+ for (char *p = bt.text + 2; *p && *p != ' '; p++)+ if (*p >= 'a' && *p <= 'z') *p = (char)(*p - 32);+ } else {+ bt.text[0] = 0;+ mo->acts[bt.act - 1]++;+ if (mo->def->act) mo->def->act(&bt, mo, bt.act - 1, bt.text, sizeof bt.text);+ }+ say(bt.text);+ bt.phase = P_RESULT;+ }+ break;+ }+ case P_ITEMS: {+ int n = item_count();+ if (pressed & K_LEFT) bt.item = bt.item % 2 ? bt.item - 1 : (bt.item >= 4 ? bt.item - 3 : bt.item);+ if (pressed & K_RIGHT) bt.item = !(bt.item % 2) && bt.item + 1 < n ? bt.item + 1 : (bt.item % 2 && bt.item + 3 < n ? bt.item + 3 : bt.item);+ if (pressed & K_UP && bt.item % 4 >= 2) bt.item -= 2;+ if (pressed & K_DOWN && bt.item % 4 < 2 && bt.item + 2 < n) bt.item += 2;+ if (bt.item >= n) bt.item = n - 1;+ if (pressed & K_X) bt.phase = P_MENU;+ if (pressed & K_Z) {+ bt.last_action = 2;+ item_use(bt.item, bt.text, sizeof bt.text, 1);+ say(bt.text);+ bt.phase = P_RESULT;+ }+ break;+ }+ case P_MERCY:+ if (pressed & K_UP) bt.mercy = 0;+ if (pressed & K_DOWN && !bt.no_flee) bt.mercy = 1;+ if (pressed & K_X) bt.phase = P_MENU;+ if (pressed & K_Z) {+ if (bt.mercy == 0) do_spare();+ else do_flee();+ }+ break;+ }+}++/* ---------------------------------------------------------------- the frame */+void battle_update(uint32_t keys, uint32_t pressed) {+ if (over) return;+ bt.t++;+ box_step();+ if (bt.scripted && bt.phase != P_INTRO) {+ flowey_battle_update(keys, pressed);+ if (bt.result == BR_SCRIPTED) battle_end(BR_SCRIPTED);+ return;+ }+ for (int i = 0; i < bt.nmon; i++) {+ Monster *m = &bt.mon[i];+ if (m->hurt) m->hurt--;+ if (m->dmg_t) m->dmg_t--;+ if (m->fade && m->fade < 30) m->fade++;+ if (m->dust && m->dust < 60) m->dust++;+ }+ if (bt.inv) bt.inv--;+ switch (bt.phase) {+ case P_INTRO:+ if (bt.t >= 24) {+ bt.phase = P_MENU;+ say(bt.text);+ bt.turn = 1;+ }+ break;+ case P_MENU:+ writer_update(&bt.w, pressed & K_X ? K_X : 0);+ menu_input(pressed);+ break;+ case P_TARGET:+ case P_ACTS:+ case P_ITEMS:+ case P_MERCY:+ menu_input(pressed);+ break;+ case P_BAR:+ bt.bar_x += bt.bar_dir * 7 * FX / 2;+ if (pressed & K_Z) strike();+ else if (bt.bar_x < 0 || bt.bar_x > 281 * FX) {+ bt.bar_x = -1;+ bt.strike_dmg = -1;+ bt.target = bt.target;+ bt.mon[bt.target].dmg_shown = -1, bt.mon[bt.target].dmg_t = 40;+ if ((bt.mon[bt.target].def->flags & MF_MISS_IS_SPARE) && bt.mon[bt.target].def->on_spare)+ bt.mon[bt.target].def->on_spare(&bt, &bt.mon[bt.target]);+ bt.phase = P_STRIKE, bt.t = 20;+ }+ break;+ case P_STRIKE:+ if (bt.t == 18) strike_land();+ if (bt.t >= 60) {+ Monster *m = &bt.mon[bt.target];+ if (m->killed && m->alive) {+ m->alive = false, m->gone = true, m->dust = 1;+ sv.kills++;+ }+ after_player_turn();+ }+ break;+ case P_RESULT:+ if (!writer_update(&bt.w, pressed)) {+ if (bt.phase == P_RESULT) after_player_turn();+ }+ if (bt.w.finished && bt.phase == P_RESULT) after_player_turn();+ break;+ case P_TALK:+ if (bt.talk_i < 0) {+ begin_attack();+ break;+ }+ if (!writer_update(&bt.bw, pressed)) {+ /* the next monster with something to say */+ int i = bt.talk_i + 1;+ while (i < bt.nmon && !bt.mon[i].say) i++;+ if (i < bt.nmon) {+ bt.talk_i = i;+ writer_start(&bt.bw, bt.mon[i].say, 0, 0, FONT_SMALL);+ bt.bw.line_h = 9;+ } else {+ /* some monsters leave after they have spoken (Napstablook) */+ for (int k = 0; k < bt.nmon; k++) {+ Monster *m = &bt.mon[k];+ if (m->alive && !m->gone && m->def->ends && m->def->ends(&bt, m)) m->gone = true, m->fade = 1;+ }+ if (all_gone()) win();+ else begin_attack();+ }+ }+ break;+ case P_BOX_IN:+ if (box_settled()) {+ bt.sx = board_cx() * FX, bt.sy = board_cy() * FX;+ bt.phase = P_ATTACK;+ }+ break;+ case P_ATTACK:+ soul_update(keys);+ attack_run(&bt, bt.atk, bt.atk_t);+ bullets_update();+ soul_hit_test();+ if (sv.hp <= 0) {+ bt.phase = P_DEATH, bt.t = 0;+ break;+ }+ if (++bt.atk_t >= bt.atk_len) {+ bullets_clear();+ bt.phase = P_BOX_OUT;+ box_menu();+ }+ break;+ case P_BOX_OUT:+ if (box_settled()) begin_menu();+ break;+ case P_WON:+ if (!writer_update(&bt.w, pressed)) battle_end(bt.result == BR_SPARED ? BR_SPARED : BR_WON);+ break;+ case P_FLEE:+ writer_update(&bt.w, 0);+ if (bt.t >= 40) battle_end(BR_FLED);+ break;+ case P_DEATH:+ if (bt.t >= 70) battle_end(BR_DIED);+ break;+ }+}++/* ---------------------------------------------------------------- drawing */+static void draw_monster(Monster *m, int i) {+ const MonDef *d = m->def;+ if (m->gone && !m->dust && m->fade >= 30) return;+ if (!m->alive && !m->dust) return;+ int x = m->x + (m->hurt ? (m->hurt / 2 % 2 ? 2 : -2) * (m->hurt > 10) : 0), y = m->y;+ if (d->draw) {+ d->draw(&bt, m, x, y);+ return;+ }+ uint16_t s = m->spr_override ? (uint16_t)m->spr_override : m->hurt && d->spr_hurt ? d->spr_hurt : d->spr_idle;+ int fr = m->hurt ? 0 : (int)(g_frame / (d->idle_speed ? d->idle_speed : 6));+ if (m->dust) {+ /* turning to dust: drawn whiter and thinner, from the top down */+ int a = 255 - m->dust * 4;+ if (a > 0) gfx_sprite_color(s, 0, x, y, 0, C_WHITE, (uint8_t)a);+ return;+ }+ uint8_t alpha = m->fade ? (uint8_t)max_i(0, 128 - m->fade * 4) : 255;+ gfx_sprite(s, fr, x, y, 0, alpha);+ (void)i;+}++static void draw_hud(void) {+ char s[40];+ gfx_text(FONT_SMALL, 15, 201, sv.name, C_WHITE, 255);+ snprintf(s, sizeof s, "LV %d", sv.lv);+ gfx_text(FONT_SMALL, 66, 201, s, C_WHITE, 255);+ gfx_sprite(SPR_HPNAME, 0, 122, 202, 0, 255);+ int w = sv.maxhp * 6 / 10 + 1, f = max_i(0, sv.hp) * 6 / 10;+ if (sv.hp > 0 && f == 0) f = 1;+ gfx_rect(137, 200, w, 10, rgb565(0xC00000), 255);+ gfx_rect(137, 200, f, 10, C_YELLOW, 255);+ snprintf(s, sizeof s, "%d / %d", max_i(0, sv.hp), sv.maxhp);+ gfx_text(FONT_SMALL, 137 + w + 8, 201, s, C_WHITE, 255);+}++static void draw_buttons(void) {+ static const uint16_t spr[4] = {SPR_FIGHTBT, SPR_TALKBT, SPR_ITEMBT, SPR_SPAREBT};+ bool menu = bt.phase >= P_MENU && bt.phase <= P_MERCY;+ for (int i = 0; i < 4; i++) {+ bool on = menu && bt.button == i;+ gfx_sprite(spr[i], on ? 1 : 0, button_x[i], BUTTON_Y, 0, 255);+ if (on && bt.phase == P_MENU) gfx_sprite(SPR_HEART, 0, button_x[i] + 8, BUTTON_Y + 11, 0, 255);+ }+}++/* "* Name" lines in the box, two columns or one */+static void draw_list(const char *const *names, int n, int sel, bool two_cols, const uint16_t *colors) {+ for (int i = 0; i < n; i++) {+ int col = two_cols ? i % 2 : 0, row = two_cols ? i / 2 : i;+ int x = 50 + col * 128, y = 133 + row * 16;+ char s[48];+ snprintf(s, sizeof s, "* %s", names[i]);+ gfx_text(FONT_MAIN, x, y, s, colors ? colors[i] : C_WHITE, 255);+ if (i == sel) gfx_sprite(SPR_HEART, 0, x - 14, y + 7, 0, 255);+ }+}++static void draw_box_contents(void) {+ switch (bt.phase) {+ case P_MENU:+ case P_RESULT:+ case P_WON:+ case P_FLEE:+ writer_draw(&bt.w, C_WHITE);+ break;+ case P_TARGET: {+ const char *names[MAX_MON];+ uint16_t cols[MAX_MON];+ int n = 0;+ for (int i = 0; i < bt.nmon; i++)+ if (bt.mon[i].alive && !bt.mon[i].gone) {+ names[n] = bt.mon[i].def->name;+ cols[n] = bt.mon[i].spare && flag(F_FROGGIT_YELLOW) != 1 ? (flag(F_FROGGIT_YELLOW) == 2 ? rgb565(0xFF80C0) : C_YELLOW) : C_WHITE;+ if (bt.button == B_FIGHT) {+ /* the HP bar, in the game's green over red */+ int y = 133 + n * 16 + 3, hp = bt.mon[i].hp * 50 / max_i(1, bt.mon[i].maxhp);+ gfx_rect(200, y, 50, 8, rgb565(0xC00000), 255);+ gfx_rect(200, y, hp, 8, rgb565(0x00FF00), 255);+ }+ n++;+ }+ draw_list(names, n, bt.target, false, cols);+ break;+ }+ case P_ACTS: {+ Monster *m = &bt.mon[alive_index(bt.target)];+ const char *names[6] = {"Check"};+ int n = 1;+ for (int i = 0; i < m->def->nacts; i++) names[n++] = m->def->acts[i];+ draw_list(names, n, bt.act, true, NULL);+ break;+ }+ case P_ITEMS: {+ const char *names[4];+ int page = bt.item / 4, n = 0;+ for (int i = page * 4; i < page * 4 + 4 && i < item_count(); i++) names[n++] = item_info(sv.items[i])->short_name;+ draw_list(names, n, bt.item % 4, true, NULL);+ char s[16];+ snprintf(s, sizeof s, "PAGE %d", page + 1);+ gfx_text(FONT_MAIN, 236, 165, s, C_WHITE, 255);+ break;+ }+ case P_MERCY: {+ const char *names[2] = {"Spare", "Flee"};+ bool any = false;+ for (int i = 0; i < bt.nmon; i++) any |= bt.mon[i].alive && !bt.mon[i].gone && bt.mon[i].spare;+ uint16_t cols[2] = {any && flag(F_FROGGIT_YELLOW) != 1 ? (flag(F_FROGGIT_YELLOW) == 2 ? rgb565(0xFF80C0) : C_YELLOW) : C_WHITE, C_WHITE};+ draw_list(names, bt.no_flee ? 1 : 2, bt.mercy, false, cols);+ break;+ }+ case P_BAR:+ case P_STRIKE:+ gfx_sprite(SPR_TARGET, 0, 19, 128, 0, 255);+ if (bt.phase == P_BAR || (bt.t / 2) % 2) {+ int bx = 19 + bt.bar_x / FX - 3;+ if (bt.bar_x >= 0) gfx_sprite(SPR_TARGETCHOICE, bt.phase == P_STRIKE ? (bt.t / 3) % 2 : 0, bx, 128, 0, 255);+ }+ break;+ default:+ break;+ }+}++static void draw_bubble(void) {+ if (bt.phase != P_TALK || bt.talk_i < 0) return;+ Monster *m = &bt.mon[bt.talk_i];+ int bx = m->x + 30, by = m->y - 70;+ if (bx + 104 > 316) bx = 316 - 104;+ int w = 100, h = 44;+ gfx_rect(bx, by, w, h, C_WHITE, 255);+ gfx_rect(bx - 6, by + 18, 6, 4, C_WHITE, 255);+ bt.bw.x = bx + 6, bt.bw.y = by + 5;+ writer_draw(&bt.bw, C_BLACK);+}++static void draw_damage(void) {+ for (int i = 0; i < bt.nmon; i++) {+ Monster *m = &bt.mon[i];+ if (!m->dmg_t) continue;+ int y = m->y - 70 - (m->dmg_t > 35 ? (45 - m->dmg_t) : 10);+ char s[16];+ if (m->dmg_shown <= 0) {+ gfx_text(FONT_MAIN, m->x - 16, y, "MISS", rgb565(0xC0C0C0), 255);+ } else {+ snprintf(s, sizeof s, "%d", m->dmg_shown);+ gfx_text(FONT_MAIN, m->x - 4 * (int)strlen(s), y, s, C_RED, 255);+ /* the HP bar, going down */+ int w = 60, shown = m->bar_shown - (int)((m->bar_shown - max_i(0, m->hp)) * min_i(45 - m->dmg_t, 15) / 15);+ gfx_rect(m->x - w / 2, y + 14, w, 7, rgb565(0x404040), 255);+ gfx_rect(m->x - w / 2, y + 14, shown * w / max_i(1, m->maxhp), 7, rgb565(0x00FF00), 255);+ }+ }+}++void battle_draw(void) {+ if (bt.phase == P_INTRO) {+ /* the SOUL flickers where Frisk was, then flies to FIGHT */+ if (bt.t < 12) {+ if ((bt.t / 2) % 2 == 0) gfx_sprite(SPR_HEART, 0, heart_x, heart_y, 0, 255);+ return;+ }+ int k = bt.t - 12;+ int tx = bt.scripted ? soul_x() : button_x[0] + 8, ty = bt.scripted ? soul_y() : BUTTON_Y + 11;+ int x = heart_x + (tx - heart_x) * k / 12, y = heart_y + (ty - heart_y) * k / 12;+ gfx_sprite(SPR_HEART, 0, x, y, 0, 255);+ return;+ }+ if (bt.phase == P_DEATH) {+ int x = soul_x(), y = soul_y();+ if (bt.t < 25) gfx_sprite(SPR_HEART, 0, x, y, 0, 255);+ else if (bt.t < 45) gfx_sprite(SPR_HEARTBREAK, 0, x, y, 0, 255);+ else+ for (int k = 0; k < 6; k++) {+ int dt = bt.t - 45;+ gfx_sprite(SPR_HEARTSHARDS, (dt / 4) % 4, x + (k - 3) * dt / 2, y - 10 + dt * dt / 30 - (k % 3) * 6, 0, 255);+ }+ return;+ }+ if (bt.scripted) {+ flowey_battle_draw();+ return;+ }+ gfx_sprite(SPR_BATTLEBG, 0, 0, 0, 0, 255);+ for (int i = 0; i < bt.nmon; i++) draw_monster(&bt.mon[i], i);+ /* the box */+ gfx_rect(bt.x0, bt.y0, bt.x1 - bt.x0, bt.y1 - bt.y0, C_WHITE, 255);+ gfx_rect(bt.x0 + 2, bt.y0 + 2, bt.x1 - bt.x0 - 4, bt.y1 - bt.y0 - 4, C_BLACK, 255);+ gfx_clip(bt.x0 + 2, bt.y0 + 2, bt.x1 - bt.x0 - 4, bt.y1 - bt.y0 - 4);+ draw_box_contents();+ if (bt.phase == P_ATTACK) {+ attack_draw(&bt, bt.atk, bt.atk_t);+ bullets_draw();+ if (!bt.inv || (bt.inv / 2) % 2) gfx_sprite(SPR_HEART, bt.soul_mode, soul_x(), soul_y(), 0, 255);+ }+ gfx_clip_all();+ if (bt.phase == P_FLEE) gfx_sprite(SPR_HEARTGTFO, (bt.t / 3) % 2, button_x[3] + 8 - bt.t * 3, BUTTON_Y + 11, 0, 255);+ if (bt.phase == P_STRIKE && bt.t < 18 && bt.strike_dmg >= 0) {+ Monster *m = &bt.mon[bt.target];+ gfx_sprite(SPR_STRIKE, bt.t / 3, m->x, m->y - 30, 0, 255);+ }+ draw_damage();+ draw_bubble();+ draw_hud();+ draw_buttons();+}diff --git a/games/undertale/src/battle.h b/games/undertale/src/battle.hindex d64165ca..f9f5f29c 100644--- a/games/undertale/src/battle.h+++ b/games/undertale/src/battle.h@@ -1,13 +1,167 @@-/* Undertale: battles (encounters). */+/* Undertale: battles.+ *+ * The original's battle is 640 x 480; here it is halved, as everything else:+ * the box, the buttons and the bullets at half their size, the monsters at the+ * size the game draws them (it doubles its sprites). Positions on the bullet+ * board are kept in 1/16 pixels, so bullets move as finely as there.+ *+ * A battle is a formation (FORM_*: who is fought, where, its music none) of+ * up to three monsters (MonDef: stats, texts, ACTs, attacks). A turn: the+ * player picks FIGHT, ACT, ITEM or MERCY; the monsters talk in their bubbles;+ * the box shrinks to the bullet board and the monsters attack the SOUL. */#ifndef UT_BATTLE_H#define UT_BATTLE_H#include <stdbool.h>#include <stdint.h>+#include "text.h"-void battle_start(int id);+enum { BR_WON, BR_SPARED, BR_FLED, BR_DIED, BR_SCRIPTED };++#define FX 16 /* fixed point: 1/16 pixel */+#define MAX_MON 3+#define MAX_BULLETS 96++/* the box: the menu's, wide; the board's for attacks, by default */+#define BOX_X0 16+#define BOX_Y0 125+#define BOX_X1 303+#define BOX_Y1 195+#define BOARD_W 77+#define BOARD_H 65++struct Battle;+struct Monster;++typedef struct {+ const char *name;+ int16_t hp, at, df, exp, gold;+ uint16_t spr_idle, spr_hurt; /* SPR_*: idle animation (its frames), hurt */+ uint8_t idle_speed; /* frames between idle frames */+ int8_t dx, dy; /* where it stands, from its slot */+ const char *check; /* after "* NAME - ATK n DEF n\n" */+ const char *acts[5]; /* besides Check */+ uint8_t nacts;+ const char *encounter; /* the first turn's text, alone */+ const char *neutral[6]; /* flavour texts, at random */+ const char *quotes[6]; /* what it says, at random */+ const char *low_hp, *spareable; /* flavour texts then */+ uint8_t flags; /* MF_* */+ /* its own ways: an ACT (fills text, may set m->spare), its talk this turn, its attack this turn */+ void (*act)(struct Battle *b, struct Monster *m, int act, char *text, int cap);+ const char *(*talk)(struct Battle *b, struct Monster *m);+ int (*attack)(struct Battle *b, struct Monster *m);+ const char *(*flavor)(struct Battle *b, struct Monster *m);+ bool (*on_hit)(struct Battle *b, struct Monster *m, int dmg); /* true: its own way (no death) */+ void (*draw)(struct Battle *b, struct Monster *m, int x, int y);+ bool (*ends)(struct Battle *b, struct Monster *m); /* after its talk: true, it leaves the battle */+ void (*on_spare)(struct Battle *b, struct Monster *m); /* spared while it isn't spareable (Toriel counts) */+} MonDef;+enum {+ MF_ONE_HIT = 1, /* dies in one hit (Whimsun) */+ MF_BOSS = 2, /* no fleeing */+ MF_NO_CHECK = 4,+ MF_SPARE_START = 8, /* spareable from the start */+ MF_NOT_KILLABLE = 16,+ MF_MISS_IS_SPARE = 32 /* a missed FIGHT counts as a spare (Toriel) */+};++typedef struct Monster {+ const MonDef *def;+ int16_t hp, maxhp, at, df, exp, gold;+ int16_t x, y; /* its feet's middle */+ bool alive, gone, spare; /* gone: killed, spared or ran */+ bool killed;+ uint8_t fade, dust; /* fading out (spared or ran) / turning to dust: frames */+ uint8_t hurt; /* frames showing hurt */+ int16_t shake; /* hurt: its shake */+ int16_t acts[6]; /* each ACT, how often */+ int16_t state, state2, turns;+ const char *say; /* its bubble this turn */+ int16_t dmg_shown, dmg_t; /* the damage number over it */+ int16_t bar_shown; /* its HP before the hit (the bar's fall) */+ int16_t spare_gold; /* the gold it gives when spared */+ int16_t spr_override; /* a sprite its ways show instead, 0 none */+} Monster;++/* the bullets: a pool, moved by kind each frame; the attack's code makes them and steers them */+enum { BK_NONE, BK_LINEAR, BK_GRAVITY, BK_HOMING, BK_ORBIT, BK_SCRIPT };+enum { BC_WHITE, BC_BLUE, BC_ORANGE, BC_GREEN };+typedef struct {+ uint8_t kind, color, frame, flags;+ uint16_t spr;+ int32_t x, y, vx, vy, ax, ay; /* 1/16 pixels (and per frame, per frame squared) */+ int16_t r; /* its hit radius (pixels), a box if w/h */+ int16_t w, h;+ int16_t t, life; /* frames alive; dies at life (0: never) */+ int16_t a, b, c, d; /* the attack's own */+ uint8_t anim, alpha;+ int8_t dmg; /* 0: the monster's AT */+ int16_t angle; /* drawn turned (1/256 turns) */+ uint8_t owner; /* the monster that made it */+} Bullet;+enum { BF_NOCLIP = 1, BF_ONCE = 2, BF_HIDDEN = 4, BF_BOX = 8, BF_FRONT = 16, BF_HARMLESS = 32 };++typedef struct Battle {+ int form;+ int phase, t;+ Monster mon[MAX_MON];+ int nmon;+ int button, target, act, item, mercy, page;+ Writer w; /* the box's text */+ char text[600];+ /* the box now, and where it is going */+ int16_t x0, y0, x1, y1, tx0, ty0, tx1, ty1;+ /* the SOUL */+ int32_t sx, sy; /* its middle, 1/16 pixels */+ int soul_mode;+ int inv; /* invincible for frames */+ int speed; /* 1/16 pixels a frame */+ /* the attack */+ int atk, atk_t, atk_len;+ int atk_owner;+ int turn;+ int bar_x, bar_dir; /* FIGHT: the bar, 1/16 px */+ int strike_t, strike_dmg;+ bool flee_ok, scripted, no_flee, no_buttons;+ int result;+ int exp_won, gold_won;+ int kr; /* (for later) */+ int16_t state[8]; /* the formation's own */+ int32_t as[16]; /* the attack's own */+ int talk_i; /* whose bubble shows */+ Writer bw; /* the bubble's text */+ int hp_shown;+ int last_action; /* 0 FIGHT, 1 ACT, 2 ITEM, 3 Spare, 4 Flee */+} Battle;+extern Battle bt;+extern Bullet bullets[MAX_BULLETS];++void battle_start(int form);bool battle_over(void);-int battle_result(void); /* BR_*: how the last one ended */+int battle_result(void);void battle_update(uint32_t keys, uint32_t pressed);void battle_draw(void);-enum { BR_WON, BR_SPARED, BR_FLED, BR_DIED, BR_SCRIPTED };++/* for the attacks (bullets.c) */+Bullet *bullet_new(uint16_t spr, int x, int y, int vx, int vy); /* x, y in 1/16 px */+void bullets_clear(void);+void bullets_update(void);+void bullets_draw(void);+bool soul_hit_test(void); /* bullets touching the SOUL hurt it */+void soul_update(uint32_t keys);+void soul_hurt(int dmg, int color);+void board_set(int w, int h); /* the board, centred, as the game resizes it */+void board_set_rect(int x0, int y0, int x1, int y1);+int board_cx(void), board_cy(void); /* its middle (pixels) */+int soul_x(void), soul_y(void); /* (pixels) */+int damage_to_player(int at);+int count_alive(void);++/* the monsters and formations (monsters.c) */+const MonDef *mon_def(int id);+int form_setup(int form, Battle *b); /* places the monsters; returns the encounter text's id */+const char *form_text(int form);+void attack_run(Battle *b, int atk, int t); /* one frame of attack atk (ATK_*) */+void attack_draw(Battle *b, int atk, int t); /* what it draws besides its bullets */+int attack_length(int atk);#endifdiff --git a/games/undertale/src/bullets.c b/games/undertale/src/bullets.cindex 60389322..a4768b78 100644--- a/games/undertale/src/bullets.c+++ b/games/undertale/src/bullets.c@@ -1,2 +1,149 @@-/* The bullet board's bullets. */+/* The bullet board: the SOUL, the bullets, what hurts (battle.h). */+#include <string.h>+#include "battle.h"#include "game.h"+#include "gfx.h"+#include "items.h"+#include "res.h"++Bullet bullets[MAX_BULLETS];++Bullet *bullet_new(uint16_t spr, int x, int y, int vx, int vy) {+ for (int i = 0; i < MAX_BULLETS; i++)+ if (bullets[i].kind == BK_NONE) {+ Bullet *b = &bullets[i];+ memset(b, 0, sizeof *b);+ b->kind = BK_LINEAR, b->spr = spr, b->x = x, b->y = y, b->vx = vx, b->vy = vy;+ b->alpha = 255;+ Frame f;+ if (frame_get(spr, 0, &f)) b->r = (int16_t)(((f.w < f.h ? f.w : f.h) * 2 / 5));+ if (b->r < 2) b->r = 2;+ return b;+ }+ return NULL;+}++void bullets_clear(void) { memset(bullets, 0, sizeof bullets); }++int soul_x(void) { return bt.sx / FX; }+int soul_y(void) { return bt.sy / FX; }++/* the SOUL: 8 x 8, moved inside the board by the arrows (X held: slower, as in the game) */+void soul_update(uint32_t keys) {+ int sp = bt.speed;+ if (keys & K_X) sp /= 2;+ int dx = ((keys & K_RIGHT) != 0) - ((keys & K_LEFT) != 0), dy = ((keys & K_DOWN) != 0) - ((keys & K_UP) != 0);+ bt.sx += dx * sp, bt.sy += dy * sp;+ int x0 = (bt.x0 + 2 + 4) * FX, x1 = (bt.x1 - 2 - 4) * FX, y0 = (bt.y0 + 2 + 4) * FX, y1 = (bt.y1 - 2 - 4) * FX;+ if (bt.sx < x0) bt.sx = x0;+ if (bt.sx > x1) bt.sx = x1;+ if (bt.sy < y0) bt.sy = y0;+ if (bt.sy > y1) bt.sy = y1;+}++void bullets_update(void) {+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind == BK_NONE) continue;+ if (b->kind != BK_SCRIPT) b->t++;+ switch (b->kind) {+ case BK_LINEAR:+ case BK_GRAVITY:+ b->vx += b->ax, b->vy += b->ay;+ b->x += b->vx, b->y += b->vy;+ break;+ case BK_HOMING: {+ /* turns towards the SOUL, at its speed */+ int dx = bt.sx - b->x, dy = bt.sy - b->y;+ int d = abs_i(dx) + abs_i(dy);+ if (d) {+ b->vx += dx * b->a / d, b->vy += dy * b->a / d;+ int cap = b->b;+ if (b->vx > cap) b->vx = cap;+ if (b->vx < -cap) b->vx = -cap;+ if (b->vy > cap) b->vy = cap;+ if (b->vy < -cap) b->vy = -cap;+ }+ b->x += b->vx, b->y += b->vy;+ break;+ }+ case BK_ORBIT: {+ /* around (a, b) in pixels, radius c (1/16 px), angle d (1/256 turns, x16) */+ b->d = (int16_t)(b->d + b->vx);+ int r = b->c;+ b->x = b->a * FX + r * gfx_cos(b->d / 16) / 1024;+ b->y = b->b * FX + r * gfx_sin(b->d / 16) / 1024;+ b->c = (int16_t)(b->c + b->vy);+ break;+ }+ default:+ break;+ }+ if (b->kind != BK_SCRIPT && b->anim && b->t % b->anim == 0) b->frame++;+ if (b->life && b->t >= b->life) b->kind = BK_NONE;+ /* far out of the board: gone */+ int px = b->x / FX, py = b->y / FX;+ if (px < -60 || px > SCREEN_W + 60 || py < -60 || py > SCREEN_H + 60) b->kind = BK_NONE;+ }+}++void bullets_draw(void) {+ for (int i = 0; i < MAX_BULLETS; i++) {+ const Bullet *b = &bullets[i];+ if (b->kind == BK_NONE || (b->flags & BF_HIDDEN)) continue;+ uint16_t color = b->color == BC_BLUE ? rgb565(0x14A9FF) : b->color == BC_ORANGE ? rgb565(0xFCA600) : b->color == BC_GREEN ? rgb565(0x00C000) : 0;+ if (b->angle) gfx_sprite_ex(b->spr, b->frame, b->x / FX, b->y / FX, 256, 256, b->angle, color, b->alpha);+ else if (color) gfx_sprite_color(b->spr, b->frame, b->x / FX, b->y / FX, 0, color, b->alpha);+ else gfx_sprite(b->spr, b->frame, b->x / FX, b->y / FX, 0, b->alpha);+ }+}++/* the damage of a monster's bullet, as the game reckons it: its AT, one more at each of the SOUL's HP+ * thresholds, less a fifth of DF (the shown one), at least 1 */+int damage_to_player(int at) {+ static const int16_t steps[] = {21, 30, 40, 50, 60, 70, 80, 90};+ int dmg = at;+ for (int i = 0; i < 8; i++)+ if (sv.maxhp >= steps[i]) dmg++;+ int df = sv.df - 10 + armor_df();+ dmg -= (df + 2) / 5;+ return dmg < 1 ? 1 : dmg;+}++void soul_hurt(int dmg, int color) {+ (void)color;+ if (bt.inv) return;+ sv.hp = (int16_t)(sv.hp - dmg);+ if (sv.hp < 0) sv.hp = 0;+ bt.inv = 30;+}++bool soul_hit_test(void) {+ int sx = bt.sx / FX, sy = bt.sy / FX;+ bool hit = false;+ static int last_x, last_y;+ bool moved = sx != last_x || sy != last_y;+ last_x = sx, last_y = sy;+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind == BK_NONE || (b->flags & (BF_HIDDEN | BF_HARMLESS))) continue;+ int bx = b->x / FX, by = b->y / FX;+ bool touch;+ if (b->flags & BF_BOX) touch = sx + 3 > bx - b->w / 2 && sx - 3 < bx + b->w / 2 && sy + 3 > by - b->h / 2 && sy - 3 < by + b->h / 2;+ else touch = (sx - bx) * (sx - bx) + (sy - by) * (sy - by) <= (b->r + 3) * (b->r + 3);+ if (!touch) continue;+ if (b->color == BC_BLUE && !moved) continue; /* blue: still, it passes */+ if (b->color == BC_ORANGE && moved) continue; /* orange: moving, it passes */+ if (b->color == BC_GREEN) {+ sv.hp = (int16_t)min_i(sv.maxhp, sv.hp + (b->dmg ? b->dmg : 1));+ b->kind = BK_NONE;+ continue;+ }+ if (bt.inv) continue;+ int at = b->dmg ? b->dmg : bt.mon[b->owner].at;+ soul_hurt(damage_to_player(at), b->color);+ hit = true;+ if (b->flags & BF_ONCE) b->kind = BK_NONE;+ }+ return hit;+}diff --git a/games/undertale/src/data.h b/games/undertale/src/data.hindex e553aee7..6d246b80 100644--- a/games/undertale/src/data.h+++ b/games/undertale/src/data.h@@ -21,42 +21,95 @@#define SPR_SAVEPOINT 8#define SPR_FLOWEY 9#define SPR_HEART 10-#define SPR_FACE_TORIELTALK 11-#define SPR_FACE_TORIELTALKSIDE 12-#define SPR_FACE_TORIELHAPPYTALK 13-#define SPR_FACE_TORIELWHAT 14-#define SPR_FACE_TORIELWHATSIDE 15-#define SPR_FACE_TORIELMAD 16-#define SPR_FACE_TORIELCOLD 17-#define SPR_FACE_TORIELSURPRISED 18-#define SPR_FACE_TORIELEMBARRASSED 19-#define SPR_FACE_TORIELGLASSES 20-#define SPR_FACE_TORIELREVENGETALK 21-#define SPR_FACE_TORIELSCARED 22-#define SPR_FACE_TORIELBLINK 23-#define SPR_FACE_TORIELHAPPYBLINK 24-#define SPR_FACE_TORIELBODY 25-#define SPR_FACE_SANS 26-#define SPR_FACE_SANSCHUCKLE 27-#define SPR_FACE_SANSWINK 28-#define SPR_FACE_PAPYRUS 29-#define SPR_FACE_PAPYRUSEVIL 30-#define SPR_FACE_PAPYRUSLAUGH 31-#define SPR_FACE_PAPYRUSMAD 32-#define SPR_FACE_PAPYRUSSIDE 33-#define SPR_FLOWEYNICE 34-#define SPR_FLOWEYNICESIDE 35-#define SPR_FLOWEYSASSY 36-#define SPR_FLOWEYEVIL 37-#define SPR_FLOWEYGRIN 38-#define SPR_FLOWEYPLAIN 39-#define SPR_FLOWEYPISSED 40-#define SPR_FLOWEYWINK 41-#define SPR_FLOWEYLAUGH 42-#define SPR_FLOWEYHURT 43-#define SPR_FLOWEYTORIEL 44-#define SPR_FLOWEYTORIEL2 45-#define SPR_FLOWEYSIDESHOCK 46+#define SPR_FIGHTBT 11+#define SPR_TALKBT 12+#define SPR_ITEMBT 13+#define SPR_SPAREBT 14+#define SPR_TARGET 15+#define SPR_TARGETCHOICE 16+#define SPR_HPNAME 17+#define SPR_BATTLEBG 18+#define SPR_STRIKE 19+#define SPR_HEARTBREAK 20+#define SPR_HEARTSHARDS 21+#define SPR_HEARTGTFO 22+#define SPR_FLYBULLET 23+#define SPR_SMALLFROGBULLET 24+#define SPR_BUTTERFLYBLLT 25+#define SPR_BULLETMD 26+#define SPR_BULLETSM 27+#define SPR_CIRCLEBULLETMD1 28+#define SPR_CIRCLEBULLETSM 29+#define SPR_VEGBULLET 30+#define SPR_CARROTBULLET 31+#define SPR_ROACHBULLET 32+#define SPR_ROACHDANCE 33+#define SPR_TEARDROP 34+#define SPR_FIREBULLET 35+#define SPR_HANDBULLET 36+#define SPR_SPINBULLET 37+#define SPR_STREAMBULLET 38+#define SPR_DIRBULLET 39+#define SPR_BULLETGENMD 40+#define SPR_DUMMY_B 41+#define SPR_FROGGIT_B 42+#define SPR_FROGGIT_HURT 43+#define SPR_WHIMSUN_B 44+#define SPR_WHIMSUN_HURT 45+#define SPR_MOLDSMAL_B 46+#define SPR_LOOX_B 47+#define SPR_LOOX_HURT 48+#define SPR_VEGETOID_B 49+#define SPR_VEGETOID_HURT 50+#define SPR_MIGOSP_B 51+#define SPR_MIGOSP_HURT 52+#define SPR_NAPSTABLOOK_B 53+#define SPR_NAPSTA_HAT 54+#define SPR_TORIEL_B 55+#define SPR_TORIEL_HURT 56+#define SPR_TORIEL_AWAY 57+#define SPR_TORIEL_FIGHT 58+#define SPR_TORIEL_DEAD 59+#define SPR_INTROIMAGE 60+#define SPR_INTROLAST 61+#define SPR_UNDERTALETITLE 62+#define SPR_GAMEOVERBG 63+#define SPR_FACE_TORIELTALK 64+#define SPR_FACE_TORIELTALKSIDE 65+#define SPR_FACE_TORIELHAPPYTALK 66+#define SPR_FACE_TORIELWHAT 67+#define SPR_FACE_TORIELWHATSIDE 68+#define SPR_FACE_TORIELMAD 69+#define SPR_FACE_TORIELCOLD 70+#define SPR_FACE_TORIELSURPRISED 71+#define SPR_FACE_TORIELEMBARRASSED 72+#define SPR_FACE_TORIELGLASSES 73+#define SPR_FACE_TORIELREVENGETALK 74+#define SPR_FACE_TORIELSCARED 75+#define SPR_FACE_TORIELBLINK 76+#define SPR_FACE_TORIELHAPPYBLINK 77+#define SPR_FACE_TORIELBODY 78+#define SPR_FACE_SANS 79+#define SPR_FACE_SANSCHUCKLE 80+#define SPR_FACE_SANSWINK 81+#define SPR_FACE_PAPYRUS 82+#define SPR_FACE_PAPYRUSEVIL 83+#define SPR_FACE_PAPYRUSLAUGH 84+#define SPR_FACE_PAPYRUSMAD 85+#define SPR_FACE_PAPYRUSSIDE 86+#define SPR_FLOWEYNICE 87+#define SPR_FLOWEYNICESIDE 88+#define SPR_FLOWEYSASSY 89+#define SPR_FLOWEYEVIL 90+#define SPR_FLOWEYGRIN 91+#define SPR_FLOWEYPLAIN 92+#define SPR_FLOWEYPISSED 93+#define SPR_FLOWEYWINK 94+#define SPR_FLOWEYLAUGH 95+#define SPR_FLOWEYHURT 96+#define SPR_FLOWEYTORIEL 97+#define SPR_FLOWEYTORIEL2 98+#define SPR_FLOWEYSIDESHOCK 99#define AREA_RUINS 0#define ROOM_AREA1 0#define ROOM_AREA1_2 1@@ -105,8 +158,9 @@#define ROOM_BASEMENT3 44#define ROOM_RUINSEXIT 45#define FONT_MAIN 0-#define FONT_SMALL 1-#define NUM_SPRITES 47+#define FONT_MENU 1+#define FONT_SMALL 2+#define NUM_SPRITES 100#define NUM_ROOMS 46-#define NUM_FONTS 2+#define NUM_FONTS 3#endifdiff --git a/games/undertale/src/events.h b/games/undertale/src/events.hindex f1ff3fbc..99abfcf6 100644--- a/games/undertale/src/events.h+++ b/games/undertale/src/events.h@@ -4,6 +4,8 @@enum {EV_NONE,EV_SIGN, /* a sign or anything that only says its text (self->arg: the text, in events.c) */+ EV_PHONE_MENU, /* CELL: calling Toriel */+ EV_SAVE, /* a SAVE Point */EV_COUNT};#endifdiff --git a/games/undertale/src/flowey.c b/games/undertale/src/flowey.cnew file mode 100644index 00000000..a46156a0--- /dev/null+++ b/games/undertale/src/flowey.c@@ -0,0 +1,211 @@+/* The first battle: Flowey and his "friendliness pellets", then Toriel. A scripted battle: no buttons, the SOUL+ * free in a small box, Flowey's words in his bubble (from the Undertale Wiki's records of the game). */+#include <stdio.h>+#include <string.h>+#include "battle.h"+#include "game.h"+#include "gfx.h"+#include "monsters.h"+#include "res.h"++enum {+ F_TALK1, F_PELLETS, F_AFTER, F_RING, F_FIREBALL, F_TORIEL_IN, F_TORIEL_TALK, F_DONE+};+/* bt.state: [0] step, [1] times dodged, [2] Flowey's face (a sprite), [3] Flowey's x offset, [4] Toriel's x */+static const char *talk_text;+static int ring_r;++static void bubble(const char *s) {+ talk_text = s;+ writer_start(&bt.bw, s, 0, 0, FONT_SMALL);+ bt.bw.line_h = 9;+}++static const char intro_text[] =+ "See that\nheart? That\nis your SOUL,\nthe very\nculmination\nof your\nbeing!|"+ "Your SOUL\nstarts off\nweak, but can\ngrow strong\nif you gain a\nlot of LV.|"+ "What's LV\nstand for?\nWhy, LOVE,\nof course!|"+ "You want\nsome LOVE,\ndon't you?|"+ "`7Don't worry,\nI'll share\nsome with\nyou!";+static const char share_text[] =+ "Down here,\nLOVE is\nshared\nthrough...|"+ "Little\nwhite...\n\"friendliness\npellets.\"|"+ "Are you\nready?|"+ "Move around!\nGet as many\nas you can!";+static const char hit_text[] =+ "`4You idiot.|"+ "In this\nworld, it's\nkill or BE\nkilled.|"+ "Why would\nANYONE pass\nup an\nopportunity\nlike this!?|"+ "`3Die.";+static const char miss1_text[] = "`6Hey buddy,\nyou missed\nthem.|`0Let's try\nagain, okay?";+static const char miss2_text[] = "`6Is this a\njoke? Are you\nbraindead?|RUN. INTO.\nTHE.\nfriendliness\npellets";+static const char miss3_text[] = "`2You know\nwhat's going\non here,\ndon't you?|You just\nwanted to see\nme suffer.|`3Die.";+static const char toriel_text[] =+ "What a\nterrible\ncreature,\ntorturing\nsuch a poor,\ninnocent\nyouth...|"+ "Ah, do not\nbe afraid, my\nchild.|"+ "I am #YTORIEL#X,\ncaretaker of\nthe #YRUINS#X.|"+ "I pass\nthrough this\nplace every\nday to see if\nanyone has\nfallen down.|"+ "You are the\nfirst human\nto come here\nin a long\ntime.|"+ "Come! I will\nguide you\nthrough the\ncatacombs.";++static const uint16_t faces[] = {SPR_FLOWEYNICE, SPR_FLOWEYNICESIDE, SPR_FLOWEYSASSY, SPR_FLOWEYEVIL, SPR_FLOWEYGRIN,+ SPR_FLOWEYPLAIN, SPR_FLOWEYPISSED, SPR_FLOWEYWINK, SPR_FLOWEYLAUGH, SPR_FLOWEYHURT};++void flowey_battle_start(void) {+ bt.scripted = true;+ bt.state[0] = F_TALK1;+ bt.state[2] = 0;+ board_set(84, 76);+ bt.x0 = bt.tx0, bt.y0 = bt.ty0, bt.x1 = bt.tx1, bt.y1 = bt.ty1;+ bt.sx = board_cx() * FX, bt.sy = board_cy() * FX + 8 * FX;+ bubble(intro_text);+}++static void pellets(void) {+ /* five pellets in an arc over Flowey, then at the SOUL, a little aimed */+ bullets_clear();+ for (int k = 0; k < 5; k++) {+ int x = 160 + (k - 2) * 14, y = 72 - (k == 0 || k == 4 ? 0 : k == 2 ? 14 : 9);+ Bullet *b = bullet_new(SPR_SPINBULLET, x * FX, y * FX, 0, 0);+ if (b) b->kind = BK_SCRIPT, b->a = (int16_t)(20 + k * 3), b->anim = 3, b->dmg = 19, b->flags = BF_NOCLIP;+ }+}++void flowey_battle_update(uint32_t keys, uint32_t pressed) {+ int st = bt.state[0];+ if (st != F_FIREBALL && st != F_TORIEL_IN) soul_update(keys);+ switch (st) {+ case F_TALK1:+ case F_AFTER:+ case F_TORIEL_TALK:+ bt.state[2] = bt.bw.face_expr;+ if (!writer_update(&bt.bw, pressed)) {+ if (st == F_TORIEL_TALK) {+ bt.state[0] = F_DONE;+ bt.result = BR_SCRIPTED;+ return;+ }+ if (talk_text == intro_text) {+ bubble(share_text);+ bt.state[2] = 7;+ } else if (talk_text == hit_text || talk_text == miss3_text) {+ bt.state[0] = F_RING, bt.t = 0;+ ring_r = 70;+ bullets_clear();+ for (int k = 0; k < 24; k++) {+ Bullet *b = bullet_new(SPR_SPINBULLET, 0, 0, 0, 0);+ if (b) b->kind = BK_SCRIPT, b->a = (int16_t)(k * 256 / 24), b->anim = 3, b->flags = BF_HARMLESS | BF_NOCLIP;+ }+ bt.state[2] = 8;+ } else {+ bt.state[0] = F_PELLETS, bt.t = 0;+ pellets();+ }+ }+ break;+ case F_PELLETS: {+ int alive = 0;+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ alive++;+ if (b->a > 0) {+ b->t++;+ if (b->t % 3 == 0) b->frame++;+ if (--b->a == 0) {+ int dx = bt.sx - b->x, dy = bt.sy - b->y, d = abs_i(dx) + abs_i(dy);+ if (!d) d = 1;+ b->vx = dx * FX * 2 / d, b->vy = dy * FX * 2 / d;+ }+ continue;+ }+ /* a little homing */+ b->vx += (bt.sx > b->x ? 1 : -1) * FX / 32;+ b->x += b->vx, b->y += b->vy;+ if (++b->t % 3 == 0) b->frame++;+ if (b->y > (bt.y1 + 10) * FX || b->x < 0 || b->x > SCREEN_W * FX) b->kind = BK_NONE;+ int dx = b->x / FX - soul_x(), dy = b->y / FX - soul_y();+ if (dx * dx + dy * dy < 36) {+ /* hit: 19, down to 1 */+ sv.hp = 1;+ bullets_clear();+ bt.state[0] = F_AFTER;+ bubble(hit_text);+ bt.state[2] = 4;+ return;+ }+ }+ if (!alive) {+ bt.state[1]++;+ bt.state[0] = F_AFTER;+ bubble(bt.state[1] == 1 ? miss1_text : bt.state[1] == 2 ? miss2_text : miss3_text);+ }+ break;+ }+ case F_RING:+ /* the ring closes in, slowly; Flowey laughs */+ if (bt.t % 3 == 0 && ring_r > 14) ring_r--;+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->x = bt.sx + ring_r * gfx_cos(b->a + bt.t) * FX / 1024;+ b->y = bt.sy + ring_r * gfx_sin(b->a + bt.t) * FX / 1024;+ if (bt.t % 3 == 0) b->frame++;+ }+ if (ring_r <= 14 && bt.t > 150) bt.state[0] = F_FIREBALL, bt.t = 0;+ break;+ case F_FIREBALL:+ /* a fireball from the right knocks Flowey away */+ if (bt.t == 10) {+ bullets_clear();+ bt.state[2] = 9;+ }+ if (bt.t > 10) bt.state[3] = (int16_t)(bt.state[3] - 6);+ if (bt.t == 30) sv.hp = sv.maxhp;+ if (bt.t > 40) bt.state[0] = F_TORIEL_IN, bt.t = 0, bt.state[4] = 340;+ break;+ case F_TORIEL_IN:+ bt.state[4] = (int16_t)max_i(160, bt.state[4] - 4);+ if (bt.state[4] == 160 && bt.t > 30) {+ bt.state[0] = F_TORIEL_TALK;+ bubble(toriel_text);+ }+ break;+ }+}++void flowey_battle_draw(void) {+ int st = bt.state[0];+ gfx_sprite(SPR_BATTLEBG, 0, 0, 0, 0, 255);+ int fx = 160 + bt.state[3];+ if (st <= F_FIREBALL) {+ int e = bt.state[2];+ if (e < 0 || e > 9) e = 0;+ uint16_t s = faces[e];+ int talking = writer_talking(&bt.bw);+ gfx_sprite(s, talking ? (int)(g_frame / 4) % 2 : 0, fx - 21, 72, 0, 255);+ }+ if (st == F_FIREBALL && bt.t < 12) gfx_sprite(SPR_FIREBULLET, (int)(g_frame / 2) % 2, 330 - bt.t * 15, 90, 0, 255);+ if (st >= F_TORIEL_IN) gfx_sprite(SPR_TORIEL_B, 0, bt.state[4], 120, 0, 255);+ gfx_rect(bt.x0, bt.y0, bt.x1 - bt.x0, bt.y1 - bt.y0, C_WHITE, 255);+ gfx_rect(bt.x0 + 2, bt.y0 + 2, bt.x1 - bt.x0 - 4, bt.y1 - bt.y0 - 4, C_BLACK, 255);+ bullets_draw();+ gfx_sprite(SPR_HEART, 0, soul_x(), soul_y(), 0, 255);+ /* the bubble */+ if (st == F_TALK1 || st == F_AFTER || st == F_TORIEL_TALK) {+ int bx = (st == F_TORIEL_TALK ? bt.state[4] : fx) + 30, by = 40;+ gfx_rect(bx, by, 96, 72, C_WHITE, 255);+ gfx_rect(bx - 6, by + 20, 6, 5, C_WHITE, 255);+ bt.bw.x = bx + 5, bt.bw.y = by + 5;+ writer_draw(&bt.bw, C_BLACK);+ }+ /* the HUD, without the buttons */+ char s[32];+ gfx_text(FONT_SMALL, 70, 201, "LV 1", C_WHITE, 255);+ gfx_sprite(SPR_HPNAME, 0, 122, 202, 0, 255);+ int w = sv.maxhp * 6 / 10 + 1, f = max_i(1, sv.hp * 6 / 10);+ gfx_rect(137, 200, w, 10, rgb565(0xC00000), 255);+ gfx_rect(137, 200, f, 10, C_YELLOW, 255);+ snprintf(s, sizeof s, "%02d / %d", sv.hp, sv.maxhp);+ gfx_text(FONT_SMALL, 137 + w + 8, 201, s, C_WHITE, 255);+}diff --git a/games/undertale/src/game.c b/games/undertale/src/game.cindex 748ffb39..79e84ba1 100644--- a/games/undertale/src/game.c+++ b/games/undertale/src/game.c@@ -79,7 +79,8 @@ void game_new(const char *name) {void game_init(void) {save_restore_copy();- menu_title_open();+ /* back where Home left it, or the intro */+ if (!save_resume_load()) menu_title_open();}void game_open_menu(void) { menu_open(); }diff --git a/games/undertale/src/menu.c b/games/undertale/src/menu.cindex 9a1bc484..b4e9cb80 100644--- a/games/undertale/src/menu.c+++ b/games/undertale/src/menu.c@@ -1,8 +1,500 @@-/* The menus (menu.h). */+/* The menus: the intro, the title, naming the fallen human, the C menu (ITEM, STAT, CELL), SAVE, and GAME OVER+ * (menu.h). Their texts are the game's. */+#include <stdio.h>+#include <string.h>+#include "battle.h"+#include "event.h"+#include "events.h"#include "game.h"+#include "gfx.h"+#include "items.h"#include "menu.h"-void menu_title_open(void) { game_new("Frisk"); }-void menu_open(void) {}-void menu_save_open(void) {}-void menu_update(uint32_t keys, uint32_t pressed) { (void)keys, (void)pressed; }-void menu_draw(void) {}+#include "res.h"+#include "save.h"+#include "text.h"+#include "world.h"++static int st, sel, sub, t; /* the screen's step, what is selected, a second level, its frames */+static Writer w;+static char buf[400];+static char name[8];+static int name_len;+static int letter; /* the naming screen's cursor */+static int item_sel, item_act;+static bool msg_open; /* a text in the dialogue box over the menu */++enum { S_INTRO, S_TITLE, S_INSTR, S_NAME, S_CONFIRM, S_CONTINUE, S_GAMEOVER, S_FADEIN };++/* ---------------------------------------------------------------- the intro */+static const char *const intro_lines[] = {+ "Long ago, two races\nruled over Earth:\nHUMANS and MONSTERS.",+ "One day, war broke\nout between the two\nraces.",+ "After a long battle,\nthe humans were\nvictorious.",+ "They sealed the monsters\nunderground with a magic\nspell.",+ "Many years later...",+ " MT. EBOTT\n 201X",+ "Legends say that those\nwho climb the mountain\nnever return.",+ "", "", "", "",+};+#define INTRO_SLIDES 11+static int slide;++static void intro_slide(int k) {+ slide = k, t = 0;+ writer_start(&w, intro_lines[k], 60, 152, FONT_MAIN);+ w.line_h = 16, w.speed = 2;+}++void menu_title_open(void) {+ g_mode = MODE_INTRO;+ st = S_INTRO;+ intro_slide(0);+}++static void title_open(void) {+ g_mode = MODE_TITLE;+ st = S_TITLE, t = 0, sel = 0;+}++/* ---------------------------------------------------------------- naming */+static const char *const special_forbidden[][2] = {+ {"alphys", "D-don't do that."}, {"asgore", "You cannot."}, {"asriel", "..."}, {"flowey", "I already CHOSE that name."},+ {"sans", "nope."}, {"toriel", "I think you should\nthink of your own\nname, my child."}, {"undyne", "Get your OWN name!"},+};+static const char *const special_allowed[][2] = {+ {"frisk", "WARNING: This name will\nmake your life hell.\nProceed anyway?"}, {"murder", "That's a little on-the-\nnose, isn't it...?"},+ {"mercy", "That's a little on-the-\nnose, isn't it...?"}, {"aaaaaa", "Not very creative...?"},+ {"aaron", "Is this name correct? ;)"}, {"alphy", "Uh... OK?"}, {"bpants", "You are really scraping\nthe bottom of the barrel."},+ {"bratty", "Like, OK I guess."}, {"catty", "Bratty! Bratty!\nThat's MY name!"}, {"chara", "The true name."},+ {"gerson", "Wah ha ha! Why not?"}, {"jerry", "Jerry."}, {"metta", "OOOOH!!! ARE YOU\nPROMOTING MY BRAND?"},+ {"mett", "OOOOH!!! ARE YOU\nPROMOTING MY BRAND?"}, {"mtt", "OOOOH!!! ARE YOU\nPROMOTING MY BRAND?"},+ {"napsta", "............\n(They're powerless to\nstop you.)"}, {"blooky", "............\n(They're powerless to\nstop you.)"},+ {"papyru", "I'LL ALLOW IT!!!!"}, {"shyren", "...?"}, {"temmie", "hOI!"}, {"woshua", "Clean name."},+};+static const char *confirm_text;+static bool confirm_allowed;++static bool same_lower(const char *a, const char *b) {+ for (; *a && *b; a++, b++) {+ char x = *a >= 'A' && *a <= 'Z' ? (char)(*a + 32) : *a;+ if (x != *b) return false;+ }+ return !*a && !*b;+}++static void name_done(void) {+ name[name_len] = 0;+ if (!name_len) {+ confirm_text = "You must choose a name.";+ confirm_allowed = false;+ } else {+ confirm_text = "Is this name correct?", confirm_allowed = true;+ for (size_t i = 0; i < sizeof special_forbidden / sizeof special_forbidden[0]; i++)+ if (same_lower(name, special_forbidden[i][0])) confirm_text = special_forbidden[i][1], confirm_allowed = false;+ for (size_t i = 0; i < sizeof special_allowed / sizeof special_allowed[0]; i++)+ if (same_lower(name, special_allowed[i][0])) confirm_text = special_allowed[i][1];+ if (same_lower(name, "gaster")) {+ /* the game starts over */+ name_len = 0;+ menu_title_open();+ return;+ }+ }+ st = S_CONFIRM, sel = 0, t = 0;+}++/* the letters: A-Z, then a-z, 7 a row, then Quit / Backspace / Done */+#define LETTERS 52+static char letter_at(int i) { return (char)(i < 26 ? 'A' + i : 'a' + i - 26); }+static void letter_xy(int i, int *x, int *y) {+ int k = i < 26 ? i : i - 26, row = k / 7, col = k % 7;+ *x = 60 + col * 32, *y = (i < 26 ? 58 : 122) + row * 14;+}++static void name_input(uint32_t pressed) {+ /* letter: 0..51, 52 Quit, 53 Backspace, 54 Done */+ if (pressed & K_RIGHT) letter = letter >= 52 ? (letter == 54 ? 52 : letter + 1) : (letter + 1) % LETTERS;+ if (pressed & K_LEFT) letter = letter >= 52 ? (letter == 52 ? 54 : letter - 1) : (letter + LETTERS - 1) % LETTERS;+ if (pressed & K_DOWN) {+ if (letter >= 52) letter = letter == 52 ? 0 : letter == 53 ? 3 : 6;+ else {+ int k = letter < 26 ? letter : letter - 26;+ if (k + 7 < 26) letter += 7;+ else if (letter < 26) letter = 26 + k % 7;+ else letter = k % 7 < 2 ? 52 : k % 7 < 5 ? 53 : 54;+ }+ }+ if (pressed & K_UP) {+ if (letter >= 52) letter = 26 + 21 + (letter == 52 ? 0 : letter == 53 ? 2 : 4);+ else {+ int k = letter < 26 ? letter : letter - 26;+ if (k >= 7) letter -= 7;+ else if (letter >= 26) letter = (k + 21 < 26) ? k + 21 : k + 14;+ else letter = k < 2 ? 52 : k < 5 ? 53 : 54;+ }+ }+ if (pressed & K_X && name_len) name_len--;+ if (pressed & K_Z) {+ if (letter < LETTERS) {+ if (name_len < 6) name[name_len++] = letter_at(letter);+ } else if (letter == 52) title_open();+ else if (letter == 53) {+ if (name_len) name_len--;+ } else name_done();+ }+}++/* ---------------------------------------------------------------- the C menu */+enum { M_MAIN, M_ITEMS, M_ITEM_ACT, M_STAT, M_CELL };++void menu_open(void) {+ g_mode = MODE_MENU;+ st = M_MAIN, sel = 0;+ msg_open = false;+}++static void menu_close(void) { g_mode = MODE_WORLD; }++static void menu_say(const char *s) {+ snprintf(buf, sizeof buf, "%s", s);+ text_set_top(-1);+ text_open(FACE_NONE, buf);+ msg_open = true;+}++static void c_menu_input(uint32_t pressed) {+ if (msg_open) {+ text_update(pressed);+ if (!text_active()) {+ msg_open = false;+ if (st != M_MAIN) {+ menu_close();+ return;+ }+ }+ return;+ }+ switch (st) {+ case M_MAIN: {+ int n = flag(F_PHONE) ? 3 : 2;+ if (pressed & K_UP) sel = (sel + n - 1) % n;+ if (pressed & K_DOWN) sel = (sel + 1) % n;+ if (pressed & (K_X | K_C)) menu_close();+ if (pressed & K_Z) {+ if (sel == 0 && item_count()) st = M_ITEMS, item_sel = 0;+ else if (sel == 1) st = M_STAT;+ else if (sel == 2) st = M_CELL, sub = 0;+ }+ break;+ }+ case M_ITEMS: {+ int n = item_count();+ if (pressed & K_UP) item_sel = (item_sel + n - 1) % n;+ if (pressed & K_DOWN) item_sel = (item_sel + 1) % n;+ if (pressed & K_X) st = M_MAIN;+ if (pressed & K_Z) st = M_ITEM_ACT, item_act = 0;+ break;+ }+ case M_ITEM_ACT:+ if (pressed & K_LEFT) item_act = (item_act + 2) % 3;+ if (pressed & K_RIGHT) item_act = (item_act + 1) % 3;+ if (pressed & K_X) st = M_ITEMS;+ if (pressed & K_Z) {+ if (item_act == 0) {+ item_use(item_sel, buf, sizeof buf, 0);+ menu_say(buf);+ } else if (item_act == 1) {+ char s[300];+ item_info_text(sv.items[item_sel], s, sizeof s);+ menu_say(s);+ } else {+ char s[120];+ snprintf(s, sizeof s, "* The %s was\n thrown away.", item_info(sv.items[item_sel])->name);+ item_remove_at(item_sel);+ menu_say(s);+ }+ }+ break;+ case M_STAT:+ if (pressed & (K_X | K_Z)) st = M_MAIN;+ break;+ case M_CELL:+ if (pressed & K_X) st = M_MAIN;+ if (pressed & K_Z) {+ menu_close();+ event_start(EV_PHONE_MENU, NULL);+ }+ break;+ }+}++static void panel(int x, int y, int w, int h) {+ gfx_rect(x, y, w, h, C_WHITE, 255);+ gfx_rect(x + 3, y + 3, w - 6, h - 6, C_BLACK, 255);+}++static void draw_c_menu(void) {+ bool low = pl.y + 15 - cam_y < 130;+ int oy = low ? 135 : 0;+ char s[64];+ /* the name and its numbers */+ oy = low ? 135 : 0;+ panel(16, 26 + oy, 71, 55);+ gfx_text(FONT_MENU, 23, 30 + oy, sv.name, C_WHITE, 255);+ snprintf(s, sizeof s, "LV %d", sv.lv);+ gfx_text(FONT_SMALL, 23, 50 + oy, s, C_WHITE, 255);+ snprintf(s, sizeof s, "HP %d/%d", sv.hp, sv.maxhp);+ gfx_text(FONT_SMALL, 23, 59 + oy, s, C_WHITE, 255);+ snprintf(s, sizeof s, "G %d", sv.gold);+ gfx_text(FONT_SMALL, 23, 68 + oy, s, C_WHITE, 255);+ /* ITEM, STAT, CELL */+ int n = flag(F_PHONE) ? 3 : 2;+ panel(16, 84, 71, 18 + 18 * n);+ static const char *const opts[] = {"ITEM", "STAT", "CELL"};+ for (int i = 0; i < n; i++) {+ uint16_t c = (i == 0 && !item_count()) ? rgb565(0x808080) : C_WHITE;+ gfx_text(FONT_MENU, 41, 92 + 18 * i, opts[i], c, 255);+ }+ if (st == M_MAIN) gfx_sprite(SPR_HEART, 0, 32, 98 + 18 * sel, 0, 255);+ if (st == M_ITEMS || st == M_ITEM_ACT) {+ panel(94, 26, 173, 181);+ for (int i = 0; i < item_count(); i++) {+ gfx_text(FONT_MENU, 116, 38 + 16 * i, item_info(sv.items[i])->name, C_WHITE, 255);+ if (st == M_ITEMS && i == item_sel) gfx_sprite(SPR_HEART, 0, 107, 44 + 16 * i, 0, 255);+ }+ static const char *const acts[] = {"USE", "INFO", "DROP"};+ for (int i = 0; i < 3; i++) {+ gfx_text(FONT_MENU, 116 + i * 48, 180, acts[i], C_WHITE, 255);+ if (st == M_ITEM_ACT && i == item_act) gfx_sprite(SPR_HEART, 0, 107 + i * 48, 186, 0, 255);+ }+ }+ if (st == M_STAT) {+ panel(94, 26, 173, 209);+ snprintf(s, sizeof s, "\"%s\"", sv.name);+ gfx_text(FONT_MENU, 108, 38, s, C_WHITE, 255);+ snprintf(s, sizeof s, "LV %d", sv.lv);+ gfx_text(FONT_MENU, 108, 68, s, C_WHITE, 255);+ snprintf(s, sizeof s, "HP %d / %d", sv.hp, sv.maxhp);+ gfx_text(FONT_MENU, 108, 84, s, C_WHITE, 255);+ snprintf(s, sizeof s, "AT %d(%d)", sv.at - 10, weapon_at());+ gfx_text(FONT_MENU, 108, 114, s, C_WHITE, 255);+ snprintf(s, sizeof s, "DF %d(%d)", sv.df - 10, armor_df());+ gfx_text(FONT_MENU, 108, 130, s, C_WHITE, 255);+ snprintf(s, sizeof s, "EXP: %d", (int)sv.exp);+ gfx_text(FONT_MENU, 190, 114, s, C_WHITE, 255);+ snprintf(s, sizeof s, "NEXT: %d", sv.lv < 20 ? lv_exp_for(sv.lv + 1) - (int)sv.exp : 0);+ gfx_text(FONT_MENU, 190, 130, s, C_WHITE, 255);+ snprintf(s, sizeof s, "WEAPON: %s", item_info(sv.weapon)->name);+ gfx_text(FONT_MENU, 108, 160, s, C_WHITE, 255);+ snprintf(s, sizeof s, "ARMOR: %s", item_info(sv.armor)->name);+ gfx_text(FONT_MENU, 108, 176, s, C_WHITE, 255);+ snprintf(s, sizeof s, "GOLD: %d", sv.gold);+ gfx_text(FONT_MENU, 108, 200, s, C_WHITE, 255);+ if (sv.kills > 20) {+ snprintf(s, sizeof s, "KILLS: %d", sv.kills);+ gfx_text(FONT_MENU, 190, 200, s, C_WHITE, 255);+ }+ }+ if (st == M_CELL) {+ panel(94, 26, 173, 60);+ gfx_text(FONT_MENU, 116, 38, "Toriel's Phone", C_WHITE, 255);+ gfx_sprite(SPR_HEART, 0, 107, 44, 0, 255);+ }+}++/* ---------------------------------------------------------------- SAVE */+static int save_state; /* 0 the choice, 1 saved */++void menu_save_open(void) {+ g_mode = MODE_SAVE;+ sel = 0, save_state = 0;+}++static void draw_save(void) {+ panel(54, 58, 212, 82);+ char s[48];+ uint16_t c = save_state ? C_YELLOW : C_WHITE;+ const Save *shown = &sv;+ gfx_text(FONT_MENU, 68, 68, shown->name, c, 255);+ snprintf(s, sizeof s, "LV %d", shown->lv);+ gfx_text(FONT_MENU, 136, 68, s, c, 255);+ unsigned secs = shown->time / 30;+ snprintf(s, sizeof s, "%u:%02u", secs / 60, secs % 60);+ gfx_text(FONT_MENU, 222, 68, s, c, 255);+ RoomInfo ri;+ room_info(g_room, &ri);+ gfx_text(FONT_MENU, 68, 88, str(ri.name), c, 255);+ if (save_state) gfx_text(FONT_MENU, 84, 116, "File saved.", c, 255);+ else {+ gfx_text(FONT_MENU, 84, 116, "Save", C_WHITE, 255);+ gfx_text(FONT_MENU, 180, 116, "Return", C_WHITE, 255);+ gfx_sprite(SPR_HEART, 0, sel ? 170 : 74, 122, 0, 255);+ }+}++static void save_input(uint32_t pressed) {+ if (save_state) {+ if (pressed & (K_Z | K_X)) g_mode = MODE_WORLD;+ return;+ }+ if (pressed & (K_LEFT | K_RIGHT)) sel ^= 1;+ if (pressed & K_X) g_mode = MODE_WORLD;+ if (pressed & K_Z) {+ if (sel) g_mode = MODE_WORLD;+ else {+ sv.room = (int16_t)g_room, sv.x = pl.x, sv.y = pl.y;+ save_write();+ save_state = 1;+ }+ }+}++/* ---------------------------------------------------------------- GAME OVER */+void menu_gameover_open(void) {+ g_mode = MODE_GAMEOVER;+ st = S_GAMEOVER, t = 0;+ snprintf(buf, sizeof buf, "You cannot give\nup just yet...|%s!\nStay determined...", sv.name);+ writer_start(&w, buf, 90, 160, FONT_MAIN);+ w.speed = 2;+}++/* ---------------------------------------------------------------- the frame */+void menu_update(uint32_t keys, uint32_t pressed) {+ (void)keys;+ t++;+ switch (g_mode) {+ case MODE_INTRO:+ if (pressed & K_Z) {+ title_open();+ return;+ }+ writer_update(&w, 0);+ if (t > (slide == 10 ? 300 : slide >= 7 ? 90 : 180)) {+ if (slide + 1 < INTRO_SLIDES) intro_slide(slide + 1);+ else title_open();+ }+ break;+ case MODE_TITLE:+ if (st == S_TITLE) {+ if (pressed & K_Z && t > 15) {+ if (save_exists()) st = S_CONTINUE, sel = 0;+ else st = S_INSTR, sel = 0;+ t = 0;+ }+ } else if (st == S_INSTR) {+ if (pressed & (K_UP | K_DOWN)) sel ^= 1;+ if (pressed & K_Z && t > 5) {+ if (sel == 0) st = S_NAME, letter = 0, name_len = 0, t = 0;+ }+ } else if (st == S_NAME) {+ name_input(pressed);+ } else if (st == S_CONFIRM) {+ if (!confirm_allowed) {+ if (pressed & (K_Z | K_X)) st = S_NAME;+ break;+ }+ if (pressed & (K_LEFT | K_RIGHT)) sel ^= 1;+ if (pressed & K_X) st = S_NAME;+ if (pressed & K_Z) {+ if (sel == 0) st = S_NAME;+ else {+ if (same_lower(name, "frisk")) set_flag(F_HARDMODE, 1);+ game_new(name);+ st = S_FADEIN;+ }+ }+ } else if (st == S_CONTINUE) {+ if (pressed & (K_LEFT | K_RIGHT)) sel ^= 1;+ if (pressed & K_Z) {+ if (sel == 0) save_load();+ else st = S_NAME, letter = 0, name_len = 0;+ }+ }+ break;+ case MODE_MENU:+ c_menu_input(pressed);+ break;+ case MODE_SAVE:+ save_input(pressed);+ break;+ case MODE_GAMEOVER:+ if (t > 40 && !writer_update(&w, pressed)) {+ if (!save_load()) game_new(sv.name);+ }+ break;+ }+}++void menu_draw(void) {+ char s[64];+ switch (g_mode) {+ case MODE_INTRO:+ if (slide < 10) gfx_sprite(SPR_INTROIMAGE, slide, 0, 0, 0, (uint8_t)min_i(255, t * 16));+ else gfx_sprite(SPR_INTROLAST, 0, 60, 28 - min_i(t / 2, 110), 0, 255);+ writer_draw(&w, C_WHITE);+ break;+ case MODE_TITLE:+ if (st == S_TITLE) {+ gfx_sprite(SPR_UNDERTALETITLE, 0, 0, 0, 0, 255);+ if (t > 30) gfx_text(FONT_SMALL, 128, 184, "[PRESS OK]", rgb565(0x808080), 255);+ } else if (st == S_INSTR) {+ static const char *const lines[] = {" --- Instruction --- ", "[OK] - Confirm", "[Back] - Cancel",+ "[toolbox] - Menu (In-game)", "[Home] - Quit", "When HP is 0, you lose."};+ for (int i = 0; i < 6; i++) gfx_text(FONT_MAIN, 50, 30 + i * 18 + (i > 0) * 4, lines[i], rgb565(0xC0C0C0), 255);+ gfx_text(FONT_MAIN, 74, 160, "Begin Game", sel == 0 ? C_YELLOW : C_WHITE, 255);+ gfx_text(FONT_MAIN, 74, 180, "Settings", sel == 1 ? C_YELLOW : rgb565(0x808080), 255);+ } else if (st == S_NAME) {+ gfx_text(FONT_MAIN, 76, 18, "Name the fallen human.", C_WHITE, 255);+ memcpy(s, name, (size_t)name_len), s[name_len] = 0;+ gfx_text(FONT_MAIN, 128, 40, s, C_WHITE, 255);+ for (int i = 0; i < LETTERS; i++) {+ int x, y;+ letter_xy(i, &x, &y);+ char c[2] = {letter_at(i), 0};+ /* (the game's letters tremble; at half its size, one at a time) */+ bool shakes = (g_frame / 3 + (uint32_t)i * 7) % 23 == 0;+ gfx_text_ex(FONT_MAIN, x, y, c, 1, i == letter ? C_YELLOW : C_WHITE, shakes, (uint32_t)(i * 77 + g_frame / 3), 0);+ }+ static const char *const opts[] = {"Quit", "Backspace", "Done"};+ static const int16_t ox[] = {60, 122, 220};+ for (int i = 0; i < 3; i++) gfx_text(FONT_MAIN, ox[i], 200, opts[i], letter == 52 + i ? C_YELLOW : C_WHITE, 255);+ } else if (st == S_CONFIRM) {+ gfx_text(FONT_MAIN, 76, 18, confirm_text, C_WHITE, 255);+ memcpy(s, name, (size_t)name_len), s[name_len] = 0;+ /* the name grows */+ int k = min_i(t, 60);+ gfx_text_ex(FONT_MAIN, 128 - k / 2, 60 + k / 2, s, name_len, C_WHITE, 1, g_frame / 2, k / 10);+ if (confirm_allowed) {+ gfx_text(FONT_MAIN, 60, 200, "No", sel == 0 ? C_YELLOW : C_WHITE, 255);+ gfx_text(FONT_MAIN, 228, 200, "Yes", sel == 1 ? C_YELLOW : C_WHITE, 255);+ } else+ gfx_text(FONT_MAIN, 60, 200, "Go back", C_YELLOW, 255);+ } else if (st == S_CONTINUE) {+ Save saved;+ if (save_peek(&saved)) {+ gfx_text(FONT_MENU, 70, 60, saved.name, C_WHITE, 255);+ snprintf(s, sizeof s, "LV %d", saved.lv);+ gfx_text(FONT_MENU, 140, 60, s, C_WHITE, 255);+ unsigned secs = saved.time / 30;+ snprintf(s, sizeof s, "%u:%02u", secs / 60, secs % 60);+ gfx_text(FONT_MENU, 222, 60, s, C_WHITE, 255);+ RoomInfo ri;+ room_info(saved.room, &ri);+ gfx_text(FONT_MENU, 70, 80, str(ri.name), C_WHITE, 255);+ }+ gfx_text(FONT_MENU, 84, 110, "Continue", sel == 0 ? C_YELLOW : C_WHITE, 255);+ gfx_text(FONT_MENU, 190, 110, "Reset", sel == 1 ? C_YELLOW : C_WHITE, 255);+ }+ break;+ case MODE_MENU:+ draw_c_menu();+ break;+ case MODE_SAVE:+ draw_save();+ break;+ case MODE_GAMEOVER:+ gfx_sprite(SPR_GAMEOVERBG, 0, 0, 0, 0, (uint8_t)min_i(255, t * 6));+ if (t > 40) writer_draw(&w, C_WHITE);+ break;+ }+}diff --git a/games/undertale/src/menu.h b/games/undertale/src/menu.hindex 18fcaa16..bc4589ab 100644--- a/games/undertale/src/menu.h+++ b/games/undertale/src/menu.h@@ -5,6 +5,7 @@void menu_title_open(void);void menu_open(void);void menu_save_open(void);+void menu_gameover_open(void);void menu_update(uint32_t keys, uint32_t pressed);void menu_draw(void);void game_new(const char *name);diff --git a/games/undertale/src/monsters.c b/games/undertale/src/monsters.cnew file mode 100644index 00000000..153e35dd--- /dev/null+++ b/games/undertale/src/monsters.c@@ -0,0 +1,696 @@+/* The Ruins' monsters: stats, texts and ACTs (from the Undertale Wiki's records of the game), their formations,+ * and their attacks, as the wiki describes them. */+#include <stdio.h>+#include <string.h>+#include "battle.h"+#include "game.h"+#include "gfx.h"+#include "monsters.h"+#include "res.h"++/* ---------------------------------------------------------------- the ACTs */+static void act_dummy(Battle *b, Monster *m, int act, char *t, int cap) {+ (void)b;+ snprintf(t, (size_t)cap, "* You talk to the DUMMY.\n* ...|* It doesn't seem much for\n conversation.|* TORIEL seems happy with you.");+ m->gone = true, m->fade = 1;+ set_flag(F_DUMMY, 1);+}+static const char *flavor_dummy(Battle *b, Monster *m) {+ if (b->turn >= 4) return "* Dummy tires of your aimless\n shenanigans.";+ return NULL;+}++static void act_froggit(Battle *b, Monster *m, int act, char *t, int cap) {+ (void)b;+ if (act == 0) {+ snprintf(t, (size_t)cap, "* Froggit didn't understand\n what you said, but was\n flattered anyway.");+ m->state = 1;+ } else {+ snprintf(t, (size_t)cap, "* Froggit didn't understand\n what you said, but was scared\n anyway.");+ m->state = 2;+ }+ m->spare = true;+}+static const char *talk_froggit(Battle *b, Monster *m) {+ if (m->state == 1) { m->state = 3; return "(Blushes\ndeeply.)\nRibbit.."; }+ if (m->state == 2) { m->state = 3; return "Shiver,\nshiver."; }+ static const char *const q[] = {"Ribbit,\nribbit.", "Croak,\ncroak.", "Hop,\nhop.", "Meow."};+ return q[rnd_n(4)];+}+static int attack_froggit(Battle *b, Monster *m) { return rnd_n(10) == 0 ? ATK_FROG_RAIN : rnd_n(2) ? ATK_FROG_FLIES : ATK_FROG_JUMP; }++static void act_whimsun(Battle *b, Monster *m, int act, char *t, int cap) {+ if (act == 0) {+ snprintf(t, (size_t)cap, "* Halfway through your first\n word, Whimsun bursts into\n tears and runs away.");+ m->gone = true, m->fade = 1;+ m->spare_gold = m->state ? 2 : 0;+ } else {+ snprintf(t, (size_t)cap, "* You raise your arms and\n wiggle your fingers.\n* Whimsun freaks out!");+ m->state = 1;+ }+ (void)b;+}+static const char *talk_whimsun(Battle *b, Monster *m) {+ if (m->state == 1) { m->state = 2; return "I can't\nhandle\nthis..."; }+ static const char *const q[] = {"I'm sorry...", "*sniff sniff*", "I have no\nchoice..", "Forgive me..."};+ return q[rnd_n(4)];+}+static const char *flavor_whimsun(Battle *b, Monster *m) { return m->state ? "* Whimsun is hyperventilating." : NULL; }+static int attack_whimsun(Battle *b, Monster *m) { return rnd_n(2) ? ATK_WHIM_RING : ATK_WHIM_COLS; }++static void act_moldsmal(Battle *b, Monster *m, int act, char *t, int cap) {+ if (act == 0) snprintf(t, (size_t)cap, "* You lie immobile with\n Moldsmal.\n* You feel like you understand\n the world a little better.");+ else snprintf(t, (size_t)cap, "* You wiggle your hips.\n* Moldsmal wiggles back.\n* What a meaningful conversation!");+ m->spare_gold = 1;+ (void)b;+}+static int attack_moldsmal(Battle *b, Monster *m) { return rnd_n(2) ? ATK_MOLD_ZIG : ATK_MOLD_BURST; }++static void act_loox(Battle *b, Monster *m, int act, char *t, int cap) {+ if (act == 0) {+ snprintf(t, (size_t)cap, "* You don't pick on Loox.");+ if (m->exp > 4) m->exp = (int16_t)(m->exp - 2);+ m->spare = true, m->state = 2;+ } else {+ snprintf(t, (size_t)cap, "* You do something mean...\n* You pick on Loox. Loox is\n furious!");+ if (m->exp < 20 && !m->spare) m->exp = (int16_t)(m->exp + 5);+ m->state = 1;+ }+ (void)b;+}+static const char *talk_loox(Battle *b, Monster *m) {+ if (m->state == 1) { m->state = 0; m->state2 = 1; return "You rude\nlittle\nsnipe!"; }+ if (m->state == 2) { m->state = 0; m->state2 = 0; return "Finally\nsomeone\ngets it."; }+ if (b->turn <= 1) return "Please don't\npick on me.";+ static const char *const q[] = {"Quit staring\nat me.", "I've got my\neye on you.", "How about a\nstaring\ncontest?", "Don't point\nthat at me.", "What an\neyesore."};+ return q[rnd_n(5)];+}+static int attack_loox(Battle *b, Monster *m) { return rnd_n(2) ? ATK_LOOX_BOUNCE : ATK_LOOX_WORM; }++static void act_vegetoid(Battle *b, Monster *m, int act, char *t, int cap) {+ if (act == 0) {+ snprintf(t, (size_t)cap, "* You try to talk with\n Vegetoid, but it just smiles.");+ m->state = 3;+ } else if (act == 1) {+ if (m->spare || m->hp < m->maxhp / 2) {+ snprintf(t, (size_t)cap, "* You took a bite out of\n Vegetoid.\n* You recovered 5 HP!");+ sv.hp = (int16_t)min_i(sv.maxhp, sv.hp + 5);+ m->gone = true, m->fade = 1;+ } else+ snprintf(t, (size_t)cap, "* You tried to eat Vegetoid,\n but it wasn't weakened\n enough.");+ } else {+ snprintf(t, (size_t)cap, "* You pat your stomach.\n* Vegetoid offers a healthy\n meal.");+ m->state = 1;+ }+ (void)b;+}+static const char *talk_vegetoid(Battle *b, Monster *m) {+ if (m->state == 3) { m->state = 0; return "Plants Can't\nTalk Dummy"; }+ if (m->state == 1) return "Eat Your\nGreens";+ if (m->state == 2) return "Ate Your\nGreens";+ static const char *const q[] = {"Farmed\nLocally,\nVery Locally", "Part Of A\nComplete\nBreakfast", "Fresh\nMorning\nTaste", "Contains\nVitamin A"};+ return q[rnd_n(4)];+}+static int attack_vegetoid(Battle *b, Monster *m) { return rnd_n(2) ? ATK_VEG_FALL : ATK_VEG_CARROT; }++static void act_migosp(Battle *b, Monster *m, int act, char *t, int cap) {+ if (count_alive() == 1) {+ snprintf(t, (size_t)cap, "* You talk to Migosp.\n* It doesn't seem to care about\n anything.");+ m->state = 1;+ } else+ snprintf(t, (size_t)cap, "* You talk to Migosp.\n* It doesn't seem to want to\n listen.");+ (void)b;+}+static const char *talk_migosp(Battle *b, Monster *m) {+ if (count_alive() == 1) {+ m->spare = true;+ if (m->state == 1) { m->state = 0; return "Hiya~"; }+ static const char *const q[] = {"Bein' me is\nthe best!", "La la~ Just\nbe yourself~", "Nothin' like\nalone time!", "Mmm, cha\ncha cha!", "Swing your\narms, baby"};+ return q[rnd_n(5)];+ }+ if (m->state == 1) { m->state = 0; return "I DON'T\nCARE."; }+ static const char *const q[] = {"FILTHY\nSINGLE\nMINDER..", "OBEY THE\nOVERMIND ..", "LEGION! WE\nARE LEGION", "HEED THE\nSWARM", "IN UNISON,\nNOW"};+ return q[rnd_n(5)];+}+static const char *flavor_migosp(Battle *b, Monster *m) {+ if (count_alive() == 1) return "* Migosp doesn't have a care\n in the world.";+ return NULL;+}+static int attack_migosp(Battle *b, Monster *m) {+ if (count_alive() == 1) return ATK_MIGOSP_DANCE;+ return rnd_n(8) == 0 ? ATK_FROG_FLIES : ATK_MIGOSP_ROACH;+}++/* ---------------------------------------------------------------- Napstablook */+static void act_napsta(Battle *b, Monster *m, int act, char *t, int cap) {+ /* state: cheers; state2: 1 after the hat, 2 morose (turns of it in b->state[1]) */+ if (act == 2) { /* Cheer */+ if (m->state2 == 2) {+ snprintf(t, (size_t)cap, "* You try to console\n Napstablook...");+ b->state[1]++;+ if (b->state[1] >= 8) m->state2 = 0, m->state = 0;+ return;+ }+ m->state++;+ if (m->state == 1) snprintf(t, (size_t)cap, "* You gave Napstablook a\n patient smile.");+ else if (m->state == 2) snprintf(t, (size_t)cap, "* You told Napstablook a\n little joke.");+ else if (m->state == 3) snprintf(t, (size_t)cap, "* Napstablook wants to show\n you something.");+ else {+ snprintf(t, (size_t)cap, "* ...");+ b->state[2] = 1; /* the end: Napstablook goes */+ }+ } else if (act == 0) { /* Flirt */+ if (m->state >= 3) {+ snprintf(t, (size_t)cap, "* You flirt with Napstablook.");+ b->state[2] = 2;+ } else {+ snprintf(t, (size_t)cap, "* You try your best pickup\n line.");+ m->state2 = 2, b->state[1] = 0;+ }+ } else { /* Threat */+ snprintf(t, (size_t)cap, "* You give Napstablook a\n cruel look.");+ m->state2 = 2, b->state[1] = 0, b->state[3] = m->state >= 3;+ m->state = 0;+ }+}+static const char *talk_napsta(Battle *b, Monster *m) {+ if (b->state[2] == 3)+ return "umm... you do\nknow you cant\nkill ghosts,\nright?|we're sorta\nincorporeal\nand all|i was just\nlowering my\nhp because i\ndidnt want to\nbe rude|sorry... i\njust made\nthis more\nawkward...|pretend you\nbeat me...|oooooooooo";+ if (b->state[2] == 1) return "oh gee...";+ if (b->state[2] == 2) return "oh no...";+ if (b->state[3]) { b->state[3] = 0; return "i knew it..."; }+ if (m->acts[0] && m->state2 == 2 && b->state[1] == 0 && b->state[4] != 1) { b->state[4] = 1; return "i'd just\nweigh you\ndown."; }+ if (m->acts[1] && m->state2 == 2 && b->state[1] == 0 && b->state[4] != 2) { b->state[4] = 2; return "go ahead,\ndo it."; }+ if (m->state == 1 && b->state[5] != 1) { b->state[5] = 1; return "heh..."; }+ if (m->state == 2 && b->state[5] != 2) { b->state[5] = 2; return "heh heh..."; }+ if (m->state == 3 && b->state[5] != 3) { b->state[5] = 3; return "let me\ntry...|i call it\n\"dapper\nblook\"|do you\nlike it..."; }+ static const char *const q[] = {"nnnnnnggghhh.", "just pluggin\nalong...", "i'm fine,\nthanks."};+ return q[rnd_n(3)];+}+static const char *flavor_napsta(Battle *b, Monster *m) {+ if (m->state2 == 2) return NULL;+ if (m->state == 1) return "* Napstablook looks just a\n little bit better.";+ if (m->state == 2) return "* Cheering seems to have\n improved Napstablook's mood\n again.";+ if (m->state >= 3) return "* Napstablook eagerly awaits\n your response.";+ return NULL;+}+static int attack_napsta(Battle *b, Monster *m) {+ if (b->state[2]) return -1;+ if (b->turn == 2) return ATK_NAPSTA_TEXT;+ if (m->state == 3 && b->state[5] == 3 && !b->state[6]) { b->state[6] = 1; return ATK_NAPSTA_HAT; }+ if (m->state >= 3) return ATK_IDLE;+ if (m->state2 == 2) return b->state[1] >= 6 ? ATK_NAPSTA_TEARS : ATK_NAPSTA_FLOOD;+ return rnd_n(2) ? ATK_NAPSTA_TEARS : ATK_NAPSTA_WIGGLE;+}+static bool ends_napsta(Battle *b, Monster *m) {+ if (b->state[2] == 3) m->killed = false, m->exp = 0;+ if (b->state[2]) set_flag(F_NAPSTA, b->state[2] == 3 ? 2 : 1);+ return b->state[2] != 0;+}+static bool hit_napsta(Battle *b, Monster *m, int dmg) {+ m->hp = (int16_t)(m->hp - dmg);+ m->hurt = 30;+ if (m->hp <= 0) {+ m->hp = 0;+ b->state[2] = 3; /* "you cant kill ghosts": then gone, no EXP */+ }+ return true;+}++/* ---------------------------------------------------------------- the monsters */+static const MonDef defs[MON_COUNT] = {+ [MON_DUMMY] = {+ .name = "Dummy", .hp = 15, .at = 0, .df = -5, .exp = 0, .gold = 0, .spr_idle = SPR_DUMMY_B, .spr_hurt = SPR_DUMMY_B,+ .check = "A cotton heart and a button\n eye\n* You are the apple of my eye",+ .acts = {"Talk"}, .nacts = 1, .neutral = {"* Dummy looks like it's going\n to fall over.", "* Dummy stands around\n absentmindedly."},+ .quotes = {"....."}, .act = act_dummy, .flavor = flavor_dummy, .flags = MF_SPARE_START},+ [MON_FROGGIT_FIRST] = {+ .name = "Froggit", .hp = 20, .at = 4, .df = 1, .exp = 10, .gold = 20, .spr_idle = SPR_FROGGIT_B, .spr_hurt = SPR_FROGGIT_HURT,+ .idle_speed = 8, .check = "Life is difficult for this\n enemy.", .acts = {"Compliment", "Threaten"}, .nacts = 2,+ .neutral = {"* Froggit doesn't seem to know\n why it's here.", "* Froggit hops to and fro.",+ "* The battlefield is filled\n with the smell of mustard\n seed.", "* You are intimidated by\n Froggit's raw strength.\n* Only kidding."},+ .low_hp = "* Froggit is trying to run\n away.", .spareable = "* Froggit seems reluctant to\n fight you.",+ .act = act_froggit, .talk = talk_froggit, .attack = attack_froggit},+ [MON_FROGGIT] = {+ .name = "Froggit", .hp = 30, .at = 4, .df = 4, .exp = 3, .gold = 2, .spr_idle = SPR_FROGGIT_B, .spr_hurt = SPR_FROGGIT_HURT,+ .idle_speed = 8, .check = "Life is difficult for this\n enemy.", .acts = {"Compliment", "Threaten"}, .nacts = 2,+ .neutral = {"* Froggit doesn't seem to know\n why it's here.", "* Froggit hops to and fro.",+ "* The battlefield is filled\n with the smell of mustard\n seed.", "* You are intimidated by\n Froggit's raw strength.\n* Only kidding."},+ .low_hp = "* Froggit is trying to run\n away.", .spareable = "* Froggit seems reluctant to\n fight you.",+ .act = act_froggit, .talk = talk_froggit, .attack = attack_froggit},+ [MON_WHIMSUN] = {+ .name = "Whimsun", .hp = 10, .at = 4, .df = 0, .exp = 2, .gold = 2, .spr_idle = SPR_WHIMSUN_B, .spr_hurt = SPR_WHIMSUN_HURT,+ .idle_speed = 4, .dy = -10, .check = "This monster is too sensitive\n to fight...", .acts = {"Console", "Terrorize"}, .nacts = 2,+ .neutral = {"* Whimsun continues to mutter\n apologies.", "* Whimsun avoids eye contact.", "* Whimsun is fluttering.",+ "* It's starting to smell like\n lavender and mothballs."},+ .flags = MF_ONE_HIT | MF_SPARE_START, .act = act_whimsun, .talk = talk_whimsun, .attack = attack_whimsun, .flavor = flavor_whimsun},+ [MON_MOLDSMAL] = {+ .name = "Moldsmal", .hp = 50, .at = 4, .df = 0, .exp = 3, .gold = 3, .spr_idle = SPR_MOLDSMAL_B, .spr_hurt = SPR_MOLDSMAL_B,+ .idle_speed = 6, .check = "Stereotypical: Curvaceously\n attractive, but no brains...", .acts = {"Imitate", "Flirt"}, .nacts = 2,+ .neutral = {"* Moldsmal waits pensively.", "* Moldsmal burbles quietly.", "* Moldsmal is ruminating.",+ "* The sound of slurping fills\n the room.", "* Moldsmal hasn't brushed its\n teeth."},+ .quotes = {"Squorch...", "Burble\nburb...", "*Slime\nsounds*", "*Sexy\nwiggle*"},+ .flags = MF_SPARE_START, .act = act_moldsmal, .attack = attack_moldsmal},+ [MON_LOOX] = {+ .name = "Loox", .hp = 50, .at = 5, .df = 4, .exp = 7, .gold = 5, .spr_idle = SPR_LOOX_B, .spr_hurt = SPR_LOOX_HURT,+ .idle_speed = 6, .check = "Don't pick on him.\n* Family name: Eyewalker", .acts = {"Don't Pick On", "Pick On"}, .nacts = 2,+ .neutral = {"* Loox is staring right\n through you.", "* Loox is gazing at you.", "* Loox gnashes its teeth.",+ "* Smells like eyedrops."},+ .low_hp = "* Loox is watering.", .act = act_loox, .talk = talk_loox, .attack = attack_loox},+ [MON_VEGETOID] = {+ .name = "Vegetoid", .hp = 72, .at = 5, .df = 0, .exp = 6, .gold = 1, .spr_idle = SPR_VEGETOID_B, .spr_hurt = SPR_VEGETOID_HURT,+ .idle_speed = 6, .check = "Serving Size: 1 Monster\n* Not monitored by the USDA", .acts = {"Talk", "Devour", "Dinner"}, .nacts = 3,+ .neutral = {"* Vegetoid cackles softly.", "* Vegetoid's here for your\n health.", "* Vegetoid gave a mysterious\n smile.",+ "* It smells like steamed\n carrots and peas."},+ .low_hp = "* Vegetoid seems kind of\n bruised.", .act = act_vegetoid, .talk = talk_vegetoid, .attack = attack_vegetoid},+ [MON_MIGOSP] = {+ .name = "Migosp", .hp = 40, .at = 5, .df = 4, .exp = 5, .gold = 2, .spr_idle = SPR_MIGOSP_B, .spr_hurt = SPR_MIGOSP_HURT,+ .idle_speed = 4, .check = "It seems evil, but it's just\n with the wrong crowd...", .acts = {"Talk"}, .nacts = 1,+ .neutral = {"* Migosp skitters around.", "* Migosp is knitting its brow.", "* Migosp is whispering to the\n others.",+ "* It's starting to smell like a\n roach motel."},+ .low_hp = "* Migosp refuses to give up.", .act = act_migosp, .talk = talk_migosp, .attack = attack_migosp,+ .flavor = flavor_migosp},+ [MON_NAPSTABLOOK] = {+ .name = "Napstablook", .hp = 88, .at = 3, .df = 4, .exp = 0, .gold = 0, .spr_idle = SPR_NAPSTABLOOK_B, .spr_hurt = SPR_NAPSTABLOOK_B,+ .idle_speed = 6, .dy = -16, .check = "This monster doesn't seem to\n have a sense of humor...", .acts = {"Flirt", "Threat", "Cheer"}, .nacts = 3,+ .neutral = {"* Napstablook is wishing they\n weren't here.", "* Napstablook is staring into\n the distance.",+ "* Napstablook is pretending to\n sleep.", "* The faint odor of ectoplasm\n permeates the vicinity."},+ .flags = MF_BOSS, .act = act_napsta, .talk = talk_napsta, .attack = attack_napsta, .flavor = flavor_napsta,+ .on_hit = hit_napsta, .ends = ends_napsta},+};++extern const MonDef toriel_def;+const MonDef *mon_def(int id) { return id == MON_TORIEL ? &toriel_def : &defs[id]; }++/* ---------------------------------------------------------------- formations */+typedef struct {+ uint8_t mon[MAX_MON];+ uint8_t n;+ const char *text;+} Form;+static const Form forms[FORM_COUNT] = {+ [FORM_DUMMY] = {{MON_DUMMY}, 1, "* You encountered the Dummy."},+ [FORM_FROGGIT_FIRST] = {{MON_FROGGIT_FIRST}, 1, "* Froggit attacks you!"},+ [FORM_FROGGIT] = {{MON_FROGGIT}, 1, "* Froggit hopped close!"},+ [FORM_WHIMSUN] = {{MON_WHIMSUN}, 1, "* Whimsun approached meekly!"},+ [FORM_FROGGIT_WHIMSUN] = {{MON_FROGGIT, MON_WHIMSUN}, 2, "* Froggit and Whimsun drew\n near!"},+ [FORM_FROGGIT2] = {{MON_FROGGIT, MON_FROGGIT}, 2, "* A pair of Froggits hop\n towards you."},+ [FORM_MOLDSMAL] = {{MON_MOLDSMAL}, 1, "* Moldsmal blocks the way!"},+ [FORM_MOLDSMAL2] = {{MON_MOLDSMAL, MON_MOLDSMAL}, 2, "* Moldsmal and Moldsmal block\n the way."},+ [FORM_MOLDSMAL3] = {{MON_MOLDSMAL, MON_MOLDSMAL, MON_MOLDSMAL}, 3, "* You're surrounded by\n Moldsmal!"},+ [FORM_MOLDSMAL_MIGOSP] = {{MON_MIGOSP, MON_MOLDSMAL}, 2, "* Migosp crawled up close!"},+ [FORM_LOOX] = {{MON_LOOX}, 1, "* Loox drew near!"},+ [FORM_LOOX2] = {{MON_LOOX, MON_LOOX}, 2, "* A pair of Loox decided to\n pick on you!"},+ [FORM_VEGETOID] = {{MON_VEGETOID}, 1, "* Vegetoid came out of the\n earth!"},+ [FORM_VEGETOID2] = {{MON_VEGETOID, MON_VEGETOID}, 2, "* A pair of Vegetoids came out\n of the ground!"},+ [FORM_VEGETOID_MIGOSP] = {{MON_MIGOSP, MON_VEGETOID}, 2, "* Vegetoid came out of the\n earth!"},+ [FORM_LOOX_VEG_MIGOSP] = {{MON_VEGETOID, MON_LOOX, MON_MIGOSP}, 3, "* Loox and co. decided to pick\n on you!"},+ [FORM_NAPSTABLOOK] = {{MON_NAPSTABLOOK}, 1, "* Here comes Napstablook."},+ [FORM_TORIEL] = {{MON_TORIEL}, 1, "* Toriel blocks the way!"},+ [FORM_FLOWEY] = {{MON_FLOWEY}, 1, ""},+};++int form_setup(int form, Battle *b) {+ const Form *f = &forms[form];+ static const int16_t xs[3][3] = {{160}, {108, 212}, {70, 160, 250}};+ b->nmon = f->n;+ for (int i = 0; i < f->n; i++) {+ Monster *m = &b->mon[i];+ memset(m, 0, sizeof *m);+ m->def = mon_def(f->mon[i]);+ m->x = (int16_t)(xs[f->n - 1][i] + m->def->dx), m->y = (int16_t)(120 + m->def->dy);+ }+ if (form == FORM_NAPSTABLOOK || form == FORM_TORIEL || form == FORM_FLOWEY) b->no_flee = form == FORM_FLOWEY;+ return 0;+}+const char *form_text(int form) { return forms[form].text; }++/* ---------------------------------------------------------------- attacks */+/* the board's inside, in pixels */+static int bx0(void) { return bt.x0 + 3; }+static int bx1(void) { return bt.x1 - 3; }+static int by0(void) { return bt.y0 + 3; }+static int by1(void) { return bt.y1 - 3; }+#define P(v) ((v) * FX)++static Bullet *shot(uint16_t spr, int x, int y, int vx, int vy) {+ Bullet *b = bullet_new(spr, P(x), P(y), vx, vy);+ if (b) b->owner = (uint8_t)bt.atk_owner;+ return b;+}++static int toward(int from, int to, int sp, int dist) { return dist ? (to - from) * sp / dist : 0; }+static int isqrt(int v) {+ int r = 0;+ while ((r + 1) * (r + 1) <= v) r++;+ return r;+}++static void atk_frog_flies(int t) {+ /* five flies come down one after another, stop, then dart at the SOUL */+ if (t < 0) return;+ if (t < 50 && t % 10 == 0) {+ Bullet *b = shot(SPR_FLYBULLET, bx0() + 6 + rnd_n(bx1() - bx0() - 12), by0() - 6, 0, P(3) / 2);+ if (b) b->kind = BK_SCRIPT, b->anim = 3, b->a = (int16_t)(by0() + 6 + rnd_n(20)), b->b = 0;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ if (b->b == 0 && b->y / FX >= b->a) b->vy = 0, b->b = 1, b->c = 0;+ else if (b->b == 1 && ++b->c >= 14) {+ int dx = bt.sx - b->x, dy = bt.sy - b->y, d = isqrt((dx / 4) * (dx / 4) + (dy / 4) * (dy / 4)) * 4;+ b->vx = toward(b->x, bt.sx, P(2), d), b->vy = toward(b->y, bt.sy, P(2), d);+ b->b = 2;+ }+ b->x += b->vx, b->y += b->vy;+ b->t++;+ if (b->t % 3 == 0) b->frame++;+ }+}++static void atk_frog_jump(int t) {+ /* a little frog hops from the bottom right to the other side, with random force */+ if (t < 0) return;+ if (t == 0) {+ Bullet *b = shot(SPR_SMALLFROGBULLET, bx1() - 8, by1() - 8, 0, 0);+ if (b) b->kind = BK_SCRIPT, b->a = 20, b->r = 7;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ if (b->a > 0) {+ b->frame = 0;+ if (--b->a == 0) b->vx = -(P(1) + rnd_n(P(3) / 2)), b->vy = -(P(2) + rnd_n(P(1)));+ continue;+ }+ b->frame = 1;+ b->vy += P(1) / 7;+ b->x += b->vx, b->y += b->vy;+ if (b->y >= P(by1() - 8)) {+ b->y = P(by1() - 8), b->vy = 0, b->vx = 0, b->a = 18;+ }+ }+}++static void atk_frog_rain(int t) {+ /* flies falling smoothly down the board, not aimed */+ if (t < 0) return;+ if (t < 100 && t % 9 == 0) {+ Bullet *b = shot(SPR_FLYBULLET, bx0() + 4 + rnd_n(bx1() - bx0() - 8), by0() - 6, 0, P(1));+ if (b) b->anim = 3;+ }+}++static void atk_whim_ring(int t) {+ /* a ring of moths around the board's middle, growing and shrinking */+ int n = 10;+ if (t < 0) return;+ if (t == 0)+ for (int k = 0; k < n; k++) {+ Bullet *b = shot(SPR_BUTTERFLYBLLT, board_cx(), board_cy(), 0, 0);+ if (b) b->kind = BK_SCRIPT, b->a = (int16_t)(k * 256 / n), b->anim = 4;+ }+ int r = 26 + gfx_sin(t * 2) * 14 / 1024;+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ int a = b->a + t;+ b->x = P(board_cx()) + r * gfx_cos(a) * FX / 1024, b->y = P(board_cy()) + r * gfx_sin(a) * FX / 1024;+ if (++b->t % 4 == 0) b->frame++;+ }+}++static void atk_whim_cols(int t) {+ /* two columns of moths rising on either side of the SOUL, following it */+ if (t < 0) return;+ if (t == 0) bt.as[0] = bt.sx;+ bt.as[0] += (bt.sx - bt.as[0]) / 12;+ if (t < 120 && t % 7 == 0)+ for (int s = -1; s <= 1; s += 2) {+ Bullet *b = shot(SPR_BUTTERFLYBLLT, 0, by1() + 6, 0, -P(3) / 2);+ if (b) b->kind = BK_SCRIPT, b->a = (int16_t)s, b->anim = 4, b->x = bt.as[0] + s * P(16);+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->x = bt.as[0] + b->a * P(16);+ b->y += b->vy;+ if (++b->t % 4 == 0) b->frame++;+ if (b->y < P(by0() - 10)) b->kind = BK_NONE;+ }+}++static void atk_mold_zig(int t) {+ /* pellets falling in a zigzag */+ if (t < 0) return;+ if (t < 110 && t % 10 == 0) {+ Bullet *b = shot(SPR_BULLETMD, bx0() + 8 + rnd_n(bx1() - bx0() - 16), by0() - 4, P(1), P(1));+ if (b) b->kind = BK_SCRIPT;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ if (++b->t % 14 == 0) b->vx = -b->vx;+ b->x += b->vx, b->y += b->vy;+ }+}++static void atk_mold_burst(int t) {+ /* pellets that fall a little way, then burst into rings of smaller ones */+ if (t < 0) return;+ if (t < 100 && t % 25 == 0) {+ Bullet *b = shot(SPR_BULLETMD, bx0() + 10 + rnd_n(bx1() - bx0() - 20), by0() - 4, 0, P(1));+ if (b) b->kind = BK_SCRIPT, b->a = (int16_t)(12 + rnd_n(16));+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->y += b->vy;+ if (++b->t >= b->a) {+ for (int k = 0; k < 8; k++) {+ Bullet *s = shot(SPR_BULLETSM, b->x / FX, b->y / FX, gfx_cos(k * 32) * P(1) / 1024, gfx_sin(k * 32) * P(1) / 1024);+ if (s) s->life = 60;+ }+ b->kind = BK_NONE;+ }+ }+}++static void atk_loox_bounce(int t) {+ /* spheres come in from the sides and bounce around the board, faster and faster */+ bool mad = bt.mon[bt.atk_owner].state2;+ if (t < 0) return;+ if (t < 40 && t % 8 == 0) {+ int s = (t / 8) % 2 ? 1 : -1;+ Bullet *b = shot(SPR_CIRCLEBULLETMD1, s < 0 ? bx0() - 4 : bx1() + 4, by0() + 8 + rnd_n(by1() - by0() - 16), -s * P(1), (rnd_n(2) ? 1 : -1) * P(1));+ if (b) b->kind = BK_SCRIPT, b->a = 0;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ if (++b->t % 20 == 0) b->vx += b->vx / (mad ? 4 : 8), b->vy += b->vy / (mad ? 4 : 8);+ b->x += b->vx, b->y += b->vy;+ if (b->t > 8) {+ if (b->x < P(bx0() + 3) || b->x > P(bx1() - 3)) b->vx = -b->vx;+ if (b->y < P(by0() + 3) || b->y > P(by1() - 3)) b->vy = -b->vy;+ }+ }+}++static void atk_loox_worm(int t) {+ /* worms of spheres wiggling across the board */+ bool mad = bt.mon[bt.atk_owner].state2;+ if (t < 0) return;+ int every = mad ? 16 : 24;+ if (t < 110 && t % every == 0) {+ int s = (t / every) % 2 ? 1 : -1, y = by0() + 10 + rnd_n(by1() - by0() - 20);+ for (int k = 0; k < 4; k++) {+ Bullet *b = shot(SPR_CIRCLEBULLETSM, (s < 0 ? bx0() - 4 - k * 6 : bx1() + 4 + k * 6), y, -s * (mad ? P(3) / 2 : P(1)), 0);+ if (b) b->kind = BK_SCRIPT, b->a = (int16_t)y, b->b = (int16_t)(k * 20);+ }+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->t++;+ b->x += b->vx;+ b->y = P(b->a) + gfx_sin(b->t * 6 + b->b) * P(6) / 1024;+ if ((b->vx > 0 && b->x > P(bx1() + 8)) || (b->vx < 0 && b->x < P(bx0() - 8))) b->kind = BK_NONE;+ }+}++static void atk_veg_fall(int t) {+ /* vegetables fall and bounce off the bottom; after Dinner, one of them is green */+ Monster *m = &bt.mon[bt.atk_owner];+ if (t < 0) return;+ if (t < 100 && t % 14 == 0) {+ Bullet *b = shot(SPR_VEGBULLET, bx0() + 8 + rnd_n(bx1() - bx0() - 16), by0() - 6, (rnd_n(2) ? 1 : -1) * rnd_n(P(1)), P(1));+ if (b) {+ b->kind = BK_SCRIPT, b->frame = (uint8_t)rnd_n(3), b->a = 0;+ if (m->state == 1 && t == 42) b->color = BC_GREEN;+ }+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->vy += P(1) / 12;+ b->x += b->vx, b->y += b->vy;+ if (b->y > P(by1() - 5) && b->vy > 0 && !b->a) b->vy = -b->vy * 2 / 3, b->a = 1;+ }+ if (m->state == 1 && t == 99) m->state = 2, m->spare = true;+}++static void atk_veg_carrot(int t) {+ /* carrots dropping straight down */+ Monster *m = &bt.mon[bt.atk_owner];+ if (t < 0) return;+ if (t < 110 && t % 11 == 0) {+ Bullet *b = shot(SPR_CARROTBULLET, bx0() + 6 + rnd_n(bx1() - bx0() - 12), by0() - 8, 0, P(2));+ if (b && m->state == 1 && t == 44) b->color = BC_GREEN;+ if (b) b->kind = BK_SCRIPT;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->y += b->vy;+ if (b->y > P(by1())) b->kind = BK_NONE;+ }+ if (m->state == 1 && t == 109) m->state = 2, m->spare = true;+}++static void atk_migosp_roach(int t) {+ /* roaches flying up the left and the right halves */+ if (t < 0) return;+ if (t < 110 && t % 8 == 0) {+ int left = (t / 8) % 2, w = (bx1() - bx0()) / 2 - 8;+ int x = left ? bx0() + 4 + rnd_n(w) : board_cx() + 4 + rnd_n(w);+ Bullet *b = shot(SPR_ROACHBULLET, x, by1() + 6, 0, -P(3) / 2);+ if (b) b->anim = 3;+ }+}++static void atk_migosp_dance(int t) {+ /* a tiny roach dancing at the bottom: touching it ends the turn */+ if (t < 0) return;+ if (t == 0) {+ Bullet *b = shot(SPR_ROACHDANCE, board_cx(), by1() - 6, P(1), 0);+ if (b) b->kind = BK_SCRIPT, b->anim = 5, b->flags = BF_HARMLESS;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->x += b->vx;+ if (b->x < P(bx0() + 6) || b->x > P(bx1() - 6)) b->vx = -b->vx;+ if (++b->t % 5 == 0) b->frame++;+ int dx = b->x / FX - soul_x(), dy = b->y / FX - soul_y();+ if (dx * dx + dy * dy < 64) bt.atk_t = bt.atk_len;+ }+}++static void atk_napsta_tears(int t, int every) {+ /* tears arching out of Napstablook's eyes, then falling straight */+ if (t < 0) return;+ if (t < 120 && t % every == 0) {+ int s = rnd_n(2) ? 1 : -1;+ Bullet *b = shot(SPR_TEARDROP, board_cx() + s * 10, by0() - 4, s * (P(1) / 2 + rnd_n(P(1))), -P(1));+ if (b) b->kind = BK_GRAVITY, b->ay = P(1) / 16, b->dmg = 3;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind == BK_GRAVITY && b->vy > P(1)) b->vx = b->vx * 7 / 8;+ }+}++static void atk_napsta_wiggle(int t) {+ /* long wiggly tears turning at the walls, falling once over the SOUL */+ if (t < 0) return;+ if (t < 100 && t % 20 == 0) {+ Bullet *b = shot(SPR_TEARDROP, bx0() + 6 + rnd_n(bx1() - bx0() - 12), by0() + 2, (rnd_n(2) ? 1 : -1) * P(3) / 2, 0);+ if (b) b->kind = BK_SCRIPT, b->dmg = 3, b->a = 0;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->t++;+ if (!b->a) {+ b->x += b->vx;+ b->y = b->y + (gfx_sin(b->t * 10) > 0 ? FX / 4 : -FX / 4);+ if (b->x < P(bx0() + 4) || b->x > P(bx1() - 4)) b->vx = -b->vx;+ if (abs_i(b->x - bt.sx) < P(3) && b->t > 15) b->a = 1, b->vy = P(2);+ } else+ b->y += b->vy;+ }+}++static void atk_napsta_text(int t) {+ /* REALLY NOT FEELIN UP TO IT RIGHT NOW. SORRY. in grey, harmless: drawn by attack_draw */+ (void)t;+}++static void atk_napsta_hat(int t) {+ /* the tears rise to make a top hat on Napstablook's head */+ if (t < 0) return;+ if (t < 60 && t % 4 == 0) {+ Bullet *b = shot(SPR_TEARDROP, bx0() + 6 + rnd_n(bx1() - bx0() - 12), by1() - 2, 0, -P(2));+ if (b) b->flags |= BF_HARMLESS, b->kind = BK_SCRIPT;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->y += b->vy;+ if (b->y < P(by0())) b->kind = BK_NONE;+ }+ if (t == 70) bt.mon[0].spr_override = SPR_NAPSTA_HAT;+}++static const int16_t lengths[ATK_COUNT] = {+ [ATK_FROG_FLIES] = 150, [ATK_FROG_JUMP] = 150, [ATK_FROG_RAIN] = 150, [ATK_WHIM_RING] = 150, [ATK_WHIM_COLS] = 150,+ [ATK_MOLD_ZIG] = 150, [ATK_MOLD_BURST] = 150, [ATK_LOOX_BOUNCE] = 160, [ATK_LOOX_WORM] = 150, [ATK_VEG_FALL] = 150,+ [ATK_VEG_CARROT] = 150, [ATK_MIGOSP_ROACH] = 150, [ATK_MIGOSP_DANCE] = 180, [ATK_NAPSTA_TEARS] = 150,+ [ATK_NAPSTA_WIGGLE] = 150, [ATK_NAPSTA_TEXT] = 120, [ATK_NAPSTA_HAT] = 100, [ATK_NAPSTA_FLOOD] = 150,+ [ATK_TOR_HANDS] = 170, [ATK_TOR_WAVES] = 180, [ATK_TOR_HELIX] = 180, [ATK_TOR_FALTER] = 150, [ATK_IDLE] = 60,+};+int attack_length(int atk) { return atk >= 0 && atk < ATK_COUNT && lengths[atk] ? lengths[atk] : 150; }++void toriel_attack(int atk, int t); /* toriel.c */++void attack_run(Battle *b, int atk, int t) {+ (void)b;+ switch (atk) {+ case ATK_FROG_FLIES: atk_frog_flies(t); break;+ case ATK_FROG_JUMP: atk_frog_jump(t); break;+ case ATK_FROG_RAIN: atk_frog_rain(t); break;+ case ATK_WHIM_RING: atk_whim_ring(t); break;+ case ATK_WHIM_COLS: atk_whim_cols(t); break;+ case ATK_MOLD_ZIG: atk_mold_zig(t); break;+ case ATK_MOLD_BURST: atk_mold_burst(t); break;+ case ATK_LOOX_BOUNCE: atk_loox_bounce(t); break;+ case ATK_LOOX_WORM: atk_loox_worm(t); break;+ case ATK_VEG_FALL: atk_veg_fall(t); break;+ case ATK_VEG_CARROT: atk_veg_carrot(t); break;+ case ATK_MIGOSP_ROACH: atk_migosp_roach(t); break;+ case ATK_MIGOSP_DANCE: atk_migosp_dance(t); break;+ case ATK_NAPSTA_TEARS: atk_napsta_tears(t, 9); break;+ case ATK_NAPSTA_FLOOD: atk_napsta_tears(t, 3); break;+ case ATK_NAPSTA_WIGGLE: atk_napsta_wiggle(t); break;+ case ATK_NAPSTA_TEXT: atk_napsta_text(t); break;+ case ATK_NAPSTA_HAT: atk_napsta_hat(t); break;+ case ATK_TOR_HANDS: case ATK_TOR_WAVES: case ATK_TOR_HELIX: case ATK_TOR_FALTER: toriel_attack(atk, t); break;+ default: break;+ }+}++void toriel_draw_attack(int atk, int t); /* toriel.c */++void attack_draw(Battle *b, int atk, int t) {+ (void)b;+ if (atk == ATK_NAPSTA_TEXT) {+ /* the grey words drift down: harmless */+ static const char *const words[] = {"REALLY", "NOT", "FEELIN", "UP", "TO", "IT", "RIGHT", "NOW.", "SORRY."};+ for (int k = 0; k < 9; k++) {+ int y = by0() - 10 + (t - k * 8) * 2 / 3;+ if (y > by0() - 12 && y < by1()) gfx_text(FONT_SMALL, bx0() + 4 + (k * 23) % (bx1() - bx0() - 30), y, words[k], rgb565(0x808080), 255);+ }+ }+ if (atk >= ATK_TOR_HANDS && atk <= ATK_TOR_FALTER) toriel_draw_attack(atk, t);+}diff --git a/games/undertale/src/monsters.h b/games/undertale/src/monsters.hnew file mode 100644index 00000000..68eaaa33--- /dev/null+++ b/games/undertale/src/monsters.h@@ -0,0 +1,23 @@+/* Undertale: the monsters, their formations and their attacks. */+#ifndef UT_MONSTERS_H+#define UT_MONSTERS_H++enum {+ MON_DUMMY, MON_FROGGIT_FIRST, MON_FROGGIT, MON_WHIMSUN, MON_MOLDSMAL, MON_LOOX, MON_VEGETOID, MON_MIGOSP,+ MON_NAPSTABLOOK, MON_TORIEL, MON_FLOWEY, MON_COUNT+};++/* formations: who is fought */+enum {+ FORM_DUMMY, FORM_FROGGIT_FIRST, FORM_FROGGIT, FORM_WHIMSUN, FORM_FROGGIT_WHIMSUN, FORM_FROGGIT2, FORM_MOLDSMAL,+ FORM_MOLDSMAL2, FORM_MOLDSMAL3, FORM_MOLDSMAL_MIGOSP, FORM_LOOX, FORM_LOOX2, FORM_VEGETOID, FORM_VEGETOID2,+ FORM_VEGETOID_MIGOSP, FORM_LOOX_VEG_MIGOSP, FORM_NAPSTABLOOK, FORM_TORIEL, FORM_FLOWEY, FORM_COUNT+};++enum {+ ATK_NONE, ATK_FROG_FLIES, ATK_FROG_JUMP, ATK_FROG_RAIN, ATK_WHIM_RING, ATK_WHIM_COLS, ATK_MOLD_ZIG, ATK_MOLD_BURST,+ ATK_LOOX_BOUNCE, ATK_LOOX_WORM, ATK_VEG_FALL, ATK_VEG_CARROT, ATK_MIGOSP_ROACH, ATK_MIGOSP_DANCE,+ ATK_NAPSTA_TEARS, ATK_NAPSTA_WIGGLE, ATK_NAPSTA_TEXT, ATK_NAPSTA_HAT, ATK_NAPSTA_FLOOD,+ ATK_TOR_HANDS, ATK_TOR_WAVES, ATK_TOR_HELIX, ATK_TOR_FALTER, ATK_IDLE, ATK_COUNT+};+#endifdiff --git a/games/undertale/src/save.c b/games/undertale/src/save.cindex 9355a6cc..46a9d62d 100644--- a/games/undertale/src/save.c+++ b/games/undertale/src/save.c@@ -1,8 +1,170 @@-/* Saves (save.h). */+/* Saves (save.h): the game's SAVE (undertale.sav), the moment kept when Home quits (utresume.sav, read once+ * on the next start), and a copy of both in a Python script: installing apps from the NumWorks website keeps+ * only Python scripts, so the saves come back from it when the files themselves are gone. Each is a+ * comment line "#>name:base64" (the way NumPlay and its big games keep theirs). */+#include <string.h>+#include "game.h"+#include "menu.h"+#include "plat.h"#include "save.h"-void save_restore_copy(void) {}-bool save_write(void) { return false; }-bool save_exists(void) { return false; }-bool save_load(void) { return false; }-void save_resume_write(void) {}-bool save_resume_load(void) { return false; }+#include "text.h"+#include "world.h"++#define SAVE_NAME "undertale.sav"+#define RESUME_NAME "utresume.sav"+#define COPY_NAME "undertale_saves.py"+#define MAGIC 0x31305455u /* "UT01" */++typedef struct {+ uint32_t magic;+ uint32_t size;+ Save s;+} SaveFile;++static const char *const kept[] = {SAVE_NAME, RESUME_NAME};+#define NKEPT (int)(sizeof kept / sizeof kept[0])+static const char copy_head[] =+ "# Undertale keeps a copy of your SAVE here, so that\n"+ "# reinstalling it doesn't erase it. If you delete this file,\n"+ "# the game writes it again: your SAVE stays either way.\n";+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";++static void copy_write(void) {+ uint32_t total = 1 + (sizeof copy_head - 1) + 1, len;+ int n = 0;+ for (int k = 0; k < NKEPT; k++)+ if (plat_load(kept[k], &len) && len) total += 2 + (uint32_t)strlen(kept[k]) + 1 + (len + 2) / 3 * 4 + 1, n++;+ plat_remove(COPY_NAME);+ if (!n) return;+ uint8_t *o = plat_reserve(COPY_NAME, total), *p = o;+ if (!o) return;+ *p++ = 0; /* (the script's status byte: not imported) */+ memcpy(p, copy_head, sizeof copy_head - 1), p += sizeof copy_head - 1;+ for (int k = 0; k < NKEPT; k++) {+ const uint8_t *d = plat_load(kept[k], &len);+ if (!d || !len) continue;+ uint32_t nl = (uint32_t)strlen(kept[k]);+ *p++ = '#', *p++ = '>';+ memcpy(p, kept[k], nl), p += nl;+ *p++ = ':';+ for (uint32_t i = 0; i < len; i += 3, p += 4) {+ /* (written out: GCC 15.2 for the calculator got a loop over the four wrong, see NumPlay's+ * launcher/src/storage.c) */+ uint32_t left = len - i, v = (uint32_t)d[i] << 16 | (left > 1 ? d[i + 1] << 8 : 0) | (left > 2 ? d[i + 2] : 0);+ p[0] = (uint8_t)b64[v >> 18 & 63];+ p[1] = (uint8_t)b64[v >> 12 & 63];+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';+ }+ *p++ = '\n';+ }+ *p++ = 0;+ plat_reserved(COPY_NAME, (uint32_t)(p - o));+}++static int b64_value(uint8_t c) {+ for (int i = 0; i < 64; i++)+ if ((uint8_t)b64[i] == c) return i;+ return -1;+}++static const uint8_t *copy_line(int k, uint32_t *len) {+ uint32_t n, nl = (uint32_t)strlen(kept[k]);+ const uint8_t *c = plat_load(COPY_NAME, &n);+ if (!c) return NULL;+ for (uint32_t i = 0; i + nl + 3 <= n; i++)+ if (c[i] == '#' && c[i + 1] == '>' && !memcmp(c + i + 2, kept[k], nl) && c[i + 2 + nl] == ':') {+ const uint8_t *l = c + i + 3 + nl;+ uint32_t m = 0;+ while (l + m < c + n && l[m] != '\n' && l[m]) m++;+ *len = m;+ return l;+ }+ return NULL;+}++void save_restore_copy(void) {+ for (int k = 0; k < NKEPT; k++) {+ uint32_t have, len;+ if (plat_load(kept[k], &have)) continue;+ const uint8_t *l = copy_line(k, &len);+ if (!l || !len || len % 4) continue;+ uint32_t size = len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=');+ uint8_t *out = plat_reserve(kept[k], size);+ if (!out) continue;+ l = copy_line(k, &len);+ bool ok = l != NULL && len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=') == size;+ uint32_t o = 0;+ for (uint32_t i = 0; ok && i < len; i += 4) {+ int v0 = l[i] == '=' ? 0 : b64_value(l[i]), v1 = l[i + 1] == '=' ? 0 : b64_value(l[i + 1]);+ int v2 = l[i + 2] == '=' ? 0 : b64_value(l[i + 2]), v3 = l[i + 3] == '=' ? 0 : b64_value(l[i + 3]);+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) ok = false;+ uint32_t wd = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);+ if (o < size) out[o++] = (uint8_t)(wd >> 16);+ if (o < size) out[o++] = (uint8_t)(wd >> 8);+ if (o < size) out[o++] = (uint8_t)wd;+ }+ if (ok && o == size) plat_reserved(kept[k], size);+ else plat_remove(kept[k]);+ }+}++static bool write_file(const char *name, const Save *s) {+ SaveFile f;+ memset(&f, 0, sizeof f);+ f.magic = MAGIC, f.size = sizeof(Save), f.s = *s;+ bool ok = plat_save(name, &f, sizeof f);+ copy_write();+ return ok;+}++static bool read_file(const char *name, Save *s) {+ uint32_t len;+ const uint8_t *d = plat_load(name, &len);+ if (!d || len != sizeof(SaveFile)) return false;+ SaveFile f;+ memcpy(&f, d, sizeof f);+ if (f.magic != MAGIC || f.size != sizeof(Save)) return false;+ *s = f.s;+ return true;+}++bool save_write(void) { return write_file(SAVE_NAME, &sv); }+bool save_exists(void) {+ Save s;+ return read_file(SAVE_NAME, &s);+}+bool save_peek(Save *s) { return read_file(SAVE_NAME, s); }++bool save_load(void) {+ Save s;+ if (!read_file(SAVE_NAME, &s)) return false;+ sv = s;+ memcpy(g_name, sv.name, sizeof g_name);+ sv.hp = sv.maxhp;+ g_mode = MODE_WORLD;+ world_enter(sv.room, sv.x, sv.y, DIR_DOWN);+ return true;+}++/* Home: the moment kept as it is, room and place; read back once */+void save_resume_write(void) {+ if (g_mode != MODE_WORLD && g_mode != MODE_MENU && g_mode != MODE_SAVE) {+ /* (in a battle: kept as it was before it) */+ }+ Save s = sv;+ s.room = (int16_t)g_room, s.x = pl.x, s.y = pl.y;+ write_file(RESUME_NAME, &s);+}++bool save_resume_load(void) {+ Save s;+ if (!read_file(RESUME_NAME, &s)) return false;+ plat_remove(RESUME_NAME);+ copy_write();+ sv = s;+ memcpy(g_name, sv.name, sizeof g_name);+ g_mode = MODE_WORLD;+ world_enter(sv.room, sv.x, sv.y, DIR_DOWN);+ return true;+}diff --git a/games/undertale/src/save.h b/games/undertale/src/save.hindex 0458f15e..fc9b6aec 100644--- a/games/undertale/src/save.h+++ b/games/undertale/src/save.h@@ -2,10 +2,12 @@#ifndef UT_SAVE_H#define UT_SAVE_H#include <stdbool.h>+#include "game.h"void save_restore_copy(void);bool save_write(void);bool save_exists(void);bool save_load(void);+bool save_peek(Save *s); /* the SAVE's facts, without loading it */void save_resume_write(void);bool save_resume_load(void);#endifdiff --git a/games/undertale/src/toriel.c b/games/undertale/src/toriel.cnew file mode 100644index 00000000..932acdd1--- /dev/null+++ b/games/undertale/src/toriel.c@@ -0,0 +1,252 @@+/* Toriel, the Ruins' boss: her fire, her spares, her words (from the Undertale Wiki's records of the fight). */+#include <stdio.h>+#include <string.h>+#include "battle.h"+#include "game.h"+#include "gfx.h"+#include "monsters.h"+#include "res.h"++/* b->state[0]: spares; [1]: talks; [2]: 1 she stopped fighting, 2 she gives up, 3 defeated, 4 betrayed */+static const char *const spare_lines[] = {+ ".....", "..... .....", "..... ..... .....", "...?", "What are\nyou doing?", "Attack or\nrun away!",+ "What are you\nproving this\nway?", "Fight me or\nleave!", "Stop it.", "Stop looking\nat me that\nway.", "Go away!", "...",+ "... ...", "I know you\nwant to go\nhome, but...", "But please...\ngo upstairs\nnow.", "I promise I\nwill take\ngood care of\nyou here.",+ "I know we do\nnot have\nmuch, but...", "We can have\na good life\nhere.", "Why are you\nmaking this\nso difficult?",+ "Please, go\nupstairs.", ".....", "Ha ha...", "Pathetic, is\nit not? I\ncannot save\neven a single\nchild.", "...",+ "No, I\nunderstand.|You would\njust be\nunhappy\ntrapped down\nhere.|The RUINS\nare very small\nonce you get\nused to them.|It would not\nbe right for\nyou to grow up\nin a place\nlike this.|My\nexpectations...\nMy\nloneliness...\nMy fear...|For you, my\nchild... I\nwill put them\naside."};+#define NSPARES (int)(sizeof spare_lines / sizeof spare_lines[0])++static void act_toriel(Battle *b, Monster *m, int act, char *t, int cap) {+ int n = b->state[1]++;+ if (n == 0) snprintf(t, (size_t)cap, "* You couldn't think of any\n conversation topics.");+ else if (n == 1) snprintf(t, (size_t)cap, "* You tried to think of\n something to say again,\n but...");+ else snprintf(t, (size_t)cap, "* Ironically, talking does not\n seem to be the solution to\n this situation.");+ (void)m;+}++static const char *talk_toriel(Battle *b, Monster *m) {+ if (b->state[2] == 3)+ return "Urgh...|You are\nstronger than\nI thought...|Listen to me,\nsmall one...|If you go\nbeyond this\ndoor,|Keep walking\nas far as you\ncan.|Eventually\nyou will reach\nan exit.|... ....|#RASGORE#X...\nDo not let\n#RASGORE#X\ntake your\nsoul.|His plan\ncannot be\nallowed to\nsucceed.|......|Be good,\nwon't you?|M y c h i l d .";+ if (b->state[2] == 4)+ return "You...|... at my\nmost\nvulnerable\nmoment...|To think I\nwas worried\nyou wouldn't\nfit in out\nthere...|Eheheheh!!!\nYou really are\nno different\nthan them!|Ha... ha...";+ if (b->last_action == 3) {+ int k = b->state[0] - 1;+ if (k >= 0 && k < NSPARES) return spare_lines[k];+ }+ return NULL;+}++static void spare_toriel(Battle *b, Monster *m) {+ (void)m;+ b->state[0]++;+ if (b->state[0] >= 12) b->no_flee = true;+}++static const char *flavor_toriel(Battle *b, Monster *m) {+ /* her DF falls when the SOUL is weak, and when she is */+ m->df = (int16_t)(sv.hp < 3 ? -15 : m->hp < 150 ? -140 : 1);+ if (b->state[0] >= 12) return "* ...";+ return NULL;+}++/* she uses one of her attacks; her fire falters when the SOUL is weak, and stops after enough spares */+static int attack_toriel(Battle *b, Monster *m) {+ if (b->state[2]) return -1;+ if (b->state[0] >= 12) return -1;+ if (sv.hp <= 2 || b->state[0] >= 11) return ATK_TOR_FALTER;+ int k = b->turn % 3;+ return k == 0 ? ATK_TOR_HANDS : k == 1 ? ATK_TOR_WAVES : ATK_TOR_HELIX;+}++static bool hit_toriel(Battle *b, Monster *m, int dmg) {+ /* hit once she stopped fighting: at once, betrayed */+ if (b->state[0] >= 12) {+ m->hp = 0, m->hurt = 30;+ b->state[2] = 4;+ return true;+ }+ if (dmg >= m->hp) {+ m->hp = 0, m->hurt = 30;+ b->state[2] = 3;+ return true;+ }+ return false;+}++static bool ends_toriel(Battle *b, Monster *m) {+ if (b->state[2] == 3 || b->state[2] == 4) {+ m->killed = true, m->alive = false, m->dust = 1;+ set_flag(F_TORIEL_FIGHT, 2);+ sv.kills++;+ return true;+ }+ if (b->state[0] >= 25) {+ set_flag(F_TORIEL_FIGHT, 1);+ m->spare = true;+ return true;+ }+ return false;+}++static void draw_toriel(Battle *b, Monster *m, int x, int y) {+ uint16_t s = SPR_TORIEL_B;+ if (m->hurt) s = SPR_TORIEL_HURT;+ else if (b->state[2] == 3 || b->state[2] == 4 || m->hp <= 0) s = SPR_TORIEL_DEAD;+ else if (b->atk == ATK_TOR_FALTER && b->phase >= 10) s = SPR_TORIEL_AWAY;+ if (m->dust) {+ int a = 255 - m->dust * 4;+ if (a > 0) gfx_sprite_color(SPR_TORIEL_DEAD, 0, x, y, 0, C_WHITE, (uint8_t)a);+ return;+ }+ gfx_sprite(s, 0, x, y, 0, 255);+}++const MonDef toriel_def = {+ .name = "Toriel", .hp = 440, .at = 6, .df = 1, .exp = 150, .gold = 0, .spr_idle = SPR_TORIEL_B, .spr_hurt = SPR_TORIEL_HURT,+ .check = "Knows best for you.", .acts = {"Talk"}, .nacts = 1,+ .neutral = {"* Toriel looks through you.", "* Toriel prepares a magical\n attack.", "* Toriel takes a deep breath.",+ "* Toriel is acting aloof."},+ .flags = MF_BOSS | MF_MISS_IS_SPARE, .on_spare = spare_toriel, .act = act_toriel, .talk = talk_toriel, .attack = attack_toriel, .flavor = flavor_toriel,+ .on_hit = hit_toriel, .ends = ends_toriel, .draw = draw_toriel,+};++/* ---------------------------------------------------------------- her attacks */+#define P(v) ((v) * FX)+static int bx0(void) { return bt.x0 + 3; }+static int bx1(void) { return bt.x1 - 3; }+static int by0(void) { return bt.y0 + 3; }+static int by1(void) { return bt.y1 - 3; }++static Bullet *fire(int x, int y, int vx, int vy, int dmg) {+ Bullet *b = bullet_new(SPR_FIREBULLET, P(x), P(y), vx, vy);+ if (b) b->owner = (uint8_t)bt.atk_owner, b->dmg = (int8_t)dmg, b->anim = 4;+ return b;+}++/* the paws: from the top-left (and, later, the bottom-right) they sweep across, leaving fire that then flies+ * at the SOUL and bounces off the walls; touching a paw ends it */+static void tor_hands(int t) {+ bool two = bt.turn >= 4;+ if (t < 0) {+ board_set(110, 80);+ return;+ }+ int sweep = 40;+ for (int k = 0; k < (two ? 2 : 1); k++) {+ if (t <= sweep) {+ /* the arc: from one top corner to the other, dipping down */+ int a = t * 128 / sweep; /* half a turn */+ int cx = board_cx(), r = (bx1() - bx0()) / 2 - 6;+ int x = cx - r * gfx_cos(a) / 1024, y = (k ? by1() - 6 - r * gfx_sin(a) / 1024 / 3 : by0() + 6 + r * gfx_sin(a) / 1024 / 3);+ if (k) x = cx + r * gfx_cos(a) / 1024;+ bt.as[2 * k] = x, bt.as[2 * k + 1] = y;+ if (t % 4 == 0) {+ Bullet *b = fire(x, y, 0, 0, 3);+ if (b) b->kind = BK_SCRIPT, b->a = 0;+ }+ }+ /* the paw touching the SOUL: 4 and the attack is over */+ int px = (int)bt.as[2 * k], py = (int)bt.as[2 * k + 1];+ if (t <= sweep + 6 && abs_i(px - soul_x()) < 12 && abs_i(py - soul_y()) < 8) {+ soul_hurt(damage_to_player(4), 0);+ bt.atk_t = bt.atk_len;+ }+ }+ if (t == sweep + 4)+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ int dx = bt.sx - b->x, dy = bt.sy - b->y, d = abs_i(dx) + abs_i(dy);+ if (!d) d = 1;+ b->vx = dx * P(2) / d, b->vy = dy * P(2) / d, b->a = 1;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT || !b->a) continue;+ b->x += b->vx, b->y += b->vy;+ if (b->x < P(bx0() + 3) || b->x > P(bx1() - 3)) b->vx = -b->vx;+ if (b->y < P(by0() + 3) || b->y > P(by1() - 3)) b->vy = -b->vy;+ if (++b->t > 90) b->kind = BK_NONE;+ }+}++/* two streams of fire falling, crossing over each other, with holes to slip through */+static void tor_waves(int t) {+ bool hard = bt.turn >= 6;+ if (t < 0) {+ board_set(110, hard ? 60 : 80);+ return;+ }+ if (t < 150 && t % 4 == 0) {+ int w = bx1() - bx0() - 16;+ for (int s = 0; s < 2; s++) {+ if ((t / 4 + s * 3) % 9 >= 7) continue; /* the holes */+ int x = board_cx() + (s ? 1 : -1) * w / 2 * gfx_sin(t * 2) / 1024;+ Bullet *b = fire(x, by0() - 4, 0, P(3) / 2, 4);+ if (b) b->kind = BK_SCRIPT;+ }+ }+ if (hard && t % 30 == 0) {+ fire(bx0() + 3, by0() + 10 + rnd_n(by1() - by0() - 20), 0, 0, 4);+ fire(bx1() - 3, by0() + 10 + rnd_n(by1() - by0() - 20), 0, 0, 4);+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ b->t++;+ if (b->y < P(by1() - 3)) b->y += b->vy;+ else if (b->t > 120) b->kind = BK_NONE; /* (they gather at the bottom a while) */+ }+}++/* thicker streams that stay in their lanes: safe between them */+static void tor_helix(int t) {+ if (t < 0) {+ board_set(120, 80);+ return;+ }+ if (t < 150 && t % 5 == 0) {+ int lanes = 3, w = bx1() - bx0();+ for (int l = 0; l < lanes; l++) {+ int cx = bx0() + w * (2 * l + 1) / (2 * lanes) + 10 * gfx_sin(t * 3 + l * 85) / 1024;+ for (int k = -1; k <= 1; k++) fire(cx + k * 6, by0() - 4, 0, P(3) / 2, 4);+ }+ }+}++/* her fire falls anywhere, but turns away from the SOUL: she cannot bring herself to hurt it */+static void tor_falter(int t) {+ if (t < 0) {+ board_set(110, 80);+ return;+ }+ if (t < 120 && t % 6 == 0) {+ Bullet *b = fire(bx0() + 4 + rnd_n(bx1() - bx0() - 8), by0() - 4, 0, P(3) / 2, 0);+ if (b) b->kind = BK_SCRIPT, b->flags |= BF_HARMLESS;+ }+ for (int i = 0; i < MAX_BULLETS; i++) {+ Bullet *b = &bullets[i];+ if (b->kind != BK_SCRIPT) continue;+ int dx = b->x - bt.sx, dy = b->y - bt.sy;+ if (abs_i(dx) < P(16) && abs_i(dy) < P(16)) b->x += dx > 0 ? P(2) : -P(2);+ b->y += b->vy;+ if (b->y > P(by1() + 6)) b->kind = BK_NONE;+ }+}++void toriel_attack(int atk, int t) {+ switch (atk) {+ case ATK_TOR_HANDS: tor_hands(t); break;+ case ATK_TOR_WAVES: tor_waves(t); break;+ case ATK_TOR_HELIX: tor_helix(t); break;+ case ATK_TOR_FALTER: tor_falter(t); break;+ }+}++void toriel_draw_attack(int atk, int t) {+ if (atk == ATK_TOR_HANDS && t <= 46) {+ bool two = bt.turn >= 4;+ for (int k = 0; k < (two ? 2 : 1); k++) gfx_sprite(SPR_HANDBULLET, 0, (int)bt.as[2 * k], (int)bt.as[2 * k + 1], k ? GFX_FLIPX : 0, 255);+ }+}diff --git a/games/undertale/tools/pack.py b/games/undertale/tools/pack.pyindex 0b03f50f..d941607b 100644--- a/games/undertale/tools/pack.py+++ b/games/undertale/tools/pack.py@@ -137,10 +137,32 @@ def halve(img):return Image.fromarray(b.astype(np.uint8), "RGBA")+def shrink(img):+ """art drawn for the original's 640 x 480 battle -> half its size: each 2 x 2 block becomes its most common+ opaque colour (or clear, if most of it is)"""+ a = np.array(img.convert("RGBA"))+ h, w = a.shape[:2]+ a = np.pad(a, ((0, h % 2), (0, w % 2), (0, 0)))+ H, W = a.shape[0] // 2, a.shape[1] // 2+ out = np.zeros((H, W, 4), np.uint8)+ for y in range(H):+ for x in range(W):+ block = a[2 * y:2 * y + 2, 2 * x:2 * x + 2].reshape(4, 4)+ op = block[block[:, 3] >= 128]+ if len(op) < 2:+ continue+ cols, counts = np.unique(op[:, :3], axis=0, return_counts=True)+ # ties: the darker colour keeps outlines+ best = sorted(zip(counts, [-int(c.sum()) for c in cols], range(len(cols))))[-1][2]+ out[y, x, :3] = cols[best]+ out[y, x, 3] = 255+ return Image.fromarray(out, "RGBA")++def load_frames(ref, src):"""'demo:spr_name' (all its numbered frames), 'wiki:File.png' or .gif (all frames, halved), 'png:path'"""kind, name = src.split(":", 1)- if kind == "demo":+ if kind in ("demo", "demo2"):d = os.path.join(ref, "demo", "mac", "sprites")out, k = [], 0while os.path.exists(os.path.join(d, "%s %d.png" % (name, k))):@@ -148,7 +170,7 @@ def load_frames(ref, src):k += 1if not out:raise FileNotFoundError(src)- return out+ return [shrink(f) for f in out] if kind == "demo2" else outif kind == "demobg":return [Image.open(os.path.join(ref, "demo", "mac", "bgs", name + ".png")).convert("RGBA")]if kind in ("wiki", "wiki1"):@@ -160,6 +182,9 @@ def load_frames(ref, src):return framesif kind == "png":return [Image.open(os.path.join(HERE, name)).convert("RGBA")]+ if kind == "wiki2": # a wiki file of the 640 x 480 battle, at the game's size: halved+ im = Image.open(os.path.join(ref, "wiki", "sprites", name))+ return [shrink(fr.convert("RGBA")) for fr in ImageSequence.Iterator(im)]raise ValueError(src)@@ -374,6 +399,21 @@ def main():imgs = []for s in (src if isinstance(src, list) else [src]):imgs += load_frames(ref, s)+ if opts.get("clear_black"): # drawn over black anyway: its black is clear, and costs nothing+ out = []+ for im in imgs:+ a = np.array(im)+ a[(a[:, :, 0] < 8) & (a[:, :, 1] < 8) & (a[:, :, 2] < 8)] = 0+ out.append(Image.fromarray(a, "RGBA"))+ imgs = out+ if opts.get("dedupe"): # GIFs repeat a frame for its duration: once each+ out = []+ for im in imgs:+ if not out or im.tobytes() != out[-1].tobytes():+ out.append(im)+ imgs = out+ if "every" in opts:+ imgs = imgs[::opts["every"]]if "frames" in opts:imgs = [imgs[i] for i in opts["frames"]]if "scale" in opts:diff --git a/games/undertale/tools/sprites.py b/games/undertale/tools/sprites.pyindex 1891747b..78e27a63 100644--- a/games/undertale/tools/sprites.py+++ b/games/undertale/tools/sprites.py@@ -20,7 +20,50 @@ SPRITES = [("toriel_r", "demo:spr_toriel_r", {"origin": (0, 0), "keep": True}),("savepoint", "demo:spr_savepoint", {"origin": (0, 0), "keep": True}),("flowey", "demo:spr_flowey", {"origin": (0, 0), "keep": True}),- ("heart", "demo:spr_heart", {"center": True, "keep": True}),+ # the SOUL, 8 x 8 as the game's 16 x 16 halved; frame 1: blue+ ("heart", "demo2:spr_heart", {"center": True, "keep": True}),+] + [+ # the battle: its buttons (orange, then yellow when chosen), the FIGHT target and its bar, the slash, the HP label+ (n, "demo2:spr_" + n, {"keep": True}) for n in [+ "fightbt", "talkbt", "itembt", "sparebt", "target", "targetchoice", "hpname", "battlebg"]+] + [+ ("strike", "demo2:spr_strike", {"center": True, "keep": True}),+ ("heartbreak", "demo2:spr_heartbreak", {"center": True, "keep": True}),+ ("heartshards", "demo2:spr_heartshards", {"center": True, "keep": True}),+ ("heartgtfo", "demo2:spr_heartgtfo", {"center": True, "keep": True}),+] + [+ # bullets, their middle at their origin+ (n, "demo2:spr_" + n, {"center": True}) for n in [+ "flybullet", "smallfrogbullet", "butterflybllt", "bulletmd", "bulletsm", "circlebulletmd1", "circlebulletsm",+ "vegbullet", "carrotbullet", "roachbullet", "roachdance", "teardrop", "firebullet", "handbullet", "spinbullet",+ "streambullet", "dirbullet", "bulletgenmd"]+] + [+ # the monsters, from the wiki (the game's battle sprites, halved); their origin at their feet+ ("dummy_b", "wiki:Dummy_battle.png", {"feet": True}),+ ("froggit_b", "wiki:Froggit_battle_idle.gif", {"feet": True, "dedupe": True, "every": 2}),+ ("froggit_hurt", "wiki:Froggit_battle_hurt.png", {"feet": True}),+ ("whimsun_b", "wiki:Whimsun_battle_idle.gif", {"feet": True, "dedupe": True, "every": 4}),+ ("whimsun_hurt", "wiki:Whimsun_battle_hurt.png", {"feet": True}),+ ("moldsmal_b", "wiki:Moldsmal_battle.gif", {"feet": True, "dedupe": True, "every": 4}),+ ("loox_b", "wiki:Loox_battle_idle.gif", {"feet": True, "dedupe": True}),+ ("loox_hurt", "wiki:Loox_battle_hurt.png", {"feet": True}),+ ("vegetoid_b", "wiki:Vegetoid_battle_idle.gif", {"feet": True, "dedupe": True}),+ ("vegetoid_hurt", "wiki:Vegetoid_battle_hurt.png", {"feet": True}),+ ("migosp_b", "wiki:Migosp_battle_idle.gif", {"feet": True, "dedupe": True, "every": 2}),+ ("migosp_hurt", "wiki:Migosp_battle_hurt.png", {"feet": True}),+ ("napstablook_b", "wiki:Napstablook_battle_idle.gif", {"feet": True, "dedupe": True}),+ ("napsta_hat", "demo:spr_blookhat", {"feet": True}),+ ("toriel_b", "wiki:Toriel_battle.png", {"feet": True}),+ ("toriel_hurt", "wiki:Toriel_battle_hurt.png", {"feet": True}),+ ("toriel_away", "wiki:Toriel_battle_shocked.png", {"feet": True}),+ ("toriel_fight", "wiki:Toriel_battle_in_fight.png", {"feet": True}),+ ("toriel_dead", "wiki:Toriel_battle_fatal_Neutral.png", {"feet": True}),+] + [+ # the intro's pictures, the title, GAME OVER+ ("introimage", "demo:spr_introimage", {"keep": True, "clear_black": True}),+ ("introlast", "demo:spr_introlast", {"keep": True, "clear_black": True}),+ ("undertaletitle", "demo:spr_undertaletitle", {"clear_black": True}),+ ("gameoverbg", "demo2:spr_gameoverbg", {"clear_black": True}),] + [# faces in the dialogue box (the demo's: a head, and Toriel's shoulders under it); 2 frames: talking("face_" + n, "demo:spr_face_" + n, {"keep": True}) for n in [@@ -38,6 +81,8 @@ SPRITES = [FONTS = [# the dialogue's font: Pixel Operator Mono Bold, 8 pixels a letterdict(name="main", ttf="PixelOperatorMono-Bold.ttf", size=16, height=12, advance=8, ascent=13),+ # the menus' text: Pixel Operator Bold+ dict(name="menu", ttf="PixelOperator-Bold.ttf", size=16, height=12, ascent=13),# small text (the menu's numbers)dict(name="small", ttf="PixelOperator8.ttf", size=8, height=8, ascent=8),]