2 mcl_clock, renew of the renew of the watch mod
4 Original from Echo, here: http://forum.minetest.net/viewtopic.php?id=3795
9 -- This is the itemstring of the default clock item. It is used for the default inventory image, help entries, and the like
10 mcl_clock
.stereotype
= "mcl_clock:clock"
15 local clock_frames
= 64
17 -- Timer for random clock spinning
18 local random_timer
= 0.0
19 local random_timer_trigger
= 1.0 -- random clock spinning tick in seconds. Increase if there are performance problems
20 local random_frame
= math
.random(0, clock_frames
-1)
22 -- Image of all possible faces
24 for frame
=0, clock_frames
-1 do
25 local sframe
= tostring(frame
)
26 if string.len(sframe
) == 1 then
27 sframe
= "0" .. sframe
29 table.insert(watch
.images
, "mcl_clock_clock_"..sframe
..".png")
32 local function round(num
)
33 return math
.floor(num
+ 0.5)
36 function watch
.get_clock_frame()
37 local t
= clock_frames
* minetest
.get_timeofday()
39 if t
== clock_frames
then t
= 0 end
43 local doc_mod
= minetest
.get_modpath("doc") ~= nil
46 function watch
.register_item(name
, image
, creative
, frame
)
51 local use_doc
= name
== mcl_clock
.stereotype
52 if doc_mod
and not use_doc
then
53 doc
.add_entry_alias("craftitems", mcl_clock
.stereotype
, "craftitems", name
)
55 local longdesc
, usagehelp
57 longdesc
= "Clocks are tools which shows the current time of day in the Overworld."
58 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."
60 minetest
.register_craftitem(name
, {
61 description
= "Clock",
62 _doc_items_create_entry
= use_doc
,
63 _doc_items_longdesc
= longdesc
,
64 _doc_items_usagehelp
= usagehelp
,
65 inventory_image
= image
,
66 groups
= {not_in_creative_inventory
=g
, tool
=1, clock=frame
, disable_repair
=1},
72 -- This timer makes sure the clocks get updated from time to time regardless of time_speed,
73 -- just in case some clocks in the world go wrong
74 local force_clock_update_timer
= 0
76 minetest
.register_globalstep(function(dtime
)
77 local now
= watch
.get_clock_frame()
78 force_clock_update_timer
= force_clock_update_timer
+ dtime
79 random_timer
= random_timer
+ dtime
80 -- This causes the random spinning of the clock
81 if random_timer
>= random_timer_trigger
then
82 random_frame
= (random_frame
+ math
.random(-4, 4)) % clock_frames
86 if watch
.old_time
== now
and force_clock_update_timer
< 60 then
89 force_clock_update_timer
= 0
93 local players
= minetest
.get_connected_players()
94 for p
, player
in ipairs(players
) do
95 for s
, stack
in ipairs(player
:get_inventory():get_list("main")) do
96 local dim
= mcl_worlds
.pos_to_dimension(player
:get_pos())
98 -- Clocks do not work in certain zones
99 if not mcl_worlds
.clock_works(player
:get_pos()) then
104 local count
= stack
:get_count()
105 if stack
:get_name() == mcl_clock
.stereotype
then
106 player
:get_inventory():set_stack("main", s
, "mcl_clock:clock_"..frame
.." "..count
)
107 elseif minetest
.get_item_group(stack
:get_name(), "clock") ~= 0 then
108 player
:get_inventory():set_stack("main", s
, "mcl_clock:clock_"..frame
.." "..count
)
114 -- Immediately set correct clock time after crafting
115 minetest
.register_on_craft(function(itemstack
)
116 if itemstack
:get_name() == mcl_clock
.stereotype
then
117 itemstack
:set_name("mcl_clock:clock_"..watch
.get_clock_frame())
122 minetest
.register_craft({
123 output
= mcl_clock
.stereotype
,
125 {'', 'mcl_core:gold_ingot', ''},
126 {'mcl_core:gold_ingot', 'mesecons:redstone', 'mcl_core:gold_ingot'},
127 {'', 'mcl_core:gold_ingot', ''}
132 watch
.register_item(mcl_clock
.stereotype
, watch
.images
[1], true, 1)
135 for a
=0,clock_frames
-1,1 do
142 watch
.register_item("mcl_clock:clock_"..tostring(a
), watch
.images
[b
+1], false, a
+1)