public void HexTools_CheckHex_ThrowsExceptionWhenInputIsEmpty() { Assert.Throws <InvalidHexException>(() => { HexTools.CheckHex(""); }); }
public string StoreData(byte[] data, string extension = null) { var hasher = new SHA256Managed(); hasher.Initialize(); string hash = null; using (hasher) { hash = HexTools.ByteArrayToHexString(hasher.ComputeHash(data)); var targetPath = BuildPath(hash, extension); if (_storage.Exists(targetPath)) { return(null); } var directory = Path.GetDirectoryName(targetPath); _storage.CreateFolder(directory); var file = _storage.Open(targetPath); using (file) { file.Write(data, 0, data.Length); } } return(hash); }
public void GenerateMap() { ClearMap(); tilemapScale = GameObject.FindGameObjectWithTag(Tags.TerrainTilemap).transform.localScale; tileHeight = Vector3.Distance( tilemap.CellToWorld(HexTools.GetAdjacentTileOffset(Vector3Int.zero, Cardinality.N)), tilemap.CellToWorld(new Vector3Int(0, 0, 0))); float diagonalDist = Vector3.Distance( tilemap.CellToWorld(Vector3Int.zero), tilemap.CellToWorld(HexTools.GetAdjacentTileOffset(Vector3Int.zero, Cardinality.NE))); tileWidth = Mathf.Sqrt(diagonalDist * diagonalDist - Mathf.Pow(tileHeight * .5f, 2)) * 4 / 3; tileDefinitions.terrainData.Sort(); var regionDict = StageOne.RunGeneration(this); StageTwo.RunGeneration(this, regionDict); //if (useNoise) // FillMapArea(Vector3Int.zero, initialViewRadius); //else // RandomFillMap(); //for (int i = 0; i < smoothSteps; ++i) //{ // if (!SmoothMap(Vector3Int.zero)) // break; //} //ProcessMap(); }
public void HexTools_CheckHex_DoesNotThrowsExceptionWhenNumberOfHexCharsAreEven() { Assert.DoesNotThrow(() => { HexTools.CheckHex("AABB"); }); }
public void HexTools_CheckHex_ThrowsExceptionWhenNumberOfHexCharsAreOddAndHexContainsInvalidChars() { Assert.Throws <InvalidHexException>(() => { HexTools.CheckHex("AAG"); }); }
public void MakeMesh() { hexHeight = Vector3.Distance( tilemap.CellToWorld(HexTools.GetAdjacentTileOffset(Vector3Int.zero, Cardinality.N)), tilemap.CellToWorld(new Vector3Int(0, 0, 0))); float diagonalDist = Vector3.Distance( tilemap.CellToWorld(Vector3Int.zero), tilemap.CellToWorld(HexTools.GetAdjacentTileOffset(Vector3Int.zero, Cardinality.NE))); hexWidth = Mathf.Sqrt(diagonalDist * diagonalDist - Mathf.Pow(hexHeight * .5f, 2)) * 4 / 3; tilemap.ClearAllTiles(); var tiles = HexTools.GetSpiral(Vector3Int.zero, mapRadius); TileBase[] tileArray = new TileBase[tiles.Count]; for (int index = 0; index < tiles.Count; index++) { tileArray[index] = testTile; } tilemap.SetTiles(tiles.ToArray(), tileArray); if (mapRadius == 0) { mapRadius = 1; } float[,] noiseMap = Noise.GenerateNoiseMap(4 * mapRadius * 2, 3 * mapRadius * 2, noiseSettings, Vector2.zero); MeshIt(tiles.ToArray(), noiseMap); }
public TerrainTile CreateAndSetTile(Vector3Int coord, TerrainTile tilePrefab, TerrainTile originalTile = null) { TerrainTile newTile = Instantiate(tilePrefab); newTile.coordinates = coord; if (debugCells) { if (originalTile == null) { GameObject newObj = Instantiate(locTextPrefab); Vector3 worldPoint = tilemap.CellToWorld(coord); newObj.transform.position = worldPoint; newObj.transform.SetParent(textHolder, true); TextMeshPro text = newObj.GetComponent <TextMeshPro>(); text.transform.localScale = tilemapScale * .5f; text.name = coord.ToString(); text.SetText(coord.ToString() + "\n" + HexTools.OffsetToCube(coord)); newTile.text = text; } else { newTile.text = originalTile.text; newTile.text.SetText(coord.ToString() + "\n" + HexTools.OffsetToCube(coord)); } } if (originalTile != null) { tilemap.SetTile(coord, null); } tilemap.SetTile(coord, newTile); return(newTile); }
public void HexTools_CheckHex_DoesNotThrowsExceptionWhenAllHexCharsAreValid() { Assert.DoesNotThrow(() => { // Testing with Every Valid Char HexTools.CheckHex("0123456789ABCDEF"); }); }
public void HexTools_CheckHex_ThrowsExceptionWhenHexContainsInvalidChars() { Assert.Throws <InvalidHexException>(() => { // 'G' is not Valid in Hex HexTools.CheckHex("AABG"); }); }
private void writeData(object sender, EventArgs e) { string offset_str = this.writeOffset.Text; string data_str = this.writeHex.Text; byte[] data = HexTools.spacedHexToData(data_str); this.requestWrite(HexTools.hexToInt(offset_str), data); }
private void getData(object sender, EventArgs e) { string offset_str = this.dataOffset.Text; string length_str = this.dataLength.Text; int offset = HexTools.hexToInt(offset_str); byte[] data = this.requestData(offset, HexTools.hexToInt(length_str)); accessedData.Add(Tuple.Create(offset, data)); display.Text += "offset: " + HexTools.intToHex(offset) + "\r\ndata: \r\n" + HexTools.dataToPrettyString(data) + "\r\n"; }
private string makeText() { string acc = ""; foreach (Tuple <int, byte[]> data in accessedData) { acc += "offset: " + HexTools.intToOffset(data.Item1) + "\r\ndata: \r\n" + HexTools.dataToPrettyString(data.Item2) + "\r\n"; } return(acc); }
private void refreshList() { string acc = ""; ReadOnlyCollection <Tuple <int, int> > ranges = owner.freeRanges; List <Tuple <int, int> > ranges_mutable = new List <Tuple <int, int> >(ranges); if (offsetSort) { ranges_mutable.Sort(delegate(Tuple <int, int> first, Tuple <int, int> second) { if (first.Item1 == second.Item1) { return(0); } else if (first.Item1 < second.Item1) { return(-1); } else { return(1); } }); } else { ranges_mutable.Sort(delegate(Tuple <int, int> first, Tuple <int, int> second) { int firstLen = first.Item2 - first.Item1; int secondLen = second.Item2 - second.Item1; if (firstLen == secondLen) { return(0); } else if (firstLen < secondLen) { return(-1); } else { return(1); } }); } foreach (Tuple <int, int> aRange in ranges_mutable) { acc += "Start: " + HexTools.intToOffset(aRange.Item1) + " End: " + HexTools.intToOffset(aRange.Item2) + "\r\nLength: " + HexTools.intToOffset(aRange.Item2 - aRange.Item1) + "\r\n\r\n"; } freespaceListing.Text = acc; }
private static void CreatePassage(Region regionA, Region regionB, Vector3Int tileA, Vector3Int tileB) { List <Vector3Int> line = HexTools.GetLine(tileA, tileB); Passageway passageway = new Passageway(regionA, regionB, tileA, tileB, line); Region.ConnectRegions(regionA, regionB, passageway); //Debug.DrawLine(tilemap.CellToWorld(tileA), tilemap.CellToWorld(tileB), Color.green, 10); foreach (Vector3Int tileCoord in line) { mapGen.ChangeAllTilesAround(tileCoord, rules.passageSize, rules.openTile); } }
private static bool SmoothMap(Vector3Int centerPoint, int radiusToSmooth = int.MaxValue) { Dictionary <TerrainTile, TerrainType> changesToMake = new Dictionary <TerrainTile, TerrainType>(); bool ringEmpty = false; int i = 0; while (i <= radiusToSmooth && !ringEmpty) { ringEmpty = true; List <Vector3Int> ring = HexTools.GetRing(centerPoint, i++); foreach (Vector3Int ringTile in ring) { TerrainTile ttb = mapGen.GetTile(ringTile); if (ttb == null) { continue; } ringEmpty = false; int wallCount = 0; TerrainTile[] surroundingTiles = mapGen.GetSurroundingTiles(ringTile); foreach (TerrainTile tile in surroundingTiles) { if (tile == null || tile.terrainType == TerrainType.WaterGenerator) { ++wallCount; } } if (wallCount > rules.minNeighboursToTurnToWater && ttb.terrainType != TerrainType.WaterGenerator) { changesToMake[ttb] = TerrainType.WaterGenerator; } else if (wallCount < rules.minNeighboursToTurnToWater && ttb.terrainType != TerrainType.LandGenerator) { changesToMake[ttb] = TerrainType.LandGenerator; } } } foreach (var change in changesToMake) { mapGen.CreateAndSetTile(change.Key.coordinates, mapGen.GetTerrainData(change.Value).tile, change.Key); } return(changesToMake.Count > 0); }
public void ChangeAllTilesAround(Vector3Int center, int radius, TerrainTile newType) { List <Vector3Int> tiles = HexTools.GetTilesInRange(center, radius - 1); foreach (Vector3Int tile in tiles) { TerrainTile ttb = GetTile(tile); if (tile == null) { continue; } CreateAndSetTile(tile, newType, ttb); } }
static void Main(string[] args) { Console.WriteLine("Please Enter Path of the File"); string filepath; filepath = Console.ReadLine(); HexTools program = new HexTools(); List <HexTools.lineofData> Completedatalist = new List <HexTools.lineofData>(); Completedatalist = program.ReadHexFile(filepath); Console.ReadLine(); }
private void markAsAllocated(object sender, EventArgs e) { if (this.useEnd.Checked) { int start = HexTools.hexToInt(this.startBox.Text); int end = HexTools.hexToInt(this.endBox.Text); owner.markAllocated(start, end - start); } else { int start = HexTools.hexToInt(this.startBox.Text); int length = HexTools.hexToInt(this.endBox.Text); owner.markAllocated(start, length); } startBox.ResetText(); endBox.ResetText(); lengthBox.ResetText(); refreshList(); }
private List <List <Vector3Int> > GetRegionsRingSearch(TerrainType regionType, int distanceToCheck) { List <List <Vector3Int> > regions = new List <List <Vector3Int> >(); Dictionary <Vector3Int, bool> mapFlags = new Dictionary <Vector3Int, bool>(); Vector3Int centerCoords = Vector3Int.zero; bool ringEmpty = false; int i = 0; while (i <= distanceToCheck && !ringEmpty) { ringEmpty = true; List <Vector3Int> ring = HexTools.GetRing(centerCoords, i++); foreach (Vector3Int ringTile in ring) { TerrainTile ttb = GetTile(ringTile); if (ttb == null) { continue; } ringEmpty = false; if (mapFlags.TryGetValue(ringTile, out bool searched) == true && searched == true) { continue; } if (ttb.terrainType == regionType) { List <Vector3Int> newRegion = GetRegionTiles(ringTile); foreach (Vector3Int coord in newRegion) { mapFlags[coord] = true; } regions.Add(newRegion); } } } return(regions); }
private static void FillMapArea(Vector3Int spawnCenter, int radius) { noiseMap = Noise.GenerateNoiseMap( radius * 2, radius * 2, mapGen.noiseSettings, new Vector2(spawnCenter.y, -spawnCenter.x)); for (int x = 0; x < noiseMap.GetLength(0); ++x) { for (int y = 0; y < noiseMap.GetLength(1); ++y) { Vector3Int coord = spawnCenter + new Vector3Int( Mathf.CeilToInt(-noiseMap.GetLength(1) * .5f) + y, Mathf.CeilToInt(-noiseMap.GetLength(0) * .5f) + x, 0); if (mapGen.GetTile(coord) != null || HexTools.DistanceInTiles(coord, spawnCenter) > radius) { continue; } CreateAndSetTileFromNoise(coord, noiseMap[x, y]); } } }
void Update() { Vector3 mousepos = Input.mousePosition; Ray ray = cam.ScreenPointToRay(mousepos); Plane plane = new Plane(Vector3.back, Vector3.zero); if (plane.Raycast(ray, out float hitDist)) { Vector3 worldpoint = ray.GetPoint(hitDist); Vector3Int tilepoint = tilemap.WorldToCell(worldpoint); TerrainTile tile = GetTile(tilepoint); if (startTile != null) { foreach (Vector3Int lineTile in lastLine) { tilemap.SetColor(lineTile, Color.white); } lastLine.Clear(); foreach (Vector3Int radiusTile in lastRadius) { tilemap.SetColor(radiusTile, Color.white); } lastRadius.Clear(); foreach (Vector3Int ringTile in lastRing) { tilemap.SetColor(ringTile, Color.white); } lastRing.Clear(); if (Input.GetMouseButtonUp(0)) { startTile = null; } else { int n = HexTools.DistanceInTiles(startTile.coordinates, tilepoint); List <Vector3Int> radius = HexTools.GetSpiral(startTile.coordinates, n); //HexTools.GetTilesInRange(startTile.coordinates, n); List <Vector3Int> line = HexTools.GetLine(startTile.coordinates, tilepoint); List <Vector3Int> ring = HexTools.GetRing(startTile.coordinates, n); foreach (Vector3Int radiusTile in radius) { tilemap.SetColor(radiusTile, Color.yellow); } foreach (Vector3Int radiusTile in ring) { tilemap.SetColor(radiusTile, Color.red); } foreach (Vector3Int lineTile in line) { tilemap.SetColor(lineTile, Color.magenta); } lastLine = line; lastRadius = radius; lastRing = ring; } } if (tile == null && Input.GetMouseButtonDown(0)) { RevealArea(tilepoint, revealRadius); } else { if (tile != null) { if (Input.GetMouseButtonDown(0)) { Debug.Log("World: " + worldpoint + " Coords: " + tilepoint); startTile = tile; } } else { if (Input.GetMouseButtonDown(0)) { Debug.Log("Null Tilepoint: " + tilepoint); } } } } }
private static void ConnectClosestRegions(List <Region> allRegions, bool forceAccessibilityFromMainRegion = false) { List <Region> regionListA = new List <Region>(); List <Region> regionListB = new List <Region>(); if (forceAccessibilityFromMainRegion) { foreach (Region region in allRegions) { if (!region.isAccessibleFromMainRegion) { regionListA.Add(region); } else { regionListB.Add(region); } } } else { regionListA = allRegions; regionListB = allRegions; } int shortestDist = 0; Vector3Int bestTileA = new Vector3Int(); Vector3Int bestTileB = new Vector3Int(); Region bestRegionA = new Region(); Region bestRegionB = new Region(); bool possibleConnectionFound = false; foreach (Region regionA in regionListA) { if (!forceAccessibilityFromMainRegion) { possibleConnectionFound = false; if (regionA.connectedRegions.Count > 0) { continue; } } foreach (Region regionB in regionListB) { if (regionA == regionB || regionA.IsConnected(regionB)) { continue; } for (int tileIndexA = 0; tileIndexA < regionA.edgeTiles.Count; ++tileIndexA) { for (int tileIndexB = 0; tileIndexB < regionB.edgeTiles.Count; ++tileIndexB) { Vector3Int tileA = regionA.edgeTiles[tileIndexA]; Vector3Int tileB = regionB.edgeTiles[tileIndexB]; int distanceBetweenRooms = HexTools.DistanceInTiles(tileA, tileB); if (distanceBetweenRooms < shortestDist || !possibleConnectionFound) { shortestDist = (int)distanceBetweenRooms; possibleConnectionFound = true; bestTileA = tileA; bestTileB = tileB; bestRegionA = regionA; bestRegionB = regionB; } } } } if (possibleConnectionFound && !forceAccessibilityFromMainRegion) { CreatePassage(bestRegionA, bestRegionB, bestTileA, bestTileB); } } if (possibleConnectionFound && forceAccessibilityFromMainRegion) { CreatePassage(bestRegionA, bestRegionB, bestTileA, bestTileB); ConnectClosestRegions(allRegions, true); } if (!forceAccessibilityFromMainRegion) { ConnectClosestRegions(allRegions, true); } }
private void MeshIt(Vector3Int[] tiles, float[,] noiseMap) { Vector3 toTopLeft = -Vector3.right * .25f * hexWidth + Vector3.forward * .5f * hexHeight; Vector3 toTopRight = Vector3.right * .25f * hexWidth + Vector3.forward * .5f * hexHeight; Vector3 toRight = Vector3.right * .5f * hexWidth; Dictionary <Vector3Int, TileMeshData> tileVertexCodex = new Dictionary <Vector3Int, TileMeshData>(); int[] triangleIndices = new int[tiles.Length * 12]; int vertexIndex = 0; int triangleIndex = 0; float minX = float.MaxValue; float maxX = float.MinValue; float minY = float.MaxValue; float maxY = float.MinValue; foreach (Vector3Int tile in tiles) { Vector3 tileCenter = tilemap.GetCellCenterWorld(tile); Vector3 topLeftCorner = tileCenter + toTopLeft; Vector3 topRightCorner = tileCenter + toTopRight; Vector3 rightCorner = tileCenter + toRight; Vector3 bottomRightCorner = tileCenter - toTopLeft; Vector3 bottomLeftCorner = tileCenter - toTopRight; Vector3 leftCorner = tileCenter - toRight; minX = Mathf.Min(minX, leftCorner.x); maxX = Mathf.Max(maxX, rightCorner.x); minY = Mathf.Min(minY, bottomRightCorner.z); maxY = Mathf.Max(maxY, topRightCorner.z); TileMeshData tmd = new TileMeshData(); tmd.verticeIndices = new Tuple <Vector3, int> [6]; // look for shared vertices Vector3Int[] surroundingTiles = HexTools.GetSurroundingTilesOffset(tile); if (tileVertexCodex.TryGetValue(surroundingTiles[(int)Cardinality.N], out TileMeshData sharedData)) { tmd.verticeIndices[(int)IndexOrder.TopLeft] = new Tuple <Vector3, int>(topLeftCorner, sharedData.verticeIndices[(int)IndexOrder.BottomLeft].Item2); tmd.verticeIndices[(int)IndexOrder.TopRight] = new Tuple <Vector3, int>(topRightCorner, sharedData.verticeIndices[(int)IndexOrder.BottomRight].Item2); } if (tileVertexCodex.TryGetValue(surroundingTiles[(int)Cardinality.NE], out sharedData)) { tmd.verticeIndices[(int)IndexOrder.TopRight] = new Tuple <Vector3, int>(topRightCorner, sharedData.verticeIndices[(int)IndexOrder.Left].Item2); tmd.verticeIndices[(int)IndexOrder.Right] = new Tuple <Vector3, int>(rightCorner, sharedData.verticeIndices[(int)IndexOrder.BottomLeft].Item2); } if (tileVertexCodex.TryGetValue(surroundingTiles[(int)Cardinality.SE], out sharedData)) { tmd.verticeIndices[(int)IndexOrder.Right] = new Tuple <Vector3, int>(rightCorner, sharedData.verticeIndices[(int)IndexOrder.TopLeft].Item2); tmd.verticeIndices[(int)IndexOrder.BottomRight] = new Tuple <Vector3, int>(bottomRightCorner, sharedData.verticeIndices[(int)IndexOrder.Left].Item2); } if (tileVertexCodex.TryGetValue(surroundingTiles[(int)Cardinality.S], out sharedData)) { tmd.verticeIndices[(int)IndexOrder.BottomRight] = new Tuple <Vector3, int>(bottomRightCorner, sharedData.verticeIndices[(int)IndexOrder.TopRight].Item2); tmd.verticeIndices[(int)IndexOrder.BottomLeft] = new Tuple <Vector3, int>(bottomLeftCorner, sharedData.verticeIndices[(int)IndexOrder.TopLeft].Item2); } if (tileVertexCodex.TryGetValue(surroundingTiles[(int)Cardinality.SW], out sharedData)) { tmd.verticeIndices[(int)IndexOrder.BottomLeft] = new Tuple <Vector3, int>(bottomLeftCorner, sharedData.verticeIndices[(int)IndexOrder.Right].Item2); tmd.verticeIndices[(int)IndexOrder.Left] = new Tuple <Vector3, int>(leftCorner, sharedData.verticeIndices[(int)IndexOrder.TopRight].Item2); } if (tileVertexCodex.TryGetValue(surroundingTiles[(int)Cardinality.NW], out sharedData)) { tmd.verticeIndices[(int)IndexOrder.Left] = new Tuple <Vector3, int>(leftCorner, sharedData.verticeIndices[(int)IndexOrder.BottomRight].Item2); tmd.verticeIndices[(int)IndexOrder.TopLeft] = new Tuple <Vector3, int>(topLeftCorner, sharedData.verticeIndices[(int)IndexOrder.Right].Item2); } if (tmd.verticeIndices[(int)IndexOrder.TopLeft] == null) { tmd.verticeIndices[(int)IndexOrder.TopLeft] = new Tuple <Vector3, int>(topLeftCorner, vertexIndex++); } if (tmd.verticeIndices[(int)IndexOrder.TopRight] == null) { tmd.verticeIndices[(int)IndexOrder.TopRight] = new Tuple <Vector3, int>(topRightCorner, vertexIndex++); } if (tmd.verticeIndices[(int)IndexOrder.Right] == null) { tmd.verticeIndices[(int)IndexOrder.Right] = new Tuple <Vector3, int>(rightCorner, vertexIndex++); } if (tmd.verticeIndices[(int)IndexOrder.BottomRight] == null) { tmd.verticeIndices[(int)IndexOrder.BottomRight] = new Tuple <Vector3, int>(bottomRightCorner, vertexIndex++); } if (tmd.verticeIndices[(int)IndexOrder.BottomLeft] == null) { tmd.verticeIndices[(int)IndexOrder.BottomLeft] = new Tuple <Vector3, int>(bottomLeftCorner, vertexIndex++); } if (tmd.verticeIndices[(int)IndexOrder.Left] == null) { tmd.verticeIndices[(int)IndexOrder.Left] = new Tuple <Vector3, int>(leftCorner, vertexIndex++); } tileVertexCodex[tile] = tmd; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.TopLeft].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.TopRight].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.BottomRight].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.TopRight].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.Right].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.BottomRight].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.BottomRight].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.BottomLeft].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.TopLeft].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.BottomLeft].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.Left].Item2; triangleIndices[triangleIndex++] = tmd.verticeIndices[(int)IndexOrder.TopLeft].Item2; } Vector3[] vertices = new Vector3[vertexIndex]; int mapX = noiseMap.GetLength(0); int mapY = noiseMap.GetLength(1); int i = 0; foreach (var kvp in tileVertexCodex) { TileMeshData tmd = kvp.Value; foreach (Tuple <Vector3, int> verts in tmd.verticeIndices) { if (vertices[verts.Item2] != Vector3.zero) { continue; // If the tilemap is positioned at 0,0 there should never be a vertex there } Vector3 pos = verts.Item1; float percentX = Mathf.InverseLerp(minX, maxX, pos.x); float percentY = Mathf.InverseLerp(minY, maxY, pos.z); int x = Mathf.RoundToInt(percentX * (mapX - 1)); int y = Mathf.RoundToInt(percentY * (mapY - 1)); vertices[verts.Item2] = pos + new Vector3(0, noiseMap[x, y] * 5, 0); ++i; } } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangleIndices; mesh.RecalculateBounds(); GetComponent <MeshFilter>().sharedMesh = mesh; Debug.Log("i: " + i + " vertices: " + vertices.Length + " triangles: " + (triangleIndices.Length / 3)); }