Fix cloaked units.

This commit is contained in:
Paul Chote
2010-11-27 01:34:55 +13:00
parent 9655b34e5f
commit 434ea26950
15 changed files with 99 additions and 63 deletions

View File

@@ -242,6 +242,7 @@ namespace OpenRA.Traits
{ {
string[] TargetTypes { get; } string[] TargetTypes { get; }
IEnumerable<int2> TargetableCells( Actor self ); IEnumerable<int2> TargetableCells( Actor self );
bool TargetableBy(Actor self, Actor byActor);
} }
public interface INotifyKeyPress { bool KeyPressed(Actor self, KeyInput e); } public interface INotifyKeyPress { bool KeyPressed(Actor self, KeyInput e); }

View File

@@ -19,12 +19,16 @@ namespace OpenRA.Mods.RA.Activities
public class Attack : CancelableActivity public class Attack : CancelableActivity
{ {
Target Target; Target Target;
ITargetable targetable;
int Range; int Range;
bool AllowMovement; bool AllowMovement;
public Attack(Target target, int range, bool allowMovement) public Attack(Target target, int range, bool allowMovement)
{ {
Target = target; Target = target;
if (target.IsActor)
targetable = target.Actor.TraitOrDefault<ITargetable>();
Range = range; Range = range;
AllowMovement = allowMovement; AllowMovement = allowMovement;
} }
@@ -50,6 +54,9 @@ namespace OpenRA.Mods.RA.Activities
if (!Target.IsValid) if (!Target.IsValid)
return NextActivity; return NextActivity;
if (targetable != null && !targetable.TargetableBy(Target.Actor, self))
return NextActivity;
if (!Combat.IsInRange(self.CenterLocation, Range, Target)) if (!Combat.IsInRange(self.CenterLocation, Range, Target))
return (AllowMovement) ? Util.SequenceActivities(self.Trait<Mobile>().MoveWithinRange(Target, Range), this) : NextActivity; return (AllowMovement) ? Util.SequenceActivities(self.Trait<Mobile>().MoveWithinRange(Target, Range), this) : NextActivity;

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.RA.Air
{ {
Aircraft Aircraft; Aircraft Aircraft;
public TargetableAircraft(Actor self, TargetableAircraftInfo info) public TargetableAircraft(Actor self, TargetableAircraftInfo info)
: base(info) : base(self, info)
{ {
Aircraft = self.Trait<Aircraft>(); Aircraft = self.Trait<Aircraft>();
} }

View File

@@ -76,7 +76,11 @@ namespace OpenRA.Mods.RA
if (!target.IsValid) return false; if (!target.IsValid) return false;
if (Weapons.All(w => w.IsReloading)) return false; if (Weapons.All(w => w.IsReloading)) return false;
if (self.TraitsImplementing<IDisable>().Any(d => d.Disabled)) return false; if (self.TraitsImplementing<IDisable>().Any(d => d.Disabled)) return false;
if (target.IsActor && target.Actor.HasTrait<ITargetable>() &&
!target.Actor.Trait<ITargetable>().TargetableBy(target.Actor,self))
return false;
return true; return true;
} }

View File

@@ -103,7 +103,10 @@ namespace OpenRA.Mods.RA
public bool IsVisible(Actor self) public bool IsVisible(Actor self)
{ {
return !Cloaked || self.Owner == self.World.LocalPlayer; if (!Cloaked || self.Owner == self.World.LocalPlayer || self.Owner.Stances[self.World.LocalPlayer] == Stance.Ally)
return true;
return self.World.Queries.WithTrait<DetectCloaked>().Any(a => (self.Location - a.Actor.Location).Length < a.Actor.Info.Traits.Get<DetectCloakedInfo>().Range);
} }
public Color RadarColorOverride(Actor self) public Color RadarColorOverride(Actor self)

View File

@@ -9,6 +9,7 @@
#endregion #endregion
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Mods.RA;
namespace OpenRA.Mods.RA.Crates namespace OpenRA.Mods.RA.Crates
{ {
@@ -44,7 +45,11 @@ namespace OpenRA.Mods.RA.Crates
collector.World.AddFrameEndTask(w => collector.World.AddFrameEndTask(w =>
{ {
w.Remove(collector); w.Remove(collector);
collector.AddTrait(cloak); collector.AddTrait(cloak);
if (collector.HasTrait<TargetableUnit<TargetableUnitInfo>>())
collector.Trait<TargetableUnit<TargetableUnitInfo>>().RecievedCloak(collector);
w.Add(collector); w.Add(collector);
}); });

View File

@@ -294,7 +294,6 @@
<Compile Include="AppearsOnRadar.cs" /> <Compile Include="AppearsOnRadar.cs" />
<Compile Include="ColorPickerPaletteModifier.cs" /> <Compile Include="ColorPickerPaletteModifier.cs" />
<Compile Include="Crates\RevealMapCrateAction.cs" /> <Compile Include="Crates\RevealMapCrateAction.cs" />
<Compile Include="TargetableCloaked.cs" />
<Compile Include="SpawnMPUnits.cs" /> <Compile Include="SpawnMPUnits.cs" />
<Compile Include="CreateMPPlayers.cs" /> <Compile Include="CreateMPPlayers.cs" />
<Compile Include="HackyAI.cs" /> <Compile Include="HackyAI.cs" />
@@ -324,6 +323,7 @@
<Compile Include="Widgets\Delegates\ServerBrowserDelegate.cs" /> <Compile Include="Widgets\Delegates\ServerBrowserDelegate.cs" />
<Compile Include="Widgets\Delegates\SettingsMenuDelegate.cs" /> <Compile Include="Widgets\Delegates\SettingsMenuDelegate.cs" />
<Compile Include="Widgets\Delegates\VideoPlayerDelegate.cs" /> <Compile Include="Widgets\Delegates\VideoPlayerDelegate.cs" />
<Compile Include="TargetableSubmarine.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.RA
} }
public string[] TargetTypes { get { return info.TargetTypes; } } public string[] TargetTypes { get { return info.TargetTypes; } }
public bool TargetableBy(Actor self, Actor byActor) { return true; }
public IEnumerable<int2> TargetableCells( Actor self ) public IEnumerable<int2> TargetableCells( Actor self )
{ {
return self.Trait<Building>().OccupiedCells(); return self.Trait<Building>().OccupiedCells();

View File

@@ -1,46 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class TargetableCloakedInfo : TargetableUnitInfo, ITraitPrerequisite<CloakInfo>
{
public readonly string[] CloakedTargetTypes = {};
public override object Create( ActorInitializer init ) { return new TargetableCloaked(init.self, this); }
}
public class TargetableCloaked : TargetableUnit<TargetableCloakedInfo>
{
Cloak Cloak;
public TargetableCloaked(Actor self, TargetableCloakedInfo info)
: base(info)
{
Cloak = self.Trait<Cloak>();
}
public override string[] TargetTypes
{
get { return (Cloak.Cloaked) ? info.CloakedTargetTypes
: info.TargetTypes;}
}
// Todo: Finish me
public bool TargetableBy(Actor self, Actor byActor)
{
if (!Cloak.Cloaked || self.Owner == byActor.Owner || self.Owner.Stances[byActor.Owner] == Stance.Ally)
return true;
return self.World.Queries.WithTrait<DetectCloaked>().Any(a => (self.Location - a.Actor.Location).Length < a.Actor.Info.Traits.Get<DetectCloakedInfo>().Range);
}
}
}

View File

@@ -0,0 +1,33 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class TargetableSubmarineInfo : TargetableUnitInfo, ITraitPrerequisite<CloakInfo>
{
public readonly string[] CloakedTargetTypes = {};
public override object Create( ActorInitializer init ) { return new TargetableSubmarine(init.self, this); }
}
public class TargetableSubmarine : TargetableUnit<TargetableSubmarineInfo>
{
public TargetableSubmarine(Actor self, TargetableSubmarineInfo info)
: base(self, info) {}
public override string[] TargetTypes
{
get { return (Cloak.Cloaked) ? info.CloakedTargetTypes
: info.TargetTypes;}
}
}
}

View File

@@ -19,16 +19,36 @@ namespace OpenRA.Mods.RA
public class TargetableUnitInfo : ITraitInfo public class TargetableUnitInfo : ITraitInfo
{ {
public readonly string[] TargetTypes = { }; public readonly string[] TargetTypes = { };
public virtual object Create( ActorInitializer init ) { return new TargetableUnit<TargetableUnitInfo>( this ); } public virtual object Create( ActorInitializer init ) { return new TargetableUnit<TargetableUnitInfo>( init.self, this ); }
} }
public class TargetableUnit<Info> : ITargetable public class TargetableUnit<Info> : ITargetable
where Info : TargetableUnitInfo where Info : TargetableUnitInfo
{ {
protected readonly Info info; protected readonly Info info;
public TargetableUnit( Info info ) protected Cloak Cloak;
public TargetableUnit( Actor self, Info info )
{ {
this.info = info; this.info = info;
RecievedCloak(self);
}
// Arbitrary units can recieve cloak via a crate during gameplay
public void RecievedCloak(Actor self)
{
Cloak = self.TraitOrDefault<Cloak>();
}
public virtual bool TargetableBy(Actor self, Actor byActor)
{
if (Cloak == null)
return true;
if (!Cloak.Cloaked || self.Owner == byActor.Owner || self.Owner.Stances[byActor.Owner] == Stance.Ally)
return true;
return self.World.Queries.WithTrait<DetectCloaked>().Any(a => (self.Location - a.Actor.Location).Length < a.Actor.Info.Traits.Get<DetectCloakedInfo>().Range);
} }
public virtual string[] TargetTypes { get { return info.TargetTypes; } } public virtual string[] TargetTypes { get { return info.TargetTypes; } }

View File

@@ -300,6 +300,7 @@ HQ:
Range: 10 Range: 10
Bib: Bib:
ProvidesRadar: ProvidesRadar:
RenderDetectionCircle:
DetectCloaked: DetectCloaked:
Range: 8 Range: 8
@@ -414,6 +415,9 @@ EYE:
Range: 10 Range: 10
Bib: Bib:
ProvidesRadar: ProvidesRadar:
RenderDetectionCircle:
DetectCloaked:
Range: 8
IonControl: IonControl:
TMPL: TMPL:
@@ -480,6 +484,9 @@ OBLI:
-RenderBuilding: -RenderBuilding:
RenderRangeCircle: RenderRangeCircle:
-EmitInfantryOnSell: -EmitInfantryOnSell:
RenderDetectionCircle:
DetectCloaked:
Range: 6
CYCL: CYCL:
Inherits: ^Wall Inherits: ^Wall
@@ -574,7 +581,9 @@ GUN:
-RenderBuilding: -RenderBuilding:
-DeadBuildingState: -DeadBuildingState:
RenderRangeCircle: RenderRangeCircle:
RenderDetectionCircle:
DetectCloaked:
Range: 3
SAM: SAM:
Inherits: ^Building Inherits: ^Building
Valued: Valued:
@@ -633,6 +642,7 @@ GTWR:
AutoTarget: AutoTarget:
DetectCloaked: DetectCloaked:
Range: 3 Range: 3
RenderDetectionCircle:
RenderRangeCircle: RenderRangeCircle:
ATWR: ATWR:
@@ -660,6 +670,7 @@ ATWR:
PrimaryWeapon: Tomahawk PrimaryWeapon: Tomahawk
SecondaryWeapon: Tomahawk SecondaryWeapon: Tomahawk
AutoTarget: AutoTarget:
RenderDetectionCircle:
DetectCloaked: DetectCloaked:
Range: 6 Range: 6
RenderRangeCircle: RenderRangeCircle:

View File

@@ -211,8 +211,8 @@ CRATE:
SelectionShares: 5 SelectionShares: 5
CloakCrateAction: CloakCrateAction:
SelectionShares: 5 SelectionShares: 5
InitialDelay: .4 InitialDelay: 5
CloakDelay: 2.0 CloakDelay: 5
CloakSound: appear1.aud CloakSound: appear1.aud
UncloakSound: appear1.aud UncloakSound: appear1.aud
Effect: stealth Effect: stealth

View File

@@ -438,17 +438,15 @@ STNK:
RevealsShroud: RevealsShroud:
Range: 4 Range: 4
Cloak: Cloak:
InitialDelay: 0 InitialDelay: 5
CloakDelay: 2.0 CloakDelay: 5
CloakSound: appear1.aud CloakSound: appear1.aud
UncloakSound: appear1.aud UncloakSound: appear1.aud
AttackFrontal: AttackFrontal:
PrimaryWeapon: 227mm PrimaryWeapon: 227mm
RenderUnit: RenderUnit:
AutoTarget: AutoTarget:
-TargetableUnit: TargetableUnit:
TargetableCloaked:
CloakedTargetTypes: cloaked
TRAN: TRAN:
Inherits: ^Helicopter Inherits: ^Helicopter

View File

@@ -495,7 +495,7 @@ SS:
RevealsShroud: RevealsShroud:
Range: 6 Range: 6
-TargetableUnit: -TargetableUnit:
TargetableCloaked: TargetableSubmarine:
TargetTypes: Ground, Water TargetTypes: Ground, Water
CloakedTargetTypes: Underwater CloakedTargetTypes: Underwater
RenderUnit: RenderUnit:
@@ -538,7 +538,7 @@ MSUB:
Range: 6 Range: 6
RenderUnit: RenderUnit:
-TargetableUnit: -TargetableUnit:
TargetableCloaked: TargetableSubmarine:
TargetTypes: Ground, Water TargetTypes: Ground, Water
CloakedTargetTypes: Underwater CloakedTargetTypes: Underwater
Cloak: Cloak: