Ejemplo n.º 1
0
    def getPositionIndex(self, unit, pos):
        # TODO: maybe calculate from the middle, not the corner
        pt1x = self.pt1[0] - self._unitCornerOffset[0] * unit.HALF_WIDTH
        pt1y = self.pt1[1] - self._unitCornerOffset[1] * unit.HALF_HEIGHT

        dist = math.distance((pt1x, pt1y), pos)
        fullDist = math.distance((0, 0), self.deltaPt)
        midPoint = 0.5 * fullDist
        return int(floor((dist - midPoint) / D_UNIT + 0.5))
Ejemplo n.º 2
0
    def isRouteGoodEnough(self, route):
        if route.finishPoint is None:
            # Route isn't yet sure where it will end
            return True

        # Don't recalculate the route if we're still far from the target
        target = self.getTargetPos()
        error = distance(route.finishPoint, target)
        total = distance(self.bot.player.pos, target)
        if error / total >= 0.2:
            return False
        return True
Ejemplo n.º 3
0
    def advance(self):
        try:
            onlyGrav, xAcc, yAcc = self.getAcceleration()
            if self.stopped and onlyGrav:
                return
            self.stopped = False

            deltaT = self.world.tickPeriod
            self.xVel += xAcc * deltaT
            self.yVel += yAcc * deltaT

            deltaX = self.xVel * deltaT
            deltaY = self.yVel * deltaT

            oldX, oldY = self.pos
            obstacle = self.world.physics.moveUnit(self, deltaX, deltaY)
            if distance(self.pos, (oldX, oldY)) < self.stopToleranceDistance:
                self.stationaryTicks += 1
                if self.stationaryTicks > self.stopToleranceTicks:
                    self.xVel = self.yVel = 0
                    self.stopped = True
                    return
            else:
                self.stationaryTicks = 0

            if obstacle is not None:
                self.performRebound(obstacle)

        except Exception:
            log.exception('Error advancing {}'.format(self.__class__.__name__))
Ejemplo n.º 4
0
    def advance(self):
        if self.stopped:
            return

        deltaT = self.world.tickPeriod

        try:
            delta = (self.xVel * deltaT, self.yVel * deltaT)

            oldPos = self.pos
            self.pos, collision = self.world.physics.getMotion(
                self, delta, ignoreLedges=self.ignoreLedges)
            if distance(self.pos, oldPos) < self.stopToleranceDistance:
                self.stationaryTicks += 1
                if self.stationaryTicks > self.stopToleranceTicks:
                    self.stopped = True
                    self.xVel = self.yVel = 0
                    return
            else:
                self.stationaryTicks = 0

            if collision:
                self.performRebound(collision)

            vFinal = self.yVel + self.getGravity() * deltaT
            if vFinal > self.getMaxFallVel():
                self.yVel = self.getMaxFallVel()
            else:
                self.yVel = vFinal

        except Exception:
            log.exception('Error advancing bouncy unit')
Ejemplo n.º 5
0
    def advance(self):
        try:
            onlyGrav, xAcc, yAcc = self.getAcceleration()
            if self.stopped and onlyGrav:
                return
            self.stopped = False

            deltaT = self.world.tickPeriod
            self.xVel += xAcc * deltaT
            self.yVel += yAcc * deltaT

            delta = (self.xVel * deltaT, self.yVel * deltaT)

            oldX, oldY = self.pos
            self.pos, collision = self.world.physics.getMotion(self, delta)
            if distance(self.pos, (oldX, oldY)) < self.stopToleranceDistance:
                self.stationaryTicks += 1
                if self.stationaryTicks > self.stopToleranceTicks:
                    self.xVel = self.yVel = 0
                    self.stopped = True
                    return
            else:
                self.stationaryTicks = 0

            if collision:
                self.performRebound(collision)

        except Exception:
            log.exception('Error advancing {}'.format(self.__class__.__name__))
Ejemplo n.º 6
0
    def reevaluate(self):
        if self.bot.player.dead:
            self.returnToParent()
            return

        zone = self.bot.player.getZone()
        if zone != self.zone:
            self.returnToParent()
            return

        playersInZone = set(p for p in zone.players if not p.dead)
        if playersInZone != self.playersInZone:
            self.returnToParent()
            return

        enemiesInZone = [
            p for p in playersInZone if not p.isFriendsWith(self.bot.player)
        ]
        if not enemiesInZone:
            self.returnToParent()
            return

        target = min(enemiesInZone,
                     key=lambda p: distance(p.pos, self.bot.player.pos))
        self.bot.attackPlayer(target)
Ejemplo n.º 7
0
 def unitsHaveAdvanced(self):
     for team in self.world.teams:
         distance = math.distance(
             self.world.trosballManager.getPosition(),
             self.world.trosballManager.getTargetZoneDefn(team).pos)
         if distance < ZONE_CAP_DISTANCE:
             self.onTrosballScore(
                 team, self.world.trosballManager.lastTrosballPlayer)
