Co-op offline with an AI partner

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Richard ZinkandClaude Opus 5.5 committed 2026-11-05 21:04:49 +01:00
1 parent e3a9d32d61
commit f87b59390e
22 files changed
+272 -5

No files matched your search

+139 -3
View File
@@ -7,6 +7,10 @@ use crate::map::*;
use serde::{Deserialize, Serialize};
pub const RIVAL_CORE_HP: f32 = 2000.0;
// a co-op AI partner helps masters short of gold: every minute it may send some
pub const PARTNER_GIFT_EVERY: f32 = 60.0;
pub const PARTNER_GIFT_BELOW: f32 = 300.0;
pub const PARTNER_GIFT: f32 = 400.0;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Rival {
@@ -20,6 +24,8 @@ pub struct Rival {
pub attack_t: f32,
pub difficulty: u8, // 0 easy, 1 medium, 2 hard
pub power: f32, // grows with the time: level of the new creatures
#[serde(default)]
pub gift_t: f32, // co-op AI partner: seconds until it looks whether its allies need gold
}
struct Pace {
@@ -135,7 +141,7 @@ impl Game {
t.gold = 300;
t.res = 0;
}
self.rivals.push(Rival { side, core, hp: RIVAL_CORE_HP, alive: true, radius: r, grow_t: 20.0, spawn_t: 5.0, attack_t: pace(difficulty).attack, difficulty, power: 0.0 });
self.rivals.push(Rival { side, core, hp: RIVAL_CORE_HP, alive: true, radius: r, grow_t: 20.0, spawn_t: 5.0, attack_t: pace(difficulty).attack, difficulty, power: 0.0, gift_t: PARTNER_GIFT_EVERY });
for k in 0..3 {
self.spawn_rival_unit(side, EK::Kobold, 1, k as f32);
}
@@ -167,6 +173,14 @@ impl Game {
r.spawn_t -= dt;
r.attack_t -= dt;
}
// a co-op partner sends gold to allied masters who run short
if self.is_partner(side) {
self.rivals[k].gift_t -= dt;
if self.rivals[k].gift_t <= 0.0 {
self.rivals[k].gift_t = PARTNER_GIFT_EVERY;
self.partner_gift(side);
}
}
// growth: one more ring of ground (no imps needed)
if self.rivals[k].grow_t <= 0.0 {
self.rivals[k].grow_t = p.grow * (0.8 + self.rng.f() * 0.4);
@@ -294,7 +308,14 @@ impl Game {
let (x, y) = self.rivals[k].centre();
self.fx(1, x, y, 0.0, 0.0, 1.0, 0xffffffff);
self.sfx(Sfx::Victory, x, y);
self.msg(if side == 2 { "msg.rival1_out" } else { "msg.rival2_out" }, vec![]);
let key = if self.is_partner(side) {
"msg.partner_out"
} else if side == 2 {
"msg.rival1_out"
} else {
"msg.rival2_out"
};
self.msg(key, vec![]);
// its creatures scatter and vanish, its ground is free
for e in self.ents.iter_mut() {
if e.alive && !e.hero && e.side == side {
@@ -385,6 +406,41 @@ impl Game {
}
// units of a rival master: fight enemies nearby, march when sent, otherwise guard home
// an AI seat of a co-op game: an allied master who builds his own dungeon
pub fn is_partner(&self, side: u8) -> bool {
self.coop && self.ai_sides.contains(&side)
}
// the partner gives gold to every allied master below the limit
fn partner_gift(&mut self, side: u8) {
let me = self.me_side;
for s in self.master_sides() {
if self.is_out(s) || self.gold_of(s) >= PARTNER_GIFT_BELOW {
continue;
}
self.switch_to(s);
let got = self.add_gold(PARTNER_GIFT).floor();
if got >= 1.0 {
self.msg("msg.gold_received", vec![(got as i32).to_string(), format!("@net.side{side}")]);
self.sfx_ui(Sfx::Payday);
}
}
self.switch_to(me);
}
// a co-op partner's creatures follow the attack flag of the first master still standing
fn partner_flag(&self, side: u8) -> Option<usize> {
if !self.is_partner(side) {
return None;
}
let lead = self.master_sides().into_iter().find(|s| !self.is_out(*s))?;
if lead == self.me_side {
self.flag
} else {
self.masters.iter().find(|m| m.side == lead)?.flag
}
}
pub fn rival_ai(&mut self, e: &mut Ent, dt: f32) {
if e.think <= 0.0 {
e.think = 0.5;
@@ -397,18 +453,41 @@ impl Game {
}
}
}
// two of three healthy creatures of a co-op partner join the attack at the allies' flag
let assist = if e.id % 3 != 0 && e.hp > e.max_hp * 0.4 { self.partner_flag(e.side) } else { None };
if let Job::Attack(id) = e.job {
let (hx, hy) = e.home.unwrap_or((e.x, e.y));
let far = e.target.is_none() && (e.x - hx).powi(2) + (e.y - hy).powi(2) > 15.0 * 15.0;
let far = e.target.is_none() && assist.is_none() && (e.x - hx).powi(2) + (e.y - hy).powi(2) > 15.0 * 15.0;
if !far && self.attack_job(e, id, dt) {
return;
}
e.job = Job::None;
e.path.clear();
}
if let Some(f) = assist {
// the flag at the humans' gate: attack the gate
let (fx, fy) = xy(f);
let (fx, fy) = (fx as f32 + 0.5, fy as f32 + 0.5);
let (gx, gy) = self.gate_center();
if e.target.is_none() && self.has_gate() && self.gate_hp > 0.0 && (fx - gx).powi(2) + (fy - gy).powi(2) <= 8.0 * 8.0 {
e.target = Some(1);
}
}
if self.march_on(e, dt) {
return;
}
if let Some(f) = assist {
let (fx, fy) = xy(f);
let d2 = (e.x - fx as f32 - 0.5).powi(2) + (e.y - fy as f32 - 0.5).powi(2);
if d2 > 2.5 * 2.5 && e.path.is_empty() && e.repath <= 0.0 {
e.repath = 1.5;
if let Some(p) = self.map.creature_path(flies(e.kind), e.tile(), f) {
e.path = p;
}
}
self.step_path(e, dt);
return;
}
// guard the own dungeon
let (hx, hy) = e.home.unwrap_or((e.x, e.y));
let d2 = (e.x - hx).powi(2) + (e.y - hy).powi(2);
@@ -538,4 +617,61 @@ mod tests {
g.spawn_wave();
assert!(g.ents[before..].iter().filter(|e| e.hero).all(|e| e.target.is_none()), "the wave comes for the player");
}
// the first mission of the co-op league, set up like an offline co-op game: player on side 0,
// an AI partner on side 2, no server
fn offline_league_game() -> Game {
let doc = crate::editor::coop_campaign::coop_campaign();
let mut g = crate::campaign::start_editor_coop_mission(&doc.missions[0], 0, 77);
g.coop = true;
assert!(g.start_match(&[0, 2], 0));
g.sync_ai_sides(&[2], 1);
g.end_match();
g
}
#[test]
fn an_offline_coop_partner_is_an_allied_ai_master() {
let mut g = offline_league_game();
assert!(g.netplay.is_none() && g.masters.is_empty());
assert!(g.rival_alive(2) && g.is_partner(2));
assert!(g.allied(0, 2) && g.allied(2, 0));
// short of gold: the partner helps out within a minute
g.gold = 0.0;
for _ in 0..(65 * 20) {
g.update(0.05);
}
assert!(g.msgs.iter().any(|m| m.key == "msg.gold_received"), "the partner sent gold");
assert_eq!(g.state, 0);
// the partner's creatures never fight the player's
assert!(!g.ents.iter().any(|e| e.alive && !e.hero && e.side == 0 && e.hp < e.max_hp && matches!(e.job, Job::Attack(_))));
}
#[test]
fn the_partner_follows_the_attack_flag() {
let mut g = offline_league_game();
for _ in 0..(90 * 20) {
g.update(0.05);
}
let (rx, ry) = g.rivals.iter().find(|r| r.side == 2).unwrap().centre();
// a flag on the partner's own ground a little away from its core
let f = (0..g.map.t.len())
.find(|&i| {
let (x, y) = xy(i);
let d2 = (x as f32 - rx).powi(2) + (y as f32 - ry).powi(2);
g.map.t[i].owner == Owner::of_side(2) && g.map.walk(x, y) && d2 > 16.0 && d2 < 36.0
})
.expect("partner ground");
g.flag = Some(f);
let (fx, fy) = xy(f);
let mut near = false;
for _ in 0..(30 * 20) {
g.update(0.05);
near |= g.ents.iter().any(|e| e.alive && e.side == 2 && !e.hero && e.id % 3 != 0 && (e.x - fx as f32 - 0.5).powi(2) + (e.y - fy as f32 - 0.5).powi(2) < 3.0 * 3.0);
if near {
break;
}
}
assert!(near, "partner creatures gather at the flag");
}
}