Extract StoresResources from Harvester

This commit is contained in:
Gustas
2023-03-22 22:31:05 +02:00
committed by Matthias Mailänder
parent 60a446123b
commit d427072cc9
20 changed files with 277 additions and 100 deletions

View File

@@ -173,7 +173,31 @@ namespace OpenRA.Traits
}
[RequireExplicitImplementation]
public interface IStoreResources { int Capacity { get; } }
public interface IStoresResourcesInfo : ITraitInfoInterface
{
string[] ResourceTypes { get; }
}
public interface IStoresResources
{
bool HasType(string resourceType);
/// <summary>The amount of resources that can be stored.</summary>
int Capacity { get; }
/// <summary>Stored resources.</summary>
/// <remarks>Dictionary key refers to resourceType, value refers to resource amount.</remarks>
IReadOnlyDictionary<string, int> Contents { get; }
/// <summary>A performance cheap method of getting the total sum of contents.</summary>
int ContentsSum { get; }
/// <summary>Returns the amount of <paramref name="value"/> that was not added.</summary>
int AddResource(string resourceType, int value);
/// <summary>Returns the amount of <paramref name="value"/> that was not removed.</summary>
int RemoveResource(string resourceType, int value);
}
public interface IEffectiveOwner
{

View File

@@ -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);

View File

@@ -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);

View File

@@ -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;

View File

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

View 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);
}
}
}

View File

@@ -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;
}
void INotifyKilled.Killed(Actor self, AttackInfo e)
{
// Lose the stored resources
player.TakeResources(Stored);
contents[resourceType] += value;
ContentsSum += value;
return 0;
}
void INotifyAddedToWorld.AddedToWorld(Actor self)
int IStoresResources.RemoveResource(string resourceType, int value)
{
player.AddStorage(info.Capacity);
if (!HasType(resourceType))
return value;
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;
}
}
}

View File

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

View File

@@ -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(),

View File

@@ -219,7 +219,7 @@
red: pip-red
^StoresResources:
StoresResources:
StoresPlayerResources:
WithResourceStoragePipsDecoration:
Position: BottomLeft
Margin: 4, 3

View File

@@ -247,7 +247,7 @@ PROC:
IsDragRequired: True
DragOffset: -554,512,0
DragLength: 12
StoresResources:
StoresPlayerResources:
Capacity: 1000
Selectable:
Bounds: 3072, 2389
@@ -291,7 +291,7 @@ SILO:
-WithSpriteBody:
WithResourceLevelSpriteBody:
Sequence: stages
StoresResources:
StoresPlayerResources:
Capacity: 3000
-SpawnActorsOnSell:
Power:

View File

@@ -56,13 +56,15 @@ HARV:
DecorationBounds: 1536, 1536
Harvester:
Resources: Tiberium, BlueTiberium
Capacity: 20
BaleLoadDelay: 12
BaleUnloadDelay: 6
SearchFromProcRadius: 15
SearchFromHarvesterRadius: 8
HarvestFacings: 8
EmptyCondition: no-tiberium
StoresResources:
Capacity: 20
Resources: Tiberium, BlueTiberium
DockClientManager:
Mobile:
Speed: 72
@@ -89,7 +91,7 @@ HARV:
Explodes:
RequiresCondition: !no-tiberium
Weapon: TiberiumExplosion
WithHarvesterPipsDecoration:
WithStoresResourcesPipsDecoration:
Position: BottomLeft
Margin: 4, 3
RequiresSelection: true

View File

@@ -287,7 +287,7 @@ refinery:
Type: Unload
DockAngle: 640
DockOffset: 1c0,512,0
StoresResources:
StoresPlayerResources:
Capacity: 2000
CustomSellValue:
Value: 500
@@ -357,7 +357,7 @@ silo:
-WithSpriteBody:
WithResourceLevelSpriteBody:
Sequence: stages
StoresResources:
StoresPlayerResources:
Capacity: 2000
-SpawnActorsOnSell:
Power:

View File

