Skip to content

Commit 7094f0b

Browse files
DesournumberZero
authored andcommittedOct 8, 2017
Improve movestones (#366)
Improve the code and add vertical movestones
1 parent b08e93f commit 7094f0b

File tree

1 file changed

+149
-95
lines changed

1 file changed

+149
-95
lines changed
 

‎mesecons_movestones/init.lua

+149-95
Original file line numberDiff line numberDiff line change
@@ -8,126 +8,151 @@
88
-- Pushes all block in front of it
99
-- Pull all blocks in its back
1010

11-
function mesecon.get_movestone_direction(pos)
12-
local lpos
13-
local rules = {
14-
{x=0, y=1, z=-1},
15-
{x=0, y=0, z=-1},
16-
{x=0, y=-1, z=-1},
17-
{x=0, y=1, z=1},
18-
{x=0, y=-1, z=1},
19-
{x=0, y=0, z=1},
20-
{x=1, y=0, z=0},
21-
{x=1, y=1, z=0},
22-
{x=1, y=-1, z=0},
23-
{x=-1, y=1, z=0},
24-
{x=-1, y=-1, z=0},
25-
{x=-1, y=0, z=0}}
26-
27-
lpos = {x=pos.x+1, y=pos.y, z=pos.z}
28-
for n = 1, 3 do
29-
if mesecon.is_power_on(lpos, rules[n]) then
30-
return {x=0, y=0, z=-1}
11+
-- settings:
12+
local timer_interval = 1 / mesecon.setting("movestone_speed", 3)
13+
local max_push = mesecon.setting("movestone_max_push", 50)
14+
local max_pull = mesecon.setting("movestone_max_pull", 50)
15+
16+
-- helper functions:
17+
local function get_movestone_direction(rulename, is_vertical)
18+
if is_vertical then
19+
if rulename.z > 0 then
20+
return {x = 0, y = -1, z = 0}
21+
elseif rulename.z < 0 then
22+
return {x = 0, y = 1, z = 0}
23+
elseif rulename.x > 0 then
24+
return {x = 0, y = -1, z = 0}
25+
elseif rulename.x < 0 then
26+
return {x = 0, y = 1, z = 0}
3127
end
32-
end
33-
34-
lpos = {x = pos.x-1, y = pos.y, z = pos.z}
35-
for n=4, 6 do
36-
if mesecon.is_power_on(lpos, rules[n]) then
37-
return {x=0, y=0, z=1}
38-
end
39-
end
40-
41-
lpos = {x = pos.x, y = pos.y, z = pos.z+1}
42-
for n=7, 9 do
43-
if mesecon.is_power_on(lpos, rules[n]) then
44-
return {x=-1, y=0, z=0}
45-
end
46-
end
47-
48-
lpos = {x = pos.x, y = pos.y, z = pos.z-1}
49-
for n=10, 12 do
50-
if mesecon.is_power_on(lpos, rules[n]) then
51-
return {x=1, y=0, z=0}
28+
else
29+
if rulename.z > 0 then
30+
return {x = -1, y = 0, z = 0}
31+
elseif rulename.z < 0 then
32+
return {x = 1, y = 0, z = 0}
33+
elseif rulename.x > 0 then
34+
return {x = 0, y = 0, z = -1}
35+
elseif rulename.x < 0 then
36+
return {x = 0, y = 0, z = 1}
5237
end
5338
end
5439
end
5540

56-
function mesecon.register_movestone(name, def, is_sticky)
57-
local timer_interval = 1 / mesecon.setting("movestone_speed", 3)
58-
local name_active = name.."_active"
59-
60-
local function movestone_move (pos)
61-
if minetest.get_node(pos).name ~= name_active then
62-
return
63-
end
64-
65-
local direction = mesecon.get_movestone_direction(pos)
66-
if not direction then
67-
minetest.set_node(pos, {name = name})
68-
return
69-
end
41+
-- registration functions:
42+
function mesecon.register_movestone(name, def, is_sticky, is_vertical)
43+
local function movestone_move(pos, node, rulename)
44+
local direction = get_movestone_direction(rulename, is_vertical)
7045
local frontpos = vector.add(pos, direction)
71-
local backpos = vector.subtract(pos, direction)
7246

7347
-- ### Step 1: Push nodes in front ###
74-
local maxpush = mesecon.setting("movestone_max_push", 50)
75-
local maxpull = mesecon.setting("movestone_max_pull", 50)
76-
local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, maxpush)
77-
if success then
78-
mesecon.mvps_process_stack(stack)
79-
mesecon.mvps_move_objects(frontpos, direction, oldstack)
80-
-- Too large stack/stopper in the way: try again very soon
81-
else
82-
minetest.after(0.05, movestone_move, pos)
48+
local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, max_push)
49+
if not success then
50+
minetest.get_node_timer(pos):start(timer_interval)
8351
return
8452
end
53+
mesecon.mvps_process_stack(stack)
54+
mesecon.mvps_move_objects(frontpos, direction, oldstack)
8555