Ejemplo n.º 8
0
 def checkOrderCompletion(self, player):
     playerZone = player.getZone()
     if self.stopOnEntry:
         if playerZone == self.zone and player.attachedObstacle:
             return True
     elif distance(player.pos, self.zone.defn.pos) < ZONE_CAP_DISTANCE:
         return True
     return False
Ejemplo n.º 9
0
    def reevaluate(self):
        if self.bot.player.dead:
            self.returnToParent()
            return

        playerPos = self.bot.player.pos
        targetZone = max(self.bot.world.zones,
                         key=lambda z: distance(z.defn.pos, playerPos))
        self.bot.moveToOrb(targetZone)
Ejemplo n.º 10
0
    def moveToFriendlyZone(self):
        zones = [
            z for z in self.world.zones if self.player.isZoneRespawnable(z)
        ]
        if not zones:
            self.world.callLater(3, self.orderFinished)
            return

        playerPos = self.player.pos
        bestZone = min(zones, key=lambda z: distance(z.defn.pos, playerPos))
        self.moveToZone(bestZone)
Ejemplo n.º 11
0
    def getPositionFromIndex(self, unit, posIndex):
        pt1x = self.pt1[0] - self._unitCornerOffset[0] * unit.HALF_WIDTH
        pt1y = self.pt1[1] - self._unitCornerOffset[1] * unit.HALF_HEIGHT
        fullDist = math.distance((0, 0), self.deltaPt)
        midPoint = 0.5 * fullDist

        u = midPoint + posIndex * D_UNIT
        f = min(0.999, max(0.001, u / fullDist))
        x = pt1x + f * self.deltaPt[0]
        y = pt1y + f * self.deltaPt[1]
        return (x, y)
Ejemplo n.º 12
0
    def _reAim(self):
        now = self.bot.world.getMonotonicTime()
        dist = distance(self.bot.player.pos, self.zone.defn.pos)

        if dist < 100:
            if self.bot.player.ghostThrust:
                self.bot.sendRequest(
                    AimPlayerAtMsg(0, 0, self.bot.world.lastTickId))
        elif dist < 300 or self.nextCheckTime < now:
            playerPos = self.bot.player.pos
            zonePos = self.zone.defn.pos
            dx = zonePos[0] - playerPos[0]
            dy = zonePos[1] - playerPos[1]
            theta = atan2(dx, -dy)
            thrust = min(1, distance(playerPos, zonePos) / 300.)

            self.bot.sendRequest(
                AimPlayerAtMsg(theta, thrust, self.bot.world.lastTickId))

            now = self.bot.world.getMonotonicTime()
            self.nextCheckTime = now + 0.5
Ejemplo n.º 13
0
    def tick(self):
        if self.bot.player.ghostThrust == 0:
            self._doAim()

        if self.stopOnZoneEntry:
            if self.bot.player.getZone() == self.stopOnZoneEntry:
                self.bot.orderFinished()
                return
        else:
            if distance(self.bot.player.pos, self.pos) < ZONE_CAP_DISTANCE:
                self.bot.orderFinished()
                return
Ejemplo n.º 14
0
    def startMoving(self):
        enemies = [
            p for p in self.world.players
            if not (p.dead or self.player.isFriendsWith(p))
        ]
        playerPos = self.player.pos

        if enemies:
            nearestEnemy = min(enemies,
                               key=lambda p: distance(p.pos, playerPos))
            self.attackPlayer(nearestEnemy)
        else:
            zones = [
                z for z in self.world.zones if z.owner != self.player.team
                and z.adjacentToAnotherZoneOwnedBy(self.player.team)
            ]
            if zones:
                nearestZone = min(
                    zones, key=lambda z: distance(z.defn.pos, playerPos))
                self.moveToOrb(nearestZone)
            else:
                self.world.callLater(3, self.orderFinished)
Ejemplo n.º 15
0
    def select_target(self, potentials):
        my_pos = self.bot.player.pos
        scoreboard = self.bot.world.scoreboard
        best_score = -1
        best_target = None
        for player in potentials:
            d = distance(my_pos, player.pos)
            if d == 0:
                return player

            # Select player based on (score ** 2) / distance
            score = scoreboard.playerScores.get(player, 0)**2 / d
            if score > best_score:
                best_score = score
                best_target = player
        return best_target
    def updatePlayerViewAngle(self):
        '''Updates the viewing angle of the player based on the mouse pointer
        being at the position pos. This gets its own method because it needs
        to happen as the result of a mouse motion and of the viewManager
        scrolling the screen.'''
        if self.world.uiOptions.showPauseMessage:
            return

        if self.player and self.player.dead and \
                self.gameInterface.detailsInterface.trajectoryOverlay.enabled:

            # Move ghost towards the orb
            pos0 = self.player.pos
            zone = self.player.getZone()
            if zone is None:
                theta = 0
                dist = 0
            else:
                pos1 = zone.defn.pos
                theta = atan2(pos1[0] - pos0[0], -(pos1[1] - pos0[1]))
                dist = distance(pos0, pos1) * MAP_TO_SCREEN_SCALE
        else:
            dx, dy = self.mousePos
            pos = (self.playerSprite.rect.center[0] + dx,
                   self.playerSprite.rect.center[1] + dy)

            if self.playerSprite.rect.collidepoint(pos):
                return

            # Angle is measured clockwise from vertical.
            theta = atan2(dx, -dy)
            dist = (dx**2 + dy**2)**0.5

        # Calculate a thrust value based on distance.
        if dist < self.NO_GHOST_THRUST_MOUSE_RADIUS:
            thrust = 0.0
        elif dist > self.FULL_GHOST_THRUST_MOUSE_RADIUS:
            thrust = 1.0
        else:
            span = (self.FULL_GHOST_THRUST_MOUSE_RADIUS -
                    self.NO_GHOST_THRUST_MOUSE_RADIUS)
            thrust = (dist - self.NO_GHOST_THRUST_MOUSE_RADIUS) / span
            thrust **= 2

        self.gameInterface.sendRequest(
            AimPlayerAtMsg(theta, thrust, tickId=self.getTickId()))
