private CircuitNode OrderDfsRVisit0(CircuitNode n, Dictionary <CircuitNode, DfsMark> marks, List <CircuitNode> L, Stack <CircuitNode> path, HashSet <Connection> removedEdges) { path.Push(n); CircuitNode result = OrderDfsRVisit1(n, marks, L, path, removedEdges); CircuitNode popped = path.Pop(); DebugUtils.Assert(ReferenceEquals(popped, n)); return(result); }
public IConvertible GetValue() { if (Node.Manager.UseIntValuesOnly) { return(CircuitNode.ValueToInt(GetInternalValue())); } else { return(GetInternalValue()); } }
public void RemoveNode(CircuitNode node) { DebugUtils.Assert(object.ReferenceEquals(nodes[node.RingEvaluationPriority], node)); nodes.RemoveAt(node.RingEvaluationPriority); for (int i = node.RingEvaluationPriority, s = nodes.Count; i < s; i++) { DebugUtils.Assert(nodes[i].RingEvaluationPriority == i + 1); nodes[i].RingEvaluationPriority = i; } node.EvaluationRequired -= Node_EvaluationRequired; node.RingEvaluationPriority = -1; InvalidateOrder(); }
public void AddNode(CircuitNode node) { DebugUtils.Assert(!nodes.Contains(node)); if (nodes.Contains(node)) { return; } node.RingEvaluationPriority = nodes.Count; nodes.Add(node); node.ConnectionChanged += Node_ConnectionChanged; node.EvaluationRequired += Node_EvaluationRequired; InvalidateOrder(); }
internal void UpdateNodePriority(CircuitNode node, int newPriority) { newPriority = Math.Min(Math.Max(newPriority, 0), nodes.Count - 1); int oldPriority = node.RingEvaluationPriority; DebugUtils.Assert(object.Equals(nodes[oldPriority], node)); if (newPriority == oldPriority) { return; } nodes.RemoveAt(oldPriority); nodes.Insert(newPriority, node); for (int i = Math.Min(oldPriority, newPriority), max = Math.Max(oldPriority, newPriority); i <= max; i++) { nodes[i].RingEvaluationPriority = i; } DebugUtils.Assert(object.Equals(nodes[newPriority], node)); DebugUtils.Assert(node.RingEvaluationPriority == newPriority); InvalidateOrder(); }
public void Fill() { List <CircuitNode> nodes = RRCSManager.Instance.circuitManager.Nodes; graph = new StorageNode[nodes.Count]; for (int i = 0; i < graph.Length; i++) { CircuitNode node = nodes[i]; DebugUtils.Assert(node.RingEvaluationPriority == i); StorageNode storageNode = new StorageNode(); NodeUi ui = UiManager.GetUi(node); storageNode.uiType = ui.GetType().FullName; storageNode.typeParams = ui.GetParams(); storageNode.position = ui.transform.position; storageNode.settings = new NodeSettingContainer[node.settings.Length]; for (int j = 0; j < storageNode.settings.Length; j++) { NodeSetting setting = node.settings[j]; storageNode.settings[j] = new NodeSettingContainer { type = setting.type, value = setting.currentValue.ToString() }; } graph[i] = storageNode; } for (int nodeIndex = 0; nodeIndex < graph.Length; nodeIndex++) { CircuitNode node = nodes[nodeIndex]; StorageNode storageNode = graph[nodeIndex]; storageNode.connections = new NodeConnection[node.inputPorts.Length]; for (int portIndex = 0; portIndex < node.inputPorts.Length; portIndex++) { InputPort port = node.inputPorts[portIndex]; if (port.IsConnected) { NodeConnection connection = new NodeConnection(); OutputPort connectedPort = ((DataConnection)port.connections[0]).SourceDataPort; connection.nodeIndex = connectedPort.RingEvaluationPriority; connection.portIndex = Array.IndexOf(connectedPort.Node.outputPorts, connectedPort); storageNode.connections[portIndex] = connection; } } } for (int nodeIndex = 0; nodeIndex < graph.Length; nodeIndex++) { CircuitNode node = nodes[nodeIndex]; StorageNode storageNode = graph[nodeIndex]; if (node.statePort != null) { List <StateMachineTransition> outgoingTransitions = node.statePort.GetOutgoingTransitions().ToList(); storageNode.transitions = new NodeTransition[outgoingTransitions.Count]; for (int transitionIndex = 0; transitionIndex < storageNode.transitions.Length; transitionIndex++) { StateMachineTransition outgoingTransition = outgoingTransitions[transitionIndex]; NodeTransition transition = new NodeTransition { sourceNodeIndex = outgoingTransition.SourceStatePort.RingEvaluationPriority, targetNodeIndex = outgoingTransition.TargetStatePort.RingEvaluationPriority }; if (outgoingTransition.TransitionEnabledPort != null && outgoingTransition.TransitionEnabledPort.IsConnected) { OutputPort connectedPort = ((DataConnection)outgoingTransition.TransitionEnabledPort.connections[0]).SourceDataPort; NodeConnection connection = new NodeConnection { nodeIndex = connectedPort.RingEvaluationPriority, portIndex = Array.IndexOf(connectedPort.Node.outputPorts, connectedPort) }; transition.transitionEnabledConnection = connection; } storageNode.transitions[transitionIndex] = transition; } } else { storageNode.transitions = new NodeTransition[0]; } } }
public IEnumerator Restore(RRCSManager manager) { Dictionary <string, GameObject> typeMap = new Dictionary <string, GameObject>(); typeMap.Add(typeof(RRButtonUi).FullName, manager.NodeUiPrefabRoot.GetComponentInChildren <RRButtonUi>(true).gameObject); typeMap.Add(typeof(StageLightUi).FullName, manager.NodeUiPrefabRoot.GetComponentInChildren <StageLightUi>(true).gameObject); typeMap.Add(typeof(SignUi).FullName, manager.NodeUiPrefabRoot.GetComponentInChildren <SignUi>(true).gameObject); var chipPrefabs = manager.NodeUiPrefabRoot.GetComponentsInChildren <ChipUi>(true); foreach (var prefab in chipPrefabs) { typeMap.Add(Enum.GetName(typeof(ChipUi.ChipType), prefab.type), prefab.gameObject); } var gizmoPrefabs = manager.NodeUiPrefabRoot.GetComponentsInChildren <GizmoUi>(true); foreach (var prefab in gizmoPrefabs) { typeMap.Add(Enum.GetName(typeof(GizmoUi.GizmoType), prefab.type), prefab.gameObject); } Dictionary <NodeSetting.SettingType, int> settingsTypeIndex = new Dictionary <NodeSetting.SettingType, int>(); foreach (StorageNode storageNode in graph) { settingsTypeIndex.Clear(); var typeKey = storageNode.uiType; if (typeKey == typeof(ChipUi).FullName) { typeKey = storageNode.typeParams; } if (typeKey == typeof(GizmoUi).FullName) { typeKey = storageNode.typeParams; } if (!typeMap.ContainsKey(typeKey)) { throw new InvalidOperationException("Invalid node type " + typeKey + "."); } GameObject go = GameObject.Instantiate(typeMap[typeKey], manager.WorldCanvas.transform); go.transform.SetAsFirstSibling(); NodeUi ui = go.GetComponent <NodeUi>(); ui.ParseParams(storageNode.typeParams); go.transform.position = storageNode.position; CircuitNode node = ui.Node; foreach (NodeSettingContainer settingContainer in storageNode.settings) { int offset; settingsTypeIndex.TryGetValue(settingContainer.type, out offset); int index = Array.FindIndex(node.settings, offset, s => s.type == settingContainer.type); if (index < 0) { Debug.Log("Stored setting " + settingContainer.type + " for type " + typeKey + " not found with offset " + offset + "."); continue; } NodeSetting setting = node.settings[index]; DebugUtils.Assert(settingContainer.type == setting.type); setting.ParseValue(settingContainer.value); settingsTypeIndex[settingContainer.type] = index + 1; } } DebugUtils.Assert(manager.circuitManager.Nodes.Count == graph.Length); yield return(0); List <CircuitNode> nodes = manager.circuitManager.Nodes; for (int nodeIndex = 0; nodeIndex < graph.Length; nodeIndex++) { CircuitNode node = nodes[nodeIndex]; StorageNode storageNode = graph[nodeIndex]; for (int portIndex = 0; portIndex < storageNode.connections.Length; portIndex++) { NodeConnection connection = storageNode.connections[portIndex]; if (connection.nodeIndex >= 0 && connection.portIndex >= 0) { InputPort port = node.inputPorts[portIndex]; OutputPort connectedPort = nodes[connection.nodeIndex].outputPorts[connection.portIndex]; port.Connect(connectedPort); } } if (storageNode.transitions != null) { foreach (var transition in storageNode.transitions) { var sourceStatePort = nodes[transition.sourceNodeIndex].statePort; sourceStatePort.Connect(nodes[transition.targetNodeIndex].statePort); if (transition.transitionEnabledConnection != null && transition.transitionEnabledConnection.nodeIndex >= 0 && transition.transitionEnabledConnection.portIndex >= 0) { StateMachineTransition newTransition = (StateMachineTransition)sourceStatePort.connections[ sourceStatePort.connections.Count - 1]; var connectedNode = nodes[transition.transitionEnabledConnection.nodeIndex]; var connectedPort = connectedNode.outputPorts[transition.transitionEnabledConnection.portIndex]; newTransition.TransitionEnabledPort.Connect(connectedPort); } } } } }
private void Node_RingEvaluationPriorityChanged(CircuitNode source) { effectiveEvaluationIndexText.text = EffectiveEvaluationIndexString(source.RingEvaluationPriority); }
public DataPort(CircuitNode node, bool isInput, bool isReset) : base(node, isInput ? (isReset ? PortType.DataResetInputPort : PortType.DataInput) : (isReset ? PortType.DataResetOutputPort : PortType.DataOutput)) { }
public static NodeUi GetUi(CircuitNode node) { return(nodes[node]); }
private CircuitNode OrderDfsRVisit1(CircuitNode n, Dictionary <CircuitNode, DfsMark> marks, List <CircuitNode> L, Stack <CircuitNode> path, HashSet <Connection> removedEdges) { switch (marks[n]) { case DfsMark.Unmarked: break; case DfsMark.Permanent: return(null); case DfsMark.Temmporary: throw new InvalidOperationException(); } marks[n] = DfsMark.Temmporary; while (true) { CircuitNode backtrackTo = null; foreach (Connection e in n.IncomingConnections().Where(a => !removedEdges.Contains(a)).OrderBy((Connection c) => c.SourcePort.Node)) { DebugUtils.Assert(ReferenceEquals(e.TargetPort.Node, n)); CircuitNode m = e.SourcePort.Node; if (marks[m] == DfsMark.Temmporary) { SortedSet <CircuitNode> loop = new SortedSet <CircuitNode>(); foreach (CircuitNode cn in path) { loop.Add(cn); if (ReferenceEquals(cn, m)) { break; } } DebugUtils.Assert(loop.Count > 1); foreach (CircuitNode cn in loop) { foreach (Connection c in cn.IncomingConnections().Where(a => !removedEdges.Contains(a)).OrderBy(a => a, edgeComparer)) { DebugUtils.Assert(ReferenceEquals(c.TargetPort.Node, cn)); CircuitNode src = c.SourcePort.Node; if (!loop.Contains(src)) { continue; } removedEdges.Add(c); backtrackTo = cn; goto afterLoop; } } throw new InvalidOperationException(); } backtrackTo = OrderDfsRVisit0(m, marks, L, path, removedEdges); if (backtrackTo != null) { break; } } afterLoop: if (backtrackTo == null) { break; } else if (!ReferenceEquals(backtrackTo, n)) { marks[n] = DfsMark.Unmarked; return(backtrackTo); } } if (marks[n] == DfsMark.Temmporary) { marks[n] = DfsMark.Permanent; L.Add(n); } else { throw new InvalidOperationException(); } return(null); }
private void Node_EvaluationRequired(CircuitNode source) { Invalidate(); }
private void Node_ConnectionChanged(CircuitNode source) { InvalidateOrder(); }
protected Port(CircuitNode node, PortType portType) { this.Node = node; this.portType = portType; }
public InputPort(CircuitNode node, bool isReset) : base(node, true, isReset) { stateChip = node as StateChip; }
public OutputPort(CircuitNode node, bool isReset) : base(node, false, isReset) { }
public StatePort(CircuitNode node, bool isRoot) : base(node, isRoot ? PortType.StateRoot : PortType.StatePort) { Connected += StatePort_Connected; Disconnected += StatePort_Disconnected; }