Added accelrator and teleport pipes

This commit is contained in:
Novatux 2013-01-19 13:14:41 +01:00
parent 143aa0ee04
commit aea0207b7f
3 changed files with 117 additions and 17 deletions

View File

@ -153,10 +153,13 @@ minetest.register_entity("pipeworks:tubed_item", {
end,
get_staticdata = function(self)
if self.start_pos==nil then return end
local velocity=self.object:getvelocity()
--self.object:setvelocity({x=0,y=0,z=0})
self.object:setpos(self.start_pos)
return minetest.serialize({
itemstring=self.itemstring,
velocity=self.object:getvelocity(),
velocity=velocity,
start_pos=self.start_pos
})
end,
@ -196,23 +199,25 @@ minetest.register_entity("pipeworks:tubed_item", {
local velocitycopy={x=velocity.x,y=velocity.y,z=velocity.z}
local moved=false
local speed=math.abs(velocity.x+velocity.y+velocity.z)
local vel={x=velocity.x/speed,y=velocity.y/speed,z=velocity.z/speed}
if math.abs(velocity.x)==1 then
if math.abs(vel.x)==1 then
local next_node=math.abs(pos.x-self.start_pos.x)
if next_node >= 1 then
self.start_pos.x=self.start_pos.x+velocity.x
self.start_pos.x=self.start_pos.x+vel.x
moved=true
end
elseif math.abs(velocity.y)==1 then
elseif math.abs(vel.y)==1 then
local next_node=math.abs(pos.y-self.start_pos.y)
if next_node >= 1 then
self.start_pos.y=self.start_pos.y+velocity.y
self.start_pos.y=self.start_pos.y+vel.y
moved=true
end
elseif math.abs(velocity.z)==1 then
elseif math.abs(vel.z)==1 then
local next_node=math.abs(pos.z-self.start_pos.z)
if next_node >= 1 then
self.start_pos.z=self.start_pos.z+velocity.z
self.start_pos.z=self.start_pos.z+vel.z
moved=true
end
end
@ -220,7 +225,7 @@ minetest.register_entity("pipeworks:tubed_item", {
node = minetest.env:get_node(self.start_pos)
if moved and minetest.get_item_group(node.name,"tubedevice_receiver")==1 then
if minetest.registered_nodes[node.name].tube and minetest.registered_nodes[node.name].tube.insert_object then
leftover = minetest.registered_nodes[node.name].tube.insert_object(self.start_pos,node,stack,velocity)
leftover = minetest.registered_nodes[node.name].tube.insert_object(self.start_pos,node,stack,vel)
else
leftover = stack
end
@ -278,13 +283,23 @@ function go_next(pos,velocity,stack)
local len=1
local n
local can_go
local speed=math.abs(velocity.x+velocity.y+velocity.z)
local vel={x=velocity.x/speed,y=velocity.y/speed,z=velocity.z/speed,speed=speed}
if speed>=4.1 then
speed=4
elseif speed>=1.1 then
speed=speed-0.1
else
speed=1
end
vel.speed=speed
if minetest.registered_nodes[cnode.name].tube and minetest.registered_nodes[cnode.name].tube.can_go then
can_go=minetest.registered_nodes[cnode.name].tube.can_go(pos,node,velocity,stack)
can_go=minetest.registered_nodes[cnode.name].tube.can_go(pos,node,vel,stack)
else
can_go=adjlist
end
for _,vect in ipairs(can_go) do
if vect.x~=-velocity.x or vect.y~=-velocity.y or vect.z~=-velocity.z then
if vect.x~=-vel.x or vect.y~=-vel.y or vect.z~=-vel.z then
npos=addVect(pos,vect)
node=minetest.env:get_node(npos)
tube_receiver=minetest.get_item_group(node.name,"tubedevice_receiver")
@ -334,9 +349,9 @@ function go_next(pos,velocity,stack)
end
until false
meta:set_int("tubedir",n)
velocity.x=tubes[n].vect.x
velocity.y=tubes[n].vect.y
velocity.z=tubes[n].vect.z
velocity.x=tubes[n].vect.x*vel.speed
velocity.y=tubes[n].vect.y*vel.speed
velocity.z=tubes[n].vect.z*vel.speed
end
else
local i=1
@ -352,9 +367,9 @@ function go_next(pos,velocity,stack)
break
end
until false
velocity.x=chests[n].vect.x
velocity.y=chests[n].vect.y
velocity.z=chests[n].vect.z
velocity.x=chests[n].vect.x*speed
velocity.y=chests[n].vect.y*speed
velocity.z=chests[n].vect.z*speed
end
return 1
end

75
teleport_tube.lua Normal file
View File

@ -0,0 +1,75 @@
filename=minetest.get_worldpath() .. "/teleport_tubes"
function read_file()
local f = io.open(filename, "r")
if f==nil then return {} end
local t = f:read("*all")
f:close()
if t=="" or t==nil then return {} end
return minetest.deserialize(t)
end
function write_file(tbl)
local f = io.open(filename, "w")
f:write(minetest.serialize(tbl))
f:close()
end
function add_tube_in_file(pos,channel)
tbl=read_file()
for _,val in ipairs(tbl) do
if val.x==pos.x and val.y==pos.y and val.z==pos.z then
return
end
end
table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel})
write_file(tbl)
end
function remove_tube_in_file(pos)
tbl=read_file()
newtbl={}
for _,val in ipairs(tbl) do
if val.x~=pos.x or val.y~=pos.y or val.z~=pos.z then
table.insert(newtbl,val)
end
end
write_file(newtbl)
end
function get_tubes_in_file(pos,channel)
tbl=read_file()
newtbl={}
for _,val in ipairs(tbl) do
if val.channel==channel and (val.x~=pos.x or val.y~=pos.y or val.z~=pos.z) then
table.insert(newtbl,val)
end
end
return newtbl
end
register_tube("pipeworks:teleport_tube","Teleporter pneumatic tube segment",plain_textures,noctr_textures,end_textures,
short_texture,inv_texture,
{tube={can_go=function(pos,node,velocity,stack)
velocity.x=0
velocity.y=0
velocity.z=0
local meta = minetest.env:get_meta(pos)
channel=meta:get_int("channel")
goto=get_tubes_in_file(pos,channel)
if goto[1]==nil then return {} end
pos.x=goto[1].x
pos.y=goto[1].y
pos.z=goto[1].z
return meseadjlist
end},
on_construct = function(pos)
local meta = minetest.env:get_meta(pos)
meta:set_int("channel",0)
add_tube_in_file(pos,0)
end,
after_dig_node = function(pos)
remove_tube_in_file(pos)
end})

View File

@ -371,3 +371,13 @@ register_tube("pipeworks:detector_tube_off","Detector tube segment",detector_pla
groups={mesecon=2},
mesecons={receptor={state="off",
rules=mesecons_rules}}})
register_tube("pipeworks:accelerator_tube","Accelerator pneumatic tube segment",plain_textures,noctr_textures,end_textures,
short_texture,inv_texture,
{tube={can_go=function(pos,node,velocity,stack)
velocity.speed=velocity.speed+1
return meseadjlist
end}})
modpath=minetest.get_modpath("pipeworks")
dofile(modpath.."/teleport_tube.lua")