diff --git a/OpenRA.Mods.RA/Activities/FindResources.cs b/OpenRA.Mods.RA/Activities/FindResources.cs index f8101db3c6..220ab71da0 100755 --- a/OpenRA.Mods.RA/Activities/FindResources.cs +++ b/OpenRA.Mods.RA/Activities/FindResources.cs @@ -44,7 +44,7 @@ namespace OpenRA.Mods.RA.Activities var mobile = self.Trait(); var mobileInfo = self.Info.Traits.Get(); var resLayer = self.World.WorldActor.Trait(); - var territory = self.World.WorldActor.Trait(); + var territory = self.World.WorldActor.TraitOrDefault(); // Find harvestable resources nearby: var path = self.World.WorldActor.Trait().FindPath( @@ -79,9 +79,12 @@ namespace OpenRA.Mods.RA.Activities // Can the harvester collect this kind of resource? if (!harvInfo.Resources.Contains(resType.info.Name)) return 1; - // Another harvester has claimed this resource: - ResourceClaim claim; - if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) return 1; + if (territory != null) + { + // Another harvester has claimed this resource: + ResourceClaim claim; + if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) return 1; + } return 0; }) @@ -105,8 +108,11 @@ namespace OpenRA.Mods.RA.Activities } // Attempt to claim a resource as ours: - if (!territory.ClaimResource(self, path[0])) - return Util.SequenceActivities(new Wait(25), new FindResources()); + if (territory != null) + { + if (!territory.ClaimResource(self, path[0])) + return Util.SequenceActivities(new Wait(25), new FindResources()); + } // If not given a direct order, assume ordered to the first resource location we find: if (harv.LastOrderLocation == null) @@ -130,10 +136,10 @@ namespace OpenRA.Mods.RA.Activities { if (isHarvesting) return this; - var territory = self.World.WorldActor.Trait(); + var territory = self.World.WorldActor.TraitOrDefault(); if (IsCanceled) { - territory.UnclaimByActor(self); + if (territory != null) territory.UnclaimByActor(self); return NextActivity; } @@ -142,7 +148,7 @@ namespace OpenRA.Mods.RA.Activities if (harv.IsFull) { - territory.UnclaimByActor(self); + if (territory != null) territory.UnclaimByActor(self); return NextActivity; } @@ -150,7 +156,7 @@ namespace OpenRA.Mods.RA.Activities var resource = resLayer.Harvest(self.Location); if (resource == null) { - territory.UnclaimByActor(self); + if (territory != null) territory.UnclaimByActor(self); return NextActivity; } diff --git a/OpenRA.Mods.RA/Harvester.cs b/OpenRA.Mods.RA/Harvester.cs index beeaae0758..3d5ab4fbe9 100644 --- a/OpenRA.Mods.RA/Harvester.cs +++ b/OpenRA.Mods.RA/Harvester.cs @@ -152,7 +152,9 @@ namespace OpenRA.Mods.RA self.QueueActivity(mobile.MoveTo(moveTo, 1)); self.SetTargetLine(Target.FromCell(moveTo), Color.Gray, false); - self.World.WorldActor.Trait().ClaimResource(self, moveTo); + var territory = self.World.WorldActor.TraitOrDefault(); + if (territory != null) territory.ClaimResource(self, moveTo); + self.QueueActivity(new FindResources()); return; } @@ -258,10 +260,19 @@ namespace OpenRA.Mods.RA if (order.TargetLocation != CPos.Zero) { var loc = order.TargetLocation; - var territory = self.World.WorldActor.Trait(); + var territory = self.World.WorldActor.TraitOrDefault(); - // Find the nearest claimable cell to the order location (useful for group-select harvest): - loc = mobile.NearestCell(loc, p => mobile.CanEnterCell(p) && territory.ClaimResource(self, p), 1, 6); + if (territory != null) + { + // Find the nearest claimable cell to the order location (useful for group-select harvest): + loc = mobile.NearestCell(loc, p => mobile.CanEnterCell(p) && territory.ClaimResource(self, p), 1, 6); + } + else + { + // Find the nearest cell to the order location (useful for group-select harvest): + var taken = new HashSet(); + loc = mobile.NearestCell(loc, p => mobile.CanEnterCell(p) && taken.Add(p), 1, 6); + } self.QueueActivity(mobile.MoveTo(loc, 0)); self.SetTargetLine(Target.FromCell(loc), Color.Red); @@ -310,7 +321,7 @@ namespace OpenRA.Mods.RA var harvInfo = self.Info.Traits.Get(); var mobileInfo = self.Info.Traits.Get(); var resLayer = self.World.WorldActor.Trait(); - var territory = self.World.WorldActor.Trait(); + var territory = self.World.WorldActor.TraitOrDefault(); // Find any harvestable resources: var path = self.World.WorldActor.Trait().FindPath( @@ -325,8 +336,11 @@ namespace OpenRA.Mods.RA if (!harvInfo.Resources.Contains(resType.info.Name)) return 1; // Another harvester has claimed this resource: - ResourceClaim claim; - if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) return 1; + if (territory != null) + { + ResourceClaim claim; + if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) return 1; + } return 0; })