2019-03-07 23:40:43 +01:00
local S = minetest.get_translator ( " mcl_clock " )
2017-01-09 03:02:23 +01:00
--[[
2021-05-03 13:43:46 +02:00
mcl_clock , renew of the renew of the mcl_clock mod
2017-01-09 03:02:23 +01:00
Original from Echo , here : http : // forum.minetest . net / viewtopic.php ? id = 3795
] ] --
2017-02-20 21:16:51 +01:00
mcl_clock = { }
2017-03-20 18:12:05 +01:00
-- This is the itemstring of the default clock item. It is used for the default inventory image, help entries, and the like
mcl_clock.stereotype = " mcl_clock:clock "
2021-05-03 13:43:46 +02:00
mcl_clock.old_time = - 1
2017-01-09 03:08:24 +01:00
2017-08-17 21:26:09 +02:00
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
2021-05-03 13:43:46 +02:00
mcl_clock.images = { }
2017-08-17 21:26:09 +02:00
for frame = 0 , clock_frames - 1 do
2018-05-20 01:50:53 +02:00
local sframe = tostring ( frame )
if string.len ( sframe ) == 1 then
sframe = " 0 " .. sframe
end
2021-05-03 13:43:46 +02:00
table.insert ( mcl_clock.images , " mcl_clock_clock_ " .. sframe .. " .png " )
2017-01-09 03:08:24 +01:00
end
local function round ( num )
return math.floor ( num + 0.5 )
end
2021-05-03 13:43:46 +02:00
function mcl_clock . get_clock_frame ( )
2017-08-17 21:26:09 +02:00
local t = clock_frames * minetest.get_timeofday ( )
2017-02-19 21:11:49 +01:00
t = round ( t )
2017-08-17 21:26:09 +02:00
if t == clock_frames then t = 0 end
2017-02-19 21:11:49 +01:00
return tostring ( t )
2017-01-09 03:08:24 +01:00
end
2017-03-20 18:12:05 +01:00
local doc_mod = minetest.get_modpath ( " doc " ) ~= nil
2017-01-09 03:08:24 +01:00
-- Register items
2021-05-03 13:43:46 +02:00
function mcl_clock . register_item ( name , image , creative , frame )
2017-01-09 03:08:24 +01:00
local g = 1
if creative then
g = 0
end
2017-03-20 18:12:05 +01:00
local use_doc = name == mcl_clock.stereotype
if doc_mod and not use_doc then
doc.add_entry_alias ( " craftitems " , mcl_clock.stereotype , " craftitems " , name )
end
2020-02-19 04:54:17 +01:00
local longdesc , usagehelp , tt
2017-03-20 18:12:05 +01:00
if use_doc then
2019-03-07 23:40:43 +01:00
longdesc = S ( " Clocks are tools which shows the current time of day in the Overworld. " )
usagehelp = S ( " The clock contains a rotating disc with a sun symbol (yellow disc) and moon symbol and a little “pointer” which shows the current time of day by estimating the real position of the sun and the moon in the sky. Noon is represented by the sun symbol and midnight is represented by the moon symbol. " )
2020-03-12 01:35:11 +01:00
tt = S ( " Displays the time of day in the Overworld " )
2017-03-02 21:55:25 +01:00
end
2017-02-08 17:02:45 +01:00
minetest.register_craftitem ( name , {
2019-03-07 23:40:43 +01:00
description = S ( " Clock " ) ,
2020-02-19 04:54:17 +01:00
_tt_help = tt ,
2017-03-20 18:12:05 +01:00
_doc_items_create_entry = use_doc ,
2017-03-02 21:55:25 +01:00
_doc_items_longdesc = longdesc ,
_doc_items_usagehelp = usagehelp ,
2017-01-09 03:08:24 +01:00
inventory_image = image ,
2018-12-07 21:23:39 +01:00
groups = { not_in_creative_inventory = g , tool = 1 , clock = frame , disable_repair = 1 } ,
2017-01-09 03:08:24 +01:00
wield_image = " " ,
2017-02-08 17:02:45 +01:00
stack_max = 64 ,
2017-01-09 03:08:24 +01:00
} )
end
2017-01-09 03:18:28 +01:00
-- This timer makes sure the clocks get updated from time to time regardless of time_speed,
-- just in case some clocks in the world go wrong
local force_clock_update_timer = 0
2017-01-09 03:08:24 +01:00
minetest.register_globalstep ( function ( dtime )
2021-05-03 13:43:46 +02:00
local now = mcl_clock.get_clock_frame ( )
2017-01-09 03:18:28 +01:00
force_clock_update_timer = force_clock_update_timer + dtime
2017-08-17 21:26:09 +02:00
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
2017-01-09 03:08:24 +01:00
2021-05-03 13:43:46 +02:00
if mcl_clock.old_time == now and force_clock_update_timer < 60 then
2017-01-09 03:08:24 +01:00
return
end
2017-01-09 03:18:28 +01:00
force_clock_update_timer = 0
2017-01-09 03:08:24 +01:00
2021-05-03 13:43:46 +02:00
mcl_clock.old_time = now
2021-05-04 08:49:21 +02:00
mcl_clock.random_frame = random_frame
2017-01-09 03:08:24 +01:00
2021-03-16 17:35:46 +01:00
for p , player in pairs ( minetest.get_connected_players ( ) ) do
for s , stack in pairs ( player : get_inventory ( ) : get_list ( " main " ) ) do
2017-08-17 21:26:09 +02:00
local frame
2017-08-22 18:18:53 +02:00
-- Clocks do not work in certain zones
2019-02-01 06:33:07 +01:00
if not mcl_worlds.clock_works ( player : get_pos ( ) ) then
2017-08-17 21:26:09 +02:00
frame = random_frame
else
frame = now
end
2021-05-04 08:49:21 +02:00
2017-08-17 21:26:09 +02:00
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 )
2017-01-09 03:08:24 +01:00
end
end
end
end )
2017-01-09 03:18:28 +01:00
-- Immediately set correct clock time after crafting
minetest.register_on_craft ( function ( itemstack )
2017-03-20 18:12:05 +01:00
if itemstack : get_name ( ) == mcl_clock.stereotype then
2021-05-03 13:43:46 +02:00
itemstack : set_name ( " mcl_clock:clock_ " .. mcl_clock.get_clock_frame ( ) )
2017-01-09 03:18:28 +01:00
end
end )
2017-01-09 03:08:24 +01:00
-- Clock recipe
minetest.register_craft ( {
2017-03-20 18:12:05 +01:00
output = mcl_clock.stereotype ,
2017-01-09 03:08:24 +01:00
recipe = {
2017-01-31 23:32:56 +01:00
{ ' ' , ' mcl_core:gold_ingot ' , ' ' } ,
{ ' mcl_core:gold_ingot ' , ' mesecons:redstone ' , ' mcl_core:gold_ingot ' } ,
{ ' ' , ' mcl_core:gold_ingot ' , ' ' }
2017-01-09 03:08:24 +01:00
}
} )
-- Clock tool
2021-05-03 13:43:46 +02:00
mcl_clock.register_item ( mcl_clock.stereotype , mcl_clock.images [ 1 ] , true , 1 )
2017-01-09 03:08:24 +01:00
-- Faces
2017-08-17 21:26:09 +02:00
for a = 0 , clock_frames - 1 , 1 do
2017-01-09 03:08:24 +01:00
local b = a
if b > 31 then
b = b - 32
else
b = b + 32
end
2021-05-03 13:43:46 +02:00
mcl_clock.register_item ( " mcl_clock:clock_ " .. tostring ( a ) , mcl_clock.images [ b + 1 ] , false , a + 1 )
2017-01-09 03:08:24 +01:00
end
2017-02-20 21:16:51 +01:00