From: Ian Jackson Date: Fri, 22 Apr 2022 18:26:29 +0000 (+0100) Subject: Introduce PieceLoadArgs X-Git-Tag: otter-1.1.0~429 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e783bccaf759ecc5c7386a7e0d2b4a62412dacea;p=otter.git Introduce PieceLoadArgs So that we can add more parameters to PieceTrait::load() without having to edit everywhere *again*. In fact it turns out I don't need to do this right now (for currency and fastsplit) but I think it's worth keeping. Signed-off-by: Ian Jackson --- diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 3d49123d..73a0e766 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -1118,7 +1118,12 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( rotateable: true, }; let SpecLoaded { p, occultable, special } = - info.load(piece_i as usize, &mut gpc, ig, SpecDepth::zero())?; + info.load(PLA { + i: piece_i as usize, + gpc: &mut gpc, + ig, + depth: SpecDepth::zero(), + })?; if p.nfaces() <= face.into() { throw!(SpecError::FaceNotFound); } diff --git a/src/clock.rs b/src/clock.rs index 95eabb7f..b8c49bc9 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -404,8 +404,7 @@ fn unprepared_update(piece: PieceId) -> UnpreparedUpdates { #[typetag::serde(name="ChessClock")] impl PieceSpec for Spec { #[throws(SpecError)] - fn load(&self, _: usize, gpc: &mut GPiece, _ig: &Instance, _:SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { gpc,.. }: PLA) -> SpecLoaded { if self.time <= 0 { throw!(SpecError::NegativeTimeout) } let clock = Clock { diff --git a/src/currency.rs b/src/currency.rs index 1a69fc22..b427e248 100644 --- a/src/currency.rs +++ b/src/currency.rs @@ -44,8 +44,7 @@ impl PieceXData for Value { fn dummy() -> Self { Value { qty: 0 } } } #[typetag::serde(name="Currency")] impl PieceSpec for Spec { #[throws(SpecError)] - fn load(&self, _: usize, gpc: &mut GPiece, ig: &Instance, depth: SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { gpc,ig,depth,.. }: PLA) -> SpecLoaded { gpc.rotateable = false; let Spec { ref image, ref currency, qty } = *self; diff --git a/src/deck.rs b/src/deck.rs index 7949caee..006af8d0 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -37,8 +37,7 @@ impl OutlineTrait for Deck { #[typetag::serde(name="PickupDeck")] impl PieceSpec for piece_specs::Deck { #[throws(SpecError)] - fn load(&self, _: usize, gpc: &mut GPiece, _ig: &Instance, _:SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { gpc,.. }: PLA) -> SpecLoaded { let common = SimpleCommon { itemname: None, faces: self.faces.clone(), diff --git a/src/dice.rs b/src/dice.rs index 4b8b388a..5026eaad 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -100,8 +100,7 @@ struct OverlayTemplateContext<'c> { #[typetag::serde(name="Die")] impl PieceSpec for Spec { #[throws(SpecError)] - fn load(&self, _: usize, gpc: &mut GPiece, ig: &Instance, depth: SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { gpc,ig,depth,.. }: PLA) -> SpecLoaded { gpc.rotateable = false; let SpecLoadedInert { p: image, occultable: img_occultable } = diff --git a/src/gamestate.rs b/src/gamestate.rs index 54045328..523eb8a8 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -313,12 +313,18 @@ pub struct PieceSpecialProperties { pub multigrab: bool, } +pub struct PieceLoadArgs<'a> { + pub i: usize, + pub gpc: &'a mut GPiece, + pub ig: &'a Instance, + pub depth: SpecDepth, +} + #[typetag::serde(tag="type")] pub trait PieceSpec: Debug + Sync + Send + 'static { #[throws(SpecError)] fn count(&self, _pcaliases: &PieceAliases) -> usize { 1 } - fn load(&self, i: usize, gpc: &mut GPiece, ig: &Instance, depth: SpecDepth) - -> Result; + fn load(&self, pla: PieceLoadArgs<'_>) -> Result; /// Used when a piece wants to use another for its occulted form fn load_inert(&self, _ig: &Instance, _:SpecDepth) -> Result { diff --git a/src/hand.rs b/src/hand.rs index a6c02802..23972717 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -119,8 +119,7 @@ impl piece_specs::OwnedCommon { #[typetag::serde] impl PieceSpec for piece_specs::Hand { #[throws(SpecError)] - fn load(&self, _: usize, gpc: &mut GPiece, _ig: &Instance, _:SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { gpc,..}: PLA) -> SpecLoaded { gpc.moveable = PieceMoveable::IfWresting; gpc.rotateable = false; self.c.load(Behaviour::Hand)? @@ -130,8 +129,7 @@ impl PieceSpec for piece_specs::Hand { #[typetag::serde] impl PieceSpec for piece_specs::PlayerLabel { #[throws(SpecError)] - fn load(&self, _: usize, _: &mut GPiece, _ig: &Instance, _:SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { .. }: PLA) -> SpecLoaded { self.c.load(Behaviour::PlayerLabel)? } } diff --git a/src/pcaliases.rs b/src/pcaliases.rs index e123adb8..84b0788f 100644 --- a/src/pcaliases.rs +++ b/src/pcaliases.rs @@ -64,10 +64,9 @@ impl PieceSpec for Alias { self.resolve(pcaliases)?.count(&default())? } #[throws(SpecError)] - fn load(&self, i: usize, gpc: &mut GPiece, ig: &Instance, depth: SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { i,gpc,ig,depth,.. }: PLA) -> SpecLoaded { let r = self.resolve(&ig.pcaliases)? - .load(i, gpc, ig, self.new_depth(depth)?)?; + .load(PLA { i, gpc, ig, depth: self.new_depth(depth)? })?; r } #[throws(SpecError)] diff --git a/src/pieces.rs b/src/pieces.rs index cf52fb5e..b0927596 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -353,8 +353,7 @@ macro_rules! impl_PieceSpec_for_SimplePieceSpec { { $ty:ty } => { #[typetag::serde] impl PieceSpec for $ty { #[throws(SpecError)] - fn load(&self, _: usize, _: &mut GPiece, _ig: &Instance, _:SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { .. }: PLA) -> SpecLoaded { SpecLoaded { p: Box::new(self.load_raw()?.0), occultable: None, diff --git a/src/prelude.rs b/src/prelude.rs index 20c0f970..c03b62f4 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -216,6 +216,9 @@ pub type POEPP = PieceOpErrorPartiallyProcessed; pub type SvgE = SVGProcessingError; pub type SpE = SpecError; +// gamestate.rs +pub use PieceLoadArgs as PLA; + // hidden.rs pub type OccK = OccultationKind; pub use OccultationKindGeneral as OccKG; diff --git a/src/shapelib.rs b/src/shapelib.rs index 98df2054..f1e6bef6 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -654,8 +654,7 @@ impl Contents { #[typetag::serde(name="Lib")] impl PieceSpec for ItemSpec { #[throws(SpecError)] - fn load(&self, _: usize, _: &mut GPiece, ig: &Instance, depth: SpecDepth) - -> SpecLoaded { + fn load(&self, PLA { ig,depth,.. }: PLA) -> SpecLoaded { self.find_load(ig,depth)?.into() } #[throws(SpecError)] @@ -671,8 +670,7 @@ impl PieceSpec for MultiSpec { fn count(&self, _pcaliases: &PieceAliases) -> usize { self.items.len() } #[throws(SpecError)] - fn load(&self, i: usize, _: &mut GPiece, ig: &Instance, depth:SpecDepth) - -> SpecLoaded + fn load(&self, PLA { i,ig,depth,.. }: PLA) -> SpecLoaded { let item = self.items.get(i).ok_or_else( || SpE::InternalError(format!("item {:?} from {:?}", i, &self))