8656
-- ### Step 2: Move the movestone ###
87-
local node = minetest.get_node(pos)
8857
minetest.set_node(frontpos, node)
8958
minetest.remove_node(pos)
9059
mesecon.on_dignode(pos, node)
9160
mesecon.on_placenode(frontpos, node)
92-
minetest.after(timer_interval, movestone_move, frontpos)
61+
minetest.get_node_timer(frontpos):start(timer_interval)
9362

9463
-- ### Step 3: If sticky, pull stack behind ###
9564
if is_sticky then
96-
mesecon.mvps_pull_all(backpos, direction, maxpull)
65+
local backpos = vector.subtract(pos, direction)
66+
mesecon.mvps_pull_all(backpos, direction, max_pull)
9767
end
9868
end
9969

10070
def.mesecons = {effector = {
101-
action_on = function (pos)
102-
if minetest.get_node(pos).name ~= name_active then
103-
minetest.set_node(pos, {name = name_active})
104-
movestone_move(pos)
71+
action_on = function(pos, node, rulename)
72+
if rulename and not minetest.get_node_timer(pos):is_started() then
73+
movestone_move(pos, node, rulename)
10574
end
10675
end,
107-
action_off = function (pos)
108-
minetest.set_node(pos, {name = name})
109-
end
76+
rules = mesecon.rules.default,
11077
}}
11178

112-
def.drop = name
79+
def.on_timer = function(pos, elapsed)
80+
local sourcepos = mesecon.is_powered(pos)
81+
if not sourcepos then
82+
return
83+
end
84+
local rulename = vector.subtract(sourcepos[1], pos)
85+
mesecon.activate(pos, minetest.get_node(pos), rulename, 0)
86+
end
87+
88+
def.on_blast = mesecon.on_blastnode
11389

11490
def.on_blast = mesecon.on_blastnode
11591

11692
minetest.register_node(name, def)
117-
118-
-- active node only
119-
local def_active = table.copy(def)
120-
def_active.groups.not_in_creative_inventory = 1
121-
minetest.register_node(name_active, def_active)
12293
end
12394

