<commit_before>{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE LambdaCase #-}

-- |General stuff on which other modules depend.
module Types.World where

import Data.Default
import qualified Data.Foldable as F
import Data.List (intercalate)
import qualified Data.Map as M
import Math.Geometry.Grid.Square
import Math.Geometry.Grid.SquareInternal (SquareDirection(..))

import Debug.Trace

instance Ord SquareDirection where
   x <= y = fromEnum x <= fromEnum y

instance Enum SquareDirection where
   toEnum 0 = North
   toEnum 1 = East
   toEnum 2 = South
   toEnum 3 = West
   toEnum n = error $ "toEnum (SquareDirection) with invalid value " ++ show n

   fromEnum North = 0
   fromEnum East = 1
   fromEnum South = 2
   fromEnum West = 3

instance Bounded SquareDirection where
   minBound = North
   maxBound = West

-- |A value between in percent. 1 corresponds to 100%.
type Percentage = Rational

type EntityName = String
type GestureName = String

-- |Enumeration for entity types.
data EntityType = TyAgent | TyWumpus deriving (Show, Eq, Ord, Read)

-- Agent data
-------------------------------------------------------------------------------

data Item = Gold | Fruit | Meat
   deriving (Show, Eq, Ord, Enum, Bounded)

-- |An action that an agent can take.
data Action =
   NoOp
   | Rotate SquareDirection
   | Move SquareDirection
   | Attack SquareDirection
   | Give SquareDirection Item
   | Gather
   | Collect Item
   | Drop Item
   | Eat Item
   | Gesture SquareDirection String
   deriving (Show, Eq, Ord)

-- |A agent's entire state.
data Agent s = Agent {
   _agentName :: EntityName,
   _agentDirection :: SquareDirection,
   _agentHealth :: Rational,
   _agentStamina :: Rational,
   _agentInventory :: M.Map Item Int,
   _agentState :: s
   }

-- |Equality by agent name.
instance Eq (Agent s) where
   Agent{_agentName = n1} == Agent{_agentName = n2} = n1 == n2

instance Default s => Default (Agent s) where
   def = Agent "" North 1 1 M.empty def

-- |The slice of an agent's state that another agent
--  may perceive visually.
data VisualAgent = VisualAgent {
   _visualAgentName :: EntityName,
   _visualAgentDirection :: SquareDirection,
   _visualAgentHealth :: Rational,
   _visualAgentStamina :: Rational,
   _visualAgentInventory :: Maybe (M.Map Item Int)
   }
   deriving (Show, Eq, Ord)

-- Wumpus data
-------------------------------------------------------------------------------

data Wumpus s = Wumpus {
   _wumpusState :: s,
   _wumpusName :: EntityName,
   _wumpusHealth :: Rational,
   _wumpusStamina :: Rational
   }

-- |Equality by wumpus name.
instance Eq (Wumpus s) where
   Wumpus{_wumpusName = n1} == Wumpus{_wumpusName = n2} = n1 == n2


data VisualWumpus = VisualWumpus {
   _visualWumpusName :: EntityName,
   _visualWumpusHealth :: Rational,
   _visualWumpusStamina :: Rational
   }
   deriving (Show, Eq, Ord)


-- Entities
-------------------------------------------------------------------------------

data Entity s t = Ag s | Wu t
   deriving (Show, Eq, Ord)

type Entity' = Entity (Agent SomeMind) (Wumpus SomeMind)

-- |Returns True iff the entity is an agent.
isAgent :: Entity s t -> Bool
isAgent (Ag _) = True
isAgent _      = False

-- |Returns True iff the entity is a Wumpus.
isWumpus :: Entity s t -> Bool
isWumpus (Wu _) = True
isWumpus _      = False

fromAgent :: Entity s t -> s
fromAgent (Ag s) = s
fromAgent _ = error "fromAgent called on non-Agent!"

fromWumpus :: Entity s t -> t
fromWumpus (Wu s) = s
fromWumpus _ = error "fromWumpus called on non-Wumpus!"
<commit_msg>Added default instances for VisualAgent and VisualWumpus.hs<commit_after>{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE LambdaCase #-}

-- |General stuff on which other modules depend.
module Types.World where

import Data.Default
import qualified Data.Foldable as F
import Data.List (intercalate)
import qualified Data.Map as M
import Math.Geometry.Grid.Square
import Math.Geometry.Grid.SquareInternal (SquareDirection(..))

import Debug.Trace

instance Ord SquareDirection where
   x <= y = fromEnum x <= fromEnum y

instance Enum SquareDirection where
   toEnum 0 = North
   toEnum 1 = East
   toEnum 2 = South
   toEnum 3 = West
   toEnum n = error $ "toEnum (SquareDirection) with invalid value " ++ show n

   fromEnum North = 0
   fromEnum East = 1
   fromEnum South = 2
   fromEnum West = 3

instance Bounded SquareDirection where
   minBound = North
   maxBound = West

-- |A value between in percent. 1 corresponds to 100%.
type Percentage = Rational

type EntityName = String
type GestureName = String

-- |Enumeration for entity types.
data EntityType = TyAgent | TyWumpus deriving (Show, Eq, Ord, Read)

-- Agent data
-------------------------------------------------------------------------------

data Item = Gold | Fruit | Meat
   deriving (Show, Eq, Ord, Enum, Bounded)

-- |An action that an agent can take.
data Action =
   NoOp
   | Rotate SquareDirection
   | Move SquareDirection
   | Attack SquareDirection
   | Give SquareDirection Item
   | Gather
   | Collect Item
   | Drop Item
   | Eat Item
   | Gesture SquareDirection String
   deriving (Show, Eq, Ord)

-- |A agent's entire state.
data Agent s = Agent {
   _agentName :: EntityName,
   _agentDirection :: SquareDirection,
   _agentHealth :: Rational,
   _agentStamina :: Rational,
   _agentInventory :: M.Map Item Int,
   _agentState :: s
   }

-- |Equality by agent name.
instance Eq (Agent s) where
   Agent{_agentName = n1} == Agent{_agentName = n2} = n1 == n2

instance Default s => Default (Agent s) where
   def = Agent "" North 1 1 M.empty def

-- |The slice of an agent's state that another agent
--  may perceive visually.
data VisualAgent = VisualAgent {
   _visualAgentName :: EntityName,
   _visualAgentDirection :: SquareDirection,
   _visualAgentHealth :: Rational,
   _visualAgentStamina :: Rational,
   _visualAgentInventory :: Maybe (M.Map Item Int)
   }
   deriving (Show, Eq, Ord)

instance Default VisualAgent where
   def = VisualAgent "" North 0 0 Nothing

-- Wumpus data
-------------------------------------------------------------------------------

data Wumpus s = Wumpus {
   _wumpusState :: s,
   _wumpusName :: EntityName,
   _wumpusHealth :: Rational,
   _wumpusStamina :: Rational
   }

-- |Equality by wumpus name.
instance Eq (Wumpus s) where
   Wumpus{_wumpusName = n1} == Wumpus{_wumpusName = n2} = n1 == n2


data VisualWumpus = VisualWumpus {
   _visualWumpusName :: EntityName,
   _visualWumpusHealth :: Rational,
   _visualWumpusStamina :: Rational
   }
   deriving (Show, Eq, Ord)

instance Default VisualWumpus where
   def = VisualWumpus "" 0 0


-- Entities
-------------------------------------------------------------------------------

data Entity s t = Ag s | Wu t
   deriving (Show, Eq, Ord)

type Entity' = Entity (Agent SomeMind) (Wumpus SomeMind)

-- |Returns True iff the entity is an agent.
isAgent :: Entity s t -> Bool
isAgent (Ag _) = True
isAgent _      = False

-- |Returns True iff the entity is a Wumpus.
isWumpus :: Entity s t -> Bool
isWumpus (Wu _) = True
isWumpus _      = False

fromAgent :: Entity s t -> s
fromAgent (Ag s) = s
fromAgent _ = error "fromAgent called on non-Agent!"

fromWumpus :: Entity s t -> t
fromWumpus (Wu s) = s
fromWumpus _ = error "fromWumpus called on non-Wumpus!"

{'fraction_non_alphanumeric': 0.1496966499604326, 'fraction_numerical': 0.006330783434450013, 'mean_word_length': 2.6212989493791787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5773133', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2320, 'n_words': 802}