summaryrefslogtreecommitdiff
path: root/pipeworks
diff options
context:
space:
mode:
Diffstat (limited to 'pipeworks')
-rw-r--r--pipeworks/common.lua131
-rw-r--r--pipeworks/filter-injector.lua8
-rw-r--r--pipeworks/item_transport.lua30
-rw-r--r--pipeworks/legacy.lua2
-rw-r--r--pipeworks/luaentity.lua38
-rw-r--r--pipeworks/vacuum_tubes.lua2
-rw-r--r--pipeworks/wielder.lua91
7 files changed, 189 insertions, 113 deletions
diff --git a/pipeworks/common.lua b/pipeworks/common.lua
index 5574bb2..0f8c34c 100644
--- a/pipeworks/common.lua
+++ b/pipeworks/common.lua
@@ -157,3 +157,134 @@ function pipeworks.load_position(pos)
local vm = minetest.get_voxel_manip()
vm:read_from_map(pos, pos)
end
+
+local function delay(...)
+ local args = {...}
+ return (function() return unpack(args) end)
+end
+
+local function get_set_wrap(name, is_dynamic)
+ return (function(self)
+ return self["_" .. name]
+ end), (function(self, value)
+ if is_dynamic then
+ self["_" .. name] = type(value) == "table"
+ and table.copy(value) or value
+ end
+ end)
+end
+
+function pipeworks.create_fake_player(def, is_dynamic)
+ local wielded_item = ItemStack("")
+ if def.inventory and def.wield_list then
+ wielded_item = def.inventory:get_stack(def.wield_list, def.wield_index or 1)
+ end
+ local p = {
+ get_player_name = delay(def.name),
+ is_player = delay(true),
+ is_fake_player = true,
+
+ _formspec = def.formspec or default.gui_survival_form,
+ _hp = def.hp or 20,
+ _breath = 11,
+ _pos = def.position and table.copy(def.position) or vector.new(),
+ _properties = def.properties or { eye_height = def.eye_height or 1.47 },
+ _inventory = def.inventory,
+ _wield_index = def.wield_index or 1,
+ _wielded_item = wielded_item,
+
+ -- Model and view
+ _eye_offset1 = vector.new(),
+ _eye_offset3 = vector.new(),
+ set_eye_offset = function(self, first, third)
+ self._eye_offset1 = table.copy(first)
+ self._eye_offset3 = table.copy(third)
+ end,
+ get_eye_offset = function(self)
+ return self._eye_offset1, self._eye_offset3
+ end,
+ get_look_dir = delay(def.look_dir or {x=0, y=0, z=1}),
+ get_look_pitch = delay(def.look_pitch or 0),
+ get_look_yaw = delay(def.look_yaw or 0),
+ get_look_horizontal = delay(def.look_yaw or 0),
+ get_look_vertical = delay(-(def.look_pitch or 0)),
+ set_animation = delay(),
+
+ -- Controls
+ get_player_control = delay({
+ jump=false, right=false, left=false, LMB=false, RMB=false,
+ sneak=def.sneak, aux1=false, down=false, up=false
+ }),
+ get_player_control_bits = delay(def.sneak and 64 or 0),
+
+ -- Inventory and ItemStacks
+ get_inventory = delay(def.inventory),
+ set_wielded_item = function(self, item)
+ if self._inventory and def.wield_list then
+ return self._inventory:set_stack(def.wield_list,
+ self._wield_index, item)
+ end
+ _wielded_item = ItemStack(item)
+ end,
+ get_wielded_item = function(self, item)
+ if self._inventory and def.wield_list then
+ return self._inventory:get_stack(def.wield_list,
+ self._wield_index)
+ end
+ return ItemStack(self._wielded_item)
+ end,
+ get_wield_list = delay(def.wield_list),
+
+ punch = delay(),
+ remove = delay(),
+ right_click = delay(),
+ set_attach = delay(),
+ set_detach = delay(),
+ set_bone_position = delay(),
+ hud_change = delay(),
+ }
+ local _trash
+ -- Getter & setter functions
+ p.get_inventory_formspec, p.set_inventory_formspec
+ = get_set_wrap("formspec", is_dynamic)
+ p.get_breath, p.set_breath = get_set_wrap("breath", is_dynamic)
+ p.get_hp, p.set_hp = get_set_wrap("hp", is_dynamic)
+ p.get_pos, p.set_pos = get_set_wrap("pos", is_dynamic)
+ _trash, p.move_to = get_set_wrap("pos", is_dynamic)
+ p.get_wield_index, p.set_wield_index = get_set_wrap("wield_index", true)
+ p.get_properties, p.set_properties = get_set_wrap("properties", false)
+
+ -- Backwards compatibilty
+ p.getpos = p.get_pos
+ p.setpos = p.set_pos
+ p.moveto = p.move_to
+
+ -- TODO "implement" all these
+ -- set_armor_groups
+ -- get_armor_groups
+ -- get_animation
+ -- get_bone_position
+ -- get_player_velocity
+ -- set_look_pitch
+ -- set_look_yaw
+ -- set_physics_override
+ -- get_physics_override
+ -- hud_add
+ -- hud_remove
+ -- hud_get
+ -- hud_set_flags
+ -- hud_get_flags
+ -- hud_set_hotbar_itemcount
+ -- hud_get_hotbar_itemcount
+ -- hud_set_hotbar_image
+ -- hud_get_hotbar_image
+ -- hud_set_hotbar_selected_image
+ -- hud_get_hotbar_selected_image
+ -- hud_replace_builtin
+ -- set_sky
+ -- get_sky
+ -- override_day_night_ratio
+ -- get_day_night_ratio
+ -- set_local_animation
+ return p
+end
diff --git a/pipeworks/filter-injector.lua b/pipeworks/filter-injector.lua
index 0f651fb..a8659ad 100644
--- a/pipeworks/filter-injector.lua
+++ b/pipeworks/filter-injector.lua
@@ -167,11 +167,9 @@ local function punch_filter(data, filtpos, filtnode, msg)
local filtmeta = minetest.get_meta(filtpos)
local filtinv = filtmeta:get_inventory()
local owner = filtmeta:get_string("owner")
- local fakePlayer = {
- get_player_name = delay(owner),
- is_fake_player = ":pipeworks",
- get_wielded_item = delay(ItemStack(nil))
- } -- TODO: use a mechanism as the wielder one
+ local fakePlayer = pipeworks.create_fake_player({
+ name = owner
+ })
local dir = pipeworks.facedir_to_right_dir(filtnode.param2)
local frompos = vector.subtract(filtpos, dir)
local fromnode = minetest.get_node(frompos)
diff --git a/pipeworks/item_transport.lua b/pipeworks/item_transport.lua
index 7219f7e..388e2e7 100644
--- a/pipeworks/item_transport.lua
+++ b/pipeworks/item_transport.lua
@@ -13,7 +13,7 @@ function pipeworks.tube_inject_item(pos, start_pos, velocity, item, owner)
local obj = luaentity.add_entity(pos, "pipeworks:tubed_item")
obj:set_item(stack:to_string())
obj.start_pos = vector.new(start_pos)
- obj:setvelocity(velocity)
+ obj:set_velocity(velocity)
obj.owner = owner
--obj:set_color("red") -- todo: this is test-only code
return obj
@@ -207,7 +207,7 @@ minetest.register_entity("pipeworks:tubed_item", {
textures = {stack:get_name()}
})
local def = stack:get_definition()
- self.object:setyaw((def and def.type == "node") and 0 or math.pi * 0.25)
+ self.object:set_yaw((def and def.type == "node") and 0 or math.pi * 0.25)
end,
get_staticdata = luaentity.get_staticdata,
@@ -220,7 +220,7 @@ minetest.register_entity("pipeworks:tubed_item", {
return
end
local item = minetest.deserialize(staticdata)
- pipeworks.tube_inject_item(self.object:getpos(), item.start_pos, item.velocity, item.itemstring)
+ pipeworks.tube_inject_item(self.object:get_pos(), item.start_pos, item.velocity, item.itemstring)
self.object:remove()
end,
})
@@ -293,15 +293,15 @@ luaentity.register_entity("pipeworks:tubed_item", {
end,
on_step = function(self, dtime)
- local pos = self:getpos()
+ local pos = self:get_pos()
if self.start_pos == nil then
self.start_pos = vector.round(pos)
- self:setpos(pos)
+ self:set_pos(pos)
end
local stack = ItemStack(self.itemstring)
- local velocity = self:getvelocity()
+ local velocity = self:get_velocity()
local moved = false
local speed = math.abs(velocity.x + velocity.y + velocity.z)
@@ -331,8 +331,8 @@ luaentity.register_entity("pipeworks:tubed_item", {
return
end
velocity = vector.multiply(velocity, -1)
- self:setpos(vector.subtract(self.start_pos, vector.multiply(vel, moved_by - 1)))
- self:setvelocity(velocity)
+ self:set_pos(vector.subtract(self.start_pos, vector.multiply(vel, moved_by - 1)))
+ self:set_velocity(velocity)
self:set_item(leftover:to_string())
return
end
@@ -350,13 +350,13 @@ luaentity.register_entity("pipeworks:tubed_item", {
-- compatible with Minetest 0.4.13.
-- Using item_drop here makes Minetest 0.4.13 crash.
local dropped_item = minetest.add_item(self.start_pos, stack)
- dropped_item:setvelocity(vector.multiply(velocity, 5))
+ dropped_item:set_velocity(vector.multiply(velocity, 5))
self:remove()
return
else
velocity = vector.multiply(velocity, -1)
- self:setpos(vector.subtract(self.start_pos, vector.multiply(vel, moved_by - 1)))
- self:setvelocity(velocity)
+ self:set_pos(vector.subtract(self.start_pos, vector.multiply(vel, moved_by - 1)))
+ self:set_velocity(velocity)
end
elseif is_multimode(multimode) then
-- create new stacks according to returned data.
@@ -371,8 +371,8 @@ luaentity.register_entity("pipeworks:tubed_item", {
if new_velocity and not vector.equals(velocity, new_velocity) then
local nvelr = math.abs(new_velocity.x + new_velocity.y + new_velocity.z)
- self:setpos(vector.add(self.start_pos, vector.multiply(new_velocity, (moved_by - 1) / nvelr)))
- self:setvelocity(new_velocity)
+ self:set_pos(vector.add(self.start_pos, vector.multiply(new_velocity, (moved_by - 1) / nvelr)))
+ self:set_velocity(new_velocity)
end
end
end
@@ -388,11 +388,11 @@ if minetest.get_modpath("mesecons_mvps") then
end
for id, entity in pairs(luaentity.entities) do
if entity.name == "pipeworks:tubed_item" then
- local pos = entity:getpos()
+ local pos = entity:get_pos()
local rpos = vector.round(pos)
local dir = moved[minetest.hash_node_position(rpos)]
if dir then
- entity:setpos(vector.add(pos, dir))
+ entity:set_pos(vector.add(pos, dir))
entity.start_pos = vector.add(entity.start_pos, dir)
end
end
diff --git a/pipeworks/legacy.lua b/pipeworks/legacy.lua
index b36cded..bb92a8f 100644
--- a/pipeworks/legacy.lua
+++ b/pipeworks/legacy.lua
@@ -36,7 +36,7 @@ if not minetest.get_modpath("auto_tree_tap") and
end,
after_place_node = function (pos, placer)
pipeworks.scan_for_tube_objects(pos, placer)
- local placer_pos = placer:getpos()
+ local placer_pos = placer:get_pos()
--correct for the player's height
if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
diff --git a/pipeworks/luaentity.lua b/pipeworks/luaentity.lua
index 34b6665..c11c030 100644
--- a/pipeworks/luaentity.lua
+++ b/pipeworks/luaentity.lua
@@ -74,7 +74,7 @@ local move_entities_globalstep_part1 = function(dtime)
local active_block_range = tonumber(minetest.settings:get("active_block_range")) or 2
local new_active_blocks = {}
for _, player in ipairs(minetest.get_connected_players()) do
- local blockpos = get_blockpos(player:getpos())
+ local blockpos = get_blockpos(player:get_pos())
local minp = vector.subtract(blockpos, active_block_range)
local maxp = vector.add(blockpos, active_block_range)
@@ -114,9 +114,9 @@ local entitydef_default = {
if not def.entity then
return
end
- def.entity:setpos(vector.add(self._pos, def.offset))
- def.entity:setvelocity(self._velocity)
- def.entity:setacceleration(self._acceleration)
+ def.entity:set_pos(vector.add(self._pos, def.offset))
+ def.entity:set_velocity(self._velocity)
+ def.entity:set_acceleration(self._acceleration)
end,
_attach_all = function(self)
local master = self._attached_entities_master
@@ -186,43 +186,43 @@ local entitydef_default = {
self:_add_attached(id)
end
end,
- getid = function(self)
+ get_id = function(self)
return self._id
end,
- getpos = function(self)
+ get_pos = function(self)
return vector.new(self._pos)
end,
- setpos = function(self, pos)
+ set_pos = function(self, pos)
self._pos = vector.new(pos)
--for _, entity in pairs(self._attached_entities) do
-- if entity.entity then
- -- entity.entity:setpos(vector.add(self._pos, entity.offset))
+ -- entity.entity:set_pos(vector.add(self._pos, entity.offset))
-- end
--end
local master = self._attached_entities_master
if master then
local master_def = self._attached_entities[master]
- master_def.entity:setpos(vector.add(self._pos, master_def.offset))
+ master_def.entity:set_pos(vector.add(self._pos, master_def.offset))
end
end,
- getvelocity = function(self)
+ get_velocity = function(self)
return vector.new(self._velocity)
end,
- setvelocity = function(self, velocity)
+ set_velocity = function(self, velocity)
self._velocity = vector.new(velocity)
local master = self._attached_entities_master
if master then
- self._attached_entities[master].entity:setvelocity(self._velocity)
+ self._attached_entities[master].entity:set_velocity(self._velocity)
end
end,
- getacceleration = function(self)
+ get_acceleration = function(self)
return vector.new(self._acceleration)
end,
- setacceleration = function(self, acceleration)
+ set_acceleration = function(self, acceleration)
self._acceleration = vector.new(acceleration)
local master = self._attached_entities_master
if master then
- self._attached_entities[master].entity:setacceleration(self._acceleration)
+ self._attached_entities[master].entity:set_acceleration(self._acceleration)
end
end,
remove = function(self)
@@ -314,7 +314,7 @@ function luaentity.get_objects_inside_radius(pos, radius)
local objects = {}
local index = 1
for id, entity in pairs(luaentity.entities) do
- if vector.distance(pos, entity:getpos()) <= radius then
+ if vector.distance(pos, entity:get_pos()) <= radius then
objects[index] = entity
index = index + 1
end
@@ -329,11 +329,11 @@ local move_entities_globalstep_part2 = function(dtime)
local master = entity._attached_entities_master
local master_def = master and entity._attached_entities[master]
local master_entity = master_def and master_def.entity
- local master_entity_pos = master_entity and master_entity:getpos()
+ local master_entity_pos = master_entity and master_entity:get_pos()
if master_entity_pos then
entity._pos = vector.subtract(master_entity_pos, master_def.offset)
- entity._velocity = master_entity:getvelocity()
- entity._acceleration = master_entity:getacceleration()
+ entity._velocity = master_entity:get_velocity()
+ entity._acceleration = master_entity:get_acceleration()
else
entity._pos = vector.add(vector.add(
entity._pos,
diff --git a/pipeworks/vacuum_tubes.lua b/pipeworks/vacuum_tubes.lua
index 211c3ee..d616835 100644
--- a/pipeworks/vacuum_tubes.lua
+++ b/pipeworks/vacuum_tubes.lua
@@ -87,7 +87,7 @@ local function vacuum(pos, radius)
for _, object in pairs(minetest.get_objects_inside_radius(pos, math.sqrt(3) * radius)) do
local lua_entity = object:get_luaentity()
if not object:is_player() and lua_entity and lua_entity.name == "__builtin:item" then
- local obj_pos = object:getpos()
+ local obj_pos = object:get_pos()
local minpos = vector.subtract(pos, radius)
local maxpos = vector.add(pos, radius)
if obj_pos.x >= minpos.x and obj_pos.x <= maxpos.x
diff --git a/pipeworks/wielder.lua b/pipeworks/wielder.lua
index eb7931f..98f1ef0 100644
--- a/pipeworks/wielder.lua
+++ b/pipeworks/wielder.lua
@@ -53,11 +53,10 @@ local function wielder_on(data, wielder_pos, wielder_node)
local wielder_meta = minetest.get_meta(wielder_pos)
local inv = wielder_meta:get_inventory()
local wield_inv_name = data.wield_inv_name
- local wieldindex, wieldstack
+ local wieldindex
for i, stack in ipairs(inv:get_list(wield_inv_name)) do
if not stack:is_empty() then
wieldindex = i
- wieldstack = stack
break
end
end
@@ -66,7 +65,6 @@ local function wielder_on(data, wielder_pos, wielder_node)
wield_inv_name = data.ghost_inv_name
inv:set_stack(wield_inv_name, 1, ItemStack(data.ghost_tool))
wieldindex = 1
- wieldstack = inv:get_stack(wield_inv_name, 1)
end
local dir = minetest.facedir_to_dir(wielder_node.param2)
-- under/above is currently intentionally left switched
@@ -98,73 +96,20 @@ local function wielder_on(data, wielder_pos, wielder_node)
yaw = 0
pitch = math.pi/2
end
- local virtplayer = {
- get_inventory_formspec = delay(wielder_meta:get_string("formspec")),
- get_look_dir = delay(vector.multiply(dir, -1)),
- get_look_pitch = delay(pitch),
- get_look_yaw = delay(yaw),
- get_look_horizontal = delay(yaw),
- get_look_vertical = delay(pitch),
- get_player_control = delay({ jump=false, right=false, left=false, LMB=false, RMB=false, sneak=data.sneak, aux1=false, down=false, up=false }),
- get_player_control_bits = delay(data.sneak and 64 or 0),
- get_player_name = delay(data.masquerade_as_owner and wielder_meta:get_string("owner") or ":pipeworks:"..minetest.pos_to_string(wielder_pos)),
- is_player = delay(true),
- is_fake_player = true,
- set_inventory_formspec = delay(),
- getpos = delay(vector.subtract(wielder_pos, assumed_eye_pos)),
- get_hp = delay(20),
- get_inventory = delay(inv),
- get_wielded_item = delay(wieldstack),
- get_wield_index = delay(wieldindex),
- get_wield_list = delay(wield_inv_name),
- moveto = delay(),
- punch = delay(),
- remove = delay(),
- right_click = delay(),
- setpos = delay(),
- set_hp = delay(),
- set_properties = delay(),
- set_wielded_item = function(self, item)
- wieldstack = item
- inv:set_stack(wield_inv_name, wieldindex, item)
- end,
- set_animation = delay(),
- set_attach = delay(),
- set_detach = delay(),
- set_bone_position = delay(),
- hud_change = delay(),
- get_breath = delay(11),
- -- TODO "implement" all these
- -- set_armor_groups
- -- get_armor_groups
- -- get_animation
- -- get_attach
- -- get_bone_position
- -- get_properties
- -- get_player_velocity
- -- set_look_pitch
- -- set_look_yaw
- -- set_breath
- -- set_physics_override
- -- get_physics_override
- -- hud_add
- -- hud_remove
- -- hud_get
- -- hud_set_flags
- -- hud_get_flags
- -- hud_set_hotbar_itemcount
- -- hud_get_hotbar_itemcount
- -- hud_set_hotbar_image
- -- hud_get_hotbar_image
- -- hud_set_hotbar_selected_image
- -- hud_get_hotbar_selected_image
- -- hud_replace_builtin
- -- set_sky
- -- get_sky
- -- override_day_night_ratio
- -- get_day_night_ratio
- -- set_local_animation
- }
+ local virtplayer = pipeworks.create_fake_player({
+ name = data.masquerade_as_owner and wielder_meta:get_string("owner")
+ or ":pipeworks:" .. minetest.pos_to_string(wielder_pos),
+ formspec = wielder_meta:get_string("formspec"),
+ look_dir = vector.multiply(dir, -1),
+ look_pitch = pitch,
+ look_yaw = yaw,
+ sneak = data.sneak,
+ position = vector.subtract(wielder_pos, assumed_eye_pos),
+ inventory = inv,
+ wield_index = wieldindex,
+ wield_list = wield_inv_name
+ })
+
local pointed_thing = { type="node", under=under_pos, above=above_pos }
data.act(virtplayer, pointed_thing)
if data.eject_drops then
@@ -258,7 +203,7 @@ local function register_wielder(data)
end,
after_place_node = function (pos, placer)
pipeworks.scan_for_tube_objects(pos)
- local placer_pos = placer:getpos()
+ local placer_pos = placer:get_pos()
if placer_pos and placer:is_player() then placer_pos = vector.add(placer_pos, assumed_eye_pos) end
if placer_pos then
local dir = vector.subtract(pos, placer_pos)
@@ -495,7 +440,9 @@ if pipeworks.enable_dispenser then
sneak = true,
act = function(virtplayer, pointed_thing)
local wieldstack = virtplayer:get_wielded_item()
- virtplayer:set_wielded_item((minetest.registered_items[wieldstack:get_name()] or {on_drop=minetest.item_drop}).on_drop(wieldstack, virtplayer, virtplayer:getpos()) or wieldstack)
+ virtplayer:set_wielded_item((minetest.registered_items[wieldstack:get_name()] or
+ {on_drop=minetest.item_drop}).on_drop(wieldstack, virtplayer, virtplayer:get_pos()) or
+ wieldstack)
end,
eject_drops = false,
})