-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.lua
138 lines (126 loc) · 3.78 KB
/
types.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function updateCell(p, getNeighbor, setNeighbor)
if (p.type == 1) then
return updateDust(p, getNeighbor, setNeighbor)
elseif (p.type == 2) then
return updateWater(p, getNeighbor, setNeighbor)
elseif (p.type == 3) then
return updateGas(p, getNeighbor, setNeighbor)
elseif (p.type == 4) then
return updateWall(p, getNeighbor, setNeighbor)
elseif (p.type == 5) then
return updateClone(p, getNeighbor, setNeighbor)
elseif (p.type == 6) then
return updateFire(p, getNeighbor, setNeighbor)
else
print("unknown type")
end
end
function updateWall(p, getNeighbor, setNeighbor)
setNeighbor({x = 0, y = 0}, p)
end
function updateGas(p, getNeighbor, setNeighbor)
local d = {x = math.random(-1, 1), y = math.random(-1, 1)}
if (getNeighbor(d) == 0) then
setNeighbor({x = 0, y = 0}, 0)
setNeighbor(d, p)
end
end
function updateWater(p, getNeighbor, setNeighbor)
local d = {x = math.random(-1, 1), y = 1}
p.rA = math.random() * 0.4 + 0.7
if (getNeighbor(d) == 0) then
setNeighbor({x = 0, y = 0}, 0)
setNeighbor(d, p)
elseif (getNeighbor({x = d.x, y = 0}) == 0) then
setNeighbor({x = 0, y = 0}, 0)
setNeighbor({x = d.x, y = 0}, p)
elseif (getNeighbor({x = -d.x, y = 0}) == 0) then
setNeighbor({x = 0, y = 0}, 0)
setNeighbor({x = -d.x, y = 0}, p)
else
setNeighbor({x = 0, y = 0}, p)
end
end
function updateDust(p, getNeighbor, setNeighbor)
local d = {x = math.random(-1, 1), y = 1}
if (getNeighbor(d) == 0) then
setNeighbor({x = 0, y = 0}, 0)
setNeighbor(d, p)
end
end
function updateClone(p, getNeighbor, setNeighbor)
local d = {x = 0, y = 0}
for x = -1, 1 do
for y = -1, 1 do
local nbr = getNeighbor({x = x, y = y})
if (p.rB == 0) then
if (nbr ~= 0 and nbr.type ~= 5 and p.rB == 0) then
p.rB = nbr.type
p.rA = 1.0
return
end
else
if (nbr == 0) then
setNeighbor({x = x, y = y}, {type = p.rB, rA = 0.5, rB = 0})
break
end
end
end
end
setNeighbor({x = 0, y = 0}, p)
end
function updateFire(p, getNeighbor, setNeighbor)
local d = {x = math.random(-1, 1), y = math.random(-1, 0)}
for x = -1, 1 do
for y = -1, 1 do
local nbr = getNeighbor({x = x, y = y})
if (nbr == 0 or nbr == nil) then
-- continue;
elseif nbr.type == 3 then
setNeighbor({x = x, y = y}, {type = 6, rA = 1.0, rB = 0})
return
elseif nbr.type == 2 then
p.rA = 0
end
end
end
p.rA = p.rA - 0.01
if (getNeighbor(d) == 0) then
setNeighbor({x = 0, y = 0}, 0)
if (p.rA > 0) then
setNeighbor(d, p)
end
end
end
function updateLife(p, getNeighbor, setNeighbor)
local d = {x = 0, y = 0}
local isAlive = p.rA > 0.5
local n = -1
for x = -1, 1 do
for y = -1, 1 do
local nbr = getNeighbor({x = x, y = y})
if (nbr ~= 0 and nbr.type == 5 and nbr.rA > 0.5) then
n = n + 1
end
end
end
if (isAlive == false) then
if (n == 3 or math.random(100) < 1) then
isAlive = true
end
else
if (n < 2) then
isAlive = false
elseif (n < 4) then
isAlive = true
else
isAlive = false
end
end
p.rA = isAlive and 0.9 or 0.1
setNeighbor({x = 0, y = 0}, p)
-- if (getNeighbor(d) == 0) then
-- setNeighbor({x = 0, y = 0}, 0)
-- setNeighbor(d, p)
-- end
end