add force laod cooldown

This commit is contained in:
chmodsayshello 2022-04-27 18:23:50 +00:00
parent 3e152db05a
commit 3a08ba6f78
1 changed files with 18 additions and 6 deletions

View File

@ -7,13 +7,25 @@ mcl_compass = {}
local compass_frames = 32
local function get_far_node(pos) --code from minetest dev wiki: https://dev.minetest.net/minetest.get_node
local function get_far_node(pos, itemstack) --code from minetest dev wiki: https://dev.minetest.net/minetest.get_node, some edits have been made to add a cooldown for force loads
local node = minetest.get_node(pos)
if node.name == "ignore" then
minetest.get_voxel_manip():read_from_map(pos, pos)
node = minetest.get_node(pos)
tstamp = tonumber(itemstack:get_meta():get_string("last_forceload"))
if tstamp == nil then --this is only relevant for new lodestone compasses, the ones that have never performes a forceload yet
itemstack:get_meta():set_string("last_forceload", tostring(os.time(os.date("!*t"))))
tstamp = tonumber(os.time(os.date("!*t")))
end
if tonumber(os.time(os.date("!*t"))) - tstamp > 180 then --current time in secounds - old time in secounds, if it is over 180 (3 mins): forceload
itemstack:get_meta():set_string("last_forceload", tostring(os.time(os.date("!*t"))))
minetest.get_voxel_manip():read_from_map(pos, pos)
node = minetest.get_node(pos)
minetest.log("forceloaded, cooldown reset!")
else
minetest.log("cooldown not over yet")
node = {name="mcl_compass:lodestone"} --cooldown not over yet, pretend like there is something...
end
end
return node
return node
end
@ -40,7 +52,7 @@ function mcl_compass.get_compass_image(pos, dir, itemstack)
if dim == playerdim then --Check if player and compass target are in the same dimension, above check is just if the diemension is valid for the non lodestone compass
if get_far_node(lodestone_pos).name == "mcl_compass:lodestone" then --check if lodestone still exists
if get_far_node(lodestone_pos, itemstack).name == "mcl_compass:lodestone" then --check if lodestone still exists
local angle_north = math.deg(math.atan2(lodestone_pos.x - pos.x, lodestone_pos.z - pos.z))
if angle_north < 0 then angle_north = angle_north + 360 end
local angle_dir = -math.deg(dir)
@ -99,6 +111,7 @@ minetest.register_globalstep(function(dtime)
local compass_image = mcl_compass.get_compass_image(pos, player:get_look_horizontal(), stack)
if minetest.get_item_group(stack:get_name(), "compass")-1 ~= compass_image and minetest.get_item_group(stack:get_name(), "compass")-1 .. "_lodestone" ~=compass_image then --Explaination: First check for normal compasses, secound check for lodestone ones
local itemname = "mcl_compass:"..compass_image
--minetest.log(os.time(os.date("!*t")))
stack:set_name(itemname)
player:get_inventory():set_stack("main", j, stack)
end
@ -191,7 +204,6 @@ minetest.register_node("mcl_compass:lodestone",{
if itemstack.get_name(itemstack).match(itemstack.get_name(itemstack),"mcl_compass:") then
if itemstack.get_name(itemstack) ~= "mcl_compass:lodestone" then
itemstack:get_meta():set_string("pointsto", minetest.pos_to_string(pos))
end
end
end,