Version 0.41.3
[MineClone/MineClone2/MineClone2-Fixes.git] / mods / HUD / awards / triggers.lua
blob88a952fdfc422eeed5a2bf26fe4cd503bb29e2ec
1 -- AWARDS
2 --
3 -- Copyright (C) 2013-2015 rubenwardy
4 -- This program is free software; you can redistribute it and/or modify
5 -- it under the terms of the GNU Lesser General Public License as published by
6 -- the Free Software Foundation; either version 2.1 of the License, or
7 -- (at your option) any later version.
8 -- This program is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU Lesser General Public License for more details.
12 -- You should have received a copy of the GNU Lesser General Public License along
13 -- with this program; if not, write to the Free Software Foundation, Inc.,
14 -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 local S
18 if minetest.get_modpath("intllib") then
19 S = intllib.Getter()
20 else
21 S = function ( s ) return s end
22 end
24 awards.register_trigger("dig", function(def)
25 local tmp = {
26 award = def.name,
27 node = def.trigger.node,
28 target = def.trigger.target,
30 table.insert(awards.on.dig, tmp)
31 def.getProgress = function(self, data)
32 local itemcount
33 if tmp.node then
34 itemcount = awards.get_item_count(data, "count", tmp.node) or 0
35 else
36 itemcount = awards.get_total_item_count(data, "count")
37 end
38 return {
39 perc = itemcount / tmp.target,
40 label = string.format(S("%d/%d dug"), itemcount, tmp.target)
42 end
43 def.getDefaultDescription = function(self)
44 if self.trigger.node then
45 local nname = minetest.registered_nodes[self.trigger.node].description
46 if nname == nil then
47 nname = self.trigger.node
48 end
49 if self.trigger.target ~= 1 then
50 return string.format(S("Mine blocks: %d×%s"), self.trigger.target, nname)
51 else
52 return string.format(S("Mine a block: %s"), nname)
53 end
54 else
55 return string.format(S("Mine %d block(s)."), self.trigger.target)
56 end
57 end
58 end)
60 awards.register_trigger("place", function(def)
61 local tmp = {
62 award = def.name,
63 node = def.trigger.node,
64 target = def.trigger.target,
66 table.insert(awards.on.place, tmp)
67 def.getProgress = function(self, data)
68 local itemcount
69 if tmp.node then
70 itemcount = awards.get_item_count(data, "place", tmp.node) or 0
71 else
72 itemcount = awards.get_total_item_count(data, "place")
73 end
74 return {
75 perc = itemcount / tmp.target,
76 label = string.format(S("%d/%d placed"), itemcount, tmp.target)
78 end
79 def.getDefaultDescription = function(self)
80 if self.trigger.node then
81 local nname = minetest.registered_nodes[self.trigger.node].description
82 if nname == nil then
83 nname = self.trigger.node
84 end
85 if self.trigger.target ~= 1 then
86 return string.format(S("Place blocks: %d×%s"), self.trigger.target, nname)
87 else
88 return string.format(S("Place a block: %s"), nname)
89 end
90 else
91 return string.format(S("Place %d block(s)."), self.trigger.target)
92 end
93 end
94 end)
96 awards.register_trigger("eat", function(def)
97 local tmp = {
98 award = def.name,
99 item = def.trigger.item,
100 target = def.trigger.target,
102 table.insert(awards.on.eat, tmp)
103 def.getProgress = function(self, data)
104 local itemcount
105 if tmp.item then
106 itemcount = awards.get_item_count(data, "eat", tmp.item) or 0
107 else
108 itemcount = awards.get_total_item_count(data, "eat")
110 return {
111 perc = itemcount / tmp.target,
112 label = string.format(S("%d/%d eaten"), itemcount, tmp.target)
115 def.getDefaultDescription = function(self)
116 if self.trigger.item then
117 local iname = minetest.registered_items[self.trigger.item].description
118 if iname == nil then
119 iname = self.trigger.iode
121 if self.trigger.target ~= 1 then
122 return string.format(S("Eat: %d×%s"), self.trigger.target, iname)
123 else
124 return string.format(S("Eat: %s"), iname)
126 else
127 return string.format(S("Eat %d item(s)."), self.trigger.target)
130 end)
132 awards.register_trigger("death", function(def)
133 local tmp = {
134 award = def.name,
135 target = def.trigger.target,
137 table.insert(awards.on.death, tmp)
138 def.getProgress = function(self, data)
139 local itemcount = data.deaths or 0
140 return {
141 perc = itemcount / tmp.target,
142 label = string.format(S("%d/%d deaths"), itemcount, tmp.target)
145 def.getDefaultDescription = function(self)
146 if self.trigger.target ~= 1 then
147 return string.format(S("Die %d times."), self.trigger.target)
148 else
149 return S("Die.")
152 end)
154 awards.register_trigger("chat", function(def)
155 local tmp = {
156 award = def.name,
157 target = def.trigger.target,
159 table.insert(awards.on.chat, tmp)
160 def.getProgress = function(self, data)
161 local itemcount = data.chats or 0
162 return {
163 perc = itemcount / tmp.target,
164 label = string.format(S("%d/%d chat messages"), itemcount, tmp.target)
167 def.getDefaultDescription = function(self)
168 if self.trigger.target ~= 1 then
169 return string.format(S("Write %d chat messages."), self.trigger.target)
170 else
171 return S("Write something in chat.")
174 end)
176 awards.register_trigger("join", function(def)
177 local tmp = {
178 award = def.name,
179 target = def.trigger.target,
181 table.insert(awards.on.join, tmp)
182 def.getProgress = function(self, data)
183 local itemcount = data.joins or 0
184 return {
185 perc = itemcount / tmp.target,
186 label = string.format(S("%d/%d game joins"), itemcount, tmp.target)
189 def.getDefaultDescription = function(self)
190 if self.trigger.target ~= 1 then
191 return string.format(S("Join the game %d times."), self.trigger.target)
192 else
193 return S("Join the game.")
196 end)
198 awards.register_trigger("craft", function(def)
199 local tmp = {
200 award = def.name,
201 item = def.trigger.item,
202 target = def.trigger.target,
204 table.insert(awards.on.craft, tmp)
205 def.getProgress = function(self, data)
206 local itemcount
207 if tmp.item then
208 itemcount = awards.get_item_count(data, "craft", tmp.item) or 0
209 else
210 itemcount = awards.get_total_item_count(data, "craft")
212 return {
213 perc = itemcount / tmp.target,
214 label = string.format(S("%d/%d crafted"), itemcount, tmp.target)
217 def.getDefaultDescription = function(self)
218 if self.trigger.item then
219 local iname = minetest.registered_items[self.trigger.item].description
220 if iname == nil then
221 iname = self.trigger.item
223 if self.trigger.target ~= 1 then
224 return string.format(S("Craft: %d×%s"), self.trigger.target, iname)
225 else
226 return string.format(S("Craft: %s"), iname)
228 else
229 return string.format(S("Craft %d item(s)."), self.trigger.target)
232 end)
234 -- Backwards compatibility
235 awards.register_onDig = awards.register_on_dig
236 awards.register_onPlace = awards.register_on_place
237 awards.register_onDeath = awards.register_on_death
238 awards.register_onChat = awards.register_on_chat
239 awards.register_onJoin = awards.register_on_join
240 awards.register_onCraft = awards.register_on_craft
242 -- Trigger Handles
243 minetest.register_on_dignode(function(pos, oldnode, digger)
244 if not digger or not pos or not oldnode then
245 return
248 local data = awards.players[digger:get_player_name()]
249 if not awards.increment_item_counter(data, "count", oldnode.name) then
250 return
252 awards.run_trigger_callbacks(digger, data, "dig", function(entry)
253 if entry.target then
254 if entry.node then
255 local tnodedug = string.split(entry.node, ":")
256 local tmod = tnodedug[1]
257 local titem = tnodedug[2]
258 if not tmod or not titem or not data.count[tmod] or not data.count[tmod][titem] then
259 -- table running failed!
260 elseif data.count[tmod][titem] > entry.target-1 then
261 return entry.award
263 elseif awards.get_total_item_count(data, "count") > entry.target-1 then
264 return entry.award
267 end)
268 end)
270 minetest.register_on_placenode(function(pos, node, digger)
271 if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then
272 return
274 local data = awards.players[digger:get_player_name()]
275 if not awards.increment_item_counter(data, "place", node.name) then
276 return
279 awards.run_trigger_callbacks(digger, data, "place", function(entry)
280 if entry.target then
281 if entry.node then
282 local tnodedug = string.split(entry.node, ":")
283 local tmod = tnodedug[1]
284 local titem = tnodedug[2]
285 if not tmod or not titem or not data.place[tmod] or not data.place[tmod][titem] then
286 -- table running failed!
287 elseif data.place[tmod][titem] > entry.target-1 then
288 return entry.award
290 elseif awards.get_total_item_count(data, "place") > entry.target-1 then
291 return entry.award
294 end)
295 end)
297 minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user, pointed_thing)
298 if not user or not itemstack or not user:get_player_name() or user:get_player_name()=="" then
299 return
301 local data = awards.players[user:get_player_name()]
302 if not awards.increment_item_counter(data, "eat", itemstack:get_name()) then
303 return
305 awards.run_trigger_callbacks(user, data, "eat", function(entry)
306 if entry.target then
307 if entry.item then
308 local titemstring = string.split(entry.item, ":")
309 local tmod = titemstring[1]
310 local titem = titemstring[2]
311 if not tmod or not titem or not data.eat[tmod] or not data.eat[tmod][titem] then
312 -- table running failed!
313 elseif data.eat[tmod][titem] > entry.target-1 then
314 return entry.award
316 elseif awards.get_total_item_count(data, "eat") > entry.target-1 then
317 return entry.award
320 end)
321 end)
323 minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
324 if not player or not itemstack then
325 return
328 local data = awards.players[player:get_player_name()]
329 if not awards.increment_item_counter(data, "craft", itemstack:get_name(), itemstack:get_count()) then
330 return
333 awards.run_trigger_callbacks(player, data, "craft", function(entry)
334 if entry.target then
335 if entry.item then
336 local titemcrafted = string.split(entry.item, ":")
337 local tmod = titemcrafted[1]
338 local titem = titemcrafted[2]
339 if not tmod or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
340 -- table running failed!
341 elseif data.craft[tmod][titem] > entry.target-1 then
342 return entry.award
344 elseif awards.get_total_item_count(data, "craft") > entry.target-1 then
345 return entry.award
348 end)
349 end)
351 minetest.register_on_dieplayer(function(player)
352 -- Run checks
353 local name = player:get_player_name()
354 if not player or not name or name=="" then
355 return
358 -- Get player
359 awards.assertPlayer(name)
360 local data = awards.players[name]
362 -- Increment counter
363 data.deaths = data.deaths + 1
365 awards.run_trigger_callbacks(player, data, "death", function(entry)
366 if entry.target and entry.award and data.deaths and
367 data.deaths >= entry.target then
368 return entry.award
370 end)
371 end)
373 minetest.register_on_joinplayer(function(player)
374 -- Run checks
375 local name = player:get_player_name()
376 if not player or not name or name=="" then
377 return
380 -- Get player
381 awards.assertPlayer(name)
382 local data = awards.players[name]
384 -- Increment counter
385 data.joins = data.joins + 1
387 awards.run_trigger_callbacks(player, data, "join", function(entry)
388 if entry.target and entry.award and data.joins and
389 data.joins >= entry.target then
390 return entry.award
392 end)
393 end)
395 minetest.register_on_chat_message(function(name, message)
396 -- Run checks
397 local idx = string.find(message,"/")
398 if not name or (idx ~= nil and idx <= 1) then
399 return
402 -- Get player
403 awards.assertPlayer(name)
404 local data = awards.players[name]
405 local player = minetest.get_player_by_name(name)
407 -- Increment counter
408 data.chats = data.chats + 1
410 awards.run_trigger_callbacks(player, data, "chat", function(entry)
411 if entry.target and entry.award and data.chats and
412 data.chats >= entry.target then
413 return entry.award
415 end)
416 end)