#!/usr/bin/env python
# -*- coding: utf-8 -*-
#PK对战数据

import pkwarflog
import math
import random

#英雄技能
class PKHeroSkill():
    def __init__(self,phero,skillCID,skillleve):
        self.hero = phero               #技能所属英雄
        self.level = skillleve          #技能等级
        self.cid = skillCID             #技能配置ID
        self.skillType = 0              #技能效果类型
        
        self.skillConftab = {}
#     参数1    参数2    参数3   参数4
# 1,单体伤害,次数激发,伤害值 ,额外伤害值    
# 2,范围伤害,次数激发,范围值,伤害值,额外伤害
# 3,持续伤害,次数激发,伤害值,持续时间,额外伤害值
# 4,攻击回血,次数激发,伤害值,回复比例(百分比),额外回血
# 5,反伤,次数激发,反伤比例(百分比),持续时间,额外伤害值
# 6,降攻击,次数激发,范围值,提升数值,提升时间
# 7,降攻速,次数激发,范围值,提升数值,提升时间
# 8,降防御,次数激发,范围值,提升数值,提升时间
# 9,升攻击,次数激发,范围值,提升数值,提升时间
# 10,升攻速,次数激发,范围值,提升数值,提升时间
# 11,升防御,次数激发,范围值,提升数值,提升时间
        self.skillP1 = 0                #技能参数1
        self.skillP2 = 0                #技能参数2
        self.skillP3 = 0                #技能参数3
        self.skillP4 = 0                #技能参数4
    def initSkillData(self,pkwart):
        self.skillConftab = pkwart.getConfigDataWithTableNameAndID('tab_skill',self.cid*100+self.level)
        self.skillType = int(self.skillConftab['state_effect'])         #技能效果类型
        self.skillEffectID = int(self.skillConftab['skillEffectId'])    #技能效果ID
        self.skillP1 = int(self.skillConftab['val1'])                   #技能参数1
        self.skillP2 = int(self.skillConftab['val2'])                   #技能参数2
        self.skillP3 = int(self.skillConftab['val3'])                   #技能参数3
        self.skillP4 = int(self.skillConftab['val4'])                   #技能参数4
#装备
class PKEquip():
    def __init__(self,pHero,pkEquipData):
        self.hero = pHero                   #装备所在英雄
        self.nid = pkEquipData.equipNID
        self.cid = pkEquipData.equipCID
        self.level = pkEquipData.equipLevel
        self.star = pkEquipData.equipStar
        
        
        self.econftab = {}
        self.ettrConftab = {}
        self.equipskilltab = {}
        self.equipType = 0                          #装备部位
        
        #星级成长
        self.starBlood = 0                          #星级血量成长
        self.starAttack = 0                         #星级攻击力成长
        self.starArmor = 0                          #星级护甲值成长
        self.effectAdd = 0                          #装备特效增加百分比
        
        #品质增加
        self.quality = pkEquipData.equipQuality
        self.quAddBlood = 0                         #品质增加血量
        self.quAddAttack = 0                        #品质增加攻击力
        self.quAddArmor = 0                         #品质增加护甲
        self.quAddAttackSpeed = 0                   #品质增加攻速
        self.quAddMoveSpeed = 0                     #品质增加移动速度
        self.quAddAttackRange = 0                   #品质增加攻击范围
        self.quAddTroops = 0                        #品质增加带兵数
        
        #基础战斗属性
        self.blood = 0
        self.attack = 0
        self.armor = 0
        self.attackSpeed = 0
        self.moveSpeed = 0
        self.attackRange = 0
        self.troops = 0         
        self.effectSkillCID = 0                     #特效配置ID
        self.effectValue = 0                        #特效基础效果值
        
        #装备特效技能
        self.effectType = 0                         #装备特效类型
        self.effectP1 = 0                           #装备特效参数1
        self.effectP2 = 0                           #装备特效参数2
        self.effectP3 = 0                           #装备特效参数3
        self.effectP4 = 0                           #装备特效参数4
        
        self.equipSkill = None
        
    def initEquipData(self,pkwart):
        self.econftab = pkwart.getConfigDataWithTableNameAndID('tab_goods',self.nid)
        self.ettrConftab = pkwart.getConfigDataWithTableNameAndID('tab_equipAttr',self.nid*10+self.star)
        #基础属性
        self.blood = int(self.econftab['bloodInit'])                #血量
        self.attack = int(self.econftab['attackInit'])              #基础攻击力
        self.armor = int(self.econftab['armorInit'])                #基础护甲值
        self.attackSpeed = int(self.econftab['attackSpeed'])        #基础攻击速度
        self.moveSpeed = int(self.econftab['moveSpeed'])            #基础移动速度
        self.attackRange = int(self.econftab['attackRange'])        #基础攻击范围
        self.troops = int(self.econftab['troops'])                  #基础代兵数     
        self.effectSkillCID = int(self.econftab['attackEffects'])   #特效配置ID
        self.Initial = int(self.econftab['initial'])                #特效基础效果值
        
        #星级成长
        self.starBlood = int(self.ettrConftab['blood'])                                 #星级血量成长
        self.starAttack = int(self.ettrConftab['attack'])                               #星级攻击力成长
        self.starArmor = int(self.ettrConftab['armor'])                                 #星级护甲值成长
        self.starInitial = int(self.ettrConftab['initialIncrease'])                     #装备特效增加百分比
        
        self.quAddBlood = int(self.ettrConftab['bloodIncrease'])                         #品质增加血量
        self.quAddAttack = int(self.ettrConftab['attackIncrease'])                       #品质增加攻击力
        self.quAddArmor = int(self.ettrConftab['armorIncrease'])                         #品质增加护甲
        self.quAddAttackSpeed = int(self.ettrConftab['attackSpeedIncrease'])             #品质增加攻速
        self.quAddMoveSpeed = int(self.ettrConftab['moveSpeedIncrease'])                     #品质增加移动速度
        self.quAddAttackRange = int(self.ettrConftab['attackRangeIncrease'])                   #品质增加攻击范围
        self.quAddTroops = int(self.ettrConftab['troopsIncrease'])                        #品质增加带兵数     
        
        self.initEquipSkill(pkwart)
    def initEquipSkill(self,pkwart):
        self.equipskilltab = pkwart.getConfigDataWithTableNameAndID('tab_attackEffects',self.effectSkillCID)
        self.effectType = int(self.equipskilltab['debuff'])                       #装备特效类型
        self.effectP1 = int(self.equipskilltab['val1'])                           #装备特效参数1
        self.effectP2 = int(self.equipskilltab['val2'])                           #装备特效参数2
        self.effectP3 = int(self.equipskilltab['val3'])                           #装备特效参数3
        self.effectP4 = int(self.equipskilltab['val4'])                           #装备特效参数4
        
        self.initWarData()
    def initWarData(self):
        self.blood += (self.starBlood*self.level +  self.quality*self.quAddBlood)
        self.attack += (self.starAttack*self.level + self.quality*self.quAddAttack)
        self.armor += (self.starArmor*self.level + self.quality*self.quAddArmor)
        self.attackSpeed += (self.quality*self.quAddAttackSpeed)
        self.moveSpeed += (self.quality*self.quAddMoveSpeed)
        self.attackRange += (self.quality*self.quAddAttackRange)
        self.troops += (self.quality*self.quAddTroops)
        self.Initial += (self.starInitial*self.level)
    
    
#英雄
class PKHero():
    def __init__(self,pPKWarT,pkHeroData,pnumber,onceWarCount,heroCamp):
        self.pkWarT = pPKWarT                       #战斗战场对象
        self.heroCID = pkHeroData.heroCID
        self.heroNID = pkHeroData.heroNID
        
        self.heroNumber = pnumber                   #英雄出站编号,
        self.onceWarCount = onceWarCount            #单次战斗出站英雄数量
        self.heroSpacePoint = int(pnumber%onceWarCount)    #英雄站位
        self.warWidth = self.pkWarT.mapWidth        #地图宽度
        
        self.warTimes = 0                           #英雄在本场战斗中出战次数,在帮战中英雄会出现多次出战情况。每个英雄最多不能超过3次出战
        
        self.px = -100                              #英雄x坐标
        self.py = -100                              #英雄y坐标
        
        #英雄等级
        self.heroLevel = pkHeroData.heroLevel
        #星级成长
        self.heroStar = pkHeroData.heroStar         #英雄星级
        self.starBlood = 0                          #星级血量成长
        self.starAttack = 0                         #星级攻击力成长
        self.starArmor = 0                          #星级护甲成长
        
        #品质增加
        self.heroQuality = 0                        #英雄品质
        self.quAddBlood = 0                         #品质增加血量
        self.quAddAttack = 0                        #品质增加攻击力
        self.quAddArmor = 0                         #品质增加护甲

        #英雄相关配置表数据
        self.heroConfTab = {}       #英雄属性表数据,通过配置ID得到
        self.heroStarConfTab = {}   #英雄星级表数据,通过英雄配置ID和星级计算得到表ID
        
        #技能
        self.skillCID = pkHeroData.skillCID       #英雄技能配置ID
        self.skillLevel = pkHeroData.skillLevel   #英雄技能等级
        self.heroSkill = None           #英雄技能对像
        self.heroSkillConfTab = {}      #英雄技能配置表数据
        
        #羁绊
        self.otherHeros = {}            #羁绊英雄CID,这里保存一同战斗的英雄{英雄配置ID:[英雄网络ID,英雄网络ID,...]}
        self.uesedOtherHeros = []       #从配置表初始读到的当前英雄羁绊相关英雄配置ID
        self.otherHeroP1 = 0            #伙伴参数1
        self.otherHeroP2 = 0            #伙伴参数2
        self.heroCamp = heroCamp        #英雄阵营
        
        #装备
        self.equipdatas = pkHeroData.equips   #def __init__(self,equipNID,equipCID,equipLevel,equipStar,equipQuality):
        self.equips = {}                #装备数据对象{装备网络ID:装备数据对象}
        self.eAttack = 0
        self.eBlood = 0
        self.eArmor = 0
        self.eAttackSpeed = 0
        self.eMovespeed = 0
        self.eAttackRange = 0
        self.eTroops = 0
        
        #英雄战斗基础表中算得的属性
        self.attack = 0
        self.blood = 0
        self.armor = 0
        self.attackSpeed = 0
        self.movespeed = 0
        self.attackRange = 0
        self.attackCastT = (150.0 - self.attackSpeed)/100.0
        
        
        #战斗事件相关
        self.warRun = None
        
        #英雄战斗数据
        self.warAttack = 0              #战斗中的动态攻击力
        self.warBlood = 0               #战斗中的动态血量
        self.warArmor = 0               #战斗中动态护甲
        self.warAttackSpeed = 0         #战斗中动态攻击速度
        self.warMoveSpeed = 0           #战斗中动态移动速度
        self.warAttackRange = 0         #战斗中动态攻击范围
        
        self.warBaseAttack = 0
        self.warBaseBlood = 0
        self.warBaseArmor = 0
        self.warBaseAttackSpeed = 0
        self.warBaseMoveSpeed = 0
        self.warBaseAttackRange = 0
        
        
        #移动到目标坐标
        self.startMoveTime = -1                                          #开始移动的时间
        self.nextMoveToX = self.px                                      #下一个移动目标点x坐标
        self.nextMoveToY = self.py                                      #下一个移动目标点y坐标
        
        #攻击或移向攻击的目标
        self.attackTarget = None                        #攻击锁定目标，一旦目标锁定，除非目标被打死，否则不改变攻击目标
        self.warState = pkwarflog.flog_move             #当前英雄战斗状态,分为:1.攻击,2.移动(眩晕为特殊移动)
        
        self.attackTimes = 0                            #当前英雄攻击次数
        
        self.isSubSpeed = False                         #是否被减速,因为技能伤害特殊性所以记录这些状态
        self.isSwam = False                             #是否被击晕,因为技能伤害特殊性所以记录这些状态
        self.isSubArmor = False                         #是否被破甲,因为技能伤害特殊性所以记录这些状态
        
        self.backAttackPersent = 0                      #返弹伤害百分比
        self.isStopAttack = False                       #是否停止攻击
        
        
        self.initHeroPKData()
        
    def getAttackCastTime(self):
        return (150.0 - self.attackSpeed)/100.0
    def initHeroPKData(self):           #初始化英雄战斗数据
        self.heroConfTab = self.pkWarT.getConfigDataWithTableNameAndID('tab_heroInfo',self.heroCID)
        self.heroStarConfTab = self.pkWarT.getConfigDataWithTableNameAndID('tab_heroAttr',self.heroCID*10 + self.heroStar)
        
        self.heroQuality = int(self.heroConfTab['rank'])                #品质
        self.blood = int(self.heroConfTab['bloodInit'])                 #基础血量
        self.attack = int(self.heroConfTab['attackInit'])               #基础攻击力
        self.armor = int(self.heroConfTab['armorInit'])                 #基础护甲
        self.attackSpeed = int(self.heroConfTab['attackSpeed'])         #基础攻击速度
        self.movespeed = int(self.heroConfTab['moveSpeed'])             #基础移动速度
        self.attackRange = int(self.heroConfTab['attackRange'])         #基础攻击范围
        
        self.starBlood = int(self.heroStarConfTab['blood'])             #血量成长
        self.starAttack = int(self.heroStarConfTab['attack'])           #攻击成长
        self.starArmor = int(self.heroStarConfTab['armor'])             #护甲成长
        
        self.quAddBlood = int(self.heroStarConfTab['bloodIncrease'])    #品质增加血量
        self.quAddAttack = int(self.heroStarConfTab['attackIncrease'])  #品质增加攻击力
        self.quAddArmor = int(self.heroStarConfTab['armorIncrease'])    #品质增加护甲
        
        self.initEquips()           #初始化装备数据
        
    def initEquips(self):           
        for edata in self.equipdatas:
            tmpequip = PKEquip(self,edata)
            tmpequip.initEquipData(self.pkWarT)
            self.eAttack += tmpequip.attack
            self.eBlood += tmpequip.blood
            self.eArmor += tmpequip.armor
            self.eAttackSpeed += tmpequip.attackSpeed
            self.eMovespeed += tmpequip.moveSpeed
            self.eAttackRange += tmpequip.attackRange
            self.equips[edata.equipNID] = tmpequip
        self.initSkill()                                    #初始化技能数据
    def initSkill(self):            
        self.heroSkill = PKHeroSkill(self,self.skillCID,self.skillLevel)
        self.heroSkill.initSkillData(self.pkWarT)
        self.initWarHeroData()                              #通过装备和技能数据初始化最终战斗数据
    #初始化英雄战斗数据
    def initWarHeroData(self):#初始化英雄战斗数据
        #英雄战斗数据
        self.warAttack = self.attack + self.heroLevel*self.starAttack + self.quAddAttack*self.heroQuality + self.eAttack              #战斗攻击力=英雄基础攻击力+英雄等级成长攻击力+英雄品质增加攻击力+所有装备基础攻击力+所有装备等级成长攻击力+所有装备品质增加攻击力
        self.warBlood = self.blood + self.heroLevel*self.starBlood + self.quAddBlood*self.heroQuality + self.eBlood                   #战斗中的动态血量
        self.warArmor = self.armor + self.heroLevel*self.starArmor + self.quAddArmor*self.heroQuality + self.eArmor                   #战斗中动态护甲
        self.warAttackSpeed = self.attackSpeed + self.eAttackSpeed         #战斗中动态攻击速度
        self.warMoveSpeed = self.movespeed + self.eMovespeed               #战斗中动态移动速度
        self.warAttackRange = self.attackRange + self.eAttackRange         #战斗中动态攻击范围

        self.warBaseAttack = self.warAttack
        self.warBaseBlood = self.warBlood
        self.warBaseArmor = self.warArmor
        self.warBaseAttackSpeed = self.warAttackSpeed
        self.warBaseMoveSpeed = self.warMoveSpeed
        self.warBaseAttackRange = self.warAttackRange
    def heroSetPoint(self):
        maptab = self.pkWarT.getConfigDataWithTableNameAndID('tab_warMap',self.pkWarT.warType)
        if self.heroCamp == pkwarflog.flog_upHero:
            hnamestr = 'upherox' + str(self.heroSpacePoint+1)
            self.px = int(maptab[hnamestr])
            hnamestr = 'upheroy' + str(self.heroSpacePoint+1)
            self.py = int(maptab[hnamestr])
        elif self.heroCamp == pkwarflog.flog_downHero:
            hnamestr = 'downherox' + str(self.heroSpacePoint+1)
            self.px = int(maptab[hnamestr])
            hnamestr = 'downheroy' + str(self.heroSpacePoint+1)
            self.py = int(maptab[hnamestr])
        print 'init hero point:%d,%d,%d'%(self.heroSpacePoint,self.px,self.py)
    def warStart(self):#战斗开始
        self.autoFindTarget()
        
    def changeBlood(self,value):
        if value < 0:
            subBlood = self.warArmor + value
            if  subBlood >= 0:#玩有护甲可以抵挡所有伤害
                self.warBlood -= 1
                self.warRun.onEventHeroBloodChange(self.heroNID,-1)              #heroNID,changeValue):
            else:
                self.warBlood += subBlood
                self.warRun.onEventHeroBloodChange(self.heroNID,subBlood)              #heroNID,changeValue):
        else:#玩家回血
            self.warBlood += value
            self.warRun.onEventHeroBloodChange(self.heroNID,value)              #heroNID,changeValue):
    def changeBloodSomeTimes(self,value,delayTime,pTimes):                  #持续多少次，每次造成多少伤害,或者回多少血
        if pTimes > 0:
            self.changeBlood(value)
            def delayFunc(changeValue = value):
                self.changeBlood(changeValue)
            self.warRun.addTimerFunc(self,delayFunc,timespace = delayTime,isRepeatforever = False,repeattimes = pTimes - 1,isRunBefor = False,isSendEvent = True,eventKey = 'debuff')
    def changeMoveSpeedSomeTime(self,value,ptime):                          #一定时间内改变移动速度
        self.warMoveSpeed += value
        if self.startMoveTime < 0:
            if value < 0:
                self.isSubSpeed = True
            def delayFunc(changeValue = value):
                self.warMoveSpeed -= changeValue
                if changeValue < 0:
                    self.isSubSpeed = False
            self.warRun.addTimerFunc(self,delayFunc,timespace = ptime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = False)
        else:#当前英雄正在移动中
            oldmovetime = self.warRun.getTargetMoveSpeedTimer(self) #得到上次设置的英雄移动时间
            self.getNowPoint(oldmovetime)                           #设置当前英雄移动到的坐标
            self.warMoveSpeed += value
            self.startMoveTime = -1
            movetime = self.moveToTarget()
            def delayFunc(changeValue = value):
                self.warMoveSpeed -= changeValue
                if changeValue < 0:
                    self.isSubSpeed = False
                self.autoFindTarget()
            self.startMoveTime = self.warRun.nowTime
            self.warRun.onEventHeroMove(self.heroNID,self.getNextOneIntPoint(),movetime)    #设置英雄移动速度改变后的移动
            self.warRun.addTimerFunc(self,delayFunc,timespace = ptime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'move')
    def changeAttackSpeedSomeTime(self,value,ptime):                        #一定时间内改变攻击速度
        self.warAttackSpeed += value
        def delayFunc(changeValue = value):
            self.warAttackSpeed -= value
        self.warRun.addTimerFunc(self,delayFunc,timespace = ptime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = False)
    def skillBackAttackSomeTime(self,persentValue,pTime):
        if self.backAttackPersent < 0:
            self.backAttackPersent = 0
        self.backAttackPersent += persentValue       #反弹伤害一定百分比,只反弹攻击伤害，技能debuff伤害不反弹
        def delayFunc(changeValue = persentValue):
            self.backAttackPersent -= changeValue
        self.warRun.addTimerFunc(self,delayFunc,timespace = pTime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = False)
    def changeArmorSomeTime(self,value,ptime):                              #一定时间内改变护甲
        self.warArmor += value
        if value < 0:
            self.isSubArmor = True
        def delayFunc(changeValue = value):
            self.warArmor -= changeValue
            if changeValue < 0:
                self.isSubArmor = False
        self.warRun.addTimerFunc(self,delayFunc,timespace = ptime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = False)
    def stopAttackSomeTime(self,ptime):    #停止攻击一定时间
        self.isStopAttack = True
        self.warRun.removeTargetAttackTimer(self)               #移除自动攻击定时器
        def delayFunc():
            self.isStopAttack = False
            self.autoFindTarget()
        self.warRun.addTimerFunc(self,delayFunc,timespace = ptime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = False)
    def changeAttackSomeTime(self,value,ptime):                             #一定时间内改变攻击力
        self.warAttack += value
        def delayFunc(changeValue = value):
            self.warAttack -=changeValue
        self.warRun.addTimerFunc(self,delayFunc,timespace = ptime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = False)
    def changeSwamSomeTimeAndSomeTimes(self,someSwam,delayReTime,pTimes):   #眩晕一定时间，每延时一段时间触发一次，一共触发pTimes次
        self.isSwam = True  #设置自已被眩晕
        if self.startMoveTime < 0:#当前目标不在移动中
            self.warMoveSpeed = 0
            self.warRun.removeTargetAttackTimer(self)               #移除自动攻击定时器
            def delayFunc():
                self.warMoveSpeed = self.warBaseMoveSpeed
                self.isSwam = False
                self.autoFindTarget()
            self.startMoveTime = self.warRun.nowTime
            self.warRun.onEventHeroMove(self.heroNID,self.getOneIntPoint(),someSwam)    #眩晕时英雄将在原地不动一段时间
            self.warRun.addTimerFunc(self,delayFunc,timespace = someSwam,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'move')
        else:#当前英雄在移动中
            if pTimes == 0:#没有延时多次触发眩晕
                oldmovetime = self.warRun.getTargetMoveSpeedTimer(self) #得到上次设置的英雄移动时间
                self.getNowPoint(oldmovetime)                           #设置当前英雄移动到的坐标
                oldmoveSpeed = self.warMoveSpeed
                self.warMoveSpeed = 0
                self.warRun.removeTargetAttackTimer(self)               #移除自动攻击定时器
                def delayFunc(omovespeed = oldmoveSpeed):
                    self.warMoveSpeed = self.warBaseMoveSpeed
                    self.isSwam = False
                    self.autoFindTarget()
                self.startMoveTime = self.warRun.nowTime
                self.warRun.onEventHeroMove(self.heroNID,self.getOneIntPoint(),someSwam)    #眩晕时英雄将在原地不动一段时间
                self.warRun.addTimerFunc(self,delayFunc,timespace = someSwam,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'move')
            elif pTimes > 0:
                oldmovetime = self.warRun.getTargetMoveSpeedTimer(self) #得到上次设置的英雄移动时间
                self.getNowPoint(oldmovetime)                           #设置当前英雄移动到的坐标
                oldmoveSpeed = self.warMoveSpeed
                self.warMoveSpeed = 0
                self.warRun.removeTargetAttackTimer(self)               #移除自动攻击定时器
                def delayFunc(swamtime = someSwam,pSwamReTime = delayReTime ,pSwamTimes = pTimes):
                    self.warMoveSpeed = self.warBaseMoveSpeed
                    self.isSwam = False
                    self.autoFindTarget()
                    if pSwamTimes > 0:
                        def redelayFunc(reswamtime = swamtime, reSwamTime = pSwamReTime,reTimes = pSwamTimes - 1):
                            self.changeSwamSomeTimeAndSomeTimes(reswamtime,reSwamTime,reTimes)
                        #多次触发的眩晕技能
                        self.warRun.addTimerFunc(self,redelayFunc,timespace = pSwamReTime,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'move')
                self.warRun.onEventHeroMove(self.heroNID,self.getOneIntPoint(),someSwam)    #眩晕时英雄将在原地不动一段时间
                self.warRun.addTimerFunc(self,delayFunc,timespace = someSwam,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'move')
            
    def moveToTarget(self):#向目标移动 
        if self.startMoveTime != -1:#当前英雄正在移动
            oldmovetime = self.warRun.getTargetMoveSpeedTimer(self)                             #得到上次设置的英雄移动时间
            self.getNowPoint(oldmovetime)                                                       #设置当前英雄移动到的坐标
        des = self.getDestenceToTarget()
        if des > self.warAttackRange:
            mx = (des - self.warAttackRange)*((self.attackTarget.px - self.px)/des)
            my = (des - self.warAttackRange)*((self.attackTarget.py - self.py)/des)
            self.nextMoveToX = self.px + mx
            self.nextMoveToY = self.py + my
            moveTimex = float((des-self.warAttack)/self.warMoveSpeed)
            def delayFunc():
                self.px = self.nextMoveToX
                self.py = self.nextMoveToY
                self.startMoveTime = -1
                self.autoFindTarget()
            self.warRun.onEventHeroMove(self.heroNID,self.getNextOneIntPoint(),moveTimex)       #设置英雄移动改变
            self.warRun.addTimerFunc(self,delayFunc,timespace = moveTimex,isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'move')
            return moveTimex
        else:
            self.nextMoveToX = self.px
            self.nextMoveToY = self.py
            self.startMoveTime = -1
            self.autoFindTarget()
            return 0
    def getNowPoint(self,moveOldCastTime):#通过英雄已行走的时间重新设置英雄当前所在坐标
        nowTime = self.warRun.nowTime
        moveTime = nowTime - self.startMoveTime
        mx = float((self.nextMoveToX - self.px)*(moveTime/moveOldCastTime))
        my = float((self.nextMoveToY - self.py)*(moveTime/moveOldCastTime))
        self.px = self.px + mx
        self.py = self.py + my
        self.nextMoveToX = self.px
        self.nextMoveToY = self.py
    
    def moveToAndAttack(self):                              #移动到目标，并开始攻击
        if self.attackTarget.warBlood <= 0:
            self.attackTarget = None
            self.autoFindTarget()
        elif self.warAttackRange >= self.getDestenceToTarget():#目标在攻击范围内
            if self.isStopAttack:#被停止攻击
                return
            self.startMoveTime = -1     #设置英雄为非移动状态
            attack = self.warAttack
            if self.isHeaveAttackSkill():#本次攻击有技能攻击
                if self.heroSkill.skillEffectID == 1001:#每#1次攻击后触发;对当前目标造成#2伤害，目标处于减速效果，则造成额外伤害#3
                    if self.attackTarget.isSubSpeed:#目标处与减速效果
                        attack += self.heroSkill.skillP3
                    else:
                        attack += self.heroSkill.skillP2
                    self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,self.attackTarget.heroNID)
                elif self.heroSkill.skillEffectID == 1002:#每#1次攻击后触发;自身周边#2范围内造成#3伤害，目标处于眩晕效果，则造成额外伤害#4
                    if self.attackTarget.isSwam:#目标处于被击晕效果
                        rangeheros = self.getRangeEenamys(self.heroSkill.skillP2)
                        if rangeheros:##范围内有目标
                            heronids = []
                            for hx in rangeheros:
                                heronids.append(hx.heroNID)
                                hx.changeBlood(-self.heroSkill.skillP4)
                            if self.attackTarget in rangeheros:
                                self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,heronids)
                            else:
                                heronids.append(self.attackTarget.heroNID)
                                self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,heronids)
                    else:
                        rangeheros = self.getRangeEenamys(self.heroSkill.skillP2)
                        if rangeheros:##范围内有目标
                            heronids = []
                            for hx in rangeheros:
                                heronids.append(hx.heroNID)
                                hx.changeBlood(-self.heroSkill.skillP3)
                            if self.attackTarget in rangeheros:
                                self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,heronids)
                            else:
                                heronids.append(self.attackTarget.heroNID)
                                self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,heronids)
                elif self.heroSkill.skillEffectID == 1003:#每#1次攻击后触发;对当前目标每秒造成#2点伤害，持续3#，目标处于减速效果，则伤害提升#4
                    if self.attackTarget.isSubSpeed:#目标处于减速效果
                        attack += self.heroSkill.skillP4
                        self.attackTarget.changeBloodSomeTimes(-self.heroSkill.skillP2,1.0,self.heroSkill.skillP3)
                    else:
                        self.attackTarget.changeBloodSomeTimes(-self.heroSkill.skillP2,1.0,self.heroSkill.skillP3)
                    self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,self.attackTarget.heroNID)
                elif self.heroSkill.skillEffectID == 1004:#每#1次攻击后触发;反弹#2%伤害值，持续#3秒，目标处于破甲效果，则造成额外伤害#4
                    self.skillBackAttackSomeTime(self.heroSkill.skillP2,self.heroSkill.skillP3)
                    if self.attackTarget.isSubArmor:#目标处于破甲效果
                        attack += self.heroSkill.skillP4
                    else:
                        pass
                    self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,self.attackTarget.heroNID)
                elif self.heroSkill.skillEffectID == 1005:#每#1次攻击后触发;对当前目标造成#2伤害，并恢复造成伤害#3%的血量值，目标处于减速效果，则造成额外伤害#4
                    if self.attackTarget.isSubSpeed:#目标处于减速效果
                        attack += self.heroSkill.skillP4
                        self.changeBlood(attack*(self.heroSkill.skillP3/100))
                    else:
                        attack += self.heroSkill.skillP2
                        self.changeBlood(attack*(self.heroSkill.skillP3/100))
                    self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,self.attackTarget.heroNID)
                elif self.heroSkill.skillEffectID == 1006:#每#1次攻击后触发;自身周边#2范围内降低对方#3点攻击，持续#4秒
                    rangeheros = self.getRangeEenamys(self.heroSkill.skillP2)
                    if rangeheros:##范围内有目标
                        for hx in rangeheros:
                            hx.changeAttackSomeTime(-self.heroSkill.skillP3,self.heroSkill.skillP4)
                    self.warRun.onEventSkill(pkwarflog.skillType_heroSkill,self.heroSkill.skillEffectID,self.heroNID,self.attackTarget.heroNID)
            equipxs = self.isHeaveEquipSKill()#本次攻击有装备特效
            for enid in equipxs:
                equiptmp = self.equips[enid]
                if equiptmp.effectType == 1:#有#1%几率产生暴击，造成#2倍伤害，并附带重伤效果持续#3秒,每秒造成#4点伤害
                    attack *= equiptmp.effectP1
                    self.attackTarget.changeBloodSomeTimes(equiptmp.effectP4,1.0,equiptmp.effectP3)#self,value,delayTime,pTimes                 #持续多少次，每次造成多少伤害,或者回多少血
                    self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,self.attackTarget.heroNID)
                elif equiptmp.effectType == 2:#造成#1点破甲，持续#2秒
                    self.attackTarget.changeArmorSomeTime(equiptmp.effectP1,equiptmp.effectP2)
                    self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,self.attackTarget.heroNID)
                elif equiptmp.effectType == 3:#有#1%几率产生击晕效果，持续#2秒，该效果每#3秒触发一次，触发#4次
                    self.attackTarget.changeSwamSomeTimeAndSomeTimes(equiptmp.effectP2,equiptmp.effectP3,equiptmp.effectP4)   #眩晕一定时间，每延时一段时间触发一次，一共触发pTimes次
                    self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,self.attackTarget.heroNID)
                elif equiptmp.effectType == 4:#同时攻击#1个目标，并触发狙击效果，持续#2秒
                    rangeheros = self.getRangeEenamys(self.warAttackRange)
                    if rangeheros:
                        attheros = []
                        for rh in rangeheros:
                            attheros.append(rh.heroNID)
                            if rh != self.attackTarget:
                                rh.changeBlood(-attack)
                            self.stopAttackSomeTime(equiptmp.effectP2)
                        if self.attackTarget in attheros:
                            self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,attheros)
                        else:
                            attheros.append(self.attackTarget.heroNID)
                            self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,attheros)
                    else:
                        self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,self.attackTarget.heroNID)
                elif equiptmp.effectType == 5:#造成#1%减速效果，持续#2，在此状态下，每秒造成#3点伤害
                    subattackspeed = int(self.attackTarget.warAttackSpeed*(equiptmp.effectP1/100.0)) - self.attackTarget.warAttackSpeed
                    self.attackTarget.changeAttackSomeTime(subattackspeed,equiptmp.effectP2)
                    self.attackTarget.changeBloodSomeTimes(equiptmp.effectP3,1.0,equiptmp.effectP2)#self,value,delayTime,pTimes:                  #持续多少次，每次造成多少伤害,或者回多少血
                    self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,self.attackTarget.heroNID)
                elif equiptmp.effectType == 6:##1%几率出现闪电攻击，造成#2点伤害，并附带麻痹效果持续#3秒
                    self.attackTarget.changeBlood(equiptmp.effectP2)
                    self.attackTarget.stopAttackSomeTime(equiptmp.effectP3) #停止攻击
                    self.warRun.onEventSkill(pkwarflog.skillType_equipSKill,equiptmp.cid,self.heroNID,self.attackTarget.heroNID)
            self.attackTarget.changeBlood(-attack)
            def delayFunc():
                self.autoFindTarget()
            self.warRun.addTimerFunc(self,delayFunc,timespace = self.getAttackCastTime(),isRepeatforever = False,repeattimes = 1,isRunBefor = False,isSendEvent = True,eventKey = 'attack')
        else:#目标超出攻击范围，开始移动动作,并设置移动结束定时器
            self.moveToTarget()
    def isHeaveAttackSkill(self):#计算本次攻击是否有英雄技能攻击
        self.attackTimes += 1
        if self.attackTimes >= self.heroSkill.skillP1:#本次攻击有技能攻击
            self.attackTimes = 0
            return True
        else:
            return False
    def isHeaveEquipSKill(self):#计算一次攻击的所有装备特效攻击,返回有效触发的技能网络ID
        equipNids = []
        for eq in self.equips.keys():
            if self.equips[eq].effectType == 1:#有#1%几率产生暴击，造成#2倍伤害，并附带重伤效果持续#3秒,每秒造成#4点伤害
                rdat = random.randrange(0,9999)
                if rdat < 100*self.equips[eq].effectP1:
                    equipNids.append(eq)
            elif self.equips[eq].effectType == 2:#造成#1点破甲，持续#2秒
                equipNids.append(eq)
            elif self.equips[eq].effectType == 3:#有#1%几率产生击晕效果，持续#2秒，该效果每#3秒触发一次，触发#4次
                rdat = random.randrange(0,9999)
                if rdat < 100*self.equips[eq].effectP1:
                    equipNids.append(eq)
            elif self.equips[eq].effectType == 4:#同时攻击#1个目标，并触发狙击效果，持续#2秒
                equipNids.append(eq)
            elif self.equips[eq].effectType == 5:#造成#1%减速效果，持续#2，在此状态下，每秒造成#3点伤害
                equipNids.append(eq)
            elif self.equips[eq].effectType == 6:##1%几率出现闪电攻击，造成#2点伤害，并附带麻痹效果持续#3秒
                if rdat < 100*self.equips[eq].effectP1:
                    equipNids.append(eq)
        return equipNids
    def getRangeEenamys(self,prange):
        enamyheros = []
        if self.heroCamp == pkwarflog.flog_upHero:#已方为上部英雄
            for rh in self.warRun.dheros:
                if prange >= self.getDestenceToPoint(rh.px, rh.py):
                    enamyheros.append(rh)
        else:#已方为下部英雄
            for rh in self.warRun.uheros:
                if prange >= self.getDestenceToPoint(rh.px, rh.py):
                    enamyheros.append(rh)
        return enamyheros
    def getDestenceToPoint(self,x,y):
        return math.sqrt((x - self.px)**2+(y - self.py)**2)
    def getDestenceToTarget(self):#计算自已与目标的距离
        return math.sqrt((self.attackTarget.px - self.px)**2+(self.attackTarget.py - self.py)**2)
    def getOneIntPoint(self):
        return self.py*self.warWidth+self.px
    def getNextOneIntPoint(self):
        return self.nextMoveToY*self.warWidth+self.nextMoveToX
    def autoFindTarget(self):#攻击的第一个目标死亡，寻找第二个可以攻击的目标
        if self.warBlood <= 0:#当前英雄死亡,删除所有定时器
            self.warRun.removeAllTimer(self)
            return
        if self.heroCamp == pkwarflog.flog_upHero:#当前英雄为上方英雄
            if self.heroSpacePoint == 0:      #第一个站位
                if self.warRun.dheros[0].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[0]
                elif self.warRun.dheros[1].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[1]
                elif self.warRun.dheros[2].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[2]
            elif self.heroSpacePoint == 1:    #第二个站位
                if self.warRun.dheros[1].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[1]
                elif self.warRun.dheros[0].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[0]
                elif self.warRun.dheros[2].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[2]
            elif self.heroSpacePoint == 2:    #第二个站位
                if self.warRun.dheros[2].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[2]
                elif self.warRun.dheros[1].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[1]
                elif self.warRun.dheros[0].warBlood > 0:
                    self.attackTarget = self.warRun.dheros[0]
        else:#当前英雄为下方英雄
            if self.heroSpacePoint == 0:      #第一个站位
                if self.warRun.uheros[0].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[0]
                elif self.warRun.uheros[1].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[1]
                elif self.warRun.uheros[2].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[2]
            elif self.heroSpacePoint == 1:    #第二个站位
                if self.warRun.uheros[1].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[1]
                elif self.warRun.uheros[0].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[0]
                elif self.warRun.uheros[2].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[2]
            elif self.heroSpacePoint == 2:    #第二个站位
                if self.warRun.uheros[2].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[2]
                elif self.warRun.uheros[1].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[1]
                elif self.warRun.uheros[0].warBlood > 0:
                    self.attackTarget = self.warRun.uheros[0]
        if self.attackTarget:
            self.moveToAndAttack()
        else:
            self.warRun.warEnd()