More cleanup, nearing completion of block refactor

This commit is contained in:
Anuken
2020-03-06 12:19:26 -05:00
parent 1c48c1a43e
commit 4744465986
47 changed files with 1405 additions and 1361 deletions

View File

@@ -13,7 +13,6 @@ import arc.struct.*;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import arc.util.io.*;
import arc.util.pooling.*;
import mindustry.annotations.Annotations.*;
import mindustry.content.*;
import mindustry.ctype.*;
@@ -28,7 +27,6 @@ import mindustry.world.*;
import mindustry.world.blocks.environment.*;
import mindustry.world.blocks.power.*;
import mindustry.world.consumers.*;
import mindustry.world.meta.*;
import mindustry.world.modules.*;
import static mindustry.Vars.*;
@@ -134,6 +132,18 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
timeScaleDuration = Math.max(timeScaleDuration, duration);
}
public Tilec nearby(int dx, int dy){
return world.ent(tile.x + dx, tile.y + dy);
}
public Tilec nearby(int rotation){
if(rotation == 0) return world.ent(tile.x + 1, tile.y);
if(rotation == 1) return world.ent(tile.x, tile.y + 1);
if(rotation == 2) return world.ent(tile.x - 1, tile.y);
if(rotation == 3) return world.ent(tile.x, tile.y - 1);
return null;
}
public byte relativeTo(Tile tile){
return relativeTo(tile.x, tile.y);
}
@@ -155,6 +165,22 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
return -1;
}
public @Nullable Tilec front(){
return nearby((rotation() + 4) % 4);
}
public @Nullable Tilec right(){
return nearby((rotation() + 3) % 4);
}
public @Nullable Tilec back(){
return nearby((rotation() + 2) % 4);
}
public @Nullable Tilec left(){
return nearby((rotation() + 1) % 4);
}
public int pos(){
return pos();
}
@@ -551,36 +577,6 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
public void drawSelect(){
}
public float drawPlaceText(String text, int x, int y, boolean valid){
if(renderer.pixelator.enabled()) return 0;
Color color = valid ? Pal.accent : Pal.remove;
BitmapFont font = Fonts.outline;
GlyphLayout layout = Pools.obtain(GlyphLayout.class, GlyphLayout::new);
boolean ints = font.usesIntegerPositions();
font.setUseIntegerPositions(false);
font.getData().setScale(1f / 4f / Scl.scl(1f));
layout.setText(font, text);
float width = layout.width;
font.setColor(color);
float dx = x * tilesize + block.offset(), dy = y * tilesize + block.offset() + block.size * tilesize / 2f + 3;
font.draw(text, dx, dy + layout.height + 1, Align.center);
dy -= 1f;
Lines.stroke(2f, Color.darkGray);
Lines.line(dx - layout.width / 2f - 2f, dy, dx + layout.width / 2f + 1.5f, dy);
Lines.stroke(1f, color);
Lines.line(dx - layout.width / 2f - 2f, dy, dx + layout.width / 2f + 1.5f, dy);
font.setUseIntegerPositions(ints);
font.setColor(Color.white);
font.getData().setScale(1f);
Draw.reset();
Pools.free(layout);
return width;
}
public void draw(){
Draw.rect(block.region, x, y, block.rotate ? rotation() * 90 : 0);
}
@@ -658,16 +654,6 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
}
}
public float sumAttribute(Attribute attr, int x, int y){
Tile tile = world.tile(x, y);
if(tile == null) return 0;
float sum = 0;
for(Tile other : tile.getLinkedTilesAs(block, tempTiles)){
sum += other.floor().attributes.get(attr);
}
return sum;
}
public float percentSolid(int x, int y){
Tile tile = world.tile(x, y);
if(tile == null) return 0;
@@ -678,7 +664,17 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
return sum / block.size / block.size;
}
/** Called when the block is tapped. This is equivalent to being configured with null. */
/** Called when arbitrary configuration is applied to a tile. */
public void configured(@Nullable Playerc player, @Nullable Object value){
//null is of type Void.class; anonymous classes use their superclass.
Class<?> type = value == null ? void.class : value.getClass().isAnonymousClass() ? value.getClass().getSuperclass() : value.getClass();
if(block.configurations.containsKey(type)){
block.configurations.get(type).get(tile, value);
}
}
/** Called when the block is tapped.*/
public void tapped(Playerc player){
}

View File

@@ -13,7 +13,7 @@ import arc.struct.Array;
import arc.struct.EnumSet;
import arc.struct.*;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import arc.util.pooling.*;
import mindustry.annotations.Annotations.*;
import mindustry.ctype.*;
import mindustry.entities.*;
@@ -31,7 +31,7 @@ import mindustry.world.meta.values.*;
import java.lang.reflect.*;
import java.util.*;
import static mindustry.Vars.tilesize;
import static mindustry.Vars.*;
public class Block extends UnlockableContent{
public static final int crackRegions = 8, maxCrackSize = 5;
@@ -157,7 +157,8 @@ public class Block extends UnlockableContent{
protected Prov<Tilec> entityType = null; //initialized later
protected TextureRegion[] cacheRegions = {};
protected Array<String> cacheRegionStrings = new Array<>();
protected ObjectMap<Class<?>, Cons2> configurations = new ObjectMap<>();
//TODO move
public ObjectMap<Class<?>, Cons2> configurations = new ObjectMap<>();
//TODO move
protected TextureRegion[] generatedIcons;
@@ -166,6 +167,7 @@ public class Block extends UnlockableContent{
//TODO move
public static TextureRegion[][] cracks;
protected static final Array<Tile> tempTiles = new Array<>();
/** Dump timer ID.*/
protected final int timerDump = timers++;
@@ -187,10 +189,62 @@ public class Block extends UnlockableContent{
}
}
public void drawLayer(Tile tile){
if(tile.entity != null) tile.entity.drawLayer();
}
public void drawLayer2(Tile tile){
if(tile.entity != null) tile.entity.drawLayer2();
}
/** Drawn when you are placing a block. */
public void drawPlace(int x, int y, int rotation, boolean valid){
}
public float drawPlaceText(String text, int x, int y, boolean valid){
if(renderer.pixelator.enabled()) return 0;
Color color = valid ? Pal.accent : Pal.remove;
BitmapFont font = Fonts.outline;
GlyphLayout layout = Pools.obtain(GlyphLayout.class, GlyphLayout::new);
boolean ints = font.usesIntegerPositions();
font.setUseIntegerPositions(false);
font.getData().setScale(1f / 4f / Scl.scl(1f));
layout.setText(font, text);
float width = layout.width;
font.setColor(color);
float dx = x * tilesize + offset(), dy = y * tilesize + offset() + size * tilesize / 2f + 3;
font.draw(text, dx, dy + layout.height + 1, Align.center);
dy -= 1f;
Lines.stroke(2f, Color.darkGray);
Lines.line(dx - layout.width / 2f - 2f, dy, dx + layout.width / 2f + 1.5f, dy);
Lines.stroke(1f, color);
Lines.line(dx - layout.width / 2f - 2f, dy, dx + layout.width / 2f + 1.5f, dy);
font.setUseIntegerPositions(ints);
font.setColor(Color.white);
font.getData().setScale(1f);
Draw.reset();
Pools.free(layout);
return width;
}
public float sumAttribute(Attribute attr, int x, int y){
Tile tile = world.tile(x, y);
if(tile == null) return 0;
float sum = 0;
for(Tile other : tile.getLinkedTilesAs(this, tempTiles)){
sum += other.floor().attributes.get(attr);
}
return sum;
}
public String getDisplayName(Tile tile){
return tile.entity == null ? localizedName : tile.entity.getDisplayName();
}
/** @return a custom minimap color for this or 0 to use default colors. */
public int minimapColor(Tile tile){
return 0;
@@ -322,16 +376,6 @@ public class Block extends UnlockableContent{
}
/** Called when arbitrary configuration is applied to a tile. */
public void configured(Tilec tile, @Nullable Playerc player, @Nullable Object value){
//null is of type Void.class; anonymous classes use their superclass.
Class<?> type = value == null ? void.class : value.getClass().isAnonymousClass() ? value.getClass().getSuperclass() : value.getClass();
if(configurations.containsKey(type)){
configurations.get(type).get(tile, value);
}
}
/** Configure when a null value is passed.*/
public void configClear(Cons<Tilec> cons){
configurations.put(void.class, (tile, value) -> cons.get((Tilec)tile));

View File

@@ -181,11 +181,11 @@ public class Conveyor extends Block implements Autotiler{
float centerx = 0f, centery = 0f;
if(Math.abs(tx) > Math.abs(ty)){
centery = Mathf.clamp((tile.worldy() - unit.y()) / centerDstScl, -centerSpeed, centerSpeed);
if(Math.abs(tile.worldy() - unit.y()) < 1f) centery = 0f;
centery = Mathf.clamp((y - unit.y()) / centerDstScl, -centerSpeed, centerSpeed);
if(Math.abs(y - unit.y()) < 1f) centery = 0f;
}else{
centerx = Mathf.clamp((tile.worldx() - unit.x()) / centerDstScl, -centerSpeed, centerSpeed);
if(Math.abs(tile.worldx() - unit.x()) < 1f) centerx = 0f;
centerx = Mathf.clamp((x - unit.x()) / centerDstScl, -centerSpeed, centerSpeed);
if(Math.abs(x - unit.x()) < 1f) centerx = 0f;
}
if(len * itemSpace < 0.9f){

View File

@@ -14,50 +14,52 @@ public class ExtendingItemBridge extends ItemBridge{
super(name);
hasItems = true;
}
public class ExtendingItemBridgeEntity extends ItemBridgeEntity{
@Override
public void drawLayer(){
Tile other = world.tile(link);
if(!linkValid(tile, other)) return;
@Override
public void drawLayer(){
Tile other = world.tile(link);
if(!linkValid(tile, other)) return;
int i = tile.absoluteRelativeTo(other.x, other.y);
int i = tile.absoluteRelativeTo(other.x, other.y);
float ex = other.worldx() - x - Geometry.d4[i].x * tilesize / 2f,
ey = other.worldy() - y - Geometry.d4[i].y * tilesize / 2f;
float ex = other.worldx() - tile.worldx() - Geometry.d4[i].x * tilesize / 2f,
ey = other.worldy() - tile.worldy() - Geometry.d4[i].y * tilesize / 2f;
float uptime = state.isEditor() ? 1f : this.uptime;
float uptime = state.isEditor() ? 1f : uptime;
ex *= uptime;
ey *= uptime;
ex *= uptime;
ey *= uptime;
float opacity = Core.settings.getInt("bridgeopacity") / 100f;
if(Mathf.zero(opacity)) return;
Draw.alpha(opacity);
float opacity = Core.settings.getInt("bridgeopacity") / 100f;
if(Mathf.zero(opacity)) return;
Draw.alpha(opacity);
Lines.stroke(8f);
Lines.line(bridgeRegion,
x + Geometry.d4[i].x * tilesize / 2f,
y + Geometry.d4[i].y * tilesize / 2f,
x + ex,
y + ey, CapStyle.none, 0f);
Lines.stroke(8f);
Lines.line(bridgeRegion,
tile.worldx() + Geometry.d4[i].x * tilesize / 2f,
tile.worldy() + Geometry.d4[i].y * tilesize / 2f,
tile.worldx() + ex,
tile.worldy() + ey, CapStyle.none, 0f);
Draw.rect(endRegion, x, y, i * 90 + 90);
Draw.rect(endRegion,
x + ex + Geometry.d4[i].x * tilesize / 2f,
y + ey + Geometry.d4[i].y * tilesize / 2f, i * 90 + 270);
Draw.rect(endRegion, x, y, i * 90 + 90);
Draw.rect(endRegion,
tile.worldx() + ex + Geometry.d4[i].x * tilesize / 2f,
tile.worldy() + ey + Geometry.d4[i].y * tilesize / 2f, i * 90 + 270);
int dist = Math.max(Math.abs(other.x - tile.x), Math.abs(other.y - tile.y));
int dist = Math.max(Math.abs(other.x - tile.x), Math.abs(other.y - tile.y));
int arrows = (dist) * tilesize / 6 - 1;
int arrows = (dist) * tilesize / 6 - 1;
Draw.color();
Draw.color();
for(int a = 0; a < arrows; a++){
Draw.alpha(Mathf.absin(a / (float)arrows - time / 100f, 0.1f, 1f) * uptime * opacity);
Draw.rect(arrowRegion,
tile.worldx() + Geometry.d4[i].x * (tilesize / 2f + a * 6f + 2) * uptime,
tile.worldy() + Geometry.d4[i].y * (tilesize / 2f + a * 6f + 2) * uptime, i * 90f);
for(int a = 0; a < arrows; a++){
Draw.alpha(Mathf.absin(a / (float)arrows - time / 100f, 0.1f, 1f) * uptime * opacity);
Draw.rect(arrowRegion,
x + Geometry.d4[i].x * (tilesize / 2f + a * 6f + 2) * uptime,
y + Geometry.d4[i].y * (tilesize / 2f + a * 6f + 2) * uptime, i * 90f);
}
Draw.reset();
}
Draw.reset();
}
}

View File

@@ -170,7 +170,7 @@ public class ItemBridge extends Block{
@Override
public boolean onConfigureTileTapped(Tilec other){
if(linkValid(tile, other)){
if(linkValid(tile, other.tile())){
if(link == other.pos()){
tile.configure(-1);
}else{
@@ -195,12 +195,12 @@ public class ItemBridge extends Block{
}
}
Tilec other = world.tile(link);
Tile other = world.tile(link);
if(!linkValid(tile, other)){
dump(tile);
dump();
uptime = 0f;
}else{
((ItemBridgeEntity)world.tile(link).entity).incoming.add(tile.pos());
((ItemBridgeEntity)other.entity).incoming.add(tile.pos());
if(consValid() && Mathf.zero(1f - efficiency())){
uptime = Mathf.lerpDelta(uptime, 1f, 0.04f);
@@ -208,7 +208,7 @@ public class ItemBridge extends Block{
uptime = Mathf.lerpDelta(uptime, 0f, 0.02f);
}
updateTransport(other);
updateTransport(other.entity);
}
}
@@ -243,8 +243,8 @@ public class ItemBridge extends Block{
Lines.stroke(8f);
Lines.line(bridgeRegion,
tile.worldx(),
tile.worldy(),
x,
y,
other.worldx(),
other.worldy(), CapStyle.none, -tilesize / 2f);
@@ -258,8 +258,8 @@ public class ItemBridge extends Block{
for(int a = 0; a < arrows; a++){
Draw.alpha(Mathf.absin(a / (float)arrows - time / 100f, 0.1f, 1f) * uptime * opacity);
Draw.rect(arrowRegion,
tile.worldx() + Geometry.d4[i].x * (tilesize / 2f + a * 4f + time % 4f),
tile.worldy() + Geometry.d4[i].y * (tilesize / 2f + a * 4f + time % 4f), i * 90f);
x + Geometry.d4[i].x * (tilesize / 2f + a * 4f + time % 4f),
y + Geometry.d4[i].y * (tilesize / 2f + a * 4f + time % 4f), i * 90f);
}
Draw.reset();
}
@@ -276,7 +276,7 @@ public class ItemBridge extends Block{
if(rel == rel2) return false;
}else{
return source.block() instanceof ItemBridge && source.<ItemBridgeEntity>ent().link == tile.pos() && items.total() < itemCapacity;
return source.block() instanceof ItemBridge && ((ItemBridgeEntity)source).link == tile.pos() && items.total() < itemCapacity;
}
return items.total() < itemCapacity;
@@ -318,7 +318,7 @@ public class ItemBridge extends Block{
int rel2 = tile.relativeTo(source.tileX(), source.tileY());
if(rel == rel2) return false;
}else if(!(source.block() instanceof ItemBridge && source.<ItemBridgeEntity>ent().link == tile.pos())){
}else if(!(source.block() instanceof ItemBridge && ((ItemBridgeEntity)source).link == tile.pos())){
return false;
}
@@ -329,7 +329,7 @@ public class ItemBridge extends Block{
public boolean canDump(Tilec to, Item item){
Tile other = world.tile(link);
if(!linkValid(tile, other)){
Tile edge = Edges.getFacingEdge(to, tile);
Tile edge = Edges.getFacingEdge(to.tile(), tile);
int i = tile.absoluteRelativeTo(edge.x, edge.y);
IntSetIterator it = incoming.iterator();

View File

@@ -1,14 +1,11 @@
package mindustry.world.blocks.distribution;
import arc.util.Time;
import arc.util.*;
import arc.util.io.*;
import mindustry.gen.*;
import mindustry.gen.BufferItem;
import mindustry.type.Item;
import mindustry.world.Block;
import mindustry.world.DirectionalItemBuffer;
import mindustry.world.Tile;
import mindustry.world.meta.BlockGroup;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import static mindustry.Vars.content;
@@ -49,15 +46,14 @@ public class Junction extends Block{
if(Time.time() >= time + speed || Time.time() < time){
Item item = content.item(BufferItem.item(l));
Tile dest = tile.getNearby(i);
if(dest != null) dest = dest.link();
Tilec dest = nearby(i);
//skip blocks that don't want the item, keep waiting until they do
if(dest == null || !dest.block().acceptItem(dest, tile, item) || dest.team() != team){
if(dest == null || !dest.acceptItem(this, item) || dest.team() != team){
continue;
}
dest.block().handleItem(dest, tile, item);
dest.handleItem(this, item);
System.arraycopy(buffer.buffers[i], 1, buffer.buffers[i], 0, buffer.indexes[i] - 1);
buffer.indexes[i] --;
}
@@ -66,18 +62,18 @@ public class Junction extends Block{
}
@Override
public void handleItem(Tile source, Item item){
public void handleItem(Tilec source, Item item){
int relative = source.relativeTo(tile.x, tile.y);
buffer.accept(relative, item);
}
@Override
public boolean acceptItem(Tile source, Item item){
public boolean acceptItem(Tilec source, Item item){
int relative = source.relativeTo(tile.x, tile.y);
if(relative == -1 || !buffer.accepts(relative)) return false;
Tile to = tile.getNearby(relative);
return to != null && to.link().entity != null && to.team() == team;
Tilec to = nearby(relative);
return to != null && to.team() == team;
}
@Override

View File

@@ -212,7 +212,7 @@ public class MassDriver extends Block{
}
@Override
public boolean acceptItem(Tile source, Item item){
public boolean acceptItem(Tilec source, Item item){
//mass drivers that ouput only cannot accept items
return tile.items.total() < itemCapacity && linkValid(tile);
}

View File

@@ -28,100 +28,99 @@ public class OverflowGate extends Block{
return true;
}
@Override
public int acceptStack(Item item, int amount, Teamc source){
return 0;
}
@Override
public int removeStack(Item item, int amount){
int result = super.removeStack(tile, item, amount);
if(result != 0 && item == lastItem){
lastItem = null;
}
return result;
}
@Override
public void updateTile(){
if(lastItem == null && items.total() > 0){
items.clear();
}
if(lastItem != null){
if(lastInput == null){
lastItem = null;
return;
}
time += 1f / speed * Time.delta();
Tile target = getTileTarget(tile, lastItem, lastInput, false);
if(target != null && (time >= 1f)){
getTileTarget(tile, lastItem, lastInput, true);
target.block().handleItem(target, Edges.getFacingEdge(tile, target), lastItem);
items.remove(lastItem, 1);
lastItem = null;
}
}
}
@Override
public boolean acceptItem(Tile source, Item item){
return team == source.team() && lastItem == null && items.total() == 0;
}
@Override
public void handleItem(Tile source, Item item){
items.add(item, 1);
lastItem = item;
time = 0f;
lastInput = source;
update(tile);
}
public Tile getTileTarget(Item item, Tile src, boolean flip){
int from = tile.relativeTo(src.x, src.y);
if(from == -1) return null;
Tile to = tile.getNearby((from + 2) % 4);
if(to == null) return null;
Tile edge = Edges.getFacingEdge(tile, to);
boolean canForward = to.block().acceptItem(to, edge, item) && to.team() == team && !(to.block() instanceof OverflowGate);
if(!canForward || invert){
Tile a = tile.getNearby(Mathf.mod(from - 1, 4));
Tile b = tile.getNearby(Mathf.mod(from + 1, 4));
boolean ac = a != null && a.block().acceptItem(a, edge, item) && !(a.block() instanceof OverflowGate) && a.team() == team;
boolean bc = b != null && b.block().acceptItem(b, edge, item) && !(b.block() instanceof OverflowGate) && b.team() == team;
if(!ac && !bc){
return invert && canForward ? to : null;
}
if(ac && !bc){
to = a;
}else if(bc && !ac){
to = b;
}else{
if(tile.rotation() == 0){
to = a;
if(flip) tile.rotation((byte) 1);
}else{
to = b;
if(flip) tile.rotation((byte) 0);
}
}
}
return to;
}
public class OverflowGateEntity extends TileEntity{
Item lastItem;
Tile lastInput;
float time;
@Override
public int acceptStack(Item item, int amount, Teamc source){
return 0;
}
@Override
public int removeStack(Item item, int amount){
int result = super.removeStack(item, amount);
if(result != 0 && item == lastItem){
lastItem = null;
}
return result;
}
@Override
public void updateTile(){
if(lastItem == null && items.total() > 0){
items.clear();
}
if(lastItem != null){
if(lastInput == null){
lastItem = null;
return;
}
time += 1f / speed * Time.delta();
Tilec target = getTileTarget(lastItem, lastInput, false);
if(target != null && (time >= 1f)){
getTileTarget(lastItem, lastInput, true);
target.handleItem(this, lastItem);
items.remove(lastItem, 1);
lastItem = null;
}
}
}
@Override
public boolean acceptItem(Tilec source, Item item){
return team == source.team() && lastItem == null && items.total() == 0;
}
@Override
public void handleItem(Tilec source, Item item){
items.add(item, 1);
lastItem = item;
time = 0f;
lastInput = source.tile();
updateTile();
}
public Tilec getTileTarget(Item item, Tile src, boolean flip){
int from = relativeTo(src.x, src.y);
if(from == -1) return null;
Tilec to = nearby((from + 2) % 4);
if(to == null) return null;
boolean canForward = to.acceptItem(this, item) && to.team() == team && !(to.block() instanceof OverflowGate);
if(!canForward || invert){
Tilec a = nearby(Mathf.mod(from - 1, 4));
Tilec b = nearby(Mathf.mod(from + 1, 4));
boolean ac = a != null && a.acceptItem(this, item) && !(a.block() instanceof OverflowGate) && a.team() == team;
boolean bc = b != null && b.acceptItem(this, item) && !(b.block() instanceof OverflowGate) && b.team() == team;
if(!ac && !bc){
return invert && canForward ? to : null;
}
if(ac && !bc){
to = a;
}else if(bc && !ac){
to = b;
}else{
if(tile.rotation() == 0){
to = a;
if(flip) tile.rotation((byte) 1);
}else{
to = b;
if(flip) tile.rotation((byte) 0);
}
}
}
return to;
}
@Override
public byte version(){
return 3;

View File

@@ -1,12 +1,11 @@
package mindustry.world.blocks.distribution;
import arc.struct.Array;
import arc.util.Time;
import arc.util.*;
import mindustry.content.*;
import mindustry.gen.*;
import mindustry.type.Item;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.meta.BlockGroup;
import mindustry.world.meta.*;
public class Router extends Block{
public float speed = 8f;
@@ -21,64 +20,63 @@ public class Router extends Block{
unloadable = false;
}
@Override
public void updateTile(){
if(lastItem == null && items.total() > 0){
items.clear();
}
if(lastItem != null){
time += 1f / speed * Time.delta();
Tile target = getTileTarget(tile, lastItem, lastInput, false);
if(target != null && (time >= 1f || !(target.block() instanceof Router))){
getTileTarget(tile, lastItem, lastInput, true);
target.block().handleItem(target, Edges.getFacingEdge(tile, target), lastItem);
items.remove(lastItem, 1);
lastItem = null;
}
}
}
@Override
public boolean acceptItem(Tile source, Item item){
return team == source.team() && lastItem == null && items.total() == 0;
}
@Override
public void handleItem(Tile source, Item item){
items.add(item, 1);
lastItem = item;
time = 0f;
lastInput = source;
}
Tile getTileTarget(Item item, Tile from, boolean set){
Array<Tile> proximity = tile.proximity();
int counter = tile.rotation();
for(int i = 0; i < proximity.size; i++){
Tile other = proximity.get((i + counter) % proximity.size);
if(set) tile.rotation((byte)((tile.rotation() + 1) % proximity.size));
if(other == from && from.block() == Blocks.overflowGate) continue;
if(other.block().acceptItem(other, Edges.getFacingEdge(tile, other), item)){
return other;
}
}
return null;
}
@Override
public int removeStack(Item item, int amount){
int result = super.removeStack(tile, item, amount);
if(result != 0 && item == lastItem){
lastItem = null;
}
return result;
}
public class RouterEntity extends TileEntity{
Item lastItem;
Tile lastInput;
float time;
@Override
public void updateTile(){
if(lastItem == null && items.total() > 0){
items.clear();
}
if(lastItem != null){
time += 1f / speed * Time.delta();
Tilec target = getTileTarget(lastItem, lastInput, false);
if(target != null && (time >= 1f || !(target.block() instanceof Router))){
getTileTarget(lastItem, lastInput, true);
target.handleItem(this, lastItem);
items.remove(lastItem, 1);
lastItem = null;
}
}
}
@Override
public boolean acceptItem(Tilec source, Item item){
return team == source.team() && lastItem == null && items.total() == 0;
}
@Override
public void handleItem(Tilec source, Item item){
items.add(item, 1);
lastItem = item;
time = 0f;
lastInput = source.tile();
}
Tilec getTileTarget(Item item, Tile from, boolean set){
int counter = tile.rotation();
for(int i = 0; i < proximity.size; i++){
Tilec other = proximity.get((i + counter) % proximity.size);
if(set) tile.rotation((byte)((tile.rotation() + 1) % proximity.size));
if(other.tile() == from && from.block() == Blocks.overflowGate) continue;
if(other.acceptItem(this, item)){
return other;
}
}
return null;
}
@Override
public int removeStack(Item item, int amount){
int result = super.removeStack(item, amount);
if(result != 0 && item == lastItem){
lastItem = null;
}
return result;
}
}
}

View File

@@ -27,29 +27,8 @@ public class Sorter extends Block{
group = BlockGroup.transportation;
configurable = true;
unloadable = false;
config(Item.class, (tile, item) -> tile.<SorterEntity>ent().sortItem = item);
configClear(tile -> tile.<SorterEntity>ent().sortItem = null);
}
@Override
public boolean outputsItems(){
return true;
}
@Override
public void playerPlaced(){
if(lastItem != null){
tile.configure(lastItem);
}
}
@Override
public void configured(Playerc player, Object value){
super.configured(tile, player, value);
if(!headless){
renderer.minimap.update(tile);
}
config(Item.class, (tile, item) -> ((SorterEntity)tile).sortItem = item);
configClear(tile -> ((SorterEntity)tile).sortItem = null);
}
@Override
@@ -58,89 +37,108 @@ public class Sorter extends Block{
}
@Override
public void draw(){
super.draw();
if(sortItem == null) return;
Draw.color(sortItem.color);
Draw.rect("center", tile.worldx(), tile.worldy());
Draw.color();
public boolean outputsItems(){
return true;
}
@Override
public int minimapColor(){
public int minimapColor(Tile tile){
return tile.<SorterEntity>ent().sortItem == null ? 0 : tile.<SorterEntity>ent().sortItem.color.rgba();
}
@Override
public boolean acceptItem(Tile source, Item item){
Tile to = getTileTarget(item, tile, source, false);
return to != null && to.block().acceptItem(to, tile, item) && to.team() == team;
}
@Override
public void handleItem(Tile source, Item item){
Tile to = getTileTarget(item, tile, source, true);
to.block().handleItem(to, tile, item);
}
boolean isSame(Tile other){
//uncomment comment below to prevent sorter/gate chaining (hacky)
return other != null && (other.block() instanceof Sorter/* || other.block() instanceof OverflowGate */);
}
Tile getTileTarget(Item item, Tile dest, Tile source, boolean flip){
SorterEntity entity = dest.ent();
int dir = source.relativeTo(dest.x, dest.y);
if(dir == -1) return null;
Tile to;
if((item == sortItem) != invert){
//prevent 3-chains
if(isSame(dest, source) && isSame(dest, dest.getNearby(dir))){
return null;
}
to = dest.getNearby(dir);
}else{
Tile a = dest.getNearby(Mathf.mod(dir - 1, 4));
Tile b = dest.getNearby(Mathf.mod(dir + 1, 4));
boolean ac = a != null && !(a.block().instantTransfer && source.block().instantTransfer) &&
a.block().acceptItem(a, dest, item);
boolean bc = b != null && !(b.block().instantTransfer && source.block().instantTransfer) &&
b.block().acceptItem(b, dest, item);
if(ac && !bc){
to = a;
}else if(bc && !ac){
to = b;
}else if(!bc){
return null;
}else{
if(dest.rotation() == 0){
to = a;
if(flip) dest.rotation((byte)1);
}else{
to = b;
if(flip) dest.rotation((byte)0);
}
}
}
return to;
}
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(table, content.items(), () -> sortItem, item -> tile.configure(lastItem = item));
}
public class SorterEntity extends TileEntity{
@Nullable Item sortItem;
@Override
public void playerPlaced(){
if(lastItem != null){
tile.configure(lastItem);
}
}
@Override
public void configured(Playerc player, Object value){
super.configured(player, value);
if(!headless){
renderer.minimap.update(tile);
}
}
@Override
public void draw(){
super.draw();
if(sortItem == null) return;
Draw.color(sortItem.color);
Draw.rect("center", x, y);
Draw.color();
}
@Override
public boolean acceptItem(Tilec source, Item item){
Tilec to = getTileTarget(item, source, false);
return to != null && to.acceptItem(this, item) && to.team() == team;
}
@Override
public void handleItem(Tilec source, Item item){
Tilec to = getTileTarget(item, source, true);
to.handleItem(this, item);
}
boolean isSame(Tilec other){
//uncomment comment below to prevent sorter/gate chaining (hacky)
return other != null && (other.block() instanceof Sorter/* || other.block() instanceof OverflowGate */);
}
Tilec getTileTarget(Item item, Tilec source, boolean flip){
int dir = source.relativeTo(tile.x, tile.y);
if(dir == -1) return null;
Tilec to;
if((item == sortItem) != invert){
//prevent 3-chains
if(isSame(source) && isSame(nearby(dir))){
return null;
}
to = nearby(dir);
}else{
Tilec a = nearby(Mathf.mod(dir - 1, 4));
Tilec b = nearby(Mathf.mod(dir + 1, 4));
boolean ac = a != null && !(a.block().instantTransfer && source.block().instantTransfer) &&
a.acceptItem(this, item);
boolean bc = b != null && !(b.block().instantTransfer && source.block().instantTransfer) &&
b.acceptItem(this, item);
if(ac && !bc){
to = a;
}else if(bc && !ac){
to = b;
}else if(!bc){
return null;
}else{
if(rotation() == 0){
to = a;
if(flip) rotation((byte)1);
}else{
to = b;
if(flip) rotation((byte)0);
}
}
}
return to;
}
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(table, content.items(), () -> sortItem, item -> tile.configure(lastItem = item));
}
@Override
public Item config(){
return sortItem;

View File

@@ -17,7 +17,7 @@ public class Cliff extends Block{
}
@Override
public void drawBase(){
public void drawBase(Tile tile){
int r = tile.rotation();
for(int i = 0; i < 8; i++){
if((r & (1 << i)) != 0){
@@ -30,7 +30,7 @@ public class Cliff extends Block{
}
@Override
public int minimapColor(){
public int minimapColor(Tile tile){
return Tmp.c1.set(tile.floor().mapColor).mul(1.2f).rgba();
}
}

View File

@@ -1,7 +1,8 @@
package mindustry.world.blocks.environment;
import arc.graphics.g2d.Draw;
import arc.math.Mathf;
import arc.graphics.g2d.*;
import arc.math.*;
import mindustry.world.*;
public class DoubleOverlayFloor extends OverlayFloor{
@@ -10,7 +11,7 @@ public class DoubleOverlayFloor extends OverlayFloor{
}
@Override
public void draw(){
public void drawBase(Tile tile){
Draw.colorl(0.4f);
Draw.rect(variantRegions[Mathf.randomSeed(tile.pos(), 0, Math.max(0, variantRegions.length - 1))], tile.worldx(), tile.worldy() - 0.75f);
Draw.color();

View File

@@ -153,7 +153,7 @@ public class Floor extends Block{
}
@Override
public void drawBase(){
public void drawBase(Tile tile){
Mathf.random.setSeed(tile.pos());
Draw.rect(variantRegions[Mathf.randomSeed(tile.pos(), 0, Math.max(0, variantRegions.length - 1))], tile.worldx(), tile.worldy());
@@ -170,17 +170,17 @@ public class Floor extends Block{
return drownTime > 0;
}
public void drawNonLayer(){
public void drawNonLayer(Tile tile){
Mathf.random.setSeed(tile.pos());
drawEdges(tile, true);
}
protected void drawEdges(){
protected void drawEdges(Tile tile){
drawEdges(tile, false);
}
protected void drawEdges(boolean sameLayer){
protected void drawEdges(Tile tile, boolean sameLayer){
blenders.clear();
blended.clear();
eq = 0;
@@ -216,7 +216,7 @@ public class Floor extends Block{
}
//'new' style of edges with shadows instead of colors, not used currently
protected void drawEdgesFlat(boolean sameLayer){
protected void drawEdgesFlat(Tile tile, boolean sameLayer){
for(int i = 0; i < 4; i++){
Tile other = tile.getNearby(i);
if(other != null && doEdge(other.floor(), sameLayer)){

View File

@@ -79,7 +79,7 @@ public class OreBlock extends OverlayFloor{
}
@Override
public String getDisplayName(){
public String getDisplayName(Tile tile){
return itemDrop.localizedName;
}
}

View File

@@ -12,7 +12,7 @@ public class OverlayFloor extends Floor{
}
@Override
public void drawBase(){
public void drawBase(Tile tile){
Draw.rect(variantRegions[Mathf.randomSeed(tile.pos(), 0, Math.max(0, variantRegions.length - 1))], tile.worldx(), tile.worldy());
}
}

View File

@@ -17,7 +17,7 @@ public class Rock extends Block{
}
@Override
public void draw(){
public void drawBase(Tile tile){
if(variants > 0){
Draw.rect(variantRegions[Mathf.randomSeed(tile.pos(), 0, Math.max(0, variantRegions.length - 1))], tile.worldx(), tile.worldy());
}else{

View File

@@ -13,7 +13,7 @@ public class StaticTree extends StaticWall{
}
@Override
public void drawBase(){
public void drawBase(Tile tile){
TextureRegion r = Tmp.tr1;
r.set(region);
int crop = (region.getWidth() - tilesize*4) / 2;
@@ -38,6 +38,6 @@ public class StaticTree extends StaticWall{
}
}
}
Draw.rect(r, x + ox * Draw.scl, y + oy * Draw.scl);
Draw.rect(r, tile.drawx() + ox * Draw.scl, tile.drawy() + oy * Draw.scl);
}
}

View File

@@ -22,7 +22,7 @@ public class StaticWall extends Rock{
}
@Override
public void drawBase(){
public void drawBase(Tile tile){
int rx = tile.x / 2 * 2;
int ry = tile.y / 2 * 2;

View File

@@ -16,10 +16,10 @@ public class TreeBlock extends Block{
}
@Override
public void drawBase(){}
public void drawBase(Tile tile){}
@Override
public void drawLayer(){
Draw.rect(region, x, y, Mathf.randomSeed(tile.pos(), 0, 4) * 90);
public void drawLayer(Tile tile){
Draw.rect(region, tile.worldx(), tile.worldy(), Mathf.randomSeed(tile.pos(), 0, 4) * 90);
}
}

View File

@@ -2,7 +2,7 @@ package mindustry.world.blocks.liquid;
import arc.*;
import arc.graphics.g2d.*;
import mindustry.entities.AllDefs.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
@@ -20,24 +20,26 @@ public class ArmoredConduit extends Conduit{
capRegion = Core.atlas.find(name + "-cap");
}
@Override
public void draw(){
super.draw();
// draw the cap when a conduit would normally leak
Tile next = tile.front();
if(next != null && next.team() == team && next.block().hasLiquids) return;
Draw.rect(capRegion, x, y, tile.rotation() * 90);
}
@Override
public boolean acceptLiquid(Tile source, Liquid liquid, float amount){
return super.acceptLiquid(tile, source, liquid, amount) && (source.block() instanceof Conduit) || Edges.getFacingEdge(source, tile).relativeTo(tile) == tile.rotation();
}
@Override
public boolean blends(int rotation, int otherx, int othery, int otherrot, Block otherblock){
return otherblock.outputsLiquid && blendsArmored(tile, rotation, otherx, othery, otherrot, otherblock);
return otherblock.outputsLiquid && blendsArmored(rotation, otherx, othery, otherrot, otherblock);
}
public class ArmoredConduitEntity extends TileEntity{
@Override
public void draw(){
super.draw();
// draw the cap when a conduit would normally leak
Tilec next = tile.front();
if(next != null && next.team() == team && next.block().hasLiquids) return;
Draw.rect(capRegion, x, y, tile.rotation() * 90);
}
@Override
public boolean acceptLiquid(Tilec source, Liquid liquid, float amount){
return super.acceptLiquid(source, liquid, amount) && (source.block() instanceof Conduit) || Edges.getFacingEdge(source.tile(), tile).relativeTo(tile) == tile.rotation();
}
}
}

View File

@@ -75,7 +75,7 @@ public class Conduit extends LiquidBlock implements Autotiler{
@Override
public boolean blends(int rotation, int otherx, int othery, int otherrot, Block otherblock){
return otherblock.hasLiquids && otherblock.outputsLiquid && lookingAt(tile, rotation, otherx, othery, otherrot, otherblock);
return otherblock.hasLiquids && otherblock.outputsLiquid && lookingAt(rotation, otherx, othery, otherrot, otherblock);
}
@Override
@@ -106,7 +106,7 @@ public class Conduit extends LiquidBlock implements Autotiler{
public void onProximityUpdate(){
super.onProximityUpdate();
blendbits = buildBlending(tile, tile.rotation(), null, true)[0];
blendbits = buildBlending(rotation(), null, true)[0];
}
@Override

View File

@@ -1,10 +1,8 @@
package mindustry.world.blocks.liquid;
import arc.math.*;
import arc.util.*;
import mindustry.entities.AllDefs.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.distribution.*;
import mindustry.world.meta.*;
@@ -20,40 +18,42 @@ public class LiquidBridge extends ItemBridge{
group = BlockGroup.liquids;
}
@Override
public void updateTile(){
time += cycleSpeed * Time.delta();
time2 += (cycleSpeed - 1f) * Time.delta();
public class LiquidBridgeEntity extends ItemBridgeEntity{
@Override
public void updateTile(){
time += cycleSpeed * delta();
time2 += (cycleSpeed - 1f) * delta();
Tile other = world.tile(link);
if(!linkValid(tile, other)){
tryDumpLiquid(tile, liquids.current());
}else{
((ItemBridgeEntity)world.tile(link).entity).incoming.add(tile.pos());
if(consValid()){
float alpha = 0.04f;
if(hasPower){
alpha *= efficiency(); // Exceed boot time unless power is at max.
}
uptime = Mathf.lerpDelta(uptime, 1f, alpha);
Tilec other = world.ent(link);
if(other == null || !linkValid(tile, other.tile())){
dumpLiquid(liquids.current());
}else{
uptime = Mathf.lerpDelta(uptime, 0f, 0.02f);
}
((ItemBridgeEntity)other).incoming.add(tile.pos());
if(uptime >= 0.5f){
if(tryMoveLiquid(tile, other, false, liquids.current()) > 0.1f){
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 4f, 0.05f);
if(consValid()){
float alpha = 0.04f;
if(hasPower){
alpha *= efficiency(); // Exceed boot time unless power is at max.
}
uptime = Mathf.lerpDelta(uptime, 1f, alpha);
}else{
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 1f, 0.01f);
uptime = Mathf.lerpDelta(uptime, 0f, 0.02f);
}
if(uptime >= 0.5f){
if(moveLiquid(other, false, liquids.current()) > 0.1f){
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 4f, 0.05f);
}else{
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 1f, 0.01f);
}
}
}
}
}
@Override
public boolean acceptItem(Tile source, Item item){
return false;
@Override
public boolean acceptItem(Tilec source, Item item){
return false;
}
}
}

View File

@@ -1,10 +1,8 @@
package mindustry.world.blocks.liquid;
import arc.math.*;
import arc.util.*;
import mindustry.entities.AllDefs.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.distribution.*;
import mindustry.world.meta.*;
@@ -20,35 +18,37 @@ public class LiquidExtendingBridge extends ExtendingItemBridge{
group = BlockGroup.liquids;
}
@Override
public void updateTile(){
time += cycleSpeed * Time.delta();
time2 += (cycleSpeed - 1f) * Time.delta();
public class LiquidExtendingBridgeEntity extends ItemBridgeEntity{
@Override
public void updateTile(){
time += cycleSpeed * delta();
time2 += (cycleSpeed - 1f) * delta();
Tile other = world.tile(link);
if(!linkValid(tile, other)){
tryDumpLiquid(tile, liquids.current());
}else{
((ItemBridgeEntity)world.tile(link).entity).incoming.add(tile.pos());
if(consValid()){
uptime = Mathf.lerpDelta(uptime, 1f, 0.04f);
Tilec other = world.ent(link);
if(other == null || !linkValid(tile, other.tile())){
dumpLiquid(liquids.current());
}else{
uptime = Mathf.lerpDelta(uptime, 0f, 0.02f);
}
((ItemBridgeEntity)other).incoming.add(tile.pos());
if(uptime >= 0.5f){
if(tryMoveLiquid(tile, other, false, liquids.current()) > 0.1f){
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 4f, 0.05f);
if(consValid()){
uptime = Mathf.lerpDelta(uptime, 1f, 0.04f);
}else{
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 1f, 0.01f);
uptime = Mathf.lerpDelta(uptime, 0f, 0.02f);
}
if(uptime >= 0.5f){
if(moveLiquid(other, false, liquids.current()) > 0.1f){
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 4f, 0.05f);
}else{
cycleSpeed = Mathf.lerpDelta(cycleSpeed, 1f, 0.01f);
}
}
}
}
}
@Override
public boolean acceptItem(Tile source, Item item){
return false;
@Override
public boolean acceptItem(Tilec source, Item item){
return false;
}
}
}

View File

@@ -2,9 +2,8 @@ package mindustry.world.blocks.liquid;
import arc.*;
import arc.graphics.g2d.*;
import mindustry.entities.AllDefs.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.meta.*;
public class LiquidJunction extends LiquidBlock{
@@ -25,24 +24,28 @@ public class LiquidJunction extends LiquidBlock{
bars.remove("liquid");
}
@Override
public void draw(){
Draw.rect(name, tile.worldx(), tile.worldy());
}
@Override
public TextureRegion[] generateIcons(){
return new TextureRegion[]{Core.atlas.find(name)};
}
@Override
public Tile getLiquidDestination(Tile source, Liquid liquid){
int dir = source.relativeTo(tile.x, tile.y);
dir = (dir + 4) % 4;
Tile next = tile.getNearbyLink(dir);
if(next == null || !next.block().acceptLiquid(next, tile, liquid, 0f) && !(next.block() instanceof LiquidJunction)){
return tile;
public class LiquidJunctionEntity extends TileEntity{
@Override
public void draw(){
Draw.rect(region, x, y);
}
@Override
public Tilec getLiquidDestination(Tilec source, Liquid liquid){
int dir = source.relativeTo(tile.x, tile.y);
dir = (dir + 4) % 4;
Tilec next = nearby(dir);
if(next == null || !next.acceptLiquid(this, liquid, 0f) && !(next.block() instanceof LiquidJunction)){
return this;
}
return next.getLiquidDestination(this, liquid);
}
return next.block().getLiquidDestination(next, tile, liquid);
}
}

View File

@@ -10,7 +10,6 @@ public class LiquidRouter extends LiquidBlock{
}
public class LiquidRouterEntity extends LiquidBlockEntity{
@Override
public void updateTile(){
if(liquids.total() > 0.01f){

View File

@@ -2,7 +2,7 @@ package mindustry.world.blocks.power;
import arc.graphics.*;
import arc.graphics.g2d.*;
import mindustry.world.*;
import mindustry.gen.*;
import static mindustry.Vars.tilesize;
@@ -18,12 +18,14 @@ public class Battery extends PowerDistributor{
consumesPower = true;
}
@Override
public void draw(){
Draw.color(emptyLightColor, fullLightColor, tile.power.status);
Fill.square(x, y, tilesize * size / 2f - 1);
Draw.color();
public class BatteryEntity extends TileEntity{
@Override
public void draw(){
Draw.color(emptyLightColor, fullLightColor, power.status);
Fill.square(x, y, tilesize * size / 2f - 1);
Draw.color();
Draw.rect(reg(topRegion), x, y);
Draw.rect(reg(topRegion), x, y);
}
}
}

View File

@@ -12,11 +12,8 @@ import mindustry.game.EventType.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.ui.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import java.io.*;
import static mindustry.Vars.*;
public class ImpactReactor extends PowerGenerator{
@@ -52,7 +49,7 @@ public class ImpactReactor extends PowerGenerator{
bars.add("poweroutput", entity -> new Bar(() ->
Core.bundle.format("bar.poweroutput",
Strings.fixed(Math.max(block().getPowerProduction(tile()) - consumes.getPower().usage, 0) * 60 * timeScale(), 1)),
Strings.fixed(Math.max(entity.getPowerProduction() - consumes.getPower().usage, 0) * 60 * entity.timeScale(), 1)),
() -> Pal.powerBar,
() -> ((GeneratorEntity)entity).productionEfficiency));
}
@@ -66,97 +63,98 @@ public class ImpactReactor extends PowerGenerator{
}
}
@Override
public void updateTile(){
if(consValid() && power.status >= 0.99f){
boolean prevOut = getPowerProduction(tile) <= consumes.getPower().requestedPower(entity);
warmup = Mathf.lerpDelta(warmup, 1f, warmupSpeed);
if(Mathf.equal(warmup, 1f, 0.001f)){
warmup = 1f;
}
if(!prevOut && (getPowerProduction(tile) > consumes.getPower().requestedPower(entity))){
Events.fire(Trigger.impactPower);
}
if(timer(timerUse, itemDuration / timeScale())){
consume();
}
}else{
warmup = Mathf.lerpDelta(warmup, 0f, 0.01f);
}
productionEfficiency = Mathf.pow(warmup, 5f);
}
@Override
public void draw(){
Draw.rect(reg(bottomRegion), x, y);
for(int i = 0; i < plasmas; i++){
float r = 29f + Mathf.absin(Time.time(), 2f + i * 1f, 5f - i * 0.5f);
Draw.color(plasma1, plasma2, (float)i / plasmas);
Draw.alpha((0.3f + Mathf.absin(Time.time(), 2f + i * 2f, 0.3f + i * 0.05f)) * warmup);
Draw.blend(Blending.additive);
Draw.rect(reg(plasmaRegions[i]), x, y, r, r, Time.time() * (12 + i * 6f) * warmup);
Draw.blend();
}
Draw.color();
Draw.rect(region, x, y);
Draw.color();
}
@Override
public void drawLight(){
float fract = tile.<FusionReactorEntity>ent().warmup;
renderer.lights.add(x, y, (110f + Mathf.absin(5, 5f)) * fract, Tmp.c1.set(plasma2).lerp(plasma1, Mathf.absin(7f, 0.2f)), 0.8f * fract);
}
@Override
public TextureRegion[] generateIcons(){
return new TextureRegion[]{Core.atlas.find(name + "-bottom"), Core.atlas.find(name)};
}
@Override
public void onDestroyed(){
super.onDestroyed();
if(warmup < 0.4f || !state.rules.reactorExplosions) return;
Sounds.explosionbig.at(tile);
Effects.shake(6f, 16f, tile.worldx(), tile.worldy());
Fx.impactShockwave.at(tile.worldx(), tile.worldy());
for(int i = 0; i < 6; i++){
Time.run(Mathf.random(80), () -> Fx.impactcloud.at(tile.worldx(), tile.worldy()));
}
Damage.damage(tile.worldx(), tile.worldy(), explosionRadius * tilesize, explosionDamage * 4);
for(int i = 0; i < 20; i++){
Time.run(Mathf.random(80), () -> {
Tmp.v1.rnd(Mathf.random(40f));
Fx.explosion.at(Tmp.v1.x + tile.worldx(), Tmp.v1.y + tile.worldy());
});
}
for(int i = 0; i < 70; i++){
Time.run(Mathf.random(90), () -> {
Tmp.v1.rnd(Mathf.random(120f));
Fx.impactsmoke.at(Tmp.v1.x + tile.worldx(), Tmp.v1.y + tile.worldy());
});
}
}
public class FusionReactorEntity extends GeneratorEntity{
public float warmup;
@Override
public void updateTile(){
if(consValid() && power.status >= 0.99f){
boolean prevOut = getPowerProduction() <= consumes.getPower().requestedPower(this);
warmup = Mathf.lerpDelta(warmup, 1f, warmupSpeed);
if(Mathf.equal(warmup, 1f, 0.001f)){
warmup = 1f;
}
if(!prevOut && (getPowerProduction() > consumes.getPower().requestedPower(this))){
Events.fire(Trigger.impactPower);
}
if(timer(timerUse, itemDuration / timeScale())){
consume();
}
}else{
warmup = Mathf.lerpDelta(warmup, 0f, 0.01f);
}
productionEfficiency = Mathf.pow(warmup, 5f);
}
@Override
public void draw(){
Draw.rect(reg(bottomRegion), x, y);
for(int i = 0; i < plasmas; i++){
float r = 29f + Mathf.absin(Time.time(), 2f + i * 1f, 5f - i * 0.5f);
Draw.color(plasma1, plasma2, (float)i / plasmas);
Draw.alpha((0.3f + Mathf.absin(Time.time(), 2f + i * 2f, 0.3f + i * 0.05f)) * warmup);
Draw.blend(Blending.additive);
Draw.rect(reg(plasmaRegions[i]), x, y, r, r, Time.time() * (12 + i * 6f) * warmup);
Draw.blend();
}
Draw.color();
Draw.rect(region, x, y);
Draw.color();
}
@Override
public void drawLight(){
float fract = tile.<FusionReactorEntity>ent().warmup;
renderer.lights.add(x, y, (110f + Mathf.absin(5, 5f)) * fract, Tmp.c1.set(plasma2).lerp(plasma1, Mathf.absin(7f, 0.2f)), 0.8f * fract);
}
@Override
public void onDestroyed(){
super.onDestroyed();
if(warmup < 0.4f || !state.rules.reactorExplosions) return;
Sounds.explosionbig.at(tile);
Effects.shake(6f, 16f, x, y);
Fx.impactShockwave.at(x, y);
for(int i = 0; i < 6; i++){
Time.run(Mathf.random(80), () -> Fx.impactcloud.at(x, y));
}
Damage.damage(x, y, explosionRadius * tilesize, explosionDamage * 4);
for(int i = 0; i < 20; i++){
Time.run(Mathf.random(80), () -> {
Tmp.v1.rnd(Mathf.random(40f));
Fx.explosion.at(Tmp.v1.x + x, Tmp.v1.y + y);
});
}
for(int i = 0; i < 70; i++){
Time.run(Mathf.random(90), () -> {
Tmp.v1.rnd(Mathf.random(120f));
Fx.impactsmoke.at(Tmp.v1.x + x, Tmp.v1.y + y);
});
}
}
@Override
public void write(Writes write){
super.write(write);

View File

@@ -8,7 +8,6 @@ import arc.util.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.consumers.*;
import mindustry.world.meta.*;
@@ -84,94 +83,6 @@ public class ItemLiquidGenerator extends PowerGenerator{
}
}
@Override
public boolean productionValid(){
return generateTime > 0;
}
@Override
public void updateTile(){
//Note: Do not use this delta when calculating the amount of power or the power efficiency, but use it for resource consumption if necessary.
//Power amount is delta'd by PowerGraph class already.
float calculationDelta = delta();
if(!consValid()){
productionEfficiency = 0.0f;
return;
}
Liquid liquid = null;
for(Liquid other : content.liquids()){
if(hasLiquids && liquids.get(other) >= 0.001f && getLiquidEfficiency(other) >= minLiquidEfficiency){
liquid = other;
break;
}
}
heat = Mathf.lerpDelta(heat, generateTime >= 0.001f ? 1f : 0f, 0.05f);
//liquid takes priority over solids
if(hasLiquids && liquid != null && liquids.get(liquid) >= 0.001f){
float baseLiquidEfficiency = getLiquidEfficiency(liquid);
float maximumPossible = maxLiquidGenerate * calculationDelta;
float used = Math.min(liquids.get(liquid) * calculationDelta, maximumPossible);
liquids.remove(liquid, used * power.graph.getUsageFraction());
productionEfficiency = baseLiquidEfficiency * used / maximumPossible;
if(used > 0.001f && Mathf.chance(0.05 * delta())){
generateEffect.at(x + Mathf.range(3f), y + Mathf.range(3f));
}
}else if(hasItems){
// No liquids accepted or none supplied, try using items if accepted
if(generateTime <= 0f && items.total() > 0){
generateEffect.at(tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f));
Item item = items.take();
productionEfficiency = getItemEfficiency(item);
explosiveness = item.explosiveness;
generateTime = 1f;
}
if(generateTime > 0f){
generateTime -= Math.min(1f / itemDuration * delta() * power.graph.getUsageFraction(), generateTime);
if(randomlyExplode && state.rules.reactorExplosions && Mathf.chance(delta() * 0.06 * Mathf.clamp(explosiveness - 0.5f))){
//this block is run last so that in the event of a block destruction, no code relies on the block type
Core.app.post(() -> {
damage(Mathf.random(11f));
explodeEffect.at(tile.worldx() + Mathf.range(size * tilesize / 2f), tile.worldy() + Mathf.range(size * tilesize / 2f));
});
}
}else{
productionEfficiency = 0.0f;
}
}
}
@Override
public void draw(){
super.draw();
if(hasItems){
Draw.color(heatColor);
Draw.alpha(heat * 0.4f + Mathf.absin(Time.time(), 8f, 0.6f) * heat);
Draw.rect(topRegion, x, y);
Draw.reset();
}
if(hasLiquids){
Draw.color(liquids.current().color);
Draw.alpha(liquids.currentAmount() / liquidCapacity);
Draw.rect(liquidRegion, x, y);
Draw.color();
}
}
@Override
public void drawLight(){
renderer.lights.add(x, y, (60f + Mathf.absin(10f, 5f)) * productionEfficiency * size, Color.orange, 0.5f);
}
protected float getItemEfficiency(Item item){
return 0.0f;
}
@@ -183,5 +94,93 @@ public class ItemLiquidGenerator extends PowerGenerator{
public class ItemLiquidGeneratorEntity extends GeneratorEntity{
public float explosiveness;
public float heat;
@Override
public boolean productionValid(){
return generateTime > 0;
}
@Override
public void updateTile(){
//Note: Do not use this delta when calculating the amount of power or the power efficiency, but use it for resource consumption if necessary.
//Power amount is delta'd by PowerGraph class already.
float calculationDelta = delta();
if(!consValid()){
productionEfficiency = 0.0f;
return;
}
Liquid liquid = null;
for(Liquid other : content.liquids()){
if(hasLiquids && liquids.get(other) >= 0.001f && getLiquidEfficiency(other) >= minLiquidEfficiency){
liquid = other;
break;
}
}
heat = Mathf.lerpDelta(heat, generateTime >= 0.001f ? 1f : 0f, 0.05f);
//liquid takes priority over solids
if(hasLiquids && liquid != null && liquids.get(liquid) >= 0.001f){
float baseLiquidEfficiency = getLiquidEfficiency(liquid);
float maximumPossible = maxLiquidGenerate * calculationDelta;
float used = Math.min(liquids.get(liquid) * calculationDelta, maximumPossible);
liquids.remove(liquid, used * power.graph.getUsageFraction());
productionEfficiency = baseLiquidEfficiency * used / maximumPossible;
if(used > 0.001f && Mathf.chance(0.05 * delta())){
generateEffect.at(x + Mathf.range(3f), y + Mathf.range(3f));
}
}else if(hasItems){
// No liquids accepted or none supplied, try using items if accepted
if(generateTime <= 0f && items.total() > 0){
generateEffect.at(x + Mathf.range(3f), y + Mathf.range(3f));
Item item = items.take();
productionEfficiency = getItemEfficiency(item);
explosiveness = item.explosiveness;
generateTime = 1f;
}
if(generateTime > 0f){
generateTime -= Math.min(1f / itemDuration * delta() * power.graph.getUsageFraction(), generateTime);
if(randomlyExplode && state.rules.reactorExplosions && Mathf.chance(delta() * 0.06 * Mathf.clamp(explosiveness - 0.5f))){
//this block is run last so that in the event of a block destruction, no code relies on the block type
Core.app.post(() -> {
damage(Mathf.random(11f));
explodeEffect.at(x + Mathf.range(size * tilesize / 2f), y + Mathf.range(size * tilesize / 2f));
});
}
}else{
productionEfficiency = 0.0f;
}
}
}
@Override
public void draw(){
super.draw();
if(hasItems){
Draw.color(heatColor);
Draw.alpha(heat * 0.4f + Mathf.absin(Time.time(), 8f, 0.6f) * heat);
Draw.rect(topRegion, x, y);
Draw.reset();
}
if(hasLiquids){
Draw.color(liquids.current().color);
Draw.alpha(liquids.currentAmount() / liquidCapacity);
Draw.rect(liquidRegion, x, y);
Draw.color();
}
}
@Override
public void drawLight(){
renderer.lights.add(x, y, (60f + Mathf.absin(10f, 5f)) * productionEfficiency * size, Color.orange, 0.5f);
}
}
}

View File

@@ -24,45 +24,45 @@ public class LightBlock extends Block{
update = true;
topRegion = reg("-top");
configurable = true;
config(Integer.class, (tile, value) -> tile.<LightEntity>ent().color = value);
}
@Override
public void playerPlaced(){
if(lastColor != 0){
tile.configure(lastColor);
}
}
@Override
public void draw(){
super.draw();
Draw.blend(Blending.additive);
Draw.color(Tmp.c1.set(color), efficiency() * 0.3f);
Draw.rect(reg(topRegion), x, y);
Draw.color();
Draw.blend();
}
@Override
public void buildConfiguration(Table table){
table.addImageButton(Icon.pencil, () -> {
ui.picker.show(Tmp.c1.set(color).a(0.5f), false, res -> {
color = res.rgba();
lastColor = color;
});
control.input.frag.config.hideConfig();
}).size(40f);
}
@Override
public void drawLight(){
renderer.lights.add(x, y, radius, Tmp.c1.set(color), brightness * tile.efficiency());
config(Integer.class, (tile, value) -> ((LightEntity)tile).color = value);
}
public class LightEntity extends TileEntity{
public int color = Pal.accent.rgba();
@Override
public void playerPlaced(){
if(lastColor != 0){
tile.configure(lastColor);
}
}
@Override
public void draw(){
super.draw();
Draw.blend(Blending.additive);
Draw.color(Tmp.c1.set(color), efficiency() * 0.3f);
Draw.rect(reg(topRegion), x, y);
Draw.color();
Draw.blend();
}
@Override
public void buildConfiguration(Table table){
table.addImageButton(Icon.pencil, () -> {
ui.picker.show(Tmp.c1.set(color).a(0.5f), false, res -> {
color = res.rgba();
lastColor = color;
});
control.input.frag.config.hideConfig();
}).size(40f);
}
@Override
public void drawLight(){
renderer.lights.add(x, y, radius, Tmp.c1.set(color), brightness * efficiency());
}
@Override
public Integer config(){
return color;

View File

@@ -14,12 +14,9 @@ import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.type.*;
import mindustry.ui.*;
import mindustry.world.*;
import mindustry.world.consumers.*;
import mindustry.world.meta.*;
import java.io.*;
import static mindustry.Vars.*;
public class NuclearReactor extends PowerGenerator{
@@ -46,7 +43,7 @@ public class NuclearReactor extends PowerGenerator{
liquidCapacity = 30;
hasItems = true;
hasLiquids = true;
rebuildable = false;
rebuildable = false;
}
@Override
@@ -72,111 +69,110 @@ public class NuclearReactor extends PowerGenerator{
bars.add("heat", entity -> new Bar("bar.heat", Pal.lightOrange, () -> ((NuclearReactorEntity)entity).heat));
}
@Override
public void updateTile(){
ConsumeLiquid cliquid = consumes.get(ConsumeType.liquid);
Item item = consumes.<ConsumeItems>get(ConsumeType.item).items[0].item;
int fuel = items.get(item);
float fullness = (float)fuel / itemCapacity;
productionEfficiency = fullness;
if(fuel > 0){
heat += fullness * heating * Math.min(delta(), 4f);
if(timer(timerFuel, itemDuration / timeScale())){
consume();
}
}
Liquid liquid = cliquid.liquid;
if(heat > 0){
float maxUsed = Math.min(liquids.get(liquid), heat / coolantPower);
heat -= maxUsed * coolantPower;
liquids.remove(liquid, maxUsed);
}
if(heat > smokeThreshold){
float smoke = 1.0f + (heat - smokeThreshold) / (1f - smokeThreshold); //ranges from 1.0 to 2.0
if(Mathf.chance(smoke / 20.0 * delta())){
Fx.reactorsmoke.at(tile.worldx() + Mathf.range(size * tilesize / 2f),
tile.worldy() + Mathf.random(size * tilesize / 2f));
}
}
heat = Mathf.clamp(heat);
if(heat >= 0.999f){
Events.fire(Trigger.thoriumReactorOverheat);
kill();
}
}
@Override
public void onDestroyed(){
super.onDestroyed();
Sounds.explosionbig.at(tile);
int fuel = items.get(consumes.<ConsumeItems>get(ConsumeType.item).items[0].item);
if((fuel < 5 && heat < 0.5f) || !state.rules.reactorExplosions) return;
Effects.shake(6f, 16f, tile.worldx(), tile.worldy());
Fx.nuclearShockwave.at(tile.worldx(), tile.worldy());
for(int i = 0; i < 6; i++){
Time.run(Mathf.random(40), () -> Fx.nuclearcloud.at(tile.worldx(), tile.worldy()));
}
Damage.damage(tile.worldx(), tile.worldy(), explosionRadius * tilesize, explosionDamage * 4);
for(int i = 0; i < 20; i++){
Time.run(Mathf.random(50), () -> {
tr.rnd(Mathf.random(40f));
Fx.explosion.at(tr.x + tile.worldx(), tr.y + tile.worldy());
});
}
for(int i = 0; i < 70; i++){
Time.run(Mathf.random(80), () -> {
tr.rnd(Mathf.random(120f));
Fx.nuclearsmoke.at(tr.x + tile.worldx(), tr.y + tile.worldy());
});
}
}
@Override
public void drawLight(){
float fract = productionEfficiency;
renderer.lights.add(x, y, (90f + Mathf.absin(5, 5f)) * fract, Tmp.c1.set(lightColor).lerp(Color.scarlet, heat), 0.6f * fract);
}
@Override
public void draw(){
super.draw();
Draw.color(coolColor, hotColor, heat);
Fill.rect(x, y, size * tilesize, size * tilesize);
Draw.color(liquids.current().color);
Draw.alpha(liquids.currentAmount() / liquidCapacity);
Draw.rect(topRegion, x, y);
if(heat > flashThreshold){
float flash = 1f + ((heat - flashThreshold) / (1f - flashThreshold)) * 5.4f;
flash += flash * Time.delta();
Draw.color(Color.red, Color.yellow, Mathf.absin(flash, 9f, 1f));
Draw.alpha(0.6f);
Draw.rect(lightsRegion, x, y);
}
Draw.reset();
}
public class NuclearReactorEntity extends GeneratorEntity{
public float heat;
public float flash;
@Override
public void updateTile(){
ConsumeLiquid cliquid = consumes.get(ConsumeType.liquid);
Item item = consumes.<ConsumeItems>get(ConsumeType.item).items[0].item;
int fuel = items.get(item);
float fullness = (float)fuel / itemCapacity;
productionEfficiency = fullness;
if(fuel > 0){
heat += fullness * heating * Math.min(delta(), 4f);
if(timer(timerFuel, itemDuration / timeScale())){
consume();
}
}
Liquid liquid = cliquid.liquid;
if(heat > 0){
float maxUsed = Math.min(liquids.get(liquid), heat / coolantPower);
heat -= maxUsed * coolantPower;
liquids.remove(liquid, maxUsed);
}
if(heat > smokeThreshold){
float smoke = 1.0f + (heat - smokeThreshold) / (1f - smokeThreshold); //ranges from 1.0 to 2.0
if(Mathf.chance(smoke / 20.0 * delta())){
Fx.reactorsmoke.at(x + Mathf.range(size * tilesize / 2f),
y + Mathf.random(size * tilesize / 2f));
}
}
heat = Mathf.clamp(heat);
if(heat >= 0.999f){
Events.fire(Trigger.thoriumReactorOverheat);
kill();
}
}
@Override
public void onDestroyed(){
super.onDestroyed();
Sounds.explosionbig.at(tile);
int fuel = items.get(consumes.<ConsumeItems>get(ConsumeType.item).items[0].item);
if((fuel < 5 && heat < 0.5f) || !state.rules.reactorExplosions) return;
Effects.shake(6f, 16f, x, y);
Fx.nuclearShockwave.at(x, y);
for(int i = 0; i < 6; i++){
Time.run(Mathf.random(40), () -> Fx.nuclearcloud.at(x, y));
}
Damage.damage(x, y, explosionRadius * tilesize, explosionDamage * 4);
for(int i = 0; i < 20; i++){
Time.run(Mathf.random(50), () -> {
tr.rnd(Mathf.random(40f));
Fx.explosion.at(tr.x + x, tr.y + y);
});
}
for(int i = 0; i < 70; i++){
Time.run(Mathf.random(80), () -> {
tr.rnd(Mathf.random(120f));
Fx.nuclearsmoke.at(tr.x + x, tr.y + y);
});
}
}
@Override
public void drawLight(){
float fract = productionEfficiency;
renderer.lights.add(x, y, (90f + Mathf.absin(5, 5f)) * fract, Tmp.c1.set(lightColor).lerp(Color.scarlet, heat), 0.6f * fract);
}
@Override
public void draw(){
super.draw();
Draw.color(coolColor, hotColor, heat);
Fill.rect(x, y, size * tilesize, size * tilesize);
Draw.color(liquids.current().color);
Draw.alpha(liquids.currentAmount() / liquidCapacity);
Draw.rect(topRegion, x, y);
if(heat > flashThreshold){
float flash = 1f + ((heat - flashThreshold) / (1f - flashThreshold)) * 5.4f;
flash += flash * Time.delta();
Draw.color(Color.red, Color.yellow, Mathf.absin(flash, 9f, 1f));
Draw.alpha(0.6f);
Draw.rect(lightsRegion, x, y);
}
Draw.reset();
}
@Override
public void write(Writes write){

View File

@@ -3,6 +3,7 @@ package mindustry.world.blocks.power;
import arc.Core;
import arc.math.Mathf;
import mindustry.entities.units.*;
import mindustry.gen.*;
import mindustry.ui.Bar;
import arc.util.Eachable;
import mindustry.ui.Cicon;
@@ -11,6 +12,7 @@ import mindustry.world.Block;
import arc.graphics.g2d.Draw;
import mindustry.graphics.Pal;
import arc.graphics.g2d.TextureRegion;
import mindustry.world.meta.*;
public class PowerDiode extends Block{
public TextureRegion arrow;
@@ -21,45 +23,15 @@ public class PowerDiode extends Block{
update = true;
solid = true;
insulated = true;
}
@Override
public void updateTile(){
super.updateTile();
if(tile.front() == null || tile.back() == null || !tile.back().block().hasPower || !tile.front().block().hasPower || tile.back().team() != tile.front().team()) return;
PowerGraph backGraph = tile.back().power.graph;
PowerGraph frontGraph = tile.front().power.graph;
if(backGraph == frontGraph) return;
// 0f - 1f of battery capacity in use
float backStored = backGraph.getBatteryStored() / backGraph.getTotalBatteryCapacity();
float frontStored = frontGraph.getBatteryStored() / frontGraph.getTotalBatteryCapacity();
// try to send if the back side has more % capacity stored than the front side
if(backStored > frontStored) {
// send half of the difference
float amount = backGraph.getBatteryStored() * (backStored - frontStored) / 2;
// prevent sending more than the front can handle
amount = Mathf.clamp(amount, 0, frontGraph.getTotalBatteryCapacity() * (1 - frontStored));
backGraph.useBatteries(amount);
frontGraph.chargeBatteries(amount);
}
}
// battery % of the graph on either side, defaults to zero
public float bar(){
return (tile != null && tile.block().hasPower) ? tile.power.graph.getBatteryStored() / tile.power.graph.getTotalBatteryCapacity() : 0f;
group = BlockGroup.power;
}
@Override
public void setBars(){
super.setBars();
bars.add("back", entity -> new Bar("bar.input", Pal.powerBar, () -> bar(tile().back())));
bars.add("front", entity -> new Bar("bar.output", Pal.powerBar, () -> bar(tile().front())));
bars.add("back", entity -> new Bar("bar.input", Pal.powerBar, () -> bar(entity.back())));
bars.add("front", entity -> new Bar("bar.output", Pal.powerBar, () -> bar(entity.front())));
}
@Override
@@ -68,12 +40,6 @@ public class PowerDiode extends Block{
arrow = Core.atlas.find(name + "-arrow");
}
@Override
public void draw(){
Draw.rect(region, x, y, 0);
Draw.rect(arrow, x, y, rotate ? tile.rotation() * 90 : 0);
}
@Override
public void drawRequestRegion(BuildRequest req, Eachable<BuildRequest> list) {
TextureRegion reg = icon(Cicon.full);
@@ -86,4 +52,43 @@ public class PowerDiode extends Block{
arrow.getHeight() * req.animScale * Draw.scl,
!rotate ? 0 : req.rotation * 90);
}
// battery % of the graph on either side, defaults to zero
public float bar(Tilec tile){
return (tile != null && tile.block().hasPower) ? tile.power().graph.getBatteryStored() / tile.power().graph.getTotalBatteryCapacity() : 0f;
}
public class PowerDiodeEntity extends TileEntity{
@Override
public void draw(){
Draw.rect(region, x, y, 0);
Draw.rect(arrow, x, y, rotate ? tile.rotation() * 90 : 0);
}
@Override
public void updateTile(){
super.updateTile();
if(tile.front() == null || tile.back() == null || !tile.back().block().hasPower || !tile.front().block().hasPower || tile.back().team() != tile.front().team()) return;
PowerGraph backGraph = tile.back().power().graph;
PowerGraph frontGraph = tile.front().power().graph;
if(backGraph == frontGraph) return;
// 0f - 1f of battery capacity in use
float backStored = backGraph.getBatteryStored() / backGraph.getTotalBatteryCapacity();
float frontStored = frontGraph.getBatteryStored() / frontGraph.getTotalBatteryCapacity();
// try to send if the back side has more % capacity stored than the front side
if(backStored > frontStored) {
// send half of the difference
float amount = backGraph.getBatteryStored() * (backStored - frontStored) / 2;
// prevent sending more than the front can handle
amount = Mathf.clamp(amount, 0, frontGraph.getTotalBatteryCapacity() * (1 - frontStored));
backGraph.useBatteries(amount);
frontGraph.chargeBatteries(amount);
}
}
}
}

View File

@@ -1,17 +1,14 @@
package mindustry.world.blocks.power;
import arc.Core;
import arc.struct.EnumSet;
import arc.util.Strings;
import arc.*;
import arc.struct.*;
import arc.util.*;
import arc.util.io.*;
import mindustry.gen.*;
import mindustry.graphics.Pal;
import mindustry.ui.Bar;
import mindustry.world.Tile;
import mindustry.graphics.*;
import mindustry.ui.*;
import mindustry.world.meta.*;
import java.io.*;
public class PowerGenerator extends PowerDistributor{
/** The amount of power produced per tick in case of an efficiency of 1.0, which represents 100%. */
public float powerProduction;
@@ -37,17 +34,12 @@ public class PowerGenerator extends PowerDistributor{
if(hasPower && outputsPower && !consumes.hasPower()){
bars.add("power", entity -> new Bar(() ->
Core.bundle.format("bar.poweroutput",
Strings.fixed(block().getPowerProduction(tile()) * 60 * timeScale(), 1)),
Strings.fixed(entity.getPowerProduction() * 60 * entity.timeScale(), 1)),
() -> Pal.powerBar,
() -> ((GeneratorEntity)entity).productionEfficiency));
}
}
@Override
public float getPowerProduction(){
return powerProduction * tile.<GeneratorEntity>ent().productionEfficiency;
}
@Override
public boolean outputsItems(){
return false;
@@ -58,6 +50,11 @@ public class PowerGenerator extends PowerDistributor{
/** The efficiency of the producer. An efficiency of 1.0 means 100% */
public float productionEfficiency = 0.0f;
@Override
public float getPowerProduction(){
return powerProduction * productionEfficiency;
}
@Override
public void write(Writes write){
super.write(write);

View File

@@ -63,8 +63,7 @@ public class PowerGraph{
public float getPowerProduced(){
float powerProduced = 0f;
for(Tilec producer : producers){
if(producer.entity == null) continue;
powerProduced += producer.block().getPowerProduction(producer) * producer.delta();
powerProduced += producer.getPowerProduction() * producer.delta();
}
return powerProduced;
}
@@ -230,7 +229,7 @@ public class PowerGraph{
}
public void add(Tilec tile){
if(tile.entity == null || tile.power() == null) return;
if(tile == null || tile.power() == null) return;
tile.power().graph = this;
all.add(tile);
@@ -253,7 +252,7 @@ public class PowerGraph{
while(queue.size > 0){
Tilec child = queue.removeFirst();
add(child);
for(Tilec next : child.block().getPowerConnections(child, outArray2)){
for(Tilec next : child.getPowerConnections(outArray2)){
if(!closedSet.contains(next.pos())){
queue.addLast(next);
closedSet.add(next.pos());
@@ -275,7 +274,7 @@ public class PowerGraph{
closedSet.clear();
//go through all the connections of this tile
for(Tilec other : tile.block().getPowerConnections(tile, outArray1)){
for(Tilec other : tile.getPowerConnections(outArray1)){
//a graph has already been assigned to this tile from a previous call, skip it
if(other.power().graph != this) continue;
@@ -293,7 +292,7 @@ public class PowerGraph{
//add it to the new branch graph
graph.add(child);
//go through connections
for(Tilec next : child.block().getPowerConnections(child, outArray2)){
for(Tilec next : child.getPowerConnections(outArray2)){
//make sure it hasn't looped back, and that the new graph being assigned hasn't already been assigned
//also skip closed tiles
if(next != tile && next.power().graph != graph && !closedSet.contains(next.pos())){

View File

@@ -15,6 +15,7 @@ import mindustry.graphics.*;
import mindustry.ui.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import mindustry.world.modules.*;
import static mindustry.Vars.*;
@@ -36,49 +37,49 @@ public class PowerNode extends PowerBlock{
configurable = true;
consumesPower = false;
outputsPower = false;
config(Integer.class, (tile, value) -> {
Tilec entity = tile.entity;
Tile other = world.tile(value);
boolean contains = power.links.contains(value), valid = other != null && other.entity != null && other.power != null;
config(Integer.class, (entity, value) -> {
PowerModule power = entity.power();
Tilec other = world.ent(value);
boolean contains = power.links.contains(value), valid = other != null && other.power() != null;
if(contains){
//unlink
power.links.removeValue(value);
if(valid) other.power.links.removeValue(tile.pos());
if(valid) other.power().links.removeValue(entity.pos());
PowerGraph newgraph = new PowerGraph();
//reflow from this point, covering all tiles on this side
newgraph.reflow(tile);
newgraph.reflow(entity);
if(valid && other.power.graph != newgraph){
if(valid && other.power().graph != newgraph){
//create new graph for other end
PowerGraph og = new PowerGraph();
//reflow from other end
og.reflow(other);
}
}else if(linkValid(tile, other) && valid && power.links.size < maxNodes){
}else if(linkValid(entity, other) && valid && power.links.size < maxNodes){
if(!power.links.contains(other.pos())){
power.links.add(other.pos());
}
if(other.getTeamID() == tile.getTeamID()){
if(other.team() == entity.team()){
if(!other.power.links.contains(tile.pos())){
other.power.links.add(tile.pos());
if(!other.power().links.contains(entity.pos())){
other.power().links.add(entity.pos());
}
}
power.graph.add(other.power.graph);
power.graph.add(other.power().graph);
}
});
config(Point2[].class, (tile, value) -> {
tile.power.links.clear();
tile.power().links.clear();
for(Point2 p : value){
if(tile.power.links.size < maxNodes){
tile.power.links.add(Point2.pack(p.x + tile.x, p.y + tile.y));
if(tile.power().links.size < maxNodes){
tile.power().links.add(Point2.pack(p.x + tile.tileX(), p.y + tile.tileY()));
}
}
});
@@ -97,72 +98,15 @@ public class PowerNode extends PowerBlock{
super.setBars();
bars.add("power", entity -> new Bar(() ->
Core.bundle.format("bar.powerbalance",
((power.graph.getPowerBalance() >= 0 ? "+" : "") + Strings.fixed(power.graph.getPowerBalance() * 60, 1))),
() -> Pal.powerBar,
() -> Mathf.clamp(power.graph.getLastPowerProduced() / power.graph.getLastPowerNeeded())));
((entity.power().graph.getPowerBalance() >= 0 ? "+" : "") + Strings.fixed(entity.power().graph.getPowerBalance() * 60, 1))),
() -> Pal.powerBar,
() -> Mathf.clamp(entity.power().graph.getLastPowerProduced() / entity.power().graph.getLastPowerNeeded())));
bars.add("batteries", entity -> new Bar(() ->
Core.bundle.format("bar.powerstored",
(ui.formatAmount((int)power.graph.getBatteryStored())), ui.formatAmount((int)power.graph.getTotalBatteryCapacity())),
() -> Pal.powerBar,
() -> Mathf.clamp(power.graph.getBatteryStored() / power.graph.getTotalBatteryCapacity())));
}
@Override
public void placed(){
if(net.client()) return;
Boolf<Tile> valid = other -> other != null && other != tile && ((!other.block().outputsPower && other.block().consumesPower) || (other.block().outputsPower && !other.block().consumesPower) || other.block() instanceof PowerNode) && linkValid(tile, other)
&& !other.proximity().contains(tile) && other.power.graph != tile.power.graph;
tempTiles.clear();
Geometry.circle(tile.x, tile.y, (int)(laserRange + 2), (x, y) -> {
Tile other = world.ltile(x, y);
if(valid.get(other)){
if(!insulated(tile, other)){
tempTiles.add(other);
}
}
});
tempTiles.sort((a, b) -> {
int type = -Boolean.compare(a.block() instanceof PowerNode, b.block() instanceof PowerNode);
if(type != 0) return type;
return Float.compare(a.dst2(tile), b.dst2(tile));
});
tempTiles.each(valid, other -> {
if(!tile.power.links.contains(other.pos())){
tile.configureAny(other.pos());
}
});
super.placed();
}
private void getPotentialLinks(Cons<Tile> others){
Boolf<Tile> valid = other -> other != null && other != tile && other.entity != null && other.power != null &&
((!other.block().outputsPower && other.block().consumesPower) || (other.block().outputsPower && !other.block().consumesPower) || other.block() instanceof PowerNode) &&
overlaps(tile.x * tilesize + offset(), tile.y * tilesize + offset(), other, laserRange * tilesize) && other.team() == player.team()
&& !other.proximity().contains(tile) && !graphs.contains(other.power.graph);
tempTiles.clear();
graphs.clear();
Geometry.circle(tile.x, tile.y, (int)(laserRange + 2), (x, y) -> {
Tile other = world.ltile(x, y);
if(valid.get(other) && !tempTiles.contains(other)){
tempTiles.add(other);
}
});
tempTiles.sort((a, b) -> {
int type = -Boolean.compare(a.block() instanceof PowerNode, b.block() instanceof PowerNode);
if(type != 0) return type;
return Float.compare(a.dst2(tile), b.dst2(tile));
});
tempTiles.each(valid, t -> {
graphs.add(t.power.graph);
others.get(t);
});
(ui.formatAmount((int)entity.power().graph.getBatteryStored())), ui.formatAmount((int)entity.power().graph.getTotalBatteryCapacity())),
() -> Pal.powerBar,
() -> Mathf.clamp(entity.power().graph.getBatteryStored() / entity.power().graph.getTotalBatteryCapacity())));
}
@Override
@@ -173,81 +117,6 @@ public class PowerNode extends PowerBlock{
stats.add(BlockStat.powerConnections, maxNodes, StatUnit.none);
}
@Override
public void updateTile(){
tile.power.graph.update();
}
@Override
public boolean onConfigureTileTapped(Tile other){
Tilec entity = tile.ent();
other = other.link();
if(linkValid(tile, other)){
tile.configure(other.pos());
return false;
}
if(tile == other){
if(other.power.links.size == 0){
int[] total = {0};
getPotentialLinks(tile, link -> {
if(!insulated(tile, link) && total[0]++ < maxNodes){
tile.configure(link.pos());
}
});
}else{
while(power.links.size > 0){
tile.configure(power.links.get(0));
}
}
return false;
}
return true;
}
@Override
public void drawSelect(){
super.drawSelect();
Lines.stroke(1f);
Draw.color(Pal.accent);
Drawf.circles(x, y, laserRange * tilesize);
Draw.reset();
}
@Override
public void drawConfigure(){
Draw.color(Pal.accent);
Lines.stroke(1.5f);
Lines.circle(x, y,
tile.block().size * tilesize / 2f + 1f + Mathf.absin(Time.time(), 4f, 1f));
Drawf.circles(x, y, laserRange * tilesize);
Lines.stroke(1.5f);
for(int x = (int)(tile.x - laserRange - 2); x <= tile.x + laserRange + 2; x++){
for(int y = (int)(tile.y - laserRange - 2); y <= tile.y + laserRange + 2; y++){
Tile link = world.ltile(x, y);
if(link != tile && linkValid(tile, link, false)){
boolean linked = linked(tile, link);
if(linked){
Drawf.square(link.drawx(), link.drawy(), link.block().size * tilesize / 2f + 1f, Pal.place);
}
}
}
}
Draw.reset();
}
@Override
public void drawPlace(int x, int y, int rotation, boolean valid){
Tile tile = world.tile(x, y);
@@ -269,80 +138,6 @@ public class PowerNode extends PowerBlock{
Draw.reset();
}
@Override
public void drawLayer(){
if(Core.settings.getInt("lasersopacity") == 0) return;
Tilec entity = tile.ent();
for(int i = 0; i < power.links.size; i++){
Tile link = world.tile(power.links.get(i));
if(!linkValid(tile, link)) continue;
if(link.block() instanceof PowerNode && !(link.pos() < tile.pos())) continue;
drawLaser(tile, link);
}
Draw.reset();
}
@Override
public void drawRequestConfigTop(BuildRequest req, Eachable<BuildRequest> list){
if(req.config instanceof Point2[]){
for(Point2 point : (Point2[])req.config){
otherReq = null;
list.each(other -> {
if((other.x == req.x + point.x && other.y == req.y + point.y) && other != req){
otherReq = other;
}
});
if(otherReq == null || otherReq.block == null) return;
drawLaser(req.drawx(), req.drawy(), otherReq.drawx(), otherReq.drawy(), 1f, size, otherReq.block.size);
}
}
}
protected boolean linked(Tile other){
return tile.power.links.contains(other.pos());
}
public boolean linkValid(Tilec tile, Tilec link){
return linkValid(tile, link, true);
}
public boolean linkValid(Tilec tile, Tilec link, boolean checkMaxNodes){
if(tile == link || link == null || !link.block().hasPower || team != link.team()) return false;
if(overlaps(tile, link, laserRange * tilesize) || (link.block() instanceof PowerNode && overlaps(link, tile, link.<PowerNode>cblock().laserRange * tilesize))){
if(checkMaxNodes && link.block() instanceof PowerNode){
return link.power.links.size < link.<PowerNode>cblock().maxNodes || link.power.links.contains(tile.pos());
}
return true;
}
return false;
}
protected boolean overlaps(float srcx, float srcy, Tile other, float range){
return Intersector.overlaps(Tmp.cr1.set(srcx, srcy, range), other.getHitbox(Tmp.r1));
}
protected boolean overlaps(Tile src, Tile other, float range){
return overlaps(src.drawx(), src.drawy(), other, range);
}
public boolean overlaps(@Nullable Tile src, @Nullable Tile other){
if(src == null || other == null) return true;
return Intersector.overlaps(Tmp.cr1.set(src.worldx() + offset(), src.worldy() + offset(), laserRange * tilesize), Tmp.r1.setSize(size * tilesize).setCenter(other.worldx() + offset(), other.worldy() + offset()));
}
protected void drawLaser(Tile target){
drawLaser(x, y, target.drawx(), target.drawy(), tile.power.graph.getSatisfaction(), size, target.block().size);
}
protected void drawLaser(float x1, float y1, float x2, float y2, float satisfaction, int size1, int size2){
int opacityPercentage = Core.settings.getInt("lasersopacity");
if(opacityPercentage == 0) return;
@@ -366,6 +161,64 @@ public class PowerNode extends PowerBlock{
Draw.color();
}
protected boolean overlaps(float srcx, float srcy, Tile other, float range){
return Intersector.overlaps(Tmp.cr1.set(srcx, srcy, range), other.getHitbox(Tmp.r1));
}
protected boolean overlaps(Tile src, Tile other, float range){
return overlaps(src.drawx(), src.drawy(), other, range);
}
public boolean overlaps(@Nullable Tile src, @Nullable Tile other){
if(src == null || other == null) return true;
return Intersector.overlaps(Tmp.cr1.set(src.worldx() + offset(), src.worldy() + offset(), laserRange * tilesize), Tmp.r1.setSize(size * tilesize).setCenter(other.worldx() + offset(), other.worldy() + offset()));
}
protected void getPotentialLinks(Tile tile, Cons<Tilec> others){
Boolf<Tilec> valid = other -> other != null && other.tile() != tile && other.power() != null &&
((!other.block().outputsPower && other.block().consumesPower) || (other.block().outputsPower && !other.block().consumesPower) || other.block() instanceof PowerNode) &&
overlaps(tile.x * tilesize + offset(), tile.y * tilesize + offset(), other, laserRange * tilesize) && other.team() == player.team()
&& !other.proximity().contains(tile) && !graphs.contains(other.power().graph);
tempTiles.clear();
graphs.clear();
Geometry.circle(tile.x, tile.y, (int)(laserRange + 2), (x, y) -> {
Tile other = world.ltile(x, y);
if(valid.get(other) && !tempTiles.contains(other)){
tempTiles.add(other);
}
});
tempTiles.sort((a, b) -> {
int type = -Boolean.compare(a.block() instanceof PowerNode, b.block() instanceof PowerNode);
if(type != 0) return type;
return Float.compare(a.dst2(tile), b.dst2(tile));
});
tempTiles.each(valid, t -> {
graphs.add(t.power.graph);
others.get(t);
});
}
@Override
public void drawRequestConfigTop(BuildRequest req, Eachable<BuildRequest> list){
if(req.config instanceof Point2[]){
for(Point2 point : (Point2[])req.config){
otherReq = null;
list.each(other -> {
if((other.x == req.x + point.x && other.y == req.y + point.y) && other != req){
otherReq = other;
}
});
if(otherReq == null || otherReq.block == null) return;
drawLaser(req.drawx(), req.drawy(), otherReq.drawx(), otherReq.drawy(), 1f, size, otherReq.block.size);
}
}
}
public static boolean insulated(Tilec tile, Tilec other){
return insulated(tile.tileX(), tile.tileY(), other.tileX(), other.tileY());
}
@@ -376,11 +229,11 @@ public class PowerNode extends PowerBlock{
return returnValue;
}
public static void insulators(int x, int y, int x2, int y2, Cons<Tile> iterator){
public static void insulators(int x, int y, int x2, int y2, Cons<Tilec> iterator){
world.raycastEach(x, y, x2, y2, (wx, wy) -> {
Tile tile = world.ltile(wx, wy);
if(tile != null && tile.block() != null && tile.block().insulated){
Tilec tile = world.ent(wx, wy);
if(tile != null && tile.block().insulated){
iterator.get(tile);
}
@@ -390,6 +243,156 @@ public class PowerNode extends PowerBlock{
public class PowerNodeEntity extends TileEntity{
@Override
public void placed(){
if(net.client()) return;
Boolf<Tilec> valid = other -> other != null && other != tile && ((!other.block().outputsPower && other.block().consumesPower) ||
(other.block().outputsPower && !other.block().consumesPower) || other.block() instanceof PowerNode) && linkValid(this, other)
&& !other.proximity().contains(this) && other.power().graph != power.graph;
tempTiles.clear();
Geometry.circle(tile.x, tile.y, (int)(laserRange + 2), (x, y) -> {
Tile other = world.ltile(x, y);
if(valid.get(other)){
if(!insulated(tile, other)){
tempTiles.add(other);
}
}
});
tempTiles.sort((a, b) -> {
int type = -Boolean.compare(a.block() instanceof PowerNode, b.block() instanceof PowerNode);
if(type != 0) return type;
return Float.compare(a.dst2(tile), b.dst2(tile));
});
tempTiles.each(valid, other -> {
if(!tile.power.links.contains(other.pos())){
tile.configureAny(other.pos());
}
});
super.placed();
}
@Override
public void updateTile(){
tile.power.graph.update();
}
@Override
public boolean onConfigureTileTapped(Tile other){
Tilec entity = tile.ent();
other = other.link();
if(linkValid(tile, other)){
tile.configure(other.pos());
return false;
}
if(tile == other){
if(other.power.links.size == 0){
int[] total = {0};
getPotentialLinks(tile, link -> {
if(!insulated(tile, link) && total[0]++ < maxNodes){
tile.configure(link.pos());
}
});
}else{
while(power.links.size > 0){
tile.configure(power.links.get(0));
}
}
return false;
}
return true;
}
@Override
public void drawSelect(){
super.drawSelect();
Lines.stroke(1f);
Draw.color(Pal.accent);
Drawf.circles(x, y, laserRange * tilesize);
Draw.reset();
}
@Override
public void drawConfigure(){
Draw.color(Pal.accent);
Lines.stroke(1.5f);
Lines.circle(x, y,
tile.block().size * tilesize / 2f + 1f + Mathf.absin(Time.time(), 4f, 1f));
Drawf.circles(x, y, laserRange * tilesize);
Lines.stroke(1.5f);
for(int x = (int)(tile.x - laserRange - 2); x <= tile.x + laserRange + 2; x++){
for(int y = (int)(tile.y - laserRange - 2); y <= tile.y + laserRange + 2; y++){
Tile link = world.ltile(x, y);
if(link != tile && linkValid(tile, link, false)){
boolean linked = linked(tile, link);
if(linked){
Drawf.square(link.drawx(), link.drawy(), link.block().size * tilesize / 2f + 1f, Pal.place);
}
}
}
}
Draw.reset();
}
@Override
public void drawLayer(){
if(Core.settings.getInt("lasersopacity") == 0) return;
Tilec entity = tile.ent();
for(int i = 0; i < power.links.size; i++){
Tile link = world.tile(power.links.get(i));
if(!linkValid(tile, link)) continue;
if(link.block() instanceof PowerNode && !(link.pos() < tile.pos())) continue;
drawLaser(tile, link);
}
Draw.reset();
}
protected boolean linked(Tile other){
return tile.power.links.contains(other.pos());
}
public boolean linkValid(Tilec tile, Tilec link){
return linkValid(tile, link, true);
}
public boolean linkValid(Tilec tile, Tilec link, boolean checkMaxNodes){
if(tile == link || link == null || !link.block().hasPower || team != link.team()) return false;
if(overlaps(tile, link, laserRange * tilesize) || (link.block() instanceof PowerNode && overlaps(link, tile, link.<PowerNode>cblock().laserRange * tilesize))){
if(checkMaxNodes && link.block() instanceof PowerNode){
return link.power.links.size < link.<PowerNode>cblock().maxNodes || link.power.links.contains(tile.pos());
}
return true;
}
return false;
}
protected void drawLaser(Tile target){
drawLaser(x, y, target.drawx(), target.drawy(), power.graph.getSatisfaction(), size, target.block().size);
}
@Override
public Point2[] config(){
Point2[] out = new Point2[power.links.size];

View File

@@ -1,7 +1,6 @@
package mindustry.world.blocks.power;
import arc.struct.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import static mindustry.Vars.state;
@@ -10,20 +9,21 @@ public class SolarGenerator extends PowerGenerator{
public SolarGenerator(String name){
super(name);
// Remove the BlockFlag.producer flag to make this a lower priority target than other generators.
//remove the BlockFlag.producer flag to make this a lower priority target than other generators.
flags = EnumSet.of();
}
@Override
public void updateTile(){
tile.<GeneratorEntity>ent().productionEfficiency = state.rules.solarPowerMultiplier < 0 ? (state.rules.lighting ? 1f - state.rules.ambientLight.a : 1f) : state.rules.solarPowerMultiplier;
}
@Override
public void setStats(){
super.setStats();
// Solar Generators don't really have an efficiency (yet), so for them 100% = 1.0f
stats.remove(generationType);
stats.add(generationType, powerProduction * 60.0f, StatUnit.powerSecond);
}
public class SolarGeneratorEntity extends GeneratorEntity{
@Override
public void updateTile(){
productionEfficiency = state.rules.solarPowerMultiplier < 0 ? (state.rules.lighting ? 1f - state.rules.ambientLight.a : 1f) : state.rules.solarPowerMultiplier;
}
}
}

View File

@@ -5,7 +5,6 @@ import arc.graphics.*;
import arc.math.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.Effects.*;
import mindustry.world.*;
import mindustry.world.meta.*;
@@ -26,40 +25,42 @@ public class ThermalGenerator extends PowerGenerator{
stats.add(BlockStat.tiles, attribute);
}
@Override
public void updateTile(){
if(productionEfficiency > 0.1f && Mathf.chance(0.05 * delta())){
generateEffect.at(x + Mathf.range(3f), y + Mathf.range(3f));
}
}
@Override
public void drawPlace(int x, int y, int rotation, boolean valid){
drawPlaceText(Core.bundle.formatFloat("bar.efficiency", sumAttribute(attribute, x, y) * 100, 1), x, y, valid);
}
@Override
public void drawLight(){
renderer.lights.add(x, y, (40f + Mathf.absin(10f, 5f)) * productionEfficiency * size, Color.scarlet, 0.4f);
}
@Override
public void onProximityAdded(){
super.onProximityAdded();
productionEfficiency = sumAttribute(attribute, tile.x, tile.y);
}
@Override
public float getPowerProduction(){
//in this case, productionEfficiency means 'total heat'
//thus, it may be greater than 1.0
return powerProduction * tile.<GeneratorEntity>ent().productionEfficiency;
}
@Override
public boolean canPlaceOn(){
public boolean canPlaceOn(Tile tile){
//make sure there's heat at this location
return tile.getLinkedTilesAs(this, tempTiles).sumf(other -> other.floor().attributes.get(attribute)) > 0.01f;
}
public class ThermalGeneratorEntity extends GeneratorEntity{
@Override
public void updateTile(){
if(productionEfficiency > 0.1f && Mathf.chance(0.05 * delta())){
generateEffect.at(x + Mathf.range(3f), y + Mathf.range(3f));
}
}
@Override
public void drawLight(){
renderer.lights.add(x, y, (40f + Mathf.absin(10f, 5f)) * productionEfficiency * size, Color.scarlet, 0.4f);
}
@Override
public void onProximityAdded(){
super.onProximityAdded();
productionEfficiency = sumAttribute(attribute, tile.x, tile.y);
}
@Override
public float getPowerProduction(){
//in this case, productionEfficiency means 'total heat'
//thus, it may be greater than 1.0
return powerProduction * productionEfficiency;
}
}
}

View File

@@ -6,7 +6,6 @@ import arc.math.*;
import arc.util.*;
import arc.util.io.*;
import mindustry.content.*;
import mindustry.entities.AllDefs.*;
import mindustry.entities.*;
import mindustry.gen.*;
import mindustry.type.*;

View File

@@ -54,14 +54,14 @@ public class Incinerator extends Block{
}
@Override
public void handleItem(Tile source, Item item){
public void handleItem(Tilec source, Item item){
if(Mathf.chance(0.3)){
effect.at(x, y);
}
}
@Override
public boolean acceptItem(Tile source, Item item){
public boolean acceptItem(Tilec source, Item item){
return heat > 0.5f;
}

View File

@@ -24,15 +24,8 @@ public class ItemSource extends Block{
solid = true;
group = BlockGroup.transportation;
configurable = true;
config(Item.class, (tile, item) -> tile.<ItemSourceEntity>ent().outputItem = item);
configClear(tile -> tile.<ItemSourceEntity>ent().outputItem = null);
}
@Override
public void playerPlaced(){
if(lastItem != null){
Core.app.post(() -> tile.configure(lastItem));
}
config(Item.class, (tile, item) -> ((ItemSourceEntity)tile).outputItem = item);
configClear(tile -> ((ItemSourceEntity)tile).outputItem = null);
}
@Override
@@ -51,39 +44,46 @@ public class ItemSource extends Block{
return true;
}
@Override
public void draw(){
super.draw();
if(outputItem == null) return;
Draw.color(outputItem.color);
Draw.rect("center", tile.worldx(), tile.worldy());
Draw.color();
}
@Override
public void updateTile(){
if(outputItem == null) return;
items.set(outputItem, 1);
tryDump(tile, outputItem);
items.set(outputItem, 0);
}
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(table, content.items(), () -> outputItem, item -> tile.configure(lastItem = item));
}
@Override
public boolean acceptItem(Tile source, Item item){
return false;
}
public class ItemSourceEntity extends TileEntity{
Item outputItem;
@Override
public void playerPlaced(){
if(lastItem != null){
Core.app.post(() -> tile.configure(lastItem));
}
}
@Override
public void draw(){
super.draw();
if(outputItem == null) return;
Draw.color(outputItem.color);
Draw.rect("center", x, y);
Draw.color();
}
@Override
public void updateTile(){
if(outputItem == null) return;
items.set(outputItem, 1);
dump(outputItem);
items.set(outputItem, 0);
}
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(table, content.items(), () -> outputItem, item -> tile.configure(lastItem = item));
}
@Override
public boolean acceptItem(Tilec source, Item item){
return false;
}
@Override
public Item config(){
return outputItem;

View File

@@ -1,8 +1,8 @@
package mindustry.world.blocks.sandbox;
import mindustry.type.Item;
import mindustry.world.Block;
import mindustry.world.Tile;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
public class ItemVoid extends Block{
@@ -11,12 +11,13 @@ public class ItemVoid extends Block{
update = solid = true;
}
@Override
public void handleItem(Tile source, Item item){
}
public class ItemVoidEntity extends TileEntity{
@Override
public void handleItem(Tilec source, Item item){}
@Override
public boolean acceptItem(Tile source, Item item){
return true;
@Override
public boolean acceptItem(Tilec source, Item item){
return true;
}
}
}

View File

@@ -26,15 +26,8 @@ public class LiquidSource extends Block{
liquidCapacity = 100f;
configurable = true;
outputsLiquid = true;
config(Liquid.class, (tile, l) -> tile.<LiquidSourceEntity>ent().source = l);
configClear(tile -> tile.<LiquidSourceEntity>ent().source = null);
}
@Override
public void playerPlaced(){
if(lastLiquid != null){
Core.app.post(() -> tile.configure(lastLiquid));
}
config(Liquid.class, (tile, l) -> ((LiquidSourceEntity)tile).source = l);
configClear(tile -> ((LiquidSourceEntity)tile).source = null);
}
@Override
@@ -44,40 +37,47 @@ public class LiquidSource extends Block{
bars.remove("liquid");
}
@Override
public void updateTile(){
if(source == null){
tile.liquids.clear();
}else{
tile.liquids.add(source, liquidCapacity);
tryDumpLiquid(tile, source);
}
}
@Override
public void drawRequestConfig(BuildRequest req, Eachable<BuildRequest> list){
drawRequestConfigCenter(req, (Content)req.config, "center");
}
@Override
public void draw(){
super.draw();
if(source != null){
Draw.color(source.color);
Draw.rect("center", tile.worldx(), tile.worldy());
Draw.color();
}
}
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(table, content.liquids(), () -> source, liquid -> tile.configure(lastLiquid = liquid));
}
class LiquidSourceEntity extends TileEntity{
public @Nullable Liquid source = null;
@Override
public void updateTile(){
if(source == null){
liquids.clear();
}else{
liquids.add(source, liquidCapacity);
dumpLiquid(source);
}
}
@Override
public void draw(){
super.draw();
if(source != null){
Draw.color(source.color);
Draw.rect("center", x, y);
Draw.color();
}
}
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(table, content.liquids(), () -> source, liquid -> tile.configure(lastLiquid = liquid));
}
@Override
public void playerPlaced(){
if(lastLiquid != null){
Core.app.post(() -> tile.configure(lastLiquid));
}
}
@Override
public Liquid config(){
return source;

View File

@@ -1,5 +1,6 @@
package mindustry.world.blocks.sandbox;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
@@ -18,12 +19,15 @@ public class LiquidVoid extends Block{
bars.remove("liquid");
}
@Override
public boolean acceptLiquid(Tile source, Liquid liquid, float amount){
return true;
public class LiquidVoidEntity extends TileEntity{
@Override
public boolean acceptLiquid(Tilec source, Liquid liquid, float amount){
return true;
}
@Override
public void handleLiquid(Tilec source, Liquid liquid, float amount){
}
}
@Override
public void handleLiquid(Tile source, Liquid liquid, float amount){}
}

View File

@@ -1,7 +1,6 @@
package mindustry.world.blocks.sandbox;
import mindustry.world.Tile;
import mindustry.world.blocks.power.PowerNode;
import mindustry.world.blocks.power.*;
public class PowerSource extends PowerNode{
@@ -12,9 +11,11 @@ public class PowerSource extends PowerNode{
consumesPower = false;
}
@Override
public float getPowerProduction(){
return 10000f;
public class PowerSourceEntity extends PowerNodeEntity{
@Override
public float getPowerProduction(){
return 10000f;
}
}
}

View File

@@ -35,11 +35,11 @@ public class CoreBlock extends StorageBlock{
}
@Remote(called = Loc.server)
public static void onUnitRespawn(Playerc player){
if(player == null || tile.entity == null) return;
public static void onUnitRespawn(Tilec tile, Playerc player){
if(player == null || tile == null) return;
//TODO really fix
Fx.spawn.at(entity);
Fx.spawn.at(tile);
//progress = 0;
//spawnPlayer = player;
//TODO fix
@@ -61,139 +61,146 @@ public class CoreBlock extends StorageBlock{
}
@Override
public void drawLight(){
renderer.lights.add(x, y, 30f * size, Pal.accent, 0.5f + Mathf.absin(20f, 0.1f));
}
@Override
public boolean acceptItem(Tile source, Item item){
return tile.items.get(item) < getMaximumAccepted(tile, item);
}
@Override
public int getMaximumAccepted(Item item){
return item.type == ItemType.material ? storageCapacity : 0;
}
@Override
public void onProximityUpdate(){
for(Tilec other : state.teams.cores(team)){
if(other.tile() != tile){
items(other.items());
}
}
state.teams.registerCore(entity);
storageCapacity = itemCapacity + proximity().sum(e -> isContainer(e) ? e.block().itemCapacity : 0);
proximity().each(this::isContainer, t -> {
t.items(items);
t.<StorageBlockEntity>ent().linkedCore = tile;
});
for(Tilec other : state.teams.cores(team)){
if(other.tile() == tile) continue;
storageCapacity += other.block().itemCapacity + other.proximity().sum(e -> isContainer(e) ? e.block().itemCapacity : 0);
}
if(!world.isGenerating()){
for(Item item : content.items()){
items.set(item, Math.min(items.get(item), storageCapacity));
}
}
for(CoreEntity other : state.teams.cores(team)){
other.storageCapacity = storageCapacity;
}
}
@Override
public void drawSelect(){
Lines.stroke(1f, Pal.accent);
Cons<Tile> outline = t -> {
for(int i = 0; i < 4; i++){
Point2 p = Geometry.d8edge[i];
float offset = -Math.max(t.block().size - 1, 0) / 2f * tilesize;
Draw.rect("block-select", t.drawx() + offset * p.x, t.drawy() + offset * p.y, i * 90);
}
};
if(tile.proximity().contains(e -> isContainer(e) && e.items == tile.items)){
outline.get(tile);
}
tile.proximity().each(e -> isContainer(e) && e.items == tile.items, outline);
Draw.reset();
}
public boolean isContainer(){
return tile.entity instanceof StorageBlockEntity;
}
@Override
public float handleDamage(float amount){
if(player != null && team == player.team()){
Events.fire(Trigger.teamCoreDamage);
}
return amount;
}
@Override
public boolean canBreak(){
public boolean canBreak(Tile tile){
return false;
}
@Override
public void removed(){
int total = tile.proximity().count(e -> e.entity != null && e.items != null && e.items == tile.items);
float fract = 1f / total / state.teams.cores(team).size;
public class CoreEntity extends TileEntity{
// protected Playerc spawnPlayer;
//protected float progress;
protected float time;
protected float heat;
protected int storageCapacity;
tile.proximity().each(e -> isContainer(e) && e.items == tile.items, t -> {
StorageBlockEntity ent = (StorageBlockEntity)t.entity;
ent.linkedCore = null;
ent.items(new ItemModule());
@Override
public void drawLight(){
renderer.lights.add(x, y, 30f * size, Pal.accent, 0.5f + Mathf.absin(20f, 0.1f));
}
@Override
public boolean acceptItem(Tilec source, Item item){
return items.get(item) < getMaximumAccepted(item);
}
@Override
public int getMaximumAccepted(Item item){
return item.type == ItemType.material ? storageCapacity : 0;
}
@Override
public void onProximityUpdate(){
for(Tilec other : state.teams.cores(team)){
if(other.tile() != tile){
items(other.items());
}
}
state.teams.registerCore(this);
storageCapacity = itemCapacity + proximity().sum(e -> isContainer(e) ? e.block().itemCapacity : 0);
proximity.each(this::isContainer, t -> {
t.items(items);
((StorageBlockEntity)t).linkedCore = tile;
});
for(Tilec other : state.teams.cores(team)){
if(other.tile() == tile) continue;
storageCapacity += other.block().itemCapacity + other.proximity().sum(e -> isContainer(e) ? e.block().itemCapacity : 0);
}
if(!world.isGenerating()){
for(Item item : content.items()){
items.set(item, Math.min(items.get(item), storageCapacity));
}
}
for(CoreEntity other : state.teams.cores(team)){
other.storageCapacity = storageCapacity;
}
}
@Override
public void drawSelect(){
Lines.stroke(1f, Pal.accent);
Cons<Tilec> outline = t -> {
for(int i = 0; i < 4; i++){
Point2 p = Geometry.d8edge[i];
float offset = -Math.max(t.block().size - 1, 0) / 2f * tilesize;
Draw.rect("block-select", t.x() + offset * p.x, t.y() + offset * p.y, i * 90);
}
};
if(proximity.contains(e -> isContainer(e) && e.items() == items)){
outline.get(this);
}
proximity.each(e -> isContainer(e) && e.items() == items, outline);
Draw.reset();
}
public boolean isContainer(Tilec tile){
return tile instanceof StorageBlockEntity;
}
@Override
public float handleDamage(float amount){
if(player != null && team == player.team()){
Events.fire(Trigger.teamCoreDamage);
}
return amount;
}
@Override
public void onRemoved(){
int total = proximity.count(e -> e.items() != null && e.items() == items);
float fract = 1f / total / state.teams.cores(team).size;
proximity.each(e -> isContainer(e) && e.items() == items, t -> {
StorageBlockEntity ent = (StorageBlockEntity)t;
ent.linkedCore = null;
ent.items(new ItemModule());
for(Item item : content.items()){
ent.items().set(item, (int)(fract * items.get(item)));
}
});
state.teams.unregisterCore(this);
int max = itemCapacity * state.teams.cores(team).size;
for(Item item : content.items()){
ent.items().set(item, (int)(fract * tile.items.get(item)));
items.set(item, Math.min(items.get(item), max));
}
});
state.teams.unregisterCore(entity);
int max = itemCapacity * state.teams.cores(team).size;
for(Item item : content.items()){
tile.items.set(item, Math.min(tile.items.get(item), max));
for(CoreEntity other : state.teams.cores(team)){
other.onProximityUpdate();
}
}
for(CoreEntity other : state.teams.cores(team)){
other.block().onProximityUpdate(other.tile());
@Override
public void placed(){
super.placed();
state.teams.registerCore(this);
}
}
@Override
public void placed(){
super.placed();
state.teams.registerCore(entity);
}
@Override
public void drawLayer(){
if(heat > 0.001f){
//TODO implement
//RespawnBlock.drawRespawn(tile, heat, progress, time, spawnPlayer, mech);
}
}
@Override
public void drawLayer(){
if(heat > 0.001f){
@Override
public void handleItem(Tilec source, Item item){
if(net.server() || !net.active()){
super.handleItem(source, item);
if(state.rules.tutorial){
Events.fire(new CoreItemDeliverEvent());
}
}
}
@Override
public void updateTile(){
//TODO implement
//RespawnBlock.drawRespawn(tile, heat, progress, time, spawnPlayer, mech);
}
}
@Override
public void handleItem(Tile source, Item item){
if(net.server() || !net.active()){
super.handleItem(tile, source, item);
if(state.rules.tutorial){
Events.fire(new CoreItemDeliverEvent());
}
}
}
@Override
public void updateTile(){
//TODO implement
/*
if(spawnPlayer != null){
if(!spawnPlayer.dead() || !spawnPlayer.isAdded()){
@@ -212,18 +219,12 @@ public class CoreBlock extends StorageBlock{
}else{
heat = Mathf.lerpDelta(heat, 0f, 0.1f);
}*/
}
}
@Override
public boolean shouldActiveSound(){
return spawnPlayer != null;
}
public class CoreEntity extends TileEntity{
// protected Playerc spawnPlayer;
//protected float progress;
protected float time;
protected float heat;
protected int storageCapacity;
@Override
public boolean shouldActiveSound(){
//TODO
return false;//spawnPlayer != null;
}
}
}

View File

@@ -38,7 +38,7 @@ public class LaunchPad extends StorageBlock{
}
@Override
public boolean acceptItem(Tile source, Item item){
public boolean acceptItem(Tilec source, Item item){
return item.type == ItemType.material && tile.items.total() < itemCapacity;
}

View File

@@ -14,8 +14,8 @@ public abstract class StorageBlock extends Block{
}
@Override
public boolean acceptItem(Tile source, Item item){
return linkedCore != null ? linkedCore.block().acceptItem(linkedCore, source, item) : tile.items.get(item) < getMaximumAccepted(tile, item);
public boolean acceptItem(Tilec source, Item item){
return linkedCore != null ? linkedCore.acceptItem(linkedCore, source, item) : tile.items.get(item) < getMaximumAccepted(tile, item);
}
@Override

View File

@@ -110,7 +110,7 @@ public class Unloader extends Block{
super.draw();
Draw.color(sortItem == null ? Color.clear : sortItem.color);
Draw.rect("unloader-center", tile.worldx(), tile.worldy());
Draw.rect("unloader-center", x, y);
Draw.color();
}