;'Pawn Shop' - Buyer NPC

SendMessageToUser(#USERID,"The Shop Buyer says, 'Under work.'",#MSG_SAY)
GoTo END


STATUS_ITEM = 1
STATUS_ACCEPT = 2 

Status = GetUserFlag(#USERID,45)
Item = GetUserFlag(#USERID,17)

If Lower(GetUserMessage(#USERID))="clear"
	SetUserFlag(#USERID,45,0)
	SetUserFlag(#USERID,17,0)
	GoTo END
ElseIf Lower(GetUserMessage(#USERID))="clear"
	SetUserFlag(#USERID,45,0)
	SetUserFlag(#USERID,17,0)
	GoTo END
EndIf


If Status = STATUS_ACCEPT
	Value = Floor(GetItemValue(Item)*0.75)
	Value = Value - Floor(Mod(Value,100))
	
	If Lower(GetUserMessage(#USERID))="yes"
		If HasItem(#USERID,Item) = 0
			SendMessageToUser(#USERID,"The Shop Buyer says, 'Hmm, don't have the item with you? That's fine, just come back later, we'll try again some other time.'",#MSG_SAY)
			SetUserFlag(#USERID,45,0)
			SetUserFlag(#USERID,17,0)
			GoTo END
		EndIf
		
		TakeItem(#USERID,Item)
		SetUserSilver(#USERID,GetUserSilver(#USERID)+Value)
		SendMessageToUser(#USERID,"Your purse contains "+GetUserSilver(#USERID)+" silver pieces.",#MSG_TELL)
		GoTo CYCLE
	ElseIf Lower(GetUserMessage(#USERID))="no"
		SendMessageToUser(#USERID,"The Shop Buyer says, 'That's the best price I can offer you. What else do you have for sale?'",#MSG_SAY)
		SetUserFlag(#USERID,45,0)
		SetUserFlag(#USERID,17,0)
	Else
		SendMessageToUser(#USERID,"The Shop Buyer says, 'I can buy this "+GetItemName(Item)+" for "+Value+" silver. Is that alright with you?'",#MSG_SAY)
	EndIf
ElseIf Status = STATUS_ITEM
	Item = GetItemID(GetUserMessage(#USERID))
	If Item > 0
		If HasItem(#USERID,Item) = 0
			SendMessageToUser(#USERID,"The Shop Buyer says, 'You don't have that item, do you? The object must be on hand for me to purchase.'",#MSG_SAY)
			SetUserFlag(#USERID,45,0)
			SetUserFlag(#USERID,17,0)
			GoTo END
		EndIf
		If Substring(GetUserMessage(#USERID),1,1)="*"
			SendMessageToUser(#USERID,"The Shop Buyer says, 'This item cannot be sold, it's non-tradable.'",#MSG_SAY)
			SetUserFlag(#USERID,45,0)
			SetUserFlag(#USERID,17,0)
			GoTo END
		EndIf
		
		Value = Floor(GetItemValue(Item)*0.75)
		Value = Value - Floor(Mod(Value,100))
		If Value < 1000
			SendMessageToUser(#USERID,"The Shop Buyer says, 'What would I do with that? I don't want to buy that.'",#MSG_SAY)
			SetUserFlag(#USERID,45,0)
			SetUserFlag(#USERID,17,0)
			GoTo END
		EndIf
		
		SendMessageToUser(#USERID,"The Shop Buyer says, 'I can buy this "+GetItemName(Item)+" for "+Value+" silver. Is that alright with you?'",#MSG_SAY)
		SetUserFlag(#USERID,45,STATUS_ACCEPT)
		SetUserFlag(#USERID,17,Item)
	Else
		SendMessageToUser(#USERID,"The Shop Buyer says, 'What do you wish to sell?'",#MSG_SAY)
	EndIf
Else
	If Lower(GetUserMessage(#USERID))="yes"
		SendMessageToUser(#USERID,"The Shop Buyer says, 'What do you wish to sell?'",#MSG_SAY)
		SetUserFlag(#USERID,45,STATUS_ITEM)
	ElseIf Lower(GetUserMessage(#USERID))="sell"
		SendMessageToUser(#USERID,"The Shop Buyer says, 'What do you wish to sell?'",#MSG_SAY)
		SetUserFlag(#USERID,45,STATUS_ITEM)
	ElseIf Lower(GetUserMessage(#USERID))="no"
		SendMessageToUser(#USERID,"The Shop Buyer says, 'Than why are you bothering me?'",#MSG_SAY)
	Else
		SendMessageToUser(#USERID,"The Shop Buyer says, 'Do you wish to [sell] a item?'",#MSG_SAY)
	EndIf
EndIf

GoTo END
Label CYCLE

If GetItemAtXY(354,6,9) > 0
	RemoveItem(354,GetItemAtXY(354,6,9))
EndIf

i = 9

While i < 15
	i=i+1
	ITEM = GetItemAtXY(354,6,i)
	If ITEM > 0
		RemoveItem(354,ITEM)
		SpawnItem(354,ITEM,6,i-1)	
	EndIf
Wend

SpawnItem(354,GetUserFlag(#USERID,17),6,15)

SetUserFlag(#USERID,45,0)
SetUserFlag(#USERID,17,0)

Label END

Timer(342,30000,#USERID,0,0)

Continue = 1