public static async Task PositionRandom(List <Unit> Units, Player _opp) { int[][] Pos = new int[20][]; for (int i = 0; i < 20; i++) { Pos[i] = new int[50]; for (int j = 0; j < 50; j++) { Pos[i][j] = 0; } } Random rnd = new Random(); int x = 0; int y = 0; Vector2 vec = new Vector2(x, y); foreach (Unit unit in Units) { bool valid = false; while (valid == false) { x = rnd.Next(0, 19); y = rnd.Next(0, 49); vec = new Vector2(x, y); if (Pos[x][y] == 0) { if (unit.Size == 1) { if (Pos[x - 1][y] == 1 || Pos[x + 1][y] == 1 || Pos[x][y - 1] == 1 || Pos[x][y + 1] == 1) { continue; } Pos[x - 1][y] = 1; Pos[x + 1][y] = 1; Pos[x][y - 1] = 1; Pos[x][y + 1] = 1; } else if (unit.Size == 2) { if (Pos[x - 1][y] == 1 || Pos[x + 1][y] == 1 || Pos[x][y - 1] == 1 || Pos[x][y + 1] == 1 || Pos[x - 2][y] == 1 || Pos[x - 1][y - 1] == 1 || Pos[x][y - 2] == 1 || Pos[x + 1][y - 1] == 1 || Pos[x + 2][y] == 1 || Pos[x + 1][y + 1] == 1 || Pos[x][y + 2] == 1 || Pos[x - 1][y + 1] == 1) { continue; } Pos[x - 1][y] = 1; Pos[x + 1][y] = 1; Pos[x][y - 1] = 1; Pos[x][y + 1] = 1; Pos[x - 2][y] = 1; Pos[x - 1][y - 1] = 1; Pos[x][y - 2] = 1; Pos[x + 1][y - 1] = 1; Pos[x + 2][y] = 1; Pos[x + 1][y + 1] = 1; Pos[x][y + 2] = 1; Pos[x - 1][y + 1] = 1; } Pos[x][y] = 1; unit.PlacePos = new Vector2(x, y); valid = true; } else { } } } foreach (Unit myunit in Units.Where(x => x.Status != UnitStatuses.Available)) { myunit.PlacePos = new Vector2(myunit.PlacePos.Y / 2, myunit.PlacePos.X / 2); UnitService.NewUnitPos(_opp, myunit); } }
public static async Task PositionRandomLinemod(List <Unit> Units, Player _opp, float sx = -1, float sy = -1) { int[][] Pos = new int[20][]; for (int i = 0; i < 20; i++) { Pos[i] = new int[50]; for (int j = 0; j < 50; j++) { Unit unit = _opp.Units.Where(x => x.Status == UnitStatuses.Spawned && x.PlacePos.Y == (float)i / 2 && x.PlacePos.X == (float)j / 2).FirstOrDefault(); if (sx != -1 && unit != null) { // TODO unit size fill Pos[i][j] = 1; } else { Pos[i][j] = 0; } } } int x = 0; int y = 0; Random rnd = new Random(); if (sx != -1) { x = (int)(sy * 2); y = (int)(sx * 2); } else { x = rnd.Next(0, 19); y = rnd.Next(0, 49); } Vector2 vec = new Vector2(x, y); Vector2 firstvec = new Vector2(x, y); foreach (Unit unit in Units) { bool reverse = false; bool valid = false; while (valid == false) { if (reverse == false) { int newx = x; int newy = y + 2; if (newy > 49) { newx++; newy = 0; if (newx > 19) { reverse = true; } } if (reverse == false) { x = newx; y = newy; } } if (reverse == true) { int newx = x; int newy = y - 2; if (newy < 0) { newx--; newy = 49; if (newx < 0) { reverse = false; } } x = newx; y = newy; } vec = new Vector2(x, y); if (Pos[x][y] == 0) { if (unit.Size == 1) { if (x + 1 > 19 || x - 1 < 0 || y + 1 > 49 || y - 1 < 0) { continue; } if (Pos[x - 1][y] == 1 || Pos[x + 1][y] == 1 || Pos[x][y - 1] == 1 || Pos[x][y + 1] == 1) { continue; } Pos[x - 1][y] = 1; Pos[x + 1][y] = 1; Pos[x][y - 1] = 1; Pos[x][y + 1] = 1; } else if (unit.Size == 2) { if (x + 1 > 19 || x - 1 < 0 || y + 1 > 49 || y - 1 < 0 || x + 2 > 19 || x - 2 < 0 || y + 2 > 49 || y - 2 < 0) { continue; } if (Pos[x - 1][y] == 1 || Pos[x + 1][y] == 1 || Pos[x][y - 1] == 1 || Pos[x][y + 1] == 1 || Pos[x - 2][y] == 1 || Pos[x - 1][y - 1] == 1 || Pos[x][y - 2] == 1 || Pos[x + 1][y - 1] == 1 || Pos[x + 2][y] == 1 || Pos[x + 1][y + 1] == 1 || Pos[x][y + 2] == 1 || Pos[x - 1][y + 1] == 1) { continue; } Pos[x - 1][y] = 1; Pos[x + 1][y] = 1; Pos[x][y - 1] = 1; Pos[x][y + 1] = 1; Pos[x - 2][y] = 1; Pos[x - 1][y - 1] = 1; Pos[x][y - 2] = 1; Pos[x + 1][y - 1] = 1; Pos[x + 2][y] = 1; Pos[x + 1][y + 1] = 1; Pos[x][y + 2] = 1; Pos[x - 1][y + 1] = 1; } Pos[x][y] = 1; unit.PlacePos = new Vector2(x, y); valid = true; } } } foreach (Unit myunit in Units) { myunit.PlacePos = new Vector2(myunit.PlacePos.Y / 2, myunit.PlacePos.X / 2); UnitService.NewUnitPos(_opp, myunit); } }
public static async Task PositionRandomDistmod(List <Unit> Units, Player _opp, float sx = -1, float sy = -1) { Unit punit; int[][] Pos = new int[20][]; for (int i = 0; i < 20; i++) { Pos[i] = new int[50]; for (int j = 0; j < 50; j++) { if (sx != -1) { punit = _opp.Units.FirstOrDefault(x => x.Status == UnitStatuses.Spawned && x.PlacePos.Y == (float)i / 2 && x.PlacePos.X == (float)j / 2); // TODO unit size fill if (punit != null) { Pos[i][j] = 1; } else { Pos[i][j] = 0; } } else { Pos[i][j] = 0; } } } Random rnd = new Random(); int x = 0; int y = 0; if (sx != -1) { x = (int)(sy * 2); y = (int)(sx * 2); } else { x = rnd.Next(0, 19); y = rnd.Next(0, 49); } Vector2 vec = new Vector2(x, y); Vector2 lastvec = new Vector2(x, y); foreach (Unit unit in Units.Where(x => x.Status == UnitStatuses.Placed)) { rnd = new Random(); bool valid = false; while (valid == false) { x = rnd.Next(0, 19); y = rnd.Next(0, 49); vec = new Vector2(x, y); int d = rnd.Next(7, 33); float shortdistance = Vector2.DistanceSquared(vec, lastvec); for (int k = 0; k <= d; k++) { int dx = rnd.Next(0, 19); int dy = rnd.Next(0, 49); vec = new Vector2(dx, dy); float distance = Vector2.DistanceSquared(vec, lastvec); if (distance < shortdistance && Pos[dx][dy] == 0) { shortdistance = distance; x = dx; y = dy; vec = new Vector2(x, y); } } if (Pos[x][y] == 0) { if (unit.Size == 1) { if (x + 1 > 19 || x - 1 < 0 || y + 1 > 49 || y - 1 < 0) { continue; } if (Pos[x - 1][y] == 1 || Pos[x + 1][y] == 1 || Pos[x][y - 1] == 1 || Pos[x][y + 1] == 1) { continue; } Pos[x - 1][y] = 1; Pos[x + 1][y] = 1; Pos[x][y - 1] = 1; Pos[x][y + 1] = 1; } else if (unit.Size == 2) { if (x + 1 > 19 || x - 1 < 0 || y + 1 > 49 || y - 1 < 0 || x + 2 > 19 || x - 2 < 0 || y + 2 > 49 || y - 2 < 0) { continue; } if (Pos[x - 1][y] == 1 || Pos[x + 1][y] == 1 || Pos[x][y - 1] == 1 || Pos[x][y + 1] == 1 || Pos[x - 2][y] == 1 || Pos[x - 1][y - 1] == 1 || Pos[x][y - 2] == 1 || Pos[x + 1][y - 1] == 1 || Pos[x + 2][y] == 1 || Pos[x + 1][y + 1] == 1 || Pos[x][y + 2] == 1 || Pos[x - 1][y + 1] == 1) { continue; } Pos[x - 1][y] = 1; Pos[x + 1][y] = 1; Pos[x][y - 1] = 1; Pos[x][y + 1] = 1; Pos[x - 2][y] = 1; Pos[x - 1][y - 1] = 1; Pos[x][y - 2] = 1; Pos[x + 1][y - 1] = 1; Pos[x + 2][y] = 1; Pos[x + 1][y + 1] = 1; Pos[x][y + 2] = 1; Pos[x - 1][y + 1] = 1; } Pos[x][y] = 1; unit.PlacePos = new Vector2(x, y); valid = true; lastvec = new Vector2(x, y); } } } foreach (Unit myunit in Units.Where(x => x.Status == UnitStatuses.Placed)) { myunit.PlacePos = new Vector2(myunit.PlacePos.Y / 2, myunit.PlacePos.X / 2); UnitService.NewUnitPos(_opp, myunit); } }