;'Blacksmith Repairer' NPC script

;GoTo END

RepairCostScale = 1.75
;RepairCostScale = 2.50
InvItemID = 0
ItemCost = 0
ItemPercentage = 0
ItemDur = 0
ItemMaxDur = 0
TotalCost = 0

MSG=Lower(GetUserMessage(#USERID))

If GetUserFlag(#USERID,45) > 0
	InvItemID = GetUserFlag(#USERID,45) - 1
	SetUserFlag(#USERID,45,0)	
	If InvItemID = -1
		GoTo CONTINUE
	ElseIf InvItemID > 31
		GoTo CONTINUE
	EndIf
	
	GetUserFlag(#USERID,45)
	
	If MSG="accept"
		GoTo REPAIR
	EndIf
EndIf
Label CONTINUE

If MSG="repair"
	SendMessageToUser(#USERID,"Apprentice says, 'What item should I repair? Your equiped [armor], or your equiped [weapon]?'",#MSG_SAY)
ElseIf MSG="armor"
	InvItemID = GetUserEquippedArmor(#USERID)
	If InvItemID = -1
		SendMessageToUser(#USERID,"Apprentice says, 'You don't... you don't have any armor equipped... I mean, what the heck man, how can you mistakenly think you have a 40 pound piece of armor on your chest?'",#MSG_SAY)
		GoTo END
	EndIf
	
	ItemCost = GetItemValue(GetUserInvItem(#USERID,InvItemID))
	ItemDur = GetItemDurability(#USERID,InvItemID)
	ItemMaxDur = GetItemFlag2(GetUserInvItem(#USERID,InvItemID))
	ItemPercentage = ItemDur/ItemMaxDur
	ItemPercentage = 1 - ItemPercentage
	TotalCost = ItemCost*ItemPercentage
	TotalCost = Floor(TotalCost*RepairCostScale)
	
	If ItemDur = -1
		SendMessageToUser(#USERID,"Apprentice says, 'This armor is unbreakable! How can I possibly repair it?'",#MSG_SAY)
		GoTo END
	EndIf
	
	If ItemPercentage < 1
		SendMessageToUser(#USERID,"Apprentice says, 'To repair your armor, it'll cost "+TotalCost+" silver.' [accept]",#MSG_SAY)
		SetUserFlag(#USERID,45,InvItemID+1)
		GoTo END
	EndIf
	
	SendMessageToUser(#USERID,"Apprentice says, 'Your armor is in excellant condition, I can't do much more than polish it, and even that is unneccasary.'",#MSG_SAY)
	GoTo END
ElseIf MSG="weapon"
	InvItemID = GetUserEquippedWeapon(#USERID)
	If InvItemID = -1
		SendMessageToUser(#USERID,"Apprentice says, 'What weapon do you have equipped? That's a rhetorical question, by the way.'",#MSG_SAY)
		GoTo END
	EndIf
	
	ItemCost = GetItemValue(GetUserInvItem(#USERID,InvItemID))
	ItemDur = GetItemDurability(#USERID,InvItemID)
	ItemMaxDur = GetItemFlag2(GetUserInvItem(#USERID,InvItemID))
	ItemPercentage = ItemDur/ItemMaxDur
	ItemPercentage = 1 - ItemPercentage
	TotalCost = ItemCost*ItemPercentage
	TotalCost = Floor(TotalCost*RepairCostScale)
	
	If ItemDur = -1
		SendMessageToUser(#USERID,"Apprentice says, 'This weapon is unbreakable! How can I possibly repair it?'",#MSG_SAY)
		GoTo END
	EndIf
	
	If ItemPercentage < 1
		SendMessageToUser(#USERID,"Apprentice says, 'To repair your weapon, it'll cost "+TotalCost+" silver.' [accept]",#MSG_SAY)
		SetUserFlag(#USERID,45,InvItemID+1)
		GoTo END
	EndIf
	
	SendMessageToUser(#USERID,"Apprentice says, 'Your weapon is in excellant condition, I can't do much more than polish it, and even that is unneccasary.'",#MSG_SAY)
	GoTo END
Else
	SendMessageToUser(#USERID,"Apprentice says, 'I can [repair] a weapon or armor for you, bringing it back to perfect condition.'",#MSG_SAY)
EndIf

GoTo END
Label REPAIR

ItemCost = GetItemValue(GetUserInvItem(#USERID,InvItemID))
ItemDur = GetItemDurability(#USERID,InvItemID)
ItemMaxDur = GetItemFlag2(GetUserInvItem(#USERID,InvItemID))
ItemPercentage = ItemDur/ItemMaxDur
ItemPercentage = 1 - ItemPercentage
TotalCost = ItemCost*ItemPercentage
TotalCost = Floor(TotalCost*RepairCostScale)

If GetUserSilver(#USERID) < TotalCost
	SendMessageToUser(#USERID,"Apprentice says, 'You don't have enough money, sorry.'",#MSG_SAY)
	GoTo END
EndIf

SetUserSilver(#USERID,GetUserSilver(#USERID)-TotalCost)
SendMessageToUser(#USERID,"You now have "+GetUserSilver(#USERID)+" silver pieces.",#MSG_TELL)
SetItemDurability(#USERID,InvItemID,ItemMaxDur)

If Random(0,1000) = 0
	SendMessageToUser(#USERID,"Apprentice says, 'Uh, your "+GetItemName(GetUserInvItem(#USERID,InvItemID))+" was destroyed when I tried to repair it...'",#MSG_SAY)
	SendMessageToUser(#USERID,"Apprentice says, 'Only joking, your "+GetItemName(GetUserInvItem(#USERID,InvItemID))+" is fine, and completely restored.'",#MSG_SAY)
Else
	SendMessageToUser(#USERID,"Apprentice says, 'Your "+GetItemName(GetUserInvItem(#USERID,InvItemID))+" has been perfectly restored.'",#MSG_SAY)
EndIf

Label END

Continue = 1

