Match active status effect storage and lifecycle notifications
This commit is contained in:
@@ -0,0 +1,316 @@
|
||||
use horizon_world::{
|
||||
active_effects::{ActiveEffects, EffectHooks},
|
||||
java_hash_map::JavaKey,
|
||||
status_effect::{BlendState, Details, EffectInstance},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
fs,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
};
|
||||
#[derive(Clone)]
|
||||
struct Key {
|
||||
id: usize,
|
||||
hash: i32,
|
||||
identity: i32,
|
||||
}
|
||||
impl PartialEq for Key {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
impl Eq for Key {}
|
||||
impl JavaKey for Key {
|
||||
fn java_hash(&self) -> i32 {
|
||||
self.hash
|
||||
}
|
||||
fn tie_break(&self, other: &Self) -> Ordering {
|
||||
if self.identity <= other.identity {
|
||||
Ordering::Less
|
||||
} else {
|
||||
Ordering::Greater
|
||||
}
|
||||
}
|
||||
}
|
||||
type Effect = EffectInstance<Key>;
|
||||
type Storage = ActiveEffects<Key>;
|
||||
fn n(v: &Value, k: &str) -> i32 {
|
||||
v[k].as_i64().unwrap() as i32
|
||||
}
|
||||
fn fl(v: &Value, k: &str) -> f32 {
|
||||
f32::from_bits(v[k].as_u64().unwrap() as u32)
|
||||
}
|
||||
fn bits(v: f32) -> u32 {
|
||||
if v.is_nan() { 0x7fc00000 } else { v.to_bits() }
|
||||
}
|
||||
fn details(v: &Value) -> Details {
|
||||
Details {
|
||||
duration: n(v, "duration"),
|
||||
amplifier: n(v, "amplifier"),
|
||||
ambient: v["ambient"].as_bool().unwrap(),
|
||||
visible: v["visible"].as_bool().unwrap(),
|
||||
show_icon: v["icon"].as_bool().unwrap(),
|
||||
}
|
||||
}
|
||||
struct World<'a> {
|
||||
f: Value,
|
||||
out: &'a mut dyn Write,
|
||||
keys: Vec<Key>,
|
||||
pool: Vec<Effect>,
|
||||
ids: Vec<Effect>,
|
||||
calls: i32,
|
||||
changed: bool,
|
||||
}
|
||||
impl World<'_> {
|
||||
fn config(&self) -> &Value {
|
||||
&self.f[if self.changed { "after" } else { "initial" }]
|
||||
}
|
||||
fn id(&mut self, e: &Effect) -> usize {
|
||||
if let Some(i) = self.ids.iter().position(|v| v.same_reference(e)) {
|
||||
i
|
||||
} else {
|
||||
self.ids.push(e.clone());
|
||||
self.ids.len() - 1
|
||||
}
|
||||
}
|
||||
fn reset(&mut self) {
|
||||
let c = self.config().clone();
|
||||
for (i, v) in c["states"].as_array().unwrap().iter().enumerate() {
|
||||
let mut e = self.pool[i].write();
|
||||
e.details = details(v);
|
||||
e.blend = BlendState {
|
||||
factor: fl(v, "factor"),
|
||||
previous: fl(v, "previous"),
|
||||
};
|
||||
}
|
||||
}
|
||||
fn all(&mut self, s: &Storage) {
|
||||
writeln!(self.out, "state {} {}", self.calls, self.changed).unwrap();
|
||||
let mut roots = Vec::new();
|
||||
let entries = s
|
||||
.entries
|
||||
.entries()
|
||||
.map(|(k, e)| {
|
||||
let id = self.id(e);
|
||||
roots.push(e.clone());
|
||||
format!("{}={id}", k.id)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
writeln!(self.out, "map {entries}").unwrap();
|
||||
for (i, e) in self.pool.clone().iter().enumerate() {
|
||||
let id = self.id(e);
|
||||
writeln!(self.out, "pool {i} {id}").unwrap();
|
||||
roots.push(e.clone());
|
||||
}
|
||||
let mut seen = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < roots.len() {
|
||||
let e = roots[i].clone();
|
||||
i += 1;
|
||||
if seen.iter().any(|v: &Effect| v.same_reference(&e)) {
|
||||
continue;
|
||||
}
|
||||
seen.push(e.clone());
|
||||
let id = self.id(&e);
|
||||
let d = e.read();
|
||||
let h = d
|
||||
.hidden
|
||||
.as_ref()
|
||||
.map(|h| {
|
||||
roots.push(h.clone());
|
||||
self.id(h).to_string()
|
||||
})
|
||||
.unwrap_or("none".into());
|
||||
writeln!(
|
||||
self.out,
|
||||
"node {id} {} {} {} {} {} {} {h} {} {}",
|
||||
d.effect.id,
|
||||
d.details.duration,
|
||||
d.details.amplifier,
|
||||
d.details.ambient,
|
||||
d.details.visible,
|
||||
d.details.show_icon,
|
||||
bits(d.blend.factor),
|
||||
bits(d.blend.previous)
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
fn raw(&mut self, s: &mut Storage, op: &Value) {
|
||||
let a = n(op, "a") as usize;
|
||||
let b = n(op, "b") as usize;
|
||||
match op["op"].as_str().unwrap() {
|
||||
"put" => {
|
||||
s.entries.put(self.keys[a].clone(), self.pool[b].clone());
|
||||
}
|
||||
"drop" => {
|
||||
s.entries.remove(&self.keys[a]);
|
||||
}
|
||||
"clear" => s.entries.clear(),
|
||||
"copy" => self.pool[b] = self.pool[a].copy_instance(),
|
||||
"alias" => self.pool[b] = self.pool[a].clone(),
|
||||
_ => panic!("raw op"),
|
||||
}
|
||||
}
|
||||
fn event(&mut self, s: &mut Storage, name: String) -> Result<(), &'static str> {
|
||||
writeln!(self.out, "event {name}").unwrap();
|
||||
let i = self.calls;
|
||||
self.calls += 1;
|
||||
self.all(s);
|
||||
if i == n(&self.f, "fail") {
|
||||
return Err("callback");
|
||||
}
|
||||
if i == n(&self.f, "mutate") {
|
||||
self.changed = true;
|
||||
self.reset();
|
||||
for op in self.f["mutations"].as_array().unwrap().clone() {
|
||||
self.raw(s, &op);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl EffectHooks<Key> for World<'_> {
|
||||
type Error = &'static str;
|
||||
type Source = i32;
|
||||
type Level = i32;
|
||||
fn can_be_affected(&mut self, s: &mut Storage, e: Effect) -> Result<bool, Self::Error> {
|
||||
let i = self.id(&e);
|
||||
self.event(s, format!("eligible {i}"))?;
|
||||
Ok(self.config()["allow"].as_bool().unwrap())
|
||||
}
|
||||
fn on_added(
|
||||
&mut self,
|
||||
s: &mut Storage,
|
||||
e: Effect,
|
||||
source: Option<i32>,
|
||||
) -> Result<(), Self::Error> {
|
||||
let i = self.id(&e);
|
||||
self.event(s, format!("added {i} {}", source.unwrap_or(-1)))
|
||||
}
|
||||
fn on_updated(
|
||||
&mut self,
|
||||
s: &mut Storage,
|
||||
e: Effect,
|
||||
reapply: bool,
|
||||
source: Option<i32>,
|
||||
) -> Result<(), Self::Error> {
|
||||
let i = self.id(&e);
|
||||
self.event(s, format!("updated {i} {reapply} {}", source.unwrap_or(-1)))
|
||||
}
|
||||
fn effect_added(&mut self, s: &mut Storage, e: Key, amp: i32) -> Result<(), Self::Error> {
|
||||
self.event(s, format!("effect-added {} {amp}", e.id))
|
||||
}
|
||||
fn effect_started(&mut self, s: &mut Storage, e: Key, amp: i32) -> Result<(), Self::Error> {
|
||||
self.event(s, format!("effect-started {} {amp}", e.id))
|
||||
}
|
||||
fn level(&mut self, s: &mut Storage) -> Result<i32, Self::Error> {
|
||||
self.event(s, "level".into())?;
|
||||
Ok(n(self.config(), "level"))
|
||||
}
|
||||
fn is_client(&mut self, s: &mut Storage, l: i32) -> Result<bool, Self::Error> {
|
||||
self.event(s, format!("client {l}"))?;
|
||||
Ok(self.config()["client"].as_bool().unwrap())
|
||||
}
|
||||
fn on_removed(&mut self, s: &mut Storage, es: Vec<Effect>) -> Result<(), Self::Error> {
|
||||
let ids = es
|
||||
.iter()
|
||||
.map(|e| self.id(e).to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
self.event(s, format!("removed {ids}"))
|
||||
}
|
||||
fn get_effect(&mut self, s: &mut Storage, key: Key) -> Result<Option<Effect>, Self::Error> {
|
||||
self.event(s, format!("get {}", key.id))?;
|
||||
Ok(s.get(&key))
|
||||
}
|
||||
fn actor_removed(&self) -> bool {
|
||||
self.config()["removed"].as_bool().unwrap()
|
||||
}
|
||||
}
|
||||
fn run(f: Value, out: &mut dyn Write) {
|
||||
let keys = f["hashes"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(id, v)| Key {
|
||||
id,
|
||||
hash: v.as_i64().unwrap() as i32,
|
||||
identity: f["identities"][id].as_i64().unwrap() as i32,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let mut pool: Vec<Effect> = Vec::new();
|
||||
for v in f["initial"]["states"].as_array().unwrap() {
|
||||
let h = n(v, "hidden");
|
||||
pool.push(Effect::new(
|
||||
keys[n(v, "key") as usize].clone(),
|
||||
details(v),
|
||||
(h >= 0).then(|| pool[h as usize].clone()),
|
||||
));
|
||||
}
|
||||
let mut w = World {
|
||||
f,
|
||||
out,
|
||||
keys,
|
||||
pool,
|
||||
ids: Vec::new(),
|
||||
calls: 0,
|
||||
changed: false,
|
||||
};
|
||||
let mut s = Storage::default();
|
||||
w.reset();
|
||||
for op in w.f["setup"].as_array().unwrap().clone() {
|
||||
w.raw(&mut s, &op);
|
||||
}
|
||||
w.all(&s);
|
||||
for op in w.f["ops"].as_array().unwrap().clone() {
|
||||
let a = n(&op, "a") as usize;
|
||||
let b = n(&op, "b") as usize;
|
||||
let source = n(&op, "source");
|
||||
let source = (source >= 0).then_some(source);
|
||||
let result: Result<String, &'static str> = match op["op"].as_str().unwrap() {
|
||||
"add" => s
|
||||
.add(w.pool[a].clone(), source, &mut w)
|
||||
.map(|v| v.to_string()),
|
||||
"force" => s
|
||||
.force_add(w.pool[a].clone(), source, &mut w)
|
||||
.map(|_| "ok".into()),
|
||||
"remove" => s.remove(&w.keys[a].clone(), &mut w).map(|v| v.to_string()),
|
||||
"remove-raw" => Ok(s
|
||||
.remove_no_update(&w.keys[a])
|
||||
.map(|e| w.id(&e).to_string())
|
||||
.unwrap_or("none".into())),
|
||||
"all" => s.remove_all(&mut w).map(|v| v.to_string()),
|
||||
"has" => Ok(s.has(&w.keys[a]).to_string()),
|
||||
"get" => {
|
||||
let key = w.keys[a].clone();
|
||||
EffectHooks::get_effect(&mut w, &mut s, key)
|
||||
.map(|e| e.map(|e| w.id(&e).to_string()).unwrap_or("none".into()))
|
||||
}
|
||||
"blend" => s
|
||||
.blend_factor(w.keys[a].clone(), fl(&op, "partial"), &mut w)
|
||||
.map(|v| bits(v).to_string()),
|
||||
"put" | "drop" | "clear" | "copy" | "alias" => {
|
||||
w.raw(&mut s, &op);
|
||||
Ok("ok".into())
|
||||
}
|
||||
_ => panic!("operation {b}"),
|
||||
};
|
||||
writeln!(w.out, "result {}", result.unwrap_or_else(str::to_string)).unwrap();
|
||||
w.all(&s);
|
||||
}
|
||||
}
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let arg = std::env::args().nth(1).unwrap();
|
||||
let side = std::env::args().nth(2).unwrap();
|
||||
let dir = Path::new(&arg);
|
||||
let mut out = io::BufWriter::new(fs::File::create(dir.join(format!("rust-{side}.txt")))?);
|
||||
for line in fs::read_to_string(dir.join(format!("inputs-{side}.txt")))?.lines() {
|
||||
run(serde_json::from_str(line)?, &mut out);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
//! Living entity status storage and add/replace/remove lifecycle ordering.
|
||||
use crate::{
|
||||
java_hash_map::{JavaHashMap, JavaKey},
|
||||
status_effect::EffectInstance,
|
||||
};
|
||||
|
||||
pub struct ActiveEffects<H> {
|
||||
/// Native activeEffects is exposed by getActiveEffectsMap. Direct mutations
|
||||
/// bypass lifecycle notifications, but retain the map's iterator semantics.
|
||||
pub entries: JavaHashMap<H, EffectInstance<H>>,
|
||||
}
|
||||
impl<H: JavaKey> Default for ActiveEffects<H> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
entries: JavaHashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub trait EffectHooks<H: JavaKey + Clone> {
|
||||
type Error;
|
||||
type Source: Clone;
|
||||
type Level: Copy;
|
||||
fn can_be_affected(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
incoming: EffectInstance<H>,
|
||||
) -> Result<bool, Self::Error>;
|
||||
fn on_added(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
incoming: EffectInstance<H>,
|
||||
source: Option<Self::Source>,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn on_updated(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
existing: EffectInstance<H>,
|
||||
reapply: bool,
|
||||
source: Option<Self::Source>,
|
||||
) -> Result<(), Self::Error>;
|
||||
/// Concrete MobEffect.onEffectAdded dispatch, after the actor callback.
|
||||
fn effect_added(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
effect: H,
|
||||
amplifier: i32,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn effect_started(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
effect: H,
|
||||
amplifier: i32,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn level(&mut self, storage: &mut ActiveEffects<H>) -> Result<Self::Level, Self::Error>;
|
||||
fn is_client(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
level: Self::Level,
|
||||
) -> Result<bool, Self::Error>;
|
||||
fn on_removed(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
effects: Vec<EffectInstance<H>>,
|
||||
) -> Result<(), Self::Error>;
|
||||
fn get_effect(
|
||||
&mut self,
|
||||
storage: &mut ActiveEffects<H>,
|
||||
effect: H,
|
||||
) -> Result<Option<EffectInstance<H>>, Self::Error> {
|
||||
Ok(storage.get(&effect))
|
||||
}
|
||||
/// Entity.isRemoved is a final field-based query.
|
||||
fn actor_removed(&self) -> bool;
|
||||
}
|
||||
impl<H: JavaKey + Clone> ActiveEffects<H> {
|
||||
pub fn get(&self, effect: &H) -> Option<EffectInstance<H>> {
|
||||
self.entries.get(effect).cloned()
|
||||
}
|
||||
pub fn has(&self, effect: &H) -> bool {
|
||||
self.entries.get(effect).is_some()
|
||||
}
|
||||
pub fn add<W: EffectHooks<H>>(
|
||||
&mut self,
|
||||
incoming: EffectInstance<H>,
|
||||
source: Option<W::Source>,
|
||||
world: &mut W,
|
||||
) -> Result<bool, W::Error> {
|
||||
if !world.can_be_affected(self, incoming.clone())? {
|
||||
return Ok(false);
|
||||
}
|
||||
let key = incoming.read().effect.clone();
|
||||
let existing = self.get(&key);
|
||||
let mut changed = false;
|
||||
if let Some(existing) = existing {
|
||||
if existing.update(&incoming) {
|
||||
world.on_updated(self, existing, true, source)?;
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
self.entries.put(key, incoming.clone());
|
||||
world.on_added(self, incoming.clone(), source)?;
|
||||
changed = true;
|
||||
let data = incoming.read();
|
||||
let effect = data.effect.clone();
|
||||
let amplifier = data.details.amplifier;
|
||||
drop(data);
|
||||
world.effect_added(self, effect, amplifier)?;
|
||||
}
|
||||
let data = incoming.read();
|
||||
let effect = data.effect.clone();
|
||||
let amplifier = data.details.amplifier;
|
||||
drop(data);
|
||||
world.effect_started(self, effect, amplifier)?;
|
||||
Ok(changed)
|
||||
}
|
||||
pub fn force_add<W: EffectHooks<H>>(
|
||||
&mut self,
|
||||
incoming: EffectInstance<H>,
|
||||
source: Option<W::Source>,
|
||||
world: &mut W,
|
||||
) -> Result<(), W::Error> {
|
||||
if !world.can_be_affected(self, incoming.clone())? {
|
||||
return Ok(());
|
||||
}
|
||||
let key = incoming.read().effect.clone();
|
||||
let previous = self.entries.put(key, incoming.clone());
|
||||
if let Some(previous) = previous {
|
||||
incoming.copy_blend_from(&previous);
|
||||
world.on_updated(self, incoming, true, source)?;
|
||||
} else {
|
||||
world.on_added(self, incoming, source)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn remove_no_update(&mut self, effect: &H) -> Option<EffectInstance<H>> {
|
||||
self.entries.remove(effect)
|
||||
}
|
||||
pub fn remove<W: EffectHooks<H>>(
|
||||
&mut self,
|
||||
effect: &H,
|
||||
world: &mut W,
|
||||
) -> Result<bool, W::Error> {
|
||||
if let Some(instance) = self.remove_no_update(effect) {
|
||||
world.on_removed(self, vec![instance])?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
pub fn remove_all<W: EffectHooks<H>>(&mut self, world: &mut W) -> Result<bool, W::Error> {
|
||||
let level = world.level(self)?;
|
||||
if world.is_client(self, level)? || self.entries.is_empty() {
|
||||
return Ok(false);
|
||||
}
|
||||
let previous = self.entries.copied();
|
||||
self.entries.clear();
|
||||
let values = previous.entries().map(|(_, v)| v.clone()).collect();
|
||||
world.on_removed(self, values)?;
|
||||
Ok(true)
|
||||
}
|
||||
pub fn blend_factor<W: EffectHooks<H>>(
|
||||
&mut self,
|
||||
effect: H,
|
||||
partial: f32,
|
||||
world: &mut W,
|
||||
) -> Result<f32, W::Error> {
|
||||
let effect = world.get_effect(self, effect)?;
|
||||
Ok(effect.map_or(0.0, |e| e.blend_factor(partial, world.actor_removed())))
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Vanilla-compatible world data for Horizon. This crate does not run gameplay.
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod active_effects;
|
||||
pub mod attribute_health;
|
||||
pub mod attributes;
|
||||
pub mod block_collision;
|
||||
|
||||
Reference in New Issue
Block a user