Skip to content

Commit

Permalink
faster, deterministic and better looking extra heroes placement
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikolaj committed Mar 3, 2011
1 parent a754ad3 commit 6c92870
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/Dungeon.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/Geometry.hs
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/LambdaHack.hs
Expand Up @@ -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.