Remove local functions.

This commit is contained in:
Duane 2016-06-14 02:18:21 -05:00
parent 3f0b5d08b2
commit e5cd050121
9 changed files with 157 additions and 209 deletions

180
abms.lua
View file

@ -10,26 +10,12 @@ local dps_count = hunger_delay
-- maximum number of mobs near player in fortresses
local fortress_mob_count = 5
local get_us_time = minetest.get_us_time
local floor = math.floor
local abs = math.abs
local max = math.max
local min = math.min
local rand = math.random
local mushrooms = {"flowers:mushroom_brown", "flowers:mushroom_red"}
local get_node_light = minetest.get_node_light
local remove_node = minetest.remove_node
local set_node = minetest.set_node
local get_node_or_nil = minetest.get_node_or_nil
local get_connected_players = minetest.get_connected_players
local find_nodes_in_area = minetest.find_nodes_in_area
local get_item_group = minetest.get_item_group
local find_nodes_in_area_under_air = minetest.find_nodes_in_area_under_air
local hunger_mod = minetest.get_modpath("hunger")
minetest.register_globalstep(function(dtime)
local time = get_us_time()
local time = minetest.get_us_time()
if last_dps_check and time - last_dps_check < dps_delay then
return
@ -40,13 +26,13 @@ minetest.register_globalstep(function(dtime)
if not mob.initial_promotion then
local pos = mob.object:getpos()
if mob.hp_max and mob.object and mob.health and mob.damage then
local factor = 1 + (max(abs(pos.x), abs(pos.y), abs(pos.z)) / 6200)
local factor = 1 + (math.max(math.abs(pos.x), math.abs(pos.y), math.abs(pos.z)) / 6200)
if fun_caves.is_fortress(pos) then
mob.started_in_fortress = true
factor = factor * 1.5
end
mob.hp_max = floor(mob.hp_max * factor)
mob.damage = floor(mob.damage * factor)
mob.hp_max = math.floor(mob.hp_max * factor)
mob.damage = math.floor(mob.damage * factor)
--print("Promoting "..mob.name..": "..mob.hp_max.." at "..pos.x..","..pos.y..","..pos.z)
mob.object:set_hp(mob.hp_max)
mob.health = mob.hp_max
@ -57,7 +43,7 @@ minetest.register_globalstep(function(dtime)
end
-- Spawn mobs in fortresses -- only when a player is near
local players = get_connected_players()
local players = minetest.get_connected_players()
for i = 1, #players do
local player = players[i]
local pos = player:getpos()
@ -69,7 +55,7 @@ minetest.register_globalstep(function(dtime)
for _, mob in pairs(minetest.luaentities) do
if mob.health and mob.started_in_fortress then
local dist = vector.subtract(pos, mob.object:getpos())
local dist2 = max(abs(dist.x), abs(dist.y * 5), abs(dist.z))
local dist2 = math.max(math.abs(dist.x), math.abs(dist.y * 5), math.abs(dist.z))
if dist2 < 30 then
--print(mob.name, dist.y)
mob_count = mob_count + 1
@ -81,11 +67,11 @@ minetest.register_globalstep(function(dtime)
end
if mob_count < fortress_mob_count then
local pos1, count = find_nodes_in_area_under_air({x=pos.x-30, y=pos.y-2, z=pos.z-30}, {x=pos.x+30, y=pos.y+2, z=pos.z+30}, {"group:fortress"})
local pos1, count = minetest.find_nodes_in_area_under_air({x=pos.x-30, y=pos.y-2, z=pos.z-30}, {x=pos.x+30, y=pos.y+2, z=pos.z+30}, {"group:fortress"})
if #pos1 > 0 then
local pos2 = pos1[rand(#pos1)]
local pos2 = pos1[math.random(#pos1)]
pos2.y = pos2.y + 2
local name = fun_caves.fortress_spawns[rand(#fun_caves.fortress_spawns)]
local name = fun_caves.fortress_spawns[math.random(#fun_caves.fortress_spawns)]
local mob = minetest.add_entity(pos2, name)
if mob then
print("Fun Caves: Spawned "..name.." at ("..pos2.x..","..pos2.y..","..pos2.z..")")
@ -102,19 +88,19 @@ minetest.register_globalstep(function(dtime)
return
else
-- Environmental damage from surfaces/hunger
local counts = find_nodes_in_area(minp, maxp, {"group:surface_hot"})
local counts = minetest.find_nodes_in_area(minp, maxp, {"group:surface_hot"})
if #counts > 1 then
player:set_hp(player:get_hp() - 1)
end
-- Environmental damage from surfaces/hunger
local counts = find_nodes_in_area(minp, maxp, {"group:poison"})
local counts = minetest.find_nodes_in_area(minp, maxp, {"group:poison"})
if #counts > 1 then
player:set_hp(player:get_hp() - 1)
end
if dps_count % cold_delay == 0 then
counts = find_nodes_in_area(minp, maxp, {"group:surface_cold"})
counts = minetest.find_nodes_in_area(minp, maxp, {"group:surface_cold"})
if #counts > 1 then
player:set_hp(player:get_hp() - 1)
end
@ -132,7 +118,7 @@ minetest.register_globalstep(function(dtime)
end
end
last_dps_check = get_us_time()
last_dps_check = minetest.get_us_time()
dps_count = dps_count - 1
end)
@ -148,21 +134,21 @@ minetest.register_abm({
return
end
local pos_up = {x=pos.x,y=pos.y+1,z=pos.z}
local node_up = get_node_or_nil(pos_up)
local node_up = minetest.get_node_or_nil(pos_up)
if not node_up then
return
end
if node_up.name ~= "air" then
return
end
local node_under = get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
if not node_under then
return
end
if get_item_group(node_under.name, "soil") ~= 0 and
(get_node_light(pos_up, nil) or 99) <= fun_caves.light_max then
set_node(pos_up, {name = "fun_caves:huge_mushroom_cap"})
set_node(pos, {name = "fun_caves:giant_mushroom_stem"})
if minetest.get_item_group(node_under.name, "soil") ~= 0 and
(minetest.get_node_light(pos_up, nil) or 99) <= fun_caves.light_max then
minetest.set_node(pos_up, {name = "fun_caves:huge_mushroom_cap"})
minetest.set_node(pos, {name = "fun_caves:giant_mushroom_stem"})
end
end
})
@ -173,30 +159,30 @@ minetest.register_abm({
interval = 500 * fun_caves.time_factor,
chance = 30,
action = function(pos, node)
if get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
set_node(pos, {name = "air"})
if minetest.get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
minetest.set_node(pos, {name = "air"})
return
end
local pos_up = {x=pos.x,y=pos.y+1,z=pos.z}
local node_up = get_node_or_nil(pos_up)
local node_up = minetest.get_node_or_nil(pos_up)
if not node_up then
return
end
if node_up.name ~= "air" then
return
end
local node_under = get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
if not node_under or node_under.name ~= "fun_caves:giant_mushroom_stem" then
return
end
node_under = get_node_or_nil({x = pos.x, y = pos.y - 2, z = pos.z})
node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 2, z = pos.z})
if not node_under then
return
end
if get_item_group(node_under.name, "soil") ~= 0 and
(get_node_light(pos_up, nil) or 99) <= fun_caves.light_max then
set_node(pos_up, {name = "fun_caves:giant_mushroom_cap"})
set_node(pos, {name = "fun_caves:giant_mushroom_stem"})
if minetest.get_item_group(node_under.name, "soil") ~= 0 and
(minetest.get_node_light(pos_up, nil) or 99) <= fun_caves.light_max then
minetest.set_node(pos_up, {name = "fun_caves:giant_mushroom_cap"})
minetest.set_node(pos, {name = "fun_caves:giant_mushroom_stem"})
end
end
})
@ -208,15 +194,15 @@ minetest.register_abm({
chance = 10,
action = function(pos, node)
local pos_up = {x=pos.x,y=pos.y+1,z=pos.z}
local node_up = get_node_or_nil(pos_up)
local node_up = minetest.get_node_or_nil(pos_up)
if not node_up then
return
end
if node_up.name ~= "air" then
return
end
if (get_node_light(pos_up, nil) or 99) <= fun_caves.light_max then
set_node(pos_up, {name = "fun_caves:huge_mushroom_cap"})
if (minetest.get_node_light(pos_up, nil) or 99) <= fun_caves.light_max then
minetest.set_node(pos_up, {name = "fun_caves:huge_mushroom_cap"})
end
end
})
@ -227,20 +213,20 @@ minetest.register_abm({
interval = 15 * fun_caves.time_factor,
chance = 10,
action = function(pos, node)
if get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
set_node(pos, {name = "air"})
if minetest.get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
minetest.set_node(pos, {name = "air"})
return
end
local pos_down = pos
pos_down.y = pos_down.y - 1
local pos1, count = find_nodes_in_area_under_air(vector.subtract(pos_down, 4), vector.add(pos_down, 4), {"group:soil"})
local pos1, count = minetest.find_nodes_in_area_under_air(vector.subtract(pos_down, 4), vector.add(pos_down, 4), {"group:soil"})
if #pos1 < 1 then
return
end
local random = pos1[rand(1, #pos1)]
local random = pos1[math.random(1, #pos1)]
random.y = random.y + 1
if (get_node_light(random, nil) or 99) <= fun_caves.light_max then
set_node(random, {name = mushrooms[rand(#mushrooms)]})
if (minetest.get_node_light(random, nil) or 99) <= fun_caves.light_max then
minetest.set_node(random, {name = mushrooms[math.random(#mushrooms)]})
end
end
})
@ -297,10 +283,10 @@ minetest.register_abm({
end
local grow_pos = {x=pos.x, y=pos.y+1, z=pos.z}
local grow_node = get_node_or_nil(grow_pos)
local grow_node = minetest.get_node_or_nil(grow_pos)
if grow_node and grow_node.name == "air" then
if (get_node_light(grow_pos, nil) or 99) <= fun_caves.light_max then
set_node(grow_pos, {name = mushrooms[rand(#mushrooms)]})
if (minetest.get_node_light(grow_pos, nil) or 99) <= fun_caves.light_max then
minetest.set_node(grow_pos, {name = mushrooms[math.random(#mushrooms)]})
return
end
end
@ -319,20 +305,20 @@ minetest.register_abm({
end
if spike_num < #fun_caves.hot_spikes then
set_node(pos, {name=fun_caves.hot_spikes[spike_num+1]})
minetest.set_node(pos, {name=fun_caves.hot_spikes[spike_num+1]})
return
end
local random = {
x = pos.x + rand(-2, 2),
y = pos.y + rand(-1, 1),
z = pos.z + rand(-2, 2)
x = pos.x + math.random(-2, 2),
y = pos.y + math.random(-1, 1),
z = pos.z + math.random(-2, 2)
}
local random_node = get_node_or_nil(random)
local random_node = minetest.get_node_or_nil(random)
if not random_node or (random_node.name ~= "air" and random_node.name ~= "default:lava_source" and random_node.name ~= "default:lava_flowing") then
return
end
local node_under = get_node_or_nil({x = random.x,
local node_under = minetest.get_node_or_nil({x = random.x,
y = random.y - 1, z = random.z})
if not node_under then
return
@ -341,7 +327,7 @@ minetest.register_abm({
--print("node_under ("..random.x..","..(random.y-1)..","..random.z.."): "..node_under.name)
if node_under.name == "fun_caves:hot_cobble" or node_under.name == "fun_caves:black_sand" then
--print("setting ("..random.x..","..random.y..","..random.z.."): "..node_under.name)
set_node(random, {name = hot_spikes[1]})
minetest.set_node(random, {name = hot_spikes[1]})
end
end
})
@ -388,35 +374,35 @@ minetest.register_abm({
chance = 10,
catch_up = false,
action = function(pos, node)
if get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
remove_node(pos)
if minetest.get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
minetest.remove_node(pos)
return
end
local grow_pos = {x=pos.x, y=pos.y-1, z=pos.z}
local grow_node = get_node_or_nil(grow_pos)
local grow_node = minetest.get_node_or_nil(grow_pos)
if grow_node and grow_node.name == "air" then
set_node(grow_pos, {name = node.name})
minetest.set_node(grow_pos, {name = node.name})
return
end
grow_pos = {x=rand(-1,1)+pos.x, y=rand(-1,1)+pos.y, z=rand(-1,1)+pos.z}
grow_node = get_node_or_nil(grow_pos)
if grow_node and grow_node.name == "air" and get_node_light(grow_pos, nil) <= fun_caves.light_max then
set_node(grow_pos, {name = node.name})
grow_pos = {x=math.random(-1,1)+pos.x, y=math.random(-1,1)+pos.y, z=math.random(-1,1)+pos.z}
grow_node = minetest.get_node_or_nil(grow_pos)
if grow_node and grow_node.name == "air" and minetest.get_node_light(grow_pos, nil) <= fun_caves.light_max then
minetest.set_node(grow_pos, {name = node.name})
return
elseif grow_node and leaves[grow_node.name] and grow_node.name ~= node.name then
set_node(grow_pos, {name = 'air'})
minetest.set_node(grow_pos, {name = 'air'})
return
end
if rand(40) == 1 then
set_node(pos, {name = "fun_caves:fungal_tree_fruit"})
if math.random(40) == 1 then
minetest.set_node(pos, {name = "fun_caves:fungal_tree_fruit"})
return
end
if rand(100) == 1 then
set_node(pos, {name = fungal_tree_leaves[rand(#fungal_tree_leaves)]})
if math.random(100) == 1 then
minetest.set_node(pos, {name = fungal_tree_leaves[math.random(#fungal_tree_leaves)]})
return
end
end
@ -434,9 +420,9 @@ minetest.register_abm({
end
local grow_pos = {x=pos.x, y=pos.y+1, z=pos.z}
local grow_node = get_node_or_nil(grow_pos)
if grow_node and grow_node.name == "air" and (get_node_light(grow_pos, nil) or 99) <= fun_caves.light_max then
set_node(grow_pos, {name = fungal_tree_leaves[rand(#fungal_tree_leaves)]})
local grow_node = minetest.get_node_or_nil(grow_pos)
if grow_node and grow_node.name == "air" and (minetest.get_node_light(grow_pos, nil) or 99) <= fun_caves.light_max then
minetest.set_node(grow_pos, {name = fungal_tree_leaves[math.random(#fungal_tree_leaves)]})
return
end
end
@ -492,15 +478,15 @@ local function destroy(pos, cid)
return
end
local new = "air"
--if rand(1,2) == 1 then
--if math.random(1,2) == 1 then
if true then
local node_under = get_node_or_nil({x = pos.x,
local node_under = minetest.get_node_or_nil({x = pos.x,
y = pos.y - 1, z = pos.z})
if node_under and node_under.name ~= "air" then
--new = node.name
end
end
set_node(pos, {name=new})
minetest.set_node(pos, {name=new})
end
local function explode(pos, radius)
@ -522,7 +508,7 @@ local function explode(pos, radius)
local vi = a:index(pos.x + (-radius), pos.y + y, pos.z + z)
for x = -radius, radius do
if (x * x) + (y * y / 4) + (z * z) <=
(radius * radius) + rand(-radius, radius) then
(radius * radius) + math.random(-radius, radius) then
local cid = data[vi]
p.x = pos.x + x
p.y = pos.y + y
@ -544,7 +530,7 @@ local function calc_velocity(pos1, pos2, old_vel, power)
-- Divide by distance
local dist = vector.distance(pos1, pos2)
dist = max(dist, 1)
dist = math.max(dist, 1)
vel = vector.divide(vel, dist)
-- Add old velocity
@ -559,7 +545,7 @@ local function entity_physics(pos, radius)
for _, obj in pairs(objs) do
local obj_pos = obj:getpos()
local obj_vel = obj:getvelocity()
local dist = max(1, vector.distance(pos, obj_pos))
local dist = math.max(1, vector.distance(pos, obj_pos))
if obj_vel ~= nil then
obj:setvelocity(calc_velocity(pos, obj_pos,
@ -576,14 +562,14 @@ fun_caves.soft_boom = function(pos)
return
end
local node = get_node_or_nil(pos)
local node = minetest.get_node_or_nil(pos)
if not node then
return
end
minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
local radius = 5
set_node(pos, {name="air"})
minetest.set_node(pos, {name="air"})
explode(pos, radius)
entity_physics(pos, radius)
add_effects(pos, radius)
@ -603,30 +589,30 @@ end
-- interval = 1 * fun_caves.time_factor,
-- chance = 50,
-- action = function(pos, node)
-- if get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
-- remove_node(pos)
-- if minetest.get_node_light(pos, nil) >= default.LIGHT_MAX - 2 then
-- minetest.remove_node(pos)
-- return
-- end
-- local random = {
-- x = pos.x + rand(-2, 2),
-- y = pos.y + rand(-1, 1),
-- z = pos.z + rand(-2, 2)
-- x = pos.x + math.random(-2, 2),
-- y = pos.y + math.random(-1, 1),
-- z = pos.z + math.random(-2, 2)
-- }
-- local random_node = get_node_or_nil(random)
-- local random_node = minetest.get_node_or_nil(random)
-- if not random_node or random_node.name ~= "air" then
-- return
-- end
-- local node_under = get_node_or_nil({x = random.x,
-- local node_under = minetest.get_node_or_nil({x = random.x,
-- y = random.y - 1, z = random.z})
-- if not node_under then
-- return
-- end
--
-- if (get_item_group(node_under.name, "soil") ~= 0 or
-- get_item_group(node_under.name, "tree") ~= 0) and
-- get_node_light(pos, 0.5) <= fun_caves.light_max and
-- get_node_light(random, 0.5) <= fun_caves.light_max then
-- set_node(random, {name = node.name})
-- if (minetest.get_item_group(node_under.name, "soil") ~= 0 or
-- minetest.get_item_group(node_under.name, "tree") ~= 0) and
-- minetest.get_node_light(pos, 0.5) <= fun_caves.light_max and
-- minetest.get_node_light(random, 0.5) <= fun_caves.light_max then
-- minetest.set_node(random, {name = node.name})
-- end
-- end
--})