Remove legacy turret/muzzle positioning code.

This commit is contained in:
Paul Chote
2013-03-29 21:54:27 +13:00
parent 491468b84f
commit 5e74d3c54e
15 changed files with 31 additions and 193 deletions

View File

@@ -18,16 +18,8 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public enum CoordinateModel { Legacy, World };
public class Barrel
{
// Legacy coordinates
public PVecInt TurretSpaceOffset; // position in turret space
public PVecInt ScreenSpaceOffset; // screen-space hack to make things line up good.
public int Facing; // deviation from turret facing
// World coordinates
public WVec Offset;
public WAngle Yaw;
}
@@ -44,10 +36,6 @@ namespace OpenRA.Mods.RA
[Desc("Time (in frames) until the weapon can fire again.")]
public readonly int FireDelay = 0;
public readonly float LegacyRecoilRecovery = 0.2f;
public readonly int[] LegacyLocalOffset = { };
public CoordinateModel OffsetModel = CoordinateModel.Legacy;
[Desc("Muzzle position relative to turret or body. (forward, right, up) triples")]
public readonly WRange[] LocalOffset = {};
[Desc("Muzzle yaw relative to turret or body.")]
@@ -57,14 +45,7 @@ namespace OpenRA.Mods.RA
[Desc("Recoil recovery per-frame")]
public readonly WRange RecoilRecovery = new WRange(9);
public object Create(ActorInitializer init)
{
// Auto-detect coordinate type
if (LocalOffset.Length > 0 && OffsetModel == CoordinateModel.Legacy)
OffsetModel = CoordinateModel.World;
return new Armament(init.self, this);
}
public object Create(ActorInitializer init) { return new Armament(init.self, this); }
}
public class Armament : ITick
@@ -76,7 +57,6 @@ namespace OpenRA.Mods.RA
Lazy<ILocalCoordinatesModel> Coords;
public WRange Recoil;
public float LegacyRecoil { get; private set; }
public int FireDelay { get; private set; }
public int Burst { get; private set; }
@@ -91,27 +71,10 @@ namespace OpenRA.Mods.RA
Weapon = Rules.Weapons[info.Weapon.ToLowerInvariant()];
Burst = Weapon.Burst;
var barrels = new List<Barrel>();
if (Info.OffsetModel == CoordinateModel.Legacy)
{
for (var i = 0; i < info.LocalOffset.Length / 5; i++)
barrels.Add(new Barrel
{
TurretSpaceOffset = new PVecInt(info.LegacyLocalOffset[5 * i], info.LegacyLocalOffset[5 * i + 1]),
ScreenSpaceOffset = new PVecInt(info.LegacyLocalOffset[5 * i + 2], info.LegacyLocalOffset[5 * i + 3]),
Facing = info.LegacyLocalOffset[5 * i + 4],
});
// if no barrels specified, the default is "turret position; turret facing".
if (barrels.Count == 0)
barrels.Add(new Barrel { TurretSpaceOffset = PVecInt.Zero, ScreenSpaceOffset = PVecInt.Zero, Facing = 0 });
}
else
{
if (info.LocalOffset.Length % 3 != 0)
throw new InvalidOperationException("Invalid LocalOffset array length");
var barrels = new List<Barrel>();
for (var i = 0; i < info.LocalOffset.Length / 3; i++)
{
barrels.Add(new Barrel
@@ -120,9 +83,10 @@ namespace OpenRA.Mods.RA
Yaw = info.LocalYaw.Length > i ? info.LocalYaw[i] : WAngle.Zero
});
}
if (barrels.Count == 0)
barrels.Add(new Barrel { Offset = WVec.Zero, Yaw = WAngle.Zero });
}
Barrels = barrels.ToArray();
}
@@ -130,7 +94,6 @@ namespace OpenRA.Mods.RA
{
if (FireDelay > 0)
--FireDelay;
LegacyRecoil = Math.Max(0f, LegacyRecoil - Info.LegacyRecoilRecovery);
Recoil = new WRange(Math.Max(0, Recoil.Range - Info.RecoilRecovery.Range));
}
@@ -151,19 +114,10 @@ namespace OpenRA.Mods.RA
var barrel = Barrels[Burst % Barrels.Length];
var destMove = target.IsActor ? target.Actor.TraitOrDefault<IMove>() : null;
var legacyMuzzlePosition = self.CenterLocation + (PVecInt)MuzzlePxOffset(self, facing, barrel).ToInt2();
var legacyMuzzleAltitude = move != null ? move.Altitude : 0;
var legacyFacing = barrel.Facing + (Turret.Value != null ? Turret.Value.turretFacing :
facing != null ? facing.Facing : Util.GetFacing(target.CenterLocation - self.CenterLocation, 0));
if (Info.OffsetModel == CoordinateModel.World)
{
var muzzlePosition = self.CenterPosition + MuzzleOffset(self, barrel);
legacyMuzzlePosition = PPos.FromWPos(muzzlePosition);
legacyMuzzleAltitude = Game.CellSize*muzzlePosition.Z/1024;
legacyFacing = MuzzleOrientation(self, barrel).Yaw.Angle / 4;
}
var legacyMuzzlePosition = PPos.FromWPos(muzzlePosition);
var legacyMuzzleAltitude = Game.CellSize*muzzlePosition.Z/1024;
var legacyFacing = MuzzleOrientation(self, barrel).Yaw.Angle / 4;
var args = new ProjectileArgs
{
@@ -199,7 +153,6 @@ namespace OpenRA.Mods.RA
foreach (var na in self.TraitsImplementing<INotifyAttack>())
na.Attacking(self, target);
LegacyRecoil = Info.LegacyRecoil;
Recoil = Info.Recoil;
if (--Burst > 0)
@@ -221,48 +174,8 @@ namespace OpenRA.Mods.RA
public bool IsReloading { get { return FireDelay > 0; } }
PVecFloat GetUnitspaceBarrelOffset(Actor self, IFacing facing, Barrel b)
{
if (Turret.Value == null && facing == null)
return PVecFloat.Zero;
var turretFacing = Turret.Value != null ? Turret.Value.turretFacing : facing.Facing;
return (PVecFloat)Util.RotateVectorByFacing(b.TurretSpaceOffset.ToFloat2(), turretFacing, .7f);
}
// Note: facing is only used by the legacy positioning code
public PVecFloat MuzzlePxOffset(Actor self, IFacing facing, Barrel b)
{
// Hack for external code unaware of world coordinates
if (Info.OffsetModel == CoordinateModel.World)
return (PVecFloat)PPos.FromWPosHackZ(WPos.Zero + MuzzleOffset(self, b)).ToFloat2();
PVecFloat pos = b.ScreenSpaceOffset;
// local facing offset doesn't make sense for actors that don't rotate
if (Turret.Value == null && facing == null)
return pos;
if (Turret.Value != null)
pos += Turret.Value.PxPosition(self, facing);
// Add local unitspace/turretspace offset
var f = Turret.Value != null ? Turret.Value.turretFacing : facing.Facing;
// This is going away, so no point adding unnecessary usings
var ru = self.TraitOrDefault<RenderUnit>();
var numDirs = (ru != null) ? ru.anim.CurrentSequence.Facings : 8;
var quantizedFacing = Util.QuantizeFacing(f, numDirs) * (256 / numDirs);
pos += (PVecFloat)Util.RotateVectorByFacing(b.TurretSpaceOffset.ToFloat2(), quantizedFacing, .7f);
return pos;
}
public WVec MuzzleOffset(Actor self, Barrel b)
{
if (Info.OffsetModel != CoordinateModel.World)
throw new InvalidOperationException("Armament.MuzzlePosition requires a world coordinate offset");
var bodyOrientation = Coords.Value.QuantizeOrientation(self, self.Orientation);
var localOffset = b.Offset + new WVec(-Recoil, WRange.Zero, WRange.Zero);
if (Turret.Value != null)

View File

@@ -31,8 +31,7 @@ namespace OpenRA.Mods.RA
public DebugFiringOffsets(Actor self)
{
armaments = Lazy.New(() => self.TraitsImplementing<Armament>()
.Where(a => a.Info.OffsetModel == CoordinateModel.World));
armaments = Lazy.New(() => self.TraitsImplementing<Armament>());
}
public void RenderAfterWorld(WorldRenderer wr, Actor self)

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Render
anim.Play("turret");
anims.Add("turret_{0}".F(i++), new AnimationWithOffset(anim,
() => t.PxPosition(self, null).ToFloat2(), null));
() => PPos.FromWPosHackZ(WPos.Zero + t.Position(self)).ToFloat2(), null));
}
}
}

