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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
fireworkz = {}
--Variables
local modname = "fireworkz"
local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(minetest.get_current_modname())
--Settings
fireworkz.settings = {}
local settings = Settings(modpath .. "/fireworkz.conf")
fireworkz.settings.igniter = settings:get("igniter") or "default:torch"
fireworkz.settings.ignition_time = tonumber(settings:get("ignition_time")) or 3
fireworkz.settings.max_hear_distance_fuse = tonumber(settings:get("max_hear_distance_fuse")) or 5
fireworkz.settings.max_hear_distance_launch = tonumber(settings:get("max_hear_distance_launch")) or 13
fireworkz.settings.max_hear_distance_bang = tonumber(settings:get("max_hear_distance_bang")) or 90
local color_map = {
blue = "cyan",
green = "lime",
}
local figure_map = {
default = "yellow",
ball = "magenta",
love = "red",
hi = "blue"
}
local figure_ingredient_map = {
--[[
1 2 3
4 x 5
6 x 7
]]
default = {false, true},
ball = {true, true, true, true, true, true, true},
love = {true, false, true, true, true},
hi = {true, false, false, true, true, true, true}
}
local color_variants = {"red", "green", "blue", "yellow", "white"}
local figure_variants = {"default", "ball", "love", "hi"}
local variant_list = {
{color = "blue_white_red", figure = "ball_default_love", desc = S("Blue-White-Love"),
rdt = {
{color = "blue", figure = "ball"},
{color = "yellow", figure = "default"},
{color = "red", figure = "love"},
},
recipe = {
output = "fireworkz:rocket_ball_default_love_blue_white_red",
recipe = {
{"dye:blue", "dye:white", "dye:red"},
{"default:paper", "tnt:gunpowder", "default:paper"},
{"default:paper", "default:paper", "default:paper"}
},
},
},
{color = "green_yellow_red", figure = "ball_default_love", desc = S("Green-Yellow-Love"),
rdt = {
{color = "green", figure = "ball"},
{color = "yellow", figure = "default"},
{color = "red", figure = "love"},
},
recipe = {
output = "fireworkz:rocket_ball_default_love_green_yellow_red",
recipe = {
{"dye:green", "dye:yellow", "dye:red"},
{"default:paper", "tnt:gunpowder", "default:paper"},
{"default:paper", "default:paper", "default:paper"}
},
}
},
}
--Functions
local figures = {}
function figures.line(t, pos, off, rep, vel)
if not t then t = {} end
local v = vector.new(pos)
for i = 0, rep, 1 do
t[#t+1] = vector.new(v)
t[#t].v = vel
v = vector.add(v, off)
end
return t
end
-- Figures
function figures.default(r)
local tab = {}
for x=-r, r, 0.02 do
for y=-r, r, 0.02 do
for z=-r, r, 0.02 do
if x*x + y*y + z*z <= r*r then
local v = math.random(21,35) --velocity
local xrand = math.random(-5, 5) * 0.001
local yrand = math.random(-5, 5) * 0.001
local zrand = math.random(-5, 5) * 0.001
tab[#tab+1] = {x=x+xrand, y=y+yrand, z=z+zrand, v=v}
end
end
end
end
return tab, 2, 4
end
function figures.ball(r)
local tab = {}
for x= -r, r, 0.01 do
for z= -r, r, 0.01 do
local y = math.sqrt(r*r-x*x-z*z)
if y == y then -- y ~= NaN
y = math.floor(y*100)/100
tab[#tab+1] = {x=x,y=y,z=z,v=43}
if y~=0 then tab[#tab+1] = {x=x,y=-y,z=z,v=43} end
end
end
end
return tab, 4, 4
end
function figures.love(r)
local tab = {
{x=0,y=0,z=0,v=60},
{x=0,y=0,z=-0.02,v=60},
{x=0.01,y=0,z=-0.03,v=60},
{x=0.02,y=0,z=-0.04,v=60},
{x=0.03,y=0,z=-0.04,v=60},
{x=0.04,y=0,z=-0.03,v=60},
{x=0.05,y=0,z=-0.02,v=60},
{x=0.05,y=0,z=-0.01,v=60},
{x=0.04,y=0,z=0,v=60},
{x=0.04,y=0,z=0.01,v=60},
{x=0.03,y=0,z=0.02,v=60},
{x=0.02,y=0,z=0.03,v=60},
{x=0.01,y=0,z=0.04,v=60},
{x=0,y=0,z=0.05,v=60},
{x=-0.01,y=0,z=0.04,v=60},
{x=-0.02,y=0,z=0.03,v=60},
{x=-0.03,y=0,z=0.02,v=60},
{x=-0.04,y=0,z=0.01,v=60},
{x=-0.04,y=0,z=0,v=60},
{x=-0.05,y=0,z=-0.01,v=60},
{x=-0.05,y=0,z=-0.02,v=60},
{x=-0.04,y=0,z=-0.03,v=60},
{x=-0.03,y=0,z=-0.04,v=60},
{x=-0.02,y=0,z=-0.04,v=60},
{x=-0.01,y=0,z=-0.03,v=60},
}
return tab, 7, 7
end
function figures.hi(r)
-- x
-- x x
-- x
-- x x
-- 0 xxxx x
-- x x x
-- x x x
-- x x x
-- x x x
--
-- 3210123
local tab = {
{x=0.01,y=0,z=-0.00,v=60},
{x=0.02,y=0,z=-0.00,v=60},
{x=-0.03,y=0,z=0.03,v=60}
}
figures.line(tab, {x=0.03,y=0,z=-0.04}, {x=0,y=0,z=0.01}, 8, 60)
figures.line(tab, {x=-0.03, y=0, z=0.01}, {x=0,y=0,z=-0.01}, 5, 60)
figures.line(tab, {x=0,y=0,z=-0.01}, {x=0,y=0,z=-0.01}, 3, 60)
return tab, 7, 7
end
-- Activate fireworks
local function partcl_gen(pos, tab, size_min, size_max, color)
for _,i in pairs(tab) do
minetest.add_particle({
pos = {x=pos.x, y=pos.y, z=pos.z},
velocity = {x= i.x*i.v, y= i.y*i.v, z= i.z*i.v},
acceleration = {x=0, y=-1.5, z=0},
expirationtime = 3,
size = math.random(size_min, size_max),
--collisiondetection = true,
--collision_removal = false,
vertical = false,
animation = {type="vertical_frames", aspect_w=9, aspect_h=9, length = 3.5,},
glow = 30,
texture = "anim_white_star.png^[multiply:"..(color_map[color] or color),
})
end
end
-- Entity Definition
local rocket = {
physical = true, --collides with things
wield_image = "rocket_default.png",
collisionbox = {0, -0.5 ,0 ,0 ,0.5 ,0},
visual = "sprite",
textures = {"rocket_default.png"},
timer = 0,
rocket_firetime = 0,
rocket_flytime = 0,
rdt = {} -- rocket data table
}
--Entity Registration
minetest.register_entity("fireworkz:rocket", rocket)
function rocket:on_activate(staticdata)
minetest.sound_play("fireworkz_rocket", {pos=self.object:getpos(), max_hear_distance = fireworkz.settings.max_hear_distance_launch, gain = 1,})
self.rocket_flytime = math.random(13,15)/10
self.object:setvelocity({x=0, y=9, z=0})
self.object:setacceleration({x= math.random(-5, 5), y= 33, z= math.random(-5, 5)})
end
-- Called periodically
function rocket:on_step(dtime)
self.timer = self.timer + dtime
self.rocket_firetime = self.rocket_firetime + dtime
if self.rocket_firetime > 0.1 then
local pos = self.object:getpos()
self.rocket_firetime = 0
local xrand = math.random(-15, 15) / 10
minetest.add_particle({
pos = {x=pos.x, y=pos.y - 0.4, z=pos.z},
velocity = {x=xrand, y=-3, z=xrand},
acceleration = {x=0, y=0, z=0},
expirationtime = 1.5,
size = 3,
collisiondetection = true,
vertical = false,
animation = {type="vertical_frames", aspect_w=9, aspect_h=9, length = 1.6,},
glow = 10,
texture = "anim_white_star.png",
})
end
if self.timer > self.rocket_flytime then
if #self.rdt > 0 then
minetest.sound_play("fireworkz_bang", {pos= self.object:get_pos(), max_hear_distance = fireworkz.settings.max_hear_distance_bang, gain = 3,})
for _, i in pairs(self.rdt) do
local pos = self.object:getpos()
if figures[i.figure] then
local t, min, max = figures[i.figure](0.1)
partcl_gen(pos, t, min, max, i.color)
end
end
end
self.object:remove()
end
end
--Nodes
local function register_fireworkz(color, figure, desc, rdt, recipe)
local figure_desc
if not desc then desc = S(color:sub(1,1):upper()..color:sub(2)) end
if figure == "ball_default_love" then
figure_desc = S("Love Ball")
else
figure_desc = S(figure:sub(1,1):upper()..figure:sub(2))
end
inv_image = ("rocket_default_%s.png^(rocket_default.png^[multiply:%s)"):format(color, figure_map[figure] or "white")
if not rdt then rdt = {{color = color, figure = figure},} end
rdt.texture = inv_image
minetest.register_node("fireworkz:rocket_"..figure.."_"..color, {
description = S("Rocket").." (".. desc .. "|"..figure_desc..")",
drawtype = "plantlike",
light_source = 5,
inventory_image = inv_image,
tiles = {inv_image},
wield_image = inv_image,
paramtype = "light",
sunlight_propagates = true,
walkable = false,
is_ground_content = false,
groups = {choppy = 3, explody = 1, firework = 1},
mesecons = {
effector = {
action_on = function(pos)
minetest.remove_node(pos)
fireworkz.launch(pos,rdt)
end
},
},
on_punch = function(pos)
minetest.remove_node(pos)
fireworkz.launch(pos,rdt)
end,
on_use = function(itemstack, user, pointed_thing)
local pos = minetest.get_pointed_thing_position(pointed_thing, true)
if pos then
itemstack:take_item()
fireworkz.launch(pos, rdt)
end
return itemstack
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("firework:rdt", minetest.serialize(rdt))
end,
})
if recipe then
if recipe == true then
local m = figure_ingredient_map[figure]
if m then
local function r(x)
if not m[x] then return "" end
if m[x] == true then return "default:paper" end
return m[x]
end
minetest.register_craft {
output = "fireworkz:rocket_"..figure.."_"..color,
recipe = {
{r(1), r(2), r(3)},
{r(4), "tnt:gunpowder", r(5)},
{r(6), "dye:"..color, r(7)}
}
}
end
else
minetest.register_craft(recipe)
end
end
end
fireworkz.launch = function(pos, rdt)
local obj = minetest.add_entity(pos, "fireworkz:rocket") --activate
if obj then
obj:set_properties({
textures={rdt.texture}
})
end
local obj_ent = obj:get_luaentity()
obj_ent.rdt = rdt
end
-- Register
for _, i in pairs(color_variants) do
for _, j in pairs(figure_variants) do
register_fireworkz(i, j, nil, nil, true)
end
end
for _, i in pairs(variant_list) do
register_fireworkz(i.color, i.figure, i.desc, i.rdt, i.recipe)
end
for k,v in pairs {green = "green", orange = "yellow", red = "red", violet = "blue"} do
minetest.register_alias("fireworks:"..k, "fireworkz:rocket_default_"..v)
end
|