@@ -70,12 +70,14 @@ harvester:
Class: harvester
DecorationBounds: 1344, 1344
Harvester:
Capacity: 28
HarvestFacings: 8
Resources: Spice
BaleUnloadDelay: 5
SearchFromProcRadius: 30
SearchFromHarvesterRadius: 15
StoresResources:
Capacity: 28
Resources: Spice
DockClientManager:
CarryableHarvester:
Health:
@@ -107,7 +109,7 @@ harvester:
Delay: 3
StartIfBelow: 50
-RevealOnFire:
WithHarvesterPipsDecoration:
WithStoresResourcesPipsDecoration:
Position: BottomLeft
Margin: 1, 4
RequiresSelection: true

View File

@@ -1290,7 +1290,7 @@ PROC:
Type: Unload
DockAngle: 256
DockOffset: 0, 1c0, 0
StoresResources:
StoresPlayerResources:
Capacity: 2000
CustomSellValue:
Value: 300
@@ -1375,7 +1375,7 @@ SILO:
-WithSpriteBody:
WithResourceLevelSpriteBody:
Sequence: stages
StoresResources:
StoresPlayerResources:
Capacity: 3000
-SpawnActorsOnSell:
Power:

View File

@@ -320,13 +320,15 @@ HARV:
Selectable:
DecorationBounds: 1792, 1792
Harvester:
Capacity: 20
Resources: Ore,Gems
BaleUnloadDelay: 1
SearchFromProcRadius: 15
SearchFromHarvesterRadius: 8
HarvestFacings: 8
EmptyCondition: no-ore
StoresResources:
Capacity: 20
Resources: Ore,Gems
DockClientManager:
Health:
HP: 60000
@@ -357,7 +359,7 @@ HARV:
WithHarvesterSpriteBody:
ImageByFullness: harvempty, harvhalf, harv
-WithFacingSpriteBody:
WithHarvesterPipsDecoration:
WithStoresResourcesPipsDecoration:
Position: BottomLeft
Margin: 4, 3
RequiresSelection: true

View File

@@ -593,7 +593,7 @@ NAWAST:
Type: UnloadWeed
DockAngle: 640
DockOffset: 724,724,0
StoresResources:
StoresPlayerResources:
Capacity: 56
Power:
Amount: -40

View File

@@ -352,13 +352,15 @@ WEED:
Description: Collects veins for processing.\n Unarmed
Harvester:
Type: UnloadWeed
Capacity: 7
Resources: Veins
BaleUnloadDelay: 20
BaleLoadDelay: 40
SearchFromProcRadius: 72
SearchFromHarvesterRadius: 36
HarvestVoice: Attack
StoresResources:
Capacity: 7
Resources: Veins
DockClientManager:
Voice: Move
Mobile:
@@ -381,7 +383,7 @@ WEED:
WithVoxelUnloadBody:
-DamagedByTerrain@VEINS:
-LeavesTrails@VEINS:
WithHarvesterPipsDecoration:
WithStoresResourcesPipsDecoration:
Position: BottomLeft
RequiresSelection: true
Margin: 5, 2

View File

@@ -125,7 +125,7 @@ PROC:
Type: Unload
DockAngle: 640
DockOffset: 362,362,0
StoresResources:
StoresPlayerResources:
Capacity: 2000
CustomSellValue:
Value: 600
@@ -211,7 +211,7 @@ GASILO:
RequiresCondition: !build-incomplete
Sequence: idle-lights-bright
Palette: bright
StoresResources:
StoresPlayerResources:
Capacity: 1500
Power:
Amount: -10

View File

@@ -59,7 +59,6 @@ HARV:
Bounds: 1086, 2172
DecorationBounds: 1086, 2172
Harvester:
Capacity: 28
Resources: Tiberium, BlueTiberium
BaleLoadDelay: 15
BaleUnloadDelay: 15
@@ -68,6 +67,9 @@ HARV:
SearchFromHarvesterRadius: 18
HarvestVoice: Attack
EmptyCondition: no-tiberium
StoresResources:
Capacity: 28
Resources: Tiberium, BlueTiberium
DockClientManager:
Voice: Move
Mobile:
@@ -100,7 +102,7 @@ HARV:
nod: harv.nod
-DamagedByTerrain@VEINS:
-LeavesTrails@VEINS:
WithHarvesterPipsDecoration:
WithStoresResourcesPipsDecoration:
Position: BottomLeft
RequiresSelection: true
Margin: 5, 2