;'Back Stab' skill

;If GetUserName(#USERID)<>"ZombieFever"
	;If GetUserAccess(#USERID) < 1
	;	SendMessageToUser(#USERID,"This skill is under re-construction - Comrade",#MSG_SERVER)
	;	GoTo END
	;EndIf
;EndIf

;I was on a red map and couldn't attack Xenophobe, who was guildless


If GetUserSkill(#USERID,27) > 10
	SendCombatMessage(#USERID,"You can't use this skill so soon! ")
	GoTo END
EndIf

UserID = #USERID
TargetID = 0
MonsterID = 0
Damage = 0

If GetUserDirection(UserID) = #DIR_UP
	TargetID = GetUserAtXY(#MAPID,#XPOS,#YPOS-1)
	MonsterID = GetMonsterAtXY(#MAPID,#XPOS,#YPOS-1)
ElseIf GetUserDirection(UserID) = #DIR_DOWN
	TargetID = GetUserAtXY(#MAPID,#XPOS,#YPOS+1)
	MonsterID = GetMonsterAtXY(#MAPID,#XPOS,#YPOS+1)
ElseIf GetUserDirection(UserID) = #DIR_LEFT
	TargetID = GetUserAtXY(#MAPID,#XPOS-1,#YPOS)
	MonsterID = GetMonsterAtXY(#MAPID,#XPOS-1,#YPOS)
Else
	TargetID = GetUserAtXY(#MAPID,#XPOS+1,#YPOS)
	MonsterID = GetMonsterAtXY(#MAPID,#XPOS+1,#YPOS)
EndIf

If GetUserAccess(UserID) < 6
	If GetUserEquippedWeapon(UserID) <> -1
		If GetUserInvItem(UserID,GetUserEquippedWeapon(UserID)) <> 202
			SendCombatMessage(UserID,"You are too encumbered. ")
			GoTo END
		EndIf
	EndIf
	If GetUserEquippedArmor(UserID) <> -1
		If GetUserInvItem(#USERID,GetUserEquippedArmor(UserID)) <> 201
			SendCombatMessage(UserID,"You are too encumbered. ")
			GoTo END
		EndIf
	EndIf
	If GetUserEquippedHelmet(UserID) <> -1
		SendCombatMessage(UserID,"You are too encumbered. ")
		GoTo END
	EndIf
	If GetUserEquippedBoots(UserID) <> -1
		SendCombatMessage(UserID,"You are too encumbered. ")
		GoTo END
	EndIf
	If GetUserEquippedAccessory(UserID) <> -1
		If GetUserInvItem(#USERID,GetUserEquippedAccessory(UserID)) <> 203
			SendCombatMessage(UserID,"You are too encumbered. ")
			GoTo END
		EndIf
	EndIf
	
	If GetUserAccess(TargetID) > 5
		SendCombatMessage(UserID,"Target too awesome to backstab! ")
		GoTo END
	EndIf
EndIf

If TargetID = #USERID
	TargetID = 0
EndIf

If GetMapType(#MAPID) = #MAPTYPE_FRIENDLY 
	TargetID = 0
EndIf

If GetUserGuild(UserID) = GetUserGuild(TargetID)
	TargetID = 0
EndIf

If GetMapType(#MAPID) = #MAPTYPE_NEUTRAL 
	If GetUserGuild(UserID) = 0
		TargetID = 0
	EndIf
	If GetUserGuild(TargetID) = 0
		TargetID = 0
	EndIf
EndIf

If TargetID > 0
	If GetUserDirection(UserID) <> GetUserDirection(TargetID)
		SendCombatMessage(UserID,"You aren't facing "+GetUserName(TargetID)+"'s back. ")
		GoTo END	
	EndIf
	
	Damage = GetUserSkill(UserID,27)
	Damage = Damage * 35
	AddDamage = GetUserStrengthTotal(UserID)
	AddDamage = Floor(AddDamage * 2.5)
	Damage = Damage + AddDamage 
	
	Damage = Damage - GetUserEnduranceTotal(TargetID)
	If Damage < 10
		Damage = 10
	EndIf
	
	If Random(0, 10) = 0
		SendCombatMessage(TargetID,GetUserName(UserID)+" tried to backstab you! ")
		SendCombatMessage(UserID,"You missed "+GetUserName(TargetID)+"! ")
		GoTo END
	EndIf
	
	If GetUserAccess(#USERID) < 6
		SetUserSkill(UserID,27,GetUserSkill(UserID,27)+10)
		Timer(345,30000,UserID,0,0)
	EndIf
	
	If GetUserHP(TargetID) < Damage
		SendMessageToAll("* "+GetUserName(TargetID)+" has been assassinated by "+GetUserName(UserID)+"!",#MSG_CRITICAL)
	EndIf
	
	SendCombatMessage(TargetID,"You've been backstabbed! ")
	SendCombatMessage(UserID,"You backstabbed "+GetUserName(TargetID)+"! ")
	UserDamageUser(UserID, Damage, TargetID)
ElseIf MonsterID > -1
	
	If GetUserDirection(UserID) <> GetMonsterDirection(#MAPID, MonsterID)
		SendCombatMessage(UserID,"You aren't facing the creature's back. ")
		GoTo END	
	EndIf
	
	Damage = GetUserSkill(UserID,27)
	Damage = Damage * 25
	AddDamage = GetUserStrengthTotal(UserID)
	AddDamage = Floor(AddDamage * 2.5)
	Damage = Damage + AddDamage 
	
	Damage = Damage - 25
	If Damage < 10
		Damage = 10
	EndIf
	
	If Random(0, 20) = 0
		SendCombatMessage(UserID,"Your backstab missed! ")
		GoTo END
	EndIf

	If GetUserAccess(#USERID) < 6
		SetUserSkill(UserID,27,GetUserSkill(UserID,27) + 10)
		Timer(345,30000,UserID,0,0)
	EndIf
	
	SendCombatMessage(UserID,"You backstabbed the creature before you! ")
	UserDamageMonster(MonsterID, Damage, UserID)
	SetMonsterTarget(#MAPID,MonsterID,UserID)
Else
	SendCombatMessage(UserID,"Nobody in range! ")
EndIf

Label END

Continue = 1
