Apply disable_repair group
[MineClone/MineClone2.git] / mods / ITEMS / mcl_clock / init.lua
blobc5a87e5b6a550c82c356267abd82ed1af430f590
1 --[[
2 mcl_clock, renew of the renew of the watch mod
4 Original from Echo, here: http://forum.minetest.net/viewtopic.php?id=3795
5 ]]--
7 mcl_clock = {}
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"
12 local watch = {}
13 watch.old_time = -1
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
23 watch.images = {}
24 for frame=0, clock_frames-1 do
25 local sframe = tostring(frame)
26 if string.len(sframe) == 1 then
27 sframe = "0" .. sframe
28 end
29 table.insert(watch.images, "mcl_clock_clock_"..sframe..".png")
30 end
32 local function round(num)
33 return math.floor(num + 0.5)
34 end
36 function watch.get_clock_frame()
37 local t = clock_frames * minetest.get_timeofday()
38 t = round(t)
39 if t == clock_frames then t = 0 end
40 return tostring(t)
41 end
43 local doc_mod = minetest.get_modpath("doc") ~= nil
45 -- Register items
46 function watch.register_item(name, image, creative, frame)
47 local g = 1
48 if creative then
49 g = 0
50 end
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)
54 end
55 local longdesc, usagehelp
56 if use_doc then
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."
59 end
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},
67 wield_image = "",
68 stack_max = 64,
70 end
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
83 random_timer = 0
84 end
86 if watch.old_time == now and force_clock_update_timer < 60 then
87 return
88 end
89 force_clock_update_timer = 0
91 watch.old_time = now
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())
97 local frame
98 -- Clocks do not work in certain zones
99 if not mcl_worlds.clock_works(player:getpos()) then
100 frame = random_frame
101 else
102 frame = now
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)
112 end)
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())
119 end)
121 -- Clock recipe
122 minetest.register_craft({
123 output = mcl_clock.stereotype,
124 recipe = {
125 {'', 'mcl_core:gold_ingot', ''},
126 {'mcl_core:gold_ingot', 'mesecons:redstone', 'mcl_core:gold_ingot'},
127 {'', 'mcl_core:gold_ingot', ''}
131 -- Clock tool
132 watch.register_item(mcl_clock.stereotype, watch.images[1], true, 1)
134 -- Faces
135 for a=0,clock_frames-1,1 do
136 local b = a
137 if b > 31 then
138 b = b - 32
139 else
140 b = b + 32
142 watch.register_item("mcl_clock:clock_"..tostring(a), watch.images[b+1], false, a+1)