def _doAttack(self, task):
     if self.target.isEmpty():
         self.exit()
         return task.done
     attack = SuitUtils.attack(self.suit, self.target)
     distance = self.suit.getDistance(self.target)
     speed = 50.0
     if attack == 'glowerpower':
         speed = 100.0
     timeUntilRelease = distance / speed
     if attack != 'glowerpower':
         if self.suit.getSuit()[0].getSuitType() == SuitType.C:
             timeUntilRelease += 2.2
         else:
             timeUntilRelease += 3.0
     else:
         timeUntilRelease += 1.0
     hp = int(self.suit.getMaxHealth() / SuitAttacks.SuitAttackDamageFactors[attack])
     if self.suit.suitPlan.getName() == SuitGlobals.VicePresident:
         hp = int(200 / SuitAttacks.SuitAttackDamageFactors[attack])
     self.suitAttackTurretTrack = Sequence(Wait(timeUntilRelease), Func(self._damageTurret, hp))
     self.suitAttackTurretTrack.start()
     timeout = SuitAttacks.SuitAttackLengths[attack]
     task.delayTime = timeout
     return task.again
    def _attackTask(self, useSafeDistance, task):
        if not self.isAvatarReachable(self.target) or not self.isPlayerVisible(self.target, checkVisionAngle = False):
            self.resetNextFrame()
            return task.done

        if useSafeDistance:
            safeDistance = self.attackSafeDistance
        else:
            safeDistance = SuitPursueToonBehaviorAI.MaxNonSafeDistance

        if self.suit.getDistance(self.target) > safeDistance:
            # Nope, we're too far away! We need to chase them down!
            self.fsm.request('pursue')
            return task.done

        if self.target.isDead():
            # They've died, stop attacking
            self.resetNextFrame()
            return task.done

        attack = SuitUtils.attack(self.suit, self.target)
        timeout = SuitAttacks.SuitAttacks.attack2attackClass[attack].length

        task.delayTime = timeout
        return task.again
 def _attackTask(self, useSafeDistance, task):
     if not self.isAvatarReachable(self.target):
         return task.done
     if useSafeDistance:
         safeDistance = self.attackSafeDistance
     else:
         safeDistance = SuitPursueToonBehavior.MaxNonSafeDistance
     if self.suit.getDistance(self.target) > safeDistance:
         self.fsm.request('pursue')
         return task.done
     attack = SuitUtils.attack(self.suit, self.target)
     timeout = SuitAttacks.SuitAttackLengths[attack]
     task.delayTime = timeout
     return task.again
    def startAttacking(self, task=None):
        if hasattr(self.suit,
                   'DELETED') or not hasattr(self, 'attacksThisSession'):
            self.stopAttacking()
            if task:
                return Task.done
        # Do we need to reset the avatars in-range?
        if self.attacksThisSession > 0:
            self.resetAvatarsInRange()

        # Stop attacking if low on health or if there's nothing around to attack.
        if hasattr(self.suit, 'DELETED'):
            self.stopAttacking()
            return

        # If there's nobody to attack or we're taking heavy damage, let's abandon mission.
        brain = self.suit.getBrain()
        if len(self.avatarsInRange) < 1 or self.isTimeToPanic():
            self.stopAttacking()
            return

        # Let's check if we have a backup behavior if toons get out of control.
        from src.coginvasion.cog.SuitCallInBackupBehaviorAI import SuitCallInBackupBehaviorAI
        backupBehavior = brain.getBehavior(SuitCallInBackupBehaviorAI)

        if not backupBehavior is None:
            if backupBehavior.shouldStart():
                brain.queueBehavior(SuitCallInBackupBehaviorAI)
                self.stopAttacking()
                return

        # Let's select a target and look at them.
        target = self.avatarsInRange[0]
        attack = SuitUtils.attack(self.suit, target)

        # Let's handle when we're attacking a turret.
        if target.__class__.__name__ == 'DistributedPieTurretAI':
            distance = self.suit.getDistance(target)
            speed = 50.0

            if attack == 'glowerpower':
                speed = 100.0

            timeUntilRelease = distance / speed
            if attack != 'glowerpower':
                if self.suit.getSuit()[0].getSuitType() == SuitType.C:
                    timeUntilRelease += 2.2
                else:
                    timeUntilRelease += 3.0
            else:
                timeUntilRelease += 1.0
            turretPos = target.getPos(render)
            hp = int(self.suit.getMaxHealth() /
                     SuitAttacks.SuitAttackDamageFactors[attack])

            if self.suit.suitPlan.getName() == SuitGlobals.VicePresident:
                hp = int(200 / SuitAttacks.SuitAttackDamageFactors[attack])

            self.suitAttackTurretTrack = Sequence(
                Wait(timeUntilRelease),
                Func(self.damageTurret, target, turretPos, hp))
            self.suitAttackTurretTrack.start()

        self.isAttacking = True
        self.attacksThisSession += 1
        self.attacksDone += 1

        self.ATTACK_COOLDOWN = SuitAttacks.SuitAttackLengths[attack]

        # Are we allowed to continue attacking?
        if self.attacksThisSession < self.maxAttacksPerSession and not target.isDead(
        ):
            taskMgr.doMethodLater(self.ATTACK_COOLDOWN, self.startAttacking,
                                  self.suit.uniqueName('attackTask'))
        else:
            taskMgr.doMethodLater(self.ATTACK_COOLDOWN, self.stopAttacking,
                                  self.suit.uniqueName('finalAttack'))

        if task:
            return Task.done
