Merge pull request #7664 from penev92/bleed_harvesterInsurance

Implement Carryall edge spawn, harvester delivery by carryall and harvester insurance for D2k
This commit is contained in:
Oliver Brakmann
2015-03-25 20:03:00 +01:00
17 changed files with 615 additions and 296 deletions

View File

@@ -0,0 +1,110 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.IO;
using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.D2k.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Player receives a unit for free once the building is placed.",
"If you want more than one unit to be delivered, copy this section and assign IDs like FreeActorWithDelivery@2, ...")]
public class FreeActorWithDeliveryInfo : FreeActorInfo
{
[ActorReference]
[Desc("Name of the delivering actor. This actor must have the `Carryall` trait")]
public readonly string DeliveringActor = null;
[Desc("Cell coordinates for spawning the delivering actor. If left blank, the closest edge cell will be chosen.")]
public readonly CPos SpawnLocation = CPos.Zero;
[Desc("Offset relative to the top-left cell of the building.")]
public readonly CVec DeliveryOffset = CVec.Zero;
public override object Create(ActorInitializer init) { return new FreeActorWithDelivery(init, this); }
}
public class FreeActorWithDelivery
{
public readonly FreeActorWithDeliveryInfo Info;
readonly Actor self;
public FreeActorWithDelivery(ActorInitializer init, FreeActorWithDeliveryInfo info)
{
if (string.IsNullOrEmpty(info.Actor))
throw new InvalidDataException("Actor type was not specified!");
if (string.IsNullOrEmpty(info.DeliveringActor))
throw new InvalidDataException("Delivering actor type was not specified!");
self = init.Self;
Info = info;
DoDelivery(self.Location + info.DeliveryOffset, info.Actor, info.DeliveringActor, info.InitialActivity);
}
public void DoDelivery(CPos location, string actorName, string carrierActorName, string clientInitialActivity)
{
Actor cargo;
Actor carrier;
CreateActors(actorName, carrierActorName, out cargo, out carrier);
if (clientInitialActivity != null)
cargo.QueueActivity(Game.CreateObject<Activity>(clientInitialActivity));
cargo.Trait<Carryable>().Destination = location;
carrier.Trait<Carryall>().AttachCarryable(cargo);
carrier.QueueActivity(new DeliverUnit(carrier));
carrier.QueueActivity(new HeliFly(carrier, Target.FromCell(self.World, self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom))));
carrier.QueueActivity(new RemoveSelf());
self.World.AddFrameEndTask(w => self.World.Add(carrier));
}
void CreateActors(string actorName, string deliveringActorName, out Actor cargo, out Actor carrier)
{
// Get a carryall spawn location
var location = Info.SpawnLocation;
if (location == CPos.Zero)
location = self.World.Map.ChooseClosestEdgeCell(self.Location);
var spawn = self.World.Map.CenterOfCell(location);
var initialFacing = self.World.Map.FacingBetween(location, self.Location, 0);
// If aircraft, spawn at cruise altitude
var aircraftInfo = self.World.Map.Rules.Actors[deliveringActorName.ToLower()].Traits.GetOrDefault<AircraftInfo>();
if (aircraftInfo != null)
spawn += new WVec(0, 0, aircraftInfo.CruiseAltitude.Range);
// Create delivery actor
carrier = self.World.CreateActor(false, deliveringActorName, new TypeDictionary
{
new LocationInit(location),
new CenterPositionInit(spawn),
new OwnerInit(self.Owner),
new FacingInit(initialFacing)
});
// Create delivered actor
cargo = self.World.CreateActor(false, actorName, new TypeDictionary
{
new OwnerInit(self.Owner),
});
}
}
}

View File

@@ -0,0 +1,82 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Produce a unit on the closest map edge cell and move into the world.")]
class ProductionFromMapEdgeInfo : ProductionInfo
{
public override object Create(ActorInitializer init) { return new ProductionFromMapEdge(init, this); }
}
class ProductionFromMapEdge : Production
{
public ProductionFromMapEdge(ActorInitializer init, ProductionInfo info)
: base(init, info) { }
public override bool Produce(Actor self, ActorInfo producee, string raceVariant)
{
var location = self.World.Map.ChooseClosestEdgeCell(self.Location);
var pos = self.World.Map.CenterOfCell(location);
// If aircraft, spawn at cruise altitude
var aircraftInfo = producee.Traits.GetOrDefault<AircraftInfo>();
if (aircraftInfo != null)
pos += new WVec(0, 0, aircraftInfo.CruiseAltitude.Range);
var initialFacing = self.World.Map.FacingBetween(location, self.Location, 0);
self.World.AddFrameEndTask(w =>
{
var td = new TypeDictionary
{
new OwnerInit(self.Owner),
new LocationInit(location),
new CenterPositionInit(pos),
new FacingInit(initialFacing)
};
if (raceVariant != null)
td.Add(new RaceInit(raceVariant));
var newUnit = self.World.CreateActor(producee.Name, td);
var move = newUnit.TraitOrDefault<IMove>();
if (move != null)
newUnit.QueueActivity(move.MoveIntoWorld(newUnit, self.Location));
newUnit.SetTargetLine(Target.FromCell(self.World, self.Location), Color.Green, false);
if (!self.IsDead)
foreach (var t in self.TraitsImplementing<INotifyProduction>())
t.UnitProduced(self, newUnit, self.Location);
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
foreach (var notify in notifyOthers)
notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit);
var bi = newUnit.Info.Traits.GetOrDefault<BuildableInfo>();
if (bi != null && bi.InitialActivity != null)
newUnit.QueueActivity(Game.CreateObject<Activity>(bi.InitialActivity));
foreach (var t in newUnit.TraitsImplementing<INotifyBuildComplete>())
t.BuildingComplete(newUnit);
});
return true;
}
}
}

