summaryrefslogtreecommitdiff
path: root/nodes/node_crate.lua
blob: 40739939e171e35df3cdbc480545885141709de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
-- internationalization boilerplate
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")

local modpath_awards = minetest.get_modpath("awards")


local player_permitted = function(pos, player)
	if player then
		if minetest.check_player_privs(player, "protection_bypass") then
			return true
		end
	else
		return false
	end

	local meta = minetest.get_meta(pos)
	local owner = meta:get_string("owner")

	if not owner or owner == "" or owner == player:get_player_name() then
		return true
	end
end

local store_digtron = function(pos, clicker, loaded_node_name, protected)
	local layout = DigtronLayout.create(pos, clicker)
	local protection_prefix = ""
	local protection_suffix = ""
	if protected then
		protection_prefix = S("Digtron Crate") .. "\n" .. S("Owned by @1", clicker:get_player_name() or "")
		protection_suffix = S("Owned by @1", clicker:get_player_name() or "")
	end
	
	if layout.contains_protected_node then
		local meta = minetest.get_meta(pos)
		minetest.sound_play("buzzer", {gain=0.5, pos=pos})
		meta:set_string("infotext", protection_prefix .. "\n" .. S("Digtron can't be packaged, it contains protected blocks"))
		-- no stealing other peoples' digtrons
		return
	end
	
	if #layout.all == 1 then
		local meta = minetest.get_meta(pos)
		minetest.sound_play("buzzer", {gain=0.5, pos=pos})
		meta:set_string("infotext", protection_prefix .. "\n" .. S("No Digtron components adjacent to package"))
		return
	end

	digtron.award_crate(layout, clicker:get_player_name())
	
	local layout_string = layout:serialize()
	
	-- destroy everything. Note that this includes the empty crate, which will be bundled up with the layout.
	for _, node_image in pairs(layout.all) do
		local old_pos = node_image.pos
		local old_node = node_image.node
		minetest.remove_node(old_pos)
		
		if modpath_awards then
			-- We're about to tell the awards mod that we're digging a node, but we
			-- don't want it to count toward any actual awards. Pre-decrement.
			local data = awards.players[clicker:get_player_name()]
			awards.increment_item_counter(data, "count", old_node.name, -1)
		end
		
		for _, callback in ipairs(minetest.registered_on_dignodes) do
			-- Copy pos and node because callback can modify them
			local pos_copy = {x=old_pos.x, y=old_pos.y, z=old_pos.z}
			local oldnode_copy = {name=old_node.name, param1=old_node.param1, param2=old_node.param2}
			callback(pos_copy, oldnode_copy, clicker)
		end			
	end
	
	-- Create the loaded crate node
	minetest.set_node(pos, {name=loaded_node_name})
	minetest.sound_play("machine1", {gain=1.0, pos=pos})
	
	local meta = minetest.get_meta(pos)
	meta:set_string("crated_layout", layout_string)
	meta:set_string("title", S("Crated Digtron"))
	meta:set_string("infotext", S("Crated Digtron") .. "\n" .. protection_suffix)
end

minetest.register_node("digtron:empty_crate", {
	description = S("Digtron Crate (Empty)"),
	_doc_items_longdesc = digtron.doc.empty_crate_longdesc,
    _doc_items_usagehelp = digtron.doc.empty_crate_usagehelp,
	groups = {cracky = 3, oddly_breakable_by_hand=3},
	sounds = default.node_sound_wood_defaults(),
	tiles = {"digtron_crate.png"},
	is_ground_content = false,
	drawtype = "nodebox",
	node_box = {
        type = "fixed",
        fixed = {
            {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
        },
    },
	paramtype = "light",
	
	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		store_digtron(pos, clicker, "digtron:loaded_crate")
	end
})

minetest.register_node("digtron:empty_locked_crate", {
	description = S("Digtron Locked Crate (Empty)"),
	_doc_items_longdesc = digtron.doc.empty_locked_crate_longdesc,
    _doc_items_usagehelp = digtron.doc.empty_locked_crate_usagehelp,
	groups = {cracky = 3, oddly_breakable_by_hand=3},
	sounds = default.node_sound_wood_defaults(),
	tiles = {"digtron_crate.png","digtron_crate.png","digtron_crate.png^digtron_lock.png","digtron_crate.png^digtron_lock.png","digtron_crate.png^digtron_lock.png","digtron_crate.png^digtron_lock.png"},
	is_ground_content = false,
	drawtype = "nodebox",
	node_box = {
        type = "fixed",
        fixed = {
            {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
        },
    },
	paramtype = "light",
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", "")
		meta:set_string("infotext", "")
	end,
	after_place_node = function(pos, placer)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", placer:get_player_name() or "")
		meta:set_string("infotext", S("Digtron Crate") .. "\n" .. S("Owned by @1", placer:get_player_name() or ""))
	end,
	can_dig = function(pos,player)
		return player_permitted(pos,player)
	end,
	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		if player_permitted(pos,clicker) then
			store_digtron(pos, clicker, "digtron:loaded_locked_crate", true)
		end
	end,
})

local modpath_doc = minetest.get_modpath("doc")
local loaded_formspec_string
if modpath_doc then
	loaded_formspec_string =
	"size[4.1,1.5]" ..
	default.gui_bg ..
	default.gui_bg_img ..
	default.gui_slots ..
	"field[0.3,0.5;4,0.5;title;" .. S("Digtron Name") .. ";${title}]" ..
	"button_exit[0.0,1.2;1,0.1;save;" .. S("Save\nTitle") .. "]" ..
	"tooltip[save;" .. S("Saves the title of this Digtron") .. "]" ..
	"button_exit[1.0,1.2;1,0.1;show;" .. S("Show\nBlocks") .. "]" ..
	"tooltip[show;" .. S("Shows which blocks the packed Digtron will occupy if unpacked") .. "]" ..
	"button_exit[2.0,1.2;1,0.1;unpack;" .. S("Unpack") .. "]" ..
	"tooltip[unpack;" .. S("Attempts to unpack the Digtron on this location") .. "]" ..
	"button_exit[3.0,1.2;1,0.1;help;" .. S("Help") .. "]" ..
	"tooltip[help;" .. S("Show documentation about this block") .. "]"
else
	loaded_formspec_string =
	"size[4,1.5]" ..
	default.gui_bg ..
	default.gui_bg_img ..
	default.gui_slots ..
	"field[0.3,0.5;4,0.5;title;" .. S("Digtron Name") .. ";${title}]" ..
	"button_exit[0.5,1.2;1,0.1;save;" .. S("Save\nTitle") .. "]" ..
	"tooltip[show;" .. S("Saves the title of this Digtron") .. "]" ..
	"button_exit[1.5,1.2;1,0.1;show;" .. S("Show\nBlocks") .. "]" ..
	"tooltip[save;" .. S("Shows which blocks the packed Digtron will occupy if unpacked") .. "]" ..
	"button_exit[2.5,1.2;1,0.1;unpack;" .. S("Unpack") .. "]" ..
	"tooltip[unpack;" .. S("Attempts to unpack the Digtron on this location") .. "]"
end

local loaded_formspec = function(pos, meta)
	return loaded_formspec_string
end

local loaded_on_recieve = function(pos, fields, sender, protected)
	local meta = minetest.get_meta(pos)

	if fields.unpack or fields.save or fields.show or fields.key_enter then
		meta:set_string("title", minetest.formspec_escape(fields.title))
	end
	local title = meta:get_string("title")
	
	if protected then
		infotext = title .. "\n" .. S("Owned by @1", sender:get_player_name())
	else
		infotext = title
	end
	meta:set_string("infotext", infotext)
	
	if fields.help and minetest.get_modpath("doc") then --check for mod in case someone disabled it after this digger was built
		minetest.after(0.5, doc.show_entry, sender:get_player_name(), "nodes", "digtron:loaded_crate", true)
	end

	if not (fields.unpack or fields.show) then
		return
	end
	
	local layout_string = meta:get_string("crated_layout")
	local layout = DigtronLayout.deserialize(layout_string)

	if layout == nil then
		meta:set_string("infotext", infotext .. "\n" .. S("Unable to read layout from crate metadata, regrettably this Digtron may be corrupted."))
		minetest.sound_play("buzzer", {gain=0.5, pos=pos})			
		-- Something went horribly wrong
		return
	end
	
	local protected_node = false
	local obstructed_node = false
	
	local pos_diff = vector.subtract(pos, layout.controller)
	layout.controller = pos
	for _, node_image in pairs(layout.all) do
		node_image.pos = vector.add(pos_diff, node_image.pos)
		if not vector.equals(pos, node_image.pos) then
			if minetest.is_protected(node_image.pos, sender:get_player_name()) and not minetest.check_player_privs(sender, "protection_bypass") then
				protected_node = true
				minetest.add_entity(node_image.pos, "digtron:marker_crate_bad")
			elseif not minetest.registered_nodes[minetest.get_node(node_image.pos).name].buildable_to then
				obstructed_node = true
				minetest.add_entity(node_image.pos, "digtron:marker_crate_bad")
			else
				minetest.add_entity(node_image.pos, "digtron:marker_crate_good")
			end
		end
	end
	
	if not fields.unpack then
		return
	end
	
	if protected_node then
		meta:set_string("infotext", infotext .. "\n" .. S("Unable to deploy Digtron due to protected blocks in target area"))
		minetest.sound_play("buzzer", {gain=0.5, pos=pos})
		return
	end
	
	if obstructed_node then
		meta:set_string("infotext", infotext .. "\n" .. S("Unable to deploy Digtron due to obstruction in target area"))
		minetest.sound_play("buzzer", {gain=0.5, pos=pos})
		return
	end
	
	-- build digtron. Since the empty crate was included in the layout, that will overwrite this loaded crate and destroy it.
	minetest.sound_play("machine2", {gain=1.0, pos=pos})
	layout:write_layout_image(sender)
end

local loaded_on_dig = function(pos, player, loaded_node_name)
	local meta = minetest.get_meta(pos)
	local to_serialize = {title=meta:get_string("title"), layout=meta:get_string("crated_layout")}
		
	local stack = ItemStack({name=loaded_node_name, count=1, wear=0, metadata=minetest.serialize(to_serialize)})
	local inv = player:get_inventory()
	local stack = inv:add_item("main", stack)
	if stack:get_count() > 0 then
		minetest.add_item(pos, stack)
	end		
	-- call on_dignodes callback
	minetest.remove_node(pos)
end

local loaded_after_place = function(pos, itemstack)
	local deserialized = minetest.deserialize(itemstack:get_metadata())
	if deserialized then
		local meta = minetest.get_meta(pos)
			
		meta:set_string("crated_layout", deserialized.layout)
		meta:set_string("title", deserialized.title)
		meta:set_string("infotext", deserialized.title)
		--meta:set_string("formspec", loaded_formspec(pos, meta)) -- not needed, on_construct handles this
	end
end

minetest.register_node("digtron:loaded_crate", {
	description = S("Digtron Crate (Loaded)"),
	_doc_items_longdesc = digtron.doc.loaded_crate_longdesc,
    _doc_items_usagehelp = digtron.doc.loaded_crate_usagehelp,
	_digtron_formspec = loaded_formspec,
	groups = {cracky = 3, oddly_breakable_by_hand=3, not_in_creative_inventory=1, digtron_protected=1},
	stack_max = 1,
	sounds = default.node_sound_wood_defaults(),
	tiles = {"digtron_plate.png^digtron_crate.png"},
	is_ground_content = false,
	
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("formspec", loaded_formspec(pos, meta))
	end,
	
	on_receive_fields = function(pos, formname, fields, sender)
		return loaded_on_recieve(pos, fields, sender)
	end,
		
	on_dig = function(pos, node, player)
		return loaded_on_dig(pos, player, "digtron:loaded_crate")
	end,
	
	after_place_node = function(pos, placer, itemstack, pointed_thing)
		loaded_after_place(pos, itemstack)
	end,
})

minetest.register_node("digtron:loaded_locked_crate", {
	description = S("Digtron Locked Crate (Loaded)"),
	_doc_items_longdesc = digtron.doc.loaded_locked_crate_longdesc,
    _doc_items_usagehelp = digtron.doc.loaded_locked_crate_usagehelp,
	groups = {cracky = 3, oddly_breakable_by_hand=3, not_in_creative_inventory=1, digtron_protected=1},
	stack_max = 1,
	sounds = default.node_sound_wood_defaults(),
	tiles = {"digtron_plate.png^digtron_crate.png","digtron_plate.png^digtron_crate.png","digtron_plate.png^digtron_crate.png^digtron_lock.png","digtron_plate.png^digtron_crate.png^digtron_lock.png","digtron_plate.png^digtron_crate.png^digtron_lock.png","digtron_plate.png^digtron_crate.png^digtron_lock.png"},
	is_ground_content = false,
	
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", "")
	end,
	
	on_dig = function(pos, node, player)
		if player_permitted(pos,player) then
			return loaded_on_dig(pos, player, "digtron:loaded_locked_crate")
		else
			return false
		end
	end,
	
	after_place_node = function(pos, placer, itemstack, pointed_thing)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", placer:get_player_name() or "")
		loaded_after_place(pos, itemstack)
		meta:set_string("infotext", meta:get_string("infotext") .. "\n" .. S("Owned by @1", meta:get_string("owner")))
	end,
	
	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		if player_permitted(pos,clicker) then
			local meta = minetest.get_meta(pos)
			minetest.show_formspec(
				clicker:get_player_name(),
				"digtron:loaded_locked_crate"..minetest.pos_to_string(pos),
				loaded_formspec_string:gsub("${title}", meta:get_string("title"), 1))
		end
	end,	
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
	if formname:sub(1, 27) == "digtron:loaded_locked_crate" then
		local pos = minetest.string_to_pos(formname:sub(28, -1))
		loaded_on_recieve(pos, fields, player, true)
		return true
	end
end)