Massive amount of fixes and changes with multiplayer/annotations

This commit is contained in:
Anuken
2018-06-08 22:43:23 -04:00
parent 0620c635e5
commit 690571eec0
34 changed files with 557 additions and 290 deletions

View File

@@ -31,7 +31,6 @@ public class Mindustry extends ModuleCore {
module(ui = new UI());
module(netServer = new NetServer());
module(netClient = new NetClient());
module(netCommon = new NetCommon());
}
@Override

View File

@@ -123,7 +123,6 @@ public class Vars{
public static Renderer renderer;
public static UI ui;
public static World world;
public static NetCommon netCommon;
public static NetServer netServer;
public static NetClient netClient;

View File

@@ -138,7 +138,7 @@ public class Control extends Module{
Events.on(PlayEvent.class, () -> {
for(Player player : players){
player.dead = true;
player.add();
}
state.set(State.playing);

View File

@@ -4,10 +4,12 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.annotations.Annotations.Variant;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.traits.SyncTrait;
import io.anuke.mindustry.gen.Call;
import io.anuke.mindustry.gen.RemoteReadClient;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.NetworkIO;
@@ -102,7 +104,10 @@ public class NetClient extends Module {
finishConnecting();
});
Net.handleClient(InvokePacket.class, packet -> {});
Net.handleClient(InvokePacket.class, packet -> {
packet.writeBuffer.position(0);
RemoteReadClient.readPacket(packet.writeBuffer, packet.type);
});
}
@Override
@@ -165,9 +170,16 @@ public class NetClient extends Module {
}
}
@Remote(one = true, all = false, unreliable = true)
public static void onSnapshot(byte[] snapshot, int snapshotID){
@Remote(variants = Variant.one)
public static void onKick(KickReason reason){
netClient.disconnectQuietly();
state.set(State.menu);
if(!reason.quiet) ui.showError("$text.server.kicked." + reason.name());
ui.loadfrag.hide();
}
@Remote(variants = Variant.one, unreliable = true)
public static void onSnapshot(byte[] snapshot, int snapshotID){
//skip snapshot IDs that have already been recieved
if(snapshotID == netClient.lastSnapshotID){
return;
@@ -177,7 +189,7 @@ public class NetClient extends Module {
byte[] result;
int length;
if (snapshotID == -1) { //-1 = fresh snapshot
if (snapshotID == 0) { //fresh snapshot
result = snapshot;
length = snapshot.length;
netClient.lastSnapshot = snapshot;
@@ -189,6 +201,8 @@ public class NetClient extends Module {
netClient.lastSnapshot = Arrays.copyOf(result, length);
}
netClient.lastSnapshotID = snapshotID;
//set stream bytes to begin write
netClient.byteStream.setBytes(result, 0, length);
@@ -214,6 +228,7 @@ public class NetClient extends Module {
//entity must not be added yet, so create it
if(entity == null){
entity = (SyncTrait) ClassReflection.newInstance(group.getType()); //TODO solution without reflection?
entity.resetID(id);
entity.add();
}
@@ -222,15 +237,11 @@ public class NetClient extends Module {
}
}
//confirm that snapshot 0 has been recieved if this is the initial snapshot
if(snapshotID == -1){
netClient.lastSnapshotID = 0;
}else{ //confirm that the snapshot has been recieved
netClient.lastSnapshotID = snapshotID;
}
//confirm that snapshot has been recieved
netClient.lastSnapshotID = snapshotID;
}catch (IOException | ReflectionException e){
throw new RuntimeException(e);
e.printStackTrace();
}
}
}

View File

@@ -1,19 +0,0 @@
package io.anuke.mindustry.core;
import io.anuke.mindustry.entities.Player;
import io.anuke.ucore.modules.Module;
import static io.anuke.mindustry.Vars.playerGroup;
public class NetCommon extends Module {
public void sendMessage(String message){
//TODO implement
}
public String colorizeName(int id, String name){
Player player = playerGroup.getByID(id);
if(name == null || player == null) return null;
return "[#" + player.color.toString().toUpperCase() + "]" + name;
}
}

View File

@@ -3,6 +3,7 @@ package io.anuke.mindustry.core;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.annotations.Annotations.Loc;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.mindustry.content.Mechs;
import io.anuke.mindustry.core.GameState.State;
@@ -32,10 +33,8 @@ import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
public class NetServer extends Module{
private final static float serverSyncTime = 4, itemSyncTime = 10, kickDuration = 30 * 1000;
private final static int timerEntitySync = 0;
private final static int timerStateSync = 1;
private final static float serverSyncTime = 4, kickDuration = 30 * 1000;
private final static boolean preventDuplicatNames = false;
public final Administration admins = new Administration();
@@ -58,7 +57,13 @@ public class NetServer extends Module{
}
});
Net.handleServer(Disconnect.class, (id, packet) -> {});
Net.handleServer(Disconnect.class, (id, packet) -> {
Player player = connections.get(id);
if(player != null){
Call.sendMessage("[accent]" + player.name + " has disconnected.");
player.remove();
}
});
Net.handleServer(ConnectPacket.class, (id, packet) -> {
String uuid = new String(Base64Coder.encode(packet.uuid));
@@ -81,10 +86,12 @@ public class NetServer extends Module{
return;
}
for(Player player : playerGroup.all()){
if(player.name.equalsIgnoreCase(packet.name)){
kick(id, KickReason.nameInUse);
return;
if(preventDuplicatNames) {
for (Player player : playerGroup.all()) {
if (player.name.equalsIgnoreCase(packet.name)) {
kick(id, KickReason.nameInUse);
return;
}
}
}
@@ -172,6 +179,7 @@ public class NetServer extends Module{
}
//TODO kick player, send kick packet
Call.onKick(connection, reason);
Timers.runTask(2f, con::close);
@@ -184,15 +192,21 @@ public class NetServer extends Module{
void sync(){
try {
//TODO implement snapshot packets w/ delta compression
//iterate through each player
for (Player player : connections.values()) {
NetConnection connection = Net.getConnection(player.clientid);
if(connection == null){
Log.err("Player {0} failed to connect.", player.name);
connections.remove(player.clientid);
player.remove();
return;
}
if(!player.timer.get(Player.timeSync, serverSyncTime)) continue;
//if the player hasn't acknolwedged that it has recieved the packet, send the same thing again
//if the player hasn't acknowledged that it has recieved the packet, send the same thing again
if(connection.lastSentSnapshotID > connection.lastSnapshotID){
Call.onSnapshot(connection.id, connection.lastSentSnapshot, connection.lastSentSnapshotID);
return;
@@ -215,7 +229,7 @@ public class NetServer extends Module{
//check for syncable groups
for (EntityGroup<?> group : Entities.getAllGroups()) {
//TODO range-check sync positions?
//TODO range-check sync positions to optimize?
if (group.isEmpty() || !(group.all().get(0) instanceof SyncTrait)) continue;
//make sure mapping is enabled for this group
@@ -223,13 +237,20 @@ public class NetServer extends Module{
throw new RuntimeException("Entity group '" + group.getType() + "' contains SyncTrait entities, yet mapping is not enabled. In order for syncing to work, you must enable mapping for this group.");
}
int size = group.size();
if(group.getType() == Player.class){
size --;
}
//write group ID + group size
dataStream.writeByte(group.getID());
dataStream.writeShort(group.size());
dataStream.writeShort(size);
//write timestamp
dataStream.writeLong(TimeUtils.millis());
for(Entity entity : group.all()){
if(entity == player) continue;
//write all entities now
dataStream.writeInt(entity.getID());
((SyncTrait)entity).write(dataStream);
@@ -237,17 +258,17 @@ public class NetServer extends Module{
}
byte[] bytes = syncStream.toByteArray();
if(connection.lastSnapshot == null){
connection.lastSentSnapshot = bytes;
if(connection.lastSnapshotID == -1){
//no snapshot to diff, send it all
Call.onSnapshot(connection.id, bytes, -1);
Call.onSnapshot(connection.id, bytes, 0);
connection.lastSnapshotID = 0;
}else{
//increment snapshot ID
connection.lastSnapshotID ++;
//send diff, otherwise
byte[] diff = ByteDeltaEncoder.toDiff(new ByteMatcherHash(connection.lastSnapshot, bytes), encoder);
Call.onSnapshot(connection.id, diff, connection.lastSnapshotID);
connection.lastSentSnapshot = bytes;
Call.onSnapshot(connection.id, diff, connection.lastSnapshotID + 1);
//increment snapshot ID
connection.lastSentSnapshotID ++;
}
}
@@ -256,10 +277,10 @@ public class NetServer extends Module{
}
}
@Remote(server = false)
@Remote(targets = Loc.client)
public static void connectConfirm(Player player){
player.add();
netCommon.sendMessage("[accent]" + player.name + " has connected.");
Call.sendMessage("[accent]" + player.name + " has connected.");
Log.info("&y{0} has connected.", player.name);
}
}

View File

@@ -30,7 +30,9 @@ import io.anuke.ucore.graphics.Fill;
import io.anuke.ucore.graphics.Lines;
import io.anuke.ucore.util.*;
import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
@@ -192,6 +194,7 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
@Override
public void removed() {
Log.info("\n\nPLAYER REMOVED\n\n");
dropCarry();
}
@@ -368,7 +371,7 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
hitTime = Math.max(0f, hitTime - Timers.delta());
if(!isLocal){
interpolate();
//interpolate();
return;
}
@@ -552,9 +555,9 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
dead = true;
respawning = false;
trail.clear();
health = maxHealth();
add();
heal();
}
public boolean isShooting(){
@@ -601,7 +604,6 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
private void readSaveSuper(DataInput stream) throws IOException {
super.readSave(stream);
byte uamount = stream.readByte();
for (int i = 0; i < uamount; i++) {
upgrades.add(Upgrade.getByID(stream.readByte()));
@@ -611,13 +613,23 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
}
@Override
public void write(DataOutput buffer) {
//todo
public void write(DataOutput buffer) throws IOException {
super.writeSave(buffer);
buffer.writeUTF(name);
buffer.writeInt(Color.rgba8888(color));
buffer.writeBoolean(dead);
buffer.writeByte(weapon.id);
buffer.writeByte(mech.id);
}
@Override
public void read(DataInput buffer, long time) {
//todo
public void read(DataInput buffer, long time) throws IOException {
super.readSave(buffer);
name = buffer.readUTF();
color.set(buffer.readInt());
dead = buffer.readBoolean();
weapon = Upgrade.getByID(buffer.readByte());
mech = Upgrade.getByID(buffer.readByte());
}
//endregion

View File

@@ -63,7 +63,9 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
x = interpolator.pos.x;
y = interpolator.pos.y;
rotation = interpolator.values[0];
if(interpolator.values.length > 0){
rotation = interpolator.values[0];
}
}
@Override
@@ -100,6 +102,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
stream.writeByte(team.ordinal());
stream.writeFloat(x);
stream.writeFloat(y);
stream.writeFloat(rotation);
stream.writeShort((short)health);
stream.writeByte(status.current().id);
stream.writeFloat(status.getTime());
@@ -111,6 +114,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
byte team = stream.readByte();
float x = stream.readFloat();
float y = stream.readFloat();
float rotation = stream.readFloat();
int health = stream.readShort();
byte effect = stream.readByte();
float etime = stream.readFloat();
@@ -120,6 +124,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
this.health = health;
this.x = x;
this.y = y;
this.rotation = rotation;
this.status.set(StatusEffect.getByID(effect), etime);
}

View File

@@ -51,9 +51,7 @@ public class BasicBulletType extends BulletType {
for (int i = 0; i < fragBullets; i++) {
float len = Mathf.random(1f, 7f);
float a = Mathf.random(360f);
Bullet bullet = Bullet.create(fragBullet, b,
x + Angles.trnsx(a, len), y + Angles.trnsy(a, len), a);
bullet.getVelocity().scl(Mathf.random(fragVelocityMin, fragVelocityMax));
Bullet.create(fragBullet, b, x + Angles.trnsx(a, len), y + Angles.trnsy(a, len), a, Mathf.random(fragVelocityMin, fragVelocityMax));
}
}
}

View File

@@ -3,10 +3,8 @@ package io.anuke.mindustry.entities.bullet;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Pools;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.traits.SyncTrait;
import io.anuke.mindustry.entities.traits.TeamTrait;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.net.Interpolator;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.entities.impl.BulletEntity;
@@ -15,31 +13,31 @@ import io.anuke.ucore.entities.trait.SolidTrait;
import io.anuke.ucore.entities.trait.VelocityTrait;
import io.anuke.ucore.util.Timer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import static io.anuke.mindustry.Vars.bulletGroup;
import static io.anuke.mindustry.Vars.world;
public class Bullet extends BulletEntity<BulletType> implements TeamTrait, SyncTrait{
public class Bullet extends BulletEntity<BulletType> implements TeamTrait{
private static Vector2 vector = new Vector2();
private Interpolator interpolator = new Interpolator();
//private Interpolator interpolator = new Interpolator();
private Team team;
public Timer timer = new Timer(3);
public static Bullet create(BulletType type, TeamTrait owner, float x, float y, float angle){
return create(type, owner, owner.getTeam(), x, y, angle);
public static void create (BulletType type, TeamTrait owner, float x, float y, float angle){
create(type, owner, owner.getTeam(), x, y, angle);
}
public static Bullet create (BulletType type, Entity owner, Team team, float x, float y, float angle){
public static void create (BulletType type, Entity owner, Team team, float x, float y, float angle){
create(type, owner, team, x, y, angle, 1f);
}
public static void create (BulletType type, Entity owner, Team team, float x, float y, float angle, float velocityScl){
Bullet bullet = Pools.obtain(Bullet.class);
bullet.type = type;
bullet.owner = owner;
bullet.velocity.set(0, type.speed).setAngle(angle);
bullet.velocity.set(0, type.speed).setAngle(angle).scl(velocityScl);
bullet.velocity.add(owner instanceof VelocityTrait ? ((VelocityTrait)owner).getVelocity() : Vector2.Zero);
bullet.hitbox.setSize(type.hitsize);
@@ -47,11 +45,14 @@ public class Bullet extends BulletEntity<BulletType> implements TeamTrait, SyncT
bullet.type = type;
bullet.set(x, y);
bullet.add();
return bullet;
}
public static Bullet create(BulletType type, Bullet parent, float x, float y, float angle){
return create(type, parent.owner, parent.team, x, y, angle);
public static void create(BulletType type, Bullet parent, float x, float y, float angle){
create(type, parent.owner, parent.team, x, y, angle);
}
public static void create(BulletType type, Bullet parent, float x, float y, float angle, float velocityScl){
create(type, parent.owner, parent.team, x, y, angle, velocityScl);
}
/**Internal use only!*/
@@ -60,7 +61,7 @@ public class Bullet extends BulletEntity<BulletType> implements TeamTrait, SyncT
public boolean collidesTiles(){
return true; //TODO make artillery and such not do this
}
/*
@Override
public boolean doSync(){
return type.syncable;
@@ -85,7 +86,7 @@ public class Bullet extends BulletEntity<BulletType> implements TeamTrait, SyncT
y = data.readFloat();
team = Team.values()[data.readByte()];
type = BulletType.getByID(data.readByte());
}
}*/
@Override
public Team getTeam() {

View File

@@ -1,19 +1,18 @@
package io.anuke.mindustry.entities.units;
import io.anuke.mindustry.content.fx.ExplosionFx;
import io.anuke.mindustry.entities.traits.TargetTrait;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.Units;
import io.anuke.mindustry.entities.bullet.Bullet;
import io.anuke.mindustry.entities.traits.TargetTrait;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.type.AmmoType;
import io.anuke.mindustry.type.Item;
import io.anuke.mindustry.world.meta.BlockFlag;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.meta.BlockFlag;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Effects.Effect;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.util.Angles;
@@ -21,7 +20,9 @@ import io.anuke.ucore.util.Geometry;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer;
import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
@@ -55,12 +56,6 @@ public abstract class BaseUnit extends Unit{
rotation = Mathf.slerpDelta(rotation, angle, type.rotatespeed);
}
public void effectAt(Effect effect, float rotation, float dx, float dy){
Effects.effect(effect,
x + Angles.trnsx(rotation, dx, dy),
y + Angles.trnsy(rotation, dx, dy), Mathf.atan2(dx, dy) + rotation);
}
public boolean targetHasFlag(BlockFlag flag){
return target instanceof TileEntity &&
((TileEntity)target).tile.block().flags.contains(flag);
@@ -248,12 +243,13 @@ public abstract class BaseUnit extends Unit{
}
@Override
public void write(DataOutput data) {
//todo
public void write(DataOutput data) throws IOException{
writeSave(data);
}
@Override
public void read(DataInput data, long time) {
//todo
public void read(DataInput data, long time) throws IOException{
super.readSave(data);
this.type = UnitType.getByID(data.readByte());
}
}

View File

@@ -3,6 +3,9 @@ package io.anuke.mindustry.io;
import io.anuke.annotations.Annotations.ReadClass;
import io.anuke.annotations.Annotations.WriteClass;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.net.Packets.KickReason;
import io.anuke.mindustry.type.Upgrade;
import io.anuke.mindustry.type.Weapon;
import io.anuke.mindustry.world.Tile;
@@ -34,19 +37,47 @@ public class TypeIO {
return world.tile(buffer.getInt());
}
@WriteClass(KickReason.class)
public static void writeKick(ByteBuffer buffer, KickReason reason){
buffer.put((byte)reason.ordinal());
}
@ReadClass(KickReason.class)
public static KickReason readKick(ByteBuffer buffer){
return KickReason.values()[buffer.get()];
}
@WriteClass(Weapon.class)
public static void writeWeapon(ByteBuffer buffer, Weapon weapon){
buffer.put(weapon.id);
}
@ReadClass(Weapon.class)
public static Weapon readWeapon(ByteBuffer buffer){
return Upgrade.getByID(buffer.get());
}
@WriteClass(String.class)
public static void writeString(ByteBuffer buffer, String string){
byte[] bytes = string.getBytes();
buffer.putShort((short)bytes.length);
buffer.put(bytes);
if(string != null) {
byte[] bytes = string.getBytes();
buffer.putShort((short) bytes.length);
buffer.put(bytes);
}else{
buffer.putShort((short)-1);
}
}
@ReadClass(String.class)
public static String readString(ByteBuffer buffer){
short length = buffer.getShort();
byte[] bytes = new byte[length];
buffer.get(bytes);
return new String(bytes);
if(length != -1) {
byte[] bytes = new byte[length];
buffer.get(bytes);
return new String(bytes);
}else{
return null;
}
}
@WriteClass(byte[].class)

View File

@@ -0,0 +1,8 @@
package io.anuke.mindustry.net;
/**Stores class nameas for remote method invocation for consistency's sake.*/
public class In {
public static final String normal = "Call";
public static final String entities = "CallEntity";
public static final String blocks = "CallBlocks";
}

View File

@@ -10,7 +10,7 @@ public class Interpolator {
//used for movement
public Vector2 target = new Vector2();
public Vector2 last = new Vector2();
public float[] targets;
public float[] targets = {};
public float spacing = 1f;
public float time;

View File

@@ -56,7 +56,6 @@ public class Net{
for(int i = 0; i < packetQueue.size; i ++){
Log.info("Processing {0} packet post-load.", ClassReflection.getSimpleName(packetQueue.get(i).getClass()));
handleClientReceived(packetQueue.get(i));
Pools.free(packetQueue.get(i));
}
}
//clear inbound packet queue
@@ -180,6 +179,7 @@ public class Net{
}
}else if(clientListeners.get(object.getClass()) != null ||
listeners.get(object.getClass()) != null){
if(clientLoaded || object instanceof ImportantPacket){
if(clientListeners.get(object.getClass()) != null) clientListeners.get(object.getClass()).accept(object);
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);

View File

@@ -1,5 +1,32 @@
package io.anuke.mindustry.net;
import io.anuke.annotations.Annotations.Loc;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.annotations.Annotations.Variant;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Player;
import static io.anuke.mindustry.Vars.playerGroup;
public class NetEvents {
@Remote(called = Loc.both, targets = Loc.both)
public static void sendMessage(Player player, String message){
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, player == null ? null : colorizeName(player.id, player.name));
}
}
@Remote(called = Loc.both, variants = Variant.both)
public static void sendMessage(String message){
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, null);
}
}
private static String colorizeName(int id, String name){
Player player = playerGroup.getByID(id);
if(name == null || player == null) return null;
return "[#" + player.color.toString().toUpperCase() + "]" + name;
}
}

View File

@@ -2,15 +2,15 @@ package io.anuke.mindustry.net;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.content.Weapons;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.game.GameMode;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.game.TeamInfo;
import io.anuke.mindustry.game.TeamInfo.TeamData;
import io.anuke.mindustry.io.Map;
import io.anuke.mindustry.io.MapMeta;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.type.Upgrade;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.BlockPart;
import io.anuke.ucore.core.Timers;
@@ -37,16 +37,11 @@ public class NetworkIO {
stream.writeInt(state.wave); //wave
stream.writeFloat(state.wavetime); //wave countdown
stream.writeInt(state.enemies); //enemy amount
stream.writeBoolean(state.friendlyFire); //friendly fire state
stream.writeInt(player.id); //player remap ID
stream.writeBoolean(player.isAdmin);
stream.writeByte(player.upgrades.size);
for(Upgrade u : player.upgrades){
stream.writeByte(u.id);
}
stream.writeInt(player.id);
player.write(stream);
//--MAP DATA--
@@ -80,6 +75,17 @@ public class NetworkIO {
}
}
//write team data
stream.writeByte(state.teams.getTeams().size);
for(TeamData data : state.teams.getTeams()){
stream.writeByte(data.team.ordinal());
stream.writeBoolean(data.ally);
stream.writeShort(data.cores.size);
for(Tile tile : data.cores){
stream.writeInt(tile.packedPosition());
}
}
}catch (IOException e){
throw new RuntimeException(e);
}
@@ -105,29 +111,18 @@ public class NetworkIO {
int wave = stream.readInt();
float wavetime = stream.readFloat();
int enemies = stream.readInt();
boolean friendlyfire = stream.readBoolean();
state.enemies = enemies;
state.wave = wave;
state.wavetime = wavetime;
state.mode = GameMode.values()[mode];
state.friendlyFire = friendlyfire;
int pid = stream.readInt();
boolean admin = stream.readBoolean();
byte weapons = stream.readByte();
for(int i = 0; i < weapons; i ++){
player.upgrades.add(Upgrade.getByID(stream.readByte()));
}
player.weapon = Weapons.blaster;
Entities.clear();
player.id = pid;
player.isAdmin = admin;
int id = stream.readInt();
player.read(stream, TimeUtils.millis());
player.resetID(id);
player.add();
world.beginMapLoad();
@@ -174,7 +169,20 @@ public class NetworkIO {
}
}
player.dead = true;
player.reset();
state.teams = new TeamInfo();
byte teams = stream.readByte();
for (int i = 0; i < teams; i++) {
Team team = Team.values()[stream.readByte()];
boolean ally = stream.readBoolean();
short cores = stream.readShort();
state.teams.add(team, ally);
for (int j = 0; j < cores; j++) {
state.teams.get(team).cores.add(world.tile(stream.readInt()));
}
}
world.endMapLoad();

View File

@@ -3,14 +3,14 @@ package io.anuke.mindustry.net;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.gen.RemoteReadClient;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.net.Packet.ImportantPacket;
import io.anuke.mindustry.net.Packet.UnimportantPacket;
import io.anuke.ucore.io.ByteBufferInput;
import io.anuke.ucore.io.ByteBufferOutput;
import io.anuke.ucore.io.IOUtils;
import io.anuke.ucore.io.ByteBufferInput;
import java.io.IOException;
import java.nio.ByteBuffer;
/**Class for storing all packets.*/
@@ -66,19 +66,17 @@ public class Packets {
@Override
public void read(ByteBuffer buffer) {
type = buffer.get();
if(Net.client()){
RemoteReadClient.readPacket(buffer, type);
}else{
byte[] bytes = new byte[writeLength];
buffer.get(bytes);
writeBuffer = ByteBuffer.wrap(bytes);
}
writeLength = buffer.getShort();
byte[] bytes = new byte[writeLength];
buffer.get(bytes);
writeBuffer = ByteBuffer.wrap(bytes);
}
@Override
public void write(ByteBuffer buffer) {
buffer.put(type);
buffer.putShort((short)writeLength);
writeBuffer.position(0);
for(int i = 0; i < writeLength; i ++){
buffer.put(writeBuffer.get());
@@ -109,7 +107,11 @@ public class Packets {
buffer.putInt(lastSnapshot);
buffer.putInt(player.id);
buffer.putLong(TimeUtils.millis());
player.write(out);
try {
player.write(out);
}catch (IOException e){
e.printStackTrace();
}
}
@Override
@@ -120,7 +122,11 @@ public class Packets {
int id = buffer.getInt();
long time = buffer.getLong();
player = Vars.playerGroup.getByID(id);
player.read(in, time);
try {
player.read(in, time);
}catch (IOException e){
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,13 @@
package io.anuke.mindustry.net;
import io.anuke.mindustry.entities.Player;
/**Thrown when a client sends invalid information.*/
public class ValidateException extends RuntimeException{
public final Player player;
public ValidateException(Player player, String s) {
super(s);
this.player = player;
}
}

View File

@@ -8,7 +8,7 @@ import io.anuke.ucore.core.Effects.Effect;
public class AmmoType implements Content{
private static int lastID = 0;
private static Array<AmmoType> allTypes = new Array<>();
private static Array<AmmoType> allTypes = new Array<>(32);
public final byte id;
/**The item used. Always null if liquid isn't.*/

View File

@@ -2,10 +2,14 @@ package io.anuke.mindustry.type;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.annotations.Annotations.Loc;
import io.anuke.mindustry.content.fx.Fx;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.bullet.Bullet;
import io.anuke.mindustry.gen.CallEntity;
import io.anuke.mindustry.net.In;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Effects.Effect;
import io.anuke.ucore.graphics.Draw;
@@ -51,7 +55,7 @@ public class Weapon extends Upgrade {
public void update(Player p, boolean left, float pointerX, float pointerY){
int t = left ? Player.timerShootLeft : Player.timerShootRight;
int t2 = !left ? Player.timerShootRight : Player.timerShootLeft;
int t2 = !left ? Player.timerShootLeft : Player.timerShootRight;
if(p.inventory.hasAmmo() && p.timer.get(t, reload)){
if(roundrobin){
p.timer.reset(t2, reload/2f);
@@ -78,8 +82,7 @@ public class Weapon extends Upgrade {
}
public void shoot(Player p, float x, float y, float angle, boolean left){
shootInternal(p, x, y, angle, left);
CallEntity.onShootWeapon(p, this, x, y, angle, left);
p.inventory.useAmmo();
}
@@ -92,26 +95,30 @@ public class Weapon extends Upgrade {
ammoMap.put(type.item, type);
}
}
void shootInternal(Player p, float x, float y, float rotation, boolean left){
Angles.shotgun(shots, spacing, rotation, f -> bullet(p, x, y, f + Mathf.range(inaccuracy)));
AmmoType type = p.inventory.getAmmo();
tr.trns(rotation + 180f, type.recoil);
p.getVelocity().add(tr);
tr.trns(rotation, 3f);
Effects.shake(shake, shake, x, y);
Effects.effect(ejectEffect, x, y, rotation * -Mathf.sign(left));
Effects.effect(type.shootEffect, x + tr.x, y + tr.y, rotation, p);
Effects.effect(type.smokeEffect, x + tr.x, y + tr.y, rotation, p);
}
void bullet(Unit owner, float x, float y, float angle){
tr.trns(angle, 3f);
Bullet.create(owner.inventory.getAmmo().bullet, owner, x + tr.x, y + tr.y, angle);
}
@Remote(targets = Loc.both, called = Loc.both, in = In.entities, forward = true)
public static void onShootWeapon(Player player, Weapon weapon, float x, float y, float rotation, boolean left){
Angles.shotgun(weapon.shots, weapon.spacing, rotation, f -> weapon.bullet(player, x, y, f + Mathf.range(weapon.inaccuracy)));
AmmoType type = player.inventory.getAmmo();
weapon.tr.trns(rotation + 180f, type.recoil);
player.getVelocity().add(weapon.tr);
weapon.tr.trns(rotation, 3f);
Effects.shake(weapon.shake, weapon.shake, x, y);
Effects.effect(weapon.ejectEffect, x, y, rotation * -Mathf.sign(left));
Effects.effect(type.shootEffect, x + weapon.tr.x, y + weapon.tr.y, rotation, player);
Effects.effect(type.smokeEffect, x + weapon.tr.x, y + weapon.tr.y, rotation, player);
//reset timer for remote players
player.timer.getTime(left ? Player.timerShootLeft : Player.timerShootRight);
}
}

View File

@@ -9,6 +9,7 @@ import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.core.Platform;
import io.anuke.mindustry.gen.Call;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Inputs;
@@ -21,6 +22,7 @@ import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.players;
import static io.anuke.mindustry.Vars.state;
import static io.anuke.ucore.core.Core.scene;
import static io.anuke.ucore.core.Core.skin;
@@ -166,7 +168,8 @@ public class ChatFragment extends Table implements Fragment{
if(message.replaceAll(" ", "").isEmpty()) return;
history.insert(1, message);
//TODO send the message
Call.sendMessage(players[0], message);
}
public void toggle(){

View File

@@ -39,7 +39,7 @@ import static io.anuke.mindustry.Vars.*;
public class Block extends BaseBlock implements UnlockableContent{
private static int lastid;
private static Array<Block> blocks = new Array<>();
private static Array<Block> blocks = new Array<>(140);
private static ObjectMap<String, Block> map = new ObjectMap<>();
protected Array<Tile> tempTiles = new Array<>();

View File

@@ -189,6 +189,7 @@ public class Tile implements PosTrait, TargetTrait {
return isLinked() || !((floor.solid && (block == Blocks.air || block.solidifes)) || (block.solid && (!block.destructible && !block.update)));
}
/**Whether this block was placed by a player/unit.*/
public boolean synthetic(){
Block block = block();
return block.update || block.destructible;
@@ -197,7 +198,8 @@ public class Tile implements PosTrait, TargetTrait {
public boolean solid(){
Block block = block();
Block floor = floor();
return block.solid || (floor.solid && (block == Blocks.air || block.solidifes)) || block.isSolidFor(this);
return block.solid || (floor.solid && (block == Blocks.air || block.solidifes)) || block.isSolidFor(this)
|| (isLinked() && getLinked().block().isSolidFor(getLinked()));
}
public boolean breakable(){

View File

@@ -2,17 +2,24 @@ package io.anuke.mindustry.world.blocks.storage;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import io.anuke.annotations.Annotations.Loc;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.content.fx.Fx;
import io.anuke.mindustry.entities.*;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.Units;
import io.anuke.mindustry.entities.effect.ItemTransfer;
import io.anuke.mindustry.gen.CallBlocks;
import io.anuke.mindustry.graphics.Palette;
import io.anuke.mindustry.graphics.Shaders;
import io.anuke.mindustry.net.In;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.type.Item;
import io.anuke.mindustry.type.ItemType;
import io.anuke.mindustry.world.meta.BlockFlag;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.meta.BlockFlag;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Timers;
@@ -21,6 +28,10 @@ import io.anuke.ucore.graphics.Lines;
import io.anuke.ucore.util.EnumSet;
import io.anuke.ucore.util.Mathf;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static io.anuke.mindustry.Vars.debug;
import static io.anuke.mindustry.Vars.state;
@@ -130,7 +141,7 @@ public class CoreBlock extends StorageBlock {
CoreEntity entity = tile.entity();
if(!entity.solid && !Units.anyEntities(tile)){
entity.solid = true;
CallBlocks.setCoreSolid(tile, true);
}
if(entity.currentPlayer != null){
@@ -145,8 +156,8 @@ public class CoreBlock extends StorageBlock {
if(entity.progress >= 1f){
Effects.effect(Fx.spawn, entity);
CallBlocks.setCoreSolid(tile, false);
entity.progress = 0;
entity.solid = false;
entity.currentPlayer.heal();
entity.currentPlayer.rotation = 90f;
entity.currentPlayer.baseRotation = 90f;
@@ -182,6 +193,12 @@ public class CoreBlock extends StorageBlock {
return new CoreEntity();
}
@Remote(called = Loc.server, in = In.blocks)
public static void setCoreSolid(Tile tile, boolean solid){
CoreEntity entity = tile.entity();
entity.solid = solid;
}
public class CoreEntity extends TileEntity{
Player currentPlayer;
boolean solid = true;
@@ -196,5 +213,15 @@ public class CoreBlock extends StorageBlock {
progress = 0f;
return true;
}
@Override
public void write(DataOutputStream stream) throws IOException {
stream.writeBoolean(solid);
}
@Override
public void read(DataInputStream stream) throws IOException {
solid = stream.readBoolean();
}
}
}