View File

@@ -7,16 +7,15 @@
* see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.D2k.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Can be carried by units with the trait `AutoCarryall`.")]
[Desc("Can be carried by units with the trait `Carryall`.")]
public class CarryableInfo : ITraitInfo
{
[Desc("Required distance away from destination before requesting a pickup.")]
@@ -32,7 +31,7 @@ namespace OpenRA.Mods.D2k.Traits
public bool Reserved { get; private set; }
// If we're locked there isnt much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
// If we're locked there isn't much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
bool locked;
public bool WantsTransport { get; private set; }
@@ -62,15 +61,14 @@ namespace OpenRA.Mods.D2k.Traits
Destination = destination;
this.afterLandActivity = afterLandActivity;
WantsTransport = true;
if (locked || Reserved)
return;
WantsTransport = true;
// Inform all idle carriers
var carriers = self.World.ActorsWithTrait<AutoCarryall>()
.Where(c => !c.Trait.Busy && !c.Actor.IsDead && c.Actor.Owner == self.Owner)
var carriers = self.World.ActorsWithTrait<Carryall>()
.Where(c => !c.Trait.IsBusy && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
foreach (var carrier in carriers)
@@ -90,7 +88,7 @@ namespace OpenRA.Mods.D2k.Traits
WantsTransport = false;
afterLandActivity = null;
// TODO: We could implement something like a carrier.Trait<AutoCarryAll>().CancelTransportNotify(self) and call it here
// TODO: We could implement something like a carrier.Trait<Carryall>().CancelTransportNotify(self) and call it here
}
// We do not handle Harvested notification
@@ -99,8 +97,8 @@ namespace OpenRA.Mods.D2k.Traits
public Actor GetClosestIdleCarrier()
{
// Find carriers
var carriers = self.World.ActorsWithTrait<AutoCarryall>()
.Where(p => p.Actor.Owner == self.Owner && !p.Trait.Busy)
var carriers = self.World.ActorsWithTrait<Carryall>()
.Where(p => p.Actor.Owner == self.Owner && !p.Trait.IsBusy && p.Actor.IsInWorld)
.Select(h => h.Actor);
return WorldUtils.ClosestTo(carriers, self);

View File

@@ -7,62 +7,70 @@
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.D2k.Activities;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries")]
public class AutoCarryallInfo : ITraitInfo, Requires<IBodyOrientationInfo>
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries.")]
public class CarryallInfo : ITraitInfo, Requires<IBodyOrientationInfo>
{
public object Create(ActorInitializer init) { return new AutoCarryall(init.Self, this); }
[Desc("Set to false when the carryall should not automatically get new jobs.")]
public readonly bool Automatic = true;
public object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
}
public class AutoCarryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender
public class Carryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender
{
readonly Actor self;
readonly WRange carryHeight;
readonly CarryallInfo info;
// The actor we are currently carrying.
[Sync] Actor carrying;
bool isCarrying;
[Sync] public Actor Carrying { get; internal set; }
public bool IsCarrying { get; internal set; }
// TODO: Use ActorPreviews so that this can support actors with multiple sprites
Animation anim;
public bool Busy { get; internal set; }
public bool IsBusy { get; internal set; }
public AutoCarryall(Actor self, AutoCarryallInfo info)
public Carryall(Actor self, CarryallInfo info)
{
this.self = self;
this.info = info;
IsBusy = false;
IsCarrying = false;
carryHeight = self.Trait<Helicopter>().Info.LandAltitude;
}
public void OnBecomingIdle(Actor self)
{
FindCarryableForTransport();
if (info.Automatic)
FindCarryableForTransport();
if (!Busy)
if (!IsBusy)
self.QueueActivity(new HeliFlyCircle(self));
}
// A carryable notifying us that he'd like to be carried
public bool RequestTransportNotify(Actor carryable)
{
if (Busy)
if (IsBusy || !info.Automatic)
return false;
if (ReserveCarryable(carryable))
{
self.QueueActivity(false, new CarryUnit(self, carryable));
self.QueueActivity(false, new PickupUnit(self, carryable));
self.QueueActivity(true, new DeliverUnit(self));
return true;
}
@@ -71,29 +79,32 @@ namespace OpenRA.Mods.D2k.Traits
void FindCarryableForTransport()
{
// get all carryables who want transport
if (!self.IsInWorld)
return;
// Get all carryables who want transport
var carryables = self.World.ActorsWithTrait<Carryable>()
.Where(p =>
{
var actor = p.Actor;
if (actor == null)
return false;
{
var actor = p.Actor;
if (actor == null)
return false;
if (actor.Owner != self.Owner)
return false;
if (actor.Owner != self.Owner)
return false;
if (actor.IsDead)
return false;
if (actor.IsDead)
return false;
var trait = p.Trait;
if (trait.Reserved)
return false;
var trait = p.Trait;
if (trait.Reserved)
return false;
if (!trait.WantsTransport)
return false;
if (!trait.WantsTransport)
return false;
return true;
})
return true;
})
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
foreach (var p in carryables)
@@ -101,7 +112,8 @@ namespace OpenRA.Mods.D2k.Traits
// Check if its actually me who's the best candidate
if (p.Trait.GetClosestIdleCarrier() == self && ReserveCarryable(p.Actor))
{
self.QueueActivity(false, new CarryUnit(self, p.Actor));
self.QueueActivity(false, new PickupUnit(self, p.Actor));
self.QueueActivity(true, new DeliverUnit(self));
break;
}
}
@@ -112,8 +124,8 @@ namespace OpenRA.Mods.D2k.Traits
{
if (carryable.Trait<Carryable>().Reserve(self))
{
carrying = carryable;
Busy = true;
Carrying = carryable;
IsBusy = true;
return true;
}
@@ -123,24 +135,26 @@ namespace OpenRA.Mods.D2k.Traits
// Unreserve the carryable
public void UnreserveCarryable()
{
if (carrying != null)
if (Carrying != null)
{
if (carrying.IsInWorld && !carrying.IsDead)
carrying.Trait<Carryable>().UnReserve(self);
if (Carrying.IsInWorld && !Carrying.IsDead)
Carrying.Trait<Carryable>().UnReserve(self);
carrying = null;
Carrying = null;
}
Busy = false;
IsBusy = false;
}
// INotifyKilled
public void Killed(Actor self, AttackInfo e)
{
if (carrying != null)
if (Carrying != null)
{
if (isCarrying && carrying.IsInWorld && !carrying.IsDead)
carrying.Kill(e.Attacker);
if (IsCarrying && Carrying.IsInWorld && !Carrying.IsDead)
Carrying.Kill(e.Attacker);
Carrying = null;
}
UnreserveCarryable();
@@ -149,7 +163,9 @@ namespace OpenRA.Mods.D2k.Traits
// Called when carryable is inside.
public void AttachCarryable(Actor carryable)
{
isCarrying = true;
IsBusy = true;
IsCarrying = true;
Carrying = carryable;
// Create a new animation for our carryable unit
var rs = carryable.Trait<RenderSprites>();
@@ -161,7 +177,7 @@ namespace OpenRA.Mods.D2k.Traits
// Called when released
public void CarryableReleased()
{
isCarrying = false;
IsCarrying = false;
anim = null;
}
@@ -171,7 +187,8 @@ namespace OpenRA.Mods.D2k.Traits
if (anim != null && !self.World.FogObscures(self))
{
anim.Tick();
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Range), wr.Palette("player" + carrying.Owner.InternalName));
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Range),
wr.Palette("player" + Carrying.Owner.InternalName));
foreach (var rr in renderables)
yield return rr;

View File

@@ -0,0 +1,48 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("A player with this trait will receive a free harvester when his last one gets eaten by a sandworm, provided he has at least one refinery.")]
public class HarvesterInsuranceInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new HarvesterInsurance(init.Self); }
}
public class HarvesterInsurance
{
readonly Actor self;
public HarvesterInsurance(Actor self)
{
this.self = self;
}
public void TryActivate()
{
var harvesters = self.World.ActorsWithTrait<Harvester>().Where(x => x.Actor.Owner == self.Owner);
if (harvesters.Any())
return;
var refineries = self.World.ActorsWithTrait<Refinery>().Where(x => x.Actor.Owner == self.Owner);
if (!refineries.Any())
return;
var refinery = refineries.First().Actor;
var delivery = refinery.Trait<FreeActorWithDelivery>();
delivery.DoDelivery(refinery.Location + delivery.Info.DeliveryOffset, delivery.Info.Actor,
delivery.Info.DeliveringActor, delivery.Info.InitialActivity);
}
}
}