-
Notifications
You must be signed in to change notification settings - Fork 41
/
core_itemrepair.lua
43 lines (37 loc) · 1.17 KB
/
core_itemrepair.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- how much "extra" points are healed per a repair operation (fraction of full health)
local BONUS = 0.05
function OnCraftingNoRecipe(Player, Grid, Recipe)
local Items = {}
for x = 0, Grid:GetWidth() - 1 do
for y = 0, Grid:GetHeight() - 1 do
local Item = Grid:GetItem(x, y)
if (Item.m_ItemType ~= E_ITEM_EMPTY) then
Item.x = x
Item.y = y
table.insert(Items, Item)
end
end
end
if (#Items ~= 2) then
-- Only two items together can be fixed
return false
end
if (not Items[1]:IsSameType(Items[2])) then
-- Only items of the same type can be fixed
return false
end
local MaxDamage = Items[1]:GetMaxDamage()
if (MaxDamage == 0) then
-- Not repairable
return false
end
local BonusHealth = MaxDamage * BONUS
local FirstItemHealth = MaxDamage - Items[1].m_ItemDamage
local SecondItemHealth = MaxDamage - Items[2].m_ItemDamage
local NewDamage = MaxDamage - (FirstItemHealth + SecondItemHealth + BonusHealth)
NewDamage = math.max(0, NewDamage) -- Not lower than zero
Recipe:SetResult(Items[1].m_ItemType, 1, NewDamage)
Recipe:SetIngredient(Items[1].x, Items[1].y, Items[1])
Recipe:SetIngredient(Items[2].x, Items[2].y, Items[2])
return true
end