This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmovefloor.lua
207 lines (152 loc) · 4.57 KB
/
movefloor.lua
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
local lwcomp = ...
local S = lwcomp.S
-- depreciated
local movefloor_groups = { cracky = 2 }
if minetest.global_exists ("lwcomponents") then
movefloor_groups.not_in_creative_inventory = 1
end
if lwcomp.mesecon_supported and mesecon.mvps_push then
local mesecon_rules =
{
{ x = 1, y = 1, z = 0 },
{ x = -1, y = 1, z = 0 },
{ x = 0, y = 1, z = 1 },
{ x = 0, y = 1, z = -1 },
{ x = 1, y = -1, z = 0 },
{ x = -1, y = -1, z = 0 },
{ x = 0, y = -1, z = 1 },
{ x = 0, y = -1, z = -1 },
}
-- use mesecons movestone settings
local timer_interval = 1 / mesecon.setting ("movestone_speed", 3)
local max_push = 3
local max_pull = 3
-- helper functions:
local function get_movefloor_direction (rulename)
if rulename.y > 0 then
return { x = 0, y = 1, z = 0 }
elseif rulename.y < 0 then
return { x = 0, y = -1, z = 0 }
end
end
local function add_movefloor_list (pos, list)
for i = 1, #list do
if list[i].x == pos.x and
list[i].y == pos.y and
list[i].z == pos.z then
return false
end
end
list[#list + 1] = { x = pos.x, y = pos.y, z = pos.z }
return true
end
local function find_adjoining_movefloor (pos, list)
local tpos =
{
{ x = pos.x + 1, y = pos.y, z = pos.z },
{ x = pos.x - 1, y = pos.y, z = pos.z },
{ x = pos.x, y = pos.y, z = pos.z + 1 },
{ x = pos.x, y = pos.y, z = pos.z - 1 }
}
for i = 1, #tpos do
local node = minetest.get_node (tpos[i])
if node and node.name == "lwcomputers:movefloor" then
if add_movefloor_list (tpos[i], list) then
find_adjoining_movefloor (tpos[i], list)
end
end
end
end
-- copied from mesecons movestone
local function movefloor_move (pos, node, rulename, is_sticky)
local direction = get_movefloor_direction (rulename)
local play_sound = false
local list =
{
{ x = pos.x, y = pos.y, z = pos.z }
}
find_adjoining_movefloor (pos, list)
for i = 1, #list do
local frontpos = vector.add (list[i], direction)
local meta = minetest.get_meta (list[i])
local owner = meta:get_string ("owner")
local continue = true
-- ### Step 1: Push nodes in front ###
local success, stack, oldstack = mesecon.mvps_push (frontpos, direction, max_push, owner)
if not success then
if stack == "protected" then
meta:set_string ("infotext", "Can't move: protected area on the way")
else
minetest.get_node_timer (list[i]):start (timer_interval)
continue = false
end
end
if continue then
mesecon.mvps_move_objects (frontpos, direction, oldstack)
-- ### Step 2: Move the movestone ###
minetest.set_node (frontpos, node)
local meta2 = minetest.get_meta (frontpos)
meta2:set_string ("owner", owner)
minetest.remove_node (list[i])
mesecon.on_dignode (list[i], node)
mesecon.on_placenode (frontpos, node)
minetest.get_node_timer (frontpos):start (timer_interval)
play_sound = true
-- ### Step 3: If sticky, pull stack behind ###
if is_sticky and direction.y < 0 then
local backpos = vector.subtract (list[i], direction)
success, stack, oldstack = mesecon.mvps_pull_all (backpos, direction, max_pull, owner)
if success then
mesecon.mvps_move_objects (backpos, vector.multiply (direction, -1), oldstack, -1)
end
end
-- ### Step 4: Let things fall ###
minetest.check_for_falling (vector.add (list[i], { x = 0, y = 1, z = 0 }))
end
end
if play_sound then
minetest.sound_play("movestone", { pos = pos, max_hear_distance = 20, gain = 0.5 }, true)
end
end
local function on_timer (pos, elapsed)
local sourcepos = mesecon.is_powered (pos)
if not sourcepos then
return
end
local rulename = vector.subtract (sourcepos[1], pos)
mesecon.activate (pos, minetest.get_node (pos), rulename, 0)
end
local function mesecon_support ()
return
{
effector =
{
rules = table.copy (mesecon_rules),
action_on = function (pos, node, rulename)
-- do something to turn the effector on
if rulename and not minetest.get_node_timer (pos):is_started () then
movefloor_move (pos, node, rulename, true)
end
end
}
}
end
minetest.register_node("lwcomputers:movefloor", {
description = S("Moving Floor"),
tiles = { "lwcomputers_movefloortop.png", "lwcomputers_movefloortop.png",
"lwcomputers_movefloorside.png", "lwcomputers_movefloorside.png",
"lwcomputers_movefloorside.png", "lwcomputers_movefloorside.png" },
sunlight_propagates = false,
drawtype = "normal",
node_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
}
},
groups = movefloor_groups,
sounds = default.node_sound_wood_defaults (),
mesecons = mesecon_support (),
on_timer = on_timer,
})
end