Skip to content

Commit 928f04f

Browse files
DesournumberZero
authored andcommittedOct 8, 2017
Rewrite pistons (#362)
1 parent c4a1aa0 commit 928f04f

File tree

2 files changed

+279
-541
lines changed

2 files changed

+279
-541
lines changed
 

‎mesecons_pistons/init.lua

+231-541
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
1-
-- Get mesecon rules of pistons
2-
local piston_rules = {
3-
{x=0, y=0, z=1}, --everything apart from z- (pusher side)
4-
{x=1, y=0, z=0},
5-
{x=-1, y=0, z=0},
6-
{x=1, y=1, z=0},
7-
{x=1, y=-1, z=0},
8-
{x=-1, y=1, z=0},
9-
{x=-1, y=-1, z=0},
10-
{x=0, y=1, z=1},
11-
{x=0, y=-1, z=1}
12-
}
13-
14-
local piston_up_rules = {
15-
{x=0, y=0, z=-1}, --everything apart from y+ (pusher side)
16-
{x=1, y=0, z=0},
17-
{x=-1, y=0, z=0},
18-
{x=0, y=0, z=1},
19-
{x=1, y=-1, z=0},
20-
{x=-1, y=-1, z=0},
21-
{x=0, y=-1, z=1},
22-
{x=0, y=-1, z=-1}
23-
}
24-
25-
local piston_down_rules = {
26-
{x=0, y=0, z=-1}, --everything apart from y- (pusher side)
27-
{x=1, y=0, z=0},
28-
{x=-1, y=0, z=0},
29-
{x=0, y=0, z=1},
30-
{x=1, y=1, z=0},
31-
{x=-1, y=1, z=0},
32-
{x=0, y=1, z=1},
33-
{x=0, y=1, z=-1}
1+
local specs = {
2+
normal = {
3+
offname = "mesecons_pistons:piston_normal_off",
4+
onname = "mesecons_pistons:piston_normal_on",
5+
pusher = "mesecons_pistons:piston_pusher_normal",
6+
},
7+
sticky = {
8+
offname = "mesecons_pistons:piston_sticky_off",
9+
onname = "mesecons_pistons:piston_sticky_on",
10+
pusher = "mesecons_pistons:piston_pusher_sticky",
11+
sticky = true,
12+
},
3413
}
3514

36-
local function piston_get_rules(node)
37-
local rules = piston_rules
38-
for i = 1, node.param2 do
39-
rules = mesecon.rotate_rules_left(rules)
15+
local function get_pistonspec_name(name, part)
16+
if part then
17+
for spec_name, spec in pairs(specs) do
18+
if name == spec[part] then
19+
return spec_name, part
20+
end
21+
end
22+
return
23+
end
24+
for spec_name, spec in pairs(specs) do
25+
for part, value in pairs(spec) do
26+
if name == value then
27+
return spec_name, part
28+
end
29+
end
4030
end
41-
return rules
4231
end
4332

44-
local function piston_facedir_direction(node)
45-
local rules = {{x = 0, y = 0, z = -1}}
46-
for i = 1, node.param2 do
47-
rules = mesecon.rotate_rules_left(rules)
48-
end
49-
return rules[1]
33+
local function get_pistonspec(name, part)
34+
return specs[get_pistonspec_name(name, part)]
5035
end
5136

52-
local function piston_get_direction(dir, node)
53-
if type(dir) == "function" then
54-
return dir(node)
55-
else
56-
return dir
37+
local max_push = mesecon.setting("piston_max_push", 15)
38+
local max_pull = mesecon.setting("piston_max_pull", 15)
39+
40+
-- Get mesecon rules of pistons
41+
local function piston_get_rules(node)
42+
local dir = minetest.facedir_to_dir(node.param2)
43+
for k, v in pairs(dir) do
44+
if v ~= 0 then
45+
dir = {k, -v}
46+
break
47+
end
48+
end
49+
local rules = table.copy(mesecon.rules.default)
50+
for i, rule in ipairs(rules) do
51+
if rule[dir[1]] == dir[2] then
52+
table.remove(rules, i)
53+
end
5754
end
55+
return rules
5856
end
5957

60-
local piston_remove_pusher = function(pos, node)
61-
local pistonspec = minetest.registered_nodes[node.name].mesecons_piston
62-
local dir = piston_get_direction(pistonspec.dir, node)
58+
local function piston_remove_pusher(pos, node)
59+
local pistonspec = get_pistonspec(node.name, "onname")
60+
local dir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
6361
local pusherpos = vector.add(pos, dir)
6462
local pushername = minetest.get_node(pusherpos).name
6563

@@ -74,67 +72,163 @@ local piston_remove_pusher = function(pos, node)
7472
max_hear_distance = 20,
7573
gain = 0.3,
7674
})
77-
nodeupdate(pusherpos)
7875
end
7976

