fun_caves/abms.lua
2017-01-24 19:54:46 -06:00

639 lines
16 KiB
Lua

-- Fun_Caves abms.lua
-- Copyright Duane Robertson (duane@duanerobertson.com), 2017
-- Distributed under the LGPLv2.1 (https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html)
-------------------------------------------------------------------
-- Abms
-------------------------------------------------------------------
-- player surface damage
local dps_delay = 3
local last_dps_check = 0
local cold_delay = 5
local dps_count = cold_delay
local mushrooms = {"flowers:mushroom_brown", "flowers:mushroom_red"}
local time_factor = (fun_caves_mod.time_factor or 10)
local light_max = (fun_caves_mod.light_max or 10)
-- fungal tree nodes
local fungal_tree_leaves = {}
for i = 1, 4 do
fungal_tree_leaves[#fungal_tree_leaves+1] = "fun_caves:fungal_tree_leaves_"..i
end
local is_fungal_leaf = {}
for _, leaf in ipairs(fungal_tree_leaves) do
is_fungal_leaf[leaf] = true
end
local fungal_nodes = {}
for _, leaf in pairs(fungal_tree_leaves) do
fungal_nodes[#fungal_nodes+1] = {name = leaf}
end
fungal_nodes[#fungal_nodes+1] = {name = 'fun_caves:fungal_tree_fruit'}
local fungi_to_explode = {} -- see below
local exploding_fungi = fun_caves_mod.exploding_fungi
if not exploding_fungi then
exploding_fungi = {}
end
-- hot spike parameters
local spike_air = {}
spike_air['default:lava_source'] = true
spike_air['default:lava_source'] = true
spike_air['default:lava_flowing'] = true
local spike_soil = {}
spike_soil['fun_caves:hot_cobble'] = true
spike_soil['fun_caves:black_sand'] = true
------------------------------------------------------------
-- all the fun_caves globalstep functions
------------------------------------------------------------
local hot_stuff = {"group:surface_hot"}
local cold_stuff = {"group:surface_cold"}
local poison_stuff = {"group:poison"}
local minetest_find_nodes_in_area = minetest.find_nodes_in_area
minetest.register_globalstep(function(dtime)
if not (dtime and type(dtime) == 'number') then
return
end
local time = minetest.get_gametime()
if not (time and type(time) == 'number') then
return
end
-- Execute only after an interval.
if not last_dps_check or time - last_dps_check < dps_delay then
return
end
local players = minetest.get_connected_players()
if not (players and type(players) == 'table') then
return
end
for i = 1, #players do
local player = players[i]
local pos = player:getpos()
pos = vector.round(pos)
local player_name = player:get_player_name()
-- environmental damage
if fun_caves_mod.DEBUG and player:get_hp() < 20 then
-- Regenerate the player while testing.
print("HP: "..player:get_hp())
player:set_hp(20)
return
else
local minp = vector.subtract(pos, 0.5)
local maxp = vector.add(pos, 0.5)
-- ... from standing on or near hot objects.
local counts = minetest_find_nodes_in_area(minp, maxp, hot_stuff)
if not (counts and type(counts) == 'table') then
return
end
if #counts > 1 then
player:set_hp(player:get_hp() - 1)
end
-- ... from standing on or near poison.
local counts = minetest_find_nodes_in_area(minp, maxp, poison_stuff)
if not (counts and type(counts) == 'table') then
return
end
if #counts > 1 then
player:set_hp(player:get_hp() - 1)
end
-- ... from standing on or near cold objects (less often).
if dps_count % cold_delay == 0 then
counts = minetest_find_nodes_in_area(minp, maxp, cold_stuff)
if not (counts and type(counts) == 'table') then
return
end
if #counts > 1 then
player:set_hp(player:get_hp() - 1)
end
end
end
end
-- Set this outside of the player loop, to affect everyone.
if dps_count % cold_delay < 1 then
dps_count = cold_delay
end
last_dps_check = minetest.get_gametime()
if not (last_dps_check and type(last_dps_check) == 'number') then
last_dps_check = 0
end
dps_count = dps_count - 1
end)
------------------------------------------------------------
-- destruction
------------------------------------------------------------
minetest.register_abm({
nodenames = {"fun_caves:hot_cobble",},
neighbors = {"group:water"},
interval = time_factor,
chance = 30,
catch_up = false,
action = function(pos, node)
if not (pos and node) then
return
end
minetest.set_node(pos, {name = "default:cobble"})
minetest.sound_play("default_cool_lava",
{pos = pos, max_hear_distance = 16, gain = 0.25})
end,
})
-- Exploding fungal fruit
minetest.register_abm({
nodenames = {"fun_caves:fungal_tree_fruit"},
interval = 12 * time_factor,
chance = 400,
catch_up = false,
action = function(pos, node)
if not (pos and node) then
return
end
fun_caves_mod.soft_boom(pos)
end
})
-- Destroy mushroom caps in the light.
minetest.register_abm({
nodenames = {"fun_caves:giant_mushroom_cap", "fun_caves:huge_mushroom_cap"},
interval = 3 * time_factor,
chance = 200,
action = function(pos, node)
if not (pos and node) then
return
end
if (minetest.get_node_light(pos, nil) or 99) >= light_max + 2 then
minetest.remove_node(pos)
return
end
end
})
--minetest.register_abm({
-- nodenames = {"fire:basic_flame"},
-- interval = 2 * time_factor,
-- chance = 50,
-- action = function(p0, node, _, _)
-- minetest.remove_node(p0)
-- end,
--})
------------------------------------------------------------
-- creation
------------------------------------------------------------
-- fungal spread
minetest.register_abm({
nodenames = fungal_tree_leaves,
neighbors = {"air", "group:liquid"},
interval = 4 * time_factor,
chance = 200,
catch_up = false,
action = function(pos, node)
if not (pos and node) then
return
end
if (minetest.get_node_light(pos, nil) or 99) >= light_max + 2 then
minetest.remove_node(pos)
return
end
if math.random(40) == 1 then
minetest.set_node(pos, fungal_nodes[#fungal_nodes])
return
end
if math.random(100) == 1 then
minetest.set_node(pos, fungal_nodes[math.random(#fungal_nodes - 1)])
return
end
pos.y = pos.y - 1
local grow_node = minetest.get_node_or_nil(pos)
if grow_node and grow_node.name == "air" then
minetest.set_node(pos, node)
return
end
pos.x = pos.x + math.random(-1, 1)
pos.y = pos.y + math.random(-1, 1) + 1
pos.z = pos.z + math.random(-1, 1)
grow_node = minetest.get_node_or_nil(pos)
if grow_node and grow_node.name == "air" and (minetest.get_node_light(pos, nil) or 99) <= light_max then
minetest.set_node(pos, node)
return
elseif grow_node and is_fungal_leaf[grow_node.name] and grow_node.name ~= node.name then
minetest.remove_node(pos)
return
end
end
})
-- mushroom growth -- caps regenerate in time
local huge_mushroom_cap_node = {name = 'fun_caves:huge_mushroom_cap'}
minetest.register_abm({
nodenames = {"fun_caves:giant_mushroom_stem"},
neighbors = {'air'},
interval = 4 * time_factor,
chance = 200,
action = function(pos, node)
if not (pos and node) then
return
end
pos.y = pos.y + 1
local node_up = minetest.get_node_or_nil(pos)
if not node_up or node_up.name ~= "air" then
return
end
if (minetest.get_node_light(pos, nil) or 99) <= light_max then
minetest.set_node(pos, huge_mushroom_cap_node)
end
end
})
-- new fungi
local mushroom_nodes = {}
for _, mushroom in pairs(mushrooms) do
mushroom_nodes[#mushroom_nodes+1] = {name = mushroom}
end
minetest.register_abm({
nodenames = {"default:dirt"},
neighbors = {'fun_caves:stone_with_lichen', 'fun_caves:stone_with_algae', 'fun_caves:giant_mushroom_stem'},
interval = 10 * time_factor,
chance = 500,
catch_up = false,
action = function(pos, node)
if not (pos and node and pos.y < 0) then
return
end
pos.y = pos.y + 1
local grow_node = minetest.get_node_or_nil(pos)
if grow_node and grow_node.name == "air"
and (minetest.get_node_light(pos, nil) or 99) <= light_max then
if math.random(4) == 1 then
minetest.set_node(pos, fungal_nodes[math.random(#fungal_nodes - 1)])
else
minetest.set_node(pos, mushroom_nodes[math.random(#mushroom_nodes)])
end
end
end
})
-- mushroom growth -- small into huge
local giant_mushroom_stem_node = {name = 'fun_caves:giant_mushroom_stem'}
minetest.register_abm({
nodenames = mushrooms,
interval = 20 * time_factor,
chance = 700,
catch_up = false,
action = function(pos, node)
if not (pos and node and pos.y < 0) then
return
end
local pos_up = {x=pos.x,y=pos.y+1,z=pos.z}
local node_up = minetest.get_node_or_nil(pos_up)
if not node_up or node_up.name ~= "air" then
return
end
local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
if not node_under
or minetest.get_item_group(node_under.name, "soil") == 0
or (minetest.get_node_light(pos_up, nil) or 99) > light_max then
return
end
minetest.set_node(pos_up, huge_mushroom_cap_node)
minetest.set_node(pos, giant_mushroom_stem_node)
end
})
-- mushroom growth -- huge into giant
local giant_mushroom_cap_node = {name = "fun_caves:giant_mushroom_cap"}
minetest.register_abm({
nodenames = {"fun_caves:huge_mushroom_cap"},
interval = 40 * time_factor,
chance = 2000,
catch_up = false,
action = function(pos, node)
if not (pos and node) then
return
end
local pos_up = {x=pos.x,y=pos.y+1,z=pos.z}
local node_up = minetest.get_node_or_nil(pos_up)
if not node_up or node_up.name ~= "air" then
return
end
-- Check for soil.
local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 2, z = pos.z})
if not node_under
or minetest.get_item_group(node_under.name, "soil") == 0
or (minetest.get_node_light(pos_up, nil) or 99) > light_max then
return
end
minetest.set_node(pos_up, giant_mushroom_cap_node)
minetest.set_node(pos, {name = "fun_caves:giant_mushroom_stem"})
end
})
-- Spike spread and death
minetest.register_abm({
nodenames = fun_caves_mod.hot_spikes,
interval = 40 * time_factor,
chance = 2000,
action = function(pos, node)
if not (pos and node and pos.y < 0) then
return
end
if not (fun_caves_mod.hot_spike and fun_caves_mod.hot_spikes) then
return
end
local spike_num = fun_caves_mod.hot_spike[node.name]
if not spike_num then
return
end
if spike_num < #fun_caves_mod.hot_spikes then
minetest.set_node(pos, {name=fun_caves_mod.hot_spikes[spike_num+1]})
return
end
pos.x = pos.x + math.random(-2, 2)
pos.y = pos.y + math.random(-1, 1)
pos.z = pos.z + math.random(-2, 2)
local new_node = minetest.get_node_or_nil(pos)
if not (new_node and spike_air[new_node.name]) then
return
end
local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
if not (node_under and spike_soil[node_under.name]) then
return
end
minetest.set_node(pos, {name = hot_spikes[1]})
end
})
------------------------------------------------------------
-- explosive functions
------------------------------------------------------------
-- All of this is copied from TNT, but modified to leave stone intact.
-- Fill a list with data for content IDs, after all nodes are registered
local cid_data = {}
minetest.after(0, function()
for name, def in pairs(minetest.registered_nodes) do
cid_data[minetest.get_content_id(name)] = {
name = name,
--drops = def.drops,
flammable = def.groups.flammable,
choppy = def.groups.choppy,
fleshy = def.groups.fleshy,
snappy = def.groups.snappy,
on_blast = def.on_blast,
}
end
-- This also has to be done after content ids are established.
for i = 1, 4 do
fungi_to_explode[minetest.get_content_id("fun_caves:fungal_tree_leaves_"..i)] = true
end
fungi_to_explode[minetest.get_content_id('fun_caves:fungal_tree_fruit')] = true
fungi_to_explode[minetest.get_content_id('fun_caves:giant_mushroom_cap')] = true
fungi_to_explode[minetest.get_content_id('fun_caves:giant_mushroom_stem')] = true
fungi_to_explode[minetest.get_content_id('fun_caves:huge_mushroom_cap')] = true
end)
local function add_effects(pos, radius)
if not (pos and radius and type(radius) == 'number') then
return
end
minetest.add_particlespawner({
amount = 128,
time = 1,
minpos = vector.subtract(pos, radius / 2),
maxpos = vector.add(pos, radius / 2),
minvel = {x=-20, y=-20, z=-20},
maxvel = {x=20, y=20, z=20},
minacc = vector.new(),
maxacc = vector.new(),
minexptime = 1,
maxexptime = 3,
minsize = 8,
maxsize = 16,
texture = "tnt_smoke.png",
})
end
local function destroy(pos, cid)
if not (pos and cid) then
return
end
local def = cid_data[cid]
if not (def and def.flammable and def.flammable > 0 and not minetest.is_protected(pos, "")) then
return
end
if def.on_blast then
def.on_blast(vector.new(pos), 1)
return
end
if def.snappy == nil and def.choppy == nil and def.fleshy == nil and def.name ~= "fire:basic_flame" then
return
end
if exploding_fungi or fungi_to_explode[cid] then
minetest.remove_node(pos)
end
end
local math_random = math.random
local function explode(pos, radius)
if not (pos and radius and type(radius) == 'number') then
return
end
local pos = vector.round(pos)
local vm = VoxelManip()
local p1 = vector.subtract(pos, radius)
local p2 = vector.add(pos, radius)
local minp, maxp = vm:read_from_map(p1, p2)
local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
local data = vm:get_data()
local p = {}
local c_air = minetest.get_content_id("air")
for z = -radius, radius do
for y = -radius, 4*radius do
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) + math_random(-radius, radius) then
local cid = data[vi]
p.x = pos.x + x
p.y = pos.y + y
p.z = pos.z + z
if cid ~= c_air then
destroy(p, cid)
end
end
vi = vi + 1
end
end
end
end
local function calc_velocity(pos1, pos2, old_vel, power)
if not (pos1 and pos2 and old_vel and power) then
return
end
local vel = vector.direction(pos1, pos2)
vel = vector.normalize(vel)
vel = vector.multiply(vel, power)
-- Divide by distance
local dist = vector.distance(pos1, pos2)
dist = math.max(dist, 1)
vel = vector.divide(vel, dist)
-- Add old velocity
vel = vector.add(vel, old_vel)
return vel
end
local function entity_physics(pos, radius)
if not (pos and radius and type(radius) == 'number') then
return
end
-- Make the damage radius larger than the destruction radius
radius = radius * 2
local objs = minetest.get_objects_inside_radius(pos, radius)
if not (objs and type(objs) == 'table') then
return
end
local math_max = math.max
local vector_distance = vector.distance
for _, obj in pairs(objs) do
local obj_pos = obj:getpos()
local obj_vel = obj:getvelocity()
local dist = math_max(1, vector_distance(pos, obj_pos))
if obj_vel ~= nil then
obj:setvelocity(calc_velocity(pos, obj_pos,
obj_vel, radius * 10))
end
local damage = (4 / dist) * radius
obj:set_hp(obj:get_hp() - damage)
end
end
fun_caves_mod.soft_boom = function(pos)
if not pos then
return
end
local node = minetest.get_node_or_nil(pos)
if not node then
return
end
if exploding_fungi then
minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
end
local radius = 5
minetest.remove_node(pos)
explode(pos, radius)
if exploding_fungi then
entity_physics(pos, radius)
add_effects(pos, radius)
end
end
-----------------------------------------------
-- testing only -- remove before distribution
-----------------------------------------------
-- Mushroom spread and death
--minetest.register_abm({
-- nodenames = mushrooms,
-- interval = 1 * time_factor,
-- chance = 50,
-- action = function(pos, node)
-- if minetest.get_node_light(pos, nil) >= light_max + 2 then
-- minetest.remove_node(pos)
-- return
-- end
-- local random = {
-- x = pos.x + math.random(-2, 2),
-- y = pos.y + math.random(-1, 1),
-- z = pos.z + math.random(-2, 2)
-- }
-- local random_node = minetest.get_node_or_nil(random)
-- if not random_node or random_node.name ~= "air" then
-- return
-- end
-- 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 (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) <= light_max and
-- minetest.get_node_light(random, 0.5) <= light_max then
-- minetest.set_node(random, {name = node.name})
-- end
-- end
--})