From 5d74e552f9599437ac70c97e426178f31a281e36 Mon Sep 17 00:00:00 2001
From: TenPlus1 <kinsellaja@yahoo.com>
Date: Tue, 8 Sep 2015 16:56:22 +0100
Subject: [PATCH] Code tidy and particle effect added

---
 init.lua              | 129 ++++++++++++++++++++++++++----------------
 textures/particle.png | Bin 0 -> 173 bytes
 2 files changed, 81 insertions(+), 48 deletions(-)
 create mode 100644 textures/particle.png

diff --git a/init.lua b/init.lua
index 2e34fb3..badbfb7 100755
--- a/init.lua
+++ b/init.lua
@@ -1,19 +1,20 @@
 
---= Teleport Potion mod 0.3 by TenPlus1
+--= Teleport Potion mod 0.4 by TenPlus1
 
---= Create potion/pad, right-click to enter coords and walk into the blue light,
---= Portal closes after 10 seconds, pad remains...  SFX are license Free...
+-- 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...  SFX are license Free...
 
 teleport = {}
 
--- Teleport Portal recipe
+-- teleport portal recipe
 minetest.register_craft({
  	output = 'teleport_potion:potion',
 	type = "shapeless",
  	recipe = {'vessels:glass_bottle', 'default:diamondblock'}
 })
 
--- Teleport Pad recipe
+-- teleport pad recipe
 minetest.register_craft({
 	output = 'teleport_potion:pad',
 	recipe = {
@@ -23,10 +24,10 @@ minetest.register_craft({
 	}
 })
 
--- Default coords
-teleport.default = {x = 0, y = 0, z = 0}
+-- 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})
 
