Merge pull request #7525 from penev92/bleed_enemySightedNotification2

Refactor EnemyWatcher and AnnounceOnSeen traits
This commit is contained in:
Matthias Mailänder
2015-03-21 14:16:06 +01:00
6 changed files with 74 additions and 29 deletions

View File

@@ -104,6 +104,7 @@ namespace OpenRA.Traits
public interface INotifyEffectiveOwnerChanged { void OnEffectiveOwnerChanged(Actor self, Player oldEffectiveOwner, Player newEffectiveOwner); } public interface INotifyEffectiveOwnerChanged { void OnEffectiveOwnerChanged(Actor self, Player oldEffectiveOwner, Player newEffectiveOwner); }
public interface INotifyCapture { void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner); } public interface INotifyCapture { void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner); }
public interface INotifyInfiltrated { void Infiltrated(Actor self, Actor infiltrator); } public interface INotifyInfiltrated { void Infiltrated(Actor self, Actor infiltrator); }
public interface INotifyDiscovered { void OnDiscovered(Actor self, Player discoverer, bool playNotification); }
public interface IDisableMove { bool MoveDisabled(Actor self); } public interface IDisableMove { bool MoveDisabled(Actor self); }
public interface ISeedableResource { void Seed(Actor self); } public interface ISeedableResource { void Seed(Actor self); }

View File

@@ -8,9 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -25,13 +23,13 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Minimal ticks in-between notifications.")] [Desc("Minimal ticks in-between notifications.")]
public readonly int NotificationInterval = 750; public readonly int NotificationInterval = 750;
public object Create(ActorInitializer init) { return new EnemyWatcher(init.Self, this); } public object Create(ActorInitializer init) { return new EnemyWatcher(this); }
} }
class EnemyWatcher : ITick class EnemyWatcher : ITick
{ {
readonly EnemyWatcherInfo info; readonly EnemyWatcherInfo info;
readonly Lazy<RadarPings> radarPings; readonly HashSet<Player> discoveredPlayers;
bool announcedAny; bool announcedAny;
int rescanInterval; int rescanInterval;
@@ -40,15 +38,16 @@ namespace OpenRA.Mods.Common.Traits
HashSet<uint> visibleActorIds; HashSet<uint> visibleActorIds;
HashSet<string> playedNotifications; HashSet<string> playedNotifications;
public EnemyWatcher(Actor self, EnemyWatcherInfo info) public EnemyWatcher(EnemyWatcherInfo info)
{ {
lastKnownActorIds = new HashSet<uint>(); lastKnownActorIds = new HashSet<uint>();
discoveredPlayers = new HashSet<Player>();
this.info = info; this.info = info;
rescanInterval = info.ScanInterval; rescanInterval = info.ScanInterval;
ticksBeforeNextNotification = info.NotificationInterval; ticksBeforeNextNotification = info.NotificationInterval;
radarPings = Exts.Lazy(() => self.World.WorldActor.Trait<RadarPings>());
} }
// Here self is the player actor
public void Tick(Actor self) public void Tick(Actor self)
{ {
// TODO: Make the AI handle such notifications and remove Owner.IsBot from this check // TODO: Make the AI handle such notifications and remove Owner.IsBot from this check
@@ -70,9 +69,9 @@ namespace OpenRA.Mods.Common.Traits
foreach (var actor in self.World.ActorsWithTrait<AnnounceOnSeen>()) foreach (var actor in self.World.ActorsWithTrait<AnnounceOnSeen>())
{ {
// We only care about enemy actors (creeps should be enemies) // We don't want notifications for allied actors
if ((actor.Actor.EffectiveOwner != null && self.Owner.Stances[actor.Actor.EffectiveOwner.Owner] != Stance.Enemy) if ((actor.Actor.EffectiveOwner != null && self.Owner.Stances[actor.Actor.EffectiveOwner.Owner] == Stance.Ally)
|| self.Owner.Stances[actor.Actor.Owner] != Stance.Enemy) || self.Owner.Stances[actor.Actor.Owner] == Stance.Ally)
continue; continue;
if (actor.Actor.IsDead || !actor.Actor.IsInWorld) if (actor.Actor.IsDead || !actor.Actor.IsInWorld)
@@ -88,12 +87,28 @@ namespace OpenRA.Mods.Common.Traits
if (lastKnownActorIds.Contains(actor.Actor.ActorID)) if (lastKnownActorIds.Contains(actor.Actor.ActorID))
continue; continue;
var notificationPlayed = playedNotifications.Contains(actor.Trait.Info.Notification);
// Notify the actor that he has been discovered
foreach (var trait in actor.Actor.TraitsImplementing<INotifyDiscovered>())
trait.OnDiscovered(actor.Actor, self.Owner, !notificationPlayed);
var discoveredPlayer = actor.Actor.Owner;
if (!discoveredPlayers.Contains(discoveredPlayer))
{
// Notify the actor's owner that he has been discovered
foreach (var trait in discoveredPlayer.PlayerActor.TraitsImplementing<INotifyDiscovered>())
trait.OnDiscovered(actor.Actor, self.Owner, false);
discoveredPlayers.Add(discoveredPlayer);
}
// We have already played this type of notification // We have already played this type of notification
if (playedNotifications.Contains(actor.Trait.Info.Notification)) if (notificationPlayed)
continue; continue;
if (self.Owner == self.World.RenderPlayer) playedNotifications.Add(actor.Trait.Info.Notification);
Announce(self, actor); announcedAny = true;
} }
if (announcedAny) if (announcedAny)
@@ -101,18 +116,5 @@ namespace OpenRA.Mods.Common.Traits
lastKnownActorIds = visibleActorIds; lastKnownActorIds = visibleActorIds;
} }
void Announce(Actor self, TraitPair<AnnounceOnSeen> announce)
{
// Audio notification
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", announce.Trait.Info.Notification, self.Owner.Country.Race);
// Radar notificaion
if (announce.Trait.Info.PingRadar && radarPings.Value != null)
radarPings.Value.Add(() => true, announce.Actor.CenterPosition, Color.Red, 50);
playedNotifications.Add(announce.Trait.Info.Notification);
announcedAny = true;
}
} }
} }

View File

@@ -8,6 +8,8 @@
*/ */
#endregion #endregion
using System;
using System.Drawing;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -18,18 +20,43 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Should there be a radar ping on enemies' radar at the actor's location when they see him")] [Desc("Should there be a radar ping on enemies' radar at the actor's location when they see him")]
public readonly bool PingRadar = false; public readonly bool PingRadar = false;
public readonly string Notification = "EnemyUnitSighted"; public readonly string Notification = null;
public object Create(ActorInitializer init) { return new AnnounceOnSeen(this); } public readonly bool AnnounceNeutrals = false;
public object Create(ActorInitializer init) { return new AnnounceOnSeen(init.Self, this); }
} }
public class AnnounceOnSeen public class AnnounceOnSeen : INotifyDiscovered
{ {
public readonly AnnounceOnSeenInfo Info; public readonly AnnounceOnSeenInfo Info;
public AnnounceOnSeen(AnnounceOnSeenInfo info) readonly Lazy<RadarPings> radarPings;
public AnnounceOnSeen(Actor self, AnnounceOnSeenInfo info)
{ {
Info = info; Info = info;
radarPings = Exts.Lazy(() => self.World.WorldActor.Trait<RadarPings>());
}
public void OnDiscovered(Actor self, Player discoverer, bool playNotification)
{
if (!playNotification || discoverer != self.World.RenderPlayer)
return;
// Hack to disable notifications for neutral actors so some custom maps don't need fixing
if (!Info.AnnounceNeutrals &&
((self.EffectiveOwner != null && discoverer.Stances[self.EffectiveOwner.Owner] != Stance.Enemy)
|| discoverer.Stances[self.Owner] != Stance.Enemy))
return;
// Audio notification
if (discoverer != null && !string.IsNullOrEmpty(Info.Notification))
Sound.PlayNotification(self.World.Map.Rules, discoverer, "Speech", Info.Notification, discoverer.Country.Race);
// Radar notificaion
if (Info.PingRadar && radarPings.Value != null)
radarPings.Value.Add(() => true, self.CenterPosition, Color.Red, 50);
} }
} }
} }

View File

@@ -34,6 +34,7 @@ BADR:
-EjectOnDeath: -EjectOnDeath:
-GpsDot: -GpsDot:
RejectsOrders: RejectsOrders:
-AnnounceOnSeen:
BADR.Bomber: BADR.Bomber:
Inherits: ^Plane Inherits: ^Plane
@@ -350,4 +351,5 @@ U2:
Offset: -1c43,0,0 Offset: -1c43,0,0
Interval: 2 Interval: 2
RejectsOrders: RejectsOrders:
-AnnounceOnSeen:

View File

@@ -72,6 +72,8 @@
TimedUpgradeBar: TimedUpgradeBar:
Upgrade: invulnerability Upgrade: invulnerability
MustBeDestroyed: MustBeDestroyed:
AnnounceOnSeen:
Notification: EnemyUnitsApproaching
^Tank: ^Tank:
AppearsOnRadar: AppearsOnRadar:
@@ -157,6 +159,8 @@
GroundCorpsePalette: GroundCorpsePalette:
WaterCorpseSequence: WaterCorpseSequence:
WaterCorpsePalette: WaterCorpsePalette:
AnnounceOnSeen:
Notification: EnemyUnitsApproaching
^Infantry: ^Infantry:
AppearsOnRadar: AppearsOnRadar:
@@ -247,6 +251,8 @@
UpgradeMinEnabledLevel: 1 UpgradeMinEnabledLevel: 1
UpgradeManager: UpgradeManager:
MustBeDestroyed: MustBeDestroyed:
AnnounceOnSeen:
Notification: EnemyUnitsApproaching
^Ship: ^Ship:
AppearsOnRadar: AppearsOnRadar:
@@ -301,6 +307,8 @@
Upgrade: invulnerability Upgrade: invulnerability
UpgradeMinEnabledLevel: 1 UpgradeMinEnabledLevel: 1
MustBeDestroyed: MustBeDestroyed:
AnnounceOnSeen:
Notification: EnemyUnitsApproaching
^Plane: ^Plane:
AppearsOnRadar: AppearsOnRadar:
@@ -358,6 +366,8 @@
Upgrade: invulnerability Upgrade: invulnerability
WithShadow: WithShadow:
MustBeDestroyed: MustBeDestroyed:
AnnounceOnSeen:
Notification: EnemyUnitsApproaching
^Helicopter: ^Helicopter:
Inherits: ^Plane Inherits: ^Plane
@@ -366,6 +376,8 @@
GpsDot: GpsDot:
String: Helicopter String: Helicopter
Hovers: Hovers:
AnnounceOnSeen:
Notification: EnemyUnitsApproaching
^Building: ^Building:
AppearsOnRadar: AppearsOnRadar:

View File

@@ -73,4 +73,5 @@ Player:
Name: Unrestricted Name: Unrestricted
Prerequisites: techlevel.infonly, techlevel.low, techlevel.medium, techlevel.unrestricted Prerequisites: techlevel.infonly, techlevel.low, techlevel.medium, techlevel.unrestricted
GlobalUpgradeManager: GlobalUpgradeManager:
EnemyWatcher: