Formatting

This commit is contained in:
Anuken
2018-07-12 20:37:15 -04:00
parent a3bda3a941
commit baaeb229cf
379 changed files with 17470 additions and 16215 deletions
@@ -26,32 +26,53 @@ import static io.anuke.mindustry.Vars.*;
//TODO consider using quadtrees for finding specific types of blocks within an area
//TODO maybe use Arrays instead of ObjectSets?
/**Class used for indexing special target blocks for AI.*/
public class BlockIndexer {
/**Size of one ore quadrant.*/
/**
* Class used for indexing special target blocks for AI.
*/
public class BlockIndexer{
/**
* Size of one ore quadrant.
*/
private final static int oreQuadrantSize = 20;
/**Size of one structure quadrant.*/
/**
* Size of one structure quadrant.
*/
private final static int structQuadrantSize = 12;
/**Set of all ores that are being scanned.*/
/**
* Set of all ores that are being scanned.
*/
private final ObjectSet<Item> scanOres = ObjectSet.with(Items.tungsten, Items.coal, Items.lead, Items.thorium, Items.titanium);
/**Stores all ore quadtrants on the map.*/
private ObjectMap<Item, ObjectSet<Tile>> ores;
private final ObjectSet<Item> itemSet = new ObjectSet<>();
/**Tags all quadrants.*/
/**
* Stores all ore quadtrants on the map.
*/
private ObjectMap<Item, ObjectSet<Tile>> ores;
/**
* Tags all quadrants.
*/
private Bits[] structQuadrants;
/**Maps teams to a map of flagged tiles by type.*/
/**
* Maps teams to a map of flagged tiles by type.
*/
private ObjectMap<BlockFlag, ObjectSet<Tile>> enemyMap = new ObjectMap<>();
/**Maps teams to a map of flagged tiles by type.*/
/**
* Maps teams to a map of flagged tiles by type.
*/
private ObjectMap<BlockFlag, ObjectSet<Tile>> allyMap = new ObjectMap<>();
/**Empty map for invalid teams.*/
/**
* Empty map for invalid teams.
*/
private ObjectMap<BlockFlag, ObjectSet<Tile>> emptyMap = new ObjectMap<>();
/**Maps tile positions to their last known tile index data.*/
/**
* Maps tile positions to their last known tile index data.
*/
private IntMap<TileIndex> typeMap = new IntMap<>();
/**Empty array used for returning.*/
/**
* Empty array used for returning.
*/
private ObjectSet<Tile> emptyArray = new ObjectSet<>();
public BlockIndexer(){
@@ -74,18 +95,18 @@ public class BlockIndexer {
//create bitset for each team type that contains each quadrant
structQuadrants = new Bits[Team.all.length];
for(int i = 0; i < Team.all.length; i ++){
structQuadrants[i] = new Bits(Mathf.ceil(world.width() / (float)structQuadrantSize) * Mathf.ceil(world.height() / (float)structQuadrantSize));
for(int i = 0; i < Team.all.length; i++){
structQuadrants[i] = new Bits(Mathf.ceil(world.width() / (float) structQuadrantSize) * Mathf.ceil(world.height() / (float) structQuadrantSize));
}
for(int x = 0; x < world.width(); x ++){
for (int y = 0; y < world.height(); y++) {
for(int x = 0; x < world.width(); x++){
for(int y = 0; y < world.height(); y++){
process(world.tile(x, y));
}
}
for (int x = 0; x < quadWidth(); x++) {
for (int y = 0; y < quadHeight(); y++) {
for(int x = 0; x < quadWidth(); x++){
for(int y = 0; y < quadHeight(); y++){
updateQuadrant(world.tile(x * structQuadrantSize, y * structQuadrantSize));
}
}
@@ -94,12 +115,16 @@ public class BlockIndexer {
});
}
/**Get all allied blocks with a flag.*/
/**
* Get all allied blocks with a flag.
*/
public ObjectSet<Tile> getAllied(Team team, BlockFlag type){
return (state.teams.get(team).ally ? allyMap : enemyMap).get(type, emptyArray);
}
/**Get all enemy blocks with a flag.*/
/**
* Get all enemy blocks with a flag.
*/
public ObjectSet<Tile> getEnemy(Team team, BlockFlag type){
return (!state.teams.get(team).ally ? allyMap : enemyMap).get(type, emptyArray);
}
@@ -108,13 +133,13 @@ public class BlockIndexer {
Entity closest = null;
float dst = 0;
for(int rx = Math.max((int)((x-range)/tilesize/structQuadrantSize), 0); rx <= (int)((x+range)/tilesize/structQuadrantSize) && rx < quadWidth(); rx ++){
for(int ry = Math.max((int)((y-range)/tilesize/structQuadrantSize), 0); ry <= (int)((y+range)/tilesize/structQuadrantSize) && ry < quadHeight(); ry ++){
for(int rx = Math.max((int) ((x - range) / tilesize / structQuadrantSize), 0); rx <= (int) ((x + range) / tilesize / structQuadrantSize) && rx < quadWidth(); rx++){
for(int ry = Math.max((int) ((y - range) / tilesize / structQuadrantSize), 0); ry <= (int) ((y + range) / tilesize / structQuadrantSize) && ry < quadHeight(); ry++){
if(!getQuad(team, rx, ry)) continue;
for(int tx = rx * structQuadrantSize; tx < (rx + 1) * structQuadrantSize && tx < world.width(); tx ++){
for(int ty = ry * structQuadrantSize; ty < (ry + 1) * structQuadrantSize && ty < world.height(); ty ++ ){
for(int tx = rx * structQuadrantSize; tx < (rx + 1) * structQuadrantSize && tx < world.width(); tx++){
for(int ty = ry * structQuadrantSize; ty < (ry + 1) * structQuadrantSize && ty < world.height(); ty++){
Tile other = world.tile(tx, ty);
if(other == null || other.entity == null || !pred.test(other)) continue;
@@ -134,22 +159,26 @@ public class BlockIndexer {
return (TileEntity) closest;
}
/**Returns a set of tiles that have ores of the specified type nearby.
/**
* Returns a set of tiles that have ores of the specified type nearby.
* While each tile in the set is not guaranteed to have an ore directly on it,
* each tile will at least have an ore within {@link #oreQuadrantSize} / 2 blocks of it.
* Only specific ore types are scanned. See {@link #scanOres}.*/
* Only specific ore types are scanned. See {@link #scanOres}.
*/
public ObjectSet<Tile> getOrePositions(Item item){
return ores.get(item, emptyArray);
}
/**Find the closest ore block relative to a position.*/
/**
* Find the closest ore block relative to a position.
*/
public Tile findClosestOre(float xp, float yp, Item item){
Tile tile = Geometry.findClosest(xp, yp, world.indexer().getOrePositions(item));
Tile tile = Geometry.findClosest(xp, yp, world.indexer().getOrePositions(item));
if(tile == null) return null;
for (int x = Math.max(0, tile.x - oreQuadrantSize/2); x < tile.x + oreQuadrantSize/2 && x < world.width(); x++) {
for (int y = Math.max(0, tile.y - oreQuadrantSize/2); y < tile.y + oreQuadrantSize/2 && y < world.height(); y++) {
for(int x = Math.max(0, tile.x - oreQuadrantSize / 2); x < tile.x + oreQuadrantSize / 2 && x < world.width(); x++){
for(int y = Math.max(0, tile.y - oreQuadrantSize / 2); y < tile.y + oreQuadrantSize / 2 && y < world.height(); y++){
Tile res = world.tile(x, y);
if(res.block() == Blocks.air && res.floor().drops != null && res.floor().drops.item == item){
return res;
@@ -186,12 +215,12 @@ public class BlockIndexer {
int quadrantY = tile.y / oreQuadrantSize;
itemSet.clear();
Tile rounded = world.tile(Mathf.clamp(quadrantX * oreQuadrantSize + oreQuadrantSize /2, 0, world.width() - 1),
Mathf.clamp(quadrantY * oreQuadrantSize + oreQuadrantSize /2, 0, world.height() - 1));
Tile rounded = world.tile(Mathf.clamp(quadrantX * oreQuadrantSize + oreQuadrantSize / 2, 0, world.width() - 1),
Mathf.clamp(quadrantY * oreQuadrantSize + oreQuadrantSize / 2, 0, world.height() - 1));
//find all items that this quadrant contains
for (int x = quadrantX * structQuadrantSize; x < world.width() && x < (quadrantX + 1) * structQuadrantSize; x++) {
for (int y = quadrantY * structQuadrantSize; y < world.height() && y < (quadrantY + 1) * structQuadrantSize; y++) {
for(int x = quadrantX * structQuadrantSize; x < world.width() && x < (quadrantX + 1) * structQuadrantSize; x++){
for(int y = quadrantY * structQuadrantSize; y < world.height() && y < (quadrantY + 1) * structQuadrantSize; y++){
Tile result = world.tile(x, y);
if(result.block().drops == null || !scanOres.contains(result.block().drops.item)) continue;
@@ -200,7 +229,7 @@ public class BlockIndexer {
}
//update quadrant at this position
for (Item item : scanOres){
for(Item item : scanOres){
ObjectSet<Tile> set = ores.get(item);
//update quadrant status depending on whether the item is in it
@@ -219,7 +248,7 @@ public class BlockIndexer {
int index = quadrantX + quadrantY * quadWidth();
//Log.info("Updating quadrant: {0} {1}", quadrantX, quadrantY);
for(TeamData data : state.teams.getTeams()) {
for(TeamData data : state.teams.getTeams()){
//fast-set this quadrant to 'occupied' if the tile just placed is already of this team
if(tile.getTeam() == data.team && tile.entity != null){
@@ -230,8 +259,8 @@ public class BlockIndexer {
structQuadrants[data.team.ordinal()].clear(index);
outer:
for (int x = quadrantX * structQuadrantSize; x < world.width() && x < (quadrantX + 1) * structQuadrantSize; x++) {
for (int y = quadrantY * structQuadrantSize; y < world.height() && y < (quadrantY + 1) * structQuadrantSize; y++) {
for(int x = quadrantX * structQuadrantSize; x < world.width() && x < (quadrantX + 1) * structQuadrantSize; x++){
for(int y = quadrantY * structQuadrantSize; y < world.height() && y < (quadrantY + 1) * structQuadrantSize; y++){
Tile result = world.tile(x, y);
//when a targetable block is found, mark this quadrant as occupied and stop searching
if(result.entity != null && result.getTeam() == data.team){
@@ -244,16 +273,16 @@ public class BlockIndexer {
}
private boolean getQuad(Team team, int quadrantX, int quadrantY){
int index = quadrantX + quadrantY * Mathf.ceil(world.width() / (float)structQuadrantSize);
int index = quadrantX + quadrantY * Mathf.ceil(world.width() / (float) structQuadrantSize);
return structQuadrants[team.ordinal()].get(index);
}
private int quadWidth(){
return Mathf.ceil(world.width() / (float)structQuadrantSize);
return Mathf.ceil(world.width() / (float) structQuadrantSize);
}
private int quadHeight(){
return Mathf.ceil(world.height() / (float)structQuadrantSize);
return Mathf.ceil(world.height() / (float) structQuadrantSize);
}
private ObjectMap<BlockFlag, ObjectSet<Tile>> getMap(Team team){
@@ -269,10 +298,10 @@ public class BlockIndexer {
ores.put(item, new ObjectSet<>());
}
for(int x = 0; x < world.width(); x ++){
for (int y = 0; y < world.height(); y++) {
int qx = (x/ oreQuadrantSize);
int qy = (y/ oreQuadrantSize);
for(int x = 0; x < world.width(); x++){
for(int y = 0; y < world.height(); y++){
int qx = (x / oreQuadrantSize);
int qy = (y / oreQuadrantSize);
Tile tile = world.tile(x, y);
@@ -280,8 +309,8 @@ public class BlockIndexer {
if(tile.floor().drops != null && scanOres.contains(tile.floor().drops.item) && tile.block() == Blocks.air){
ores.get(tile.floor().drops.item).add(world.tile(
//make sure to clamp quadrant middle position, since it might go off bounds
Mathf.clamp(qx * oreQuadrantSize + oreQuadrantSize /2, 0, world.width() - 1),
Mathf.clamp(qy * oreQuadrantSize + oreQuadrantSize /2, 0, world.height() - 1)));
Mathf.clamp(qx * oreQuadrantSize + oreQuadrantSize / 2, 0, world.width() - 1),
Mathf.clamp(qy * oreQuadrantSize + oreQuadrantSize / 2, 0, world.height() - 1)));
}
}
}
@@ -291,7 +320,7 @@ public class BlockIndexer {
public final EnumSet<BlockFlag> flags;
public final Team team;
public TileIndex(EnumSet<BlockFlag> flags, Team team) {
public TileIndex(EnumSet<BlockFlag> flags, Team team){
this.flags = flags;
this.team = team;
}
+15 -15
View File
@@ -20,7 +20,7 @@ import io.anuke.ucore.util.Log;
import static io.anuke.mindustry.Vars.state;
import static io.anuke.mindustry.Vars.world;
public class Pathfinder {
public class Pathfinder{
private long maxUpdate = TimeUtils.millisToNanos(4);
private PathData[] paths;
private IntArray blocked = new IntArray();
@@ -56,7 +56,7 @@ public class Pathfinder {
Tile target = null;
float tl = 0f;
for(GridPoint2 point : Geometry.d8) {
for(GridPoint2 point : Geometry.d8){
int dx = tile.x + point.x, dy = tile.y + point.y;
Tile other = world.tile(dx, dy);
@@ -89,16 +89,16 @@ public class Pathfinder {
}
private void update(Tile tile, Team team){
if(paths[team.ordinal()] != null) {
if(paths[team.ordinal()] != null){
PathData path = paths[team.ordinal()];
if(!passable(tile, team)){
path.weights[tile.x][tile.y] = Float.MAX_VALUE;
}
path.search ++;
path.search++;
if(path.lastSearchTime + 1000/60*3 > TimeUtils.millis()){
if(path.lastSearchTime + 1000 / 60 * 3 > TimeUtils.millis()){
path.frontier.clear();
}
@@ -115,17 +115,17 @@ public class Pathfinder {
private void createFor(Team team){
PathData path = new PathData();
path.search ++;
path.search++;
path.frontier.ensureCapacity((world.width() + world.height()) * 3);
paths[team.ordinal()] = path;
for (int x = 0; x < world.width(); x++) {
for (int y = 0; y < world.height(); y++) {
for(int x = 0; x < world.width(); x++){
for(int y = 0; y < world.height(); y++){
Tile tile = world.tile(x, y);
if (tile.block().flags != null && state.teams.areEnemies(tile.getTeam(), team)
&& tile.block().flags.contains(BlockFlag.target)) {
if(tile.block().flags != null && state.teams.areEnemies(tile.getTeam(), team)
&& tile.block().flags.contains(BlockFlag.target)){
path.frontier.addFirst(tile);
path.weights[x][y] = 0;
path.searches[x][y] = path.search;
@@ -143,20 +143,20 @@ public class Pathfinder {
long start = TimeUtils.nanoTime();
while (path.frontier.size > 0 && (nsToRun < 0 || TimeUtils.timeSinceNanos(start) <= nsToRun)) {
while(path.frontier.size > 0 && (nsToRun < 0 || TimeUtils.timeSinceNanos(start) <= nsToRun)){
Tile tile = path.frontier.removeLast();
float cost = path.weights[tile.x][tile.y];
if (cost < Float.MAX_VALUE) {
for (GridPoint2 point : Geometry.d4) {
if(cost < Float.MAX_VALUE){
for(GridPoint2 point : Geometry.d4){
int dx = tile.x + point.x, dy = tile.y + point.y;
Tile other = world.tile(dx, dy);
if (other != null && (path.weights[dx][dy] > cost + 1 || path.searches[dx][dy] < path.search)
if(other != null && (path.weights[dx][dy] > cost + 1 || path.searches[dx][dy] < path.search)
&& passable(other, team)){
path.frontier.addFirst(world.tile(dx, dy));
path.weights[dx][dy] = cost + other.cost/2f;
path.weights[dx][dy] = cost + other.cost / 2f;
path.searches[dx][dy] = path.search;
}
}
+28 -28
View File
@@ -18,7 +18,7 @@ import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
public class WaveSpawner {
public class WaveSpawner{
private static final int quadsize = 4;
private Bits quadrants;
@@ -34,28 +34,28 @@ public class WaveSpawner {
public void write(DataOutput output) throws IOException{
output.writeShort(flySpawns.size);
for (FlyerSpawn spawn : flySpawns){
for(FlyerSpawn spawn : flySpawns){
output.writeFloat(spawn.angle);
}
output.writeShort(groundSpawns.size);
for (GroundSpawn spawn : groundSpawns){
output.writeShort((short)spawn.x);
output.writeShort((short)spawn.y);
for(GroundSpawn spawn : groundSpawns){
output.writeShort((short) spawn.x);
output.writeShort((short) spawn.y);
}
}
public void read(DataInput input) throws IOException{
short flya = input.readShort();
for (int i = 0; i < flya; i++) {
for(int i = 0; i < flya; i++){
FlyerSpawn spawn = new FlyerSpawn();
spawn.angle = input.readFloat();
flySpawns.add(spawn);
}
short grounda = input.readShort();
for (int i = 0; i < grounda; i++) {
for(int i = 0; i < grounda; i++){
GroundSpawn spawn = new GroundSpawn();
spawn.x = input.readShort();
spawn.y = input.readShort();
@@ -80,13 +80,13 @@ public class WaveSpawner {
int addGround = groundGroups - groundSpawns.size, addFly = flyGroups - flySpawns.size;
//add extra groups if the total exceeds it
for (int i = 0; i < addGround; i++) {
for(int i = 0; i < addGround; i++){
GroundSpawn spawn = new GroundSpawn();
findLocation(spawn);
groundSpawns.add(spawn);
}
for (int i = 0; i < addFly; i++) {
for(int i = 0; i < addFly; i++){
FlyerSpawn spawn = new FlyerSpawn();
findLocation(spawn);
flySpawns.add(spawn);
@@ -99,7 +99,7 @@ public class WaveSpawner {
int groups = group.getGroupsSpawned(state.wave);
int spawned = group.getUnitsSpawned(state.wave);
for (int i = 0; i < groups; i++) {
for(int i = 0; i < groups; i++){
Squad squad = new Squad();
float spawnX, spawnY;
float spread;
@@ -109,11 +109,11 @@ public class WaveSpawner {
//TODO verify flyer spawn
float margin = 40f; //how far away from the edge flying units spawn
spawnX = world.width() *tilesize/2f + Mathf.sqrwavex(spawn.angle) * (world.width()/2f*tilesize + margin);
spawnY = world.height() * tilesize/2f + Mathf.sqrwavey(spawn.angle) * (world.height()/2f*tilesize + margin);
spawnX = world.width() * tilesize / 2f + Mathf.sqrwavex(spawn.angle) * (world.width() / 2f * tilesize + margin);
spawnY = world.height() * tilesize / 2f + Mathf.sqrwavey(spawn.angle) * (world.height() / 2f * tilesize + margin);
spread = margin / 1.5f;
flyCount ++;
flyCount++;
}else{
GroundSpawn spawn = groundSpawns.get(groundCount);
checkQuadrant(spawn.x, spawn.y);
@@ -121,14 +121,14 @@ public class WaveSpawner {
findLocation(spawn);
}
spawnX = spawn.x * quadsize * tilesize + quadsize * tilesize/2f;
spawnY = spawn.y * quadsize * tilesize + quadsize * tilesize/2f;
spread = quadsize*tilesize/3f;
spawnX = spawn.x * quadsize * tilesize + quadsize * tilesize / 2f;
spawnY = spawn.y * quadsize * tilesize + quadsize * tilesize / 2f;
spread = quadsize * tilesize / 3f;
groundCount ++;
groundCount++;
}
for (int j = 0; j < spawned; j++) {
for(int j = 0; j < spawned; j++){
BaseUnit unit = group.createUnit(Team.red);
unit.setWave();
unit.setSquad(squad);
@@ -140,19 +140,19 @@ public class WaveSpawner {
}
public void checkAllQuadrants(){
for(int x = 0; x < quadWidth(); x ++){
for(int y = 0; y < quadHeight(); y ++){
for(int x = 0; x < quadWidth(); x++){
for(int y = 0; y < quadHeight(); y++){
checkQuadrant(x, y);
}
}
}
private void checkQuadrant(int quadx, int quady){
setQuad(quadx, quady, true);
outer:
for (int x = quadx * quadsize; x < world.width() && x < (quadx + 1)*quadsize; x++) {
for (int y = quady * quadsize; y < world.height() && y < (quady + 1)*quadsize; y++) {
for(int x = quadx * quadsize; x < world.width() && x < (quadx + 1) * quadsize; x++){
for(int y = quady * quadsize; y < world.height() && y < (quady + 1) * quadsize; y++){
Tile tile = world.tile(x, y);
if(tile == null || tile.solid() || world.pathfinder().getValueforTeam(Team.red, x, y) == Float.MAX_VALUE){
@@ -190,7 +190,7 @@ public class WaveSpawner {
spawn.x = -1;
spawn.y = -1;
int shellWidth = quadWidth()*2 + quadHeight() * 2 * 6;
int shellWidth = quadWidth() * 2 + quadHeight() * 2 * 6;
shellWidth = Math.min(quadWidth() * quadHeight() / 4, shellWidth);
Mathf.traverseSpiral(quadWidth(), quadHeight(), Mathf.random(shellWidth), (x, y) -> {
@@ -210,11 +210,11 @@ public class WaveSpawner {
}
private int quadWidth(){
return Mathf.ceil(world.width() / (float)quadsize);
return Mathf.ceil(world.width() / (float) quadsize);
}
private int quadHeight(){
return Mathf.ceil(world.height() / (float)quadsize);
return Mathf.ceil(world.height() / (float) quadsize);
}
private class FlyerSpawn{