public void GetAreaByHash(int value, out Area area, out Passability passability) { AreaPassabilityPair val = _hashToArea[value]; area = val.area; passability = val.passability; }
public void GetAreaByHash(short hash, out Area area, out Passability passability) { int hashInt = hash; area = areaByIndex[hashInt >> 4]; passability = (Passability)(hashInt & 15); }
public bool AddCell(List <Vector3> nodes, Area area, Passability passability, int layer) { CellContentData d = new CellContentData(nodes[0], nodes[1]); if (GetEdge(d).CellsContains(nodes) == false) { List <GenerationEdgeInfo> edges = new List <GenerationEdgeInfo>(); for (int i = 0; i < nodes.Count - 1; i++) { edges.Add(GetEdge(new CellContentData(nodes[i], nodes[i + 1]))); } edges.Add(GetEdge(new CellContentData(nodes[0], nodes[nodes.Count - 1]))); GenerationCellInfo newCell = new GenerationCellInfo(area, passability, layer, graph, nodes, edges); cells.Add(newCell); foreach (var edge in edges) { edge.SetCellToEdge(newCell); } return(true); } else { return(false); } }
public PathNode(Passability isClear, Vector3 pos, int x, int y, int h) { clear = isClear; position = pos; gridX = x; gridY = y; gridH = h; }
public void SetObject(int column, int row, GameObject sprite, Passability passability = Passability.block) { sprite.Position = new Vector2(column * this._tileLength, row * _tileLength); sprite.Column = column; sprite.Row = row; sprite.IsDisplaced = false; sprite.Passability = passability; this.ObjMap[column, row] = sprite; }
public AreaPassabilityPair(Area area, Passability passability) { if (area == null) { Debug.LogError("Area Pair cant be created with null"); } this.area = area; this.passability = passability; }
public override int GetHashCode() { unchecked { int result = TileNumber; result = (result * 397) ^ Passability.GetHashCode(); result = (result * 397) ^ ObjectNumber; return(result); } }
List <CellContentData> _originalEdges; //just cell edges without jump spots to recreate cell if needed public Cell(Area area, Passability passability, int layer, Graph graph, IEnumerable <CellContentData> originalEdges) { this.area = area; this.passability = passability; this.layer = layer; this.graph = graph; _originalEdges = new List <CellContentData>(originalEdges); foreach (var oe in originalEdges) { _contentDictionary.Add(oe, null); } }
public TriangulatorDataSet( NavMeshTemplateRecast template, List <NodeAbstract> nodes, List <EdgeAbstract> edges, Dictionary <NodeAbstract, TriangulatorNodeData> values, int layer, Area area, Passability passability) { this.area = area; this.layer = layer; this.passability = passability; Dictionary <NodeAbstract, int> indexes = new Dictionary <NodeAbstract, int>(); _nodes = new TriangulatorNode[nodes.Count]; _data = new TriangulatorNodeData[nodes.Count]; for (int i = 0; i < nodes.Count; i++) { _nodes[i] = new TriangulatorNode(nodes[i], i); _data[i] = values[nodes[i]]; indexes.Add(nodes[i], i); } edgeMap = new List <TriangulatorEdge> [EDGE_MAP_SIZE * EDGE_MAP_SIZE]; for (int i = 0; i < EDGE_MAP_SIZE * EDGE_MAP_SIZE; i++) { edgeMap[i] = new List <TriangulatorEdge>(); } chunkPos = template.chunkData.realPositionV2; edgeMapPiselSize = EDGE_MAP_SIZE / PathFinder.gridSize; foreach (var edge in edges) { AddEdge(indexes[edge.a], indexes[edge.b], 1, false); } //debug //Vector3 realNotOffsetedPosition = template.chunkData.realPositionV3; //for (int x = 0; x < EDGE_MAP_SIZE + 1; x++) { // Vector3 A = realNotOffsetedPosition + new Vector3(x * edgeMapPiselSize, 0, 0); // Vector3 B = realNotOffsetedPosition + new Vector3(x * edgeMapPiselSize, 0, PathFinder.gridSize); // Debuger_K.AddLine(A, B, Color.red); //} //for (int z = 0; z < EDGE_MAP_SIZE + 1; z++) { // Vector3 A = realNotOffsetedPosition + new Vector3(0, 0, z * edgeMapPiselSize); // Vector3 B = realNotOffsetedPosition + new Vector3(PathFinder.gridSize, 0, z * edgeMapPiselSize); // Debuger_K.AddLine(A, B, Color.red); //} }
List <CellContentData> _originalEdges; //just cell edges without jump spots to recreate cell if needed public Cell(Area area, Passability passability, int layer, Graph graph, IEnumerable <CellContentData> originalEdges) { this.area = area; this.passability = passability; this.layer = layer; this.graph = graph; _originalEdges = new List <CellContentData>(originalEdges); foreach (var oe in originalEdges) { _contentDictionary.Add(oe, null); } advancedAreaCell = area is AreaAdvanced; if (advancedAreaCell) { pathContent = new List <CellPathContentAbstract>(); } }
public GenerationCellInfo(Area area, Passability passability, int layer, Graph graph, IEnumerable <Vector3> nodes, List <GenerationEdgeInfo> edges) { List <CellContentData> originalEdges = new List <CellContentData>(); for (int i = 0; i < edges.Count; i++) { originalEdges.Add(edges[i].data); } cell = new Cell(area, passability, layer, graph, originalEdges); this.nodes = new HashSet <Vector3>(nodes); this.edges = edges; Vector3 cellCenter = SomeMath.MidPoint(nodes); if (edges.Count > 3) { Dictionary <GenerationEdgeInfo, float> triangleArea = new Dictionary <GenerationEdgeInfo, float>(); Dictionary <GenerationEdgeInfo, Vector3> centers = new Dictionary <GenerationEdgeInfo, Vector3>(); float areaSum = 0; foreach (var item in edges) { Vector3 curTriangleCenter = SomeMath.MidPoint(cellCenter, item.data.leftV3, item.data.rightV3); centers.Add(item, curTriangleCenter); float curArea = Vector3.Cross(item.data.leftV3 - curTriangleCenter, item.data.rightV3 - curTriangleCenter).magnitude * 0.5f; areaSum += curArea; triangleArea.Add(item, curArea); } Vector3 actualCenter = Vector3.zero; foreach (var item in edges) { actualCenter += (centers[item] * (triangleArea[item] / areaSum)); } cellCenter = actualCenter; } cell.SetCenter(cellCenter); }
//targeted by area and passability public static void Raycast(Vector3 origin, Vector3 direction, AgentProperties properties, out RaycastHitNavMesh hit, Area expectedArea, Passability expectedPassability, float length = 1000f, int maxIterations = 100) { //try get cell at target position Cell cell; bool outsideCell; if (TryGetCell(origin, properties, out cell, out outsideCell) == false || outsideCell) { hit = new RaycastHitNavMesh(origin, false); return; } //cell we found are not with expected area or passability if (cell.area != expectedArea | cell.passability != expectedPassability) { hit = new RaycastHitNavMesh(origin, true); return; } Raycast(origin, direction, properties, out hit, length, true, true, maxIterations, expectedPassability, expectedArea, cell); }
public static void GetAreaByHash(int value, out Area area, out Passability passability) { lock (_hashData) _hashData.GetAreaByHash(value, out area, out passability); }
public short GetAreaHash(Area area, Passability passability) { return(GetAreaHash(areaToIndex[area], (byte)passability)); }
public void SetPassability(int x, int z, Passability p) { passability[x][z] = (int)p; }
public void FillObjectRange(int startColumn, int startRow, int endColumn, int endRow, Texture2D texture, Passability passability = Passability.block) { for (int x = startColumn; x < endColumn; x++) { for (int y = startRow; y < endRow; y++) { SetObject(x, y, new GameObject(texture), passability); } } }
public GameObject(Texture2D texture, int column, int row, Passability passability) : this(texture, column, row) { Passability = passability; }
public static void Raycast2Body2(float posX, float posY, float posZ, float rayDirX, float rayDirY, Cell cell, float maxLength, bool checkArea, bool checkPass, Area expArea, Passability expPass, RaycastAllocatedData rad, out RaycastHitNavMesh2 hit) { if (SomeMath.SqrMagnitude(rayDirX, rayDirY) == 0f) { hit = new RaycastHitNavMesh2(posX, posY, posZ, NavmeshRaycastResultType2.ReachMaxDistance, cell); return; } rad.chunkPixelSize = PathFinder.gridSize / PathFinder.CELL_GRID_SIZE; //trick to fix case when raycast start on "near" edge //currently can't be on chunk edge so we dont care if chunk changed rad.currentChunkData = cell.graph.chunk; int curGridX = (int)((posX - rad.currentChunkData.realX) / rad.chunkPixelSize); int curGridY = (int)((posZ - rad.currentChunkData.realZ) / rad.chunkPixelSize); //if (curGridX < 0) curGridX = 0; else if (curGridX > 9) curGridX = 9; //if (curGridY < 0) curGridY = 0; else if (curGridY > 9) curGridY = 9; rad.startIntX = (rad.currentChunkData.x * 10) + curGridX; rad.startIntY = (rad.currentChunkData.z * 10) + curGridY; var tempVal = maxLength / rad.chunkPixelSize; if (tempVal > 10000) //too big number anyway { rad.gridDistanceTreshold = SomeMath.Sqr(10000); } else { rad.gridDistanceTreshold = (int)SomeMath.Sqr(maxLength / rad.chunkPixelSize) + 1; } rad.dataArray = cell.graph.dataMap[curGridX][curGridY]; if (rad.dataArray != null) { for (int i = 0; i < rad.dataArray.Length; i++) { CellDataMapValue mapData = rad.dataArray[i]; if (mapData.from != cell) { continue; } if (mapData.data.RotateRightAndReturnDot(rayDirX, rayDirY) < 0) { continue; } float sqrDist = SomeMath.SqrDistance(mapData.data.NearestPointXZ(posX, posZ), new Vector2(posX, posZ)); if (sqrDist < 0.0001f) { Vector2 dirToCellCenter = (cell.centerVector2 - new Vector2(posX, posZ)).normalized * 0.001f; posX += dirToCellCenter.x; posZ += dirToCellCenter.y; //if (dot < 0.001f) {//oh wow. start point exactly on edge and ray alond side //} break; } } } //if (DEBUG) { // Debuger_K.AddRay(new Vector3(posX, posY + 0.1f, posZ), new Vector3(rayDirX, 0, rayDirY), Color.gray); //} rad.posX = posX; rad.posY = posY; rad.posZ = posZ; rad.rayDirX = rayDirX; rad.rayDirY = rayDirY; rad.checkPass = checkPass; rad.checkArea = checkArea; rad.expPass = expPass; rad.expArea = expArea; rad.raycastType = NavmeshRaycastResultType2.Nothing; rad.maxSqrLength = SomeMath.Sqr(maxLength); rad.curCell = cell; rad.prevCell = null; rad.raycastDone = false; float chunkX, chunkZ, curHullX, curHullZ, lastHullX = posX, lastHullZ = posZ; for (int i = 0; i < 4; i++) { rad.raycastSamples[i] = raycastSamplesTemplate[i].RotateRightAndReturnDot(rayDirX, rayDirY) < 0; } int chunkIteration = 0; while (rad.raycastDone == false) { chunkIteration++; if (chunkIteration > 50) { string s = string.Format("chunkIteration too large. x {0}, y {1}, z {2}, dx {3}, dy {4}, max {5}", posX, posY, posZ, rayDirX, rayDirY, maxLength); //HandleTextFile.WriteString(s); //Debuger_K.AddRay(new Vector3(posX, posY, posZ), Vector3.down, Color.cyan); //Debuger_K.AddRay(new Vector3(posX, posY, posZ), new Vector3(rayDirX, 0, rayDirY), Color.yellow, 50); //Debuger_K.UserfulPublicFlag = true; Debug.LogError(s); break; } rad.currentChunkData = rad.curCell.graph.chunk; rad.curChunkIntX = rad.currentChunkData.x * 10; rad.curChunkIntY = rad.currentChunkData.z * 10; rad.dataMap = rad.curCell.graph.dataMap; chunkX = rad.currentChunkData.realX; chunkZ = rad.currentChunkData.realZ; #region border points curHullX = posX; curHullZ = posZ; for (int i = 0; i < 4; i++) { if (rad.raycastSamples[i]) { CellContentData curSide = raycastSamplesTemplate[i]; float rX, rZ; if (SomeMath.RayIntersectSegment(posX, posZ, rayDirX, rayDirY, curSide.xLeft + chunkX, curSide.zLeft + chunkZ, curSide.xRight + chunkX, curSide.zRight + chunkZ, out rX, out rZ)) { curHullX = rX; curHullZ = rZ; } //if (DEBUG) // Debuger_K.AddLine(curSide.a, curSide.b, Color.red, chunkIteration); } } #region debug //if (DEBUG) { // Debuger_K.AddLine(new Vector3(curHullX, 0, curHullZ), new Vector3(lastHullX, 0, lastHullZ), Color.yellow, chunkIteration); // for (int x = 0; x < PathFinder.CELL_GRID_SIZE + 1; x++) { // Debuger_K.AddLine( // currentChunkData.realPositionV3 + new Vector3(x * chunkPixelSize, 0, 0), // currentChunkData.realPositionV3 + new Vector3(x * chunkPixelSize, 0, PathFinder.gridSize), // Color.red); // } // for (int z = 0; z < PathFinder.CELL_GRID_SIZE + 1; z++) { // Debuger_K.AddLine( // currentChunkData.realPositionV3 + new Vector3(0, 0, z * chunkPixelSize), // currentChunkData.realPositionV3 + new Vector3(PathFinder.gridSize, 0, z * chunkPixelSize), // Color.red); // } //} #endregion #endregion DDARasterization.DrawLine( lastHullX - chunkX, lastHullZ - chunkZ, curHullX - chunkX, curHullZ - chunkZ, rad.chunkPixelSize, rad, RaycastDelegate); lastHullX = curHullX; lastHullZ = curHullZ; } hit = new RaycastHitNavMesh2(rad.raycastResultX, rad.raycastResultY, rad.raycastResultZ, rad.raycastType, rad.curCell); }
public int GetAreaHash(Area area, Passability passability) { return(_areaToHash[new AreaPassabilityPair(area, passability)]); }
public BattleGridPoint(Vector3 pos, Passability passability, VectorInt.Vector2Int gridPos) { this._pos = pos; this.passability = passability; this.gridPos = gridPos; }
protected void RasterizeTriangle(Volume volume, Vector3 A, Vector3 B, Vector3 C, float fragmentSize, int startX, int endX, int startZ, int endZ, Area area, Passability passability, params VoxelState[] trueStates) { int minX = Mathf.Clamp(Mathf.RoundToInt(SomeMath.Min(A.x, B.x, C.x) / fragmentSize) - 1, startX, endX); int minZ = Mathf.Clamp(Mathf.RoundToInt(SomeMath.Min(A.z, B.z, C.z) / fragmentSize) - 1, startZ, endZ); int maxX = Mathf.Clamp(Mathf.RoundToInt(SomeMath.Max(A.x, B.x, C.x) / fragmentSize) + 1, startX, endX); int maxZ = Mathf.Clamp(Mathf.RoundToInt(SomeMath.Max(A.z, B.z, C.z) / fragmentSize) + 1, startZ, endZ); if (minX == maxX || minZ == maxZ) { return; //if too small return } Vector3[] vectorsStart = new Vector3[3] { A, B, C }; for (int x = minX; x < maxX; ++x) { int vertsInLength1 = ClipPolyToPlane(1f, 0.0f, -x * fragmentSize, vectorsStart, 3, ref _polyListXTemp); if (vertsInLength1 >= 3) { int vertsInLength2 = ClipPolyToPlane(-1f, 0.0f, (x + 1) * fragmentSize, _polyListXTemp, vertsInLength1, ref _polyListXFinal); if (vertsInLength2 >= 3) { for (int z = minZ; z < maxZ; ++z) { int vertsInLength3 = ClipPolyToPlane(0.0f, 1f, -z * fragmentSize, _polyListXFinal, vertsInLength2, ref _polyListZTemp); if (vertsInLength3 >= 3) { int vertsInLength4 = ClipPolyToPlane(0.0f, -1f, (z + 1) * fragmentSize, _polyListZTemp, vertsInLength3, ref _polyListZFinal); if (vertsInLength4 >= 3) { float min = _polyListZFinal[0].y; float max = _polyListZFinal[0].y; for (int index = 1; index < vertsInLength4; ++index) { min = Math.Min(min, _polyListZFinal[index].y); max = Math.Max(max, _polyListZFinal[index].y); } int indexX = Math.Abs(x - startX); int indexZ = Math.Abs(z - startZ); volume.SetVoxel(indexX, indexZ, max, min, area, (int)passability); } } } } } } }
public AreaPassabilityPair(Area area, Passability passability) { this.area = area; this.passability = passability; }
public Tile(Texture2D texture, Vector2 position, Passability passability) : base(texture) { this.Position = position; this.Passability = passability; }
//prefer cloning and use clone than this cause it lock public static int GetAreaHash(Area area, Passability passability) { lock (_hashData) return(_hashData.GetAreaHash(area, passability)); }
private void Raycast(Vector3 origin, Vector3 direction, out RaycastHitNavMesh hit, float length, int maxIterations, Passability expectedPassability, Area expectedArea, Cell cell) { HashSet <CellContentData> raycastExclude = new HashSet <CellContentData>(); List <RaycastSomeData> raycastTempData = new List <RaycastSomeData>(); float maxLengthSqr = length * length; for (int iteration = 0; iteration < maxIterations; iteration++) { raycastTempData.Clear();//iteration data cleared foreach (var pair in cell.dataContentPairs) { CellContentData curData = pair.Key; if (!raycastExclude.Add(curData))//mean it's already contain this { continue; } Vector3 intersect; if (SomeMath.RayIntersectXZ(origin, direction, curData.leftV3, curData.rightV3, out intersect)) { if (pair.Value != null) { Cell otherCell = pair.Value.connection; if (otherCell == cell | !otherCell.canBeUsed) { continue; } if (cell.passability != otherCell.passability || cell.area != otherCell.area) { hit = new RaycastHitNavMesh(intersect, SomeMath.SqrDistance(origin, intersect) < maxLengthSqr);//!!! return; } raycastTempData.Add(new RaycastSomeData(intersect, otherCell)); } else { raycastTempData.Add(new RaycastSomeData(intersect, null)); } } } //check if there possible connection for (int i = 0; i < raycastTempData.Count; i++) { if (raycastTempData[i].cell != null) { cell = raycastTempData[i].cell; goto CONTINUE; } } //now we definetly hit something and now find furthest float furthestSqrDist = 0f; Vector3 furthest = origin; for (int i = 0; i < raycastTempData.Count; i++) { float curSqrDist = SomeMath.SqrDistance(raycastTempData[i].point, origin); if (curSqrDist > furthestSqrDist) { furthestSqrDist = curSqrDist; furthest = raycastTempData[i].point; } } hit = new RaycastHitNavMesh(furthest, SomeMath.SqrDistance(origin, furthest) < maxLengthSqr); return; CONTINUE : { continue; } } hit = new RaycastHitNavMesh(origin, true, true); return; }
public void Draw(SpriteBatch batch, GameTime time, RenderTarget2D output) { // Effectue une setup des render target SetupRenderTargets(); batch.GraphicsDevice.SetRenderTarget(m_tilesRenderTarget); // batch.GraphicsDevice.Clear(Color.Transparent); batch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); // Dessin de debug de la map. int beginX = Math.Max(0, Scrolling.X / UnitSize); int beginY = Math.Max(0, Scrolling.Y / UnitSize); int endX = Math.Min(beginX + (Viewport.Width / UnitSize + 1), Passability.GetLength(0)); int endY = Math.Min(beginY + (Viewport.Height / UnitSize + 1), Passability.GetLength(1)); int smoothAlpha = (__oldScroll == ScrollingVector2 && UnitSize == __oldUnitSize) ? 25 : 255; bool[,] passability = Passability; for (int x = beginX; x < endX; x++) { for (int y = beginY; y < endY; y++) { Point drawPos = new Point(x * UnitSize - Scrolling.X, y * UnitSize - Scrolling.Y); int r = passability[x, y] ? 0 : 255; int b = HasVision(VisionDisplayed, new Vector2(x, y)) ? 255 : 0; int a = SMOOTH_LIGHT ? smoothAlpha : 255; batch.Draw(Ressources.DummyTexture, new Rectangle(drawPos.X / TILE_SCALE, drawPos.Y / TILE_SCALE, UnitSize / TILE_SCALE, UnitSize / TILE_SCALE), new Color(r, 0, b, a)); } } batch.End(); // Dessin des entités batch.GraphicsDevice.SetRenderTarget(m_entitiesRenderTarget); batch.GraphicsDevice.Clear(Color.Transparent); batch.Begin(SpriteSortMode.FrontToBack, BlendState.NonPremultiplied); switch (m_sceneRenderer.Mode) { case DataMode.Direct: foreach (var kvp in Map.Entities) { m_entityRenderer.Draw(batch, time, kvp.Value); } foreach (var cast in Map.Spellcasts) { float radius = 0.0f; Vector2 size = Vector2.Zero; Codinsa2015.Server.Shapes.CircleShape circ = cast.GetShape() as Codinsa2015.Server.Shapes.CircleShape; Codinsa2015.Server.Shapes.RectangleShape rect = cast.GetShape() as Codinsa2015.Server.Shapes.RectangleShape; if (circ != null) { radius = circ.Radius; } if (rect != null) { size = new Vector2(rect.Width, rect.Height); } m_spellCastRenderer.Draw( batch, new Views.GenericShapeView() { Position = cast.GetShape().Position, Radius = radius, Size = size, ShapeType = (circ == null) ? Views.GenericShapeType.Rectangle : Views.GenericShapeType.Circle }, ((Server.Spellcasts.SpellcastBase)cast).Name); } break; case DataMode.Remote: foreach (var entity in MapView.Entities) { // m_entityRenderer.Draw(batch, time, entity.Position, entity.Type, entity.Role); } foreach (var cast in MapView.SpellCasts) { m_spellCastRenderer.Draw(batch, cast.Shape, cast.Name); } break; } batch.End(); // Blur /*for (int i = 0; i < BLUR_PASSES; i++) * { * m_blur.PerformGaussianBlur(m_tilesRenderTarget, m_tmpRenderTarget, m_tmpRenderTarget2, batch); * m_blur.PerformGaussianBlur(m_tmpRenderTarget2, m_tmpRenderTarget, m_tilesRenderTarget, batch); * }*/ // Dessin du tout batch.GraphicsDevice.SetRenderTarget(output); batch.GraphicsDevice.Clear(Color.DarkGray); Ressources.MapEffect.Parameters["xSourceTexture"].SetValue(m_tilesRenderTarget); Ressources.MapEffect.Parameters["scrolling"].SetValue(new Vector2(Scrolling.X / (float)Viewport.Width, Scrolling.Y / (float)Viewport.Height)); Ressources.MapEffect.Parameters["xPixelSize"].SetValue(new Vector2(1.0f / Viewport.Width, 1.0f / Viewport.Height)); Ressources.MapEffect.Parameters["xUnitSize"].SetValue(UnitSize); __xShaderTime += 0.0005f; Ressources.MapEffect.Parameters["xTime"].SetValue(__xShaderTime); // Dessine les tiles avec le shader batch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone, Ressources.MapEffect); batch.Draw(m_tilesRenderTarget, Viewport, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, GraphicsHelpers.Z.Map); batch.End(); // Dessine les entités. batch.Begin(); batch.Draw(m_entitiesRenderTarget, Viewport, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, GraphicsHelpers.Z.Entities); batch.End(); __oldScroll = ScrollingVector2; __oldUnitSize = UnitSize; }