class SniperAimingSystem(IAimingSystem): turretYaw = property(lambda self: self.__idealTurretYaw + self.__oscillator.deviation.x) gunPitch = property(lambda self: self.__idealGunPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) __activeSystem = None @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 if SniperAimingSystem.__activeSystem is not None: SniperAimingSystem.__activeSystem.resetIdealDirection() return @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) @staticmethod def enableFilter(enable): pass def __init__(self, dataSec): IAimingSystem.__init__(self) self.__idealTurretYaw = 0.0 self.__idealGunPitch = 0.0 self.__worldYaw = 0.0 self.__worldPitch = 0.0 self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2.0 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) self.__pitchCompensating = 0.0 return def destroy(self): IAimingSystem.destroy(self) SniperAimingSystem.__activeSystem = None return def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) self.__yawLimits = self.__vehicleTypeDescriptor.gun['turretYawLimits'] self.__pitchLimits = self.__vehicleTypeDescriptor.gun['pitchLimits'] self.__idealTurretYaw, self.__idealGunPitch = AimingSystems.getTurretYawGunPitch(self.__vehicleTypeDescriptor, player.getOwnVehicleMatrix(), targetPos, True) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealTurretYaw, self.__idealGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) self.__oscillator.reset() SniperAimingSystem.__activeSystem = self def disable(self): SniperAimingSystem.__activeSystem = None return def getDesiredShotPoint(self): start = self._matrix.translation scanDir = self._matrix.applyVector(Vector3(0.0, 0.0, 1.0)) return AimingSystems.getDesiredShotPoint(start, scanDir) def resetIdealDirection(self): self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) def handleMovement(self, dx, dy): self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) self.__pitchCompensating = 0.0 self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw + dx, self.__idealGunPitch + dy) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealTurretYaw, self.__idealGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__oscillator.velocity = Vector3(0.0, 0.0, 0.0) _, uncompensatedPitch = AimingSystems.getTurretYawGunPitch(self.__vehicleTypeDescriptor, BigWorld.player().getOwnVehicleMatrix(), self.getDesiredShotPoint()) self.__pitchCompensating = mathUtils.clamp(math.radians(-2.0), math.radians(2.0), self.__idealGunPitch - uncompensatedPitch) def __clampToLimits(self, turretYaw, gunPitch): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) pitchLimits = calcPitchLimitsFromDesc(turretYaw, self.__pitchLimits) gunPitch = mathUtils.clamp(pitchLimits[0], pitchLimits[1] + self.__pitchCompensating, gunPitch) return (turretYaw, gunPitch) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply(mathUtils.createRotationMatrix((worldYaw, worldPitch, 0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale(self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) vehicleMat = Matrix(self.__vehicleMProv) curTurretYaw, curGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) yprDelta = Vector3(curTurretYaw - self.__idealTurretYaw, curGunPitch - self.__idealGunPitch, 0.0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) curTurretYaw = self.__idealTurretYaw + self.__oscillator.deviation.x curGunPitch = self.__idealGunPitch + self.__oscillator.deviation.y curTurretYaw, curGunPitch = self.__clampToLimits(curTurretYaw, curGunPitch) yprDelta = Vector3(curTurretYaw - self.__idealTurretYaw, curGunPitch - self.__idealGunPitch, 0.0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(curTurretYaw, curGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__vehiclePrevMat = vehicleMat return 0.0
class SniperAimingSystem(IAimingSystem): turretYaw = property(lambda self: self.__idealYaw + self.__oscillator.deviation.x) gunPitch = property(lambda self: self.__idealPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) __activeSystem = None __FILTER_ENABLED = True @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 if SniperAimingSystem.__activeSystem is not None: SniperAimingSystem.__activeSystem.resetIdealDirection() return @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) @staticmethod def enableFilter(enable): SniperAimingSystem.__FILTER_ENABLED = enable def __init__(self, dataSec): IAimingSystem.__init__(self) self.__idealYaw = 0.0 self.__idealPitch = 0.0 self.__g_curPitch = 0.0 self.__g_curYaw = 0.0 self.__stailizationLimit = math.radians(60.0) self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2.0 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) self.__pitchfilter = InputFilter() self.reloadConfig(dataSec) self.__pitchCompensating = 0.0 return def reloadConfig(self, dataSec): filterSec = dataSec['aimingSystem'] self.__pitchDeviation = readVec2(filterSec, 'deviation', (-_MAX_DEVIATION_DEG, -_MAX_DEVIATION_DEG), (_MAX_DEVIATION_DEG, _MAX_DEVIATION_DEG), (0.0, 0.0)) self.__pitchDeviation = (-math.radians(self.__pitchDeviation[1]), -math.radians(self.__pitchDeviation[0])) self.__pitchfilter.reloadConfig(filterSec) def destroy(self): IAimingSystem.destroy(self) SniperAimingSystem.__activeSystem = None return def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleStabilisedMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) self.__yawLimits = self.__vehicleTypeDescriptor.gun['turretYawLimits'] self.__pitchLimits = self.__vehicleTypeDescriptor.gun['pitchLimits'] self.__idealYaw, self.__idealPitch = AimingSystems.getTurretYawGunPitch(self.__vehicleTypeDescriptor, self.__vehicleMProv, targetPos, True) self.__idealYaw, self.__idealPitch = self.__clampToLimits(self.__idealYaw, self.__idealPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, self.__idealPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits(self.__idealYaw, self.__idealPitch) self.__oscillator.reset() self.__pitchfilter.reset(currentGunMat.pitch) SniperAimingSystem.__activeSystem = self vehicle = player.getVehicleAttached() if vehicle is not None: if not vehicle.filter.placingOnGround: vehicle.filter.calcPlacedMatrix(True) self.__baseMatrix = vehicle.filter.placingMatrix else: self.__baseMatrix = vehicle.matrix return def disable(self): SniperAimingSystem.__activeSystem = None player = BigWorld.player() vehicle = player.getVehicleAttached() if vehicle is not None: if not vehicle.filter.placingOnGround: vehicle.filter.calcPlacedMatrix(False) return def getDesiredShotPoint(self): start = self._matrix.translation scanDir = self._matrix.applyVector(Vector3(0.0, 0.0, 1.0)) return AimingSystems.getDesiredShotPoint(start, scanDir) def resetIdealDirection(self): self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits(self.__idealYaw, self.__idealPitch) self.__pitchfilter.reset(self.__g_curPitch) def handleMovement(self, dx, dy): self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__pitchfilter.value()) newPitch = self.__idealPitch + dy newYaw = self.__idealYaw + dx self.__idealYaw, idealPitch, inLimit, pitchMin, dp = self.__inLimit(self.__idealYaw, newYaw, newPitch) newPitch += dp if not inLimit: d1 = pitchMin - self.__idealPitch d2 = pitchMin - newPitch if math.fabs(d1) >= math.fabs(d2): self.__idealPitch = idealPitch currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, self.__idealPitch) self.__pitchfilter.adjust(currentGunMat.pitch - self.__pitchfilter.value()) else: currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, idealPitch) self.__pitchfilter.reset(currentGunMat.pitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__oscillator.velocity = Vector3(0.0, 0.0, 0.0) _, uncompensatedPitch = AimingSystems.getTurretYawGunPitch(self.__vehicleTypeDescriptor, BigWorld.player().getOwnVehicleStabilisedMatrix(), self.getDesiredShotPoint()) if inLimit: self.__pitchCompensating = mathUtils.clamp(math.radians(-2.0), math.radians(2.0), idealPitch - uncompensatedPitch) else: self.__pitchCompensating = 0.0 def __clampToLimits(self, turretYaw, gunPitch): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) pitchLimits = calcPitchLimitsFromDesc(turretYaw, self.__pitchLimits) if SniperAimingSystem.__FILTER_ENABLED: pitchLimitsMin = min(pitchLimits[0] + self.__pitchDeviation[0], _MAX_DEVIATION) pitchLimitsMax = max(pitchLimits[1] + self.__pitchDeviation[1], -_MAX_DEVIATION) else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] gunPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax + self.__pitchCompensating, gunPitch) return (turretYaw, gunPitch) def __inLimit(self, prevYaw, newYaw, newPitch): if self.__yawLimits is not None: prevYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], prevYaw) newYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], newYaw) prevPitchLimits = calcPitchLimitsFromDesc(prevYaw, self.__pitchLimits) pitchLimits = calcPitchLimitsFromDesc(newYaw, self.__pitchLimits) if SniperAimingSystem.__FILTER_ENABLED: pitchLimitsMin = pitchLimits[0] + self.__pitchDeviation[0] pitchLimitsMax = pitchLimits[1] + self.__pitchDeviation[1] prevLimMin = prevPitchLimits[0] + self.__pitchDeviation[0] prevLimMax = prevPitchLimits[1] + self.__pitchDeviation[1] else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] prevLimMin = prevPitchLimits[0] prevLimMax = prevPitchLimits[1] prevLimitedPitch = mathUtils.clamp(prevLimMin, prevLimMax, newPitch) limitedPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, newPitch) dp = limitedPitch - prevLimitedPitch return (newYaw, limitedPitch, pitchLimitsMin <= newPitch <= pitchLimitsMax, pitchLimitsMin, dp) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply(mathUtils.createRotationMatrix((worldYaw, worldPitch, 0.0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale(self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) l_curYaw, l_curPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__g_curPitch) stabilizationOn = math.fabs(self._matrix.roll) < self.__stailizationLimit and SniperAimingSystem.__FILTER_ENABLED if stabilizationOn: l_curYaw, l_curNewPitch = self.__clampToLimits(l_curYaw, l_curPitch) else: l_curNewPitch = l_curPitch if stabilizationOn: newLocal = l_curPitch + (l_curNewPitch - l_curPitch) newGunMat = AimingSystems.getPlayerGunMat(l_curYaw, newLocal) new__g_curPitch = newGunMat.pitch new__g_curPitch = self.__pitchfilter.update(new__g_curPitch, deltaTime) globalDelta = new__g_curPitch - self.__g_curPitch else: globalDelta = l_curNewPitch - self.__idealPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) l_curYaw = self.__idealYaw + self.__oscillator.deviation.x if stabilizationOn: l_curPitch = l_curPitch + self.__oscillator.deviation.y else: l_curPitch = self.__idealPitch + self.__oscillator.deviation.y l_curYaw, l_newCurPitch = self.__clampToLimits(l_curYaw, l_curPitch) if not stabilizationOn: globalDelta = l_newCurPitch - self.__idealPitch l_curPitch = l_newCurPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(l_curYaw, l_curPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self._matrix.set(currentGunMat) return 0.0
class SniperAimingSystem(IAimingSystem): turretYaw = property( lambda self: self.__idealTurretYaw + self.__oscillator.deviation.x) gunPitch = property( lambda self: self.__idealGunPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) def __init__(self): IAimingSystem.__init__(self) self.__idealTurretYaw = 0.0 self.__idealGunPitch = 0.0 self.__worldYaw = 0.0 self.__worldPitch = 0.0 self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) return def destroy(self): IAimingSystem.destroy(self) def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) player = BigWorld.player() desc = player.vehicleTypeDescriptor self.__yawLimits = desc.gun['turretYawLimits'] self.__idealTurretYaw, self.__idealGunPitch = getShotAngles( desc, player.getOwnVehicleMatrix(), (0, 0), targetPos, False) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits( self.__idealTurretYaw, self.__idealGunPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealTurretYaw, self.__idealGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret( self.__worldYaw, self.__worldPitch) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits( self.__idealTurretYaw, self.__idealGunPitch) self.__oscillator.reset() def getDesiredShotPoint(self): start = self.matrix.translation scanDir = self.matrix.applyVector(Vector3(0, 0, 1)) return AimingSystems.getDesiredShotPoint(start, scanDir) def handleMovement(self, dx, dy): self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret( self.__worldYaw, self.__worldPitch) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits( self.__idealTurretYaw, self.__idealGunPitch) self.__idealTurretYaw += dx self.__idealGunPitch += dy self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits( self.__idealTurretYaw, self.__idealGunPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealTurretYaw, self.__idealGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__oscillator.velocity = Vector3(0, 0, 0) def __clampToLimits(self, turretYaw, gunPitch): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) desc = BigWorld.player().vehicleTypeDescriptor pitchLimits = calcPitchLimitsFromDesc(turretYaw, desc.gun['pitchLimits']) pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] gunPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, gunPitch) return (turretYaw, gunPitch) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply( mathUtils.createRotationMatrix((worldYaw, worldPitch, 0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale( self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) vehicleMat = Matrix(self.__vehicleMProv) curTurretYaw, curGunPitch = self.__worldYawPitchToTurret( self.__worldYaw, self.__worldPitch) yprDelta = Vector3(curTurretYaw - self.__idealTurretYaw, curGunPitch - self.__idealGunPitch, 0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) curTurretYaw = self.__idealTurretYaw + self.__oscillator.deviation.x curGunPitch = self.__idealGunPitch + self.__oscillator.deviation.y curTurretYaw, curGunPitch = self.__clampToLimits( curTurretYaw, curGunPitch) yprDelta = Vector3(curTurretYaw - self.__idealTurretYaw, curGunPitch - self.__idealGunPitch, 0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(curTurretYaw, curGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__vehiclePrevMat = vehicleMat return 0.0
class SniperAimingSystem(IAimingSystem): turretYaw = property(lambda self: self.__idealTurretYaw + self.__oscillator.deviation.x) gunPitch = property(lambda self: self.__idealGunPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) def __init__(self): IAimingSystem.__init__(self) self.__idealTurretYaw = 0.0 self.__idealGunPitch = 0.0 self.__worldYaw = 0.0 self.__worldPitch = 0.0 self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) return def destroy(self): IAimingSystem.destroy(self) def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) player = BigWorld.player() desc = player.vehicleTypeDescriptor self.__yawLimits = desc.gun['turretYawLimits'] self.__idealTurretYaw, self.__idealGunPitch = getShotAngles(desc, player.getOwnVehicleMatrix(), (0, 0), targetPos, False) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealTurretYaw, self.__idealGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) self.__oscillator.reset() def getDesiredShotPoint(self): start = self.matrix.translation scanDir = self.matrix.applyVector(Vector3(0, 0, 1)) return AimingSystems.getDesiredShotPoint(start, scanDir) def handleMovement(self, dx, dy): self.__idealTurretYaw, self.__idealGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) self.__idealTurretYaw += dx self.__idealGunPitch += dy self.__idealTurretYaw, self.__idealGunPitch = self.__clampToLimits(self.__idealTurretYaw, self.__idealGunPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealTurretYaw, self.__idealGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__oscillator.velocity = Vector3(0, 0, 0) def __clampToLimits(self, turretYaw, gunPitch): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) desc = BigWorld.player().vehicleTypeDescriptor pitchLimits = calcPitchLimitsFromDesc(turretYaw, desc.gun['pitchLimits']) pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] gunPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, gunPitch) return (turretYaw, gunPitch) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply(mathUtils.createRotationMatrix((worldYaw, worldPitch, 0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale(self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) vehicleMat = Matrix(self.__vehicleMProv) curTurretYaw, curGunPitch = self.__worldYawPitchToTurret(self.__worldYaw, self.__worldPitch) yprDelta = Vector3(curTurretYaw - self.__idealTurretYaw, curGunPitch - self.__idealGunPitch, 0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) curTurretYaw = self.__idealTurretYaw + self.__oscillator.deviation.x curGunPitch = self.__idealGunPitch + self.__oscillator.deviation.y curTurretYaw, curGunPitch = self.__clampToLimits(curTurretYaw, curGunPitch) yprDelta = Vector3(curTurretYaw - self.__idealTurretYaw, curGunPitch - self.__idealGunPitch, 0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(curTurretYaw, curGunPitch) self.__worldYaw = currentGunMat.yaw self.__worldPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__vehiclePrevMat = vehicleMat return 0.0
class SniperAimingSystem(IAimingSystem): turretYaw = property(lambda self: self.__idealYaw + self.__oscillator.deviation.x) gunPitch = property(lambda self: self.__idealPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) __activeSystem = None __FILTER_ENABLED = False @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 if SniperAimingSystem.__activeSystem is not None: SniperAimingSystem.__activeSystem.resetIdealDirection() SniperAimingSystem.enableFilter(useVerticalStabilizer) return @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) @staticmethod def enableFilter(enable): SniperAimingSystem.__FILTER_ENABLED = enable def __init__(self, dataSec): IAimingSystem.__init__(self) self.__idealYaw = 0.0 self.__idealPitch = 0.0 self.__g_curPitch = 0.0 self.__g_curYaw = 0.0 self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2.0 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) self.__dInputPitch = 0.0 self.__dInputYaw = 0.0 self.__pitchfilter = InputFilter() self.reloadConfig(dataSec) return def reloadConfig(self, dataSec): filterSec = dataSec['aimingSystem'] self.__pitchDeviation = readVec2(filterSec, 'deviation', (-_MAX_DEVIATION_DEG, -_MAX_DEVIATION_DEG), (_MAX_DEVIATION_DEG, _MAX_DEVIATION_DEG), (0.0, 0.0)) self.__pitchDeviation = (-math.radians(self.__pitchDeviation[1]), -math.radians(self.__pitchDeviation[0])) self.__pitchfilter.reloadConfig(filterSec) def destroy(self): IAimingSystem.destroy(self) SniperAimingSystem.__activeSystem = None return def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) self.__yawLimits = self.__vehicleTypeDescriptor.gun['turretYawLimits'] self.__pitchLimits = self.__vehicleTypeDescriptor.gun['pitchLimits'] self.__idealYaw, self.__idealPitch = getShotAngles(self.__vehicleTypeDescriptor, player.getOwnVehicleMatrix(), (0.0, 0.0), targetPos, False) self.__idealYaw, self.__idealPitch = self.__clampToLimits(self.__idealYaw, self.__idealPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, self.__idealPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits(self.__idealYaw, self.__idealPitch) self.__oscillator.reset() self.__pitchfilter.reset(currentGunMat.pitch) SniperAimingSystem.__activeSystem = self def disable(self): SniperAimingSystem.__activeSystem = None return def getDesiredShotPoint(self): start = self._matrix.translation scanDir = self._matrix.applyVector(Vector3(0.0, 0.0, 1.0)) return AimingSystems.getDesiredShotPoint(start, scanDir) def resetIdealDirection(self): self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits(self.__idealYaw, self.__idealPitch) self.__pitchfilter.reset(self.__g_curPitch) def handleMovement(self, dx, dy): self.__dInputYaw += dx self.__dInputPitch += dy def __clampToLimits(self, turretYaw, gunPitch, withDeviation = False): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) pitchLimits = calcPitchLimitsFromDesc(turretYaw, self.__pitchLimits) if withDeviation: pitchLimitsMin = min(pitchLimits[0] + self.__pitchDeviation[0], _MAX_DEVIATION) pitchLimitsMax = max(pitchLimits[1] + self.__pitchDeviation[1], -_MAX_DEVIATION) else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] gunPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, gunPitch) return (turretYaw, gunPitch) def __inLimit(self, yaw, pitch, withDeviation = False): if self.__yawLimits is not None: yaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], yaw) pitchLimits = calcPitchLimitsFromDesc(yaw, self.__pitchLimits) if withDeviation: pitchLimitsMin = pitchLimits[0] + self.__pitchDeviation[0] pitchLimitsMax = pitchLimits[1] + self.__pitchDeviation[1] else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] limitedPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, pitch) return (pitchLimitsMin <= pitch <= pitchLimitsMax, limitedPitch) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply(mathUtils.createRotationMatrix((worldYaw, worldPitch, 0.0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale(self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) l_curYaw, l_curPitch = self.__worldYawPitchToTurret(self.__g_curYaw, self.__g_curPitch) if self.__dInputYaw != 0.0 or self.__dInputPitch != 0.0: if SniperAimingSystem.__FILTER_ENABLED: wasInLimit, _ = self.__inLimit(l_curYaw, l_curPitch, True) self.__g_curYaw += self.__dInputYaw g_newPitch = self.__g_curPitch + self.__dInputPitch l_newYaw, l_newPitch = self.__worldYawPitchToTurret(self.__g_curYaw, g_newPitch) if self.__dInputYaw != 0.0: self.__idealYaw, l_limPitch = self.__clampToLimits(l_newYaw, l_newPitch, True) l_curYaw = self.__idealYaw if math.fabs(l_limPitch - l_newPitch) > 1e-05: self.__dInputPitch = l_limPitch - l_curPitch g_newPitch = self.__g_curPitch + self.__dInputPitch l_newPitch = l_limPitch self.__idealPitch = l_limPitch if self.__dInputPitch != 0.0: inLimit, limPitch = self.__inLimit(self.__idealYaw, l_newPitch, True) if not wasInLimit and not inLimit and math.fabs(l_newPitch) >= math.fabs(l_curPitch): self.__pitchfilter.resetTimer() else: if wasInLimit and not inLimit: self.__g_curPitch += limPitch - l_curPitch l_curPitch = self.__idealPitch = limPitch else: self.__g_curPitch = g_newPitch l_curPitch = self.__idealPitch = l_newPitch if inLimit: self.__pitchfilter.reset(self.__g_curPitch) else: self.__idealYaw, self.__idealPitch = self.__clampToLimits(l_curYaw + self.__dInputYaw, l_curPitch + self.__dInputPitch) l_curYaw = self.__idealYaw currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, self.__idealPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self.__oscillator.velocity = Vector3(0.0, 0.0, 0.0) self.__dInputYaw = 0.0 self.__dInputPitch = 0.0 l_curNewPitch = self.__idealPitch elif SniperAimingSystem.__FILTER_ENABLED: l_curYaw, l_curNewPitch = self.__clampToLimits(l_curYaw, l_curPitch) else: l_curNewPitch = l_curPitch if SniperAimingSystem.__FILTER_ENABLED: new__g_curPitch = self.__g_curPitch + (l_curNewPitch - l_curPitch) new__g_curPitch = self.__pitchfilter.update(new__g_curPitch, deltaTime) globalDelta = new__g_curPitch - self.__g_curPitch else: globalDelta = l_curNewPitch - self.__idealPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) l_curYaw = self.__idealYaw + self.__oscillator.deviation.x if SniperAimingSystem.__FILTER_ENABLED: l_curPitch = l_curPitch + self.__oscillator.deviation.y else: l_curPitch = self.__idealPitch + self.__oscillator.deviation.y l_curYaw, l_newCurPitch = self.__clampToLimits(l_curYaw, l_curPitch) if not SniperAimingSystem.__FILTER_ENABLED: globalDelta = l_newCurPitch - self.__idealPitch l_curPitch = l_newCurPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(l_curYaw, l_curPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self._matrix.set(currentGunMat) return 0.0
class SniperAimingSystem(IAimingSystem): turretYaw = property( lambda self: self.__idealYaw + self.__oscillator.deviation.x) gunPitch = property( lambda self: self.__idealPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) __activeSystem = None __FILTER_ENABLED = False @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 if SniperAimingSystem.__activeSystem is not None: SniperAimingSystem.__activeSystem.resetIdealDirection() SniperAimingSystem.enableFilter(useVerticalStabilizer) @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) @staticmethod def enableFilter(enable): SniperAimingSystem.__FILTER_ENABLED = enable def __init__(self, dataSec): IAimingSystem.__init__(self) self.__idealYaw = 0.0 self.__idealPitch = 0.0 self.__g_curPitch = 0.0 self.__g_curYaw = 0.0 self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2.0 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) self.__dInputPitch = 0.0 self.__dInputYaw = 0.0 self.__pitchfilter = InputFilter() self.reloadConfig(dataSec) def reloadConfig(self, dataSec): filterSec = dataSec['aimingSystem'] self.__pitchDeviation = readVec2( filterSec, 'deviation', (-_MAX_DEVIATION_DEG, -_MAX_DEVIATION_DEG), (_MAX_DEVIATION_DEG, _MAX_DEVIATION_DEG), (0.0, 0.0)) self.__pitchDeviation = (-math.radians(self.__pitchDeviation[1]), -math.radians(self.__pitchDeviation[0])) self.__pitchfilter.reloadConfig(filterSec) def destroy(self): IAimingSystem.destroy(self) SniperAimingSystem.__activeSystem = None def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) self.__yawLimits = self.__vehicleTypeDescriptor.gun['turretYawLimits'] self.__pitchLimits = self.__vehicleTypeDescriptor.gun['pitchLimits'] self.__idealYaw, self.__idealPitch = getShotAngles( self.__vehicleTypeDescriptor, player.getOwnVehicleMatrix(), (0.0, 0.0), targetPos, False) self.__idealYaw, self.__idealPitch = self.__clampToLimits( self.__idealYaw, self.__idealPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, self.__idealPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits( self.__idealYaw, self.__idealPitch) self.__oscillator.reset() self.__pitchfilter.reset(currentGunMat.pitch) SniperAimingSystem.__activeSystem = self def disable(self): SniperAimingSystem.__activeSystem = None def getDesiredShotPoint(self): start = self._matrix.translation scanDir = self._matrix.applyVector(Vector3(0.0, 0.0, 1.0)) return AimingSystems.getDesiredShotPoint(start, scanDir) def resetIdealDirection(self): self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits( self.__idealYaw, self.__idealPitch) self.__pitchfilter.reset(self.__g_curPitch) def handleMovement(self, dx, dy): self.__dInputYaw += dx self.__dInputPitch += dy def __clampToLimits(self, turretYaw, gunPitch, withDeviation=False): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) pitchLimits = calcPitchLimitsFromDesc(turretYaw, self.__pitchLimits) if withDeviation: pitchLimitsMin = min(pitchLimits[0] + self.__pitchDeviation[0], _MAX_DEVIATION) pitchLimitsMax = max(pitchLimits[1] + self.__pitchDeviation[1], -_MAX_DEVIATION) else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] gunPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, gunPitch) return (turretYaw, gunPitch) def __inLimit(self, yaw, pitch, withDeviation=False): if self.__yawLimits is not None: yaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], yaw) pitchLimits = calcPitchLimitsFromDesc(yaw, self.__pitchLimits) if withDeviation: pitchLimitsMin = pitchLimits[0] + self.__pitchDeviation[0] pitchLimitsMax = pitchLimits[1] + self.__pitchDeviation[1] else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] limitedPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, pitch) return (pitchLimitsMin <= pitch <= pitchLimitsMax, limitedPitch) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply( mathUtils.createRotationMatrix((worldYaw, worldPitch, 0.0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale( self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) l_curYaw, l_curPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__g_curPitch) if self.__dInputYaw != 0.0 or self.__dInputPitch != 0.0: if SniperAimingSystem.__FILTER_ENABLED: wasInLimit, _ = self.__inLimit(l_curYaw, l_curPitch, True) self.__g_curYaw += self.__dInputYaw g_newPitch = self.__g_curPitch + self.__dInputPitch l_newYaw, l_newPitch = self.__worldYawPitchToTurret( self.__g_curYaw, g_newPitch) if self.__dInputYaw != 0.0: self.__idealYaw, l_limPitch = self.__clampToLimits( l_newYaw, l_newPitch, True) l_curYaw = self.__idealYaw if math.fabs(l_limPitch - l_newPitch) > 1e-05: self.__dInputPitch = l_limPitch - l_curPitch g_newPitch = self.__g_curPitch + self.__dInputPitch l_newPitch = l_limPitch self.__idealPitch = l_limPitch if self.__dInputPitch != 0.0: inLimit, limPitch = self.__inLimit(self.__idealYaw, l_newPitch, True) if not wasInLimit and not inLimit and math.fabs( l_newPitch) >= math.fabs(l_curPitch): self.__pitchfilter.resetTimer() else: if wasInLimit and not inLimit: self.__g_curPitch += limPitch - l_curPitch l_curPitch = self.__idealPitch = limPitch else: self.__g_curPitch = g_newPitch l_curPitch = self.__idealPitch = l_newPitch if inLimit: self.__pitchfilter.reset(self.__g_curPitch) else: self.__idealYaw, self.__idealPitch = self.__clampToLimits( l_curYaw + self.__dInputYaw, l_curPitch + self.__dInputPitch) l_curYaw = self.__idealYaw currentGunMat = AimingSystems.getPlayerGunMat( self.__idealYaw, self.__idealPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self.__oscillator.velocity = Vector3(0.0, 0.0, 0.0) self.__dInputYaw = 0.0 self.__dInputPitch = 0.0 l_curNewPitch = self.__idealPitch elif SniperAimingSystem.__FILTER_ENABLED: l_curYaw, l_curNewPitch = self.__clampToLimits( l_curYaw, l_curPitch) else: l_curNewPitch = l_curPitch if SniperAimingSystem.__FILTER_ENABLED: new__g_curPitch = self.__g_curPitch + (l_curNewPitch - l_curPitch) new__g_curPitch = self.__pitchfilter.update( new__g_curPitch, deltaTime) globalDelta = new__g_curPitch - self.__g_curPitch else: globalDelta = l_curNewPitch - self.__idealPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) l_curYaw = self.__idealYaw + self.__oscillator.deviation.x if SniperAimingSystem.__FILTER_ENABLED: l_curPitch = l_curPitch + self.__oscillator.deviation.y else: l_curPitch = self.__idealPitch + self.__oscillator.deviation.y l_curYaw, l_newCurPitch = self.__clampToLimits(l_curYaw, l_curPitch) if not SniperAimingSystem.__FILTER_ENABLED: globalDelta = l_newCurPitch - self.__idealPitch l_curPitch = l_newCurPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(l_curYaw, l_curPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self._matrix.set(currentGunMat) return 0.0
class SniperAimingSystem(IAimingSystem): turretYaw = property( lambda self: self.__idealYaw + self.__oscillator.deviation.x) gunPitch = property( lambda self: self.__idealPitch + self.__oscillator.deviation.y) __CONSTRAINTS_MULTIPLIERS = Vector3(1.0, 1.0, 1.0) __activeSystem = None __FILTER_ENABLED = True @staticmethod def setStabilizerSettings(useHorizontalStabilizer, useVerticalStabilizer): SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x = 1.0 if useHorizontalStabilizer else 0.0 SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y = 1.0 if useVerticalStabilizer else 0.0 if SniperAimingSystem.__activeSystem is not None: SniperAimingSystem.__activeSystem.resetIdealDirection() return @staticmethod def getStabilizerSettings(): return (SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.x > 0.0, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS.y > 0.0) @staticmethod def enableFilter(enable): SniperAimingSystem.__FILTER_ENABLED = enable def __init__(self, dataSec): IAimingSystem.__init__(self) self.__idealYaw = 0.0 self.__idealPitch = 0.0 self.__g_curPitch = 0.0 self.__g_curYaw = 0.0 self.__stailizationLimit = math.radians(60.0) self.__vehicleTypeDescriptor = None self.__vehicleMProv = None self.__vehiclePrevMat = None self.__yprDeviationConstraints = Vector3(math.pi * 2.1, math.pi / 2.0 * 0.95, 0.0) self.__oscillator = Oscillator(1.0, Vector3(0.0, 0.0, 15.0), Vector3(0.0, 0.0, 3.5), self.__yprDeviationConstraints) self.__pitchfilter = InputFilter() self.reloadConfig(dataSec) self.__pitchCompensating = 0.0 return def reloadConfig(self, dataSec): filterSec = dataSec['aimingSystem'] self.__pitchDeviation = readVec2( filterSec, 'deviation', (-_MAX_DEVIATION_DEG, -_MAX_DEVIATION_DEG), (_MAX_DEVIATION_DEG, _MAX_DEVIATION_DEG), (0.0, 0.0)) self.__pitchDeviation = (-math.radians(self.__pitchDeviation[1]), -math.radians(self.__pitchDeviation[0])) self.__pitchfilter.reloadConfig(filterSec) def destroy(self): IAimingSystem.destroy(self) SniperAimingSystem.__activeSystem = None return def enableHorizontalStabilizerRuntime(self, enable): yawConstraint = math.pi * 2.1 if enable else 0.0 self.__yprDeviationConstraints.x = yawConstraint def enable(self, targetPos): player = BigWorld.player() self.__vehicleTypeDescriptor = player.vehicleTypeDescriptor self.__vehicleMProv = player.getOwnVehicleStabilisedMatrix() self.__vehiclePrevMat = Matrix(self.__vehicleMProv) IAimingSystem.enable(self, targetPos) self.__yawLimits = self.__vehicleTypeDescriptor.gun['turretYawLimits'] self.__pitchLimits = self.__vehicleTypeDescriptor.gun['pitchLimits'] self.__idealYaw, self.__idealPitch = AimingSystems.getTurretYawGunPitch( self.__vehicleTypeDescriptor, self.__vehicleMProv, targetPos, True) self.__idealYaw, self.__idealPitch = self.__clampToLimits( self.__idealYaw, self.__idealPitch) currentGunMat = AimingSystems.getPlayerGunMat(self.__idealYaw, self.__idealPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = (targetPos - currentGunMat.translation).pitch self._matrix.set(currentGunMat) self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits( self.__idealYaw, self.__idealPitch) self.__oscillator.reset() self.__pitchfilter.reset(currentGunMat.pitch) SniperAimingSystem.__activeSystem = self vehicle = player.getVehicleAttached() if vehicle is not None: if not vehicle.filter.placingOnGround: vehicle.filter.calcPlacedMatrix(True) self.__baseMatrix = vehicle.filter.placingMatrix else: self.__baseMatrix = vehicle.matrix return def disable(self): SniperAimingSystem.__activeSystem = None player = BigWorld.player() vehicle = player.getVehicleAttached() if vehicle is not None: if not vehicle.filter.placingOnGround: vehicle.filter.calcPlacedMatrix(False) return def getDesiredShotPoint(self): start = self._matrix.translation scanDir = self._matrix.applyVector(Vector3(0.0, 0.0, 1.0)) return AimingSystems.getDesiredShotPoint(start, scanDir) def resetIdealDirection(self): self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__g_curPitch) self.__idealYaw, self.__idealPitch = self.__clampToLimits( self.__idealYaw, self.__idealPitch) self.__pitchfilter.reset(self.__g_curPitch) def handleMovement(self, dx, dy): self.__idealYaw, self.__idealPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__pitchfilter.value()) newPitch = self.__idealPitch + dy newYaw = self.__idealYaw + dx self.__idealYaw, idealPitch, inLimit, pitchMin, dp = self.__inLimit( self.__idealYaw, newYaw, newPitch) newPitch += dp if not inLimit: d1 = pitchMin - self.__idealPitch d2 = pitchMin - newPitch if math.fabs(d1) >= math.fabs(d2): self.__idealPitch = idealPitch currentGunMat = AimingSystems.getPlayerGunMat( self.__idealYaw, self.__idealPitch) self.__pitchfilter.adjust(currentGunMat.pitch - self.__pitchfilter.value()) else: currentGunMat = AimingSystems.getPlayerGunMat( self.__idealYaw, idealPitch) self.__pitchfilter.reset(currentGunMat.pitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self._matrix.set(currentGunMat) self.__oscillator.velocity = Vector3(0.0, 0.0, 0.0) _, uncompensatedPitch = AimingSystems.getTurretYawGunPitch( self.__vehicleTypeDescriptor, BigWorld.player().getOwnVehicleStabilisedMatrix(), self.getDesiredShotPoint()) if inLimit: self.__pitchCompensating = mathUtils.clamp( math.radians(-2.0), math.radians(2.0), idealPitch - uncompensatedPitch) else: self.__pitchCompensating = 0.0 def __clampToLimits(self, turretYaw, gunPitch): if self.__yawLimits is not None: turretYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], turretYaw) pitchLimits = calcPitchLimitsFromDesc(turretYaw, self.__pitchLimits) if SniperAimingSystem.__FILTER_ENABLED: pitchLimitsMin = min(pitchLimits[0] + self.__pitchDeviation[0], _MAX_DEVIATION) pitchLimitsMax = max(pitchLimits[1] + self.__pitchDeviation[1], -_MAX_DEVIATION) else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] gunPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax + self.__pitchCompensating, gunPitch) return (turretYaw, gunPitch) def __inLimit(self, prevYaw, newYaw, newPitch): if self.__yawLimits is not None: prevYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], prevYaw) newYaw = mathUtils.clamp(self.__yawLimits[0], self.__yawLimits[1], newYaw) prevPitchLimits = calcPitchLimitsFromDesc(prevYaw, self.__pitchLimits) pitchLimits = calcPitchLimitsFromDesc(newYaw, self.__pitchLimits) if SniperAimingSystem.__FILTER_ENABLED: pitchLimitsMin = pitchLimits[0] + self.__pitchDeviation[0] pitchLimitsMax = pitchLimits[1] + self.__pitchDeviation[1] prevLimMin = prevPitchLimits[0] + self.__pitchDeviation[0] prevLimMax = prevPitchLimits[1] + self.__pitchDeviation[1] else: pitchLimitsMin = pitchLimits[0] pitchLimitsMax = pitchLimits[1] prevLimMin = prevPitchLimits[0] prevLimMax = prevPitchLimits[1] prevLimitedPitch = mathUtils.clamp(prevLimMin, prevLimMax, newPitch) limitedPitch = mathUtils.clamp(pitchLimitsMin, pitchLimitsMax, newPitch) dp = limitedPitch - prevLimitedPitch return (newYaw, limitedPitch, pitchLimitsMin <= newPitch <= pitchLimitsMax, pitchLimitsMin, dp) def __worldYawPitchToTurret(self, worldYaw, worldPitch): worldToTurret = Matrix(self.__vehicleMProv) worldToTurret.invert() worldToTurret.preMultiply( mathUtils.createRotationMatrix((worldYaw, worldPitch, 0.0))) return (worldToTurret.yaw, worldToTurret.pitch) def update(self, deltaTime): self.__oscillator.constraints = mathUtils.matrixScale( self.__yprDeviationConstraints, SniperAimingSystem.__CONSTRAINTS_MULTIPLIERS) l_curYaw, l_curPitch = self.__worldYawPitchToTurret( self.__g_curYaw, self.__g_curPitch) stabilizationOn = math.fabs( self._matrix.roll ) < self.__stailizationLimit and SniperAimingSystem.__FILTER_ENABLED if stabilizationOn: l_curYaw, l_curNewPitch = self.__clampToLimits( l_curYaw, l_curPitch) else: l_curNewPitch = l_curPitch if stabilizationOn: newLocal = l_curPitch + (l_curNewPitch - l_curPitch) newGunMat = AimingSystems.getPlayerGunMat(l_curYaw, newLocal) new__g_curPitch = newGunMat.pitch new__g_curPitch = self.__pitchfilter.update( new__g_curPitch, deltaTime) globalDelta = new__g_curPitch - self.__g_curPitch else: globalDelta = l_curNewPitch - self.__idealPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta self.__oscillator.update(deltaTime) l_curYaw = self.__idealYaw + self.__oscillator.deviation.x if stabilizationOn: l_curPitch = l_curPitch + self.__oscillator.deviation.y else: l_curPitch = self.__idealPitch + self.__oscillator.deviation.y l_curYaw, l_newCurPitch = self.__clampToLimits(l_curYaw, l_curPitch) if not stabilizationOn: globalDelta = l_newCurPitch - self.__idealPitch l_curPitch = l_newCurPitch yprDelta = Vector3(l_curYaw - self.__idealYaw, globalDelta, 0.0) self.__oscillator.deviation = yprDelta currentGunMat = AimingSystems.getPlayerGunMat(l_curYaw, l_curPitch) self.__g_curYaw = currentGunMat.yaw self.__g_curPitch = currentGunMat.pitch self._matrix.set(currentGunMat) return 0.0