Skip to content

Commit 13b7411

Browse files
Use the new knowsIngredientsFor helper
1 parent 121eb66 commit 13b7411

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/Swarm/Game/Recipe.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Swarm.Game.Recipe (
3434

3535
-- * Looking up recipes
3636
missingIngredientsFor,
37+
knowsIngredientsFor,
3738
recipesFor,
3839
make,
3940
make',
@@ -187,6 +188,13 @@ missingIngredientsFor (inv, ins) (Recipe inps _ reqs _ _) =
187188
findLacking inven = filter ((> 0) . fst) . map (countNeeded inven)
188189
countNeeded inven (need, entity) = (need - E.lookup entity inven, entity)
189190

191+
-- | Figure out if a recipe is available, but it can be lacking items.
192+
knowsIngredientsFor :: (Inventory, Inventory) -> Recipe Entity -> Bool
193+
knowsIngredientsFor (inv, ins) recipe =
194+
knowsAll inv (recipe ^. recipeInputs) && knowsAll ins (recipe ^. recipeRequirements)
195+
where
196+
knowsAll xs = all (E.contains xs . snd)
197+
190198
-- | Try to make a recipe, deleting the recipe's inputs from the
191199
-- inventory. Return either a description of which items are
192200
-- lacking, if the inventory does not contain sufficient inputs,

src/Swarm/Game/Step.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ updateDiscoveredEntities e = do
16421642
if E.contains0plus e allDiscovered
16431643
then pure ()
16441644
else do
1645-
let newAllDiscovered = E.insertCount 9001 e allDiscovered
1645+
let newAllDiscovered = E.insertCount 1 e allDiscovered
16461646
allDevices <- use allInstalledDevices
16471647
updateAvailableRecipes (newAllDiscovered, allDevices) e
16481648
allDiscoveredEntities .= newAllDiscovered
@@ -1654,7 +1654,7 @@ updateInstalledDevice e = do
16541654
if E.contains0plus e allDevices
16551655
then pure ()
16561656
else do
1657-
let newAllDevices = E.insertCount 9001 e allDevices
1657+
let newAllDevices = E.insertCount 1 e allDevices
16581658
-- Check every known entities to see if the new device enables new recipes
16591659
allDiscovered <- use allDiscoveredEntities
16601660
traverse_ (updateAvailableRecipes (allDiscovered, newAllDevices) . snd) (elems allDiscovered)
@@ -1672,10 +1672,7 @@ updateAvailableRecipes :: Has (State GameState) sig m => (Inventory, Inventory)
16721672
updateAvailableRecipes invs e = do
16731673
allInRecipes <- use recipesIn
16741674
let entityRecipes = recipesFor allInRecipes e
1675-
let canMake recipe = case missingIngredientsFor invs recipe of
1676-
[] -> True
1677-
_ -> False
1678-
let usableRecipes = filter canMake entityRecipes
1675+
usableRecipes = filter (knowsIngredientsFor invs) entityRecipes
16791676
(knownLength, knownRecipes) <- use availableRecipes
16801677
let newRecipes = filter (`notElem` knownRecipes) usableRecipes
16811678
newLength = case newRecipes of

0 commit comments

Comments
 (0)