Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. #region Copyright & License Information
  2. /*
  3.  * Copyright 2017-2017 The MW Developers)
  4.  * This file is part of Medieval Warfare, which is free software. It is made
  5.  * available to you under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation, either version 3 of
  7.  * the License, or (at your option) any later version. For more
  8.  * information, see COPYING.
  9.  */
  10. #endregion
  11.  
  12.  
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using OpenRA.Activities;
  17. using OpenRA.Mods.Common.Activities;
  18. using OpenRA.Mods.Common.Scripting;
  19. using OpenRA.Mods.Common.Traits;
  20. using OpenRA.Traits;
  21.  
  22.  
  23. namespace OpenRA.Mods.Mw.Traits
  24. {
  25.     [Desc("A actor has to enter the building before the unit spawns.")]
  26.     public class WithActorProductionInfo : ProductionInfo, Requires<ExitInfo>
  27.     {
  28.         public readonly string ReadyAudio = "UnitReady";
  29.        
  30.         [Desc("Valid actortypes wich can pe used to produce/ convert.")]
  31.         public readonly HashSet<string> TrainingActors = new HashSet<string>();
  32.        
  33.         [Desc("The range in cells where should be looked for.")]
  34.         public readonly int FindRadius = 0;
  35.        
  36.         public override object Create(ActorInitializer init) { return new WithActorProduction(init, this); }
  37.     }
  38.  
  39.     class WithActorProduction : Production
  40.     {
  41.         readonly WithActorProductionInfo info;
  42.         [Sync] private HashSet<Actor> InUse = new HashSet<Actor>();
  43.        
  44.         public WithActorProduction(ActorInitializer init, WithActorProductionInfo info)
  45.             : base(init, info)
  46.         {
  47.             this.info = info;
  48.         }
  49.        
  50.         bool IsAcceptableActor(Actor mover)
  51.         {
  52.             return info.TrainingActors.Count > 0 ||
  53.                    info.TrainingActors.Contains(mover.Info.Name);
  54.         }
  55.  
  56.         public IEnumerable<Actor> FindProducingActors(Actor self)
  57.         {
  58.             var possibles = self.World.ActorsHavingTrait<WithActorProduction>()
  59.                 .Where(a =>
  60.                 {
  61.                     if (a.Owner != self.Owner)
  62.                         return false;
  63.                    
  64.                     return true;
  65.                 });
  66.             return possibles;
  67.         }
  68.  
  69.         public bool CheckHash(Actor actor, HashSet<Actor> hashSet)
  70.         {
  71.             if (hashSet.Contains(actor))
  72.                 return false;
  73.         }
  74.  
  75.         public Actor PossibleActor(Actor self)
  76.         {
  77.             // Find all Actors within the range, and filter thier Type and if they r taken
  78.             var possibles = self.World.FindActorsInCircle(self.CenterPosition, WDist.FromCells(info.FindRadius))
  79.                 .Where(a =>
  80.                     {
  81.                         if (a == self)
  82.                             return false;
  83.  
  84.                         if (a.Owner != self.Owner)
  85.                             return false;
  86.  
  87.                         var Factories = FindProducingActors(self);
  88.  
  89.                         foreach (var n in Factories)
  90.                         {
  91.                             if (n.CheckHash(a))
  92.                                 return false;
  93.                         }
  94.  
  95.                         return IsAcceptableActor(a);
  96.                     });
  97.                
  98.             // Filter the Actors by path lenght
  99.             //List<CPos> path;
  100.             var closest = possibles.ClosestTo(self);
  101.             //var mi = self.Info.TraitInfo<MobileInfo>();
  102.             //using (var search = PathSearch.FromPoints(self.World, mi, self, possibles.Select(r => r.Location), self.Location, false))
  103.             //  path = self.World.WorldActor.Trait<IPathFinder>().FindPath(search);
  104.            
  105.            
  106.             //Take the closest Actor
  107.             if (closest != null)
  108.                 return closest;
  109.  
  110.             return null;
  111.         }
  112.        
  113.         public WPos Position(Actor actor)
  114.         {
  115.             return actor.CenterPosition;
  116.         }
  117.  
  118.         public override bool Produce(Actor self, ActorInfo producee, string factionVariant)
  119.         {
  120.             var owner = self.Owner;
  121.             var exit = CPos.Zero;
  122.             //var infiltrate = self.CenterPosition + exitinfo.SpawnOffset;
  123.  
  124.             owner.World.AddFrameEndTask(w =>
  125.             {
  126.             if (!self.IsInWorld || self.IsDead)
  127.                 return;
  128.  
  129.  
  130.                 var actor = PossibleActor(self);
  131.  
  132.                 if (actor == null)
  133.                     return;
  134.  
  135.                 InUse.Add(actor);
  136.                 var newexit = self.Info.TraitInfos<ExitInfo>().First();
  137.                 var move = actor.TraitOrDefault<IMove>();
  138.                 exit = self.Location + newexit.ExitCell;
  139.                 var infiltrate = self.CenterPosition + newexit.SpawnOffset;
  140.  
  141.                 if (!actor.IsInWorld || !actor.IsDead)
  142.                 {
  143.  
  144.                     actor.CancelActivity();
  145.                     actor.QueueActivity(move.MoveIntoWorld(actor, exit));
  146.                     actor.QueueActivity(new CallFunc(() =>
  147.                     {
  148.                    
  149.                         if (!self.IsInWorld || self.IsDead)
  150.                         {
  151.                             if (InUse.Contains(actor))
  152.                                 InUse.Add(actor);
  153.                         }
  154.                     }));
  155.                     actor.QueueActivity(move.VisualMove(actor, Position(actor), infiltrate));
  156.                     actor.QueueActivity(new CallFunc(() =>
  157.                     {
  158.                         if (!self.IsInWorld || self.IsDead)
  159.                         {
  160.                             if (InUse.Contains(actor))
  161.                                 InUse.Add(actor);
  162.                         }
  163.                        
  164.                         if (InUse.Contains(actor))
  165.                             InUse.Add(actor);
  166.                        
  167.                         self.World.AddFrameEndTask(ww => DoProduction(self, producee, newexit, factionVariant));
  168.                         Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", info.ReadyAudio,
  169.                             self.Owner.Faction.InternalName);
  170.                     }));
  171.                     actor.QueueActivity(new RemoveSelf());
  172.                     actor.QueueActivity(new CallFunc(() =>
  173.                     {
  174.                    
  175.                         if (!self.IsInWorld || self.IsDead)
  176.                         {
  177.                             if (InUse.Contains(actor))
  178.                                 InUse.Add(actor);
  179.                         }
  180.                     }));
  181.  
  182.                 }
  183.             });
  184.            
  185.             return true;
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement