;SendMessageToUser(#USERID,"Auctioneer says, 'Sorry "+GetUserName(#USERID)+". We are closed at the moment, come back later.'", #MSG_SAY)
;Goto End

If GetUserAccess(#USERID)>0
	If GetUserMessage(#USERID)="reset"
		GoTo ResetAuction
	EndIf 
Endif

If GetUserFlag(0,16)=0
;If GetUserAccess(#USERID)<1
;	SendMessageToUser(#USERID,"Auctioneer says, 'Sorry "+GetUserName(#USERID)+". Only Gods can start auctions right now.'", #MSG_SAY)
;	Goto End
;Endif
	
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	;To stop hogging of the auction system
	If GetUserAccess(#USERID) = 0
		If GetUserFlag(#USERID,45) > -1
			SetUserFlag(#USERID,45,-1)
		Else
			SendMessageToUser(#USERID,"Auctioneer says, 'Please wait 15 seconds before starting another auction.'",#MSG_SAY)
			Timer(344,15000,#USERID,45,0)
			GoTo END
		EndIf
	EndIf
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	
	If GetUserMessage(#USERID)="Yes"
		Goto StartAuction
	Elseif GetUserMessage(#USERID)="yes"
		Goto StartAuction
	Elseif GetUserMessage(#USERID)="YES"
		Goto StartAuction
	Else
		SendMessageToUser(#USERID,"Auctioneer says, 'Welcome to the Auction House "+GetUserName(#USERID)+".'", #MSG_SAY)
		SendMessageToUser(#USERID,"Auctioneer says, 'Would you like to start an auction?'", #MSG_SAY)
		Goto End
	Endif
Elseif GetUserFlag(0,16)=1
	If #USERID=GetUserFlag(1,16)
		Goto StartAuction
	Else
		Goto AuctionBusy
	Endif
Elseif GetUserFlag(0,16)=2
	If #USERID=GetUserFlag(1,16)
		Goto StartAuctionBid
	Else
		Goto AuctionBusy
	Endif
Elseif GetUserFlag(0,16)=3
	If #USERID=GetUserFlag(1,16)
		Goto StartAuctionBid2
	Else
		Goto AuctionBusy
	Endif
Elseif GetUserFlag(0,16)=4
	If #USERID=GetUserFlag(1,16)
		Goto StartAuctionStep
	Else
		Goto AuctionBusy
	Endif
Elseif GetUserFlag(0,16)=5
	SendMessageToUser(#USERID,"Auctioneer says, 'The auction is about to begin. Please wait...'", #MSG_SAY)
	Goto End
Elseif GetUserFlag(0,16)>5
	Goto AuctionUserBid
Endif


Label StartAuction
SetUserFlag(1,16,#USERID)
Timer(300,15000,GetUserFlag(0,16),0,0)

;If GetUserAccess(#USERID)<1
;	SendMessageToUser(#USERID,"Auctioneer says, 'Sorry "+GetUserName(#USERID)+". Only Gods can start auctions right now.'", #MSG_SAY)
;	Goto End
;Endif
SetUserFlag(0,16,1)

If GetItemAtXY(GetUserMap(#USERID),GetUserX(#USERID),GetUserY(#USERID))>0
	SetUserFlag(0,17,GetItemAtXY(GetUserMap(#USERID),GetUserX(#USERID),GetUserY(#USERID)))
	;What's this for?
	RemoveItemAtXY(#MAPID,#XPOS,#YPOS)
	;RemoveItem(#MAPID,GetUserFlag(0,17))
	GiveItem(#USERID,GetUserFlag(0,17))
	Goto StartAuctionBid
Endif

If GetItemID(GetUserMessage(#USERID))>0
	If Substring(GetUserMessage(#USERID),1,1)="*"
		SendMessageToUser(#USERID,"Auctioneer says, 'This item cannot be sold, it's non-tradable.'",#MSG_SAY)
		SetUserFlag(0,16,0)       
	  	SetUserFlag(0,17,0)
	  	SetUserFlag(0,40,0)
		GoTo END
	EndIf
	If HasItem(#USERID,GetItemID(GetUserMessage(#USERID)))<>1
		SendMessageToUser(#USERID,"Auctioneer says, 'Hey! You don't have a "+GetItemName(GetItemID(GetUserMessage(#USERID)))+" with you!'", #MSG_SAY)
		Goto End
	Else
	SetUserFlag(0,17,GetItemID(GetUserMessage(#USERID)))
	Goto StartAuctionBid
	Endif
Else
	SendMessageToUser(#USERID,"Auctioneer says, 'Enter the item name you would like to auction...'", #MSG_SAY)
	Goto End
Endif

Label StartAuctionBid
Timer(300,15000,GetUserFlag(0,16),0,0)
;If GetUserAccess(#USERID)<1
;	SendMessageToUser(#USERID,"Auctioneer says, 'Sorry "+GetUserName(#USERID)+". Only Gods can start auctions right now.'", #MSG_SAY)
;	Goto End
;Endif
SetUserFlag(0,16,2)
SendMessageToUser(#USERID,"Auctioneer says, 'How much do you want to start the bidding for the "+GetItemName(GetUserFlag(0,17))+" at?'", #MSG_SAY)
SetUserFlag(0,16,3)
Goto End

Label StartAuctionBid2
Timer(300,15000,GetUserFlag(0,16),0,0)
;If GetUserAccess(#USERID)<1
;	SendMessageToUser(#USERID,"Auctioneer says, 'Sorry "+GetUserName(#USERID)+". Only Gods can start auctions right now.'", #MSG_SAY)
;	Goto End
;Endif
If Floor(GetUserMessage(#USERID))>0
	Goto StartAuctionStep
Else
	SendMessageToUser(#USERID,"Auctioneer says, 'How much do you want to start the bidding for the "+GetItemName(GetUserFlag(0,17))+" at?'", #MSG_SAY)
	Goto End
Endif

Label StartAuctionStep
Timer(300,15000,GetUserFlag(0,16),0,0)
;If GetUserAccess(#USERID)<1
;	SendMessageToUser(#USERID,"Auctioneer says, 'Sorry "+GetUserName(#USERID)+". Only Gods can start auctions right now.'", #MSG_SAY)
;	Goto End
;Endif
SetUserFlag(0,16,4)
If Floor(GetUserMessage(#USERID))<10
	SendMessageToUser(#USERID,"Auctioneer says, 'The starting bid must be at least 10 silver.'", #MSG_SAY)
	Goto End
Endif

SetUserFlag(0,16,Floor(GetUserMessage(#USERID)))
SendMessageToUser(#USERID,"Auctioneer says, 'The bidding will start at "+GetUserFlag(0,16)+" silver for the "+GetItemName(GetUserFlag(0,17))+".'", #MSG_SAY)
Goto BeginAuction

Label BeginAuction
If HasItem(#USERID,GetUserFlag(0,17))=1
;What's this for?
RemoveItemAtXY(#MAPID,#XPOS,#YPOS)
TakeItem(#USERID,GetUserFlag(0,17))
SpawnItem(#MAPID,GetUserFlag(0,17),10,5)
Else
SendMessageToUser(#USERID,"Auctioneer says, 'Hey you idiot! You don't have a "+GetItemName(GetUserFlag(0,17))+" with you anymore!'", #MSG_SAY)
SetUserFlag(0,16,0)
Goto End
Endif
SetUserFlag(0,40,0)
Timer(297,0,0,0,0)
Timer(299,30000,GetUserFlag(0,16),0,0)
Goto End

Label AuctionUserBid
If #USERID=GetUserFlag(1,16)
	SendMessageToUser(#USERID,"Auctioneer says, 'You can't bid for your own item... Sorry!'", #MSG_SAY)
	Goto End
Endif

If GetUserSilver(#USERID)>Floor(GetUserFlag(0,16))-1
If GetUserMessage(#USERID)="Yes"
	Goto AuctionUserBid2
Elseif GetUserMessage(#USERID)="yes"
	Goto AuctionUserBid2
Elseif GetUserMessage(#USERID)="YES"
	Goto AuctionUserBid2
Else
		SendMessageToUser(#USERID,"Auctioneer says, 'Would you like to bid "+GetUserFlag(0,16)+" silver for the "+GetItemName(GetUserFlag(0,17))+"?'", #MSG_SAY)
		Goto End
Endif
Else
	SendMessageToUser(#USERID,"Auctioneer says, 'You don't have "+GetUserFlag(0,16)+" silver to bid. Sorry!'", #MSG_SAY)
	Goto End
Endif

Label AuctionUserBid2

;Check to make sure the user has room
If IsInvFull(#USERID) = 1
	SendMessageToUser(#USERID,"Your inventory is full - Drop or destroy a item before bidding.",#MSG_CRITICAL)
	GoTo END
EndIf

LastBidder = GetUserFlag(0,40)
If LastBidder > 0
	PreviousBid = GetUserFlag(0,16)-Floor(GetUserFlag(0,16)/11)
	SetUserSilver(LastBidder,GetUserSilver(LastBidder)+PreviousBid)
	;SendMessageToAll(GetUserName(LastBidder)+" gets his "+PreviousBid+" silver back.",#MSG_SERVER)
EndIf

SendMessageToMap(#MAPID,GetUserName(#USERID)+" bids "+GetUserFlag(0,16)+" silver for the "+GetItemName(GetUserFlag(0,17))+"! "+"Now going for "+Floor(GetUserFlag(0,16)+Floor(GetUserFlag(0,16)/10))+".", #MSG_EMOTE)

SetUserFlag(0,40,#USERID)
SetUserSilver(#USERID,GetUserSilver(#USERID)-GetUserFlag(0,16))

Timer(298,10000,#USERID,GetUserFlag(0,16),1)
Timer(298,20000,#USERID,GetUserFlag(0,16),2)
Timer(298,30000,#USERID,GetUserFlag(0,16),3)
SetUserFlag(0,16,GetUserFlag(0,16)+Floor(GetUserFlag(0,16)/10))

Goto End

Label AuctionBusy
If GetUserFlag(1,16) = -1
	SendMessageToUser(#USERID,"Auctioneer says, 'Sorry, but Master Regulus is already starting an auction. Please wait...'", #MSG_SAY)
Else
	;Check to make sure that user is online and in the auction house.
	If IsOnline(GetUserFlag(1,16)) = 1
		If GetUserMap(GetUserFlag(1,16)) = 683
			SendMessageToUser(#USERID,"Auctioneer says, 'Sorry, but "+GetUserName(GetUserFlag(1,16))+" is already starting an auction. Please wait...'", #MSG_SAY)
		Else
			SendMessageToUser(#USERID,"The auctioneer stares at you with a blank look.", #MSG_EMOTE)
			SendMessageToUser(#USERID,"Auctioneer says, 'Err, sorry, what did you say?'", #MSG_SAY)
			GoTo ResetAuction
		EndIf
	Else
		SendMessageToUser(#USERID,"The auctioneer stares at you with a blank look.", #MSG_EMOTE)
		SendMessageToUser(#USERID,"Auctioneer says, 'Err, sorry, what did you say?'", #MSG_SAY)
		GoTo ResetAuction
	EndIf
EndIf
Goto End

Label ResetAuction
SetUserFlag(0,16,0)       
SetUserFlag(0,17,0)
SetUserFlag(0,40,0)
GoTo End

Label End

Continue = 1
