#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; using System.Drawing; using System.Linq; using OpenRA.Activities; using OpenRA.Mods.Common.Activities; using OpenRA.Primitives; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("This unit has access to build queues.")] public class ProductionInfo : ITraitInfo { [Desc("e.g. Infantry, Vehicles, Aircraft, Buildings")] public readonly string[] Produces = { }; public virtual object Create(ActorInitializer init) { return new Production(init, this); } } public class Production { readonly Lazy rp; public ProductionInfo Info; public string Race { get; private set; } public Production(ActorInitializer init, ProductionInfo info) { Info = info; rp = Exts.Lazy(() => init.Self.IsDead ? null : init.Self.TraitOrDefault()); Race = init.Contains() ? init.Get() : init.Self.Owner.Country.Race; } public void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string raceVariant) { var exit = self.Location + exitinfo.ExitCell; var spawn = self.CenterPosition + exitinfo.SpawnOffset; var to = self.World.Map.CenterOfCell(exit); var fi = producee.Traits.GetOrDefault(); var initialFacing = exitinfo.Facing < 0 ? Util.GetFacing(to - spawn, fi == null ? 0 : fi.GetInitialFacing()) : exitinfo.Facing; var exitLocation = rp.Value != null ? rp.Value.Location : exit; var target = Target.FromCell(self.World, exitLocation); var bi = producee.Traits.GetOrDefault(); if (bi != null && bi.ForceRace != null) raceVariant = bi.ForceRace; self.World.AddFrameEndTask(w => { var td = new TypeDictionary { new OwnerInit(self.Owner), new LocationInit(exit), new CenterPositionInit(spawn), new FacingInit(initialFacing) }; if (raceVariant != null) td.Add(new RaceInit(raceVariant)); var newUnit = self.World.CreateActor(producee.Name, td); var move = newUnit.TraitOrDefault(); if (move != null) { if (exitinfo.MoveIntoWorld) { newUnit.QueueActivity(move.MoveIntoWorld(newUnit, exit)); newUnit.QueueActivity(new AttackMoveActivity( newUnit, move.MoveTo(exitLocation, 1))); } } newUnit.SetTargetLine(target, rp.Value != null ? Color.Red : Color.Green, false); if (!self.IsDead) foreach (var t in self.TraitsImplementing()) t.UnitProduced(self, newUnit, exit); var notifyOthers = self.World.ActorsWithTrait(); foreach (var notify in notifyOthers) notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit); if (bi != null && bi.InitialActivity != null) newUnit.QueueActivity(Game.CreateObject(bi.InitialActivity)); foreach (var t in newUnit.TraitsImplementing()) t.BuildingComplete(newUnit); }); } public virtual bool Produce(Actor self, ActorInfo producee, string raceVariant) { if (Reservable.IsReserved(self)) return false; // pick a spawn/exit point pair var exit = self.Info.Traits.WithInterface().Shuffle(self.World.SharedRandom) .FirstOrDefault(e => CanUseExit(self, producee, e)); if (exit != null) { DoProduction(self, producee, exit, raceVariant); return true; } return false; } static bool CanUseExit(Actor self, ActorInfo producee, ExitInfo s) { var mobileInfo = producee.Traits.GetOrDefault(); self.NotifyBlocker(self.Location + s.ExitCell); return mobileInfo == null || mobileInfo.CanEnterCell(self.World, self, self.Location + s.ExitCell, self); } } }