1
0
Fork 0
VoxeLibre/mods/ITEMS/mcl_clock/init.lua

141 lines
4.2 KiB
Lua

--[[
mcl_clock, renew of the renew of the watch mod
Original from Echo, here: http://forum.minetest.net/viewtopic.php?id=3795
]]--
mcl_clock = {}
-- 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"
local watch = {}
watch.old_time = -1
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, clock_frames-1 do
table.insert(watch.images, "mcl_clock_clock.png^[verticalframe:"..clock_frames..":"..frame)
end
local function round(num)
return math.floor(num + 0.5)
end
function watch.get_clock_frame()
local t = clock_frames * minetest.get_timeofday()
t = round(t)
if t == clock_frames then t = 0 end
return tostring(t)
end
local doc_mod = minetest.get_modpath("doc") ~= nil
-- Register items
function watch.register_item(name, image, creative, frame)
local g = 1
if creative then
g = 0
end
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
local longdesc, usagehelp
if use_doc then
longdesc = "Clocks are tools which shows the current time of day in the Overworld."
usagehelp = "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."
end
minetest.register_craftitem(name, {
description = "Clock",
_doc_items_create_entry = use_doc,
_doc_items_longdesc = longdesc,
_doc_items_usagehelp = usagehelp,
inventory_image = image,
groups = {not_in_creative_inventory=g, tool=1, clock=frame},
wield_image = "",
stack_max = 64,
})
end
-- 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
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
end
force_clock_update_timer = 0
watch.old_time = now
local players = minetest.get_connected_players()
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 certain zones
if not mcl_util.clock_works(player:getpos()) 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
end)
-- Immediately set correct clock time after crafting
minetest.register_on_craft(function(itemstack)
if itemstack:get_name() == mcl_clock.stereotype then
itemstack:set_name("mcl_clock:clock_"..watch.get_clock_frame())
end
end)
-- Clock recipe
minetest.register_craft({
output = mcl_clock.stereotype,
recipe = {
{'', 'mcl_core:gold_ingot', ''},
{'mcl_core:gold_ingot', 'mesecons:redstone', 'mcl_core:gold_ingot'},
{'', 'mcl_core:gold_ingot', ''}
}
})
-- Clock tool
watch.register_item(mcl_clock.stereotype, watch.images[1], true, 1)
-- Faces
for a=0,clock_frames-1,1 do
local b = a
if b > 31 then
b = b - 32
else
b = b + 32
end
watch.register_item("mcl_clock:clock_"..tostring(a), watch.images[b+1], false, a+1)
end