use globalstep instead of abm

This commit is contained in:
chmodsayshello 2022-07-23 10:18:56 +00:00
parent 10a607be7e
commit c695776105
1 changed files with 49 additions and 43 deletions

View File

@ -63,7 +63,42 @@ local function beacon_blockcheck(pos)
end
end
end
local function effect_player(effect,pos,power_level, effect_level)
local all_objects = minetest.get_objects_inside_radius(pos, (power_level+1)*10)
for _,obj2 in ipairs(all_objects) do
if obj2:is_player() then
if effect == "swiftness" then
mcl_potions.swiftness_func(obj2,effect_level,16)
return
elseif effect == "leaping" then
mcl_potions.leaping_func(obj2, effect_level, 16)
return
elseif effect == "strenght" then
mcl_potions.strength_func(obj2, effect_level, 16)
return
elseif effect == "regeneration" then
mcl_potions.regeneration_func(obj2, effect_level, 16)
return
end
end
end
end
local function globalstep_function(pos, forceexecute)
local meta = minetest.get_meta(pos)
if(meta:get_int("last-effect-cycle")+10 < os.time() or forceexecute) then
meta:set_int("last-effect-cycle", os.time())
local power_level = beacon_blockcheck(pos)
local effect_string = meta:get_string("effect")
if meta:get_int("effect_level") == 2 and power_level < 4 then
return
else
effect_player(effect_string,pos,power_level,meta:get_int("effect_level"))
end
end
end
minetest.register_node("mcl_beacons:beacon", {
@ -150,6 +185,7 @@ minetest.register_node("mcl_beacons:beacon", {
if successful then
input:take_item()
inv:set_stack("input",1,input)
globalstep_function(pos,true)--call it once outside the globalstep so the player gets the effect right after selecting it
end
end
end,
@ -171,47 +207,17 @@ function register_beaconfuel(itemstring)
table.insert(beacon_fuellist, itemstring)
end
local function effect_player(effect,pos,power_level, effect_level)
local all_objects = minetest.get_objects_inside_radius(pos, (power_level+1)*10)
for _,obj2 in ipairs(all_objects) do
if obj2:is_player() then
if effect == "swiftness" then
mcl_potions.swiftness_func(obj2,effect_level,16)
return
elseif effect == "leaping" then
mcl_potions.leaping_func(obj2, effect_level, 16)
return
elseif effect == "strenght" then
mcl_potions.strength_func(obj2, effect_level, 16)
return
elseif effect == "regeneration" then
mcl_potions.regeneration_func(obj2, effect_level, 16)
return
end
local time_since_last_beacon_interval = os.time()
minetest.register_globalstep(function(dtime)
if time_since_last_beacon_interval + 10 < os.time() then
time_since_last_beacon_interval = os.time()
for _, player in ipairs(minetest.get_connected_players()) do
local player_pos = player.get_pos(player)
local pos_list = minetest.find_nodes_in_area({x=player_pos.x-50, y=player_pos.y-50, z=player_pos.z-50}, {x=player_pos.x+50, y=player_pos.y+50, z=player_pos.z+50},"mcl_beacons:beacon")
for _, pos in ipairs(pos_list) do
globalstep_function(pos, false)
end
end
end
end
local function abm_function(pos)
local meta = minetest.get_meta(pos)
if(meta:get_int("last-effect-cycle")+10 < os.time()) then
meta:set_int("last-effect-cycle", os.time())
local power_level = beacon_blockcheck(pos)
local effect_string = meta:get_string("effect")
if meta:get_int("effect_level") == 2 and power_level < 4 then
return
else
effect_player(effect_string,pos,power_level,meta:get_int("effect_level"))
end
end
end
minetest.register_abm{
label = "beacon check & apply effect(s)",
nodenames = {"mcl_beacons:beacon"},
interval = 1,
chance = 1,
action = abm_function,
}
end)