1
0
mirror of https://github.com/HybridDog/nether-pack.git synced 2024-12-26 18:50:24 +01:00
nether-pack/nether/pearl.lua

199 lines
4.3 KiB
Lua
Raw Normal View History

2015-02-18 18:34:03 +01:00
local function table_contains(t, v)
for _,i in pairs(t) do
if v == i then
return true
end
end
return false
end
2017-07-05 18:52:46 +02:00
local creative = minetest.settings:get_bool("creative_mode")
2015-02-18 18:34:03 +01:00
local function throw_pearl(item, player)
local playerpos = player:getpos()
2016-01-01 12:39:21 +01:00
playerpos.y = playerpos.y+1.625
local obj = minetest.add_entity(playerpos, "nether:pearl_entity")
2015-02-18 18:34:03 +01:00
local dir = player:get_look_dir()
2016-01-01 12:39:21 +01:00
obj:setvelocity(vector.multiply(dir, 30))
2015-02-18 18:34:03 +01:00
obj:setacceleration({x=dir.x*-3, y=-dir.y^8*80-10, z=dir.z*-3})
2016-01-01 12:39:21 +01:00
obj:get_luaentity().player = player:get_player_name()
if not creative then
2015-02-18 18:34:03 +01:00
item:take_item()
2016-01-01 12:39:21 +01:00
return item
2015-02-18 18:34:03 +01:00
end
end
2016-01-01 12:39:21 +01:00
local function get_node(pos)
local name = minetest.get_node(pos).name
2016-01-01 14:25:07 +01:00
if name ~= "ignore" then
return name
end
minetest.get_voxel_manip():read_from_map(pos, pos)
name = minetest.get_node_or_nil(pos)
if not name then
return
2016-01-01 12:39:21 +01:00
end
2016-01-01 14:25:07 +01:00
return name.name
2016-01-01 12:39:21 +01:00
end
2015-02-18 18:34:03 +01:00
2016-01-01 12:39:21 +01:00
local softs = {}
local function is_soft(pos)
local name = get_node(pos)
if not name then
return false
end
local is_soft = softs[name]
if is_soft ~= nil then
return is_soft
end
if not minetest.registered_nodes[name] then
softs[name] = false
return false
end
is_soft = minetest.registered_nodes[name].walkable == false
softs[name] = is_soft
return is_soft
end
-- teleports the player there if there's free space
2015-02-18 18:34:03 +01:00
local function teleport_player(pos, player)
if not is_soft(pos) then
return false
2015-02-18 18:34:03 +01:00
end
if not is_soft({x=pos.x, y=pos.y+1, z=pos.z})
and not is_soft({x=pos.x, y=pos.y-1, z=pos.z}) then
return false
end
pos.y = pos.y+0.05
player:moveto(pos)
return true
2015-02-18 18:34:03 +01:00
end
2016-01-01 12:39:21 +01:00
--[[
local dg_ps = {}
local function forceload(pos)
dg_ps[#dg_ps+1] = pos
minetest.forceload_block(pos)
minetest.after(5, function(pos)
minetest.forceload_free_block(pos)
for i,p in pairs(dg_ps) do
if vector.equals(p, pos) then
dg_ps[i] = nil
return
end
end
end, pos)
end
minetest.register_on_shutdown(function()
for _,p in pairs(dg_ps) do
minetest.forceload_free_block(p)
2015-02-18 18:34:03 +01:00
end
2016-01-01 12:39:21 +01:00
end)--]]
minetest.register_entity("nether:pearl_entity", {
collisionbox = {0,0,0,0,0,0}, --not pointable
2016-01-01 14:25:07 +01:00
visual_size = {x=0.1, y=0.1},
2016-01-01 12:39:21 +01:00
physical = false, -- Collides with things
textures = {"nether_pearl.png"},
on_activate = function(self, staticdata)
if not staticdata
or staticdata == "" then
return
end
local tmp = minetest.deserialize(staticdata)
if not tmp then
minetest.log("error", "[nether] pearl: invalid staticdata ")
return
end
self.player = tmp.player
end,
get_staticdata = function(self)
--forceload(vector.round(self.object:getpos()))
return minetest.serialize({
player = self.player,
})
end,
2016-01-01 14:25:07 +01:00
timer = 0,
2016-01-01 12:39:21 +01:00
on_step = function(self, dtime)
self.timer = self.timer+dtime
--[[
local delay = self.delay
if delay < 0.1 then
self.delay = delay+dtime
return
end
self.delay = 0--]]
if self.timer > 20 then
self.object:remove()
return
end
local pos = self.object:getpos()
local rpos = vector.round(pos)
local lastpos = self.lastpos
if not lastpos then
self.lastpos = vector.new(rpos)
return
end
if lastpos.x
and vector.equals(vector.round(lastpos), rpos) then
return
end
local player = self.player
if not player then
minetest.log("error", "[nether] pearl: missing playername")
self.object:remove()
return
end
player = minetest.get_player_by_name(player)
if not player then
minetest.log("error", "[nether] pearl: missing player")
self.object:remove()
return
end
if not get_node(rpos) then
minetest.log("error", "[nether] pearl: missing node")
self.object:remove()
return
end
self.lastpos = vector.new(pos)
2015-02-18 18:34:03 +01:00
local free, p = minetest.line_of_sight(lastpos, pos)
2016-01-01 12:39:21 +01:00
if free then
return
end
if is_soft(p) then
return
end
self.object:remove()
minetest.after(0, function(p) --minetest.after is used that the sound is played after the teleportation
minetest.sound_play("nether_pearl", {pos=p, max_hear_distance=10})
end, p)
p.y = p.y+1
2016-01-01 14:25:07 +01:00
if teleport_player(vector.new(p), player) then
2016-01-01 12:39:21 +01:00
return
end
p.y = p.y-2
for i = -1,1,2 do
for _,j in pairs({{i, 0}, {0, i}}) do
if teleport_player({x=p.x+j[1], y=p.y, z=p.z+j[2]}, player) then
2015-02-18 18:34:03 +01:00
return
end
2016-01-01 12:39:21 +01:00
end
end
for i = -1,1,2 do
for j = -1,1,2 do
if teleport_player({x=p.x+j, y=p.y, z=p.z+i}, player) then
return
2015-02-18 18:34:03 +01:00
end
end
end
end
2016-01-01 12:39:21 +01:00
})
2015-02-18 18:34:03 +01:00
minetest.override_item("nether:pearl", {on_use = throw_pearl})