Move some Building traits and related elements to Mods.Common

This commit is contained in:
reaperrr
2014-12-26 02:34:49 +01:00
parent 2f67a88b93
commit 9dfd369446
44 changed files with 61 additions and 35 deletions

View File

@@ -122,10 +122,17 @@
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Buildings\BaseBuilding.cs" />
<Compile Include="Traits\Buildings\BaseProvider.cs" />
<Compile Include="Traits\Buildings\Bib.cs" />
<Compile Include="Traits\Buildings\Building.cs" />
<Compile Include="Traits\Buildings\BuildingInfluence.cs" />
<Compile Include="Traits\Buildings\BuildingUtils.cs" />
<Compile Include="Traits\Buildings\DeadBuildingState.cs" />
<Compile Include="Traits\Buildings\FreeActor.cs" />
<Compile Include="Traits\Buildings\LineBuild.cs" />
<Compile Include="Traits\Buildings\LineBuildNode.cs" />
<Compile Include="Traits\Buildings\RallyPoint.cs" />
<Compile Include="Traits\Buildings\RepairsUnits.cs" />
<Compile Include="Traits\Buildings\FootprintUtils.cs" />
<Compile Include="Traits\Burns.cs" />
<Compile Include="Traits\Huntable.cs" />
<Compile Include="Traits\CustomBuildTimeValue.cs" />
@@ -137,6 +144,7 @@
<Compile Include="Traits\JamsMissiles.cs" />
<Compile Include="Traits\KillsSelf.cs" />
<Compile Include="Traits\Modifiers\DisabledOverlay.cs" />
<Compile Include="Traits\Modifiers\FrozenUnderFog.cs" />
<Compile Include="Traits\Modifiers\HiddenUnderFog.cs" />
<Compile Include="Traits\Modifiers\UpgradeOverlay.cs" />
<Compile Include="Traits\MustBeDestroyed.cs" />
@@ -162,6 +170,7 @@
<Compile Include="Traits\Power\ScalePowerWithHealth.cs" />
<Compile Include="Traits\ProvidesRadar.cs" />
<Compile Include="Traits\RadarColorFromTerrain.cs" />
<Compile Include="Traits\Render\RenderBuilding.cs" />
<Compile Include="Traits\Render\RenderEditorOnly.cs" />
<Compile Include="Traits\Render\RenderFlare.cs" />
<Compile Include="Traits\Render\RenderNameTag.cs" />
@@ -170,6 +179,7 @@
<Compile Include="Traits\Render\RenderUnit.cs" />
<Compile Include="Traits\Render\TimedUpgradeBar.cs" />
<Compile Include="Traits\Render\WithBuildingPlacedAnimation.cs" />
<Compile Include="Traits\Render\WithMakeAnimation.cs" />
<Compile Include="Traits\Render\WithCrateBody.cs" />
<Compile Include="Traits\Render\WithDeathAnimation.cs" />
<Compile Include="Traits\Render\WithHarvestAnimation.cs" />
@@ -200,6 +210,7 @@
<Compile Include="Traits\World\RadarPings.cs" />
<Compile Include="Traits\World\ResourceClaim.cs" />
<Compile Include="Traits\World\ResourceClaimLayer.cs" />
<Compile Include="Traits\World\ResourceLayer.cs" />
<Compile Include="Traits\World\ShroudPalette.cs" />
<Compile Include="Traits\World\ShroudRenderer.cs" />
<Compile Include="Traits\World\SmudgeLayer.cs" />

View File

