Skip to content

Commit

Permalink
Allow harvester definitions to exist on non-mobile actors
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun authored and Mailaender committed Dec 2, 2023
1 parent 8e7fa26 commit d67e0a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public HarvesterTraitWrapper(Actor actor)
Actor = actor;
Harvester = actor.Trait<Harvester>();
Parachutable = actor.TraitOrDefault<Parachutable>();
Mobile = actor.Trait<Mobile>();
Mobile = actor.TraitOrDefault<Mobile>();
}
}

Expand Down Expand Up @@ -110,6 +110,9 @@ void IBotTick.BotTick(IBot bot)
// Find idle harvesters and give them orders:
foreach (var h in harvesters)
{
if (h.Value.Mobile == null)
continue;

if (!h.Key.IsIdle)
{
// Ignore this actor if FindAndDeliverResources is working fine or it is performing a different activity
Expand Down
14 changes: 7 additions & 7 deletions OpenRA.Mods.Common/Traits/Harvester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace OpenRA.Mods.Common.Traits
{
public class HarvesterInfo : DockClientBaseInfo, Requires<MobileInfo>, Requires<IStoresResourcesInfo>, IRulesetLoaded
public class HarvesterInfo : DockClientBaseInfo, Requires<IStoresResourcesInfo>, IRulesetLoaded
{
[Desc("Docking type")]
public readonly BitSet<DockType> Type = new("Unload");
Expand Down Expand Up @@ -90,7 +90,7 @@ void IRulesetLoaded<ActorInfo>.RulesetLoaded(Ruleset rules, ActorInfo info)
public class Harvester : DockClientBase<HarvesterInfo>, IIssueOrder, IResolveOrder, IOrderVoice,
ISpeedModifier, ISync, INotifyCreated
{
readonly Mobile mobile;
Mobile mobile;
readonly IResourceLayer resourceLayer;
readonly ResourceClaimLayer claimLayer;
readonly IStoresResources[] storesResources;
Expand All @@ -104,18 +104,18 @@ public class Harvester : DockClientBase<HarvesterInfo>, IIssueOrder, IResolveOrd
public Harvester(Actor self, HarvesterInfo info)
: base(self, info)
{
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>();
}

protected override void Created(Actor self)
{
mobile = self.TraitOrDefault<Mobile>();
UpdateCondition(self);

// Note: This is queued in a FrameEndTask because otherwise the activity is dropped/overridden while moving out of a factory.
if (Info.SearchOnCreation)
if (Info.SearchOnCreation && mobile != null)
self.World.AddFrameEndTask(w => self.QueueActivity(new FindAndDeliverResources(self)));

base.Created(self);
Expand Down Expand Up @@ -218,7 +218,7 @@ IEnumerable<IOrderTargeter> IIssueOrder.Orders
{
get
{
if (IsTraitDisabled)
if (IsTraitDisabled || mobile == null)
yield break;

yield return new HarvestOrderTargeter();
Expand All @@ -235,15 +235,15 @@ Order IIssueOrder.IssueOrder(Actor self, IOrderTargeter order, in Target target,

string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)
{
if (order.OrderString == "Harvest")
if (order.OrderString == "Harvest" && mobile != null)
return Info.HarvestVoice;

return null;
}

void IResolveOrder.ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "Harvest")
if (order.OrderString == "Harvest" && mobile != null)
{
CPos loc;
if (order.Target.Type != TargetType.Invalid)
Expand Down

0 comments on commit d67e0a4

Please sign in to comment.