From 6c9287067bfc67eaf0b586489ee9582e674dd7a3 Mon Sep 17 00:00:00 2001 From: Mikolaj Date: Thu, 3 Mar 2011 18:35:04 +0100 Subject: [PATCH] faster, deterministic and better looking extra heroes placement --- src/Dungeon.hs | 22 +++++++++++----------- src/Geometry.hs | 4 ++-- src/LambdaHack.hs | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Dungeon.hs b/src/Dungeon.hs index a40faf09d..175cfa5d1 100644 --- a/src/Dungeon.hs +++ b/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 diff --git a/src/Geometry.hs b/src/Geometry.hs index b20f08a0a..0e97d110e 100644 --- a/src/Geometry.hs +++ b/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 diff --git a/src/LambdaHack.hs b/src/LambdaHack.hs index b90cf6d05..0dcbaeb1b 100644 --- a/src/LambdaHack.hs +++ b/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