Browse files

faster, deterministic and better looking extra heroes placement

  • Loading branch information...
1 parent a754ad3 commit 6c9287067bfc67eaf0b586489ee9582e674dd7a3 @Mikolaj Mikolaj committed Mar 3, 2011
Showing with 14 additions and 14 deletions.
  1. +11 −11 src/Dungeon.hs
  2. +2 −2 src/Geometry.hs
  3. +1 −1 src/LambdaHack.hs
View
22 src/Dungeon.hs
@@ -7,6 +7,7 @@ import Data.Map as M
import qualified Data.IntMap as IM
import Data.List as L
import Data.Ratio
+import Data.Maybe
import State
import Geometry
@@ -328,15 +329,14 @@ addMonster state@(State { slevel = lvl@(Level { lmonsters = ms,
else return lvl
-- | Create a new hero in the level, close to the player.
-addHero :: Int -> State -> Int -> Rnd State
+addHero :: Int -> State -> Int -> State
addHero hp state@(State { splayer = player,
- slevel = lvl@(Level { lmonsters = ms }) }) n =
- do
- let hs = levelHeroList state
- ploc <- findLocTry 10000 lvl -- TODO: bad for large levels
- (\ l t -> open t
- && not (l `L.elem` L.map mloc (hs ++ ms)))
- (\ l t -> floor t
- && distance (mloc player, l) < 6 + L.length hs `div` 3)
- let hero = defaultHero n ploc hp
- return (updateLevel (updateHeroes (IM.insert n hero)) state)
+ slevel = lvl@(Level { lmap = map }) }) n =
+ let hs = levelHeroList state
+ ms = lmonsters lvl
+ ploc = mloc player
+ places = ploc : L.nub (concatMap surroundings places)
+ good l = open (map `at` l) && not (l `L.elem` L.map mloc (hs ++ ms))
+ place = fromMaybe (error "no place for a hero") $ L.find good places
+ hero = defaultHero n place hp
+ in updateLevel (updateHeroes (IM.insert n hero)) state
View
4 src/Geometry.hs
@@ -51,9 +51,9 @@ shift (y0,x0) (y1,x1) = (y0+y1,x0+x1)
neg :: Dir -> Dir
neg (y,x) = (-y,-x)
--- | Get the vectors of all the moves.
+-- | Get the vectors of all the moves, clockwise, starting north-west.
moves :: [Dir]
-moves = [ (x,y) | x <- [-1..1], y <- [-1..1], x /= 0 || y /= 0 ]
+moves = [(-1,-1), (-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1)]
up, down, left, right :: Dir
upleft, upright, downleft, downright :: Dir
View
2 src/LambdaHack.hs
@@ -79,5 +79,5 @@ generate config session msg =
defState = defaultState player dng lvl
state = defState { sassocs = assocs, sconfig = config }
k = Config.get config "heroes" "extraHeroes"
- hstate <- rndToIO $ foldM (addHero hp) state [1..k]
+ hstate = foldl (addHero hp) state [1..k]
handlerToIO session hstate msg handle

0 comments on commit 6c92870

Please sign in to comment.