67 lines
2.2 KiB
Java
67 lines
2.2 KiB
Java
package electrosphere.game.server.town;
|
|
|
|
import electrosphere.game.server.structure.virtual.StructurePlacer;
|
|
import electrosphere.game.server.terrain.manager.ServerTerrainChunk;
|
|
import electrosphere.main.Globals;
|
|
import java.util.Random;
|
|
import org.joml.Vector2i;
|
|
|
|
/**
|
|
*
|
|
* @author amaterasu
|
|
*/
|
|
public class Town {
|
|
|
|
int id;
|
|
static int idIncrementer = 0;
|
|
|
|
final static int avgDiffThreshold = 10;
|
|
public static Vector2i findValidTownLocation(){
|
|
for(int x = 0; x < Globals.serverTerrainManager.getWorldDiscreteSize(); x++){
|
|
for(int y = 0; y < Globals.serverTerrainManager.getWorldDiscreteSize(); y++){
|
|
ServerTerrainChunk chunk = Globals.serverTerrainManager.getChunk(x, y);
|
|
float[][] macroValues = chunk.getMacroValues();
|
|
float sum = 0;
|
|
int count = 0;
|
|
for(int i = 0; i < 5; i++){
|
|
for(int j = 0; j < 5; j++){
|
|
sum = sum + macroValues[i][j];
|
|
count++;
|
|
}
|
|
}
|
|
float average = sum / (float)count;
|
|
if(average > 1000){
|
|
float diffSum = 0;
|
|
for(int i = 0; i < 5; i++){
|
|
for(int j = 0; j < 5; j++){
|
|
diffSum = diffSum + (float)Math.abs(average - macroValues[i][j]);
|
|
}
|
|
}
|
|
float averageDiff = diffSum / (float)count;
|
|
if(averageDiff < avgDiffThreshold){
|
|
return new Vector2i(x,y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Town(){
|
|
this.id = idIncrementer;
|
|
idIncrementer++;
|
|
}
|
|
|
|
public static Town createTown(int x, int y){
|
|
Town rVal = new Town();
|
|
Random rand = new Random();
|
|
int structCount = (rand.nextInt(3) + 2);
|
|
int chunkSize = Globals.serverTerrainManager.getChunkWidth();
|
|
for(int i = 0; i < structCount; i++){
|
|
StructurePlacer.placeStructureAtPoint(x * chunkSize + rand.nextFloat() * 100, y * chunkSize + rand.nextFloat() * 100, "building1");
|
|
}
|
|
return rVal;
|
|
}
|
|
|
|
}
|