8077
local piston_on = function(pos, node)
81-
local pistonspec = minetest.registered_nodes[node.name].mesecons_piston
82-
83-
local dir = piston_get_direction(pistonspec.dir, node)
84-
local np = vector.add(pos, dir)
85-
local maxpush = mesecon.setting("piston_max_push", 15)
86-
local success, stack, oldstack = mesecon.mvps_push(np, dir, maxpush)
87-
if success then
88-
minetest.set_node(pos, {param2 = node.param2, name = pistonspec.onname})
89-
minetest.set_node(np, {param2 = node.param2, name = pistonspec.pusher})
90-
minetest.sound_play("piston_extend", {
91-
pos = pos,
92-
max_hear_distance = 20,
93-
gain = 0.3,
94-
})
95-
mesecon.mvps_process_stack(stack)
96-
mesecon.mvps_move_objects(np, dir, oldstack)
78+
local pistonspec = get_pistonspec(node.name, "offname")
79+
local dir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
80+
local pusher_pos = vector.add(pos, dir)
81+
local success, stack, oldstack = mesecon.mvps_push(pusher_pos, dir, max_push)
82+
if not success then
83+
return
9784
end
85+
minetest.set_node(pos, {param2 = node.param2, name = pistonspec.onname})
86+
minetest.set_node(pusher_pos, {param2 = node.param2, name = pistonspec.pusher})
87+
minetest.sound_play("piston_extend", {
88+
pos = pos,
89+
max_hear_distance = 20,
90+
gain = 0.3,
91+
})
92+
mesecon.mvps_process_stack(stack)
93+
mesecon.mvps_move_objects(pusher_pos, dir, oldstack)
9894
end
9995

100-
local piston_off = function(pos, node)
101-
local pistonspec = minetest.registered_nodes[node.name].mesecons_piston
96+
local function piston_off(pos, node)
97+
local pistonspec = get_pistonspec(node.name, "onname")
10298
minetest.set_node(pos, {param2 = node.param2, name = pistonspec.offname})
10399
piston_remove_pusher(pos, node)
104100

105-
if pistonspec.sticky then
106-
local maxpull = mesecon.setting("piston_max_pull", 15)
107-
local dir = piston_get_direction(pistonspec.dir, node)
108-
local pullpos = vector.add(pos, vector.multiply(dir, 2))
109-
local stack = mesecon.mvps_pull_single(pullpos, vector.multiply(dir, -1), maxpull)
110-
mesecon.mvps_process_stack(pos, dir, stack)
101+
if not pistonspec.sticky then
102+
return
111103
end
104+
local dir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
105+
local pullpos = vector.add(pos, vector.multiply(dir, 2))
106+
local stack = mesecon.mvps_pull_single(pullpos, vector.multiply(dir, -1), max_pull)
107+
mesecon.mvps_process_stack(pos, dir, stack)
112108
end
113109

114-
local piston_orientate = function(pos, placer)
115-
-- not placed by player
116-
if not placer then return end
117-
118-
-- placer pitch in degrees
119-
local pitch = placer:get_look_pitch() * (180 / math.pi)
110+
local orientations = {
111+
[0] = { 4, 8},
112+
{13, 17},
113+
{10, 6},
114+
{20, 15},
115+
}
120116

117+
local function piston_orientate(pos, placer)
118+
if not placer then
119+
return
120+
end
121+
local pitch = math.deg(placer:get_look_vertical())
121122
local node = minetest.get_node(pos)
122-
local pistonspec = minetest.registered_nodes[node.name].mesecons_piston
123+
if pitch > 55 then
124+
node.param2 = orientations[node.param2][1]
125+
elseif pitch < -55 then
126+
node.param2 = orientations[node.param2][2]
127+
else
128+
return
129+
end
130+
minetest.swap_node(pos, node)
131+
-- minetest.after, because on_placenode for unoriented piston must be processed first
132+
minetest.after(0, mesecon.on_placenode, pos, node)
133+
end
123134

124-
-- looking upwards (pitch > 55) / looking downwards (pitch < -55)
125-
local nn = nil
126-
if pitch > 55 then nn = {name = pistonspec.piston_down} end
127-
if pitch < -55 then nn = {name = pistonspec.piston_up} end
135+
local rotations = {
136+
{0, 16, 20, 12},
137+
{2, 14, 22, 18},
138+
{1, 5, 23, 9},
139+
{3, 11, 21, 7},
140+
{4, 13, 10, 19},
141+
{6, 15, 8, 17},
142+
}
128143

129-
if nn then
130-
minetest.set_node(pos, nn)
131-
-- minetest.after, because on_placenode for unoriented piston must be processed first
132-
minetest.after(0, mesecon.on_placenode, pos, nn)
144+
local function get_rotation(param2)
145+
for a = 1, #rotations do
146+
for f = 1, #rotations[a] do
147+
if rotations[a][f] == param2 then
148+
return a, f
149+
end
150+
end
133151
end
134152
end
135153

154+
local function rotate(param2, mode)
155+
local axis, face = get_rotation(param2)
156+
if mode == screwdriver.ROTATE_FACE then
157+
face = face + 1
158+
if face > 4 then
159+
face = 1
160+
end
161+
elseif mode == screwdriver.ROTATE_AXIS then
162+
axis = axis + 1
163+
if axis > 6 then
164+
axis = 1
165+
end
166+
face = 1
167+
else
168+
return param2
169+
end
170+
return rotations[axis][face]
171+
end
136172

