Minor optimization

This commit is contained in:
Anuken
2018-07-05 15:23:20 -04:00
parent bc36c57f9f
commit 5e66dfcc36
4 changed files with 33 additions and 21 deletions

View File

@@ -115,6 +115,10 @@ public class World extends Module{
if(!Mathf.inBounds(x, y, tiles)) return null; if(!Mathf.inBounds(x, y, tiles)) return null;
return tiles[x][y]; return tiles[x][y];
} }
public Tile rawTile(int x, int y){
return tiles[x][y];
}
public Tile tileWorld(float x, float y){ public Tile tileWorld(float x, float y){
return tile(Mathf.scl2(x, tilesize), Mathf.scl2(y, tilesize)); return tile(Mathf.scl2(x, tilesize), Mathf.scl2(y, tilesize));

View File

@@ -59,19 +59,25 @@ public class BlockRenderer{
Graphics.surface(renderer.effectSurface); Graphics.surface(renderer.effectSurface);
for(int x = -rangex - expandr; x <= rangex + expandr; x++){ int avgx = Mathf.scl(camera.position.x, tilesize);
for(int y = -rangey - expandr; y <= rangey + expandr; y++){ int avgy = Mathf.scl(camera.position.y, tilesize);
int worldx = Mathf.scl(camera.position.x, tilesize) + x;
int worldy = Mathf.scl(camera.position.y, tilesize) + y; int minx = Math.max(avgx - rangex - expandr, 0);
boolean expanded = (x < -rangex || x > rangex || y < -rangey || y > rangey); int miny = Math.max(avgy - rangey - expandr, 0);
int maxx = Math.min(world.width() - 1, avgx + rangex + expandr);
int maxy = Math.min(world.height() - 1, avgy+ rangey + expandr);
for(int x = minx; x <= maxx; x++){
for(int y = miny; y <= maxy; y++){
boolean expanded = (Math.abs(x - avgx) > rangex || Math.abs(y - avgy) > rangey);
synchronized (Tile.tileSetLock) { synchronized (Tile.tileSetLock) {
Tile tile = world.tile(worldx, worldy); Tile tile = world.rawTile(x, y);
if (tile != null) { if (tile != null) {
Block block = tile.block(); Block block = tile.block();
if (!expanded && block != Blocks.air && world.isAccessible(worldx, worldy)) { if (!expanded && block != Blocks.air && world.isAccessible(x, y)) {
tile.block().drawShadow(tile); tile.block().drawShadow(tile);
} }

View File

@@ -27,7 +27,8 @@ public class Tile implements PosTrait, TargetTrait {
public static final Object tileSetLock = new Object(); public static final Object tileSetLock = new Object();
/**Block ID data.*/ /**Block ID data.*/
private byte floor, wall; private Block wall;
private Floor floor;
/**Rotation, 0-3. Also used to store offload location for routers, in which case it can be any number.*/ /**Rotation, 0-3. Also used to store offload location for routers, in which case it can be any number.*/
private byte rotation; private byte rotation;
/**Team ordinal.*/ /**Team ordinal.*/
@@ -52,15 +53,15 @@ public class Tile implements PosTrait, TargetTrait {
public Tile(int x, int y, byte floor, byte wall){ public Tile(int x, int y, byte floor, byte wall){
this(x, y); this(x, y);
this.floor = floor; this.floor = (Floor) Block.getByID(floor);
this.wall = wall; this.wall = Block.getByID(wall);
changed(); changed();
} }
public Tile(int x, int y, byte floor, byte wall, byte rotation, byte team, byte elevation){ public Tile(int x, int y, byte floor, byte wall, byte rotation, byte team, byte elevation){
this(x, y); this(x, y);
this.floor = floor; this.floor =(Floor) Block.getByID(floor);
this.wall = wall; this.wall = Block.getByID(wall);
this.rotation = rotation; this.rotation = rotation;
this.elevation = elevation; this.elevation = elevation;
changed(); changed();
@@ -72,11 +73,11 @@ public class Tile implements PosTrait, TargetTrait {
} }
public byte getWallID(){ public byte getWallID(){
return wall; return (byte)wall.id;
} }
public byte getFloorID(){ public byte getFloorID(){
return floor; return (byte)floor.id;
} }
/**Return relative rotation to a coordinate. Returns -1 if the coordinate is not near this tile.*/ /**Return relative rotation to a coordinate. Returns -1 if the coordinate is not near this tile.*/
@@ -129,11 +130,11 @@ public class Tile implements PosTrait, TargetTrait {
} }
public Floor floor(){ public Floor floor(){
return (Floor)Block.getByID(getFloorID()); return floor;
} }
public Block block(){ public Block block(){
return Block.getByID(getWallID()); return wall;
} }
public Team getTeam(){ public Team getTeam(){
@@ -161,7 +162,7 @@ public class Tile implements PosTrait, TargetTrait {
public void setBlock(Block type, int rotation){ public void setBlock(Block type, int rotation){
synchronized (tileSetLock) { synchronized (tileSetLock) {
if(rotation < 0) rotation = (-rotation + 2); if(rotation < 0) rotation = (-rotation + 2);
this.wall = (byte)type.id; this.wall = type;
this.link = 0; this.link = 0;
setRotation((byte) (rotation % 4)); setRotation((byte) (rotation % 4));
changed(); changed();
@@ -170,14 +171,14 @@ public class Tile implements PosTrait, TargetTrait {
public void setBlock(Block type){ public void setBlock(Block type){
synchronized (tileSetLock) { synchronized (tileSetLock) {
this.wall = (byte)type.id; this.wall = type;
this.link = 0; this.link = 0;
changed(); changed();
} }
} }
public void setFloor(Block type){ public void setFloor(Floor type){
this.floor = (byte)type.id; this.floor = type;
} }
public void setRotation(byte rotation){ public void setRotation(byte rotation){

View File

@@ -15,6 +15,7 @@ import io.anuke.mindustry.io.MapTileData.TileDataMarker;
import io.anuke.mindustry.type.Item; import io.anuke.mindustry.type.Item;
import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Floor;
import io.anuke.ucore.noise.RidgedPerlin; import io.anuke.ucore.noise.RidgedPerlin;
import io.anuke.ucore.noise.Simplex; import io.anuke.ucore.noise.Simplex;
import io.anuke.ucore.util.Geometry; import io.anuke.ucore.util.Geometry;
@@ -133,7 +134,7 @@ public class WorldGenerator {
if(entry.noise.octaveNoise2D(2, 0.7, 1f / (2 + i*2), x, y)/2f + if(entry.noise.octaveNoise2D(2, 0.7, 1f / (2 + i*2), x, y)/2f +
entry.ridge.getValue(x, y, 1f / (28 + i*4)) >= 2.0f - entry.frequency*4.0f entry.ridge.getValue(x, y, 1f / (28 + i*4)) >= 2.0f - entry.frequency*4.0f
&& entry.ridge.getValue(x+9999, y+9999, 1f/100f) > 0.4){ && entry.ridge.getValue(x+9999, y+9999, 1f/100f) > 0.4){
tile.setFloor(OreBlocks.get(tile.floor(), entry.item)); tile.setFloor((Floor) OreBlocks.get(tile.floor(), entry.item));
break; break;
} }
} }