Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

let monsters apply and throw items sometimes

  • Loading branch information...
commit 66e9f5526501b7321359c06f2c4a35e996364201 1 parent da2aa2e
@Mikolaj Mikolaj authored
View
13 src/Effect.hs
@@ -26,3 +26,16 @@ effectToName SummonEnemy = "of summoning"
effectToName ApplyPerfume = "of rose water"
effectToName Regneration = "of regeneration"
effectToName Searching = "of searching"
+
+-- | How much AI benefits from applying the effect. Multipllied by item power.
+-- Negative means harm to the enemy when thrown. Zero won't ever be used.
+effectToBenefit :: Effect -> Int
+effectToBenefit NoEffect = 0
+effectToBenefit Heal = 10 -- TODO: depends on (maxhp - hp)
+effectToBenefit (Wound _) = -10 -- TODO: dice ignored for now
+effectToBenefit Dominate = 0 -- AI can't use this
+effectToBenefit SummonFriend = 100
+effectToBenefit SummonEnemy = 0
+effectToBenefit ApplyPerfume = 0
+effectToBenefit Regneration = 0 -- much more benefit from carrying around
+effectToBenefit Searching = 0 -- AI does not need to search
View
4 src/ItemAction.hs
@@ -78,6 +78,7 @@ applyGroupItem actor verb item = do
when (loc `S.member` ptvisible per) $ message msg
b <- itemEffectAction consumed actor actor
when b $ removeFromInventory actor consumed loc
+ advanceTime actor
playerApplyGroupItem :: String -> Action ()
playerApplyGroupItem groupName = do
@@ -90,7 +91,6 @@ playerApplyGroupItem groupName = do
let verb = applyToVerb (ItemKind.jname (ItemKind.getIK (ikind i)))
in applyGroupItem pl verb i
Nothing -> neverMind True
- playerAdvanceTime
applyToVerb :: String -> String
applyToVerb "potion" = "quaff"
@@ -124,6 +124,7 @@ zapGroupItem source loc verb item = do
let msg = subjectVerbIObject state sm verb consumed ""
when (sloc `S.member` ptvisible per) $ message msg
modify (updateLevel (dropItemsAt [consumed] loc))
+ advanceTime source
playerZapGroupItem :: String -> Action ()
playerZapGroupItem groupName = do
@@ -144,7 +145,6 @@ playerZapGroupItem groupName = do
in zapGroupItem pl loc verb i
else abortWith "target not reachable"
Nothing -> neverMind True
- playerAdvanceTime
zapToVerb :: String -> String
zapToVerb "wand" = "aim"
View
3  src/Strategy.hs
@@ -22,7 +22,8 @@ instance Monad Strategy where
| x <- runStrategy m ]
liftFrequency :: Frequency a -> Strategy a
-liftFrequency f = Strategy $ return f
+liftFrequency f =
+ Strategy $ filter (\ (Frequency xs) -> not (null xs)) $ return f
instance MonadPlus Strategy where
mzero = Strategy []
View
39 src/StrategyState.hs
@@ -21,6 +21,9 @@ import State
import Action
import Actions
import ItemAction
+import qualified ItemKind
+import Item
+import qualified Effect
strategy :: Actor -> State -> Perceptions -> Strategy (Action ())
strategy actor
@@ -33,7 +36,8 @@ strategy actor
per =
strategy
where
- Movable { mkind = mk, mloc = me, mdir = mdir, mtarget = tgt } =
+ Movable { mkind = mk, mloc = me, mdir = mdir,
+ mtarget = tgt, mitems = items } =
getActor actor oldState
delState = deleteActor actor oldState
enemyVisible a l =
@@ -93,7 +97,6 @@ strategy actor
onlyOpenable = onlyMoves openableHere me
accessibleHere = accessible lmap me
onlySensible = onlyMoves (\ l -> accessibleHere l || openableHere l) me
- greedyMonster = niq mk < 5
focusedMonster = niq mk > 10
smells =
L.map fst $
@@ -104,11 +107,39 @@ strategy actor
strategy =
fromDir True (onlyFoe moveFreely)
- .| (greedyMonster && lootHere me) .=> actionPickup
- .| nsight mk .=> fromDir False moveTowards
+ .| isJust floc .=> liftFrequency (msum freqs)
.| lootHere me .=> actionPickup
.| fromDir True moveAround
actionPickup = return $ actorPickupItem actor
+ tis = titems $ lmap `at` me
+ freqs = [applyFreq items 1, applyFreq tis 2,
+ throwFreq items 2, throwFreq tis 5, towardsFreq]
+ applyFreq is multi = Frequency
+ [ (benefit * multi, actionApply (ItemKind.jname ik) i)
+ | i <- is,
+ let ik = ItemKind.getIK (ikind i),
+ let benefit =
+ (1 + ipower i) * Effect.effectToBenefit (ItemKind.jeffect ik),
+ benefit > 0,
+ nsight mk || not (ItemKind.jname ik == "scroll")]
+ actionApply groupName item =
+ applyGroupItem actor (applyToVerb groupName) item
+ throwFreq is multi = if not $ nsight mk then mzero else Frequency
+ [ (benefit * multi, actionThrow (ItemKind.jname ik) i)
+ | i <- is,
+ let ik = ItemKind.getIK (ikind i),
+ let benefit =
+ - (1 + ipower i) * Effect.effectToBenefit (ItemKind.jeffect ik),
+ benefit > 0,
+ -- Wasting swords would be too cruel to the player.
+ not (ItemKind.jname ik == "sword")]
+ actionThrow groupName item =
+ zapGroupItem actor (fromJust floc) (zapToVerb groupName) item
+ towardsFreq =
+ let freqs = runStrategy $ fromDir False moveTowards
+ in if nsight mk && not (L.null freqs)
+ then scale 30 $ head freqs
+ else mzero
moveTowards =
onlySensible $
onlyUnoccupied (towardsFoe moveFreely)
Please sign in to comment.
Something went wrong with that request. Please try again.