public void RecalculateTerrain(TriCell cell, bool buildable) { Vector3 nextCorner, prevCorner; EdgeVertices edge; for (TriDirection direction = TriDirection.VERT; direction <= TriDirection.RIGHT; direction++) { Vector3 center = cell.Position, v1, v2; //entityMenu.transform.localPosition = center + new Vector3(0, 20, 0); v1 = center + (cell.inverted ? -1 : 1) * TriMetrics.GetFirstSolidCorner(direction); v2 = center + (cell.inverted ? -1 : 1) * TriMetrics.GetSecondSolidCorner(direction); edge = new EdgeVertices(v1, v2); nextCorner = (center + edge.v1) / 2f; prevCorner = (center + edge.v5) / 2f; AddTriangle(nextCorner, edge.v1, edge.v2); AddTriangle(prevCorner, edge.v4, edge.v5); AddTriangle(center, edge.v2, edge.v4); AddTriangle(center, edge.v4, prevCorner); AddTriangle(center, nextCorner, edge.v2); Color c = buildable ? Color.blue : Color.red; AddTriangleColor(c); AddTriangleColor(c); AddTriangleColor(c); AddTriangleColor(c); AddTriangleColor(c); } }
public void AddUnit(Unit unit, TriCell location, float orientation) { units.Add(unit); unit.transform.SetParent(transform, false); unit.Location = location; unit.Orientation = orientation; }
public IEnumerator <Coroutine> FindPathAndMove(TriCell target, bool entityCheck, bool terrainCheck) { TriGrid inst = TriGrid.Instance; if (target && IsValidDestination(target)) { inst.FindPath(Location, target, entityCheck, terrainCheck); if (inst.HasPath) { pathToTravel = inst.GetPath(); CancelNowAct(); yield return(nowRoutine = StartCoroutine(TravelPath())); inst.ClearPath(); ActResult = true; } else { ActResult = false; } } else { inst.ClearPath(); ActResult = false; } yield return(null); }
public BuildCommand(TriCell location, TriDirection dir, Entity target) { this.location = location; this.dir = dir; this.target = target; type = CommandType.BUILD; }
void Triangulate(TriCell cell) { for (TriDirection d = TriDirection.VERT; d <= TriDirection.RIGHT; d++) { Triangulate(d, cell); } }
public void RecalculateTerrain(TriCell cell) { Vector3 nextCorner, prevCorner; EdgeVertices edge; for (TriDirection direction = TriDirection.VERT; direction <= TriDirection.RIGHT; direction++) { Vector3 center = cell.Position, v1, v2; v1 = center + (cell.inverted ? -1 : 1) * TriMetrics.GetFirstSolidCorner(direction); v2 = center + (cell.inverted ? -1 : 1) * TriMetrics.GetSecondSolidCorner(direction); edge = new EdgeVertices(v1, v2); nextCorner = (center + edge.v1) / 2f; prevCorner = (center + edge.v5) / 2f; terrainSelectionViewer.AddTriangle(nextCorner, edge.v1, edge.v2); terrainSelectionViewer.AddTriangle(prevCorner, edge.v4, edge.v5); terrainSelectionViewer.AddTriangle(center, edge.v2, edge.v4); terrainSelectionViewer.AddTriangle(center, edge.v4, prevCorner); terrainSelectionViewer.AddTriangle(center, nextCorner, edge.v2); terrainSelectionViewer.AddTriangleColor(Color.blue); terrainSelectionViewer.AddTriangleColor(Color.blue); terrainSelectionViewer.AddTriangleColor(Color.blue); terrainSelectionViewer.AddTriangleColor(Color.blue); terrainSelectionViewer.AddTriangleColor(Color.blue); } }
public static new Human Load(BinaryReader reader) { Human ret = Instantiate((Human)TriIsland.GetUnitPrefabs((int)UnitType.PERSON, 0)); TriCell tLoc = TriGrid.Instance.GetCell(TriCoordinates.Load(reader)); if (tLoc) { ret.Home = (Inn)tLoc.Statics; } else { (TriIsland.GetCamp() as Hall).Homeless.Add(ret); } tLoc = TriGrid.Instance.GetCell(TriCoordinates.Load(reader)); if (tLoc) { ret.Company = (Company)tLoc.Statics; } tLoc = TriGrid.Instance.GetCell(TriCoordinates.Load(reader)); if (tLoc) { ret.Work = tLoc.Statics; } return(ret); }
public void StartCalculateTerrain() { terrainSelectionViewer.Clear(); switch (terrainType) { case SizeType.HEX: TriCell k = selectedCell; int elev = selectedCell.Elevation; TriDirection tDir = dir.Previous(); for (int i = 0; i < 6; i++) { if (!k) { break; } RecalculateTerrain(k); k = k.GetNeighbor(tDir); tDir = tDir.Next(); } break; case SizeType.SINGLE: RecalculateTerrain(selectedCell); break; } terrainSelectionViewer.Apply(); }
IEnumerator <WaitForEndOfFrame> CreateRivers() { List <TriCell> riverOrigins = ListPool <TriCell> .Get(); for (int i = 0; i < cellCount; i++) { TriCell cell = grid.GetCell(i); if (cell.IsUnderwater) { continue; } ClimateData data = climate[i]; float weight = (float)(cell.Elevation) / (float)(elevationMaximum); //grid.labels[i].text = weight.ToString("F"); if (weight > 0.75f) { riverOrigins.Add(cell); riverOrigins.Add(cell); } if (weight > 0.5f) { riverOrigins.Add(cell); } if (weight > 0.25f) { riverOrigins.Add(cell); } } int riverBudget = Mathf.RoundToInt(landCells * riverPercentage * 0.01f); int overflowChecker = 0; while (riverBudget > 0 && riverOrigins.Count > 0) { if (overflowChecker++ > 1000) { Debug.Log("checking river stack overflowed"); break; } int index = Random.Range(0, riverOrigins.Count); int lastIndex = riverOrigins.Count - 1; TriCell origin = riverOrigins[index]; riverOrigins[index] = riverOrigins[lastIndex]; riverOrigins.RemoveAt(lastIndex); if (!origin.HasRiver) { riverBudget -= CreateRiver(origin); } yield return(null); } if (riverBudget > 0) { Debug.LogWarning("Failed to use up river budget."); } ListPool <TriCell> .Add(riverOrigins); }
public MoveCommand(TriCell location, bool entityCheck, bool terrainCheck, bool chaining = false) { type = CommandType.MOVE; this.location = location; this.entityCheck = entityCheck; this.terrainCheck = terrainCheck; Chaining = chaining; }
public static new BuildCommand Load(BinaryReader reader) { TriGrid instance = TriGrid.Instance; TriCell tCell = instance.GetCell(TriCoordinates.Load(reader)); TriDirection tDir = (TriDirection)reader.ReadInt32(); Entity prefab = TriIsland.GetBuildingPrefabs(reader.ReadInt32(), reader.ReadInt32(), 0); return(new BuildCommand(tCell, tDir, prefab)); }
public void FindPath(TriCell fromCell, TriCell toCell, bool entityCheck = true) { ClearLabel(); ClearPath(); currentPathFrom = fromCell; currentPathTo = toCell; currentPathExists = Search(fromCell, toCell, entityCheck); Debug.Log("pathFindResult : " + currentPathExists); }
void DestroyBuilding() { TriCell cell = GetCellUnderCursor(); if (cell && cell.Statics) { entities.RemoveBuilding(cell.Statics.ID); } }
void DestroyUnit() { TriCell cell = GetCellUnderCursor(); if (cell && cell.Statics) { entities.RemoveUnit(cell.Statics.ID); } }
public IEnumerator <Coroutine> LoadBuilding(string path) { ClearBuildings(); using (BinaryReader reader = new BinaryReader(File.OpenRead(Path.Combine(path, "building.dat")))) { int header = reader.ReadInt32(); if (header <= 0) { int counter = reader.ReadInt32(); for (int i = 0; i < counter; i++) { TriCoordinates coord = TriCoordinates.Load(reader); Building loaded = Building.Load(reader); if (loaded.type == BuildingType.HALL) { camp = loaded as Hall; } if (loaded) { loaded.ID = i; loaded.Location = grid.GetCell(coord); loaded.EntranceDirection = loaded.EntranceDirection; switch (loaded.sizeType) { case SizeType.SINGLE: loaded.Location.Statics = loaded; break; case SizeType.HEX: TriCell k = loaded.Location; TriDirection tDir = loaded.EntranceDirection.Previous(); for (int j = 0; j < 6; j++) { if (!k) { break; } k.Statics = loaded; k = k.GetNeighbor(tDir); tDir = tDir.Next(); } break; } loaded.transform.SetParent(BuildingGroup); AddBuilding(loaded); } // if (i % Strings.refreshLimit == 0) yield return null; } } else { Debug.LogWarning("Unknown building format " + header); } } yield return(null); }
public override List <BuildState> GetBuildStatus(TriCoordinates coord, TriDirection dir) { List <BuildState> ret = new List <BuildState>(); TriCell cell = TriGrid.Instance.GetCell(coord); int elev = cell.Elevation; ret.Add(new BuildState(coord, cell.IsBuildable())); cell = cell.GetNeighbor(dir); ret.Add(new BuildState(cell.coordinates, cell.IsBuildable() && Mathf.Abs(elev - cell.Elevation) < 2)); return(ret); }
void AddCellToChunk(int x, int z, TriCell cell) { int chunkX = x / TriMetrics.chunkSizeX; int chunkZ = z / TriMetrics.chunkSizeZ; TriGridChunk chunk = chunks[chunkX + chunkZ * chunkCountX]; int localX = x - chunkX * TriMetrics.chunkSizeX; int localZ = z - chunkZ * TriMetrics.chunkSizeZ; chunk.AddCell(localX + localZ * TriMetrics.chunkSizeX, cell); }
public void SetRiver(TriDirection direction) { if (!isRiver[(int)direction]) { isRiver[(int)direction] = true; RefreshSelfOnly(); TriCell neighbor = GetNeighbor(direction); neighbor.isRiver[(int)direction] = true; neighbor.RefreshSelfOnly(); } }
bool UpdateCurrentCell() { TriCell cell = grid.GetCell(Camera.main.ScreenPointToRay(Input.mousePosition)); if (cell != currentCell) { currentCell = cell; return(true); } return(false); }
public virtual List <BuildState> GetBuildStatus(TriCoordinates coord, TriDirection dir) { List <BuildState> ret = new List <BuildState>(); TriGrid grid = TriGrid.Instance; TriCell cell = grid.GetCell(coord); int elev = cell.Elevation; ret.Add(new BuildState(cell.coordinates, cell.IsBuildable())); cell = grid.GetCell(coord).GetNeighbor(dir); ret.Add(new BuildState(cell.coordinates, cell.IsBuildable() && Mathf.Abs(cell.Elevation - elev) < 2)); return(ret); }
public void RemoveRiver(TriDirection direction) { if (isRiver[(int)direction]) { isRiver[(int)direction] = false; RefreshSelfOnly(); TriCell neighbor = GetNeighbor(direction); neighbor.isRiver[(int)direction] = false; neighbor.RefreshSelfOnly(); } }
public Building CreateBuilding(TriDirection dir, TriCell cell, Building prefab) { List <BuildState> result = prefab.GetBuildStatus(cell.coordinates, dir); bool buildable = true; foreach (BuildState i in result) { if (!i.value) { buildable = false; } } if (cell && buildable) { Building ret = Instantiate(prefab); ret.ID = entities.BuildingCount; ret.Location = cell; switch (ret.sizeType) { case SizeType.SINGLE: cell.Statics = ret; break; case SizeType.HEX: TriCell k = cell; TriDirection tDir = dir.Previous(); for (int i = 0; i < 6; i++) { if (!k) { break; } k.Statics = ret; k = k.GetNeighbor(tDir); tDir = tDir.Next(); } break; } ret.EntranceDirection = dir; ret.personList = personList; entities.AddBuilding(ret); island.ShowSummary = island.ShowSummary; Debug.Log(ret + " built"); return(ret); } else { Debug.Log("building failed"); return(null); } }
public void ClearPath() { if (currentPathExists) { TriCell current = currentPathTo; while (current != currentPathFrom) { current = current.PathFrom; } currentPathExists = false; searchFrontier.Clear(); } currentPathFrom = currentPathTo = null; }
public TriCell Dequeue() { count--; for (; minimum < list.Count; minimum++) { TriCell cell = list[minimum]; if (cell != null) { list[minimum] = cell.NextWithSamePriority; return(cell); } } return(null); }
public void ShowDrag() { DragArrow.gameObject.SetActive(true); tCell = GetRay(); if (tCell == null) { return; } transform.localRotation = Quaternion.LookRotation(tCell.Position - dragStart.Location.Position); Vector3 pos = ((dragStart.transform.position + tCell.Position) / 2); pos.y = 20f; transform.position = pos; DragArrow.size = new Vector2(DragArrow.size.x, Vector3.Magnitude(dragStart.Location.Position - tCell.Position)); }
public void FindPath(TriCell fromCell, TriCell toCell, bool entityCheck, bool terrainCheck) { ClearPath(); currentPathFrom = fromCell; currentPathTo = toCell; currentPathExists = Search(fromCell, toCell, entityCheck, terrainCheck); if (currentPathExists) { Debug.Log("pathFindResult : " + currentPathExists); } else { Debug.LogWarning("pathFindResult : " + currentPathExists); } }
void EvolveClimate(int cellIndex) { TriCell cell = grid.GetCell(cellIndex); ClimateData cellClimate = climate[cellIndex]; float tMoisture = cellClimate.moisture, tClouds = cellClimate.clouds; if (cell.IsUnderwater) { tMoisture = 1f; tClouds += evaporationFactor; } else { float evaporation = tMoisture * evaporationFactor; tMoisture -= evaporation; tClouds += evaporation; } float precipitation = tClouds * precipitationFactor; tClouds -= precipitation; tMoisture += precipitation; //SetClimateData(cellIndex, tMoisture, tClouds); float cloudDispersal = tClouds * (1f / 6f); float runoff = tMoisture * runoffFactor * (1f / 6f); for (int i = 0; i < 4; i++) { TriCell neighbor = grid.GetCell(TriMetrics.TriToHex(cell.coordinates) + new Vector2Int(TriMetrics.hexDir[i, 0], TriMetrics.hexDir[i, 1])); if (!neighbor) { continue; } ClimateData neighborClimate = climate[neighbor.Index]; float tNMoisture = neighborClimate.moisture, tNClouds = neighborClimate.clouds; tNClouds += cloudDispersal; int elevationDelta = neighbor.Elevation - cell.Elevation; if (elevationDelta < 0) { tMoisture -= runoff; tNMoisture += runoff; } SetClimateData(neighbor.Index, tNMoisture, tNClouds); //climate[neighbor.Index] = neighborClimate; } tClouds = 0f; SetClimateData(cellIndex, tMoisture, tClouds); //climate[cellIndex] = cellClimate; }
int CreateRiver(TriCell origin) { int length = 0; TriCell cell = origin; TriDirection direction = TriDirection.VERT; while (!cell.IsUnderwater) { flowDirections.Clear(); for (TriDirection d = TriDirection.VERT; d <= TriDirection.RIGHT; d++) { TriCell neighbor = cell.GetNeighbor(d); if (!neighbor || neighbor.HasRiver) { continue; } int delta = neighbor.Elevation - cell.Elevation; if (delta > 0) { continue; } if (delta < 0) { flowDirections.Add(d); flowDirections.Add(d); flowDirections.Add(d); } if ( length == 1 || (d != direction.Next2() && d != direction.Previous2()) ) { flowDirections.Add(d); } flowDirections.Add(d); } if (flowDirections.Count == 0) { return(length > 1 ? length : 0); } direction = flowDirections[Random.Range(0, flowDirections.Count)]; cell.SetRiver(direction); cell.GetNeighbor(direction).SetRiver(direction); length += 1; cell = cell.GetNeighbor(direction); } return(length); }
public List <TriCell> GetPath() { if (!currentPathExists) { return(null); } List <TriCell> path = ListPool <TriCell> .Get(); for (TriCell c = currentPathTo; c != currentPathFrom; c = c.PathFrom) { path.Add(c); } path.Add(currentPathFrom); path.Reverse(); return(path); }
public void Enqueue(TriCell cell) { count++; int priority = cell.SearchPriority; if (priority < minimum) { minimum = priority; } while (priority >= list.Count) { list.Add(null); } cell.NextWithSamePriority = list[priority]; list[priority] = cell; }