flocc 0.5.18 → 0.5.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,8 +17,8 @@ declare class KDTree {
17
17
  axis(): "x" | "y" | "z" | null;
18
18
  locateSubtree(pt: Agent | Point): KDTree;
19
19
  subtreesWithinDistance(pt: Point | Agent, d: number, trees?: KDTree[]): KDTree[];
20
- intersectsAlongDimension: (pt: Agent | Point, d: number, coord: "x" | "y" | "z") => boolean;
21
- sphereIntersectsBBox: (pt: Agent | Point, d: number) => boolean;
20
+ intersectsAlongDimension: (pt: Point | Agent, d: number, coord: "x" | "y" | "z") => boolean;
21
+ sphereIntersectsBBox: (pt: Point | Agent, d: number) => boolean;
22
22
  /**
23
23
  * Return all the Agents in this KDTree that are within `d` distance
24
24
  * of the given Point or Agent `pt`.
@@ -2,157 +2,233 @@
2
2
  import { Agent } from "../agents/Agent";
3
3
  import { Environment } from "../environments/Environment";
4
4
  import { Array2D } from "./Array2D";
5
- interface AgentCallback {
6
- (agent: Agent, index: number): any;
7
- }
8
5
  /**
6
+ * A `Network` allows {@linkcode Agent}s to be connected to each other.
9
7
  * @since 0.1.3
10
8
  */
11
9
  declare class Network implements EnvironmentHelper {
10
+ /** @hidden */
12
11
  adjacencyList: Map<Agent, Agent[]>;
12
+ /**
13
+ * instantiated and updated in _resetAdjacencyMatrix
14
+ * @hidden
15
+ */
13
16
  adjacencyMatrix: Array2D;
14
17
  /**
15
- * list (JS array) of all the agents
16
- * in the order they were added to the graph
18
+ * An array of the {@linkcode Agent}s in this `Network`
19
+ * (in the order they were added).
17
20
  */
18
21
  agents: Agent[];
19
22
  /**
20
23
  * Add an agent to the network.
21
- * Returns `true` if the agent was successfully added.
22
- * Returns `false` if the agent was already in the network.
23
- * @param {Agent} agent
24
+ * @returns Returns `true` if the `Agent` was successfully added, `false` otherwise.
25
+ *
26
+ * ```js
27
+ * const a = new Agent();
28
+ * network.addAgent(a); // returns true
29
+ * network.addAgent(a); // returns false since `a` was already in the `Network`
30
+ * ```
24
31
  * @since 0.1.3
25
32
  */
26
33
  addAgent(agent: Agent): boolean;
27
34
  /**
28
- * Add all agents in an environment to this network.
29
- * @param {Environment} environment
35
+ * Given an {@linkcode Environment}, add all the {@linkcode Agent}s in that `Environment`
36
+ * to this `Network`. (This is a shortcut for calling `environment.getAgents().forEach(a => network.addAgent(a)));`)
30
37
  * @since 0.2.1
31
38
  */
32
39
  addFromEnvironment(environment: Environment): void;
33
40
  /**
34
- * Remove an agent from the network.
35
- * Returns `true` if the agent was successfully removed.
41
+ * Removes an {@linkcode Agent} from the `Network`.
42
+ *
43
+ * ```js
44
+ * const a = new Agent();
45
+ * network.addAgent(a);
46
+ *
47
+ * network.removeAgent(a); // returns true
48
+ *
49
+ * network.removeAgent(a); // returns false since `a` was no longer in the `Network`
50
+ * ```
51
+ *
52
+ * @returns Returns `true` if the agent was successfully removed.
53
+ *
36
54
  * Returns `false` if the agent was not in the network to begin with.
37
- * @param {Agent} agent
38
55
  * @since 0.1.3
39
56
  */
40
57
  removeAgent(agent: Agent): boolean;
41
58
  /**
42
- * Removes all agents from the network.
59
+ * Removes all {@linkcode Agent}s from the `Network`.
60
+ *
61
+ * ```js
62
+ * const network = new Network();
63
+ * network.addAgent(new Agent());
64
+ * network.size(); // returns 1
65
+ *
66
+ * network.clear();
67
+ * network.size(); // returns 0
68
+ * ```
69
+ *
43
70
  * @since 0.2.1
44
71
  */
45
72
  clear(): void;
46
73
  /**
47
- * Returns true if successfully connected the two agents, false otherwise
48
- * (for example, if tried to add an edge between an agent + itself
49
- * or if the connection already exists).
50
- * @param {*} a1
51
- * @param {*} a2
74
+ * Attempts to create a connection between {@linkcode Agent}s `a` and `b`.
75
+ * @returns Returns `true` if the connection was successfully created (i.e. if `a` and `b` were previously not connected and now are).
76
+ *
77
+ * ```js
78
+ * const a = new Agent();
79
+ * const b = new Agent();
80
+ * network.addAgent(a);
81
+ * network.addAgent(b);
82
+ *
83
+ * network.connect(a, b); // returns true
84
+ *
85
+ * network.connect(a, b); // returns false since they are now already connected
86
+ *
87
+ * const c = new Agent();
88
+ * network.connect(a, c); // returns false since `c` is not in the `Network`
89
+ * ```
90
+ *
91
+ * Returns `false` otherwise, for example if `a` and `b` are the same `Agent`, or if either is not in the `Network`.
52
92
  * @since 0.1.3
53
93
  */
54
- connect(a1: Agent, a2: Agent): boolean;
55
- /**
56
- * Returns `true` if the given agents are connected in the network.
57
- * @param {Agent} a1
58
- * @param {Agent} a2
94
+ connect(a: Agent, b: Agent): boolean;
95
+ /**
96
+ * @returns Returns `true` if {@linkcode Agent}s `a` and `b` are connected, `false` if they are not.
97
+ *
98
+ * ```js
99
+ * network.connect(a, b);
100
+ * network.areConnected(a, b); // returns true since they have been connected
101
+ *
102
+ * network.disconnect(a, b);
103
+ * network.areConnected(a, b); // returns false since they have been disconnected
104
+ * ```
105
+ *
59
106
  * @since 0.1.3
60
107
  */
61
- areConnected(a1: Agent, a2: Agent): boolean;
62
- /**
63
- * Like with connect, returns `true` if the edge was successfully
64
- * removed, false if otherwise (if edge did not exist in the first place).
65
- * @param {agent} a1
66
- * @param {agent} a2
108
+ areConnected(a: Agent, b: Agent): boolean;
109
+ /**
110
+ * Attempts to sever the connection between {@linkcode Agent}s `a` and `b`.
111
+ * @returns Returns `true` if the `Agent`s were successfully disconnected, `false` otherwise.
112
+ *
113
+ * ```js
114
+ * const a = new Agent();
115
+ * const b = new Agent();
116
+ * network.addAgent(a);
117
+ * network.addAgent(b);
118
+ *
119
+ * network.connect(a, b);
120
+ * network.disconnect(a, b); // returns true since they were connected and are no longer
121
+ *
122
+ * network.disconnect(a, b); // returns false since they were already not connected
123
+ * ```
124
+ *
67
125
  * @since 0.1.3
68
126
  */
69
127
  disconnect(a1: Agent, a2: Agent): boolean;
70
128
  /**
71
- * Number of agents in the network.
129
+ * @returns Returns the number of {@linkcode Agent}s in the `Network`.
130
+ *
131
+ * ```js
132
+ * const a = new Agent();
133
+ * const b = new Agent();
134
+ * const c = new Agent();
135
+ * [a, b, c].forEach(agt => network.addAgent(agt));
136
+ *
137
+ * network.size(); // returns 3
138
+ * ```
139
+ *
72
140
  * @since 0.1.3
73
141
  */
74
142
  size(): number;
75
143
  /**
76
- * Given a callback function, loop over all the agents in the network
77
- * and invoke the callback, passing the agent + its index as parameters.
78
- * @param {Function} cb
144
+ * Loop over all the {@linkcode Agent}s in the `Network` (in the order they were added),
145
+ * and invoke the `callback` function with the `Agent` and an index passed as parameters.
79
146
  * @since 0.1.3
80
147
  */
81
- forEach(cb: AgentCallback): void;
148
+ forEach(callback: (agent: Agent, index: number) => any): void;
82
149
  /**
83
- * Same as forEach, but in random order.
84
- * @param {Function} cb
150
+ * The same method as {@linkcode forEach}, but executes in random order.
85
151
  * @since 0.1.3
86
152
  */
87
- forEachRand(cb: AgentCallback): void;
153
+ forEachRand(callback: (agent: Agent, index: number) => any): void;
88
154
  /**
89
- * Returns true if the agent is in the network, false if it is not.
90
- * @param {Agent} agent
155
+ * Returns `true` if the given {@linkcode Agent} is in the `Network`, `false` if it is not.
91
156
  * @since 0.1.3
92
157
  */
93
158
  isInNetwork(agent: Agent): boolean;
94
159
  /**
95
- * Get the agent at index i.
96
- * @param {number} i
160
+ * Returns the {@linkcode Agent} at index `i`, where `i = 0` is the first `Agent`
161
+ * added to the `Network`, `i = 1` the second, etc.
162
+ *
163
+ * Negative indices are allowed, so `network.get(-1)` returns the `Agent` that was most recently
164
+ * added to the `Network`, `-2` the second-most recent, etc.
97
165
  * @since 0.1.3
98
166
  */
99
167
  get(i: number): Agent;
100
168
  /**
101
- * Get the index of a given agent.
102
- * @param {Agent} agent
169
+ * Returns the index of the given {@linkcode Agent} in the {@linkcode agents} array.
103
170
  * @since 0.1.3
104
171
  */
105
172
  indexOf(agent: Agent): number;
106
173
  /**
107
- * Return the agents that are neighbors of a given agent
108
- * (in a JS array). If the agent is not in the network, returns `null`.
109
- * @param {Agent} agent
174
+ * Returns an array of {@linkcode Agent}s that are connected to the given `Agent` (in no guaranteed order).
175
+ *
176
+ * Returns `null` if the given `Agent` is not in the `Network`.
177
+ *
178
+ * ```js
179
+ * // suppose a, b, and c are connected
180
+ * network.neighbors(a); // returns [b, c] (or [c, b])
181
+ *
182
+ * network.disconnect(a, c);
183
+ * network.neighbors(a); // returns [b]
184
+ * network.neighbors(c); // returns [b]
185
+ * ```
186
+ *
110
187
  * @since 0.1.3
111
188
  */
112
189
  neighbors(agent: Agent): Agent[] | null;
113
190
  /**
114
- * Connect every agent in the network to every other agent.
191
+ * Draw a connection between every pair of {@linkcode Agent}s in the `Network`.
115
192
  * @since 0.1.3
116
193
  */
117
194
  complete(): void;
118
195
  /**
119
196
  * Internal helper function to reset the adjacencyMatrix.
120
197
  * This gets called when agents are added to or removed from the network.
198
+ * @hidden
121
199
  */
122
200
  _resetAdjacencyMatrix(): void;
123
201
  /**
124
- * Returns `true` if a, b, and c are a 'triplet' of agents --
125
- * if (at least) one of the three is connected to the other two.
126
- * @param {Agent} a
127
- * @param {Agent} b
128
- * @param {Agent} c
202
+ * Returns `true` if `Agent`s a, b, and c form a 'triplet' &mdash; if (at least) one of the three is connected to the other two. Returns `false` otherwise.
129
203
  * @since 0.5.17
130
204
  */
131
205
  isTriplet(a: Agent, b: Agent, c: Agent): boolean;
132
206
  /**
133
- * Returns `true` if a, b, and c are a 'closed triplet' of agents --
134
- * each connected to the other two.
135
- * @param {Agent} a
136
- * @param {Agent} b
137
- * @param {Agent} c
207
+ * Returns `true` if `Agent`s a, b, and c form a 'closed triplet' &mdash; if each of the three are connected to the other two. Returns `false` otherwise.
138
208
  * @since 0.5.17
139
209
  */
140
210
  isClosedTriplet(a: Agent, b: Agent, c: Agent): boolean;
211
+ /** @hidden */
141
212
  _globalClusteringCoefficient(): number;
142
213
  /**
143
- * If an agent is passed as the single parameter, returns the local
144
- * clustering coefficient for that agent (a measure of how connected that
145
- * agent's neighbors are to each other).
146
- * If no parameter is passed, returns the global clustering coefficient
147
- * of the network (an aggregate measure of how connected are the agents).
148
- * @param {Agent} [agent]
214
+ * The {@link https://en.wikipedia.org/wiki/Clustering_coefficient | clustering coefficient} is a measure of how
215
+ * closely connected either an individual {@linkcode Agent}'s connections are or the `Network` as a whole is.
216
+ *
217
+ * If an `Agent` is passed as the single parameter, returns the {@link https://en.wikipedia.org/wiki/Clustering_coefficient#Local_clustering_coefficient | local
218
+ * clustering coefficient} for that `Agent`.
219
+ *
220
+ * If no parameter is passed, returns the {@link https://en.wikipedia.org/wiki/Clustering_coefficient#Global_clustering_coefficient | global clustering coefficient}
221
+ * of the `Network` (an aggregate measure of how connected the `Agent`s are).
222
+ *
149
223
  * @since 0.5.17
150
224
  */
151
225
  clusteringCoefficient(agent?: Agent): number;
152
226
  /**
153
- * Returns the average clustering coefficient for the network (the average
154
- * of the local clustering coefficient across all agents). Note that
155
- * this is a different measurement from the _global_ clustering coefficient.
227
+ * Returns the {@link https://en.wikipedia.org/wiki/Clustering_coefficient#Network_average_clustering_coefficient | average clustering coefficient} for the `Network` (the average
228
+ * of the {@link Network.clusteringCoefficient | local clustering coefficient} across all `Agent`s).
229
+ *
230
+ * Note that this is a different measurement from the _global_ clustering coefficient
231
+ * (i.e. calling {@linkcode clusteringCoefficient} without any parameters).
156
232
  * @since 0.5.17
157
233
  */
158
234
  averageClusteringCoefficient(): number;
@@ -1,4 +1,5 @@
1
1
  /**
2
+ * @hidden
2
3
  * @since 0.3.11
3
4
  */
4
5
  declare class NumArray {
@@ -4,22 +4,80 @@ declare type StepToken = number | string | Step;
4
4
  interface Step extends Array<StepToken> {
5
5
  }
6
6
  /**
7
+ * The `Rule` class is an experimental interface for adding behavior to {@linkcode Agent}s. A `Rule` object may be used in place of a `tick` function to be added as `Agent` behavior using `agent.set('tick', tickRule)`. As a trivial example, consider the following `Rule`, which increments the `Agent`'s `x` value with every time step:
8
+ *
9
+ * ```js
10
+ * const rule = new Rule(environment, [
11
+ * "set", "x", [
12
+ * "add", 1, [
13
+ * "get", "x"
14
+ * ]
15
+ * ]
16
+ * ]);
17
+ * agent.set("tick", rule);
18
+ * ```
19
+ *
20
+ * Reading from the outer arrays inward, the steps of this `Rule` instructs the `Agent` to:
21
+ * - `set` the `Agent`'s `"x"` value to...
22
+ * - The result of `add`ing `1` and...
23
+ * - The `Agent`'s current `"x"` value
24
+ *
25
+ * Generally, `Rule` steps are a deeply nested array, where the first value of any given array is always an instruction or operator (e.g. `"set"`, `"add"`, `"filter"`). See the {@linkcode constructor} function for more information about steps.
7
26
  * @since 0.3.0
8
27
  */
9
28
  declare class Rule {
29
+ /**
30
+ * Points to the {@linkcode Environment} that is passed in the `Rule`'s constructor function (should be the same `Environment` of the {@linkcode Agent} invoking this `Rule`)
31
+ */
10
32
  environment: Environment;
33
+ /** @hidden */
11
34
  steps: Step[];
35
+ /** @hidden */
12
36
  locals: {
13
37
  [key: string]: any;
14
38
  };
39
+ /**
40
+ * A single step may be as simple as `["get", "x"]`. This returns the `Agent`'s `"x"` value to the outer step that contains it. So, for example, the step `["add", 1, ["get", "x"]]`, working from the inside out, retrieves the `"x"` value and then adds `1` to it. More complex steps function similarly, always traversing to the deepest nested step, evaluating it, and 'unwrapping' until all steps have been executed.
41
+ *
42
+ * A step's first element should be a string that is one of the allowed operators, followed by a certain number of arguments.
43
+ *
44
+ * |Operator|Arguments|Notes|
45
+ * |---|---|---|
46
+ * |`"add"`|`2`|Pass 2 numbers, or two steps that evaluate to numbers|
47
+ * |`"subtract"`|`2`|""|
48
+ * |`"multiply"`|`2`|""|
49
+ * |`"divide"`|`2`|""|
50
+ * |`"mod"`|`2`|""|
51
+ * |`"power"`|`2`|""|
52
+ * |`"get"`|`1`|Pass the key of `Agent` data to retrieve|
53
+ * |`"set"`|`2`|Pass the key and value to set|
54
+ * |`"enqueue"`|`2`|Pass the key and value to enqueue|
55
+ * |`"local"`|`2`|Pass the key and value to set as local variables|
56
+ * |`"if"`|`3`|Pass the conditional (usually a step that evaluates to a boolean), the step to run when `true`, and the step to run when `false|
57
+ * |`"and"`|`2`|Pass the two steps to logically evaluate|
58
+ * |`"or"`|`2`|""|
59
+ * |`"gt"`|`2`|""|
60
+ * |`"gte"`|`2`|""|
61
+ * |`"lt"`|`2`|""|
62
+ * |`"lte"`|`2`|""|
63
+ * |`"eq"`|`2`|""|
64
+ * |`"map"`|`2`|Pass an array (or step that evaluates to an array) and a lambda to invoke for each element|
65
+ * |`"filter"`|`2`|""|
66
+ * |`"key"`|`2`|Pass an object (or step that evaluates to an object) and the key to retrieve from that object|
67
+ * |`"agent"`|`0`|No arguments; returns the `Agent`|
68
+ * |`"environment"`|`0`|No arguments, returns the `Environment`|
69
+ * |`"vector"`|`any`|Creates an n-dimensional {@linkcode Vector} from the supplied arguments|
70
+ */
15
71
  constructor(environment: Environment, steps: Step[]);
16
72
  /**
17
73
  * interpret single array step
18
74
  * @since 0.3.0
75
+ * @hidden
19
76
  */
20
77
  evaluate: (agent: Agent, step: any[]) => any;
21
78
  /**
22
79
  * @since 0.3.0
80
+ * @hidden
23
81
  */
24
82
  call(agent: Agent): any;
25
83
  }
@@ -11,53 +11,151 @@ interface Pixel {
11
11
  }
12
12
  declare type TerrainRule = (x: number, y: number) => Pixel | number | void;
13
13
  /**
14
+ * Each static member of the `Colors` class (e.g. `Colors.GREEN`, `Colors.RED`) is a pixel-like object with `r`, `g`, `b`, and `a` values that range from `0` to `255`.
14
15
  * @since 0.4.0
15
16
  */
16
- export declare const Colors: {
17
- [name: string]: Pixel;
18
- };
17
+ export declare class Colors {
18
+ /** <div style="width: 100%; height: 20px; background-color: rgb(0, 0, 0);"></div> */
19
+ static BLACK: Pixel;
20
+ /** <div style="width: 100%; height: 20px; background-color: rgb(255, 255, 255); border: 1px solid #eee;"></div> */
21
+ static WHITE: Pixel;
22
+ /** <div style="width: 100%; height: 20px; background-color: rgb(255, 0, 0);"></div> */
23
+ static RED: Pixel;
24
+ /** <div style="width: 100%; height: 20px; background-color: rgb(127, 0, 0);"></div> */
25
+ static MAROON: Pixel;
26
+ /** <div style="width: 100%; height: 20px; background-color: rgb(255,255, 0);"></div> */
27
+ static YELLOW: Pixel;
28
+ /** <div style="width: 100%; height: 20px; background-color: rgb(0, 0, 255);"></div> */
29
+ static BLUE: Pixel;
30
+ /** <div style="width: 100%; height: 20px; background-color: rgb(0, 127, 0);"></div> */
31
+ static GREEN: Pixel;
32
+ /** <div style="width: 100%; height: 20px; background-color: rgb(0, 255, 0);"></div> */
33
+ static LIME: Pixel;
34
+ /** <div style="width: 100%; height: 20px; background-color: rgb(0, 255, 255);"></div> */
35
+ static AQUA: Pixel;
36
+ /** <div style="width: 100%; height: 20px; background-color: rgb(255, 165, 0);"></div> */
37
+ static ORANGE: Pixel;
38
+ /** <div style="width: 100%; height: 20px; background-color: rgb(255, 0, 255);"></div> */
39
+ static FUCHSIA: Pixel;
40
+ /** <div style="width: 100%; height: 20px; background-color: rgb(127, 0, 127);"></div> */
41
+ static PURPLE: Pixel;
42
+ }
19
43
  /**
44
+ * The `Terrain` class lets {@linkcode Environment}s function as lattices upon which {@link https://en.wikipedia.org/wiki/Cellular_automaton | cellular automata} can grow. With a `Terrain`, {@linkcode Agent}s may not be necessary, since all the cells of a `Terrain` can follow update rules (similar to but simplified from `Agent`s).
45
+ *
46
+ * ### Usage
47
+ *
48
+ * ```js
49
+ * const environment = new Environment();
50
+ * const terrain = new Terrain(30, 30); // create a 30 x 30 Terrain
51
+ * environment.use(terrain); // tell the Environment to 'use' this Terrain as a helper
52
+ * ```
53
+ *
20
54
  * @since 0.4.0
21
55
  */
22
56
  declare class Terrain implements EnvironmentHelper {
57
+ /** @hidden */
23
58
  data: Uint8ClampedArray;
59
+ /** @hidden */
24
60
  nextData: Uint8ClampedArray;
61
+ /** @hidden */
25
62
  opts: TerrainOptions;
63
+ /**
64
+ * The number of cells across in a `Terrain`. If you use a `scale` larger than `1`, the `Terrain` will be rendered at `width * scale` pixels wide on the screen.
65
+ */
26
66
  width: number;
67
+ /**
68
+ * The number of cells from top to bottom in a `Terrain`. If you use a `scale` larger than `1`, the `Terrain` will be rendered at `height * scale` pixels high on the screen.
69
+ */
27
70
  height: number;
71
+ /** @hidden */
28
72
  rule: TerrainRule;
29
73
  /**
74
+ * Instantiate a new `Terrain` by passing its `width` and `height` as the first two parameters, and an optional configuration object as the third.
75
+ *
76
+ * ### Options
30
77
  *
31
- * @param {number} width - The width of the terrain
32
- * @param {number} height - The height of the terrain
33
- * @param options
78
+ * - `async` (*boolean* = `false`) &mdash; Whether to run the `Terrain` in synchronous (`true`) or asynchronous (`mode`). Defaults to synchronous. Depending on the timing mode, {@link addRule | Terrain update rules} should be written differently.
79
+ * - `grayscale` (*boolean* = `false`)
80
+ * - In **color mode** (the default), each cell of a `Terrain` is represented by a {@link Colors | pixel-like object} (an object with numeric keys `r`, `g`, `b`, and `a` ranging from 0-255).
81
+ * - In **grayscale mode**, each cell of a `Terrain` is represented by a single number ranging from 0 (black) to 255 (white).
82
+ * - `scale` (*number* = `1`) &mdash; The size, in pixels, of each cell's width and height when the `Terrain` is rendered using a {@linkcode CanvasRenderer}. In the below screenshots, the `Terrain` on the left uses a scale of `1` while the one on the right uses a scale of `5`:
83
+ *
84
+ * <img alt="Terrain with scale = 1" style="width: 49%;" src="https://cms.flocc.network/wp-content/uploads/2020/04/terrain-1.png">
85
+ * <img alt="Terrain with scale = 5" style="width: 49%;" src="https://cms.flocc.network/wp-content/uploads/2020/04/terrain-5.png">
86
+ *
87
+ * In addition to the above setup, you will need to {@link init | initialize} the `Terrain` and {@link addRule | add an update rule}.
34
88
  */
35
89
  constructor(width: number, height: number, options?: TerrainOptions);
36
90
  /**
37
- * Initialize (or overwrite) the terrain data by passing a function with parameters (x, y)
38
- * and returning a pixel value.
39
- * @param {Function} rule - The rule to follow to instantiate pixel values
91
+ * Initialize (or overwrite) all cell values. The rule you pass has the same signature
92
+ * as {@linkcode addRule}, but should always return a value (either a number or {@linkcode Colors | pixel-like object}).
93
+ *
94
+ * ```js
95
+ * // initializes cells randomly to either blue or red
96
+ * terrain.init((x, y) => {
97
+ * return utils.uniform() > 0.5 ? Colors.BLUE : Colors.RED;
98
+ * });
99
+ * ```
100
+ *
40
101
  * @since 0.4.0
41
102
  */
42
103
  init(rule: TerrainRule): void;
43
104
  /**
44
- * Like with agents, this adds an update rule for the terrain. The function
45
- * passed as the rule should be called with the parameters (x, y), and should return
46
- * a pixel-like object { r: number, g: number, b: number, a: number } or number.
47
- * @param {Function} rule - The update rule to be called on environment.tick()
105
+ * Similar to adding behavior to {@linkcode Agent}s, this adds an update rule for the `Terrain`.
106
+ * The function passed as the rule should be called with the parameters (`x`, `y`). In synchronous mode,
107
+ * it should return a value that is the color of that cell on the next time step.
108
+ *
109
+ * ```js
110
+ * // turns a cell red if the x-value is greater than 200,
111
+ * // blue if the x-value is less than 100,
112
+ * // and leaves it unchanged in between
113
+ * terrain.addRule((x, y) => {
114
+ * if (x > 200) {
115
+ * return Colors.RED;
116
+ * } else if (x < 100) {
117
+ * return Colors.BLUE;
118
+ * }
119
+ * });
120
+ * ```
121
+ *
122
+ * For grayscale mode, functions passed to `addRule` should return a number instead of a {@linkcode Colors | pixel-like object}.
123
+ *
124
+ * In asynchronous mode, functions should use the {@linkcode set} method to update either this cell
125
+ * or a different cell.
126
+ *
127
+ * ```js
128
+ * // swaps the colors of this cell and the one five cells to the right
129
+ * terrain.addRule((x, y) => {
130
+ * const here = terrain.sample(x, y);
131
+ * const there = terrain.sample(x + 5, y);
132
+ * terrain.set(x, y, there);
133
+ * terrain.set(x + 5, y, here);
134
+ * });
135
+ * ```
136
+ *
48
137
  * @since 0.4.0
49
138
  */
50
139
  addRule(rule: TerrainRule): void;
51
140
  /**
52
141
  * Given a local path or remote URL to an image, load that image and set
53
- * terrain pixel data accordingly. This will scale the image to match the
54
- * dimensionss of the terrain.
142
+ * `Terrain` data accordingly. This will scale the image to match the
143
+ * dimensions of the terrain.
144
+ *
55
145
  * A 2nd callback parameter fires once the image has been successfully loaded.
56
- * @param {string} path - The path or URL to the image
57
- * @param {Function} cb - The function to call once the image loads
146
+ *
147
+ * ```js
148
+ * const terrain = new Terrain(400, 400);
149
+ * terrain.load("/path/to/local/image.jpg", function() {
150
+ * console.log("Image loaded successfully!");
151
+ * });
152
+ * ```
153
+ *
154
+ * @param {string} path - The path to or URL of the image
155
+ * @param {Function} cb - The function to call once the image loads (takes no parameters)
58
156
  * @since 0.4.0
59
157
  */
60
- load(path: string, cb?: Function): void;
158
+ load(path: string, callback?: Function): void;
61
159
  /**
62
160
  * Get the pixel value at the coordinate (x, y). If in grayscale mode, this
63
161
  * returns a single number. Otherwise, it returns a pixel-like object { r: number,
@@ -68,19 +166,25 @@ declare class Terrain implements EnvironmentHelper {
68
166
  */
69
167
  sample(x: number, y: number): Pixel | number;
70
168
  /**
71
- * Get the neighbors of a coordinate within a certain radius.
72
- * Depending on the fourth parameter, retrieves either the von Neumann neighborhood
73
- * (https://en.wikipedia.org/wiki/Von_Neumann_neighborhood) or the Moore neighborhood
74
- * (https://en.wikipedia.org/wiki/Moore_neighborhood).
169
+ * Get the values of the neighbors of a cell within a certain radius.
170
+ * Depending on the fourth parameter, retrieves either the {@link https://en.wikipedia.org/wiki/Von_Neumann_neighborhood | von Neumann neighborhood}
171
+ * or the {@link https://en.wikipedia.org/wiki/Moore_neighborhood | Moore neighborhood}.
75
172
  *
76
- * @param {number} x - The x coordinate
77
- * @param {number} y - The y coordinate
78
- * @param {number} radius - how far to look for neighbors
79
- * @param {boolean} moore - whether to use the Moore neighborhood or von Neumann (defaults to von Neumann)
80
- * @returns {Pixel[] | number[]} - An array of numbers (grayscale only) or pixel-like objects
173
+ * ```js
174
+ * // in grayscale mode:
175
+ * terrain.neighbors(5, 5, 1); // returns [127, 100, 255, 255] (4 values)
176
+ *
177
+ * // in color mode:
178
+ * terrain.neighbors(5, 5, 1, true);
179
+ * // returns [{ r: 255, g: 0, b: 0, a: 255 }, { r: 127, ... }, ...] (8 values)
180
+ * ```
181
+ *
182
+ * @param moore - Defaults to using the von Neumann neighborhood.
183
+ * @returns Either an array of numbers (grayscale mode) or {@link Colors | pixel-like objects} (color mode).
81
184
  * @since 0.4.0
82
185
  */
83
186
  neighbors(x: number, y: number, radius?: number, moore?: boolean): (Pixel | number)[];
187
+ /** @hidden */
84
188
  _setAbstract(data: Uint8ClampedArray, x: number, y: number, r: number | Pixel, g?: number, b?: number, a?: number): void;
85
189
  /**
86
190
  * Set new pixel data at a coordinate on the terrain. Only call this directly if
@@ -95,8 +199,11 @@ declare class Terrain implements EnvironmentHelper {
95
199
  * @since 0.4.0
96
200
  */
97
201
  set(x: number, y: number, r: number | Pixel, g?: number, b?: number, a?: number): void;
202
+ /** @hidden */
98
203
  _setNext(x: number, y: number, r: number | Pixel, g?: number, b?: number, a?: number): void;
204
+ /** @hidden */
99
205
  _execute(x: number, y: number): void;
206
+ /** @hidden */
100
207
  _loop({ randomizeOrder }: {
101
208
  randomizeOrder?: boolean;
102
209
  }): void;