From: Ian Jackson Date: Tue, 16 Mar 2021 12:51:55 +0000 (+0000) Subject: New IPieceTraitObj: all done X-Git-Tag: otter-0.4.0~12 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=2a8d92dbe19df36bb2046e6513337c4bd56825d5;p=otter.git New IPieceTraitObj: all done Signed-off-by: Ian Jackson --- diff --git a/daemon/api.rs b/daemon/api.rs index 0e107ae9..0bb08b9c 100644 --- a/daemon/api.rs +++ b/daemon/api.rs @@ -489,7 +489,7 @@ api_route!{ let _: Void = match (self.opname.as_str(), self.wrc) { ("flip", wrc@ WRC::UpdateSvg) => { - let nfaces = ipc.p.nfaces(y); + let nfaces = ipc.show(y).nfaces(); let logents = log_did_to_piece( ioccults, &gs.occults, player, gpl, piece, gpc, ipc, "flipped" @@ -513,7 +513,7 @@ api_route!{ }; } - ipc.p.ui_operation(a, &self.opname, self.wrc, y)? + ipc.show(y).ui_operation(a, &self.opname, self.wrc)? } } } diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 76b317cc..0a2a8f32 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -396,13 +396,13 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( let &GPiece { pos, face, .. } = gpc; if let Some(ipc) = ig.ipieces.get(piece); let unocc = gpc.fully_visible_to_everyone(); - let visible = if let Some(_) = unocc { + let visible = if let Some(y) = unocc { // todo: something more sophisticated would be nice let pri = PieceRenderInstructions::new_visible( // visible id is internal one here VisiblePieceId(piece.data()) ); - let bbox = ipc.p.bbox_approx()?; + let bbox = ipc.show(y).bbox_approx()?; let desc_html = pri.describe(ioccults, gpc, ipc); Some(MgmtGamePieceVisibleInfo { pos, face, desc_html, bbox @@ -411,7 +411,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( None }; let itemname = if let Some(unocc) = unocc { - ipc.p.itemname(unocc).to_string() + ipc.show(unocc).itemname().to_string() } else { "occulted-item".to_string() }; @@ -609,7 +609,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( piece)? ); } - ipc.p.delete_hook(&gpc, gs); + ipc.p.into_inner().delete_hook(&gpc, gs); } if let Some(occilk) = ipc.occilk { ig.ioccults.ilks.dispose(occilk); } (U{ pcs: vec![(piece, PieceUpdateOp::Delete())], @@ -657,7 +657,6 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( let mut updates = Vec::with_capacity(count_len); let mut pos = pos.unwrap_or(DEFAULT_POS_START); let mut z = gs.max_z.clone_mut(); - let not_occ = ShowUnocculted::new_visible(); for piece_i in count { let PieceSpecLoaded { p, occultable } = info.load(piece_i as usize)?; let ilks = &mut ig.ioccults.ilks; @@ -665,7 +664,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( ilks.insert(ilkname, OccultIlkData { p_occ }) }); let face = face.unwrap_or_default(); - if p.nfaces(not_occ) <= face.into() { + if p.nfaces() <= face.into() { throw!(SpecError::FaceNotFound); } let gpc = GPiece { diff --git a/src/bin/otterlib.rs b/src/bin/otterlib.rs index f0e254c1..b3400384 100644 --- a/src/bin/otterlib.rs +++ b/src/bin/otterlib.rs @@ -64,8 +64,6 @@ pub enum OutputKind { pub type ItemForOutput = (String, ItemEnquiryData); -const UNOCC: ShowUnocculted = ShowUnocculted::new_visible(); - #[throws(AE)] fn preview(items: Vec) { const BORDER: f64 = 1.; @@ -85,7 +83,7 @@ fn preview(items: Vec) { self.size[0] < 20.0 } fn face_cols(&self) -> usize { - usize::from(self.p.nfaces(UNOCC)) + usize::from(self.p.nfaces()) * if self.want_several() { SEVERAL } else { 1 } } } @@ -96,7 +94,7 @@ fn preview(items: Vec) { let loaded = spec.clone().load().context("load")?; let p = loaded.p; // xxx show occulted version too let mut uos = vec![]; - p.add_ui_operations(&mut uos, &GPiece::dummy(), UNOCC) + p.add_ui_operations(&mut uos, &GPiece::dummy()) .context("add uos")?; let uos = uos.into_iter().map(|uo| uo.opname).collect::>(); let spec = spec.clone(); @@ -133,7 +131,7 @@ fn preview(items: Vec) { println!(r#"{}"#, Html::from_txt(&spec.item).0); println!(r#"{}"#, - p.describe_html(&GPiece::dummy(), UNOCC)?.0); + p.describe_html(&GPiece::dummy())?.0); let only1 = s.face_cols() == 1; for facecol in 0..(if only1 { 1 } else { max_facecols }) { @@ -154,7 +152,7 @@ fn preview(items: Vec) { _ => panic!(), }); println!(r#">"#); - if face < (p.nfaces(UNOCC) as usize) { + if face < (p.nfaces() as usize) { let viewport = [bbox[0].clone(), size.clone()] .iter().cloned() @@ -175,7 +173,7 @@ fn preview(items: Vec) { } let mut html = Html("".into()); let gpc = GPiece { face: face.into(), ..GPiece::dummy() }; - p.svg_piece(&mut html, &gpc, default(), UNOCC)?; + p.svg_piece(&mut html, &gpc, default())?; println!("{}", html.0); } println!(""); diff --git a/src/gamestate.rs b/src/gamestate.rs index b53139b7..6b8635da 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -130,31 +130,30 @@ pub trait PieceTrait: OutlineTrait + Send + Debug + 'static { /// by convention, occult face is nfaces-1 // xxx this is no good, we need a central definition of the occult // face to avoid weird behaviour with buggy gamespecs - fn nfaces(&self, y: ShowUnocculted) -> RawFaceId; + fn nfaces(&self) -> RawFaceId; #[throws(InternalError)] fn add_ui_operations(&self, _upd: &mut Vec, - _gpc: &GPiece, _y: ShowUnocculted) { } + _gpc: &GPiece) { } fn ui_operation(&self, _a: ApiPieceOpArgs<'_>, - _opname: &str, _wrc: WhatResponseToClientOp, - _y: ShowUnocculted) + _opname: &str, _wrc: WhatResponseToClientOp) -> Result { throw!(OE::BadOperation) } // #[throws] doesn't work here - fehler #todo fn svg_piece(&self, f: &mut Html, gpc: &GPiece, - id: VisiblePieceId, y: ShowUnocculted) -> Result<(),IE>; + id: VisiblePieceId) -> Result<(),IE>; - fn describe_html(&self, gpc: &GPiece, y: ShowUnocculted) -> Result; + fn describe_html(&self, gpc: &GPiece) -> Result; fn delete_hook(&self, _p: &GPiece, _gs: &mut GameState) -> ExecuteGameChangeUpdates { ExecuteGameChangeUpdates{ pcs: vec![], log: vec![], raw: None } } - fn itemname(&self, _y: ShowUnocculted) -> &str; + fn itemname(&self) -> &str; } #[typetag::serde] diff --git a/src/hand.rs b/src/hand.rs index 122b5142..27eee032 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -85,10 +85,9 @@ impl Hand { #[typetag::serde] impl PieceTrait for Hand { - fn nfaces(&self, _: ShowUnocculted) -> RawFaceId { 1 } + fn nfaces(&self) -> RawFaceId { 1 } #[throws(IE)] - fn svg_piece(&self, f: &mut Html, gpc: &GPiece, - _vpid: VisiblePieceId, _: ShowUnocculted) { + fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _vpid: VisiblePieceId) { self.shape.svg_piece_raw(f, gpc.face, &mut |f: &mut String| { if_chain!{ if let Some(xdata) = gpc.xdata.get::()?; @@ -101,7 +100,7 @@ impl PieceTrait for Hand { } #[throws(IE)] - fn describe_html(&self, gpc: &GPiece, _: ShowUnocculted) -> Html { + fn describe_html(&self, gpc: &GPiece) -> Html { let xdata = gpc.xdata.get()?; self.describe_html_inner(xdata) } @@ -110,13 +109,12 @@ impl PieceTrait for Hand { delegate!{ to self.shape { - fn itemname(&self, y: ShowUnocculted) -> &str; + fn itemname(&self) -> &str; } } #[throws(InternalError)] - fn add_ui_operations(&self, upd: &mut Vec, - gpc: &GPiece, _: ShowUnocculted) { + fn add_ui_operations(&self, upd: &mut Vec, gpc: &GPiece) { upd.push(if_chain! { if let Some(xdata) = gpc.xdata.get::()?; if let Some(_owner) = &xdata.owner; @@ -139,8 +137,7 @@ impl PieceTrait for Hand { #[throws(ApiPieceOpError)] fn ui_operation(&self, a: ApiPieceOpArgs<'_>, - opname: &str, wrc: WhatResponseToClientOp, - _: ShowUnocculted) + opname: &str, wrc: WhatResponseToClientOp) -> UpdateFromOpComplex { let ApiPieceOpArgs { gs,player,piece,ipieces,ioccults,to_permute,.. } = a; let gen = &mut gs.gen; diff --git a/src/hidden.rs b/src/hidden.rs index 150810aa..8b652caf 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -21,7 +21,6 @@ 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 { @@ -243,6 +242,8 @@ impl IPieceTraitObj { pub fn show(&self, _: ShowUnocculted) -> &Box { &self.0 } + + pub fn into_inner(self) -> Box { self.0 } } impl GPiece { @@ -444,7 +445,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.show(ounocc).describe_html(ogpc, ounocc)?) + Ok::<_,IE>(oipc.show(ounocc).describe_html(ogpc)?) }; let most_obscure = most_obscure.unwrap_or(&OccK::Visible); // no players! diff --git a/src/pcrender.rs b/src/pcrender.rs index 25d30a84..e5c4d811 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.show(y).describe_html(gpc, y)?, + Left(y) => ipc.show(y).describe_html(gpc)?, Right(i) => i.describe_html()?, } } @@ -179,7 +179,7 @@ impl PieceRenderInstructions { match instead { Left(y) => { - ipc.p.svg_piece(&mut defs, gpc, pri.vpid, y)?; + ipc.show(y).svg_piece(&mut defs, gpc, pri.vpid)?; }, Right(i) => { i.svg(&mut defs, pri.vpid)?; @@ -205,7 +205,7 @@ impl PieceRenderInstructions { type WRC = WhatResponseToClientOp; let mut out = vec![]; - if ipc.p.nfaces(y) > 1 { + if ipc.show(y).nfaces() > 1 { out.push(UoDescription { wrc: WRC::UpdateSvg, kind: UoKind::Global, @@ -214,7 +214,7 @@ impl PieceRenderInstructions { desc: Html::lit("flip"), }) } - ipc.p.add_ui_operations(&mut out, gpc, y)?; + ipc.show(y).add_ui_operations(&mut out, gpc)?; out } } diff --git a/src/pieces.rs b/src/pieces.rs index d0a387ca..e7ba1be6 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -129,12 +129,11 @@ impl OutlineTrait for GenericSimpleShape #[typetag::serde] impl PieceTrait for SimpleShape { #[throws(IE)] - fn svg_piece(&self, f: &mut Html, gpc: &GPiece, - _vpid: VisiblePieceId, _: ShowUnocculted) { + fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _vpid: VisiblePieceId) { self.svg_piece_raw(f, gpc.face, &mut |_|Ok(()))?; } #[throws(IE)] - fn describe_html(&self, gpc: &GPiece, _: ShowUnocculted) -> Html { + fn describe_html(&self, gpc: &GPiece) -> Html { Html(if_chain! { if let face = gpc.face; if let Some(colour) = self.colours.get(face); @@ -142,11 +141,11 @@ impl PieceTrait for SimpleShape { else { format!("a {}", self.desc.0) } }) } - fn nfaces(&self, _: ShowUnocculted) -> RawFaceId { + fn nfaces(&self) -> RawFaceId { self.count_faces().try_into().unwrap() } - fn itemname(&self, y: ShowUnocculted) -> &str { self.itemname(y) } + fn itemname(&self) -> &str { self.itemname() } } impl GenericSimpleShape @@ -156,7 +155,7 @@ impl GenericSimpleShape pub fn count_faces(&self) -> usize { max(self.colours.len(), self.edges.len()) } - pub fn itemname(&self, _: ShowUnocculted) -> &str { &self.itemname } + pub fn itemname(&self) -> &str { &self.itemname } #[throws(SpecError)] pub fn new(desc: Desc, outline: Outl, diff --git a/src/shapelib.rs b/src/shapelib.rs index b836952a..54632a84 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -211,24 +211,23 @@ impl FaceTransform { #[typetag::serde(name="Lib")] impl PieceTrait for Item { - fn nfaces(&self, _: ShowUnocculted) -> RawFaceId { + fn nfaces(&self) -> RawFaceId { self.faces.len().try_into().unwrap() } #[throws(IE)] - fn svg_piece(&self, f: &mut Html, gpc: &GPiece, - _vpid: VisiblePieceId, _: ShowUnocculted) { + fn svg_piece(&self, f: &mut Html, gpc: &GPiece, _vpid: VisiblePieceId) { let face = &self.faces[gpc.face]; let svgd = &self.svgs[face.svg]; face.xform.write_svgd(f, svgd)?; } #[throws(IE)] - fn describe_html(&self, gpc: &GPiece, _: ShowUnocculted) -> Html { + fn describe_html(&self, gpc: &GPiece) -> Html { self.descs[ self.faces[gpc.face].desc ].clone() } - fn itemname(&self, _: ShowUnocculted) -> &str { &self.itemname } + fn itemname(&self) -> &str { &self.itemname } } static SHAPELIBS: RwLock> = const_rwlock(None); @@ -344,7 +343,6 @@ impl Contents { let pat = glob::Pattern::new(pat).map_err(|pe| ME::BadGlob { pat: pat.to_string(), msg: pe.msg.to_string() })?; let mut out = vec![]; - let unocc_ok = ShowUnocculted::new_visible(); for (k,v) in &self.items { if !pat.matches(&k) { continue } let loaded = match self.load1(v, &k) { @@ -356,7 +354,7 @@ impl Contents { let ier = ItemEnquiryData { itemname: k.clone(), f0bbox, - f0desc: loaded.p.describe_html(&GPiece::dummy(), unocc_ok)?, + f0desc: loaded.p.describe_html(&GPiece::dummy())?, }; out.push(ier); }