From 39f4573294d68f4307daca8c46cb9b47324734e3 Mon Sep 17 00:00:00 2001
From: Wuzzy <almikes@aol.com>
Date: Thu, 17 Aug 2017 21:26:09 +0200
Subject: [PATCH] Randomly spin compass and clock in VoidNetherEnd

---
 mods/ITEMS/mcl_clock/init.lua   | 43 +++++++++++++++++--------
 mods/ITEMS/mcl_compass/init.lua | 57 +++++++++++++++++++++------------
 2 files changed, 67 insertions(+), 33 deletions(-)

diff --git a/mods/ITEMS/mcl_clock/init.lua b/mods/ITEMS/mcl_clock/init.lua
index 6a40630ce..48bced175 100644
--- a/mods/ITEMS/mcl_clock/init.lua
+++ b/mods/ITEMS/mcl_clock/init.lua
@@ -12,10 +12,17 @@ mcl_clock.stereotype = "mcl_clock:clock"
 local watch = {}
 watch.old_time = -1
 
--- Image of all 64 possible faces
+local clock_frames = 64
+
+-- Timer for random clock spinning
+local random_timer = 0.0
+local random_timer_trigger = 1.0 -- random clock spinning tick in seconds. Increase if there are performance problems
+local random_frame = math.random(0, clock_frames-1)
+
+-- Image of all possible faces
 watch.images = {}
-for frame=0,63 do
-	table.insert(watch.images, "mcl_clock_clock.png^[verticalframe:64:"..frame)
+for frame=0, clock_frames-1 do
+	table.insert(watch.images, "mcl_clock_clock.png^[verticalframe:"..clock_frames..":"..frame)
 end
 
 local function round(num)
@@ -23,9 +30,9 @@ local function round(num)
 end
 
 function watch.get_clock_frame()
-	local t = 64 * minetest.get_timeofday()
+	local t = clock_frames * minetest.get_timeofday()
 	t = round(t)
-	if t == 64 then t = 0 end
+	if t == clock_frames then t = 0 end
 	return tostring(t)
 end
 
@@ -65,6 +72,12 @@ local force_clock_update_timer = 0
 minetest.register_globalstep(function(dtime)
 	local now = watch.get_clock_frame()
 	force_clock_update_timer = force_clock_update_timer + dtime
+	random_timer = random_timer + dtime
+	-- This causes the random spinning of the clock
+	if random_timer >= random_timer_trigger then
+		random_frame = (random_frame + math.random(-4, 4)) % clock_frames
+		random_timer = 0
+	end
 
 	if watch.old_time == now and force_clock_update_timer < 60 then
 		return
@@ -77,14 +90,18 @@ minetest.register_globalstep(function(dtime)
 	for p, player in ipairs(players) do
 		for s, stack in ipairs(player:get_inventory():get_list("main")) do
 			local _, dim = mcl_util.y_to_layer(player:getpos().y)
+			local frame
 			-- Clocks do not work in the End, Nether or the Void
-			if dim ~= "end" and dim ~= "nether" and dim ~= "void" then
-				local count = stack:get_count()
-				if stack:get_name() == mcl_clock.stereotype then
-					player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..now.." "..count)
-				elseif string.sub(stack:get_name(), 1, 16) == "mcl_clock:clock_" then
-					player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..now.." "..count)
-				end
+			if dim == "end" or dim == "nether" or dim == "void" then
+				frame = random_frame
+			else
+				frame = now
+			end
+			local count = stack:get_count()
+			if stack:get_name() == mcl_clock.stereotype then
+				player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..frame.." "..count)
+			elseif minetest.get_item_group(stack:get_name(), "clock") ~= 0 then
+				player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..frame.." "..count)
 			end
 		end
 	end
