示例#1
0
        public void AddNodesFoundFrom(NodePos pos, ISignalNode node)
        {
            List <NodePos> openList = new List <NodePos> {
                pos
            };
            List <NodePos> closedList = new List <NodePos>();

            while (openList.Count > 0)
            {
                NodePos     currentPos  = openList.Last();
                ISignalNode currentNode = mod.GetDeviceAt(currentPos.blockPos)?.GetNodeAt(currentPos);

                openList.RemoveAt(openList.Count - 1);
                if (currentNode != null && !nodes.ContainsKey(currentPos))
                {
                    AddNode(currentPos, currentNode);
                    currentNode.Connections.ForEach(c => {
                        if (!closedList.Contains(c.pos2))
                        {
                            openList.Add(c.pos2);
                        }
                    });
                }
                closedList.Add(currentPos);
            }
        }
示例#2
0
        public void TryToAddConnection(Connection con)
        {
            NodePos       pos1 = con.pos1;
            NodePos       pos2 = con.pos2;
            SignalNetwork net1 = GetNetworkAt(pos1, false);
            SignalNetwork net2 = GetNetworkAt(pos2, false);

            if (net1 == null && net2 == null)
            {
                return;
            }
            if (net1 == null)
            {
                ISignalNode node = GetDeviceAt(pos1.blockPos)?.GetNodeAt(pos1);
                if (node == null)
                {
                    return;
                }
                net2.AddNodesFoundFrom(pos1, node);
            }
            else if (net2 == null)
            {
                ISignalNode node = GetDeviceAt(pos2.blockPos)?.GetNodeAt(pos2);
                if (node == null)
                {
                    return;
                }
                net1.AddNodesFoundFrom(pos2, node);
            }
            AddConnection(con);
        }
示例#3
0
 private void AddConnectionToNode(ISignalNode node, Connection con)
 {
     if (node.Pos != con.pos1)
     {
         return;
     }
     node.Connections.Add(con);
 }
示例#4
0
 public void AddNode(NodePos pos, ISignalNode node)
 {
     if (nodes.ContainsKey(pos))
     {
         mod.Api.Logger.Error("Network {0} already contains a node at pos {1}", this.networkId, pos);
     }
     nodes[pos] = node;
     isValid    = false;
 }
示例#5
0
        public SignalSource(ISignalNode owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner", "SignalSource must have an owner");
            }

            this.Owner = owner;
        }
示例#6
0
        public SignalSource(ISignalNode owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner", "SignalSource must have an owner");
            }

            this.Owner = owner;
        }
示例#7
0
        public SignalSink(ISignalNode owner, int index = 0)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner", "SignalSink must have an owner");
            }

            this.index = index;
            this.Owner = owner;
        }
示例#8
0
        public SignalSink(ISignalNode owner, int index = 0)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner", "SignalSink must have an owner");
            }

            this.index = index;
            this.Owner = owner;
        }
示例#9
0
        public void OnNodeUpdate(NodePos pos)
        {
            ISignalNode node = this.GetNodeAt(pos);

            if (node == null)
            {
                return;
            }
            IBESignalReceptor receptor = this.Blockentity as IBESignalReceptor;

            receptor?.OnValueChanged(pos, node.value);

            return;
        }
示例#10
0
        /// <summary>
        /// Compute values at nodes and notify the devices
        /// </summary>
        public void Simulate()
        {
            List <ISignalNode> sources = nodes.Values.Where(v => v.isSource).ToList();

            foreach (ISignalNode source in sources)
            {
                HashSet <NodePos> openList = new HashSet <NodePos> {
                    source.Pos
                };
                HashSet <NodePos> closedList = new HashSet <NodePos>();

                mod.Api.Logger.Debug("Network {0}: Simulation from source at {1}", this.networkId, source.Pos);
                byte startValue = (byte)15;



                while (openList.Count > 0 && closedList.Count <= nodes.Count + 1)
                {
                    if (closedList.Count == nodes.Count + 1)
                    {
                        mod.Api.Logger.Error("Network simulation: closed list larger than number of nodes in net!"); break;
                    }

                    NodePos pos = openList.Last();

                    ISignalNode currentNode = nodes[pos];

                    currentNode.value = startValue;
                    ISignalNodeProvider device = mod.GetDeviceAt(pos.blockPos);
                    device.OnNodeUpdate(pos);

                    mod.Api.Logger.Debug("Network {0}: Asigning value {1} at node {2}", this.networkId, startValue, pos);

                    openList.Remove(pos);
                    currentNode.Connections.ForEach(c => {
                        NodePos otherPos = c.pos1 == pos ? c.pos2 : c.pos1;
                        if (!closedList.Contains(otherPos) && nodes.ContainsKey(otherPos))
                        {
                            openList.Add(otherPos);
                        }
                    });
                    closedList.Add(pos);
                }
            }

            isValid = true;
        }
示例#11
0
        public override void Initialize(ICoreAPI api, JsonObject properties)
        {
            //initialize the nodes
            base.Initialize(api, properties);

            NodePos pos1 = new NodePos(this.Pos, 0);
            NodePos pos2 = new NodePos(this.Pos, 1);

            ISignalNode node1 = GetNodeAt(pos1);
            ISignalNode node2 = GetNodeAt(pos2);

            if (node1 == null || node2 == null)
            {
                return;
            }

            node1.Connections.Add(new Connection(pos1, pos2));
            node2.Connections.Add(new Connection(pos2, pos1));

            //return;

            /*JsonObject[] conON = properties["connectionsON"]?.AsArray();
             * JsonObject[] conOFF = properties["connectionsOFF"]?.AsArray();
             *
             * if (conON != null)
             * {
             *  foreach (JsonObject json in conON)
             *  {
             *      int index1 = json["i1"].AsInt(-1);
             *      int index2 = json["i2"].AsInt(-1);
             *      if (!IsConnectionValid(index1, index2)) continue;
             *      BlockPos pos = this.Blockentity.Pos;
             *      NodePos pos1 = new NodePos(pos, index1);
             *      NodePos pos2 = new NodePos(pos, index2);
             *
             *      Connection con = new Connection(pos1, pos2);
             *
             *  }
             * }*/
        }
示例#12
0
 private void SetNodeNetwork(ISignalNode node, SignalNetwork net)
 {
 }