Merge pull request #6644 from pchote/proximity-triggers

Closes #2175
This commit is contained in:
Matthias Mailänder
2014-10-05 09:12:00 +02:00
9 changed files with 449 additions and 12 deletions

View File

@@ -33,6 +33,12 @@ namespace OpenRA.Traits
public Actor Actor;
}
class Bin
{
public readonly List<Actor> Actors = new List<Actor>();
public readonly List<ProximityTrigger> ProximityTriggers = new List<ProximityTrigger>();
}
class CellTrigger
{
public readonly int Id;
@@ -79,15 +85,84 @@ namespace OpenRA.Traits
}
}
class ProximityTrigger : IDisposable
{
public readonly int Id;
public WPos Position { get; private set; }
public WRange Range { get; private set; }
public WPos TopLeft { get; private set; }
public WPos BottomRight { get; private set; }
public bool Dirty;
Action<Actor> onActorEntered;
Action<Actor> onActorExited;
IEnumerable<Actor> currentActors = Enumerable.Empty<Actor>();
public ProximityTrigger(int id, WPos pos, WRange range, Action<Actor> onActorEntered, Action<Actor> onActorExited)
{
Id = id;
this.onActorEntered = onActorEntered;
this.onActorExited = onActorExited;
Update(pos, range);
}
public void Update(WPos newPos, WRange newRange)
{
Position = newPos;
Range = newRange;
var offset = new WVec(newRange, newRange, WRange.Zero);
TopLeft = newPos - offset;
BottomRight = newPos + offset;
Dirty = true;
}
public void Tick(ActorMap am)
{
if (!Dirty)
return;
var oldActors = currentActors;
var delta = new WVec(Range, Range, WRange.Zero);
currentActors = am.ActorsInBox(Position - delta, Position + delta)
.Where(a => (a.CenterPosition - Position).HorizontalLengthSquared < Range.Range * Range.Range)
.ToList();
var entered = currentActors.Except(oldActors);
var exited = oldActors.Except(currentActors);
foreach (var a in entered)
onActorEntered(a);
foreach (var a in exited)
onActorExited(a);
Dirty = false;
}
public void Dispose()
{
foreach (var a in currentActors)
onActorExited(a);
}
}
readonly ActorMapInfo info;
readonly Map map;
readonly Dictionary<int, CellTrigger> cellTriggers = new Dictionary<int, CellTrigger>();
readonly Dictionary<CPos, List<CellTrigger>> cellTriggerInfluence = new Dictionary<CPos, List<CellTrigger>>();
readonly Dictionary<int, ProximityTrigger> proximityTriggers = new Dictionary<int, ProximityTrigger>();
int nextTriggerId;
readonly CellLayer<InfluenceNode> influence;
readonly List<Actor>[] actors;
readonly Bin[] bins;
readonly int rows, cols;
// Position updates are done in one pass
@@ -104,10 +179,10 @@ namespace OpenRA.Traits
cols = world.Map.MapSize.X / info.BinSize + 1;
rows = world.Map.MapSize.Y / info.BinSize + 1;
actors = new List<Actor>[rows * cols];
bins = new Bin[rows * cols];
for (var j = 0; j < rows; j++)
for (var i = 0; i < cols; i++)
actors[j * cols + i] = new List<Actor>();
bins[j * cols + i] = new Bin();
// Cache this delegate so it does not have to be allocated repeatedly.
actorShouldBeRemoved = removeActorPosition.Contains;
@@ -263,8 +338,13 @@ namespace OpenRA.Traits
{
// Position updates are done in one pass
// to ensure consistency during a tick
foreach (var bin in actors)
bin.RemoveAll(actorShouldBeRemoved);
foreach (var bin in bins)
{
var removed = bin.Actors.RemoveAll(actorShouldBeRemoved);
if (removed > 0)
foreach (var t in bin.ProximityTriggers)
t.Dirty = true;
}
removeActorPosition.Clear();
@@ -273,13 +353,20 @@ namespace OpenRA.Traits
var pos = a.OccupiesSpace.CenterPosition;
var i = (pos.X / info.BinSize).Clamp(0, cols - 1);
var j = (pos.Y / info.BinSize).Clamp(0, rows - 1);
actors[j * cols + i].Add(a);
var bin = bins[j * cols + i];
bin.Actors.Add(a);
foreach (var t in bin.ProximityTriggers)
t.Dirty = true;
}
addActorPosition.Clear();
foreach (var t in cellTriggers)
t.Value.Tick(this);
foreach (var t in proximityTriggers)
t.Value.Tick(this);
}
public int AddCellTrigger(CPos[] cells, Action<Actor> onEntry, Action<Actor> onExit)
@@ -317,6 +404,45 @@ namespace OpenRA.Traits
}
}
public int AddProximityTrigger(WPos pos, WRange range, Action<Actor> onEntry, Action<Actor> onExit)
{
var id = nextTriggerId++;
var t = new ProximityTrigger(id, pos, range, onEntry, onExit);
proximityTriggers.Add(id, t);
foreach (var bin in BinsInBox(t.TopLeft, t.BottomRight))
bin.ProximityTriggers.Add(t);
return id;
}
public void RemoveProximityTrigger(int id)
{
ProximityTrigger t;
if (!proximityTriggers.TryGetValue(id, out t))
return;
foreach (var bin in BinsInBox(t.TopLeft, t.BottomRight))
bin.ProximityTriggers.Remove(t);
t.Dispose();
}
public void UpdateProximityTrigger(int id, WPos newPos, WRange newRange)
{
ProximityTrigger t;
if (!proximityTriggers.TryGetValue(id, out t))
return;
foreach (var bin in BinsInBox(t.TopLeft, t.BottomRight))
bin.ProximityTriggers.Remove(t);
t.Update(newPos, newRange);
foreach (var bin in BinsInBox(t.TopLeft, t.BottomRight))
bin.ProximityTriggers.Add(t);
}
public void AddPosition(Actor a, IOccupySpace ios)
{
UpdatePosition(a, ios);
@@ -333,6 +459,23 @@ namespace OpenRA.Traits
addActorPosition.Add(a);
}
IEnumerable<Bin> BinsInBox(WPos a, WPos b)
{
var left = Math.Min(a.X, b.X);
var top = Math.Min(a.Y, b.Y);
var right = Math.Max(a.X, b.X);
var bottom = Math.Max(a.Y, b.Y);
var i1 = (left / info.BinSize).Clamp(0, cols - 1);
var i2 = (right / info.BinSize).Clamp(0, cols - 1);
var j1 = (top / info.BinSize).Clamp(0, rows - 1);
var j2 = (bottom / info.BinSize).Clamp(0, rows - 1);
for (var j = j1; j <= j2; j++)
for (var i = i1; i <= i2; i++)
yield return bins[j * cols + i];
}
public IEnumerable<Actor> ActorsInBox(WPos a, WPos b)
{
var left = Math.Min(a.X, b.X);
@@ -348,7 +491,7 @@ namespace OpenRA.Traits
{
for (var i = i1; i <= i2; i++)
{
foreach (var actor in actors[j * cols + i])
foreach (var actor in bins[j * cols + i].Actors)
{
if (actor.IsInWorld)
{
@@ -363,7 +506,7 @@ namespace OpenRA.Traits
public IEnumerable<Actor> ActorsInWorld()
{
return actors.SelectMany(bin => bin.Where(actor => actor.IsInWorld));
return bins.SelectMany(bin => bin.Actors.Where(actor => actor.IsInWorld));
}
}
}

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.RA
public void UnitProducedByOther(Actor self, Actor producer, Actor produced)
{
// No recursive cloning!
if (producer.HasTrait<ClonesProducedUnits>())
if (producer.Owner != self.Owner || producer.HasTrait<ClonesProducedUnits>())
return;
var ci = produced.Info.Traits.GetOrDefault<CloneableInfo>();

View File

@@ -544,6 +544,8 @@
<Compile Include="Warheads\GrantUpgradeWarhead.cs" />
<Compile Include="Crates\GrantUpgradeCrateAction.cs" />
<Compile Include="Scripting\Properties\UpgradeProperties.cs" />
<Compile Include="UpgradeActorsNear.cs" />
<Compile Include="WithRangeCircle.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">

View File

@@ -98,9 +98,7 @@ namespace OpenRA.Mods.RA
foreach (var t in self.TraitsImplementing<INotifyProduction>())
t.UnitProduced(self, newUnit, exit);
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>()
.Where(a => a.Actor.Owner == self.Owner);
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
foreach (var notify in notifyOthers)
notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit);

View File

@@ -0,0 +1,133 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
[Desc("Applies an upgrade to actors within a specified range.")]
public class UpgradeActorsNearInfo : ITraitInfo
{
[Desc("The upgrades to grant.")]
public readonly string[] Upgrades = { };
[Desc("The range to search for actors to upgrade.")]
public readonly WRange Range = WRange.FromCells(3);
[Desc("What diplomatic stances are affected.")]
public readonly Stance ValidStances = Stance.Ally;
[Desc("Grant the upgrades apply to this actor.")]
public readonly bool AffectsParent = false;
public readonly string EnableSound = null;
public readonly string DisableSound = null;
public object Create(ActorInitializer init) { return new UpgradeActorsNear(init.self, this); }
}
public class UpgradeActorsNear : ITick, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyOtherProduction
{
readonly UpgradeActorsNearInfo info;
readonly Actor self;
int proximityTrigger;
WPos cachedPosition;
WRange cachedRange;
WRange desiredRange;
bool cachedDisabled = true;
public UpgradeActorsNear(Actor self, UpgradeActorsNearInfo info)
{
this.info = info;
this.self = self;
cachedRange = info.Range;
}
public void AddedToWorld(Actor self)
{
cachedPosition = self.CenterPosition;
proximityTrigger = self.World.ActorMap.AddProximityTrigger(cachedPosition, cachedRange, ActorEntered, ActorExited);
}
public void RemovedFromWorld(Actor self)
{
self.World.ActorMap.RemoveProximityTrigger(proximityTrigger);
}
public void Tick(Actor self)
{
var disabled = self.IsDisabled();
if (cachedDisabled != disabled)
{
Sound.Play(disabled ? info.DisableSound : info.EnableSound, self.CenterPosition);
desiredRange = disabled ? WRange.Zero : info.Range;
cachedDisabled = disabled;
}
if (self.CenterPosition != cachedPosition || desiredRange != cachedRange)
{
cachedPosition = self.CenterPosition;
cachedRange = desiredRange;
self.World.ActorMap.UpdateProximityTrigger(proximityTrigger, cachedPosition, cachedRange);
}
}
void ActorEntered(Actor a)
{
if (a.Destroyed)
return;
if (a == self && !info.AffectsParent)
return;
var stance = self.Owner.Stances[a.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = a.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.GrantUpgrade(a, u, this);
}
public void UnitProducedByOther(Actor self, Actor producer, Actor produced)
{
// Work around for actors produced within the region not triggering until the second tick
if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= info.Range.Range * info.Range.Range)
{
var stance = self.Owner.Stances[produced.Owner];
if (!info.ValidStances.HasFlag(stance))
return;
var um = produced.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.GrantTimedUpgrade(produced, u, 1);
}
}
void ActorExited(Actor a)
{
if (a == self || a.Destroyed)
return;
var um = a.TraitOrDefault<UpgradeManager>();
if (um != null)
foreach (var u in info.Upgrades)
um.RevokeUpgrade(a, u, this);
}
}
}

View File

@@ -0,0 +1,76 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
[Desc("Renders an arbitrary circle when selected or placing a structure")]
class WithRangeCircleInfo : ITraitInfo, IPlaceBuildingDecoration
{
[Desc("Type of range circle. used to decide which circles to draw on other structures during building placement.")]
public readonly string Type = null;
[Desc("Color of the circle")]
public readonly Color Color = Color.FromArgb(128, Color.White);
[Desc("Range of the circle")]
public readonly WRange Range = WRange.Zero;
public IEnumerable<IRenderable> Render(WorldRenderer wr, World w, ActorInfo ai, WPos centerPosition)
{
yield return new RangeCircleRenderable(
centerPosition,
Range,
0,
Color,
Color.FromArgb(96, Color.Black)
);
foreach (var a in w.ActorsWithTrait<WithRangeCircle>())
if (a.Actor.Owner == a.Actor.World.LocalPlayer && a.Trait.Info.Type == Type)
foreach (var r in a.Trait.RenderAfterWorld(wr))
yield return r;
}
public object Create(ActorInitializer init) { return new WithRangeCircle(init.self, this); }
}
class WithRangeCircle : IPostRenderSelection
{
public readonly WithRangeCircleInfo Info;
readonly Actor self;
public WithRangeCircle(Actor self, WithRangeCircleInfo info)
{
this.self = self;
Info = info;
}
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr)
{
if (self.Owner != self.World.LocalPlayer)
yield break;
yield return new RangeCircleRenderable(
self.CenterPosition,
Info.Range,
0,
Info.Color,
Color.FromArgb(96, Color.Black)
);
}
}
}

