move the harvest-related traits and activities into the mod dll.

This commit is contained in:
Bob
2010-04-02 23:37:32 +12:00
committed by Chris Forbes
parent 1d38825299
commit a22ec0fd81
11 changed files with 54 additions and 21 deletions

View File

@@ -144,7 +144,6 @@
<Compile Include="Traits\Activities\HeliFly.cs" />
<Compile Include="Traits\Activities\HeliLand.cs" />
<Compile Include="Traits\Activities\HeliReturn.cs" />
<Compile Include="Traits\Activities\DeliverOre.cs" />
<Compile Include="Traits\Activities\TransformIntoActor.cs" />
<Compile Include="Actor.cs" />
<Compile Include="Effects\Bullet.cs" />
@@ -188,7 +187,6 @@
<Compile Include="Graphics\SpriteSheetBuilder.cs" />
<Compile Include="GameRules\TerrainCost.cs" />
<Compile Include="Graphics\TerrainRenderer.cs" />
<Compile Include="Traits\Activities\Harvest.cs" />
<Compile Include="Traits\Activities\Move.cs" />
<Compile Include="Traits\Activities\Follow.cs" />
<Compile Include="Traits\Activities\Turn.cs" />
@@ -213,7 +211,6 @@
<Compile Include="Traits\Explodes.cs" />
<Compile Include="Traits\Fake.cs" />
<Compile Include="Traits\GeneratesGap.cs" />
<Compile Include="Traits\Harvester.cs" />
<Compile Include="Traits\Helicopter.cs" />
<Compile Include="Traits\Modifiers\InvisibleToOthers.cs" />
<Compile Include="Traits\ConstructionYard.cs" />

View File

@@ -1,98 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it 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.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Linq;
namespace OpenRA.Traits.Activities
{
public class DeliverOre : IActivity
{
public IActivity NextActivity { get; set; }
bool isDocking;
Actor refinery;
public DeliverOre() { }
public DeliverOre( Actor refinery )
{
this.refinery = refinery;
}
Actor ChooseRefinery(Actor self)
{
var mobile = self.traits.Get<Mobile>();
var search = new PathSearch(self.World)
{
heuristic = PathSearch.DefaultEstimator(self.Location),
umt = mobile.GetMovementType(),
checkForBlocked = false,
};
var refineries = self.World.Queries.OwnedBy[self.Owner]
.Where(x => x.traits.Contains<IAcceptOre>())
.ToList();
if (refinery != null)
search.AddInitialCell(self.World, refinery.Location + refinery.traits.Get<IAcceptOre>().DeliverOffset);
else
foreach (var r in refineries)
search.AddInitialCell(self.World, r.Location + r.traits.Get<IAcceptOre>().DeliverOffset);
var path = self.World.PathFinder.FindPath(search);
path.Reverse();
if (path.Count != 0)
return refineries.FirstOrDefault(x => x.Location + x.traits.Get<IAcceptOre>().DeliverOffset == path[0]);
else
return null;
}
public IActivity Tick( Actor self )
{
var mobile = self.traits.Get<Mobile>();
if( NextActivity != null )
return NextActivity;
if( refinery != null && refinery.IsDead )
refinery = null;
if( refinery == null || self.Location != refinery.Location + refinery.traits.Get<IAcceptOre>().DeliverOffset )
{
refinery = ChooseRefinery(self);
if (refinery == null)
return new Wait(10) { NextActivity = this };
return new Move(refinery.Location + refinery.traits.Get<IAcceptOre>().DeliverOffset, 0) { NextActivity = this };
}
else if (!isDocking)
{
isDocking = true;
refinery.traits.Get<IAcceptOre>().OnDock(self, this);
}
return new Wait(10) { NextActivity = this };
}
public void Cancel(Actor self)
{
// TODO: allow canceling of deliver orders?
}
}
}

View File

@@ -1,91 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it 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.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Linq;
using OpenRA.GameRules;
namespace OpenRA.Traits.Activities
{
public class Harvest : IActivity
{
public IActivity NextActivity { get; set; }
bool isHarvesting = false;
public IActivity Tick( Actor self )
{
if( isHarvesting ) return this;
if( NextActivity != null ) return NextActivity;
var harv = self.traits.Get<Harvester>();
if( harv.IsFull )
return new DeliverOre { NextActivity = NextActivity };
if (HarvestThisTile(self))
return this;
else
{
FindMoreResource(self);
return NextActivity;
}
}
bool HarvestThisTile(Actor self)
{
var harv = self.traits.Get<Harvester>();
var renderUnit = self.traits.Get<RenderUnit>(); /* better have one of these! */
var resource = self.World.WorldActor.traits.Get<ResourceLayer>().Harvest(self.Location);
if (resource == null)
return false;
if (renderUnit.anim.CurrentSequence.Name != "harvest")
{
isHarvesting = true;
renderUnit.PlayCustomAnimation(self, "harvest", () => isHarvesting = false);
}
harv.AcceptResource(resource);
return true;
}
void FindMoreResource(Actor self)
{
var res = self.World.WorldActor.traits.Get<ResourceLayer>();
var harv = self.Info.Traits.Get<HarvesterInfo>();
self.QueueActivity(new Move(
() =>
{
var search = new PathSearch(self.World)
{
heuristic = loc => (res.GetResource(loc) != null
&& harv.Resources.Contains( res.GetResource(loc).Name )) ? 0 : 1,
umt = UnitMovementType.Wheel,
checkForBlocked = true
};
search.AddInitialCell(self.World, self.Location);
return self.World.PathFinder.FindPath(search);
}));
self.QueueActivity(new Harvest());
}
public void Cancel(Actor self) { }
}
}

View File

@@ -1,109 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it 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.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits.Activities;
namespace OpenRA.Traits
{
class HarvesterInfo : ITraitInfo
{
public readonly int Capacity = 28;
public readonly int PipCount = 7;
public readonly string[] Resources = { };
public object Create(Actor self) { return new Harvester(self); }
}
public class Harvester : IIssueOrder, IResolveOrder, IPips
{
Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
Actor self;
public Harvester(Actor self)
{
this.self = self;
}
public bool IsFull { get { return contents.Values.Sum() == self.Info.Traits.Get<HarvesterInfo>().Capacity; } }
public bool IsEmpty { get { return contents.Values.Sum() == 0; } }
public void AcceptResource(ResourceTypeInfo type)
{
if (!contents.ContainsKey(type)) contents[type] = 1;
else contents[type]++;
}
public void Deliver(Actor self, Actor proc)
{
proc.Owner.GiveOre(contents.Sum(kv => kv.Key.ValuePerUnit * kv.Value));
contents.Clear();
}
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
{
if (mi.Button == MouseButton.Left) return null;
if (underCursor != null
&& underCursor.Owner == self.Owner
&& underCursor.traits.Contains<IAcceptOre>() && !IsEmpty)
return new Order("Deliver", self, underCursor);
var res = self.World.WorldActor.traits.Get<ResourceLayer>().GetResource(xy);
var info = self.Info.Traits.Get<HarvesterInfo>();
if (underCursor == null && res != null && info.Resources.Contains(res.Name))
return new Order("Harvest", self, xy);
return null;
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "Harvest")
{
self.CancelActivity();
self.QueueActivity(new Move(order.TargetLocation, 0));
self.QueueActivity(new Harvest());
}
else if (order.OrderString == "Deliver")
{
self.CancelActivity();
self.QueueActivity(new DeliverOre(order.TargetActor));
}
}
public IEnumerable<PipType> GetPips(Actor self)
{
int numPips = self.Info.Traits.Get<HarvesterInfo>().PipCount;
int n = contents.Values.Sum();
for (int i = 0; i < numPips; i++)
{
// todo: pip colors based on ResourceTypeInfo
if (n * 1.0f / self.Info.Traits.Get<HarvesterInfo>().Capacity > i * 1.0f / numPips)
yield return PipType.Yellow;
else
yield return PipType.Transparent;
}
}
}
}

View File

@@ -23,7 +23,6 @@ using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Traits.Activities;
namespace OpenRA.Traits
{
@@ -43,11 +42,6 @@ namespace OpenRA.Traits
public interface INotifyDamage { void Damaged(Actor self, AttackInfo e); }
public interface INotifyBuildComplete { void BuildingComplete(Actor self); }
public interface INotifyProduction { void UnitProduced(Actor self, Actor other); }
public interface IAcceptOre
{
void OnDock(Actor harv, DeliverOre dockOrder);
int2 DeliverOffset { get; }
}
public interface IAcceptThief { void OnSteal(Actor self, Actor thief); }
public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); }

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Traits
public object Create(Actor self) { return new ResourceLayer(self); }
}
class ResourceLayer : IRenderOverlay, ILoadWorldHook
public class ResourceLayer : IRenderOverlay, ILoadWorldHook
{
SpriteRenderer sr;
World w;