Permalink
Browse files

move the State argument to the rear, to work well with gets

  • Loading branch information...
1 parent cf8765a commit 2eb1d33380d4562eaba068e43d825afd316f59d5 @Mikolaj Mikolaj committed Mar 23, 2011
Showing with 30 additions and 32 deletions.
  1. +17 −19 src/Actions.hs
  2. +11 −11 src/MovableState.hs
  3. +1 −1 src/Perception.hs
  4. +1 −1 src/StrategyState.hs
View
@@ -301,9 +301,9 @@ actorOpenClose actor v o dir =
state <- get
lmap <- gets (lmap . slevel)
pl <- gets splayer
+ body <- gets (getActor actor)
let txt = if o then "open" else "closed"
let hms = levelHeroList state ++ levelMonsterList state
- let body = getActor state actor -- TODO: swap args of getActor, for gets
let loc = mloc body
let isPlayer = actor == pl
let isVerbose = v && isPlayer
@@ -561,7 +561,7 @@ setCursor tgt = do
ploc <- gets (mloc . getPlayerBody)
ln <- gets (lname . slevel)
let upd cursor =
- let cloc = case targetToLoc state (ptvisible per) of
+ let cloc = case targetToLoc (ptvisible per) state of
Nothing -> ploc
Just l -> l
in cursor { ctargeting = True, clocation = cloc, clocLn = ln }
@@ -661,11 +661,11 @@ fireItem = do
Just (dart, _) -> do
let fired = dart { icount = 1 }
removeFromInventory fired
- case targetToLoc state (ptvisible per) of
+ case targetToLoc (ptvisible per) state of
Nothing -> abortWith "target invalid"
Just loc ->
if actorReachesLoc pl loc per pl
- then case locToActor state loc of
+ then case locToActor loc state of
Just ta -> actorDamageActor pl ta 1 " with a dart"
Nothing -> modify (updateLevel (scatterItems [fired] loc))
else abortWith "target not reachable"
@@ -682,11 +682,11 @@ applyItem = do
Just (wand, _) -> do
let applied = wand { icount = 1 }
removeFromInventory applied
- case targetToLoc state (ptvisible per) of
+ case targetToLoc (ptvisible per) state of
Nothing -> abortWith "target invalid"
Just loc ->
if actorReachesLoc pl loc per pl
- then case locToActor state loc of
+ then case locToActor loc state of
Just ta -> selectPlayer ta >> return ()
Nothing -> abortWith "no living target to affect"
else abortWith "target not reachable"
@@ -747,7 +747,7 @@ actorPickupItem actor =
pl <- gets splayer
per <- currentPerception
lmap <- gets (lmap . slevel)
- let movable = getActor state actor
+ movable <- gets (getActor actor)
let loc = mloc movable
let t = lmap `at` loc -- the map tile in question
let perceived = loc `S.member` ptvisible per
@@ -859,10 +859,10 @@ moveOrAttack allowAttacks autoOpen actor dir
state <- get
pl <- gets splayer
lmap <- gets (lmap . slevel)
- let sm = getActor state actor
- sloc = mloc sm -- source location
+ sm <- gets (getActor actor)
+ let sloc = mloc sm -- source location
tloc = sloc `shift` dir -- target location
- tgt = locToActor state tloc
+ tgt <- gets (locToActor tloc)
case tgt of
Just target ->
if allowAttacks then
@@ -895,9 +895,8 @@ actorAttackActor (AHero _) target@(AHero _) =
-- Select adjacent hero by bumping into him. Takes no time.
selectPlayer target >> return ()
actorAttackActor source target = do
- state <- get
- let sm = getActor state source
- -- Determine the weapon used for the attack.
+ sm <- gets (getActor source)
+ let -- Determine the weapon used for the attack.
sword = strongestSword (mitems sm)
damage = 3 + sword
weaponMsg = if sword == 0
@@ -915,9 +914,9 @@ actorDamageActor source target damage weaponMsg =
-- Extra prompt, in case many heroes attacked in one turn.
when b $ messageAddMore >> return ()
state <- get
- let sm = getActor state source
- tm = getActor state target
- -- Damage the target.
+ sm <- gets (getActor source)
+ tm <- gets (getActor target)
+ let -- Damage the target.
newHp = mhp tm - damage
killed = newHp <= 0
-- Determine how the hero perceives the event.
@@ -944,10 +943,9 @@ actorDamageActor source target damage weaponMsg =
-- This involves switching positions of the two movables.
actorRunActor :: Actor -> Actor -> Action ()
actorRunActor source target = do
- state <- get
pl <- gets splayer
- let sloc = mloc $ getActor state source -- source location
- tloc = mloc $ getActor state target -- target location
+ sloc <- gets (mloc . getActor source) -- source location
+ tloc <- gets (mloc . getActor target) -- target location
updateAnyActor source $ \ m -> m { mloc = tloc }
updateAnyActor target $ \ m -> m { mloc = sloc }
if source == pl
View
@@ -39,7 +39,7 @@ allHeroesAnyLevel state =
L.map (\ (i, _) -> (AHero i, ln)) (IM.assocs hs)
in L.concatMap one (slevel state : M.elems m)
-updateAnyActorBody :: Actor -> (Movable -> Movable) -> State -> State
+updateAnyActorBody :: Actor -> (Movable -> Movable) -> State -> State
updateAnyActorBody actor f state =
case findActorAnyLevel actor state of
Just (ln, _) ->
@@ -55,32 +55,32 @@ updateAnyLevel f ln state@(State { slevel = level,
| otherwise = updateDungeon (const $ Dungeon $ M.adjust f ln dng) state
-- | Calculate the location of player's target.
-targetToLoc :: State -> S.Set Loc -> Maybe Loc
-targetToLoc state visible =
+targetToLoc :: S.Set Loc -> State -> Maybe Loc
+targetToLoc visible state =
case mtarget (getPlayerBody state) of
TLoc loc -> Just loc
TCursor ->
if lname (slevel state) == clocLn (scursor state)
then Just $ clocation (scursor state)
else Nothing -- cursor invalid: set at a different level
TEnemy a -> do
- guard $ memActor state a -- alive and on the current level?
- let loc = mloc (getActor state a)
+ guard $ memActor a state -- alive and on the current level?
+ let loc = mloc (getActor a state)
guard $ S.member loc visible -- visible?
return loc
-- The operations below disregard levels other than the current.
-- | Checks if the actor is present on the current level.
-memActor :: State -> Actor -> Bool
-memActor (State { slevel = lvl }) a =
+memActor :: Actor -> State -> Bool
+memActor a (State { slevel = lvl }) =
case a of
AHero n -> IM.member n (lheroes lvl)
AMonster n -> IM.member n (lmonsters lvl)
-- | Gets actor body from the current level. Error if not found.
-getActor :: State -> Actor -> Movable
-getActor (State { slevel = lvl }) a =
+getActor :: Actor -> State -> Movable
+getActor a (State { slevel = lvl }) =
case a of
AHero n -> lheroes lvl IM.! n
AMonster n -> lmonsters lvl IM.! n
@@ -104,8 +104,8 @@ levelHeroList (State { slevel = Level { lheroes = hs } }) = IM.elems hs
levelMonsterList (State { slevel = Level { lmonsters = ms } }) = IM.elems ms
-- | Finds an actor at a location on the current level. Perception irrelevant.
-locToActor :: State -> Loc -> Maybe Actor
-locToActor state loc =
+locToActor :: Loc -> State -> Maybe Actor
+locToActor loc state =
getIndex (lmonsters, AMonster) `mplus` getIndex (lheroes, AHero)
where
getIndex (projection, injection) =
View
@@ -77,7 +77,7 @@ perception_ state@(State { splayer = pl,
_ -> error $ "perception_: unknown mode: " ++ show mode
-- Perception for a player-controlled monster on the current level.
- pper = if isAMonster pl && memActor state pl
+ pper = if isAMonster pl && memActor pl state
then Just $ perception fovMode (mloc (getPlayerBody state)) lmap
else Nothing
pers = IM.map (\ h -> perception fovMode (mloc h) lmap) hs
View
@@ -26,7 +26,7 @@ strategy actor
if nsmell mt then nose else openEye -- TODO: unify the 2 kinds using nsight
where
-- TODO: set monster targets and then prefer targets to other heroes
- Movable { mtype = mt, mloc = me, mdir = mdir } = getActor state actor
+ Movable { mtype = mt, mloc = me, mdir = mdir } = getActor actor state
delState = deleteActor actor state
hs = L.map (\ (i, m) -> (AHero i, mloc m)) $
IM.assocs $ lheroes $ slevel delState

0 comments on commit 2eb1d33

Please sign in to comment.