chiark / gitweb /
hidden: Move Visible into a variant of OccultationKind
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 11 Feb 2021 19:07:44 +0000 (19:07 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 12 Feb 2021 01:38:24 +0000 (01:38 +0000)
All these Options are very confusing.  This gets rid of an extension
trait too.

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

index 7592236eed77dc6b18d6281822b777e8fdc57475..e47f48814007569c735325b6cabd8610282b00a5 100644 (file)
@@ -29,60 +29,62 @@ pub struct Occultation {
   region: [Pos; 2], // automatically affect pieces here
   occulter: PieceId, // kept in synch with PieceOccult::active
   views: Vec<OccultView>,
-  defview: Option<OccultationKind>,
+  #[serde(default)] defview: OccultationKind,
   pieces: BTreeSet<PieceId>, // kept in synch with PieceOccult::passive
 }
 
 #[derive(Clone,Debug,Serialize,Deserialize)]
 pub struct OccultView {
   players: Vec<PlayerId>,
-  occult: Option<OccultationKind>,
+  #[serde(default)] occult: OccultationKind,
 }
 
 #[derive(Clone,Copy,Debug,Serialize,Deserialize)]
 #[derive(Eq,PartialEq)]
 pub enum OccultationKind {
+  Visible,
   Scrambled,
   Displaced { within: Area },
   Invisible,
 }
 
+impl Default for OccultationKind {
+  fn default() -> Self { OccK::Visible }
+}
+
 impl PartialOrd for OccultationKind {
   fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
     fn level(k: &OccK) -> u8 { use OccultationKind::*; match k {
-      Scrambled     => 0,
-      Displaced{..} => 1,
-      Invisible     => 2,
+      Visible       => 0,
+      Scrambled     => 1,
+      Displaced{..} => 2,
+      Invisible     => 3,
     } }
 
     level(self).partial_cmp(&level(rhs))
   }
 }
 
-trait OccOptExt {
-  fn at_all_visible(&self) -> bool;
-}
-
-impl OccOptExt for Option<OccultationKind> {
-  fn at_all_visible(&self) -> bool {
+impl OccultationKind {
+  fn _at_all_visible(&self) -> bool {
     match self {
-      None |
-      Some(OccK::Scrambled) |
-      Some(OccK::Displaced { .. })
+      OccK::Visible |
+      OccK::Scrambled |
+      OccK::Displaced { .. }
         => false,
-      Some(OccK::Invisible)
+      OccK::Invisible
         => true,
     }
   }
 }
 
 impl Occultation {
-  pub fn get_kind(&self, player: PlayerId) -> Option<&OccultationKind> {
+  pub fn get_kind(&self, player: PlayerId) -> &OccultationKind {
     let kind = self.views.iter().find_map(|view| {
-      if view.players.contains(&player) { return Some(view.occult.as_ref()); }
+      if view.players.contains(&player) { return Some(&view.occult); }
       None
     }).unwrap_or(
-      self.defview.as_ref()
+      &self.defview
     );
     kind
   }
@@ -96,8 +98,7 @@ impl GameOccults {
   }
 
   #[throws(IE)]
-  pub fn get_kind(&self, occid: OccId, player: PlayerId)
-              -> Option<&OccultationKind> {
+  pub fn get_kind(&self, occid: OccId, player: PlayerId) -> &OccultationKind {
     let occ = self.by_id(occid)?;
     let kind = occ.get_kind(player);
     kind