public override State LoadGame(BoardData boardData) { const string fileName = "savedata.json"; string pathToLoadFrom = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + fileName; if (boardData.GetType().GetProperties().Any(property => property == null)) { throw new SaveDataCorruptedException("Save data in " + pathToLoadFrom + " + is corrupted."); } if (boardData.GridStones.Length != _size * _size || boardData.GridGroups.Length != _size * _size || boardData.ActivePlayer != State.White && boardData.ActivePlayer != State.Black) { throw new SaveDataCorruptedException("Save data in " + pathToLoadFrom + " + is corrupted."); } // Copy the values into this object _gridStones = boardData.GridStones; _gridGroups = boardData.GridGroups; _groups = boardData.Groups; _capWhite = boardData.CapWhite; _capBlack = boardData.CapBlack; _size = boardData.Size; _nextId = boardData.NextId; _borCor = boardData.BorCor; return(boardData.ActivePlayer); }
public void printGridToConsole() { Coord[,] grid = GameGrid; Console.WriteLine(); Console.WriteLine("Row={0}", rowSize); Console.WriteLine("Column={0}", columnSize); Console.WriteLine(); for (int y = 0; y < rowSize; y++) { for (int x = 0; x < columnSize; x++) { if (grid[y, x].X == true && grid[y, x].Y == true) { Console.Write("B"); } else if (grid[y, x].X == true && grid[y, x].Y == false) { Console.Write("X"); } else if (grid[y, x].X == false && grid[y, x].Y == true) { Console.Write("Y"); } else { Console.Write("-"); } } Console.WriteLine(); } }
private bool TooCloseToOtherPoints(Coord coord, Coord[,] grid, float cellSize) { // get the position on the grid Coord gridCoord = SpaceToPoissonGrid(coord, cellSize); // check adjacent squares for (int x = gridCoord.x - 1; x < gridCoord.x + 1; x++) { for (int y = gridCoord.y - 1; y < gridCoord.y + 1; y++) { if (x < 0 || x >= grid.GetLength(0) || y < 0 || y >= grid.GetLength(1)) { continue; } Coord cell = grid [x, y]; if (cell != null) { if (coord.Distance(cell) < minDistBetweenPoints) { return(true); } } } } return(false); }
public Navigation(Map contolMap) { map = contolMap; width = map.Width; height = map.Height; cameFromCoord = new Coord[width, height]; distanceToId = new int[width, height]; ClearDistances(); }
public UHISimulation(ISimulationMap map) { mapSize = map.GetMapSize(); sunlight = new Sunlight(); sunlight.SetSunPosition(13); coordsMap = new Coord[mapSize, mapSize]; coordsList = new List <Coord>(); this.map = map; }
void Bfs(ref Coord[,] prev, ref bool[,] used, ref Queue <Coord> order, ref int[,] distanse, ref string[,] side) { Coord p = order.Dequeue(); int i = p.x; int j = p.y; used[i, j] = true; Coord q; q = Base.Right(p); q = Base.Teleporting(q); if (!used[q.x, q.y] && q.x < n && q.x >= 0 && q.y < n && q.y >= 0) { order.Enqueue(q); prev[q.x, q.y] = p; distanse[q.x, q.y] = distanse[i, j] + 1; side[q.x, q.y] = "right"; } q = Base.Up(p); q = Base.Teleporting(q); if (!used[q.x, q.y] && q.x < n && q.x >= 0 && q.y < n && q.y >= 0) { order.Enqueue(q); prev[q.x, q.y] = p; distanse[q.x, q.y] = distanse[i, j] + 1; side[q.x, q.y] = "up"; } q = Base.Left(p); q = Base.Teleporting(q); if (!used[q.x, q.y] && q.x < n && q.x >= 0 && q.y < n && q.y >= 0) { order.Enqueue(q); prev[q.x, q.y] = p; distanse[q.x, q.y] = distanse[i, j] + 1; side[q.x, q.y] = "left"; } q = Base.Down(p); q = Base.Teleporting(q); if (!used[q.x, q.y] && q.x < n && q.x >= 0 && q.y < n && q.y >= 0) { order.Enqueue(q); prev[q.x, q.y] = p; distanse[q.x, q.y] = distanse[i, j] + 1; side[q.x, q.y] = "down"; } if (order.Count > 0) { Bfs(ref prev, ref used, ref order, ref distanse, ref side); } }
private void GenerateFreeCoords() { coords = new Coord[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { coords[i, j] = new Coord(i, j, FieldType.FREE); } } }
DoublyLinkedList <string> BuildWay(Coord start, Coord finish, ref Coord[,] prev, ref string[,] side) { DoublyLinkedList <string> answer = new DoublyLinkedList <string>(); if (start != finish) { answer = BuildWay(prev[start.x, start.y], finish, ref prev, ref side); answer.Push_front(side[start.x, start.y]); } return(answer); }
public CoordsData(Field field) { Width = field.Width; Height = field.Height; Data = new Coord[field.Height, field.Width]; field.OnMove += SetVal; field.OnReset += GenData; GenData(field); }
public BoardData(State[,] gridStones, int[,] gridGroups, Dictionary <int, Group> groups, Coord[,] borCor, int capWhite, int capBlack, int size, int nextId, State activePlayer, DateTime date) { GridStones = gridStones; GridGroups = gridGroups; Groups = groups; CapWhite = capWhite; CapBlack = capBlack; Size = size; NextId = nextId; ActivePlayer = activePlayer; BorCor = borCor; Date = date; }
public void removeWord(WordData word, int x, int y, int direction) { Coord[,] grid = GameGrid; for (int i = 0; i < word.Length; i++) { if (direction == Constants.HORIZONTAL_WORD) { grid[y, x + i].X = false; } else { grid[y + i, x].Y = false; } } }
Edge[,] edges; // width-1, height-1 public Grid(int width, int height) { this.width = width; this.height = height; coords = new Coord[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { coords[x, y] = new Coord(x, y); } } // make horizontal edges // make vertical edges // look them up by the set of coords }
public MazeGraph(Maze m) { adjVertices = new Dictionary <Coord, List <Coord> >(); Coord c = m.getDimensions(); coords = new Coord[c.x + 1, c.y + 1]; for (int i = 0; i <= c.x; i++) { for (int j = 0; j <= c.y; j++) { Coord next = new Coord(i, j); coords[i, j] = next; List <Coord> l = new List <Coord>(); adjVertices.Add(next, l); } } BuildMazeGraph(m.getPaths()); }
//Check each word in the grid has a valid number of intersections. //This method can be easily adapted to suit your needs. public bool checkGridIntersections(WordData word, int startPointX, int startPointY, int direction) { //Add the word to the crozzle map. crozzleMap.addWord(word, startPointX, startPointY, direction); Coord[,] grid = crozzleMap.GameGrid; int maxIntersetions = 2; bool isValid = true; foreach (WordData placedWord in wordsInGrid) { int intersectionCount = 0; for (int i = 0; i < placedWord.Length; i++) { if (placedWord.Direction == Constants.HORIZONTAL_WORD) { //Check if X and Y is true for the coordinate, if both true an intersection exists. if (grid[placedWord.Y, placedWord.X + i].X && grid[placedWord.Y, placedWord.X + i].Y) { //Increment the intersection count. intersectionCount++; } } else if (placedWord.Direction == Constants.VERTICAL_WORD) { if (grid[placedWord.Y + i, placedWord.X].X && grid[placedWord.Y + i, placedWord.X].Y) { intersectionCount++; } } } //Check if there is a valid number of intersections. if (intersectionCount > maxIntersetions) { isValid = false; } } //Remove the word from the tracking grid, it will be officially added when the word is added to the grid later. crozzleMap.removeWord(word, startPointX, startPointY, direction); return(isValid); }
private int _size; // The size of the board public Board(int size) { _size = size; _capBlack = 0; _capWhite = 0; _nextId = 1; _gridStones = new State[_size, _size]; _gridGroups = new int[_size, _size]; _groups = new Dictionary <int, Group>(); _borCor = new Coord[size, size]; _markedGroups = new HashSet <int>(); for (var i = 0; i < size; i++) { for (var j = 0; j < size; j++) { _borCor[j, i] = new Coord(j, i); } } }
/// <summary> /// Creates set of coordinates that this 4x4 block will spawn at /// </summary> /// <param name="_spawnPosition">Transform position of the Point</param> new public static void CreateSpawnCoordinates(Vector2 _spawnPosition) { spawnCoordinates = new Coord[4, 4]; //The block's pivot brick (middle brick) will centre on spawnpostion Coord _pivotSquareCoordinate = new Coord(_spawnPosition); //calculate bottom left (bL) coOrdinate of block int _bLx = _pivotSquareCoordinate.x - 2, _bLy = _pivotSquareCoordinate.y - 2; //itterate through block and create CoOrdinate based on its relative postion to bL for (int _x = 0; _x < 4; _x++) { for (int _y = 0; _y < 4; _y++) { spawnCoordinates[_x, _y] = new Coord(_bLx + _x, _bLy + _y); } } spawnPosition = _spawnPosition; }
// Use this for initialization public virtual void Spawn() { switch (blockType) { case BlockType.J: blockStructure = BlockData.JBlockStructures; break; case BlockType.L: blockStructure = BlockData.LBlockStructures; break; case BlockType.S: blockStructure = BlockData.SBlockStructures; break; case BlockType.T: blockStructure = BlockData.TBlockStructures; break; case BlockType.Z: blockStructure = BlockData.ZBlockStructures; break; default: blockStructure = BlockData.ZBlockStructures; break; } blockCoordinates = spawnCoordinates.Clone() as Coord[, ]; blockStructureSize = 3; rotationState = 0; wallKickTests = BlockData.WallKickData; blockTransform = transform; blockTransform.localPosition = spawnPosition; }
/// <summary> /// Creates set of coordinates that all 3x3 blocks will spawn at /// </summary> /// <param name="_spawnPosition">Transform position of the Point</param> public static void CreateSpawnCoordinates(Vector2 _spawnPosition) { spawnCoordinates = new Coord[3, 3]; //The block's pivot brick (middle brick) will centre on spawnpostion Coord _pivotSquareCoordinate = new Coord(_spawnPosition); //calculate bottom left (bL) coOrdinate of block int _bLx = _pivotSquareCoordinate.x - 1, _bLy = _pivotSquareCoordinate.y - 1; //itterate through block and create CoOrdinate based on its relative postion to bL for (int _x = 0; _x < 3; _x++) { for (int _y = 0; _y < 3; _y++) { spawnCoordinates[_x, _y] = new Coord(_bLx + _x, _bLy + _y); } } //as 3x3 blocks contain an odd number of blocks, we need to offset slightly to align to grid spawnPosition = (_spawnPosition += new Vector2(5f, 5f)); }
internal void GenerateMaps() { this.mBedMap = new Coord[this.Model.MapSizeX, this.Model.MapSizeY]; this.double_0 = new double[this.Model.MapSizeX, this.Model.MapSizeY]; this.byte_2 = new byte[this.Model.MapSizeX, this.Model.MapSizeY]; this.byte_1 = new byte[this.Model.MapSizeX, this.Model.MapSizeY]; this.byte_0 = new byte[this.Model.MapSizeX, this.Model.MapSizeY]; this.double_1 = new double[this.Model.MapSizeX, this.Model.MapSizeY]; this.double_2 = new double[this.Model.MapSizeX, this.Model.MapSizeY]; for (int i = 0; i < this.Model.MapSizeY; i++) { for (int j = 0; j < this.Model.MapSizeX; j++) { this.double_0[j, i] = 0.0; this.byte_0[j, i] = 0; this.byte_2[j, i] = 0; this.byte_1[j, i] = 0; this.mBedMap[j, i] = new Coord(j, i); if (j == this.Model.DoorX && i == this.Model.DoorY) { this.byte_0[j, i] = 3; } else { if (this.Model.SqState[j, i] == SquareState.OPEN) { this.byte_0[j, i] = 1; } else { if (this.Model.SqState[j, i] == SquareState.SEAT) { this.byte_0[j, i] = 3; } } } } } foreach (RoomItem @class in this.Hashtable_0.Values) { try { if (@class.GetBaseItem().Type == 's') { if (@class.GetX >= this.Model.MapSizeX || @class.GetY >= this.Model.MapSizeY || @class.GetY < 0 || @class.GetX < 0) { this.RemoveFurniture(null, @class.Id, true, false); GameClient class2 = PhoenixEnvironment.GetGame().GetClientManager().GetClientByHabbo(this.Owner); if (class2 != null) { class2.GetHabbo().GetInventoryComponent().AddItem(@class.Id, @class.BaseItem, @class.ExtraData, true); } } else { if (@class.TotalHeight > this.double_1[@class.GetX, @class.GetY]) { this.double_1[@class.GetX, @class.GetY] = @class.TotalHeight; } if (@class.GetBaseItem().IsSeat) { this.double_2[@class.GetX, @class.GetY] = @class.TotalHeight; } if (@class.GetBaseItem().Height > 0.0 || @class.GetBaseItem().EffectF != 0 || @class.GetBaseItem().EffectM != 0 || @class.GetBaseItem().IsSeat || !(@class.GetBaseItem().InteractionType.ToLower() != "bed")) { if (this.double_0[@class.GetX, @class.GetY] <= @class.GetZ) { this.double_0[@class.GetX, @class.GetY] = @class.GetZ; if (@class.GetBaseItem().EffectF > 0) { this.byte_2[@class.GetX, @class.GetY] = @class.GetBaseItem().EffectF; } else { if (this.byte_1[@class.GetX, @class.GetY] != 0) { this.byte_2[@class.GetX, @class.GetY] = 0; } } if (@class.GetBaseItem().EffectM > 0) { this.byte_1[@class.GetX, @class.GetY] = @class.GetBaseItem().EffectM; } else { if (this.byte_1[@class.GetX, @class.GetY] != 0) { this.byte_1[@class.GetX, @class.GetY] = 0; } } if (@class.GetBaseItem().Walkable) { if (this.byte_0[@class.GetX, @class.GetY] != 3) { this.byte_0[@class.GetX, @class.GetY] = 1; } } else { if (@class.GetZ <= this.Model.SqFloorHeight[@class.GetX, @class.GetY] + 0.1 && @class.GetBaseItem().InteractionType.ToLower() == "gate" && @class.ExtraData == "1") { if (this.byte_0[@class.GetX, @class.GetY] != 3) { this.byte_0[@class.GetX, @class.GetY] = 1; } } else { if (@class.GetBaseItem().IsSeat || @class.GetBaseItem().InteractionType.ToLower() == "bed") { this.byte_0[@class.GetX, @class.GetY] = 3; } else { if (this.byte_0[@class.GetX, @class.GetY] != 3) { this.byte_0[@class.GetX, @class.GetY] = 0; } } } } } if (@class.GetBaseItem().IsSeat || @class.GetBaseItem().InteractionType.ToLower() == "bed") { this.byte_0[@class.GetX, @class.GetY] = 3; } Dictionary<int, AffectedTile> dictionary = @class.GetAffectedTiles; if (dictionary == null) { dictionary = new Dictionary<int, AffectedTile>(); } foreach (AffectedTile current in dictionary.Values) { if (@class.TotalHeight > this.double_1[current.X, current.Y]) { this.double_1[current.X, current.Y] = @class.TotalHeight; } if (@class.GetBaseItem().IsSeat) { this.double_2[current.X, current.Y] = @class.TotalHeight; } if (this.double_0[current.X, current.Y] <= @class.GetZ) { this.double_0[current.X, current.Y] = @class.GetZ; if (@class.GetBaseItem().EffectF > 0) { this.byte_2[current.X, current.Y] = @class.GetBaseItem().EffectF; } else { if (this.byte_1[current.X, current.Y] != 0) { this.byte_2[current.X, current.Y] = 0; } } if (@class.GetBaseItem().EffectM > 0) { this.byte_1[current.X, current.Y] = @class.GetBaseItem().EffectM; } else { if (this.byte_1[current.X, current.Y] != 0) { this.byte_1[current.X, current.Y] = 0; } else { if (@class.GetBaseItem().Walkable) { if (this.byte_0[current.X, current.Y] != 3) { this.byte_0[current.X, current.Y] = 1; } } else { if (@class.GetZ <= this.Model.SqFloorHeight[@class.GetX, @class.GetY] + 0.1 && @class.GetBaseItem().InteractionType.ToLower() == "gate" && @class.ExtraData == "1") { if (this.byte_0[current.X, current.Y] != 3) { this.byte_0[current.X, current.Y] = 1; } } else { if (@class.GetBaseItem().IsSeat || @class.GetBaseItem().InteractionType.ToLower() == "bed") { this.byte_0[current.X, current.Y] = 3; } else { if (this.byte_0[current.X, current.Y] != 3) { this.byte_0[current.X, current.Y] = 0; } } } } } } } if (@class.GetBaseItem().IsSeat || @class.GetBaseItem().InteractionType.ToLower() == "bed") { this.byte_0[current.X, current.Y] = 3; } if (@class.GetBaseItem().InteractionType.ToLower() == "bed") { this.byte_0[current.X, current.Y] = 3; if (@class.Rot == 0 || @class.Rot == 4) { this.mBedMap[current.X, current.Y].Y = @class.GetY; } if (@class.Rot == 2 || @class.Rot == 6) { this.mBedMap[current.X, current.Y].X = @class.GetX; } } } } } } } catch { this.RemoveFurniture(null, @class.Id, true, false); GameClient class2 = PhoenixEnvironment.GetGame().GetClientManager().GetClientByHabbo(this.Owner); if (class2 != null) { class2.GetHabbo().GetInventoryComponent().AddItem(@class.Id, @class.BaseItem, @class.ExtraData, true); } } } if (!this.AllowWalkthrough) { for (int k = 0; k < this.UserList.Length; k++) { RoomUser class3 = this.UserList[k]; if (class3 != null) { this.byte_0[class3.X, class3.Y] = 0; } } } this.byte_0[this.Model.DoorX, this.Model.DoorY] = 3; }
public static Bitmap GenerateBitmap(int xd, int yd, int frame, double zoom, double Xo, double Yo, Coord[,] datas, bool lg) { Bitmap Frm = new Bitmap(xd, yd); for (int Part = 0; Part < datas.GetLength(1); Part++) { double xa = (zoom * (datas[frame, Part].X)); int Xaff; double ya = (zoom * (datas[frame, Part].Y)); int Yaff; double za = zoom * datas[frame, Part].Z; double Zaff; if (lg) { double dist = Math.Sqrt(xa * xa + ya * ya); double LogDist = Math.Log10(1.0 + dist); double Teta = Math.Sign(ya) * Math.Acos(xa / dist); Xaff = (int)(LogDist * Math.Cos(Teta) - Xo); Yaff = yd - (int)(LogDist * Math.Sin(Teta) - Yo); Zaff = 1.0 / (1.0 + Math.Exp(za / 10)); } else { Xaff = (int)(xa - Xo); Yaff = yd - (int)(ya - Yo); Zaff = 1.0 / (1.0 + Math.Exp(za)); } if (((Xaff >= 0) && (Xaff < xd)) && ((Yaff >= 0) && (Yaff < yd))) { Frm.SetPixel(Xaff, Yaff, Color.FromArgb((int)(100 + 154.0 * Zaff), 0, 0)); } } return(Frm); }
public void GenerateMaps() { // Create matrix arrays Matrix = new MatrixState[Model.MapSizeX, Model.MapSizeY]; BedMatrix = new Coord[Model.MapSizeX, Model.MapSizeY]; HeightMatrix = new double[Model.MapSizeX, Model.MapSizeY]; TopStackHeight = new double[Model.MapSizeX, Model.MapSizeY]; // Fill in the basic data based purely on the heightmap for (int line = 0; line < Model.MapSizeY; line++) { for (int chr = 0; chr < Model.MapSizeX; chr++) { Matrix[chr, line] = MatrixState.BLOCKED; BedMatrix[chr, line] = new Coord(chr, line); HeightMatrix[chr, line] = 0; TopStackHeight[chr, line] = 0.0; if (chr == Model.DoorX && line == Model.DoorY) { Matrix[chr, line] = MatrixState.WALKABLE_LASTSTEP; } else if (Model.SqState[chr, line] == SquareState.OPEN) { Matrix[chr, line] = MatrixState.WALKABLE; } else if (Model.SqState[chr, line] == SquareState.SEAT) { Matrix[chr, line] = MatrixState.WALKABLE_LASTSTEP; } } } // Loop through the items in the room foreach (RoomItem Item in Items) { // If we're dealing with anything other than a floor item, skip if (Item.GetBaseItem().Type.ToLower() != "s") { continue; } // If this is a rug, ignore it. if (Item.GetBaseItem().Height <= 0) { continue; } // Make sure we're the highest item here! if (TopStackHeight[Item.X, Item.Y] <= Item.Z) { TopStackHeight[Item.X, Item.Y] = Item.Z; // If this item is walkable and on the floor, allow users to walk here. if (Item.GetBaseItem().Walkable) { Matrix[Item.X, Item.Y] = MatrixState.WALKABLE; HeightMatrix[Item.X, Item.Y] = Item.GetBaseItem().Height; } // If this item is a gate, open, and on the floor, allow users to walk here. else if (Item.Z <= (Model.SqFloorHeight[Item.X, Item.Y] + 0.1) && Item.GetBaseItem().InteractionType.ToLower() == "gate" && Item.ExtraData == "1") { Matrix[Item.X, Item.Y] = MatrixState.WALKABLE; } // If this item is a set or a bed, make it's square walkable (but only if last step) else if (Item.GetBaseItem().IsSeat || Item.GetBaseItem().InteractionType.ToLower() == "bed") { Matrix[Item.X, Item.Y] = MatrixState.WALKABLE_LASTSTEP; } // Finally, if it's none of those, block the square. else { Matrix[Item.X, Item.Y] = MatrixState.BLOCKED; } } Dictionary<int, AffectedTile> Points = GetAffectedTiles(Item.GetBaseItem().Length, Item.GetBaseItem().Width, Item.X, Item.Y, Item.Rot); if (Points == null) { Points = new Dictionary<int, AffectedTile>(); } foreach (AffectedTile Tile in Points.Values) { // Make sure we're the highest item here! if (TopStackHeight[Tile.X, Tile.Y] <= Item.Z) { TopStackHeight[Tile.X, Tile.Y] = Item.Z; // If this item is walkable and on the floor, allow users to walk here. if (Item.GetBaseItem().Walkable) { Matrix[Tile.X, Tile.Y] = MatrixState.WALKABLE; HeightMatrix[Tile.X, Tile.Y] = Item.GetBaseItem().Height; } // If this item is a gate, open, and on the floor, allow users to walk here. else if (Item.Z <= (Model.SqFloorHeight[Item.X, Item.Y] + 0.1) && Item.GetBaseItem().InteractionType.ToLower() == "gate" && Item.ExtraData == "1") { Matrix[Tile.X, Tile.Y] = MatrixState.WALKABLE; } // If this item is a set or a bed, make it's square walkable (but only if last step) else if (Item.GetBaseItem().IsSeat || Item.GetBaseItem().InteractionType.ToLower() == "bed") { Matrix[Tile.X, Tile.Y] = MatrixState.WALKABLE_LASTSTEP; } // Finally, if it's none of those, block the square. else { Matrix[Tile.X, Tile.Y] = MatrixState.BLOCKED; } } // Set bad maps if (Item.GetBaseItem().InteractionType.ToLower() == "bed") { if (Item.Rot == 0 || Item.Rot == 4) { BedMatrix[Tile.X, Tile.Y].y = Item.Y; } if (Item.Rot == 2 || Item.Rot == 6) { BedMatrix[Tile.X, Tile.Y].x = Item.X; } } } } }
// Construtor public Map(Channel Channel) { this.Channel = Channel; this.Coords = new Coord [4096, 4096]; }
// Call terrain generator and cache useful info void Init() { var sw = System.Diagnostics.Stopwatch.StartNew(); var terrainGenerator = FindObjectOfType <TerrainGenerator> (); terrainData = terrainGenerator.Generate(); tileCentres = terrainData.tileCentres; walkable = terrainData.walkable; size = terrainData.size; SpawnTrees(); walkableNeighboursMap = new Coord[size, size][]; preyMap = new Map(size, 10); plantMap = new Map(size, 10); // Find and store all walkable neighbours for each walkable tile on the map for (int y = 0; y < terrainData.size; y++) { for (int x = 0; x < terrainData.size; x++) { if (walkable[x, y]) { List <Coord> walkableNeighbours = new List <Coord> (); for (int offsetY = -1; offsetY <= 1; offsetY++) { for (int offsetX = -1; offsetX <= 1; offsetX++) { if (offsetX != 0 || offsetY != 0) { int neighbourX = x + offsetX; int neighbourY = y + offsetY; if (neighbourX >= 0 && neighbourX < size && neighbourY >= 0 && neighbourY < size) { if (walkable[neighbourX, neighbourY]) { walkableNeighbours.Add(new Coord(neighbourX, neighbourY)); } } } } } walkableNeighboursMap[x, y] = walkableNeighbours.ToArray(); } } } // Generate offsets within max view distance, sorted by distance ascending // Used to speed up per-tile search for closest water tile List <Coord> viewOffsets = new List <Coord> (); int viewRadius = Animal.maxViewDistance; int sqrViewRadius = viewRadius * viewRadius; for (int offsetY = -viewRadius; offsetY <= viewRadius; offsetY++) { for (int offsetX = -viewRadius; offsetX <= viewRadius; offsetX++) { int sqrOffsetDst = offsetX * offsetX + offsetY * offsetY; if ((offsetX != 0 || offsetY != 0) && sqrOffsetDst <= sqrViewRadius) { viewOffsets.Add(new Coord(offsetX, offsetY)); } } } viewOffsets.Sort((a, b) => (a.x * a.x + a.y * a.y).CompareTo(b.x * b.x + b.y * b.y)); Coord[] viewOffsetsArr = viewOffsets.ToArray(); // Find closest accessible water tile for each tile on the map: closestVisibleWaterMap = new Coord[size, size]; for (int y = 0; y < terrainData.size; y++) { for (int x = 0; x < terrainData.size; x++) { bool foundWater = false; if (walkable[x, y]) { for (int i = 0; i < viewOffsets.Count; i++) { int targetX = x + viewOffsetsArr[i].x; int targetY = y + viewOffsetsArr[i].y; if (targetX >= 0 && targetX < size && targetY >= 0 && targetY < size) { if (terrainData.shore[targetX, targetY]) { if (EnvironmentUtility.TileIsVisibile(x, y, targetX, targetY)) { closestVisibleWaterMap[x, y] = new Coord(targetX, targetY); foundWater = true; break; } } } } } if (!foundWater) { closestVisibleWaterMap[x, y] = Coord.invalid; } } } Debug.Log("Init time: " + sw.ElapsedMilliseconds); }
// Call terrain generator and cache useful info void Init() { var sw = System.Diagnostics.Stopwatch.StartNew(); var terrainGenerator = FindObjectOfType <TerrainGenerator> (); terrainData = terrainGenerator.Generate(); tileCentres = terrainData.tileCentres; walkable = terrainData.walkable; size = terrainData.size; int numSpecies = System.Enum.GetNames(typeof(Species)).Length; preyBySpecies = new Dictionary <Species, List <Species> > (); predatorsBySpecies = new Dictionary <Species, List <Species> > (); // Init species maps speciesMaps = new Dictionary <Species, Map> (); for (int i = 0; i < numSpecies; i++) { Species species = (Species)(1 << i); speciesMaps.Add(species, new Map(size, mapRegionSize)); preyBySpecies.Add(species, new List <Species> ()); predatorsBySpecies.Add(species, new List <Species> ()); } // Store predator/prey relationships for all species for (int i = 0; i < initialPopulations.Length; i++) { if (initialPopulations[i].prefab is Animal) { Animal hunter = (Animal)initialPopulations[i].prefab; Species diet = hunter.diet; for (int huntedSpeciesIndex = 0; huntedSpeciesIndex < numSpecies; huntedSpeciesIndex++) { int bit = ((int)diet >> huntedSpeciesIndex) & 1; // this bit of diet mask set (i.e. the hunter eats this species) if (bit == 1) { int huntedSpecies = 1 << huntedSpeciesIndex; preyBySpecies[hunter.species].Add((Species)huntedSpecies); predatorsBySpecies[(Species)huntedSpecies].Add(hunter.species); } } } } //LogPredatorPreyRelationships (); SpawnTrees(); walkableNeighboursMap = new Coord[size, size][]; // Find and store all walkable neighbours for each walkable tile on the map for (int y = 0; y < terrainData.size; y++) { for (int x = 0; x < terrainData.size; x++) { if (walkable[x, y]) { List <Coord> walkableNeighbours = new List <Coord> (); for (int offsetY = -1; offsetY <= 1; offsetY++) { for (int offsetX = -1; offsetX <= 1; offsetX++) { if (offsetX != 0 || offsetY != 0) { int neighbourX = x + offsetX; int neighbourY = y + offsetY; if (neighbourX >= 0 && neighbourX < size && neighbourY >= 0 && neighbourY < size) { if (walkable[neighbourX, neighbourY]) { walkableNeighbours.Add(new Coord(neighbourX, neighbourY)); } } } } } walkableNeighboursMap[x, y] = walkableNeighbours.ToArray(); } } } // Generate offsets within max view distance, sorted by distance ascending // Used to speed up per-tile search for closest water tile List <Coord> viewOffsets = new List <Coord> (); int viewRadius = Animal.maxViewDistance; int sqrViewRadius = viewRadius * viewRadius; for (int offsetY = -viewRadius; offsetY <= viewRadius; offsetY++) { for (int offsetX = -viewRadius; offsetX <= viewRadius; offsetX++) { int sqrOffsetDst = offsetX * offsetX + offsetY * offsetY; if ((offsetX != 0 || offsetY != 0) && sqrOffsetDst <= sqrViewRadius) { viewOffsets.Add(new Coord(offsetX, offsetY)); } } } viewOffsets.Sort((a, b) => (a.x * a.x + a.y * a.y).CompareTo(b.x * b.x + b.y * b.y)); Coord[] viewOffsetsArr = viewOffsets.ToArray(); // Find closest accessible water tile for each tile on the map: closestVisibleWaterMap = new Coord[size, size]; for (int y = 0; y < terrainData.size; y++) { for (int x = 0; x < terrainData.size; x++) { bool foundWater = false; if (walkable[x, y]) { for (int i = 0; i < viewOffsets.Count; i++) { int targetX = x + viewOffsetsArr[i].x; int targetY = y + viewOffsetsArr[i].y; if (targetX >= 0 && targetX < size && targetY >= 0 && targetY < size) { if (terrainData.shore[targetX, targetY]) { if (EnvironmentUtility.TileIsVisibile(x, y, targetX, targetY)) { closestVisibleWaterMap[x, y] = new Coord(targetX, targetY); foundWater = true; break; } } } } } if (!foundWater) { closestVisibleWaterMap[x, y] = Coord.invalid; } } } Debug.Log("Init time: " + sw.ElapsedMilliseconds); }