Update copyright header. Normalize line endings to LF.

This commit is contained in:
Paul Chote
2011-02-13 10:38:57 +13:00
parent ea5e2c0588
commit 094907c1a9
489 changed files with 43614 additions and 43613 deletions

View File

@@ -1,34 +1,34 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class ActorGroupProxyInfo : TraitInfo<ActorGroupProxy> { }
class ActorGroupProxy : IResolveOrder
{
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "CreateGroup")
{
/* create a group */
var actors = order.TargetString.Split(',')
.Select(id => uint.Parse(id))
.Select(id => self.World.Actors.FirstOrDefault(a => a.ActorID == id))
.Where(a => a != null);
new Group(actors);
}
}
}
}
#region Copyright & License Information
/*
* Copyright 2007-2011 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.Traits;
namespace OpenRA.Mods.RA
{
class ActorGroupProxyInfo : TraitInfo<ActorGroupProxy> { }
class ActorGroupProxy : IResolveOrder
{
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "CreateGroup")
{
/* create a group */
var actors = order.TargetString.Split(',')
.Select(id => uint.Parse(id))
.Select(id => self.World.Actors.FirstOrDefault(a => a.ActorID == id))
.Where(a => a != null);
new Group(actors);
}
}
}
}

View File

@@ -1,16 +1,16 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2011 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 LICENSE.
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Buildings;
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
@@ -18,8 +18,8 @@ namespace OpenRA.Mods.RA
public class ClassicProductionQueueInfo : ProductionQueueInfo, ITraitPrerequisite<TechTreeInfo>, ITraitPrerequisite<PowerManagerInfo>, ITraitPrerequisite<PlayerResourcesInfo>
{
public override object Create(ActorInitializer init) { return new ClassicProductionQueue(init.self, this); }
}
}
public class ClassicProductionQueue : ProductionQueue, ISync
{
public ClassicProductionQueue( Actor self, ClassicProductionQueueInfo info )

View File

@@ -1,117 +1,117 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System.Linq;
using OpenRA.Effects;
using OpenRA.FileFormats;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class PlaceBuildingInfo : TraitInfo<PlaceBuilding> {}
class PlaceBuilding : IResolveOrder
{
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "PlaceBuilding" || order.OrderString == "LineBuild")
{
self.World.AddFrameEndTask(w =>
{
var prevItems = GetNumBuildables(self.Owner);
// Find the queue with the target actor
var queue = w.Queries.WithTrait<ProductionQueue>()
.Where(p => p.Actor.Owner == self.Owner &&
p.Trait.CurrentItem() != null &&
p.Trait.CurrentItem().Item == order.TargetString &&
p.Trait.CurrentItem().RemainingTime == 0)
.Select(p => p.Trait)
.FirstOrDefault();
if (queue == null)
return;
var unit = Rules.Info[order.TargetString];
var buildingInfo = unit.Traits.Get<BuildingInfo>();
if (order.OrderString == "LineBuild")
{
bool playSounds = true;
foreach (var t in BuildingUtils.GetLineBuildCells(w, order.TargetLocation, order.TargetString, buildingInfo))
{
var building = w.CreateActor(order.TargetString, new TypeDictionary
{
new LocationInit( t ),
new OwnerInit( order.Player ),
});
if (playSounds)
foreach (var s in buildingInfo.BuildSounds)
Sound.PlayToPlayer(order.Player, s, building.CenterLocation);
playSounds = false;
}
}
else
{
if (!self.World.CanPlaceBuilding(order.TargetString, buildingInfo, order.TargetLocation, null))
{
return;
}
var building = w.CreateActor(order.TargetString, new TypeDictionary
{
new LocationInit( order.TargetLocation ),
new OwnerInit( order.Player ),
});
foreach (var s in buildingInfo.BuildSounds)
Sound.PlayToPlayer(order.Player, s, building.CenterLocation);
}
PlayBuildAnim( self, unit );
queue.FinishProduction();
if (GetNumBuildables(self.Owner) > prevItems)
w.Add(new DelayedAction(10,
() => Sound.PlayToPlayer(order.Player,
w.WorldActor.Info.Traits.Get<EvaAlertsInfo>().NewOptions)));
});
}
}
// finds a construction yard (or equivalent) and runs its "build" animation.
static void PlayBuildAnim( Actor self, ActorInfo unit )
{
var bi = unit.Traits.GetOrDefault<BuildableInfo>();
if (bi == null)
return;
var producers = self.World.Queries.OwnedBy[ self.Owner ].WithTrait<Production>()
.Where( x => x.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains( bi.Queue ) )
.ToList();
var producer = producers.Where( x => x.Actor.IsPrimaryBuilding() ).Concat( producers )
.FirstOrDefault();
if( producer.Actor != null )
producer.Actor.TraitsImplementing<RenderSimple>().First().PlayCustomAnim( producer.Actor, "build" );
}
static int GetNumBuildables(Player p)
{
if (p != p.World.LocalPlayer) return 0; // this only matters for local players.
return p.World.Queries.WithTrait<ProductionQueue>()
.Where(a => a.Actor.Owner == p)
.SelectMany(a => a.Trait.BuildableItems()).Distinct().Count();
}
}
}
#region Copyright & License Information
/*
* Copyright 2007-2011 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.Effects;
using OpenRA.FileFormats;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class PlaceBuildingInfo : TraitInfo<PlaceBuilding> {}
class PlaceBuilding : IResolveOrder
{
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "PlaceBuilding" || order.OrderString == "LineBuild")
{
self.World.AddFrameEndTask(w =>
{
var prevItems = GetNumBuildables(self.Owner);
// Find the queue with the target actor
var queue = w.Queries.WithTrait<ProductionQueue>()
.Where(p => p.Actor.Owner == self.Owner &&
p.Trait.CurrentItem() != null &&
p.Trait.CurrentItem().Item == order.TargetString &&
p.Trait.CurrentItem().RemainingTime == 0)
.Select(p => p.Trait)
.FirstOrDefault();
if (queue == null)
return;
var unit = Rules.Info[order.TargetString];
var buildingInfo = unit.Traits.Get<BuildingInfo>();
if (order.OrderString == "LineBuild")
{
bool playSounds = true;
foreach (var t in BuildingUtils.GetLineBuildCells(w, order.TargetLocation, order.TargetString, buildingInfo))
{
var building = w.CreateActor(order.TargetString, new TypeDictionary
{
new LocationInit( t ),
new OwnerInit( order.Player ),
});
if (playSounds)
foreach (var s in buildingInfo.BuildSounds)
Sound.PlayToPlayer(order.Player, s, building.CenterLocation);
playSounds = false;
}
}
else
{
if (!self.World.CanPlaceBuilding(order.TargetString, buildingInfo, order.TargetLocation, null))
{
return;
}
var building = w.CreateActor(order.TargetString, new TypeDictionary
{
new LocationInit( order.TargetLocation ),
new OwnerInit( order.Player ),
});
foreach (var s in buildingInfo.BuildSounds)
Sound.PlayToPlayer(order.Player, s, building.CenterLocation);
}
PlayBuildAnim( self, unit );
queue.FinishProduction();
if (GetNumBuildables(self.Owner) > prevItems)
w.Add(new DelayedAction(10,
() => Sound.PlayToPlayer(order.Player,
w.WorldActor.Info.Traits.Get<EvaAlertsInfo>().NewOptions)));
});
}
}
// finds a construction yard (or equivalent) and runs its "build" animation.
static void PlayBuildAnim( Actor self, ActorInfo unit )
{
var bi = unit.Traits.GetOrDefault<BuildableInfo>();
if (bi == null)
return;
var producers = self.World.Queries.OwnedBy[ self.Owner ].WithTrait<Production>()
.Where( x => x.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains( bi.Queue ) )
.ToList();
var producer = producers.Where( x => x.Actor.IsPrimaryBuilding() ).Concat( producers )
.FirstOrDefault();
if( producer.Actor != null )
producer.Actor.TraitsImplementing<RenderSimple>().First().PlayCustomAnim( producer.Actor, "build" );
}
static int GetNumBuildables(Player p)
{
if (p != p.World.LocalPlayer) return 0; // this only matters for local players.
return p.World.Queries.WithTrait<ProductionQueue>()
.Where(a => a.Actor.Owner == p)
.SelectMany(a => a.Trait.BuildableItems()).Distinct().Count();
}
}
}

View File

@@ -1,349 +1,349 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class ProductionQueueInfo : ITraitInfo
{
public readonly string Type = null;
public float BuildSpeed = 0.4f;
public readonly int LowPowerSlowdown = 3;
public readonly string ReadyAudio = "unitrdy1.aud";
public readonly string BlockedAudio = "nobuild1.aud";
public readonly string QueuedAudio = "train1.aud";
public readonly string OnHoldAudio = "onhold1.aud";
public readonly string CancelledAudio = "cancld1.aud";
public virtual object Create(ActorInitializer init) { return new ProductionQueue(init.self, init.self.Owner.PlayerActor, this); }
}
public class ProductionQueue : IResolveOrder, ITick, ITechTreeElement, INotifyCapture, ISync
{
public readonly Actor self;
public ProductionQueueInfo Info;
PowerManager PlayerPower;
PlayerResources PlayerResources;
// A list of things we are currently building
public List<ProductionItem> Queue = new List<ProductionItem>();
[Sync]
public int QueueLength { get { return Queue.Count; } }
[Sync]
public int CurrentRemainingCost { get { return QueueLength == 0 ? 0 : Queue[0].RemainingCost; } }
[Sync]
public int CurrentRemainingTime { get { return QueueLength == 0 ? 0 : Queue[0].RemainingTime; } }
[Sync]
public int CurrentSlowdown { get { return QueueLength == 0 ? 0 : Queue[0].slowdown; } }
[Sync]
public bool CurrentPaused { get { return QueueLength == 0 ? false : Queue[0].Paused; } }
[Sync]
public bool CurrentDone { get { return QueueLength == 0 ? false : Queue[0].Done; } }
// A list of things we could possibly build, even if our race doesn't normally get it
public Dictionary<ActorInfo, ProductionState> Produceable = new Dictionary<ActorInfo, ProductionState>();
public ProductionQueue( Actor self, Actor playerActor, ProductionQueueInfo info )
{
this.self = self;
this.Info = info;
PlayerResources = playerActor.Trait<PlayerResources>();
PlayerPower = playerActor.Trait<PowerManager>();
var ttc = playerActor.Trait<TechTree>();
foreach (var a in AllBuildables(Info.Type))
{
var bi = a.Traits.Get<BuildableInfo>();
// Can our race build this by satisfying normal prereqs?
var buildable = bi.Owner.Contains(self.Owner.Country.Race);
Produceable.Add( a, new ProductionState(){ Visible = buildable && !bi.Hidden } );
if (buildable)
ttc.Add( a.Name, a.Traits.Get<BuildableInfo>().Prerequisites.ToList(), this );
}
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
PlayerPower = newOwner.PlayerActor.Trait<PowerManager>();
PlayerResources = newOwner.PlayerActor.Trait<PlayerResources>();
Queue.Clear();
// Produceable contains the tech from the original owner - this is desired so we don't clear it.
}
IEnumerable<ActorInfo> AllBuildables(string category)
{
return Rules.Info.Values
.Where( x => x.Name[ 0 ] != '^' )
.Where( x => x.Traits.Contains<BuildableInfo>() )
.Where( x => x.Traits.Get<BuildableInfo>().Queue == category );
}
public void OverrideProduction(ActorInfo type, bool buildable)
{
Produceable[type].Buildable = buildable;
Produceable[type].Sticky = true;
}
public void PrerequisitesAvailable(string key)
{
var ps = Produceable[ Rules.Info[key] ];
if (!ps.Sticky)
ps.Buildable = true;
}
public void PrerequisitesUnavailable(string key)
{
var ps = Produceable[ Rules.Info[key] ];
if (!ps.Sticky)
ps.Buildable = false;
}
public ProductionItem CurrentItem()
{
return Queue.ElementAtOrDefault(0);
}
public IEnumerable<ProductionItem> AllQueued()
{
return Queue;
}
public virtual IEnumerable<ActorInfo> AllItems()
{
if (self.World.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
return Produceable.Select(a => a.Key);
return Produceable.Where(a => a.Value.Buildable || a.Value.Visible).Select(a => a.Key);
}
public virtual IEnumerable<ActorInfo> BuildableItems()
{
if (self.World.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
return Produceable.Select(a => a.Key);
return Produceable.Where(a => a.Value.Buildable).Select(a => a.Key);
}
public bool CanBuild(ActorInfo actor)
{
return Produceable.ContainsKey(actor) && Produceable[actor].Buildable;
}
public virtual void Tick( Actor self )
{
while( Queue.Count > 0 && !BuildableItems().Any(b => b.Name == Queue[ 0 ].Item) )
{
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(Queue[0].TotalCost - Queue[0].RemainingCost); // refund what's been paid so far.
FinishProduction();
}
if( Queue.Count > 0 )
Queue[ 0 ].Tick( PlayerResources, PlayerPower );
}
public void ResolveOrder( Actor self, Order order )
{
switch( order.OrderString )
{
case "StartProduction":
{
var unit = Rules.Info[order.TargetString];
var bi = unit.Traits.Get<BuildableInfo>();
if (bi.Queue != Info.Type)
return; /* Not built by this queue */
var cost = unit.Traits.Contains<ValuedInfo>() ? unit.Traits.Get<ValuedInfo>().Cost : 0;
var time = GetBuildTime(order.TargetString);
if (!BuildableItems().Any(b => b.Name == order.TargetString))
return; /* you can't build that!! */
for (var n = 0; n < order.TargetLocation.X; n++) // repeat count
{
bool hasPlayedSound = false;
BeginProduction(new ProductionItem(this, order.TargetString, (int)time, cost,
() => self.World.AddFrameEndTask(
_ =>
{
var isBuilding = unit.Traits.Contains<BuildingInfo>();
if (isBuilding && !hasPlayedSound)
{
Sound.PlayToPlayer(order.Player, Info.ReadyAudio);
hasPlayedSound = true;
}
else if (!isBuilding)
{
if (BuildUnit(order.TargetString))
Sound.PlayToPlayer(order.Player, Info.ReadyAudio);
else if (!hasPlayedSound && time > 0)
{
Sound.PlayToPlayer(order.Player, Info.BlockedAudio);
hasPlayedSound = true;
}
}
})));
}
break;
}
case "PauseProduction":
{
if( Queue.Count > 0 && Queue[0].Item == order.TargetString )
Queue[0].Paused = ( order.TargetLocation.X != 0 );
break;
}
case "CancelProduction":
{
CancelProduction(order.TargetString,order.TargetLocation.X);
break;
}
}
}
public int GetBuildTime(String unitString)
{
var unit = Rules.Info[unitString];
if (unit == null || ! unit.Traits.Contains<BuildableInfo>())
return 0;
if (self.World.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().FastBuild) return 0;
var cost = unit.Traits.Contains<ValuedInfo>() ? unit.Traits.Get<ValuedInfo>().Cost : 0;
var time = cost
* Info.BuildSpeed
* (25 * 60) /* frames per min */ /* todo: build acceleration, if we do that */
/ 1000;
return (int) time;
}
protected void CancelProduction(string itemName, int numberToCancel)
{
for (var i = 0; i < numberToCancel; i++)
CancelProductionInner(itemName);
}
void CancelProductionInner(string itemName)
{
var lastIndex = Queue.FindLastIndex(a => a.Item == itemName);
if (lastIndex > 0)
Queue.RemoveAt(lastIndex);
else if (lastIndex == 0)
{
var item = Queue[0];
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(
item.TotalCost - item.RemainingCost); // refund what has been paid
FinishProduction();
}
}
public void FinishProduction()
{
if (Queue.Count == 0) return;
Queue.RemoveAt(0);
}
protected void BeginProduction( ProductionItem item )
{
Queue.Add(item);
}
protected static bool IsDisabledBuilding(Actor a)
{
return a.TraitsImplementing<IDisable>().Any(d => d.Disabled);
}
// Builds a unit from the actor that holds this queue (1 queue per building)
// Returns false if the unit can't be built
protected virtual bool BuildUnit( string name )
{
// Cannot produce if i'm dead
if (!self.IsInWorld || self.IsDead())
{
CancelProduction(name, 1);
return true;
}
var sp = self.TraitsImplementing<Production>().Where(p => p.Info.Produces.Contains(Info.Type)).FirstOrDefault();
if (sp != null && !IsDisabledBuilding(self) && sp.Produce(self, Rules.Info[ name ]))
{
FinishProduction();
return true;
}
return false;
}
}
public class ProductionState
{
public bool Visible = false;
public bool Buildable = false;
public bool Sticky = false;
}
public class ProductionItem
{
public readonly string Item;
public readonly ProductionQueue Queue;
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;
public int slowdown = 0;
public ProductionItem(ProductionQueue queue, string item, int time, int cost, Action onComplete)
{
if (time <= 0) time = 1;
Item = item;
RemainingTime = TotalTime = time;
RemainingCost = TotalCost = cost;
OnComplete = onComplete;
Queue = queue;
//Log.Write("debug", "new ProductionItem: {0} time={1} cost={2}", item, time, cost);
}
public void Tick(PlayerResources pr, PowerManager pm)
{
if (Done)
{
if (OnComplete != null) OnComplete();
return;
}
if (Paused) return;
if (pm.PowerState != PowerState.Normal)
{
if (--slowdown <= 0)
slowdown = Queue.Info.LowPowerSlowdown;
else
return;
}
var costThisFrame = RemainingCost / RemainingTime;
if (costThisFrame != 0 && !pr.TakeCash(costThisFrame)) return;
RemainingCost -= costThisFrame;
RemainingTime -= 1;
if (RemainingTime > 0) return;
Done = true;
}
}
}
#region Copyright & License Information
/*
* Copyright 2007-2011 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;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class ProductionQueueInfo : ITraitInfo
{
public readonly string Type = null;
public float BuildSpeed = 0.4f;
public readonly int LowPowerSlowdown = 3;
public readonly string ReadyAudio = "unitrdy1.aud";
public readonly string BlockedAudio = "nobuild1.aud";
public readonly string QueuedAudio = "train1.aud";
public readonly string OnHoldAudio = "onhold1.aud";
public readonly string CancelledAudio = "cancld1.aud";
public virtual object Create(ActorInitializer init) { return new ProductionQueue(init.self, init.self.Owner.PlayerActor, this); }
}
public class ProductionQueue : IResolveOrder, ITick, ITechTreeElement, INotifyCapture, ISync
{
public readonly Actor self;
public ProductionQueueInfo Info;
PowerManager PlayerPower;
PlayerResources PlayerResources;
// A list of things we are currently building
public List<ProductionItem> Queue = new List<ProductionItem>();
[Sync]
public int QueueLength { get { return Queue.Count; } }
[Sync]
public int CurrentRemainingCost { get { return QueueLength == 0 ? 0 : Queue[0].RemainingCost; } }
[Sync]
public int CurrentRemainingTime { get { return QueueLength == 0 ? 0 : Queue[0].RemainingTime; } }
[Sync]
public int CurrentSlowdown { get { return QueueLength == 0 ? 0 : Queue[0].slowdown; } }
[Sync]
public bool CurrentPaused { get { return QueueLength == 0 ? false : Queue[0].Paused; } }
[Sync]
public bool CurrentDone { get { return QueueLength == 0 ? false : Queue[0].Done; } }
// A list of things we could possibly build, even if our race doesn't normally get it
public Dictionary<ActorInfo, ProductionState> Produceable = new Dictionary<ActorInfo, ProductionState>();
public ProductionQueue( Actor self, Actor playerActor, ProductionQueueInfo info )
{
this.self = self;
this.Info = info;
PlayerResources = playerActor.Trait<PlayerResources>();
PlayerPower = playerActor.Trait<PowerManager>();
var ttc = playerActor.Trait<TechTree>();
foreach (var a in AllBuildables(Info.Type))
{
var bi = a.Traits.Get<BuildableInfo>();
// Can our race build this by satisfying normal prereqs?
var buildable = bi.Owner.Contains(self.Owner.Country.Race);
Produceable.Add( a, new ProductionState(){ Visible = buildable && !bi.Hidden } );
if (buildable)
ttc.Add( a.Name, a.Traits.Get<BuildableInfo>().Prerequisites.ToList(), this );
}
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
PlayerPower = newOwner.PlayerActor.Trait<PowerManager>();
PlayerResources = newOwner.PlayerActor.Trait<PlayerResources>();
Queue.Clear();
// Produceable contains the tech from the original owner - this is desired so we don't clear it.
}
IEnumerable<ActorInfo> AllBuildables(string category)
{
return Rules.Info.Values
.Where( x => x.Name[ 0 ] != '^' )
.Where( x => x.Traits.Contains<BuildableInfo>() )
.Where( x => x.Traits.Get<BuildableInfo>().Queue == category );
}
public void OverrideProduction(ActorInfo type, bool buildable)
{
Produceable[type].Buildable = buildable;
Produceable[type].Sticky = true;
}
public void PrerequisitesAvailable(string key)
{
var ps = Produceable[ Rules.Info[key] ];
if (!ps.Sticky)
ps.Buildable = true;
}
public void PrerequisitesUnavailable(string key)
{
var ps = Produceable[ Rules.Info[key] ];
if (!ps.Sticky)
ps.Buildable = false;
}
public ProductionItem CurrentItem()
{
return Queue.ElementAtOrDefault(0);
}
public IEnumerable<ProductionItem> AllQueued()
{
return Queue;
}
public virtual IEnumerable<ActorInfo> AllItems()
{
if (self.World.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
return Produceable.Select(a => a.Key);
return Produceable.Where(a => a.Value.Buildable || a.Value.Visible).Select(a => a.Key);
}
public virtual IEnumerable<ActorInfo> BuildableItems()
{
if (self.World.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
return Produceable.Select(a => a.Key);
return Produceable.Where(a => a.Value.Buildable).Select(a => a.Key);
}
public bool CanBuild(ActorInfo actor)
{
return Produceable.ContainsKey(actor) && Produceable[actor].Buildable;
}
public virtual void Tick( Actor self )
{
while( Queue.Count > 0 && !BuildableItems().Any(b => b.Name == Queue[ 0 ].Item) )
{
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(Queue[0].TotalCost - Queue[0].RemainingCost); // refund what's been paid so far.
FinishProduction();
}
if( Queue.Count > 0 )
Queue[ 0 ].Tick( PlayerResources, PlayerPower );
}
public void ResolveOrder( Actor self, Order order )
{
switch( order.OrderString )
{
case "StartProduction":
{
var unit = Rules.Info[order.TargetString];
var bi = unit.Traits.Get<BuildableInfo>();
if (bi.Queue != Info.Type)
return; /* Not built by this queue */
var cost = unit.Traits.Contains<ValuedInfo>() ? unit.Traits.Get<ValuedInfo>().Cost : 0;
var time = GetBuildTime(order.TargetString);
if (!BuildableItems().Any(b => b.Name == order.TargetString))
return; /* you can't build that!! */
for (var n = 0; n < order.TargetLocation.X; n++) // repeat count
{
bool hasPlayedSound = false;
BeginProduction(new ProductionItem(this, order.TargetString, (int)time, cost,
() => self.World.AddFrameEndTask(
_ =>
{
var isBuilding = unit.Traits.Contains<BuildingInfo>();
if (isBuilding && !hasPlayedSound)
{
Sound.PlayToPlayer(order.Player, Info.ReadyAudio);
hasPlayedSound = true;
}
else if (!isBuilding)
{
if (BuildUnit(order.TargetString))
Sound.PlayToPlayer(order.Player, Info.ReadyAudio);
else if (!hasPlayedSound && time > 0)
{
Sound.PlayToPlayer(order.Player, Info.BlockedAudio);
hasPlayedSound = true;
}
}
})));
}
break;
}
case "PauseProduction":
{
if( Queue.Count > 0 && Queue[0].Item == order.TargetString )
Queue[0].Paused = ( order.TargetLocation.X != 0 );
break;
}
case "CancelProduction":
{
CancelProduction(order.TargetString,order.TargetLocation.X);
break;
}
}
}
public int GetBuildTime(String unitString)
{
var unit = Rules.Info[unitString];
if (unit == null || ! unit.Traits.Contains<BuildableInfo>())
return 0;
if (self.World.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().FastBuild) return 0;
var cost = unit.Traits.Contains<ValuedInfo>() ? unit.Traits.Get<ValuedInfo>().Cost : 0;
var time = cost
* Info.BuildSpeed
* (25 * 60) /* frames per min */ /* todo: build acceleration, if we do that */
/ 1000;
return (int) time;
}
protected void CancelProduction(string itemName, int numberToCancel)
{
for (var i = 0; i < numberToCancel; i++)
CancelProductionInner(itemName);
}
void CancelProductionInner(string itemName)
{
var lastIndex = Queue.FindLastIndex(a => a.Item == itemName);
if (lastIndex > 0)
Queue.RemoveAt(lastIndex);
else if (lastIndex == 0)
{
var item = Queue[0];
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(
item.TotalCost - item.RemainingCost); // refund what has been paid
FinishProduction();
}
}
public void FinishProduction()
{
if (Queue.Count == 0) return;
Queue.RemoveAt(0);
}
protected void BeginProduction( ProductionItem item )
{
Queue.Add(item);
}
protected static bool IsDisabledBuilding(Actor a)
{
return a.TraitsImplementing<IDisable>().Any(d => d.Disabled);
}
// Builds a unit from the actor that holds this queue (1 queue per building)
// Returns false if the unit can't be built
protected virtual bool BuildUnit( string name )
{
// Cannot produce if i'm dead
if (!self.IsInWorld || self.IsDead())
{
CancelProduction(name, 1);
return true;
}
var sp = self.TraitsImplementing<Production>().Where(p => p.Info.Produces.Contains(Info.Type)).FirstOrDefault();
if (sp != null && !IsDisabledBuilding(self) && sp.Produce(self, Rules.Info[ name ]))
{
FinishProduction();
return true;
}
return false;
}
}
public class ProductionState
{
public bool Visible = false;
public bool Buildable = false;
public bool Sticky = false;
}
public class ProductionItem
{
public readonly string Item;
public readonly ProductionQueue Queue;
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;
public int slowdown = 0;
public ProductionItem(ProductionQueue queue, string item, int time, int cost, Action onComplete)
{
if (time <= 0) time = 1;
Item = item;
RemainingTime = TotalTime = time;
RemainingCost = TotalCost = cost;
OnComplete = onComplete;
Queue = queue;
//Log.Write("debug", "new ProductionItem: {0} time={1} cost={2}", item, time, cost);
}
public void Tick(PlayerResources pr, PowerManager pm)
{
if (Done)
{
if (OnComplete != null) OnComplete();
return;
}
if (Paused) return;
if (pm.PowerState != PowerState.Normal)
{
if (--slowdown <= 0)
slowdown = Queue.Info.LowPowerSlowdown;
else
return;
}
var costThisFrame = RemainingCost / RemainingTime;
if (costThisFrame != 0 && !pr.TakeCash(costThisFrame)) return;
RemainingCost -= costThisFrame;
RemainingTime -= 1;
if (RemainingTime > 0) return;
Done = true;
}
}
}

View File

@@ -1,44 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using OpenRA.Network;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SurrenderOnDisconnectInfo : TraitInfo<SurrenderOnDisconnect>
{
}
class SurrenderOnDisconnect : ITick
{
private bool Disconnected = false;
public void Tick(Actor self)
{
if (Disconnected) return;
var p = self.Owner;
if (p.WinState == WinState.Lost || p.WinState == WinState.Won) return; /* already won or lost */
var client = p.World.LobbyInfo.ClientWithIndex(p.ClientIndex);
if (client == null)
return;
if (client.State == Session.ClientState.Disconnected)
{
Disconnected = true; /* dont call this multiple times! */
self.World.players.Do(pl => pl.Value.PlayerActor.TraitsImplementing<IResolveOrder>().Do(t => t.ResolveOrder(pl.Value.PlayerActor, new Order("Surrender", self, false))));
}
}
}
}
#region Copyright & License Information
/*
* Copyright 2007-2011 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 OpenRA.Network;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SurrenderOnDisconnectInfo : TraitInfo<SurrenderOnDisconnect>
{
}
class SurrenderOnDisconnect : ITick
{
private bool Disconnected = false;
public void Tick(Actor self)
{
if (Disconnected) return;
var p = self.Owner;
if (p.WinState == WinState.Lost || p.WinState == WinState.Won) return; /* already won or lost */
var client = p.World.LobbyInfo.ClientWithIndex(p.ClientIndex);
if (client == null)
return;
if (client.State == Session.ClientState.Disconnected)
{
Disconnected = true; /* dont call this multiple times! */
self.World.players.Do(pl => pl.Value.PlayerActor.TraitsImplementing<IResolveOrder>().Do(t => t.ResolveOrder(pl.Value.PlayerActor, new Order("Surrender", self, false))));
}
}
}
}