Merge pull request #2507 from Lookingglass/per-player-shrouds

Per-player shrouds
This commit is contained in:
Chris Forbes
2012-12-22 12:26:40 -08:00
50 changed files with 353 additions and 159 deletions

View File

@@ -54,6 +54,9 @@ namespace OpenRA.Mods.RA.Activities
if (!Target.IsValid)
return NextActivity;
if (!self.Owner.HasFogVisibility() && Target.Actor != null && Target.Actor.HasTrait<Mobile>() && !self.Owner.Shroud.IsTargetable(Target.Actor))
return NextActivity;
if (targetable != null && !targetable.TargetableBy(Target.Actor, self))
return NextActivity;

View File

@@ -50,6 +50,7 @@ namespace OpenRA.Mods.RA
{
return new TeslaAttack( newTarget );
}
public override void ResolveOrder(Actor self, Order order)
{

View File

@@ -111,11 +111,21 @@ namespace OpenRA.Mods.RA
var inRange = self.World.FindUnitsInCircle(self.CenterLocation, (int)(Game.CellSize * range));
return inRange
.Where(a => a.AppearsHostileTo(self))
.Where(a => !a.HasTrait<AutoTargetIgnore>())
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.ClosestTo( self.CenterLocation );
if (self.Owner.HasFogVisibility()) {
return inRange
.Where(a => a.AppearsHostileTo(self))
.Where(a => !a.HasTrait<AutoTargetIgnore>())
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.ClosestTo( self.CenterLocation );
}
else {
return inRange
.Where(a => a.AppearsHostileTo(self))
.Where(a => !a.HasTrait<AutoTargetIgnore>())
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.Where(a => self.Owner.Shroud.IsTargetable(a))
.ClosestTo( self.CenterLocation );
}
}
}

View File

@@ -78,7 +78,7 @@ namespace OpenRA.Mods.RA.Buildings
{
if (!cliprect.Contains(kv.Key.X, kv.Key.Y))
continue;
if (!world.LocalShroud.IsExplored(kv.Key))
if (!world.RenderedShroud.IsExplored(kv.Key))
continue;
bibSprites[kv.Value.type - 1][kv.Value.index].DrawAt(wr, kv.Key.ToPPos().ToFloat2(), "terrain");

View File

@@ -97,7 +97,7 @@ namespace OpenRA.Mods.RA
if (chargeTick <= 0 // Can jump
&& (self.Location - xy).Length <= Info.JumpDistance // Within jump range
&& movement.CanEnterCell(xy) // Can enter cell
&& (ignoreVis || self.World.LocalShroud.IsExplored(xy))) // Not in shroud
&& (ignoreVis || self.Owner.Shroud.IsExplored(xy))) // Not in shroud
return true;
else
return false;

View File

@@ -44,12 +44,10 @@ namespace OpenRA.Mods.RA
}
// Can't be used in synced code, except with ignoreVis.
public virtual bool CanChronoshiftTo(Actor self, CPos targetLocation, bool ignoreVis)
public virtual bool CanChronoshiftTo(Actor self, CPos targetLocation)
{
// Todo: Allow enemy units to be chronoshifted into bad terrain to kill them
return self.HasTrait<ITeleportable>() &&
self.Trait<ITeleportable>().CanEnterCell(targetLocation) &&
(ignoreVis || self.World.LocalShroud.IsExplored(targetLocation));
return (self.HasTrait<ITeleportable>() && self.Trait<ITeleportable>().CanEnterCell(targetLocation));
}
public virtual bool Teleport(Actor self, CPos targetLocation, int duration, bool killCargo, Actor chronosphere)

View File

@@ -92,14 +92,27 @@ namespace OpenRA.Mods.RA
lastPos = self.Location;
}
}
public bool IsVisible(Actor self)
{
if (!Cloaked || self.Owner == self.World.LocalPlayer ||
self.World.LocalPlayer == null ||
self.Owner.Stances[self.World.LocalPlayer] == Stance.Ally)
return true;
return IsVisible(null, self);
}
public bool IsVisible(Shroud s, Actor self)
{
if (self.World.LocalPlayer != null) {
if (s == null) {
if (!Cloaked || self.Owner == self.World.LocalPlayer ||
self.Owner.Stances[self.World.LocalPlayer] == Stance.Ally)
return true;
}
else {
if (!Cloaked || self.Owner == s.Owner ||
self.Owner.Stances[s.Owner] == Stance.Ally)
return true;
}
}
return self.World.ActorsWithTrait<DetectCloaked>().Any(a =>
a.Actor.Owner.Stances[self.Owner] != Stance.Ally &&
(self.Location - a.Actor.Location).Length < a.Actor.Info.Traits.Get<DetectCloakedInfo>().Range);

View File

@@ -62,7 +62,7 @@ namespace OpenRA.Mods.RA
if (self.Owner == self.World.LocalPlayer)
{
self.World.LocalShroud.Disabled = true;
self.World.RenderedShroud.Disabled = true;
Game.RunAfterDelay(Info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(self.World))
@@ -79,7 +79,7 @@ namespace OpenRA.Mods.RA
Game.Debug("{0} is victorious.".F(self.Owner.PlayerName));
if (self.Owner == self.World.LocalPlayer)
{
self.World.LocalShroud.Disabled = true;
self.World.RenderedShroud.Disabled = true;
Game.RunAfterDelay(Info.NotificationDelay, () => Sound.PlayNotification(self.Owner, "Speech", "Win", self.Owner.Country.Race));
}
}

View File

@@ -25,8 +25,7 @@ namespace OpenRA.Mods.RA
public override int GetSelectionShares (Actor collector)
{
// don't ever hide the map for people who have GPS.
var gpsWatcher = collector.Owner.PlayerActor.TraitOrDefault<GpsWatcher>();
if (gpsWatcher != null && (gpsWatcher.Granted || gpsWatcher.GrantedAllies))
if (collector.Owner.HasFogVisibility())
return 0;
return base.GetSelectionShares (collector);
@@ -36,7 +35,7 @@ namespace OpenRA.Mods.RA
{
base.Activate(collector);
if (collector.Owner == collector.World.LocalPlayer)
collector.World.WorldActor.Trait<Shroud>().ResetExploration();
collector.Owner.Shroud.ResetExploration();
}
}
}

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Mods.RA
base.Activate(collector);
if (ShouldReveal( collector.Owner ))
collector.World.WorldActor.Trait<Shroud>().ExploreAll(collector.World);
collector.Owner.Shroud.ExploreAll(collector.World);
}
}
}

View File

@@ -153,7 +153,7 @@ namespace OpenRA.Mods.RA.Effects
var altitude = float2.Lerp(Args.srcAltitude, Args.destAltitude, at);
var pos = float2.Lerp(Args.src.ToFloat2(), Args.dest.ToFloat2(), at) - new float2(0, altitude);
if (Args.firedBy.World.LocalShroud.IsVisible(((PPos) pos.ToInt2()).ToCPos()))
if (Args.firedBy.World.RenderedShroud.IsVisible(((PPos) pos.ToInt2()).ToCPos()))
{
if (Info.High || Info.Angle > 0)
{

View File

@@ -91,8 +91,8 @@ namespace OpenRA.Mods.RA
var conPos = positions[i];
var nextPos = positions[i - 1];
if (self.World.LocalShroud.IsVisible(conPos.ToCPos()) ||
self.World.LocalShroud.IsVisible(nextPos.ToCPos()))
if (self.World.RenderedShroud.IsVisible(conPos.ToCPos()) ||
self.World.RenderedShroud.IsVisible(nextPos.ToCPos()))
{
Game.Renderer.WorldLineRenderer.DrawLine(conPos.ToFloat2(), nextPos.ToFloat2(), trailStart, trailEnd);

View File

@@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA.Effects
if (
self.IsInWorld
&& (watcher.Granted || watcher.GrantedAllies)
&& !self.Trait<HiddenUnderFog>().IsVisible(self)
&& !self.Trait<HiddenUnderFog>().IsVisible(self.World.RenderedShroud, self) // WRONG
&& (!self.HasTrait<Cloak>() || !self.Trait<Cloak>().Cloaked)
&& (!self.HasTrait<Spy>() || !self.Trait<Spy>().Disguised)
)

View File

@@ -154,7 +154,7 @@ namespace OpenRA.Mods.RA.Effects
public IEnumerable<Renderable> Render()
{
if (Args.firedBy.World.LocalShroud.IsVisible(PxPosition.ToCPos()))
if (Args.firedBy.World.RenderedShroud.IsVisible(PxPosition.ToCPos()))
yield return new Renderable(anim.Image, PxPosition.ToFloat2() - 0.5f * anim.Image.size - new float2(0, Altitude),
Args.weapon.Underwater ? "shadow" : "effect", PxPosition.Y);

View File

@@ -428,7 +428,7 @@ namespace OpenRA.Mods.RA
public bool CanTargetLocation(Actor self, CPos location, List<Actor> actorsAtLocation, bool forceAttack, bool forceQueued, ref string cursor)
{
// Don't leak info about resources under the shroud
if (!self.World.LocalShroud.IsExplored(location)) return false;
if (!self.Owner.Shroud.IsExplored(location)) return false;
var res = self.World.WorldActor.Trait<ResourceLayer>().GetResource(location);
var info = self.Info.Traits.Get<HarvesterInfo>();

View File

@@ -20,18 +20,9 @@ namespace OpenRA.Mods.RA
{
public void OnInfiltrate(Actor self, Actor spy)
{
/* todo: changes for per-player shrouds:
* - apply this everywhere, not just on the victim's client
* - actually steal their exploration before resetting it
*/
if (self.World.LocalPlayer != null && self.World.LocalPlayer.Stances[self.Owner] == Stance.Ally)
{
var gpsWatcher = self.Owner.PlayerActor.TraitOrDefault<GpsWatcher>();
if (gpsWatcher != null && (gpsWatcher.Granted || gpsWatcher.GrantedAllies))
return;
self.Owner.Shroud.ResetExploration();
}
spy.Owner.Shroud.MergeShroud(self.Owner.Shroud);
if (!self.Owner.HasFogVisibility())
self.Owner.Shroud.ResetExploration();
}
}
}

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Mods.RA
class InvisibleToEnemy : IRenderModifier, IVisibilityModifier, IRadarColorModifier
{
public bool IsVisible(Actor self)
public bool IsVisible(Shroud s, Actor self)
{
return self.World.LocalPlayer == null ||
self.Owner.Stances[self.World.LocalPlayer] == Stance.Ally;
@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA
public IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> r)
{
return IsVisible(self) ? r : Nothing;
return IsVisible(self.Owner.Shroud, self) ? r : Nothing;
}
}
}

View File

@@ -18,15 +18,15 @@ namespace OpenRA.Mods.RA
class FrozenUnderFog : IRenderModifier, IVisibilityModifier
{
public bool IsVisible(Actor self)
public bool IsVisible(Shroud s, Actor self)
{
return Shroud.GetVisOrigins(self).Any(o => self.World.LocalShroud.IsVisible(o));
return Shroud.GetVisOrigins(self).Any(o => s.IsVisible(o));
}
Renderable[] cache = { };
public IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> r)
{
if (IsVisible(self))
if (IsVisible(self.World.RenderedShroud, self))
cache = r.ToArray();
return cache;
}

View File

@@ -18,15 +18,15 @@ namespace OpenRA.Mods.RA
class HiddenUnderFog : IRenderModifier, IVisibilityModifier
{
public bool IsVisible(Actor self)
public bool IsVisible(Shroud s, Actor self)
{
return Shroud.GetVisOrigins(self).Any(o => self.World.LocalShroud.IsVisible(o));
return Shroud.GetVisOrigins(self).Any(o => s.IsVisible(o));
}
static Renderable[] Nothing = { };
public IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> r)
{
return IsVisible(self) ? r : Nothing;
return IsVisible(self.World.RenderedShroud, self) ? r : Nothing;
}
}
}

View File

@@ -276,6 +276,7 @@
<Compile Include="Player\PlaceBuilding.cs" />
<Compile Include="Player\ProductionQueue.cs" />
<Compile Include="Player\TechTree.cs" />
<Compile Include="PlayerExts.cs" />
<Compile Include="PrimaryBuilding.cs" />
<Compile Include="Production.cs" />
<Compile Include="ProductionBar.cs" />

View File

@@ -0,0 +1,25 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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 OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public static class PlayerExts
{
public static bool HasFogVisibility( this Player a )
{
var gpsWatcher = a.PlayerActor.TraitOrDefault<GpsWatcher>();
return gpsWatcher != null && (gpsWatcher.Granted || gpsWatcher.GrantedAllies);
}
}
}

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA
public float GetValue()
{
// only people we like should see our production status.
if (self.World.LocalPlayer != null && self.Owner.Stances[self.World.LocalPlayer] != Stance.Ally)
if (self.World.RenderedPlayer != null && self.Owner.Stances[self.World.RenderedPlayer] != Stance.Ally)
return 0;
var queue = self.TraitsImplementing<ProductionQueue>().FirstOrDefault(q => q.CurrentItem() != null);

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Scripting
var target = kv.First;
var targetCell = kv.Second;
var cs = target.Trait<Chronoshiftable>();
if (cs.CanChronoshiftTo(target, targetCell, true))
if (chronosphere.Owner.Shroud.IsExplored(targetCell) && cs.CanChronoshiftTo(target, targetCell))
cs.Teleport(target, targetCell, duration, killCargo,chronosphere);
}
}

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA
var altitude = new PVecInt(0, move.Altitude);
position = (self.CenterLocation - Combat.GetTurretPosition(self, facing, smokeTurret));
if (self.World.LocalShroud.IsVisible(position.ToCPos()))
if (self.World.RenderedShroud.IsVisible(position.ToCPos()))
self.World.AddFrameEndTask(
w => w.Add(new Smoke(w, position - altitude, "smokey")));
}

View File

@@ -45,9 +45,8 @@ namespace OpenRA.Mods.RA
var targetCell = target.Location + (order.TargetLocation - order.ExtraLocation);
var cpi = Info as ChronoshiftPowerInfo;
if (cs.CanChronoshiftTo(target, targetCell, true))
cs.Teleport(target, targetCell,
cpi.Duration * 25, cpi.KillCargo, self);
if (self.Owner.Shroud.IsExplored(targetCell) && cs.CanChronoshiftTo(target, targetCell))
cs.Teleport(target, targetCell, cpi.Duration * 25, cpi.KillCargo, self);
}
}
@@ -61,6 +60,48 @@ namespace OpenRA.Mods.RA
return units.Distinct().Where(a => a.HasTrait<Chronoshiftable>());
}
public bool SimilarTerrain(CPos xy, CPos sourceLocation) {
if (!self.Owner.Shroud.IsExplored(xy))
return false;
int range = (Info as ChronoshiftPowerInfo).Range;
var sourceTiles = self.World.FindTilesInCircle(xy, range);
var destTiles = self.World.FindTilesInCircle(sourceLocation, range);
List<string> sourceTerrain = new List<string>();
List<string> destTerrain = new List<string>();
int j = 0;
foreach (var t in sourceTiles) {
j = j + 1;
if (!self.Owner.Shroud.IsExplored(t))
return false;
sourceTerrain.Add(self.World.GetTerrainType( t ));
}
j = 0;
foreach (var t in destTiles) {
j = j + 1;
if (!self.Owner.Shroud.IsExplored(t)) {
return false;
}
self.World.GetTerrainType( t );
destTerrain.Add(self.World.GetTerrainType( t ));
}
// HACK but I don't want to write a comparison function
if (sourceTerrain.Count != destTerrain.Count)
return false;
for (int i = 0; i < sourceTerrain.Count; i++) {
if (!sourceTerrain[i].Equals(destTerrain[i]))
return false;
}
return true;
}
class SelectTarget : IOrderGenerator
{
@@ -99,8 +140,11 @@ namespace OpenRA.Mods.RA
{
var xy = Game.viewport.ViewToWorld(Viewport.LastMousePos);
var targetUnits = power.UnitsInRange(xy);
foreach (var unit in targetUnits)
wr.DrawSelectionBox(unit, Color.Red);
foreach (var unit in targetUnits) {
if (manager.self.Owner.Shroud.IsTargetable(unit) || manager.self.Owner.HasFogVisibility()) {
wr.DrawSelectionBox(unit, Color.Red);
}
}
}
public void RenderBeforeWorld(WorldRenderer wr, World world)
@@ -175,8 +219,11 @@ namespace OpenRA.Mods.RA
public void RenderAfterWorld(WorldRenderer wr, World world)
{
foreach (var unit in power.UnitsInRange(sourceLocation))
wr.DrawSelectionBox(unit, Color.Red);
foreach (var unit in power.UnitsInRange(sourceLocation)) {
if (manager.self.Owner.Shroud.IsTargetable(unit) || manager.self.Owner.HasFogVisibility()) {
wr.DrawSelectionBox(unit, Color.Red);
}
}
}
public void RenderBeforeWorld(WorldRenderer wr, World world)
@@ -194,20 +241,24 @@ namespace OpenRA.Mods.RA
// Unit previews
foreach (var unit in power.UnitsInRange(sourceLocation))
{
var targetCell = unit.Location + (xy - sourceLocation);
foreach (var r in unit.Render())
r.Sprite.DrawAt(r.Pos - Traits.Util.CenterOfCell(unit.Location).ToFloat2() + Traits.Util.CenterOfCell(targetCell).ToFloat2(),
wr.GetPaletteIndex(r.Palette),
r.Scale*r.Sprite.size);
if (manager.self.Owner.Shroud.IsTargetable(unit)) {
var targetCell = unit.Location + (xy - sourceLocation);
foreach (var r in unit.Render())
r.Sprite.DrawAt(r.Pos - Traits.Util.CenterOfCell(unit.Location).ToFloat2() + Traits.Util.CenterOfCell(targetCell).ToFloat2(),
wr.GetPaletteIndex(r.Palette),
r.Scale*r.Sprite.size);
}
}
// Unit tiles
foreach (var unit in power.UnitsInRange(sourceLocation))
{
var targetCell = unit.Location + (xy - sourceLocation);
var canEnter = unit.Trait<Chronoshiftable>().CanChronoshiftTo(unit,targetCell, false);
var tile = canEnter ? validTile : invalidTile;
tile.DrawAt( wr, targetCell.ToPPos().ToFloat2(), "terrain" );
if (manager.self.Owner.Shroud.IsTargetable(unit)) {
var targetCell = unit.Location + (xy - sourceLocation);
var canEnter = ((manager.self.Owner.Shroud.IsExplored(targetCell) || manager.self.Owner.HasFogVisibility())&& unit.Trait<Chronoshiftable>().CanChronoshiftTo(unit,targetCell));
var tile = canEnter ? validTile : invalidTile;
tile.DrawAt( wr, targetCell.ToPPos().ToFloat2(), "terrain" );
}
}
}
@@ -217,12 +268,18 @@ namespace OpenRA.Mods.RA
foreach (var unit in power.UnitsInRange(sourceLocation))
{
var targetCell = unit.Location + (xy - sourceLocation);
if (unit.Trait<Chronoshiftable>().CanChronoshiftTo(unit,targetCell, false))
if (manager.self.Owner.Shroud.IsExplored(targetCell) && unit.Trait<Chronoshiftable>().CanChronoshiftTo(unit,targetCell))
{
canTeleport = true;
break;
}
}
if (!canTeleport) {
// Check the terrain types. This will allow chronoshifts to occur on empty terrain to terrain of
// a similar type. This also keeps the cursor from changing in non-visible property, alerting the
// chronoshifter of enemy unit presence
canTeleport = power.SimilarTerrain(sourceLocation,xy);
}
return canTeleport;
}

View File

@@ -58,15 +58,12 @@ namespace OpenRA.Mods.RA
public void RefreshGps(Actor atek)
{
RefreshGranted();
foreach (TraitPair<GpsWatcher> i in atek.World.ActorsWithTrait<GpsWatcher>())
i.Trait.RefreshGranted();
if (atek.World.LocalPlayer == null)
return;
if ((Granted || GrantedAllies) && (atek.World.LocalPlayer.Stances[atek.Owner] == Stance.Ally))
atek.World.WorldActor.Trait<Shroud>().ExploreAll(atek.World);
if ((Granted || GrantedAllies) && atek.World.LocalPlayer != null && (atek.World.LocalPlayer.Stances[atek.Owner] == Stance.Ally))
atek.Owner.Shroud.ExploreAll(atek.World);
}
void RefreshGranted()
@@ -74,6 +71,9 @@ namespace OpenRA.Mods.RA
Granted = (actors.Count > 0 && Launched);
GrantedAllies = owner.World.ActorsWithTrait<GpsWatcher>().Any(p =>
p.Actor.Owner.Stances[owner] == Stance.Ally && p.Trait.Granted);
if (Granted || GrantedAllies)
owner.Shroud.ExploreAll(owner.World);
}
}

View File

@@ -131,32 +131,38 @@ namespace OpenRA.Mods.RA
Key = key;
}
static bool InstanceDisabled(SupportPower sp)
{
return sp.self.TraitsImplementing<IDisable>().Any(d => d.Disabled);
}
bool notifiedCharging;
bool notifiedReady;
public void Tick()
{
Active = !Disabled && Instances.Any(i => !i.self.IsDisabled());
if (!Active)
return;
if (Manager.devMode.FastCharge && RemainingTime > 25)
RemainingTime = 25;
if (RemainingTime > 0) --RemainingTime;
var power = Instances.First();
if (!notifiedCharging)
if (Active)
{
power.Charging(power.self, Key);
notifiedCharging = true;
}
var power = Instances.First();
if (Manager.devMode.FastCharge && RemainingTime > 25)
RemainingTime = 25;
if (RemainingTime == 0 && !notifiedReady)
{
power.Charged(power.self, Key);
notifiedReady = true;
if (RemainingTime > 0) --RemainingTime;
if (!notifiedCharging)
{
power.Charging(power.self, Key);
notifiedCharging = true;
}
if (RemainingTime == 0
&& !notifiedReady)
{
power.Charged(power.self, Key);
notifiedReady = true;
}
}
}
@@ -173,7 +179,7 @@ namespace OpenRA.Mods.RA
if (!Ready)
return;
var power = Instances.First(i => !i.self.IsDisabled());
var power = Instances.First(i => !InstanceDisabled(i));
// Note: order.Subject is the *player* actor
power.Activate(power.self, order);

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.RA.Widgets
var cell = Game.viewport.ViewToWorld(Viewport.LastMousePos);
if (!world.Map.IsInMap(cell)) return;
if (world.LocalPlayer != null && !world.LocalPlayer.Shroud.IsExplored(cell))
if (world.LocalPlayer != null && !world.RenderedShroud.IsExplored(cell))
{
var utext = "Unexplored Terrain";
var usz = Game.Renderer.Fonts["Bold"].Measure(utext) + new int2(20, 24);

View File

@@ -83,7 +83,7 @@ namespace OpenRA.Mods.RA
{
if (!cliprect.Contains(kv.Key.X,kv.Key.Y))
continue;
if (localPlayer != null && !localPlayer.Shroud.IsExplored(kv.Key))
if (localPlayer != null && !world.RenderedShroud.IsExplored(kv.Key))
continue;
smudgeSprites[kv.Value.type- 1][kv.Value.index].DrawAt(wr, kv.Key.ToPPos().ToFloat2(), "terrain");