Permalink
Browse files

on levels with no heroes monsters now chase each other

Actually, they don't actively follow each other (no monster FOV, so they
can't see each other), but they always attack when adjacent, and not
only when bumping into each other by chance, as on levels with heroes.
To see this behaviour toggle look mode, change level and press '.'.
  • Loading branch information...
1 parent dfe2cf3 commit f459fa1b29f95b1b4f8dfa65024eaa4559b304c1 @Mikolaj Mikolaj committed Feb 7, 2011
Showing with 29 additions and 14 deletions.
  1. +12 −7 src/State.hs
  2. +17 −7 src/StrategyState.hs
View
19 src/State.hs
@@ -1,5 +1,6 @@
module State where
+import qualified Data.List as L
import qualified Data.Map as M
import qualified Data.IntMap as IM
import qualified Data.Set as S
@@ -55,15 +56,19 @@ defaultState player dng lvl =
updatePlayer :: (Hero -> Hero) -> State -> State
updatePlayer f s = s { splayer = f (splayer s) }
-levelHeroList :: State -> [Hero]
-levelHeroList (State { splayer = player,
- slevel = Level { lheroes = hs } }) =
- player : IM.elems hs
-
levelHeroAssocs :: State -> [(Int, Hero)]
levelHeroAssocs (State { splayer = player,
- slevel = Level { lheroes = hs } }) =
- (heroNumber player, player) : IM.assocs hs
+ slook = look,
+ slevel = level@Level { lheroes = hs } }) =
+ case look of
+ Just (Look { returnLn = ln })
+ | ln /= lname level ->
+ -- player not on the currently selected level
+ IM.assocs hs
+ _ -> (heroNumber player, player) : IM.assocs hs
+
+levelHeroList :: State -> [Hero]
+levelHeroList s = snd $ L.unzip $ levelHeroAssocs s
updateLook :: (Maybe Look -> Maybe Look) -> State -> State
updateLook f s = s { slook = f (slook s) }
View
24 src/StrategyState.hs
@@ -27,25 +27,35 @@ strategy m@(Monster { mtype = mt, mloc = me, mdir = mdir })
-- player is visible by the monster -- this is more efficient, but
-- is not correct with the Shadow FOV (the other FOVs are symmetrical)
-- TODO: set monster targets and then prefer targets to other heroes
- plocs = L.map mloc (levelHeroList state)
- plds = L.map (\ l -> (distance (me, l), l)) plocs
- -- Here "player" is the hero chased by the monster. As soon as the monster
- -- hits, this hero will really become the currently selected hero.
+ hs = levelHeroList state
+ -- If no heroes on the level, monsters go at each other. TODO: let them
+ -- earn XP by killing each other to make this dangerous to the player.
+ hms = if L.null hs then ms else hs
+ plocs = L.map mloc hms
+ plds = L.sort $ L.map (\ l -> (distance (me, l), l)) plocs
+ -- Below, "player" is the hero (or a monster, if no heroes on this level)
+ -- chased by the monster ("ploc" is his location, etc.).
+ -- As soon as the monster hits, this hero becomes really the currently
+ -- selected hero.
-- We have to sort the list to avoid bias towards the currently selected
-- hero; instead monsters will prefer heroes with smaller locations.
- (pdist, ploc) = L.head (L.sort plds)
+ ploc = case plds of
+ [] -> Nothing
+ (_, ploc) : _ -> Just ploc
-- TODO: currently even invisible heroes are targeted if _any_ hero
-- is visible; each hero should carry his own perception to check
-- if he's visible by a given monster
playerVisible = me `S.member` pvisible per -- monster sees any hero
- playerAdjacent = adjacent me ploc
- towardsPlayer = towards (me, ploc)
+ playerAdjacent = maybe False (adjacent me) ploc
+ towardsPlayer = maybe (0, 0) (\ ploc -> towards (me, ploc)) ploc
onlyTowardsPlayer = only (\ x -> distance (towardsPlayer, x) <= 1)
lootPresent = (\ x -> not $ L.null $ titems $ lmap `at` x)
onlyLootPresent = onlyMoves lootPresent me
onlyPreservesDir = only (\ x -> maybe True (\ d -> distance (neg d, x) > 1) mdir)
onlyUnoccupied = onlyMoves (unoccupied ms lmap) me
onlyAccessible = onlyMoves (accessible lmap me) me
+ -- TODO: restriction to 10 should be enforced elsewhere so that
+ -- monsters can't cheat. Strategy is not the place for enforcing rules.
onlyOpenable = onlyMoves (openable 10 lmap) me
smells = L.map fst $
L.sortBy (\ (_,s1) (_,s2) -> compare s2 s1) $

0 comments on commit f459fa1

Please sign in to comment.