summaryrefslogtreecommitdiff
path: root/doors.lua
blob: 07c135ef1122eaddda7c7d7ef3d8a96ab0d05e54 (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
celevator.doors = {}

celevator.doors.erefs = {}

minetest.register_node("celevator:hwdoor_fast_glass_bottom",{
	description = "Glass Hoistway Door (fast, bottom)",
	tiles = {
		"celevator_transparent.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_topbottom.png^[transformFY",
		"celevator_door_glass_topbottom.png^[transformFY",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	paramtype2 = "4dir",
	drawtype = "nodebox",
	use_texture_alpha = "clip",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,0.4,0.5,0.5,0.5},
		},
	},
})

minetest.register_node("celevator:hwdoor_fast_glass_middle",{
	description = "Glass Hoistway Door (fast, middle)",
	tiles = {
		"celevator_transparent.png",
		"celevator_transparent.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_middle.png",
		"celevator_door_glass_middle.png",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	paramtype2 = "4dir",
	drawtype = "nodebox",
	use_texture_alpha = "clip",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,0.4,0.5,0.5,0.5},
		},
	},
})

minetest.register_node("celevator:hwdoor_fast_glass_top",{
	description = "Glass Hoistway Door (fast, top)",
	tiles = {
		"celevator_door_glass_edge.png",
		"celevator_transparent.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_topbottom.png",
		"celevator_door_glass_topbottom.png",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	paramtype2 = "4dir",
	drawtype = "nodebox",
	use_texture_alpha = "clip",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,0.4,0.5,0.5,0.5},
		},
	},
})

minetest.register_node("celevator:hwdoor_slow_glass_bottom",{
	description = "Glass Hoistway Door (slow, bottom)",
	tiles = {
		"celevator_transparent.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_topbottom.png^[transformFY",
		"celevator_door_glass_topbottom.png^[transformFY",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	paramtype2 = "4dir",
	drawtype = "nodebox",
	use_texture_alpha = "clip",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,0.3,0.5,0.5,0.4},
		},
	},
})

minetest.register_node("celevator:hwdoor_slow_glass_middle",{
	description = "Glass Hoistway Door (slow, middle)",
	tiles = {
		"celevator_transparent.png",
		"celevator_transparent.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_middle.png",
		"celevator_door_glass_middle.png",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	paramtype2 = "4dir",
	drawtype = "nodebox",
	use_texture_alpha = "clip",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,0.3,0.5,0.5,0.4},
		},
	},
})

minetest.register_node("celevator:hwdoor_slow_glass_top",{
	description = "Glass Hoistway Door (slow, top)",
	tiles = {
		"celevator_door_glass_edge.png",
		"celevator_transparent.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_edge.png",
		"celevator_door_glass_topbottom.png",
		"celevator_door_glass_topbottom.png",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	paramtype2 = "4dir",
	drawtype = "nodebox",
	use_texture_alpha = "clip",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,0.3,0.5,0.5,0.4},
		},
	},
})

minetest.register_node("celevator:hwdoor_placeholder",{
	description = "Hoistway Door Open-State Placeholder",
	tiles = {
		"celevator_transparent.png",
	},
	paramtype = "light",
	drawtype = "airlike",
})

minetest.register_entity("celevator:hwdoor_moving",{
	initial_properties = {
		visual = "wielditem",
		visual_size = vector.new(0.667,0.667,0.667),
		wield_item = "default:dirt",
		static_save = false,
	},
})

function celevator.doors.hwopen(pos)
	local hwdoors_moving = minetest.deserialize(celevator.storage:get_string("hwdoors_moving")) or {}
	local hash = minetest.hash_node_position(pos)
	if hwdoors_moving[hash] then
		return
	end
	local fdir = minetest.fourdir_to_dir(minetest.get_node(pos).param2)
	local otherpanel = vector.add(pos,vector.rotate_around_axis(fdir,vector.new(0,1,0),-math.pi/2))
	local positions = {
		pos,
		vector.add(pos,vector.new(0,1,0)),
		vector.add(pos,vector.new(0,2,0)),
		otherpanel,
		vector.add(otherpanel,vector.new(0,1,0)),
		vector.add(otherpanel,vector.new(0,2,0)),
	}
	local oldnodes = {}
	for i,position in ipairs(positions) do
		oldnodes[i] = minetest.get_node(position)
	end
	local erefs = celevator.drives.entity.nodestoentities(positions)
	hwdoors_moving[hash] = {
		direction = "open",
		positions = positions,
		nodes = oldnodes,
		time = 0,
		opendir = vector.rotate_around_axis(fdir,vector.new(0,1,0),-math.pi/2),
	}
	celevator.doors.erefs[hash] = erefs
	celevator.storage:set_string("hwdoors_moving",minetest.serialize(hwdoors_moving))
	minetest.set_node(pos,{name="celevator:hwdoor_placeholder"})
	local pmeta = minetest.get_meta(pos)
	pmeta:set_string("data",minetest.serialize(hwdoors_moving[hash]))
	pmeta:set_string("state","opening")
end

function celevator.doors.hwclose(pos)
	local hwdoors_moving = minetest.deserialize(celevator.storage:get_string("hwdoors_moving")) or {}
	local hash = minetest.hash_node_position(pos)
	if hwdoors_moving[hash] then
		return
	end
	local pmeta = minetest.get_meta(pos)
	local state = pmeta:get_string("state")
	if state ~= "open" then return end
	local data = minetest.deserialize(pmeta:get_string("data"))
	if not data then return end
	for i=1,6,1 do
		minetest.set_node(data.positions[i],data.nodes[i])
	end
	data.direction = "close"
	data.time = 0
	local erefs = celevator.drives.entity.nodestoentities(data.positions,"celevator:hwdoor_moving")
	local foffset = vector.multiply(data.opendir,2)
	local soffset = data.opendir
	for i=1,3,1 do
		erefs[i]:set_pos(vector.add(erefs[i]:get_pos(),foffset))
	end
	for i=4,6,1 do
		erefs[i]:set_pos(vector.add(erefs[i]:get_pos(),soffset))
	end
	celevator.doors.erefs[hash] = erefs
	hwdoors_moving[hash] = data
	celevator.storage:set_string("hwdoors_moving",minetest.serialize(hwdoors_moving))
end

function celevator.doors.step(dtime)
	local hwdoors_moving = minetest.deserialize(celevator.storage:get_string("hwdoors_moving")) or {}
	local save = false
	for hash,data in pairs(hwdoors_moving) do
		save = true
		local present = celevator.doors.erefs[hash]
		if present then
			for i=1,6,1 do
				if not celevator.doors.erefs[hash][i]:get_pos() then
					present = false
					break
				end
			end
		end
		if present then
			if data.direction == "open" then
				data.time = data.time+dtime
				local vel = math.sin(data.time)
				for i=1,3,1 do
					celevator.doors.erefs[hash][i]:set_velocity(vector.multiply(data.opendir,vel))
				end
				for i=4,6,1 do
					celevator.doors.erefs[hash][i]:set_velocity(vector.multiply(data.opendir,vel/2))
				end
				if data.time >= math.pi then
					for i=1,6,1 do
						celevator.doors.erefs[hash][i]:remove()
					end
					minetest.get_meta(data.positions[1]):set_string("state","open")
					hwdoors_moving[hash] = nil
				end
			elseif data.direction == "close" then
				data.time = data.time+(0.66*dtime)
				local vel = math.sin(data.time)
				for i=1,3,1 do
					celevator.doors.erefs[hash][i]:set_velocity(vector.multiply(data.opendir,vel*-0.66))
				end
				for i=4,6,1 do
					celevator.doors.erefs[hash][i]:set_velocity(vector.multiply(data.opendir,vel/2*-0.66))
				end
				if data.time >= math.pi then
					for i=1,6,1 do
						celevator.doors.erefs[hash][i]:set_pos(data.positions[i])
					end
					celevator.drives.entity.entitiestonodes(celevator.doors.erefs[hash])
					hwdoors_moving[hash] = nil
				end
			end
		else
			for i=1,6,1 do
				celevator.doors.erefs[hash][i]:remove()
			end
			minetest.get_meta(data.positions[1]):set_string("state","open")
			hwdoors_moving[hash] = nil
		end
	end
	if save then
		celevator.storage:set_string("hwdoors_moving",minetest.serialize(hwdoors_moving))
	end
end

minetest.register_globalstep(celevator.doors.step)