Permalink
Browse files

strategy now contains actions, not dirs

  • Loading branch information...
1 parent c2286dc commit a9a016b87d7cda386dbd1ba399b944901374e931 @Mikolaj Mikolaj committed Apr 7, 2011
Showing with 29 additions and 25 deletions.
  1. +24 −6 src/StrategyState.hs
  2. +5 −19 src/Turn.hs
View
@@ -16,8 +16,11 @@ import Random
import Perception
import Strategy
import State
+import Action
+import Actions
+import ItemAction
-strategy :: Actor -> State -> Perceptions -> Strategy Dir
+strategy :: Actor -> State -> Perceptions -> Strategy (Action ())
strategy actor
oldState@(State { scursor = cursor,
splayer = pl,
@@ -26,7 +29,7 @@ strategy actor
lsmell = nsmap,
lmap = lmap } })
per =
- strategy
+ dirToAction actor `liftM` strategy
where
-- TODO: set monster targets and then prefer targets to other heroes
Movable { mkind = mk, mloc = me, mdir = mdir } = getActor actor oldState
@@ -79,12 +82,12 @@ strategy actor
onlySensible $
onlyTraitor moveFreely
.| onlyFoe moveFreely
- .| (greedyMonster && lootHere me) .=> wait
+ .| (greedyMonster && lootHere me) .=> return (0,0)
.| moveTowards
moveTowards =
(if pushyMonster then id else onlyUnoccupied) $
nsight mk .=> towardsFoe moveFreely
- .| lootHere me .=> wait
+ .| lootHere me .=> return (0,0)
.| nsmell mk .=> foldr (.|) reject (L.map return smells)
.| onlyOpenable moveFreely
.| moveFreely
@@ -94,11 +97,26 @@ strategy actor
.| niq mk > 5 .=> onlyKeepsDir 2 moveRandomly
.| moveRandomly
+dirToAction :: Actor -> Dir -> Action ()
+dirToAction actor dir = do
+ let waiting = dir == (0,0)
+ let nmdir = if waiting then Nothing else Just dir
+ -- advance time and update monster
+ updateAnyActor actor $ \ m -> m { mdir = nmdir }
+ tryWith (advanceTime actor) $
+ -- if the following action aborts, we just advance the time and continue
+ if waiting
+ then
+ -- monster is not moving, let's try to pick up an object
+ actorPickupItem actor
+ else
+ moveOrAttack True True actor dir
+
onlyMoves :: (Dir -> Bool) -> Loc -> Strategy Dir -> Strategy Dir
onlyMoves p l = only (\ x -> p (l `shift` x))
moveRandomly :: Strategy Dir
moveRandomly = liftFrequency $ uniform moves
-wait :: Strategy Dir
-wait = return (0,0)
+wait :: Strategy (Action ())
+wait = return $ return ()
View
@@ -11,8 +11,6 @@ import qualified Data.Char as Char
import Action
import Actions
-import EffectAction
-import ItemAction
import Command
import qualified Config
import Display hiding (display)
@@ -108,24 +106,12 @@ handleMonster actor =
do
debug "handleMonster"
state <- get
- time <- gets stime
per <- currentPerception
- -- run the AI; it currently returns a direction
- -- TODO: it should return an action
- dir <- liftIO $ rndToIO $
- frequency (head (runStrategy (strategy actor state per .| wait)))
- let waiting = dir == (0,0)
- let nmdir = if waiting then Nothing else Just dir
- -- advance time and update monster
- updateAnyActor actor $ \ m -> m { mdir = nmdir }
- tryWith (advanceTime actor) $
- -- if the following action aborts, we just advance the time and continue
- if waiting
- then
- -- monster is not moving, let's try to pick up an object
- actorPickupItem actor
- else
- moveOrAttack True True actor dir
+ -- Run the AI: choses an action from those given by the AI strategy.
+ action <-
+ liftIO $ rndToIO $
+ frequency (head (runStrategy (strategy actor state per .| wait)))
+ action
handleMonsters
-- | After everything has been handled for the current game time, we can

0 comments on commit a9a016b

Please sign in to comment.