@@ -47,11 +47,8 @@ module Swarm.Game.State (
47
47
currentScenarioPath ,
48
48
needsRedraw ,
49
49
replWorking ,
50
- applyViewCenterRule ,
51
- recalcViewCenter ,
52
- modifyViewCenter ,
50
+ recalcViewCenterAndRedraw ,
53
51
viewingRegion ,
54
- unfocus ,
55
52
focusedRobot ,
56
53
RobotRange (.. ),
57
54
focusedRange ,
@@ -84,8 +81,8 @@ import Data.Bifunctor (first)
84
81
import Data.Digest.Pure.SHA (sha1 , showDigest )
85
82
import Data.Foldable (toList )
86
83
import Data.Foldable.Extra (allM )
84
+ import Data.Function (on )
87
85
import Data.Int (Int32 )
88
- import Data.IntMap (IntMap )
89
86
import Data.IntMap qualified as IM
90
87
import Data.IntSet qualified as IS
91
88
import Data.List (partition , sortOn )
@@ -323,51 +320,19 @@ messageIsFromNearby l e = case e ^. leSource of
323
320
InfinitelyFar -> False
324
321
Measurable x -> x <= hearingDistance
325
322
326
- -- | Given a current mapping from robot names to robots, apply a
327
- -- 'ViewCenterRule' to derive the location it refers to. The result
328
- -- is 'Maybe' because the rule may refer to a robot which does not
329
- -- exist.
330
- applyViewCenterRule :: ViewCenterRule -> IntMap Robot -> Maybe (Cosmic Location )
331
- applyViewCenterRule (VCLocation l) _ = Just l
332
- applyViewCenterRule (VCRobot name) m = m ^? at name . _Just . robotLocation
333
-
334
323
-- | Recalculate the view center (and cache the result in the
335
324
-- 'viewCenter' field) based on the current 'viewCenterRule'. If
336
325
-- the 'viewCenterRule' specifies a robot which does not exist,
337
326
-- simply leave the current 'viewCenter' as it is. Set 'needsRedraw'
338
327
-- if the view center changes.
339
- recalcViewCenter :: GameState -> GameState
340
- recalcViewCenter g =
328
+ recalcViewCenterAndRedraw :: GameState -> GameState
329
+ recalcViewCenterAndRedraw g =
341
330
g
342
- { _robotInfo =
343
- (g ^. robotInfo)
344
- { _viewCenter = newViewCenter
345
- }
346
- }
347
- & (if newViewCenter /= oldViewCenter then needsRedraw .~ True else id )
331
+ & robotInfo .~ newRobotInfo
332
+ & (if ((/=) `on` (^. viewCenter)) oldRobotInfo newRobotInfo then needsRedraw .~ True else id )
348
333
where
349
- oldViewCenter = g ^. robotInfo . viewCenter
350
- newViewCenter =
351
- fromMaybe oldViewCenter $
352
- applyViewCenterRule (g ^. robotInfo . viewCenterRule) (g ^. robotInfo . robotMap)
353
-
354
- -- | Modify the 'viewCenter' by applying an arbitrary function to the
355
- -- current value. Note that this also modifies the 'viewCenterRule'
356
- -- to match. After calling this function the 'viewCenterRule' will
357
- -- specify a particular location, not a robot.
358
- modifyViewCenter :: (Cosmic Location -> Cosmic Location ) -> GameState -> GameState
359
- modifyViewCenter update g =
360
- g
361
- & case g ^. robotInfo . viewCenterRule of
362
- VCLocation l -> robotInfo . viewCenterRule .~ VCLocation (update l)
363
- VCRobot _ -> robotInfo . viewCenterRule .~ VCLocation (update (g ^. robotInfo . viewCenter))
364
-
365
- -- | "Unfocus" by modifying the view center rule to look at the
366
- -- current location instead of a specific robot, and also set the
367
- -- focused robot ID to an invalid value. In classic mode this
368
- -- causes the map view to become nothing but static.
369
- unfocus :: GameState -> GameState
370
- unfocus = (\ g -> g {_robotInfo = (g ^. robotInfo) {_focusedRobotID = - 1000 }}) . modifyViewCenter id
334
+ oldRobotInfo = g ^. robotInfo
335
+ newRobotInfo = recalcViewCenter oldRobotInfo
371
336
372
337
-- | Given a width and height, compute the region, centered on the
373
338
-- 'viewCenter', that should currently be in view.
@@ -663,10 +628,7 @@ pureScenarioToGameState scenario theSeed now toRun gsc =
663
628
gs = initGameState gsc
664
629
preliminaryGameState =
665
630
gs
666
- & robotInfo .~ (gs ^. robotInfo) {_focusedRobotID = baseID}
667
- & robotInfo %~ setRobotList robotList'
668
- & robotInfo . viewCenterRule .~ VCRobot baseID
669
- & robotInfo . robotNaming . gensym .~ initGensym
631
+ & robotInfo %~ setRobotInfo baseID robotList'
670
632
& creativeMode .~ scenario ^. scenarioCreative
671
633
& winCondition .~ theWinCondition
672
634
& winSolution .~ scenario ^. scenarioSolution
@@ -756,7 +718,6 @@ pureScenarioToGameState scenario theSeed now toRun gsc =
756
718
(\ x -> WinConditions Ongoing (ObjectiveCompletion (CompletionBuckets (NE. toList x) mempty mempty ) mempty ))
757
719
(NE. nonEmpty (scenario ^. scenarioObjectives))
758
720
759
- initGensym = length robotList - 1
760
721
addRecipesWith f = IM. unionWith (<>) (f $ scenario ^. scenarioRecipes)
761
722
762
723
-- | Create an initial game state corresponding to the given scenario.
0 commit comments