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

@@ -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 { }
}