137-
-- Horizontal pistons
173+
local function piston_rotate(pos, node, _, mode)
174+
node.param2 = rotate(node.param2, mode)
175+
minetest.swap_node(pos, node)
176+
mesecon.execute_autoconnect_hooks_now(pos, node)
177+
return true
178+
end
179+
180+
local function piston_rotate_on(pos, node, player, mode)
181+
local pistonspec = get_pistonspec(node.name, "onname")
182+
local dir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
183+
local pusher_pos = vector.add(dir, pos)
184+
local pusher_node = minetest.get_node(pusher_pos)
185+
if pusher_node.name ~= pistonspec.pusher then
186+
return piston_rotate(pos, node, nil, mode)
187+
end
188+
if mode == screwdriver.ROTATE_FACE then
189+
piston_rotate(pusher_pos, pusher_node, nil, mode)
190+
return piston_rotate(pos, node, nil, mode)
191+
elseif mode ~= screwdriver.ROTATE_AXIS then
192+
return false
193+
end
194+
local player_name = player and player:is_player() and player:get_player_name() or ""
195+
local ok, dir_after, pusher_pos_after
196+
for i = 1, 5 do
197+
node.param2 = rotate(node.param2, mode)
198+
dir_after = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
199+
pusher_pos_after = vector.add(dir_after, pos)
200+
local pusher_pos_after_node_name = minetest.get_node(pusher_pos_after).name
201+
local pusher_pos_after_node_def = minetest.registered_nodes[pusher_pos_after_node_name]
202+
if pusher_pos_after_node_def and pusher_pos_after_node_def.buildable_to and
203+
not minetest.is_protected(pusher_pos_after, player_name) then
204+
ok = true
205+
break
206+
end
207+
end
208+
if not ok then
209+
return false
210+
end
211+
pusher_node.param2 = node.param2
212+
minetest.remove_node(pusher_pos)
213+
minetest.set_node(pusher_pos_after, pusher_node)
214+
minetest.swap_node(pos, node)
215+
mesecon.execute_autoconnect_hooks_now(pos, node)
216+
return true
217+
end
218+
219+
local function piston_rotate_pusher(pos, node, player, mode)
220+
local pistonspec = get_pistonspec(node.name, "pusher")
221+
local piston_pos = vector.add(pos, minetest.facedir_to_dir(node.param2))
222+
local piston_node = minetest.get_node(piston_pos)
223+
if piston_node.name ~= pistonspec.onname then
224+
minetest.remove_node(pos) -- Make it possible to remove alone pushers.
225+
return false
226+
end
227+
return piston_rotate_on(piston_pos, piston_node, player, mode)
228+
end
229+
230+
231+
-- Boxes:
138232

139233
local pt = 3/16 -- pusher thickness
140234

@@ -143,28 +237,18 @@ local piston_pusher_box = {
143237
fixed = {
144238
{-2/16, -2/16, -.5 + pt, 2/16, 2/16, .5 + pt},
145239
{-.5 , -.5 , -.5 , .5 , .5 , -.5 + pt},
146-
}
240+
},
147241
}
148242

149243
local piston_on_box = {
150244
type = "fixed",
151245
fixed = {
152246
{-.5, -.5, -.5 + pt, .5, .5, .5}
153-
}
247+
},
154248
}
155249

156250

157-
-- Normal (non-sticky) ones:
158-
159-
local pistonspec_normal = {
160-
offname = "mesecons_pistons:piston_normal_off",
161-
onname = "mesecons_pistons:piston_normal_on",
162-
dir = piston_facedir_direction,
163-
pusher = "mesecons_pistons:piston_pusher_normal",
164-
piston_down = "mesecons_pistons:piston_down_normal_off",
165-
piston_up = "mesecons_pistons:piston_up_normal_off",
166-
}
167-
251+
-- Normal (non-sticky) Pistons:
168252
-- offstate
169253
minetest.register_node("mesecons_pistons:piston_normal_off", {
170254
description = "Piston",
@@ -175,21 +259,22 @@ minetest.register_node("mesecons_pistons:piston_normal_off", {
175259
"mesecons_piston_right.png",
176260
"mesecons_piston_back.png",
177261
"mesecons_piston_pusher_front.png"
178-
},
262+
},
179263
groups = {cracky = 3},
180264
paramtype2 = "facedir",
181265
after_place_node = piston_orientate,
182-
mesecons_piston = pistonspec_normal,
183266
sounds = default.node_sound_wood_defaults(),
184267
mesecons = {effector={
185268
action_on = piston_on,
186-
rules = piston_get_rules
269+
rules = piston_get_rules,
187270
}},
271+
on_rotate = piston_rotate,
188272
on_blast = mesecon.on_blastnode,
189273
})
190274

191275
-- onstate
192276
minetest.register_node("mesecons_pistons:piston_normal_on", {
277+
description = "Activated Piston Base",
193278
drawtype = "nodebox",
194279
tiles = {
195280
"mesecons_piston_top.png",
@@ -198,27 +283,26 @@ minetest.register_node("mesecons_pistons:piston_normal_on", {
198283
"mesecons_piston_right.png",
199284
"mesecons_piston_back.png",
200285
"mesecons_piston_on_front.png"
201-
},
202-
inventory_image = "mesecons_piston_top.png",
203-
wield_image = "mesecons_piston_top.png",
286+
},
204287
groups = {cracky = 3, not_in_creative_inventory = 1},
205288
paramtype = "light",
206289
paramtype2 = "facedir",
207290
drop = "mesecons_pistons:piston_normal_off",
208291
after_dig_node = piston_remove_pusher,
209292
node_box = piston_on_box,
210293
selection_box = piston_on_box,
211-
mesecons_piston = pistonspec_normal,
212294
sounds = default.node_sound_wood_defaults(),
213295
mesecons = {effector={
214296
action_off = piston_off,
215-
rules = piston_get_rules
297+
rules = piston_get_rules,
216298
}},
299+
on_rotate = piston_rotate_on,
217300
on_blast = mesecon.on_blastnode,
218301
})
219302

220303
-- pusher
221304
minetest.register_node("mesecons_pistons:piston_pusher_normal", {
305+
description = "Piston Pusher",
222306
drawtype = "nodebox",
223307
tiles = {
224308
"mesecons_piston_pusher_top.png",
@@ -227,28 +311,18 @@ minetest.register_node("mesecons_pistons:piston_pusher_normal", {
227311
"mesecons_piston_pusher_right.png",
228312
"mesecons_piston_pusher_back.png",
229313
"mesecons_piston_pusher_front.png"
230-
},
314+
},
315+
groups = {not_in_creative_inventory = 1},
231316
paramtype = "light",
232317
paramtype2 = "facedir",
233318
diggable = false,
234-
corresponding_piston = "mesecons_pistons:piston_normal_on",
235319
selection_box = piston_pusher_box,
236320
node_box = piston_pusher_box,
321+
on_rotate = piston_rotate_pusher,
237322
drop = "",
238323
})
239324

240325
-- Sticky ones
241-
242-
local pistonspec_sticky = {
243-
offname = "mesecons_pistons:piston_sticky_off",
244-
onname = "mesecons_pistons:piston_sticky_on",
245-
dir = piston_facedir_direction,
246-
pusher = "mesecons_pistons:piston_pusher_sticky",
247-
sticky = true,
248-
piston_down = "mesecons_pistons:piston_down_sticky_off",
249-
piston_up = "mesecons_pistons:piston_up_sticky_off",
250-
}
251-
252326
-- offstate
253327
minetest.register_node("mesecons_pistons:piston_sticky_off", {
254328
description = "Sticky Piston",
@@ -259,21 +333,22 @@ minetest.register_node("mesecons_pistons:piston_sticky_off", {
259333
"mesecons_piston_right.png",
260334
"mesecons_piston_back.png",
261335
"mesecons_piston_pusher_front_sticky.png"
262-
},
336+
},
263337
groups = {cracky = 3},
264338
paramtype2 = "facedir",
265339
after_place_node = piston_orientate,
266-
mesecons_piston = pistonspec_sticky,
267340
sounds = default.node_sound_wood_defaults(),
268341
mesecons = {effector={
269342
action_on = piston_on,
270-
rules = piston_get_rules
343+
rules = piston_get_rules,
271344
}},
345+
on_rotate = piston_rotate,
272346
on_blast = mesecon.on_blastnode,
273347
})
274348

275349
-- onstate
276350
minetest.register_node("mesecons_pistons:piston_sticky_on", {
351+
description = "Activated Sticky Piston Base",
277352
drawtype = "nodebox",
278353
tiles = {
279354
"mesecons_piston_top.png",
@@ -282,27 +357,26 @@ minetest.register_node("mesecons_pistons:piston_sticky_on", {
282357
"mesecons_piston_right.png",
283358
"mesecons_piston_back.png",
284359
"mesecons_piston_on_front.png"
285-
},
286-
inventory_image = "mesecons_piston_top.png",
287-
wield_image = "mesecons_piston_top.png",
360+
},
288361
groups = {cracky = 3, not_in_creative_inventory = 1},
289362
paramtype = "light",
290363
paramtype2 = "facedir",
291364
drop = "mesecons_pistons:piston_sticky_off",
292365
after_dig_node = piston_remove_pusher,
293366
node_box = piston_on_box,
294367
selection_box = piston_on_box,
295-
mesecons_piston = pistonspec_sticky,
296368
sounds = default.node_sound_wood_defaults(),
297369
mesecons = {effector={
298370
action_off = piston_off,
299-
rules = piston_get_rules
371+
rules = piston_get_rules,
300372
}},
373+
on_rotate = piston_rotate_on,
301374
on_blast = mesecon.on_blastnode,
302375
})
303376

304377
-- pusher
305378
minetest.register_node("mesecons_pistons:piston_pusher_sticky", {
379+
description = "Sticky Piston Pusher",
306380
drawtype = "nodebox",
307381
tiles = {
308382
"mesecons_piston_pusher_top.png",
@@ -311,412 +385,36 @@ minetest.register_node("mesecons_pistons:piston_pusher_sticky", {
311385
"mesecons_piston_pusher_right.png",
312386
"mesecons_piston_pusher_back.png",
313387
"mesecons_piston_pusher_front_sticky.png"
314-
},
388+
},
389+
groups = {not_in_creative_inventory = 1},
315390
paramtype = "light",
316391
paramtype2 = "facedir",
317392
diggable = false,
318-
corresponding_piston = "mesecons_pistons:piston_sticky_on",
319393
selection_box = piston_pusher_box,
320394
node_box = piston_pusher_box,
321-
drop = "",
322-
})
323-
324-
--
325-
--
326-
-- UP
327-
--
328-
--
329-
330-
local piston_up_pusher_box = {
331-
type = "fixed",
332-
fixed = {
333-
{-2/16, -.5 - pt, -2/16, 2/16, .5 - pt, 2/16},
334-
{-.5 , .5 - pt, -.5 , .5 , .5 , .5},
335-
}
336-
}
337-
338-
local piston_up_on_box = {
339-
type = "fixed",
340-
fixed = {
341-
{-.5, -.5, -.5 , .5, .5-pt, .5}
342-
}
343-
}
344-
345-
-- Normal
346-
347-
local pistonspec_normal_up = {
348-
offname = "mesecons_pistons:piston_up_normal_off",
349-
onname = "mesecons_pistons:piston_up_normal_on",
350-
dir = {x = 0, y = 1, z = 0},
351-
pusher = "mesecons_pistons:piston_up_pusher_normal"
352-
}
353-
354-
-- offstate
355-
minetest.register_node("mesecons_pistons:piston_up_normal_off", {
356-
tiles = {
357-
"mesecons_piston_pusher_front.png",
358-
"mesecons_piston_back.png",
359-
"mesecons_piston_left.png^[transformR270",
360-
"mesecons_piston_right.png^[transformR90",
361-
"mesecons_piston_bottom.png",
362-
"mesecons_piston_top.png^[transformR180",
363-
},
364-
inventory_image = "mesecons_piston_top.png",
365-
wield_image = "mesecons_piston_top.png",
366-
groups = {cracky = 3, not_in_creative_inventory = 1},
367-
paramtype2 = "facedir",
368-
drop = "mesecons_pistons:piston_normal_off",
369-
mesecons_piston = pistonspec_normal_up,
370-
mesecons = {effector={
371-
action_on = piston_on,
372-
rules = piston_up_rules,
373-
}},
374-
on_blast = mesecon.on_blastnode,
375-
})
376-
377-
-- onstate
378-
minetest.register_node("mesecons_pistons:piston_up_normal_on", {
379-
drawtype = "nodebox",
380-
tiles = {
381-
"mesecons_piston_on_front.png",
382-
"mesecons_piston_back.png",
383-
"mesecons_piston_left.png^[transformR270",
384-
"mesecons_piston_right.png^[transformR90",
385-
"mesecons_piston_bottom.png",
386-
"mesecons_piston_top.png^[transformR180",
387-
},
388-
inventory_image = "mesecons_piston_top.png",
389-
wield_image = "mesecons_piston_top.png",
390-
groups = {cracky = 3, not_in_creative_inventory = 1},
391-
paramtype = "light",
392-
paramtype2 = "facedir",
393-
drop = "mesecons_pistons:piston_normal_off",
394-
after_dig_node = piston_remove_pusher,
395-
node_box = piston_up_on_box,
396-
selection_box = piston_up_on_box,
397-
mesecons_piston = pistonspec_normal_up,
398-
sounds = default.node_sound_wood_defaults(),
399-
mesecons = {effector={
400-
action_off = piston_off,
401-
rules = piston_up_rules,
402-
}},
403-
on_blast = mesecon.on_blastnode,
404-
})
405-
406-
-- pusher
407-
minetest.register_node("mesecons_pistons:piston_up_pusher_normal", {
408-
drawtype = "nodebox",
409-
tiles = {
410-
"mesecons_piston_pusher_front.png",
411-
"mesecons_piston_pusher_back.png",
412-
"mesecons_piston_pusher_left.png^[transformR270",
413-
"mesecons_piston_pusher_right.png^[transformR90",
414-
"mesecons_piston_pusher_bottom.png",
415-
"mesecons_piston_pusher_top.png^[transformR180",
416-
},
417-
paramtype = "light",
418-
paramtype2 = "facedir",
419-
diggable = false,
420-
corresponding_piston = "mesecons_pistons:piston_up_normal_on",
421-
selection_box = piston_up_pusher_box,
422-
node_box = piston_up_pusher_box,
423-
drop = "",
424-
})
425-
426-
427-
428-
-- Sticky
429-
430-
431-
local pistonspec_sticky_up = {
432-
offname = "mesecons_pistons:piston_up_sticky_off",
433-
onname = "mesecons_pistons:piston_up_sticky_on",
434-
dir = {x = 0, y = 1, z = 0},
435-
pusher = "mesecons_pistons:piston_up_pusher_sticky",
436-
sticky = true
437-
}
438-
439-
-- offstate
440-
minetest.register_node("mesecons_pistons:piston_up_sticky_off", {
441-
tiles = {
442-
"mesecons_piston_pusher_front_sticky.png",
443-
"mesecons_piston_back.png",
444-
"mesecons_piston_left.png^[transformR270",
445-
"mesecons_piston_right.png^[transformR90",
446-
"mesecons_piston_bottom.png",
447-
"mesecons_piston_top.png^[transformR180",
448-
"mesecons_piston_tb.png"
449-
},
450-
inventory_image = "mesecons_piston_top.png",
451-
wield_image = "mesecons_piston_top.png",
452-
groups = {cracky = 3, not_in_creative_inventory = 1},
453-
paramtype2 = "facedir",
454-
drop = "mesecons_pistons:piston_sticky_off",
455-
mesecons_piston = pistonspec_sticky_up,
456-
sounds = default.node_sound_wood_defaults(),
457-
mesecons = {effector={
458-
action_on = piston_on,
459-
rules = piston_up_rules,
460-
}},
461-
on_blast = mesecon.on_blastnode,
462-
})
463-
464-
-- onstate
465-
minetest.register_node("mesecons_pistons:piston_up_sticky_on", {
466-
drawtype = "nodebox",
467-
tiles = {
468-
"mesecons_piston_on_front.png",
469-
"mesecons_piston_back.png",
470-
"mesecons_piston_left.png^[transformR270",
471-
"mesecons_piston_right.png^[transformR90",
472-
"mesecons_piston_bottom.png",
473-
"mesecons_piston_top.png^[transformR180",
474-
},
475-
inventory_image = "mesecons_piston_top.png",
476-
wield_image = "mesecons_piston_top.png",
477-
groups = {cracky = 3, not_in_creative_inventory = 1},
478-
paramtype = "light",
479-
paramtype2 = "facedir",
480-
drop = "mesecons_pistons:piston_normal_off",
481-
after_dig_node = piston_remove_pusher,
482-
node_box = piston_up_on_box,
483-
selection_box = piston_up_on_box,
484-
mesecons_piston = pistonspec_sticky_up,
485-
sounds = default.node_sound_wood_defaults(),
486-
mesecons = {effector={
487-
action_off = piston_off,
488-
rules = piston_up_rules,
489-
}},
490-
on_blast = mesecon.on_blastnode,
491-
})
492-
493-
-- pusher
494-
minetest.register_node("mesecons_pistons:piston_up_pusher_sticky", {
495-
drawtype = "nodebox",
496-
tiles = {
497-
"mesecons_piston_pusher_front_sticky.png",
498-
"mesecons_piston_pusher_back.png",
499-
"mesecons_piston_pusher_left.png^[transformR270",
500-
"mesecons_piston_pusher_right.png^[transformR90",
501-
"mesecons_piston_pusher_bottom.png",
502-
"mesecons_piston_pusher_top.png^[transformR180",
503-
},
504-
paramtype = "light",
505-
paramtype2 = "facedir",
506-
diggable = false,
507-
corresponding_piston = "mesecons_pistons:piston_up_sticky_on",
508-
selection_box = piston_up_pusher_box,
509-
node_box = piston_up_pusher_box,
510-
drop = "",
511-
})
512-
513-
--
514-
--
515-
-- DOWN
516-
--
517-
--
518-
519-
local piston_down_pusher_box = {
520-
type = "fixed",
521-
fixed = {
522-
{-2/16, -.5 + pt, -2/16, 2/16, .5 + pt, 2/16},
523-
{-.5 , -.5 , -.5 , .5 , -.5 + pt, .5},
524-
}
525-
}
526-
527-
local piston_down_on_box = {
528-
type = "fixed",
529-
fixed = {
530-
{-.5, -.5+pt, -.5 , .5, .5, .5}
531-
}
532-
}
533-
534-
535-
536-
-- Normal
537-
538-
local pistonspec_normal_down = {
539-
offname = "mesecons_pistons:piston_down_normal_off",
540-
onname = "mesecons_pistons:piston_down_normal_on",
541-
dir = {x = 0, y = -1, z = 0},
542-
pusher = "mesecons_pistons:piston_down_pusher_normal",
543-
}
544-
545-
-- offstate
546-
minetest.register_node("mesecons_pistons:piston_down_normal_off", {
547-
tiles = {
548-
"mesecons_piston_back.png",
549-
"mesecons_piston_pusher_front.png",
550-
"mesecons_piston_left.png^[transformR90",
551-
"mesecons_piston_right.png^[transformR270",
552-
"mesecons_piston_bottom.png^[transformR180",
553-
"mesecons_piston_top.png",
554-
},
555-
inventory_image = "mesecons_piston_top.png",
556-
wield_image = "mesecons_piston_top.png",
557-
groups = {cracky = 3, not_in_creative_inventory = 1},
558-
paramtype2 = "facedir",
559-
drop = "mesecons_pistons:piston_normal_off",
560-
mesecons_piston = pistonspec_normal_down,
561-
sounds = default.node_sound_wood_defaults(),
562-
mesecons = {effector={
563-
action_on = piston_on,
564-
rules = piston_down_rules,
565-
}},
566-
on_blast = mesecon.on_blastnode,
567-
})
568-
569-
-- onstate
570-
minetest.register_node("mesecons_pistons:piston_down_normal_on", {
571-
drawtype = "nodebox",
572-
tiles = {
573-
"mesecons_piston_back.png",
574-
"mesecons_piston_on_front.png",
575-
"mesecons_piston_left.png^[transformR90",
576-
"mesecons_piston_right.png^[transformR270",
577-
"mesecons_piston_bottom.png^[transformR180",
578-
"mesecons_piston_top.png",
579-
},
580-
inventory_image = "mesecons_piston_top.png",
581-
wield_image = "mesecons_piston_top.png",
582-
groups = {cracky = 3, not_in_creative_inventory = 1},
583-
paramtype = "light",
584-
paramtype2 = "facedir",
585-
drop = "mesecons_pistons:piston_normal_off",
586-
after_dig_node = piston_remove_pusher,
587-
node_box = piston_down_on_box,
588-
selection_box = piston_down_on_box,
589-
mesecons_piston = pistonspec_normal_down,
590-
sounds = default.node_sound_wood_defaults(),
591-
mesecons = {effector={
592-
action_off = piston_off,
593-
rules = piston_down_rules,
594-
}},
595-
on_blast = mesecon.on_blastnode,
596-
})
597-
598-
-- pusher
599-
minetest.register_node("mesecons_pistons:piston_down_pusher_normal", {
600-
drawtype = "nodebox",
601-
tiles = {
602-
"mesecons_piston_pusher_back.png",
603-
"mesecons_piston_pusher_front.png",
604-
"mesecons_piston_pusher_left.png^[transformR90",
605-
"mesecons_piston_pusher_right.png^[transformR270",
606-
"mesecons_piston_pusher_bottom.png^[transformR180",
607-
"mesecons_piston_pusher_top.png",
608-
},
609-
paramtype = "light",
610-
paramtype2 = "facedir",
611-
diggable = false,
612-
corresponding_piston = "mesecons_pistons:piston_down_normal_on",
613-
selection_box = piston_down_pusher_box,
614-
node_box = piston_down_pusher_box,
615-
drop = "",
616-
})
617-
618-
-- Sticky
619-
620-
local pistonspec_sticky_down = {
621-
onname = "mesecons_pistons:piston_down_sticky_on",
622-
offname = "mesecons_pistons:piston_down_sticky_off",
623-
dir = {x = 0, y = -1, z = 0},
624-
pusher = "mesecons_pistons:piston_down_pusher_sticky",
625-
sticky = true
626-
}
627-
628-
-- offstate
629-
minetest.register_node("mesecons_pistons:piston_down_sticky_off", {
630-
tiles = {
631-
"mesecons_piston_back.png",
632-
"mesecons_piston_pusher_front_sticky.png",
633-
"mesecons_piston_left.png^[transformR90",
634-
"mesecons_piston_right.png^[transformR270",
635-
"mesecons_piston_bottom.png^[transformR180",
636-
"mesecons_piston_top.png",
637-
},
638-
inventory_image = "mesecons_piston_top.png",
639-
wield_image = "mesecons_piston_top.png",
640-
groups = {cracky = 3, not_in_creative_inventory = 1},
641-
paramtype2 = "facedir",
642-
drop = "mesecons_pistons:piston_sticky_off",
643-
mesecons_piston = pistonspec_sticky_down,
644-
sounds = default.node_sound_wood_defaults(),
645-
mesecons = {effector={
646-
action_on = piston_on,
647-
rules = piston_down_rules,
648-
}},
649-
on_blast = mesecon.on_blastnode,
650-
})
651-
652-
-- onstate
653-
minetest.register_node("mesecons_pistons:piston_down_sticky_on", {
654-
drawtype = "nodebox",
655-
tiles = {
656-
"mesecons_piston_back.png",
657-
"mesecons_piston_on_front.png",
658-
"mesecons_piston_left.png^[transformR90",
659-
"mesecons_piston_right.png^[transformR270",
660-
"mesecons_piston_bottom.png^[transformR180",
661-
"mesecons_piston_top.png",
662-
},
663-
inventory_image = "mesecons_piston_top.png",
664-
wield_image = "mesecons_piston_top.png",
665-
groups = {cracky = 3, not_in_creative_inventory = 1},
666-
paramtype = "light",
667-
paramtype2 = "facedir",
668-
drop = "mesecons_pistons:piston_sticky_off",
669-
after_dig_node = piston_remove_pusher,
670-
node_box = piston_down_on_box,
671-
selection_box = piston_down_on_box,
672-
mesecons_piston = pistonspec_sticky_down,
673-
sounds = default.node_sound_wood_defaults(),
674-
mesecons = {effector={
675-
action_off = piston_off,
676-
rules = piston_down_rules,
677-
}},
678-
on_blast = mesecon.on_blastnode,
679-
})
680-
681-
-- pusher
682-
minetest.register_node("mesecons_pistons:piston_down_pusher_sticky", {
683-
drawtype = "nodebox",
684-
tiles = {
685-
"mesecons_piston_pusher_back.png",
686-
"mesecons_piston_pusher_front_sticky.png",
687-
"mesecons_piston_pusher_left.png^[transformR90",
688-
"mesecons_piston_pusher_right.png^[transformR270",
689-
"mesecons_piston_pusher_bottom.png^[transformR180",
690-
"mesecons_piston_pusher_top.png",
691-
},
692-
paramtype = "light",
693-
paramtype2 = "facedir",
694-
diggable = false,
695-
corresponding_piston = "mesecons_pistons:piston_down_sticky_on",
696-
selection_box = piston_down_pusher_box,
697-
node_box = piston_down_pusher_box,
395+
on_rotate = piston_rotate_pusher,
698396
drop = "",
699397
})
700398

701399

702400
-- Register pushers as stoppers if they would be seperated from the piston
703-
local piston_pusher_get_stopper = function (node, dir, stack, stackid)
401+
local function piston_pusher_get_stopper(node, dir, stack, stackid)
704402
if (stack[stackid + 1]
705-
and stack[stackid + 1].node.name == minetest.registered_nodes[node.name].corresponding_piston
403+
and stack[stackid + 1].node.name == get_pistonspec(node.name, "pusher").onname
706404
and stack[stackid + 1].node.param2 == node.param2)
707405
or (stack[stackid - 1]
708-
and stack[stackid - 1].node.name == minetest.registered_nodes[node.name].corresponding_piston
406+
and stack[stackid - 1].node.name == get_pistonspec(node.name, "pusher").onname
709407
and stack[stackid - 1].node.param2 == node.param2) then
710408
return false
711409
end
712410
return true
713411
end
714412

715-
local piston_pusher_up_down_get_stopper = function (node, dir, stack, stackid)
413+
local function piston_pusher_up_down_get_stopper(node, dir, stack, stackid)
716414
if (stack[stackid + 1]
717-
and stack[stackid + 1].node.name == minetest.registered_nodes[node.name].corresponding_piston)
415+
and stack[stackid + 1].node.name == get_pistonspec(node.name, "pusher").onname)
718416
or (stack[stackid - 1]
719-
and stack[stackid - 1].node.name == minetest.registered_nodes[node.name].corresponding_piston) then
417+
and stack[stackid - 1].node.name == get_pistonspec(node.name, "pusher").onname) then
720418
return false
721419
end
722420
return true
@@ -725,31 +423,24 @@ end
725423
mesecon.register_mvps_stopper("mesecons_pistons:piston_pusher_normal", piston_pusher_get_stopper)
726424
mesecon.register_mvps_stopper("mesecons_pistons:piston_pusher_sticky", piston_pusher_get_stopper)
727425

728-
mesecon.register_mvps_stopper("mesecons_pistons:piston_up_pusher_normal", piston_pusher_up_down_get_stopper)
729-
mesecon.register_mvps_stopper("mesecons_pistons:piston_up_pusher_sticky", piston_pusher_up_down_get_stopper)
730-
731-
mesecon.register_mvps_stopper("mesecons_pistons:piston_down_pusher_normal", piston_pusher_up_down_get_stopper)
732-
mesecon.register_mvps_stopper("mesecons_pistons:piston_down_pusher_sticky", piston_pusher_up_down_get_stopper)
733-
734426

735427
-- Register pistons as stoppers if they would be seperated from the stopper
736428
local piston_up_down_get_stopper = function (node, dir, stack, stackid)
737429
if (stack[stackid + 1]
738-
and stack[stackid + 1].node.name == minetest.registered_nodes[node.name].mesecons_piston.pusher)
430+
and stack[stackid + 1].node.name == get_pistonspec(node.name, "onname").pusher)
739431
or (stack[stackid - 1]
740-
and stack[stackid - 1].node.name == minetest.registered_nodes[node.name].mesecons_piston.pusher) then
432+
and stack[stackid - 1].node.name == get_pistonspec(node.name, "onname").pusher) then
741433
return false
742434
end
743435
return true
744436
end
745437

