-
Notifications
You must be signed in to change notification settings - Fork 2
/
torch.lua
228 lines (224 loc) · 7.41 KB
/
torch.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
-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore, math, string, tonumber, pairs, vector
= minetest, nodecore, math, string, tonumber, pairs, vector
local math_ceil, math_log, math_random, string_sub
= math.ceil, math.log, math.random, string.sub
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local checkdirs = {
{x = 1, y = 0, z = 0},
{x = -1, y = 0, z = 0},
{x = 0, y = 0, z = 1},
{x = 0, y = 0, z = -1},
{x = 0, y = 1, z = 0}
}
----------------------------------------------------------------------
nodecore.saptorch_life_base = 600 -- 5x longer than regular torch
function nodecore.get_saptorch_expire(meta, name)
local expire = meta:get_float("expire") or 0
if expire > 0 then return expire end
local ttl = nodecore.saptorch_life_base
* (nodecore.boxmuller() * 0.1 + 1)
if name then
local id = tonumber(string_sub(name, -1))
if id and id > 1 then
ttl = ttl * 0.5 ^ (id - 1)
end
end
expire = nodecore.gametime + ttl
meta:set_float("expire", expire)
return expire, true
end
----------------------------------------------------------------------
local resin = "nc_fire_coal_4.png^(" ..modname.. "_sap.png^[opacity:100)"
minetest.register_alias(modname .. ":saptorch_lit", modname .. ":saptorch_lit_1")
-- ================================================================ --
minetest.register_node(modname .. ":saptorch", {
description = "Resinated Torch",
drawtype = "mesh",
mesh = "nc_torch_torch.obj",
tiles = {
resin,
"nc_tree_tree_top.png",
resin.. "^[lowpart:50:nc_tree_tree_side.png",
"[combine:1x1"
},
backface_culling = true,
use_texture_alpha = "clip",
selection_box = nodecore.fixedbox(-1/8, -0.5, -1/8, 1/8, 0.5, 1/8),
collision_box = nodecore.fixedbox(-1/16, -0.5, -1/16, 1/16, 0.5, 1/16),
paramtype = "light",
sunlight_propagates = true,
groups = {
snappy = 1,
falling_node = 1,
flammable = 1,
firestick = 4,
stack_as_node = 1
},
sounds = nodecore.sounds("nc_tree_sticky"),
on_ignite = function(pos, node)
minetest.set_node(pos, {name = modname.. ":saptorch_lit"})
nodecore.get_saptorch_expire(minetest.get_meta(pos))
nodecore.sound_play("nc_fire_ignite", {gain = 1, pos = pos})
if node and node.count and node.count > 1 then
nodecore.item_disperse(pos, node.name, node.count - 1)
end
return true
end
})
nodecore.register_craft({
label = "assemble saptorch",
normal = {y = 1},
indexkeys = {modname.. ":lump_sap"},
nodes = {
{match = modname.. ":lump_sap", replace = "air"},
{y = -1, match = "nc_torch:torch", replace = modname .. ":saptorch"},
}
})
----------------------------------------------------------------------
nodecore.saptorch_life_stages = 4
for i = 1, nodecore.saptorch_life_stages do
local alpha = (i - 1) * (256 / nodecore.saptorch_life_stages)
if alpha > 255 then alpha = 255 end
local txr = resin.. "^nc_fire_ember_4.png^(nc_fire_ash.png^[opacity:"
.. alpha .. ")"
minetest.register_node(modname .. ":saptorch_lit_" .. i, {
description = "Lit Resinated Torch",
drawtype = "mesh",
mesh = "nc_torch_torch.obj",
tiles = {
txr,
"nc_tree_tree_top.png",
txr .. "^[lowpart:50:nc_tree_tree_side.png",
{
name = "nc_torch_flame.png",
animation = {
["type"] = "vertical_frames",
aspect_w = 3,
aspect_h = 8,
length = 0.6
}
}
},
backface_culling = true,
use_texture_alpha = "clip",
selection_box = nodecore.fixedbox(-1/8, -0.5, -1/8, 1/8, 0.5, 1/8),
collision_box = nodecore.fixedbox(-1/16, -0.5, -1/16, 1/16, 0.5, 1/16),
paramtype = "light",
sunlight_propagates = true,
light_source = 10 - i,
groups = {
snappy = 1,
falling_node = 1,
stack_as_node = 1,
saptorch_lit = 1,
flame_ambiance = 1
},
stack_max = 1,
sounds = nodecore.sounds("nc_tree_sticky"),
preserve_metadata = function(_, _, oldmeta, drops)
drops[1]:get_meta():from_table({fields = oldmeta})
end,
after_place_node = function(pos, _, itemstack)
minetest.get_meta(pos):from_table(itemstack:get_meta():to_table())
end,
node_dig_prediction = nodecore.dynamic_light_node(16 - i),
after_destruct = function(pos)
nodecore.dynamic_light_add(pos, 16 - i)
end
})
end
------------------------------------------------------------------------
minetest.register_abm({
label = "saptorch ignite",
interval = 2,
chance = 1,
nodenames = {"group:saptorch_lit"},
neighbors = {"group:flammable"},
action_delay = true,
action = function(pos)
for _, ofst in pairs(checkdirs) do
local npos = vector.add(pos, ofst)
local nbr = minetest.get_node(npos)
if minetest.get_item_group(nbr.name, "flammable") > 0
and not nodecore.quenched(npos) then
nodecore.fire_check_ignite(npos, nbr)
end
end
end
})
------------------------------------------------------------------------
local log2 = math_log(2)
local function saptorchlife(expire, pos)
local max = nodecore.saptorch_life_stages
if expire <= nodecore.gametime then return max end
local life = (expire - nodecore.gametime) / nodecore.saptorch_life_base
if life > 1 then return 1 end
local stage = 1 - math_ceil(math_log(life) / log2)
if stage < 1 then return 1 end
if stage > max then return max end
if pos and (stage >= 2) then
nodecore.smokefx(pos, {
time = 1,
rate = (stage - 1) / 2,
scale = 0.25
})
end
return stage
end
local function snufffx(pos)
nodecore.smokeburst(pos, 4)
return nodecore.sound_play("nc_fire_snuff", {gain = 1, pos = pos})
end
------------------------------------------------------------------------
minetest.register_abm({
label = "saptorch snuff",
interval = 1,
chance = 1,
nodenames = {"group:saptorch_lit"},
action = function(pos, node)
local expire = nodecore.get_saptorch_expire(minetest.get_meta(pos), node.name)
if nodecore.quenched(pos) or nodecore.gametime > expire then
minetest.remove_node(pos)
minetest.add_item(pos, {name = "nc_fire:lump_ash"})
snufffx(pos)
return
end
local nn = modname .. ":saptorch_lit_" .. saptorchlife(expire, pos)
if node.name ~= nn then
node.name = nn
return minetest.swap_node(pos, node)
end
end
})
------------------------------------------------------------------------
nodecore.register_aism({
label = "saptorch stack interact",
itemnames = {"group:saptorch_lit"},
action = function(stack, data)
local pos = data.pos
local player = data.player
local wield
if player and data.list == "main"
and player:get_wield_index() == data.slot then
wield = true
pos = vector.add(pos, vector.multiply(player:get_look_dir(), 0.5))
end
local expire, dirty = nodecore.get_saptorch_expire(stack:get_meta(), stack:get_name())
if (expire < nodecore.gametime)
or nodecore.quenched(pos, data.node and 1 or 0.3) then
snufffx(pos)
return "nc_fire:lump_ash"
end
if wield and math_random() < 0.1 then nodecore.fire_check_ignite(pos) end
local nn = modname .. ":saptorch_lit_" .. saptorchlife(expire, pos)
if stack:get_name() ~= nn then
stack:set_name(nn)
return stack
elseif dirty then
return stack
end
end
})
-- ================================================================ --