@@ -0,0 +1,77 @@
#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 OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class BibInfo : ITraitInfo, Requires<BuildingInfo>, Requires<RenderSpritesInfo>
{
public readonly string Sequence = "bib";
public readonly string Palette = "terrain";
public readonly bool HasMinibib = false;
public object Create(ActorInitializer init) { return new Bib(init.self, this); }
}
public class Bib : INotifyAddedToWorld, INotifyRemovedFromWorld
{
readonly BibInfo info;
readonly RenderSprites rs;
readonly BuildingInfo bi;
public Bib(Actor self, BibInfo info)
{
this.info = info;
rs = self.Trait<RenderSprites>();
bi = self.Info.Traits.Get<BuildingInfo>();
}
public void AddedToWorld(Actor self)
{
var width = bi.Dimensions.X;
var bibOffset = bi.Dimensions.Y - 1;
var centerOffset = FootprintUtils.CenterOffset(self.World, bi);
var location = self.Location;
var rows = info.HasMinibib ? 1 : 2;
var map = self.World.Map;
for (var i = 0; i < rows * width; i++)
{
var index = i;
var anim = new Animation(self.World, rs.GetImage(self));
var cellOffset = new CVec(i % width, i / width + bibOffset);
var cell = location + cellOffset;
// Some mods may define terrain-specific bibs
var terrain = map.GetTerrainInfo(cell).Type;
var testSequence = info.Sequence + "-" + terrain;
var sequence = anim.HasSequence(testSequence) ? testSequence : info.Sequence;
anim.PlayFetchIndex(sequence, () => index);
anim.IsDecoration = true;
// Z-order is one set to the top of the footprint
var offset = self.World.Map.CenterOfCell(cell) - self.World.Map.CenterOfCell(location) - centerOffset;
var awo = new AnimationWithOffset(anim, () => offset, null, -(offset.Y + centerOffset.Y + 512));
rs.Add("bib_{0}".F(i), awo, info.Palette);
}
}
public void RemovedFromWorld(Actor self)
{
var width = bi.Dimensions.X;
var rows = info.HasMinibib ? 1 : 2;
for (var i = 0; i < rows * width; i++)
rs.Remove("bib_{0}".F(i));
}
}
}

View File

@@ -0,0 +1,190 @@
#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.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Remove this trait to limit base-walking by cheap or defensive buildings.")]
public class GivesBuildableAreaInfo : TraitInfo<GivesBuildableArea> {}
public class GivesBuildableArea {}
public class BuildingInfo : ITraitInfo, IOccupySpaceInfo, UsesInit<LocationInit>
{
[Desc("Where you are allowed to place the building (Water, Clear, ...)")]
public readonly string[] TerrainTypes = {};
[Desc("The range to the next building it can be constructed. Set it higher for walls.")]
public readonly int Adjacent = 2;
[Desc("x means space it blocks, _ is a part that is passable by actors.")]
public readonly string Footprint = "x";
public readonly CVec Dimensions = new CVec(1, 1);
public readonly bool RequiresBaseProvider = false;
public readonly bool AllowInvalidPlacement = false;
public readonly string[] BuildSounds = { "placbldg.aud", "build5.aud" };
public readonly string[] UndeploySounds = { "cashturn.aud" };
public object Create(ActorInitializer init) { return new Building(init, this); }
public Actor FindBaseProvider(World world, Player p, CPos topLeft)
{
var center = world.Map.CenterOfCell(topLeft) + FootprintUtils.CenterOffset(world, this);
foreach (var bp in world.ActorsWithTrait<BaseProvider>())
{
var validOwner = bp.Actor.Owner == p || (world.LobbyInfo.GlobalSettings.AllyBuildRadius && bp.Actor.Owner.Stances[p] == Stance.Ally);
if (!validOwner || !bp.Trait.Ready())
continue;
// Range is counted from the center of the actor, not from each cell.
var target = Target.FromPos(bp.Actor.CenterPosition);
if (target.IsInRange(center, WRange.FromCells(bp.Trait.Info.Range)))
return bp.Actor;
}
return null;
}
public bool IsCloseEnoughToBase(World world, Player p, string buildingName, CPos topLeft)
{
if (p.PlayerActor.Trait<DeveloperMode>().BuildAnywhere)
return true;
if (RequiresBaseProvider && FindBaseProvider(world, p, topLeft) == null)
return false;
var buildingMaxBounds = Dimensions;
var buildingTraits = world.Map.Rules.Actors[buildingName].Traits;
if (buildingTraits.Contains<BibInfo>() && !(buildingTraits.Get<BibInfo>().HasMinibib))
buildingMaxBounds += new CVec(0, 1);
var scanStart = world.Map.Clamp(topLeft - new CVec(Adjacent, Adjacent));
var scanEnd = world.Map.Clamp(topLeft + buildingMaxBounds + new CVec(Adjacent, Adjacent));
var nearnessCandidates = new List<CPos>();
var bi = world.WorldActor.Trait<BuildingInfluence>();
var allyBuildRadius = world.LobbyInfo.GlobalSettings.AllyBuildRadius;
for (var y = scanStart.Y; y < scanEnd.Y; y++)
{
for (var x = scanStart.X; x < scanEnd.X; x++)
{
var pos = new CPos(x, y);
var at = bi.GetBuildingAt(pos);
if (at == null || !at.IsInWorld || !at.HasTrait<GivesBuildableArea>())
continue;
if (at.Owner == p || (allyBuildRadius && at.Owner.Stances[p] == Stance.Ally))
nearnessCandidates.Add(pos);
}
}
var buildingTiles = FootprintUtils.Tiles(world.Map.Rules, buildingName, this, topLeft).ToList();
return nearnessCandidates
.Any(a => buildingTiles
.Any(b => Math.Abs(a.X - b.X) <= Adjacent
&& Math.Abs(a.Y - b.Y) <= Adjacent));
}
}
public class Building : IOccupySpace, INotifySold, INotifyTransform, ISync, ITechTreePrerequisite, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
{
public readonly BuildingInfo Info;
public bool BuildComplete { get; private set; }
[Sync] readonly CPos topLeft;
readonly Actor self;
public readonly bool SkipMakeAnimation;
/* shared activity lock: undeploy, sell, capture, etc */
[Sync] public bool Locked = true;
public bool Lock()
{
if (Locked)
return false;
Locked = true;
return true;
}
public void Unlock() { Locked = false; }
public CPos TopLeft { get { return topLeft; } }
public WPos CenterPosition { get; private set; }
public IEnumerable<string> ProvidesPrerequisites { get { yield return self.Info.Name; } }
public Building(ActorInitializer init, BuildingInfo info)
{
this.self = init.self;
this.topLeft = init.Get<LocationInit, CPos>();
this.Info = info;
occupiedCells = FootprintUtils.UnpathableTiles( self.Info.Name, Info, TopLeft )
.Select(c => Pair.New(c, SubCell.FullCell)).ToArray();
CenterPosition = init.world.Map.CenterOfCell(topLeft) + FootprintUtils.CenterOffset(init.world, Info);
SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>();
}
Pair<CPos, SubCell>[] occupiedCells;
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; }
public void Created(Actor self)
{
if (SkipMakeAnimation || !self.HasTrait<WithMakeAnimation>())
NotifyBuildingComplete(self);
}
public void AddedToWorld(Actor self)
{
self.World.ActorMap.AddInfluence(self, this);
self.World.ActorMap.AddPosition(self, this);
self.World.ScreenMap.Add(self);
}
public void RemovedFromWorld(Actor self)
{
self.World.ActorMap.RemoveInfluence(self, this);
self.World.ActorMap.RemovePosition(self, this);
self.World.ScreenMap.Remove(self);
}
public void NotifyBuildingComplete(Actor self)
{
if (BuildComplete)
return;
BuildComplete = true;
Locked = false;
foreach (var notify in self.TraitsImplementing<INotifyBuildComplete>())
notify.BuildingComplete(self);
}
public void Selling(Actor self)
{
BuildComplete = false;
}
public void Sold(Actor self) { }
public void BeforeTransform(Actor self)
{
foreach (var s in Info.UndeploySounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterPosition);
}
public void OnTransform(Actor self) { }
public void AfterTransform(Actor self) { }
}
}

View File

@@ -0,0 +1,63 @@
#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 OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("A dictionary of buildings placed on the map. Attach this to the world actor.")]
public class BuildingInfluenceInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new BuildingInfluence(init.world); }
}
public class BuildingInfluence
{
CellLayer<Actor> influence;
Map map;
public BuildingInfluence(World world)
{
map = world.Map;
influence = new CellLayer<Actor>(map);
world.ActorAdded += a =>
{
var b = a.TraitOrDefault<Building>();
if (b == null)
return;
foreach (var u in FootprintUtils.Tiles(map.Rules, a.Info.Name, b.Info, a.Location))
if (map.Contains(u) && influence[u] == null)
influence[u] = a;
};
world.ActorRemoved += a =>
{
var b = a.TraitOrDefault<Building>();
if (b == null)
return;
foreach (var u in FootprintUtils.Tiles(map.Rules, a.Info.Name, b.Info, a.Location))
if (map.Contains(u) && influence[u] == a)
influence[u] = null;
};
}
public Actor GetBuildingAt(CPos cell)
{
if (!map.Contains(cell))
return null;
return influence[cell];
}
}
}

View File

@@ -0,0 +1,85 @@
#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.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public static class BuildingUtils
{
public static bool IsCellBuildable(this World world, CPos cell, BuildingInfo bi, Actor toIgnore = null)
{
if (!world.Map.Contains(cell))
return false;
if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(cell) != null)
return false;
if (!bi.AllowInvalidPlacement && world.ActorMap.GetUnitsAt(cell).Any(a => a != toIgnore))
return false;
return bi.TerrainTypes.Contains(world.Map.GetTerrainInfo(cell).Type);
}
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore)
{
if (building.AllowInvalidPlacement)
return true;
var res = world.WorldActor.Trait<ResourceLayer>();
return FootprintUtils.Tiles(world.Map.Rules, name, building, topLeft).All(
t => world.Map.Contains(t) && res.GetResource(t) == null &&
world.IsCellBuildable(t, building, toIgnore));
}
public static IEnumerable<CPos> GetLineBuildCells(World world, CPos location, string name, BuildingInfo bi)
{
var lbi = world.Map.Rules.Actors[name].Traits.Get<LineBuildInfo>();
var topLeft = location; // 1x1 assumption!
if (world.IsCellBuildable(topLeft, bi))
yield return topLeft;
// Start at place location, search outwards
// TODO: First make it work, then make it nice
var vecs = new[] { new CVec(1, 0), new CVec(0, 1), new CVec(-1, 0), new CVec(0, -1) };
int[] dirs = { 0, 0, 0, 0 };
for (var d = 0; d < 4; d++)
{
for (var i = 1; i < lbi.Range; i++)
{
if (dirs[d] != 0)
continue;
var cell = topLeft + i * vecs[d];
if (world.IsCellBuildable(cell, bi))
continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want?
if (world.ActorsWithTrait<LineBuildNode>().Any(a =>
(
a.Actor.Location == cell &&
a.Actor.Info.Traits.Get<LineBuildNodeInfo>().Types.Intersect(lbi.NodeTypes).Any()
)))
dirs[d] = i; // Cell contains actor of correct type
else
dirs[d] = -1; // Cell is blocked by another actor type
}
// Place intermediate-line sections
if (dirs[d] > 0)
for (var i = 1; i < dirs[d]; i++)
yield return topLeft + i * vecs[d];
}
}
}
}

View File

@@ -0,0 +1,71 @@
#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;
namespace OpenRA.Mods.Common.Traits
{
public static class FootprintUtils
{
public static IEnumerable<CPos> Tiles(Ruleset rules, string name, BuildingInfo buildingInfo, CPos topLeft)
{
var dim = (CVec)buildingInfo.Dimensions;
var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x));
var buildingTraits = rules.Actors[name].Traits;
if (buildingTraits.Contains<BibInfo>() && !buildingTraits.Get<BibInfo>().HasMinibib)
{
dim += new CVec(0, 1);
footprint = footprint.Concat(new char[dim.X]);
}
return TilesWhere(name, dim, footprint.ToArray(), a => a != '_').Select(t => t + topLeft);
}
public static IEnumerable<CPos> Tiles(Actor a)
{
return Tiles(a.World.Map.Rules, a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location);
}
public static IEnumerable<CPos> UnpathableTiles(string name, BuildingInfo buildingInfo, CPos position)
{
var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x)).ToArray();
foreach (var tile in TilesWhere(name, (CVec)buildingInfo.Dimensions, footprint, a => a == 'x'))
yield return tile + position;
}
static IEnumerable<CVec> TilesWhere(string name, CVec dim, char[] footprint, Func<char, bool> cond)
{
if (footprint.Length != dim.X * dim.Y)
throw new InvalidOperationException("Invalid footprint for " + name);
var index = 0;
for (var y = 0; y < dim.Y; y++)
for (var x = 0; x < dim.X; x++)
if (cond(footprint[index++]))
yield return new CVec(x, y);
}
public static CVec AdjustForBuildingSize(BuildingInfo buildingInfo)
{
var dim = buildingInfo.Dimensions;
return new CVec(dim.X / 2, dim.Y > 1 ? (dim.Y + 1) / 2 : 0);
}
public static WVec CenterOffset(World w, BuildingInfo buildingInfo)
{
var dim = buildingInfo.Dimensions;
return (w.Map.CenterOfCell(CPos.Zero + new CVec(dim.X, dim.Y)) - w.Map.CenterOfCell(new CPos(1, 1))) / 2;
}
}
}

View File

@@ -0,0 +1,72 @@
#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 OpenRA.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Player recives a unit for free once the building is placed. This also works for structures.",
"If you want more than one unit to appear copy this section and assign IDs like FreeActor@2, ...")]
public class FreeActorInfo : ITraitInfo
{
[ActorReference]
[Desc("Name of actor (use HARV if this trait is for refineries)")]
public readonly string Actor = null;
[Desc("What the unit should start doing. Warning: If this is not a harvester", "it will break if you use FindResources.")]
public readonly string InitialActivity = null;
[Desc("Offset relative to structure-center in 2D (e.g. 1, 2)")]
public readonly CVec SpawnOffset = CVec.Zero;
[Desc("Which direction the unit should face.")]
public readonly int Facing = 0;
public object Create( ActorInitializer init ) { return new FreeActor(init, this); }
}
public class FreeActor
{
public FreeActor(ActorInitializer init, FreeActorInfo info)
{
if (init.Contains<FreeActorInit>() && !init.Get<FreeActorInit>().value)
return;
init.self.World.AddFrameEndTask(w =>
{
var a = w.CreateActor(info.Actor, new TypeDictionary
{
new ParentActorInit(init.self),
new LocationInit(init.self.Location + info.SpawnOffset),
new OwnerInit(init.self.Owner),
new FacingInit(info.Facing),
});
if (info.InitialActivity != null)
a.QueueActivity(Game.CreateObject<Activity>(info.InitialActivity));
});
}
}
public class FreeActorInit : IActorInit<bool>
{
[FieldFromYamlKey]
public readonly bool value = true;
public FreeActorInit() { }
public FreeActorInit(bool init) { value = init; }
public bool Value(World world) { return value; }
}
public class ParentActorInit : IActorInit<Actor>
{
public readonly Actor value;
public ParentActorInit(Actor parent) { value = parent; }
public Actor Value(World world) { return value; }
}
}

View File

@@ -0,0 +1,23 @@
#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 OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class RepairsUnitsInfo : TraitInfo<RepairsUnits>
{
public readonly int ValuePercentage = 20; // charge 20% of the unit value to fully repair
public readonly int HpPerStep = 10;
public readonly int Interval = 24; // Ticks
}
public class RepairsUnits { }
}

View File

@@ -0,0 +1,136 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor will remain visible (but not updated visually) under fog, once discovered.")]
public class FrozenUnderFogInfo : ITraitInfo, Requires<BuildingInfo>
{
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[] footprintInMapsCoords;
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>();
var footprint = FootprintUtils.Tiles(init.self).ToList();
footprintInMapsCoords = footprint.Select(cell => Map.CellToMap(init.world.Map.TileShape, cell)).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)
{
// We are doing the following LINQ manually to avoid allocating an extra delegate since this is a hot path.
// var isVisible = footprintInMapsCoords.Any(mapCoord => p.Shroud.IsVisibleTest(footprintRegion)(mapCoord.X, mapCoord.Y));
var isVisibleTest = p.Shroud.IsVisibleTest(footprintRegion);
var isVisible = false;
foreach (var mapCoord in footprintInMapsCoords)
if (isVisibleTest(mapCoord.X, mapCoord.Y))
{
isVisible = true;
break;
}
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, footprintInMapsCoords, 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.TooltipInfo = tooltip.Value.TooltipInfo;
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;
}
}
}

View File

