###############################################
# Made by Mindee and DemonLord  2020
###############################################

#DON'T USE SWITCH WEAPON OF BOT SETTINGS
# Switch to the main weapon when the mob's hp is less than:
Percent = 50

# Mobs names corresponding to each weapon:
# 'Mob name': % at which script should switch weapon.
# If you want, delete "Percent" and write a different % to each mobs.
# "MobsMainWeapon" It is so as not to change weapon 149.
# % = -1  | Means, don't switch to weapon.
MobsMainWeapon = { 'Mob': -1, 'Mob': -1 }
MobsWeaponIce = { 'Mob': Percent, 'Mob': Percent }
MobsWeaponFire = { 'Mob': Percent, 'Mob': Percent }
MobsWeaponHit = { 'Mob': Percent, 'Mob': Percent }
MobsWeaponSoul = { 'Mob': -1, 'Mob': -1 }
MobsWeaponElectric = { 'Mob': Percent, 'Mob': Percent }

# Weapons IDs
MainWeapon = 0001
Weapon149Ice = 0002
Weapon149Fire = 0003
Weapon149Hit = 0004
Weapon149Soul = 0005
Weapon149Electric = 0006

# Switch to weapon 149 even with skills activated  (Yes/No | 1/0)
#  Or both are deactivated:
SkillLeft = 0
SkillRight = 0





























###################Don't touch it####################
WeaponList = { 1: MainWeapon, 2: Weapon149Ice, 3: Weapon149Fire, 4: Weapon149Hit, 5: Weapon149Soul, 6: Weapon149Electric }
EnemyHpHolder = 0
TaskRunning = False

def onScriptActivation():
	script.SetVar('weapon', 0)
	script.SetVar('Percent', Percent)

def isInEnemyList(name):
	if name in MobsMainWeapon:
		script.SetVar('weapon', 1)
		script.SetVar('Percent', MobsMainWeapon.get(name, Percent))
		return True
	if name in MobsWeaponIce:
		script.SetVar('weapon', 2)
		script.SetVar('Percent', MobsWeaponIce.get(name, Percent))
		return True
	if name in MobsWeaponFire:
		script.SetVar('weapon', 3)
		script.SetVar('Percent', MobsWeaponFire.get(name, Percent))
		return True
	if name in MobsWeaponHit:
		script.SetVar('weapon', 4)
		script.SetVar('Percent', MobsWeaponHit.get(name, Percent))
		return True
	if name in MobsWeaponSoul:
		script.SetVar('weapon', 5)
		script.SetVar('Percent', MobsWeaponSoul.get(name, Percent))
		return True
	if name in MobsWeaponElectric:
		script.SetVar('weapon', 6)
		script.SetVar('Percent', MobsWeaponElectric.get(name, Percent))
		return True

	script.SetVar('weapon', 0)
	script.SetVar('Percent', Percent)

	return False
		
def onReceiveEnemyHp(uid, hp_percent, name):
	global EnemyHpHolder, TaskRunning
	if isInEnemyList(name):
		EnemyHpHolder = hp_percent
		if not TaskRunning:
			script.RunEvent('changeWeapon', 1000)
			TaskRunning = True

def getPrefferedWeapon():
	return WeaponList.get(script.GetVar('weapon'), MainWeapon)
	
def matchesSkillCondition():
	if SkillLeft == 1 and SkillRight == 1:
		return False

	if ((script.IsSkillActivated(1) == 1 and script.IsSkillActivated(2) == 1) or
	(script.IsSkillActivated(1) == 0 and script.IsSkillActivated(2) == 1) or
	(script.IsSkillActivated(1) == 1 and script.IsSkillActivated(2) == 0)) and (SkillLeft == 0 and SkillRight == 0):
		return True

	if ((script.IsSkillActivated(1) == 1 and script.IsSkillActivated(2) == 1) or
	(script.IsSkillActivated(1) == 0 and script.IsSkillActivated(2) == 1))  and (SkillLeft == 1 and SkillRight == 0):
		return True

	if ((script.IsSkillActivated(1) == 1 and script.IsSkillActivated(2) == 1) or
	(script.IsSkillActivated(1) == 1 and script.IsSkillActivated(2) == 0))  and (SkillLeft == 0 and SkillRight == 1):
		return True

	return False

def getWeapon():
	return script.GetItemInSlot(10)

def switchToMain():
	if getWeapon() != MainWeapon:
		script.UseItemId(MainWeapon)
	script.SetVar('weapon', 0)
	script.SetVar('Percent', Percent)

def changeWeapon():
	global TaskRunning
	var_percent = script.GetVar('Percent')

	if var_percent > -1 and ((EnemyHpHolder > 0 and EnemyHpHolder <= var_percent) or matchesSkillCondition()):
		switchToMain()
	else:
		preferred = getPrefferedWeapon()
		if getWeapon() != preferred:
			if var_percent == -1:
				script.UseItemId(preferred)
			elif SkillLeft == 1 and SkillRight == 1:
				script.UseItemId(preferred)
			elif script.IsSkillActivated(1) == SkillLeft and script.IsSkillActivated(2) == SkillRight:
				script.UseItemId(preferred)
			elif script.IsSkillActivated(1) == 0 and script.IsSkillActivated(2) == 0:
				script.UseItemId(preferred)
			else:
				switchToMain()
	TaskRunning = False

def onSkillActivated(skill):
	if not TaskRunning:
		if skill == 1:
			if SkillLeft == 0:
				switchToMain()
		elif skill == 2:
			if SkillRight == 0:
				switchToMain()