-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow infinite count of items #656
Open
xsebek
wants to merge
26
commits into
main
Choose a base branch
from
infinity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4e615c6
WIP: Infinite inventory
xsebek 057adee
Add Number type with both infinities
xsebek a5aad65
Let Number prolify through the codebase
xsebek aae296c
Fix typo
xsebek 8284a59
Fix unit tests
xsebek 437f470
Fix accidental infinite loop
xsebek 4676a24
Handle addition failure correctly
xsebek d4b325c
Fix tests expecting old behavior
xsebek 94f47e8
Restyle with Fourmolu
xsebek 656b785
WIP: Fix the hashing for inventories
xsebek 47871b1
Remove redundant import
xsebek bd3f757
Fix hashing
xsebek c36368a
Add infinity constant
xsebek b20cbca
Fix error message
xsebek 450bbda
Add unit tests for infinity
xsebek e4df1a4
Use infinity in JSON
xsebek 0cdd6ba
Use infinity in entity requirements
xsebek 5d10af5
Hack around infinite requirement
xsebek a8da1ce
Test infinite requirement
xsebek 0cc6ddc
Restyle
xsebek 6c46bc3
Apply some hints
xsebek 4c02e2e
Fix typo
xsebek 21b075c
Fix typo
xsebek fec409e
Pretty print count in recipes
xsebek 00d2ba2
Fix exponentiation of infinity
xsebek 965dfe6
Make require pattern readable again
xsebek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Restyle with Fourmolu
- Loading branch information
commit 94f47e883987bf68349b413988b1ca4db87f2b80
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 |
---|---|---|
|
@@ -50,8 +50,9 @@ import Data.Tuple (swap) | |
import Linear (V2 (..), zero, (^+^)) | ||
import Swarm.Game.CESK | ||
import Swarm.Game.Display | ||
import Swarm.Game.Entity hiding (empty, lookup, singleton, union, Integer) | ||
import Swarm.Game.Entity qualified as E | ||
import Swarm.Game.Entity (Number (..)) | ||
import Swarm.Game.Entity hiding (Integer, empty, lookup, singleton, union) | ||
import Swarm.Game.Entity qualified as E | ||
import Swarm.Game.Exception | ||
import Swarm.Game.Recipe | ||
import Swarm.Game.Robot | ||
|
@@ -72,7 +73,6 @@ import System.Clock qualified | |
import System.Random (UniformRange, uniformR) | ||
import Witch (From (from), into) | ||
import Prelude hiding (lookup) | ||
import Swarm.Game.Entity (Number(..)) | ||
|
||
-- | The main function to do one game tick. The only reason we need | ||
-- @IO@ is so that robots can run programs loaded from files, via | ||
|
@@ -1377,7 +1377,6 @@ execConst c vs s k = do | |
let salvageInventory = E.union (target ^. robotInventory) (target ^. installedDevices) | ||
robotMap . at (target ^. robotID) . traverse . robotInventory .= salvageInventory | ||
|
||
|
||
-- Copy over the salvaged robot's log, if we have one | ||
inst <- use installedDevices | ||
em <- use entityMap | ||
|
@@ -1401,14 +1400,16 @@ execConst c vs s k = do | |
robotMap . at (target ^. robotID) . traverse . systemRobot .= True | ||
|
||
ourID <- use @Robot robotID | ||
let salvageItems = if system || creative | ||
then [] | ||
else concatMap (uncurry replicateCount) (E.elems salvageInventory) | ||
let salvageItems = | ||
if system || creative | ||
then [] | ||
else concatMap (uncurry replicateCount) (E.elems salvageInventory) | ||
numItems = length salvageItems | ||
replicateCount n e = e ^. entityName & case n of | ||
E.Integer x -> replicate (fromIntegral x) | ||
E.PosInfinity -> replicate 42 | ||
E.NegInfinity -> replicate (-42) | ||
replicateCount n e = | ||
e ^. entityName & case n of | ||
E.Integer x -> replicate (fromIntegral x) | ||
E.PosInfinity -> replicate 42 | ||
E.NegInfinity -> replicate (-42) | ||
|
||
-- The program for the salvaged robot to run | ||
let giveInventory = | ||
|
@@ -1989,13 +1990,13 @@ safeDiv i 0 = return $ signum i * PosInfinity | |
safeDiv (Integer a) (Integer b) = return . Integer $ a `div` b | ||
safeDiv (Integer _a) _ = return 0 | ||
safeDiv i b@(Integer _b) = return $ b * i | ||
safeDiv _ _ = throwError $ CmdFailed Div "Dividing infinities" | ||
safeDiv _ _ = throwError $ CmdFailed Div "Dividing infinities" | ||
|
||
-- | Perform exponentiation, but fail on negative powers and negative number to the poer of infinity. | ||
xsebek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
safeExp :: Has (Throw Exn) sig m => Number -> Number -> m Number | ||
safeExp = \case | ||
PosInfinity -> \b -> return $ PosInfinity * signum b | ||
NegInfinity -> \b -> return $ NegInfinity * signum b | ||
NegInfinity -> \b -> return $ NegInfinity * signum b | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well spotted. 👍 |
||
Integer a -> \case | ||
PosInfinity | ||
| a == 0 -> return 0 | ||
|
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops, this is a problem. How should we salvage infinite inventory? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On IRC we agreed that it should take 1 tick. The idea is that an infinite item is like an item generator so we move it like it had count 1.
There is still an issue with how to do that. I think some "game state update" on the stack of the reprogrammed salvaged robot could work. An internal
giveInfinite
function also makes sense.