View File

@@ -44,6 +44,11 @@
Demolishable:
ScriptTriggers:
WithMakeAnimation:
UpgradeManager:
Cloak@CLOAKGENERATOR:
RequiresUpgrade: cloakgenerator
InitialDelay: 0
CloakDelay: 90
^Wall:
AppearsOnRadar:
@@ -82,6 +87,11 @@
LuaScriptEvents:
Demolishable:
ScriptTriggers:
UpgradeManager:
Cloak@CLOAKGENERATOR:
RequiresUpgrade: cloakgenerator
InitialDelay: 0
CloakDelay: 90
^Infantry:
AppearsOnRadar:
@@ -158,6 +168,10 @@
DeathSound: Zapped
DeathTypes: 6
UpgradeManager:
Cloak@CLOAKGENERATOR:
RequiresUpgrade: cloakgenerator
InitialDelay: 0
CloakDelay: 90
^CivilianInfantry:
Inherits: ^Infantry
@@ -253,6 +267,10 @@
TimedUpgradeBar@EMPDISABLE:
Upgrade: empdisable
Color: 255,255,255
Cloak@CLOAKGENERATOR:
RequiresUpgrade: cloakgenerator
InitialDelay: 0
CloakDelay: 90
^Tank:
AppearsOnRadar:
@@ -321,6 +339,10 @@
TimedUpgradeBar@EMPDISABLE:
Upgrade: empdisable
Color: 255,255,255
Cloak@CLOAKGENERATOR:
RequiresUpgrade: cloakgenerator
InitialDelay: 0
CloakDelay: 90
^Helicopter:
AppearsOnRadar:

View File

@@ -903,6 +903,44 @@ GADEPT:
Power:
Amount: -30
NASTLH:
Inherits: ^Building
Valued:
Cost: 2500
Tooltip:
Name: Stealth Generator
Description: Generates a cloaking field
Buildable:
BuildPaletteOrder: 80
Prerequisites: proc,natech
Owner: nod
Queue: Building
Building:
Footprint: xxx xxx
Dimensions: 3,2
Health:
HP: 600
Armor:
Type: Wood
RevealsShroud:
Range: 6c0
WithIdleOverlay@pulse:
Sequence: pulse
PauseOnLowPower: true
WithRangeCircle:
Range: 12c0
Type: cloakgenerator
Power:
Amount: -350
RequiresPower:
CanPowerDown:
UpgradeActorsNear:
Upgrades: cloakgenerator
Range: 12c0
EnableSound: cloak5.aud
DisableSound: cloak5.aud
AffectsParent: true
#TODO: Placeholder, replace with Component Tower + Vulcan Upgrade
GAVULC:
Inherits: ^Building

View File

@@ -718,6 +718,31 @@ napuls:
icon: pulsicon
Start: 0
nastlh:
idle: ntstlh
Start: 0
ShadowStart: 3
damaged-idle: ntstlh
Start: 1
ShadowStart: 4
critical-idle: ntstlh
Start: 2
ShadowStart: 5
pulse: ntstlh_a
Start: 0
Length: 4
Tick: 480
damaged-pulse: ntstlh_a
Start: 4
Length: 4
Tick: 480
make: ntstlhmk
Start: 0
Length: 18
ShadowStart: 20
icon: clckicon
Start: 0
gavulc:
idle: gtctwr
Start: 0