From 94390331e0c182dda46aec69274398e54ce4b848 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 5 Jan 2023 23:15:43 +0000 Subject: [PATCH] Rtsim death event --- rtsim/src/event.rs | 8 +++++++- rtsim/src/gen/mod.rs | 7 ++++--- server/src/events/entity_manipulation.rs | 11 +++++++++-- server/src/rtsim2/mod.rs | 12 +++++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/rtsim/src/event.rs b/rtsim/src/event.rs index 1ae5cbb6bb..c1a32a85e0 100644 --- a/rtsim/src/event.rs +++ b/rtsim/src/event.rs @@ -1,4 +1,4 @@ -use super::{RtState, Rule}; +use crate::{data::NpcId, RtState, Rule}; use common::resources::{Time, TimeOfDay}; use world::{IndexRef, World}; @@ -23,3 +23,9 @@ pub struct OnTick { pub dt: f32, } impl Event for OnTick {} + +#[derive(Clone)] +pub struct OnDeath { + pub npc_id: NpcId, +} +impl Event for OnDeath {} diff --git a/rtsim/src/gen/mod.rs b/rtsim/src/gen/mod.rs index a62f9c9ec6..2f90c2c805 100644 --- a/rtsim/src/gen/mod.rs +++ b/rtsim/src/gen/mod.rs @@ -77,10 +77,11 @@ impl Data { // matches!(&index.sites.get(ws).kind, SiteKind::Refactor(_)))) .skip(1) // .take(1) { - let good_or_evil = site + let Some(good_or_evil) = site .faction .and_then(|f| this.factions.get(f)) - .map_or(true, |f| f.good_or_evil); + .map(|f| f.good_or_evil) + else { continue }; let rand_wpos = |rng: &mut SmallRng| { let wpos2d = site.wpos.map(|e| e + rng.gen_range(-10..10)); @@ -89,7 +90,7 @@ impl Data { .with_z(world.sim().get_alt_approx(wpos2d).unwrap_or(0.0)) }; if good_or_evil { - for _ in 0..250 { + for _ in 0..64 { this.npcs.create( Npc::new(rng.gen(), rand_wpos(&mut rng)) .with_faction(site.faction) diff --git a/server/src/events/entity_manipulation.rs b/server/src/events/entity_manipulation.rs index 0c9944731d..f596e94292 100644 --- a/server/src/events/entity_manipulation.rs +++ b/server/src/events/entity_manipulation.rs @@ -43,7 +43,7 @@ use rand_distr::Distribution; use specs::{ join::Join, saveload::MarkerAllocator, Builder, Entity as EcsEntity, Entity, WorldExt, }; -use std::{collections::HashMap, iter, time::Duration}; +use std::{collections::HashMap, iter, sync::Arc, time::Duration}; use tracing::{debug, error}; use vek::{Vec2, Vec3}; @@ -528,7 +528,14 @@ pub fn handle_destroy(server: &mut Server, entity: EcsEntity, last_change: Healt state .ecs() .write_resource::() - .hook_rtsim_entity_delete(rtsim_entity); + .hook_rtsim_entity_delete( + &state.ecs().read_resource::>(), + state + .ecs() + .read_resource::() + .as_index_ref(), + rtsim_entity, + ); } if let Err(e) = state.delete_entity_recorded(entity) { diff --git a/server/src/rtsim2/mod.rs b/server/src/rtsim2/mod.rs index 45eba51d8f..8ee595bb8f 100644 --- a/server/src/rtsim2/mod.rs +++ b/server/src/rtsim2/mod.rs @@ -13,7 +13,7 @@ use common_ecs::{dispatch, System}; use enum_map::EnumMap; use rtsim2::{ data::{npc::NpcMode, Data, ReadError}, - event::OnSetup, + event::{OnDeath, OnSetup}, rule::Rule, RtState, }; @@ -154,8 +154,14 @@ impl RtSim { } } - pub fn hook_rtsim_entity_delete(&mut self, entity: RtSimEntity) { - // TODO: Emit event on deletion to catch death? + pub fn hook_rtsim_entity_delete( + &mut self, + world: &World, + index: IndexRef, + entity: RtSimEntity, + ) { + // Should entity deletion be death? They're not exactly the same thing... + self.state.emit(OnDeath { npc_id: entity.0 }, world, index); self.state.data_mut().npcs.remove(entity.0); }