public async static void CheckHeartbeat(INode myNode) { Node.NodeState previousState = myNode.State; myNode.State = Node.NodeState.HEARTBEAT; List <uint?> Neighbours = myNode.Router.NetworkMap.GetEntryByID(myNode.Id).Neighbours.ToList(); //Loop through all immidate neightbours foreach (uint?node in Neighbours) { Request request = new Request() { SourceID = myNode.Id, DestinationID = node, Command = Request.Commands.HEARTBEAT, AckExpected = false, ResponseExpected = true }; Response response = await myNode.CommsModule.SendAsync(node, request, Constants.COMMS_TIMEOUT, 3); if (response.ResponseCode == Response.ResponseCodes.TIMEOUT) { break; } } myNode.State = previousState; }
void CreateGrid() { grid = new Node[gridSizeX, gridSizeY]; worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.up * gridWorldSize.y / 2; var nodeStates = new Node.NodeState[] { Node.NodeState.Border, Node.NodeState.Obstacle, Node.NodeState.BlueIntrudedLeft, Node.NodeState.BlueIntrudedRight, Node.NodeState.RedIntrudedLeft, Node.NodeState.RedIntrudedRight, Node.NodeState.BlueBattlefield, Node.NodeState.RedBattlefield, Node.NodeState.Battlefield }; var layers = new int[] { 30, 29, 23, 22, 21, 20, 19, 18, 15 }; for (int x = 0; x < gridSizeX; x++) { for (int y = 0; y < gridSizeY; y++) { Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeLengthX + nodeLengthX / 2) + Vector3.up * (y * nodeLengthY + nodeLengthY / 2); var nodeState = Node.NodeState.NoState; Collider2D[] colliders = Physics2D.OverlapBoxAll(worldPoint, new Vector3(nodeLengthX, nodeLengthY), 0); if (colliders.Length != 0) { int layerMax = colliders.Max(c => c.gameObject.layer); // 8 is "Battlefield 9 is "Obstacle" 10 is "Border", finds the maximum value among these int i = Array.IndexOf(layers, layerMax); nodeState = (Node.NodeState)nodeStates.GetValue(i); } grid[x, y] = new Node(nodeState, worldPoint); } } }
private IEnumerator RunBehavior(Repeator repeat) { Node.NodeState result = repeat.Evaluate(); while (result == Node.NodeState.RUNNING) { Debug.Log(repeat.ToString() + " result: " + result); yield return(new WaitForSeconds(5f)); result = repeat.Evaluate(); } }
// Update is called once per frame void Update() { state = comms.Node.State; SatManager.MessageProps[] messageSnapshot = SatManager._instance.SentMessages.ToArray(); if (messageSnapshot.Length > 0) { foreach (SatManager.MessageProps props in messageSnapshot) { StartCoroutine(DisplayMessageSent(props.StartVect + Vector3.up, props.EndVect + Vector3.up, props.Duration, 0f, props.Color)); } } SatManager._instance.SentMessages.Clear(); reachableSats.Clear(); KnownNeighbours = comms.Node.Router.NetworkMap.GetEntryByID(comms.Node.Id)?.Neighbours; reachableSats.Clear(); foreach (SatelliteComms node in reachableSats.Where(sat => KnownNeighbours.Contains(sat.Node.Id) == false)) { reachableSats.Remove(node); } uint?toRemove = null; foreach (uint?node in KnownNeighbours) { if (reachableSats.Select(sat => sat.Node.Id).Contains(node) == false) { if (SatManager._instance.satellites.Any(sat => sat.Node.Id == node) == false) { toRemove = node; } else { Transform nodeTransform = SatManager._instance.satellites.Find(sat => sat.Node.Id == node).transform; reachableSats.Add(nodeTransform.GetComponent <SatelliteComms>()); } } } if (toRemove != null) { KnownNeighbours.Remove(toRemove); } List <uint?> reachableSatsID = new List <uint?>(); for (int i = 0; i < reachableSats?.Count; i++) { uint?id = reachableSats[i].Node.Id; reachableSatsID.Add(id); if (commLineRenderes.ContainsKey(id) == false) { GameObject commlineGO = new GameObject(); commlineGO.transform.parent = this.transform; LineRenderer lineRenderer = commlineGO.AddComponent <LineRenderer>(); lineRenderer.material = CommLineMat; lineRenderer.material.SetColor("_BaseColor", CommsMat); lineRenderer.startWidth = 0.025f; lineRenderer.endWidth = 0.025f; lineRenderer.positionCount = 2; lineRenderer.SetPosition(0, this.transform.position); lineRenderer.SetPosition(1, reachableSats[i].transform.position); commLineRenderes.Add(id, lineRenderer); } else { Transform _transform = reachableSats[i].transform; Vector3 commLineDir = new Vector3 { x = _transform.position.x - comms.Node.Position.X, y = _transform.position.y - comms.Node.Position.Y, z = _transform.position.z - comms.Node.Position.Z, }; LineRenderer lineRenderer = commLineRenderes[id]; lineRenderer.SetPosition(1, _transform.position); lineRenderer.startWidth = 0.025f; lineRenderer.endWidth = 0.025f; } } for (int i = commLineRenderes.Count - 1; i >= 0; i--) { if (comms.Node.Active == false) { Destroy(commLineRenderes.ElementAt(i).Value); commLineRenderes.Remove(commLineRenderes.ElementAt(i).Key); continue; } uint?key = commLineRenderes.ElementAt(i).Key; if (reachableSatsID.Contains(key) == false) { Destroy(commLineRenderes[key]); commLineRenderes.Remove(key); } else { commLineRenderes[key].SetPosition(0, this.transform.position); } } uint?nextSeq = comms.Node.Router.NextSequential(comms.Node, Router.CommDir.CW); if (nextSeq != null && commLineRenderes.ContainsKey(nextSeq)) { LineRenderer nextSeqCommLine = commLineRenderes[nextSeq]; nextSeqCommLine.startWidth = 0.1f; nextSeqCommLine.endWidth = 0.1f; } switch (comms.Node.State) { case Node.NodeState.PASSIVE: meshRenderer.material.SetColor("_BaseColor", PassiveMat); //targetPositionLineRenderer.material.SetColor("_BaseColor", PassiveMat); break; case Node.NodeState.PLANNING: //meshRenderer.material.SetColor("_BaseColor", LeaderMat); //targetPositionLineRenderer.material.SetColor("_BaseColor", LeaderMat); break; case Node.NodeState.EXECUTING: meshRenderer.material.SetColor("_BaseColor", ExecuteMat); //targetPositionLineRenderer.material.SetColor("_BaseColor", ExecuteMat); break; case Node.NodeState.OVERRIDE: meshRenderer.material.SetColor("_BaseColor", OverrideMat); //targetPositionLineRenderer.material.SetColor("_BaseColor", OverrideMat); break; case Node.NodeState.DEAD: meshRenderer.material.SetColor("_BaseColor", DeadMat); break; case Node.NodeState.HEARTBEAT: meshRenderer.material.SetColor("_BaseColor", HeartbeatMat); break; } lastState = comms.Node.State; // Turn active communication link on and off if (commsSim.ActiveCommSat != null && commLineRenderes.ContainsKey(commsSim.ActiveCommSat.Node.Id)) { uint?id = commsSim.ActiveCommSat.Node.Id; commLineRenderes[id].material.SetColor("_BaseColor", CommsActiveMat); lastActiveComm = id; } else if (lastActiveComm != null && commLineRenderes.ContainsKey(lastActiveComm)) { commLineRenderes[lastActiveComm].material.SetColor("_BaseColor", CommsMat); } // Remove all non-reachable links, then update position if (comms.Node.GeneratingPlan != null) { Vector3 plannedposition = transform.position; foreach (ConstellationPlanEntry e in comms.Node.GeneratingPlan.Entries) { if (e.NodeID != null && e.NodeID == comms.Node.Id) { plannedposition = BackendHelpers.UnityVectorFromNumerics(e.Position); } } targetPositionLineRenderer.positionCount = 2; targetPositionLineRenderer.SetPositions(new Vector3[] { transform.position, plannedposition }); } }
private void Awake() { _spriteRenderer = GetComponent <SpriteRenderer>(); _nodeState = GetComponent <NodeState>(); _nodeFence = GetComponent <NodeFence>(); }