-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.lua
60 lines (52 loc) · 1.46 KB
/
template.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- CA_3D template
-- For examples look at examples/
-- Initialize two tables for processing and displaying the CA grid
local maps = {
process = {},
current = {}
}
local size = 15 -- Cube 15x15x15
local cellUpdateRate = 1/3 -- 3 times per second
function setup()
-- Fill tables with data
for x=0, size do
maps.process[x] = {}
maps.current[x] = {}
for y=0, size do
maps.process[x][y] = {}
maps.current[x][y] = {}
for z=0, size do
-- Contents of process generally do not matter, since
-- they should be overwritten in each update iteration.
maps.process[x][y][z] = {
state = 1,
r = 255,
g = 255,
b = 255
}
maps.current[x][y][z] = {
state = 1,
r = 255,
g = 255,
b = 255
}
end
end
end
-- setup() returns in order:
-- 1) The 3D grid edge length
-- 2) The current 3D grid which is to be displayed
-- 3) The rate at which update() will be called
return size, maps.current, cellUpdateRate
end
function update()
-- All of your cellular automata code should ONLY make
-- changes to maps.process by reading maps.current
-- Switch pointers (fast copy table and keep 2 tables on memory)
-- I do this to keep maps.current as the current cell grid
-- and have a table on hand (its contents do not matter since they
-- will get replaced on the next iteration)
maps.current, maps.process = maps.process, maps.current
-- update() only returns the CA grid that is to be displayed
return maps.current
end