Skip to content

Commit

Permalink
- Little restructurization.
Browse files Browse the repository at this point in the history
- RawToken structure is updated.
- Tests fixed.
  • Loading branch information
graninas committed Feb 21, 2014
1 parent 78baf84 commit 64fefc7
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 124 deletions.
12 changes: 7 additions & 5 deletions Amoeba.cabal
Expand Up @@ -45,8 +45,6 @@ library
GameLogic.Data.Object, GameLogic.Data.Object,
GameLogic.Data.Player, GameLogic.Data.Player,
GameLogic.Runtime.Evaluation, GameLogic.Runtime.Evaluation,
GameLogic.Runtime.Game,
GameLogic.Runtime.World,
Middleware.Config.Config, Middleware.Config.Config,
Middleware.Config.Facade, Middleware.Config.Facade,
Middleware.Config.Scheme, Middleware.Config.Scheme,
Expand All @@ -71,7 +69,10 @@ library
Test.EitherMonadCallTest, Test.EitherMonadCallTest,
Test.TypeFamilyTest2, Test.TypeFamilyTest2,
Test.TypeFamilyTest1, Test.TypeFamilyTest1,
GameLogic.Language.Scheme GameLogic.Language.Scheme,
GameLogic.Data.World,
GameLogic.Data.Game,
Test.Utils.WorldArfData


executable Amoeba executable Amoeba
build-depends: base >= 4 build-depends: base >= 4
Expand All @@ -82,7 +83,6 @@ executable Amoeba
Application.Boot, Application.Boot,
Application.Environment, Application.Environment,
Application.MainLoop, Application.MainLoop,
GameLogic.Runtime.Game,
GameLogic.AI.AI, GameLogic.AI.AI,
GameLogic.AI.GenericAI, GameLogic.AI.GenericAI,
GameLogic.Assets.Scenarios, GameLogic.Assets.Scenarios,
Expand Down Expand Up @@ -113,5 +113,7 @@ executable Amoeba
Test.EitherMonadCallTest, Test.EitherMonadCallTest,
Test.TypeFamilyTest2, Test.TypeFamilyTest2,
Test.TypeFamilyTest1, Test.TypeFamilyTest1,
GameLogic.Language.Scheme GameLogic.Language.Scheme,
GameLogic.Data.Game,
Test.Utils.WorldArfData
sers.ItemParser sers.ItemParser
6 changes: 5 additions & 1 deletion Amoeba/GameLogic/Data/Facade.hs
@@ -1,7 +1,11 @@
module GameLogic.Data.Facade ( module GameLogic.Data.Facade (
module GameLogic.Data.Player module GameLogic.Data.Player
, module GameLogic.Data.Object , module GameLogic.Data.Object
, module GameLogic.Data.World
, module GameLogic.Data.Game
) where ) where


import GameLogic.Data.Player import GameLogic.Data.Player
import GameLogic.Data.Object import GameLogic.Data.Object
import GameLogic.Data.World
import GameLogic.Data.Game
@@ -1,11 +1,11 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}


module GameLogic.Game where module GameLogic.Data.Game where


import Control.Lens import Control.Lens
import System.Random import System.Random


import GameLogic.World import GameLogic.Data.World


data Game = Game { _world :: World data Game = Game { _world :: World
, _rndGen :: StdGen } , _rndGen :: StdGen }
Expand Down
@@ -1,12 +1,12 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
module GameLogic.Runtime.World where module GameLogic.Data.World where


import qualified Control.Lens as L import qualified Control.Lens as L
import qualified Data.Map as M import qualified Data.Map as M
import Prelude hiding (null, lookup) import Prelude hiding (null, lookup)


import GameLogic.Base.Geometry import GameLogic.Base.Geometry
import GameLogic.Data.Facade as O import GameLogic.Data.Object


data Effect = Effect data Effect = Effect
deriving (Show, Read, Eq) deriving (Show, Read, Eq)
Expand All @@ -17,11 +17,11 @@ data Action = Action
deriving (Show, Read, Eq) deriving (Show, Read, Eq)


type WorldMap = M.Map Point Object type WorldMap = M.Map Point Object
data World = World { _worldMap :: WorldMap data World = World { worldMap :: WorldMap
, _effectMap :: EffectMap , effectMap :: EffectMap
, _width :: Int , width :: Int
, _height :: Int , height :: Int
, _defaultCell :: Maybe Object , defaultCell :: Maybe Object
} }
deriving (Show, Read, Eq) deriving (Show, Read, Eq)


Expand Down
2 changes: 1 addition & 1 deletion Amoeba/GameLogic/Language/Parsing/Common.hs
Expand Up @@ -9,4 +9,4 @@ emptyToken :: GenParser Char st RawToken
emptyToken = eol >> return EmptyToken emptyToken = eol >> return EmptyToken


comment :: GenParser Char st RawToken comment :: GenParser Char st RawToken
comment = liftM Comment commentString comment = liftM CommentToken commentString
2 changes: 1 addition & 1 deletion Amoeba/GameLogic/Language/Parsing/ItemParser.hs
Expand Up @@ -11,7 +11,7 @@ item = do
itemName <- stringConstant itemName <- stringConstant
lineEnd lineEnd
rs <- resources rs <- resources
return $ Item itemName rs return $ ItemToken itemName rs


resources :: GenParser Char st [PropertyToken] resources :: GenParser Char st [PropertyToken]
resources = many resource resources = many resource
Expand Down
6 changes: 3 additions & 3 deletions Amoeba/GameLogic/Language/Parsing/WorldParser.hs
Expand Up @@ -12,13 +12,13 @@ world = do
itemName <- stringConstant itemName <- stringConstant
lineEnd lineEnd
rs <- properties rs <- properties
return $ World itemName rs return $ WorldToken itemName rs


properties :: GenParser Char st [PropertyToken] properties :: GenParser Char st [PropertyToken]
properties = many property properties = many property


