Added Edges class, broke tiles and saving

This commit is contained in:
Anuken 2018-02-28 23:16:35 -05:00
parent d3ec50825e
commit 59e8a85c7e
5 changed files with 78 additions and 110 deletions

View File

@ -1,7 +1,7 @@
#Autogenerated file. Do not modify.
#Tue Feb 27 19:35:27 EST 2018
#Wed Feb 28 23:04:20 EST 2018
version=release
androidBuildCode=313
androidBuildCode=315
name=Mindustry
code=3.4
build=29
build=custom build

View File

@ -31,8 +31,6 @@ import io.anuke.ucore.modules.Module;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Timer;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.*;
@ -226,36 +224,6 @@ public class NetClient extends Module {
}
});
Net.handleClient(BlockSyncPacket.class, packet -> {
if (!gotData) return;
DataInputStream stream = new DataInputStream(packet.stream);
try {
float time = stream.readFloat();
float elapsed = Timers.time() - time;
while (stream.available() > 0) {
int pos = stream.readInt();
Tile tile = world.tile(pos);
short data = stream.readShort();
tile.setPackedData(data);
tile.entity.readNetwork(stream, elapsed);
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
Log.err(e);
//do nothing else...
//TODO fix
}
});
Net.handleClient(DisconnectPacket.class, packet -> {
Player player = playerGroup.getByID(packet.playerid);

View File

@ -1,6 +1,9 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.utils.*;
import com.badlogic.gdx.utils.ByteArray;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.SyncEntity;
@ -10,26 +13,21 @@ import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.net.Administration;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.NetConnection;
import io.anuke.mindustry.net.NetworkIO;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.resource.*;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Placement;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Events;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.modules.Module;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.*;
@ -389,55 +387,5 @@ public class NetServer extends Module{
Net.send(packet, SendMode.udp);
}
if(sendBlockSync && timer.get(timerBlockSync, blockSyncTime)){
Array<NetConnection> connections = Net.getConnections();
for(int i = 0; i < connections.size; i ++){
int id = connections.get(i).id;
Player player = this.connections.get(id);
if(player == null) continue;
int x = Mathf.scl2(player.x, tilesize);
int y = Mathf.scl2(player.y, tilesize);
int w = 22;
int h = 16;
sendBlockSync(id, x, y, w, h);
}
}
}
public void sendBlockSync(int client, int x, int y, int viewx, int viewy){
BlockSyncPacket packet = new BlockSyncPacket();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
//TODO compress stream
try {
DataOutputStream stream = new DataOutputStream(bs);
stream.writeFloat(Timers.time());
for (int rx = -viewx / 2; rx <= viewx / 2; rx++) {
for (int ry = -viewy / 2; ry <= viewy / 2; ry++) {
Tile tile = world.tile(x + rx, y + ry);
if (tile == null || tile.entity == null || !tile.block().syncEntity()) continue;
stream.writeInt(tile.packedPosition());
stream.writeShort(tile.getPackedData());
tile.entity.write(stream);
}
}
}catch (IOException e){
throw new RuntimeException(e);
}
packet.stream = new ByteArrayInputStream(bs.toByteArray());
Net.sendStream(client, packet);
}
}

View File

@ -0,0 +1,45 @@
package io.anuke.mindustry.world;
import com.badlogic.gdx.math.GridPoint2;
import io.anuke.ucore.util.Mathf;
import java.util.Arrays;
public class Edges {
private static final int maxSize = 11;
private static GridPoint2[][] edges = new GridPoint2[maxSize][0];
static{
for(int i = 0; i < maxSize; i ++){
int bot = -(int)(i/2f-0.5f) - 1;
int top = (int)(i/2f+0.5f) + 1;
edges[i] = new GridPoint2[(i + 1) * 4];
int idx = 0;
for(int j = 0; j < i + 1; j ++){
//bottom
edges[i][idx ++] = new GridPoint2(bot + 1 + j, bot);
//top
edges[i][idx ++] = new GridPoint2(bot + 1 + j, top);
//left
edges[i][idx ++] = new GridPoint2(bot, bot + j + 1);
//right
edges[i][idx ++] = new GridPoint2(top, bot + j + 1);
}
Arrays.sort(edges[i], (e1, e2) -> Float.compare(Mathf.atan2(e1.x, e1.y), Mathf.atan2(e2.x, e2.y)));
}
}
public static GridPoint2[] getEdges(int size){
if(size < 0 || size > maxSize) throw new RuntimeException("Block size must be between 0 and " + maxSize);
return edges[size - 1];
}
public static int getEdgeAmount(int size){
return getEdges(size).length;
}
}

View File

@ -1,5 +1,6 @@
package io.anuke.mindustry.world;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.reflect.ClassReflection;
@ -16,10 +17,11 @@ public class Tile{
public static final Object tileSetLock = new Object();
private static final Array<Tile> tmpArray = new Array<>();
/**Packed block data. Left is floor, right is block.*/
private short blocks;
/**Packed data. Left is rotation, right is extra data, packed into two half-bytes: left is dump, right is extra.*/
private short data;
/**Block ID data.*/
private byte floor, wall;
private byte rotation;
private byte dump;
private byte extra;
/**The coordinates of the core tile this is linked to, in the form of two bytes packed into one.
* This is relative to the block it is linked to; negate coords to find the link.*/
public byte link = 0;
@ -43,21 +45,19 @@ public class Tile{
}
private void iSetFloor(Block floor){
byte id = (byte)floor.id;
blocks = Bits.packShort(id, getWallID());
this.floor = (byte)floor.id;
}
private void iSetBlock(Block wall){
byte id = (byte)wall.id;
blocks = Bits.packShort(getFloorID(), id);
this.wall = (byte)wall.id;
}
public byte getWallID(){
return Bits.getRightByte(blocks);
return wall;
}
public byte getFloorID(){
return Bits.getLeftByte(blocks);
return floor;
}
/**Return relative rotation to a coordinate. Returns -1 if the coordinate is not near this tile.*/
@ -144,36 +144,39 @@ public class Tile{
}
public void setRotation(byte rotation){
data = Bits.packShort(rotation, Bits.getRightByte(data));
this.rotation = rotation;
}
public void setDump(byte dump){
data = Bits.packShort(getRotation(), Bits.packByte(dump, getExtra()));
this.dump = dump;
}
public void setExtra(byte extra){
data = Bits.packShort(getRotation(), Bits.packByte(getDump(), extra));
this.extra = extra;
}
public byte getRotation(){
return Bits.getLeftByte(data);
return rotation;
}
public byte getDump(){
return Bits.getLeftByte(Bits.getRightByte(data));
return dump;
}
public byte getExtra(){
return Bits.getRightByte(Bits.getRightByte(data));
return extra;
}
//TODO fix
/*
public short getPackedData(){
return data;
return Bits.packShort(dump, extra);
}
public void setPackedData(short data){
this.data = data;
}
this.dump = Bits.getLeftByte(data);
this.extra = Bits.getRightByte(data);
}*/
public boolean passable(){
Block block = block();
@ -235,6 +238,10 @@ public class Tile{
}
}
public Tile getNearby(GridPoint2 relative){
return world.tile(x + relative.x, y + relative.y);
}
public Tile getNearby(int rotation){
if(rotation == 0) return world.tile(x + 1, y);
if(rotation == 1) return world.tile(x, y + 1);