dig penalty

This commit is contained in:
Andrey Stepanov 2025-01-25 23:22:03 +05:00
parent 23e235c6b9
commit d920f8e9d0
Signed by: Koldun
GPG Key ID: 53DE683337F5D25F
3 changed files with 124 additions and 1 deletions

122
engine.lua Normal file
View File

@ -0,0 +1,122 @@
--[[
Copyright 2025 Andrey Stepanov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
local storage = minetest.get_mod_storage()
local dig_penalty_storage = {}
if storage:contains("dig_penalty") then
dig_penalty_storage = minetest.deserialize(
storage:get_string("dig_penalty")
)
end
---@param dig_penalty number
local function set_dig_penalty(player, dig_penalty)
local pname = player:get_player_name()
local prev_dig_penalty = dig_penalty_storage[pname] or 1
if dig_penalty == prev_dig_penalty then
return
end
dig_penalty_storage[pname] = dig_penalty
storage:set_string(
"dig_penalty",
minetest.serialize(
dig_penalty_storage
)
)
local inv = player:get_inventory()
local inv_size = inv:get_size("main")
for i = 1, inv_size do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then
toolcap_monoids.dig_speed:del_change(stack, "medical")
if dig_penalty ~= 1 then
toolcap_monoids.dig_speed:add_change(stack, dig_penalty, "medical")
end
inv:set_stack("main", i, stack)
end
end
hand_monoid.monoid:del_change(player, "medical")
local hand_caps = table.copy(hand_monoid.monoid_def.identity.groupcaps)
if dig_penalty ~= 1 then
for group, cap in pairs(hand_caps) do
if cap.times then
for i, _ in pairs(cap.times) do
cap.times[i] = cap.times[i] * dig_penalty
end
end
end
hand_monoid.monoid:add_change(
player,
{
groupcaps = hand_caps
},
"medical"
)
end
end
minetest.register_on_newplayer(
function(player)
hand_monoid.monoid:add_change(
player,
{
groupcaps = table.copy(hand_monoid.monoid_def.identity.groupcaps)
},
"medical"
)
end
)
minetest.register_on_player_inventory_action(
function(
player,
action,
inventory,
inventory_info
)
if action == "put" then
local stack = inventory_info.stack
local dig_penalty = dig_penalty_storage[player:get_player_name()] or 1
if dig_penalty ~= 1 then
toolcap_monoids.dig_speed:add_change(stack, dig_penalty, "medical")
end
elseif action == "take" then
local stack = inventory_info.stack
toolcap_monoids.dig_speed:del_change(stack, "medical")
elseif action == "move" then
if (inventory_info.to_list == "main") and (inventory_info.from_list ~= "main") then
local stack = inventory:get_stack("main", inventory_info.to_index)
toolcap_monoids.dig_speed:del_change(stack, "medical")
local dig_penalty = dig_penalty_storage[player:get_player_name()] or 1
if dig_penalty ~= 1 then
toolcap_monoids.dig_speed:add_change(stack, dig_penalty, "medical")
end
inventory:set_stack("main", inventory_info.to_index, stack)
elseif (inventory_info.from_list == "main") and (inventory_info.to_list ~= "main") then
local stack = inventory:get_stack(inventory_info.to_list, inventory_info.to_index)
toolcap_monoids.dig_speed:del_change(stack, "medical")
inventory:set_stack(inventory_info.to_list, inventory_info.to_index, stack)
end
end
end
)

View File

@ -17,4 +17,5 @@ limitations under the License.
medical = {}
local MP = minetest.get_modpath("medical")
dofile(MP .. "/engine.lua")
dofile(MP .. "/gui.lua")

View File

@ -1,3 +1,3 @@
name = medical
description = Health and nutrition
depends = unified_inventory
depends = unified_inventory, toolcap_monoids, hand_monoid