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
Kingdom Rush: the special towers, their abilities, Vez'nan, tests
Mason36352f734c427ca
diff --git a/games/kingdomrush/src/anim.h b/games/kingdomrush/src/anim.hindex 5ca191c3..a68df731 100644--- a/games/kingdomrush/src/anim.h+++ b/games/kingdomrush/src/anim.h@@ -24,5 +24,8 @@ void summon_skeleton(struct Enemy *e, int k);void demon_explode(struct Enemy *e);bool boss_power(struct Enemy *e);bool veznan_power(struct Enemy *e);+void veznan_spawn(void);+void veznan_descend(void);+void vportals_step(void);void enemy_armor_down(struct Enemy *e, int v);#endifdiff --git a/games/kingdomrush/src/enemy.c b/games/kingdomrush/src/enemy.cindex e8380cd9..5899bc8d 100644--- a/games/kingdomrush/src/enemy.c+++ b/games/kingdomrush/src/enemy.c@@ -35,11 +35,21 @@ static void play(Enemy *e, int an) {e->anim = (uint16_t)an, e->anim_t = 0;}+/* an enemy's animation (Vez'nan's demon has its own) */+static int anim_of(const Enemy *e, int a) {+ if (e->type == EN_VEZNAN && e->froze == 3) {+ static const int16_t demon[6] = {AN_E_VEZNAN_WALKINGDEMON, AN_E_VEZNAN_WALKINGDEMON, AN_E_VEZNAN_WALKINGDOWNDEMON,+ AN_E_VEZNAN_IDLEDEMON, AN_E_VEZNAN_FIGHTINGDEMON, AN_E_VEZNAN_DEAD};+ return demon[a];+ }+ return ANIM(e->type, a);+}+/* getFacing: the walk for the road's direction */static void facing(Enemy *e) {if (e->fighting) {e->charge_t = 0;- play(e, ANIM(e->type, A_IDLE));+ play(e, anim_of(e, A_IDLE));return;}Pt a = enemy_road(e, 0), b = enemy_road(e, 1);@@ -53,8 +63,8 @@ static void facing(Enemy *e) {if (d == e->dir) return;e->dir = (uint8_t)d;if (d == 1 || d == 2) e->left = d == 2;- int an = ANIM(e->type, d == 3 ? A_UP : d == 4 ? A_DOWN : A_WALK);- if (an < 0) an = ANIM(e->type, A_WALK);+ int an = anim_of(e, d == 3 ? A_UP : d == 4 ? A_DOWN : A_WALK);+ if (an < 0) an = anim_of(e, A_WALK);play(e, an);}@@ -124,7 +134,7 @@ void enemy_kill(Enemy *e, int how) {fx_add(e->flying ? AN_FX_DESINTEGRATEAIR : ST(e)->size == SZ_BIG ? AN_FX_DESINTEGRATEBIG : AN_FX_DESINTEGRATENORMAL,e->x, e->y, false, false);e->alive = 0;- } else {+ } else if (how == 0) {fx_add(ST(e)->blood == 1 ? AN_B_BLOODGREEN : ST(e)->blood == 2 ? -1 : AN_B_BLOOD, e->x, e->y + 1, false, true);}}@@ -147,6 +157,12 @@ void enemy_damage(Enemy *e, int dmg, int type, int pierce) {}}if (e->health <= 0) {+ if (e->type == EN_VEZNAN && e->froze == 1) { /* gotoDemon: the wizard falls, the demon rises */+ e->health = 1, e->froze = 2, e->sp_c = 0, e->active = 0;+ if (e->soldier >= 0) enemy_unblock(e);+ play(e, AN_E_VEZNAN_TODEMON);+ return;+ }e->health = 0;enemy_kill(e, 0);}@@ -163,7 +179,7 @@ bool enemy_dodge(Enemy *e) { return e->dodge && rnd() * 100.0f <= (float)e->dodg/* ------------------------------------------------------------------ soldiers */void enemy_block(Enemy *e, int soldier) {e->soldier = (int8_t)soldier, e->blocked = 1;- play(e, ANIM(e->type, A_IDLE));+ play(e, anim_of(e, A_IDLE));}void enemy_unblock(Enemy *e) { /* §_-Ef§ */@@ -175,7 +191,7 @@ void enemy_unblock(Enemy *e) { /* §_-Ef§ */void enemy_start_fighting(Enemy *e) {e->fighting = 1, e->dir = 0;e->y += 0.1f;- play(e, ANIM(e->type, A_FIGHT));+ play(e, anim_of(e, A_FIGHT));e->charging = 1;}@@ -277,12 +293,6 @@ static void debuffs(Enemy *e) {}/* ------------------------------------------------------------------ the specials */-static int count_alive(int type) {- int n = 0;- for (int i = 0; i < MAX_ENEMIES; i++)- if (B.en[i].alive && !B.en[i].dead && B.en[i].type == type) n++;- return n;-}/* a soldier for a ranged attack: in range, not too close (§_-F9§, §_-9s§) */static int ranged_target(Enemy *e, int range, int min_range) {@@ -573,12 +583,16 @@ static bool special_move(Enemy *e) {void enemy_step(Enemy *e) {e->anim_t++;if (e->dead) {- if (e->dead_t == 0) play(e, ANIM(e->type, A_DEAD));+ if (e->dead_t == 0) play(e, anim_of(e, A_DEAD));e->dead_t++;if ((e->type == EN_DEMON || e->type == EN_DEMONWOLF || e->type == EN_DEMONMAGE) && e->dead_t == 5) demon_explode(e);if (e->dead_t >= e->dead_time) e->alive = 0;return;}+ if (e->type == EN_VEZNAN && (e->froze == 0 || e->froze == 2)) { /* in his tower, or changing */+ veznan_power(e);+ return;+ }if (special_move(e)) return;if (e->dead) return;debuffs(e);@@ -605,11 +619,11 @@ void enemy_step(Enemy *e) {e->charge_t = 0;e->charging = 0;attack(e);- play(e, ANIM(e->type, A_IDLE));+ play(e, anim_of(e, A_IDLE));so = SO(e->soldier);if (!so || so->dead) enemy_unblock(e);} else if (++e->reload_t == e->reload) {- play(e, ANIM(e->type, A_FIGHT));+ play(e, anim_of(e, A_FIGHT));e->charging = 1;e->reload_t = 0;}diff --git a/games/kingdomrush/src/enemy2.c b/games/kingdomrush/src/enemy2.cindex 21915a10..350c862c 100644--- a/games/kingdomrush/src/enemy2.c+++ b/games/kingdomrush/src/enemy2.c@@ -161,7 +161,141 @@ static bool yeti_boss(Enemy *e) {return false;}-bool veznan_power(Enemy *e) { (void)e; return false; }+/* ------------------------------------------------------------------ Vez'nan (EnemyVeznan, Level12) */+/* his tower's spells by wave: [hold after s, towers held], [portals after s, 1, 2, 3 open, times] */+static const uint8_t hold_timers[15][2] = {{0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 2}, {0, 2}, {0, 2}, {0, 2},+ {13, 2}, {13, 3}, {14, 4}, {14, 5}, {16, 6}, {16, 7}, {18, 8}};+static const uint8_t portal_timers[15][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0},+ {0, 0, 0, 0, 0}, {15, 1, 0, 0, 3}, {10, 1, 0, 0, 2}, {20, 0, 1, 0, 3},+ {15, 1, 0, 0, 3}, {20, 1, 1, 0, 3}, {15, 1, 1, 0, 3}, {15, 1, 1, 0, 3},+ {15, 0, 0, 1, 3}, {15, 1, 1, 1, 3}, {15, 1, 1, 1, 3}};+static const int16_t portal_at[3][3] = {{362, 243, 103}, {362, 365, 148}, {362, 493, 228}};++void veznan_spawn(void) {+ int i = enemy_spawn(EN_VEZNAN, 0, 1, 0);+ if (i < 0) return;+ Enemy *e = &B.en[i];+ e->x = 362, e->y = 70, e->active = 0, e->froze = 0, e->sp_n2 = -1;+ e->anim = AN_E_VEZNAN_IDLEDOWN, e->anim_t = 0;+ B.veznan = (int8_t)i;+}++/* the portals' demons: 50% 4 to 6 demon spawns, 30% 3 demon hounds, 20% 5 spawns and a demon lord */+static void portals_open(const uint8_t *pt) {+ for (int k = 0; k < 3; k++) {+ if (!pt[1 + k]) continue;+ for (int s = 0; s < (int)(sizeof B.vportal / sizeof B.vportal[0]); s++)+ if (!B.vportal[s].on) {+ float r = rnd();+ B.vportal[s].on = 1, B.vportal[s].t = 0, B.vportal[s].k = (uint8_t)k, B.vportal[s].n = 0;+ B.vportal[s].group = r <= 0.5f ? 1 : r <= 0.8f ? 2 : 3;+ B.vportal[s].max = (uint8_t)(r <= 0.5f ? rnd_round(4, 6) : r <= 0.8f ? 3 : 6);+ break;+ }+ }+}++void vportals_step(void) {+ for (int s = 0; s < (int)(sizeof B.vportal / sizeof B.vportal[0]); s++) {+ if (!B.vportal[s].on) continue;+ if (++B.vportal[s].t % 30 == 0) {+ int g = B.vportal[s].group, k = B.vportal[s].k;+ int type = g == 2 ? EN_DEMONWOLF : g == 3 && B.vportal[s].n >= 5 ? EN_DEMONMAGE : EN_DEMON;+ int lane = B.vportal[s].n % B.nlanes[0], node = portal_at[k][2];+ if (node > path_len(0, lane) - 12) node = path_len(0, lane) - 12;+ int j = enemy_spawn(type, 0, lane, node);+ if (j >= 0) {+ B.en[j].x = portal_at[k][0], B.en[j].y = portal_at[k][1], B.en[j].active = 1;+ B.en[j].teleporting = 2, B.en[j].teleport_t = 0;+ }+ if (++B.vportal[s].n >= B.vportal[s].max) B.vportal[s].on = 0;+ }+ }+}++static void hold_towers(int n) {+ int cand[MAX_SPOTS], nc = 0;+ for (int s = 0; s < B.nspots; s++) {+ Spot *t = &B.spot[s];+ if (t->kind != TW_NONE && t->kind != 0xFF && !t->building && !t->disabled) cand[nc++] = s;+ }+ for (int k = 0; k < n && nc > 0; k++) {+ int pick = (int)(rnd() * nc);+ if (pick >= nc) pick = nc - 1;+ Spot *t = &B.spot[cand[pick]];+ t->disabled = 1, t->ice = 4, t->ice_t = 0; /* VeznanBlock */+ cand[pick] = cand[--nc];+ }+}++/* he leaves his tower when every other enemy is gone */+void veznan_descend(void) {+ Enemy *e = EN(B.veznan);+ if (!e || e->froze) return;+ e->froze = 1, e->active = 1, e->node = 0, e->dir = 0;+ Pt p = path_node(0, e->lane, 0);+ e->x = p.x, e->y = p.y;+ e->sp_t2 = 0, e->sp_n = 40, e->sp_state = 0; /* (portals every 10 s from now) */+}++bool veznan_power(Enemy *e) {+ int w = B.current_wave - 1;+ if (w < 0) w = 0;+ if (w > 14) w = 14;+ if (e->froze == 0) { /* in his tower: spells each wave */+ if (e->sp_n2 != B.current_wave) { /* (updateTimers at a wave's start) */+ e->sp_n2 = B.current_wave, e->sp_t = 0, e->sp_t2 = 0, e->sp_n = portal_timers[w][4];+ }+ e->sp_t++, e->sp_t2++;+ if (hold_timers[w][0] && e->sp_t == hold_timers[w][0] * FPS) {+ e->anim = AN_E_VEZNAN_SPELLDOWN, e->anim_t = 0;+ hold_towers(hold_timers[w][1]);+ }+ if (portal_timers[w][0] && e->sp_n > 0 && e->sp_t2 >= portal_timers[w][0] * FPS) {+ e->anim = AN_E_VEZNAN_SPELL, e->anim_t = 0;+ portals_open(portal_timers[w]);+ e->sp_t2 = 0, e->sp_n--;+ }+ if (e->anim_t > 45) e->anim = AN_E_VEZNAN_IDLEDOWN, e->anim_t = 0;+ return true;+ }+ if (e->froze == 2) { /* becoming the demon */+ if (++e->sp_c >= 34) {+ e->froze = 3, e->dir = 0;+ e->health = e->init_health = S_enemies_veznan_demonHealth * enemy_health_pct / 100;+ e->speed = e->base_speed = S_enemies_veznan_demonSpeed, e->margin = e->speed + 0.1f;+ e->charge = 53, e->reload = (int16_t)(S_enemies_veznan_demonAttackReloadTime - 53);+ e->active = 1;+ e->anim = AN_E_VEZNAN_WALKINGDEMON, e->anim_t = 0;+ }+ return true;+ }+ if (e->froze == 1) {+ /* walking down: portals every 10 s, and his soul steal (tsung) every 5 s on the soldiers around */+ if (++e->sp_t2 >= 10 * FPS && e->sp_n > 0) {+ static const uint8_t all[5] = {10, 1, 1, 1, 40};+ portals_open(all);+ e->sp_t2 = 0, e->sp_n--;+ }+ if (++e->rage_t >= S_enemies_veznan_tsungReloadTime) {+ int n = 0;+ for (int i = 0; i < MAX_SOLDIERS && n < S_enemies_veznan_tsungMaxEnemies; i++) {+ Soldier *so = &B.so[i];+ if (!so->alive || !so->active || so->kind == SK_ELEMENTAL) continue;+ if (!ellipse_contains(e->x, e->y, S_enemies_veznan_tsungRangeWidth, S_enemies_veznan_tsungRangeWidth * RANGE_RATIO, so->x, so->y)) continue;+ float dx = so->x - e->x, dy = so->y - e->y;+ if (sqrtf(dx * dx + dy * dy) <= S_enemies_veznan_tsungMinRange) continue;+ soldier_damage(so, 30000, true);+ n++;+ }+ if (n) {+ e->anim = AN_E_VEZNAN_RANGEDATTACK, e->anim_t = 0;+ e->rage_t = 0;+ }+ }+ }+ return false;+}bool boss_power(Enemy *e) {switch (e->type) {diff --git a/games/kingdomrush/src/kr.h b/games/kingdomrush/src/kr.hindex d3461e03..0bb115d8 100644--- a/games/kingdomrush/src/kr.h+++ b/games/kingdomrush/src/kr.h@@ -82,7 +82,7 @@ typedef struct Enemy {float slow_speed; /* the speed before a slow or a net */float base_speed;/* the specials: timers and counts, by enemy */- int16_t sp_t, sp_t2, sp_n, sp_n2, sp_state;+ int16_t sp_t, sp_t2, sp_n, sp_n2, sp_state, sp_c;int16_t regen_t, rage_t, respawn_t;int16_t teleport_t, teleports, thorn_t, thorn_tick, thorns, thorn_dmg, thorn_len, thorn_every;uint8_t teleporting, thorned;@@ -107,7 +107,10 @@ typedef struct Soldier {int16_t life_time, life_t, lifes;uint16_t anim, anim_t;int16_t sp_t, sp_t2; /* specials' cooldowns */- uint8_t sp_state;+ uint8_t sp_state; /* the special under way: 1 healing, 2 holy strike, 3 a net, 4 an axe */+ int16_t sp_c;+ int8_t sp_target;+ Pt sp_at;float home_x, home_y; /* reinforcements: where they were called (§_-Fm§) */} Soldier;@@ -129,10 +132,13 @@ typedef struct Spot {int8_t soldiers[3];uint8_t door; /* 0 closed, 1 opening, 2 open, 3 closing */int16_t door_t;- /* specials: each ability's level and cooldown */+ /* specials: each ability's level and cooldown, the one under way (sp_on: 1 + its index) and its count */uint8_t sp_lvl[3];int16_t sp_t[3];- uint8_t sp_flag;+ uint8_t sp_flag, sp_on;+ int16_t sp_c, poison_t;+ int8_t sp_target;+ Pt sp_at;int8_t elemental;uint8_t premade; /* built by the level (iron mode), not bought */uint8_t disabled; /* frozen, or held by Vez'nan */@@ -142,7 +148,8 @@ typedef struct Spot {enum {BU_ARROW, BU_BOLT, BU_BOMB, BU_FIREBALL, BU_ARROW_ENEMY, BU_AXE, BU_AXE_ENEMY, BU_MISSILE, BU_MISSILE_ENEMY,- BU_RAY_ARCANE, BU_RAY_DEATH, BU_RAY_POLY, BU_RAY_TESLA, BU_BOLT_SORCERER, BU_BOLT_NECRO, BU_SPEAR, BU_SHOT+ BU_RAY_ARCANE, BU_RAY_DEATH, BU_RAY_POLY, BU_RAY_TESLA, BU_BOLT_SORCERER, BU_BOLT_NECRO, BU_SPEAR, BU_SHOT,+ BU_FIELD, BU_NET};typedef struct Bullet {uint8_t alive, kind, level, active, crit, first;@@ -151,6 +158,9 @@ typedef struct Bullet {float x, y, x0, y0, tx, ty, xs, ys, acc, acc_max, g, rot;int16_t min_dmg, max_dmg, area, pierce, bombs;uint16_t anim, anim_t;+ int16_t dmg; /* a ray's whole damage, spread over its frames */+ uint8_t jump, jump_max, shielded;+ int8_t hitlist[6]; /* the tesla's arcs: whom they already hit */} Bullet;typedef struct Fx {@@ -213,6 +223,12 @@ typedef struct {/* spider eggs (SpiderEgg): where, on which road, when they hatch */struct { uint8_t on, path, n; int16_t node, t; float x, y; } egg[12];int16_t skeletons; /* currentSkeletons */+ /* the rain of fire's burning ground (ScorchedEarth) */+ struct { uint8_t on; int16_t t, dur; Pt at; } burn[8];+ int8_t veznan; /* Vez'nan, in the last stage's campaign */+ struct { uint8_t on, k, n, group, max; int16_t t; } vportal[6];+ /* the arcane wizards' portals (TeleportPortal) */+ struct { uint8_t on; int16_t t, left, nodes; Pt at; } portal[4];uint32_t frame;int32_t gold_won;uint16_t kills;diff --git a/games/kingdomrush/src/level.c b/games/kingdomrush/src/level.cindex de1c9693..25de818b 100644--- a/games/kingdomrush/src/level.c+++ b/games/kingdomrush/src/level.c@@ -6,6 +6,8 @@* how many, the frames between them, the frames before the next spawn, a fixed lane or a random one. */#include <string.h>#include "kr.h"+#include "anim.h"+#include "special.h"#include "res.h"Battle B;@@ -116,6 +118,8 @@ void battle_start(int level, int mode) {static const uint8_t kinds[5] = {0, TW_ARCHER, TW_BARRACKS, TW_MAGE, TW_ENGINEER};spot_make(premade[k][0], kinds[premade[k][1] >> 4], premade[k][1] & 15);}+ B.veznan = -1;+ if (level == NLEVELS - 1 && mode == MODE_CAMPAIGN) veznan_spawn(); /* (Level12: he waits in his tower) */}/* ------------------------------------------------------------------ cash, lives */@@ -237,7 +241,13 @@ static bool has_enemies(void) {return false;}for (int i = 0; i < MAX_ENEMIES; i++)- if (B.en[i].alive && (!B.en[i].boss || !B.en[i].dead)) return true;+ if (B.en[i].alive && (!B.en[i].boss || !B.en[i].dead) && !(i == B.veznan && B.en[i].froze == 0)) return true;+ /* Level12: the others gone, Vez'nan comes down himself */+ Enemy *v = EN(B.veznan);+ if (v && !v->dead) {+ if (v->froze == 0) veznan_descend();+ return true;+ }B.ready_win = 1, B.ready_win_t = 0;return true;}@@ -325,5 +335,8 @@ void battle_step(void) {if (B.bu[i].alive) bullet_step(&B.bu[i]);fx_step();power_step();+ eggs_step();+ portals_step();+ vportals_step();if (B.state == LS_PRE_WIN && ++B.prewin_t > 30) B.state = LS_WIN;}diff --git a/games/kingdomrush/src/play.c b/games/kingdomrush/src/play.cindex fa978afe..a46d2b82 100644--- a/games/kingdomrush/src/play.c+++ b/games/kingdomrush/src/play.c@@ -336,7 +336,6 @@ void play_step(void) {}if (P.mode == M_PAUSE || P.mode == M_QUIT) return;battle_step();- eggs_step();if (B.pending_note) {int n = B.pending_note;B.pending_note = 0;@@ -422,8 +421,9 @@ static void draw_spot(int s) {draw_tower(t, x, y);if (t->building) draw_bar(x - 7, SY(t->y - 45), 14, 30 - t->building, 30);if (t->ice) {- int an = t->ice == 1 ? AN_B_ICEBLOCK_START : t->ice == 2 ? AN_B_ICEBLOCK_IDLE : AN_B_ICEBLOCK_END;- spr_anim(an, t->ice == 2 ? 0 : t->ice_t, false, x, y, false);+ static const int16_t an[7] = {0, AN_B_ICEBLOCK_START, AN_B_ICEBLOCK_IDLE, AN_B_ICEBLOCK_END, AN_B_VEZNANBLOCK_START,+ AN_B_VEZNANBLOCK_IDLE, AN_B_VEZNANBLOCK_RELEASE};+ spr_anim(an[t->ice], t->ice == 2 || t->ice == 5 ? (int)(B.frame & 255) : t->ice_t, t->ice == 2 || t->ice == 5, x, y, false);}}@@ -492,7 +492,7 @@ static void draw_field(void) {static void draw_selection(void) {if (P.mode == M_SELECT) {Pt p = sel_pos();- int x = SX(p.x), y = SY(p.y);+ int x = SX(p.x);if (P.sel_kind == SEL_SPOT) {Spot *t = &B.spot[P.sel];int ty = SY(t->y - (t->kind == TW_NONE ? 36 : 18));diff --git a/games/kingdomrush/src/power.c b/games/kingdomrush/src/power.cindex c7487dd1..045c682c 100644--- a/games/kingdomrush/src/power.c+++ b/games/kingdomrush/src/power.c@@ -120,8 +120,37 @@ bool power_reinforce(Pt at) {return true;}+/* ------------------------------------------------------------------ scorched earth: the meteors' fire on the ground */+void special_scorched_earth(float x, float y) {+ for (int k = 0; k < (int)(sizeof B.burn / sizeof B.burn[0]); k++)+ if (!B.burn[k].on) {+ B.burn[k].on = 1, B.burn[k].t = 0, B.burn[k].at = (Pt){x, y};+ /* Blazing Earth: twice as long, hotter */+ B.burn[k].dur = up_on(UP_RAIN, 4) ? S_powers_fireball_blazingEarthDuration : S_powers_fireball_scorchedEarthDuration;+ return;+ }+}++static void burn_step(void) {+ bool blazing = up_on(UP_RAIN, 4);+ int lo = blazing ? S_powers_fireball_blazingEarthMinDamage : S_powers_fireball_scorchedEarthMinDamage;+ int hi = blazing ? S_powers_fireball_blazingEarthMaxDamage : S_powers_fireball_scorchedEarthMaxDamage;+ int r = blazing ? S_powers_fireball_blazingEarthRange : S_powers_fireball_scorchedEarthRange;+ for (int k = 0; k < (int)(sizeof B.burn / sizeof B.burn[0]); k++) {+ if (!B.burn[k].on) continue;+ if (++B.burn[k].t % FPS == 0)+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (e->alive && e->active && !e->flying && ellipse_contains(B.burn[k].at.x, B.burn[k].at.y, (float)r, r * RANGE_RATIO, e->x, e->y))+ enemy_damage(e, rnd_dmg(lo, hi), ARM_I, 0);+ }+ if (B.burn[k].t >= B.burn[k].dur) B.burn[k].on = 0;+ }+}+/* ------------------------------------------------------------------ a frame */void power_step(void) {+ burn_step();if (B.fire_open_t) B.fire_open_t--;else if (B.fire_on && B.fire_cd > 0) B.fire_cd--;if (B.reinf_open_t) B.reinf_open_t--;diff --git a/games/kingdomrush/src/soldier.c b/games/kingdomrush/src/soldier.cindex b5a542c3..3ef3aaa2 100644--- a/games/kingdomrush/src/soldier.c+++ b/games/kingdomrush/src/soldier.c@@ -225,6 +225,11 @@ static void hit(Soldier *so) {go_home(so);return;}+ if (so->kind == SK_ELEMENTAL) { /* the elemental hits all around where it strikes */+ special_elemental_hit(so);+ if (!e->active) stop_fight(so);+ return;+ }if (!enemy_dodge(e)) {enemy_damage(e, special_soldier_damage(so, rnd_dmg(so->min_dmg, so->max_dmg)), ARM_P, 0);if (!e->active) stop_fight(so);@@ -372,8 +377,8 @@ void soldier_step(Soldier *so) {so->charge_t = 0, so->charging = 0;} else if (++so->reload_t == so->reload) {play(so, kind_of(so->kind).an_fight);- special_soldier_attack_anim(so);- so->charging = 1, so->reload_t = 0;+ so->reload_t = 0;+ if (!special_soldier_attack_anim(so)) so->charging = 1;}} elsego_home(so);diff --git a/games/kingdomrush/src/special.c b/games/kingdomrush/src/special.cindex 548c5700..9b984b5f 100644--- a/games/kingdomrush/src/special.c+++ b/games/kingdomrush/src/special.c@@ -1,11 +1,14 @@-/* The special towers (TowerArcherRanger, TowerArcherMusketeer, TowerMageArcane, TowerMageSorcerer,- * TowerEngineerBfg, TowerEngineerTesla, TowerSoldierPaladin, TowerSoldierBarbarian), their abilities and- * soldiers; and the hits every mage bolt and bomb share. */+/* The special towers (TowerArcherRanger and its TowerArcherDruid, TowerArcherMusketeer, TowerMageArcane,+ * TowerMageSorcerer, TowerEngineerBfg, TowerEngineerTesla, TowerSoldierPaladin, TowerSoldierBarbarian), their+ * abilities, shots and soldiers (SoldierPaladin, SoldierBarbarian, SoldierElemental); and the hits every mage+ * bolt and bomb share (Bolt, Bomb). */#include <math.h>+#include <stdlib.h>#include <string.h>#include "special.h"#include "settings.h"#include "anim.h"+#include "gfx.h"int special_kind(int kind, int choice) {switch (kind) {@@ -17,41 +20,1088 @@ int special_kind(int kind, int choice) {}}-int special_abilities(int s, Ability *a) { (void)s, (void)a; return 0; }-void spot_buy_ability(int s, int k) { (void)s, (void)k; }-int spot_ability_cost(int s, int k) { (void)s, (void)k; return -1; }-void special_draw_bullet(const Bullet *b, int x, int y) { (void)b, (void)x, (void)y; }-void special_init(int s) { (void)s; }-void special_sold(int s) { (void)s; }-void special_timers(int s) { (void)s; }-bool special_run(int s) { (void)s; return false; }-bool special_shoot(int s) { (void)s; return false; }-void special_arrow(int s, Bullet *b) { (void)s, (void)b; }+/* ------------------------------------------------------------------ the abilities: icons, places, prices */+enum {+ IC_ELEMENTAL = 19, IC_POLYMORPH, IC_THORNS, IC_POISON, IC_SNIPER, IC_SHRAPNEL, IC_HOLY, IC_HEALING, IC_SHIELD,+ IC_AXE, IC_NETS, IC_DUAL, IC_CLUSTER, IC_MISSILE, IC_BOLT, IC_OVERCHARGE, IC_DEATH, IC_TELEPORT+};++static int power_cost(int c) { /* Industrialization: the artillery's abilities a quarter cheaper */+ return up_on(UP_ENGINEERS, 4) ? (int)floorf(c - c * 0.25f) : c;+}++int special_abilities(int s, Ability *a) {+ Spot *t = &B.spot[s];+ const uint8_t *L = t->sp_lvl;+#define AB(i, ic, pos, max, first, next, key) a[i] = (Ability){ic, pos, L[i], max, (int16_t)(L[i] ? (next) : (first)), key}+ switch (t->kind) {+ case TW_RANGER:+ AB(0, IC_POISON, 1, S_archers_ranger_poisonLevels, S_archers_ranger_poisonCost, S_archers_ranger_poisonCostLevel, "TOWER_RANGERS_POISON_NAME");+ AB(1, IC_THORNS, 2, S_archers_ranger_thornLevels, S_archers_ranger_thornCost, S_archers_ranger_thornCostLevel, "TOWER_RANGERS_THORNS_NAME");+ return 2;+ case TW_MUSKETEER:+ AB(0, IC_SNIPER, 1, S_archers_musketeer_sniperLevels, S_archers_musketeer_sniperCost, S_archers_musketeer_sniperCostLevel, "TOWER_MUSKETEERS_SNIPER_NAME");+ AB(1, IC_SHRAPNEL, 2, S_archers_musketeer_shrapnelLevels, S_archers_musketeer_shrapnelCost, S_archers_musketeer_shrapnelCostLevel, "TOWER_MUSKETEERS_SHRAPNEL_NAME");+ return 2;+ case TW_ARCANE:+ AB(0, IC_DEATH, 1, S_mages_arcane_deathRayLevels, S_mages_arcane_deathRayCost, S_mages_arcane_deathRayCostLevel, "TOWER_ARCANE_DESINTEGRATE_NAME");+ AB(1, IC_TELEPORT, 2, S_mages_arcane_teleportLevels, S_mages_arcane_teleportCost, S_mages_arcane_teleportCostLevel, "TOWER_ARCANE_TELEPORT_NAME");+ return 2;+ case TW_SORCERER:+ AB(0, IC_ELEMENTAL, 1, S_mages_sorcerer_elementalLevels, S_mages_sorcerer_elementalCost, S_mages_sorcerer_elementalCostLevel, "TOWER_SORCERER_ELEMENTAL_NAME");+ AB(1, IC_POLYMORPH, 2, S_mages_sorcerer_polymorphLevels, S_mages_sorcerer_polymorphCost, S_mages_sorcerer_polymorphCostLevel, "TOWER_SORCERER_POLIMORPH_NAME");+ return 2;+ case TW_BFG:+ AB(0, IC_CLUSTER, 1, S_engineers_bfg_clusterLevels, power_cost(S_engineers_bfg_clusterCost), power_cost(S_engineers_bfg_clusterCostLevel), "TOWER_BFG_CLUSTER_NAME");+ AB(1, IC_MISSILE, 2, S_engineers_bfg_missileLevels, power_cost(S_engineers_bfg_missileCost), power_cost(S_engineers_bfg_missileCostLevel), "TOWER_BFG_MISSILE_NAME");+ return 2;+ case TW_TESLA:+ AB(0, IC_OVERCHARGE, 1, S_engineers_tesla_overchargeLevels, power_cost(S_engineers_tesla_overchargeCost), power_cost(S_engineers_tesla_overchargeCostLevel), "TOWER_TESLA_OVERCHARGE_NAME");+ AB(1, IC_BOLT, 2, S_engineers_tesla_chargedBoltLevels, power_cost(S_engineers_tesla_chargedBoltCost), power_cost(S_engineers_tesla_chargedBoltCostLevel), "TOWER_TESLA_CHARGED_BOLT_NAME");+ return 2;+ case TW_PALADIN:+ AB(0, IC_HEALING, 10, S_soldiers_paladin_healingLevels, S_soldiers_paladin_healingCost, S_soldiers_paladin_healingCostLevel, "TOWER_PALADINS_HEALING_NAME");+ AB(1, IC_SHIELD, 7, S_soldiers_paladin_shieldLevels, S_soldiers_paladin_shieldCost, S_soldiers_paladin_shieldCostLevel, "TOWER_PALADINS_SHIELD_NAME");+ AB(2, IC_HOLY, 11, S_soldiers_paladin_holyStrikeLevels, S_soldiers_paladin_holyStrikeCost, S_soldiers_paladin_holyStrikeCostLevel, "TOWER_PALADINS_HOLY_STRIKE_NAME");+ return 3;+ case TW_BARBARIAN:+ AB(0, IC_AXE, 10, S_soldiers_barbarian_throwingLevels, S_soldiers_barbarian_throwingCost, S_soldiers_barbarian_throwingCostLevel, "TOWER_BARBARIANS_THROWING_AXES_NAME");+ AB(1, IC_NETS, 7, S_soldiers_barbarian_huntingNetsLevels, S_soldiers_barbarian_huntingNetsCost, S_soldiers_barbarian_huntingNetsCostLevel, "TOWER_BARBARIANS_HUNTER_NETS_NAME");+ AB(2, IC_DUAL, 11, S_soldiers_barbarian_dualWeaponsLevels, S_soldiers_barbarian_dualWeaponsCost, S_soldiers_barbarian_dualWeaponsCostLevel, "TOWER_BARBARIANS_DOUBLE_AXE_NAME");+ return 3;+ }+#undef AB+ return 0;+}++int spot_ability_cost(int s, int k) {+ Ability a[3];+ int n = special_abilities(s, a);+ if (k >= n || a[k].level >= a[k].max) return -1;+ return a[k].cost;+}++static Soldier *elemental(Spot *t) { return SO(t->elemental); }++void spot_buy_ability(int s, int k) {+ Spot *t = &B.spot[s];+ int c = spot_ability_cost(s, k);+ if (c < 0 || c > B.cash) return;+ battle_cash(-c);+ t->spent = (int16_t)(t->spent + c);+ t->sp_lvl[k]++;+ t->sp_t[k] = 0;+ if (t->sp_on == k + 1) t->sp_on = 0, t->sp_c = 0;+ if (t->kind == TW_SORCERER && k == 0) { /* the elemental: summoned, or stronger */+ Soldier *el = elemental(t);+ if (!el) {+ int i = soldier_new(SK_ELEMENTAL, s, t->rally, t->rally);+ t->elemental = (int8_t)i;+ } else+ special_elemental_stats(el, s);+ }+ if (t->kind == TW_PALADIN || t->kind == TW_BARBARIAN)+ for (int j = 0; j < 3; j++) {+ Soldier *so = SO(t->soldiers[j]);+ if (so) special_new_soldier(so);+ }+}++void special_init(int s) {+ Spot *t = &B.spot[s];+ t->sp_on = 0, t->sp_c = 0;+ if (t->kind == TW_PALADIN || t->kind == TW_BARBARIAN)+ for (int j = 0; j < 3; j++) {+ Soldier *so = SO(t->soldiers[j]);+ if (so) special_new_soldier(so);+ }+}++void special_sold(int s) {+ Spot *t = &B.spot[s];+ Soldier *el = elemental(t);+ if (el) soldier_free(el);+ t->elemental = -1;+}++void special_rally(int s, Pt p) {+ Spot *t = &B.spot[s];+ t->rally = p;+ Soldier *el = elemental(t);+ if (el) soldier_set_rally(el, p);+ fx_add(AN_T_FLAG, p.x, p.y, false, false);+}++/* runSpecialTimers */+void special_timers(int s) {+ Spot *t = &B.spot[s];+ switch (t->kind) {+ case TW_RANGER:+ if (t->sp_lvl[1]) { /* TowerArcherDruid: the thorns, on their own */+ int reload = S_archers_ranger_thornCoolDown;+ if (t->sp_t[1] < reload) {+ t->sp_t[1]++;+ break;+ }+ int dur = S_archers_ranger_thornDuration * t->sp_lvl[1], most = S_archers_ranger_thornMaxEnemies + 2 * t->sp_lvl[1];+ TowerStat st = tower_stat(t->kind, 4);+ if (t->sp_on != 2) {+ int n = 0;+ for (int i = 0; i < MAX_ENEMIES && n < S_archers_ranger_thornMinEnemies; i++) {+ Enemy *e = &B.en[i];+ if (e->alive && e->active && !e->boss && !e->no_thorn && !e->fighting && !e->flying &&+ ellipse_contains(t->x, t->y - 10, st.range, st.range * RANGE_RATIO, e->x + enemy_stats[e->type].x_adj, e->y + enemy_stats[e->type].y_adj))+ n++;+ }+ if (n >= S_archers_ranger_thornMinEnemies) t->sp_on = 2, t->sp_c = 0;+ } else if (t->sp_c < 34) {+ if (t->sp_c == 17) {+ int n = 0;+ for (int i = 0; i < MAX_ENEMIES && n < most; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || e->boss || e->no_thorn || e->fighting || e->flying) continue;+ if (!ellipse_contains(t->x, t->y - 10, st.range, st.range * RANGE_RATIO, e->x + enemy_stats[e->type].x_adj, e->y + enemy_stats[e->type].y_adj)) continue;+ if (enemy_dodge(e)) continue;+ /* EnemyCommon.thorn: held 18 frames, then the thorns' time, 40 damage each second */+ e->thorned = 1, e->held = 1, e->thorn_t = 0, e->thorn_dmg = S_archers_ranger_thornDamage;+ e->thorn_len = (int16_t)dur, e->thorn_every = S_archers_ranger_thornDamageTime;+ if (++e->thorns >= S_archers_ranger_thornMaxTimes) e->no_thorn = 1;+ n++;+ }+ if (n) t->sp_c++;+ else t->sp_t[1] = 0, t->sp_c = 0, t->sp_on = 0;+ } else+ t->sp_c++;+ } else+ t->sp_t[1] = 0, t->sp_c = 0, t->sp_on = 0;+ }+ if (t->sp_lvl[0]) t->poison_t++;+ break;+ case TW_MUSKETEER:+ if (t->sp_lvl[0]) t->sp_t[0]++;+ if (t->sp_lvl[1]) t->sp_t[1]++;+ break;+ case TW_ARCANE:+ if (t->sp_lvl[0] && t->sp_t[0] < S_mages_arcane_deathRayCoolDown - S_mages_arcane_deathRayLessCoolDown * t->sp_lvl[0]) t->sp_t[0]++;+ if (t->sp_lvl[1]) t->sp_t[1]++;+ break;+ case TW_SORCERER:+ if (t->sp_lvl[1]) t->sp_t[1]++;+ break;+ case TW_BFG:+ if (t->sp_lvl[1]) t->sp_t[1]++;+ if (t->sp_lvl[0]) t->sp_t[0]++;+ break;+ case TW_TESLA:+ break;+ }+}++/* the most advanced enemy in an ellipse around the tower, with a test */+static int furthest(Spot *t, float w, float h, bool (*ok)(const Enemy *)) {+ int best = -1;+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || (ok && !ok(e))) continue;+ if (!ellipse_contains(t->x, t->y - 10, w, h, e->x, e->y)) continue;+ if (best < 0 || path_len(B.en[best].path, B.en[best].lane) - B.en[best].node > path_len(e->path, e->lane) - e->node) best = i;+ }+ return best;+}+static bool not_boss(const Enemy *e) { return !e->boss; }+static bool ground(const Enemy *e) { return !e->flying; }+static bool can_teleport(const Enemy *e) { return !e->boss && !e->no_teleport; }+static bool can_poly(const Enemy *e) { return !e->boss && !e->no_poly && e->type != EN_SHEEPGROUND && e->type != EN_SHEEPFLYING; }++static void face(Spot *t, const Enemy *e) {+ t->shooter_up[0] = t->shooter_up[1] = e->y < t->y;+ t->shooter_flip[0] = t->shooter_flip[1] = e->x < t->x;+ t->shooter_t[0] = t->shooter_t[1] = 0, t->anim_t = 0;+}++/* ------------------------------------------------------------------ musketeers' shots (Bullet: a homing shot) */+static void musket_shot(Spot *t, Enemy *e, int sniper) {+ Bullet *b = bullet_new(BU_SHOT);+ if (!b) return;+ t->arrows++;+ b->x = t->x + (sniper || (t->arrows & 1) == 0 ? -10 : 6), b->y = t->y - 45;+ b->target = (int8_t)(e - B.en);+ b->tx = e->x, b->ty = e->y;+ b->acc = 20, b->level = (uint8_t)sniper;+ b->min_dmg = S_archers_musketeer_minDamage, b->max_dmg = S_archers_musketeer_maxDamage;+ if (up_on(UP_ARCHERS, 5) && rnd() <= 0.1f) b->crit = 1;+ if (up_on(UP_ARCHERS, 3)) b->pierce = 10;+ t->shots++;+}++/* runSpecial: an ability instead of the usual shot (true while it runs) */+bool special_run(int s) {+ Spot *t = &B.spot[s];+ TowerStat st = tower_stat(t->kind, t->level);+ switch (t->kind) {+ case TW_MUSKETEER:+ /* Sniper Shot: 1.5 times the range, the furthest non-boss; seek 5, aim 18, shoot, 18 */+ if (t->sp_lvl[0] && t->sp_on != 2) {+ if (t->sp_on != 1) {+ if (t->sp_t[0] < S_archers_musketeer_sniperCoolDown) goto shrapnel;+ int k = furthest(t, st.range * S_archers_musketeer_sniperRange, st.range * RANGE_RATIO * S_archers_musketeer_sniperRange, not_boss);+ if (k < 0) goto shrapnel;+ t->sp_on = 1, t->sp_c = 0, t->sp_target = (int8_t)k;+ face(t, &B.en[k]);+ return true;+ }+ t->sp_c++;+ Enemy *e = EN(t->sp_target);+ if (!e || !e->active) {+ int k = furthest(t, st.range * S_archers_musketeer_sniperRange, st.range * RANGE_RATIO * S_archers_musketeer_sniperRange, not_boss);+ if (k < 0) {+ t->sp_on = 0, t->sp_c = 0, t->ready = 0;+ return true;+ }+ t->sp_target = (int8_t)k, e = &B.en[k];+ }+ if (t->sp_c == 5 + 18) musket_shot(t, e, t->sp_lvl[0]);+ if (t->sp_c >= 5 + 18 + 18) t->sp_on = 0, t->sp_c = 0, t->ready = 0, t->reload_t = 0, t->sp_t[0] = 0;+ return true;+ }+ shrapnel:+ /* Shrapnel Shot: half the range, ground enemies; 26 frames, at 16 a burst of shrapnel */+ if (t->sp_lvl[1] && t->sp_on != 1) {+ if (t->sp_on != 2) {+ if (t->sp_t[1] < S_archers_musketeer_shrapnelCoolDown) return false;+ int k = furthest(t, st.range * S_archers_musketeer_shrapnelRange, st.range * RANGE_RATIO * S_archers_musketeer_shrapnelRange, ground);+ if (k < 0) return false;+ t->sp_on = 2, t->sp_c = 0, t->ready = 1;+ t->sp_at = enemy_road(&B.en[k], 2);+ face(t, &B.en[k]);+ return true;+ }+ if (++t->sp_c == 16) {+ Pt from = {t->x - 10, t->y - 45};+ for (int a = 0; a <= 360; a += 70) {+ float w = rnd() * 40 + 25, h = (float)(int)(w * 0.7f);+ float rad = (float)(a - 90) * (float)M_PI / 180.0f;+ Bullet *b = bullet_new(BU_BOMB);+ if (!b) break;+ b->x = b->x0 = from.x, b->y = b->y0 = from.y;+ b->tx = t->sp_at.x + cosf(rad) * w / 2, b->ty = t->sp_at.y + sinf(rad) * h / 2;+ b->t1 = (int16_t)ceilf(rnd() * 4 + 4), b->g = 0;+ b->xs = (b->tx - b->x0) / b->t1, b->ys = (b->ty - b->y0) / b->t1;+ b->area = S_archers_musketeer_shrapnelArea;+ b->min_dmg = (int16_t)(S_archers_musketeer_shrapnelMinDamage * t->sp_lvl[1]);+ b->max_dmg = (int16_t)(S_archers_musketeer_shrapnelMaxDamage * t->sp_lvl[1]);+ b->bombs = 7; /* (a shrapnel ball) */+ }+ }+ if (t->sp_c >= 26) t->sp_on = 0, t->sp_c = 0, t->ready = 0, t->reload_t = 0, t->sp_t[1] = 0;+ return true;+ }+ return false;+ case TW_ARCANE:+ /* Teleport: every 10 s, a portal under the furthest enemy */+ if (t->sp_lvl[1]) {+ if (t->sp_on == 2) {+ if (t->sp_c < 20) {+ if (t->sp_c == 4) {+ int lvl = t->sp_lvl[1];+ int most = (S_mages_arcane_teleportMaxEnemies + lvl * S_mages_arcane_teleportExtraEnemies) * lvl;+ int nodes = S_mages_arcane_teleportNodes + lvl * S_mages_arcane_teleportExtraNodes;+ portal_open(t->sp_at, most, nodes);+ }+ t->sp_c++;+ return true;+ }+ t->sp_on = 0, t->sp_c = 0, t->sp_t[1] = 0, t->reload_t = 0;+ return false;+ }+ if (t->sp_t[1] < S_mages_arcane_teleportCoolDown) return false;+ int k = furthest(t, st.range, st.range * RANGE_RATIO, can_teleport);+ if (k >= 0 && !B.en[k].flying) {+ t->sp_on = 2, t->sp_c = 0, t->sp_at = (Pt){B.en[k].x, B.en[k].y};+ face(t, &B.en[k]);+ return true;+ }+ }+ return false;+ case TW_SORCERER:+ /* Polymorph: 25 frames, at 9 the ray */+ if (t->sp_lvl[1]) {+ if (t->sp_on == 2) {+ if (t->sp_c < 25) {+ if (t->sp_c == 9) {+ Enemy *e = EN(t->sp_target);+ if (!e || !e->active) {+ int k = furthest(t, st.range, st.range * RANGE_RATIO, can_poly);+ e = EN(k);+ }+ if (!e) {+ t->sp_on = 0;+ return false;+ }+ Bullet *b = bullet_new(BU_RAY_POLY);+ if (b) b->x0 = t->x - 1.5f, b->y0 = t->y - 62.7f, b->target = (int8_t)(e - B.en), b->t1 = 30, b->level = t->sp_lvl[1];+ }+ t->sp_c++;+ return true;+ }+ t->sp_on = 0, t->sp_c = 0, t->sp_t[1] = 0, t->reload_t = 0;+ return false;+ }+ if (t->sp_t[1] < S_mages_sorcerer_polymorphCoolDown - S_mages_sorcerer_polymorphLessCoolDown * t->sp_lvl[1]) return false;+ int k = furthest(t, st.range, st.range * RANGE_RATIO, can_poly);+ if (k >= 0) {+ t->sp_on = 2, t->sp_c = 0, t->sp_target = (int8_t)k;+ face(t, &B.en[k]);+ return true;+ }+ }+ return false;+ case TW_BFG:+ /* Dragonbreath Launcher: a homing missile, further than the cannon reaches; 29 frames, at 14 */+ if (t->sp_lvl[1] && t->sp_on != 1) {+ if (t->sp_on == 2) {+ if (t->sp_c < 29) {+ if (t->sp_c == 14) {+ Enemy *e = EN(t->sp_target);+ Bullet *b = bullet_new(BU_MISSILE);+ if (b && e) {+ b->x = t->x - 20, b->y = t->y - 42, b->target = (int8_t)(e - B.en), b->level = t->sp_lvl[1];+ b->acc = 8, b->acc_max = 13, b->xs = 0, b->ys = 8, b->tx = e->x, b->ty = e->y;+ }+ }+ t->sp_c++;+ return true;+ }+ t->sp_on = 0, t->sp_c = 0, t->sp_t[1] = 0, t->ready = 0, t->reload_t = 0;+ return false;+ }+ if (t->sp_t[1] >= S_engineers_bfg_missileCoolDown) {+ float r = st.range + st.range * S_engineers_bfg_missileRangeIncrement * t->sp_lvl[1];+ int k = furthest(t, r, r * RANGE_RATIO, 0);+ if (k >= 0) {+ t->sp_on = 2, t->sp_c = 0, t->sp_target = (int8_t)k;+ return true;+ }+ }+ }+ /* Cluster Launcher: a box that bursts over the road into bomblets */+ if (t->sp_lvl[0] && t->sp_on != 2) {+ if (t->sp_on == 1) {+ if (t->sp_c < t->charge_len) {+ if (t->sp_c == t->charge_len - 26) {+ Enemy *e = EN(t->sp_target);+ Bullet *b = bullet_new(BU_BOMB);+ if (b) {+ b->x = b->x0 = t->x, b->y = b->y0 = t->y - 60;+ b->tx = t->sp_at.x, b->ty = t->sp_at.y, b->t1 = 25, b->g = 1;+ b->xs = (b->tx - b->x0) / b->t1, b->ys = (b->ty - b->y0 - b->g * b->t1 * b->t1 / 2) / b->t1;+ b->bombs = 8, b->level = t->sp_lvl[0]; /* (the box) */+ b->area = (int16_t)(e ? e->path : 0), b->pierce = (int16_t)(e ? e->node + 2 : 0);+ }+ }+ t->sp_c++, t->anim_t = (uint16_t)t->sp_c;+ return true;+ }+ t->sp_on = 0, t->sp_c = 0, t->sp_t[0] = 0, t->ready = 0, t->reload_t = 0;+ return false;+ }+ if (t->sp_t[0] >= S_engineers_bfg_clusterCoolDown) {+ int k = furthest(t, st.range, st.range * RANGE_RATIO, ground);+ if (k >= 0) {+ Enemy *e = &B.en[k];+ Pt p = enemy_road(e, enemy_nodes_speed(e));+ t->sp_on = 1, t->sp_c = 0, t->sp_target = (int8_t)k, t->sp_at = (Pt){p.x, p.y - 60};+ return true;+ }+ }+ }+ return false;+ }+ return false;+}++/* the special towers' own shots, at the shot's frame; false: the usual one */+bool special_shoot(int s) {+ Spot *t = &B.spot[s];+ Enemy *e = EN(t->target);+ switch (t->kind) {+ case TW_MUSKETEER:+ if (!e || !e->active) e = EN(tower_find_target(t, true, false));+ if (e) musket_shot(t, e, 0);+ return true;+ case TW_ARCANE: {+ if (!e || !e->active) e = EN(tower_find_target(t, true, false));+ if (!e) return true;+ Bullet *b = bullet_new(BU_RAY_ARCANE);+ if (!b) return true;+ b->x0 = t->x + 1, b->y0 = t->y - 59, b->target = (int8_t)(e - B.en), b->t1 = 10;+ TowerStat st = tower_stat(t->kind, 4);+ b->dmg = (int16_t)rnd_dmg(st.min_dmg, st.max_dmg);+ int cd = S_mages_arcane_deathRayCoolDown - S_mages_arcane_deathRayLessCoolDown * t->sp_lvl[0];+ if (t->sp_lvl[0] && t->sp_t[0] >= cd && !e->boss) { /* the death ray */+ b->kind = BU_RAY_DEATH;+ t->sp_t[0] = 0;+ }+ t->shots++;+ return true;+ }+ case TW_SORCERER: {+ if (!e || !e->active || e->curse_t) {+ /* (an enemy not yet cursed first) */+ int best = -1, cursed = -1;+ TowerStat st = tower_stat(t->kind, 4);+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *o = &B.en[i];+ if (!o->alive || !o->active || !ellipse_contains(t->x, t->y - 10, st.range, st.range * RANGE_RATIO, o->x, o->y)) continue;+ int *slot = o->curse_t ? &cursed : &best;+ if (*slot < 0 || path_len(B.en[*slot].path, B.en[*slot].lane) - B.en[*slot].node > path_len(o->path, o->lane) - o->node) *slot = i;+ }+ e = EN(best >= 0 ? best : cursed);+ }+ if (!e) return true;+ Bullet *b = bullet_new(BU_BOLT_SORCERER);+ if (!b) return true;+ bool down = e->y >= t->y;+ b->x = t->x + (down ? -8 : 8), b->y = t->y - 44, b->target = (int8_t)(e - B.en), b->acc = 1, b->acc_max = 20, b->first = 1;+ b->tx = e->x, b->ty = e->y;+ TowerStat st = tower_stat(t->kind, 4);+ b->min_dmg = st.min_dmg, b->max_dmg = st.max_dmg;+ t->shots++;+ return true;+ }+ case TW_BFG: {+ Bullet *b = bullet_new(BU_BOMB);+ if (!e || !e->active) {+ e = EN(tower_find_target(t, false, true));+ if (e) t->dest = enemy_road(e, enemy_nodes_speed(e));+ } else if (e->blocked)+ t->dest = (Pt){e->x, e->y};+ if (!b) return true;+ TowerStat st = tower_stat(t->kind, 4);+ b->x = b->x0 = t->x, b->y = b->y0 = t->y - 60, b->tx = t->dest.x, b->ty = t->dest.y, b->t1 = 27, b->g = 1;+ b->xs = (b->tx - b->x0) / b->t1, b->ys = (b->ty - b->y0 - b->g * b->t1 * b->t1 / 2) / b->t1;+ b->min_dmg = st.min_dmg, b->max_dmg = st.max_dmg, b->area = st.area, b->level = 4, b->bombs = 9;+ t->shots++;+ return true;+ }+ case TW_TESLA: {+ e = EN(tower_find_target(t, true, false));+ if (!e) return true;+ if (t->sp_lvl[0]) { /* Overcharge: a static field around the tower */+ Bullet *f = bullet_new(BU_FIELD);+ if (f) f->x = t->x, f->y = t->y, f->level = t->sp_lvl[0];+ }+ Bullet *b = bullet_new(BU_RAY_TESLA);+ if (b) {+ b->x0 = t->x + 6, b->y0 = t->y - 62, b->target = (int8_t)(e - B.en), b->t1 = 14;+ b->jump = 1, b->jump_max = (uint8_t)(3 + t->sp_lvl[1]);+ memset(b->hitlist, -1, sizeof b->hitlist);+ TowerStat st = tower_stat(t->kind, 4);+ b->dmg = (int16_t)rnd_dmg(st.min_dmg, st.max_dmg);+ }+ t->shots++;+ return true;+ }+ }+ return false;+}++/* the rangers' poison: every 12 frames an arrow carries it, to an enemy not yet poisoned */+void special_arrow(int s, Bullet *b) {+ Spot *t = &B.spot[s];+ if (t->kind != TW_RANGER || !t->sp_lvl[0]) return;+ if (t->poison_t < S_archers_ranger_poisonCoolDown) return;+ t->poison_t = 0;+ Enemy *e = EN(b->target);+ if (e && e->poison_t) { /* another, not poisoned */+ TowerStat st = tower_stat(t->kind, 4);+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *o = &B.en[i];+ if (o->alive && o->active && !o->poison_t && ellipse_contains(t->x, t->y - 10, st.range, st.range * RANGE_RATIO, o->x, o->y)) {+ b->target = (int8_t)i;+ b->tx = o->x + o->xs * b->t1, b->ty = o->y + o->ys * b->t1 - 5;+ b->xs = (b->tx - b->x0) / b->t1, b->ys = (b->ty - b->y0 - b->g * b->t1 * (b->t1 / 2.0f)) / b->t1;+ break;+ }+ }+ }+ b->bombs = t->sp_lvl[0]; /* (the poison's level) */+}+void special_bolt(int s, Bullet *b) { (void)s, (void)b; }void special_bomb(int s, Bullet *b) { (void)s, (void)b; }-void special_bullet(Bullet *b) { b->alive = 0; }-void special_scorched_earth(float x, float y) { (void)x, (void)y; }-void special_rally(int s, Pt p) { (void)s, (void)p; }-void special_elemental_stats(Soldier *so, int spot) { (void)so, (void)spot; }-bool special_soldier(Soldier *so) { (void)so; return false; }-void special_soldier_stop(Soldier *so) { (void)so; }-int special_soldier_damage(Soldier *so, int dmg) { (void)so; return dmg; }-void special_soldier_attack_anim(Soldier *so) { (void)so; }void special_poison(Enemy *e, int level) {+ if (e->type == EN_SPIDER || e->type == EN_SPIDERSMALL || e->type == EN_SPIDERTINY) return; /* (canPoison false) */if (e->poison_t && e->poison_lvl > level) return;e->poison_lvl = (int16_t)level, e->poison_t = S_archers_ranger_poisonDuration, e->poison_tick = 0;}+static void slow(Enemy *e) {+ if (e->slow_t || e->net_t || e->rage_t) return;+ e->slow_speed = e->speed, e->speed /= 2, e->margin = e->speed + 0.1f;+ e->slow_t = 15;+}+/* Bolt.hitOnTarget: magic damage; Slow Curse halves its speed half a second, Arcane Shatter takes 3 armour */void special_bolt_hit(Bullet *b, Enemy *e) {- bool slow = up_on(UP_MAGES, 5) && !e->rage_t && !e->slow_t && !e->net_t;+ bool slowed = up_on(UP_MAGES, 5) && !e->rage_t && !e->slow_t && !e->net_t;enemy_damage(e, rnd_dmg(b->min_dmg, b->max_dmg), ARM_M, 0);- if (slow && !e->dead) {- e->slow_speed = e->speed, e->speed /= 2, e->margin = e->speed + 0.1f;- e->slow_t = 15;+ if (b->kind == BU_BOLT_SORCERER && !e->dead && !e->curse_t) { /* DebuffCurse */+ e->curse_amt = (int16_t)(e->armor * S_mages_sorcerer_curseArmorReduce / 100);+ e->armor = (int16_t)(e->armor - e->curse_amt);+ e->curse_t = S_mages_sorcerer_curseDuration, e->curse_tick = 0;}+ if (slowed && !e->dead) slow(e);if (up_on(UP_MAGES, 2) && e->active) enemy_armor_down(e, 3);}-void special_bomb_hit(Bullet *b) { bomb_blast(b); }+void special_bomb_hit(Bullet *b) {+ if (b->bombs == 8) { /* the cluster box: bomblets along the road, 7 nodes apart */+ int n = S_engineers_bfg_clusterMinBombs + S_engineers_bfg_clusterIncrementBombs * b->level;+ int span = (n / 2) * 7, t1 = 10, sign = 1, path = b->area, node = b->pierce;+ if (path >= B.npaths) path = 0;+ for (int k = span; k >= -span; k -= 7) {+ if (node + k < path_len(path, 0)) {+ if (node + k < 0) break;+ if (rnd() > 0.5f) sign = -sign;+ Pt p = path_node(path, 0, node + k);+ Bullet *f = bullet_new(BU_BOMB);+ if (f) {+ f->x = f->x0 = b->x, f->y = f->y0 = b->y;+ f->tx = p.x + rnd() * 6 * sign, f->ty = p.y + rnd() * 6 * sign, f->t1 = (int16_t)t1, f->g = 1;+ f->xs = (f->tx - f->x0) / f->t1, f->ys = (f->ty - f->y0 - f->g * f->t1 * f->t1 / 2) / f->t1;+ f->area = S_engineers_bfg_clusterArea, f->min_dmg = S_engineers_bfg_clusterMinDamage, f->max_dmg = S_engineers_bfg_clusterMaxDamage;+ f->bombs = 10, f->level = 4;+ }+ }+ t1++;+ }+ return;+ }+ bomb_blast(b);+}++/* ------------------------------------------------------------------ rays, missiles, fields, the musketeers' shots */+static void enemy_hit_m(Enemy *e, int dmg, bool shielded) {+ if (shielded && e->sp_state == 9) { /* (a shield takes only a ray's first hit) */+ int keep = e->sp_state;+ e->sp_state = 0;+ enemy_damage(e, dmg, ARM_M, 0);+ if (!e->dead) e->sp_state = (int16_t)keep;+ return;+ }+ enemy_damage(e, dmg, ARM_M, 0);+}++void special_bullet(Bullet *b) {+ switch (b->kind) {+ case BU_RAY_ARCANE: case BU_RAY_DEATH: {+ /* RayArcane: its damage a tenth a frame, the rest at the end; the death ray desintegrates at the end */+ Enemy *e = EN(b->target);+ if (b->t0 >= b->t1) {+ b->alive = 0;+ return;+ }+ if (e && e->active) {+ if (b->kind == BU_RAY_DEATH) {+ if (b->t0 + 1 == b->t1) enemy_kill(e, 2);+ } else {+ int d = b->dmg / b->t1;+ if (b->t0 + 1 == b->t1) d += b->dmg - d * b->t1;+ bool slowed = !b->shielded && up_on(UP_MAGES, 5) && !e->rage_t && !e->slow_t && !e->net_t;+ enemy_hit_m(e, d, b->shielded);+ if (slowed && !e->dead) slow(e);+ if (!b->shielded && up_on(UP_MAGES, 2) && e->active) enemy_armor_down(e, 3);+ b->shielded = 1;+ }+ b->tx = e->x + enemy_stats[e->type].x_adj, b->ty = e->y + enemy_stats[e->type].y_adj;+ }+ b->t0++;+ return;+ }+ case BU_RAY_POLY: {+ Enemy *e = EN(b->target);+ if (b->t0 >= b->t1) {+ b->alive = 0;+ return;+ }+ if (e && e->active) {+ b->tx = e->x + enemy_stats[e->type].x_adj, b->ty = e->y + enemy_stats[e->type].y_adj;+ if (b->t0 == 2) polymorph(e);+ }+ b->t0++;+ return;+ }+ case BU_RAY_TESLA: {+ /* RayTesla: 14 frames, at frame 2 an arc to the next enemy (by road) within 80 */+ Enemy *e = EN(b->target);+ if (++b->t0 >= b->t1) {+ b->alive = 0;+ return;+ }+ if (!e || !e->active) return;+ float f = b->jump == 1 || up_on(UP_ENGINEERS, 5) ? 1.0f : b->jump == 2 ? 0.75f : 0.5f;+ int total = (int)(b->dmg * f), d = total / b->t1;+ if (b->t0 + 1 == b->t1) d += total - d * b->t1;+ enemy_damage(e, d, ARM_E, 0);+ if (!e->active) return;+ b->tx = e->x + enemy_stats[e->type].x_adj, b->ty = e->y + enemy_stats[e->type].y_adj;+ if (b->t0 == 2 && b->jump < b->jump_max) {+ for (int k = 0; k < 6; k++)+ if (b->hitlist[k] < 0) {+ b->hitlist[k] = b->target;+ break;+ }+ int best = -1;+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *o = &B.en[i];+ if (!o->alive || !o->active) continue;+ bool seen = false;+ for (int k = 0; k < 6; k++) seen |= b->hitlist[k] == i;+ if (seen) continue;+ float dx = o->x - e->x + enemy_stats[e->type].x_adj, dy = o->y - e->y + enemy_stats[e->type].y_adj;+ if (sqrtf(dx * dx + dy * dy) > S_engineers_tesla_chargedBoltRange) continue;+ if (best < 0 || abs(e->node - o->node) < abs(e->node - B.en[best].node)) best = i;+ }+ if (best >= 0) {+ Bullet *n = bullet_new(BU_RAY_TESLA);+ if (n) {+ *n = *b;+ n->t0 = 0, n->x0 = b->tx, n->y0 = b->ty, n->target = (int8_t)best, n->jump = (uint8_t)(b->jump + 1);+ n->dmg = (int16_t)rnd_dmg(S_engineers_tesla_minDamage, S_engineers_tesla_maxDamage);+ if (up_on(UP_ENGINEERS, 1))+ n->dmg = (int16_t)rnd_dmg(tower_stat(TW_TESLA, 4).min_dmg, tower_stat(TW_TESLA, 4).max_dmg);+ }+ }+ }+ return;+ }+ case BU_FIELD: {+ /* StaticField: grows 15 a frame to the tower's 230, then strikes all inside */+ b->t0 = (int16_t)(b->t0 + 15);+ if (b->t0 >= S_engineers_tesla_overchargeArea) {+ int lo = S_engineers_tesla_overchargeMinDamage + S_engineers_tesla_overchargeDamageIncrement * b->level;+ int hi = S_engineers_tesla_overchargeMaxDamage + S_engineers_tesla_overchargeDamageIncrement * b->level;+ int a = S_engineers_tesla_overchargeArea;+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (e->alive && e->active && ellipse_contains(b->x, b->y - 10, (float)a, a * RANGE_RATIO, e->x, e->y))+ enemy_damage(e, rnd_dmg(lo, hi), ARM_E, 0);+ }+ b->alive = 0;+ }+ return;+ }+ case BU_MISSILE: {+ /* Missile: homing, speeding up a tenth a frame to 13; finds another target if it loses its own */+ Enemy *e = EN(b->target);+ if (!e || !e->active) {+ int best = -1;+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *o = &B.en[i];+ if (!o->alive || !o->active) continue;+ float dx = o->x - b->x, dy = o->y - b->y, d = dx * dx + dy * dy;+ if (best < 0 || d < (B.en[best].x - b->x) * (B.en[best].x - b->x) + (B.en[best].y - b->y) * (B.en[best].y - b->y)) best = i;+ }+ b->target = (int8_t)best;+ e = EN(best);+ }+ if (e) b->tx = e->x, b->ty = e->y;+ if (b->acc < b->acc_max) {+ b->acc += b->acc * 0.1f;+ if (b->acc > b->acc_max) b->acc = b->acc_max;+ }+ float a = atan2f(b->ty - b->y, b->tx - b->x);+ /* (turning, not in a straight line) */+ float cur = atan2f(b->ys, b->xs), da = a - cur;+ while (da > (float)M_PI) da -= 2 * (float)M_PI;+ while (da < -(float)M_PI) da += 2 * (float)M_PI;+ if (da > 0.35f) da = 0.35f;+ if (da < -0.35f) da = -0.35f;+ cur += da;+ b->xs = cosf(cur) * b->acc, b->ys = sinf(cur) * b->acc, b->rot = cur;+ b->x += b->xs, b->y += b->ys;+ float dx = b->tx - b->x, dy = b->ty - b->y;+ if (sqrtf(dx * dx + dy * dy) < b->acc || b->anim_t > 300) {+ b->area = S_engineers_bfg_missileArea;+ b->min_dmg = (int16_t)(S_engineers_bfg_missileMinDamage + S_engineers_bfg_missileIncrementDamage * b->level);+ b->max_dmg = (int16_t)(S_engineers_bfg_missileMaxDamage + S_engineers_bfg_missileIncrementDamage * b->level);+ b->tx = b->x, b->ty = b->y;+ bomb_blast(b);+ b->active = 0, b->fade = 20, b->anim_t = 0;+ }+ return;+ }+ case BU_SHOT: {+ /* Bullet: straight at the target, 20 a frame; a sniper's shot kills outright, or hurts by its life */+ Enemy *e = EN(b->target);+ if (e && e->active) b->tx = e->x + enemy_stats[e->type].x_adj, b->ty = e->y + enemy_stats[e->type].y_adj;+ float a = atan2f(b->ty - b->y, b->tx - b->x);+ b->rot = a;+ b->x += cosf(a) * b->acc, b->y += sinf(a) * b->acc;+ float dx = b->tx - b->x, dy = b->ty - b->y;+ if (sqrtf(dx * dx + dy * dy) < b->acc) {+ if (e && e->active) {+ int dmg = rnd_dmg(b->min_dmg, b->max_dmg);+ if (b->crit) dmg *= 2;+ if (b->level) {+ float pct = (float)(S_archers_musketeer_sniperDamagePercent * b->level);+ if (rnd() < pct / 100.0f) enemy_kill(e, 1);+ else {+ int d = dmg + (int)(pct * e->init_health / 100.0f);+ if (e->health < d) enemy_kill(e, 1);+ else enemy_damage(e, d, ARM_P, 0);+ }+ } else+ enemy_damage(e, dmg, ARM_P, b->pierce);+ }+ b->alive = 0;+ }+ return;+ }+ case BU_AXE: case BU_NET: {+ /* the barbarians' axes (an arc of 20 frames) and nets (homing) */+ Enemy *e = EN(b->target);+ if (b->kind == BU_NET) {+ if (e && e->active) b->tx = e->x, b->ty = e->y - 20;+ float a = atan2f(b->ty - b->y, b->tx - b->x);+ b->x += cosf(a) * 12, b->y += sinf(a) * 12;+ float dx = b->tx - b->x, dy = b->ty - b->y;+ if (sqrtf(dx * dx + dy * dy) < 12) {+ if (e && e->active && !e->net_t && !e->slow_t && !(e->type == EN_ROCKETEER && e->sp_t)) {+ e->slow_speed = e->speed;+ e->speed -= e->speed * (S_soldiers_barbarian_huntingNetsNewSpeed + S_soldiers_barbarian_huntingNetsNewSpeedIncrement * b->level) / 100.0f;+ e->margin = e->speed + 0.1f;+ e->net_t = S_soldiers_barbarian_huntingNetsDuration;+ }+ b->alive = 0;+ }+ return;+ }+ float t = b->t0;+ b->x = b->x0 + t * b->xs, b->y = b->y0 + t * b->ys + t * t / 2;+ if (e && e->active && b->t0 + 2 >= b->t1) {+ const EnemyStat *st = &enemy_stats[e->type];+ if (b->x + 6 > e->x - st->hit_w && b->x - 6 < e->x + st->hit_w && b->y + 4 > e->y - st->hit_h && b->y - 4 < e->y + 4) {+ enemy_damage(e, rnd_dmg(b->min_dmg, b->max_dmg), ARM_P, 0);+ b->alive = 0;+ return;+ }+ }+ if (b->t0 >= b->t1) {+ b->alive = 0;+ return;+ }+ b->t0++;+ return;+ }+ default:+ b->alive = 0;+ }+}++void special_draw_bullet(const Bullet *b, int x, int y) {+ int x0 = 40 + (int)lroundf(b->x0 * 0.4f), y0 = (int)lroundf(b->y0 * 0.4f);+ int tx = 40 + (int)lroundf(b->tx * 0.4f), ty = (int)lroundf(b->ty * 0.4f);+ switch (b->kind) {+ case BU_RAY_ARCANE: case BU_RAY_DEATH:+ gfx_line(x0, y0, tx, ty, b->kind == BU_RAY_DEATH ? RGB(0x9CFF6A) : RGB(0xC27CFF));+ gfx_line(x0 + 1, y0, tx + 1, ty, RGB(0xFFFFFF));+ break;+ case BU_RAY_POLY:+ if (b->t0 < 4) gfx_line(x0, y0, tx, ty, RGB(0x7CFF9A));+ break;+ case BU_RAY_TESLA: {+ /* a jagged bolt */+ int px = x0, py = y0;+ for (int k = 1; k <= 4; k++) {+ int nx = x0 + (tx - x0) * k / 4, ny = y0 + (ty - y0) * k / 4;+ if (k < 4) nx += ((b->t0 + k) & 1) ? 2 : -2, ny += ((b->t0 + k) & 2) ? 1 : -1;+ gfx_line(px, py, nx, ny, (b->t0 & 1) ? RGB(0xBFF4FF) : RGB(0x5AC8FF));+ px = nx, py = ny;+ }+ break;+ }+ case BU_FIELD: {+ int r = (int)lroundf(b->t0 * 0.2f * S_engineers_tesla_overchargeDisplayRatio);+ gfx_ellipse(x, y - 4, r, (int)(r * RANGE_RATIO), RGB(0x8ADCFF), 20);+ break;+ }+ case BU_MISSILE:+ if (b->active) spr_anim(AN_B_MISSILE_START, b->anim_t, true, x, y, b->xs < 0);+ else spr_anim(AN_FX_EXPLODEBIG, b->anim_t, false, x, y, false);+ break;+ case BU_SHOT:+ gfx_line(x, y, x - (int)lroundf(cosf(b->rot) * 3), y - (int)lroundf(sinf(b->rot) * 3), RGB(0xFFF2A0));+ break;+ case BU_AXE:+ spr_anim(AN_B_AXE_IDLE, 0, false, x, y, b->xs < 0);+ break;+ case BU_NET:+ gfx_ellipse(x, y, 3, 2, RGB(0xD8C8A0), 32);+ break;+ }+}++/* ------------------------------------------------------------------ the arcane wizard's portal (TeleportPortal) */+void portal_open(Pt at, int most, int nodes) {+ for (int k = 0; k < (int)(sizeof B.portal / sizeof B.portal[0]); k++)+ if (!B.portal[k].on) {+ B.portal[k].on = 1, B.portal[k].t = 0, B.portal[k].at = at, B.portal[k].left = (int16_t)most, B.portal[k].nodes = (int16_t)nodes;+ return;+ }+}++void portals_step(void) {+ for (int k = 0; k < (int)(sizeof B.portal / sizeof B.portal[0]); k++) {+ if (!B.portal[k].on) continue;+ int t = ++B.portal[k].t;+ /* 5 frames opening, 10 open, 25 closing */+ if (t > 5 && t <= 15 && B.portal[k].left > 0)+ for (int i = 0; i < MAX_ENEMIES && B.portal[k].left > 0; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || e->boss || e->no_teleport || e->teleporting) continue;+ if (!ellipse_contains(B.portal[k].at.x, B.portal[k].at.y, 50, 35, e->x, e->y)) continue;+ int n = B.portal[k].nodes + rnd_round(-3, 6);+ if (e->node - n < 0) n = e->node;+ e->active = 0, e->teleporting = 1, e->teleport_t = 0;+ e->node = (int16_t)(e->node - n);+ if (e->node <= 10) e->node = 10;+ if (e->soldier >= 0) enemy_unblock(e);+ e->held = 0, e->thorned = 0, e->sp_state = e->sp_state == 9 ? 9 : 0;+ if (++e->teleports >= S_mages_arcane_teleportMaxTimes) e->no_teleport = 1;+ B.portal[k].left--;+ }+ if (t > 40) B.portal[k].on = 0;+ }+}++/* EnemyCommon.polymorph: a sheep with half its life, its gold and lives, half again as fast */+void polymorph(Enemy *e) {+ int gold = e->gold, cost = e->cost, h = e->health * S_mages_sorcerer_polymorphDamage / 100;+ int ih = e->init_health * S_mages_sorcerer_polymorphDamage / 100;+ float speed = e->speed * S_mages_sorcerer_polymorphSpeedMultiplier;+ e->gold = 0;+ bool fly = e->flying;+ int path = e->path, lane = e->lane, node = e->node;+ float x = e->x, y = e->y;+ enemy_kill(e, 3);+ e->alive = 0;+ fx_add(fly ? AN_FX_POLYMORPHAIR : AN_FX_POLYMORPHNORMAL, x, y, false, false);+ int i = enemy_spawn(fly ? EN_SHEEPFLYING : EN_SHEEPGROUND, path, lane, node);+ if (i < 0) return;+ Enemy *s = &B.en[i];+ s->x = x, s->y = y, s->health = h < 1 ? 1 : h, s->init_health = ih < 1 ? 1 : ih;+ s->gold = (int16_t)gold, s->cost = (int16_t)cost, s->speed = s->base_speed = speed, s->margin = speed + 0.1f;+ s->active = node >= 10, s->no_poly = 1;+}++/* ------------------------------------------------------------------ soldiers */+void special_elemental_stats(Soldier *so, int spot) {+ Spot *t = &B.spot[spot];+ int l = t->sp_lvl[0] ? t->sp_lvl[0] : 1;+ so->health = so->init_health = (int16_t)(S_mages_sorcerer_elementalHealth + S_mages_sorcerer_elementalExtraHealth * l);+ so->armor = (int16_t)(S_mages_sorcerer_elementalArmor + S_mages_sorcerer_elementalArmorExtra * l);+ so->min_dmg = (int16_t)(S_mages_sorcerer_elementalMinDamage + S_mages_sorcerer_elementalDamageExtra * l);+ so->max_dmg = (int16_t)(S_mages_sorcerer_elementalMaxDamage + S_mages_sorcerer_elementalDamageExtra * l);+ so->regen = S_mages_sorcerer_elementalRegen, so->regen_reload = S_mages_sorcerer_elementalRegenReload;+ so->dead_time = S_mages_sorcerer_elementalRespawnTime, so->reload = S_mages_sorcerer_elementalReload;+ so->range_w = S_mages_sorcerer_elementalRange, so->range_h = (int16_t)(S_mages_sorcerer_elementalRange * RANGE_RATIO);+ so->level = (uint8_t)l;+ if (so->respawning && !so->dead) return;+ /* (it appears as it respawns: dead now, back next frame) */+ if (!so->active && !so->walking) so->dead = 1, so->respawning = 0, so->dead_t = (int16_t)(so->dead_time - 1), so->hidden = 1;+}++void special_elemental_hit(Soldier *so) {+ /* SoldierElemental.areaAttack: 30 ahead of it, up to 4 ground enemies */+ float cx = so->x + (so->left ? -30 : 30), cy = so->y;+ int n = 0, w = S_mages_sorcerer_elementalAreaAttackRangeWidth;+ for (int i = 0; i < MAX_ENEMIES && n < S_mages_sorcerer_elementalAreaAttackMaxEnemies; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || e->flying || !ellipse_contains(cx, cy, (float)w, w * RANGE_RATIO, e->x, e->y)) continue;+ if (!enemy_dodge(e)) enemy_damage(e, rnd_dmg(so->min_dmg, so->max_dmg), ARM_P, 0);+ n++;+ }+}++void special_new_soldier(Soldier *so) {+ if (so->spot < 0) return;+ Spot *t = &B.spot[so->spot];+ BarrackStat b = barrack_stat(t->kind, 4);+ if (so->kind == SK_PALADIN) {+ so->armor = (int16_t)(b.armor + (t->sp_lvl[1] ? S_soldiers_paladin_shieldArmor : 0));+ } else if (so->kind == SK_BARBARIAN) {+ int d = t->sp_lvl[2] * S_soldiers_barbarian_dualWeaponsIncrementDamage;+ so->min_dmg = (int16_t)(b.min_dmg + d), so->max_dmg = (int16_t)(b.max_dmg + d);+ if (t->sp_lvl[2]) so->charge_dmg = 14;+ }+}++static int soldier_range_target(Soldier *so, float w, float h, int min_range, bool fliers_only, bool netless) {+ int best = -1;+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || (fliers_only && !e->flying)) continue;+ if (netless && (e->net_t || e->slow_t || (e->type == EN_ROCKETEER && e->sp_t))) continue;+ if (!ellipse_contains(so->x, so->y, w, h, e->x, e->y)) continue;+ float dx = e->x - so->x, dy = e->y - so->y;+ if (min_range && sqrtf(dx * dx + dy * dy) <= (float)min_range) continue;+ if (fliers_only) return i;+ if (best < 0 || path_len(B.en[best].path, B.en[best].lane) - B.en[best].node > path_len(e->path, e->lane) - e->node) best = i;+ }+ return best;+}++static void enemy_in_range_holy(Soldier *so, int *count, bool strike, int dmg) {+ Spot *t = &B.spot[so->spot];+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || e->flying) continue;+ if (!ellipse_contains(t->rally.x, t->rally.y + 10, so->range_w, so->range_h, e->x, e->y)) continue;+ (*count)++;+ if (strike) enemy_damage(e, dmg, ARM_I, 0);+ }+}++/* SoldierPaladin.runSpecial (healing, holy strike), SoldierBarbarian.runSpecial (nets, axes) */+bool special_soldier(Soldier *so) {+ if (so->spot < 0 || (so->kind != SK_PALADIN && so->kind != SK_BARBARIAN) || !so->active) return false;+ Spot *t = &B.spot[so->spot];+ if (so->kind == SK_PALADIN) {+ int heal_l = t->sp_lvl[0], holy_l = t->sp_lvl[2];+ if (heal_l && so->sp_state != 2) {+ if (so->sp_state != 1) {+ if (so->sp_t < S_soldiers_paladin_healingCoolDown) so->sp_t++;+ else if (so->health * 100 / so->init_health <= 70) {+ so->sp_state = 1, so->sp_c = 0;+ so->anim = AN_SO_PALADIN_HEAL, so->anim_t = 0;+ return true;+ }+ } else {+ if (so->sp_c < 20) {+ so->sp_c++;+ return true;+ }+ int v = rnd_dmg(S_soldiers_paladin_healingMin * heal_l, S_soldiers_paladin_healingMax * heal_l);+ so->health = (int16_t)(so->health + v);+ if (so->health > so->init_health) so->health = so->init_health;+ so->sp_state = 0, so->sp_t = 0;+ so->anim = so->fighting ? AN_SO_PALADIN_IDLE : AN_SO_PALADIN_IDLE, so->anim_t = 0;+ return false;+ }+ }+ if (holy_l && so->sp_state == 2) {+ if (so->sp_c < 31) {+ if (++so->sp_c == 15) {+ int n = 0;+ enemy_in_range_holy(so, &n, true, rnd_dmg(S_soldiers_paladin_holyStrikeMinDamage * holy_l, S_soldiers_paladin_holyStrikeMaxDamage * holy_l));+ }+ return true;+ }+ so->sp_state = 0;+ return false;+ }+ return false;+ }+ /* the barbarians */+ int axe_l = t->sp_lvl[0], net_l = t->sp_lvl[1];+ if (net_l && so->sp_state != 4) {+ if (so->sp_state != 3) {+ if (so->sp_t2 < S_soldiers_barbarian_huntingNetsCoolDown) so->sp_t2++;+ else {+ int r = S_soldiers_barbarian_huntingNetsRange;+ int k = soldier_range_target(so, (float)r, r * RANGE_RATIO, 0, true, true);+ if (k >= 0) {+ so->left = B.en[k].x < so->x;+ so->sp_state = 3, so->sp_c = 0, so->sp_target = (int8_t)k;+ so->anim = AN_SO_BARBARIAN_HUNTING_NETS, so->anim_t = 0;+ return true;+ }+ }+ } else {+ if (so->sp_c < 14) {+ if (++so->sp_c == 7) {+ Enemy *e = EN(so->sp_target);+ if (!e || !e->active) {+ int r = S_soldiers_barbarian_huntingNetsRange;+ e = EN(soldier_range_target(so, (float)r, r * RANGE_RATIO, 0, true, true));+ }+ Bullet *b = e ? bullet_new(BU_NET) : 0;+ if (b) b->x = so->x, b->y = so->y - 12, b->target = (int8_t)(e - B.en), b->level = (uint8_t)net_l, b->tx = e->x, b->ty = e->y;+ }+ return true;+ }+ so->sp_state = 0, so->sp_t2 = 0;+ return false;+ }+ }+ if (axe_l && so->sp_state != 3) {+ int w = S_soldiers_barbarian_throwingRange + axe_l * S_soldiers_barbarian_throwingIncrementRange;+ if (so->sp_state != 4) {+ if (so->sp_t < S_soldiers_barbarian_throwingCoolDown) {+ so->sp_t++;+ return false;+ }+ int k = soldier_range_target(so, (float)w, w * RANGE_RATIO, S_soldiers_barbarian_throwingMinRange, false, false);+ if (k < 0) return false;+ so->left = B.en[k].x < so->x;+ so->sp_at = (Pt){B.en[k].x + enemy_stats[B.en[k].type].x_adj, B.en[k].y + enemy_stats[B.en[k].type].y_adj};+ so->sp_state = 4, so->sp_c = 0, so->sp_target = (int8_t)k;+ so->anim = t->sp_lvl[2] ? AN_SO_BARBARIAN_THROWING_AXE2 : AN_SO_BARBARIAN_THROWING_AXE, so->anim_t = 0;+ return true;+ }+ if (so->sp_c < 14) {+ if (++so->sp_c == 7) {+ Enemy *e = EN(so->sp_target);+ if (!e || !e->active) e = EN(soldier_range_target(so, (float)w, w * RANGE_RATIO, S_soldiers_barbarian_throwingMinRange, false, false));+ Bullet *b = bullet_new(BU_AXE);+ if (b) {+ b->x = b->x0 = so->x, b->y = b->y0 = so->y - 12, b->t1 = 20;+ if (e) {+ b->target = (int8_t)(e - B.en);+ b->tx = e->x + enemy_stats[e->type].x_adj + e->xs * 20 + rnd() * 10;+ b->ty = e->y + enemy_stats[e->type].y_adj + e->ys * 20 + rnd() * 10 - 10;+ } else+ b->tx = so->sp_at.x, b->ty = so->sp_at.y;+ b->xs = (b->tx - b->x0) / b->t1, b->ys = (b->ty - b->y0 - b->t1 * (b->t1 / 2.0f)) / b->t1;+ b->min_dmg = (int16_t)(S_soldiers_barbarian_throwingMinDamage + S_soldiers_barbarian_throwingIncrementDamage * axe_l);+ b->max_dmg = (int16_t)(S_soldiers_barbarian_throwingMaxDamage + S_soldiers_barbarian_throwingIncrementDamage * axe_l);+ }+ }+ return true;+ }+ so->sp_state = 0, so->sp_t = 0;+ return false;+ }+ return false;+}++void special_soldier_stop(Soldier *so) {+ if (so->sp_state) so->sp_state = 0;+}++int special_soldier_damage(Soldier *so, int dmg) {+ (void)so;+ return dmg;+}++bool special_soldier_attack_anim(Soldier *so) {+ if (so->kind == SK_PALADIN) {+ Spot *t = so->spot >= 0 ? &B.spot[so->spot] : 0;+ /* readyToAttack: now and then a holy strike instead (a chance in 10, an enemy in reach) */+ if (t && t->sp_lvl[2] && so->sp_state == 0 && rnd() * 100 <= S_soldiers_paladin_holyStrikeChance) {+ int n = 0;+ enemy_in_range_holy(so, &n, false, 0);+ if (n >= S_soldiers_paladin_holyStrikeMinEnemies) {+ so->sp_state = 2, so->sp_c = 0;+ so->anim = AN_SO_PALADIN_HOLYSTRIKE, so->anim_t = 0;+ return true;+ }+ }+ so->anim = rnd() > 0.5f ? AN_SO_PALADIN_ATTACK2 : AN_SO_PALADIN_ATTACK1, so->anim_t = 0;+ } else if (so->kind == SK_BARBARIAN && so->spot >= 0 && B.spot[so->spot].sp_lvl[2])+ so->anim = AN_SO_BARBARIAN_ATTACK2, so->anim_t = 0;+ return false;+}diff --git a/games/kingdomrush/src/special.h b/games/kingdomrush/src/special.hindex 9749890c..3d15f38e 100644--- a/games/kingdomrush/src/special.h+++ b/games/kingdomrush/src/special.h@@ -39,7 +39,12 @@ void special_elemental_stats(Soldier *so, int spot);bool special_soldier(Soldier *so); /* runSpecial */void special_soldier_stop(Soldier *so);int special_soldier_damage(Soldier *so, int dmg);-void special_soldier_attack_anim(Soldier *so);+bool special_soldier_attack_anim(Soldier *so); /* true: a holy strike instead of the blow */+void portal_open(Pt at, int most, int nodes);+void portals_step(void);+void polymorph(Enemy *e);+void special_elemental_hit(Soldier *so);+void special_new_soldier(Soldier *so); /* a paladin or barbarian gets its tower's abilities */void bomb_blast(Bullet *b);void special_draw_bullet(const Bullet *b, int x, int y);#endifdiff --git a/games/kingdomrush/src/tower.c b/games/kingdomrush/src/tower.cindex 43c7fd0e..6cc67862 100644--- a/games/kingdomrush/src/tower.c+++ b/games/kingdomrush/src/tower.c@@ -376,6 +376,10 @@ static void ice_step(Spot *t) {if (++t->ice_t == 25) t->disabled = 0;if (t->ice_t >= 44) t->ice = 0;}+ /* VeznanBlock: 33 frames closing, held (towerHoldBlockTime), 10 opening */+ else if (t->ice == 4 && ++t->ice_t >= 33) t->ice = 5, t->ice_t = 0;+ else if (t->ice == 5 && ++t->ice_t >= S_enemies_veznan_towerHoldBlockTime) t->ice = 6, t->ice_t = 0;+ else if (t->ice == 6 && ++t->ice_t >= 10) t->ice = 0, t->disabled = 0;}void tower_step(int s) {diff --git a/games/kingdomrush/tests/rules.c b/games/kingdomrush/tests/rules.cindex 960cb6d5..ceb484cc 100644--- a/games/kingdomrush/tests/rules.c+++ b/games/kingdomrush/tests/rules.c@@ -1,10 +1,14 @@-/* Kingdom Rush on a computer: the rules against the game's numbers, a won stage, random keys.+/* Kingdom Rush on a computer: the rules against the game's numbers (GameSettings, Level*.as), stage 1 won by+ * script, every stage and mode played by a simple general, random keys through every screen, the save.** build/rules all the tests- * build/rules shots DIR the screens as PPM pictures in DIR */+ * build/rules shots DIR the screens as PPM pictures in DIR+ * build/rules sizes the battle's memory */+#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>+#include "anim.h"#include "game.h"#include "gfx.h"#include "kr.h"@@ -15,13 +19,15 @@extern const uint8_t kr_data[];const uint8_t *kr_bin;extern uint32_t host_keys, host_time;+extern bool host_storage_full;void host_shot(const char *path);void host_save_dir(const char *d);static uint16_t band[SCREEN_W * BAND_H];-static int fails;+static int fails, checks;#define CHECK(c, ...) \do { \+ checks++; \if (!(c)) { \fails++; \fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \@@ -39,6 +45,209 @@ static void press(uint32_t k) {step(0);}+/* ------------------------------------------------------------------ the numbers */+static void test_numbers(void) {+ memset(upgrades, 0, sizeof upgrades);+ battle_start(0, MODE_CAMPAIGN);+ /* GameSettings: towers */+ TowerStat a = tower_stat(TW_ARCHER, 1);+ CHECK(a.cost == 70 && a.range == 220 && a.min_dmg == 4 && a.max_dmg == 6 && a.reload == 24, "archers level 1");+ a = tower_stat(TW_MAGE, 3);+ CHECK(a.cost == 240 && a.range == 280 && a.min_dmg == 40 && a.max_dmg == 74 && a.reload == 45, "mages level 3");+ a = tower_stat(TW_ENGINEER, 2);+ CHECK(a.cost == 220 && a.area == 70 && a.min_dmg == 20 && a.max_dmg == 39 && a.reload == 90, "artillery level 2");+ a = tower_stat(TW_TESLA, 4);+ CHECK(a.cost == 375 && a.range == 260 && a.min_dmg == 55 && a.max_dmg == 105, "tesla");+ BarrackStat b = barrack_stat(TW_BARRACKS, 3);+ CHECK(b.cost == 160 && b.health == 150 && b.armor == 30 && b.min_dmg == 6 && b.max_dmg == 10 && b.respawn == 300, "knights");+ b = barrack_stat(TW_PALADIN, 4);+ CHECK(b.health == 200 && b.armor == 50 && b.respawn == 420, "paladins");+ /* enemies */+ CHECK(enemy_stats[EN_GOBLIN].health == 20 && enemy_stats[EN_GOBLIN].gold == 3 && fabsf(enemy_stats[EN_GOBLIN].speed - 1.2f) < 1e-6f, "goblin");+ CHECK(enemy_stats[EN_FATORC].armor == 30 && enemy_stats[EN_FATORC].health == 80, "orc");+ CHECK(enemy_stats[EN_DARKKNIGHT].armor == 80 && enemy_stats[EN_SHAMAN].magic_armor == 85, "armours");+ CHECK(enemy_stats[EN_GARGOYLE].flying && !enemy_stats[EN_GARGOYLE].blockable, "fliers can't be blocked");+ CHECK(enemy_stats[EN_JUGGERNAUT].boss && enemy_stats[EN_JUGGERNAUT].health == 10000, "the Juggernaut");+ /* Level1.as: 20 lives, 265 gold, 8 spots, 7 waves, one tower level */+ CHECK(B.lives == 20 && B.cash == 265 && B.nspots == 8 && B.max_waves == 7, "stage 1: %d lives %d gold %d spots %d waves", B.lives, B.cash, B.nspots, B.max_waves);+ CHECK(tower_locked(0, 2), "stage 1 has level 1 towers only");+ CHECK(B.npaths == 1 && B.nlanes[0] == 3 && path_len(0, 0) == 203, "stage 1's road");+ /* the armour: physical damage less armour %, explosions half of it, magic the magic armour */+ int i = enemy_spawn(EN_FATORC, 0, 0, 20);+ Enemy *e = &B.en[i];+ enemy_damage(e, 10, ARM_P, 0);+ CHECK(e->health == 73, "10 physical on 30 armour: 7 (%d left)", e->health);+ enemy_damage(e, 10, ARM_E, 0);+ CHECK(e->health == 64, "10 explosive on 30 armour: 8.5, cut (%d left)", e->health);+ enemy_damage(e, 10, ARM_M, 0);+ CHECK(e->health == 54, "10 magic: 10 (%d left)", e->health);+ /* selling: 60% back */+ battle_start(0, MODE_CAMPAIGN);+ spot_build(0, TW_ARCHER);+ CHECK(B.cash == 195 && spot_sell_value(0) == 42, "an archer tower: 70, sold for 42");+ spot_sell(0);+ CHECK(B.cash == 237 && B.spot[0].kind == TW_NONE, "sold");+ /* the stars: 3 at 18 lives or more, 2 above 5, else 1 */+ B.lives = 18;+ CHECK(battle_stars() == 3, "3 stars");+ B.lives = 6;+ CHECK(battle_stars() == 2, "2 stars");+ B.lives = 5;+ CHECK(battle_stars() == 1, "1 star");+ /* the upgrades: Eagle Eye +10% range (ceil), Hermetic Study mages 10% cheaper */+ memset(upgrades, 0, sizeof upgrades);+ upgrades[UP_ARCHERS] = 2, upgrades[UP_MAGES] = 3;+ battle_start(1, MODE_CAMPAIGN);+ CHECK(tower_stat(TW_ARCHER, 1).range == 242, "eagle eye: %d", tower_stat(TW_ARCHER, 1).range);+ CHECK(tower_stat(TW_MAGE, 1).cost == 90 && tower_stat(TW_MAGE, 1).range == 242, "hermetic study, spell reach");+ memset(upgrades, 0, sizeof upgrades);+ /* waves: the first waits for its call, an early call pays a gold a second left */+ battle_start(0, MODE_CAMPAIGN);+ for (int t = 0; t < 40; t++) battle_step();+ CHECK(B.waiting_first && B.flags_up, "the first wave waits");+ int cash = B.cash;+ CHECK(battle_call_wave(), "called");+ CHECK(B.cash == cash, "no gold for the first call");+ for (int t = 0; t < 30 * 8; t++) battle_step();+ CHECK(B.current_wave == 1, "wave 1 under way");+ int guard = 0;+ while (!B.flags_up && guard++ < 30 * 60) battle_step();+ CHECK(B.flags_up, "the next wave's flags");+ int left = battle_wave_seconds_left();+ cash = B.cash;+ battle_call_wave();+ CHECK(B.cash - cash == left || B.cash - cash == left - 1, "early call: +%d for %d s", B.cash - cash, left);+ /* heroic and iron: 1 life */+ battle_start(4, MODE_HEROIC);+ CHECK(B.lives == 1 && B.max_waves == 6, "heroic: 1 life, 6 waves (%d)", B.max_waves);+ battle_start(4, MODE_IRON);+ CHECK(B.lives == 1 && B.max_waves == 1, "iron: one wave (%d)", B.max_waves);+}++/* ------------------------------------------------------------------ a general */+static int bot_style;+static void general(void) {+ /* spells: on the enemy furthest along */+ int best = -1;+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (!e->alive || !e->active || e->dead) continue;+ if (best < 0 || path_len(e->path, e->lane) - e->node < path_len(B.en[best].path, B.en[best].lane) - B.en[best].node) best = i;+ }+ if (best >= 0) {+ Pt at = {B.en[best].x, B.en[best].y};+ Enemy *e = &B.en[best];+ if (power_fire_cooldown() == 0) power_fire(at);+ /* (soldiers where the towers reach: not at the road's start) */+ if (power_reinf_cooldown() == 0 && e->node > path_len(e->path, e->lane) / 3) power_reinforce(at);+ }+ static const uint8_t mix[4] = {TW_ARCHER, TW_BARRACKS, TW_MAGE, TW_ENGINEER};+ for (int s = 0; s < B.nspots; s++) {+ Spot *t = &B.spot[s];+ if (t->kind == 0xFF || t->building || t->disabled) continue;+ if (t->kind == TW_NONE) {+ int k = mix[(s + bot_style) % 4];+ int c = spot_cost(s, (int[]){0, 1, 2, 3, 4}[k == TW_ARCHER ? 1 : k == TW_BARRACKS ? 2 : k == TW_MAGE ? 3 : 4]);+ if (c > 0 && c <= B.cash) spot_build(s, k);+ continue;+ }+ if (t->level < 3) {+ int c = spot_cost(s, 0);+ if (c > 0 && c <= B.cash - 40) spot_upgrade(s, 0);+ } else if (t->level == 3) {+ int ch = 1 + ((s + bot_style) & 1), c = spot_cost(s, ch);+ if (c > 0 && c <= B.cash - 40) spot_upgrade(s, ch);+ } else+ for (int k = 0; k < 3; k++) {+ int c = spot_ability_cost(s, k);+ if (c > 0 && c <= B.cash - 60) spot_buy_ability(s, k);+ }+ if (t->ice == 2) t->ice_hits++, t->ice = t->ice_hits >= 3 ? 3 : 2; /* break J.T.'s ice */+ }+}++static int play_battle(int level, int mode, bool full_upgrades, int *lives) {+ memset(upgrades, full_upgrades ? 5 : 0, sizeof upgrades);+ battle_seed = (uint32_t)(level * 31 + mode * 7 + bot_style);+ battle_start(level, mode);+ for (int t = 0; t < 40; t++) battle_step();+ battle_call_wave();+ for (int t = 0; t < 30 * 60 * 20 && B.state != LS_WIN && B.state != LS_OVER; t++) {+ battle_step();+ if (t % 15 == 0) general();+ if (B.pending_note) B.pending_note = 0;+ if (B.flags_up && mode == MODE_IRON) battle_call_wave();+ }+ *lives = B.lives;+ return B.state;+}++static void test_level1(void) {+ int lives;+ bot_style = 0;+ int st = play_battle(0, MODE_CAMPAIGN, false, &lives);+ CHECK(st == LS_WIN, "stage 1 won by script (state %d)", st);+ CHECK(lives >= 18, "with 3 stars (%d lives)", lives);+}++static void test_all(void) {+ int won = 0, total = 0;+ for (int l = 0; l < NLEVELS; l++)+ for (int m = 0; m < 3; m++) {+ int lives;+ bot_style = l + m;+ int st = play_battle(l, m, true, &lives);+ CHECK(st == LS_WIN || st == LS_OVER, "stage %d mode %d ended (state %d)", l + 1, m, st);+ printf(" stage %2d %-8s %s, %d lives, %d kills\n", l + 1, m == 0 ? "campaign" : m == 1 ? "heroic" : "iron",+ st == LS_WIN ? "won" : "lost", lives, B.kills);+ won += st == LS_WIN, total++;+ }+ printf(" the general won %d of %d\n", won, total);+}++/* ------------------------------------------------------------------ random keys */+static uint32_t fz = 12345;+static uint32_t fuzz_rand(void) {+ fz ^= fz << 13, fz ^= fz >> 17, fz ^= fz << 5;+ return fz;+}++static void test_fuzz(int sessions) {+ static const uint32_t keys[] = {K_LEFT, K_RIGHT, K_UP, K_DOWN, K_OK, K_OK, K_OK, K_BACK, K_DEL, K_DIGIT(0),+ K_DIGIT(1), K_DIGIT(2), K_DIGIT(3), K_EXE, 0, 0, 0};+ for (int s = 0; s < sessions; s++) {+ game_init();+ /* every stage open, stars to spend */+ for (int i = 0; i < NLEVELS; i++) g.p.camp[i] = 3;+ for (int t = 0; t < 4000 && g.running; t++) {+ uint32_t k = keys[fuzz_rand() % (sizeof keys / sizeof keys[0])];+ step(k);+ if (t % 97 == 0) game_draw();+ }+ }+ CHECK(1, "fuzz");+}++/* ------------------------------------------------------------------ the save */+static void test_save(void) {+ game_init();+ g.p.camp[0] = 3, g.p.camp[1] = 2, g.p.heroic[0] = 1, g.p.ups[UP_RAIN] = 2;+ save_progress();+ memset(&g.p, 0, sizeof g.p);+ load_progress();+ CHECK(g.p.camp[0] == 3 && g.p.camp[1] == 2 && g.p.heroic[0] == 1 && g.p.ups[UP_RAIN] == 2, "the save comes back");+ FILE *f = fopen("build/host-saves/kingdomrush.sav", "r+b");+ if (f) {+ fseek(f, 6, SEEK_SET);+ fputc(0x77, f);+ fclose(f);+ }+ load_progress();+ CHECK(g.p.camp[0] == 0 && g.p.difficulty == 1, "a broken save: a fresh start");+ CHECK(stars_won() == 0, "no stars");+ remove("build/host-saves/kingdomrush.sav");+}+int main(int argc, char **argv) {kr_bin = kr_data;gfx_set_band_memory(band);@@ -51,28 +260,19 @@ int main(int argc, char **argv) {MAX_ENEMIES, sizeof(Soldier), MAX_SOLDIERS, sizeof(Bullet), MAX_BULLETS, sizeof(Fx), MAX_FX, sizeof(Spot));return 0;}- if (argc > 2 && !strcmp(argv[1], "fight")) {- /* level 1 by the rules alone: archers on every spot, waves called, a picture now and then */- char p[512];- memset(g.p.seen_note, 0xFF, sizeof g.p.seen_note);- play_start(0, MODE_CAMPAIGN);- for (int i = 0; i < 40; i++) step(0);- for (int s = 0; s < B.nspots && s < 3; s++) spot_build(s, s == 1 ? TW_BARRACKS : TW_ARCHER);- battle_call_wave();- for (int t = 0; t < 30 * 300 && B.state != LS_WIN && B.state != LS_OVER; t++) {- step(0);- if (B.flags_up) battle_call_wave();- for (int s = 0; s < B.nspots; s++)- if (B.spot[s].kind == TW_NONE && B.cash >= 100) spot_build(s, s % 3 == 1 ? TW_MAGE : TW_ARCHER);- else if (B.spot[s].kind != TW_NONE && B.spot[s].level < 3 && spot_cost(s, 0) > 0 && spot_cost(s, 0) <= B.cash - 50) spot_upgrade(s, 0);- if (t % 600 == 300) {- game_draw();- snprintf(p, sizeof p, "%s/fight%d.ppm", argv[2], t / 600);- host_shot(p);- }+ if (argc > 3 && !strcmp(argv[1], "debug")) { /* a stage that does not end: who is left */+ int lives;+ bot_style = atoi(argv[2]) + atoi(argv[3]);+ int st = play_battle(atoi(argv[2]), atoi(argv[3]), true, &lives);+ printf("state %d lives %d wave %d/%d index %d/%d active waves %d flags %d\n", st, lives, B.current_wave, B.max_waves,+ B.index_waves, B.nwaves, B.nactive, B.flags_up);+ for (int i = 0; i < MAX_ENEMIES; i++) {+ Enemy *e = &B.en[i];+ if (e->alive)+ printf(" #%d %s node %d/%d active %d dead %d held %d blocked %d fighting %d sp %d tele %d thorn %d resp %d froze %d hp %d speed %.2f slow %d net %d (%.0f,%.0f)\n",+ i, enemy_name(e->type), e->node, path_len(e->path, e->lane), e->active, e->dead, e->held, e->blocked, e->fighting,+ e->sp_state, e->teleporting, e->thorned, e->respawn_t, e->froze, (int)e->health, e->speed, e->slow_t, e->net_t, e->x, e->y);}- printf("state %d lives %d cash %d wave %d/%d kills %d\n", B.state, B.lives, B.cash, B.current_wave, B.max_waves, B.kills);- for (int s = 0; s < B.nspots; s++) printf("spot %d kind %d level %d shots %d\n", s, B.spot[s].kind, B.spot[s].level, B.spot[s].shots);return 0;}if (argc > 2 && !strcmp(argv[1], "shots")) {@@ -85,13 +285,40 @@ int main(int argc, char **argv) {snprintf(p, sizeof p, "%s/map.ppm", argv[2]);host_shot(p);press(K_OK);+ game_draw();+ snprintf(p, sizeof p, "%s/stage.ppm", argv[2]);+ host_shot(p);press(K_OK);for (int i = 0; i < 40; i++) step(0);+ press(K_OK);game_draw();- snprintf(p, sizeof p, "%s/battle.ppm", argv[2]);+ snprintf(p, sizeof p, "%s/ring.ppm", argv[2]);host_shot(p);+ /* a stage well under way, every upgrade bought, towers at their best */+ memset(g.p.ups, 5, sizeof g.p.ups);+ memset(g.p.seen_note, 0xFF, sizeof g.p.seen_note);+ int lvl = argc > 3 ? atoi(argv[3]) : 5;+ play_start(lvl, MODE_CAMPAIGN);+ for (int i = 0; i < 40; i++) step(0);+ battle_call_wave();+ bot_style = 1;+ for (int t = 0; t < 30 * 150; t++) {+ step(0);+ if (t % 15 == 0) general();+ if (B.flags_up && t > 30 * 60) battle_call_wave();+ if (t % 450 == 449) {+ game_draw();+ snprintf(p, sizeof p, "%s/fight%d.ppm", argv[2], t / 450);+ host_shot(p);+ }+ }return 0;}- printf("%s\n", fails ? "FAILED" : "ok");+ test_numbers();+ test_level1();+ test_save();+ test_fuzz(argc > 1 && !strcmp(argv[1], "quick") ? 20 : 150);+ if (!(argc > 1 && !strcmp(argv[1], "quick"))) test_all();+ printf("%d checks, %s\n", checks, fails ? "FAILED" : "ok");return fails != 0;}