Copyright | Brent Yorgey |
---|---|
License | BSD-3-Clause |
Maintainer | byorgey@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
A data type to represent robots.
Synopsis
- data LogSource
- = Said
- | Logged
- | ErrorTrace
- data LogEntry = LogEntry {
- _leTime :: Integer
- _leSaid :: LogSource
- _leRobotName :: Text
- _leRobotID :: Int
- _leLocation :: V2 Int64
- _leText :: Text
- leText :: Lens' LogEntry Text
- leSaid :: Lens' LogEntry LogSource
- leRobotName :: Lens' LogEntry Text
- leTime :: Lens' LogEntry Integer
- leLocation :: Lens' LogEntry (V2 Int64)
- leRobotID :: Lens' LogEntry Int
- data RobotPhase
- type RID = Int
- data RobotR (phase :: RobotPhase)
- type Robot = RobotR 'ConcreteRobot
- type TRobot = RobotR 'TemplateRobot
- data RobotContext
- defTypes :: Lens' RobotContext TCtx
- defReqs :: Lens' RobotContext ReqCtx
- defVals :: Lens' RobotContext Env
- defStore :: Lens' RobotContext Store
- robotEntity :: Lens' (RobotR phase) Entity
- robotName :: Lens' Robot Text
- trobotName :: Lens' TRobot Text
- robotCreatedAt :: Lens' Robot TimeSpec
- robotDisplay :: Lens' Robot Display
- robotLocation :: Getter Robot (V2 Int64)
- unsafeSetRobotLocation :: V2 Int64 -> Robot -> Robot
- trobotLocation :: Lens' TRobot (Maybe (V2 Int64))
- robotOrientation :: Lens' Robot (Maybe (V2 Int64))
- robotInventory :: Lens' Robot Inventory
- installedDevices :: Lens' Robot Inventory
- robotLog :: Lens' Robot (Seq LogEntry)
- robotLogUpdated :: Lens' Robot Bool
- inventoryHash :: Getter Robot Int
- robotCapabilities :: Getter Robot (Set Capability)
- robotContext :: Lens' Robot RobotContext
- robotID :: Getter Robot RID
- robotParentID :: Lens' Robot (Maybe RID)
- robotHeavy :: Lens' Robot Bool
- machine :: Lens' Robot CESK
- systemRobot :: Lens' Robot Bool
- selfDestruct :: Lens' Robot Bool
- tickSteps :: Lens' Robot Int
- runningAtomic :: Lens' Robot Bool
- mkRobot :: RobotID phase -> Maybe Int -> Text -> [Text] -> RobotLocation phase -> V2 Int64 -> Display -> CESK -> [Entity] -> [(Count, Entity)] -> Bool -> Bool -> TimeSpec -> RobotR phase
- instantiateRobot :: RID -> TRobot -> Robot
- robotKnows :: Robot -> Entity -> Bool
- isActive :: Robot -> Bool
- waitingUntil :: Robot -> Maybe Integer
- getResult :: Robot -> Maybe (Value, Store)
- hearingDistance :: Num i => i
Robots data
Robot log entries
Instances
FromJSON LogSource Source # | |
ToJSON LogSource Source # | |
Defined in Swarm.Game.Robot | |
Generic LogSource Source # | |
Show LogSource Source # | |
Eq LogSource Source # | |
Ord LogSource Source # | |
Defined in Swarm.Game.Robot | |
type Rep LogSource Source # | |
Defined in Swarm.Game.Robot type Rep LogSource = D1 ('MetaData "LogSource" "Swarm.Game.Robot" "swarm-0.1.1.0-ARNQOCsge3P61nb92PmOB2" 'False) (C1 ('MetaCons "Said" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "Logged" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ErrorTrace" 'PrefixI 'False) (U1 :: Type -> Type))) |
An entry in a robot's log.
LogEntry | |
|
Instances
Robots
data RobotPhase Source #
The phase of a robot description record.
TemplateRobot | The robot record has just been read in from a scenario description; it represents a template that may later be instantiated as one or more concrete robots. |
ConcreteRobot | The robot record represents a concrete robot in the world. |
data RobotR (phase :: RobotPhase) Source #
A value of type RobotR
is a record representing the state of a
single robot. The f
parameter is for tracking whether or not
the robot has been assigned a unique ID.
Instances
FromJSONE EntityMap TRobot Source # | We can parse a robot from a YAML file if we have access to an
|
Defined in Swarm.Game.Robot | |
(ToJSON (RobotLocation phase), ToJSON (RobotID phase)) => ToJSON (RobotR phase) Source # | |
Defined in Swarm.Game.Robot | |
Generic (RobotR phase) Source # | |
(Show (RobotLocation phase), Show (RobotID phase)) => Show (RobotR phase) Source # | |
(Eq (RobotLocation phase), Eq (RobotID phase)) => Eq (RobotR phase) Source # | |
type Rep (RobotR phase) Source # | |
Defined in Swarm.Game.Robot |
type Robot = RobotR 'ConcreteRobot Source #
A concrete robot, with a unique ID number and a specific location.
type TRobot = RobotR 'TemplateRobot Source #
A template robot, i.e. a template robot record without a unique ID number, and possibly without a location.
Robot context
data RobotContext Source #
A record that stores the information
for all defintions stored in a Robot
Instances
Lenses
robotEntity :: Lens' (RobotR phase) Entity Source #
Robots are not entities, but they have almost all the
characteristics of one (or perhaps we could think of robots as
very special sorts of entities), so for convenience each robot
carries an Entity
record to store all the information it has in
common with any Entity
.
Note there are various lenses provided for convenience that
directly reference fields inside this record; for example, one
can use robotName
instead of writing
.robotEntity
. entityName
robotDisplay :: Lens' Robot Display Source #
The Display
of a robot. This is a special lens that
automatically sets the curOrientation
to the orientation of the
robot every time you do a get
operation. Technically this does
not satisfy the lens laws---in particular, the get/put law does
not hold. But we should think of the curOrientation
as being
simply a cache of the displayed entity's direction.
robotLocation :: Getter Robot (V2 Int64) Source #
The robot's current location, represented as (x,y). This is only
a getter, since when changing a robot's location we must remember
to update the robotsByLocation
map as well. You can use the
updateRobotLocation
function for this purpose.
unsafeSetRobotLocation :: V2 Int64 -> Robot -> Robot Source #
Set a robot's location. This is unsafe and should never be
called directly except by the updateRobotLocation
function.
The reason is that we need to make sure the robotsByLocation
map stays in sync.
trobotLocation :: Lens' TRobot (Maybe (V2 Int64)) Source #
A template robot's location. Unlike robotLocation
, this is a
lens, since when dealing with robot templates there is as yet no
robotsByLocation
map to keep up-to-date.
installedDevices :: Lens' Robot Inventory Source #
A separate inventory for "installed devices", which provide the robot with certain capabilities.
Note that every time the inventory of installed devices is
modified, this lens recomputes a cached set of the capabilities
the installed devices provide, to speed up subsequent lookups to
see whether the robot has a certain capability (see robotCapabilities
)
robotLog :: Lens' Robot (Seq LogEntry) Source #
The robot's own private message log, most recent message last.
Messages can be added both by explicit use of the Log
command,
and by uncaught exceptions. Stored as a Data.Sequence so that
we can efficiently add to the end and also process from beginning
to end. Note that updating via this lens will also set the
robotLogUpdated
.
robotLogUpdated :: Lens' Robot Bool Source #
Has the robotLog
been updated since the last time it was
viewed?
inventoryHash :: Getter Robot Int Source #
A hash of a robot's entity record and installed devices, to facilitate quickly deciding whether we need to redraw the robot info panel.
robotCapabilities :: Getter Robot (Set Capability) Source #
Get the set of capabilities this robot possesses. This is only a
getter, not a lens, because it is automatically generated from
the installedDevices
. The only way to change a robot's
capabilities is to modify its installedDevices
.
robotContext :: Lens' Robot RobotContext Source #
The robot's context
robotID :: Getter Robot RID Source #
The (unique) ID number of the robot. This is only a Getter since the robot ID is immutable.
robotParentID :: Lens' Robot (Maybe RID) Source #
The ID number of the robot's parent, that is, the robot that built (or most recently reprogrammed) this robot, if there is one.
robotHeavy :: Lens' Robot Bool Source #
Is this robot extra heavy (thus requiring tank treads to move)?
systemRobot :: Lens' Robot Bool Source #
Is this robot a "system robot"? System robots are generated by the system (as opposed to created by the user) and are not subject to the usual capability restrictions.
tickSteps :: Lens' Robot Int Source #
The need for tickSteps
is a bit technical, and I hope I can
eventually find a different, better way to accomplish it.
Ideally, we would want each robot to execute a single
command at every game tick, so that e.g. two robots
executing move;move;move
and repeat 3 move
(given a
suitable definition of repeat
) will move in lockstep.
However, the second robot actually has to do more computation
than the first (it has to look up the definition of repeat
,
reduce its application to the number 3, etc.), so its CESK
machine will take more steps. It won't do to simply let each
robot run until executing a command---because robot programs
can involve arbitrary recursion, it is very easy to write a
program that evaluates forever without ever executing a
command, which in this scenario would completely freeze the
UI. (It also wouldn't help to ensure all programs are
terminating---it would still be possible to effectively do
the same thing by making a program that takes a very, very
long time to terminate.) So instead, we allocate each robot
a certain maximum number of computation steps per tick
(defined in evalStepsPerTick
), and it
suspends computation when it either executes a command or
reaches the maximum number of steps, whichever comes first.
It seems like this really isn't something the robot should be keeping track of itself, but that seemed the most technically convenient way to do it at the time. The robot needs some way to signal when it has executed a command, which it currently does by setting tickSteps to zero. However, that has the disadvantage that when tickSteps becomes zero, we can't tell whether that happened because the robot ran out of steps, or because it executed a command and set it to zero manually.
Perhaps instead, each robot should keep a counter saying how many commands it has executed. The loop stepping the robot can tell when the counter increments.
Creation & instantiation
:: RobotID phase | ID number of the robot. |
-> Maybe Int | ID number of the robot's parent, if it has one. |
-> Text | Name of the robot. |
-> [Text] | Description of the robot. |
-> RobotLocation phase | Initial location. |
-> V2 Int64 | Initial heading/direction. |
-> Display | Robot display. |
-> CESK | Initial CESK machine. |
-> [Entity] | Installed devices. |
-> [(Count, Entity)] | Initial inventory. |
-> Bool | Should this be a system robot? |
-> Bool | Is this robot heavy? |
-> TimeSpec | Creation date |
-> RobotR phase |
A general function for creating robots.
instantiateRobot :: RID -> TRobot -> Robot Source #
Instantiate a robot template to make it into a concrete robot, by
providing a robot ID. Concrete robots also require a location;
if the robot template didn't have a location already, just set
the location to (0,0) by default. If you want a different location,
set it via trobotLocation
before calling instantiateRobot
.
Query
getResult :: Robot -> Maybe (Value, Store) Source #
Get the result of the robot's computation if it is finished.
Constants
hearingDistance :: Num i => i Source #