-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.lua
230 lines (190 loc) · 5.67 KB
/
util.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
-- vi: noexpandtab
local has_digilines = minetest.get_modpath("digilines")
spacecannon.update_formspec = function(meta, is_th)
local formspec = ""
if not is_th then
formspec = formspec ..
"formspec_version[4]" ..
"size[10.5,9;]"
-- Ammo inventory
formspec = formspec ..
"list[current_name;src;0.375,0.5;1,1;]" ..
"list[current_player;main;0.375,4;8,4;]" ..
"listring[]" ..
"item_image[0.375,0.5;1,1;spacecannon:railgun_slug]" ..
"label[1.75,1;Ammunition]"
-- Manual "fire" button
formspec = formspec ..
"button_exit[5.125,0.5;5,1;fire;Fire]"
-- Digiline channel
if has_digilines then
local channel = meta:get_string("channel") or ""
formspec = formspec ..
"field[0.375,2.375;4,1;digiline_channel;Digiline Channel;" .. channel .. "]" ..
"button_exit[4.5,2.375;1,1;set_digiline_channel;Set]"
end
else
formspec = formspec .. "formspec_version[4]"
if has_digilines then
formspec = formspec .. "size[6,4;]"
else
formspec = formspec .. "size[6,2;]"
end
-- Manual "fire" button
formspec = formspec ..
"button_exit[0.5,0.5;5,1;fire;Fire]"
-- Digiline channel
if has_digilines then
local channel = meta:get_string("channel") or ""
formspec = formspec ..
"field[0.5,2.5;3.5,1;digiline_channel;Digiline Channel;" .. channel .. "]" ..
"button_exit[4.5,2.5;1,1;set_digiline_channel;Set]"
end
end
meta:set_string("formspec", formspec)
end
spacecannon.can_shoot = function()
-- arguments: pos, playername
return true
end
spacecannon.can_destroy = function()
-- arguments: pos
return true
end
spacecannon.can_damage = function(_obj)
return true
end
spacecannon.fire = function(pos, playername, color, speed, is_th, storage_require_mod)
if not spacecannon.can_shoot(pos, playername) then
return
end
-- check fuel/power
local meta = minetest.get_meta(pos)
local config_store = spacecannon.config.ki_powerstorage * storage_require_mod
if is_th then config_store = spacecannon.config.th_powerstorage * storage_require_mod end
if meta:get_int("powerstorage") < config_store then
-- not enough power
return
end
-- check ammunition
if not is_th then
local inv = meta:get_inventory()
if inv:is_empty("src") then
--minetest.chat_send_player(playername, "No ammunition loaded!")
return false
end
local src_stack = inv:get_list("src")[1]
if not src_stack or src_stack:get_name() ~= "spacecannon:railgun_slug" then
--minetest.chat_send_player(playername, "Incorrect ammunition!")
return
end
end
-- use power
meta:set_int("powerstorage", 0)
-- use ammo
if not is_th then
local src_stack = meta:get_inventory():get_list("src")[1]
src_stack:take_item();
meta:get_inventory():set_stack("src", 1, src_stack)
end
minetest.sound_play("spacecannon_shoot", {
pos = pos,
gain = 1.0,
max_hear_distance = 16
})
local node = minetest.get_node(pos)
local dir = spacecannon.facedir_to_down_dir(node.param2)
local obj = minetest.add_entity({x=pos.x+dir.x, y=pos.y+dir.y, z=pos.z+dir.z}, "spacecannon:energycube_" .. color)
obj:setvelocity({x=dir.x*speed, y=dir.y*speed, z=dir.z*speed})
end
-- destroy stuff in range
-- TODO: resilient material list
spacecannon.destroy = function(pos, range, intensity)
if not spacecannon.can_destroy(pos) then
return
end
local particle_texture = nil
for x=-range,range do
for y=-range,range do
for z=-range,range do
if x*x+y*y+z*z <= range * range + range then
local np={x=pos.x+x,y=pos.y+y,z=pos.z+z}
if minetest.is_protected(np, "") then
return -- fail fast
end
local n = minetest.get_node_or_nil(np)
if n and n.name ~= "air" then
local node_def = minetest.registered_nodes[n.name]
if node_def and node_def.tiles and node_def.tiles[1] then
particle_texture = node_def.tiles[1]
end
if node_def.on_blast then
-- custom on_blast
node_def.on_blast(np, intensity)
else
-- default behavior
local resilience = spacecannon.node_resilience[n.name] or 1
if resilience <= 1 or math.random(resilience) == resilience then
minetest.set_node(np, {name="air"})
local itemstacks = minetest.get_node_drops(n.name)
for _, itemname in ipairs(itemstacks) do
if math.random(5) == 5 then
-- chance drop
minetest.add_item(np, itemname)
end
end
end
end
end
end
end
end
end
local radius = range
-- https://github.com/minetest/minetest_game/blob/master/mods/tnt/init.lua
minetest.add_particlespawner({
amount = 64,
time = 0.5,
minpos = vector.subtract(pos, radius / 2),
maxpos = vector.add(pos, radius / 2),
minvel = {x = -10, y = -10, z = -10},
maxvel = {x = 10, y = 10, z = 10},
minacc = vector.new(),
maxacc = vector.new(),
minexptime = 1,
maxexptime = 2.5,
minsize = radius * 3,
maxsize = radius * 5,
texture = "spacecannon_spark.png",
glow = 5
})
if particle_texture then
minetest.add_particlespawner({
amount = 64,
time = 0.5,
minpos = vector.subtract(pos, radius / 2),
maxpos = vector.add(pos, radius / 2),
minvel = {x = -10, y = -10, z = -10},
maxvel = {x = 10, y = 10, z = 10},
minacc = vector.new(),
maxacc = vector.new(),
minexptime = 1,
maxexptime = 2.5,
minsize = radius * 3,
maxsize = radius * 5,
texture = particle_texture,
glow = 5
})
end
minetest.sound_play("tnt_explode", {pos = pos, gain = 1.5, max_hear_distance = math.min(radius * 20, 128)})
end
-- convert face dir to vector
spacecannon.facedir_to_down_dir = function(facing)
return (
{[0]={x=0, y=-1, z=0},
{x=0, y=0, z=-1},
{x=0, y=0, z=1},
{x=-1, y=0, z=0},
{x=1, y=0, z=0},
{x=0, y=1, z=0}})[math.floor(facing/4)]
end