746-
local piston_get_stopper = function (node, dir, stack, stackid)
747-
pistonspec = minetest.registered_nodes[node.name].mesecons_piston
748-
dir = piston_get_direction(pistonspec.dir, node)
749-
local pusherpos = vector.add(stack[stackid].pos, dir)
438+
local function piston_get_stopper(node, dir, stack, stackid)
439+
local pistonspec = get_pistonspec(node.name, "onname")
440+
local dir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
441+
local pusherpos = vector.add(stack[stackid].pos, dir)
750442
local pushernode = minetest.get_node(pusherpos)
751-
752-
if minetest.registered_nodes[node.name].mesecons_piston.pusher == pushernode.name then
443+
if pistonspec.pusher == pushernode.name then
753444
for _, s in ipairs(stack) do
754445
if vector.equals(s.pos, pusherpos) -- pusher is also to be pushed
755446
and s.node.param2 == node.param2 then
@@ -763,18 +454,13 @@ end
763454
mesecon.register_mvps_stopper("mesecons_pistons:piston_normal_on", piston_get_stopper)
764455
mesecon.register_mvps_stopper("mesecons_pistons:piston_sticky_on", piston_get_stopper)
765456

766-
mesecon.register_mvps_stopper("mesecons_pistons:piston_up_normal_on", piston_up_down_get_stopper)
767-
mesecon.register_mvps_stopper("mesecons_pistons:piston_up_sticky_on", piston_up_down_get_stopper)
768-
769-
mesecon.register_mvps_stopper("mesecons_pistons:piston_down_normal_on", piston_up_down_get_stopper)
770-
mesecon.register_mvps_stopper("mesecons_pistons:piston_down_sticky_on", piston_up_down_get_stopper)
771457

