Skip to content

Commit 4e242c1

Browse files
committed
improved recipe lists
- Add recipes where the entity is a catalyst to the recipes shown for an entity - Order recipes so we get recipes where the entity is a (1) input (2) catalyst (3) output, in that order. Closes #418.
1 parent 3866fd6 commit 4e242c1

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/Swarm/Game/Recipe.hs

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Swarm.Game.Recipe (
2525
loadRecipes,
2626
outRecipeMap,
2727
inRecipeMap,
28+
reqRecipeMap,
2829

2930
-- * Looking up recipes
3031
MissingIngredient (..),
@@ -164,17 +165,21 @@ buildRecipeMap select recipeList =
164165
outRecipeMap :: [Recipe Entity] -> IntMap [Recipe Entity]
165166
outRecipeMap = buildRecipeMap recipeOutputs
166167

168+
-- | Build a map of recipes indexed by input ingredients.
169+
inRecipeMap :: [Recipe Entity] -> IntMap [Recipe Entity]
170+
inRecipeMap = buildRecipeMap recipeInputs
171+
172+
-- | Build a map of recipes indexed by requirements.
173+
reqRecipeMap :: [Recipe Entity] -> IntMap [Recipe Entity]
174+
reqRecipeMap = buildRecipeMap recipeRequirements
175+
167176
-- | Get a list of all the recipes for the given entity. Look up an
168177
-- entity in either an 'inRecipeMap' or 'outRecipeMap' depending on
169178
-- whether you want to know recipes that consume or produce the
170179
-- given entity, respectively.
171180
recipesFor :: IntMap [Recipe Entity] -> Entity -> [Recipe Entity]
172181
recipesFor rm e = fromMaybe [] $ IM.lookup (e ^. entityHash) rm
173182

174-
-- | Build a map of recipes indexed by input ingredients.
175-
inRecipeMap :: [Recipe Entity] -> IntMap [Recipe Entity]
176-
inRecipeMap = buildRecipeMap recipeInputs
177-
178183
data MissingIngredient = MissingIngredient MissingType Count Entity
179184
deriving (Show, Eq)
180185

src/Swarm/Game/State.hs

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module Swarm.Game.State (
5151
entityMap,
5252
recipesOut,
5353
recipesIn,
54+
recipesReq,
5455
scenarios,
5556
knownEntities,
5657
world,
@@ -135,6 +136,7 @@ import Swarm.Game.Recipe (
135136
inRecipeMap,
136137
loadRecipes,
137138
outRecipeMap,
139+
reqRecipeMap,
138140
)
139141
import Swarm.Game.Robot
140142
import Swarm.Game.Scenario
@@ -270,6 +272,7 @@ data GameState = GameState
270272
, _entityMap :: EntityMap
271273
, _recipesOut :: IntMap [Recipe Entity]
272274
, _recipesIn :: IntMap [Recipe Entity]
275+
, _recipesReq :: IntMap [Recipe Entity]
273276
, _scenarios :: ScenarioCollection
274277
, _knownEntities :: [Text]
275278
, _world :: W.World Int Entity
@@ -399,6 +402,9 @@ recipesOut :: Lens' GameState (IntMap [Recipe Entity])
399402
-- | All recipes the game knows about, indexed by inputs.
400403
recipesIn :: Lens' GameState (IntMap [Recipe Entity])
401404

405+
-- | All recipes the game knows about, indexed by requirement/catalyst.
406+
recipesReq :: Lens' GameState (IntMap [Recipe Entity])
407+
402408
-- | The collection of scenarios that comes with the game.
403409
scenarios :: Lens' GameState ScenarioCollection
404410

@@ -679,6 +685,7 @@ initGameState = do
679685
, _entityMap = entities
680686
, _recipesOut = outRecipeMap recipes
681687
, _recipesIn = inRecipeMap recipes
688+
, _recipesReq = reqRecipeMap recipes
682689
, _scenarios = loadedScenarios
683690
, _knownEntities = []
684691
, _world = W.emptyWorld (fromEnum StoneT)
@@ -726,6 +733,7 @@ scenarioToGameState scenario userSeed toRun g = do
726733
, _entityMap = em
727734
, _recipesOut = addRecipesWith outRecipeMap recipesOut
728735
, _recipesIn = addRecipesWith inRecipeMap recipesIn
736+
, _recipesReq = addRecipesWith reqRecipeMap recipesReq
729737
, _knownEntities = scenario ^. scenarioKnown
730738
, _world = theWorld theSeed
731739
, _viewCenterRule = VCRobot baseID

src/Swarm/TUI/View.hs

+13-1
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,22 @@ explainRecipes s e
870870
maximumOf (traverse . recipeOutputs . traverse . to width) recipes
871871
widthLimit = 2 * max maxInputWidth maxOutputWidth + 11
872872

873+
-- | Return all recipes that involve a given entity.
873874
recipesWith :: AppState -> Entity -> [Recipe Entity]
874875
recipesWith s e =
875876
let getRecipes select = recipesFor (s ^. gameState . select) e
876-
in L.nub $ getRecipes recipesOut ++ getRecipes recipesIn
877+
in -- The order here is chosen intentionally. See https://github.com/swarm-game/swarm/issues/418.
878+
--
879+
-- 1. Recipes where the entity is an input --- these should go
880+
-- first since the first thing you will want to know when you
881+
-- obtain a new entity is what you can do with it.
882+
--
883+
-- 2. Recipes where it serves as a catalyst --- for the same reason.
884+
--
885+
-- 3. Recipes where it is an output --- these should go last,
886+
-- since if you have it, you probably already figured out how
887+
-- to make it.
888+
L.nub $ getRecipes recipesIn ++ getRecipes recipesReq ++ getRecipes recipesOut
877889

878890
-- | Draw an ASCII art representation of a recipe. For now, the
879891
-- weight is not shown.

0 commit comments

Comments
 (0)