Ejemplo n.º 17
0
 def getAcceleration(self):
     minDist = None
     closest = None
     livePlayers = [p for p in self.world.players if not p.dead]
     if livePlayers:
         minDist, discard, player = min(
             (distance(p.pos, self.pos), p.id, p) for p in livePlayers)
         if 0 < minDist < self.attractiveRadius:
             xr = (player.pos[0] - self.pos[0]) / minDist
             yr = (player.pos[1] - self.pos[1]) / minDist
             x = self.playerAttraction * xr
             if yr > 0:
                 y = self.gravity * yr
             else:
                 y = self.playerAttraction * yr
             return False, x, y
     return True, 0, self.gravity
Ejemplo n.º 18
0
    def canHitPlayer(self, target):
        physics = self.world.physics
        gunRange = physics.shotLifetime * physics.shotSpeed

        if target.dead or target.invisible or target.turret:
            return False

        if distance(self.player.pos, target.pos) < gunRange:
            # Check if we can shoot without hitting obstacles
            shot = self.player.createShot()
            deltaX = target.pos[0] - self.player.pos[0]
            deltaY = target.pos[1] - self.player.pos[1]
            obstacle, dX, dY = physics.trimPathToObstacle(
                shot, deltaX, deltaY, ())
            if obstacle is None:
                return True

        return False
Ejemplo n.º 19
0
    def canHitPlayer(self, target):
        physics = self.world.physics
        gunRange = physics.shotLifetime * physics.shotSpeed

        if target.dead or target.invisible:
            return False

        if distance(self.player.pos, target.pos) < gunRange:
            # Check if we can shoot without hitting obstacles
            shot = self.player.createShot()
            deltaX = target.pos[0] - self.player.pos[0]
            deltaY = target.pos[1] - self.player.pos[1]
            collision = physics.getCollision(shot, (deltaX, deltaY),
                                             ignoreLedges=True)
            if collision is None:
                return True

        return False
Ejemplo n.º 20
0
    def selectZone(self):
        if self.targetZone:
            self.world.sendServerCommand(
                ZoneStateMsg(self.targetZone.id, NEUTRAL_TEAM_ID, True))

        allZones = [z for z in self.world.zones if z.owner is None]
        options = [z for z in allZones if not z.players]
        if options:
            zone = random.choice(options)
        else:
            zone = min(
                allZones,
                key=lambda z: min(
                    distance(z.defn.pos, p.pos) for p in z.players))

        self.world.sendServerCommand(
            ZoneStateMsg(zone.id, self.targetTeamId, True))
        self.targetZone = zone
        return zone
Ejemplo n.º 21
0
    def popPosition(self, expectedPosition):
        if not self.positions:
            log.warning('No positions left to pop!')
            return

        self.popCount += 1
        pos = self.positions.pop(0)
        if distance(pos, expectedPosition) >= 0.5:
            log.warning('Bot simulation did not match actual position!')
            log.warning(
                'actual: %s   simulated: %s  (%s)', expectedPosition, pos,
                ', '.join(actionKind.__name__
                          for actionKind in self.actionKinds))

            raise InconsistencyDetected()

        if not self.positions:
            # Ensure we always have at least one position recorded
            self.getNextPosition()
Ejemplo n.º 22
0
 def playerIsWithinTaggingDistance(self, player):
     distance = math.distance(self.defn.pos, player.pos)
     return distance < ZONE_CAP_DISTANCE
Ejemplo n.º 23
0
 def distance(self, pos):
     return distance(self.gameViewer.viewManager.getTargetPoint(), pos)
Ejemplo n.º 24
0
 def playerIsWithinTaggingDistance(self, player):
     if player.team and not player.team.abilities.zoneCaps:
         return False
     distance = math.distance(self.defn.pos, player.pos)
     return distance < ZONE_CAP_DISTANCE
Ejemplo n.º 25
0
 def distanceFromCentre(self):
     zone = self.getZone()
     if not zone:
         return 2000
     return distance(zone.defn.pos, self.pos)