diff --git a/agent_docs/combat.md b/agent_docs/combat.md index 076f90904..1c4da87cb 100644 --- a/agent_docs/combat.md +++ b/agent_docs/combat.md @@ -1966,10 +1966,14 @@ controller's client plays the cast and the server applies it (`game::mob_skill` says what each skill does). - **The grant:** when a controller's move asks to attack, the server - (`MobSet::act`) may also let the mob cast one skill, picked at random - from those it can pay for, that are off cooldown (`interval` seconds), - whose `hp` limit the mob is at or under, and, for a summon, whose - `limit` of live summons isn't reached. A sealed mob casts nothing. The + (`MobSet::act`) may also let the mob cast one skill. It picks one of + those listed at random and grants it only if the mob can pay for it, it + is off cooldown (`interval` seconds), the mob is at or under its `hp` + limit, and, for a summon, its `limit` of live summons isn't reached; + otherwise the mob attacks, as in Cosmic's MoveLifeHandler. (Picking only + among the castable ones would hand a skill with no `interval`, such as + Prototype Lord's Confusion 132/5, over on every move, so the mob would + never attack.) A sealed mob casts nothing. The skill and level end MOVE_MONSTER_RESPONSE; the client keeps a grant until another ack grants something else (FUN_0066c23b). An unused grant lapses on the server after 10 s. diff --git a/integration-harness/src/trace.rs b/integration-harness/src/trace.rs index 1e8910f24..c6f70c529 100644 --- a/integration-harness/src/trace.rs +++ b/integration-harness/src/trace.rs @@ -1030,6 +1030,27 @@ pub async fn read_for(c: &mut MapleTestConnection, trace: &mut Trace, ms: u64) { } } +/// Ask `object_id`, a mob this client controls, at `at`, for a skill to +/// cast: want to attack until an answer grants one, and return it as +/// (skill, level). The server picks one of its listed skills at random and +/// grants it only if the mob may cast it now, so an answer may grant none. +pub async fn ask_for_skill( + c: &mut MapleTestConnection, + trace: &mut Trace, + object_id: i32, + at: (i16, i16), +) -> (u8, u8) { + for _ in 0..60 { + let (answers, granted) = (trace.field.move_responses.len(), trace.field.grants.len()); + send(c, crate::hunting::move_mob(object_id, at, true, 0, 0)).await; + until(c, trace, |t| t.field.move_responses.len() > answers).await; + if let Some(&(_, skill, level)) = trace.field.grants.get(granted) { + return (skill, level); + } + } + panic!("mob {object_id} was granted no skill: {trace:?}"); +} + /// Wait for our own chat line to come back, recording packets on the way. /// The field relays it only after earlier work for this client completed. /// A field's packets stop at a client that has moved on, so a line said as diff --git a/integration-harness/tests/mob_counters.rs b/integration-harness/tests/mob_counters.rs index e0e780854..e40689425 100644 --- a/integration-harness/tests/mob_counters.rs +++ b/integration-harness/tests/mob_counters.rs @@ -10,7 +10,7 @@ use integration_harness::database::{set_hp, set_job, set_str, set_weapon_attack}; use integration_harness::hunting::{attack, move_mob}; use integration_harness::preconditions::load_harness_config_or_fail; -use integration_harness::trace::{send, stat, until, warp, Trace}; +use integration_harness::trace::{ask_for_skill, send, stat, until, warp, Trace}; use integration_harness::{login_to_world_session, relogin}; use packet::Packet; use tokio::time::Duration; @@ -82,14 +82,12 @@ async fn a_counter_hurts_whoever_attacks_the_mob() { // It is granted its heal or its counter; a heal waits five minutes // after, so the counter comes next. - send(c, move_mob(boss, at, true, 0, 0)).await; - until(c, &mut t, |t| !t.field.grants.is_empty()).await; + ask_for_skill(c, &mut t, boss, at).await; if t.field.grants.last() == Some(&(boss, HEAL, 23)) { send(c, cast(boss, at, 1, HEAL, 23)).await; until(c, &mut t, |t| !t.field.heals.is_empty()).await; tokio::time::sleep(Duration::from_millis(600)).await; - send(c, move_mob(boss, at, true, 0, 0)).await; - until(c, &mut t, |t| t.field.grants.len() > 1).await; + ask_for_skill(c, &mut t, boss, at).await; } assert_eq!(t.field.grants.last(), Some(&(boss, COUNTER, 1))); send(c, cast(boss, at, 2, COUNTER, 1)).await; diff --git a/integration-harness/tests/mob_dispel_banish.rs b/integration-harness/tests/mob_dispel_banish.rs index 9f48191c9..8272fac9b 100644 --- a/integration-harness/tests/mob_dispel_banish.rs +++ b/integration-harness/tests/mob_dispel_banish.rs @@ -12,7 +12,7 @@ use integration_harness::connection::MapleTestConnection; use integration_harness::database::{grant_skill, set_job}; use integration_harness::hunting::{move_mob, use_skill, walk_to}; use integration_harness::preconditions::load_harness_config_or_fail; -use integration_harness::trace::{read_for, send, until, warp, Trace}; +use integration_harness::trace::{ask_for_skill, read_for, send, until, warp, Trace}; use integration_harness::{login_to_world_session, relogin}; use net::packet::op::RecvOpcode; use packet::{io::write::PktWrite, Packet}; @@ -50,10 +50,7 @@ async fn controlled(c: &mut MapleTestConnection, t: &mut Trace, mob_id: i32) -> /// animation, until it is granted and casts `skill`. async fn cast(c: &mut MapleTestConnection, t: &mut Trace, mob: i32, at: (i16, i16), skill: u8) { for _ in 0..3 { - let granted = t.field.grants.len(); - send(c, move_mob(mob, at, true, 0, 0)).await; - until(c, t, |t| t.field.grants.len() > granted).await; - let (_, grant, level) = *t.field.grants.last().unwrap(); + let (grant, level) = ask_for_skill(c, t, mob, at).await; let data = i32::from(grant) | i32::from(level) << 8; send(c, move_mob(mob, at, false, 21 << 1, data)).await; if grant == skill { diff --git a/integration-harness/tests/mob_skills.rs b/integration-harness/tests/mob_skills.rs index beb12ec8b..340fc2f24 100644 --- a/integration-harness/tests/mob_skills.rs +++ b/integration-harness/tests/mob_skills.rs @@ -7,7 +7,7 @@ use integration_harness::database::{set_job, set_str}; use integration_harness::hunting::{attack, move_mob}; use integration_harness::preconditions::load_harness_config_or_fail; -use integration_harness::trace::{read_for, send, until, warp, Trace}; +use integration_harness::trace::{ask_for_skill, read_for, send, until, warp, Trace}; use integration_harness::{login_to_world_session, relogin}; use packet::Packet; use tokio::time::Duration; @@ -122,8 +122,7 @@ async fn wounded_mobs_heal_and_summon() { // It may be granted its Banish instead, a grant that lapses after 10 // seconds uncast. for _ in 0..8 { - send(c, move_mob(wolf, at, true, 0, 0)).await; - until(c, &mut t, |t| !t.field.grants.is_empty()).await; + ask_for_skill(c, &mut t, wolf, at).await; if t.field.grants != [(wolf, BANISH, 1)] { break; } @@ -144,8 +143,7 @@ async fn wounded_mobs_heal_and_summon() { let at = t.field.mob_positions[&cerebes]; send(c, attack(cerebes, 3_000)).await; until(c, &mut t, |t| t.field.controlled.contains(&cerebes)).await; - send(c, move_mob(cerebes, at, true, 0, 0)).await; - until(c, &mut t, |t| !t.field.grants.is_empty()).await; + ask_for_skill(c, &mut t, cerebes, at).await; assert_eq!(t.field.grants, [(cerebes, SUMMON, 27)]); let before = t.field.alive(SUMMONED); send(c, cast(cerebes, at, 1, SUMMON, 27, 0)).await; diff --git a/integration-harness/tests/papulatus_boss.rs b/integration-harness/tests/papulatus_boss.rs index ded963322..053d08173 100644 --- a/integration-harness/tests/papulatus_boss.rs +++ b/integration-harness/tests/papulatus_boss.rs @@ -13,7 +13,7 @@ use integration_harness::database::{ use integration_harness::events::{clears, party, throw_down, waiting_in}; use integration_harness::hunting::{attack, move_mob, walk_to}; use integration_harness::preconditions::load_harness_config_or_fail; -use integration_harness::trace::{read_for, send, until, warp, Trace}; +use integration_harness::trace::{ask_for_skill, read_for, send, until, warp, Trace}; use integration_harness::{login_to_world_session, relogin, WorldSession}; use net::events::{CLOCKTOWER_DEPTHS, ORIGIN_OF_CLOCKTOWER}; use net::packet::op::RecvOpcode; @@ -101,9 +101,7 @@ async fn darkstars(c: &mut MapleTestConnection, t: &mut Trace, clock: i32) -> (u let at = t.field.mob_positions[&clock]; for _ in 0..40 { t.field.grants.clear(); - send(c, move_mob(clock, at, true, 0, 0)).await; - until(c, t, |t| !t.field.grants.is_empty()).await; - let (_, skill, level) = t.field.grants[0]; + let (skill, level) = ask_for_skill(c, t, clock, at).await; let nth = ACTIONS.iter().find(|a| a.0 == skill).unwrap().1; let before = t.field.mob_positions.len(); send(c, cast(clock, at, nth, skill, level)).await; diff --git a/runtime/src/actor/field_mobs.rs b/runtime/src/actor/field_mobs.rs index b12c5659f..741b3e99f 100644 --- a/runtime/src/actor/field_mobs.rs +++ b/runtime/src/actor/field_mobs.rs @@ -397,11 +397,14 @@ impl LiveMob { }) } - /// Pick a skill for its controller to cast next, by `roll` in 0..1, - /// from those it may: ones the server casts, that it can pay for, that - /// are off cooldown, whose HP limit it is at or under, and, to summon, - /// that have fewer than their limit of its `summons` alive. None while - /// sealed or waiting on a grant. + /// Pick a skill for its controller to cast next: `roll` in 0..1 picks + /// one of those listed, which is granted only if it may be cast: the + /// server casts it, the mob can pay for it, it is off cooldown, the mob + /// is at or under its HP limit, and, to summon, fewer than its limit of + /// its `summons` are alive. Otherwise the mob attacks. As Cosmic's + /// MoveLifeHandler, so a skill with no cooldown (Prototype Lord's + /// Confusion 132/5) doesn't crowd out its attacks. None while sealed or + /// waiting on a grant. fn grant( &mut self, now: Instant, @@ -421,32 +424,26 @@ impl LiveMob { } let max_hp = i64::from(self.info.max_hp.max(1)); let hp_percent = self.hp * 100 / max_hp; - let usable: Vec<(u8, u8)> = self - .info - .skills - .iter() - .filter(|entry| { - let (Ok(_), Ok(_), Some(effect)) = ( - u8::try_from(entry.skill), - u8::try_from(entry.level), - mob_skill::effect(entry.skill, 0), - ) else { - return false; - }; - let Some(level) = levels(entry.skill, entry.level) else { - return false; - }; - level.mp_cost <= self.mp - && hp_percent <= i64::from(level.hp) - && self.cooldowns.get(&entry.skill).is_none_or(|&at| at <= now) - && (effect != MobSkillEffect::Summon - || (!level.summons.is_empty() - && (level.limit <= 0 || summons < level.limit as usize))) - }) - .map(|entry| (entry.skill as u8, entry.level as u8)) - .collect(); - let index = ((roll * usable.len() as f64) as usize).min(usable.len().checked_sub(1)?); - let (skill, level) = usable[index]; + let listed = &self.info.skills; + let entry = + &listed[((roll * listed.len() as f64) as usize).min(listed.len().checked_sub(1)?)]; + let (Ok(skill), Ok(level), Some(effect)) = ( + u8::try_from(entry.skill), + u8::try_from(entry.level), + mob_skill::effect(entry.skill, 0), + ) else { + return None; + }; + let data = levels(entry.skill, entry.level)?; + let usable = data.mp_cost <= self.mp + && hp_percent <= i64::from(data.hp) + && self.cooldowns.get(&entry.skill).is_none_or(|&at| at <= now) + && (effect != MobSkillEffect::Summon + || (!data.summons.is_empty() + && (data.limit <= 0 || summons < data.limit as usize))); + if !usable { + return None; + } self.granted = Some(Grant { skill, level, @@ -2242,7 +2239,9 @@ mod tests { // An ungranted cast is shown as nothing. let early = act(&mut mobs, power_up, 100 | 1 << 8, false, now, 0.0); assert_eq!((early.action, early.cast), (NO_ACTION, None)); - // At full HP Heal (under 50%) can't be granted: Power Up or the summon. + // A roll picks one of the three listed. At full HP Heal (under 50%) + // can't be granted, and nothing else is in its place. + assert_eq!(act(&mut mobs, NO_ACTION, 0, true, now, 0.4).grant, None); let granted = act(&mut mobs, NO_ACTION, 0, true, now, 0.0); assert_eq!((granted.grant, granted.next_attack), (Some((100, 1)), true)); // While it waits, nothing more is granted, but it may still attack. @@ -2267,15 +2266,17 @@ mod tests { assert_eq!((cast.skill, cast.level, cast.effect.x), (100, 1, 115)); assert_eq!(cast.at, now + Duration::from_millis(900)); assert_eq!(mobs.reached_by(&cast), [mob]); - // Power Up cools down for 40 s; the summon is next. - let next = act(&mut mobs, NO_ACTION, 0, true, now, 0.0); + // Power Up cools down for 40 s: picking it grants nothing (a mob + // with attacks would attack), rather than the summon in its place. + assert_eq!(act(&mut mobs, NO_ACTION, 0, true, now, 0.0).grant, None); + let next = act(&mut mobs, NO_ACTION, 0, true, now, 0.99); assert_eq!(next.grant, Some((200, 1))); // A new controller starts afresh. mobs.release(1, Some(2)); assert!(mobs.get(mob).unwrap().granted.is_none()); // Summon twice: its limit is 2 alive. assert_eq!( - act(&mut mobs, NO_ACTION, 0, true, now, 0.0).grant, + act(&mut mobs, NO_ACTION, 0, true, now, 0.99).grant, Some((200, 1)) ); let summoned = mobs.summon( @@ -2295,17 +2296,18 @@ mod tests { assert!(mobs .summon(mob, &[snail_spawn(0).1], 2, || None, now) .is_empty()); - // Hurt below half, only Heal remains while the summons live, and + // Hurt below half, only Heal may be cast while the summons live, and // only with MP for it. let later = now + GRANT_TIMEOUT; mobs.damage(1, mob, 600, later); + assert_eq!(act(&mut mobs, NO_ACTION, 0, true, later, 0.99).grant, None); assert_eq!( - act(&mut mobs, NO_ACTION, 0, true, later, 0.99).grant, + act(&mut mobs, NO_ACTION, 0, true, later, 0.4).grant, Some((114, 1)) ); mobs.live.get_mut(&mob).unwrap().mp = 4; let broke = later + GRANT_TIMEOUT; - assert_eq!(act(&mut mobs, NO_ACTION, 0, true, broke, 0.0).grant, None); + assert_eq!(act(&mut mobs, NO_ACTION, 0, true, broke, 0.4).grant, None); // A sealed mob casts nothing. mobs.live.get_mut(&mob).unwrap().mp = 12; mobs.add_statuses( @@ -2316,7 +2318,7 @@ mod tests { broke, broke + GRANT_TIMEOUT, ); - assert_eq!(act(&mut mobs, NO_ACTION, 0, true, broke, 0.0).grant, None); + assert_eq!(act(&mut mobs, NO_ACTION, 0, true, broke, 0.4).grant, None); // So does one under a player's Seal. mobs.live.get_mut(&mob).unwrap().statuses.clear(); mobs.add_statuses( @@ -2327,7 +2329,7 @@ mod tests { broke, broke + GRANT_TIMEOUT, ); - assert_eq!(act(&mut mobs, NO_ACTION, 0, true, broke, 0.0).grant, None); + assert_eq!(act(&mut mobs, NO_ACTION, 0, true, broke, 0.4).grant, None); // Summons die for good. let Some(Err(_)) = mobs.damage(1, summoned[0], 100, later) else { panic!("the summon should die");