code cleanup, fixes and added new 'waiting' status

This commit is contained in:
Juraj Vajda 2016-01-07 23:34:02 +01:00
parent a2c75d673c
commit a7ef71e7ce

173
init.lua
View File

@ -3,7 +3,7 @@ local max_obj_per_mapblock = tonumber(minetest.setting_get("max_objects_per_bloc
-- main table -- main table
spawners = {} spawners = {}
-- list of mods -- list of mods
spawners.mob_mods = {"mobs", "testmod"} spawners.mob_mods = {"mobs", "creatures", "testmod"}
-- table holding all mobs info -- table holding all mobs info
spawners.mob_tables = {} spawners.mob_tables = {}
@ -13,6 +13,8 @@ for k, v in ipairs(spawners.mob_mods) do
local modpath = minetest.get_modpath(v) local modpath = minetest.get_modpath(v)
if (modpath) then if (modpath) then
-- MOBS REDO CONFIG
if v == "mobs" and mobs.mod == "redo" then
-- list of mobs and their info -- list of mobs and their info
table.insert(spawners.mob_tables, {name="sheep_white", mod_prefix_default=v, mod_prefix_custom="", dummy_size={x=0.52,y=0.52}, dummy_offset=0.2, dummy_mesh="mobs_sheep.b3d", dummy_texture={"mobs_sheep_white.png"}, night_only=false}) table.insert(spawners.mob_tables, {name="sheep_white", mod_prefix_default=v, mod_prefix_custom="", dummy_size={x=0.52,y=0.52}, dummy_offset=0.2, dummy_mesh="mobs_sheep.b3d", dummy_texture={"mobs_sheep_white.png"}, night_only=false})
@ -25,8 +27,10 @@ for k, v in ipairs(spawners.mob_mods) do
table.insert(spawners.mob_tables, {name="bunny", mod_prefix_default=v, mod_prefix_custom="", dummy_size={x=1,y=1}, dummy_offset=0.2, dummy_mesh="mobs_bunny.b3d", dummy_texture={"mobs_bunny_brown.png"}, night_only=false}) table.insert(spawners.mob_tables, {name="bunny", mod_prefix_default=v, mod_prefix_custom="", dummy_size={x=1,y=1}, dummy_offset=0.2, dummy_mesh="mobs_bunny.b3d", dummy_texture={"mobs_bunny_brown.png"}, night_only=false})
table.insert(spawners.mob_tables, {name="kitten", mod_prefix_default=v, mod_prefix_custom="", dummy_size={x=0.32,y=0.32}, dummy_offset=0, dummy_mesh="mobs_kitten.b3d", dummy_texture={"mobs_kitten_ginger.png"}, night_only=false}) table.insert(spawners.mob_tables, {name="kitten", mod_prefix_default=v, mod_prefix_custom="", dummy_size={x=0.32,y=0.32}, dummy_offset=0, dummy_mesh="mobs_kitten.b3d", dummy_texture={"mobs_kitten_ginger.png"}, night_only=false})
end
else else
print("[Spawners] MOD: "..v.." not found.") print("[MOD] Spawners: MOD "..v.." not found.")
end end
end end
@ -48,11 +52,6 @@ function spawners.start_spawning(pos, how_many, mob_name, mod_prefix)
end end
for i=1,how_many do for i=1,how_many do
-- local x = 1/math.random(1,3)
-- local z = 1/math.random(1,3)
-- local p = {x=pos.x+x,y=pos.y,z=pos.z+z}
local obj = minetest.add_entity(pos, mod_prefix..":"..mob_name) local obj = minetest.add_entity(pos, mod_prefix..":"..mob_name)
if obj then if obj then
@ -65,7 +64,6 @@ function spawners.start_spawning(pos, how_many, mob_name, mod_prefix)
}) })
end end
minetest.log("action", "Spawners: spawned "..mob_name.." at ("..pos.x..","..pos.y..","..pos.z..")")
end end
end end
@ -84,7 +82,7 @@ function spawners.check_around_radius(pos)
return player_near return player_near
end end
function check_node_status(pos, mob, night_only) function spawners.check_node_status(pos, mob, night_only)
local player_near = spawners.check_around_radius(pos) local player_near = spawners.check_around_radius(pos)
local random_pos = false local random_pos = false
@ -100,7 +98,7 @@ function check_node_status(pos, mob, night_only)
local top = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}) local top = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z})
local bottom = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) local bottom = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
if not node_light then return end if not node_light then return false end
-- make sure that at least one side of the spawner is open -- make sure that at least one side of the spawner is open
if front.name == "air" then if front.name == "air" then
@ -123,76 +121,36 @@ function check_node_status(pos, mob, night_only)
end end
-- spawner is closed from all sides -- spawner is closed from all sides
if not (front or right or back or left or top or bottom) then if not (front or right or back or left or top or bottom) then return false end
minetest.log("action","spawner is closed")
return false
end
local pick_random = math.random(1,#spawn_positions)
-- pick random from the open sides -- pick random from the open sides
local pick_random = math.random(1,#spawn_positions)
for k, v in pairs (spawn_positions) do for k, v in pairs (spawn_positions) do
if k == pick_random then if k == pick_random then
random_pos = v random_pos = v
end end
end end
if not random_pos then return end if not random_pos then return false end
print("#spawn_positions: "..#spawn_positions) -- check the node above and below the found air node
print("pick_random: "..pick_random)
print("node_light: "..node_light)
-- print("node_pos"..minetest.pos_to_string(pos))
-- print("random_pos"..minetest.pos_to_string(random_pos))
-- check the node above the found air node
local node_above = minetest.get_node({x=random_pos.x, y=random_pos.y+1, z=random_pos.z}).name local node_above = minetest.get_node({x=random_pos.x, y=random_pos.y+1, z=random_pos.z}).name
local node_below = minetest.get_node({x=random_pos.x, y=random_pos.y-1, z=random_pos.z}).name local node_below = minetest.get_node({x=random_pos.x, y=random_pos.y-1, z=random_pos.z}).name
print("node_above: "..node_above) if not (node_above == "air" or node_below == "air") then return false end
print("node_below: "..node_below)
if not (node_above == "air" or node_below == "air") then
print("node below or above is not air")
return false
end
-- spawn only at day
if not night_only and node_light < min_node_light then if not night_only and node_light < min_node_light then
print("not enough light") return false, true
return false
end end
-- At 0 time of day (tod) the day begins; it is midnight and the moon is exactly at the zenith (1). -- spawn only at night
-- At 4500 tod, the first sun rays emit from the horizon, it gets brighter (2).
-- At 4750 tod, the sun rises and it gets brighter (3).
-- At 5001 tod, it gets brighter again (4).
-- At 5200 tod, the sun becomes fully visible (4).
-- At 5250 tod, it gets brighter again (5).
-- At 5500 tod, it gets brighter again (6).
-- At 5751 tod, maximum brightness is reached (7).
-- At 12000 tod is midday; the sun is exactly at the zenith (7).
-- At 18250 tod, the day is going to end, it gets a bit darker (6).
-- At 18502 tod, it gets a bit darker again (5).
-- At 18600 tod, the sun begins to set (5).
-- At 18752 tod, it gets a bit darker yet again (4).
-- At 19000 tod, the sky gets even darker (3).
-- At 19252 tod, the sun is almost gone and the sky gets even darker (2).
-- At 19359 tod, the sun square is gone and the last sun rays emit from the horizon (2).
-- At 19500 tod, the sun rays stop from being visible (2).
-- At 19502 tod, the sky has the lowest brightness (1).
-- At 24000 tod, the day ends and the next one starts; it is midnight again (1).
if night_only then if night_only then
print("tod: "..tod)
if not (19359 > tod and tod > 5200) or node_light < min_node_light then if not (19359 > tod and tod > 5200) or node_light < min_node_light then
print("it's night")
if random_pos then
return random_pos return random_pos
else else
return false return false, true
end
else
return false
end end
end end
@ -203,6 +161,7 @@ function check_node_status(pos, mob, night_only)
end end
function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, night_only) function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, night_only)
-- dummy inside the spawner
local dummy_definition = { local dummy_definition = {
hp_max = 1, hp_max = 1,
physical = true, physical = true,
@ -228,7 +187,7 @@ function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, nigh
self.timer = self.timer + dtime self.timer = self.timer + dtime
local n = minetest.get_node_or_nil(self.object:getpos()) local n = minetest.get_node_or_nil(self.object:getpos())
if self.timer > 2 then if self.timer > 2 then
if n and n.name and n.name ~= "spawners:"..mob_name.."_spawner_active" then if n and n.name and n.name ~= "spawners:"..mod_prefix.."_"..mob_name.."_spawner_active" then
self.object:remove() self.object:remove()
end end
end end
@ -237,8 +196,8 @@ function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, nigh
minetest.register_entity("spawners:dummy_"..mob_name, dummy_definition) minetest.register_entity("spawners:dummy_"..mob_name, dummy_definition)
-- node spawner active -- node spawner active
minetest.register_node("spawners:"..mob_name.."_spawner_active", { minetest.register_node("spawners:"..mod_prefix.."_"..mob_name.."_spawner_active", {
description = mob_name.." spawner active", description = mod_prefix.."_"..mob_name.." spawner active",
paramtype = "light", paramtype = "light",
light_source = 4, light_source = 4,
drawtype = "allfaces", drawtype = "allfaces",
@ -258,16 +217,40 @@ function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, nigh
}, },
is_ground_content = true, is_ground_content = true,
groups = {cracky=1,level=2,igniter=1,not_in_creative_inventory=1}, groups = {cracky=1,level=2,igniter=1,not_in_creative_inventory=1},
drop = "spawners:"..mob_name.."_spawner", drop = "spawners:"..mod_prefix.."_"..mob_name.."_spawner",
on_construct = function(pos) on_construct = function(pos)
pos.y = pos.y + offset pos.y = pos.y + offset
minetest.add_entity(pos,"spawners:dummy_"..mob_name) minetest.add_entity(pos,"spawners:dummy_"..mob_name)
end, end,
}) })
-- node spawner inactive -- node spawner waiting for light - everything is ok but too much light or not enough light
minetest.register_node("spawners:"..mob_name.."_spawner", { minetest.register_node("spawners:"..mod_prefix.."_"..mob_name.."_spawner_waiting", {
description = mob_name.." spawner", description = mod_prefix.."_"..mob_name.." spawner waiting",
paramtype = "light",
light_source = 2,
drawtype = "allfaces",
walkable = true,
sunlight_propagates = true,
tiles = {
{
name = "spawners_spawner_off_animated.png",
animation = {
type = "vertical_frames",
aspect_w = 16,
aspect_h = 16,
length = 2.0
},
}
},
is_ground_content = true,
groups = {cracky=1,level=2,not_in_creative_inventory=1},
drop = "spawners:"..mod_prefix.."_"..mob_name.."_spawner",
})
-- node spawner inactive (default)
minetest.register_node("spawners:"..mod_prefix.."_"..mob_name.."_spawner", {
description = mod_prefix.."_"..mob_name.." spawner",
paramtype = "light", paramtype = "light",
drawtype = "allfaces", drawtype = "allfaces",
walkable = true, walkable = true,
@ -276,19 +259,20 @@ function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, nigh
is_ground_content = true, is_ground_content = true,
groups = {cracky=1,level=2}, groups = {cracky=1,level=2},
on_construct = function(pos) on_construct = function(pos)
local random_pos = check_node_status(pos, mob_name, night_only) local random_pos, waiting = spawners.check_node_status(pos, mob_name, night_only)
if random_pos then if random_pos then
minetest.set_node(pos, {name="spawners:"..mob_name.."_spawner_active"}) minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner_active"})
elseif waiting then
minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner_waiting"})
else else
minetest.log("action","no random_pos found")
end end
end, end,
}) })
-- node spawner overheated -- node spawner overheated
minetest.register_node("spawners:"..mob_name.."_spawner_overheat", { minetest.register_node("spawners:"..mod_prefix.."_"..mob_name.."_spawner_overheat", {
description = mob_name.." spawner overheated", description = mod_prefix.."_"..mob_name.." spawner overheated",
paramtype = "light", paramtype = "light",
light_source = 2, light_source = 2,
drawtype = "allfaces", drawtype = "allfaces",
@ -298,44 +282,38 @@ function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, nigh
tiles = {"spawners_spawner.png^[colorize:#FF000030"}, tiles = {"spawners_spawner.png^[colorize:#FF000030"},
is_ground_content = true, is_ground_content = true,
groups = {cracky=1,level=2,igniter=1,not_in_creative_inventory=1}, groups = {cracky=1,level=2,igniter=1,not_in_creative_inventory=1},
drop = "spawners:"..mob_name.."_spawner", drop = "spawners:"..mod_prefix.."_"..mob_name.."_spawner",
on_construct = function(pos) on_construct = function(pos)
minetest.get_node_timer(pos):start(60) minetest.get_node_timer(pos):start(60)
end, end,
on_timer = function(pos, elapsed) on_timer = function(pos, elapsed)
minetest.log("action", "============= timer elapsed: "..elapsed) minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner"})
minetest.set_node(pos, {name="spawners:"..mob_name.."_spawner"})
end, end,
}) })
-- abm -- abm
minetest.register_abm({ minetest.register_abm({
nodenames = {"spawners:"..mob_name.."_spawner", "spawners:"..mob_name.."_spawner_active", "spawners:"..mob_name.."_spawner_overheat"}, nodenames = {"spawners:"..mod_prefix.."_"..mob_name.."_spawner", "spawners:"..mod_prefix.."_"..mob_name.."_spawner_active", "spawners:"..mod_prefix.."_"..mob_name.."_spawner_overheat", "spawners:"..mod_prefix.."_"..mob_name.."_spawner_waiting"},
neighbors = {"air"}, neighbors = {"air"},
interval = 2.0, interval = 2.0,
chance = 20, chance = 20,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
minetest.log("action", "active_object_count: "..active_object_count)
minetest.log("action", "active_object_count_wider: "..active_object_count_wider)
local random_pos = check_node_status(pos, mob_name, night_only) local random_pos, waiting = spawners.check_node_status(pos, mob_name, night_only)
if random_pos then if random_pos then
-- random_pos found
-- do not spawn if too many active entities in map block -- do not spawn if too many active entities in map block and call cooldown
-- call cooldown
if active_object_count_wider > max_obj_per_mapblock then if active_object_count_wider > max_obj_per_mapblock then
minetest.log("action", "************** too many mobs in area")
-- make sure the right node status is shown
if node.name ~= "spawners:"..mob_name.."_spawner_overheat" then if node.name ~= "spawners:"..mob_name.."_spawner_overheat" then
minetest.set_node(pos, {name="spawners:"..mob_name.."_spawner_overheat"}) minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner_overheat"})
end end
-- extend the timeout if still too many entities in map block -- extend the timeout if still too many entities in map block
if node.name == "spawners:"..mob_name.."_spawner_overheat" then if node.name == "spawners:"..mod_prefix.."_"..mob_name.."_spawner_overheat" then
minetest.log("action", "++++++++++++++ extending timeout")
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()
minetest.get_node_timer(pos):start(60) minetest.get_node_timer(pos):start(60)
end end
@ -343,20 +321,27 @@ function spawners.create(mob_name, mod_prefix, size, offset, mesh, texture, nigh
return return
end end
if node.name ~= "spawners:"..mob_name.."_spawner_active" then -- make sure the right node status is shown
minetest.set_node(pos, {name="spawners:"..mob_name.."_spawner_active"}) if node.name ~= "spawners:"..mod_prefix.."_"..mob_name.."_spawner_active" then
minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner_active"})
end end
-- enough place to spawn more mobs
spawners.start_spawning(random_pos, 1, "spawners:"..mob_name, mod_prefix) spawners.start_spawning(random_pos, 1, "spawners:"..mob_name, mod_prefix)
elseif waiting then
-- waiting status
if node.name ~= "spawners:"..mod_prefix.."_"..mob_name.."_spawner_spawner_waiting" then
minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner_waiting"})
end
else else
-- no random_pos found -- no random_pos found
if minetest.get_node_timer(pos):is_started() then if minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()
minetest.log("action", "timer stopped")
end end
if node.name ~= "spawners:"..mob_name.."_spawner" then if node.name ~= "spawners:"..mod_prefix.."_"..mob_name.."_spawner" then
minetest.set_node(pos, {name="spawners:"..mob_name.."_spawner"}) minetest.set_node(pos, {name="spawners:"..mod_prefix.."_"..mob_name.."_spawner"})
end end
end end
@ -376,14 +361,14 @@ for i, mob_table in ipairs(spawners.mob_tables) do
spawners.create(mob_table.name, mod_prefix, mob_table.dummy_size, mob_table.dummy_offset, mob_table.dummy_mesh, mob_table.dummy_texture, mob_table.night_only) spawners.create(mob_table.name, mod_prefix, mob_table.dummy_size, mob_table.dummy_offset, mob_table.dummy_mesh, mob_table.dummy_texture, mob_table.night_only)
-- recipes -- recipes
minetest.register_craft({ minetest.register_craft({
output = "spawners:"..mob_table.name.."_spawner", output = "spawners:"..mod_prefix.."_"..mob_table.name.."_spawner",
recipe = { recipe = {
{"default:diamondblock", "fake_fire:flint_and_steel", "default:diamondblock"}, {"default:diamondblock", "fake_fire:flint_and_steel", "default:diamondblock"},
{"xpanes:bar", "spawners:"..mob_table.name, "xpanes:bar"}, {"xpanes:bar", mod_prefix..":"..mob_table.name, "xpanes:bar"},
{"default:diamondblock", "xpanes:bar", "default:diamondblock"}, {"default:diamondblock", "xpanes:bar", "default:diamondblock"},
} }
}) })
end end
end end
print ("[MOD] Spawners loaded") print ("[MOD] Spawners 0.1 Loaded.")