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 __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 __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)
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
def __readCfg(self, dataSec): if dataSec is None: LOG_WARNING( 'Invalid section <arcadeMode/camera> in avatar_input_handler.xml' ) self.__baseCfg = dict() bcfg = self.__baseCfg bcfg['keySensitivity'] = readFloat(dataSec, 'keySensitivity', 0, 10, 0.01) bcfg['sensitivity'] = readFloat(dataSec, 'sensitivity', 0, 10, 0.01) bcfg['scrollSensitivity'] = readFloat(dataSec, 'scrollSensitivity', 0, 10, 0.01) bcfg['angleRange'] = readVec2(dataSec, 'angleRange', (0, 0), (180, 180), (10, 110)) distRangeVec = readVec2(dataSec, 'distRange', (1, 1), (100, 100), (2, 20)) bcfg['distRange'] = MinMax(distRangeVec.x, distRangeVec.y) bcfg['minStartDist'] = readFloat(dataSec, 'minStartDist', bcfg['distRange'][0], bcfg['distRange'][1], bcfg['distRange'][0]) bcfg['optimalStartDist'] = readFloat(dataSec, 'optimalStartDist', bcfg['distRange'][0], bcfg['distRange'][1], bcfg['distRange'][0]) bcfg['angleRange'][0] = math.radians( bcfg['angleRange'][0]) - math.pi * 0.5 bcfg['angleRange'][1] = math.radians( bcfg['angleRange'][1]) - math.pi * 0.5 bcfg['fovMultMinMaxDist'] = MinMax( readFloat(dataSec, 'fovMultMinDist', 0.1, 100, 1.0), readFloat(dataSec, 'fovMultMaxDist', 0.1, 100, 1.0)) ds = Settings.g_instance.userPrefs[Settings.KEY_CONTROL_MODE] if ds is not None: ds = ds['arcadeMode/camera'] self.__userCfg = dict() ucfg = self.__userCfg from account_helpers.settings_core.SettingsCore import g_settingsCore ucfg['horzInvert'] = g_settingsCore.getSetting('mouseHorzInvert') ucfg['vertInvert'] = g_settingsCore.getSetting('mouseVertInvert') ucfg['sniperModeByShift'] = g_settingsCore.getSetting( 'sniperModeByShift') ucfg['keySensitivity'] = readFloat(ds, 'keySensitivity', 0.0, 10.0, 1.0) ucfg['sensitivity'] = readFloat(ds, 'sensitivity', 0.0, 10.0, 1.0) ucfg['scrollSensitivity'] = readFloat(ds, 'scrollSensitivity', 0.0, 10.0, 1.0) ucfg['startDist'] = readFloat(ds, 'startDist', bcfg['distRange'][0], 500, bcfg['optimalStartDist']) if ucfg['startDist'] < bcfg['minStartDist']: ucfg['startDist'] = bcfg['optimalStartDist'] ucfg['startAngle'] = readFloat(ds, 'startAngle', 5, 180, 60) ucfg['startAngle'] = math.radians(ucfg['startAngle']) - math.pi * 0.5 ucfg['fovMultMinMaxDist'] = MinMax( readFloat(ds, 'fovMultMinDist', 0.1, 100, bcfg['fovMultMinMaxDist'].min), readFloat(ds, 'fovMultMaxDist', 0.1, 100, bcfg['fovMultMinMaxDist'].max)) self.__cfg = dict() cfg = self.__cfg cfg['keySensitivity'] = bcfg['keySensitivity'] cfg['sensitivity'] = bcfg['sensitivity'] cfg['scrollSensitivity'] = bcfg['scrollSensitivity'] cfg['angleRange'] = bcfg['angleRange'] cfg['distRange'] = bcfg['distRange'] cfg['minStartDist'] = bcfg['minStartDist'] cfg['horzInvert'] = ucfg['horzInvert'] cfg['vertInvert'] = ucfg['vertInvert'] cfg['keySensitivity'] *= ucfg['keySensitivity'] cfg['sensitivity'] *= ucfg['sensitivity'] cfg['scrollSensitivity'] *= ucfg['scrollSensitivity'] cfg['startDist'] = ucfg['startDist'] cfg['startAngle'] = ucfg['startAngle'] cfg['fovMultMinMaxDist'] = ucfg['fovMultMinMaxDist'] cfg['sniperModeByShift'] = ucfg['sniperModeByShift'] enableShift = dataSec.readBool('shift', False) if enableShift: movementMappings = dict() movementMappings[Keys.KEY_A] = Math.Vector3(-1, 0, 0) movementMappings[Keys.KEY_D] = Math.Vector3(1, 0, 0) movementMappings[Keys.KEY_Q] = Math.Vector3(0, 1, 0) movementMappings[Keys.KEY_E] = Math.Vector3(0, -1, 0) movementMappings[Keys.KEY_W] = Math.Vector3(0, 0, 1) movementMappings[Keys.KEY_S] = Math.Vector3(0, 0, -1) shiftSensitivity = dataSec.readFloat('shiftSensitivity', 0.5) self.__shiftKeySensor = KeySensor(movementMappings, shiftSensitivity) self.__shiftKeySensor.reset(Math.Vector3()) dynamicsSection = dataSec['dynamics'] self.__impulseOscillator = createOscillatorFromSection( dynamicsSection['impulseOscillator'], False) self.__movementOscillator = createOscillatorFromSection( dynamicsSection['movementOscillator'], False) self.__movementOscillator = CompoundOscillator( self.__movementOscillator, Oscillator(1.0, Vector3(50), Vector3(20), Vector3(0.01, 0.0, 0.01))) self.__noiseOscillator = createOscillatorFromSection( dynamicsSection['randomNoiseOscillatorSpherical']) self.__dynamicCfg.readImpulsesConfig(dynamicsSection) self.__dynamicCfg['accelerationSensitivity'] = readFloat( dynamicsSection, 'accelerationSensitivity', -1000, 1000, 0.1) self.__dynamicCfg['frontImpulseToPitchRatio'] = math.radians( readFloat(dynamicsSection, 'frontImpulseToPitchRatio', -1000, 1000, 0.1)) self.__dynamicCfg['sideImpulseToRollRatio'] = math.radians( readFloat(dynamicsSection, 'sideImpulseToRollRatio', -1000, 1000, 0.1)) self.__dynamicCfg['sideImpulseToYawRatio'] = math.radians( readFloat(dynamicsSection, 'sideImpulseToYawRatio', -1000, 1000, 0.1)) accelerationThreshold = readFloat(dynamicsSection, 'accelerationThreshold', 0.0, 1000.0, 0.1) self.__dynamicCfg['accelerationThreshold'] = accelerationThreshold self.__dynamicCfg['accelerationMax'] = readFloat( dynamicsSection, 'accelerationMax', 0.0, 1000.0, 0.1) self.__dynamicCfg['maxShotImpulseDistance'] = readFloat( dynamicsSection, 'maxShotImpulseDistance', 0.0, 1000.0, 10.0) self.__dynamicCfg['maxExplosionImpulseDistance'] = readFloat( dynamicsSection, 'maxExplosionImpulseDistance', 0.0, 1000.0, 10.0) self.__dynamicCfg['zoomExposure'] = readFloat(dynamicsSection, 'zoomExposure', 0.0, 1000.0, 0.25) accelerationFilter = mathUtils.RangeFilter( self.__dynamicCfg['accelerationThreshold'], self.__dynamicCfg['accelerationMax'], 100, mathUtils.SMAFilter(ArcadeCamera._FILTER_LENGTH)) maxAccelerationDuration = readFloat( dynamicsSection, 'maxAccelerationDuration', 0.0, 10000.0, ArcadeCamera._DEFAULT_MAX_ACCELERATION_DURATION) self.__accelerationSmoother = AccelerationSmoother( accelerationFilter, maxAccelerationDuration) self.__inputInertia = _InputInertia(self.__cfg['fovMultMinMaxDist'], 0.0) advancedCollision = dataSec['advancedCollision'] if advancedCollision is None: LOG_ERROR('<advancedCollision> dataSection is not found!') else: BigWorld.setAdvancedColliderConstants( AdvancedColliderConstants.fromSection(advancedCollision), cfg['distRange'][0]) return
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.__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