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

@@ -19,7 +19,6 @@
#endregion
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities
{

View File

@@ -0,0 +1,100 @@
#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.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities
{
public class DeliverResources : IActivity
{
public IActivity NextActivity { get; set; }
bool isDocking;
Actor refinery;
public DeliverResources() { }
public DeliverResources( 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

@@ -0,0 +1,93 @@
#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;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.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 DeliverResources { 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) { }
}
}

111
OpenRA.Mods.RA/Harvester.cs Executable file
View File

@@ -0,0 +1,111 @@
#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.Mods.RA.Activities;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA
{
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 DeliverResources(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

@@ -47,6 +47,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\CaptureBuilding.cs" />
<Compile Include="Activities\DeliverOre.cs" />
<Compile Include="Activities\Demolish.cs" />
<Compile Include="Activities\Infiltrate.cs" />
<Compile Include="Activities\LayMine.cs" />
@@ -55,6 +56,8 @@
<Compile Include="C4Demolition.cs" />
<Compile Include="Chronoshiftable.cs" />
<Compile Include="Effects\NukeLaunch.cs" />
<Compile Include="Activities\Harvest.cs" />
<Compile Include="Harvester.cs" />
<Compile Include="OreRefinery.cs" />
<Compile Include="SupportPowers\ChronoshiftPaletteEffect.cs" />
<Compile Include="SupportPowers\ChronoshiftPower.cs" />
@@ -86,6 +89,7 @@
<Compile Include="SupportPowers\NukePower.cs" />
<Compile Include="Thief.cs" />
<Compile Include="Crates\ResetRadarCrateAction.cs" />
<Compile Include="TraitsInterfaces.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -18,6 +18,7 @@
*/
#endregion
using OpenRA.Mods.RA.Activities;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
@@ -36,7 +37,7 @@ namespace OpenRA.Mods.RA
this.self = self;
}
public int2 DeliverOffset { get { return new int2(1, 2); } }
public void OnDock(Actor harv, DeliverOre dockOrder)
public void OnDock(Actor harv, DeliverResources dockOrder)
{
var unit = harv.traits.Get<Unit>();
if (unit.Facing != 64)

View File

@@ -0,0 +1,30 @@
#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 OpenRA.Mods.RA.Activities;
namespace OpenRA.Mods.RA
{
public interface IAcceptOre
{
void OnDock(Actor harv, DeliverResources dockOrder);
int2 DeliverOffset { get; }
}
}