--- Portal
+-- teleport portal
 minetest.register_node("teleport_potion:portal", {
 	drawtype = "plantlike",
 	tiles = {
@@ -46,14 +47,14 @@ 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 also hurts player
 
-	-- Start timer when portal appears
+	-- start timer when portal appears
 	on_construct = function(pos)
 		minetest.get_node_timer(pos):start(10)
 	end,
 
-	-- Remove portal after 10 seconds
+	-- remove portal after 10 seconds
 	on_timer = function(pos)
 		minetest.sound_play("portal_close", {
 			pos = pos,
@@ -64,7 +65,7 @@ minetest.register_node("teleport_potion:portal", {
 	end,
 })
 
--- Potion
+-- teleport potion
 minetest.register_node("teleport_potion:potion", {
 	tile_images = {"pad.png"},
 	drawtype = "signlike",
@@ -75,7 +76,6 @@ minetest.register_node("teleport_potion:potion", {
 	description="Teleport Potion (place and right-click to enchant location)",
 	inventory_image = "potion.png",
 	wield_image = "potion.png",
-	metadata_name = "sign",
 	groups = {snappy = 3, dig_immediate = 3},
 	selection_box = {type = "wallmounted"},
 
@@ -83,41 +83,44 @@ minetest.register_node("teleport_potion:potion", {
 
 		local meta = minetest.get_meta(pos)
 
-		-- Text entry formspec
+		-- 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)
 
-		-- Load with default coords
-		meta:set_float("enabled", -1)
+		-- 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
+	-- right-click to enter new coords
 	on_right_click = function(pos, placer)
 		local meta = minetest.get_meta(pos)
 	end,
 
-	-- Once entered, check coords and teleport, otherwise return 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 name = sender:get_player_name()
 
-		if coords then	
+		if coords then
 
 			minetest.add_node(pos, {name = "teleport_potion:portal"})
 
 			local newmeta = 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)
 
+			-- portal open effect and sound
+			effect(pos)
+
 			minetest.sound_play("portal_open", {
 				pos = pos,
 				gain = 1.0,
@@ -132,7 +135,7 @@ minetest.register_node("teleport_potion:potion", {
 	end,
 })
 
--- Pad
+-- teleport pad
 minetest.register_node("teleport_potion:pad", {
 	tile_images = {"padd.png"},
 	drawtype = 'nodebox',
@@ -143,9 +146,8 @@ minetest.register_node("teleport_potion:pad", {
 	description="Teleport Pad (place and right-click to enchant location)",
 	inventory_image = "padd.png",
 	wield_image = "padd.png",
-	metadata_name = "sign",
 	light_source = 5,
-	groups = {snappy = 3, dig_immediate = 3},
+	groups = {snappy = 3},
 	node_box = {
 		type = "wallmounted",
 		wall_top    = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
@@ -158,24 +160,23 @@ minetest.register_node("teleport_potion:pad", {
 
 		local meta = minetest.get_meta(pos)
 
-		-- Text entry formspec
+		-- 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)
 
-		-- Load with default coords
-		meta:set_float("enabled", -1)
+		-- 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
+	-- right-click to enter new coords
 	on_right_click = function(pos, placer)
 		local meta = minetest.get_meta(pos)
 	end,
 
-	-- Once entered, check coords and teleport, otherwise return potion
+	-- once entered, check coords, if ok then return potion
 	on_receive_fields = function(pos, formname, fields, sender)
 
 		local coords = teleport.coordinates(fields.text)
@@ -187,7 +188,7 @@ minetest.register_node("teleport_potion:pad", {
 			return
 		end
 
-		if coords then	
+		if coords then
 
 			local newmeta = minetest.get_meta(pos)
 
@@ -209,60 +210,92 @@ minetest.register_node("teleport_potion:pad", {
 	end,
 })
 
--- Check coords
 teleport.coordinates = function(str)
 
 	if not str or str == "" then return nil end
 
-	-- Get coords from string
+	-- get coords from string
 	local x, y, z = string.match(str, "^(-?%d+),(-?%d+),(-?%d+)")
 
-	-- Check coords
+	-- 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 = x + 0.0
-	y = y + 0.0
-	z = z + 0.0
+	-- convert string coords to numbers
+	x = tonumber(x)
+	y = tonumber(y)
+	z = tonumber(z)
 
-	-- Are coords in map range ?
+	-- 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 ok coords
 	return {x = x, y = y, z = z}
 end
 
--- Has player walked inside portal
+-- particle effects
+function 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({
 	nodenames = {"teleport_potion:portal", "teleport_potion:pad"},
-	interval = 1.0,
+	interval = 1,
 	chance = 1,
 
 	action = function(pos, node, active_object_count, active_object_count_wider)
+
+		-- check objects inside pad/portal
 		local objs = minetest.get_objects_inside_radius(pos, 1)
-		local meta, target_coords
+		if #objs == 0 then return end
+
+		-- 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")
+		}
+
 		for k, player in pairs(objs) do
-			if player:get_player_name() then 
-				meta = minetest.get_meta(pos)
-				target_coords={
-					x=meta:get_float("x"),
-					y=meta:get_float("y"),
-					z=meta:get_float("z")
-				}
+			if player:get_player_name() then
+
+				-- play sound on portal end
 				minetest.sound_play("portal_close", {
 					pos = pos,
 					gain = 1.0,
 					max_hear_distance = 5
 				})
+
+				-- move player/object
 				player:moveto(target_coords, false)
+
+				-- paricle effects on arrival
+				effect(target_coords)
+
+				-- play sound on destination end
 				minetest.sound_play("portal_close", {
 					pos = target_coords,
 					gain = 1.0,
@@ -270,5 +303,5 @@ minetest.register_abm({
 				})
 			end
 		end
-	end	
+	end
 })
\ No newline at end of file
diff --git a/textures/particle.png b/textures/particle.png
new file mode 100644
index 0000000000000000000000000000000000000000..9a53b500b9c6b4d093de78d7cf9eed33927dd7e2
GIT binary patch
literal 173
zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPHV5AX?bO)b&wzHwvqqemy+zJ2=b
zThPm{R-hDPNswPKgTu2MX+VyTr;B3<$Mxg{1wo4$4w776Itg|Yjxr=kGTh`#;1m;M
zXWk&Qxx1U&I6`m&W5~%v3`=5V9C*@IQ`1!!h>0t^s;a6k5K>chbaZ57V@S8*SX7$W
RSO7Ga!PC{xWt~$(699z7GhqM#

literal 0
HcmV?d00001

-- 
GitLab