-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use rich entity modification info (#1604)
- Loading branch information
Showing
4 changed files
with
53 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- | ||
-- Captures the various possibilities of cell | ||
-- modification as a sum type for use by the structure recognizer | ||
-- (see 'Swarm.Game.Scenario.Topography.Structure.Recognition.Tracking.entityModified'). | ||
module Swarm.Game.World.Modify where | ||
|
||
import Control.Lens (view) | ||
import Data.Function (on) | ||
import Swarm.Game.Entity (Entity, entityHash) | ||
|
||
-- | Compare to 'WorldUpdate' in "Swarm.Game.World" | ||
data CellUpdate e | ||
= NoChange (Maybe e) | ||
| Modified (CellModification e) | ||
|
||
getModification :: CellUpdate e -> Maybe (CellModification e) | ||
getModification (NoChange _) = Nothing | ||
getModification (Modified x) = Just x | ||
|
||
data CellModification e | ||
= -- | Fields represent what existed in the cell "before" and "after", in that order. | ||
Swap e e | ||
| Remove e | ||
| Add e | ||
|
||
classifyModification :: | ||
-- | before | ||
Maybe Entity -> | ||
-- | after | ||
Maybe Entity -> | ||
CellUpdate Entity | ||
classifyModification Nothing Nothing = NoChange Nothing | ||
classifyModification Nothing (Just x) = Modified $ Add x | ||
classifyModification (Just x) Nothing = Modified $ Remove x | ||
classifyModification (Just x) (Just y) = | ||
if ((/=) `on` view entityHash) x y | ||
then Modified $ Swap x y | ||
else NoChange $ Just x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters