/
SeedsResource.cs
75 lines (61 loc) · 2.03 KB
/
SeedsResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#region Copyright & License Information
/*
* Copyright 2007-2019 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Lets the actor spread resources around it in a circle.")]
class SeedsResourceInfo : ConditionalTraitInfo
{
public readonly int Interval = 75;
public readonly string ResourceType = "Ore";
public readonly int MaxRange = 100;
public override object Create(ActorInitializer init) { return new SeedsResource(init.Self, this); }
}
class SeedsResource : ConditionalTrait<SeedsResourceInfo>, ITick, ISeedableResource
{
readonly SeedsResourceInfo info;
readonly ResourceType resourceType;
readonly ResourceLayer resLayer;
public SeedsResource(Actor self, SeedsResourceInfo info)
: base(info)
{
this.info = info;
resourceType = self.World.WorldActor.TraitsImplementing<ResourceType>()
.FirstOrDefault(t => t.Info.Type == info.ResourceType);
if (resourceType == null)
throw new InvalidOperationException("No such resource type `{0}`".F(info.ResourceType));
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
}
int ticks;
void ITick.Tick(Actor self)
{
if (IsTraitDisabled)
return;
if (--ticks <= 0)
{
Seed(self);
ticks = info.Interval;
}
}
public void Seed(Actor self)
{
var cell = Util.RandomWalk(self.Location, self.World.SharedRandom)
.Take(info.MaxRange)
.SkipWhile(p => !self.World.Map.Contains(p) ||
(resLayer.GetResourceType(p) == resourceType && resLayer.IsFull(p)))
.Cast<CPos?>().FirstOrDefault();
if (cell != null && resLayer.CanSpawnResourceAt(resourceType, cell.Value))
resLayer.AddResource(resourceType, cell.Value, 1);
}
}
}