Files
spacesuit/drowning.lua
coil 4e3d5ac072 Wear spacesuit accounting for server lag
This makes spacesuit wear independent from server lag. The timer
between updates should average one second. If it is more,
multiplying by the timer value increases wear as if an update had
happened at every additional step (except the wear value would be
rounded to an integer between 0 and 65535 every time).

Because the real average is slightly higher than one second,
multiplying by the timer value decreases how long the spacesuit
lasts. With one additional step per update and steps of
0.034 seconds (the average time I get in singleplayer), that means
2 or 3 additional wear. So I also decrease armor_use to 70, which
restores the spacesuit to lasting as long as before or slightly
longer.

Also, this allows increasing the minimum timer value to two seconds
or more without needing to change the armor_use value.
2019-06-24 18:58:34 -04:00

49 lines
1.4 KiB
Lua

local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime;
if timer >= 1 then
local t0 = minetest.get_us_time()
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local name, armor_inv = armor.get_valid_player(armor, player, "[spacesuit]")
local has_helmet = armor_inv:contains_item("armor", "spacesuit:helmet")
local has_chestplate = armor_inv:contains_item("armor", "spacesuit:chestplate")
local has_pants = armor_inv:contains_item("armor", "spacesuit:pants")
local has_boots = armor_inv:contains_item("armor", "spacesuit:boots")
local has_full_suit = has_helmet and has_chestplate and has_pants and has_boots
local armor_list = armor_inv:get_list("armor")
-- does the player wear a suit?
spacesuit.set_player_wearing(player, has_full_suit, has_helmet, armor_list)
if has_full_suit and player:get_breath() < 10 then
for i, stack in pairs(armor_inv:get_list("armor")) do
if not stack:is_empty() then
local name = stack:get_name()
local use = minetest.get_item_group(name, "armor_use") * timer or 0
armor:damage(player, i, stack, use)
end
end
player:set_breath(10)
end
end
timer = 0
local t1 = minetest.get_us_time()
local diff = t1 - t0
if diff > 10000 then
minetest.log("warning", "[spacesuit] update took " .. diff .. " us")
end
end
end)