Tweaked and tidies code, added map_generation_limit's

This commit is contained in:
TenPlus1 2016-05-08 11:14:27 +01:00
parent 632d0685b6
commit 4f88bf11d6
2 changed files with 113 additions and 121 deletions

View file

@ -7,9 +7,10 @@ https://forum.minetest.net/viewtopic.php?f=9&t=9234
Changes: Changes:
0.5 - Added throwable potions - 0.6 - Tweaked and tidied code, added map_generation_limit's
0.4 - Code tidy and particle effects added - 0.5 - Added throwable potions
0.3 - Added Teleport Pad - 0.4 - Code tidy and particle effects added
0.2 - Bug fixes - 0.3 - Added Teleport Pad
0.1 - Added SFX - 0.2 - Bug fixes
0.0 - Initial release - 0.1 - Added SFX
- 0.0 - Initial release

221
init.lua
View file

@ -1,34 +1,63 @@
--= Teleport Potion mod 0.5 by TenPlus1 --= Teleport Potion mod 0.6 by TenPlus1
-- Create teleport potion or pad, place then right-click to enter coords -- Create teleport potion or pad, place then right-click to enter coords
-- and step onto pad or walk into the blue portal light, portal closes after -- and step onto pad or walk into the blue portal light, portal closes after
-- 10 seconds, pad remains, potions are throwable... SFX are license Free... -- 10 seconds, pad remains, potions are throwable... SFX are license Free...
teleport = {} -- max teleport distance
local dist = tonumber(minetest.setting_get("map_generation_limit") or 31000)
-- teleport portal recipe local check_coordinates = function(str)
minetest.register_craft({
output = 'teleport_potion:potion',
recipe = {
{"", "default:diamond", ""},
{"default:diamond", "vessels:glass_bottle", "default:diamond"},
{"", "default:diamond", ""},
},
})
-- teleport pad recipe if not str or str == "" then
minetest.register_craft({ return nil
output = 'teleport_potion:pad', end
recipe = {
{"teleport_potion:potion", 'default:glass', "teleport_potion:potion"},
{"default:glass", "default:mese", "default:glass"},
{"teleport_potion:potion", "default:glass", "teleport_potion:potion"}
}
})
-- default coords (from static spawnpoint or default values at end) -- get coords from string
teleport.default = (minetest.setting_get_pos("static_spawnpoint") or {x = 0, y = 2, z = 0}) local x, y, z = string.match(str, "^(-?%d+),(-?%d+),(-?%d+)")
-- check coords
if x == nil or string.len(x) > 6
or y == nil or string.len(y) > 6
or z == nil or string.len(z) > 6 then
return nil
end
-- convert string coords to numbers
x = tonumber(x)
y = tonumber(y)
z = tonumber(z)
-- are coords in map range ?
if x > dist or x < -dist
or y > dist or y < -dist
or z > dist or z < -dist then
return nil
end
-- return ok coords
return {x = x, y = y, z = z}
end
-- particle effects
function tp_effect(pos)
minetest.add_particlespawner({
amount = 20,
time = 0.25,
minpos = pos,
maxpos = pos,
minvel = {x = -2, y = -2, z = -2},
maxvel = {x = 2, y = 2, z = 2},
minacc = {x = -4, y = -4, z = -4},
maxacc = {x = 4, y = 4, z = 4},
minexptime = 0.1,
maxexptime = 1,
minsize = 0.5,
maxsize = 1,
texture = "particle.png",
})
end
-- teleport portal -- teleport portal
minetest.register_node("teleport_potion:portal", { minetest.register_node("teleport_potion:portal", {
@ -50,7 +79,7 @@ minetest.register_node("teleport_potion:portal", {
buildable_to = true, buildable_to = true,
waving = 1, waving = 1,
sunlight_propagates = true, sunlight_propagates = true,
damage_per_second = 1, -- walking into portal also hurts player damage_per_second = 1, -- walking into portal hurts player
-- start timer when portal appears -- start timer when portal appears
on_construct = function(pos) on_construct = function(pos)
@ -81,7 +110,7 @@ minetest.register_node("teleport_potion:potion", {
description="Teleport Potion (place and right-click to enchant location)", description="Teleport Potion (place and right-click to enchant location)",
inventory_image = "potion.png", inventory_image = "potion.png",
wield_image = "potion.png", wield_image = "potion.png",
groups = {snappy = 3, dig_immediate = 3}, groups = {dig_immediate = 3},
selection_box = {type = "wallmounted"}, selection_box = {type = "wallmounted"},
on_construct = function(pos) on_construct = function(pos)
@ -89,14 +118,14 @@ minetest.register_node("teleport_potion:potion", {
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
-- text entry formspec -- text entry formspec
meta:set_string("formspec", "field[text;;${text}]") meta:set_string("formspec", "field[text;Enter teleport coords (e.g. 200,20,-200);${text}]")
meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)") meta:set_string("infotext", "Right-click to enchant teleport location")
meta:set_string("text", teleport.default.x..","..teleport.default.y..","..teleport.default.z) meta:set_string("text", pos.x .. "," .. pos.y .. "," .. pos.z)
-- set default coords -- set default coords
meta:set_float("x", teleport.default.x) meta:set_int("x", pos.x)
meta:set_float("y", teleport.default.y) meta:set_int("y", pos.y)
meta:set_float("z", teleport.default.z) meta:set_int("z", pos.z)
end, end,
-- throw potion when used like tool -- throw potion when used like tool
@ -113,21 +142,19 @@ minetest.register_node("teleport_potion:potion", {
-- check if coords ok then open portal, otherwise return potion -- check if coords ok then open portal, otherwise return potion
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local coords = teleport.coordinates(fields.text) local coords = check_coordinates(fields.text)
local meta = minetest.get_meta(pos)
local name = sender:get_player_name() local name = sender:get_player_name()
if coords then if coords then
minetest.set_node(pos, {name = "teleport_potion:portal"}) minetest.set_node(pos, {name = "teleport_potion:portal"})
local newmeta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
-- set portal destination -- set portal destination
newmeta:set_float("x", coords.x) meta:set_int("x", coords.x)
newmeta:set_float("y", coords.y) meta:set_int("y", coords.y)
newmeta:set_float("z", coords.z) meta:set_int("z", coords.z)
newmeta:set_string("text", fields.text)
-- portal open effect and sound -- portal open effect and sound
tp_effect(pos) tp_effect(pos)
@ -146,6 +173,16 @@ minetest.register_node("teleport_potion:potion", {
end, end,
}) })
-- teleport potion recipe
minetest.register_craft({
output = 'teleport_potion:potion',
recipe = {
{"", "default:diamond", ""},
{"default:diamond", "vessels:glass_bottle", "default:diamond"},
{"", "default:diamond", ""},
},
})
-- teleport pad -- teleport pad
minetest.register_node("teleport_potion:pad", { minetest.register_node("teleport_potion:pad", {
tiles = {"padd.png"}, tiles = {"padd.png"},
@ -173,28 +210,19 @@ minetest.register_node("teleport_potion:pad", {
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
-- text entry formspec -- text entry formspec
meta:set_string("formspec", "field[text;;${text}]") meta:set_string("formspec", "field[text;Enter teleport coords (e.g. 200,20,-200);${text}]")
meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)") meta:set_string("infotext", "Right-click to enchant teleport location")
meta:set_string("text", teleport.default.x meta:set_string("text", pos.x .. "," .. pos.y .. "," .. pos.z)
.. "," .. teleport.default.y
.. "," .. teleport.default.z)
-- set default coords -- set default coords
meta:set_float("x", teleport.default.x) meta:set_int("x", pos.x)
meta:set_float("y", teleport.default.y) meta:set_int("y", pos.y)
meta:set_float("z", teleport.default.z) meta:set_int("z", pos.z)
end,
-- right-click to enter new coords
on_right_click = function(pos, placer)
local meta = minetest.get_meta(pos)
end, end,
-- once entered, check coords, if ok then return potion -- once entered, check coords, if ok then return potion
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local coords = teleport.coordinates(fields.text)
local meta = minetest.get_meta(pos)
local name = sender:get_player_name() local name = sender:get_player_name()
if minetest.is_protected(pos, name) then if minetest.is_protected(pos, name) then
@ -202,16 +230,20 @@ minetest.register_node("teleport_potion:pad", {
return return
end end
local coords = check_coordinates(fields.text)
if coords then if coords then
local newmeta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
newmeta:set_float("x", coords.x) meta:set_int("x", coords.x)
newmeta:set_float("y", coords.y) meta:set_int("y", coords.y)
newmeta:set_float("z", coords.z) meta:set_int("z", coords.z)
newmeta:set_string("text", fields.text) meta:set_string("text", fields.text)
meta:set_string("infotext", "Pad Active ("
.. coords.x .. "," .. coords.y .. "," .. coords.z .. ")")
meta:set_string("infotext", "Pad Active ("..coords.x..","..coords.y..","..coords.z..")")
minetest.sound_play("portal_open", { minetest.sound_play("portal_open", {
pos = pos, pos = pos,
gain = 1.0, gain = 1.0,
@ -224,61 +256,20 @@ minetest.register_node("teleport_potion:pad", {
end, end,
}) })
teleport.coordinates = function(str) -- teleport pad recipe
minetest.register_craft({
output = 'teleport_potion:pad',
recipe = {
{"teleport_potion:potion", 'default:glass', "teleport_potion:potion"},
{"default:glass", "default:mese", "default:glass"},
{"teleport_potion:potion", "default:glass", "teleport_potion:potion"}
}
})
if not str or str == "" then -- check portal & pad, teleport any entities on top
return nil
end
-- get coords from string
local x, y, z = string.match(str, "^(-?%d+),(-?%d+),(-?%d+)")
-- check coords
if x == nil or string.len(x) > 6
or y == nil or string.len(y) > 6
or z == nil or string.len(z) > 6 then
return nil
end
-- convert string coords to numbers
x = tonumber(x)
y = tonumber(y)
z = tonumber(z)
-- are coords in map range ?
if x > 30900 or x < -30900
or y > 30900 or y < -30900
or z > 30900 or z < -30900 then
return nil
end
-- return ok coords
return {x = x, y = y, z = z}
end
-- particle effects
function tp_effect(pos)
minetest.add_particlespawner({
amount = 20,
time = 0.25,
minpos = pos,
maxpos = pos,
minvel = {x = -2, y = -2, z = -2},
maxvel = {x = 2, y = 2, z = 2},
minacc = {x = -4, y = -4, z = -4},
maxacc = {x = 4, y = 4, z = 4},
minexptime = 0.1,
maxexptime = 1,
minsize = 0.5,
maxsize = 1,
texture = "particle.png",
})
end
-- check pad and teleport objects on top
minetest.register_abm({ minetest.register_abm({
nodenames = {"teleport_potion:portal", "teleport_potion:pad"}, nodenames = {"teleport_potion:portal", "teleport_potion:pad"},
interval = 1, interval = 2,
chance = 1, chance = 1,
catch_up = false, catch_up = false,
@ -294,14 +285,14 @@ minetest.register_abm({
-- get coords from pad/portal -- get coords from pad/portal
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local target_coords = { local target_coords = {
x = meta:get_float("x"), x = meta:get_int("x"),
y = meta:get_float("y"), y = meta:get_int("y"),
z = meta:get_float("z") z = meta:get_int("z")
} }
for k, player in pairs(objs) do for k, player in pairs(objs) do
if player:get_player_name() then if player then
-- play sound on portal end -- play sound on portal end
minetest.sound_play("portal_close", { minetest.sound_play("portal_close", {
@ -327,7 +318,7 @@ minetest.register_abm({
end end
}) })
-- Throwable potions -- Throwable potion
local potion_entity = { local potion_entity = {
physical = true, physical = true,