Skip to content

Commit

Permalink
made everything have it's own Id type.
Browse files Browse the repository at this point in the history
====================
Preprocessing library WoWSim-0.1...
Preprocessing executables for WoWSim-0.1...
Building WoWSim-0.1...
Registering WoWSim-0.1...
dist/build/WoWSim/WoWSim 6000 +RTS -sstderr
     269,730,480 bytes allocated in the heap
       6,510,156 bytes copied during GC
       1,594,308 bytes maximum residency (3 sample(s))
          47,916 bytes maximum slop
               5 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:   513 collections,     0 parallel,  0.02s,  0.03s elapsed
  Generation 1:     3 collections,     0 parallel,  0.01s,  0.02s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.43s  (  0.43s elapsed)
  GC    time    0.04s  (  0.04s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.46s  (  0.47s elapsed)

  %GC time       7.8%  (8.7% elapsed)

  Alloc rate    632,969,551 bytes per MUT second

  Productivity  92.0% of total user, 90.3% of total elapsed
  • Loading branch information
periodic committed Jun 24, 2011
1 parent be29451 commit 42d868f
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 70 deletions.
2 changes: 1 addition & 1 deletion AI/AutoAttack.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ autoAttack _ = return ()


swing = do swing = do
resetGCD resetGCD
attack "AutoAttack" 100 attack (AbilityId "AutoAttack") 100
2 changes: 1 addition & 1 deletion AI/Info.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ onGCD = do
t <- getTime t <- getTime
return $ entityOnGCD t s return $ entityOnGCD t s


abilOnCooldown :: String -> Action Bool abilOnCooldown :: AbilityId -> Action Bool
abilOnCooldown name = do abilOnCooldown name = do
s <- getSource s <- getSource
t <- getTime t <- getTime
Expand Down
4 changes: 2 additions & 2 deletions AI/Warrior.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rotation = do
then useAbility slam then useAbility slam
else useAbility mortalStrike else useAbility mortalStrike
where where
msName = "MortalStrike" msName = AbilityId "MortalStrike"
mortalStrike = mortalStrike =
Ability { _abilName = msName Ability { _abilName = msName
, _abilCooldown = Just 6 , _abilCooldown = Just 6
Expand All @@ -31,7 +31,7 @@ rotation = do
, _abilSchool = Physical , _abilSchool = Physical
, _abilAction = weapon msName 2 100 , _abilAction = weapon msName 2 100
} }
slamName = "Slam" slamName = AbilityId "Slam"
slam = slam =
Ability { _abilName = slamName Ability { _abilName = slamName
, _abilCooldown = Nothing , _abilCooldown = Nothing
Expand Down
2 changes: 1 addition & 1 deletion Actions/Attacks.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ startAutoAttack = do
addHandler name (autoAttackHandler owner) addHandler name (autoAttackHandler owner)
after 0 (EvAutoAttackReady owner) after 0 (EvAutoAttackReady owner)
where where
name = "AutoAttack" name = AbilityId "AutoAttack"
autoAttackHandler owner (EvAutoAttackReady eid) autoAttackHandler owner (EvAutoAttackReady eid)
| eid == owner = do | eid == owner = do
delay <- getL (weaponSpeed <.> eStats) <$> getSource delay <- getL (weaponSpeed <.> eStats) <$> getSource
Expand Down
20 changes: 10 additions & 10 deletions Actions/Common.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import System.Random (StdGen)


-- * Functions on Sim -- * Functions on Sim
-- | This function lets us transform handlers defined on Action and lift them to Sim. -- | This function lets us transform handlers defined on Action and lift them to Sim.
makeHandler :: Entity -> (Event -> Action ()) -> Event -> Sim World Event () makeHandler :: EntityId -> (Event -> Action ()) -> Event -> Sim World Event ()
makeHandler e a ev = do makeHandler eid a ev = do
mpEntity <- getEntity . (getL eID) $ e mpEntity <- getEntity eid
case mpEntity of case mpEntity of
Nothing -> return () Nothing -> return ()
Just pEntity -> do Just pEntity -> do
Expand Down Expand Up @@ -51,15 +51,15 @@ after :: DTime -> Event -> Action ()
after t ev = lift $ Sim.after t ev after t ev = lift $ Sim.after t ev


-- ** Handlers Utilities -- ** Handlers Utilities
addHandler :: String -> (Event -> Action ()) -> Action() addHandler :: (Show a) => a -> (Event -> Action ()) -> Action()
addHandler name handler = do addHandler name handler = do
actionState <- ask actionState <- ask
h <- transformHandler handler h <- transformHandler handler
lift $ Sim.addHandler name h lift $ Sim.addHandler (show name) h


removeHandler :: String -> Action () removeHandler :: (Show a) => a -> Action ()
removeHandler name = do removeHandler name = do
lift $ Sim.removeHandler name lift $ Sim.removeHandler (show name)


transformHandler :: (Event -> Action ()) -> Action (Event -> Sim World Event ()) transformHandler :: (Event -> Action ()) -> Action (Event -> Sim World Event ())
transformHandler h = do transformHandler h = do
Expand Down Expand Up @@ -105,7 +105,7 @@ resetGCD =
src <- getSource src <- getSource
after 1.5 . EvGcdEnd . getL eID $ src after 1.5 . EvGcdEnd . getL eID $ src


setCooldown :: String -> Sim.DTime -> Action () setCooldown :: AbilityId -> Sim.DTime -> Action ()
setCooldown name dt = setCooldown name dt =
do t <- getTime do t <- getTime
src <- getSource src <- getSource
Expand All @@ -129,7 +129,7 @@ registerCast :: DTime -> Ability -> Action ()
registerCast dt abil= do registerCast dt abil= do
sid <- getL eID <$> getSource sid <- getL eID <$> getSource
let aid = getL abilName abil let aid = getL abilName abil
handlerName = show sid ++ aid handlerName = show sid ++ show aid
addHandler handlerName (handler sid) addHandler handlerName (handler sid)
after 0 $ EvCastStarted sid aid after 0 $ EvCastStarted sid aid
after dt $ EvCastComplete sid aid after dt $ EvCastComplete sid aid
Expand All @@ -138,7 +138,7 @@ registerCast dt abil= do
if (eid == sid) && (getL abilName abil) == aid if (eid == sid) && (getL abilName abil) == aid
then do getL abilAction abil then do getL abilAction abil
sid <- getL eID <$> getSource sid <- getL eID <$> getSource
removeHandler (show sid ++ aid) removeHandler (show sid ++ show aid)
else return () else return ()
handler _ _ = return () handler _ _ = return ()


2 changes: 0 additions & 2 deletions Types/Aura.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ isAuraInMap = member


emptyAuraMap = empty emptyAuraMap = empty


instance Show Aura where
show (Aura id school typ cat f) = printf "Aura { id = %s, school = %s, type = %s, category = %s}" (show id) (show school) (show typ) (show cat)
47 changes: 27 additions & 20 deletions Types/Common.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,39 +31,43 @@ data SimConfig = SimConfig {


-- * General types -- * General types


type HandlerId = String type Health = Integer
type HandlerAction = Event -> Action () type Damage = Integer
type HandlerList = Map String HandlerAction
-- ** IDs
newtype EntityId = EntityId String deriving (Eq, Ord)
instance Show EntityId where
show (EntityId name) = "Entity." ++ name

newtype AbilityId = AbilityId String deriving (Eq, Ord)
instance Show AbilityId where
show (AbilityId name) = "Ability." ++ name

newtype AuraId = AuraId String deriving (Eq, Ord)
instance Show AuraId where
show (AuraId name) = "Aura." ++ name


-- ** Monads
data ActionState = ActionState { _actionSource :: Entity data ActionState = ActionState { _actionSource :: Entity
, _actionTarget :: Entity , _actionTarget :: Entity
} deriving (Show) } deriving (Show)


type Action = ReaderT ActionState (Sim World Event) type Action = ReaderT ActionState (Sim World Event)


type Health = Integer -- ** Entity
type Damage = Integer

newtype EntityId = EntityId String
deriving (Eq, Ord)
instance Show EntityId where
show (EntityId name) = name

type EntityMap = Map EntityId Entity type EntityMap = Map EntityId Entity


data Entity = Entity { _eID :: !EntityId data Entity = Entity { _eID :: !EntityId
, _eTarget :: !EntityId , _eTarget :: !EntityId
, _eHealth :: !Health , _eHealth :: !Health
, _eGlobalCD :: !Time , _eGlobalCD :: !Time
, _eCast :: Maybe (Ability, Time) , _eCast :: Maybe (Ability, Time)
, _eCooldowns :: Map String Time , _eCooldowns :: Map AbilityId Time
, _eStats :: Stats , _eStats :: Stats
, _eHandlers :: HandlerList
, _eAI :: HandlerAction
, _eAuras :: AuraMap , _eAuras :: AuraMap
} }
instance Show Entity where instance Show Entity where
show (Entity id targ health gcd cast _ _ _ _ _) = show (Entity id targ health gcd cast _ _ _) =
printf "Entity { eId = \"%s\", eTarget = \"%s\", eHealth = %d, eGlobalCD = %d, eCast = %d }" printf "Entity { eId = \"%s\", eTarget = \"%s\", eHealth = %d, eGlobalCD = %d, eCast = %d }"
(show id) (show id)
(show targ) (show targ)
Expand Down Expand Up @@ -94,14 +98,15 @@ data Event = EvSimStart
--deriving (Show) --deriving (Show)


type AbilityMap = Map AbilityId Ability type AbilityMap = Map AbilityId Ability
type AbilityId = String
data Ability = Ability { _abilName :: AbilityId data Ability = Ability { _abilName :: AbilityId
, _abilCooldown :: Maybe DTime , _abilCooldown :: Maybe DTime
, _abilTriggerGCD :: Bool , _abilTriggerGCD :: Bool
, _abilCastTime :: DTime , _abilCastTime :: DTime
, _abilSchool :: SpellSchool , _abilSchool :: SpellSchool
, _abilAction :: Action () , _abilAction :: Action ()
} }
instance Show Ability where
show (Ability name _ _ _ _ _) = "Ability." ++ (show name)


-- * Auras -- * Auras
-- | Auras come in two flavors, those that buff, and those that don't. The -- | Auras come in two flavors, those that buff, and those that don't. The
Expand All @@ -124,16 +129,18 @@ data AuraType = BeneficialAura
| DebuffOther | DebuffOther
deriving (Show, Eq, Typeable, Data) deriving (Show, Eq, Typeable, Data)


type AuraId = String
type AuraMap = Map AuraId Aura type AuraMap = Map AuraId Aura
type BuffMap = Map AuraId Buff type BuffMap = Map AuraId Buff


data Aura = Aura { _auraId :: AuraId data Aura = Aura { _auraId :: AuraId
, _auraSchool :: SpellSchool , _auraOwner :: EntityId
, _auraType :: AuraType , _auraSchool :: SpellSchool
, _auraType :: AuraType
, _buffCategory :: BuffCategory , _buffCategory :: BuffCategory
, _buffFunc :: Buff , _buffFunc :: Buff
} }
instance Show Aura where
show (Aura name owner _ _ _ _) = printf "Aura.%s.%s" (show owner) (show name)




-- * Function types -- * Function types
Expand Down
28 changes: 13 additions & 15 deletions Types/Entity.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ import Types.Aura




-- |Create a default entity. -- |Create a default entity.
makeEntity name targ ai = Entity { _eID = getIdFromString name makeEntity name targ = Entity { _eID = EntityId name
, _eTarget = getIdFromString targ , _eTarget = EntityId targ
, _eHealth = 0 , _eHealth = 0
, _eGlobalCD = 0 , _eGlobalCD = 0
, _eCast = Nothing , _eCast = Nothing
, _eCooldowns = empty , _eCooldowns = empty
, _eStats = defaultStats , _eStats = defaultStats
, _eHandlers = emptyHandlerList , _eAuras = emptyAuraMap
, _eAI = ai }
, _eAuras = emptyAuraMap
}


-- |Add an entity to the entity map. -- |Add an entity to the entity map.
addEntityList :: Entity -> EntityMap -> EntityMap addEntityList :: Entity -> EntityMap -> EntityMap
Expand All @@ -58,14 +56,14 @@ entityOnGCD :: Time -> Entity -> Bool
entityOnGCD t e = getL eGlobalCD e > t entityOnGCD t e = getL eGlobalCD e > t


-- |Test whether the entity has the named ability on cooldown -- |Test whether the entity has the named ability on cooldown
entityOnCooldown :: String -> Time -> Entity -> Bool entityOnCooldown :: AbilityId -> Time -> Entity -> Bool
entityOnCooldown name t e = entityOnCooldown name t e =
case Data.Map.lookup name (getL eCooldowns e) of case Data.Map.lookup name (getL eCooldowns e) of
Nothing -> False Nothing -> False
Just t' -> t' > t Just t' -> t' > t


-- |Add a cooldown to the entity's cooldown map. -- |Add a cooldown to the entity's cooldown map.
entityAddCooldown :: String -> Time -> Entity -> Entity entityAddCooldown :: AbilityId -> Time -> Entity -> Entity
entityAddCooldown name t e = modL eCooldowns (insert name t) e entityAddCooldown name t e = modL eCooldowns (insert name t) e


-- ** Casting related functions -- ** Casting related functions
Expand All @@ -77,4 +75,4 @@ entityIsCasting t e = case getL eCast e of


-- | Start the cast timer -- | Start the cast timer
entityStartCasting :: Time -> Ability -> Entity -> Entity entityStartCasting :: Time -> Ability -> Entity -> Entity
entityStartCasting t abil = setL eCast (Just (abil, t)) entityStartCasting t abil = setL eCast (Just (abil, t))
18 changes: 9 additions & 9 deletions Types/Event.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import Text.Printf
instance Show Event where instance Show Event where
show EvSimStart = "Sim Start" show EvSimStart = "Sim Start"
show (EvGcdEnd eid) = printf "GCD end for %s" (show eid) show (EvGcdEnd eid) = printf "GCD end for %s" (show eid)
show (EvCooldownExpire eid name) = printf "Cooldown Expired for %s's %s" (show eid) name show (EvCooldownExpire eid name) = printf "Cooldown Expired for %s's %s" (show eid) (show name)
show (EvAutoAttackStart eid) = printf "Autoattack started for %s" (show eid) show (EvAutoAttackStart eid) = printf "Autoattack started for %s" (show eid)
show (EvAutoAttackStop eid) = printf "Autoattack stopped for %s" (show eid) show (EvAutoAttackStop eid) = printf "Autoattack stopped for %s" (show eid)
show (EvAutoAttackReady eid) = printf "Autoattack ready for %s" (show eid) show (EvAutoAttackReady eid) = printf "Autoattack ready for %s" (show eid)
show (EvHit pid tid abil dmg) = printf "%s's %s hit %s for %d" (show pid) abil (show tid) dmg show (EvHit pid tid abil dmg) = printf "%s's %s hit %s for %d" (show pid) (show abil) (show tid) dmg
show (EvCrit pid tid abil dmg) = printf "%s's %s crit %s for %d" (show pid) abil (show tid) dmg show (EvCrit pid tid abil dmg) = printf "%s's %s crit %s for %d" (show pid) (show abil) (show tid) dmg
show (EvDodge pid tid abil) = printf "%s dodged %s's %s" (show tid) (show pid) abil show (EvDodge pid tid abil) = printf "%s dodged %s's %s" (show tid) (show pid) (show abil)
show (EvParry pid tid abil) = printf "%s parried %s's %s" (show tid) (show pid) abil show (EvParry pid tid abil) = printf "%s parried %s's %s" (show tid) (show pid) (show abil)
show (EvMiss pid tid abil) = printf "%s %s missed %s" (show pid) abil (show tid) show (EvMiss pid tid abil) = printf "%s %s missed %s" (show pid) (show abil) (show tid)
show (EvCastStarted eid aid) = printf "%s began casting %s" (show eid) aid show (EvCastStarted eid aid) = printf "%s began casting %s" (show eid) (show aid)
show (EvCastComplete eid aid) = printf "%s completed cast of %s" (show eid) aid show (EvCastComplete eid aid) = printf "%s completed cast of %s" (show eid) (show aid)
show (EvCastInterrupted eid aid) = printf "%s's cast of %s was interrupted" (show eid) aid show (EvCastInterrupted eid aid) = printf "%s's cast of %s was interrupted" (show eid) (show aid)
2 changes: 0 additions & 2 deletions Types/World.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ module Types.World ( module Types.Common
, entityOnCooldown , entityOnCooldown
, entityAddCooldown , entityAddCooldown
, makeEntity , makeEntity
-- From Types.EntityId
, getIdFromString
-- From Types.Ability -- From Types.Ability
, realAbilCastTime , realAbilCastTime
-- From Types.Aura -- From Types.Aura
Expand Down
12 changes: 6 additions & 6 deletions WoWSim.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import AI.Warrior
main = do main = do
[dur] <- getArgs [dur] <- getArgs
gen <- newStdGen gen <- newStdGen
let pEntity = makeEntity "Player" "Target" nullHandler let pEntity = makeEntity "Player" "Target"
tEntity = makeEntity "Target" "Player" nullHandler tEntity = makeEntity "Target" "Player"
pID = getL eID pEntity
entities = addEntityList pEntity . addEntityList tEntity $ empty entities = addEntityList pEntity . addEntityList tEntity $ empty
-- TODO: move this to Types.World -- TODO: move this to Types.World
world = World { _wEntities = entities world = World { _wEntities = entities
, _wGen = gen , _wGen = gen
} }
ai = makeHandler pEntity warrior
config = defaultConfig { enableLog = True } config = defaultConfig { enableLog = True }
(t, log, world') = {-# SCC "sim" #-} simulate config world [("Warrior", ai)] EvSimStart (read dur) (t, log, world') = {-# SCC "sim" #-} simulate config world [(show pID, makeHandler pID warrior)] EvSimStart (read dur)
putStrLn . showLog $ log putStrLn . showLog $ log
let tFinal = Map.lookup (getIdFromString "Target") . getL wEntities $ world' let tFinal = Map.lookup (EntityId "Target") . getL wEntities $ world'
case tFinal of case tFinal of
Just targ -> print $ (t, getL eHealth targ) Just targ -> print $ (t, getL eHealth targ)
Nothing -> print $ (t) Nothing -> print $ (t)
Expand Down
1 change: 0 additions & 1 deletion wowsim.cabal
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ Other-modules: Actions.Attacks,
Executable: WoWSim Executable: WoWSim
Main-is: WoWSim.hs Main-is: WoWSim.hs
ghc-options: -rtsopts ghc-options: -rtsopts

0 comments on commit 42d868f

Please sign in to comment.