chiark / gitweb /
hidden: Provide IPiece::show_or_instead
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 16 Mar 2021 13:28:57 +0000 (13:28 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 16 Mar 2021 13:28:57 +0000 (13:28 +0000)
For doing things when we don't have a PieceRenderInstructions - eg
when logging, which must be player-agnostic.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/hidden.rs
src/pcrender.rs

index 8b652caff540bbc0ad428ce032e89137502b636f..265348855305454e82f56c1e102c8ae1ee7b3a53 100644 (file)
@@ -246,6 +246,27 @@ impl IPieceTraitObj {
   pub fn into_inner(self) -> Box<dyn PieceTrait> { self.0 }
 }
 
+impl IPiece {
+  #[throws(IE)]
+  pub fn show_or_instead<'p>(&self, ioccults: &'p IOccults,
+                         y: Option<ShowUnocculted>)
+          -> Either<ShowUnocculted, &'p dyn OccultedPieceTrait> {
+    match y {
+      Some(y) => Left(y),
+      None => Right({
+        let occilk = self.occilk.as_ref()
+          .ok_or_else(|| internal_logic_error(format!(
+            "occulted non-occultable {:?}", self)))?
+          .borrow();
+        let occ_data = ioccults.ilks.get(occilk)
+          .ok_or_else(|| internal_logic_error(format!(
+            "occulted ilk vanished {:?} {:?}", self, occilk)))?;
+        occ_data.p_occ.as_ref()
+      }),
+    }
+  }
+}
+
 impl GPiece {
   pub fn fully_visible_to_everyone(&self) -> Option<ShowUnocculted> {
     match self.occult.passive {
index 27782caffccf12f108fafb3194119a321736333c..032067125a6b41a94ccfcdf9145e6677d9b784ea 100644 (file)
@@ -43,24 +43,17 @@ impl VisiblePieceAngle {
 
 impl<P,Z> PriOccultedGeneral<P,Z> {
   #[throws(IE)]
-  fn instead<'p>(&self, ioccults: &'p IOccults, p: &'p IPiece)
+  pub fn instead<'p>(&self, ioccults: &'p IOccults, p: &'p IPiece)
                  -> Either<ShowUnocculted, &'p dyn OccultedPieceTrait>
   {
+    p.show_or_instead(ioccults, self.fully_visible())?
+  }
+
+  pub fn fully_visible(&self) -> Option<ShowUnocculted> {
     use PriOG::*;
     match self {
-      Visible(v) => Left(*v),
-      Occulted | Displaced(..) => {
-        Right({
-          let occilk = p.occilk.as_ref()
-            .ok_or_else(|| internal_logic_error(format!(
-              "occulted non-occultable {:?}", p)))?
-            .borrow();
-          let occ_data = ioccults.ilks.get(occilk)
-            .ok_or_else(|| internal_logic_error(format!(
-              "occulted ilk vanished {:?} {:?}", p, occilk)))?;
-          occ_data.p_occ.as_ref()
-        })
-      },
+      Visible(v) => Some(*v),
+      Occulted | Displaced(..) => None,
     }
   }