Skip to content

Instantly share code, notes, and snippets.

@PilzAdam
Created November 23, 2012 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PilzAdam/4135966 to your computer and use it in GitHub Desktop.
Save PilzAdam/4135966 to your computer and use it in GitHub Desktop.
diff --git a/mods/fire/init.lua b/mods/fire/init.lua
index 846a2d3..a8530fc 100644
--- a/mods/fire/init.lua
+++ b/mods/fire/init.lua
@@ -8,24 +8,34 @@ minetest.register_node("fire:basic_flame", {
groups = {igniter=2,dig_immediate=3},
drop = '',
walkable = false,
+
+ after_place_node = function(pos, placer)
+ fire.update_sounds_around(pos)
+ end,
+
+ after_dig_node = function(pos, oldnode, oldmetadata, digger)
+ fire.update_sounds_around(pos)
+ end,
})
-local fire = {}
-fire.D = 6
+-- Store functions in a global table
+fire = {}
+
+local D = 6
-- key: position hash of low corner of area
-- value: {handle=sound handle, name=sound name}
-fire.sounds = {}
+local sounds = {}
function fire.get_area_p0p1(pos)
local p0 = {
- x=math.floor(pos.x/fire.D)*fire.D,
- y=math.floor(pos.y/fire.D)*fire.D,
- z=math.floor(pos.z/fire.D)*fire.D,
+ x=math.floor(pos.x/D)*D,
+ y=math.floor(pos.y/D)*D,
+ z=math.floor(pos.z/D)*D,
}
local p1 = {
- x=p0.x+fire.D-1,
- y=p0.y+fire.D-1,
- z=p0.z+fire.D-1
+ x=p0.x+D-1,
+ y=p0.y+D-1,
+ z=p0.z+D-1
}
return p0, p1
end
@@ -44,10 +54,10 @@ function fire.update_sounds_around(pos)
wanted_sound = {name="fire_small", gain=1.5}
end
local p0_hash = minetest.hash_node_position(p0)
- local sound = fire.sounds[p0_hash]
+ local sound = sounds[p0_hash]
if not sound then
if should_have_sound then
- fire.sounds[p0_hash] = {
+ sounds[p0_hash] = {
handle = minetest.sound_play(wanted_sound, {pos=cp, loop=true}),
name = wanted_sound.name,
}
@@ -55,10 +65,10 @@ function fire.update_sounds_around(pos)
else
if not wanted_sound then
minetest.sound_stop(sound.handle)
- fire.sounds[p0_hash] = nil
+ sounds[p0_hash] = nil
elseif sound.name ~= wanted_sound.name then
minetest.sound_stop(sound.handle)
- fire.sounds[p0_hash] = {
+ sounds[p0_hash] = {
handle = minetest.sound_play(wanted_sound, {pos=cp, loop=true}),
name = wanted_sound.name,
}
@@ -66,16 +76,6 @@ function fire.update_sounds_around(pos)
end
end
-function fire.on_flame_add_at(pos)
- --print("flame added at "..minetest.pos_to_string(pos))
- fire.update_sounds_around(pos)
-end
-
-function fire.on_flame_remove_at(pos)
- --print("flame removed at "..minetest.pos_to_string(pos))
- fire.update_sounds_around(pos)
-end
-
function fire.find_pos_for_flame_around(pos)
return minetest.env:find_node_near(pos, 1, {"air"})
end
@@ -88,18 +88,6 @@ function fire.flame_should_extinguish(pos)
return (#ps ~= 0)
end
-minetest.register_on_placenode(function(pos, newnode, placer)
- if newnode.name == "fire:basic_flame" then
- fire.on_flame_add_at(pos)
- end
-end)
-
-minetest.register_on_dignode(function(pos, oldnode, digger)
- if oldnode.name == "fire:basic_flame" then
- fire.on_flame_remove_at(pos)
- end
-end)
-
-- Ignite neighboring nodes
minetest.register_abm({
nodenames = {"group:flammable"},
@@ -114,7 +102,7 @@ minetest.register_abm({
local p = fire.find_pos_for_flame_around(p0)
if p then
minetest.env:set_node(p, {name="fire:basic_flame"})
- fire.on_flame_add_at(p)
+ fire.update_sounds_around(p)
end
end,
})
@@ -140,7 +128,7 @@ minetest.register_abm({
local p2 = fire.find_pos_for_flame_around(p)
if p2 then
minetest.env:set_node(p2, {name="fire:basic_flame"})
- fire.on_flame_add_at(p2)
+ fire.update_sounds_around(p2)
end
end
end,
@@ -155,7 +143,7 @@ minetest.register_abm({
-- If there is water or stuff like that around flame, remove flame
if fire.flame_should_extinguish(p0) then
minetest.env:remove_node(p0)
- fire.on_flame_remove_at(p0)
+ fire.update_sounds_around(p0)
return
end
-- Make the following things rarer
@@ -165,7 +153,7 @@ minetest.register_abm({
-- If there are no flammable nodes around flame, remove flame
if not minetest.env:find_node_near(p0, 1, {"group:flammable"}) then
minetest.env:remove_node(p0)
- fire.on_flame_remove_at(p0)
+ fire.update_sounds_around(p0)
return
end
if math.random(1,4) == 1 then
@@ -181,7 +169,7 @@ minetest.register_abm({
else
-- remove flame
minetest.env:remove_node(p0)
- fire.on_flame_remove_at(p0)
+ fire.update_sounds_around(p0)
end
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment