Files
OpenRA/OpenRA.Mods.RA/Modifiers/FrozenUnderFog.cs
RoosterDragon a512d9ad0a Sped up shroud rendering.
- Only update shroud within the visible screen area, rather than the whole map. This improves performance on larger maps significantly when scrolling around since large portions of the shroud do not need to be updated.
- Provide methods in Shroud to return delegates to check for explored/visibility for tiles within a certain region. This allows it to return more efficient delegates whenever the region is within the map bounds, or shroud/fog is disabled. In the typical case where the region is in bounds and shroud/fog is enabled, the fast check is almost twice as fast as the slow check.
- Use the Shroud delegate functions in shroud rendering, frozen actors, minimap rendering and resource layer areas to provide a speedup since these areas of code can often take advantage of the fact they perform checks within the map boundary.
- Cache current element in CellRegionEnumerator to prevent repeated work if the element is accessed more than once.
- Decrease the size of elements in some arrays in hopes of reducing memory needs and improving cache hits.
2014-07-07 17:34:31 +01:00

125 lines
3.4 KiB
C#

#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 System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class FrozenUnderFogInfo : ITraitInfo, Requires<BuildingInfo>, Requires<RenderSpritesInfo>
{
public readonly bool StartsRevealed = false;
public object Create(ActorInitializer init) { return new FrozenUnderFog(init, this); }
}
public class FrozenUnderFog : IRenderModifier, IVisibilityModifier, ITick, ITickRender, ISync
{
[Sync] public int VisibilityHash;
readonly bool startsRevealed;
readonly CPos[] footprint;
readonly CellRegion footprintRegion;
readonly Lazy<IToolTip> tooltip;
readonly Lazy<Health> health;
readonly Dictionary<Player, bool> visible;
readonly Dictionary<Player, FrozenActor> frozen;
bool initialized;
public FrozenUnderFog(ActorInitializer init, FrozenUnderFogInfo info)
{
// Spawned actors (e.g. building husks) shouldn't be revealed
startsRevealed = info.StartsRevealed && !init.Contains<ParentActorInit>();
footprint = FootprintUtils.Tiles(init.self).ToArray();
footprintRegion = CellRegion.BoundingRegion(init.world.Map.TileShape, footprint);
tooltip = Exts.Lazy(() => init.self.TraitsImplementing<IToolTip>().FirstOrDefault());
tooltip = Exts.Lazy(() => init.self.TraitsImplementing<IToolTip>().FirstOrDefault());
health = Exts.Lazy(() => init.self.TraitOrDefault<Health>());
frozen = new Dictionary<Player, FrozenActor>();
visible = init.world.Players.ToDictionary(p => p, p => false);
}
public bool IsVisible(Actor self, Player byPlayer)
{
return byPlayer == null || visible[byPlayer];
}
public void Tick(Actor self)
{
if (self.Destroyed)
return;
VisibilityHash = 0;
foreach (var p in self.World.Players)
{
var isVisible = footprint.Any(p.Shroud.IsVisibleTest(footprintRegion));
visible[p] = isVisible;
if (isVisible)
VisibilityHash += p.ClientIndex;
}
if (!initialized)
{
foreach (var p in self.World.Players)
{
visible[p] |= startsRevealed;
p.PlayerActor.Trait<FrozenActorLayer>().Add(frozen[p] = new FrozenActor(self, footprint, footprintRegion));
}
initialized = true;
}
foreach (var player in self.World.Players)
{
if (!visible[player])
continue;
var actor = frozen[player];
actor.Owner = self.Owner;
if (health.Value != null)
{
actor.HP = health.Value.HP;
actor.DamageState = health.Value.DamageState;
}
if (tooltip.Value != null)
{
actor.TooltipName = tooltip.Value.Name();
actor.TooltipOwner = tooltip.Value.Owner();
}
}
}
public void TickRender(WorldRenderer wr, Actor self)
{
if (self.Destroyed || !initialized || !visible.Values.Any(v => v))
return;
var renderables = self.Render(wr).ToArray();
foreach (var player in self.World.Players)
if (visible[player])
frozen[player].Renderables = renderables;
}
public IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r)
{
return IsVisible(self, self.World.RenderPlayer) ? r : SpriteRenderable.None;
}
}
}