chiark / gitweb /
New IPieceTraitObj: all done
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 16 Mar 2021 12:51:55 +0000 (12:51 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 16 Mar 2021 12:51:55 +0000 (12:51 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/api.rs
daemon/cmdlistener.rs
src/bin/otterlib.rs
src/gamestate.rs
src/hand.rs
src/hidden.rs
src/pcrender.rs
src/pieces.rs
src/shapelib.rs

index 0e107ae9ada321d4406aaed3057755f8c20c52cb..0bb08b9ce38c540bd799bd02b9f3524115c260c3 100644 (file)
@@ -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)?
     }
   }
 }
index 76b317cc9b3d612b73cf1292a812b9012aa01d8a..0a2a8f32e220a2162f583bca203038e44e664019 100644 (file)
@@ -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 {
index f0e254c12900572af863ff98cfa3f26097de1f0d..b3400384b7360290cf5410e08eea16f398b1f239 100644 (file)
@@ -64,8 +64,6 @@ pub enum OutputKind {
 
 pub type ItemForOutput = (String, ItemEnquiryData);
 
-const UNOCC: ShowUnocculted = ShowUnocculted::new_visible();
-
 #[throws(AE)]
 fn preview(items: Vec<ItemForOutput>) {
   const BORDER: f64 = 1.;
@@ -85,7 +83,7 @@ fn preview(items: Vec<ItemForOutput>) {
       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<ItemForOutput>) {
       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::<Vec<_>>();
       let spec = spec.clone();
@@ -133,7 +131,7 @@ fn preview(items: Vec<ItemForOutput>) {
     println!(r#"<th align="left"><kbd>{}</kbd></th>"#,
              Html::from_txt(&spec.item).0);
     println!(r#"<th align="left">{}</th>"#,
-             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<ItemForOutput>) {
                _ => 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<ItemForOutput>) {
         }
         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!("{}</svg>", html.0);
       }
       println!("</td>");
index b53139b748c2b16c0241c4cd92376085ffd74c57..6b8635da7b84b6f1b6f11624dd56d21979ae82c4 100644 (file)
@@ -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<UoDescription>,
-                       _gpc: &GPiece, _y: ShowUnocculted) { }
+                       _gpc: &GPiece) { }
 
   fn ui_operation(&self, _a: ApiPieceOpArgs<'_>,
-                  _opname: &str, _wrc: WhatResponseToClientOp,
-                  _y: ShowUnocculted)
+                  _opname: &str, _wrc: WhatResponseToClientOp)
                   -> Result<UpdateFromOpComplex, ApiPieceOpError> {
     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<Html,IE>;
+  fn describe_html(&self, gpc: &GPiece) -> Result<Html,IE>;
 
   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]
index 122b5142f28dfe26d261303d73c9970ef78c95d0..27eee032a9e94907ed21e2f4220a9519a380c9e6 100644 (file)
@@ -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::<HandState>()?;
@@ -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<UoDescription>,
-                       gpc: &GPiece, _: ShowUnocculted) {
+  fn add_ui_operations(&self, upd: &mut Vec<UoDescription>, gpc: &GPiece) {
     upd.push(if_chain! {
       if let Some(xdata) = gpc.xdata.get::<HandState>()?;
       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;
index 150810aa145c88df44156d00f7d7781801e432ee..8b652caff540bbc0ad428ce032e89137502b636f 100644 (file)
@@ -21,7 +21,6 @@ pub struct ShowUnocculted(());
 #[derive(Debug,Serialize,Deserialize)]
 #[serde(transparent)]
 pub struct IPieceTraitObj(Box<dyn PieceTrait>);
-deref_to_field!{IPieceTraitObj, Box<dyn PieceTrait>, 0} // xxx
 
 #[derive(Clone,Debug,Default,Serialize,Deserialize)]
 pub struct GameOccults {
@@ -243,6 +242,8 @@ impl IPieceTraitObj {
   pub fn show(&self, _: ShowUnocculted) -> &Box<dyn PieceTrait> {
     &self.0
   }
+
+  pub fn into_inner(self) -> Box<dyn PieceTrait> { 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!
index 25d30a848610a1faa1f116e8b32dc15eac1fcc11..e5c4d8113351eee714f7016eb524dea5609af6a0 100644 (file)
@@ -78,7 +78,7 @@ impl<P,Z> PriOccultedGeneral<P,Z> {
   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
   }
 }
index d0a387caaadc07b219c7f5f8a2dc5b1c93e4082c..e7ba1be6c27d9738c0d14fa86f5e9b715a0fcd6a 100644 (file)
@@ -129,12 +129,11 @@ impl<Desc, Outl:'static> OutlineTrait for GenericSimpleShape<Desc, Outl>
 #[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<Desc, Outl:'static> GenericSimpleShape<Desc, Outl>
@@ -156,7 +155,7 @@ impl<Desc, Outl:'static> GenericSimpleShape<Desc, Outl>
   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,
index b836952add8306e71c30c28e014e2b9ff178d8b1..54632a84103dabe84ca869b4681cc09288661b15 100644 (file)
@@ -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<Option<Registry>> = 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);
     }