コード例 #1
0
ファイル: SlurryNet.cs プロジェクト: theman83554/Pawnmorpher
        /// <summary>
        /// Registers the specified comp.
        /// </summary>
        /// <param name="comp">The comp.</param>
        /// <exception cref="ArgumentNullException">comp</exception>
        public virtual void Register([NotNull] SlurryNetComp comp)
        {
            if (comp == null)
            {
                throw new ArgumentNullException(nameof(comp));
            }
            _connectors.Add(comp);
            if (comp is ISlurryNetTrader trader)
            {
                _traders.Add(trader);
            }
            if (comp is ISlurryNetStorage storage)
            {
                _storage.Add(storage);
            }

            comp.Network = this;

            foreach (IntVec3 intVec3 in comp.parent.OccupiedRect().Cells)
            {
                _disjointSet.Set(intVec3, true);
            }


#if DEBUG
            _drawer.SetDirty();
#endif
        }
コード例 #2
0
        public static IEnumerable <SlurryNetComp> GetAdjacentSlurryComps([NotNull] this SlurryNetComp comp)
        {
            if (comp == null)
            {
                throw new ArgumentNullException(nameof(comp));
            }


            foreach (IntVec3 intVec3 in comp.parent.CellsAdjacent8WayAndInside())
            {
                if (comp.parent.Map == null)
                {
                    continue;
                }
                var l = intVec3.GetThingList(comp.parent.Map);
                if (l == null)
                {
                    continue;
                }
                foreach (Thing thing in l)
                {
                    if (thing == comp.parent)
                    {
                        continue;
                    }
                    var c = thing?.TryGetComp <SlurryNetComp>();
                    if (c == null)
                    {
                        continue;
                    }
                    yield return(c);
                }
            }
        }
コード例 #3
0
        /// <summary>
        ///     Notifies this instance that the connector was destroyed.
        /// </summary>
        /// <param name="comp">The comp.</param>
        /// <exception cref="ArgumentNullException">comp</exception>
        public void NotifyConnectorDestroyed([NotNull] SlurryNetComp comp)
        {
            if (comp == null)
            {
                throw new ArgumentNullException(nameof(comp));
            }
            if (comp.Network == null)
            {
                return;
            }

            DestroyNet(comp.Network);
            CreateSlurryNets(comp.GetAdjacentSlurryComps());
        }
コード例 #4
0
        private SlurryNet CreateSlurryNetFrom([NotNull] SlurryNetComp root, [CanBeNull] HashSet <SlurryNetComp> seen)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            seen = seen ?? new HashSet <SlurryNetComp>();
            var queue   = new Queue <SlurryNetComp>();
            var network = new HashSet <SlurryNetComp>();

            if (!root.TransmitsNow)
            {
                seen.Add(root);
                return(new SlurryNet(map, root));
            }

            // basic flood fill. Start somewhere, put all neighbours on a queue,
            // continue until there are no more neighbours.
            queue.Enqueue(root);
            seen.Add(root);
            while (queue.Count > 0)
            {
                SlurryNetComp cur = queue.Dequeue();
                // sanity check; is this already in a network?
                if (cur.Network != null)
                {
                    Log.Error($"trying to add {cur.parent.Label} to a new network while it still has a network");
                }

                network.Add(cur);

                // check neighbours, add to queue if eligible
                foreach (SlurryNetComp neighbour in cur.GetAdjacentSlurryComps())
                {
                    if (!seen.Contains(neighbour))
                    {
                        seen.Add(neighbour);
                        if (!neighbour.TransmitsNow)
                        {
                            continue;
                        }
                        queue.Enqueue(neighbour);
                    }
                }
            }

            return(new SlurryNet(map, network));
        }
コード例 #5
0
        /// <summary>
        ///     notifies this instance that a connector was added
        /// </summary>
        /// <param name="comp">The comp.</param>
        /// <exception cref="ArgumentNullException">comp</exception>
        public void NotifyConnectorAdded([NotNull] SlurryNetComp comp)
        {
            if (comp == null)
            {
                throw new ArgumentNullException(nameof(comp));
            }

            DebugLogUtils.LogMsg(LogLevel.Messages, $"adding connector for '{comp.parent.Label}'");

            if (comp.Network != null || Nets.Any(n => n.Connectors.Contains(comp)))
            {
                Log.Error($"adding slurry comp {comp.parent.Label} which is already part of a network");
                return;
            }

            List <SlurryNet> neighbors = comp.GetAdjacentSlurryComps()
                                         .Select(n => n.Network)
                                         .Where(n => n != null)
                                         .Distinct()
                                         .ToList();

            if (neighbors.Count == 1)
            {
                neighbors[0].Register(comp);
            }
            else
            {
                foreach (SlurryNet slurryNet in neighbors)
                {
                    DestroyNet(slurryNet);
                }

                SlurryNet net = CreateSlurryNetFrom(comp, null);
                _nets.Add(net);
            }
        }
コード例 #6
0
ファイル: SlurryNet.cs プロジェクト: theman83554/Pawnmorpher
 /// <summary>
 /// Initializes a new instance of the <see cref="SlurryNet"/> class.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="netComp">The net comp.</param>
 public SlurryNet([NotNull] Map map, [NotNull] SlurryNetComp netComp) : this(map, new [] { netComp })
 {
 }