public void DecrementType(ProcData data, CellType type) { if (data.type != type) { switch (data.type) { case CellType.rest: --rests; break; case CellType.merchant: --merchants; break; case CellType.settlement: --settlements; break; case CellType.treasure: --treasure; break; default: break; } } }
/// <summary> /// Sets the data at position (x,y) to the given data /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="data"></param> public void SetData(int x, int y, ProcData data) { if (IsBounds(x, y) && tilemap[x, y] != null) { tilemap[x, y] = data; } }
void Connect(int x1, int y1, int x2, int y2, ProcData[,] map) { Vector2Int[] line = Line(x1, y1, x2, y2); foreach (Vector2Int point in line) { ProcData data = map[point.x, point.y]; data.type = CellType.empty; } }
void InstantiateHexes() { if (parent == null) { parent = this.gameObject; } mapBounds.width = 0; mapBounds.height = 0; mapBounds.x = islandDim; mapBounds.y = islandDim; for (int x = 0; x < islandDim; x++) { for (int y = 0; y < islandDim; y++) { ProcData data = tilemap[x, y]; if (data.type != CellType.nil) { mapBounds.width = Mathf.Max(HexWidth() * x, mapBounds.width); mapBounds.height = Mathf.Max(HexHeight() * y, mapBounds.height); mapBounds.x = Mathf.Min(mapBounds.x); mapBounds.y = Mathf.Min(mapBounds.y); Vector2 pos = HexCoordinate(x, y); data.obj = Instantiate(tile, new Vector3(0f, 0f, 0f), this.tile.transform.rotation, parent.transform); data.obj.transform.Translate(pos.x, 0f, pos.y); data.pos.x = x; data.pos.y = y; data.agent = this; HexTile hexTile = data.obj.AddComponent <HexTile>(); hexTile.data = data; hexTile.SetName(); ++tileCount; } } } if (!debug) { StaticBatchingUtility.Combine(parent); } }
public void MoveToCenter(ProcData data) { var agent = data.agent; agent.Vertex = agent.center; }
public void MoveToRandomCell(ProcData data) { ProcAgent agent = data.agent; agent.Vertex = agent.RandomPoint(); }
void SetRest(ProcData data) { //var agent = data.agent; data.type = CellType.rest; }
void SetTreasure(ProcData data) { }
void SetMerchant(ProcData data) { }
void SetSettlement(ProcData data) { }
void SetEmpty(ProcData data) { data.agent.DecrementType(data, CellType.empty); data.type = CellType.empty; }
public void LogType(ProcData data) { Debug.Log(string.Format("Cell is of type {0}", data.type)); }
public void LogData(ProcData data) { Debug.Log(string.Format("Agent moved to position ({0},{1})", data.pos.x, data.pos.y)); }
void GenerateGraph() { tileCount = 0; //Generate a hex map ProcData[,] map = new ProcData[2 * islandSize + 1, 2 * islandSize + 1]; islandDim = 2 * islandSize + 1; //Set the vector to the custom vector if the user has specified one. Vector2Int vCenter = new Vector2Int(); if (!customCenter) { vCenter = new Vector2Int(islandSize, islandSize); } else { vCenter = center; } //Debug.Log(vCenter.x + "," + vCenter.y); for (int x = 0; x < islandDim; x++) { for (int y = 0; y < islandDim; y++) { map[x, y] = new ProcData(CellType.nil); int d = Distance(x, y, vCenter.x, vCenter.y); //Find probability of the tile appearing in (x,y) float p = EvaluateProb(distribution, d); float q = Random.Range(0f, 1f); //Should we create a cell here? if (q < p) { map[x, y].type = CellType.empty; } } } //Place a cell in the center of the map map[vCenter.x, vCenter.y].type = CellType.empty; //Buffer to store cells to be removed after smooth pass List <Vector2Int> remove = new List <Vector2Int>(); //boolean hashmap used for constructing disjointed subgraphs bool[] visited = new bool[islandDim * islandDim]; //List of subgraphs List <List <Vector2Int> > subgraphs = new List <List <Vector2Int> >(); //Get rid of tiles without the minimal adjacent neighbours. for (int x = 0; x < islandDim; x++) { for (int y = 0; y < islandDim; y++) { int adjCount = 0; //Basis to be used to check cell neighbours int[] neighbours = Basis(y); //Iterate over the current cell's neighbours for (int i = 0; i < neighbours.Length; i += 2) { int xPos = x + neighbours[i]; int yPos = y + neighbours[i + 1]; if (xPos < 0 || xPos >= islandDim || yPos < 0 || yPos >= islandDim) { continue; } if (map[xPos, yPos].type == CellType.empty) { ++adjCount; } } //Add cells to a buffer to be removed later, removing them as the map is searched will modify their adjacent counts if (adjCount < minAdjacency) { remove.Add(new Vector2Int(x, y)); } //Fill any holes if specified if (map[x, y].type == CellType.nil && fillHoles && adjCount == 6) { map[x, y].type = CellType.empty; } } } foreach (Vector2Int pos in remove) { map[pos.x, pos.y].type = CellType.nil; } if (connectCells) { for (int x = 0; x < islandDim; x++) { for (int y = 0; y < islandDim; y++) { //Connect disjointed subgraphs if specified if (map[x, y].type == CellType.empty && !visited[hash(x, y)]) { subgraphs.Add(BFS(x, y, visited, map)); } } } //Connect subgraphs var graph = subgraphs[0]; for (int i = 1; i < subgraphs.Count; i++) { Debug.Log("Graph Count: " + subgraphs[i].Count); int r1 = Random.Range(0, graph.Count - 1); int r2 = Random.Range(0, subgraphs[i].Count - 1); Connect(graph[r1], subgraphs[i][r2], map); } } tilemap = map; }
//Causes the agent to traverse the graph by one step void TraverseGraph() { ++steps; List <Vector2Int> adjacent = Neighbours(vertex.x, vertex.y); int v = Random.Range(0, adjacent.Count - 1); if (adjacent.Count == 0) { vertex = RandomPoint(); } else { vertex = adjacent[v]; } ProcData data = tilemap[vertex.x, vertex.y]; HexTile tile = data.obj.GetComponent <HexTile>(); if (data.type == CellType.empty || overwriteCells) { int index = prob.pickIndex(TileDistributions()); /*if(index != 4) * { * onSet.Invoke(data); * }*/ switch (index) { case 0: if (rests < maxRests) { DecrementType(data, CellType.rest); data.type = CellType.rest; ++rests; tile.SetMaterial(); } break; case 1: if (merchants < maxMerchants) { DecrementType(data, CellType.merchant); data.type = CellType.merchant; ++merchants; tile.SetMaterial(); } break; case 2: if (settlements < maxSettlements) { DecrementType(data, CellType.settlement); data.type = CellType.settlement; ++settlements; tile.SetMaterial(); } break; case 3: if (treasure < maxTreasure) { DecrementType(data, CellType.treasure); data.type = CellType.treasure; ++treasure; tile.SetMaterial(); } break; case 4: break; } } //Agent events onMove.Invoke(data); if (adjacent.Count < 4) { onBoundary.Invoke(data); } if (vertex == center) { onCenter.Invoke(data); } if (Random.value <= 0.25f) { onRandom.Invoke(data); } if (steps < maxSteps) { TraverseGraph(); } else { steps = 0; } }