sketch of reservation support
This commit is contained in:
28
OpenRa.DataStructures/DisposableAction.cs
Normal file
28
OpenRa.DataStructures/DisposableAction.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa
|
||||
{
|
||||
public class DisposableAction : IDisposable
|
||||
{
|
||||
public DisposableAction(Action a) { this.a = a; }
|
||||
|
||||
Action a;
|
||||
bool disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
a();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~DisposableAction()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AssetInfo.cs" />
|
||||
<Compile Include="DisposableAction.cs" />
|
||||
<Compile Include="float2.cs" />
|
||||
<Compile Include="int2.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
@@ -196,6 +196,7 @@
|
||||
<Compile Include="Traits\MineImmune.cs" />
|
||||
<Compile Include="Traits\Minelayer.cs" />
|
||||
<Compile Include="Traits\LimitedAmmo.cs" />
|
||||
<Compile Include="Traits\Reservable.cs" />
|
||||
<Compile Include="Traits\SquishByTank.cs" />
|
||||
<Compile Include="Traits\Plane.cs" />
|
||||
<Compile Include="Traits\ProductionQueue.cs" />
|
||||
|
||||
@@ -16,13 +16,18 @@ namespace OpenRa.Game.Traits.Activities
|
||||
float2 w1, w2, w3; /* tangent points to turn circles */
|
||||
float2 landPoint;
|
||||
|
||||
static bool IsReserved(Actor a)
|
||||
{
|
||||
var res = a.traits.GetOrDefault<Reservable>();
|
||||
return res != null && res.IsReserved;
|
||||
}
|
||||
|
||||
Actor ChooseAirfield(Actor self)
|
||||
{
|
||||
// todo: handle reservations
|
||||
|
||||
var airfield = Game.world.Actors
|
||||
.Where(a => a.Info == Rules.UnitInfo["AFLD"]
|
||||
&& a.Owner == self.Owner)
|
||||
&& a.Owner == self.Owner
|
||||
&& !IsReserved(a))
|
||||
.FirstOrDefault();
|
||||
|
||||
if (airfield == null)
|
||||
@@ -34,6 +39,9 @@ namespace OpenRa.Game.Traits.Activities
|
||||
void Calculate(Actor self)
|
||||
{
|
||||
if (dest == null) dest = ChooseAirfield(self);
|
||||
var res = dest.traits.GetOrDefault<Reservable>();
|
||||
if (res != null)
|
||||
self.traits.Get<Plane>().reservation = res.Reserve(self);
|
||||
|
||||
var landPos = dest.CenterLocation;
|
||||
var unit = self.traits.Get<Unit>();
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace OpenRa.Game.Traits
|
||||
{
|
||||
class Plane : IOrder, IMovement
|
||||
{
|
||||
public IDisposable reservation;
|
||||
|
||||
public Plane(Actor self) {}
|
||||
|
||||
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
|
||||
@@ -18,13 +20,25 @@ namespace OpenRa.Game.Traits
|
||||
|
||||
if (underCursor.Info == Rules.UnitInfo["AFLD"]
|
||||
&& underCursor.Owner == self.Owner)
|
||||
{
|
||||
var res = underCursor.traits.GetOrDefault<Reservable>();
|
||||
if (res != null && res.IsReserved)
|
||||
return null;
|
||||
|
||||
return new Order("Enter", self, underCursor, int2.Zero, null);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ResolveOrder(Actor self, Order order)
|
||||
{
|
||||
if (reservation != null)
|
||||
{
|
||||
reservation.Dispose();
|
||||
reservation = null;
|
||||
}
|
||||
|
||||
if (order.OrderString == "Move")
|
||||
{
|
||||
self.CancelActivity();
|
||||
@@ -34,6 +48,13 @@ namespace OpenRa.Game.Traits
|
||||
|
||||
if (order.OrderString == "Enter")
|
||||
{
|
||||
var res = order.TargetActor.traits.GetOrDefault<Reservable>();
|
||||
if (res != null)
|
||||
{
|
||||
if (res.IsReserved) return;
|
||||
reservation = res.Reserve(self);
|
||||
}
|
||||
|
||||
self.CancelActivity();
|
||||
self.QueueActivity(new ReturnToBase(self, order.TargetActor));
|
||||
}
|
||||
|
||||
26
OpenRa.Game/Traits/Reservable.cs
Normal file
26
OpenRa.Game/Traits/Reservable.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace OpenRa.Game.Traits
|
||||
{
|
||||
class Reservable : ITick
|
||||
{
|
||||
public Reservable(Actor self) { }
|
||||
Actor reservedFor;
|
||||
|
||||
public bool IsReserved { get { return reservedFor != null; } }
|
||||
|
||||
public void Tick(Actor self)
|
||||
{
|
||||
if (reservedFor == null)
|
||||
return; /* nothing to do */
|
||||
|
||||
if (reservedFor.IsDead) reservedFor = null; /* not likely to arrive now. */
|
||||
}
|
||||
|
||||
public IDisposable Reserve(Actor forActor)
|
||||
{
|
||||
reservedFor = forActor;
|
||||
return new DisposableAction(() => reservedFor = null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -394,7 +394,7 @@ LongDesc=Stores excess harvested Ore
|
||||
|
||||
[HPAD]
|
||||
Description=Helipad
|
||||
Traits=Building, RenderBuilding, Production, BelowUnits
|
||||
Traits=Building, RenderBuilding, Production, BelowUnits, Reservable
|
||||
Dimensions=2,2
|
||||
Footprint=xx xx
|
||||
Produces=Plane
|
||||
@@ -410,7 +410,7 @@ SelectionPriority=3
|
||||
LongDesc=Provides an overview of the battlefield.\n Requires power to operate.
|
||||
[AFLD]
|
||||
Description=Airstrip
|
||||
Traits=Building, RenderBuilding, Production, BelowUnits
|
||||
Traits=Building, RenderBuilding, Production, BelowUnits, Reservable
|
||||
Dimensions=3,2
|
||||
Footprint=xxx xxx
|
||||
Produces=Plane
|
||||
@@ -465,7 +465,7 @@ SelectionPriority=3
|
||||
LongDesc=Produces attack dogs
|
||||
[FIX]
|
||||
Description=Service Depot
|
||||
Traits=Building, RenderBuilding, BelowUnits
|
||||
Traits=Building, RenderBuilding, BelowUnits, Reservable
|
||||
Dimensions=3,3
|
||||
Footprint=_x_ xxx _x_
|
||||
SelectionPriority=3
|
||||
|
||||
Reference in New Issue
Block a user