public modProgram.sResult GenerateGateways() { int num5; modProgram.sResult result2; int num6; int x; result2.Success = false; result2.Problem = ""; clsMap.clsTerrain terrain = this.Map.Terrain; sPossibleGateway[] gatewayArray = new sPossibleGateway[(((terrain.TileSize.X * terrain.TileSize.Y) * 2) - 1) + 1]; int num10 = terrain.TileSize.Y - 1; int y = 0; while (y <= num10) { num6 = 0; int num11 = terrain.TileSize.X - 1; for (x = 0; x <= num11; x++) { if (this.GenerateTerrainTiles[x, y].Node.GetClearance >= 1) { if (this.GenerateTerrainTiles[x, y].Node.GetClearance == 1) { if ((num6 > 3) & (num6 <= 9)) { gatewayArray[num5].StartPos.X = x - num6; gatewayArray[num5].StartPos.Y = y; gatewayArray[num5].Length = num6 + 1; gatewayArray[num5].IsVertical = false; gatewayArray[num5].MiddlePos.X = (gatewayArray[num5].StartPos.X * 0x80) + (gatewayArray[num5].Length * 0x40); gatewayArray[num5].MiddlePos.Y = gatewayArray[num5].StartPos.Y * 0x80; num5++; } num6 = 1; } else { num6++; } } } y++; } int num12 = terrain.TileSize.X - 1; x = 0; while (x <= num12) { num6 = 0; y = 0; while (y < terrain.TileSize.Y) { if (this.GenerateTerrainTiles[x, y].Node.GetClearance >= 1) { if (this.GenerateTerrainTiles[x, y].Node.GetClearance == 1) { if ((num6 >= 3) & (num6 <= 9)) { gatewayArray[num5].StartPos.X = x; gatewayArray[num5].StartPos.Y = y - num6; gatewayArray[num5].Length = num6 + 1; gatewayArray[num5].IsVertical = true; gatewayArray[num5].MiddlePos.X = gatewayArray[num5].StartPos.X * 0x80; gatewayArray[num5].MiddlePos.Y = (gatewayArray[num5].StartPos.Y * 0x80) + (gatewayArray[num5].Length * 0x40); num5++; } num6 = 1; } else { num6++; } } y++; } x++; } bool[,] flagArray = new bool[(terrain.TileSize.X - 1) + 1, (terrain.TileSize.Y - 1) + 1]; while (num5 > 0) { modMath.sXY_int _int2; int index = -1; float maxValue = float.MaxValue; int num13 = num5 - 1; int num = 0; while (num <= num13) { float length = gatewayArray[num].Length; if (length < maxValue) { maxValue = length; index = num; } num++; } if (gatewayArray[index].IsVertical) { _int2 = new modMath.sXY_int(gatewayArray[index].StartPos.X, (gatewayArray[index].StartPos.Y + gatewayArray[index].Length) - 1); this.Map.GatewayCreateStoreChange(gatewayArray[index].StartPos, _int2); int num14 = (gatewayArray[index].StartPos.Y + gatewayArray[index].Length) - 1; y = gatewayArray[index].StartPos.Y; while (y <= num14) { flagArray[gatewayArray[index].StartPos.X, y] = true; y++; } } else { _int2 = new modMath.sXY_int((gatewayArray[index].StartPos.X + gatewayArray[index].Length) - 1, gatewayArray[index].StartPos.Y); this.Map.GatewayCreateStoreChange(gatewayArray[index].StartPos, _int2); int num15 = (gatewayArray[index].StartPos.X + gatewayArray[index].Length) - 1; x = gatewayArray[index].StartPos.X; while (x <= num15) { flagArray[x, gatewayArray[index].StartPos.Y] = true; x++; } } modMath.sXY_int middlePos = gatewayArray[index].MiddlePos; double num4 = gatewayArray[index].Length * 0x80; num = 0; while (num < num5) { bool flag = true; if (gatewayArray[num].IsVertical) { int num16 = (gatewayArray[num].StartPos.Y + gatewayArray[num].Length) - 1; for (y = gatewayArray[num].StartPos.Y; y <= num16; y++) { if (flagArray[gatewayArray[num].StartPos.X, y]) { flag = false; break; } } } else { int num17 = (gatewayArray[num].StartPos.X + gatewayArray[num].Length) - 1; for (x = gatewayArray[num].StartPos.X; x <= num17; x++) { if (flagArray[x, gatewayArray[num].StartPos.Y]) { flag = false; break; } } } if (flag) { _int2 = middlePos - gatewayArray[num].MiddlePos; if (_int2.ToDoubles().GetMagnitude() < num4) { flag = false; } } if (!flag) { num5--; if (num != num5) { gatewayArray[num] = gatewayArray[num5]; } } else { num++; } } } result2.Success = true; return result2; }
public App.sResult GenerateGateways() { App.sResult ReturnResult = new App.sResult(); ReturnResult.Success = false; ReturnResult.Problem = ""; //must be done before units otherwise the units will be treated as gateway obstacles clsMap.clsTerrain Terrain = Map.Terrain; int X = 0; int SpaceCount = 0; int Y = 0; sPossibleGateway[] PossibleGateways = new sPossibleGateway[Terrain.TileSize.X * Terrain.TileSize.Y * 2]; int PossibleGatewayCount = 0; for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ ) { SpaceCount = 0; for ( X = 0; X <= Terrain.TileSize.X - 1; X++ ) { if ( GenerateTerrainTiles[X, Y].Node.GetClearance < 1 ) { } else if ( GenerateTerrainTiles[X, Y].Node.GetClearance == 1 ) { if ( SpaceCount > 3 & SpaceCount <= 9 ) { PossibleGateways[PossibleGatewayCount].StartPos.X = X - SpaceCount; PossibleGateways[PossibleGatewayCount].StartPos.Y = Y; PossibleGateways[PossibleGatewayCount].Length = SpaceCount + 1; PossibleGateways[PossibleGatewayCount].IsVertical = false; PossibleGateways[PossibleGatewayCount].MiddlePos.X = PossibleGateways[PossibleGatewayCount].StartPos.X * 128 + PossibleGateways[PossibleGatewayCount].Length * 64; PossibleGateways[PossibleGatewayCount].MiddlePos.Y = PossibleGateways[PossibleGatewayCount].StartPos.Y * 128; PossibleGatewayCount++; } SpaceCount = 1; } else { SpaceCount++; } } } for ( X = 0; X <= Terrain.TileSize.X - 1; X++ ) { SpaceCount = 0; Y = 0; while ( Y < Terrain.TileSize.Y ) { if ( GenerateTerrainTiles[X, Y].Node.GetClearance < 1 ) { } else if ( GenerateTerrainTiles[X, Y].Node.GetClearance == 1 ) { if ( SpaceCount >= 3 & SpaceCount <= 9 ) { PossibleGateways[PossibleGatewayCount].StartPos.X = X; PossibleGateways[PossibleGatewayCount].StartPos.Y = Y - SpaceCount; PossibleGateways[PossibleGatewayCount].Length = SpaceCount + 1; PossibleGateways[PossibleGatewayCount].IsVertical = true; PossibleGateways[PossibleGatewayCount].MiddlePos.X = PossibleGateways[PossibleGatewayCount].StartPos.X * 128; PossibleGateways[PossibleGatewayCount].MiddlePos.Y = PossibleGateways[PossibleGatewayCount].StartPos.Y * 128 + PossibleGateways[PossibleGatewayCount].Length * 64; PossibleGatewayCount++; } SpaceCount = 1; } else { SpaceCount++; } Y++; } } //add the best gateways int A = 0; float Value = 0; float BestValue = 0; int BestNum = 0; bool[,] TileIsGateway = new bool[Terrain.TileSize.X, Terrain.TileSize.Y]; bool Valid = default(bool); sXY_int InvalidPos = new sXY_int(); double InvalidDist = 0; while ( PossibleGatewayCount > 0 ) { BestNum = -1; BestValue = float.MaxValue; for ( A = 0; A <= PossibleGatewayCount - 1; A++ ) { //Value = 0.0F //For B = 0 To PossibleGatewayCount - 1 // Value += GetDist(PossibleGateways(A).MiddlePos, PossibleGateways(B).MiddlePos) //Next Value = PossibleGateways[A].Length; if ( Value < BestValue ) { BestValue = Value; BestNum = A; } } if ( PossibleGateways[BestNum].IsVertical ) { Map.GatewayCreateStoreChange(PossibleGateways[BestNum].StartPos, new sXY_int(PossibleGateways[BestNum].StartPos.X, PossibleGateways[BestNum].StartPos.Y + PossibleGateways[BestNum].Length - 1)); for ( Y = PossibleGateways[BestNum].StartPos.Y; Y <= PossibleGateways[BestNum].StartPos.Y + PossibleGateways[BestNum].Length - 1; Y++ ) { TileIsGateway[PossibleGateways[BestNum].StartPos.X, Y] = true; } } else { Map.GatewayCreateStoreChange(PossibleGateways[BestNum].StartPos, new sXY_int(PossibleGateways[BestNum].StartPos.X + PossibleGateways[BestNum].Length - 1, PossibleGateways[BestNum].StartPos.Y)); for ( X = PossibleGateways[BestNum].StartPos.X; X <= PossibleGateways[BestNum].StartPos.X + PossibleGateways[BestNum].Length - 1; X++ ) { TileIsGateway[X, PossibleGateways[BestNum].StartPos.Y] = true; } } InvalidPos = PossibleGateways[BestNum].MiddlePos; InvalidDist = PossibleGateways[BestNum].Length * 128; A = 0; while ( A < PossibleGatewayCount ) { Valid = true; if ( PossibleGateways[A].IsVertical ) { for ( Y = PossibleGateways[A].StartPos.Y; Y <= PossibleGateways[A].StartPos.Y + PossibleGateways[A].Length - 1; Y++ ) { if ( TileIsGateway[PossibleGateways[A].StartPos.X, Y] ) { Valid = false; break; } } } else { for ( X = PossibleGateways[A].StartPos.X; X <= PossibleGateways[A].StartPos.X + PossibleGateways[A].Length - 1; X++ ) { if ( TileIsGateway[X, PossibleGateways[A].StartPos.Y] ) { Valid = false; break; } } } if ( Valid ) { if ( (InvalidPos - PossibleGateways[A].MiddlePos).ToDoubles().GetMagnitude() < InvalidDist ) { Valid = false; } } if ( !Valid ) { PossibleGatewayCount--; if ( A != PossibleGatewayCount ) { PossibleGateways[A] = PossibleGateways[PossibleGatewayCount]; } } else { A++; } } } ReturnResult.Success = true; return ReturnResult; }