Initial commit

master
qwertymine3 2016-09-17 16:16:26 +01:00
commit b0edcaf140
8 changed files with 199 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*~

51
database.lua Normal file
View File

@ -0,0 +1,51 @@
local e = easyporter
local database_path = minetest.get_worldpath() .. "/easyporter_db.json"
local portals
local file = io.open(database_path,"r")
if file then
local portal_string = file:read("*all")
portals = minetest.parse_json(portal_string)
file:close()
end
if not portals then
portals = {}
end
local function save_database()
local file = assert(io.open(database_path,"w"))
local database_string = minetest.write_json(portals,true)
if database_string then
file:write(database_string)
end
file:close()
end
e.set_portal = function(pos,name)
if not name or not pos then
return false
end
if portals[name] then
-- Report if needed
end
portals[name] = pos
save_database()
minetest.chat_send_all("Set portal: " .. name)
return true
end
e.remove_portal = function(name)
if not name or not portals[name] then
return false
end
portals[name] = nil
save_database()
minetest.chat_send_all("Removed portal: " .. name)
return true
end
e.get_portal = function(name)
return portals[name]
end

0
depends.txt Normal file
View File

8
init.lua Normal file
View File

@ -0,0 +1,8 @@
easyporter = {}
local modpath = minetest.get_modpath("easyporter")
dofile(modpath .. "/util.lua")
dofile(modpath .. "/database.lua")
dofile(modpath .. "/portal_in.lua")
dofile(modpath .. "/portal_out.lua")

1
mod.conf Normal file
View File

@ -0,0 +1 @@
name=easyporter

31
portal_in.lua Normal file
View File

@ -0,0 +1,31 @@
local e = easyporter
local portal_in = {
description = "In portal",
tiles = {"default_mese_block.png"},
is_ground_content = false,
groups = {dig_immediate=3,source=1},
after_place_node = function(pos)
minetest.get_node_timer(pos):start(0.1)
end,
on_contruct = function(pos)
minetest.get_node_timer(pos):start(0.1)
end,
on_timer = function(pos,_)
local portal_pos = vector.add(pos,{x=0,y=1,z=0})
local entity = minetest.get_objects_inside_radius(portal_pos,0.8)
if entity and #entity >= 1 then
local portal_name = e.get_portal_node(pos)
if portal_name then
for i,v in ipairs(entity) do
e.tp_to_portal(portal_name,v)
end
e.try_collapse(portal_name)
end
end
return true
end,
}
minetest.register_node("easyporter:in",portal_in)

42
portal_out.lua Normal file
View File

@ -0,0 +1,42 @@
local e = easyporter
local function try_set_portal(pos)
local name = e.get_portal_node(pos)
if not name or not pos then
return false
end
e.set_portal(pos,name)
end
local portal_out = {
description = "Out portal",
tiles = {"default_mese_block.png"},
is_ground_content = false,
groups = {dig_immediate=3,source=1},
after_place_node = function(pos)
try_set_portal(pos)
end,
on_contruct = function(pos)
try_set_portal(pos)
end,
on_rightclick = function(pos,node)
try_set_portal(pos)
end,
--[[
after_dig_node = function(pos)
local name = e.get_portal_node(pos)
if name then
e.remove_portal(name)
end
end,
after_destruct = function(pos)
local name = e.get_portal_node(pos)
if name then
e.remove_portal(name)
end
end,
--]]
}
minetest.register_node("easyporter:out",portal_out)

65
util.lua Normal file
View File

@ -0,0 +1,65 @@
local e = easyporter
local portal_node_points = {
{x=1,y=0,z=0},
{x=-1,y=0,z=0},
{x=0,y=0,z=1},
{x=0,y=0,z=-1},
}
-- Check 4 surrounding nodes all have the same name
-- Return name if all same, else return nil
e.get_portal_node = function(pos)
local node_name = nil
for _,relative in ipairs(portal_node_points) do
local real_pos = vector.add(relative,pos)
local node = minetest.get_node_or_nil(real_pos)
if node and node.name
and (not node_name or node.name == node_name) then
node_name = node.name
else
return nil
end
end
return node_name
end
e.tp_to_portal = function(name,entity)
local portal_pos = e.get_portal(name)
if not portal_pos then
return false
end
entity:setpos(vector.add(portal_pos,{x=0,y=1,z=0}))
return true
end
e.try_collapse = function(name)
if not name then
return false
end
local pos = e.get_portal(name)
if not pos then
return false
end
local portal = minetest.get_node_or_nil(pos)
if not portal then
return false
end
if portal.name ~= "easyporter:out" then
e.remove_portal(name)
return true
end
local node = e.get_portal_node(pos)
if not node or node ~= name then
e.remove_portal(name)
if node ~= name then
e.set_portal(node,pos)
end
return true
end
return false
end