Link harvesters with refineries.

This commit is contained in:
Paul Chote
2010-06-15 18:46:09 +12:00
parent f4421d89ea
commit 5f83f97747
6 changed files with 435 additions and 328 deletions

View File

@@ -48,6 +48,11 @@ namespace OpenRA.Graphics
palette = new HardwarePalette(renderer, world.Map);
}
public void DrawLine(float2 start, float2 end, Color startColor, Color endColor)
{
lineRenderer.DrawLine(start,end,startColor,endColor);
}
public int GetPaletteIndex(string name)
{
return palette.GetPaletteIndex(name);

View File

@@ -25,6 +25,7 @@ using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Activities;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
using System.Drawing;
namespace OpenRA.Mods.Cnc
{
@@ -39,11 +40,12 @@ namespace OpenRA.Mods.Cnc
public object Create(Actor self) { return new TiberiumRefinery(self, this); }
}
class TiberiumRefinery : ITick, IAcceptOre, INotifyDamage, IPips
class TiberiumRefinery : ITick, IAcceptOre, INotifyDamage, INotifySold, INotifyCapture, IPips
{
readonly Actor self;
readonly TiberiumRefineryInfo Info;
readonly PlayerResources Player;
List<Actor> LinkedHarv;
[Sync]
int nextProcessTime = 0;
@@ -55,7 +57,18 @@ namespace OpenRA.Mods.Cnc
this.self = self;
Info = info;
Player = self.Owner.PlayerActor.traits.Get<PlayerResources>();
LinkedHarv = new List<Actor>();
}
public void LinkHarvester(Actor self, Actor harv)
{
LinkedHarv.Add(harv);
}
public void UnlinkHarvester(Actor self, Actor harv)
{
if (LinkedHarv.Contains(harv))
LinkedHarv.Remove(harv);
}
public void GiveOre(int amount)
@@ -84,13 +97,16 @@ namespace OpenRA.Mods.Cnc
public void Damaged(Actor self, AttackInfo e)
{
if (self.IsDead && Tiberium > 0)
if (self.IsDead)
{
if (Info.DeathWeapon != null)
if (Info.DeathWeapon != null && Tiberium > 0)
{
Combat.DoExplosion(e.Attacker, Info.DeathWeapon,
self.CenterLocation.ToInt2(), 0);
}
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester>().UnlinkProc(harv, self);
}
}
@@ -108,6 +124,23 @@ namespace OpenRA.Mods.Cnc
})));
}
public void OnCapture(Actor self, Actor captor)
{
// Todo: Do the right thing if a harv is docked
// Unlink any other harvs
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester>().UnlinkProc(harv, self);
}
public void Selling(Actor self) {}
public void Sold(Actor self)
{
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester>().UnlinkProc(harv, self);
}
public IEnumerable<PipType> GetPips(Actor self)
{
return Graphics.Util.MakeArray( Info.PipCount,

View File

@@ -29,64 +29,28 @@ namespace OpenRA.Mods.RA.Activities
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 )
{
if( NextActivity != null )
return NextActivity;
if( refinery != null && refinery.IsDead )
refinery = null;
var proc = self.traits.Get<Harvester>().LinkedProc;
if( refinery == null || self.Location != refinery.Location + refinery.traits.Get<IAcceptOre>().DeliverOffset )
{
refinery = ChooseRefinery(self);
if (refinery == null)
if (proc == null)
return new Wait(10) { NextActivity = this };
return new Move(refinery.Location + refinery.traits.Get<IAcceptOre>().DeliverOffset, 0) { NextActivity = this };
if( self.Location != proc.Location + proc.traits.Get<IAcceptOre>().DeliverOffset )
{
return new Move(proc.Location + proc.traits.Get<IAcceptOre>().DeliverOffset, 0) { NextActivity = this };
}
else if (!isDocking)
{
isDocking = true;
refinery.traits.Get<IAcceptOre>().OnDock(self, this);
proc.traits.Get<IAcceptOre>().OnDock(self, this);
}
return new Wait(10) { NextActivity = this };
}

View File

@@ -40,11 +40,45 @@ namespace OpenRA.Mods.RA
public class Harvester : IIssueOrder, IResolveOrder, INotifyDamage, IPips
{
Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
public Actor LinkedProc = null;
readonly HarvesterInfo Info;
public Harvester(Actor self, HarvesterInfo info)
{
Info = info;
self.QueueActivity( new CallFunc( () => ChooseNewProc(self, null)));
}
void ChooseNewProc(Actor self, Actor ignore)
{
LinkedProc = ClosestProc(self, ignore);
if (LinkedProc != null)
LinkedProc.traits.WithInterface<IAcceptOre>().FirstOrDefault().LinkHarvester(LinkedProc,self);
}
Actor ClosestProc(Actor self, Actor ignore)
{
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 != ignore && x.traits.Contains<IAcceptOre>())
.ToList();
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 bool IsFull { get { return contents.Values.Sum() == Info.Capacity; } }
@@ -69,8 +103,9 @@ namespace OpenRA.Mods.RA
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>();
@@ -91,22 +126,47 @@ namespace OpenRA.Mods.RA
else if (order.OrderString == "Deliver")
{
self.CancelActivity();
self.QueueActivity(new DeliverResources(order.TargetActor));
if (order.TargetActor != LinkedProc)
{
if (LinkedProc != null)
LinkedProc.traits.WithInterface<IAcceptOre>().FirstOrDefault().UnlinkHarvester(LinkedProc,self);
LinkedProc = order.TargetActor;
LinkedProc.traits.WithInterface<IAcceptOre>().FirstOrDefault().LinkHarvester(LinkedProc,self);
}
self.QueueActivity(new DeliverResources());
}
}
public void Damaged(Actor self, AttackInfo e)
{
if (self.IsDead && contents.Count > 0)
if (self.IsDead)
{
if (Info.DeathWeapon != null)
if (Info.DeathWeapon != null && contents.Count > 0)
{
Combat.DoExplosion(e.Attacker, Info.DeathWeapon,
self.CenterLocation.ToInt2(), 0);
}
if (LinkedProc != null)
LinkedProc.traits.WithInterface<IAcceptOre>().FirstOrDefault().UnlinkHarvester(LinkedProc,self);
}
}
public void LinkProc(Actor self, Actor proc)
{
LinkedProc = proc;
}
public void UnlinkProc(Actor self, Actor proc)
{
if (LinkedProc != proc)
return;
ChooseNewProc(self, proc);
}
public IEnumerable<PipType> GetPips(Actor self)
{
int numPips = Info.PipCount;

View File

@@ -34,22 +34,42 @@ namespace OpenRA.Mods.RA
public readonly int Capacity = 0;
public readonly int ProcessTick = 25;
public readonly int ProcessAmount = 50;
public object Create(Actor self) { return new OreRefinery(self, this); }
public readonly string DeathWeapon = null;
public object Create (Actor self)
{
return new OreRefinery (self, this);
}
}
class OreRefinery : ITick, IAcceptOre, IPips
class OreRefinery : ITick, IAcceptOre, INotifyDamage, INotifySold, INotifyCapture, IPips
{
Actor self;
OreRefineryInfo Info;
readonly Actor self;
readonly OreRefineryInfo Info;
readonly PlayerResources Player;
List<Actor> LinkedHarv;
[Sync]
int nextProcessTime = 0;
[Sync]
public int Ore = 0;
public OreRefinery (Actor self, OreRefineryInfo info)
{
this.self = self;
Info = info;
Player = self.Owner.PlayerActor.traits.Get<PlayerResources> ();
LinkedHarv = new List<Actor> ();
}
public void LinkHarvester (Actor self, Actor harv)
{
LinkedHarv.Add (harv);
}
public void UnlinkHarvester (Actor self, Actor harv)
{
if (LinkedHarv.Contains (harv))
LinkedHarv.Remove (harv);
}
public void GiveOre (int amount)
@@ -61,26 +81,29 @@ namespace OpenRA.Mods.RA
public void Tick (Actor self)
{
if (--nextProcessTime <= 0)
{
if (--nextProcessTime <= 0) {
// Convert resources to cash
var pr = self.Owner.PlayerActor.traits.Get<PlayerResources>();
int amount = Math.Min (Ore, Info.ProcessAmount);
amount = Math.Min(amount, pr.OreCapacity - pr.Ore);
if (amount > 0)
{
amount = Math.Min (amount, Player.OreCapacity - Player.Ore);
if (amount > 0) {
Ore -= amount;
pr.GiveOre(amount);
Player.GiveOre (amount);
}
nextProcessTime = Info.ProcessTick;
}
}
public IEnumerable<PipType> GetPips(Actor self)
public void Damaged (Actor self, AttackInfo e)
{
return Graphics.Util.MakeArray( Info.PipCount,
i => (Ore * 1.0f / Info.Capacity > i * 1.0f / Info.PipCount)
? Info.PipColor : PipType.Transparent );
if (self.IsDead) {
if (Info.DeathWeapon != null && Ore > 0) {
Combat.DoExplosion (e.Attacker, Info.DeathWeapon, self.CenterLocation.ToInt2 (), 0);
}
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester> ().UnlinkProc (harv, self);
}
}
public int2 DeliverOffset {get{ return new int2 (1, 2); }}
@@ -90,7 +113,8 @@ namespace OpenRA.Mods.RA
if (unit.Facing != 64)
harv.QueueActivity (new Turn (64));
harv.QueueActivity( new CallFunc( () => {
harv.QueueActivity (new CallFunc (() =>
{
var renderUnit = harv.traits.Get<RenderUnit> ();
if (renderUnit.anim.CurrentSequence.Name != "empty")
renderUnit.PlayCustomAnimation (harv, "empty", () =>
@@ -98,8 +122,27 @@ namespace OpenRA.Mods.RA
harv.traits.Get<Harvester> ().Deliver (harv, self);
harv.QueueActivity (new Harvest ());
});
}));
}
));
public void OnCapture (Actor self, Actor captor)
{
// Todo: Do the right thing if a harv is docked
// Unlink any other harvs
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester> ().UnlinkProc (harv, self);
}
public void Selling (Actor self) {}
public void Sold (Actor self)
{
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester> ().UnlinkProc (harv, self);
}
public IEnumerable<PipType> GetPips (Actor self)
{
return Graphics.Util.MakeArray (Info.PipCount, i => (Ore * 1f / Info.Capacity > i * 1f / Info.PipCount) ? Info.PipColor : PipType.Transparent);
}
}
}

View File

@@ -26,6 +26,8 @@ namespace OpenRA.Mods.RA
{
void OnDock(Actor harv, DeliverResources dockOrder);
void GiveOre(int amount);
void LinkHarvester(Actor self, Actor harv);
void UnlinkHarvester(Actor self, Actor harv);
int2 DeliverOffset { get; }
}
}