Skip to content

Commit

Permalink
Child robots only inherit their parent's displayAttr (#1722)
Browse files Browse the repository at this point in the history
Fixes #1693.  It's not necessarily appropriate to copy the entire `Display` record.  So instead, we explicitly list the fields that should be inherited.  For now, it is only `displayAttr`, but we could add `invisible` in the future (see #1670, #1663).

Tested to make sure behavior is preserved with:
```
scripts/play.sh -i scenarios/Challenges/Ranching/beekeeping.yaml --autoplay
```
  • Loading branch information
byorgey authored Jan 14, 2024
1 parent ded3c24 commit 5adc8c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/swarm-engine/Swarm/Game/Step/Const.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import Swarm.Language.Value
import Swarm.Log
import Swarm.Util hiding (both)
import Swarm.Util.Effect (throwToMaybe)
import Swarm.Util.Lens (inherit)
import Witch (From (from), into)
import Prelude hiding (Applicative (..), lookup)

Expand Down Expand Up @@ -1052,7 +1053,9 @@ execConst runChildProg c vs s k = do
( ((r ^. robotOrientation) >>= \dir -> guard (dir /= zero) >> return dir)
? north
)
((r ^. robotDisplay) & invisible .~ False)
( defaultRobotDisplay
& inherit displayAttr (r ^. robotDisplay)
)
(In cmd e s [FExec])
[]
[]
Expand Down
7 changes: 7 additions & 0 deletions src/swarm-util/Swarm/Util/Lens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
module Swarm.Util.Lens (
makeLensesNoSigs,
makeLensesExcluding,
inherit,
) where

import Control.Lens (
Lens',
generateSignatures,
lensField,
lensRules,
Expand All @@ -16,6 +18,7 @@ import Control.Lens (
(%~),
(&),
(.~),
(^.),
)
import Language.Haskell.TH (DecsQ)
import Language.Haskell.TH.Syntax (Name)
Expand All @@ -40,3 +43,7 @@ makeLensesExcluding exclude =
& lensField . mapped . mapped %~ \fn n ->
if n `elem` exclude then [] else fn n
)

-- | Copy a given field from one record to another.
inherit :: Lens' s a -> s -> (s -> s)
inherit field parent child = child & field .~ (parent ^. field)

0 comments on commit 5adc8c7

Please sign in to comment.