private void PickResourceSite(PointOfInterestTemplate poiTemplate, List <PointOfInterestDefinition> createdPOIs, List <HexPos> availableSpots) { bool flag = false; int num = 20; string propertyValue = poiTemplate.GetPropertyValue("ResourceName"); do { num--; HexPos hexPos = availableSpots.First <HexPos>(); availableSpots.RemoveAt(0); Region region = base.Context.Regions[base.Context.RegionData[hexPos.Row, hexPos.Column]]; PointOfInterestDefinition pointOfInterestDefinition = this.MakeNewPOI(hexPos, poiTemplate, 1); this.Influence.AddSpot(hexPos); if (pointOfInterestDefinition != null) { createdPOIs.Add(pointOfInterestDefinition); Dictionary <int, int> dictionary = this.CreatedSites[propertyValue]; int landMassIndex = region.LandMassIndex; int num2 = dictionary[landMassIndex]; dictionary[landMassIndex] = num2 + 1; region.Resources.Add(pointOfInterestDefinition); flag = true; } }while (!flag && num > 0 && availableSpots.Count > 0); }
private void CreateRidgesFromHexes() { while (this.ridgesHexes.Count > 0) { Ridge ridge = new Ridge(); ridge.Hexes.Add(this.ridgesHexes[0]); this.ridgesHexes.RemoveAt(0); Queue <HexPos> queue = new Queue <HexPos>(); queue.Enqueue(ridge.Hexes[0]); while (queue.Count > 0) { HexPos pos = queue.Dequeue(); foreach (HexPos item in base.Context.Grid.Adjacents(pos)) { int num = this.tmxPointOfInterestLayer.Data[item.Row, item.Column]; if (num != 0 && base.Context.Settings.TmxMap.GetTileProperty(num, ImportTmxPointOfInterestLayer.poiTypeNameProperty) == "Ridge" && !ridge.Hexes.Contains(item)) { ridge.Hexes.Add(item); this.ridgesHexes.Remove(item); queue.Enqueue(item); } } } base.Context.Ridges.Add(ridge); } }
private bool TryMoveTo(HexPos pos, int steps) { if (steps < enterCost) { return(false); } steps -= enterCost; if (this.IsAt(pos)) { return(true); } if (stopOnEnter) { return(false); } foreach (HexPath path in neighbours) { if (path.toNode.isRevealed && path.toNode.TryMoveTo(pos, steps)) { return(true); } } return(false); }
protected void Emit(List <HexPos> positions, HexPos anchor) { if (OnPossiblePlacement != null) { OnPossiblePlacement(positions, anchor); } }
public List <HexTileController> GetNeighbours(HexPos pos, int distance = 1) { var neighbours = new List <HexTileController>(); for (int x = -distance; x <= distance; x++) { for (int y = -distance; y <= distance; y++) { int dist = HexPos.GetDistance(0, 0, x, y); if (dist == 0 || dist > distance) { continue; } var neighbour = GetTile(new HexPos(pos.x + x, pos.y + y)); if (neighbour != null) { neighbours.Add(neighbour); } } } return(neighbours); }
public static List <HexPos> GetPath(HexPos start, HexPos end, HexCubMap map, Occupation criteria) { List <HexPos> path = new List <HexPos>(); List <List <HexPos> > floodFill = GetFloodFillUntil(start, end, map, criteria); int n = floodFill.Count; //If no end in sight return empty path if (n == 0 || !floodFill[n].Contains(end)) { return(path); } path.Add(end); n--; while (n > 0) { HexPos nextPos = FirstNeighbour(path[path.Count - 1], floodFill[n]); if (nextPos == null) { path.Clear(); return(path); } n--; } path.Add(start); path.Reverse(); return(path); }
public void SetPlayerPosition(HexPos pos) { if (player != null) { player.transform.position = GetPositionFor(pos) + Vector3.up * playerOffset; } }
public void RevealPosition(HexPos position, int distance) { foreach (HexTileController tile in GetNeighbours(position, distance)) { RevealPosition(tile.pos); } }
private string GetPointOfInterest_CustomRandomization(HexPos hexPos, string values) { List <string> list = values.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList <string>(); int num; if (!int.TryParse(base.Context.Settings.TmxMap.GetTileProperty(this.tmxPointOfInterestLayer.Data[hexPos.Row, hexPos.Column], "Group"), out num) || num < 0) { return(list[base.Context.Randomizer.Next(list.Count)].Trim()); } if (!this.customRandomizationGroups.ContainsKey(num)) { List <int> list2 = Enumerable.Range(0, list.Count).ToList <int>(); list2 = (from x in list2 orderby base.Context.Randomizer.Next() select x).ToList <int>(); this.customRandomizationGroups.Add(num, list2); } int index; int.TryParse(base.Context.Settings.TmxMap.GetTileProperty(this.tmxPointOfInterestLayer.Data[hexPos.Row, hexPos.Column], "Offset"), out index); int index2 = this.customRandomizationGroups[num][index] % list.Count; return(list[index2].Trim()); }
public static Vector3 GetHexWorldPos(HexPos pos) { Vector3 position = new Vector3(); position.x = (pos.X + pos.Z * 0.5f - pos.Z / 2) * (innerRadius * 2f); position.y = 0f; position.z = pos.Z * (outerRadius * 1.5f); return(position); }
public void SetPlayerStart(HexPos pos) { if (player == null) { player = FindObjectOfType <PlayerController>(); } SetPlayerPosition(pos); }
private void RevealPosition(HexPos hexPos) { var tile = GetTile(hexPos); if (tile != null) { tile.FlipTile(); } }
private void AssignRandomAnomalies_Unique() { Dictionary <HexPos, int> weights = new Dictionary <HexPos, int>(); this.randomAnomalyPositions.ForEach(delegate(HexPos h) { weights.Add(h, 0); }); WeightedRandomSelector <HexPos> weightedRandomSelector = new WeightedRandomSelector <HexPos> { Randomizer = base.Context.Randomizer }; foreach (string text in base.Context.Settings.UniqueAnomaliesQuantities.Keys) { int num = 0; if (base.Context.Anomalies.ContainsKey(text)) { num = base.Context.Anomalies[text].Count; } if (base.Context.Settings.UniqueAnomaliesQuantities[text] > num) { foreach (HexPos hexPos in this.randomAnomalyPositions) { string name = base.Context.GetTerrain(hexPos).Name; weights[hexPos] = 0; if (base.Context.Settings.AnomalyWeightsPerTerrain.ContainsKey(name) && base.Context.Settings.AnomalyWeightsPerTerrain[name].ContainsKey(text)) { weights[hexPos] = base.Context.Settings.AnomalyWeightsPerTerrain[name][text]; } } weightedRandomSelector.UseDictionary(weights); int num2 = base.Context.Settings.UniqueAnomaliesQuantities[text] - num; if (weightedRandomSelector.IsValid && num2 > 0) { for (int i = 0; i < num2; i++) { if (weightedRandomSelector.IsValid) { HexPos randomSelected = weightedRandomSelector.RandomSelected; weights[randomSelected] = 0; weightedRandomSelector.UseDictionary(weights); if (!base.Context.Anomalies.ContainsKey(text)) { base.Context.Anomalies.Add(text, new List <HexPos>()); } base.Context.Anomalies[text].Add(randomSelected); base.Context.AnomalyMap[randomSelected.Row, randomSelected.Column] = text; this.randomAnomalyPositions.Remove(randomSelected); } } } } } }
public static HexPos FirstNeighbour(HexPos refPos, List <HexPos> candidates) { for (int i = 0, l = candidates.Count; i < l; i++) { if (distance(refPos.cubePos, candidates[i].cubePos) == 1) { return(candidates[i]); } } return(null); }
HexTileController GetTile(HexPos hexPos) { HexTileController tileObject; if (tiles.TryGetValue(hexPos, out tileObject)) { return(tileObject); } return(null); }
public static IEnumerable <HexPos> GetNeighbours(Vector3 pos, HexCubMap map) { foreach (Vector3 dir in directions) { HexPos hex = map.GetHexPos(dir + pos); if (hex != null && hex.enabled) { yield return(hex); } } }
public static bool BordersRoot(HexPos pos, HexCubMap map) { foreach (HexPos neighbour in GetNeighbours(pos.cubePos, map)) { if (neighbour != null && !neighbour.isFree && neighbour.occupant.tileType == TileType.Root) { return(true); } } return(false); }
void SetupInitialField() { for (int i = 0; i < startPositions.Count; i++) { Tile tile = Instantiate(tilePrefab); tile.map = this; tile.SetType(startPositionTypes[i]); HexPos pos = GetHexPos(startPositions[i]); tile.Place(pos); } }
public UnitModel GetUnit(HexPos pos) { foreach (HexOccupier occupant in GetHex(pos).Occupants) { if (occupant is UnitModel) { return((UnitModel)occupant); } } return(null); }
public bool HasNeighbour(HexPos pos) { foreach (HexPath path in neighbours) { if (path.toNode.IsAt(pos)) { return(true); } } return(false); }
private void PlacementRule_OnPossiblePlacement(List <HexPos> positions, HexPos anchor) { if (anchor != null) { placements[anchor] = positions.ToArray(); } for (int i = 0, l = positions.Count; i < l; i++) { positions[i].acceptingTile = true; } }
public HexNode(HexPos pos, int enterCost = 1, bool stopOnEnter = false, bool isRevealed = false) { this.pos = pos; this.enterCost = enterCost; this.stopOnEnter = stopOnEnter; this.isRevealed = isRevealed; HexTileController.OnTileFlip += (tilePos) => { if (tilePos == this.pos) { Reveal(); } }; }
private void CreateNewRegion(HexPos hexe) { Tile tile = base.Context.Settings.TmxMap.GetTile(this.tmxRegionLayer.Data[hexe.Row, hexe.Column]); if (tile == null) { base.ReportTmx(string.Concat(new object[] { "?MissingTile&$Coordinate=", hexe.Column, ",", hexe.Row - base.Context.Grid.Rows + 1, "&$LayerName=Regions" })); this.anyErrorCatched = true; return; } short regionTileValue = (short)tile.Id; Region region = new Region(base.Context.Grid); region.Id = ImportTmxRegionLayer.id; ImportTmxRegionLayer.id += 1; string tileProperty = base.Context.Settings.TmxMap.GetTileProperty(this.tmxRegionLayer.Data[hexe.Row, hexe.Column], ImportTmxRegionLayer.regionNameProperty); if (!string.IsNullOrEmpty(tileProperty)) { region.Name = tileProperty; if (this.regionsGivenNames.Contains(tileProperty)) { base.ReportTmx("?RegionNameDuplication&$RegionName=" + tileProperty); } else { this.regionsGivenNames.Add(tileProperty); } } District district = new District(); district.MotherRegion = region; district.Id = (int)region.Id; district.Add(hexe); region.Districts.Add(district); this.treatedHexes[hexe.Row, hexe.Column] = true; base.Context.RegionData[hexe.Row, hexe.Column] = region.Id; base.Context.DistrictData[hexe.Row, hexe.Column] = district.Id; this.ExpandRegion(ref region, (int)regionTileValue); Diagnostics.Log(string.Format("[WorldGenerator] [ImportTmxRegionLayer] Region id is {0} it is composed of {1} hexe.", region.Id, district.Count)); base.Context.Regions.Add(region.Id, region); base.Context.Districts.Add(district.Id, district); }
public void SetPossibleMoveDistance(HexPos pos, int distance) { var tiles = mapController.GetNeighbours(pos, distance); mapController.ClearHighlights(); foreach (HexTileController tile in tiles) { var moveDist = GetMoveDistance(pos, tile.pos, forcedReveal: true); if (moveDist > 0 && moveDist <= distance) { tile.SetHighlighted(true); } } }
public static int minDistance(HexPos pos, List <HexPos> others) { int dist = -1; Vector3 a = pos.cubePos; for (int i = 0, l = others.Count; i < l; i++) { int curDist = distance(a, others[i].cubePos); if (dist < 0 || curDist < dist) { dist = curDist; } } return(dist); }
public bool CanReach(HexPos pos, int steps) { if (this.IsAt(pos)) { return(false); } foreach (HexPath path in neighbours) { if (path.toNode.isRevealed && path.toNode.TryMoveTo(pos, steps)) { return(true); } } return(false); }
public static void MoveUnit(UnitModel unit, HexPos newPos) { if (Model.GetUnit(newPos) != null && Model.GetUnit(newPos) != unit) { HandleCombat(unit, Model.GetUnit(newPos), Model.GetHex(newPos)); if (Model.GetUnit(newPos) == null) { MoveUnit(unit, newPos); } } else { Model.GetHex(unit.CurrentPos).Occupants.Remove(unit); Model.GetHex(newPos).Occupants.Add(unit); unit.InvokeUpdateUnitPos(newPos); } }
private void InstantiateHexOccupants(HexPos pos, List <HexOccupier> occupants) { foreach (HexOccupier occupant in occupants) { if (occupant is UnitModel) { UnitView newUnit = GameObject.Instantiate(UnitPrefab); occupant.CurrentPos = pos; newUnit.Model = (UnitModel)occupant; } if (occupant is PlanetModel) { PlanetView newPlanet = GameObject.Instantiate(PlanetPrefab); occupant.CurrentPos = pos; newPlanet.Model = (PlanetModel)occupant; } } }
public static List <List <HexPos> > GetFloodFillUntil(HexPos start, HexPos end, HexCubMap map, Occupation criteria) { HashSet <HexPos> visited = new HashSet <HexPos>(); List <List <HexPos> > fringes = new List <List <HexPos> >(); fringes.Add(new List <HexPos>() { start }); int k = 0; while (true) { fringes.Add(new List <HexPos> ()); k++; for (int i = 0, l = fringes[k - 1].Count; i < l; i++) { foreach (HexPos neighbour in GetNeighbours(fringes[k - 1][i].cubePos, map)) { if (visited.Contains(neighbour)) { continue; } visited.Add(neighbour); if (criteria == Occupation.Any || criteria == Occupation.Free && neighbour.isFree || criteria == Occupation.Occupied && !neighbour.isFree || neighbour == end) { fringes[k].Add(neighbour); } if (neighbour == end) { return(fringes); } } } if (fringes [k].Count == 0) { fringes.RemoveAt(k); return(fringes); } } }
public int GetMoveDistance(HexPos fromHexPos, HexPos toHexPos, bool forcedReveal = false) { HexNode fromNode, toNode; if (navigationNodes.TryGetValue(fromHexPos, out fromNode) && navigationNodes.TryGetValue(toHexPos, out toNode)) { if (toNode.enterCost >= 0 && (toNode.isRevealed || forcedReveal)) { var path = GetShortestPathAstar(fromNode, toNode); if (path.Count > 1) { path.RemoveAt(0); return(path.Aggregate(0, (sum, node) => sum + node.enterCost)); } } } return(-1); }