Exemplo n.º 5
0
 def startAttacking(self, task=None):
     if hasattr(self.suit,
                'DELETED') or not hasattr(self, 'attacksThisSession'):
         self.stopAttacking()
         if task:
             return Task.done
     if self.attacksThisSession > 0:
         self.resetAvatarsInRange()
     if hasattr(self.suit, 'DELETED'):
         self.stopAttacking()
         return
     from lib.coginvasion.cog.SuitPanicBehavior import SuitPanicBehavior
     brain = self.suit.getBrain()
     panicBehavior = brain.getBehavior(SuitPanicBehavior)
     healthPerct = float(self.suit.getHealth()) / float(
         self.suit.getMaxHealth())
     origHealthPerct = float(self.origHealth) / float(
         self.suit.getMaxHealth())
     if len(
             self.avatarsInRange
     ) < 1 or panicBehavior and healthPerct <= panicBehavior.getPanicHealthPercentage(
     ) or healthPerct - origHealthPerct >= self.ABANDON_ATTACK_PERCT:
         self.stopAttacking()
         return
     from lib.coginvasion.cog.SuitCallInBackupBehavior import SuitCallInBackupBehavior
     backupBehavior = brain.getBehavior(SuitCallInBackupBehavior)
     if backupBehavior is not None:
         if backupBehavior.shouldStart():
             brain.queueBehavior(SuitCallInBackupBehavior)
             self.stopAttacking()
             return
     target = self.avatarsInRange[0]
     attack = SuitUtils.attack(self.suit, target)
     if target.__class__.__name__ == 'DistributedPieTurretAI':
         distance = self.suit.getDistance(target)
         speed = 50.0
         if attack == 'glowerpower':
             speed = 100.0
         timeUntilRelease = distance / speed
         if attack != 'glowerpower':
             if self.suit.getSuit()[0].getSuitType() == SuitType.C:
                 timeUntilRelease += 2.2
             else:
                 timeUntilRelease += 3.0
         else:
             timeUntilRelease += 1.0
         turretPos = target.getPos(render)
         hp = int(self.suit.getMaxHealth() /
                  SuitAttacks.SuitAttackDamageFactors[attack])
         if self.suit.suitPlan.getName() == SuitGlobals.VicePresident:
             hp = int(200 / SuitAttacks.SuitAttackDamageFactors[attack])
         self.suitAttackTurretTrack = Sequence(
             Wait(timeUntilRelease),
             Func(self.damageTurret, target, turretPos, hp))
         self.suitAttackTurretTrack.start()
     self.isAttacking = True
     self.attacksThisSession += 1
     self.attacksDone += 1
     self.ATTACK_COOLDOWN = SuitAttacks.SuitAttackLengths[attack]
     if self.attacksThisSession < self.maxAttacksPerSession and not target.isDead(
     ):
         taskMgr.doMethodLater(self.ATTACK_COOLDOWN, self.startAttacking,
                               self.suit.uniqueName('attackTask'))
     else:
         taskMgr.doMethodLater(self.ATTACK_COOLDOWN, self.stopAttacking,
                               self.suit.uniqueName('finalAttack'))
     if task:
         return Task.done
     return