Added 'sleeping entity' optimization

This commit is contained in:
Anuken
2018-04-17 17:07:04 -04:00
parent b020bf6816
commit 739e636a2c
10 changed files with 53 additions and 17 deletions

View File

@@ -141,7 +141,6 @@ public class Vars{
public static final EntityGroup<Player> playerGroup = Entities.addGroup(Player.class).enableMapping();
public static final EntityGroup<TileEntity> tileGroup = Entities.addGroup(TileEntity.class, false);
public static final EntityGroup<TileEntity> disabledTileGroup = Entities.addGroup(TileEntity.class, false);
public static final EntityGroup<Bullet> bulletGroup = Entities.addGroup(Bullet.class);
public static final EntityGroup<Shield> shieldGroup = Entities.addGroup(Shield.class, false);
public static final EntityGroup<EffectEntity> effectGroup = Entities.addGroup(EffectEntity.class, false);

View File

@@ -1,6 +1,7 @@
package io.anuke.mindustry.core;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.game.EventType.GameOverEvent;
import io.anuke.mindustry.game.EventType.PlayEvent;
import io.anuke.mindustry.game.EventType.ResetEvent;
@@ -61,6 +62,7 @@ public class Logic extends Module {
Timers.clear();
Entities.clear();
TileEntity.sleepingEntities = 0;
Events.fire(ResetEvent.class);
}

View File

@@ -19,12 +19,12 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static io.anuke.mindustry.Vars.disabledTileGroup;
import static io.anuke.mindustry.Vars.tileGroup;
import static io.anuke.mindustry.Vars.world;
public class TileEntity extends Entity{
public static final float timeToSleep = 60f*3; //3 seconds to fall asleep
public static final float timeToSleep = 60f*4; //4 seconds to fall asleep
public static int sleepingEntities = 0;
public Tile tile;
public Timer timer;
@@ -51,7 +51,12 @@ public class TileEntity extends Entity{
timer = new Timer(tile.block().timers);
if(added){
add();
if(!tile.block().autoSleep) {
add();
}else{
sleeping = true;
sleepingEntities ++;
}
}
return this;
@@ -63,8 +68,8 @@ public class TileEntity extends Entity{
sleepTime += Timers.delta();
if(!sleeping && sleepTime >= timeToSleep){
remove();
add(disabledTileGroup);
sleeping = true;
sleepingEntities ++;
}
}
@@ -73,9 +78,9 @@ public class TileEntity extends Entity{
public void wakeUp(){
sleepTime = 0f;
if(sleeping){
remove();
add(tileGroup);
sleeping = false;
sleepingEntities --;
}
}

View File

@@ -2,12 +2,14 @@ package io.anuke.mindustry.io;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.utils.IntIntMap;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectMap.Entry;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.io.MapTileData.TileDataMarker;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.ColorMapper;
@@ -45,6 +47,7 @@ public class MapIO {
Block floor = Block.getByID(marker.floor);
Block wall = Block.getByID(marker.wall);
int wallc = ColorMapper.getColor(wall);
if(wallc == 0 && (wall.update || wall.solid || wall.breakable)) wallc = Color.rgba8888(Team.values()[marker.team].color);
pixmap.drawPixel(x, pixmap.getHeight() - 1 - y, wallc == 0 ? ColorMapper.getColor(floor) : wallc);
}
}

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import io.anuke.mindustry.content.UnitTypes;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.effect.Fireball;
import io.anuke.mindustry.entities.units.BaseUnit;
import io.anuke.mindustry.game.Team;
@@ -131,6 +132,7 @@ public class DebugFragment implements Fragment {
"client.connecting: " + netClient.isConnecting() + "\n" : "",
"players: " + playerGroup.size(),
"tiles: " + tileGroup.size(),
"tiles.sleeping: " + TileEntity.sleepingEntities,
"time: " + Timers.time(),
"state.gameover: " + state.gameOver,
"state: " + state.getState(),

View File

@@ -106,6 +106,8 @@ public class Block extends BaseBlock {
public BlockBars bars = new BlockBars();
/**List of block stats.*/
public BlockStats stats = new BlockStats();
/**Whether to automatically set the entity to 'sleeping' when created.*/
public boolean autoSleep;
public Block(String name) {
this.name = name;
@@ -243,16 +245,19 @@ public class Block extends BaseBlock {
tempColor.mul(1f/units);
Liquid liquid = tile.entity.liquids.liquid;
float splash = Mathf.clamp(tile.entity.liquids.amount/4f, 0f, 10f);
if(hasLiquids) {
for(int i = 0; i < Mathf.clamp(tile.entity.liquids.amount / 5, 0, 30); i ++){
Timers.run(i/2, () -> {
Tile other = world.tile(tile.x + Mathf.range(size/2), tile.y + Mathf.range(size/2));
if(other != null){
Puddle.deposit(other, liquid, splash);
}
});
Liquid liquid = tile.entity.liquids.liquid;
float splash = Mathf.clamp(tile.entity.liquids.amount / 4f, 0f, 10f);
for (int i = 0; i < Mathf.clamp(tile.entity.liquids.amount / 5, 0, 30); i++) {
Timers.run(i / 2, () -> {
Tile other = world.tile(tile.x + Mathf.range(size / 2), tile.y + Mathf.range(size / 2));
if (other != null) {
Puddle.deposit(other, liquid, splash);
}
});
}
}
DamageArea.dynamicExplosion(x, y, flammability, explosiveness, power, tilesize * size/2f, tempColor);

View File

@@ -36,7 +36,8 @@ public class ColorMapper{
"c3a490", pair(Blocks.iron),
"161616", pair(Blocks.coal),
"6277bc", pair(Blocks.titanium),
"83bc58", pair(Blocks.thorium)
"83bc58", pair(Blocks.thorium),
"000000", pair(Blocks.space)
);
public static BlockPair get(int color){

View File

@@ -42,6 +42,7 @@ public class Conveyor extends Block{
layer = Layer.overlay;
group = BlockGroup.transportation;
hasItems = true;
autoSleep = true;
}
@Override
@@ -126,6 +127,7 @@ public class Conveyor extends Block{
int minremove = Integer.MAX_VALUE;
float speed = Math.max(this.speed - (1f - (carryCapacity - entity.carrying) / carryCapacity), 0f);
float totalMoved = 0f;
for (int i = entity.convey.size - 1; i >= 0; i--) {
long value = entity.convey.get(i);
@@ -146,6 +148,7 @@ public class Conveyor extends Block{
if (maxmove > minmove) {
pos.y += maxmove;
pos.x = Mathf.lerpDelta(pos.x, 0, 0.06f);
totalMoved += maxmove;
} else {
pos.x = Mathf.lerpDelta(pos.x, pos.seed / offsetScl, 0.1f);
}
@@ -154,6 +157,7 @@ public class Conveyor extends Block{
if (pos.y >= 0.9999f && offloadDir(tile, pos.item)) {
minremove = Math.min(i, minremove);
totalMoved = 1f;
tile.entity.items.removeItem(pos.item, 1);
} else {
value = pos.pack();
@@ -167,6 +171,10 @@ public class Conveyor extends Block{
entity.carrying = 0f;
entity.minCarry = 2f;
if(totalMoved <= 0.0001f){
entity.sleep();
}
if (minremove != Integer.MAX_VALUE) entity.convey.truncate(minremove);
}
@@ -179,6 +187,7 @@ public class Conveyor extends Block{
@Override
public synchronized int removeStack(Tile tile, Item item, int amount) {
ConveyorEntity entity = tile.entity();
entity.wakeUp();
int removed = 0;
for(int j = 0; j < amount; j ++) {
@@ -214,6 +223,7 @@ public class Conveyor extends Block{
long result = ItemPos.packItem(item, 0f, 0f, (byte)Mathf.random(255));
entity.convey.insert(0, result);
entity.items.addItem(item, 1);
entity.wakeUp();
}
@Override
@@ -235,6 +245,7 @@ public class Conveyor extends Block{
float y = (ang == -1 || ang == 3) ? 1 : (ang == 1 || ang == -3) ? -1 : 0;
ConveyorEntity entity = tile.entity();
entity.wakeUp();
long result = ItemPos.packItem(item, y*0.9f, pos, (byte)Mathf.random(255));
boolean inserted = false;

View File

@@ -14,17 +14,24 @@ public class Router extends Block{
solid = true;
itemCapacity = 20;
group = BlockGroup.transportation;
autoSleep = true;
}
@Override
public void update(Tile tile){
int iterations = Math.max(1, (int) (Timers.delta() + 0.4f));
boolean moved = false;
for(int i = 0; i < iterations; i ++) {
if (tile.entity.items.totalItems() > 0) {
tryDump(tile);
moved = true;
}
}
if(!moved){
tile.entity.sleep();
}
}
@Override
@@ -40,6 +47,7 @@ public class Router extends Block{
@Override
public void handleItem(Item item, Tile tile, Tile source){
super.handleItem(item, tile, source);
tile.entity.wakeUp();
tile.setExtra(tile.relativeTo(source.x, source.y));
}