Merge branch '6.0' of https://github.com/Anuken/Mindustry into object-config

# Conflicts:
#	core/src/mindustry/entities/traits/BuilderTrait.java
#	core/src/mindustry/entities/type/TileEntity.java
#	core/src/mindustry/game/EventType.java
#	core/src/mindustry/game/Schematics.java
#	core/src/mindustry/input/InputHandler.java
#	core/src/mindustry/io/TypeIO.java
#	core/src/mindustry/world/Block.java
#	core/src/mindustry/world/blocks/distribution/Sorter.java
This commit is contained in:
Anuken 2020-03-04 08:38:36 -05:00
parent 2581353c5e
commit aeae286273
19 changed files with 55 additions and 62 deletions

View File

@ -232,10 +232,14 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
return proximity;
}
/** Tile configuration. Defaults to 0. Used for block rebuilding. */
@Override
public int config(){
return 0;
/** Tile configuration. Defaults to null. Used for block rebuilding. */
public Object config(){
return null;
}
/** Sets the config object and casts it. Does nothing by default. */
public void setConfig(Object config){
}
@Override

View File

@ -17,7 +17,7 @@ public class BuildRequest{
/** Whether this request comes with a config int. If yes, any blocks placed with this request will not call playerPlaced.*/
public boolean hasConfig;
/** Config int. Not used unless hasConfig is true.*/
public int config;
public Object config;
/** Original position, only used in schematics.*/
public int originalX, originalY, originalWidth, originalHeight;
@ -101,7 +101,7 @@ public class BuildRequest{
return y*tilesize + block.offset();
}
public BuildRequest configure(int config){
public BuildRequest configure(Object config){
this.config = config;
this.hasConfig = true;
return this;

View File

@ -116,10 +116,10 @@ public class Schematic implements Publishable, Comparable<Schematic>{
public static class Stile{
public @NonNull Block block;
public short x, y;
public int config;
public Object config;
public byte rotation;
public Stile(Block block, int x, int y, int config, byte rotation){
public Stile(Block block, int x, int y, Object config, byte rotation){
this.block = block;
this.x = (short)x;
this.y = (short)y;

View File

@ -10,6 +10,7 @@ import arc.graphics.g2d.*;
import arc.graphics.gl.*;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import arc.util.io.*;
import arc.util.io.Streams.*;
import arc.util.serialization.*;
import mindustry.*;
@ -20,6 +21,7 @@ import mindustry.game.EventType.*;
import mindustry.game.Schematic.*;
import mindustry.input.*;
import mindustry.input.Placement.*;
import mindustry.io.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.blocks.production.*;
@ -35,7 +37,7 @@ public class Schematics implements Loadable{
public static final String base64Header = "bXNjaAB";
private static final byte[] header = {'m', 's', 'c', 'h'};
private static final byte version = 0;
private static final byte version = 1;
private static final int padding = 2;
private static final int maxPreviewsMobile = 32;
@ -259,8 +261,8 @@ public class Schematics implements Loadable{
tile.set(st.block, state.rules.defaultTeam);
tile.rotation(st.rotation);
if(st.block.posConfig){
tile.configureAny(Pos.get(tile.x - st.x + Pos.x(st.config), tile.y - st.y + Pos.y(st.config)));
if(st.config instanceof Point2){
tile.configureAny(Pos.get(tile.x - st.x + ((Point2)st.config).x, tile.y - st.y + ((Point2)st.config).y));
}else{
tile.configureAny(st.config);
}
@ -348,7 +350,7 @@ public class Schematics implements Loadable{
&& (tile.entity.block().isVisible() || (tile.entity.block() instanceof CoreBlock && Core.settings.getBool("coreselect")))){
Object config = tile.entity.config();
if(config instanceof Point2){
config = Pos.get(Pos.x(config) + offsetX, Pos.y(config) + offsetY);
config = Pos.get(((Point2)config).x + offsetX, ((Point2)config).y + offsetY);
}
tiles.add(new Stile(tile.block(), tile.x + offsetX, tile.y + offsetY, config, tile.rotation()));
@ -398,10 +400,7 @@ public class Schematics implements Loadable{
}
}
int ver;
if((ver = input.read()) != version){
throw new IOException("Unknown version: " + ver);
}
int ver = input.read();
try(DataInputStream stream = new DataInputStream(new InflaterInputStream(input))){
short width = stream.readShort(), height = stream.readShort();
@ -424,7 +423,7 @@ public class Schematics implements Loadable{
for(int i = 0; i < total; i++){
Block block = blocks.get(stream.readByte());
int position = stream.readInt();
int config = stream.readInt();
Object config = ver == 0 ? stream.readInt() : TypeIO.readObject(Reads.get(stream));
byte rotation = stream.readByte();
if(block != Blocks.air){
tiles.add(new Stile(block, Pos.x(position), Pos.y(position), config, rotation));
@ -468,7 +467,7 @@ public class Schematics implements Loadable{
for(Stile tile : schematic.tiles){
stream.writeByte(blocks.orderedItems().indexOf(tile.block));
stream.writeInt(Pos.get(tile.x, tile.y));
stream.writeInt(tile.config);
TypeIO.writeObject(Writes.get(stream), tile.config);
stream.writeByte(tile.rotation);
}
}

View File

@ -159,15 +159,6 @@ public abstract class InputHandler implements InputProcessor, GestureListener{
}
}
@Remote(targets = Loc.both, called = Loc.server, forward = true)
public static void onTileTapped(Playerc player, Tile tile){
if(tile == null || player == null) return;
if(net.server() && (!Units.canInteract(player, tile) ||
!netServer.admins.allowAction(player, ActionType.tapTile, tile, action -> {}))) throw new ValidateException(player, "Player cannot tap a tile.");
tile.block().tapped(tile, player);
Core.app.post(() -> Events.fire(new TapEvent(tile, player)));
}
@Remote(targets = Loc.both, called = Loc.both, forward = true)
public static void onTileConfig(Playerc player, Tile tile, @Nullable Object value){
if(tile == null) return;
@ -318,13 +309,13 @@ public abstract class InputHandler implements InputProcessor, GestureListener{
if(req.config instanceof Point2){
int corigin = x ? req.originalWidth/2 : req.originalHeight/2;
int nvalue = -((x ? Pos.x(req.config) : Pos.y(req.config)) - corigin) + corigin;
int nvalue = -((x ? ((Point2)req.config).x : ((Point2)req.config).y) - corigin) + corigin;
if(x){
req.originalX = -(req.originalX - corigin) + corigin;
req.config = Pos.get(nvalue, Pos.y(req.config));
req.config = Pos.get(nvalue, ((Point2)req.config).y);
}else{
req.originalY = -(req.originalY - corigin) + corigin;
req.config = Pos.get(Pos.x(req.config), nvalue);
req.config = Pos.get(((Point2)req.config).x, nvalue);
}
}
@ -459,8 +450,8 @@ public abstract class InputHandler implements InputProcessor, GestureListener{
for(BuildRequest req : requests){
if(req.block != null && validPlace(req.x, req.y, req.block, req.rotation)){
BuildRequest copy = req.copy();
if(copy.hasConfig && copy.block.posConfig){
copy.config = Pos.get(Pos.x(copy.config) + copy.x - copy.originalX, Pos.y(copy.config) + copy.y - copy.originalY);
if(copy.hasConfig && copy.config instanceof Point2){
copy.config = Pos.get(((Point2)copy.config).x + copy.x - copy.originalX, ((Point2)copy.config).x + copy.y - copy.originalY);
}
player.builder().addBuild(copy);
}

View File

@ -222,8 +222,8 @@ public class MobileInput extends InputHandler implements GestureListener{
BuildRequest other = getRequest(request.x, request.y, request.block.size, null);
BuildRequest copy = request.copy();
if(copy.hasConfig && copy.block.posConfig){
copy.config = Pos.get(Pos.x(copy.config) + copy.x - copy.originalX, Pos.y(copy.config) + copy.y - copy.originalY);
if(copy.hasConfig && copy.config instanceof Point2){
copy.config = Pos.get(((Point2)copy.config).x + copy.x - copy.originalX, ((Point2)copy.config).y + copy.y - copy.originalY);
}
if(other == null){

View File

@ -221,7 +221,7 @@ public abstract class SaveVersion extends SaveFileReader{
stream.writeShort(block.y);
stream.writeShort(block.rotation);
stream.writeShort(block.block);
stream.writeInt(block.config);
TypeIO.writeObject(Writes.get(stream), block.config);
}
}
@ -243,7 +243,7 @@ public abstract class SaveVersion extends SaveFileReader{
TeamData data = team.data();
int blocks = stream.readInt();
for(int j = 0; j < blocks; j++){
data.brokenBlocks.addLast(new BrokenBlock(stream.readShort(), stream.readShort(), stream.readShort(), content.block(stream.readShort()).id, stream.readInt()));
data.brokenBlocks.addLast(new BrokenBlock(stream.readShort(), stream.readShort(), stream.readShort(), content.block(stream.readShort()).id, TypeIO.readObject(Reads.get(stream))));
}
}

View File

@ -110,7 +110,7 @@ public class TypeIO{
write.s(request.block.id);
write.b((byte)request.rotation);
write.b(request.hasConfig ? (byte)1 : 0);
write.i(request.config);
writeObject(write, request.config);
}
}
}
@ -133,7 +133,7 @@ public class TypeIO{
short block = read.s();
byte rotation = read.b();
boolean hasConfig = read.b() == 1;
int config = read.i();
Object config = readObject(read);
currentRequest = new BuildRequest(Pos.x(position), Pos.y(position), rotation, content.block(block));
if(hasConfig){
currentRequest.configure(config);

View File

@ -574,7 +574,7 @@ public class Administration{
}
public enum ActionType{
breakBlock, placeBlock, rotate, configure, withdrawItem, depositItem
breakBlock, placeBlock, rotate, configure, tapTile, withdrawItem, depositItem
}
}

View File

@ -477,9 +477,10 @@ public class Block extends BlockStorage{
/** Called when arbitrary configuration is applied to a tile. */
public void configured(Tile tile, @Nullable Playerc player, @Nullable Object value){
if(value == null){
tapped(tile, player);
//TODO
//tapped(tile, player);
}else if(configurations.containsKey(value.getClass())){
configurations.get(value.getClass()).configured(tile, player, value);
configurations.get(value.getClass()).configured(tile, value);
}
}
@ -929,7 +930,7 @@ public class Block extends BlockStorage{
}
public interface ConfigHandler<T>{
void configured(Tile tile, Player player, T value);
void configured(Tile tile, T value);
}
}

View File

@ -37,17 +37,13 @@ public class ItemBridge extends Block{
layer = Layer.power;
expanded = true;
itemCapacity = 10;
posConfig = true;
configurable = true;
hasItems = true;
unloadable = false;
group = BlockGroup.transportation;
entityType = ItemBridgeEntity::new;
}
@Override
public void configured(Tile tile, Playerc player, int value){
tile.<ItemBridgeEntity>ent().link = value;
config(Integer.class, (tile, i) -> tile.<ItemBridgeEntity>ent().link = i);
}
@Override
@ -371,7 +367,7 @@ public class ItemBridge extends Block{
public float cycleSpeed = 1f;
@Override
public int config(){
public Object config(){
return link;
}

View File

@ -36,7 +36,6 @@ public class MassDriver extends Block{
super(name);
update = true;
solid = true;
posConfig = true;
configurable = true;
hasItems = true;
layer = Layer.turret;
@ -46,7 +45,7 @@ public class MassDriver extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
tile.<MassDriverEntity>ent().link = value;
}

View File

@ -30,7 +30,8 @@ public class Sorter extends Block{
configurable = true;
unloadable = false;
entityType = SorterEntity::new;
config(Item.class, (tile, player, item) -> tile.<SorterEntity>ent().sortItem = item);
config(Item.class, (tile, item) -> tile.<SorterEntity>ent().sortItem = item);
}
@Override
@ -46,8 +47,8 @@ public class Sorter extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
tile.<SorterEntity>ent().sortItem = content.item(value);
public void configured(Tile tile, Playerc player, Object value){
tile.<SorterEntity>ent().sortItem = (Item)value;
if(!headless){
renderer.minimap.update(tile);
}
@ -148,7 +149,7 @@ public class Sorter extends Block{
@Nullable Item sortItem;
@Override
public Item config(){
public Object config(){
return sortItem;
}

View File

@ -62,7 +62,7 @@ public class LightBlock extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
tile.<LightEntity>ent().color = value;
}

View File

@ -38,7 +38,7 @@ public class PowerNode extends PowerBlock{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
Tilec entity = tile.entity;
Tile other = world.tile(value);
boolean contains = entity.power().links.contains(value), valid = other != null && other.entity != null && other.entity.power() != null;

View File

@ -30,7 +30,7 @@ public class ItemSource extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
tile.<ItemSourceEntity>ent().outputItem = content.item(value);
}

View File

@ -28,6 +28,8 @@ public class LiquidSource extends Block{
configurable = true;
outputsLiquid = true;
entityType = LiquidSourceEntity::new;
config();
}
@Override
@ -85,7 +87,7 @@ public class LiquidSource extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
tile.<LiquidSourceEntity>ent().source = value == -1 ? null : content.liquid(value);
}
@ -93,8 +95,8 @@ public class LiquidSource extends Block{
public @Nullable Liquid source = null;
@Override
public int config(){
return source == null ? -1 : source.id;
public Object config(){
return source;
}
@Override

View File

@ -55,7 +55,7 @@ public class Unloader extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
tile.entity.items().clear();
tile.<UnloaderEntity>ent().sortItem = content.item(value);
}

View File

@ -103,7 +103,7 @@ public class CommandCenter extends Block{
}
@Override
public void configured(Tile tile, Playerc player, int value){
public void configured(Tile tile, Playerc player, Object value){
UnitCommand command = UnitCommand.all[value];
((CommandCenter)tile.block()).effect.at(tile);