public void TrySetIsWall(bool _isWall) { if (IsWall == _isWall) { return; } IsWall = _isWall; if (_isWall) { NeighborFinder.TryCacheNeighbor(GridPos, Direction.All); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.TL]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.T]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.TR]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.R]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.BR]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.B]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.BL]); LampManager.GetInstance().SetNodeLightingDirty(NeighborFinder.CachedNeighbors[Direction.L]); } else { LampManager.GetInstance().SetNodeLightingDirty(this); } RoomManager.GetInstance().ScheduleUpdateForRoomOfNode(GridPos); ScheduleUpdateGraphicsForSurroundingTiles(); }
public override bool CanUseOnNode(Node _node) { if (!_node.IsWall) { return(false); } NeighborFinder.TryCacheNeighbor(_node.GridPos, Direction.T); NeighborFinder.TryCacheNeighbor(_node.GridPos, Direction.B); Node _nodeT = NeighborFinder.CachedNeighbors[Direction.T]; Node _nodeB = NeighborFinder.CachedNeighbors[Direction.B]; if (_nodeT != null && _nodeB != null && _nodeT.IsWall && _nodeB.IsWall && _nodeT.AttachedInteractiveObject == null && _nodeB.AttachedInteractiveObject == null) { currentRotation = Rotation.Left; return(true); } NeighborFinder.TryCacheNeighbor(_node.GridPos, Direction.L); NeighborFinder.TryCacheNeighbor(_node.GridPos, Direction.R); Node _nodeL = NeighborFinder.CachedNeighbors[Direction.L]; Node _nodeR = NeighborFinder.CachedNeighbors[Direction.R]; if (_nodeL != null && _nodeR != null && _nodeL.IsWall && _nodeR.IsWall && _nodeL.AttachedInteractiveObject == null && _nodeR.AttachedInteractiveObject == null) { currentRotation = Rotation.Down; return(true); } return(false); }
public Int2 GetInteractiveAsset(Int2 _tileGridPos, Sorting _sorting) { Node _nodeTL, _nodeTR, _nodeBR, _nodeBL; NeighborFinder.GetSurroundingNodes(_tileGridPos, out _nodeTL, out _nodeTR, out _nodeBR, out _nodeBL); bool _hasNodeTL = _nodeTL != null; bool _hasNodeTR = _nodeTR != null; bool _hasNodeBR = _nodeBR != null; bool _hasNodeBL = _nodeBL != null; Node.InteractiveObject _interactiveObjectTL = _hasNodeTL ? _nodeTL.AttachedInteractiveObject : null; Node.InteractiveObject _interactiveObjectTR = _hasNodeTR ? _nodeTR.AttachedInteractiveObject : null; Node.InteractiveObject _interactiveObjectBR = _hasNodeBR ? _nodeBR.AttachedInteractiveObject : null; Node.InteractiveObject _interactiveObjectBL = _hasNodeBL ? _nodeBL.AttachedInteractiveObject : null; Node.InteractiveObject _interactiveObjectTemporaryTL = _hasNodeTL ? _nodeTL.AttachedInteractiveObjectTemporary : null; Node.InteractiveObject _interactiveObjectTemporaryTR = _hasNodeTR ? _nodeTR.AttachedInteractiveObjectTemporary : null; Node.InteractiveObject _interactiveObjectTemporaryBR = _hasNodeBR ? _nodeBR.AttachedInteractiveObjectTemporary : null; Node.InteractiveObject _interactiveObjectTemporaryBL = _hasNodeBL ? _nodeBL.AttachedInteractiveObjectTemporary : null; bool _useInteractiveObjectTemporaryTL = _hasNodeTL && _nodeTL.UseAttachedInteractiveObjectTemporary; bool _useInteractiveObjectTemporaryTR = _hasNodeTR && _nodeTR.UseAttachedInteractiveObjectTemporary; bool _useInteractiveObjectTemporaryBR = _hasNodeBR && _nodeBR.UseAttachedInteractiveObjectTemporary; bool _useInteractiveObjectTemporaryBL = _hasNodeBL && _nodeBL.UseAttachedInteractiveObjectTemporary; Node.InteractiveObject _foundInteractive = null; Direction _foundInteractiveDirection = Direction.None; Node _foundInteractivesNode = null; if (TryGetInteractiveObject(_nodeTL, out _foundInteractive)) { _foundInteractiveDirection = Direction.TL; _foundInteractivesNode = _nodeTL; } else if (TryGetInteractiveObject(_nodeTR, out _foundInteractive)) { _foundInteractiveDirection = Direction.TR; _foundInteractivesNode = _nodeTR; } else if (TryGetInteractiveObject(_nodeBR, out _foundInteractive)) { _foundInteractiveDirection = Direction.BR; _foundInteractivesNode = _nodeBR; } else if (TryGetInteractiveObject(_nodeBL, out _foundInteractive)) { _foundInteractiveDirection = Direction.BL; _foundInteractivesNode = _nodeBL; } if (_foundInteractive != null) { return(_foundInteractive.GetTileAssetPos(_sorting, _foundInteractiveDirection)); } return(DefaultAssets.Empty); }
public void SetColor(byte[] _colorChannelIndices, bool _isPermanent) { Int2 _tileGridPosTL, _tileGridPosTR, _tileGridPosBR, _tileGridPosBL; NeighborFinder.GetSurroundingTiles(GridPos, out _tileGridPosTL, out _tileGridPosTR, out _tileGridPosBR, out _tileGridPosBL); if (_tileGridPosTR != Int2.MinusOne) { GameGrid.GetInstance().SetColor(_tileGridPosTR, _colorChannelIndices, _isPermanent); } }
public override void Tick(out State _state) { if (openSet == null || openSet.Count == 0) { _state = State.Abort; return; } PathNode _currentPathNode = openSet.RemoveFirst(); closedSet.Add(_currentPathNode); if (_currentPathNode.GridPos != targetNodeGridPos) { Node[] _neighbors; NeighborFinder.GetSurroundingNodes(_currentPathNode.GridPos, out _neighbors); for (int i = 0; i < _neighbors.Length; i++) { Node _neighbor = _neighbors[i]; if (_neighbor == null) { continue; } PathNode _neighborPathNode = nodeGrid[_neighbor.GridPos.x, _neighbor.GridPos.y]; if (ShouldConsiderUsingNeighbor(_neighborPathNode, _currentPathNode)) { Debug.DrawLine(_currentPathNode.WorldPos, _neighborPathNode.WorldPos, Color.magenta, 1.0f); _neighborPathNode.GCost = GetMovementCost(_currentPathNode, _neighborPathNode); _neighborPathNode.HCost = GetDistance(_neighborPathNode.GridPos, targetNodeGridPos); _neighborPathNode.Parent = _currentPathNode; if (!openSet.Contains(_neighborPathNode)) { openSet.Add(_neighborPathNode); } else { openSet.UpdateItem(_neighborPathNode); } } } _state = State.Tick; } else { RetracePath(out path, out pathFull, out pathLength); _state = State.Done; } }
public void SetLightingBasedOnNeighbors() // TODO: this causes jagged lighting along walls - can definitely be fixed though! { Node _nodeTL, _nodeT, _nodeTR, _nodeR, _nodeBR, _nodeB, _nodeBL, _nodeL; NeighborFinder.GetSurroundingNodes(GridPos, out _nodeTL, out _nodeT, out _nodeTR, out _nodeR, out _nodeBR, out _nodeB, out _nodeBL, out _nodeL); lightingTL = GetLightingFromDirection(Direction.TL); lightingTR = GetLightingFromDirection(Direction.TR); lightingBR = GetLightingFromDirection(Direction.BR); lightingBL = GetLightingFromDirection(Direction.BL); ScheduleUpdateGraphicsForSurroundingTiles(); }
public override void Tick(out State _state) { if ((next - (Vector2)owner.TaskHandler.transform.position).sqrMagnitude < 0.01f) { if (owner.TaskHandler.Owner.GetPresentNode() == null) { owner.TaskHandler.Owner.OnNodeDepartFinished(prevNode); owner.TaskHandler.Owner.OnNodeApproachFinished(nextNode); } if (!owner.TaskHandler.Owner.MayLeavePresentNode()) { _state = State.Tick; return; } waypointIndex++; if (waypointIndex >= path.Length) { _state = State.Done; return; } prev = next; prevNode = nextNode; next = path[waypointIndex]; nextNode = GameGrid.GetInstance().GetNodeFromWorldPos(next); bool _isNextNodeWithoutCloseWalls = !nextNode.IsWall && !NeighborFinder.IsCardinalNeighborWall(nextNode.GridPos); ForceCharacterLieDown(_isNextNodeWithoutCloseWalls); SetCharacterOrientation((next - prev).normalized); // set time so movement is kept at a good pace timeAtPrev = Time.time; } if (!owner.TaskHandler.Owner.MayApproachNode()) { _state = State.Tick; return; } owner.TaskHandler.transform.position += GetDeltaPos(); _state = State.Tick; owner.TaskHandler.Owner.OnNodeDeparting(prevNode); owner.TaskHandler.Owner.OnNodeApproaching(nextNode); }
public Node GetClosestFreeNode(Vector3 _worldPos) { Node _node = GetNodeFromWorldPos(_worldPos); if (_node.GetIsWalkable()) { return(_node); } Node[] _nodes; NeighborFinder.GetSurroundingNodes(_node.GridPos, out _nodes); List <Node> _neighbours = new List <Node>(_nodes); int _lastCount = 0; while (_neighbours.Count < (SIZE.x * SIZE.y)) { // iterate over _neighbours until a free node is found for (int i = _lastCount; i < _neighbours.Count; i++) { if (_neighbours[i].GetIsWalkable() && _neighbours[i].GetOccupyingNodeObject() == null) { return(_neighbours[i]); } } int _prevLastCount = _lastCount; _lastCount = _neighbours.Count; // save progress before we add new neighbours, so we don't iterate over old stuff later // iterate over _neighbours - if their neighbours aren't in _neighbours, add them. Node[] _newNeighbours; for (int i = _prevLastCount; i < _lastCount; i++) { NeighborFinder.GetSurroundingNodes(_neighbours[i].GridPos, out _newNeighbours); for (int j = 0; j < _newNeighbours.Length; j++) { if (_neighbours.Contains(_newNeighbours[j])) { continue; } _neighbours.Add(_newNeighbours[j]); } } } return(null); }
public void ClearTemporaryColor() { Int2 _tileGridPosTL, _tileGridPosTR, _tileGridPosBR, _tileGridPosBL; NeighborFinder.GetSurroundingTiles(GridPos, out _tileGridPosTL, out _tileGridPosTR, out _tileGridPosBR, out _tileGridPosBL); // if (_tileTL != null){ // _tileTL.ClearTemporaryColor(); // } if (_tileGridPosTR != Int2.MinusOne) { GameGrid.GetInstance().ClearTemporaryColor(_tileGridPosTR); } // if (_tileBR != null){ // _tileBR.ClearTemporaryColor(); // } // if (_tileBL != null){ // _tileBL.ClearTemporaryColor(); // } }
public Int2 GetWallAsset(Int2 _tileGridPos, Sorting _sorting) { Node _nodeTL, _nodeTR, _nodeBR, _nodeBL; NeighborFinder.GetSurroundingNodes(_tileGridPos, out _nodeTL, out _nodeTR, out _nodeBR, out _nodeBL); bool _hasNodeTL = _nodeTL != null; bool _hasNodeTR = _nodeTR != null; bool _hasNodeBR = _nodeBR != null; bool _hasNodeBL = _nodeBL != null; bool _isWallTL = _hasNodeTL && _nodeTL.IsWall; bool _isWallTR = _hasNodeTR && _nodeTR.IsWall; bool _isWallBR = _hasNodeBR && _nodeBR.IsWall; bool _isWallBL = _hasNodeBL && _nodeBL.IsWall; bool _isWallTemporaryTL = _hasNodeTL && _nodeTL.IsWallTemporarily; bool _isWallTemporaryTR = _hasNodeTR && _nodeTR.IsWallTemporarily; bool _isWallTemporaryBR = _hasNodeBR && _nodeBR.IsWallTemporarily; bool _isWallTemporaryBL = _hasNodeBL && _nodeBL.IsWallTemporarily; bool _useIsWallTemporaryTL = _hasNodeTL && _nodeTL.UseIsWallTemporary; bool _useIsWallTemporaryTR = _hasNodeTR && _nodeTR.UseIsWallTemporary; bool _useIsWallTemporaryBR = _hasNodeBR && _nodeBR.UseIsWallTemporary; bool _useIsWallTemporaryBL = _hasNodeBL && _nodeBL.UseIsWallTemporary; bool _isEitherKindOfWallTL = _isWallTL || (_useIsWallTemporaryTL && _isWallTemporaryTL); bool _isEitherKindOfWallTR = _isWallTR || (_useIsWallTemporaryTR && _isWallTemporaryTR); bool _isEitherKindOfWallBR = _isWallBR || (_useIsWallTemporaryBR && _isWallTemporaryBR); bool _isEitherKindOfWallBL = _isWallBL || (_useIsWallTemporaryBL && _isWallTemporaryBL); bool _isInsideRoom = false; _isInsideRoom = _isInsideRoom || _hasNodeTL && RoomManager.GetInstance().IsInsideShip(_nodeTL.RoomIndex); _isInsideRoom = _isInsideRoom || _hasNodeTR && RoomManager.GetInstance().IsInsideShip(_nodeTR.RoomIndex); _isInsideRoom = _isInsideRoom || _hasNodeBR && RoomManager.GetInstance().IsInsideShip(_nodeBR.RoomIndex); _isInsideRoom = _isInsideRoom || _hasNodeBL && RoomManager.GetInstance().IsInsideShip(_nodeBL.RoomIndex); if (!_isInsideRoom) { return(DefaultAssets.Empty); } else if (_isEitherKindOfWallTL && _isEitherKindOfWallTR && _isEitherKindOfWallBR && _isEitherKindOfWallBL) { return(DefaultAssets.Wall_TL_TR_BR_BL); } else if (_isEitherKindOfWallTL && _isEitherKindOfWallTR && _isEitherKindOfWallBR) { return(DefaultAssets.Wall_TL_TR_BR); } else if (_isEitherKindOfWallTR && _isEitherKindOfWallBR && _isEitherKindOfWallBL) { return(DefaultAssets.Wall_TR_BR_BL); } else if (_isEitherKindOfWallBR && _isEitherKindOfWallBL && _isEitherKindOfWallTL) { return(DefaultAssets.Wall_BR_BL_TL); } else if (_isEitherKindOfWallBL && _isEitherKindOfWallTL && _isEitherKindOfWallTR) { return(DefaultAssets.Wall_BL_TL_TR); } else if (_isEitherKindOfWallTL && _isEitherKindOfWallTR) { return(DefaultAssets.Wall_TL_TR); } else if (_isEitherKindOfWallTR && _isEitherKindOfWallBR) { return(DefaultAssets.Wall_TR_BR); } else if (_isEitherKindOfWallBR && _isEitherKindOfWallBL) { return(DefaultAssets.Wall_BR_BL); } else if (_isEitherKindOfWallBL && _isEitherKindOfWallTL) { return(DefaultAssets.Wall_BL_TL); } else if (_isEitherKindOfWallTL && _isEitherKindOfWallBR) { return(DefaultAssets.Wall_TL_BR); } else if (_isEitherKindOfWallTR && _isEitherKindOfWallBL) { return(DefaultAssets.Wall_TR_BL); } else if (_isEitherKindOfWallTL) { return(DefaultAssets.Wall_TL); } else if (_isEitherKindOfWallTR) { return(DefaultAssets.Wall_TR); } else if (_isEitherKindOfWallBR) { return(DefaultAssets.Wall_BR); } else if (_isEitherKindOfWallBL) { return(DefaultAssets.Wall_BL); } else { return(DefaultAssets.Wall_None); } }
int[] proccesData(CudaContext context, HostDataset host, int[][] nearestNeighbors) { int len = host.Vectors.Length; int initialCasheSize = nearestNeighbors[0].Length - K; List <int>[] associates = new List <int> [len]; for (int i = 0; i < len; i++) { associates[i] = new List <int>(); } byte[] isInDaaset = new byte[len]; int[] nextNearestNabour = new int[len]; int[] nearestNaboursSizes = new int[len]; for (int i = 0; i < isInDaaset.Length; i++) { isInDaaset[i] = 1; nextNearestNabour[i] = K + 1; nearestNaboursSizes[i] = initialCasheSize + K; } NeighborFinder finder = new NeighborFinder(context, new FlattArray <float>(host.Vectors), initialCasheSize / 2); Func <int, int, int> isClasifiedCorectly = (int vectorToCheck, int removed) => { int correctCount = 0; int myClass = host.Classes[vectorToCheck]; for (int i = 0; i < K; i++) { if (nearestNeighbors[vectorToCheck][i] != removed && host.Classes[nearestNeighbors[vectorToCheck][i]] == myClass) { correctCount++; } } if (correctCount >= K / 2) { return(1); } else { return(0); } }; Action <int, int> findNewNearestNeabout = (int vector, int removed) => { int toRemove = -1; for (int i = 0; i < K; i++) { if (removed == nearestNeighbors[vector][i]) { toRemove = i; break; } } for (int i = nextNearestNabour[vector]; i < nearestNaboursSizes[vector]; i++) { if (isInDaaset[nearestNeighbors[vector][i]] == 1) { nearestNeighbors[vector][toRemove] = nearestNeighbors[vector][i]; nextNearestNabour[vector] = i + 1; associates[nearestNeighbors[vector][i]].Add(vector); return; } } int vectorsFound; var neabours = finder.Find(isInDaaset, vector, out vectorsFound); nearestNeighbors[vector][toRemove] = neabours[0]; associates[neabours[0]].Add(vector); nextNearestNabour[vector] = K + 1; nearestNaboursSizes[vector] = K + vectorsFound; Array.Copy( neabours, 1, nearestNeighbors[vector], K + 1, neabours.Length - 1 ); }; for (int i = 0; i < len; i++) { for (int j = 0; j < K; j++) { associates[nearestNeighbors[i][j]].Add(i); } } for (int i = 0; i < len; i++) { int correctCount = 0; for (int j = 0; j < associates[i].Count; j++) { correctCount += isClasifiedCorectly(associates[i][j], i); } if (correctCount >= (associates[i].Count - correctCount)) { isInDaaset[i] = 0; for (int j = 0; j < associates[i].Count; j++) { findNewNearestNeabout(associates[i][j], i); } } } List <int> indecesInDataSet = new List <int>(); for (int i = 0; i < isInDaaset.Length; i++) { if (isInDaaset[i] == 1) { indecesInDataSet.Add(host.OrginalIndeces[i]); } } finder.Dispose(); return(indecesInDataSet.ToArray()); }
public void ScheduleUpdateGraphicsForSurroundingTiles() { ColorManager.ColorUsage _context = ColorManager.ColorUsage.Default; if (UseIsWallTemporary && IsWallTemporarily) { _context = ColorManager.ColorUsage.New; } if (UseIsWallTemporary && !IsWallTemporarily) { _context = ColorManager.ColorUsage.Delete; } if (UseAttachedInteractiveObjectTemporary && AttachedInteractiveObjectTemporary != null) { _context = ColorManager.ColorUsage.New; if (AttachedInteractiveObject != null) { _context = ColorManager.ColorUsage.Blocked; } } if (UseAttachedInteractiveObjectTemporary && AttachedInteractiveObjectTemporary == null) { _context = ColorManager.ColorUsage.Delete; } byte _colorIndex = ColorManager.GetColorIndex(_context); byte[] _colorChannelIndices = new byte[ColorManager.COLOR_CHANNEL_COUNT] { _colorIndex, _colorIndex, _colorIndex, _colorIndex, _colorIndex, _colorIndex, _colorIndex, _colorIndex, _colorIndex, _colorIndex }; Int2 _tileGridPosTL, _tileGridPosTR, _tileGridPosBR, _tileGridPosBL; NeighborFinder.GetSurroundingTiles(GridPos, out _tileGridPosTL, out _tileGridPosTR, out _tileGridPosBR, out _tileGridPosBL); if (_tileGridPosTR != Int2.MinusOne) { GameGrid.GetInstance().ScheduleUpdateForTileAsset(_tileGridPosTR); UpdateTileColor(_tileGridPosTR, _colorChannelIndices); SetTileVertexLighting(_tileGridPosTR, lightingTR, _vertexIndex: GameGridMesh.VERTEX_INDEX_BOTTOM_LEFT); } if (_tileGridPosBL != Int2.MinusOne) { GameGrid.GetInstance().ScheduleUpdateForTileAsset(_tileGridPosBL); UpdateTileColor(_tileGridPosBL, _colorChannelIndices); SetTileVertexLighting(_tileGridPosBL, lightingBL, _vertexIndex: GameGridMesh.VERTEX_INDEX_TOP_RIGHT); } if (_tileGridPosTL != Int2.MinusOne) { GameGrid.GetInstance().ScheduleUpdateForTileAsset(_tileGridPosTL); UpdateTileColor(_tileGridPosTL, _colorChannelIndices); SetTileVertexLighting(_tileGridPosTL, lightingTL, _vertexIndex: GameGridMesh.VERTEX_INDEX_BOTTOM_RIGHT); } if (_tileGridPosBR != Int2.MinusOne) { GameGrid.GetInstance().ScheduleUpdateForTileAsset(_tileGridPosBR); UpdateTileColor(_tileGridPosBR, _colorChannelIndices); SetTileVertexLighting(_tileGridPosBR, lightingBR, _vertexIndex: GameGridMesh.VERTEX_INDEX_TOP_LEFT); } }
Color32 GetLightingFromDirection(Direction _direction) { Direction _directionY = Direction.None; Direction _directionX = Direction.None; switch (_direction) { case Direction.None: case Direction.All: case Direction.T: case Direction.R: case Direction.B: case Direction.L: Debug.LogError(_direction + " isn't supported by GetLightingFromDirection()!"); break; case Direction.TL: _directionY = Direction.T; _directionX = Direction.L; break; case Direction.TR: _directionY = Direction.T; _directionX = Direction.R; break; case Direction.BR: _directionY = Direction.B; _directionX = Direction.R; break; case Direction.BL: _directionY = Direction.B; _directionX = Direction.L; break; default: Debug.LogError(_direction + " hasn't been properly implemented yet!"); break; } NeighborFinder.TryCacheNeighbor(GridPos, _direction); NeighborFinder.TryCacheNeighbor(GridPos, _directionX); NeighborFinder.TryCacheNeighbor(GridPos, _directionY); Node _neighborXY = NeighborFinder.CachedNeighbors[_direction]; Node _neighborX = NeighborFinder.CachedNeighbors[_directionY]; Node _neighborY = NeighborFinder.CachedNeighbors[_directionX]; Color32 _lightingFromNeighborXY = new Color32(); if (_neighborXY != null && !_neighborXY.IsWall) { _lightingFromNeighborXY = _neighborXY.GetLighting(NeighborFinder.GetDirectionMirrored(_direction)); } Color32 _lightingFromNeighborX = new Color32(); if (_neighborX != null && !_neighborX.IsWall) { _lightingFromNeighborX = _neighborX.GetLighting(NeighborFinder.GetDirectionMirroredInX(_direction)); } Color32 _lightingFromNeighborY = new Color32(); if (_neighborY != null && !_neighborY.IsWall) { _lightingFromNeighborY = _neighborY.GetLighting(NeighborFinder.GetDirectionMirroredInY(_direction)); } Color32 _newLighting = new Color32( (byte)Mathf.Max(_lightingFromNeighborXY.r, Mathf.Max(_lightingFromNeighborY.r, _lightingFromNeighborX.r)), (byte)Mathf.Max(_lightingFromNeighborXY.g, Mathf.Max(_lightingFromNeighborY.r, _lightingFromNeighborX.r)), (byte)Mathf.Max(_lightingFromNeighborXY.b, Mathf.Max(_lightingFromNeighborY.r, _lightingFromNeighborX.r)), (byte)Mathf.Max(_lightingFromNeighborXY.a, Mathf.Max(_lightingFromNeighborY.r, _lightingFromNeighborX.r)) ); return(_newLighting); }