Extract StoresResources from Harvester
This commit is contained in:
committed by
Matthias Mailänder
parent
60a446123b
commit
d427072cc9
@@ -87,7 +87,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
if (resource.Type == null || resourceLayer.RemoveResource(resource.Type, self.Location) != 1)
|
||||
return true;
|
||||
|
||||
harv.AcceptResource(self, resource.Type);
|
||||
harv.AddResource(self, resource.Type);
|
||||
|
||||
foreach (var t in notifyHarvestActions)
|
||||
t.Harvested(self, resource.Type);
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Primitives;
|
||||
@@ -19,7 +18,7 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class HarvesterInfo : DockClientBaseInfo, Requires<MobileInfo>
|
||||
public class HarvesterInfo : DockClientBaseInfo, Requires<MobileInfo>, Requires<IStoresResourcesInfo>, IRulesetLoaded
|
||||
{
|
||||
[Desc("Docking type")]
|
||||
public readonly BitSet<DockType> Type = new("Unload");
|
||||
@@ -27,9 +26,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[Desc("Cell to move to when automatically unblocking DeliveryBuilding.")]
|
||||
public readonly CVec UnblockCell = new(0, 4);
|
||||
|
||||
[Desc("How much resources it can carry.")]
|
||||
public readonly int Capacity = 28;
|
||||
|
||||
public readonly int BaleLoadDelay = 4;
|
||||
|
||||
[Desc("How fast it can dump its bales.")]
|
||||
@@ -41,7 +37,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public readonly int HarvestFacings = 0;
|
||||
|
||||
[Desc("Which resources it can harvest.")]
|
||||
public readonly HashSet<string> Resources = new();
|
||||
public readonly string[] Resources = Array.Empty<string>();
|
||||
|
||||
[Desc("Percentage of maximum speed when fully loaded.")]
|
||||
public readonly int FullyLoadedSpeed = 85;
|
||||
@@ -79,17 +75,25 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public readonly string HarvestCursor = "harvest";
|
||||
|
||||
public override object Create(ActorInitializer init) { return new Harvester(init.Self, this); }
|
||||
|
||||
void IRulesetLoaded<ActorInfo>.RulesetLoaded(Ruleset rules, ActorInfo info)
|
||||
{
|
||||
if (Resources.Length == 0)
|
||||
throw new YamlException($"Harvester.{nameof(Resources)} is empty.");
|
||||
|
||||
var resourceTypes = Resources.Except(info.TraitInfos<IStoresResourcesInfo>().SelectMany(sr => sr.ResourceTypes)).ToArray();
|
||||
if (resourceTypes.Length != 0)
|
||||
throw new YamlException($"Invalid Harvester.{nameof(Resources)} types: {string.Join(',', resourceTypes)}.");
|
||||
}
|
||||
}
|
||||
|
||||
public class Harvester : DockClientBase<HarvesterInfo>, IIssueOrder, IResolveOrder, IOrderVoice,
|
||||
ISpeedModifier, ISync, INotifyCreated
|
||||
{
|
||||
public readonly IReadOnlyDictionary<string, int> Contents;
|
||||
|
||||
readonly Mobile mobile;
|
||||
readonly IResourceLayer resourceLayer;
|
||||
readonly ResourceClaimLayer claimLayer;
|
||||
readonly Dictionary<string, int> contents = new();
|
||||
readonly IStoresResources[] storesResources;
|
||||
int conditionToken = Actor.InvalidConditionToken;
|
||||
|
||||
public override BitSet<DockType> GetDockType => Info.Type;
|
||||
@@ -97,23 +101,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[Sync]
|
||||
int currentUnloadTicks;
|
||||
|
||||
[Sync]
|
||||
public int ContentHash
|
||||
{
|
||||
get
|
||||
{
|
||||
var value = 0;
|
||||
foreach (var c in contents)
|
||||
value += c.Value << c.Key.Length;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public Harvester(Actor self, HarvesterInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
Contents = new ReadOnlyDictionary<string, int>(contents);
|
||||
mobile = self.Trait<Mobile>();
|
||||
storesResources = self.TraitsImplementing<IStoresResources>().Where(sr => info.Resources.Any(r => sr.HasType(r))).ToArray();
|
||||
resourceLayer = self.World.WorldActor.Trait<IResourceLayer>();
|
||||
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
||||
}
|
||||
@@ -129,9 +121,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
base.Created(self);
|
||||
}
|
||||
|
||||
public bool IsFull => contents.Values.Sum() == Info.Capacity;
|
||||
public bool IsEmpty => contents.Values.Sum() == 0;
|
||||
public int Fullness => contents.Values.Sum() * 100 / Info.Capacity;
|
||||
public bool IsFull => storesResources.All(sr => sr.ContentsSum >= sr.Capacity);
|
||||
public bool IsEmpty => storesResources.All(sr => sr.ContentsSum == 0);
|
||||
public int Fullness => storesResources.Sum(sr => sr.ContentsSum * 100 / sr.Capacity) / storesResources.Length;
|
||||
|
||||
protected override bool CanDock()
|
||||
{
|
||||
@@ -151,12 +143,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
conditionToken = self.RevokeCondition(conditionToken);
|
||||
}
|
||||
|
||||
public void AcceptResource(Actor self, string resourceType)
|
||||
public void AddResource(Actor self, string resourceType)
|
||||
{
|
||||
if (!contents.ContainsKey(resourceType))
|
||||
contents[resourceType] = 1;
|
||||
else
|
||||
contents[resourceType]++;
|
||||
foreach (var sr in storesResources)
|
||||
if (sr.AddResource(resourceType, 1) == 0)
|
||||
break;
|
||||
|
||||
UpdateCondition(self);
|
||||
}
|
||||
@@ -177,27 +168,23 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (--currentUnloadTicks > 0)
|
||||
return false;
|
||||
|
||||
if (contents.Keys.Count > 0)
|
||||
foreach (var sr in storesResources)
|
||||
{
|
||||
foreach (var c in contents)
|
||||
foreach (var c in sr.Contents)
|
||||
{
|
||||
var resourceType = c.Key;
|
||||
var count = Math.Min(c.Value, Info.BaleUnloadAmount);
|
||||
var accepted = acceptResources.AcceptResources(hostActor, resourceType, count);
|
||||
var accepted = acceptResources.AcceptResources(hostActor, c.Key, count);
|
||||
if (accepted == 0)
|
||||
continue;
|
||||
|
||||
contents[resourceType] -= accepted;
|
||||
if (contents[resourceType] <= 0)
|
||||
contents.Remove(resourceType);
|
||||
|
||||
sr.RemoveResource(c.Key, accepted);
|
||||
currentUnloadTicks = Info.BaleUnloadDelay;
|
||||
UpdateCondition(self);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return contents.Count == 0;
|
||||
return IsEmpty;
|
||||
}
|
||||
|
||||
public override void OnDockCompleted(Actor self, Actor hostActor, IDockHost dock)
|
||||
@@ -279,13 +266,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
int ISpeedModifier.GetSpeedModifier()
|
||||
{
|
||||
return 100 - (100 - Info.FullyLoadedSpeed) * contents.Values.Sum() / Info.Capacity;
|
||||
return 100 - (100 - Info.FullyLoadedSpeed) * Fullness / 100;
|
||||
}
|
||||
|
||||
protected override void TraitDisabled(Actor self)
|
||||
{
|
||||
base.TraitDisabled(self);
|
||||
contents.Clear();
|
||||
|
||||
if (conditionToken != Actor.InvalidConditionToken)
|
||||
conditionToken = self.RevokeCondition(conditionToken);
|
||||
|
||||
@@ -206,12 +206,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddStorage(int capacity)
|
||||
public void AddStorageCapacity(int capacity)
|
||||
{
|
||||
ResourceCapacity += capacity;
|
||||
}
|
||||
|
||||
public void RemoveStorage(int capacity)
|
||||
public void RemoveStorageCapacity(int capacity)
|
||||
{
|
||||
ResourceCapacity -= capacity;
|
||||
|
||||
|
||||
@@ -10,12 +10,13 @@
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.Render
|
||||
{
|
||||
public class WithHarvesterPipsDecorationInfo : WithDecorationBaseInfo, Requires<HarvesterInfo>
|
||||
public class WithStoresResourcesPipsDecorationInfo : WithDecorationBaseInfo, Requires<IStoresResourcesInfo>
|
||||
{
|
||||
[FieldLoader.Require]
|
||||
[Desc("Number of pips to display how filled unit is.")]
|
||||
@@ -42,26 +43,27 @@ namespace OpenRA.Mods.Common.Traits.Render
|
||||
[PaletteReference]
|
||||
public readonly string Palette = "chrome";
|
||||
|
||||
public override object Create(ActorInitializer init) { return new WithHarvesterPipsDecoration(init.Self, this); }
|
||||
public override object Create(ActorInitializer init) { return new WithStoresResourcesPipsDecoration(init.Self, this); }
|
||||
}
|
||||
|
||||
public class WithHarvesterPipsDecoration : WithDecorationBase<WithHarvesterPipsDecorationInfo>
|
||||
public class WithStoresResourcesPipsDecoration : WithDecorationBase<WithStoresResourcesPipsDecorationInfo>
|
||||
{
|
||||
readonly Harvester harvester;
|
||||
readonly IStoresResources storesResources;
|
||||
readonly Animation pips;
|
||||
|
||||
public WithHarvesterPipsDecoration(Actor self, WithHarvesterPipsDecorationInfo info)
|
||||
public WithStoresResourcesPipsDecoration(Actor self, WithStoresResourcesPipsDecorationInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
harvester = self.Trait<Harvester>();
|
||||
// TODO: allow to choose which stores resources trait to target.
|
||||
storesResources = self.TraitsImplementing<IStoresResources>().First();
|
||||
pips = new Animation(self.World, info.Image);
|
||||
}
|
||||
|
||||
string GetPipSequence(int i)
|
||||
{
|
||||
var n = i * harvester.Info.Capacity / Info.PipCount;
|
||||
var n = i * storesResources.Capacity / Info.PipCount;
|
||||
|
||||
foreach (var rt in harvester.Contents)
|
||||
foreach (var rt in storesResources.Contents)
|
||||
{
|
||||
if (n < rt.Value)
|
||||
{
|
||||
67
OpenRA.Mods.Common/Traits/StoresPlayerResources.cs
Normal file
67
OpenRA.Mods.Common/Traits/StoresPlayerResources.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Adds capacity to a player's harvested resource limit.")]
|
||||
public class StoresPlayerResourcesInfo : TraitInfo
|
||||
{
|
||||
[FieldLoader.Require]
|
||||
public readonly int Capacity = 0;
|
||||
|
||||
public override object Create(ActorInitializer init) { return new StoresPlayerResources(init.Self, this); }
|
||||
}
|
||||
|
||||
public class StoresPlayerResources : INotifyOwnerChanged, INotifyCapture, INotifyKilled, INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||
{
|
||||
readonly StoresPlayerResourcesInfo info;
|
||||
PlayerResources player;
|
||||
|
||||
public int Stored => player.ResourceCapacity == 0 ? 0 : (int)((long)info.Capacity * player.Resources / player.ResourceCapacity);
|
||||
|
||||
public StoresPlayerResources(Actor self, StoresPlayerResourcesInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
player = self.Owner.PlayerActor.Trait<PlayerResources>();
|
||||
}
|
||||
|
||||
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
|
||||
{
|
||||
player = newOwner.PlayerActor.Trait<PlayerResources>();
|
||||
}
|
||||
|
||||
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner, BitSet<CaptureType> captureTypes)
|
||||
{
|
||||
var resources = Stored;
|
||||
oldOwner.PlayerActor.Trait<PlayerResources>().TakeResources(resources);
|
||||
newOwner.PlayerActor.Trait<PlayerResources>().GiveResources(resources);
|
||||
}
|
||||
|
||||
void INotifyKilled.Killed(Actor self, AttackInfo e)
|
||||
{
|
||||
// Lose the stored resources.
|
||||
player.TakeResources(Stored);
|
||||
}
|
||||
|
||||
void INotifyAddedToWorld.AddedToWorld(Actor self)
|
||||
{
|
||||
player.AddStorageCapacity(info.Capacity);
|
||||
}
|
||||
|
||||
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
|
||||
{
|
||||
player.RemoveStorageCapacity(info.Capacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,62 +9,100 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using OpenRA.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Adds capacity to a player's harvested resource limit.")]
|
||||
public class StoresResourcesInfo : TraitInfo
|
||||
[Desc("Allows the storage of resources.")]
|
||||
public class StoresResourcesInfo : TraitInfo, IStoresResourcesInfo
|
||||
{
|
||||
[FieldLoader.Require]
|
||||
public readonly int Capacity = 0;
|
||||
[Desc("The amounts of resources that can be stored.")]
|
||||
public readonly int Capacity = 28;
|
||||
|
||||
[Desc("Which resources can be stored.")]
|
||||
public readonly string[] Resources = Array.Empty<string>();
|
||||
|
||||
string[] IStoresResourcesInfo.ResourceTypes => Resources;
|
||||
|
||||
public override object Create(ActorInitializer init) { return new StoresResources(init.Self, this); }
|
||||
}
|
||||
|
||||
public class StoresResources : INotifyOwnerChanged, INotifyCapture, IStoreResources, ISync, INotifyKilled, INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||
public class StoresResources : IStoresResources, ISync
|
||||
{
|
||||
readonly Dictionary<string, int> contents = new();
|
||||
readonly StoresResourcesInfo info;
|
||||
PlayerResources player;
|
||||
|
||||
[Sync]
|
||||
public int Stored => player.ResourceCapacity == 0 ? 0 : (int)((long)info.Capacity * player.Resources / player.ResourceCapacity);
|
||||
public int ContentHash
|
||||
{
|
||||
get
|
||||
{
|
||||
var value = 0;
|
||||
foreach (var c in contents)
|
||||
value += c.Value << c.Key.Length;
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public int ContentsSum { get; private set; } = 0;
|
||||
public IReadOnlyDictionary<string, int> Contents { get; }
|
||||
int IStoresResources.Capacity => info.Capacity;
|
||||
|
||||
public StoresResources(Actor self, StoresResourcesInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
player = self.Owner.PlayerActor.Trait<PlayerResources>();
|
||||
|
||||
foreach (var r in info.Resources)
|
||||
contents[r] = 0;
|
||||
|
||||
Contents = new ReadOnlyDictionary<string, int>(contents);
|
||||
}
|
||||
|
||||
int IStoreResources.Capacity => info.Capacity;
|
||||
|
||||
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
|
||||
public bool HasType(string resourceType)
|
||||
{
|
||||
player = newOwner.PlayerActor.Trait<PlayerResources>();
|
||||
return info.Resources.Contains(resourceType);
|
||||
}
|
||||
|
||||
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner, BitSet<CaptureType> captureTypes)
|
||||
int IStoresResources.AddResource(string resourceType, int value)
|
||||
{
|
||||
var resources = Stored;
|
||||
oldOwner.PlayerActor.Trait<PlayerResources>().TakeResources(resources);
|
||||
newOwner.PlayerActor.Trait<PlayerResources>().GiveResources(resources);
|
||||
if (!HasType(resourceType))
|
||||
return value;
|
||||
|
||||
if (ContentsSum + value > info.Capacity)
|
||||
{
|
||||
var added = info.Capacity - ContentsSum;
|
||||
contents[resourceType] += added;
|
||||
ContentsSum = info.Capacity;
|
||||
return value - added;
|
||||
}
|
||||
|
||||
contents[resourceType] += value;
|
||||
ContentsSum += value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void INotifyKilled.Killed(Actor self, AttackInfo e)
|
||||
int IStoresResources.RemoveResource(string resourceType, int value)
|
||||
{
|
||||
// Lose the stored resources
|
||||
player.TakeResources(Stored);
|
||||
}
|
||||
if (!HasType(resourceType))
|
||||
return value;
|
||||
|
||||
void INotifyAddedToWorld.AddedToWorld(Actor self)
|
||||
{
|
||||
player.AddStorage(info.Capacity);
|
||||
}
|
||||
if (contents[resourceType] < value)
|
||||
{
|
||||
var leftover = value - contents[resourceType];
|
||||
ContentsSum -= contents[resourceType];
|
||||
contents[resourceType] = 0;
|
||||
return leftover;
|
||||
}
|
||||
|
||||
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
|
||||
{
|
||||
player.RemoveStorage(info.Capacity);
|
||||
contents[resourceType] -= value;
|
||||
ContentsSum -= value;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||
{
|
||||
public class ExtractResourceStorageFromHarvester : UpdateRule
|
||||
{
|
||||
public override string Name => "Renames StoresResources to StoresPlayerResources and extracts StoresResources from Harvester.";
|
||||
|
||||
public override string Description =>
|
||||
"Resource storage was extracted from Harvester. WithHarvesterPipsDecoration was also renamed to WithStoresResourcesPipsDecoration.";
|
||||
|
||||
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode)
|
||||
{
|
||||
actorNode.RenameChildrenMatching("StoresResources", "StoresPlayerResources");
|
||||
actorNode.RenameChildrenMatching("WithHarvesterPipsDecoration", "WithStoresResourcesPipsDecoration");
|
||||
|
||||
var harvester = actorNode.LastChildMatching("Harvester", false);
|
||||
if (harvester == null)
|
||||
yield break;
|
||||
|
||||
var storesResources = new MiniYamlNodeBuilder("StoresResources", "");
|
||||
var capacity = harvester.LastChildMatching("Capacity", false);
|
||||
if (capacity != null)
|
||||
{
|
||||
storesResources.AddNode(capacity);
|
||||
harvester.RemoveNode(capacity);
|
||||
}
|
||||
|
||||
var resources = harvester.LastChildMatching("Resources", false);
|
||||
if (resources != null)
|
||||
storesResources.AddNode(resources);
|
||||
|
||||
actorNode.AddNode(storesResources);
|
||||
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,7 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
{
|
||||
// bleed only changes here.
|
||||
new RemoveValidRelationsFromCapturable(),
|
||||
new ExtractResourceStorageFromHarvester(),
|
||||
|
||||
// Execute these rules last to avoid premature yaml merge crashes.
|
||||
new AbstractDocking(),
|
||||
|
||||
Reference in New Issue
Block a user