772458
--craft recipes
773459
minetest.register_craft({
774460
output = "mesecons_pistons:piston_normal_off 2",
775461
recipe = {
776-
{"group:wood", "group:wood", "group:wood"},
777-
{"default:cobble", "default:steel_ingot", "default:cobble"},
462+
{"group:wood", "group:wood", "group:wood"},
463+
{"default:cobble", "default:steel_ingot", "default:cobble"},
778464
{"default:cobble", "group:mesecon_conductor_craftable", "default:cobble"},
779465
}
780466
})
@@ -786,3 +472,7 @@ minetest.register_craft({
786472
{"mesecons_pistons:piston_normal_off"},
787473
}
788474
})
475+
476+
477+
-- load legacy code
478+
dofile(minetest.get_modpath("mesecons_pistons")..DIR_DELIM.."legacy.lua")

‎mesecons_pistons/legacy.lua

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
local ground_dir = {
2+
[0] = {x = 0, y = -1, z = 0},
3+
{x = 0, y = 0, z = -1},
4+
{x = 0, y = 0, z = 1},
5+
{x = -1, y = 0, z = 0},
6+
{x = 1, y = 0, z = 0},
7+
{x = 0, y = 1, z = 0},
8+
}
9+
10+
minetest.register_lbm({
11+
label = "Upgrade legacy pistons pointing up",
12+
name = "mesecons_pistons:replace_legacy_piston_up",
13+
nodenames = {
14+
"mesecons_pistons:piston_up_normal_off",
15+
"mesecons_pistons:piston_up_normal_on",
16+
"mesecons_pistons:piston_up_pusher_normal",
17+
"mesecons_pistons:piston_up_sticky_off",
18+
"mesecons_pistons:piston_up_sticky_on",
19+
"mesecons_pistons:piston_up_pusher_sticky",
20+
},
21+
run_at_every_load = false,
22+
action = function(pos, node)
23+
local dir = ground_dir[math.floor(node.param2/4)]
24+
node.param2 = minetest.dir_to_facedir(dir, true)
25+
node.name = node.name:sub(1, 24)..node.name:sub(28)
26+
minetest.swap_node(pos, node)
27+
end,
28+
})
29+
30+
minetest.register_lbm({
31+
label = "Upgrade legacy pistons pointing down",
32+
name = "mesecons_pistons:replace_legacy_piston_down",
33+
nodenames = {
34+
"mesecons_pistons:piston_down_normal_off",
35+
"mesecons_pistons:piston_down_normal_on",
36+
"mesecons_pistons:piston_down_pusher_normal",
37+
"mesecons_pistons:piston_down_sticky_off",
38+
"mesecons_pistons:piston_down_sticky_on",
39+
"mesecons_pistons:piston_down_pusher_sticky",
40+
},
41+
run_at_every_load = false,
42+
action = function(pos, node)
43+
local dir = vector.multiply(ground_dir[math.floor(node.param2/4)], -1)
44+
node.param2 = minetest.dir_to_facedir(dir, true)
45+
node.name = node.name:sub(1, 24)..node.name:sub(30)
46+
minetest.swap_node(pos, node)
47+
end,
48+
})

0 commit comments

Comments
 (0)
Please sign in to comment.