Пример #1
0
        public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel)
        {
            HangingWiresMod mod = api.ModLoader.GetModSystem <HangingWiresMod>();

            if (mod == null)
            {
                api.Logger.Error("HangingWiresMod mod system not found");
            }
            else
            {
                NodePos pos = GetNodePosForWire(world, blockSel, mod.GetPendingNode());
                if (CanAttachWire(world, pos, mod.GetPendingNode()))
                {
                    if (pos != null)
                    {
                        mod.SetPendingNode(GetNodePosForWire(world, blockSel));
                    }
                    return(true);
                }
            }



            return(base.OnBlockInteractStart(world, byPlayer, blockSel));
        }
Пример #2
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);
            }
        }
Пример #3
0
        public Node(Node copie, NodePos direction, MapData map, Vector2 size)
        {
            CanGo = true;
            Start = copie.Start;
            Arrival = copie.Arrival;

            switch (direction)
            {
                case NodePos.U:
                    Position = new Vector2(copie.Position.X, copie.Position.Y - 32);
                    break;
                case NodePos.D:
                    Position = new Vector2(copie.Position.X, copie.Position.Y + 32);
                    break;
                case NodePos.R:
                    Position = new Vector2(copie.Position.X + 32, copie.Position.Y);
                    break;
                case NodePos.L:
                    Position = new Vector2(copie.Position.X - 32, copie.Position.Y);
                    break;
            }

            Update(map);
            if (Id < 0 || Id > map.MapWidth * map.MapHeight || !Can(direction, map, size))
                CanGo = false;
        }
Пример #4
0
        public Node(Node copie, NodePos direction, MapData map, Vector2 Size)
        {
            Start   = copie.Start;
            Arrival = copie.Arrival;

            switch (direction)
            {
            case NodePos.U:
                Position = new Vector2(copie.Position.X, copie.Position.Y - 32);
                break;

            case NodePos.D:
                Position = new Vector2(copie.Position.X, copie.Position.Y + 32);
                break;

            case NodePos.R:
                Position = new Vector2(copie.Position.X + 32, copie.Position.Y);
                break;

            case NodePos.L:
                Position = new Vector2(copie.Position.X - 32, copie.Position.Y);
                break;
            }
            Update(map);
            if (Id < 0 || Id + (int)(Size.Y / 32) * map.MapWidth + (int)(Size.X / 32) >= map.Accessibility.Length || map.Accessibility[Id] == 1)
            {
                Id = -1;
            }
        }
Пример #5
0
        public Vec3f GetAnchorPosInBlock(IWorldAccessor world, NodePos pos)
        {
            BlockEntity entity = world?.BlockAccessor?.GetBlockEntity(pos.blockPos);
            Vec3f       posOut = null;// entity?.GetBehavior<BEBehaviorCircuitHolder>()?.GetNodePosInBlock(pos);

            return(posOut != null ? posOut : new Vec3f(0, 0, 0));
        }
Пример #6
0
        public Node(Node copie, NodePos direction, MapData map, Vector2 size)
        {
            CanGo   = true;
            Start   = copie.Start;
            Arrival = copie.Arrival;

            switch (direction)
            {
            case NodePos.U:
                Position = new Vector2(copie.Position.X, copie.Position.Y - 32);
                break;

            case NodePos.D:
                Position = new Vector2(copie.Position.X, copie.Position.Y + 32);
                break;

            case NodePos.R:
                Position = new Vector2(copie.Position.X + 32, copie.Position.Y);
                break;

            case NodePos.L:
                Position = new Vector2(copie.Position.X - 32, copie.Position.Y);
                break;
            }

            Update(map);
            if (Id < 0 || Id > map.MapWidth * map.MapHeight || !Can(direction, map, size))
            {
                CanGo = false;
            }
        }
Пример #7
0
        public void AddConnection(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;
            }
            SignalNetwork mergedNetwork;

            if (net1 != net2)
            {
                mergedNetwork = net1.Merge(net2);
                Api.Logger.Debug("Merging signal networks {0} and {1}, into net {2}", net1.networkId, net2.networkId, mergedNetwork.networkId);
                foreach (ISignalNode node in mergedNetwork.nodes.Values)
                {
                    SetNodeNetwork(node, mergedNetwork);
                }
            }
            else
            {
                mergedNetwork = net1;
            }

            Api.Logger.Debug("Adding connection into net {0}", mergedNetwork.networkId);
            mergedNetwork.AddConnection(con);

            //Notify handler and sync ect...
        }
Пример #8
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);
        }
Пример #9
0
        public ISignalNode GetNodeAt(NodePos pos)
        {
            ISignalNode node;

            nodes.TryGetValue(pos, out node);
            return(node);
        }
Пример #10
0
        public Vec3f GetNodePosinBlock(NodePos pos)
        {
            Vec3f       vec   = Circuit.getNodePosinBlock(pos);
            BlockFacing dummy = null;

            RotateFromCircuittoBE(ref vec, ref dummy, new Vec3f(0.5f, 0.5f, 0.5f));
            return(vec);
        }
Пример #11
0
 public bool CanAttachWire(IWorldAccessor world, NodePos pos, NodePos posInit = null)
 {
     if (posInit != null && posInit == pos)
     {
         return(false);
     }
     return(pos != null);
 }
Пример #12
0
 public NodePos[] GetWireAnchors(IWorldAccessor world, BlockPos pos)
 {
     NodePos[] nodes = new NodePos[wireAnchors.Length];
     for (int i = 0; i < wireAnchors.Length; i++)
     {
         nodes[i] = new NodePos(pos, wireAnchors[i].index);
     }
     return(nodes);
 }
Пример #13
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;
 }
Пример #14
0
 public Vec3f GetAnchorPosInBlock(IWorldAccessor world, NodePos pos)
 {
     foreach (WireAnchor box in wireAnchors)
     {
         if (box.index == pos.index)
         {
             return(new Vec3f((box.x1 + box.x2) / 2, (box.y1 + box.y2) / 2, (box.z1 + box.z2) / 2));
         }
     }
     return(new Vec3f(0f, 0f, 0f));
 }
Пример #15
0
 /// <summary>
 /// remove a node within the network
 /// </summary>
 public void RemoveNode(NodePos pos)
 {
     if (!nodes.ContainsKey(pos))
     {
         mod.Api.Logger.Error("removing node in network, no node to remove at {0}", pos); return;
     }
     if (!nodes.Remove(pos))
     {
         mod.Api.Logger.Error("removing node in network, failed to remove at {0}", pos); return;
     }
     isValid = false;
 }
Пример #16
0
        public Vec3f getNodePosinBlock(NodePos pos)
        {
            BlockPos blockPos = myBE.Pos;

            if (blockPos == null || blockPos != pos.blockPos)
            {
                return(null);
            }

            Vec3i voxelpos = getVectorFromIndex((ushort)pos.index);

            return(new Vec3f((voxelpos.X + 0.5f) * 1f / SIZEX, (voxelpos.Y + 0.5f) * 1f / SIZEY, (voxelpos.Z + 0.5f) * 1f / SIZEZ));
        }
Пример #17
0
        private bool Can(NodePos direction, MapData map, Vector2 size)
        {
            bool isNearNode = ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y)) < map.MapHeight * map.MapWidth;

            switch (direction)
            {
            case NodePos.U:
                if (map.Accessibility[Id] == 1 ||
                    (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] == 1) ||
                    (map.SideAccess[Id] & 2) == 2 ||
                    (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] & 2) == 2))
                {
                    return(false);
                }
                break;

            case NodePos.D:
                if (map.Accessibility[Id] == 1 ||
                    (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] == 1) ||
                    (map.SideAccess[Id] & 1) == 1 ||
                    (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] & 1) == 1))
                {
                    return(false);
                }
                break;

            case NodePos.L:
                if (map.Accessibility[Id] == 1 ||
                    (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] == 1) ||
                    (map.SideAccess[Id] & 8) == 8 ||
                    (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] & 8) == 8))
                {
                    return(false);
                }
                break;

            case NodePos.R:
                if (map.Accessibility[Id] == 1 ||
                    (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] == 1) ||
                    (map.SideAccess[Id] & 4) == 4 ||
                    (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] & 4) == 4))
                {
                    return(false);
                }
                break;
            }
            return(true);
        }
Пример #18
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;
        }
Пример #19
0
        internal List <WireConnection> GetWireConnectionsFrom(NodePos pos)
        {
            List <WireConnection> toProcess = data.connections.Where(c => c.pos1 == pos || c.pos2 == pos).ToList();
            List <WireConnection> output    = new List <WireConnection>();

            foreach (WireConnection con in toProcess)
            {
                if (con.pos1 == pos)
                {
                    output.Add(new WireConnection(con.pos1, con.pos2));
                }
                else
                {
                    output.Add(new WireConnection(con.pos2, con.pos1));
                }
            }
            return(output);
        }
Пример #20
0
        private SignalNetwork GetNetworkAt(NodePos pos, bool createIfnull)
        {
            foreach (SignalNetwork net in data.networksById.Values)
            {
                if (net.nodes.ContainsKey(pos))
                {
                    return(net);
                }
            }

            if (!createIfnull)
            {
                return(null);
            }

            SignalNetwork newNet = CreateNetwork();

            return(newNet);
        }
