]> git.lizzy.rs Git - Crafter.git/blob - mods/armor/init.lua
Fix typo
[Crafter.git] / mods / armor / init.lua
1 local
2 minetest,math,pairs
3 =
4 minetest,math,pairs
5
6 local get_item_group = minetest.get_item_group
7 local get_itemdef    = minetest.get_itemdef
8
9
10 local ceil  = math.ceil
11 local random = math.random
12
13 local inv
14 local player_skin
15 local armor_skin
16 local stack
17 local skin_element
18 function recalculate_armor(player)
19     
20     if not player or (player and not player:is_player()) then return end
21
22     inv = player:get_inventory()
23
24     player_skin = get_skin(player)
25     armor_skin = "blank_skin.png"
26
27     stack = inv:get_stack("armor_head",1):get_name()
28     if stack ~= "" and get_item_group(stack,"helmet") > 0 then
29         skin_element = get_itemdef(stack, "wearing_texture")
30         player_skin = player_skin.."^"..skin_element
31     end
32
33     stack = inv:get_stack("armor_torso",1):get_name()
34     if stack ~= "" and get_item_group(stack,"chestplate") > 0 then
35         skin_element = get_itemdef(stack, "wearing_texture")
36         armor_skin = armor_skin.."^"..skin_element
37     end
38
39     stack = inv:get_stack("armor_legs",1):get_name()
40     if stack ~= "" and get_item_group(stack,"leggings") > 0 then
41         skin_element = get_itemdef(stack, "wearing_texture")
42         armor_skin = armor_skin.."^"..skin_element
43     end
44
45     stack = inv:get_stack("armor_feet",1):get_name()
46     if stack ~= "" and get_item_group(stack,"boots") > 0 then
47         skin_element = get_itemdef(stack, "wearing_texture")
48         armor_skin = armor_skin.."^"..skin_element
49     end
50     player:set_properties({textures = {player_skin,armor_skin}})
51 end
52
53 local inv
54 local armor_absorbtion
55 local level
56 local defense
57 local stack
58 function calculate_armor_absorbtion(player)
59     if not player or (player and not player:is_player()) then return end
60
61     inv = player:get_inventory()
62     armor_absorbtion = 0
63
64     stack = inv:get_stack("armor_head",1):get_name()
65     if stack ~= "" then
66         level = get_item_group(stack,"armor_level")
67         defense = get_item_group(stack,"armor_defense")
68         armor_absorbtion = armor_absorbtion + (level*defense)
69     end
70
71     stack = inv:get_stack("armor_torso",1):get_name()
72     if stack ~= "" then
73         level = get_item_group(stack,"armor_level")
74         defense = get_item_group(stack,"armor_defense")
75         armor_absorbtion = armor_absorbtion + (level*defense)
76     end
77
78     stack = inv:get_stack("armor_legs",1):get_name()
79     if stack ~= "" then
80         level = get_item_group(stack,"armor_level")
81         defense = get_item_group(stack,"armor_defense")
82         armor_absorbtion = armor_absorbtion + (level*defense)
83     end
84
85     stack = inv:get_stack("armor_feet",1):get_name()
86     if stack ~= "" then
87         level = get_item_group(stack,"armor_level")
88         defense = get_item_group(stack,"armor_defense")
89         armor_absorbtion = armor_absorbtion + (level*defense)
90     end
91     if armor_absorbtion > 0 then
92         armor_absorbtion = ceil(armor_absorbtion/4)
93     end
94     return(armor_absorbtion)
95 end
96
97 local level
98 function set_armor_gui(player)
99     if not player or (player and not player:is_player()) then return end
100     level = calculate_armor_absorbtion(player)
101     hud_manager.change_hud({
102                 player    =  player ,
103                 hud_name  = "armor_fg",
104                 element   = "number",
105                 data      =  level
106         })
107 end
108
109
110 local inv
111 local recalc
112 local stack
113 local name
114 local wear_level
115 local new_stack
116
117 function damage_armor(player,damage)
118     if not player or (player and not player:is_player()) then return end
119
120     inv = player:get_inventory()
121     
122     recalc = false
123
124     stack = inv:get_stack("armor_head",1)
125     name = stack:get_name()
126     if name ~= "" then
127         wear_level = ((9-get_item_group(name,"armor_level"))*8)*(5-get_item_group(name,"armor_type"))*damage
128         stack:add_wear(wear_level)
129         inv:set_stack("armor_head", 1, stack)
130         new_stack = inv:get_stack("armor_head",1):get_name()
131         if new_stack == "" then
132             recalc = true
133         end
134     end
135
136     stack = inv:get_stack("armor_torso",1)
137     name = stack:get_name()
138     if name ~= "" then
139         wear_level = ((9-get_item_group(name,"armor_level"))*4)*(5-get_item_group(name,"armor_type"))*damage
140         stack:add_wear(wear_level)
141         inv:set_stack("armor_torso", 1, stack)
142         new_stack = inv:get_stack("armor_torso",1):get_name()
143         if new_stack == "" then
144             recalc = true
145         end
146     end
147
148     stack = inv:get_stack("armor_legs",1)
149     name = stack:get_name()
150     if name ~= "" then
151         wear_level = ((9-get_item_group(name,"armor_level"))*6)*(5-get_item_group(name,"armor_type"))*damage
152         stack:add_wear(wear_level)
153         inv:set_stack("armor_legs", 1, stack)
154         new_stack = inv:get_stack("armor_legs",1):get_name()
155         if new_stack == "" then
156             recalc = true
157         end
158     end
159
160     stack = inv:get_stack("armor_feet",1)
161     name = stack:get_name()
162     if name ~= "" then
163         wear_level = ((9-get_item_group(name,"armor_level"))*10)*(5-get_item_group(name,"armor_type"))*damage
164         stack:add_wear(wear_level)
165         inv:set_stack("armor_feet", 1, stack)
166         new_stack = inv:get_stack("armor_feet",1):get_name()
167         if new_stack == "" then
168             recalc = true
169         end
170     end
171
172     if recalc == true then
173         minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=random(80,100)/100})
174         recalculate_armor(player)
175         set_armor_gui(player)
176         --do particles too
177     end
178 end
179
180 local inv
181 minetest.register_on_joinplayer(function(player)
182         hud_manager.add_hud(player,"armor_bg",{
183                 hud_elem_type = "statbar",
184                 position = {x = 0.5, y = 1},
185                 text = "armor_icon_bg.png",
186                 number = 20,
187                 size = {x = 24, y = 24},
188                 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
189         })
190         hud_manager.add_hud(player,"armor_fg",{
191                 hud_elem_type = "statbar",
192                 position = {x = 0.5, y = 1},
193                 text = "armor_icon.png",
194                 number = calculate_armor_absorbtion(player),
195                 size = {x = 24, y = 24},
196                 offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
197         })
198     
199     inv = player:get_inventory()
200     inv:set_size("armor_head" ,1)
201     inv:set_size("armor_torso",1)
202     inv:set_size("armor_legs" ,1)
203     inv:set_size("armor_feet" ,1)
204 end)
205
206 minetest.register_on_dieplayer(function(player)
207     set_armor_gui(player)
208 end)
209
210 local acceptable = {
211     ["armor_head"]  = true,
212     ["armor_torso"] = true,
213     ["armor_legs"]  = true,
214     ["armor_feet"]  = true,
215 }
216 minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
217     if acceptable[inventory_info.from_list] or acceptable[inventory_info.to_list] then
218         minetest.after(0,function()
219             recalculate_armor(player)
220             set_armor_gui(player)
221         end)
222     end
223 end)
224
225 --only allow players to put armor in the right slots to stop exploiting chestplates
226 local stack
227 local item
228 minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
229     if inventory_info.to_list == "armor_head" then
230         stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
231         item = stack:get_name()
232         if get_item_group(item, "helmet") == 0 then
233             return(0)
234         end
235     elseif inventory_info.to_list == "armor_torso" then
236         stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
237         item = stack:get_name()
238         if get_item_group(item, "chestplate") == 0 then
239             return(0)
240         end
241     elseif inventory_info.to_list == "armor_legs" then
242         stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
243         item = stack:get_name()
244         if get_item_group(item, "leggings") == 0 then
245             return(0)
246         end
247     elseif inventory_info.to_list == "armor_feet" then
248         stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
249         item = stack:get_name()
250         if get_item_group(item, "boots") == 0 then
251             return(0)
252         end
253     end
254 end)
255
256 local materials = {["coal"]=1,["lapis"]=2,["iron"]=3,["chain"]=4,["gold"]=2,["diamond"]=5,["emerald"]=6,["sapphire"]=7,["ruby"]=8} --max 8
257 local armor_type = {["helmet"]=2,["chestplate"]=4,["leggings"]=3,["boots"]=1} --max 4
258
259 local function bool_int(state)
260     if state == true then return(1) end
261     if state == false or not state then return(0) end
262 end
263
264 for material_id,material in pairs(materials) do
265     for armor_id,armor in pairs(armor_type) do
266         --print(material_id,material,"|",armor_id,armor)
267         minetest.register_tool("armor:"..material_id.."_"..armor_id,{
268             description = material_id:gsub("^%l", string.upper).." "..armor_id:gsub("^%l", string.upper),
269     
270             groups = {
271                 armor         = 1,
272                 armor_level   = material,
273                 armor_defense = armor,
274                 helmet        = bool_int(armor_id == "helmet"),
275                 chestplate    = bool_int(armor_id == "chestplate"),
276                 leggings      = bool_int(armor_id == "leggings"),
277                 boots         = bool_int(armor_id == "boots"),
278             },
279             inventory_image = material_id.."_"..armor_id.."_item.png",
280             stack_max = 1,
281             wearing_texture = material_id.."_"..armor_id..".png",
282             tool_capabilities = {
283                 full_punch_interval = 0,
284                 max_drop_level = 0,
285                 groupcaps = {
286                 },
287                 damage_groups = {
288
289                 },
290                 punch_attack_uses = 0,
291             }
292         })
293
294         if armor_id == "helmet" then
295             minetest.register_craft({
296                 output = "armor:"..material_id.."_"..armor_id,
297                 recipe = {
298                     {"main:"..material_id, "main:"..material_id, "main:"..material_id},
299                     {"main:"..material_id, ""                  , "main:"..material_id},
300                     {""                  , ""                  , ""                  }
301                 }
302             })
303         elseif armor_id == "chestplate" then
304             minetest.register_craft({
305                 output = "armor:"..material_id.."_"..armor_id,
306                 recipe = {
307                     {"main:"..material_id, ""                  , "main:"..material_id},
308                     {"main:"..material_id, "main:"..material_id, "main:"..material_id},
309                     {"main:"..material_id, "main:"..material_id, "main:"..material_id}
310                 }
311             })
312         elseif armor_id == "leggings" then
313             minetest.register_craft({
314                 output = "armor:"..material_id.."_"..armor_id,
315                 recipe = {
316                     {"main:"..material_id, "main:"..material_id, "main:"..material_id},
317                     {"main:"..material_id, ""                  , "main:"..material_id},
318                     {"main:"..material_id, ""                  , "main:"..material_id}
319                 }
320             })
321         elseif armor_id == "boots" then
322             minetest.register_craft({
323                 output = "armor:"..material_id.."_"..armor_id,
324                 recipe = {
325                     {""                  , "", ""                  },
326                     {"main:"..material_id, "", "main:"..material_id},
327                     {"main:"..material_id, "", "main:"..material_id}
328                 }
329             })
330             minetest.register_node("armor:"..material_id.."_"..armor_id.."particletexture", {
331                 description = "NIL",
332                 tiles = {material_id.."_"..armor_id.."_item.png"},
333                 groups = {},
334                 drop = "",
335                 drawtype = "allfaces",
336                 on_construct = function(pos)
337                     minetest.remove_node(pos)
338                 end,
339             })
340         end
341         
342     end
343 end