categorize traits better
This commit is contained in:
43
OpenRa.Game/Traits/Player/PlaceBuilding.cs
Normal file
43
OpenRa.Game/Traits/Player/PlaceBuilding.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using OpenRa.GameRules;
|
||||
|
||||
namespace OpenRa.Traits
|
||||
{
|
||||
class PlaceBuildingInfo : StatelessTraitInfo<PlaceBuilding> { }
|
||||
|
||||
class PlaceBuilding : IResolveOrder
|
||||
{
|
||||
public void ResolveOrder( Actor self, Order order )
|
||||
{
|
||||
if( order.OrderString == "PlaceBuilding" )
|
||||
{
|
||||
self.World.AddFrameEndTask( _ =>
|
||||
{
|
||||
var queue = self.traits.Get<ProductionQueue>();
|
||||
var unit = Rules.Info[ order.TargetString ];
|
||||
var producing = queue.CurrentItem(unit.Category);
|
||||
if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 )
|
||||
return;
|
||||
|
||||
self.World.CreateActor( order.TargetString, order.TargetLocation, order.Player );
|
||||
Sound.PlayToPlayer(order.Player, "placbldg.aud");
|
||||
Sound.PlayToPlayer(order.Player, "build5.aud");
|
||||
|
||||
var facts = self.World.Queries.OwnedBy[self.Owner]
|
||||
.WithTrait<ConstructionYard>().Select(x => x.Actor);
|
||||
|
||||
var primaryFact = facts.Where(y => y.traits.Get<Production>().IsPrimary);
|
||||
var fact = (primaryFact.Count() > 0) ? primaryFact.FirstOrDefault() : facts.FirstOrDefault();
|
||||
|
||||
if (fact != null)
|
||||
fact.traits.Get<RenderBuilding>().PlayCustomAnim(fact, "build");
|
||||
|
||||
queue.FinishProduction(unit.Category);
|
||||
} );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
232
OpenRa.Game/Traits/Player/ProductionQueue.cs
Normal file
232
OpenRa.Game/Traits/Player/ProductionQueue.cs
Normal file
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using IjwFramework.Collections;
|
||||
|
||||
namespace OpenRa.Traits
|
||||
{
|
||||
class ProductionQueueInfo : ITraitInfo
|
||||
{
|
||||
public readonly string BuildingSelectAudio = "abldgin1.aud";
|
||||
public readonly string BuildingReadyAudio = "conscmp1.aud";
|
||||
public readonly string BuildingCannotPlaceAudio = "nodeply1.aud";
|
||||
public readonly string UnitSelectAudio = "train1.aud";
|
||||
public readonly string UnitReadyAudio = "unitrdy1.aud";
|
||||
public readonly string OnHoldAudio = "onhold1.aud";
|
||||
public readonly string CancelledAudio = "cancld1.aud";
|
||||
public readonly string ClickAudio = "ramenu1.aud";
|
||||
|
||||
public object Create(Actor self) { return new ProductionQueue(self); }
|
||||
}
|
||||
|
||||
class ProductionQueue : IResolveOrder, ITick
|
||||
{
|
||||
Actor self;
|
||||
|
||||
public ProductionQueue( Actor self )
|
||||
{
|
||||
this.self = self;
|
||||
}
|
||||
|
||||
public void Tick( Actor self )
|
||||
{
|
||||
foreach( var p in production )
|
||||
if( p.Value.Count > 0 )
|
||||
(p.Value)[0].Tick( self.Owner );
|
||||
}
|
||||
|
||||
public void ResolveOrder( Actor self, Order order )
|
||||
{
|
||||
switch( order.OrderString )
|
||||
{
|
||||
case "StartProduction":
|
||||
{
|
||||
var unit = Rules.Info[ order.TargetString ];
|
||||
var ui = unit.Traits.Get<BuildableInfo>();
|
||||
var time = ui.Cost
|
||||
* Rules.General.BuildSpeed /* todo: country-specific build speed bonus */
|
||||
* ( 25 * 60 ) /* frames per min */ /* todo: build acceleration, if we do that */
|
||||
/ 1000;
|
||||
|
||||
time = .08f * time; /* temporary hax so we can build stuff fast for test */
|
||||
|
||||
if( !Rules.TechTree.BuildableItems( order.Player, unit.Category ).Contains( order.TargetString ) )
|
||||
return; /* you can't build that!! */
|
||||
|
||||
bool hasPlayedSound = false;
|
||||
|
||||
BeginProduction( unit.Category,
|
||||
new ProductionItem( order.TargetString, (int)time, ui.Cost,
|
||||
() => self.World.AddFrameEndTask(
|
||||
_ =>
|
||||
{
|
||||
var isBuilding = unit.Traits.Contains<BuildingInfo>();
|
||||
if( !hasPlayedSound )
|
||||
{
|
||||
Sound.PlayToPlayer( order.Player, isBuilding ? self.Info.Traits.Get<ProductionQueueInfo>().BuildingReadyAudio : self.Info.Traits.Get<ProductionQueueInfo>().UnitReadyAudio );
|
||||
hasPlayedSound = true;
|
||||
}
|
||||
if( !isBuilding )
|
||||
BuildUnit( order.TargetString );
|
||||
} ) ) );
|
||||
break;
|
||||
}
|
||||
case "PauseProduction":
|
||||
{
|
||||
var producing = CurrentItem( Rules.Info[ order.TargetString ].Category );
|
||||
if( producing != null && producing.Item == order.TargetString )
|
||||
producing.Paused = ( order.TargetLocation.X != 0 );
|
||||
break;
|
||||
}
|
||||
case "CancelProduction":
|
||||
{
|
||||
CancelProduction(order.TargetString);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Key: Production category.
|
||||
// TODO: sync this
|
||||
readonly Cache<string, List<ProductionItem>> production
|
||||
= new Cache<string, List<ProductionItem>>( _ => new List<ProductionItem>() );
|
||||
|
||||
public ProductionItem CurrentItem(string category)
|
||||
{
|
||||
return production[category].ElementAtOrDefault(0);
|
||||
}
|
||||
|
||||
public IEnumerable<ProductionItem> AllItems(string category)
|
||||
{
|
||||
return production[category];
|
||||
}
|
||||
|
||||
public void CancelProduction( string itemName )
|
||||
{
|
||||
var category = Rules.Info[itemName].Category;
|
||||
var queue = production[ category ];
|
||||
if (queue.Count == 0) return;
|
||||
|
||||
var lastIndex = queue.FindLastIndex( a => a.Item == itemName );
|
||||
if (lastIndex > 0)
|
||||
{
|
||||
queue.RemoveAt(lastIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
var item = queue[0];
|
||||
self.Owner.GiveCash(item.TotalCost - item.RemainingCost); // refund what's been paid so far.
|
||||
FinishProduction(category);
|
||||
}
|
||||
}
|
||||
|
||||
public void FinishProduction( string category )
|
||||
{
|
||||
var queue = production[category];
|
||||
if (queue.Count == 0) return;
|
||||
queue.RemoveAt(0);
|
||||
}
|
||||
|
||||
public void BeginProduction( string group, ProductionItem item )
|
||||
{
|
||||
production[group].Add(item);
|
||||
}
|
||||
|
||||
public void BuildUnit( string name )
|
||||
{
|
||||
var newUnitType = Rules.Info[ name ];
|
||||
var producerTypes = Rules.TechTree.UnitBuiltAt( newUnitType );
|
||||
Actor producer = null;
|
||||
|
||||
// Prioritise primary structure in build order
|
||||
var primaryProducers = self.World.Queries.OwnedBy[self.Owner]
|
||||
.WithTrait<Production>()
|
||||
.Where(x => producerTypes.Contains(x.Actor.Info)
|
||||
&& x.Trait.IsPrimary);
|
||||
|
||||
foreach (var p in primaryProducers)
|
||||
{
|
||||
// Ignore buildings that are disabled
|
||||
if (p.Actor.traits.Contains<Building>() && p.Actor.traits.Get<Building>().Disabled)
|
||||
continue;
|
||||
producer = p.Actor;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO: Be smart about disabled buildings. Units in progress should be paused(?)
|
||||
// Ignore this for now
|
||||
|
||||
// Pick the first available producer
|
||||
if (producer == null)
|
||||
{
|
||||
producer = self.World.Queries.OwnedBy[self.Owner]
|
||||
.Where( x => producerTypes.Contains( x.Info ) )
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
// Something went wrong somewhere...
|
||||
if( producer == null )
|
||||
{
|
||||
CancelProduction( newUnitType.Category );
|
||||
return;
|
||||
}
|
||||
|
||||
if( producer.traits.WithInterface<IProducer>().Any( p => p.Produce( producer, newUnitType ) ) )
|
||||
FinishProduction( newUnitType.Category );
|
||||
}
|
||||
}
|
||||
|
||||
class ProductionItem
|
||||
{
|
||||
public readonly string Item;
|
||||
|
||||
public readonly int TotalTime;
|
||||
public readonly int TotalCost;
|
||||
public int RemainingTime { get; private set; }
|
||||
public int RemainingCost { get; private set; }
|
||||
|
||||
public bool Paused = false, Done = false;
|
||||
public Action OnComplete;
|
||||
|
||||
int slowdown = 0;
|
||||
|
||||
public ProductionItem(string item, int time, int cost, Action onComplete)
|
||||
{
|
||||
if (time <= 0)
|
||||
time = 1;
|
||||
Item = item;
|
||||
RemainingTime = TotalTime = time;
|
||||
RemainingCost = TotalCost = cost;
|
||||
OnComplete = onComplete;
|
||||
}
|
||||
|
||||
public void Tick(Player player)
|
||||
{
|
||||
if (Done)
|
||||
{
|
||||
if (OnComplete != null) OnComplete();
|
||||
return;
|
||||
}
|
||||
|
||||
if (Paused) return;
|
||||
|
||||
if (player.GetPowerState() != PowerState.Normal)
|
||||
{
|
||||
if (--slowdown <= 0)
|
||||
slowdown = Rules.General.LowPowerSlowdown;
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
var costThisFrame = RemainingCost / RemainingTime;
|
||||
if (costThisFrame != 0 && !player.TakeCash(costThisFrame)) return;
|
||||
|
||||
RemainingCost -= costThisFrame;
|
||||
RemainingTime -= 1;
|
||||
if (RemainingTime > 0) return;
|
||||
|
||||
Done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user