init.lua 6.83 KB
Newer Older
PilzAdam's avatar
PilzAdam committed
1
-- Minetest 0.4 mod: bones
2
-- See README.txt for licensing and other information.
Craig Davison's avatar
Craig Davison committed
3

PilzAdam's avatar
PilzAdam committed
4
local function is_owner(pos, name)
5
	local owner = minetest.get_meta(pos):get_string("owner")
6
	if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then
PilzAdam's avatar
PilzAdam committed
7
8
9
10
11
		return true
	end
	return false
end

12
local bones_formspec =
13
14
15
16
17
18
19
20
21
	"size[8,9]" ..
	default.gui_bg ..
	default.gui_bg_img ..
	default.gui_slots ..
	"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]" ..
Craig Davison's avatar
Craig Davison committed
22
23
	default.get_hotbar_bg(0,4.85)

24
25
local share_bones_time = tonumber(minetest.setting_get("share_bones_time")) or 1200
local share_bones_time_early = tonumber(minetest.setting_get("share_bones_time_early")) or share_bones_time / 4
Tim's avatar
Tim committed
26

PilzAdam's avatar
PilzAdam committed
27
28
29
minetest.register_node("bones:bones", {
	description = "Bones",
	tiles = {
paramat's avatar
paramat committed
30
		"bones_top.png^[transform2",
PilzAdam's avatar
PilzAdam committed
31
32
33
34
35
36
37
		"bones_bottom.png",
		"bones_side.png",
		"bones_side.png",
		"bones_rear.png",
		"bones_front.png"
	},
	paramtype2 = "facedir",
38
	groups = {dig_immediate = 2},
paramat's avatar
paramat committed
39
	sounds = default.node_sound_gravel_defaults(),
40

PilzAdam's avatar
PilzAdam committed
41
	can_dig = function(pos, player)
42
		local inv = minetest.get_meta(pos):get_inventory()
MT-Modder's avatar
MT-Modder committed
43
44
45
46
47
		local name = ""
		if player then
			name = player:get_player_name()
		end
		return is_owner(pos, name) and inv:is_empty("main")
PilzAdam's avatar
PilzAdam committed
48
	end,
49

PilzAdam's avatar
PilzAdam committed
50
51
52
53
54
55
	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,
56

PilzAdam's avatar
PilzAdam committed
57
58
59
	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
		return 0
	end,
60

PilzAdam's avatar
PilzAdam committed
61
62
63
64
65
66
	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,
67

PilzAdam's avatar
PilzAdam committed
68
	on_metadata_inventory_take = function(pos, listname, index, stack, player)
69
		local meta = minetest.get_meta(pos)
70
71
		if meta:get_inventory():is_empty("main") then
			minetest.remove_node(pos)
PilzAdam's avatar
PilzAdam committed
72
73
		end
	end,
74

75
	on_punch = function(pos, node, player)
76
		if not is_owner(pos, player:get_player_name()) then
77
78
			return
		end
79
80

		if minetest.get_meta(pos):get_string("infotext") == "" then
81
82
			return
		end
83

84
85
86
		local inv = minetest.get_meta(pos):get_inventory()
		local player_inv = player:get_inventory()
		local has_space = true
87
88

		for i = 1, inv:get_size("main") do
89
90
91
92
93
94
95
96
97
			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
98

99
100
		-- remove bones if player emptied them
		if has_space then
101
102
103
104
105
			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
106
107
108
			minetest.remove_node(pos)
		end
	end,
109

PilzAdam's avatar
PilzAdam committed
110
	on_timer = function(pos, elapsed)
111
		local meta = minetest.get_meta(pos)
Tim's avatar
Tim committed
112
113
		local time = meta:get_int("time") + elapsed
		if time >= share_bones_time then
114
			meta:set_string("infotext", meta:get_string("owner") .. "'s old bones")
PilzAdam's avatar
PilzAdam committed
115
116
			meta:set_string("owner", "")
		else
Tim's avatar
Tim committed
117
			meta:set_int("time", time)
PilzAdam's avatar
PilzAdam committed
118
119
120
			return true
		end
	end,
121
122
	on_blast = function(pos)
	end,
PilzAdam's avatar
PilzAdam committed
123
124
})

Tim's avatar
Tim committed
125
126
127
128
local function may_replace(pos, player)
	local node_name = minetest.get_node(pos).name
	local node_definition = minetest.registered_nodes[node_name]

129
	-- if the node is unknown, we return false
Tim's avatar
Tim committed
130
	if not node_definition then
131
		return false
Tim's avatar
Tim committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	end

	-- allow replacing air and liquids
	if node_name == "air" or 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
	-- exception are of course any protected buildable_to
	return node_definition.buildable_to and not minetest.is_protected(pos, player:get_player_name())
end

151
local drop = function(pos, itemstack)
152
	local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
153
154
155
156
157
158
159
160
161
	if obj then
		obj:setvelocity({
			x = math.random(-10, 10) / 9,
			y = 5,
			z = math.random(-10, 10) / 9,
		})
	end
end

PilzAdam's avatar
PilzAdam committed
162
minetest.register_on_dieplayer(function(player)
163
164
165
166
167
168
169
170

	local bones_mode = minetest.setting_get("bones_mode") or "bones"
	if bones_mode ~= "bones" and bones_mode ~= "drop" and bones_mode ~= "keep" then
		bones_mode = "bones"
	end

	-- return if keep inventory set or in creative mode
	if bones_mode == "keep" or minetest.setting_getbool("creative_mode") then
PilzAdam's avatar
PilzAdam committed
171
172
		return
	end
173

174
175
176
177
178
179
	local player_inv = player:get_inventory()
	if player_inv:is_empty("main") and
		player_inv:is_empty("craft") then
		return
	end

180
	local pos = vector.round(player:getpos())
181
182
	local player_name = player:get_player_name()

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	-- check if it's possible to place bones, if not go 1 higher
	if bones_mode == "bones" and not may_replace(pos, player) then
		pos.y = pos.y + 1
	end

	-- still cannot place bones? change mode to 'drop'
	if bones_mode == "bones" and not may_replace(pos, player) then
		bones_mode = "drop"
	end

	if bones_mode == "drop" then

		-- drop inventory items
		for i = 1, player_inv:get_size("main") do
			drop(pos, player_inv:get_stack("main", i))
		end
		player_inv:set_list("main", {})

		-- drop crafting grid items
		for i = 1, player_inv:get_size("craft") do
			drop(pos, player_inv:get_stack("craft", i))
PilzAdam's avatar
PilzAdam committed
204
		end
205
206
207
208
		player_inv:set_list("craft", {})

		drop(pos, ItemStack("bones:bones"))
		return
PilzAdam's avatar
PilzAdam committed
209
	end
210
211
212

	local param2 = minetest.dir_to_facedir(player:get_look_dir())
	minetest.set_node(pos, {name = "bones:bones", param2 = param2})
Milan's avatar
Milan committed
213
214
    minetest.chat_send_player(player_name, "Your stuff is waiting for you at "..minetest.pos_to_string(pos).. ". Go and grab it! ;-)")
    minetest.log("action", player_name.." left their bones at "..minetest.pos_to_string(pos))
Milan's avatar
Milan committed
215

216
	local meta = minetest.get_meta(pos)
PilzAdam's avatar
PilzAdam committed
217
	local inv = meta:get_inventory()
218
	inv:set_size("main", 8 * 4)
PilzAdam's avatar
PilzAdam committed
219
	inv:set_list("main", player_inv:get_list("main"))
220
221

	for i = 1, player_inv:get_size("craft") do
222
223
224
225
226
		local stack = player_inv:get_stack("craft", i)
		if inv:room_for_item("main", stack) then
			inv:add_item("main", stack)
		else
			--drop if no space left
227
			drop(pos, stack)
228
		end
PilzAdam's avatar
PilzAdam committed
229
	end
230

231
232
	player_inv:set_list("main", {})
	player_inv:set_list("craft", {})
233
234

	meta:set_string("formspec", bones_formspec)
235
	meta:set_string("owner", player_name)
236

Tim's avatar
Tim committed
237
	if share_bones_time ~= 0 then
238
		meta:set_string("infotext", player_name .. "'s fresh bones")
Tim's avatar
Tim committed
239
240
241
242
243
244
245
246
247
248
249

		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", player_name.."'s bones")
	end
PilzAdam's avatar
PilzAdam committed
250
end)