Пример #21
0
        //Detects when the player interacts with right click, usually to place a component
        public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel)
        {
            HangingWiresMod mod = api.ModLoader.GetModSystem <HangingWiresMod>();

            if (mod != null && api.Side == EnumAppSide.Client)
            {
                NodePos pos = GetNodePosForWire(world, blockSel);
                if (pos != null)
                {
                    mod.SetPendingNode(GetNodePosForWire(world, blockSel));
                }
            }

            base.OnBlockInteractStart(world, byPlayer, blockSel);
            if (api.Side == EnumAppSide.Client)
            {
                BlockEntity entity = world.BlockAccessor.GetBlockEntity(blockSel.Position);
                entity?.GetBehavior <BEBehaviorCircuitHolder>()?.OnUseOver(byPlayer, blockSel, false);
            }

            return(true);
        }
Пример #22
0
        //devices that have been initialized, can be after block placement, or chunk loading
        public void LoadDevices()
        {
            if (devicesToLoad.Count == 0)
            {
                return;
            }
            foreach (ISignalNodeProvider device in devicesToLoad)
            {
                Api.Logger.Debug("Loading device at pos");


                //If the device is not in proxies, create a network if the device contains a source node
                foreach (ISignalNode node in device.GetNodes().Values)
                {
                    //ISignalNode node = kv.Value;
                    NodePos pos = node.Pos;
                    if (pos == null)
                    {
                        continue;
                    }
                    Api.Logger.Debug("node found, at {0}, looking at connections", pos);
                    List <Connection> connections = node.Connections;
                    SignalNetwork     net;
                    if (node.isSource)
                    {
                        Api.Logger.Debug("node is source, creating a network and lauching network discovery");
                        net = GetNetworkAt(pos, true);
                        net.AddNodesFoundFrom(node.Pos, node);
                        continue;
                    }

                    foreach (Connection con in connections)
                    {
                        TryToAddConnection(con);
                    }
                }
            }
            devicesToLoad.Clear();
        }
Пример #23
0
        public void SetPendingNode(NodePos pos)
        {
            if (api.Side == EnumAppSide.Server)
            {
                return;
            }

            if (pendingNode == null)
            {
                pendingNode = pos;
                capi?.ShowChatMessage(String.Format("Pending {0}:{1}", pos.blockPos, pos.index));
            }
            else
            {
                capi?.ShowChatMessage(String.Format("trying to attach {0}:{1}", pos.blockPos, pos.index));
                WireConnection connection = new WireConnection(pendingNode, pos);
                clientChannel.SendPacket(new AddConnectionPacket()
                {
                    connection = connection
                });
                pendingNode = null;
            }
        }
Пример #24
0
        public Node(Node copie, NodePos direction, MapData map, Vector2 Size)
        {
            Start = copie.Start;
            Arrival = copie.Arrival;

            switch (direction)
            {
                case NodePos.U:
                    Position = new Vector2(copie.Position.X, copie.Position.Y - 32);
                    break;
                case NodePos.D:
                    Position = new Vector2(copie.Position.X, copie.Position.Y + 32);
                    break;
                case NodePos.R:
                    Position = new Vector2(copie.Position.X + 32, copie.Position.Y);
                    break;
                case NodePos.L:
                    Position = new Vector2(copie.Position.X - 32, copie.Position.Y);
                    break;
            }
            Update(map);
            if (Id < 0 || Id + (int)(Size.Y / 32) * map.MapWidth + (int)(Size.X / 32) >= map.Accessibility.Length || map.Accessibility[Id] == 1)
                Id = -1;
        }
Пример #25
0
 private bool Can(NodePos direction, MapData map, Vector2 size)
 {
     bool isNearNode = ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y)) < map.MapHeight * map.MapWidth;
     switch (direction)
     {
         case NodePos.U:
             if (map.Accessibility[Id] == 1 ||
                 (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] == 1) ||
                 (map.SideAccess[Id] & 2) == 2 ||
                 (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] & 2) == 2))
                 return false;
             break;
         case NodePos.D:
             if (map.Accessibility[Id] == 1 ||
                 (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] == 1) ||
                 (map.SideAccess[Id] & 1) == 1 ||
                 (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X + size.X, Position.Y))] & 1) == 1))
                 return false;
             break;
         case NodePos.L:
             if (map.Accessibility[Id] == 1 ||
                 (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] == 1) ||
                 (map.SideAccess[Id] & 8) == 8 ||
                 (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] & 8) == 8))
                 return false;
             break;
         case NodePos.R:
             if (map.Accessibility[Id] == 1 ||
                 (isNearNode && map.Accessibility[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] == 1) ||
                 (map.SideAccess[Id] & 4) == 4 ||
                 (isNearNode && (map.SideAccess[ConversionManager.VectToId(map, new Vector2(Position.X, Position.Y + size.Y))] & 4) == 4))
                 return false;
             break;
     }
     return true;
 }
Пример #26
0
        /// <summary>
        /// Draws the node frame and calls NodeGUI. Can be overridden to customize drawing.
        /// </summary>
        protected internal virtual void DrawNode()
        {
#if UNITY_EDITOR
            // Create a rect that is adjusted to the editor zoom and pixel perfect
            Rect    nodeRect = rect;
            Vector2 pos      = NodeEditor.curEditorState.zoomPanAdjust + NodeEditor.curEditorState.panOffset;
            nodeRect.position = new Vector2((int)(nodeRect.x + pos.x), (int)(nodeRect.y + pos.y));
            contentOffset     = new Vector2(0, 20);

            GUI.color = backgroundColor;

            // Create a headerRect out of the previous rect and draw it, marking the selected node as such by making the header bold
            Rect headerRect = new Rect(nodeRect.x, nodeRect.y, nodeRect.width, contentOffset.y);
            GUI.color = backgroundColor;
            GUI.Box(headerRect, GUIContent.none, GUI.skin.box);
            GUI.color = Color.white;
            GUI.Label(headerRect, Title, NodeEditor.curEditorState.selectedNode == this? NodeEditorGUI.nodeLabelBoldCentered : NodeEditorGUI.nodeLabelCentered);

            // Begin the body frame around the NodeGUI
            Rect bodyRect = new Rect(nodeRect.x, nodeRect.y + contentOffset.y, nodeRect.width, nodeRect.height - contentOffset.y);
            GUI.color = backgroundColor;
            GUI.BeginGroup(bodyRect, GUI.skin.box);
            GUI.color         = Color.white;
            bodyRect.position = Vector2.zero;
            GUILayout.BeginArea(bodyRect);
            // Call NodeGUI
            GUI.changed = false;
            bool hasFront = (GetFront() != null);
            bool hasNext  = (GetNext() != null);
            if ((!hasFront) && hasNext)
            {
                bool isFirst = GUILayout.Toggle(nodePos == NodePos.First, "Is First");
                if (isFirst)
                {
                    nodePos = NodePos.First;
                }
                else
                {
                    nodePos = NodePos.Mid;
                }
            }
            else if (hasFront && !hasNext)
            {
                bool isLast = GUILayout.Toggle(nodePos == NodePos.Last, "Is Last");
                if (isLast)
                {
                    nodePos = NodePos.Last;
                }
                else
                {
                    nodePos = NodePos.Mid;
                }
            }
            else
            {
                nodePos = NodePos.Mid;
            }

            if (GUILayout.Button("Clear All Connect"))
            {
                DeleteAllPorts();
            }

            GUILayout.BeginHorizontal();
            GUILayout.Label("姓名");
            enname = EditorGUILayout.TextField(enname);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical();
            GUILayout.Label("执行条件");
            cond = (Cond)EditorGUILayout.EnumPopup(cond);
            GUILayout.EndVertical();
            if (cond == Cond.ControlByVar)
            {
                GUILayout.BeginVertical();
                GUILayout.Label("变量名称");
                condName = EditorGUILayout.TextField(condName);
                GUILayout.EndVertical();
            }
            if (cond != Cond.Instance)
            {
                GUILayout.BeginVertical();
                GUILayout.Label("参数");
                condParam = EditorGUILayout.IntField(condParam);
                GUILayout.EndVertical();
            }
            GUILayout.EndHorizontal();

            NodeGUI();

            if (Event.current.type == EventType.Repaint)
            {
                nodeGUIHeight = GUILayoutUtility.GetLastRect().max + contentOffset;
            }

            // End NodeGUI frame
            GUILayout.EndArea();
            GUI.EndGroup();

            // Automatically node if desired
            AutoLayoutNode();
#endif
        }
Пример #27
0
 public WireConnection(NodePos pos1, NodePos pos2) : base(pos1, pos2)
 {
 }
Пример #28
0
        public NodePos GetNodePosForWire(IWorldAccessor world, BlockSelection blockSel, NodePos posInit = null)
        {
            BlockEntity entity = world?.BlockAccessor?.GetBlockEntity(blockSel.Position);

            return(null);// entity?.GetNodePos(blockSel);
        }
Пример #29
0
 public NodePos GetNodePosForWire(IWorldAccessor world, BlockSelection blockSel, NodePos posInit = null)
 {
     foreach (WireAnchor box in wireAnchors)
     {
         if (box.index == blockSel.SelectionBoxIndex)
         {
             return(new NodePos(blockSel.Position, blockSel.SelectionBoxIndex));
         }
     }
     return(null);
 }
Пример #30
0
 public override ISignalNode GetNodeAt(NodePos pos)
 {
     return(base.GetNodeAt(pos));
 }
Пример #31
0
 public override Vec3f GetNodePosinBlock(NodePos pos)
 {
     return(null);
 }
Пример #32
0
 public ISignalNode GetNodeAt(NodePos pos)
 {
     throw new NotImplementedException();
 }