summaryrefslogtreecommitdiff
path: root/tool.lua
blob: 864c35f510e7cf5567e0235fd2c4a5548a994509 (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

-- protector placement tool (thanks to Shara for code and idea)

minetest.register_craftitem("protector:tool", {
	description = "Protector Placer Tool (stand near protector, face direction and use)",
	inventory_image = "protector_display.png^protector_logo.png",
	stack_max = 1,

	on_use = function(itemstack, user, pointed_thing)

		local name = user:get_player_name()

		-- check for protector near player (2 block radius)
		local pos = user:get_pos()
		local pp = minetest.find_nodes_in_area(
			vector.subtract(pos, 2), vector.add(pos, 2),
			{"protector:protect", "protector:protect2"})

		if #pp == 0 then return end -- none found

		pos = pp[1] -- take position of first protector found

		-- get members on protector
		local meta = minetest.get_meta(pos)
		local members = meta:get_string("members") or ""

		-- get direction player is facing
		local dir = minetest.dir_to_facedir( user:get_look_dir() )
		local vec = {x = 0, y = 0, z = 0}
		local gap = (protector.radius * 2) + 1
		local pit =  user:get_look_pitch()

		-- set placement coords
		if pit > 1.2 then
			vec.y = gap -- up
		elseif pit < -1.2 then
			vec.y = -gap -- down
		elseif dir == 0 then
			vec.z = gap -- north
		elseif dir == 1 then
			vec.x = gap -- east
		elseif dir == 2 then
			vec.z = -gap -- south
		elseif dir == 3 then
			vec.x = -gap -- west
		end

		-- new position
		pos.x = pos.x + vec.x
		pos.y = pos.y + vec.y
		pos.z = pos.z + vec.z

		-- does placing a protector overlap existing area
		if not protector.can_dig(protector.radius * 2, pos,
			user:get_player_name(), true, 3) then

			minetest.chat_send_player(name,
				"Overlaps into above players protected area")

			return
		end

		-- does a protector already exist ?
		if #minetest.find_nodes_in_area(
			vector.subtract(pos, 1), vector.add(pos, 1),
			{"protector:protect", "protector:protect2"}) > 0 then

			minetest.chat_send_player(name, "Protector already in place!")
			return
		end

		-- do we have protectors to use ?
		local nod
		local inv = user:get_inventory()

		if not inv:contains_item("main", "protector:protect")
		and not inv:contains_item("main", "protector:protect2") then
			minetest.chat_send_player(name, "No protectors available to place!")
			return
		end

		-- take protector (block first then logo)
		if inv:contains_item("main", "protector:protect") then

			inv:remove_item("main", "protector:protect")
			nod = "protector:protect"

		elseif inv:contains_item("main", "protector:protect2") then

			inv:remove_item("main", "protector:protect2")
			nod = "protector:protect2"
		end

		-- place protector
		minetest.set_node(pos, {name = nod, param2 = 1})

		-- set protector metadata
		local meta = minetest.get_meta(pos)

		meta:set_string("owner", name)
		meta:set_string("infotext", "Protection (owned by " .. name .. ")")

		-- copy members across if holding sneak when using tool
		if user:get_player_control().sneak then
			meta:set_string("members", members)
		else
			meta:set_string("members", "")
		end

		minetest.chat_send_player(name,
				"Protector placed at " .. minetest.pos_to_string(pos))

	end,
})

-- tool recipe
minetest.register_craft({
	output = "protector:tool",
	recipe = {
		{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
		{"default:steel_ingot", "protector:protect", "default:steel_ingot"},
		{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
	}
})