Skip to content

Commit

Permalink
Cleanup 'use' command code (#1299)
Browse files Browse the repository at this point in the history
From review feedback in #1287
  • Loading branch information
kostmo committed Jun 4, 2023
1 parent abe0ae8 commit bdea72f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion data/scenarios/Testing/1007-use-command.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ entities:
char: 'k'
description:
- Can open a closed gate
capabilities: [drill]
properties: [known, portable]
- name: closed gate
display:
Expand Down
23 changes: 15 additions & 8 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,26 +2065,33 @@ execConst c vs s k = do
applyDevice ins "drill" d tool

applyDevice ins verbPhrase d tool = do
(nextLoc, nextE) <- getDrillTarget verbPhrase d
(nextLoc, nextE) <- getDeviceTarget verbPhrase d
inRs <- use recipesIn

let recipes = filter drilling (recipesFor inRs nextE)
drilling = any ((== tool) . snd) . view recipeRequirements
let recipes = filter isApplicableRecipe (recipesFor inRs nextE)
isApplicableRecipe = any ((== tool) . snd) . view recipeRequirements

not (null recipes) `holdsOrFail` ["There is no way to", verbPhrase, indefinite (nextE ^. entityName) <> "."]
not (null recipes)
`holdsOrFail` [ "There is no way to"
, verbPhrase
, indefinite (nextE ^. entityName) <> "."
]

inv <- use robotInventory

-- add the targeted entity so it can be consumed by the recipe
let makeRecipe r = (,r) <$> make' (insert nextE inv, ins) r
chosenRecipe <- weightedChoice (\((_, _), r) -> r ^. recipeWeight) (rights (map makeRecipe recipes))
chosenRecipe <-
weightedChoice (\((_, _), r) -> r ^. recipeWeight) $
rights $
map makeRecipe recipes
((invTaken, outs), recipe) <-
chosenRecipe
`isJustOrFail` ["You don't have the ingredients to", verbPhrase, indefinite (nextE ^. entityName) <> "."]

let (out, down) = L.partition ((`hasProperty` Portable) . snd) outs
let learn = map (LearnEntity . snd) down
let gain = map (uncurry AddEntity) out
learn = map (LearnEntity . snd) down
gain = map (uncurry AddEntity) out

newEntity <- case down of
[] -> pure Nothing
Expand All @@ -2103,7 +2110,7 @@ execConst c vs s k = do
let cmdOutput = asValue $ snd <$> listToMaybe out
finishCookingRecipe recipe cmdOutput [changeWorld] (learn <> gain)

getDrillTarget verb d = do
getDeviceTarget verb d = do
rname <- use robotName

(nextLoc, nextME) <- lookInDirection d
Expand Down

0 comments on commit bdea72f

Please sign in to comment.