Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Copyright & License Information
- /*
- * Copyright 2017-2017 The MW Developers)
- * This file is part of Medieval Warfare, 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, either version 3 of
- * the License, or (at your option) any later version. For more
- * information, see COPYING.
- */
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using OpenRA.Activities;
- using OpenRA.Mods.Common.Activities;
- using OpenRA.Mods.Common.Scripting;
- using OpenRA.Mods.Common.Traits;
- using OpenRA.Traits;
- namespace OpenRA.Mods.Mw.Traits
- {
- [Desc("A actor has to enter the building before the unit spawns.")]
- public class WithActorProductionInfo : ProductionInfo, Requires<ExitInfo>
- {
- public readonly string ReadyAudio = "UnitReady";
- [Desc("Valid actortypes wich can pe used to produce/ convert.")]
- public readonly HashSet<string> TrainingActors = new HashSet<string>();
- [Desc("The range in cells where should be looked for.")]
- public readonly int FindRadius = 0;
- public override object Create(ActorInitializer init) { return new WithActorProduction(init, this); }
- }
- class WithActorProduction : Production
- {
- readonly WithActorProductionInfo info;
- [Sync] private HashSet<Actor> InUse = new HashSet<Actor>();
- public WithActorProduction(ActorInitializer init, WithActorProductionInfo info)
- : base(init, info)
- {
- this.info = info;
- }
- bool IsAcceptableActor(Actor mover)
- {
- return info.TrainingActors.Count > 0 ||
- info.TrainingActors.Contains(mover.Info.Name);
- }
- public IEnumerable<Actor> FindProducingActors(Actor self)
- {
- var possibles = self.World.ActorsHavingTrait<WithActorProduction>()
- .Where(a =>
- {
- if (a.Owner != self.Owner)
- return false;
- return true;
- });
- return possibles;
- }
- public bool CheckHash(Actor actor, HashSet<Actor> hashSet)
- {
- if (hashSet.Contains(actor))
- return false;
- }
- public Actor PossibleActor(Actor self)
- {
- // Find all Actors within the range, and filter thier Type and if they r taken
- var possibles = self.World.FindActorsInCircle(self.CenterPosition, WDist.FromCells(info.FindRadius))
- .Where(a =>
- {
- if (a == self)
- return false;
- if (a.Owner != self.Owner)
- return false;
- var Factories = FindProducingActors(self);
- foreach (var n in Factories)
- {
- if (n.CheckHash(a))
- return false;
- }
- return IsAcceptableActor(a);
- });
- // Filter the Actors by path lenght
- //List<CPos> path;
- var closest = possibles.ClosestTo(self);
- //var mi = self.Info.TraitInfo<MobileInfo>();
- //using (var search = PathSearch.FromPoints(self.World, mi, self, possibles.Select(r => r.Location), self.Location, false))
- // path = self.World.WorldActor.Trait<IPathFinder>().FindPath(search);
- //Take the closest Actor
- if (closest != null)
- return closest;
- return null;
- }
- public WPos Position(Actor actor)
- {
- return actor.CenterPosition;
- }
- public override bool Produce(Actor self, ActorInfo producee, string factionVariant)
- {
- var owner = self.Owner;
- var exit = CPos.Zero;
- //var infiltrate = self.CenterPosition + exitinfo.SpawnOffset;
- owner.World.AddFrameEndTask(w =>
- {
- if (!self.IsInWorld || self.IsDead)
- return;
- var actor = PossibleActor(self);
- if (actor == null)
- return;
- InUse.Add(actor);
- var newexit = self.Info.TraitInfos<ExitInfo>().First();
- var move = actor.TraitOrDefault<IMove>();
- exit = self.Location + newexit.ExitCell;
- var infiltrate = self.CenterPosition + newexit.SpawnOffset;
- if (!actor.IsInWorld || !actor.IsDead)
- {
- actor.CancelActivity();
- actor.QueueActivity(move.MoveIntoWorld(actor, exit));
- actor.QueueActivity(new CallFunc(() =>
- {
- if (!self.IsInWorld || self.IsDead)
- {
- if (InUse.Contains(actor))
- InUse.Add(actor);
- }
- }));
- actor.QueueActivity(move.VisualMove(actor, Position(actor), infiltrate));
- actor.QueueActivity(new CallFunc(() =>
- {
- if (!self.IsInWorld || self.IsDead)
- {
- if (InUse.Contains(actor))
- InUse.Add(actor);
- }
- if (InUse.Contains(actor))
- InUse.Add(actor);
- self.World.AddFrameEndTask(ww => DoProduction(self, producee, newexit, factionVariant));
- Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", info.ReadyAudio,
- self.Owner.Faction.InternalName);
- }));
- actor.QueueActivity(new RemoveSelf());
- actor.QueueActivity(new CallFunc(() =>
- {
- if (!self.IsInWorld || self.IsDead)
- {
- if (InUse.Contains(actor))
- InUse.Add(actor);
- }
- }));
- }
- });
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement