-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomb.lua
246 lines (226 loc) · 9.7 KB
/
bomb.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
local timer = require "timer"
local t = {}
local rotationSpeed = 1
local scheduleCheck = true
function t.newBomb(_serialNumber, _time)
return {
serialNumber=_serialNumber,
timer=timer.newTimer(_time),
sources={},
modules={}
}
end
function t.addModule(_bomb, _module)
table.insert(_bomb.modules, _module)
end
function t.newModule(_type)
return {type=_type}
end
function t.newModuleFromPrefab(_prefab)
if (_prefab.type == "none") then
return {type="none"}
else
return {type=_prefab.type, rotation=_prefab.rotation, rotateTo=_prefab.rotation}
end
return t.newModule("test")
end
function t.doAction(_bomb, _selectedModule)
if _bomb.modules[_selectedModule].type ~= "none" then
local toRotate = {
_selectedModule
}
if (_bomb.joinedModules[_selectedModule]) then
for i, val in ipairs(_bomb.joinedModules[_selectedModule]) do
table.insert(toRotate, val)
end
end
for i, val in ipairs(toRotate) do
_bomb.modules[val].rotateTo = _bomb.modules[val].rotateTo + 1
if (_bomb.modules[val].rotateTo >= 4) then
_bomb.modules[val].rotateTo = _bomb.modules[val].rotateTo - 4
end
end
scheduleCheck = true
end
end
local function checkRules(_rules, _additionalRules, _timerOut)
scheduleCheck = false
local energized = {
{type="battery"}
}
if (_timerOut) then
table.insert(energized, {type="timer"})
end
while (true) do
local toEnergize = {}
for i, node in ipairs(energized) do
for i, rule in ipairs(_rules) do
if (node.type == rule[1].type) then
local check = false
if (node.type ~= "module") then
local add = true
for i, val in ipairs(energized) do
if (rule[2] == val) then
add = false
break
end
end
if (add) then
for i, val in ipairs(toEnergize) do
if (rule[2] == val) then
add = false
break
end
end
end
if (add) then
table.insert(toEnergize, rule[2])
end
check = true
else
if (node.number == rule[1].number and node.direction == rule[1].direction) then
local add = true
for i, val in ipairs(energized) do
if (rule[2] == val) then
add = false
break
end
end
if (add) then
for i, val in ipairs(toEnergize) do
if (rule[2] == val) then
add = false
break
end
end
end
if (add) then
table.insert(toEnergize, rule[2])
end
check = true
end
end
if (check and rule[2].type == "bomb") then
return true
end
end
end
for i, rule in ipairs(_additionalRules) do
if (node.type == rule[1].type) then
local check = false
if (node.number == rule[1].number and node.direction == rule[1].direction) then
local add = true
for i, val in ipairs(energized) do
if (rule[2] == val) then
add = false
break
end
end
if (add) then
for i, val in ipairs(toEnergize) do
if (rule[2] == val) then
add = false
break
end
end
end
if (add) then
table.insert(toEnergize, rule[2])
end
check = true
end
if (check and rule[2].type == "bomb") then
return true
end
end
end
end
if (table.getn(toEnergize) == 0) then
break
end
for i, node in ipairs(toEnergize) do
table.insert(energized, node)
end
end
return false
end
function t.updateBomb(_bomb, _dt, _timeSpeed)
timer.updateTimer(_bomb.timer, _timeSpeed * _dt)
for i, module in ipairs(_bomb.modules) do
if (module.type ~= "none") then
if (math.abs(module.rotation - module.rotateTo) > 0.1) then
module.rotation = module.rotation + rotationSpeed * _dt
if (module.rotation >= 4) then
module.rotation = module.rotation - 4
end
else
module.rotation = module.rotateTo
end
end
end
if (_bomb.timer.time <= 0) then
scheduleCheck = true
end
local exploded = false
if (scheduleCheck) then
local rules = _bomb.rules
local additionalRules = {}
for i, module in ipairs(_bomb.modules) do
local topBottomRule = {{type="module", number=i, direction="top"}, {type="module", number=i, direction="bottom"}}
local bottomTopRule = {{type="module", number=i, direction="bottom"}, {type="module", number=i, direction="top"}}
local leftRightRule = {{type="module", number=i, direction="left"}, {type="module", number=i, direction="right"}}
local rightLeftRule = {{type="module", number=i, direction="right"}, {type="module", number=i, direction="left"}}
local leftTopRule = {{type="module", number=i, direction="left"}, {type="module", number=i, direction="top"}}
local topLeftRule = {{type="module", number=i, direction="top"}, {type="module", number=i, direction="left"}}
local topRightRule = {{type="module", number=i, direction="top"}, {type="module", number=i, direction="right"}}
local rightTopRule = {{type="module", number=i, direction="right"}, {type="module", number=i, direction="top"}}
local rightBottomRule = {{type="module", number=i, direction="right"}, {type="module", number=i, direction="bottom"}}
local bottomRightRule = {{type="module", number=i, direction="bottom"}, {type="module", number=i, direction="right"}}
local bottomLeftRule = {{type="module", number=i, direction="bottom"}, {type="module", number=i, direction="left"}}
local leftBottomRule = {{type="module", number=i, direction="left"}, {type="module", number=i, direction="bottom"}}
if (module.type == "straight") then
if (module.rotateTo == 1 or module.rotateTo == 3) then
table.insert(additionalRules, topBottomRule)
table.insert(additionalRules, bottomTopRule)
elseif (module.rotateTo == 0 or module.rotateTo == 2) then
table.insert(additionalRules, leftRightRule)
table.insert(additionalRules, rightLeftRule)
end
elseif (module.type == "left") then
if (module.rotateTo == 0) then
table.insert(additionalRules, leftTopRule)
table.insert(additionalRules, topLeftRule)
elseif (module.rotateTo == 1) then
table.insert(additionalRules, topRightRule)
table.insert(additionalRules, rightTopRule)
elseif (module.rotateTo == 2) then
table.insert(additionalRules, rightBottomRule)
table.insert(additionalRules, bottomRightRule)
elseif (module.rotateTo == 3) then
table.insert(additionalRules, bottomLeftRule)
table.insert(additionalRules, leftBottomRule)
end
elseif (module.type == "doubleLeft") then
if (module.rotateTo == 0 or module.rotateTo == 2) then
table.insert(additionalRules, leftTopRule)
table.insert(additionalRules, topLeftRule)
table.insert(additionalRules, rightBottomRule)
table.insert(additionalRules, bottomRightRule)
elseif (module.rotateTo == 1 or module.rotateTo == 3) then
table.insert(additionalRules, leftBottomRule)
table.insert(additionalRules, bottomLeftRule)
table.insert(additionalRules, rightTopRule)
table.insert(additionalRules, topRightRule)
end
end
end
exploded = checkRules(rules, additionalRules, _bomb.timer.time <= 0)
end
if (exploded) then
return "explode"
elseif (_bomb.timer.time <= 0) then
return "win"
end
return "none"
end
return t