View File

@@ -42,15 +42,6 @@ namespace OpenRA.Mods.RA.Render
}
float2 TurretPosition(Actor self, Turreted t, IFacing facing)
{
if (t.CoordinateModel == CoordinateModel.Legacy)
{
var recoil = self.TraitsImplementing<Armament>()
.Where(w => w.Info.Turret == t.Name)
.Sum(w => w.LegacyRecoil);
return t.PxPosition(self, facing).ToFloat2() + Traits.Util.RotateVectorByFacing(new float2(0, recoil), t.turretFacing, .7f);
}
else
{
var recoil = self.TraitsImplementing<Armament>()
.Where(w => w.Info.Turret == t.Name)
@@ -65,4 +56,3 @@ namespace OpenRA.Mods.RA.Render
}
}
}
}

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Mods.RA.Render
muzzleFlashes.Add("muzzle{0}".F(muzzleFlashes.Count), new AnimationWithOffset(
muzzleFlash,
() => a.MuzzlePxOffset(self, facing, barrel).ToFloat2(),
() => PPos.FromWPosHackZ(WPos.Zero + a.MuzzleOffset(self, barrel)).ToFloat2(),
() => !isShowing));
}
}

View File

@@ -19,7 +19,6 @@ namespace OpenRA.Mods.RA
public readonly int ProneTime = 100; /* ticks, =4s */
public readonly float ProneDamage = .5f;
public readonly decimal ProneSpeed = .5m;
public readonly int[] LegacyProneOffset = {0,-2,0,4};
public readonly WVec ProneOffset = new WVec(85, 0, -171);
public override object Create(ActorInitializer init) { return new TakeCover(init, this); }
@@ -44,10 +43,8 @@ namespace OpenRA.Mods.RA
if (e.Damage > 0 && (e.Warhead == null || !e.Warhead.PreventProne)) /* Don't go prone when healed */
{
if (!IsProne)
{
turret = new Turret(Info.LegacyProneOffset);
LocalOffset = Info.ProneOffset;
}
remainingProneTime = Info.ProneTime;
}
}
@@ -56,11 +53,8 @@ namespace OpenRA.Mods.RA
{
base.Tick(self);
if (IsProne && --remainingProneTime == 0)
{
turret = new Turret(Info.LegacyOffset);
LocalOffset = WVec.Zero;
}
}
public float GetDamageModifier(Actor attacker, WarheadInfo warhead )
{

View File

@@ -23,28 +23,12 @@ namespace OpenRA.Mods.RA
[Desc("Rate of Turning")]
public readonly int ROT = 255;
public readonly int InitialFacing = 128;
public readonly int[] LegacyOffset = {0,0};
public readonly bool AlignWhenIdle = false;
public CoordinateModel OffsetModel = CoordinateModel.Legacy;
[Desc("Muzzle position relative to turret or body. (forward, right, up) triples")]
public readonly WVec Offset = WVec.Zero;
bool HasWorldOffset(ArmamentInfo ai)
{
return ai.OffsetModel == CoordinateModel.World || ai.LocalOffset.Length > 0;
}
public virtual object Create(ActorInitializer init)
{
// Auto-detect coordinate type
var arms = init.self.Info.Traits.WithInterface<ArmamentInfo>();
if (Offset != WVec.Zero || arms.Any(ai => HasWorldOffset(ai)))
OffsetModel = CoordinateModel.World;
return new Turreted(init, this);
}
public virtual object Create(ActorInitializer init) { return new Turreted(init, this); }
}
public class Turreted : ITick, ISync, IResolveOrder
@@ -52,7 +36,6 @@ namespace OpenRA.Mods.RA
[Sync] public int turretFacing = 0;
public int? desiredFacing;
TurretedInfo info;
protected Turret turret;
IFacing facing;
// For subclasses that want to move the turret relative to the body
@@ -60,7 +43,6 @@ namespace OpenRA.Mods.RA
public WVec Offset { get { return info.Offset + LocalOffset; } }
public string Name { get { return info.Turret; } }
public CoordinateModel CoordinateModel { get { return info.OffsetModel; } }
public static int GetInitialTurretFacing(ActorInitializer init, int def)
{
@@ -78,7 +60,6 @@ namespace OpenRA.Mods.RA
this.info = info;
turretFacing = GetInitialTurretFacing(init, info.InitialFacing);
facing = init.self.TraitOrDefault<IFacing>();
turret = new Turret(info.LegacyOffset);
}
public virtual void Tick(Actor self)
@@ -99,21 +80,9 @@ namespace OpenRA.Mods.RA
desiredFacing = null;
}
public PVecFloat PxPosition(Actor self, IFacing facing)
{
// Hack for external code unaware of world coordinates
if (info.OffsetModel == CoordinateModel.World)
return (PVecFloat)PPos.FromWPosHackZ(WPos.Zero + Position(self)).ToFloat2();
return turret.PxPosition(self, facing);
}
// Turret offset in world-space
public WVec Position(Actor self)
{
if (info.OffsetModel != CoordinateModel.World)
throw new InvalidOperationException("Turreted.Position requires a world coordinate offset");
var coords = self.Trait<ILocalCoordinatesModel>();
var bodyOrientation = coords.QuantizeOrientation(self, self.Orientation);
return coords.LocalToWorld(Offset.Rotate(bodyOrientation));
@@ -127,6 +96,7 @@ namespace OpenRA.Mods.RA
}
}
// TODO: Remove this
public class Turret
{
public PVecInt UnitSpacePosition; // where, in the unit's local space.

View File

@@ -102,7 +102,6 @@ JEEP:
Offset: -85,0,171
Armament:
Weapon: MachineGun
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:
@@ -135,7 +134,6 @@ APC:
ROT: 10
Armament:
Weapon: MachineGun
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:
@@ -174,7 +172,6 @@ BGGY:
Offset: -43,0,128
Armament:
Weapon: MachineGun
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:

View File

@@ -216,7 +216,6 @@ BGGY:
Offset: -43,0,128
Armament:
Weapon: MachineGun
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:
@@ -292,7 +291,6 @@ JEEP:
Offset: -85,0,171
Armament:
Weapon: MachineGun
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:

View File

@@ -80,7 +80,6 @@ MSUB:
Armament:
Weapon: SubMissile
FireDelay: 2
OffsetModel: World
AttackFrontal:
Selectable:
Bounds: 44,44
@@ -245,10 +244,8 @@ PT:
Offset: 256,0,43
Armament@PRIMARY:
Weapon: 2Inch
OffsetModel: World
Armament@SECONDARY:
Weapon: DepthCharge
OffsetModel: World
AttackTurreted:
Selectable:
Bounds: 32,32

View File

@@ -496,7 +496,6 @@ GUN:
RenderBuildingTurreted:
Armament:
Weapon: TurretGun
OffsetModel: World
AttackTurreted:
AutoTarget:
IronCurtainable:
@@ -568,7 +567,6 @@ SAM:
RenderBuildingTurreted:
Armament:
Weapon: Nike
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
AutoTarget:

View File

@@ -20,7 +20,6 @@ V2RL:
Range: 5
Armament:
Weapon: SCUD
OffsetModel: World
AttackFrontal:
RenderUnitReload:
AutoTarget:
@@ -54,7 +53,6 @@ V2RL:
Weapon: 25mm
Recoil: 85
RecoilRecovery: 25
OffsetModel: World
AttackTurreted:
RenderUnitTurreted:
AutoTarget:
@@ -88,7 +86,6 @@ V2RL:
Weapon: 90mm
Recoil: 128
RecoilRecovery: 38
OffsetModel: World
AttackTurreted:
RenderUnitTurreted:
AutoTarget:
@@ -203,7 +200,6 @@ ARTY:
Range: 5
Armament:
Weapon: 155mm
OffsetModel: World
AttackFrontal:
RenderUnit:
Explodes:
@@ -314,7 +310,6 @@ JEEP:
Offset: 0,0,85
Armament:
Weapon: M60mg
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:

View File

@@ -84,7 +84,6 @@ MSUB:
Armament:
Weapon: SubMissile
FireDelay: 2
OffsetModel: World
AttackFrontal:
Selectable:
Bounds: 44,44
@@ -259,10 +258,8 @@ PT:
Offset: 256,0,43
Armament@PRIMARY:
Weapon: 2Inch
OffsetModel: World
Armament@SECONDARY:
Weapon: DepthCharge
OffsetModel: World
AttackTurreted:
Selectable:
Bounds: 32,32

View File

@@ -328,10 +328,8 @@ AGUN:
RenderBuildingTurreted:
Armament@PRIMARY:
Weapon: ZSU-23
OffsetModel: World
Armament@SECONDARY:
Weapon: ZSU-23
OffsetModel: World
AttackTurreted:
AutoTarget:
IronCurtainable:
@@ -748,7 +746,6 @@ GUN:
RenderBuildingTurreted:
Armament:
Weapon: TurretGun
OffsetModel: World
AttackTurreted:
AutoTarget:
DebugRetiliateAgainstAggressor:
@@ -828,7 +825,6 @@ SAM:
RenderBuildingTurreted:
Armament:
Weapon: Nike
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
AutoTarget:

View File

@@ -21,7 +21,6 @@ V2RL:
Range: 5
Armament:
Weapon: SCUD
OffsetModel: World
AttackFrontal:
RenderUnitReload:
AutoTarget:
@@ -57,7 +56,6 @@ V2RL:
Weapon: 25mm
Recoil: 85
RecoilRecovery: 25
OffsetModel: World
AttackTurreted:
RenderUnitTurreted:
AutoTarget:
@@ -97,7 +95,6 @@ V2RL:
Weapon: 90mm
Recoil: 128
RecoilRecovery: 38
OffsetModel: World
AttackTurreted:
RenderUnitTurreted:
AutoTarget:
@@ -229,7 +226,6 @@ ARTY:
Range: 5
Armament:
Weapon: 155mm
OffsetModel: World
AttackFrontal:
RenderUnit:
Explodes:
@@ -346,7 +342,6 @@ JEEP:
Offset: 0,0,85
Armament:
Weapon: M60mg
OffsetModel: World
AttackTurreted:
WithMuzzleFlash:
RenderUnitTurreted:
@@ -683,7 +678,6 @@ FTRK:
Armament:
Weapon: FLAK-23
Recoil: 85
OffsetModel: World
AttackTurreted:
RenderUnitTurreted:
AutoTarget: