summaryrefslogtreecommitdiff
path: root/init.lua
blob: 7a86e80b80d35ad113cec811eb79cbbd32a3caeb (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

--Todo make lift go up and down!

doors.register_door("lifter:door", {
	description = "Lift Door",
	inventory_image = "lifter_door_inv.png",
	groups = {choppy=3, cracky=3, oddly_breakable_by_hand=1, flammable=2, door=1},
	tiles_bottom = {"lifter_door_b.png", "lifter_door.png"},
	tiles_top = {"lifter_door_a.png", "lifter.png"},
	only_placer_can_open = false,
	sounds = default.node_sound_wood_defaults(),
	sunlight = false
})

-- should only be ignore if there's not generated map
local function get_far_node(pos)
	local node = minetest.get_node(pos)
	if node.name == "ignore" then
		minetest.get_voxel_manip():read_from_map(pos, pos)
		node = minetest.get_node(pos)
	end
	return node
end


local function fetch_lift(pos, node, clicker, rel, i, open_door, plus)
	local wnode = get_far_node({x=pos.x+1, y=pos.y+i, z=pos.z})
	local snode = get_far_node({x=pos.x-1, y=pos.y+i, z=pos.z})
	local anode = get_far_node({x=pos.x, y=pos.y+i, z=pos.z+1})
	local dnode = get_far_node({x=pos.x, y=pos.y+i, z=pos.z-1})
	local name = clicker:get_player_name()
	if not name then
	   return
	end
	if wnode.name ~= "lifter:lift" and snode.name ~= "lifter:lift" and 
		anode.name ~= "lifter:lift" and dnode.name ~= "lifter:lift" then
		
			if wnode.name ~= "air" and snode.name ~= "air" and 
			   anode.name ~= "air" and dnode.name ~= "air" and
			   wnode.name ~= "bones:bones" and snode.name ~= "bones:bones" and 
			anode.name ~= "bones:bones" and dnode.name ~= "bones:bones"then
				minetest.chat_send_player(name, "lift not found, no air")
				return
			end
			
			local test = minetest.find_nodes_in_area({x=pos.x-2, y=pos.y+i, z=pos.z-2}, {x=pos.x+2, y=pos.y+i, z=pos.z+2}, "air")
			if #test == 9 then
				minetest.chat_send_player(name, "lift not found, too much air")
				return
			end
			
			if i%20 == 0 then
				minetest.after(1, fetch_lift, pos, node, clicker, rel, i+plus, open_door, plus)
			else
				fetch_lift(pos, node, clicker, rel, i+plus, open_door, plus)
			end
	else 	
		if wnode.name == "lifter:lift" then
			local name = minetest.get_node({x=pos.x+1, y=pos.y+rel, z=pos.z}).name
			if name == "air" or name == "ignore" or name == "bones:bones" then
				minetest.remove_node({x=pos.x+1, y=pos.y+i, z=pos.z})
				minetest.add_node({x=pos.x+1, y=pos.y+rel, z=pos.z}, {name="lifter:lift"})
			else
				minetest.chat_send_player(name, "lift blocked")
			end
		end
		if snode.name == "lifter:lift" then
			local name = minetest.get_node({x=pos.x-1, y=pos.y+rel, z=pos.z}).name
			if name == "air" or name == "ignore" or name == "bones:bones" then
				minetest.remove_node({x=pos.x-1, y=pos.y+i, z=pos.z})
				minetest.add_node({x=pos.x-1, y=pos.y+rel, z=pos.z}, {name="lifter:lift"})
			else
				minetest.chat_send_player(name, "lift blocked")
			end
		end
		if anode.name == "lifter:lift" then
			local name = minetest.get_node({x=pos.x, y=pos.y+rel, z=pos.z+1}).name
			if name == "air" or name == "ignore" or name == "bones:bones" then
				minetest.remove_node({x=pos.x, y=pos.y+i, z=pos.z+1})
				minetest.add_node({x=pos.x, y=pos.y+rel, z=pos.z+1}, {name="lifter:lift"})
			else
				minetest.chat_send_player(name, "lift blocked")
			end
		end
		if dnode.name == "lifter:lift" then
			local name = minetest.get_node({x=pos.x, y=pos.y+rel, z=pos.z-1}).name
			if name == "air" or name == "ignore" or name == "bones:bones" then
				minetest.remove_node({x=pos.x, y=pos.y+i, z=pos.z-1})
				minetest.add_node({x=pos.x, y=pos.y+rel, z=pos.z-1}, {name="lifter:lift"})
			else
				minetest.chat_send_player(name, "lift blocked")
			end
		end
		open_door(pos,node,clicker)
	end
end

local b1rc = minetest.registered_nodes["lifter:door_a"].on_rightclick
--local t2rc = minetest.registered_nodes["lifter:door_t_2"].on_rightclick

minetest.override_item("lifter:door_a", {
	on_rightclick = function(pos, node, clicker)
		if string.sub(node.name, -2) == "_a" then
			if clicker:is_player() then
				minetest.chat_send_player(clicker:get_player_name(), "You called for a lift...")
			end
			fetch_lift(pos, node, clicker, -1, 0, b1rc, 1)
			fetch_lift(pos, node, clicker, -1, 0, b1rc, -1)
--			minetest.after(5, b1rc, pos, node, clicker)
		else
			b1rc(pos, node, player)
		end
	end
})

minetest.register_abm({
      nodenames = {"lifter:door_b"},
      interval = 8,
      chance = 1,
      catch_up = false,
      action = function(pos, node)
	 minetest.after(1, b1rc, pos, node, "")
      end
})

--minetest.override_item("lifter:door_b_2", {
	--on_rightclick = hijack_click(b2rc)
--})


--minetest.override_item("lifter:door_t_2", {
	--on_rightclick = hijack_click(t2rc)
--})

local b2rc = minetest.registered_nodes["lifter:door_b"].on_rightclick

minetest.register_node("lifter:lift", {
	tiles = {"lifter.png"},
	description = "Lift",
	drawtype = "normal",
	paramtype = "light",
	groups = {crumbly=3},
	on_rightclick = function(pos, node, player, itemstack, pointed_thing) 
		local obj = minetest.add_entity(pos, "lifter:travelling_lift")
		minetest.remove_node(pos)
		
		if not player then
			return
		end
		
		obj:get_luaentity().driver = player

		player:set_attach(obj, "", {x=0, y=15, z=0}, {x=0, y=0, z=0})
		player:set_eye_offset({x=0, y=6, z=0},{x=0, y=0, z=0})
		
		local door = minetest.find_node_near(pos, 2, "lifter:door_b")
		if door then
			b2rc(door, minetest.get_node(door), player)
		end
	end,
})

minetest.register_entity("lifter:travelling_lift", {
	physical = true,
	collide_with_objects = true,
	collisionbox = {-0.5,-0.5,-0.5, 0.5,2.5,0.5},
	visual = "cube",
	textures = {"lifter.png", "lifter.png", "lifter.png", "lifter.png", "lifter.png", "lifter.png"},
	--visual_size = {x=1, y=1},
	
	driver = nil,
	direction = 0,
	
	on_punch = function(self, dtime)
	end,
	
	on_step = function(self, dtime)
		local exit = false
		
		-- Turn to actual sand when collides to ground or just move
		local pos = self.object:getpos()
		local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
		local bcn = minetest.get_node(bcp)
		local bcd = minetest.registered_nodes[bcn.name]
		-- Note: walkable is in the node definition, not in item groups
		local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
		
		if not self.driver then
			minetest.add_node(np, {name="lifter:lift"})
			self.object:remove()
			nodeupdate(np)
			return
		end
		
		local ctrl = self.driver:get_player_control()
		if ctrl.jump then
			self.direction = 1
		end
		if ctrl.sneak then
			self.direction = -1
		end
		if ctrl.aux1 then
			self.direction = 0
			self.object:setvelocity({x=0, y=0, z=0})
		end
		
		pos.y = pos.y+1
		local nex = 1
		local wnode = minetest.get_node({x=pos.x+1, y=pos.y, z=pos.z})
		local wbnode = minetest.get_node({x=pos.x+1, y=pos.y-nex, z=pos.z})
		local snode = minetest.get_node({x=pos.x-1, y=pos.y, z=pos.z})
		local sbnode = minetest.get_node({x=pos.x-1, y=pos.y-nex, z=pos.z})
		local anode = minetest.get_node({x=pos.x, y=pos.y, z=pos.z+1})
		local abnode = minetest.get_node({x=pos.x, y=pos.y-nex, z=pos.z+1})
		local dnode = minetest.get_node({x=pos.x, y=pos.y, z=pos.z-1})
		local dbnode = minetest.get_node({x=pos.x, y=pos.y-nex, z=pos.z-1})
		
		if self.direction ~= 0 and not ctrl.jump and not ctrl.sneak then
		 	if (wnode.name == "air" and wbnode.name ~= "air") or 
		 		(snode.name == "air" and sbnode.name ~= "air") or 
		 		(anode.name == "air" and abnode.name ~= "air") or 
		 		(dnode.name == "air" and dbnode.name ~= "air") then
				self.direction = 0
				self.object:setvelocity({x=0, y=0, z=0})
				self.object:setacceleration({x=0, y=0, z=0})
			end
			if (wnode.name:find("door") and not wbnode.name:find("door")) or 
		 		(snode.name:find("door") and not sbnode.name:find("door")) or 
		 		(anode.name:find("door") and not abnode.name:find("door")) or 
		 		(dnode.name:find("door") and not dbnode.name:find("door")) then
				self.direction = 0
				self.object:setvelocity({x=0, y=0, z=0})
				self.object:setacceleration({x=0, y=0, z=0})
			end
		end
		
		if wnode.name == "air" and snode.name == "air" and anode.name == "air" and dnode.name == "air" then
			if self.direction >= 0  then
				self.direction = 0
				self.object:setvelocity({x=0, y=0, z=0})
			else 
				self.object:setacceleration({x=0, y=-10, z=0})
			end
		end
		
		local vel = self.object:getvelocity()
		if vector.equals(vel, {x=0,y=0,z=0}) then
			if ctrl.up or ctrl.down or ctrl.left or ctrl.right then
				exit = true
			end
			local npos = self.object:getpos()
			if self.direction == 1 then
				npos.y = npos.y+0.5
				if minetest.get_node({x=npos.x, y=npos.y+2.5, z=npos.z}).name ~= "air" then
					npos.y = npos.y-0.5
				end
			end
			if self.direction == -1 then
				npos.y = npos.y-1
				if minetest.get_node(npos).name ~= "air" then
					npos.y = npos.y+0.5
				end
			end
			self.object:setpos(vector.round(npos))
		end
		
		
		if exit then
			local door = minetest.find_node_near(np, 2, "lifter:door_a")
			if door then
				b1rc(door, minetest.get_node(door), self.driver)
			end
		
			-- Create node and remove entity
			minetest.add_node(np, {name="lifter:lift"})
			self.object:remove()
			nodeupdate(np)
			
			if self.driver then
				self.driver:set_detach()
				self.driver:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})
				pos.y = pos.y
				self.driver:setpos(pos)
			end
			
			return
		end
		
		if ctrl.jump or ctrl.sneak then
			-- Set gravity
			self.object:setacceleration({x=0, y=self.direction*5, z=0})
		else
			self.object:setacceleration({x=0, y=0, z=0})
		end
	end
})

minetest.register_craft({
	output = "lifter:lift",
	recipe = {
		{"group:wood", "group:stick", "group:wood"},
		{"group:wood", "default:mese", "group:wood"},
		{"group:wood", "group:stick", "group:wood"},
	},
})

minetest.register_craft({
	output = "lifter:door",
	recipe = {
		{"group:stick", "group:stick", "group:stick"},
		{"group:stick", "doors:door_wood", "group:stick"},
		{"group:stick", "group:stick", "group:stick"},
	},
})