95+
96+
-- registration:
12497
mesecon.register_movestone("mesecons_movestones:movestone", {
125-
tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
126-
groups = {cracky=3},
127-
description="Movestone",
98+
tiles = {
99+
"jeija_movestone_side.png",
100+
"jeija_movestone_side.png",
101+
"jeija_movestone_arrows.png^[transformFX",
102+
"jeija_movestone_arrows.png^[transformFX",
103+
"jeija_movestone_arrows.png",
104+
"jeija_movestone_arrows.png",
105+
},
106+
groups = {cracky = 3},
107+
description = "Movestone",
108+
sounds = default.node_sound_stone_defaults()
109+
}, false, false)
110+
111+
mesecon.register_movestone("mesecons_movestones:sticky_movestone", {
112+
tiles = {
113+
"jeija_movestone_side.png",
114+
"jeija_movestone_side.png",
115+
"jeija_sticky_movestone.png^[transformFX",
116+
"jeija_sticky_movestone.png^[transformFX",
117+
"jeija_sticky_movestone.png",
118+
"jeija_sticky_movestone.png",
119+
},
120+
groups = {cracky = 3},
121+
description = "Sticky Movestone",
122+
sounds = default.node_sound_stone_defaults(),
123+
}, true, false)
124+
125+
mesecon.register_movestone("mesecons_movestones:movestone_vertical", {
126+
tiles = {
127+
"jeija_movestone_side.png",
128+
"jeija_movestone_side.png",
129+
"jeija_movestone_arrows.png^[transformFXR90",
130+
"jeija_movestone_arrows.png^[transformR90",
131+
"jeija_movestone_arrows.png^[transformFXR90",
132+
"jeija_movestone_arrows.png^[transformR90",
133+
},
134+
groups = {cracky = 3},
135+
description = "Vertical Movestone",
128136
sounds = default.node_sound_stone_defaults()
129-
}, false)
137+
}, false, true)
138+
139+
mesecon.register_movestone("mesecons_movestones:sticky_movestone_vertical", {
140+
tiles = {
141+
"jeija_movestone_side.png",
142+
"jeija_movestone_side.png",
143+
"jeija_sticky_movestone.png^[transformFXR90",
144+
"jeija_sticky_movestone.png^[transformR90",
145+
"jeija_sticky_movestone.png^[transformFXR90",
146+
"jeija_sticky_movestone.png^[transformR90",
147+
},
148+
groups = {cracky = 3},
149+
description = "Vertical Sticky Movestone",
150+
sounds = default.node_sound_stone_defaults(),
151+
}, true, true)
130152

153+
154+
-- crafting:
155+
-- base recipe:
131156
minetest.register_craft({
132157
output = "mesecons_movestones:movestone 2",
133158
recipe = {
@@ -137,22 +162,51 @@ minetest.register_craft({
137162
}
138163
})
139164

140-
-- STICKY_MOVESTONE
141-
mesecon.register_movestone("mesecons_movestones:sticky_movestone", {
142-
tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_sticky_movestone.png", "jeija_sticky_movestone.png"},
143-
inventory_image = minetest.inventorycube("jeija_sticky_movestone.png", "jeija_movestone_side.png", "jeija_movestone_side.png"),
144-
groups = {cracky=3},
145-
description="Sticky Movestone",
146-
sounds = default.node_sound_stone_defaults(),
147-
}, true)
165+
-- conversation:
166+
minetest.register_craft({
167+
type = "shapeless",
168+
output = "mesecons_movestones:movestone",
169+
recipe = {"mesecons_movestones:movestone_vertical"},
170+
})
171+
172+
minetest.register_craft({
173+
type = "shapeless",
174+
output = "mesecons_movestones:movestone_vertical",
175+
recipe = {"mesecons_movestones:movestone"},
176+
})
177+
178+
minetest.register_craft({
179+
type = "shapeless",
180+
output = "mesecons_movestones:sticky_movestone",
181+
recipe = {"mesecons_movestones:sticky_movestone_vertical"},
182+
})
183+
184+
minetest.register_craft({
185+
type = "shapeless",
186+
output = "mesecons_movestones:sticky_movestone_vertical",
187+
recipe = {"mesecons_movestones:sticky_movestone"},
188+
})
148189

190+
-- make sticky:
149191
minetest.register_craft({
150192
output = "mesecons_movestones:sticky_movestone",
151193
recipe = {
152194
{"mesecons_materials:glue", "mesecons_movestones:movestone", "mesecons_materials:glue"},
153195
}
154196
})
155197

156-
-- Don't allow pushing movestones while they're active
157-
mesecon.register_mvps_stopper("mesecons_movestones:movestone_active")
158-
mesecon.register_mvps_stopper("mesecons_movestones:sticky_movestone_active")
198+
minetest.register_craft({
199+
output = "mesecons_movestones:sticky_movestone_vertical",
200+
recipe = {
201+
{"mesecons_materials:glue"},
202+
{"mesecons_movestones:movestone_vertical"},
203+
{"mesecons_materials:glue"},
204+
}
205+
})
206+
207+
208+
-- legacy code:
209+
minetest.register_alias("mesecons_movestones:movestone_active",
210+
"mesecons_movestones:movestone")
211+
minetest.register_alias("mesecons_movestones:sticky_movestone_active",
212+
"mesecons_movestones:sticky_movestone")

0 commit comments

Comments
 (0)
Please sign in to comment.