Use "proper" vectors more

main
copygirl 1 year ago
parent 6b0326af1f
commit 8b4e042f79
  1. 36
      contraption.lua

@ -165,8 +165,8 @@ function contraption.create(region)
-- Change nodes from base nodes to their contraption version. -- Change nodes from base nodes to their contraption version.
for pos_hash, node in pairs(nodes) do for pos_hash, node in pairs(nodes) do
local rel_pos = minetest.get_position_from_hash(pos_hash) local rel_pos = vector.copy(minetest.get_position_from_hash(pos_hash))
local abs_pos = vector.add(region.min, rel_pos) local abs_pos = region.min + rel_pos
minetest.set_node(abs_pos, node) minetest.set_node(abs_pos, node)
end end
@ -178,11 +178,11 @@ end
function contraption:on_initialize() function contraption:on_initialize()
local above_nodes = {} local above_nodes = {}
for orig_pos_hash, node in pairs(self.nodes) do for orig_pos_hash, node in pairs(self.nodes) do
local orig_pos = minetest.get_position_from_hash(orig_pos_hash) local orig_pos = vector.copy(minetest.get_position_from_hash(orig_pos_hash))
-- Calculate the nodes above the contraption that it -- Calculate the nodes above the contraption that it
-- could potentially pull along with it as it moves. -- could potentially pull along with it as it moves.
local above_pos = vector.offset(orig_pos, 0, 1, 0) local above_pos = orig_pos:offset(0, 1, 0)
local above_pos_hash = minetest.hash_node_position(above_pos) local above_pos_hash = minetest.hash_node_position(above_pos)
if not self.nodes[above_pos_hash] then if not self.nodes[above_pos_hash] then
-- Non-contraption block above this one. Move items here along. -- Non-contraption block above this one. Move items here along.
@ -234,8 +234,8 @@ function contraption:destroy()
active_contraptions[self.id] = nil active_contraptions[self.id] = nil
for pos_hash, node in pairs(self.nodes) do for pos_hash, node in pairs(self.nodes) do
local rel_pos = minetest.get_position_from_hash(pos_hash) local rel_pos = vector.copy(minetest.get_position_from_hash(pos_hash))
local abs_pos = vector.add(self.region.min, rel_pos) local abs_pos = self.region.min + rel_pos
local def = minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
minetest.set_node(abs_pos, def.drop_in_place) minetest.set_node(abs_pos, def.drop_in_place)
end end
@ -251,7 +251,7 @@ function contraption:push(pusher, dir)
self.recent_pusher[pusher_name] = 1.0 -- seconds self.recent_pusher[pusher_name] = 1.0 -- seconds
self.motion = self.motion or vector.zero() self.motion = self.motion or vector.zero()
self.motion = vector.add(self.motion, dir) self.motion = self.motion + dir
end end
function contraption:update(delta_time) function contraption:update(delta_time)
@ -329,11 +329,11 @@ function contraption:move(offset)
end end
for old_rel_pos_hash, node in pairs(self.nodes) do for old_rel_pos_hash, node in pairs(self.nodes) do
local old_rel_pos = minetest.get_position_from_hash(old_rel_pos_hash) local old_rel_pos = vector.copy(minetest.get_position_from_hash(old_rel_pos_hash))
local old_abs_pos = vector.add(self.region.min, old_rel_pos) local old_abs_pos = self.region.min + old_rel_pos
local new_abs_pos = vector.add(old_abs_pos, offset) local new_abs_pos = old_abs_pos + offset
local new_rel_pos = vector.add(old_rel_pos, offset) local new_rel_pos = old_rel_pos + offset
local new_rel_pos_hash = minetest.hash_node_position(new_rel_pos) local new_rel_pos_hash = minetest.hash_node_position(new_rel_pos)
-- Node is being moved to this position, no need to clear here. -- Node is being moved to this position, no need to clear here.
@ -390,8 +390,8 @@ function contraption:move(offset)
-- The `to_push` table contains "lose" nodes that are not directly -- The `to_push` table contains "lose" nodes that are not directly
-- pushed by the contraption, and may or may not be pushed. -- pushed by the contraption, and may or may not be pushed.
for pos_hash, node in pairs(to_push) do for pos_hash, node in pairs(to_push) do
local rel_pos = minetest.get_position_from_hash(pos_hash) local rel_pos = vector.copy(minetest.get_position_from_hash(pos_hash))
local forward_rel_pos = vector.add(rel_pos, offset) local forward_rel_pos = rel_pos + offset
local forward_rel_pos_hash = minetest.hash_node_position(forward_rel_pos) local forward_rel_pos_hash = minetest.hash_node_position(forward_rel_pos)
-- If there's a node to push ahead of this one, skip. -- If there's a node to push ahead of this one, skip.
@ -411,7 +411,7 @@ function contraption:move(offset)
to_set[forward_rel_pos_hash] = node to_set[forward_rel_pos_hash] = node
while true do while true do
local backward_rel_pos = vector.add(rel_pos, -offset) local backward_rel_pos = rel_pos - offset
local backward_rel_pos_hash = minetest.hash_node_position(backward_rel_pos) local backward_rel_pos_hash = minetest.hash_node_position(backward_rel_pos)
node = to_push[backward_rel_pos_hash] node = to_push[backward_rel_pos_hash]
if not node then break end if not node then break end
@ -428,16 +428,16 @@ function contraption:move(offset)
-- Set nodes that need to be changed. -- Set nodes that need to be changed.
-- TODO: Can this use a vector key instead of hash, or an array? -- TODO: Can this use a vector key instead of hash, or an array?
for pos_hash, node in pairs(to_set) do for pos_hash, node in pairs(to_set) do
local rel_pos = minetest.get_position_from_hash(pos_hash) local rel_pos = vector.copy(minetest.get_position_from_hash(pos_hash))
local abs_pos = vector.add(self.region.min, rel_pos) local abs_pos = self.region.min + rel_pos
minetest.set_node(abs_pos, node.node or node) minetest.set_node(abs_pos, node.node or node)
if node.meta then minetest.get_meta(abs_pos):from_table(node.meta) end if node.meta then minetest.get_meta(abs_pos):from_table(node.meta) end
end end
-- Clear nodes that need to be cleared (to AIR). -- Clear nodes that need to be cleared (to AIR).
for pos_hash, node in pairs(to_clear) do for pos_hash, node in pairs(to_clear) do
local rel_pos = minetest.get_position_from_hash(pos_hash) local rel_pos = vector.copy(minetest.get_position_from_hash(pos_hash))
local abs_pos = vector.add(self.region.min, rel_pos) local abs_pos = self.region.min + rel_pos
minetest.set_node(abs_pos, node) minetest.set_node(abs_pos, node)
end end

Loading…
Cancel
Save