torches/init.lua

202 lines
5.2 KiB
Lua
Raw Normal View History

2014-05-01 09:58:00 -07:00
-- Reduce particles send to client if on Server
local SERVER = minetest.is_singleplayer() or false
SERVER = not SERVER
2015-02-16 02:14:35 -08:00
local dur = 2
2014-05-01 09:58:00 -07:00
if SERVER then
2015-02-16 02:14:35 -08:00
dur = 9 -- lowering sends more pakets to clients and let flames faster disappear (not recommended)
2014-05-01 09:58:00 -07:00
end
2015-02-16 02:14:35 -08:00
local VIEW_DISTANCE = 13 -- from what distance (in nodes) flames are send to player/client
2014-01-16 05:28:15 -08:00
2015-02-23 16:23:07 -08:00
-- constants
2015-02-15 21:34:16 -08:00
local rotat = {"I", "FX"}
local particle_def = {
2015-02-15 21:01:21 -08:00
pos = {x = 0, y = 0, z = 0},
velocity = { x= 0, y = 0, z = 0},
acceleration = {x = 0, y = 0, z = 0},
expirationtime = 1,
2015-02-15 21:06:12 -08:00
size = 3.0,
collisiondetection = true,
vertical = false,
texture = "torches_fire_1.png",
}
2015-02-23 16:23:07 -08:00
-- fire particles (flames)
local function add_fire(pos, duration, offset)
if offset then
pos.x = pos.x + offset.x
pos.z = pos.z + offset.z
end
2015-02-15 21:01:21 -08:00
pos.y = pos.y + 0.19
particle_def.pos = pos
particle_def.expirationtime = duration
2015-02-15 21:34:16 -08:00
particle_def.texture = "torches_fire"..tostring(math.random(1, 2)) ..".png^[transform"..rotat[math.random(1,2)]
minetest.add_particle(particle_def)
2015-02-15 21:01:21 -08:00
pos.y = pos.y + 0.01
particle_def.pos = pos
2015-02-15 21:34:16 -08:00
particle_def.texture = "torches_fire"..tostring(math.random(1, 2)) ..".png^[transform"..rotat[math.random(1,2)]
minetest.add_particle(particle_def)
2013-05-25 14:54:00 -07:00
end
2015-02-23 16:23:07 -08:00
-- helper functions
2014-01-16 05:28:15 -08:00
local function player_near(pos)
2015-02-15 21:01:21 -08:00
for _,object in ipairs(minetest.get_objects_inside_radius(pos, VIEW_DISTANCE)) do
2014-01-16 05:28:15 -08:00
if object:is_player() then
return true
end
end
return false
end
2015-02-23 16:23:07 -08:00
local function get_offset(wdir)
local z = 0
local x = 0
2015-02-23 16:23:07 -08:00
if wdir == 4 then
z = 0.15
2015-02-23 16:23:07 -08:00
elseif wdir == 2 then
x = 0.15
2015-02-23 16:23:07 -08:00
elseif wdir == 5 then
z = -0.15
2015-02-23 16:23:07 -08:00
elseif wdir == 3 then
x = -0.15
end
2015-02-15 21:01:21 -08:00
return {x = x, y = -0.06, z = z}
end
2014-01-22 04:40:48 -08:00
-- abms for flames
2013-05-25 14:54:00 -07:00
minetest.register_abm({
nodenames = {"torches:wand"},
2015-02-16 02:14:35 -08:00
interval = dur - 1,
2013-05-25 14:54:00 -07:00
chance = 1,
action = function(pos)
2014-01-16 05:28:15 -08:00
if player_near(pos) then
local n = minetest.get_node_or_nil(pos)
2015-02-15 21:01:21 -08:00
local dir = {x = 0, y = 0, z = 0}
if n and n.param2 then
dir = get_offset(n.param2)
end
2015-02-16 02:14:35 -08:00
add_fire(pos, dur - .9, dir)
2014-01-16 05:28:15 -08:00
end
2013-05-25 14:54:00 -07:00
end
})
minetest.register_abm({
nodenames = {"torches:floor"},
2015-02-16 02:14:35 -08:00
interval = dur - 1,
2013-05-25 14:54:00 -07:00
chance = 1,
2014-01-22 04:40:48 -08:00
action = function(pos)
2014-01-16 05:28:15 -08:00
if player_near(pos) then
2015-02-16 02:14:35 -08:00
add_fire(pos, dur - .9)
2014-01-16 05:28:15 -08:00
end
2013-05-25 14:54:00 -07:00
end
})
2014-01-22 04:40:48 -08:00
-- convert old torches and remove ceiling placed
minetest.register_abm({
nodenames = {"default:torch"},
interval = 1,
chance = 1,
action = function(pos)
local n = minetest.get_node(pos)
local def = minetest.registered_nodes[n.name]
2015-02-23 16:23:07 -08:00
if n and def then
2014-01-22 04:40:48 -08:00
local wdir = n.param2
if wdir == 0 then
minetest.remove_node(pos)
elseif wdir == 1 then
2015-02-23 16:23:07 -08:00
minetest.set_node(pos, {name = "torches:floor", param2 = wdir})
2014-01-22 04:40:48 -08:00
else
2015-02-23 16:23:07 -08:00
minetest.set_node(pos, {name = "torches:wall", param2 = wdir})
2014-01-22 04:40:48 -08:00
end
end
2013-05-25 14:54:00 -07:00
end
2014-01-22 04:40:48 -08:00
})
2013-05-25 14:54:00 -07:00
2015-02-23 16:23:07 -08:00
-- Item definitions
2013-05-25 14:54:00 -07:00
minetest.register_craftitem(":default:torch", {
description = "Torch",
inventory_image = "torches_torch.png^[transformR90",
wield_image = "torches_torch.png^[transformR90",
2015-02-15 21:01:21 -08:00
wield_scale = {x = 1, y = 1, z = 1 + 1/16},
2013-05-25 14:54:00 -07:00
liquids_pointable = false,
on_place = function(itemstack, placer, pointed_thing)
2015-02-16 02:14:35 -08:00
if pointed_thing.type ~= "node" then
2013-05-26 05:54:07 -07:00
return itemstack
end
2015-02-16 02:14:35 -08:00
2013-05-25 14:54:00 -07:00
local above = pointed_thing.above
local under = pointed_thing.under
local wdir = minetest.dir_to_wallmounted({x = under.x - above.x, y = under.y - above.y, z = under.z - above.z})
2015-02-16 02:14:35 -08:00
2015-02-23 16:23:07 -08:00
local fakestack = itemstack
local retval = false
if wdir < 1 then
2015-02-16 02:14:35 -08:00
return itemstack
2015-02-23 16:23:07 -08:00
elseif wdir == 1 then
retval = fakestack:set_name("torches:floor")
2013-05-25 14:54:00 -07:00
else
2015-02-23 16:23:07 -08:00
retval = fakestack:set_name("torches:wall")
2013-05-25 14:54:00 -07:00
end
2015-02-23 16:23:07 -08:00
if not retval then
return itemstack
2013-05-25 14:54:00 -07:00
end
2015-02-23 16:23:07 -08:00
itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
itemstack:set_name("default:torch")
2015-02-23 16:23:07 -08:00
-- add flame if placing was sucessfull
if retval then
-- expect node switch one sever step (default 0.1) delayed
minetest.after(0.1, add_fire, above, dur, get_offset(wdir))
end
2013-05-25 14:54:00 -07:00
return itemstack
end
})
minetest.register_node("torches:floor", {
inventory_image = "default_torch.png",
wield_image = "torches_torch.png",
2015-02-15 21:01:21 -08:00
wield_scale = {x = 1, y = 1, z = 1 + 2/16},
drawtype = "mesh",
mesh = "torch_floor.obj",
tiles = {"torches_torch.png"},
2013-05-25 14:54:00 -07:00
paramtype = "light",
paramtype2 = "none",
sunlight_propagates = true,
drop = "default:torch",
walkable = false,
light_source = 13,
2015-02-23 16:23:07 -08:00
groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1},
2013-05-25 14:54:00 -07:00
legacy_wallmounted = true,
selection_box = {
type = "fixed",
fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
},
2013-05-25 14:54:00 -07:00
})
2015-02-23 16:23:07 -08:00
minetest.register_node("torches:wall", {
2013-05-25 14:54:00 -07:00
inventory_image = "default_torch.png",
wield_image = "torches_torch.png",
2015-02-15 21:01:21 -08:00
wield_scale = {x = 1, y = 1, z = 1 + 1/16},
drawtype = "mesh",
mesh = "torch_wall.obj",
tiles = {"torches_torch.png"},
2013-05-25 14:54:00 -07:00
paramtype = "light",
2015-02-23 16:23:07 -08:00
paramtype2 = "wallmounted",
2013-05-25 14:54:00 -07:00
sunlight_propagates = true,
walkable = false,
light_source = 13,
2015-02-23 16:23:07 -08:00
groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1},
2013-05-25 14:54:00 -07:00
drop = "default:torch",
selection_box = {
2015-02-23 16:23:07 -08:00
type = "wallmounted",
wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
2013-05-25 14:54:00 -07:00
},
})
2014-01-22 04:40:48 -08:00
2015-02-23 16:23:07 -08:00
minetest.register_alias("torches:wand", "torches:wall")