1
0
mirror of https://github.com/minetest/minetest_game.git synced 2024-10-31 23:30:31 +01:00
minetest_game/mods/bones/init.lua

292 lines
8.1 KiB
Lua
Raw Normal View History

-- bones/init.lua
2013-05-18 16:06:57 +02:00
-- Minetest 0.4 mod: bones
-- See README.txt for licensing and other information.
2014-12-24 20:54:24 +01:00
-- Load support for MT game translation.
local S = minetest.get_translator("bones")
bones = {}
2013-05-18 16:06:57 +02:00
local function is_owner(pos, name)
2013-05-25 00:40:03 +02:00
local owner = minetest.get_meta(pos):get_string("owner")
if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then
2013-05-18 16:06:57 +02:00
return true
end
return false
end
local bones_formspec =
"size[8,9]" ..
"list[current_name;main;0,0.3;8,4;]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[current_name;main]" ..
"listring[current_player;main]" ..
2014-12-24 20:54:24 +01:00
default.get_hotbar_bg(0,4.85)
2017-05-22 15:41:17 +02:00
local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200
local share_bones_time_early = tonumber(minetest.settings:get("share_bones_time_early")) or share_bones_time / 4
2013-05-18 16:06:57 +02:00
minetest.register_node("bones:bones", {
description = S("Bones"),
2013-05-18 16:06:57 +02:00
tiles = {
"bones_top.png^[transform2",
2013-05-18 16:06:57 +02:00
"bones_bottom.png",
"bones_side.png",
"bones_side.png",
"bones_rear.png",
"bones_front.png"
},
paramtype2 = "facedir",
groups = {dig_immediate = 2},
sounds = default.node_sound_gravel_defaults(),
2013-05-18 16:06:57 +02:00
can_dig = function(pos, player)
2013-05-25 00:40:03 +02:00
local inv = minetest.get_meta(pos):get_inventory()
2016-02-26 00:05:23 +01:00
local name = ""
if player then
name = player:get_player_name()
end
return is_owner(pos, name) and inv:is_empty("main")
2013-05-18 16:06:57 +02:00
end,
2013-05-18 16:06:57 +02:00
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
if is_owner(pos, player:get_player_name()) then
return count
end
return 0
end,
2013-05-18 16:06:57 +02:00
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
return 0
end,
2013-05-18 16:06:57 +02:00
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if is_owner(pos, player:get_player_name()) then
return stack:get_count()
end
return 0
end,
2013-05-18 16:06:57 +02:00
on_metadata_inventory_take = function(pos, listname, index, stack, player)
2013-05-25 00:40:03 +02:00
local meta = minetest.get_meta(pos)
if meta:get_inventory():is_empty("main") then
local inv = player:get_inventory()
if inv:room_for_item("main", {name = "bones:bones"}) then
inv:add_item("main", {name = "bones:bones"})
else
minetest.add_item(pos, "bones:bones")
end
minetest.remove_node(pos)
2013-05-18 16:06:57 +02:00
end
end,
on_punch = function(pos, node, player)
if not is_owner(pos, player:get_player_name()) then
return
end
if minetest.get_meta(pos):get_string("infotext") == "" then
return
end
local inv = minetest.get_meta(pos):get_inventory()
local player_inv = player:get_inventory()
local has_space = true
for i = 1, inv:get_size("main") do
local stk = inv:get_stack("main", i)
if player_inv:room_for_item("main", stk) then
inv:set_stack("main", i, nil)
player_inv:add_item("main", stk)
else
has_space = false
break
end
end
-- remove bones if player emptied them
if has_space then
if player_inv:room_for_item("main", {name = "bones:bones"}) then
player_inv:add_item("main", {name = "bones:bones"})
else
minetest.add_item(pos,"bones:bones")
end
minetest.remove_node(pos)
end
end,
2013-05-18 16:06:57 +02:00
on_timer = function(pos, elapsed)
2013-05-25 00:40:03 +02:00
local meta = minetest.get_meta(pos)
local time = meta:get_int("time") + elapsed
if time >= share_bones_time then
meta:set_string("infotext", S("@1's old bones", meta:get_string("owner")))
2013-05-18 16:06:57 +02:00
meta:set_string("owner", "")
else
meta:set_int("time", time)
2013-05-18 16:06:57 +02:00
return true
end
end,
on_blast = function(pos)
end,
2013-05-18 16:06:57 +02:00
})
local function may_replace(pos, player)
local node_name = minetest.get_node(pos).name
local node_definition = minetest.registered_nodes[node_name]
-- if the node is unknown, we return false
if not node_definition then
return false
end
-- allow replacing air
if node_name == "air" then
return true
end
-- don't replace nodes inside protections
if minetest.is_protected(pos, player:get_player_name()) then
return false
end
-- allow replacing liquids
if node_definition.liquidtype ~= "none" then
return true
end
-- don't replace filled chests and other nodes that don't allow it
local can_dig_func = node_definition.can_dig
if can_dig_func and not can_dig_func(pos, player) then
return false
end
-- default to each nodes buildable_to; if a placed block would replace it, why shouldn't bones?
-- flowers being squished by bones are more realistical than a squished stone, too
return node_definition.buildable_to
end
local drop = function(pos, itemstack)
local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
if obj then
2018-07-01 21:44:03 +02:00
obj:set_velocity({
x = math.random(-10, 10) / 9,
y = 5,
z = math.random(-10, 10) / 9,
})
end
end
local player_inventory_lists = { "main", "craft" }
bones.player_inventory_lists = player_inventory_lists
local function is_all_empty(player_inv)
for _, list_name in ipairs(player_inventory_lists) do
if not player_inv:is_empty(list_name) then
return false
end
end
return true
end
2013-05-18 16:06:57 +02:00
minetest.register_on_dieplayer(function(player)
2017-05-22 15:41:17 +02:00
local bones_mode = minetest.settings:get("bones_mode") or "bones"
if bones_mode ~= "bones" and bones_mode ~= "drop" and bones_mode ~= "keep" then
bones_mode = "bones"
end
local bones_position_message = minetest.settings:get_bool("bones_position_message") == true
local player_name = player:get_player_name()
local pos = vector.round(player:get_pos())
local pos_string = minetest.pos_to_string(pos)
-- return if keep inventory set or in creative mode
if bones_mode == "keep" or minetest.is_creative_enabled(player_name) then
minetest.log("action", player_name .. " dies at " .. pos_string ..
". No bones placed")
if bones_position_message then
minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
end
2013-05-18 16:06:57 +02:00
return
end
local player_inv = player:get_inventory()
if is_all_empty(player_inv) then
minetest.log("action", player_name .. " dies at " .. pos_string ..
". No bones placed")
if bones_position_message then
minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
end
return
end
-- check if it's possible to place bones, if not find space near player
if bones_mode == "bones" and not may_replace(pos, player) then
local air = minetest.find_node_near(pos, 1, {"air"})
if air and not minetest.is_protected(air, player_name) then
pos = air
else
bones_mode = "drop"
end
end
if bones_mode == "drop" then
for _, list_name in ipairs(player_inventory_lists) do
for i = 1, player_inv:get_size(list_name) do
drop(pos, player_inv:get_stack(list_name, i))
end
player_inv:set_list(list_name, {})
2013-05-18 16:06:57 +02:00
end
drop(pos, ItemStack("bones:bones"))
minetest.log("action", player_name .. " dies at " .. pos_string ..
". Inventory dropped")
if bones_position_message then
minetest.chat_send_player(player_name, S("@1 died at @2, and dropped their inventory.", player_name, pos_string))
end
return
2013-05-18 16:06:57 +02:00
end
local param2 = minetest.dir_to_facedir(player:get_look_dir())
minetest.set_node(pos, {name = "bones:bones", param2 = param2})
minetest.log("action", player_name .. " dies at " .. pos_string ..
". Bones placed")
if bones_position_message then
minetest.chat_send_player(player_name, S("@1 died at @2, and bones were placed.", player_name, pos_string))
end
2013-05-25 00:40:03 +02:00
local meta = minetest.get_meta(pos)
2013-05-18 16:06:57 +02:00
local inv = meta:get_inventory()
inv:set_size("main", 8 * 4)
for _, list_name in ipairs(player_inventory_lists) do
for i = 1, player_inv:get_size(list_name) do
local stack = player_inv:get_stack(list_name, i)
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
else -- no space left
drop(pos, stack)
end
end
player_inv:set_list(list_name, {})
2013-05-18 16:06:57 +02:00
end
meta:set_string("formspec", bones_formspec)
meta:set_string("owner", player_name)
if share_bones_time ~= 0 then
meta:set_string("infotext", S("@1's fresh bones", player_name))
if share_bones_time_early == 0 or not minetest.is_protected(pos, player_name) then
meta:set_int("time", 0)
else
meta:set_int("time", (share_bones_time - share_bones_time_early))
end
minetest.get_node_timer(pos):start(10)
else
meta:set_string("infotext", S("@1's bones", player_name))
end
2013-05-18 16:06:57 +02:00
end)