Implement living hurt cooldown and outer damage dispatch
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
use horizon_world::living_hurt::{HurtState, HurtTag, HurtWorld, hurt_server};
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
};
|
||||
fn single(v: &Value) -> f32 {
|
||||
f32::from_bits(u32::from_str_radix(v.as_str().unwrap(), 16).unwrap())
|
||||
}
|
||||
fn bits(v: f32) -> u32 {
|
||||
if v.is_nan() {
|
||||
f32::NAN.to_bits()
|
||||
} else {
|
||||
v.to_bits()
|
||||
}
|
||||
}
|
||||
fn state(s: &HurtState<()>) -> String {
|
||||
format!(
|
||||
"{} {} {:08x} {} {} {} {}",
|
||||
s.no_action_time,
|
||||
s.cooldown,
|
||||
bits(s.last_hurt),
|
||||
s.hurt_duration,
|
||||
s.hurt_time,
|
||||
s.last_source.is_some(),
|
||||
s.last_stamp
|
||||
)
|
||||
}
|
||||
struct Env<'a> {
|
||||
out: &'a mut dyn Write,
|
||||
op: &'a Value,
|
||||
calls: i64,
|
||||
hit: bool,
|
||||
recorded: f32,
|
||||
player: bool,
|
||||
}
|
||||
impl Env<'_> {
|
||||
fn flag(&self, n: &str) -> bool {
|
||||
self.op[n].as_bool().unwrap()
|
||||
}
|
||||
fn event(&mut self, s: &str) -> io::Result<()> {
|
||||
writeln!(self.out, "{s}")?;
|
||||
let n = self.calls;
|
||||
self.calls += 1;
|
||||
if n == self.op["fail"].as_i64().unwrap() {
|
||||
Err(io::Error::other("injected hurt callback failure"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
impl HurtWorld for Env<'_> {
|
||||
type Error = io::Error;
|
||||
type Source = ();
|
||||
type UseItem = bool;
|
||||
type Blocking = ();
|
||||
type Sound = ();
|
||||
fn source(&self) {}
|
||||
fn is_server_player(&self) -> bool {
|
||||
self.player
|
||||
}
|
||||
fn invulnerable(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("invulnerable")?;
|
||||
Ok(self.flag("invulnerable"))
|
||||
}
|
||||
fn fire_resistance(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("fire-resistance")?;
|
||||
Ok(self.flag("fire_resistance"))
|
||||
}
|
||||
fn sleeping(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("sleeping")?;
|
||||
Ok(self.flag("sleeping"))
|
||||
}
|
||||
fn freeze_hurts_extra(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("freeze-extra")?;
|
||||
Ok(self.flag("freeze_extra"))
|
||||
}
|
||||
fn stop_sleeping(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("wake")
|
||||
}
|
||||
fn resolve_mob(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("resolve-mob")
|
||||
}
|
||||
fn resolve_player(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("resolve-player")
|
||||
}
|
||||
fn broadcast_damage(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("broadcast")
|
||||
}
|
||||
fn mark_hurt(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("mark")
|
||||
}
|
||||
fn secondary_sound(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("thorns")
|
||||
}
|
||||
fn die(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("die")
|
||||
}
|
||||
fn hurt_sound(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("hurt-sound")
|
||||
}
|
||||
|
||||
fn dead_or_dying(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("dead")?;
|
||||
Ok(self.flag(if self.hit {
|
||||
"dead_after"
|
||||
} else {
|
||||
"dead_before"
|
||||
}))
|
||||
}
|
||||
fn tag(&mut self, _: &mut HurtState<()>, tag: HurtTag) -> io::Result<bool> {
|
||||
let n = match tag {
|
||||
HurtTag::Fire => "fire",
|
||||
HurtTag::Freezing => "freezing",
|
||||
HurtTag::DamagesHelmet => "damages_helmet",
|
||||
HurtTag::BypassesCooldown => "bypasses_cooldown",
|
||||
HurtTag::NoImpact => "no_impact",
|
||||
HurtTag::NoKnockback => "no_knockback",
|
||||
};
|
||||
self.event(&format!("tag {n}"))?;
|
||||
Ok(self.flag(n))
|
||||
}
|
||||
fn use_item(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("use-item")?;
|
||||
Ok(self.flag("component"))
|
||||
}
|
||||
fn apply_blocking(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<f32> {
|
||||
self.event(&format!("blocking {:08x}", bits(v)))?;
|
||||
Ok(single(&self.op["blocked"]))
|
||||
}
|
||||
fn helmet_empty(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("helmet")?;
|
||||
Ok(!self.flag("helmet"))
|
||||
}
|
||||
fn hurt_helmet(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> {
|
||||
self.event(&format!("hurt-helmet {:08x}", bits(v)))
|
||||
}
|
||||
fn actually_hurt(&mut self, s: &mut HurtState<()>, v: f32) -> io::Result<()> {
|
||||
self.event(&format!("hurt {:08x} {}", bits(v), state(s)))?;
|
||||
self.hit = true;
|
||||
self.recorded = v;
|
||||
if self.flag("mutate_hit") {
|
||||
s.cooldown = self.op["hit_cooldown"].as_i64().unwrap() as i32;
|
||||
s.last_hurt = single(&self.op["hit_last"]);
|
||||
s.hurt_duration = self.op["hit_duration"].as_i64().unwrap() as i32;
|
||||
s.hurt_time = self.op["hit_time"].as_i64().unwrap() as i32;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn blocking_component(&mut self, v: bool) -> Option<()> {
|
||||
v.then_some(())
|
||||
}
|
||||
fn on_blocked(&mut self, _: &mut HurtState<()>, _: ()) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn knockback(&mut self, _: &mut HurtState<()>, v: f32, b: bool) -> io::Result<()> {
|
||||
self.event(&format!("knockback {:08x} {b}", bits(v)))
|
||||
}
|
||||
fn death_protection(&mut self, _: &mut HurtState<()>) -> io::Result<bool> {
|
||||
self.event("tag bypasses_invulnerability")?;
|
||||
if !self.flag("bypasses_invulnerability") {
|
||||
self.event("hand 0")?;
|
||||
self.event("hand 1")?;
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
fn death_sound(&mut self, _: &mut HurtState<()>) -> io::Result<()> {
|
||||
self.event("death-sound")
|
||||
}
|
||||
fn make_sound(&mut self, _: &mut HurtState<()>, _: ()) -> io::Result<()> {
|
||||
self.event("make-sound")
|
||||
}
|
||||
fn game_time(&mut self, _: &mut HurtState<()>) -> io::Result<i64> {
|
||||
self.event("level")?;
|
||||
self.event("game-time")?;
|
||||
Ok(self.op["game_time"].as_i64().unwrap())
|
||||
}
|
||||
fn effects_on_hurt(&mut self, _: &mut HurtState<()>, v: f32) -> io::Result<()> {
|
||||
self.event("effects")?;
|
||||
for i in 0..self.op["effects"].as_u64().unwrap() {
|
||||
self.event(&format!("effect {i} {:08x}", bits(v)))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn player_hurt_criterion(
|
||||
&mut self,
|
||||
_: &mut HurtState<()>,
|
||||
_: f32,
|
||||
_: f32,
|
||||
_: bool,
|
||||
) -> io::Result<()> {
|
||||
self.event("criterion")
|
||||
}
|
||||
fn award_blocked(&mut self, _: &mut HurtState<()>, v: i32) -> io::Result<()> {
|
||||
self.event(&format!("blocked-stat {v}"))
|
||||
}
|
||||
fn attacker_criterion(
|
||||
&mut self,
|
||||
_: &mut HurtState<()>,
|
||||
_: f32,
|
||||
_: f32,
|
||||
_: bool,
|
||||
) -> io::Result<()> {
|
||||
self.event("attacker")
|
||||
}
|
||||
}
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let arg = std::env::args().nth(1).unwrap();
|
||||
let dir = Path::new(&arg);
|
||||
let mut out = io::BufWriter::new(fs::File::create(dir.join("rust.txt"))?);
|
||||
for line in fs::read_to_string(dir.join("inputs.txt"))?.lines() {
|
||||
let f: Value = serde_json::from_str(line)?;
|
||||
let mut s = HurtState {
|
||||
no_action_time: f["no_action"].as_i64().unwrap() as i32,
|
||||
cooldown: f["cooldown"].as_i64().unwrap() as i32,
|
||||
last_hurt: single(&f["last_hurt"]),
|
||||
hurt_duration: f["duration"].as_i64().unwrap() as i32,
|
||||
hurt_time: f["time"].as_i64().unwrap() as i32,
|
||||
last_source: f["source"].as_bool().unwrap().then_some(()),
|
||||
last_stamp: f["stamp"].as_i64().unwrap(),
|
||||
};
|
||||
let mut recorded = 0.0;
|
||||
for op in f["ops"].as_array().unwrap() {
|
||||
let mut e = Env {
|
||||
out: &mut out,
|
||||
op,
|
||||
calls: 0,
|
||||
hit: false,
|
||||
recorded,
|
||||
player: f["player"].as_bool().unwrap(),
|
||||
};
|
||||
match hurt_server(&mut s, &mut e, single(&op["damage"])) {
|
||||
Ok(v) => writeln!(e.out, "bool {v}\nresult ok")?,
|
||||
Err(_) => writeln!(e.out, "result error")?,
|
||||
};
|
||||
recorded = e.recorded;
|
||||
writeln!(
|
||||
e.out,
|
||||
"state {} {:08x} {} {}",
|
||||
state(&s),
|
||||
bits(recorded),
|
||||
e.hit,
|
||||
e.calls
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -46,6 +46,7 @@ pub mod living_damage;
|
||||
pub mod living_fall;
|
||||
pub mod living_fluid;
|
||||
pub mod living_health;
|
||||
pub mod living_hurt;
|
||||
pub mod living_landing;
|
||||
pub mod living_motion;
|
||||
pub mod movement_emission;
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
//! Outer living damage dispatch, cooldown writes and post-hit service ordering.
|
||||
use crate::living_damage::{STAT_LIMIT, stat_amount};
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum HurtTag {
|
||||
Fire,
|
||||
Freezing,
|
||||
DamagesHelmet,
|
||||
BypassesCooldown,
|
||||
NoImpact,
|
||||
NoKnockback,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct HurtState<S> {
|
||||
pub no_action_time: i32,
|
||||
pub cooldown: i32,
|
||||
pub last_hurt: f32,
|
||||
pub hurt_duration: i32,
|
||||
pub hurt_time: i32,
|
||||
pub last_source: Option<S>,
|
||||
pub last_stamp: i64,
|
||||
}
|
||||
/// One invocation retains the same incoming source and server level. Services
|
||||
/// receive live state so reentrant callbacks preserve native write ordering.
|
||||
pub trait HurtWorld {
|
||||
type Error;
|
||||
type Source: Copy;
|
||||
type UseItem: Copy;
|
||||
type Blocking: Copy;
|
||||
type Sound: Copy;
|
||||
fn source(&self) -> Self::Source;
|
||||
fn is_server_player(&self) -> bool;
|
||||
fn invulnerable(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn dead_or_dying(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn tag(&mut self, s: &mut HurtState<Self::Source>, tag: HurtTag) -> Result<bool, Self::Error>;
|
||||
fn fire_resistance(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn sleeping(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn stop_sleeping(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn use_item(&mut self, s: &mut HurtState<Self::Source>) -> Result<Self::UseItem, Self::Error>;
|
||||
fn apply_blocking(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
damage: f32,
|
||||
) -> Result<f32, Self::Error>;
|
||||
fn freeze_hurts_extra(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn helmet_empty(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn hurt_helmet(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
damage: f32,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn actually_hurt(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
damage: f32,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn resolve_mob(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn resolve_player(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn blocking_component(&mut self, item: Self::UseItem) -> Option<Self::Blocking>;
|
||||
fn on_blocked(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
block: Self::Blocking,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn broadcast_damage(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn mark_hurt(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn knockback(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
damage: f32,
|
||||
blocked: bool,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn death_protection(&mut self, s: &mut HurtState<Self::Source>) -> Result<bool, Self::Error>;
|
||||
fn death_sound(&mut self, s: &mut HurtState<Self::Source>) -> Result<Self::Sound, Self::Error>;
|
||||
fn make_sound(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
sound: Self::Sound,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn secondary_sound(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn die(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn hurt_sound(&mut self, s: &mut HurtState<Self::Source>) -> Result<(), Self::Error>;
|
||||
fn game_time(&mut self, s: &mut HurtState<Self::Source>) -> Result<i64, Self::Error>;
|
||||
fn effects_on_hurt(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
damage: f32,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn player_hurt_criterion(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
original: f32,
|
||||
damage: f32,
|
||||
blocked: bool,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn award_blocked(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
amount: i32,
|
||||
) -> Result<(), Self::Error>;
|
||||
/// Looks up the attacker, then dispatches its criterion if a server player.
|
||||
fn attacker_criterion(
|
||||
&mut self,
|
||||
s: &mut HurtState<Self::Source>,
|
||||
original: f32,
|
||||
damage: f32,
|
||||
blocked: bool,
|
||||
) -> Result<(), Self::Error>;
|
||||
}
|
||||
pub fn hurt_server<W: HurtWorld>(
|
||||
s: &mut HurtState<W::Source>,
|
||||
w: &mut W,
|
||||
mut damage: f32,
|
||||
) -> Result<bool, W::Error> {
|
||||
if w.invulnerable(s)? || w.dead_or_dying(s)? {
|
||||
return Ok(false);
|
||||
}
|
||||
if w.tag(s, HurtTag::Fire)? && w.fire_resistance(s)? {
|
||||
return Ok(false);
|
||||
}
|
||||
if w.sleeping(s)? {
|
||||
w.stop_sleeping(s)?;
|
||||
}
|
||||
s.no_action_time = 0;
|
||||
if damage < 0.0 {
|
||||
damage = 0.0;
|
||||
}
|
||||
let original = damage;
|
||||
let item = w.use_item(s)?;
|
||||
let blocked_amount = w.apply_blocking(s, damage)?;
|
||||
damage -= blocked_amount;
|
||||
let blocked = blocked_amount > 0.0;
|
||||
if w.tag(s, HurtTag::Freezing)? && w.freeze_hurts_extra(s)? {
|
||||
damage *= 5.0;
|
||||
}
|
||||
if w.tag(s, HurtTag::DamagesHelmet)? && !w.helmet_empty(s)? {
|
||||
w.hurt_helmet(s, damage)?;
|
||||
damage *= 0.75;
|
||||
}
|
||||
if !damage.is_finite() {
|
||||
damage = f32::MAX;
|
||||
}
|
||||
let fresh = if s.cooldown as f32 > 10.0 && !w.tag(s, HurtTag::BypassesCooldown)? {
|
||||
if damage <= s.last_hurt {
|
||||
return Ok(false);
|
||||
}
|
||||
w.actually_hurt(s, damage - s.last_hurt)?;
|
||||
s.last_hurt = damage;
|
||||
false
|
||||
} else {
|
||||
s.last_hurt = damage;
|
||||
s.cooldown = 20;
|
||||
w.actually_hurt(s, damage)?;
|
||||
s.hurt_duration = 10;
|
||||
s.hurt_time = s.hurt_duration;
|
||||
true
|
||||
};
|
||||
w.resolve_mob(s)?;
|
||||
w.resolve_player(s)?;
|
||||
if fresh {
|
||||
let component = w.blocking_component(item);
|
||||
if let Some(component) = component.filter(|_| blocked) {
|
||||
w.on_blocked(s, component)?;
|
||||
} else {
|
||||
w.broadcast_damage(s)?;
|
||||
}
|
||||
if !w.tag(s, HurtTag::NoImpact)? && (!blocked || damage > 0.0) {
|
||||
w.mark_hurt(s)?;
|
||||
}
|
||||
let fully_blocked = blocked && damage <= 0.0;
|
||||
if !w.tag(s, HurtTag::NoKnockback)? && !fully_blocked {
|
||||
w.knockback(s, damage, blocked)?;
|
||||
}
|
||||
}
|
||||
if w.dead_or_dying(s)? {
|
||||
if !w.death_protection(s)? {
|
||||
if fresh {
|
||||
let sound = w.death_sound(s)?;
|
||||
w.make_sound(s, sound)?;
|
||||
w.secondary_sound(s)?;
|
||||
}
|
||||
w.die(s)?;
|
||||
}
|
||||
} else if fresh {
|
||||
w.hurt_sound(s)?;
|
||||
w.secondary_sound(s)?;
|
||||
}
|
||||
let accepted = !blocked || damage > 0.0;
|
||||
if accepted {
|
||||
s.last_source = Some(w.source());
|
||||
s.last_stamp = w.game_time(s)?;
|
||||
w.effects_on_hurt(s, damage)?;
|
||||
}
|
||||
if w.is_server_player() {
|
||||
w.player_hurt_criterion(s, original, damage, blocked)?;
|
||||
if blocked_amount > 0.0 && blocked_amount < STAT_LIMIT {
|
||||
w.award_blocked(s, stat_amount(blocked_amount))?;
|
||||
}
|
||||
}
|
||||
w.attacker_criterion(s, original, damage, blocked)?;
|
||||
Ok(accepted)
|
||||
}
|
||||
Reference in New Issue
Block a user