{- Uncomment for GHC 7.6. {- Uncomment for GHC 7.6.
{-# LANGUAGE MultiWayIf #-} LANGUAGE MultiWayIf
property :: GenParser Char st PropertyToken property :: GenParser Char st PropertyToken
property = do property = do
identation 4 identation 4
Expand Down Expand Up @@ -63,7 +63,7 @@ object = do
objectName <- stringConstant objectName <- stringConstant
many trueSpace many trueSpace
playerName <- stringConstant playerName <- stringConstant
return $ Object objectName playerName return $ ObjectToken objectName playerName


cellsProperty :: String -> GenParser Char st PropertyToken cellsProperty :: String -> GenParser Char st PropertyToken
cellsProperty name = do cellsProperty name = do
Expand Down
8 changes: 4 additions & 4 deletions Amoeba/GameLogic/Language/RawToken.hs
Expand Up @@ -11,9 +11,9 @@ data PropertyToken = IntProperty Name Int
| IntResource Name (Int, Int) | IntResource Name (Int, Int)
deriving (Show, Read, Eq) deriving (Show, Read, Eq)


data RawToken = Comment String data RawToken = CommentToken String
| World Name [PropertyToken] | WorldToken Name [PropertyToken]
| Item Name [PropertyToken] | ItemToken Name [PropertyToken]
| Object Name PlayerName | ObjectToken Name PlayerName
| EmptyToken | EmptyToken
deriving (Show, Read, Eq) deriving (Show, Read, Eq)
43 changes: 18 additions & 25 deletions Amoeba/GameLogic/Language/Translating/Actions.hs
Expand Up @@ -3,47 +3,40 @@ module GameLogic.Language.Translating.Actions where
import GameLogic.Language.RawToken import GameLogic.Language.RawToken
import GameLogic.Language.Translating.Runtime import GameLogic.Language.Translating.Runtime


import GameLogic.Data.World

import Prelude hiding (log) import Prelude hiding (log)
import Control.Monad.Trans.Either (left) import Control.Monad.Trans.Either (left)


-- Binds trigger to an action -- Binds trigger to an action
(/>) :: Show a => (a -> Bool) -> (a -> Trans ()) -> a -> Trans () (/>) :: Show a => (a -> Bool) -> (a -> Trans ()) -> a -> Trans ()
(/>) trigger act token = if trigger token (/>) trigger act token = if trigger token
then act token then act token
else logExt $ "Token not triggered: " ++ show token else logExt $ "Token hasn't been triggered: " ++ show token


-- Action that do nothing, only logs info. -- Action that do nothing, only logs info.
skip :: Show a => a -> Trans () skip :: Show a => a -> Trans ()
skip t = log $ "Skip for: " ++ show t skip t = log $ "Skip for: " ++ show t


-- Action inserts item as object template. -- Action inserts item as object template.
addItem (Item name props) = do addItem (ItemToken name props) = do
log $ "Adding object template for: " ++ show name log $ "Adding object template for: " ++ show name
insertObjectTemplate name props insertObjectTemplate name props
addItem t = left $ "addItem: Item expected but got " ++ show t addItem t = left $ "addItem: unexpected token got: " ++ show t


{- setupWorld rules (WorldToken name props) = do
setupWorld (World name props) = do
log "Setting World."
wh <- get int "width" props
ht <- get int "height" props
cells <- getWorldCells props
constructWorld wg ht cells
setupWorld t = left $ "setupWorld: World expected but got " ++ show t
getIntProperty :: String -> PropertyToken -> Trans Int
getIntProperty name (IntProperty n i : ps) | name == n = return i
getIntProperty name _ =
getWorldCells props = do
-}

--setupWorld :: a -> PropertyToken -> Trans ()
setupWorld rules (World name props) = do
log $ "Setting World: " ++ name ++ "." log $ "Setting World: " ++ name ++ "."
translate rules props translate rules props
setupWorld _ t = left $ "setupWorld: World expected but got " ++ show t setupWorld _ t = left $ "setupWorld: unexpected token got: " ++ show t

setWidth (IntProperty _ i) = do
log "setWidth"
-- w <- getWorld
-- setWorld $ w {

setWidth p = left $ "setWidth: unexpected property got: " ++ show p

setHeight (IntProperty _ i) = log $ "setHeight"
setHeight p = left $ "setHeight: unexpected property got: " ++ show p


setWidth _ = log $ "setWidth"
setHeight _ = log $ "setHeight"
setCells _ = log $ "setCells" setCells _ = log $ "setCells"
51 changes: 28 additions & 23 deletions Amoeba/GameLogic/Language/Translating/Runtime.hs
Expand Up @@ -2,20 +2,20 @@ module GameLogic.Language.Translating.Runtime where


import Control.Monad (when, liftM) import Control.Monad (when, liftM)
import Control.Monad.State import Control.Monad.State
import Control.Monad.Trans --import Control.Monad.Trans
import Control.Monad.Trans.Either as E import Control.Monad.Trans.Either as E
import qualified Data.Map as M import qualified Data.Map as M
import Prelude hiding (log) import Prelude hiding (log)


import GameLogic.Data.Types import GameLogic.Data.Types
import GameLogic.Data.World
import GameLogic.Language.RawToken import GameLogic.Language.RawToken
import GameLogic.Runtime.World


type ObjectTemplate = (ObjectType, [PropertyToken]) type ObjectTemplate = (ObjectType, [PropertyToken])


data TransRt = TransRt { trtNextId :: State TransRt Int data TransRt = TransRt { trtNextId :: State TransRt Int
, trtItemMap :: M.Map String ObjectTemplate , trtItemMap :: M.Map String ObjectTemplate
, trtWorldConstructor :: String --State TransRt World , trtWorld :: World
, trtLog :: [String] , trtLog :: [String]
, trtExtendedLogs :: Bool , trtExtendedLogs :: Bool
} }
Expand All @@ -27,6 +27,9 @@ type Trans a = EitherT String (State TransRt) a
getExtendedLogs :: Trans Bool getExtendedLogs :: Trans Bool
getExtendedLogs = liftM trtExtendedLogs get getExtendedLogs = liftM trtExtendedLogs get


getWorld :: Trans World
getWorld = liftM trtWorld get

log s = do log s = do
ctx <- get ctx <- get
let newCtx = ctx { trtLog = trtLog ctx ++ [s] } let newCtx = ctx { trtLog = trtLog ctx ++ [s] }
Expand All @@ -36,27 +39,10 @@ logExt s = do
isExtendedLogs <- getExtendedLogs isExtendedLogs <- getExtendedLogs
when isExtendedLogs (log s) when isExtendedLogs (log s)


initialRt idCounter = TransRt idCounter M.empty "Empty" [] False

-- Specific

getItemMap = liftM trtItemMap get
putItemMap m = do
ctx <- get
put $ ctx { trtItemMap = m }


objectTemplate objType props = (objType, props) initialRt idCounter = TransRt idCounter M.empty emptyWorld [] False


insertObjectTemplate :: String -> [PropertyToken] -> Trans () -- Makes from rules list the list of context modifiers.
insertObjectTemplate name props = do
m <- getItemMap
let objectType = M.size m + 1
case M.lookup name m of
Nothing -> putItemMap $ M.insert name (objectTemplate objectType props) m
Just _ -> left $ "Object template for item " ++ name ++ " is duplicated."
log $ "Object template for item " ++ name ++ " added."

-- Makes from rules list the list of context modificators.
-- Folds the new list with applying to current context. -- Folds the new list with applying to current context.
apply rules t = mapM_ ($ t) rules apply rules t = mapM_ ($ t) rules


Expand All @@ -74,4 +60,23 @@ apply rules t = do
foldM_ (ctxModifier t) ctx rules foldM_ (ctxModifier t) ctx rules
-} -}


translate rules = mapM_ (apply rules) translate rules = mapM_ (apply rules)

-- Specific

getItemMap = liftM trtItemMap get
putItemMap m = do
ctx <- get
put $ ctx { trtItemMap = m }

objectTemplate objType props = (objType, props)

insertObjectTemplate :: String -> [PropertyToken] -> Trans ()
insertObjectTemplate name props = do
m <- getItemMap
let objectType = M.size m + 1
case M.lookup name m of
Nothing -> putItemMap $ M.insert name (objectTemplate objectType props) m
Just _ -> left $ "Object template for item " ++ name ++ " is duplicated."
log $ "Object template for item " ++ name ++ " added."

11 changes: 2 additions & 9 deletions Amoeba/GameLogic/Language/Translating/Translator.hs
Expand Up @@ -3,19 +3,12 @@ module GameLogic.Language.Translating.Translator where
import GameLogic.Language.Parsing.RawParser as RP import GameLogic.Language.Parsing.RawParser as RP
import GameLogic.Language.RawToken as RT import GameLogic.Language.RawToken as RT


import qualified GameLogic.Runtime.World as W
import qualified GameLogic.Data.Facade as D

import GameLogic.Language.Translating.Runtime import GameLogic.Language.Translating.Runtime
import GameLogic.Language.Translating.Rules import GameLogic.Language.Translating.Rules


import Prelude hiding (log)

import Control.Monad.State import Control.Monad.State
import Control.Monad.Trans --import Control.Monad.Trans
import Control.Monad.Trans.Either import Control.Monad.Trans.Either (runEitherT)
import Control.Monad
import Data.Either.Combinators (isRight)




nextId :: Int -> State TransRt Int nextId :: Int -> State TransRt Int
Expand Down
6 changes: 3 additions & 3 deletions Amoeba/GameLogic/Language/Translating/Triggers.hs
Expand Up @@ -3,16 +3,16 @@ module GameLogic.Language.Translating.Triggers where
import GameLogic.Language.RawToken import GameLogic.Language.RawToken


onComment :: RawToken -> Bool onComment :: RawToken -> Bool
onComment (Comment _) = True onComment (CommentToken _) = True
onComment _ = False onComment _ = False


onEmpty EmptyToken = True onEmpty EmptyToken = True
onEmpty _ = False onEmpty _ = False


onItem (Item n props) = True onItem (ItemToken n props) = True
onItem _ = False onItem _ = False


onWorld (World name props) = True onWorld (WorldToken name props) = True
onWorld _ = False onWorld _ = False


onProp :: String -> PropertyToken -> Bool onProp :: String -> PropertyToken -> Bool
Expand Down
2 changes: 1 addition & 1 deletion Amoeba/Test/Data/Raws/World2.adt
@@ -1 +1 @@
Right [Comment " World definition file",EmptyToken,World "Pandora" [IntProperty "width" 20,IntProperty "height" 20,ObjectProperty "defaultCell" (Object "Empty" "Player0"),CellsProperty "cells" [CellProperty "cell" (10,10) (Object "Karyon" "Player1"),CellProperty "cell" (9,9) (Object "Plasma" "Player1"),CellProperty "cell" (9,10) (Object "Plasma" "Player1"),CellProperty "cell" (9,11) (Object "Plasma" "Player1"),CellProperty "cell" (10,9) (Object "Plasma" "Player1"),CellProperty "cell" (10,11) (Object "Plasma" "Player1"),CellProperty "cell" (11,9) (Object "Plasma" "Player1"),CellProperty "cell" (11,10) (Object "Plasma" "Player1"),CellProperty "cell" (11,11) (Object "Plasma" "Player1"),CellProperty "cell" (15,15) (Object "Karyon" "Player2")]]] Right [CommentToken " World definition file",EmptyToken,WorldToken "Pandora" [IntProperty "width" 20,IntProperty "height" 20,ObjectProperty "defaultCell" (ObjectToken "Empty" "Player0"),CellsProperty "cells" [CellProperty "cell" (10,10) (ObjectToken "Karyon" "Player1"),CellProperty "cell" (9,9) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (9,10) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (9,11) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (10,9) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (10,11) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (11,9) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (11,10) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (11,11) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (15,15) (ObjectToken "Karyon" "Player2")]]]
2 changes: 1 addition & 1 deletion Amoeba/Test/Data/Raws/World3.adt
@@ -1 +1 @@
Right [EmptyToken,Comment " General items",EmptyToken,Item "Karyon" [IntResource "lifebound" (0,5000),IntResource "durability" (100,100),IntResource "energy" (300,2000)],EmptyToken,Comment " Conductor",Item "Conductor" [IntResource "lifebound" (0,1000),IntResource "durability" (100,100),IntResource "energy" (0,100)],EmptyToken,Comment " World definition file",EmptyToken,World "Pandora" [IntProperty "width" 20,IntProperty "height" 20,ObjectProperty "defaultCell" (Object "Empty" "Player0"),CellsProperty "cells" [CellProperty "cell" (10,10) (Object "Karyon" "Player1"),CellProperty "cell" (9,9) (Object "Plasma" "Player1"),CellProperty "cell" (9,10) (Object "Plasma" "Player1"),CellProperty "cell" (9,11) (Object "Plasma" "Player1"),CellProperty "cell" (10,9) (Object "Plasma" "Player1"),CellProperty "cell" (10,11) (Object "Plasma" "Player1"),CellProperty "cell" (11,9) (Object "Plasma" "Player1"),CellProperty "cell" (11,10) (Object "Plasma" "Player1"),CellProperty "cell" (11,11) (Object "Plasma" "Player1"),CellProperty "cell" (15,15) (Object "Karyon" "Player2")]]] Right [EmptyToken,CommentToken " General items",EmptyToken,ItemToken "Karyon" [IntResource "lifebound" (0,5000),IntResource "durability" (100,100),IntResource "energy" (300,2000)],EmptyToken,CommentToken " Conductor",ItemToken "Conductor" [IntResource "lifebound" (0,1000),IntResource "durability" (100,100),IntResource "energy" (0,100)],EmptyToken,CommentToken " World definition file",EmptyToken,WorldToken "Pandora" [IntProperty "width" 20,IntProperty "height" 20,ObjectProperty "defaultCell" (ObjectToken "Empty" "Player0"),CellsProperty "cells" [CellProperty "cell" (10,10) (ObjectToken "Karyon" "Player1"),CellProperty "cell" (9,9) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (9,10) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (9,11) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (10,9) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (10,11) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (11,9) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (11,10) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (11,11) (ObjectToken "Plasma" "Player1"),CellProperty "cell" (15,15) (ObjectToken "Karyon" "Player2")]]]
2 changes: 1 addition & 1 deletion Amoeba/Test/EitherMonadCallTest.hs
Expand Up @@ -17,4 +17,4 @@ callEitherMonad = do


main = do main = do
let res = callEitherMonad let res = callEitherMonad
putStrLn $ show res print res

0 comments on commit 64fefc7

Please sign in to comment.