Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350 00351 00352 00353 00354 00355 00356 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 |
/** WVPersesMAS.PersesHomingMissile Creation date: 2013-12-12 12:52 Last change: $Id$ Copyright (c) 2013, Wormbo */ class PersesHomingMissile extends PersesProjectileBase; //============================================================================= // Properties //============================================================================= var() float HomingExtrapolationMaxTime; var() float HomingExtrapolationMaxError; var() float HomingAnglePerSecond; var() float HomingPredictionTimeFactor; var() float HomingPredictionMaxTime; var() float HomingCheckInterval; var() float HomingMaxAimAngle; var() bool bAutoHoming; var() float AutoHomingViewRange; var() float AutoHomingViewAngle; var() float InitialHomingViewRange; var() float InitialHomingViewAngle; var() float LateralDampenFactor; //============================================================================= // Variables //============================================================================= var Actor HomingTarget; var bool bNoHomingTarget; // fallback data in case HomingTarget is not relevant: var vector HomingLocation, HomingVelocity; var float HomingPositionTime; var vector AlternateTargetOffset; var float LastTargetCheckTime; var struct THomingPosition { var vector Location; var vector Velocity; } RepHomingPosition, OldHomingPosition; //============================================================================= // Replication //============================================================================= replication { reliable if (bNetDirty) HomingTarget, bNoHomingTarget; reliable if (bNetDirty && !bNoHomingTarget) RepHomingPosition, AlternateTargetOffset; } /* function PostBeginPlay() { LastTargetCheckTime = Level.TimeSeconds; SetHomingTarget(PickTarget(InitialHomingViewAngle, InitialHomingViewRange, vector(Rotation))); } */ function Init(vector Direction) { Super.Init(Direction); LastTargetCheckTime = Level.TimeSeconds; SetHomingTarget(PickTarget(InitialHomingViewAngle, InitialHomingViewRange, vector(Rotation))); } function SetHomingTarget(Actor NewTarget) { if (HomingTarget != NewTarget) { if (Vehicle(HomingTarget) != None) Vehicle(HomingTarget).NotifyEnemyLostLock(); HomingTarget = NewTarget; if (Vehicle(HomingTarget) != None) Vehicle(HomingTarget).NotifyEnemyLockedOn(); bNoHomingTarget = (NewTarget == None); if (HomingTarget != None) AlternateTargetOffset = vect(0,0,1) * HomingTarget.CollisionHeight; NetUpdateTime = Level.TimeSeconds - 1; } } simulated event PostNetReceive() { if (OldHomingPosition != RepHomingPosition) { OldHomingPosition = RepHomingPosition; HomingPositionTime = Level.TimeSeconds; } } simulated event ShutDown() { SetHomingTarget(None); Super.ShutDown(); } simulated event Destroyed() { SetHomingTarget(None); Super.Destroyed(); } simulated function UpdateHomingPosition() { local THomingPosition NewPosition; if (HomingTarget != None) { if (Role == ROLE_Authority && (Level.TimeSeconds - HomingPositionTime > HomingExtrapolationMaxTime || VSize(RepHomingPosition.Location + (Level.TimeSeconds - HomingPositionTime) * RepHomingPosition.Velocity - HomingTarget.Location) > HomingExtrapolationMaxError)) { NewPosition.Location = RoundVector(HomingTarget.Location); NewPosition.Velocity = RoundVector(HomingTarget.Velocity); RepHomingPosition = NewPosition; } HomingLocation = HomingTarget.Location; HomingVelocity = HomingTarget.Velocity; HomingPositionTime = Level.TimeSeconds; } else if (Role == ROLE_Authority && HomingLocation != vect(0,0,0)) { HomingLocation = vect(0,0,0); HomingVelocity = vect(0,0,0); HomingPositionTime = Level.TimeSeconds; RepHomingPosition = NewPosition; } else if (Role < ROLE_Authority) { HomingVelocity = RepHomingPosition.Velocity; HomingLocation = RepHomingPosition.Location + (Level.TimeSeconds - HomingPositionTime) * HomingVelocity; } } simulated event Tick(float DeltaTime) { local vector Dir; Super.Tick(DeltaTime); if (bShuttingDown) return; if (Role == ROLE_Authority && Level.TimeSeconds - LastTargetCheckTime > HomingCheckInterval) { LastTargetCheckTime = Level.TimeSeconds; if (bAutoHoming) PickNewHomingTarget(); else if (!bNoHomingTarget && !IsValidTarget(HomingTarget)) SetHomingTarget(None); if (Pawn(HomingTarget) != None && Pawn(HomingTarget).Controller != None) Pawn(HomingTarget).Controller.ReceiveProjectileWarning(self); } if (!bNoHomingTarget) { UpdateHomingPosition(); UpdateRotation(DeltaTime); } Dir = vector(Rotation); Acceleration = AccelRate * Dir; if (Normal(Velocity) dot Dir ~= 1.0) return; Acceleration -= LateralDampenFactor * (Velocity - Dir * (Velocity dot Dir)); } simulated function bool UpdateRotation(float DeltaTime) { local float a, b, c, d; local float t1, t2, t; local vector TargetLocation, TargetDir; local vector X, Y, Z; local float TargetAngle; if (vector(Rotation) dot Normal(HomingLocation - Location) < HomingMaxAimAngle) return false; // can't see target anymore if (HomingPredictionMaxTime > 0 && VSize(HomingVelocity) > 0) { // calculate required target leading // upper bound: time towards stationary target t = FMin(VSize(Location - HomingLocation) / VSize(Velocity), HomingPredictionMaxTime); /* Assume linear movement of projectile and homing target, but assume unknown direction for projectile movement. For calculating the ideal interception point, the target is a moving point, while the rocket is an expanding sphere. The code below calculates the smallest t > 0 for which the moving point touches the sphere's surface, which is the time to interception on an ideal course. This boils down to a quadratic equation: a * t**2 + b * t + c == 0 The parameters a, b and c are: a = |tVel|**2 - pSpeed**2 b = (tLoc - pLoc) * tVel c = |tLoc - pLoc|**2 pSpeed: projectile speed (scalar, assume MaxSpeed) tVel: target velocity (vector, HomingVelocity) pLoc: projectile location (vector, Location) tLoc: target location (vector, HomingLocation) */ TargetDir = HomingLocation - Location; a = HomingVelocity dot HomingVelocity - Square(MaxSpeed); b = TargetDir dot HomingVelocity; c = TargetDir dot TargetDir; d = Square(b) - 4*a*c; if (d > 0) // otherwise can't reach target { t1 = -0.5 * (b - Sqrt(d)) / a; t2 = -0.5 * (b + Sqrt(d)) / a; if (t1 > 0 && t2 > t1) { t = FMin(t1 * HomingPredictionTimeFactor, t); } else if (t2 > 0 && t1 > t2) { t = FMin(t2 * HomingPredictionTimeFactor, t); } } } TargetLocation = HomingLocation + t * HomingVelocity; if (!FastTrace(TargetLocation, Location)) { // computed target location isn't visible, i.e. would likely crash into wall first TargetLocation = HomingLocation; if (!FastTrace(TargetLocation, Location)) { // target not visible directly either, can we see at least the top? TargetLocation += AlternateTargetOffset; if (AlternateTargetOffset != vect(0,0,0) && !FastTrace(TargetLocation, Location)) return false; // I give up :/ } } //TargetDir = Normal(Normal(TargetLocation - Location) - 0.5 * Normal(Velocity)); TargetDir = Normal(TargetLocation - Location); // now try rotating towards target direction // The current direction (X) and the target direction span a plane. Based // on that, a rotation axis (Z) perpendicular that plane is calculated. // A helper vector (Y) in the same plane as the current and target // directions is calculated, such that it is perpendicular to the current // direction and the rotation axis and that the shortest rotation direction // from the current direction to the target direction is the same as the // shortest rotation direction from the current direction to the helper // vector. X = vector(Rotation); if (X dot TargetDir ~= 1.0 && Normal(Velocity) dot TargetDir ~= 1.0) return true; // already facing target direction Z = Normal(X cross TargetDir); Y = Z cross X; // now calculate the rotation angle TargetAngle = FMin(DeltaTime * HomingAnglePerSecond, ACos(X dot TargetDir)); // finally, calculate the new target direction and apply it TargetDir = X * Cos(TargetAngle) + Y * Sin(TargetAngle); SetRotation(rotator(TargetDir)); return true; } function PickNewHomingTarget() { local Actor NewTarget; local float BestRating, Rating; local bool bTargetVisible; local vector TargetLocation; if (IsValidTarget(HomingTarget)) { TargetLocation = HomingTarget.Location; if (FastTrace(TargetLocation, Location)) { bTargetVisible = True; BestRating = RateTargetLocation(HomingTarget.Location, vector(Rotation)); } TargetLocation = HomingTarget.Location + vect(0,0,1) * HomingTarget.CollisionHeight; if (bTargetVisible || FastTrace(TargetLocation, Location)) { Rating = RateTargetLocation(TargetLocation, vector(Rotation)); if (bTargetVisible) BestRating = FMin(BestRating, Rating); else BestRating = Rating; bTargetVisible = True; } TargetLocation = HomingTarget.Location - vect(0,0,1) * HomingTarget.CollisionHeight; if (bTargetVisible || FastTrace(TargetLocation, Location)) { Rating = RateTargetLocation(TargetLocation, vector(Rotation)); if (bTargetVisible) BestRating = FMin(BestRating, Rating); else BestRating = Rating; bTargetVisible = True; } } NewTarget = PickTarget(AutoHomingViewAngle, AutoHomingViewRange, GetCheckDir(), Rating); if (!bTargetVisible || NewTarget != None && Rating < BestRating) { SetHomingTarget(NewTarget); } } //============================================================================= // Default values //============================================================================= defaultproperties { ProjectileName = "Homing Missile" bAutoInit = True LifeSpan = 6.0 Damage = 75.0 DamageRadius = 220.0 SplashMomentum = 50000.0 TransferDamageAmount = 0.004 MomentumTransfer = 4.0 MyDamageType = class'DamTypePersesHomingHit' SplashDamageType = class'DamTypePersesHomingSplash' ExplosionDecal = class'RocketMark' Mass = 3.0 Speed = 2000 MaxSpeed = 3500 AccelRate = 5000 bAutoHoming = True FlightParticleSystem = class'PersesHomingMissileFlightEffects' ExplosionParticleSystem = class'NewExplosionA' ExplosionSound = Sound'BExplosion3' AmbientSound = Sound'RocketLauncherProjectile' TransientSoundVolume = 1.0 TransientSoundRadius = 500.0 bNetTemporary = False bUpdateSimulatedPosition = True bNetNotify = True NetPriority = 2.65 // slightly higher than other projectiles and mines HomingExtrapolationMaxTime = 1.0 HomingExtrapolationMaxError = 10.0 HomingPredictionTimeFactor = 0.5 HomingPredictionMaxTime = 1.0 HomingAnglePerSecond = 5.0 HomingCheckInterval = 0.1 HomingMaxAimAngle = 0.1 AutoHomingViewRange = 3000.0 AutoHomingViewAngle = 0.5 InitialHomingViewRange = 10000.0 InitialHomingViewAngle = 0.85 LateralDampenFactor = 0.5 bNoHomingTarget = True // until explicitly set } |
Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |