Remove dead code
[minetest_tt.git] / init.lua
blob34368c2e4bfcd3111b367151c2c69af9fde31379
1 local S = minetest.get_translator("tt")
2 local COLOR_DEFAULT = "#d0ffd0"
3 local COLOR_DANGER = "#ffff00"
4 local COLOR_GOOD = "#00ff00"
6 tt = {}
7 tt.registered_snippets = {}
9 local function get_min_digtime(caps)
10 local mintime
11 local unique = true
12 if caps.maxlevel and caps.maxlevel > 1 then
13 unique = false
14 end
15 if caps.times then
16 for r=1,3 do
17 local time = caps.times[r]
18 if caps.maxlevel and caps.maxlevel > 1 then
19 time = time / caps.maxlevel
20 end
21 if (not mintime) or (time and time < mintime) then
22 if time and mintime and (time < mintime) then
23 unique = false
24 end
25 mintime = time
26 end
27 end
28 end
29 return mintime, unique
30 end
32 tt.register_snippet = function(func)
33 table.insert(tt.registered_snippets, func)
34 end
36 --- CORE SNIPPETS ---
38 tt.register_snippet(function(itemstring)
39 local def = minetest.registered_items[itemstring]
40 -- Custom text
41 if def._tt_help then
42 return def._tt_help, false
43 end
44 end)
46 tt.register_snippet(function(itemstring)
47 local def = minetest.registered_items[itemstring]
48 local desc = ""
49 -- Tool info
50 if def.tool_capabilities then
51 -- Digging times
52 local digs = ""
53 local d
54 if def.tool_capabilities.groupcaps then
55 for group, caps in pairs(def.tool_capabilities.groupcaps) do
56 local mintime, unique_mintime
57 if caps.times then
58 mintime, unique_mintime = get_min_digtime(caps)
59 if mintime and (mintime > 0 and (not unique_mintime)) then
60 d = S("Digs @1 blocks", group) .. "\n"
61 d = d .. S("Minimum dig time: @1s", string.format("%.2f", mintime))
62 digs = digs .. "\n" .. d
63 elseif mintime and mintime == 0 then
64 d = S("Digs @1 blocks instantly", group)
65 digs = digs .. "\n" .. d
66 end
67 end
68 end
69 if digs ~= "" then
70 desc = desc .. minetest.colorize(COLOR_DEFAULT, digs)
71 end
72 end
73 -- Weapon stats
74 if def.tool_capabilities.damage_groups then
75 for group, damage in pairs(def.tool_capabilities.damage_groups) do
76 local msg
77 if group == "fleshy" then
78 if damage >= 0 then
79 msg = S("Damage: @1", damage)
80 else
81 msg = S("Healing: @1", math.abs(damage))
82 end
83 else
84 if damage >= 0 then
85 msg = S("Damage (@1): @2", group, damage)
86 else
87 msg = S("Healing (@1): @2", group, math.abs(damage))
88 end
89 end
90 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, msg)
91 end
92 local full_punch_interval = def.tool_capabilities.full_punch_interval
93 if not full_punch_interval then
94 full_punch_interval = 1
95 end
96 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Full punch interval: @1s", full_punch_interval))
97 end
98 end
99 if desc == "" then
100 desc = nil
102 return desc, false
103 end)
105 tt.register_snippet(function(itemstring)
106 local def = minetest.registered_items[itemstring]
107 local desc
108 -- Food
109 if def._tt_food then
110 desc = S("Food item")
111 if def._tt_food_hp then
112 local msg = S("+@1 food points", def._tt_food_hp)
113 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, msg)
116 return desc, false
117 end)
119 tt.register_snippet(function(itemstring)
120 local def = minetest.registered_items[itemstring]
121 local desc = ""
122 -- Node info
123 if def.damage_per_second then
124 if def.damage_per_second > 0 then
125 desc = desc .. "\n" .. minetest.colorize(COLOR_DANGER, S("Contact damage: @1 per second", def.damage_per_second))
126 elseif def.damage_per_second < 0 then
127 desc = desc .. "\n" .. minetest.colorize(COLOR_GOOD, S("Contact healing: @1 per second", math.abs(def.damage_per_second)))
130 if def.drowning and def.drowning ~= 0 then
131 desc = desc .. "\n" .. minetest.colorize(COLOR_DANGER, S("Drowning damage: @1", def.drowning))
133 local tmp = minetest.get_item_group(itemstring, "fall_damage_add_percent")
134 if tmp > 0 then
135 desc = desc .. "\n" .. minetest.colorize(COLOR_DANGER, S("Fall damage: +@1%", tmp))
136 elseif tmp == -100 then
137 desc = desc .. "\n" .. minetest.colorize(COLOR_GOOD, S("No fall damage"))
138 elseif tmp < 0 then
139 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Fall damage: @1%", tmp))
141 -- Movement-related node facts
142 if minetest.get_item_group(itemstring, "disable_jump") == 1 and not def.climbable then
143 if def.liquidtype == "none" then
144 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("No jumping"))
145 else
146 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("No swimming upwards"))
149 if def.climbable then
150 if minetest.get_item_group(itemstring, "disable_jump") == 1 then
151 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Climbable (only downwards)"))
152 else
153 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Climbable"))
156 if minetest.get_item_group(itemstring, "slippery") >= 1 then
157 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Slippery"))
159 local tmp = minetest.get_item_group(itemstring, "bouncy")
160 if tmp >= 1 then
161 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Bouncy (@1%)", tmp))
163 -- Node appearance
164 tmp = def.light_source
165 if tmp and tmp >= 1 then
166 desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Luminance: @1", tmp))
168 if desc == "" then
169 desc = nil
171 return desc, false
172 end)
175 -- Apply item description update
177 local function append_descs()
178 for itemstring, def in pairs(minetest.registered_items) do
179 if itemstring ~= "" and itemstring ~= "air" and itemstring ~= "ignore" and itemstring ~= "unknown" and def ~= nil and def.description ~= nil and def.description ~= "" and def._tt_ignore ~= true then
180 local desc = def.description
181 local orig_desc = desc
182 local first = true
183 -- Apply snippets
184 for s=1, #tt.registered_snippets do
185 local str, snippet_color = tt.registered_snippets[s](itemstring)
186 if snippet_color == nil then
187 snippet_color = COLOR_DEFAULT
188 elseif snippet_color == false then
189 snipped_color = false
191 if str then
192 if first then
193 first = false
194 else
195 desc = desc .. "\n"
197 if snippet_color == false then
198 desc = desc .. minetest.colorize(snippet_color, str)
199 else
200 desc = desc .. str
204 if desc ~= def.description then
205 minetest.override_item(itemstring, { description = desc, _tt_original_description = orig_desc })
211 minetest.register_on_mods_loaded(append_descs)