mw-engine.utils
Utility functions needed by MicroWorld and, specifically, in the interpretation of MicroWorld rule.
add-history-event
(add-history-event cell rule)
(add-history-event result rule detail)
If cell
is non-nil, expect it to be a map representing a cell; add to its history an an event recording the firing of this rule. If detail
is passed, treat it as a map of additional data to be added to the event.
get-cell
(get-cell world x y)
Return the cell a x, y in this world, if any.
world
a world as defined in world.clj;x
a number which may or may not be a valid x coordinate within that world;y
a number which may or may not be a valid y coordinate within that world.
get-int
(get-int map key)
Get the value of a property expected to be an integer from a map; if not present (or not an integer) return 0.
map
a map;key
a symbol or keyword, presumed to be a key into themap
.
get-int-or-zero
(get-int-or-zero map property)
Return the value of this property
from this map
if it is a integer; otherwise return zero.
get-least-cell
(get-least-cell cells property)
Return the cell from among these cells
which has the lowest numeric value for this property
.
get-most-cell
(get-most-cell cells property)
Return the cell from among these cells
which has the highest numeric value for this property
.
get-neighbours
(get-neighbours world x y depth)
(get-neighbours world cell depth)
(get-neighbours world cell)
Get the neighbours to distance depth of a cell in this world.
Several overloads: * world
a world, as described in world.clj; * cell
a cell within that world Gets immediate neighbours of the specified cell.
world
a world, as described inworld.clj;cell
a cell within that worlddepth
an integer representing the depth to search from thecell
Gets neighbours within the specified distance of the cell.-
world
a world, as described inworld.clj; x
an integer representing an x coordinate in that world;y
an integer representing an y coordinate in that world;depth
an integer representing the distance from x,y that should be searched Gets the neighbours within the specified distance of the cell at coordinates x,y in this world.
get-neighbours-with-property-value
(get-neighbours-with-property-value world x y depth property value op)
(get-neighbours-with-property-value world x y depth property value)
(get-neighbours-with-property-value world cell depth property value)
(get-neighbours-with-property-value world cell property value)
Get the neighbours to distance depth of the cell at x, y in this world which have this value for this property.
world
a world, as described in world.clj;cell
a cell within that world;depth
an integer representing the distance from x,y that should be searched (optional);property
a keyword representing a property of the neighbours;value
a value of that property (or, possibly, the name of another);op
a comparator function to use in place of=
(optional).
It gets messy.
get-neighbours-with-state
(get-neighbours-with-state world x y depth state)
(get-neighbours-with-state world cell depth state)
(get-neighbours-with-state world cell state)
get-num
macro
(get-num map key)
Get the value of a property expected to be a number from a map; if not present (or not a number) return 0.
map
a map;key
a symbol or keyword, presumed to be a key into themap
.
history-string
(history-string cell)
Return the history of this cell
as a string for presentation to the user.
in-bounds
deprecated in 1.1.7
(in-bounds world x y)
True if x, y are in bounds for this world (i.e., there is a cell at x, y) else false. DEPRECATED: it’s a predicate, prefer in-bounds?
.
world
a world as defined in world.clj;x
a number which may or may not be a valid x coordinate within that world;y
a number which may or may not be a valid y coordinate within that world.
in-bounds?
added in 1.1.7
(in-bounds? world x y)
True if x, y are in bounds for this world (i.e., there is a cell at x, y) else false.
world
a world as defined in world.clj;x
a number which may or may not be a valid x coordinate within that world;y
a number which may or may not be a valid y coordinate within that world.
init-generation
(init-generation _ cell)
Return a cell like this cell
, but having a value for :generation, zero if the cell passed had no integer value for generation, otherwise the value taken from the cell passed. The world
argument is present only for consistency with the rule engine and is ignored.
map-world
(map-world world function)
(map-world world function additional-args)
Apply this function
to each cell in this world
to produce a new world. the arguments to the function will be the world, the cell, and any additional-args
supplied. Note that we parallel map over rows but just map over cells within a row. That’s because it isn’t worth starting a new thread for each cell, but there may be efficiency gains in running rows in parallel.
map-world-n-n
(map-world-n-n world function)
(map-world-n-n world function additional-args)
Wholly non-parallel map world implementation; see documentation for map-world
.
map-world-p-p
(map-world-p-p world function)
(map-world-p-p world function additional-args)
Wholly parallel map-world implementation; see documentation for map-world
.
memo-get-neighbours
Memoised get neighbours is more efficient when running deeply recursive algorithms on the same world. But it’s less efficient when running the engine in its normal iterative style, because then we will rarely call get naighbours on the same cell of the same world twice.
merge-cell
(merge-cell world cell)
Return a world like this world
, but merge the values from this cell
with those from the cell in the world with the same co-ordinates
population
(population cell species)
Return the population of this species in this cell. Currently a synonym for get-int
, but may not always be (depending whether species are later implemented as actors)
cell
a map;species
a keyword representing a species which may populate that cell.
set-property
(set-property world cell property value)
(set-property world x y property value)
Return a world like this world
but with the value of exactly one property
of one cell
changed to this value
summarise-history
(summarise-history cell)
Return, as a string, a shorter summary of the history of this cell