do not store those gateNodes and remove unnecessary code, e.g. whitespace fixes

This commit is contained in:
HybridDog 2016-01-09 10:52:31 +01:00 committed by Auke Kok
parent bd77409cf3
commit a29dda359e
2 changed files with 164 additions and 236 deletions

View File

@ -1,199 +1,128 @@
function swap_gate_node(pos,name,dir) local function swap_gate_node(pos,name,dir)
local node = core.get_node(pos) local node = core.get_node(pos)
local meta = core.get_meta(pos) local meta = core.get_meta(pos)
local meta0 = meta:to_table() local meta0 = meta:to_table()
node.name = name node.name = name
node.param1=0
node.param2=dir node.param2=dir
core.set_node(pos,node) core.set_node(pos,node)
meta:from_table(meta0) meta:from_table(meta0)
end end
function getDir (player) local function addGateNode(gateNodes, pos)
local dir=player:get_look_dir() gateNodes[#gateNodes+1] = vector.new(pos)
if math.abs(dir.x)>math.abs(dir.z) then
if dir.x>0 then return 0 end
return 1
end
if dir.z>0 then return 2 end
return 3
end end
function checkNode (pos) local function placeGate(player,pos)
local node=core.get_node(pos) local dir = minetest.dir_to_facedir(player:get_look_dir())
if node.name == "air" then return 0 end local pos1 = vector.new(pos)
return 1 local gateNodes = {}
end addGateNode(gateNodes, pos1)
if dir == 1
function addGateNode (gateNodes,i,pos) or dir == 3 then
gateNodes[i].pos.x=pos.x
gateNodes[i].pos.y=pos.y
gateNodes[i].pos.z=pos.z
end
function placeGate (player,pos)
local player_name=player:get_player_name()
local dir=minetest.dir_to_facedir(player:get_look_dir())
local pos1=pos
local gateNodes={}
for i=1,9,1 do
gateNodes[i]={}
gateNodes[i].pos={}
end
if dir==1 then
addGateNode(gateNodes,1,pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
addGateNode(gateNodes,2,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z-2 pos1.z=pos1.z-2
addGateNode(gateNodes,3,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
pos1.y=pos1.y+1 pos1.y=pos1.y+1
addGateNode(gateNodes,4,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
addGateNode(gateNodes,5,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z-2 pos1.z=pos1.z-2
addGateNode(gateNodes,6,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
pos1.y=pos1.y+1 pos1.y=pos1.y+1
addGateNode(gateNodes,7,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
addGateNode(gateNodes,8,pos1) addGateNode(gateNodes, pos1)
pos1.z=pos1.z-2 pos1.z=pos1.z-2
addGateNode(gateNodes,9,pos1) addGateNode(gateNodes, pos1)
else
pos1.x=pos1.x+1
addGateNode(gateNodes, pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes, pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
addGateNode(gateNodes, pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes, pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes, pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
addGateNode(gateNodes, pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes, pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes, pos1)
end end
if dir==3 then for i=1,9 do
addGateNode(gateNodes,1,pos1) if core.get_node(gateNodes[i]).name ~= "air" then
pos1.z=pos1.z+1 print("not enough space")
addGateNode(gateNodes,3,pos1) return false
pos1.z=pos1.z-2
addGateNode(gateNodes,2,pos1)
pos1.z=pos1.z+1
pos1.y=pos1.y+1
addGateNode(gateNodes,4,pos1)
pos1.z=pos1.z+1
addGateNode(gateNodes,6,pos1)
pos1.z=pos1.z-2
addGateNode(gateNodes,5,pos1)
pos1.z=pos1.z+1
pos1.y=pos1.y+1
addGateNode(gateNodes,7,pos1)
pos1.z=pos1.z+1
addGateNode(gateNodes,9,pos1)
pos1.z=pos1.z-2
addGateNode(gateNodes,8,pos1)
end end
if dir==2 then
addGateNode(gateNodes,1,pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes,2,pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes,3,pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
addGateNode(gateNodes,4,pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes,5,pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes,6,pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
addGateNode(gateNodes,7,pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes,8,pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes,9,pos1)
end end
if dir==0 then core.set_node(pos, {name="stargate:gatenode_off", param2=dir})
addGateNode(gateNodes,1,pos1) local player_name = player:get_player_name()
pos1.x=pos1.x+1 local meta = core.get_meta(pos)
addGateNode(gateNodes,3,pos1) meta:set_string("infotext", "Stargate\rOwned by: "..player_name)
pos1.x=pos1.x-2 meta:set_int("gateActive", 0)
addGateNode(gateNodes,2,pos1) meta:set_string("owner", player_name)
pos1.x=pos1.x+1 meta:set_string("dont_destroy", "false")
pos1.y=pos1.y+1 stargate.registerGate(player_name, pos, dir)
addGateNode(gateNodes,4,pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes,6,pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes,5,pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
addGateNode(gateNodes,7,pos1)
pos1.x=pos1.x+1
addGateNode(gateNodes,9,pos1)
pos1.x=pos1.x-2
addGateNode(gateNodes,8,pos1)
end
for i=1,9,1 do
local node=core.get_node(gateNodes[i].pos)
if node.name ~= "air" then return false end
end
core.set_node(gateNodes[1].pos,{name="stargate:gatenode_off", param1=0, param2=dir})
local meta = core.get_meta(gateNodes[1].pos)
meta:set_string("infotext", "Stargate\nOwned by: "..player_name)
meta:set_string("gateNodes",minetest.serialize(gateNodes))
meta:set_int("gateActive",0)
meta:set_string("owner",player_name)
meta:set_string("dont_destroy","false")
stargate.registerGate(player_name,gateNodes[1].pos,dir)
return true return true
end end
function removeGate (pos) local function removeGate(pos)
local meta = core.get_meta(pos) local meta = core.get_meta(pos)
if meta:get_string("dont_destroy") == "true" then return end if meta:get_string("dont_destroy") == "true" then
local player_name=meta:get_string("owner") -- when swapping it
local gateNodes=minetest.deserialize(meta:get_string("gateNodes")) return
if gateNodes ~= nil then
stargate.unregisterGate(player_name,gateNodes[1].pos)
end end
stargate.unregisterGate(meta:get_string("owner"), pos)
end end
function activateGate (pos) function stargate.activateGate(pos)
local node = core.get_node(pos) local node = core.get_node(pos)
local dir=node.param2 local dir=node.param2
local meta = core.get_meta(pos) local meta = core.get_meta(pos)
local gateNodes=minetest.deserialize(meta:get_string("gateNodes"))
meta:set_int("gateActive",1) meta:set_int("gateActive",1)
meta:set_string("dont_destroy","true") meta:set_string("dont_destroy","true")
minetest.sound_play("gateOpen", {pos = pos, gain = 1.0,loop = false, max_hear_distance = 72,}) minetest.sound_play("gateOpen", {pos = pos, max_hear_distance = 72,})
swap_gate_node(gateNodes[1].pos,"stargate:gatenode_on",dir) swap_gate_node(pos,"stargate:gatenode_on",dir)
meta:set_string("dont_destroy","false") meta:set_string("dont_destroy","false")
end end
function deactivateGate (pos) function stargate.deactivateGate(pos)
local node = core.get_node(pos) local node = core.get_node(pos)
local dir=node.param2 local dir=node.param2
local meta = core.get_meta(pos) local meta = core.get_meta(pos)
local gateNodes=minetest.deserialize(meta:get_string("gateNodes"))
meta:set_int("gateActive",0) meta:set_int("gateActive",0)
meta:set_string("dont_destroy","true") meta:set_string("dont_destroy","true")
minetest.sound_play("gateClose", {pos = pos, gain = 1.0,loop = false, max_hear_distance = 72,}) minetest.sound_play("gateClose", {pos = pos, gain = 1.0,loop = false, max_hear_distance = 72,})
swap_gate_node(gateNodes[1].pos,"stargate:gatenode_off",dir) swap_gate_node(pos,"stargate:gatenode_off",dir)
meta:set_string("dont_destroy","false") meta:set_string("dont_destroy","false")
end end
gateCanDig = function(pos,player) local function gateCanDig(pos, player)
local player_name = player:get_player_name()
local meta = core.get_meta(pos) local meta = core.get_meta(pos)
if meta:get_string("dont_destroy") == "true" then return end return meta:get_string("dont_destroy") ~= "true"
local owner=meta:get_string("owner") and player:get_player_name() == meta:get_string("owner")
if player_name==owner then return true
else return false end
end end
sg_collision_box = { local sg_collision_box = {
type = "fixed", type = "fixed",
fixed={{-1.5,-0.5,-3/20,1.5,2.5,3/20},}, fixed={{-1.5,-0.5,-3/20,1.5,2.5,3/20},},
} }
sg_selection_box = { local sg_selection_box = {
type = "fixed", type = "fixed",
fixed={{-1.5,-0.5,-3/20,1.5,2.5,3/20},}, fixed={{-1.5,-0.5,-3/20,1.5,2.5,3/20},},
} }
sg_groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1} local sg_groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}
sg_groups1 = {snappy=2,choppy=2,oddly_breakable_by_hand=2} local sg_groups1 = {snappy=2,choppy=2,oddly_breakable_by_hand=2}
minetest.register_node("stargate:gatenode_on",{ minetest.register_node("stargate:gatenode_on",{
tiles = { tiles = {
@ -223,9 +152,7 @@ minetest.register_node("stargate:gatenode_on",{
selection_box = sg_selection_box, selection_box = sg_selection_box,
collision_box = sg_collision_box, collision_box = sg_collision_box,
can_dig = gateCanDig, can_dig = gateCanDig,
on_destruct = function (pos) on_destruct = removeGate,
removeGate(pos)
end,
on_rightclick=stargate.gateFormspecHandler, on_rightclick=stargate.gateFormspecHandler,
}) })
@ -250,9 +177,7 @@ minetest.register_node("stargate:gatenode_off",{
selection_box = sg_selection_box, selection_box = sg_selection_box,
collision_box = sg_collision_box, collision_box = sg_collision_box,
can_dig = gateCanDig, can_dig = gateCanDig,
on_destruct = function (pos) on_destruct = removeGate,
removeGate(pos)
end,
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
local pos = pointed_thing.above local pos = pointed_thing.above
if placeGate(placer,pos)==true then if placeGate(placer,pos)==true then
@ -269,27 +194,29 @@ minetest.register_abm({
nodenames = {"stargate:gatenode_on"}, nodenames = {"stargate:gatenode_on"},
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos)
local meta = core.get_meta(pos) --local owner
for _,object in ipairs(core.get_objects_inside_radius(pos, 1)) do for _,object in pairs(core.get_objects_inside_radius(pos, 1)) do
if object:is_player() then if object:is_player() then
local player_name = object:get_player_name() local player_name = object:get_player_name()
local owner=meta:get_string("owner") local gate = stargate.findGate(pos)
local gate=stargate.findGate (pos) if not gate then
if gate==nil then print("Gate is not registered!") return end print("Gate is not registered!")
local pos1={}
pos1.x=gate["destination"].x
pos1.y=gate["destination"].y
pos1.z=gate["destination"].z
local dest_gate=stargate.findGate (pos1)
if dest_gate==nil then
gate["destination"]=nil
deactivateGate(pos)
stargate.save_data(owner)
return return
end end
if player_name~=owner and gate["type"]=="private" then return end --owner = owner or core.get_meta(pos):get_string("owner")
local dir1=gate["destination_dir"] if gate.type == "private"
and player_name ~= core.get_meta(pos):get_string("owner") then
return
end
local pos1 = vector.new(gate.destination)
if not stargate.findGate(pos1) then
gate.destination = nil
stargate.deactivateGate(pos)
stargate.save_data(core.get_meta(pos):get_string("owner"))
return
end
local dir1 = gate.destination_dir
local dest_angle local dest_angle
if dir1 == 0 then if dir1 == 0 then
pos1.z = pos1.z-2 pos1.z = pos1.z-2
@ -306,7 +233,7 @@ minetest.register_abm({
end end
object:moveto(pos1,false) object:moveto(pos1,false)
object:set_look_yaw(math.rad(dest_angle)) object:set_look_yaw(math.rad(dest_angle))
core.sound_play("enterEventHorizon", {pos = pos, gain = 1.0,loop = false, max_hear_distance = 72,}) core.sound_play("enterEventHorizon", {pos = pos, max_hear_distance = 72})
end end
end end
end end

View File

@ -63,15 +63,16 @@ minetest.register_on_joinplayer(function(player)
stargate.save_data("registered_players") stargate.save_data("registered_players")
stargate.save_data(player_name) stargate.save_data(player_name)
end end
stargate_network["players"][player_name]={} stargate_network["players"][player_name]={
stargate_network["players"][player_name]["formspec"]="" formspec = "",
stargate_network["players"][player_name]["current_page"]=stargate.default_page current_page = stargate.default_page,
stargate_network["players"][player_name]["own_gates"]={} own_gates ={},
stargate_network["players"][player_name]["own_gates_count"]=0 own_gates_count =0,
stargate_network["players"][player_name]["public_gates"]={} public_gates ={},
stargate_network["players"][player_name]["public_gates_count"]=0 public_gates_count =0,
stargate_network["players"][player_name]["current_index"]=0 current_index =0,
stargate_network["players"][player_name]["temp_gate"]={} temp_gate ={},
}
end) end)
stargate.registerGate = function(player_name,pos,dir) stargate.registerGate = function(player_name,pos,dir)
@ -103,25 +104,24 @@ stargate.unregisterGate = function(player_name,pos)
end end
stargate.findGate = function(pos) stargate.findGate = function(pos)
for __,tab in ipairs(stargate_network["registered_players"]) do for _,tab in pairs(stargate_network.registered_players) do
local player_name=tab["player_name"] local player_name=tab.player_name
if type(stargate_network[player_name])=="table" then if type(stargate_network[player_name])=="table" then
for __,gates in ipairs(stargate_network[player_name]) do for _,gates in pairs(stargate_network[player_name]) do
if gates then if gates
if gates["pos"].x==pos.x and gates["pos"].y==pos.y and gates["pos"].z==pos.z then and vector.equals(gates.pos, pos) then
return gates return gates
end end
end end
end end
end end
end
return nil return nil
end end
--show formspec to player --show formspec to player
stargate.gateFormspecHandler = function(pos, node, clicker, itemstack) stargate.gateFormspecHandler = function(pos, node, clicker, itemstack)
local player_name = clicker:get_player_name() local player_name = clicker:get_player_name()
local meta = minetest.env:get_meta(pos) local meta = minetest.get_meta(pos)
local owner=meta:get_string("owner") local owner=meta:get_string("owner")
if player_name~=owner then return end if player_name~=owner then return end
local current_gate=nil local current_gate=nil
@ -154,7 +154,7 @@ stargate.gateFormspecHandler = function(pos, node, clicker, itemstack)
end end
-- print(dump(stargate_network["players"][player_name]["public_gates"])) -- print(dump(stargate_network["players"][player_name]["public_gates"]))
if current_gate==nil then if current_gate == nil then
print ("Gate not registered in network! Please remove it and place once again.") print ("Gate not registered in network! Please remove it and place once again.")
return nil return nil
end end
@ -344,7 +344,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.save_changes then if fields.save_changes then
minetest.sound_play("click", {to_player=player_name, gain = 0.5}) minetest.sound_play("click", {to_player=player_name, gain = 0.5})
local meta = minetest.env:get_meta(temp_gate["pos"]) local meta = minetest.get_meta(temp_gate["pos"])
local infotext="" local infotext=""
current_gate["type"]=temp_gate["type"] current_gate["type"]=temp_gate["type"]
current_gate["description"]=temp_gate["description"] current_gate["description"]=temp_gate["description"]
@ -364,9 +364,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
current_gate["destination"]=nil current_gate["destination"]=nil
end end
if current_gate["destination"] then if current_gate["destination"] then
activateGate (current_gate["pos"]) stargate.activateGate (current_gate.pos)
--stargate.activateGate (current_gate.destination)
else else
deactivateGate (current_gate["pos"]) stargate.deactivateGate (current_gate["pos"])
end end
if current_gate["type"]=="private" then infotext="Private" else infotext="Public" end if current_gate["type"]=="private" then infotext="Private" else infotext="Public" end
infotext=infotext.." Gate: "..current_gate["description"].."\n" infotext=infotext.." Gate: "..current_gate["description"].."\n"