@@ -111,7 +128,7 @@ minetest.register_craft({
 watch.register_item(mcl_clock.stereotype, watch.images[1], true, 1)
 
 -- Faces
-for a=0,63,1 do
+for a=0,clock_frames-1,1 do
 	local b = a
 	if b > 31 then
 		b = b - 32
diff --git a/mods/ITEMS/mcl_compass/init.lua b/mods/ITEMS/mcl_compass/init.lua
index 8c230b223..ff9db2aeb 100644
--- a/mods/ITEMS/mcl_compass/init.lua
+++ b/mods/ITEMS/mcl_compass/init.lua
@@ -1,9 +1,23 @@
 mcl_compass = {}
 
+local compass_frames = 32
+
 local default_spawn_settings = minetest.settings:get("static_spawnpoint")
 
+-- Timer for random compass spinning
+local random_timer = 0
+local random_timer_trigger = 0.5 -- random compass spinning tick in seconds. Increase if there are performance problems
+
+local random_frame = math.random(0, compass_frames-1)
+
 minetest.register_globalstep(function(dtime)
+	random_timer = random_timer + dtime
 	local players  = minetest.get_connected_players()
+
+	if random_timer >= random_timer_trigger then
+		random_frame = (random_frame + math.random(-1, 1)) % compass_frames
+		random_timer = 0
+	end
 	for i,player in ipairs(players) do
 		local function has_compass(player)
 			for _,stack in ipairs(player:get_inventory():get_list("main")) do
@@ -14,34 +28,37 @@ minetest.register_globalstep(function(dtime)
 			return false
 		end
 		if has_compass(player) then
-			local spawn = {x=0,y=0,z=0}
-			local s = minetest.settings:get("static_spawnpoint")
-			if s then
-				local numbers = string.split(s, ",")
-				spawn.x = tonumber(numbers[1])
-				spawn.y = tonumber(numbers[2])
-				spawn.z = tonumber(numbers[3])
-				if type(spawn.x) ~= "number" and type(spawn.y) ~= "number" and type(spawn.z) ~= "number" then
-					spawn = {x=0,y=0,z=0}
-				end
-			end
 			local pos = player:getpos()
 			local _, dim = mcl_util.y_to_layer(pos.y)
+			local compass_image
 			-- Compasses do not work in the End, Nether or the Void
-			if dim ~= "end" and dim ~= "nether" and dim ~= "void" then
+			if dim == "end" or dim == "nether" or dim == "void" then
+				compass_image = random_frame
+			else
+				local spawn = {x=0,y=0,z=0}
+				local s = minetest.settings:get("static_spawnpoint")
+				if s then
+					local numbers = string.split(s, ",")
+					spawn.x = tonumber(numbers[1])
+					spawn.y = tonumber(numbers[2])
+					spawn.z = tonumber(numbers[3])
+					if type(spawn.x) ~= "number" and type(spawn.y) ~= "number" and type(spawn.z) ~= "number" then
+						spawn = {x=0,y=0,z=0}
+					end
+				end
 				local dir = player:get_look_horizontal()
 				local angle_north = math.deg(math.atan2(spawn.x - pos.x, spawn.z - pos.z))
 				if angle_north < 0 then angle_north = angle_north + 360 end
 				local angle_dir = -math.deg(dir)
 				local angle_relative = (angle_north - angle_dir + 180) % 360
-				local compass_image = math.floor((angle_relative/11.25) + 0.5)%32
+				compass_image = math.floor((angle_relative/11.25) + 0.5) % compass_frames
+			end
 
-				for j,stack in ipairs(player:get_inventory():get_list("main")) do
-					if minetest.get_item_group(stack:get_name(), "compass") ~= 0 and
-							minetest.get_item_group(stack:get_name(), "compass")-1 ~= compass_image then
-						local count = stack:get_count()
-						player:get_inventory():set_stack("main", j, ItemStack("mcl_compass:"..compass_image.." "..count))
-					end
+			for j,stack in ipairs(player:get_inventory():get_list("main")) do
+				if minetest.get_item_group(stack:get_name(), "compass") ~= 0 and
+						minetest.get_item_group(stack:get_name(), "compass")-1 ~= compass_image then
+					local count = stack:get_count()
+					player:get_inventory():set_stack("main", j, ItemStack("mcl_compass:"..compass_image.." "..count))
 				end
 			end
 		end
@@ -49,7 +66,7 @@ minetest.register_globalstep(function(dtime)
 end)
 
 local images = {}
-for frame=0,31 do
+for frame = 0, compass_frames-1 do
 	local s = string.format("%02d", frame)
 	table.insert(images, "mcl_compass_compass_"..s..".png")
 end