@@ -0,0 +1,93 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class RenderBuildingInfo : RenderSimpleInfo, Requires<BuildingInfo>, IPlaceBuildingDecoration
{
public readonly bool PauseOnLowPower = false;
public override object Create(ActorInitializer init) { return new RenderBuilding(init, this);}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World w, ActorInfo ai, WPos centerPosition)
{
if (!ai.Traits.Get<BuildingInfo>().RequiresBaseProvider)
yield break;
foreach (var a in w.ActorsWithTrait<BaseProvider>())
foreach (var r in a.Trait.RenderAfterWorld(wr))
yield return r;
}
}
public class RenderBuilding : RenderSimple, INotifyDamageStateChanged, INotifyBuildComplete
{
RenderBuildingInfo info;
public RenderBuilding(ActorInitializer init, RenderBuildingInfo info)
: this(init, info, () => 0) { }
public RenderBuilding(ActorInitializer init, RenderBuildingInfo info, Func<int> baseFacing)
: base(init.self, baseFacing)
{
var self = init.self;
this.info = info;
DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle"));
}
public virtual void BuildingComplete(Actor self)
{
DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle"));
if (info.PauseOnLowPower)
{
var disabled = self.TraitsImplementing<IDisable>();
DefaultAnimation.Paused = () => disabled.Any(d => d.Disabled)
&& DefaultAnimation.CurrentSequence.Name == NormalizeSequence(self, "idle");
}
}
public void PlayCustomAnimThen(Actor self, string name, Action a)
{
DefaultAnimation.PlayThen(NormalizeSequence(self, name),
() => { DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle")); a(); });
}
public void PlayCustomAnimRepeating(Actor self, string name)
{
DefaultAnimation.PlayThen(NormalizeSequence(self, name),
() => PlayCustomAnimRepeating(self, name));
}
public void PlayCustomAnimBackwards(Actor self, string name, Action a)
{
DefaultAnimation.PlayBackwardsThen(NormalizeSequence(self, name),
() => { DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle")); a(); });
}
public void CancelCustomAnim(Actor self)
{
DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle"));
}
public virtual void DamageStateChanged(Actor self, AttackInfo e)
{
if (DefaultAnimation.CurrentSequence != null)
DefaultAnimation.ReplaceAnim(NormalizeSequence(self, DefaultAnimation.CurrentSequence.Name));
}
}
}

View File

@@ -0,0 +1,59 @@
#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.Linq;
using OpenRA.Activities;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class WithMakeAnimationInfo : ITraitInfo, Requires<BuildingInfo>, Requires<RenderBuildingInfo>
{
[Desc("Sequence name to use")]
public readonly string Sequence = "make";
public object Create(ActorInitializer init) { return new WithMakeAnimation(init, this); }
}
public class WithMakeAnimation
{
readonly WithMakeAnimationInfo info;
readonly RenderBuilding renderBuilding;
public WithMakeAnimation(ActorInitializer init, WithMakeAnimationInfo info)
{
this.info = info;
var self = init.self;
renderBuilding = self.Trait<RenderBuilding>();
var building = self.Trait<Building>();
if (!building.SkipMakeAnimation)
{
renderBuilding.PlayCustomAnimThen(self, info.Sequence, () =>
{
building.NotifyBuildingComplete(self);
});
}
}
public void Reverse(Actor self, Activity activity)
{
renderBuilding.PlayCustomAnimBackwards(self, info.Sequence, () =>
{
// avoids visual glitches as we wait for the actor to get destroyed
renderBuilding.DefaultAnimation.PlayFetchIndex(info.Sequence, () => 0);
self.QueueActivity(activity);
});
}
}
}

View File

@@ -0,0 +1,249 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")]
public class ResourceLayerInfo : TraitInfo<ResourceLayer>, Requires<ResourceTypeInfo> { }
public class ResourceLayer : IRenderOverlay, IWorldLoaded, ITickRender
{
static readonly CellContents EmptyCell = new CellContents();
World world;
BuildingInfluence buildingInfluence;
protected CellLayer<CellContents> content;
protected CellLayer<CellContents> render;
List<CPos> dirty;
public void Render(WorldRenderer wr)
{
var shroudObscured = world.ShroudObscuresTest(wr.Viewport.VisibleCells);
foreach (var uv in wr.Viewport.VisibleCells.MapCoords)
{
if (shroudObscured(uv.X, uv.Y))
continue;
var c = render[uv.X, uv.Y];
if (c.Sprite != null)
new SpriteRenderable(c.Sprite, wr.world.Map.CenterOfCell(Map.MapToCell(world.Map.TileShape, uv)),
WVec.Zero, -511, c.Type.Palette, 1f, true).Render(wr); // TODO ZOffset is ignored
}
}
int GetAdjacentCellsWith(ResourceType t, CPos cell)
{
var sum = 0;
for (var u = -1; u < 2; u++)
for (var v = -1; v < 2; v++)
if (content[cell + new CVec(u, v)].Type == t)
++sum;
return sum;
}
public void WorldLoaded(World w, WorldRenderer wr)
{
this.world = w;
buildingInfluence = world.WorldActor.Trait<BuildingInfluence>();
content = new CellLayer<CellContents>(w.Map);
render = new CellLayer<CellContents>(w.Map);
dirty = new List<CPos>();
var resources = w.WorldActor.TraitsImplementing<ResourceType>()
.ToDictionary(r => r.Info.ResourceType, r => r);
foreach (var cell in w.Map.Cells)
{
ResourceType t;
if (!resources.TryGetValue(w.Map.MapResources.Value[cell].Type, out t))
continue;
if (!AllowResourceAt(t, cell))
continue;
content[cell] = CreateResourceCell(t, cell);
}
// Set initial density based on the number of neighboring resources
foreach (var cell in w.Map.Cells)
{
var type = content[cell].Type;
if (type != null)
{
// Adjacent includes the current cell, so is always >= 1
var adjacent = GetAdjacentCellsWith(type, cell);
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
var temp = content[cell];
temp.Density = Math.Max(density, 1);
render[cell] = content[cell] = temp;
UpdateRenderedSprite(cell);
}
}
}
protected virtual void UpdateRenderedSprite(CPos cell)
{
var t = render[cell];
if (t.Density > 0)
{
var sprites = t.Type.Variants[t.Variant];
var frame = int2.Lerp(0, sprites.Length - 1, t.Density - 1, t.Type.Info.MaxDensity);
t.Sprite = sprites[frame];
}
else
t.Sprite = null;
render[cell] = t;
}
protected virtual string ChooseRandomVariant(ResourceType t)
{
return t.Variants.Keys.Random(Game.CosmeticRandom);
}
public void TickRender(WorldRenderer wr, Actor self)
{
var remove = new List<CPos>();
foreach (var c in dirty)
{
if (!self.World.FogObscures(c))
{
render[c] = content[c];
UpdateRenderedSprite(c);
remove.Add(c);
}
}
foreach (var r in remove)
dirty.Remove(r);
}
public bool AllowResourceAt(ResourceType rt, CPos cell)
{
if (!world.Map.Contains(cell))
return false;
if (!rt.Info.AllowedTerrainTypes.Contains(world.Map.GetTerrainInfo(cell).Type))
return false;
if (!rt.Info.AllowUnderActors && world.ActorMap.AnyUnitsAt(cell))
return false;
if (!rt.Info.AllowUnderBuildings && buildingInfluence.GetBuildingAt(cell) != null)
return false;
return true;
}
public bool CanSpawnResourceAt(ResourceType newResourceType, CPos cell)
{
var currentResourceType = GetResource(cell);
return (currentResourceType == newResourceType && !IsFull(cell))
|| (currentResourceType == null && AllowResourceAt(newResourceType, cell));
}
CellContents CreateResourceCell(ResourceType t, CPos cell)
{
world.Map.CustomTerrain[cell] = world.TileSet.GetTerrainIndex(t.Info.TerrainType);
return new CellContents
{
Type = t,
Variant = ChooseRandomVariant(t),
};
}
public void AddResource(ResourceType t, CPos p, int n)
{
var cell = content[p];
if (cell.Type == null)
cell = CreateResourceCell(t, p);
if (cell.Type != t)
return;
cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n);
content[p] = cell;
if (!dirty.Contains(p))
dirty.Add(p);
}
public bool IsFull(CPos cell)
{
return content[cell].Density == content[cell].Type.Info.MaxDensity;
}
public ResourceType Harvest(CPos cell)
{
var c = content[cell];
if (c.Type == null)
return null;
if (--c.Density < 0)
{
content[cell] = EmptyCell;
world.Map.CustomTerrain[cell] = byte.MaxValue;
}
else
content[cell] = c;
if (!dirty.Contains(cell))
dirty.Add(cell);
return c.Type;
}
public void Destroy(CPos cell)
{
// Don't break other users of CustomTerrain if there are no resources
if (content[cell].Type == null)
return;
// Clear cell
content[cell] = EmptyCell;
world.Map.CustomTerrain[cell] = byte.MaxValue;
if (!dirty.Contains(cell))
dirty.Add(cell);
}
public ResourceType GetResource(CPos cell) { return content[cell].Type; }
public ResourceType GetRenderedResource(CPos cell) { return render[cell].Type; }
public int GetResourceDensity(CPos cell) { return content[cell].Density; }
public int GetMaxResourceDensity(CPos cell)
{
if (content[cell].Type == null)
return 0;
return content[cell].Type.Info.MaxDensity;
}
public struct CellContents
{
public ResourceType Type;
public int Density;
public string Variant;
public Sprite Sprite;
}
}
}

View File

@@ -10,6 +10,7 @@
using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
@@ -20,6 +21,11 @@ namespace OpenRA.Mods.Common.Traits
void OnNotifyResourceClaimLost(Actor self, ResourceClaim claim, Actor claimer);
}
public interface IPlaceBuildingDecoration
{
IEnumerable<IRenderable> Render(WorldRenderer wr, World w, ActorInfo ai, WPos centerPosition);
}
public interface INotifyChat { bool OnChat(string from, string message); }
public interface INotifyParachuteLanded { void OnLanded(); }
public interface IRenderActorPreviewInfo { IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init); }