Knight fortresses: knight towers, patrols, alarm, claimable knight ground and vaults
- fortress.rs: fortress builder (masonry walls with doors, courtyard, corner towers, vault chambers full of mixed treasure, shard chamber, gate of the order as the heart, guards, patrols) - Knight towers shoot the player's units; creatures tear down knight towers within reach - Patrols walk their round; guards and patrols that spot an enemy alarm the others nearby - Imps claim knight ground (slower, not next to fighting heroes); vaults hand over their treasure, shard chambers give the shard back; claiming alarms the knights - Knights tear down the player's bridges near their posts - Fix: units next to a tower could stop at the tile edge just out of melee reach - Tests for towers, vault looting, patrols/alarm and bridge tearing Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7e84cae910
commit
e15b16cc60
7 files changed
+565
-23
No files matched your search
+360
-15
@@ -153,6 +153,7 @@ pub enum Job {
|
||||
Carry(u32), // imp carries an unconscious hero (id) to the torture chamber
|
||||
Pray(usize), // temple tile
|
||||
HealCore, // flame priestess heals the soul core
|
||||
TearBridge(usize), // knight tears down a bridge of the player
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
@@ -205,6 +206,10 @@ pub struct Ent {
|
||||
pub guard: bool, // stays at its home (gate defenders, treasure guards)
|
||||
#[serde(default)]
|
||||
pub home: Option<(f32, f32)>, // guard post
|
||||
#[serde(default)]
|
||||
pub patrol: Vec<(i32, i32)>, // fortress patrol: route of tiles, walked in a loop
|
||||
#[serde(default)]
|
||||
pub patrol_k: usize, // next point of the route
|
||||
}
|
||||
|
||||
impl Ent {
|
||||
@@ -248,6 +253,8 @@ impl Ent {
|
||||
stun: 0.0,
|
||||
guard: false,
|
||||
home: None,
|
||||
patrol: Vec::new(),
|
||||
patrol_k: 0,
|
||||
}
|
||||
}
|
||||
pub fn tile(&self) -> usize {
|
||||
@@ -326,6 +333,14 @@ pub struct Game {
|
||||
pub training_bill: f32, // training costs, paid on the next payday
|
||||
#[serde(default)]
|
||||
pub payday_warned: bool,
|
||||
#[serde(default)]
|
||||
pub loot: f32, // value taken from knight vaults
|
||||
#[serde(default)]
|
||||
pub shards_taken: u32,
|
||||
#[serde(default)]
|
||||
pub towers_razed: u32, // knight towers destroyed
|
||||
#[serde(skip)]
|
||||
pub contested: Vec<bool>, // tiles next to fighting heroes: knight ground there cannot be claimed
|
||||
#[serde(skip)]
|
||||
pub hints: bool, // show hint messages (setting)
|
||||
#[serde(skip, default = "hint_times")]
|
||||
@@ -426,6 +441,10 @@ impl Game {
|
||||
incoming: Vec::new(),
|
||||
training_bill: 0.0,
|
||||
payday_warned: false,
|
||||
loot: 0.0,
|
||||
shards_taken: 0,
|
||||
towers_razed: 0,
|
||||
contested: Vec::new(),
|
||||
};
|
||||
let (cx, cy) = g.map.core;
|
||||
// the soul core starts surrounded by a one tile wide ring of treasury
|
||||
@@ -504,7 +523,7 @@ impl Game {
|
||||
}
|
||||
}
|
||||
// towers count with half their combat value
|
||||
for t in &self.towers {
|
||||
for t in self.towers.iter().filter(|t| !t.hero) {
|
||||
h += 0.5 * t.max_hp();
|
||||
d += 0.5 * t.dmg() / tower_stats(t.kind).interval;
|
||||
}
|
||||
@@ -799,6 +818,7 @@ impl Game {
|
||||
self.gate_hp = (self.gate_hp + 8.0 * dt).min(GATE_HP);
|
||||
}
|
||||
|
||||
self.update_contested();
|
||||
for i in 0..self.ents.len() {
|
||||
if !self.ents[i].alive || self.ents[i].held {
|
||||
continue;
|
||||
@@ -1387,6 +1407,7 @@ impl Game {
|
||||
e.path.clear();
|
||||
e.guard = false;
|
||||
e.home = None;
|
||||
e.patrol.clear();
|
||||
let b = st(kind).bounty as f32;
|
||||
let got = self.add_gold(b);
|
||||
self.fx(5, x, y, 0.0, 0.0, 1.0, 0xff30d0ff);
|
||||
@@ -1546,6 +1567,126 @@ impl Game {
|
||||
}
|
||||
}
|
||||
|
||||
// tiles within two of a fighting hero
|
||||
fn update_contested(&mut self) {
|
||||
let n = self.map.t.len();
|
||||
if self.contested.len() != n {
|
||||
self.contested = vec![false; n];
|
||||
} else {
|
||||
self.contested.iter_mut().for_each(|c| *c = false);
|
||||
}
|
||||
for e in &self.ents {
|
||||
if !e.alive || e.held || !e.active_hero() {
|
||||
continue;
|
||||
}
|
||||
let (x, y) = (e.x as i32, e.y as i32);
|
||||
for dy in -2..=2 {
|
||||
for dx in -2..=2 {
|
||||
if inb(x + dx, y + dy) {
|
||||
self.contested[idx(x + dx, y + dy)] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a knight tile was claimed: vaults hand over their treasure, shards come back
|
||||
fn take_knight_tile(&mut self, i: usize) {
|
||||
let t = self.map.t[i];
|
||||
let (x, y) = (xy(i).0 as f32 + 0.5, xy(i).1 as f32 + 0.5);
|
||||
if t.kind == Kind::Treasury && t.gold > 0 {
|
||||
// the content stays in the (now own) treasury tile and counts as own gold
|
||||
self.gold += t.gold as f32;
|
||||
self.loot += t.gold as f32;
|
||||
self.fx(5, x, y, 0.0, 0.0, 1.0, 0xff30d0ff);
|
||||
self.sfx(Sfx::Payday, x, y);
|
||||
self.msg("msg.vault_taken", vec![t.gold.to_string()]);
|
||||
}
|
||||
if t.kind == Kind::Shard {
|
||||
self.shards_taken += 1;
|
||||
self.fx(7, x, y, 0.0, 0.0, 1.2, 0xff6060ff);
|
||||
self.sfx(Sfx::LevelUp, x, y);
|
||||
self.msg("msg.shard_taken", vec![]);
|
||||
}
|
||||
}
|
||||
|
||||
// a fortress unit spotted an enemy: guards and patrols nearby join the fight
|
||||
fn alarm(&mut self, x: f32, y: f32, target: u32) {
|
||||
for o in self.ents.iter_mut() {
|
||||
if !o.alive || o.held || !o.active_hero() || !(o.guard || !o.patrol.is_empty()) {
|
||||
continue;
|
||||
}
|
||||
if matches!(o.job, Job::Attack(_)) || (o.x - x).powi(2) + (o.y - y).powi(2) > 13.0 * 13.0 {
|
||||
continue;
|
||||
}
|
||||
o.job = Job::Attack(target);
|
||||
o.path.clear();
|
||||
o.repath = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
// fortress units: a bridge of the player near the post (None if none)
|
||||
fn bridge_to_tear(&self, e: &Ent) -> Option<usize> {
|
||||
let (hx, hy) = e.home.unwrap_or((e.x, e.y));
|
||||
let (cx, cy) = (hx as i32, hy as i32);
|
||||
for r in 0..=9i32 {
|
||||
for dy in -r..=r {
|
||||
for dx in -r..=r {
|
||||
if dx.abs() != r && dy.abs() != r {
|
||||
continue;
|
||||
}
|
||||
if let Some(t) = self.map.get(cx + dx, cy + dy) {
|
||||
if t.bridge() && t.owner == Owner::Player {
|
||||
return Some(idx(cx + dx, cy + dy));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// a knight walks next to the bridge tile and tears it down
|
||||
fn tear_bridge(&mut self, e: &mut Ent, i: usize, dt: f32) -> bool {
|
||||
let t = self.map.t[i];
|
||||
if !t.bridge() || t.owner != Owner::Player {
|
||||
return false;
|
||||
}
|
||||
let (bx, by) = xy(i);
|
||||
let cur = e.tile();
|
||||
let (cx, cy) = xy(cur);
|
||||
if (cx - bx).abs() + (cy - by).abs() == 1 {
|
||||
e.path.clear();
|
||||
e.working = true;
|
||||
e.face = sgn(bx as f32 + 0.5 - e.x);
|
||||
e.timer += dt;
|
||||
if e.timer >= 4.0 {
|
||||
e.timer = 0.0;
|
||||
let lava = t.kind == Kind::LavaBridge;
|
||||
let tl = &mut self.map.t[i];
|
||||
tl.kind = if lava { Kind::Lava } else { Kind::Water };
|
||||
tl.owner = Owner::None;
|
||||
tl.reserved = 0;
|
||||
self.ground_lost(i);
|
||||
self.fx(6, bx as f32 + 0.5, by as f32 + 0.5, 0.0, 0.0, 0.6, 0xff3a5a7a);
|
||||
self.sfx(Sfx::DigDone, bx as f32 + 0.5, by as f32 + 0.5);
|
||||
self.msg("msg.bridge_torn", vec![]);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if e.path.is_empty() && e.repath <= 0.0 {
|
||||
e.repath = 1.5;
|
||||
e.timer = 0.0;
|
||||
match self.map.search(cur, self.map.walk_cost(), |j| j != i && adj4(i, j), true) {
|
||||
Some(p) => e.path = p,
|
||||
None => return false,
|
||||
}
|
||||
}
|
||||
self.step_path(e, dt);
|
||||
true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- pit imps (workers)
|
||||
|
||||
fn dig_neighbor(&self, i: usize, me: u32) -> Option<usize> {
|
||||
@@ -1571,7 +1712,7 @@ impl Game {
|
||||
let (x, y) = xy(i);
|
||||
self.towers.iter().find(|t| {
|
||||
let (tx, ty) = xy(t.tile);
|
||||
(tx - x).abs() <= 1 && (ty - y).abs() <= 1 && t.hp < t.max_hp() - 1.0
|
||||
!t.hero && (tx - x).abs() <= 1 && (ty - y).abs() <= 1 && t.hp < t.max_hp() - 1.0
|
||||
}).map(|t| t.id)
|
||||
}
|
||||
|
||||
@@ -1593,7 +1734,13 @@ impl Game {
|
||||
|
||||
fn claimable(&self, i: usize, me: u32) -> bool {
|
||||
let t = &self.map.t[i];
|
||||
if t.owner != Owner::None || !matches!(t.kind, Kind::Floor | Kind::Portal | Kind::Bridge | Kind::LavaBridge | Kind::Shard) {
|
||||
let ok = match t.owner {
|
||||
Owner::None => matches!(t.kind, Kind::Floor | Kind::Portal | Kind::Bridge | Kind::LavaBridge | Kind::Shard),
|
||||
// knight ground (also vaults and shard chambers), but not while heroes fight there
|
||||
Owner::Hero => matches!(t.kind, Kind::Floor | Kind::Treasury | Kind::Bridge | Kind::LavaBridge | Kind::Shard) && !self.contested.get(i).copied().unwrap_or(false),
|
||||
Owner::Player => false,
|
||||
};
|
||||
if !ok {
|
||||
return false;
|
||||
}
|
||||
if t.reserved != 0 && t.reserved != me {
|
||||
@@ -1745,10 +1892,16 @@ impl Game {
|
||||
}
|
||||
e.working = true;
|
||||
e.timer += dt;
|
||||
if e.timer >= 1.0 {
|
||||
// knight ground takes longer
|
||||
let knight = self.map.t[t].owner == Owner::Hero;
|
||||
if e.timer >= if knight { 3.0 } else { 1.0 } {
|
||||
self.unreserve(me);
|
||||
e.job = Job::None;
|
||||
let (tx, ty) = xy(t);
|
||||
if knight {
|
||||
self.take_knight_tile(t);
|
||||
self.alarm(tx as f32 + 0.5, ty as f32 + 0.5, e.id);
|
||||
}
|
||||
if let (Kind::Portal, Some(pk)) = (self.map.t[t].kind, self.map.portal_at(tx, ty)) {
|
||||
let (px, py) = self.map.portals[pk];
|
||||
for y in 0..3 {
|
||||
@@ -1929,9 +2082,24 @@ impl Game {
|
||||
e.job = Job::Attack(id);
|
||||
e.path.clear();
|
||||
e.repath = 0.0;
|
||||
} else if !matches!(e.job, Job::AttackTower(_) | Job::Sleep(_) | Job::Eat(_)) {
|
||||
// knight towers in reach are torn down
|
||||
if let Some(id) = self.enemy_tower_near(e.x, e.y, radius - 1.0) {
|
||||
self.unreserve(me);
|
||||
e.job = Job::AttackTower(id);
|
||||
e.path.clear();
|
||||
e.repath = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Job::AttackTower(id) = e.job {
|
||||
if !self.attack_tower(e, id, dt) {
|
||||
e.job = Job::None;
|
||||
e.path.clear();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if e.kind == EK::Chainbreaker {
|
||||
// restless: without a fight for two minutes its mood sinks (skill_cd counts down as idle time)
|
||||
if matches!(e.job, Job::Attack(_)) {
|
||||
@@ -2489,15 +2657,27 @@ impl Game {
|
||||
e.cd = 2.5;
|
||||
}
|
||||
}
|
||||
let fort = e.guard || !e.patrol.is_empty();
|
||||
if !matches!(e.job, Job::Attack(_)) {
|
||||
let radius = if e.guard { 8.0 } else { 6.5 };
|
||||
let radius = if fort { 8.0 } else { 6.5 };
|
||||
if let Some(id) = self.nearest_enemy(e, radius) {
|
||||
e.job = Job::Attack(id);
|
||||
e.path.clear();
|
||||
e.repath = 0.0;
|
||||
} else if !matches!(e.job, Job::AttackTower(_)) {
|
||||
if fort {
|
||||
self.alarm(e.x, e.y, id);
|
||||
}
|
||||
} else if e.guard && !matches!(e.job, Job::TearBridge(_)) && matches!(e.kind, EK::Knight | EK::Dwarf | EK::Paladin) && self.rng.f() < 0.05 {
|
||||
// knights cut the player's way in: bridges near their post are torn down
|
||||
if let Some(b) = self.bridge_to_tear(e) {
|
||||
e.job = Job::TearBridge(b);
|
||||
e.path.clear();
|
||||
e.repath = 0.0;
|
||||
e.timer = 0.0;
|
||||
}
|
||||
} else if !matches!(e.job, Job::AttackTower(_) | Job::TearBridge(_)) {
|
||||
// no unit around: towers within reach are attacked
|
||||
let near = self.towers.iter().filter(|t| {
|
||||
let near = self.towers.iter().filter(|t| !t.hero).filter(|t| {
|
||||
let (tx, ty) = t.pos();
|
||||
(tx - e.x).powi(2) + (ty - e.y).powi(2) < radius * radius
|
||||
}).min_by(|a, b| {
|
||||
@@ -2521,9 +2701,21 @@ impl Game {
|
||||
e.path.clear();
|
||||
e.think = 1.5;
|
||||
}
|
||||
if let Job::TearBridge(b) = e.job {
|
||||
if self.tear_bridge(e, b, dt) {
|
||||
return;
|
||||
}
|
||||
e.job = Job::None;
|
||||
e.path.clear();
|
||||
}
|
||||
if let Job::Attack(id) = e.job {
|
||||
let (gx, gy) = e.home.unwrap_or_else(|| self.gate_center());
|
||||
let far = e.guard && (e.x - gx).powi(2) + (e.y - gy).powi(2) > 14.0 * 14.0;
|
||||
// guards and patrols do not follow too far
|
||||
let (gx, gy) = match (e.home, e.patrol.get(e.patrol_k % e.patrol.len().max(1))) {
|
||||
(Some(h), _) => h,
|
||||
(None, Some(&(px, py))) => (px as f32 + 0.5, py as f32 + 0.5),
|
||||
_ => self.gate_center(),
|
||||
};
|
||||
let far = (e.guard || !e.patrol.is_empty()) && (e.x - gx).powi(2) + (e.y - gy).powi(2) > 14.0 * 14.0;
|
||||
if !far && self.attack_job(e, id, dt) {
|
||||
return;
|
||||
}
|
||||
@@ -2531,6 +2723,25 @@ impl Game {
|
||||
e.path.clear();
|
||||
e.think = 1.5;
|
||||
}
|
||||
if !e.patrol.is_empty() {
|
||||
// patrols walk their route in a loop
|
||||
let k = e.patrol_k % e.patrol.len();
|
||||
let (px, py) = e.patrol[k];
|
||||
if (e.x - px as f32 - 0.5).powi(2) + (e.y - py as f32 - 0.5).powi(2) < 0.8 {
|
||||
e.patrol_k = (k + 1) % e.patrol.len();
|
||||
e.path.clear();
|
||||
}
|
||||
if e.path.is_empty() && e.repath <= 0.0 {
|
||||
e.repath = 1.0;
|
||||
let (px, py) = e.patrol[e.patrol_k % e.patrol.len()];
|
||||
match self.map.path_to(e.tile(), idx(px, py)) {
|
||||
Some(p) => e.path = p,
|
||||
None => e.patrol_k = (e.patrol_k + 1) % e.patrol.len(), // blocked: next point
|
||||
}
|
||||
}
|
||||
self.step_path(e, dt);
|
||||
return;
|
||||
}
|
||||
if e.guard {
|
||||
// guards stay at their post (gate or treasure cave)
|
||||
let (gx, gy) = e.home.unwrap_or_else(|| self.gate_center());
|
||||
@@ -2716,7 +2927,7 @@ impl Game {
|
||||
continue;
|
||||
}
|
||||
let i = idx(x, y);
|
||||
if let Some(k) = self.towers.iter().position(|t| t.tile == i) {
|
||||
if let Some(k) = self.towers.iter().position(|t| t.tile == i && !t.hero) {
|
||||
refund += self.towers[k].invested() as f32 * 0.5;
|
||||
self.towers.remove(k);
|
||||
self.map.t[i].kind = Kind::Floor;
|
||||
@@ -2764,6 +2975,11 @@ impl Game {
|
||||
let s = tower_stats(kind);
|
||||
if let Some(k) = self.towers.iter().position(|t| t.tile == i) {
|
||||
let t = &self.towers[k];
|
||||
if t.hero {
|
||||
self.hint(16, "hint.tower_where");
|
||||
self.sfx_ui(Sfx::Error);
|
||||
return;
|
||||
}
|
||||
if t.kind != kind {
|
||||
self.hint(14, "hint.tower_other");
|
||||
return;
|
||||
@@ -2806,7 +3022,7 @@ impl Game {
|
||||
self.map.t[i].kind = Kind::Tower;
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
let tower = Tower { id, kind, tile: i, level: 1, hp: s.hp, cd: 1.0, aim: 0.0, flash: 0.0 };
|
||||
let tower = Tower { id, kind, tile: i, level: 1, hp: s.hp, cd: 1.0, aim: 0.0, flash: 0.0, hero: false };
|
||||
let (px, py) = tower.pos();
|
||||
self.towers.push(tower);
|
||||
// units standing on the tile step aside
|
||||
@@ -2832,7 +3048,23 @@ impl Game {
|
||||
let (px, py) = t.pos();
|
||||
self.fx(3, px, py, 0.0, 0.0, 1.0, 0xff505070);
|
||||
self.sfx(Sfx::DigDone, px, py);
|
||||
self.msg("msg.tower_destroyed", vec![format!("@{}", tower_name(t.kind))]);
|
||||
if t.hero {
|
||||
self.towers_razed += 1;
|
||||
self.msg("msg.enemy_tower_destroyed", vec![format!("@{}", tower_name(t.kind))]);
|
||||
} else {
|
||||
self.msg("msg.tower_destroyed", vec![format!("@{}", tower_name(t.kind))]);
|
||||
}
|
||||
}
|
||||
|
||||
// a knight tower near the unit (for the player's creatures)
|
||||
fn enemy_tower_near(&self, x: f32, y: f32, radius: f32) -> Option<u32> {
|
||||
self.towers
|
||||
.iter()
|
||||
.filter(|t| t.hero)
|
||||
.map(|t| (t.id, (t.pos().0 - x).powi(2) + (t.pos().1 - y).powi(2)))
|
||||
.filter(|&(_, d)| d < radius * radius)
|
||||
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
|
||||
.map(|(id, _)| id)
|
||||
}
|
||||
|
||||
// damage dealt to a unit by a tower (no attacker unit involved)
|
||||
@@ -2852,7 +3084,9 @@ impl Game {
|
||||
let mut best = None;
|
||||
let mut bd = r * r;
|
||||
for (j, e) in self.ents.iter().enumerate() {
|
||||
if !e.alive || e.held || !e.active_hero() {
|
||||
// knight towers shoot the player's units, the player's towers shoot heroes
|
||||
let enemy = if t.hero { !e.hero } else { e.active_hero() };
|
||||
if !e.alive || e.held || !enemy {
|
||||
continue;
|
||||
}
|
||||
let d = (e.x - px).powi(2) + (e.y - py).powi(2);
|
||||
@@ -2878,12 +3112,14 @@ impl Game {
|
||||
let t = self.towers[k].clone();
|
||||
let Some(j) = self.tower_target(&t) else { continue };
|
||||
let s = tower_stats(t.kind);
|
||||
if s.mana > 0.0 && self.mana < s.mana {
|
||||
if s.mana > 0.0 && !t.hero && self.mana < s.mana {
|
||||
self.hint(17, "hint.tower_mana");
|
||||
self.towers[k].cd = 1.0;
|
||||
continue;
|
||||
}
|
||||
self.mana -= s.mana;
|
||||
if !t.hero {
|
||||
self.mana -= s.mana;
|
||||
}
|
||||
self.towers[k].cd = s.interval;
|
||||
self.towers[k].flash = 0.15;
|
||||
let (px, py) = t.pos();
|
||||
@@ -3017,6 +3253,8 @@ impl Game {
|
||||
let (ix, iy) = xy(i);
|
||||
(ix - x).abs() <= 1 && (iy - y).abs() <= 1 && i != tile
|
||||
}, true) {
|
||||
// already next to the tower: step to the middle of the tile (in reach)
|
||||
Some(p) if p.is_empty() => e.path = vec![e.tile()],
|
||||
Some(p) => e.path = p,
|
||||
None => return false,
|
||||
}
|
||||
@@ -3268,6 +3506,8 @@ pub struct Tower {
|
||||
pub cd: f32,
|
||||
pub aim: f32, // direction of the last shot (radians, for drawing)
|
||||
pub flash: f32,
|
||||
#[serde(default)]
|
||||
pub hero: bool, // tower of a knight fortress: shoots the player's units
|
||||
}
|
||||
|
||||
impl Tower {
|
||||
@@ -3302,6 +3542,8 @@ impl Tower {
|
||||
// Language key describing what a unit is doing
|
||||
pub fn job_name(e: &Ent) -> &'static str {
|
||||
match e.job {
|
||||
Job::None if !e.patrol.is_empty() => "job.patrol",
|
||||
Job::None if e.guard => "job.guard",
|
||||
Job::None => "job.none",
|
||||
Job::Dig(_) => "job.dig",
|
||||
Job::Claim(_) => "job.claim",
|
||||
@@ -3318,6 +3560,7 @@ pub fn job_name(e: &Ent) -> &'static str {
|
||||
Job::Train(_) => "job.train",
|
||||
Job::Study(_) => "job.study",
|
||||
Job::Attack(_) => "job.fight",
|
||||
Job::TearBridge(_) => "job.tear_bridge",
|
||||
Job::AttackCore => {
|
||||
if e.working {
|
||||
"job.core_attack"
|
||||
@@ -3531,6 +3774,108 @@ mod tests {
|
||||
assert!((gold - g.gold - cost).abs() < 1.0);
|
||||
}
|
||||
|
||||
// a fortress east of the start area, joined to it by a claimed corridor
|
||||
fn fort_game(spec: impl Fn(i32, i32) -> crate::fortress::FortSpec) -> (Game, crate::fortress::FortSpec) {
|
||||
let mut g = Game::new(11, 96, 64);
|
||||
let (cx, cy) = g.map.core;
|
||||
let f = spec(cx + 14, cy - 6);
|
||||
g.build_fortress(&f);
|
||||
for x in cx + 5..f.x {
|
||||
for y in cy..=cy + 2 {
|
||||
let t = &mut g.map.t[idx(x, y)];
|
||||
t.kind = Kind::Floor;
|
||||
t.owner = Owner::Player;
|
||||
}
|
||||
}
|
||||
(g, f)
|
||||
}
|
||||
|
||||
fn spec(x: i32, y: i32) -> crate::fortress::FortSpec {
|
||||
crate::fortress::FortSpec { x, y, w: 15, h: 15, doors: vec![3], towers: vec![], tower_level: 1, guards: vec![], patrols: vec![], vaults: 0, vault_value: 0, shard: false, heart: false }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knight_towers_and_creatures_fight() {
|
||||
let (mut g, f) = fort_game(|x, y| crate::fortress::FortSpec { towers: vec![TowerKind::Arrow], ..spec(x, y) });
|
||||
assert_eq!(g.towers.len(), 1);
|
||||
assert!(g.towers[0].hero);
|
||||
let (tx, ty) = g.towers[0].pos();
|
||||
let id = g.spawn(EK::Ogre, false, tx + 3.0, ty + 2.0, 4);
|
||||
let hp0 = g.ents.iter().find(|e| e.id == id).unwrap().hp;
|
||||
let thp0 = g.towers[0].hp;
|
||||
for _ in 0..200 {
|
||||
g.update(0.05);
|
||||
}
|
||||
let hurt = g.ents.iter().find(|e| e.id == id).map_or(true, |e| e.hp < hp0);
|
||||
let tower_hurt = g.towers.first().map_or(true, |t| t.hp < thp0);
|
||||
assert!(hurt, "the knight tower shoots the creature");
|
||||
assert!(tower_hurt, "the creature attacks the knight tower");
|
||||
let _ = f;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vaults_are_claimed_and_looted() {
|
||||
let (mut g, _) = fort_game(|x, y| crate::fortress::FortSpec { vaults: 2, vault_value: 1800, ..spec(x, y) });
|
||||
let gold0 = g.gold;
|
||||
for _ in 0..(240 * 20) {
|
||||
g.update(0.05);
|
||||
if g.loot >= 3000.0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("loot after claiming: {}", g.loot);
|
||||
assert!(g.loot > 1000.0);
|
||||
assert!(g.gold > gold0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn patrols_walk_and_guards_raise_the_alarm() {
|
||||
let (mut g, f) = fort_game(|x, y| crate::fortress::FortSpec {
|
||||
guards: vec![(EK::Knight, 1); 4],
|
||||
patrols: vec![vec![(EK::Archer, 1)]],
|
||||
..spec(x, y)
|
||||
});
|
||||
let pj = g.ents.iter().position(|e| !e.patrol.is_empty()).unwrap();
|
||||
let pid = g.ents[pj].id;
|
||||
let start = (g.ents[pj].x, g.ents[pj].y);
|
||||
for _ in 0..200 {
|
||||
g.update(0.05);
|
||||
}
|
||||
let p = g.ents.iter().find(|e| e.id == pid).unwrap();
|
||||
assert!((p.x - start.0).abs() + (p.y - start.1).abs() > 3.0, "the patrol moves");
|
||||
// an intruder next to one guard: the others join
|
||||
let (hx, hy) = g.ents.iter().find(|e| e.guard).unwrap().home.unwrap();
|
||||
g.spawn(EK::Ogre, false, hx + 1.0, hy, 8);
|
||||
for _ in 0..20 {
|
||||
g.update(0.05);
|
||||
}
|
||||
let fighting = g.ents.iter().filter(|e| e.guard && matches!(e.job, Job::Attack(_))).count();
|
||||
assert!(fighting >= 3, "alarm: {} guards fight", fighting);
|
||||
let _ = f;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knights_tear_down_bridges() {
|
||||
let (mut g, f) = fort_game(|x, y| crate::fortress::FortSpec { guards: vec![(EK::Knight, 1)], ..spec(x, y) });
|
||||
// a bridge of the player in the courtyard, on a small pond
|
||||
let (bx, by) = (f.x + f.w / 2 + 2, f.y + f.h / 2 + 2);
|
||||
for y in by - 1..=by + 1 {
|
||||
g.map.t[idx(bx, y)].kind = Kind::Water;
|
||||
g.map.t[idx(bx, y)].owner = Owner::None;
|
||||
}
|
||||
g.map.t[idx(bx, by)].kind = Kind::Bridge;
|
||||
g.map.t[idx(bx, by)].owner = Owner::Player;
|
||||
let mut torn = false;
|
||||
for _ in 0..(90 * 20) {
|
||||
g.update(0.05);
|
||||
if g.map.t[idx(bx, by)].kind == Kind::Water {
|
||||
torn = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(torn);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn some_imps_always_claim() {
|
||||
// lots of digging work: still at least 10% (min. one) of the imps claim the dug-out floor
|
||||
|
||||
Reference in new issue
Block a user