From: Ian Jackson Date: Thu, 11 Feb 2021 19:07:44 +0000 (+0000) Subject: hidden: Move Visible into a variant of OccultationKind X-Git-Tag: otter-0.4.0~550 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=03199824973ee1eca873098d7fd8ea5089560f35;p=otter.git hidden: Move Visible into a variant of OccultationKind All these Options are very confusing. This gets rid of an extension trait too. Signed-off-by: Ian Jackson --- diff --git a/src/hidden.rs b/src/hidden.rs index 7592236e..e47f4881 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -29,60 +29,62 @@ pub struct Occultation { region: [Pos; 2], // automatically affect pieces here occulter: PieceId, // kept in synch with PieceOccult::active views: Vec, - defview: Option, + #[serde(default)] defview: OccultationKind, pieces: BTreeSet, // kept in synch with PieceOccult::passive } #[derive(Clone,Debug,Serialize,Deserialize)] pub struct OccultView { players: Vec, - occult: Option, + #[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 { 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 { - 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