From 4f88bf11d6f01cc0f09888e09dcec722c488ae79 Mon Sep 17 00:00:00 2001
From: TenPlus1 <kinsellaja@yahoo.com>
Date: Sun, 8 May 2016 11:14:27 +0100
Subject: [PATCH] Tweaked and tidies code, added map_generation_limit's

---
 README.md |  13 ++--
 init.lua  | 221 ++++++++++++++++++++++++++----------------------------
 2 files changed, 113 insertions(+), 121 deletions(-)

diff --git a/README.md b/README.md
index 7970042..645c916 100644
--- a/README.md
+++ b/README.md
@@ -7,9 +7,10 @@ https://forum.minetest.net/viewtopic.php?f=9&t=9234
 
 Changes:
 
-0.5 - Added throwable potions
-0.4 - Code tidy and particle effects added
-0.3 - Added Teleport Pad
-0.2 - Bug fixes
-0.1 - Added SFX
-0.0 - Initial release
\ No newline at end of file
+- 0.6 - Tweaked and tidied code, added map_generation_limit's
+- 0.5 - Added throwable potions
+- 0.4 - Code tidy and particle effects added
+- 0.3 - Added Teleport Pad
+- 0.2 - Bug fixes
+- 0.1 - Added SFX
+- 0.0 - Initial release
\ No newline at end of file
diff --git a/init.lua b/init.lua
index f9eed37..825bc53 100755
--- a/init.lua
+++ b/init.lua
@@ -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
 -- 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...
 
-teleport = {}
+-- max teleport distance
+local dist = tonumber(minetest.setting_get("map_generation_limit") or 31000)
 
--- teleport portal recipe
-minetest.register_craft({
-	output = 'teleport_potion:potion',
-	recipe = {
-		{"", "default:diamond", ""},
-		{"default:diamond", "vessels:glass_bottle", "default:diamond"},
-		{"", "default:diamond", ""},
-	},
-})
+local check_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
+		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)
 
