|
| 1 | +#region Copyright & License Information |
| 2 | +/* |
| 3 | + * Copyright (c) The OpenRA Developers and Contributors |
| 4 | + * This file is part of OpenRA, which is free software. It is made |
| 5 | + * available to you under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation, either version 3 of |
| 7 | + * the License, or (at your option) any later version. For more |
| 8 | + * information, see COPYING. |
| 9 | + */ |
| 10 | +#endregion |
| 11 | + |
| 12 | +using System; |
| 13 | +using System.Collections.Generic; |
| 14 | +using System.Linq; |
| 15 | +using OpenRA.Mods.Common.Traits; |
| 16 | +using OpenRA.Traits; |
| 17 | + |
| 18 | +namespace OpenRA.Mods.CA.Traits |
| 19 | +{ |
| 20 | + [Desc("Gradually converts resources within a given radius.")] |
| 21 | + sealed class ConvertsResourcesInfo : ConditionalTraitInfo |
| 22 | + { |
| 23 | + public readonly string[] ConvertFrom = { "Ore", "Tiberium", "Gems", "BlueTiberium" }; |
| 24 | + public readonly int Interval = 75; |
| 25 | + public readonly string ConvertTo = "BlackTiberium"; |
| 26 | + public readonly WDist Range = WDist.FromCells(5); |
| 27 | + public readonly int Amount = 1; |
| 28 | + |
| 29 | + public override object Create(ActorInitializer init) { return new ConvertsResources(init.Self, this); } |
| 30 | + } |
| 31 | + |
| 32 | + sealed class ConvertsResources : ConditionalTrait<ConvertsResourcesInfo>, ITick |
| 33 | + { |
| 34 | + readonly ConvertsResourcesInfo info; |
| 35 | + readonly IResourceLayer resourceLayer; |
| 36 | + readonly Dictionary<CPos, int> cellsToConvert; |
| 37 | + |
| 38 | + public ConvertsResources(Actor self, ConvertsResourcesInfo info) |
| 39 | + : base(info) |
| 40 | + { |
| 41 | + this.info = info; |
| 42 | + resourceLayer = self.World.WorldActor.Trait<IResourceLayer>(); |
| 43 | + cellsToConvert = new Dictionary<CPos, int>(); |
| 44 | + ticks = info.Interval; |
| 45 | + } |
| 46 | + |
| 47 | + int ticks; |
| 48 | + |
| 49 | + void ITick.Tick(Actor self) |
| 50 | + { |
| 51 | + if (IsTraitDisabled) |
| 52 | + return; |
| 53 | + |
| 54 | + if (--ticks <= 0) |
| 55 | + { |
| 56 | + Convert(self); |
| 57 | + ticks = info.Interval; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + void Convert(Actor self) |
| 62 | + { |
| 63 | + // Find cells with convertible resources within range |
| 64 | + var cells = self.World.Map.FindTilesInCircle(self.Location, info.Range.Length / 1024) |
| 65 | + .Where(c => |
| 66 | + { |
| 67 | + var resource = resourceLayer.GetResource(c); |
| 68 | + return resource.Type != null && |
| 69 | + info.ConvertFrom.Contains(resource.Type) && |
| 70 | + (resource.Density > 0 || cellsToConvert.ContainsKey(c)); |
| 71 | + }); |
| 72 | + |
| 73 | + if (cells.Any()) |
| 74 | + Remove(cells.Random(self.World.SharedRandom)); |
| 75 | + |
| 76 | + // Only try to add resources to cells that are ready for conversion |
| 77 | + var convertedCells = cellsToConvert |
| 78 | + .Where(kv => resourceLayer.GetResource(kv.Key).Type == null || resourceLayer.GetResource(kv.Key).Type == info.ConvertTo) |
| 79 | + .Select(kv => kv.Key) |
| 80 | + .ToList(); |
| 81 | + |
| 82 | + if (convertedCells.Count > 0) |
| 83 | + Add(convertedCells.Random(self.World.SharedRandom)); |
| 84 | + } |
| 85 | + |
| 86 | + void Remove(CPos cell) |
| 87 | + { |
| 88 | + var resource = resourceLayer.GetResource(cell); |
| 89 | + var amountRemoved = resourceLayer.RemoveResource(resource.Type, cell, info.Amount); |
| 90 | + |
| 91 | + // Remove some of the original resource |
| 92 | + if (amountRemoved > 0) |
| 93 | + { |
| 94 | + // Track how much we've removed from this cell |
| 95 | + if (!cellsToConvert.ContainsKey(cell)) |
| 96 | + cellsToConvert[cell] = 0; |
| 97 | + |
| 98 | + cellsToConvert[cell] += amountRemoved; |
| 99 | + var updatedResource = resourceLayer.GetResource(cell); |
| 100 | + |
| 101 | + // If none of the original resource is left, mark it as converted |
| 102 | + if (updatedResource.Density == 0) |
| 103 | + Add(cell); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + void Add(CPos cell) |
| 108 | + { |
| 109 | + if (!resourceLayer.CanAddResource(info.ConvertTo, cell)) |
| 110 | + return; |
| 111 | + |
| 112 | + var toConvert = cellsToConvert[cell]; |
| 113 | + var maxDensity = resourceLayer.GetMaxDensity(info.ConvertTo); |
| 114 | + var currentDensity = resourceLayer.GetResource(cell).Density; |
| 115 | + |
| 116 | + // Only try to add info.Amount, limited by remaining space |
| 117 | + var amountToAdd = Math.Min(info.Amount, maxDensity - currentDensity); |
| 118 | + var amountAdded = resourceLayer.AddResource(info.ConvertTo, cell, amountToAdd); |
| 119 | + |
| 120 | + if (amountAdded > 0) |
| 121 | + { |
| 122 | + var remaining = toConvert - amountAdded; |
| 123 | + if (remaining > 0) |
| 124 | + cellsToConvert[cell] = remaining; |
| 125 | + else |
| 126 | + cellsToConvert.Remove(cell); |
| 127 | + } |
| 128 | + else if (currentDensity >= maxDensity) |
| 129 | + cellsToConvert.Remove(cell); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments