chiark / gitweb /
Provide remove_occultation
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Feb 2021 22:27:48 +0000 (22:27 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Feb 2021 22:53:41 +0000 (22:53 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/hidden.rs

index 92e8af03647027d63a37af90d7d38aa3f1a9a488..ec3a17bc47982b5648a30eee417ae01ef457fbaf 100644 (file)
@@ -530,3 +530,34 @@ pub fn create_occultation<V: OccultationViewDef>(
 
   updates
 }
+
+#[throws(IE)]
+pub fn remove_occultation(
+  gs: &mut GameState,
+  ipieces: &PiecesLoaded,
+  occid: OccId,
+) -> Vec<(PieceId, PieceUpdateOps)> {
+  let occultation = gs.occults.occults.remove(occid).ok_or_else(
+    || internal_logic_error("removing nonexistent occultation"))?;
+
+  let mut updates = vec![];
+  let mut aggerr = AggregatedIE::new();
+  for &ppiece in &occultation.pieces {
+    recalculate_occultation_ofmany(gs, ipieces, ppiece, &mut updates)
+      .unwrap_or_else(|e| {
+        aggerr.record(e);
+        if let Some(pgpc) = gs.pieces.get_mut(ppiece) {
+          pgpc.occult.passive = None;
+        }
+      });
+  }
+
+  if let Some(ogpc) = gs.pieces.get_mut(occultation.occulter) {
+    ogpc.occult.active = None;
+  } else {
+    aggerr.record(internal_logic_error("removing occultation of non-piece"));
+  }
+
+  aggerr.ok()?;
+  updates
+}