--- default coords (from static spawnpoint or default values at end)
-teleport.default = (minetest.setting_get_pos("static_spawnpoint") or {x = 0, y = 2, z = 0})
+	-- 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
 minetest.register_node("teleport_potion:portal", {
@@ -50,7 +79,7 @@ minetest.register_node("teleport_potion:portal", {
 	buildable_to = true,
 	waving = 1,
 	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
 	on_construct = function(pos)
@@ -81,7 +110,7 @@ minetest.register_node("teleport_potion:potion", {
 	description="Teleport Potion (place and right-click to enchant location)",
 	inventory_image = "potion.png",
 	wield_image = "potion.png",
-	groups = {snappy = 3, dig_immediate = 3},
+	groups = {dig_immediate = 3},
 	selection_box = {type = "wallmounted"},
 
 	on_construct = function(pos)
@@ -89,14 +118,14 @@ minetest.register_node("teleport_potion:potion", {
 		local meta = minetest.get_meta(pos)
 
 		-- text entry formspec
-		meta:set_string("formspec", "field[text;;${text}]")
-		meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)")
-		meta:set_string("text", teleport.default.x..","..teleport.default.y..","..teleport.default.z)
+		meta:set_string("formspec", "field[text;Enter teleport coords (e.g. 200,20,-200);${text}]")
+		meta:set_string("infotext", "Right-click to enchant teleport location")
+		meta:set_string("text", pos.x .. "," .. pos.y .. "," .. pos.z)
 
 		-- set default coords
-		meta:set_float("x", teleport.default.x)
-		meta:set_float("y", teleport.default.y)
-		meta:set_float("z", teleport.default.z)
+		meta:set_int("x", pos.x)
+		meta:set_int("y", pos.y)
+		meta:set_int("z", pos.z)
 	end,
 
 	-- 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
 	on_receive_fields = function(pos, formname, fields, sender)
 
-		local coords = teleport.coordinates(fields.text)
-		local meta = minetest.get_meta(pos)
+		local coords = check_coordinates(fields.text)
 		local name = sender:get_player_name()
 
 		if coords then
 
 			minetest.set_node(pos, {name = "teleport_potion:portal"})
 
-			local newmeta = minetest.get_meta(pos)
+			local meta = minetest.get_meta(pos)
 
 			-- set portal destination
-			newmeta:set_float("x", coords.x)
-			newmeta:set_float("y", coords.y)
-			newmeta:set_float("z", coords.z)
-			newmeta:set_string("text", fields.text)
+			meta:set_int("x", coords.x)
+			meta:set_int("y", coords.y)
+			meta:set_int("z", coords.z)
 
 			-- portal open effect and sound
 			tp_effect(pos)
@@ -146,6 +173,16 @@ minetest.register_node("teleport_potion:potion", {
 	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
 minetest.register_node("teleport_potion:pad", {
 	tiles = {"padd.png"},
@@ -173,28 +210,19 @@ minetest.register_node("teleport_potion:pad", {
 		local meta = minetest.get_meta(pos)
 
 		-- text entry formspec
-		meta:set_string("formspec", "field[text;;${text}]")
-		meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)")
-		meta:set_string("text", teleport.default.x
-			.. "," .. teleport.default.y
-			.. "," .. teleport.default.z)
+		meta:set_string("formspec", "field[text;Enter teleport coords (e.g. 200,20,-200);${text}]")
+		meta:set_string("infotext", "Right-click to enchant teleport location")
+		meta:set_string("text", pos.x .. "," .. pos.y .. "," .. pos.z)
 
 		-- set default coords
-		meta:set_float("x", teleport.default.x)
-		meta:set_float("y", teleport.default.y)
-		meta:set_float("z", teleport.default.z)
-	end,
-
-	-- right-click to enter new coords
-	on_right_click = function(pos, placer)
-		local meta = minetest.get_meta(pos)
+		meta:set_int("x", pos.x)
+		meta:set_int("y", pos.y)
+		meta:set_int("z", pos.z)
 	end,
 
 	-- once entered, check coords, if ok then return potion
 	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()
 
 		if minetest.is_protected(pos, name) then
@@ -202,16 +230,20 @@ minetest.register_node("teleport_potion:pad", {
 			return
 		end
 
+		local coords = check_coordinates(fields.text)
+
 		if coords then
 
-			local newmeta = minetest.get_meta(pos)
+			local meta = minetest.get_meta(pos)
+
+			meta:set_int("x", coords.x)
+			meta:set_int("y", coords.y)
+			meta:set_int("z", coords.z)
+			meta:set_string("text", fields.text)
 
-			newmeta:set_float("x", coords.x)
-			newmeta:set_float("y", coords.y)
-			newmeta:set_float("z", coords.z)
-			newmeta: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", {
 				pos = pos,
 				gain = 1.0,
@@ -224,61 +256,20 @@ minetest.register_node("teleport_potion:pad", {
 	end,
 })
 
-teleport.coordinates = function(str)
-
-	if not str or str == "" then
-		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
+-- 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"}
+	}
+})
 
--- check pad and teleport objects on top
+-- check portal & pad, teleport any entities on top
 minetest.register_abm({
 	nodenames = {"teleport_potion:portal", "teleport_potion:pad"},
-	interval = 1,
+	interval = 2,
 	chance = 1,
 	catch_up = false,
 
@@ -294,14 +285,14 @@ minetest.register_abm({
 		-- get coords from pad/portal
 		local meta = minetest.get_meta(pos)
 		local target_coords = {
-			x = meta:get_float("x"),
-			y = meta:get_float("y"),
-			z = meta:get_float("z")
+			x = meta:get_int("x"),
+			y = meta:get_int("y"),
+			z = meta:get_int("z")
 		}
 
 		for k, player in pairs(objs) do
 
-			if player:get_player_name() then
+			if player then
 
 				-- play sound on portal end
 				minetest.sound_play("portal_close", {
@@ -327,7 +318,7 @@ minetest.register_abm({
 	end
 })
 
--- Throwable potions
+-- Throwable potion
 
 local potion_entity = {
 	physical = true,
-- 
GitLab