From: Ian Jackson Date: Tue, 16 Mar 2021 12:41:49 +0000 (+0000) Subject: New IPieceTraitObj: proof of concept X-Git-Tag: otter-0.4.0~13 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=953e8918da36a8dd75b30d53d46558e8e11b5701;p=otter.git New IPieceTraitObj: proof of concept Signed-off-by: Ian Jackson --- diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 0c613859..76b317cc 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -683,6 +683,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( gpc.pos.clamped(gs.table_size).map_err(|_| SpecError::PosOffTable)?; if gpc.zlevel.z > gs.max_z { gs.max_z = gpc.zlevel.z.clone() } let piece = gs.pieces.as_mut(modperm).insert(gpc); + let p = IPieceTraitObj::new(p); ig.ipieces.as_mut(modperm).insert(piece, IPiece { p, occilk }); updates.push((piece, PieceUpdateOp::Insert(()))); pos = (pos + posd)?; diff --git a/src/global.rs b/src/global.rs index a1ec77c1..78408d78 100644 --- a/src/global.rs +++ b/src/global.rs @@ -70,9 +70,10 @@ pub struct IPlayer { // usual variable: ipl #[derive(Debug,Serialize,Deserialize)] pub struct IPiece { - pub p: Box, + pub p: IPieceTraitObj, pub occilk: Option, } +deref_to_field!{IPiece, IPieceTraitObj, p} #[derive(Debug,Serialize,Deserialize)] #[serde(transparent)] diff --git a/src/hidden.rs b/src/hidden.rs index dd6bd198..150810aa 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -18,6 +18,11 @@ visible_slotmap_key!{ OccId(b'H') } #[derive(Copy,Clone,Debug)] pub struct ShowUnocculted(()); +#[derive(Debug,Serialize,Deserialize)] +#[serde(transparent)] +pub struct IPieceTraitObj(Box); +deref_to_field!{IPieceTraitObj, Box, 0} // xxx + #[derive(Clone,Debug,Default,Serialize,Deserialize)] pub struct GameOccults { occults: DenseSlotMap, @@ -232,6 +237,14 @@ impl PieceRenderInstructions { } } +impl IPieceTraitObj { + pub fn new(p: Box) -> Self { Self(p) } + + pub fn show(&self, _: ShowUnocculted) -> &Box { + &self.0 + } +} + impl GPiece { pub fn fully_visible_to_everyone(&self) -> Option { match self.occult.passive { @@ -431,7 +444,7 @@ fn recalculate_occultation_general< let ogpc = gpieces.get(opiece).ok_or_else(bad)?; let ounocc = ogpc.fully_visible_to_everyone() .ok_or_else(||internal_error_bydebug(&(occulteds, &ogpc)))?; - Ok::<_,IE>(oipc.p.describe_html(ogpc, ounocc)?) + Ok::<_,IE>(oipc.show(ounocc).describe_html(ogpc, ounocc)?) }; let most_obscure = most_obscure.unwrap_or(&OccK::Visible); // no players! diff --git a/src/pcrender.rs b/src/pcrender.rs index 4a06ab4f..25d30a84 100644 --- a/src/pcrender.rs +++ b/src/pcrender.rs @@ -78,7 +78,7 @@ impl PriOccultedGeneral { pub fn describe_fallible(&self, ioccults: &IOccults, gpc: &GPiece, ipc: &IPiece) -> Html { match self.instead(ioccults, ipc)? { - Left(y) => ipc.p.describe_html(gpc, y)?, + Left(y) => ipc.show(y).describe_html(gpc, y)?, Right(i) => i.describe_html()?, } } @@ -158,7 +158,7 @@ impl PieceRenderInstructions { let instead = pri.instead(ioccults, ipc)?; let o: &dyn OutlineTrait = match instead { - Left(_) => Borrow::::borrow(&ipc.p).dyn_upcast(), + Left(y) => Borrow::::borrow(ipc.show(y)).dyn_upcast(), Right(i) => i.dyn_upcast(), };