Use telephone for networking code

This commit is contained in:
austin 2021-06-11 23:13:45 -04:00
parent 323a966516
commit ba494b32b5
31 changed files with 1726 additions and 104 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
/src/main/resources/Config/terrain.json
/src/main/resources/Config/localconfig.json
/src/main/resources/Config/keybinds.json
/Telephone-*.jar

View File

@ -109,12 +109,6 @@
<version>0.1</version>
</dependency>
<dependency>
<groupId>electrosphere</groupId>
<artifactId>RendererNetworkParser</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<profiles>

View File

@ -8,7 +8,7 @@ import electrosphere.entity.state.MovementTree.MovementTreeState;
import electrosphere.main.Globals;
import electrosphere.menu.MenuTransition;
import electrosphere.net.client.ClientNetworkMessage;
import electrosphere.net.message.EntityMessage;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.util.Utilities;
import java.util.HashMap;
import java.util.List;

View File

@ -71,6 +71,7 @@ public class EntityDataStrings {
/*
Collision Entity
*/
public static final String COLLISION_ENTITY_ID = "collisionEntityId";
public static final String DATA_STRING_COLLISION_ENTITY = "collisionEntity";
public static final String DATA_STRING_COLLISION_ENTITY_TYPE_SPHERE = "collisionSphere";
@ -81,7 +82,9 @@ public class EntityDataStrings {
public static final String COLLISION_ENTITY_DATA_PARENT = "collisionDataParent";
public static final String COLLISION_ENTITY_DATA_BONE = "collisionDataBone";
public static final String COLLISION_ENTITY_DATA_SIZE = "collisionSphereSize";
public static final String COLLISION_ENTITY_DATA_RADIUS = "collisionSphereRadius";
public static final String COLLISION_ENTITY_BEHAVIOR_TREE = "collisionEntityBehaviorTree";
/*

View File

@ -1,6 +1,8 @@
package electrosphere.entity.collision;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.game.world.World;
import java.util.ArrayList;
import java.util.List;
@ -82,9 +84,27 @@ public class CollisionEngine {
}
public void registerCollidableEntity(Entity collidable){
collisionEntities.add(collidable);
}
public List<Entity> getCollisionEntities(){
return collisionEntities;
}
public boolean collisionSphereCheck(Entity hitbox1, Entity hitbox2){
Vector3f position1 = EntityUtils.getEntityPosition(hitbox1);
Vector3f position2 = EntityUtils.getEntityPosition(hitbox2);
float radius1 = (float)hitbox1.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS);
float radius2 = (float)hitbox2.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS);
float distance = position1.distance(position2);
if(distance < radius1 + radius2){
return true;
} else {
return false;
}
}
}

View File

@ -5,7 +5,7 @@ import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import electrosphere.net.NetUtils;
import electrosphere.net.message.EntityMessage;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.renderer.Actor;
import electrosphere.renderer.anim.Animation;
import electrosphere.renderer.Model;
@ -57,21 +57,6 @@ public class MovementTree {
state = MovementTreeState.SLOWDOWN;
}
public void transitionState(){
switch(state){
case STARTUP:
//transition if velocity >= acceleration
state = MovementTreeState.MOVE;
break;
case MOVE:
state = MovementTreeState.SLOWDOWN;
break;
case SLOWDOWN:
state = MovementTreeState.IDLE;
break;
}
}
public void simulate(){
float velocity = CreatureUtils.getVelocity(parent);
float acceleration = CreatureUtils.getAcceleration(parent);
@ -86,11 +71,11 @@ public class MovementTree {
for(EntityMessage message : networkMessageQueue){
networkMessageQueue.remove(message);
// System.out.println("MOVE to " + message.getX() + " " + message.getY() + " " + message.getZ());
switch(message.getEntityMessageType()){
switch(message.getMessageSubtype()){
case MOVE:
position.set(message.getX(), message.getY(), message.getZ());
position.set(message.getpositionX(), message.getpositionY(), message.getpositionZ());
if(Globals.mainConfig.runServer){
Globals.server.broadcastMessage(EntityMessage.constructMoveMessage(parent.getId(), message.getX(), message.getY(), message.getZ()));
Globals.server.broadcastMessage(EntityMessage.constructMoveMessage(parent.getId(), message.getpositionX(), message.getpositionY(), message.getpositionZ()));
}
break;
}

View File

@ -9,7 +9,7 @@ import electrosphere.entity.types.creature.creaturemap.HitboxData;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.net.message.EntityMessage;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.renderer.Model;
import electrosphere.util.ModelLoader;
import org.joml.Quaternionf;
@ -44,7 +44,11 @@ public class CreatureUtils {
CreatureType rawType = Globals.entityTypeMap.get(creatureId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
for(HitboxData hitboxdata : rawType.getHitboxes()){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getSize()));
if(hitboxdata.getType().equals("hit")){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
} else if(hitboxdata.getType().equals("hurt")){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHurtbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
}
}
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true);
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_TYPE, creatureId);

View File

@ -7,7 +7,7 @@ package electrosphere.entity.types.creature.creaturemap;
public class HitboxData {
String type;
String bone;
float size;
float radius;
public String getType() {
return type;
@ -17,8 +17,8 @@ public class HitboxData {
return bone;
}
public float getSize() {
return size;
public float getRadius() {
return radius;
}

View File

@ -1,8 +1,10 @@
package electrosphere.entity.types.hitbox;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
*
@ -10,7 +12,8 @@ import java.util.List;
*/
public class HitboxManager {
List<Entity> hitboxes = new ArrayList();
CopyOnWriteArrayList<Entity> hitboxes = new CopyOnWriteArrayList();
long idIncrementer = 0;
public HitboxManager(){
@ -18,9 +21,11 @@ public class HitboxManager {
public void registerHitbox(Entity hitbox){
hitboxes.add(hitbox);
idIncrementer++;
hitbox.putData(EntityDataStrings.COLLISION_ENTITY_ID, idIncrementer);
}
public List<Entity> getAllHitboxes(){
public CopyOnWriteArrayList<Entity> getAllHitboxes(){
return hitboxes;
}
}

View File

@ -3,6 +3,7 @@ package electrosphere.entity.types.hitbox;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
@ -19,8 +20,10 @@ public class HitboxUtils {
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE,size);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS,size);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE,EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
Globals.hitboxManager.registerHitbox(rVal);
return rVal;
}
@ -28,8 +31,10 @@ public class HitboxUtils {
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE,size);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS,size);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE,EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
Globals.hitboxManager.registerHitbox(rVal);
return rVal;
}
@ -67,4 +72,30 @@ public class HitboxUtils {
((Vector3f)hitbox.getData(EntityDataStrings.DATA_STRING_POSITION)).set(worldPosition);
}
public static void collideEntities(Entity generatorHitbox){
// long generatorId = (Long)generatorHitbox.getData(EntityDataStrings.COLLISION_ENTITY_ID);
Entity generatorParent = (Entity)generatorHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT);
for(Entity receiverHitbox : Globals.hitboxManager.getAllHitboxes()){
Entity receiverParent = (Entity)receiverHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT);
// long targetId = (Long)receiverHitbox.getData(EntityDataStrings.COLLISION_ENTITY_ID);
if(receiverParent != generatorParent && Globals.collisionEngine.collisionSphereCheck(generatorHitbox, receiverHitbox)){
String generatorType = (String)generatorHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE);
String receiverType = (String)receiverHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE);
if(
(generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT) || generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)) &&
(receiverType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT) || receiverType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)) &&
generatorType != receiverType
){
if(generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
EntityUtils.getEntityPosition(generatorParent).set(Globals.spawnPoint);
} else if(receiverParent.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
EntityUtils.getEntityPosition(receiverParent).set(Globals.spawnPoint);
}
}
}
}
}
}

View File

@ -22,7 +22,7 @@ public class ItemUtils {
CreatureType rawType = Globals.entityTypeMap.get(itemId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
for(HitboxData hitboxdata : rawType.getHitboxes()){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getSize()));
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
}
rVal.putData(EntityDataStrings.ITEM_IS_ITEM, true);
rVal.putData(EntityDataStrings.ITEM_TYPE, itemId);

View File

@ -85,7 +85,7 @@ public class TerrainManager {
elevationMapCacheContents.add(0, targetChunkName);
return elevationMapCache.get(targetChunkName);
} else {
float[][] targetChunk = model.getAugmentedElevationForChunk(x, y);
float[][] targetChunk = model.getAugmentedElevationForChunk(model.getMacroValuesAtPosition(x, y), model.getRandomizerValuesAtPosition(x, y));
if(elevationMapCacheContents.size() > cacheSize){
String oldChunk = elevationMapCacheContents.remove(elevationMapCacheContents.size() - 1);
elevationMapCache.remove(oldChunk);

View File

@ -254,8 +254,10 @@ public class Globals {
defaultMeshShader = ShaderProgram.smart_assemble_shader(false,true);
//init skybox
assetManager.registerModelToSpecificString(RenderUtils.createSkyboxModel(null), AssetDataStrings.ASSET_STRING_SKYBOX_BASIC);
//init hitbox
//init hurtbox
assetManager.addModelPathToQueue("Models/unitsphere.fbx");
//init hitbox
assetManager.addModelPathToQueue("Models/unitsphere_1.fbx");
//init smallcube
assetManager.addModelPathToQueue("Models/SmallCube.fbx");
}

View File

@ -216,16 +216,21 @@ public class Main {
for(Entity item : Globals.entityManager.getItemEntities()){
ItemUtils.updateItemActorAnimation(item);
}
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//simulate creature behavior trees
for(Entity currentMoveable : Globals.entityManager.getMoveable()){
MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);
behaviorTree.simulate();
}
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//update hitbox positions
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.updatePosition(currentHitbox);
}
//collide hitboxes
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.collideEntities(currentHitbox);
}
}

View File

@ -2,7 +2,7 @@ package electrosphere.net;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.Entity;
import electrosphere.net.message.EntityMessage;
import electrosphere.net.parser.net.message.EntityMessage;
/**
*
@ -16,13 +16,13 @@ public class NetUtils {
static String address = "127.0.0.1";
public static EntityMessage createSpawnEntityMessage(Entity e){
EntityMessage rVal = EntityMessage.constructCreateMessage(e.getId(), CreatureUtils.getCreatureType(e));
EntityMessage rVal = EntityMessage.constructCreateMessage(e.getId(), CreatureUtils.getCreatureType(e), 0.0f, 0.0f, 0.0f);
return rVal;
}
public static EntityMessage createSetCreatureControllerIdEntityMessage(Entity e){
System.out.println("Controller id: " + CreatureUtils.getControllerPlayerId(e));
EntityMessage rVal = EntityMessage.constructSetPropertyMessage(e.getId(), 0, CreatureUtils.getControllerPlayerId(e));
EntityMessage rVal = EntityMessage.constructsetPropertyMessage(e.getId(), 0, CreatureUtils.getControllerPlayerId(e));
return rVal;
}

View File

@ -5,10 +5,10 @@ import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.net.message.EntityMessage;
import electrosphere.net.message.NetworkMessage;
import electrosphere.net.message.PlayerMessage;
import electrosphere.net.raw.NetworkParser;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.parser.net.message.PlayerMessage;
import electrosphere.net.parser.net.raw.NetworkParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -111,33 +111,33 @@ public class ClientNetworking implements Runnable{
}
void handleEntityMessage(EntityMessage message){
System.out.println(message.getEntityMessageType());
switch(message.getEntityMessageType()){
System.out.println(message.getMessageSubtype());
switch(message.getMessageSubtype()){
case CREATE:
System.out.println("Spawn ID " + message.getEntityType());
Entity newlySpawnedEntity = CreatureUtils.spawnBasicCreature(message.getEntityType(), 0.005f, 0.025f);
System.out.println("Spawn ID " + message.getentityID());
Entity newlySpawnedEntity = CreatureUtils.spawnBasicCreature(message.getcreatureType(), 0.005f, 0.025f);
EntityUtils.getEntityScale(newlySpawnedEntity).set(0.005f);
EntityUtils.getEntityPosition(newlySpawnedEntity).set(10 - 0.5f,Globals.terrainManager.getHeightAtPosition(10, 10),10 - 0.5f);
EntityUtils.setEntityID(newlySpawnedEntity, message.getId());
EntityUtils.setEntityID(newlySpawnedEntity, message.getentityID());
break;
case DESTROY:
break;
case MOVE:
//literally just adding this to scope so I can use `` Entity target; `` again
if(message.getId() != -1){
Entity target = Globals.entityManager.getEntityFromId(message.getId());
EntityUtils.getEntityPosition(target).set(message.getX(),message.getY(),message.getZ());
if(message.getentityID() != -1){
Entity target = Globals.entityManager.getEntityFromId(message.getentityID());
EntityUtils.getEntityPosition(target).set(message.getpositionX(),message.getpositionY(),message.getpositionZ());
}
// CreatureUtils.attachEntityMessageToMovementTree(Globals.entityManager.getEntityFromId(message.getId()),message);
break;
case SET_BEHAVIOR_TREE:
case SETBEHAVIORTREE:
break;
case SET_PROPERTY:
if(message.getPropertyType() == 0){
Entity target = Globals.entityManager.getEntityFromId(message.getId());
case SETPROPERTY:
if(message.getpropertyType()== 0){
Entity target = Globals.entityManager.getEntityFromId(message.getentityID());
if(target != null){
CreatureUtils.setControllerPlayerId(target, message.getPropertyValue());
if(message.getPropertyValue() == Main.playerId){
CreatureUtils.setControllerPlayerId(target, message.getpropertyValue());
if(message.getpropertyValue() == Main.playerId){
Globals.playerCharacter = target;
}
}
@ -147,9 +147,9 @@ public class ClientNetworking implements Runnable{
}
void handlePlayerMessage(PlayerMessage message){
switch(message.getPlayerMessageType()){
case SET_PLAYER_ID:
Main.playerId = message.getId();
switch(message.getMessageSubtype()){
case SET_ID:
Main.playerId = message.getplayerID();
System.out.println("Player ID is " + Main.playerId);
break;
}

View File

@ -0,0 +1,389 @@
package electrosphere.net.parser.net.message;
import electrosphere.net.parser.util.ByteStreamUtils;
import java.util.LinkedList;
public class EntityMessage extends NetworkMessage {
public enum EntityMessageType {
CREATE,
SETPOSITION,
MOVE,
DESTROY,
SETBEHAVIORTREE,
SETPROPERTY,
}
EntityMessageType messageType;
int creatureType;
int entityID;
float positionX;
float positionY;
float positionZ;
int propertyType;
int propertyValue;
int treeType;
int treeStatus;
EntityMessage(EntityMessageType messageType){
this.type = MessageType.ENTITY_MESSAGE;
this.messageType = messageType;
}
public EntityMessageType getMessageSubtype(){
return this.messageType;
}
public int getcreatureType() {
return creatureType;
}
public void setcreatureType(int creatureType) {
this.creatureType = creatureType;
}
public int getentityID() {
return entityID;
}
public void setentityID(int entityID) {
this.entityID = entityID;
}
public float getpositionX() {
return positionX;
}
public void setpositionX(float positionX) {
this.positionX = positionX;
}
public float getpositionY() {
return positionY;
}
public void setpositionY(float positionY) {
this.positionY = positionY;
}
public float getpositionZ() {
return positionZ;
}
public void setpositionZ(float positionZ) {
this.positionZ = positionZ;
}
public int getpropertyType() {
return propertyType;
}
public void setpropertyType(int propertyType) {
this.propertyType = propertyType;
}
public int getpropertyValue() {
return propertyValue;
}
public void setpropertyValue(int propertyValue) {
this.propertyValue = propertyValue;
}
public int gettreeType() {
return treeType;
}
public void settreeType(int treeType) {
this.treeType = treeType;
}
public int gettreeStatus() {
return treeStatus;
}
public void settreeStatus(int treeStatus) {
this.treeStatus = treeStatus;
}
static void stripPacketHeader(LinkedList<Byte> byteStream){
byteStream.remove(0);
byteStream.remove(0);
}
public static boolean canParseMessage(LinkedList<Byte> byteStream, byte secondByte){
switch(secondByte){
case TypeBytes.ENTITY_MESSAGE_TYPE_CREATE:
if(byteStream.size() >= TypeBytes.ENTITY_MESSAGE_TYPE_CREATE_SIZE){
return true;
} else {
return false;
}
case TypeBytes.ENTITY_MESSAGE_TYPE_SETPOSITION:
if(byteStream.size() >= TypeBytes.ENTITY_MESSAGE_TYPE_SETPOSITION_SIZE){
return true;
} else {
return false;
}
case TypeBytes.ENTITY_MESSAGE_TYPE_MOVE:
if(byteStream.size() >= TypeBytes.ENTITY_MESSAGE_TYPE_MOVE_SIZE){
return true;
} else {
return false;
}
case TypeBytes.ENTITY_MESSAGE_TYPE_DESTROY:
if(byteStream.size() >= TypeBytes.ENTITY_MESSAGE_TYPE_DESTROY_SIZE){
return true;
} else {
return false;
}
case TypeBytes.ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE:
if(byteStream.size() >= TypeBytes.ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE_SIZE){
return true;
} else {
return false;
}
case TypeBytes.ENTITY_MESSAGE_TYPE_SETPROPERTY:
if(byteStream.size() >= TypeBytes.ENTITY_MESSAGE_TYPE_SETPROPERTY_SIZE){
return true;
} else {
return false;
}
}
return false;
}
public static EntityMessage parseCreateMessage(LinkedList<Byte> byteStream){
EntityMessage rVal = new EntityMessage(EntityMessageType.CREATE);
stripPacketHeader(byteStream);
rVal.setcreatureType(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setentityID(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setpositionX(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setpositionY(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setpositionZ(ByteStreamUtils.popFloatFromByteQueue(byteStream));
return rVal;
}
public static EntityMessage constructCreateMessage(int creatureType,int entityID,float positionX,float positionY,float positionZ){
EntityMessage rVal = new EntityMessage(EntityMessageType.CREATE);
rVal.setcreatureType(creatureType);
rVal.setentityID(entityID);
rVal.setpositionX(positionX);
rVal.setpositionY(positionY);
rVal.setpositionZ(positionZ);
rVal.serialize();
return rVal;
}
public static EntityMessage parseSetPositionMessage(LinkedList<Byte> byteStream){
EntityMessage rVal = new EntityMessage(EntityMessageType.SETPOSITION);
stripPacketHeader(byteStream);
rVal.setentityID(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setpositionX(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setpositionY(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setpositionZ(ByteStreamUtils.popFloatFromByteQueue(byteStream));
return rVal;
}
public static EntityMessage constructSetPositionMessage(int entityID,float positionX,float positionY,float positionZ){
EntityMessage rVal = new EntityMessage(EntityMessageType.SETPOSITION);
rVal.setentityID(entityID);
rVal.setpositionX(positionX);
rVal.setpositionY(positionY);
rVal.setpositionZ(positionZ);
rVal.serialize();
return rVal;
}
public static EntityMessage parseMoveMessage(LinkedList<Byte> byteStream){
EntityMessage rVal = new EntityMessage(EntityMessageType.MOVE);
stripPacketHeader(byteStream);
rVal.setentityID(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setpositionX(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setpositionY(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setpositionZ(ByteStreamUtils.popFloatFromByteQueue(byteStream));
return rVal;
}
public static EntityMessage constructMoveMessage(int entityID,float positionX,float positionY,float positionZ){
EntityMessage rVal = new EntityMessage(EntityMessageType.MOVE);
rVal.setentityID(entityID);
rVal.setpositionX(positionX);
rVal.setpositionY(positionY);
rVal.setpositionZ(positionZ);
rVal.serialize();
return rVal;
}
public static EntityMessage parseDestroyMessage(LinkedList<Byte> byteStream){
EntityMessage rVal = new EntityMessage(EntityMessageType.DESTROY);
stripPacketHeader(byteStream);
rVal.setentityID(ByteStreamUtils.popIntFromByteQueue(byteStream));
return rVal;
}
public static EntityMessage constructDestroyMessage(int entityID){
EntityMessage rVal = new EntityMessage(EntityMessageType.DESTROY);
rVal.setentityID(entityID);
rVal.serialize();
return rVal;
}
public static EntityMessage parseSetBehaviorTreeMessage(LinkedList<Byte> byteStream){
EntityMessage rVal = new EntityMessage(EntityMessageType.SETBEHAVIORTREE);
stripPacketHeader(byteStream);
rVal.setentityID(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.settreeType(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.settreeStatus(ByteStreamUtils.popIntFromByteQueue(byteStream));
return rVal;
}
public static EntityMessage constructSetBehaviorTreeMessage(int entityID,int treeType,int treeStatus){
EntityMessage rVal = new EntityMessage(EntityMessageType.SETBEHAVIORTREE);
rVal.setentityID(entityID);
rVal.settreeType(treeType);
rVal.settreeStatus(treeStatus);
rVal.serialize();
return rVal;
}
public static EntityMessage parsesetPropertyMessage(LinkedList<Byte> byteStream){
EntityMessage rVal = new EntityMessage(EntityMessageType.SETPROPERTY);
stripPacketHeader(byteStream);
rVal.setentityID(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setpropertyType(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setpropertyValue(ByteStreamUtils.popIntFromByteQueue(byteStream));
return rVal;
}
public static EntityMessage constructsetPropertyMessage(int entityID,int propertyType,int propertyValue){
EntityMessage rVal = new EntityMessage(EntityMessageType.SETPROPERTY);
rVal.setentityID(entityID);
rVal.setpropertyType(propertyType);
rVal.setpropertyValue(propertyValue);
rVal.serialize();
return rVal;
}
@Override
void serialize(){
byte[] intValues = new byte[8];
switch(this.messageType){
case CREATE:
rawBytes = new byte[TypeBytes.ENTITY_MESSAGE_TYPE_CREATE_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_ENTITY;
//entity messaage header
rawBytes[1] = TypeBytes.ENTITY_MESSAGE_TYPE_CREATE;
intValues = ByteStreamUtils.serializeIntToBytes(creatureType);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeIntToBytes(entityID);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeFloatToBytes(positionX);
for(int i = 0; i < 4; i++){
rawBytes[10+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(positionY);
for(int i = 0; i < 4; i++){
rawBytes[14+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(positionZ);
for(int i = 0; i < 4; i++){
rawBytes[18+i] = intValues[i];
} break;
case SETPOSITION:
rawBytes = new byte[TypeBytes.ENTITY_MESSAGE_TYPE_SETPOSITION_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_ENTITY;
//entity messaage header
rawBytes[1] = TypeBytes.ENTITY_MESSAGE_TYPE_SETPOSITION;
intValues = ByteStreamUtils.serializeIntToBytes(entityID);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeFloatToBytes(positionX);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(positionY);
for(int i = 0; i < 4; i++){
rawBytes[10+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(positionZ);
for(int i = 0; i < 4; i++){
rawBytes[14+i] = intValues[i];
} break;
case MOVE:
rawBytes = new byte[TypeBytes.ENTITY_MESSAGE_TYPE_MOVE_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_ENTITY;
//entity messaage header
rawBytes[1] = TypeBytes.ENTITY_MESSAGE_TYPE_MOVE;
intValues = ByteStreamUtils.serializeIntToBytes(entityID);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeFloatToBytes(positionX);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(positionY);
for(int i = 0; i < 4; i++){
rawBytes[10+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(positionZ);
for(int i = 0; i < 4; i++){
rawBytes[14+i] = intValues[i];
} break;
case DESTROY:
rawBytes = new byte[TypeBytes.ENTITY_MESSAGE_TYPE_DESTROY_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_ENTITY;
//entity messaage header
rawBytes[1] = TypeBytes.ENTITY_MESSAGE_TYPE_DESTROY;
intValues = ByteStreamUtils.serializeIntToBytes(entityID);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
break;
case SETBEHAVIORTREE:
rawBytes = new byte[TypeBytes.ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_ENTITY;
//entity messaage header
rawBytes[1] = TypeBytes.ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE;
intValues = ByteStreamUtils.serializeIntToBytes(entityID);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeIntToBytes(treeType);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeIntToBytes(treeStatus);
for(int i = 0; i < 4; i++){
rawBytes[10+i] = intValues[i];
}
break;
case SETPROPERTY:
rawBytes = new byte[TypeBytes.ENTITY_MESSAGE_TYPE_SETPROPERTY_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_ENTITY;
//entity messaage header
rawBytes[1] = TypeBytes.ENTITY_MESSAGE_TYPE_SETPROPERTY;
intValues = ByteStreamUtils.serializeIntToBytes(entityID);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeIntToBytes(propertyType);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeIntToBytes(propertyValue);
for(int i = 0; i < 4; i++){
rawBytes[10+i] = intValues[i];
}
break;
}
serialized = true;
}
}

View File

@ -0,0 +1,115 @@
package electrosphere.net.parser.net.message;
import electrosphere.net.parser.util.ByteStreamUtils;
import java.util.LinkedList;
public abstract class NetworkMessage {
public enum MessageType {
WORLD_MESSAGE,
PLAYER_MESSAGE,
ENTITY_MESSAGE,
}
MessageType type;
boolean serialized; // has this message been converted to bytes?
byte[] rawBytes;
public MessageType getType() {
return type;
}
public byte[] getRawBytes() {
return rawBytes;
}
public static NetworkMessage parseBytestreamForMessage(LinkedList<Byte> byteStream){
NetworkMessage rVal = null;
byte firstByte;
byte secondByte;
if(byteStream.size() > 1){
firstByte = byteStream.get(0);
switch(firstByte){
case TypeBytes.MESSAGE_TYPE_WORLD:
secondByte = byteStream.get(1);
switch(secondByte){
case TypeBytes.WORLD_MESSAGE_TYPE_INFO:
if(WorldMessage.canParseMessage(byteStream,secondByte)){
rVal = WorldMessage.parseInfoMessage(byteStream);
}
break;
case TypeBytes.WORLD_MESSAGE_TYPE_UPDATE:
if(WorldMessage.canParseMessage(byteStream,secondByte)){
rVal = WorldMessage.parseUpdateMessage(byteStream);
}
break;
case TypeBytes.WORLD_MESSAGE_TYPE_MACROVALUE:
if(WorldMessage.canParseMessage(byteStream,secondByte)){
rVal = WorldMessage.parseMacroValueMessage(byteStream);
}
break;
case TypeBytes.WORLD_MESSAGE_TYPE_RANDOMIZERVALUE:
if(WorldMessage.canParseMessage(byteStream,secondByte)){
rVal = WorldMessage.parseRandomizerValueMessage(byteStream);
}
break;
}
break;
case TypeBytes.MESSAGE_TYPE_PLAYER:
secondByte = byteStream.get(1);
switch(secondByte){
case TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID:
if(PlayerMessage.canParseMessage(byteStream,secondByte)){
rVal = PlayerMessage.parseSet_IDMessage(byteStream);
}
break;
}
break;
case TypeBytes.MESSAGE_TYPE_ENTITY:
secondByte = byteStream.get(1);
switch(secondByte){
case TypeBytes.ENTITY_MESSAGE_TYPE_CREATE:
if(EntityMessage.canParseMessage(byteStream,secondByte)){
rVal = EntityMessage.parseCreateMessage(byteStream);
}
break;
case TypeBytes.ENTITY_MESSAGE_TYPE_SETPOSITION:
if(EntityMessage.canParseMessage(byteStream,secondByte)){
rVal = EntityMessage.parseSetPositionMessage(byteStream);
}
break;
case TypeBytes.ENTITY_MESSAGE_TYPE_MOVE:
if(EntityMessage.canParseMessage(byteStream,secondByte)){
rVal = EntityMessage.parseMoveMessage(byteStream);
}
break;
case TypeBytes.ENTITY_MESSAGE_TYPE_DESTROY:
if(EntityMessage.canParseMessage(byteStream,secondByte)){
rVal = EntityMessage.parseDestroyMessage(byteStream);
}
break;
case TypeBytes.ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE:
if(EntityMessage.canParseMessage(byteStream,secondByte)){
rVal = EntityMessage.parseSetBehaviorTreeMessage(byteStream);
}
break;
case TypeBytes.ENTITY_MESSAGE_TYPE_SETPROPERTY:
if(EntityMessage.canParseMessage(byteStream,secondByte)){
rVal = EntityMessage.parsesetPropertyMessage(byteStream);
}
break;
}
break;
}
}
return rVal;
}
public boolean isSerialized(){
return serialized;
}
abstract void serialize();
}

View File

@ -0,0 +1,82 @@
package electrosphere.net.parser.net.message;
import electrosphere.net.parser.util.ByteStreamUtils;
import java.util.LinkedList;
public class PlayerMessage extends NetworkMessage {
public enum PlayerMessageType {
SET_ID,
}
PlayerMessageType messageType;
int playerID;
PlayerMessage(PlayerMessageType messageType){
this.type = MessageType.PLAYER_MESSAGE;
this.messageType = messageType;
}
public PlayerMessageType getMessageSubtype(){
return this.messageType;
}
public int getplayerID() {
return playerID;
}
public void setplayerID(int playerID) {
this.playerID = playerID;
}
static void stripPacketHeader(LinkedList<Byte> byteStream){
byteStream.remove(0);
byteStream.remove(0);
}
public static boolean canParseMessage(LinkedList<Byte> byteStream, byte secondByte){
switch(secondByte){
case TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID:
if(byteStream.size() >= TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID_SIZE){
return true;
} else {
return false;
}
}
return false;
}
public static PlayerMessage parseSet_IDMessage(LinkedList<Byte> byteStream){
PlayerMessage rVal = new PlayerMessage(PlayerMessageType.SET_ID);
stripPacketHeader(byteStream);
rVal.setplayerID(ByteStreamUtils.popIntFromByteQueue(byteStream));
return rVal;
}
public static PlayerMessage constructSet_IDMessage(int playerID){
PlayerMessage rVal = new PlayerMessage(PlayerMessageType.SET_ID);
rVal.setplayerID(playerID);
rVal.serialize();
return rVal;
}
@Override
void serialize(){
byte[] intValues = new byte[8];
switch(this.messageType){
case SET_ID:
rawBytes = new byte[TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_PLAYER;
//entity messaage header
rawBytes[1] = TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID;
intValues = ByteStreamUtils.serializeIntToBytes(playerID);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
break;
}
serialized = true;
}
}

View File

@ -0,0 +1,52 @@
package electrosphere.net.parser.net.message;
public class TypeBytes {
/*
Message categories
*/
public static final byte MESSAGE_TYPE_WORLD = 0;
public static final byte MESSAGE_TYPE_PLAYER = 1;
public static final byte MESSAGE_TYPE_ENTITY = 2;
/*
World subcategories
*/
public static final byte WORLD_MESSAGE_TYPE_INFO = 0;
public static final byte WORLD_MESSAGE_TYPE_UPDATE = 1;
public static final byte WORLD_MESSAGE_TYPE_MACROVALUE = 2;
public static final byte WORLD_MESSAGE_TYPE_RANDOMIZERVALUE = 3;
/*
World packet sizes
*/
public static final byte WORLD_MESSAGE_TYPE_INFO_SIZE = 6;
public static final byte WORLD_MESSAGE_TYPE_UPDATE_SIZE = 10;
public static final byte WORLD_MESSAGE_TYPE_MACROVALUE_SIZE = 74;
public static final byte WORLD_MESSAGE_TYPE_RANDOMIZERVALUE_SIZE = 38;
/*
Player subcategories
*/
public static final byte PLAYER_MESSAGE_TYPE_SET_ID = 0;
/*
Player packet sizes
*/
public static final byte PLAYER_MESSAGE_TYPE_SET_ID_SIZE = 6;
/*
Entity subcategories
*/
public static final byte ENTITY_MESSAGE_TYPE_CREATE = 0;
public static final byte ENTITY_MESSAGE_TYPE_SETPOSITION = 1;
public static final byte ENTITY_MESSAGE_TYPE_MOVE = 2;
public static final byte ENTITY_MESSAGE_TYPE_DESTROY = 3;
public static final byte ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE = 4;
public static final byte ENTITY_MESSAGE_TYPE_SETPROPERTY = 5;
/*
Entity packet sizes
*/
public static final byte ENTITY_MESSAGE_TYPE_CREATE_SIZE = 22;
public static final byte ENTITY_MESSAGE_TYPE_SETPOSITION_SIZE = 18;
public static final byte ENTITY_MESSAGE_TYPE_MOVE_SIZE = 18;
public static final byte ENTITY_MESSAGE_TYPE_DESTROY_SIZE = 6;
public static final byte ENTITY_MESSAGE_TYPE_SETBEHAVIORTREE_SIZE = 14;
public static final byte ENTITY_MESSAGE_TYPE_SETPROPERTY_SIZE = 14;
}

View File

@ -0,0 +1,442 @@
package electrosphere.net.parser.net.message;
import electrosphere.net.parser.util.ByteStreamUtils;
import java.util.LinkedList;
public class WorldMessage extends NetworkMessage {
public enum WorldMessageType {
INFO,
UPDATE,
MACROVALUE,
RANDOMIZERVALUE,
}
WorldMessageType messageType;
int worldSize;
int locationX;
int locationY;
long macroValue00;
long macroValue01;
long macroValue02;
long macroValue10;
long macroValue11;
long macroValue12;
long macroValue20;
long macroValue21;
long macroValue22;
float randomizerValue00;
float randomizerValue01;
float randomizerValue02;
float randomizerValue10;
float randomizerValue11;
float randomizerValue12;
float randomizerValue20;
float randomizerValue21;
float randomizerValue22;
WorldMessage(WorldMessageType messageType){
this.type = MessageType.WORLD_MESSAGE;
this.messageType = messageType;
}
public WorldMessageType getMessageSubtype(){
return this.messageType;
}
public int getworldSize() {
return worldSize;
}
public void setworldSize(int worldSize) {
this.worldSize = worldSize;
}
public int getlocationX() {
return locationX;
}
public void setlocationX(int locationX) {
this.locationX = locationX;
}
public int getlocationY() {
return locationY;
}
public void setlocationY(int locationY) {
this.locationY = locationY;
}
public long getmacroValue00() {
return macroValue00;
}
public void setmacroValue00(long macroValue00) {
this.macroValue00 = macroValue00;
}
public long getmacroValue01() {
return macroValue01;
}
public void setmacroValue01(long macroValue01) {
this.macroValue01 = macroValue01;
}
public long getmacroValue02() {
return macroValue02;
}
public void setmacroValue02(long macroValue02) {
this.macroValue02 = macroValue02;
}
public long getmacroValue10() {
return macroValue10;
}
public void setmacroValue10(long macroValue10) {
this.macroValue10 = macroValue10;
}
public long getmacroValue11() {
return macroValue11;
}
public void setmacroValue11(long macroValue11) {
this.macroValue11 = macroValue11;
}
public long getmacroValue12() {
return macroValue12;
}
public void setmacroValue12(long macroValue12) {
this.macroValue12 = macroValue12;
}
public long getmacroValue20() {
return macroValue20;
}
public void setmacroValue20(long macroValue20) {
this.macroValue20 = macroValue20;
}
public long getmacroValue21() {
return macroValue21;
}
public void setmacroValue21(long macroValue21) {
this.macroValue21 = macroValue21;
}
public long getmacroValue22() {
return macroValue22;
}
public void setmacroValue22(long macroValue22) {
this.macroValue22 = macroValue22;
}
public float getrandomizerValue00() {
return randomizerValue00;
}
public void setrandomizerValue00(float randomizerValue00) {
this.randomizerValue00 = randomizerValue00;
}
public float getrandomizerValue01() {
return randomizerValue01;
}
public void setrandomizerValue01(float randomizerValue01) {
this.randomizerValue01 = randomizerValue01;
}
public float getrandomizerValue02() {
return randomizerValue02;
}
public void setrandomizerValue02(float randomizerValue02) {
this.randomizerValue02 = randomizerValue02;
}
public float getrandomizerValue10() {
return randomizerValue10;
}
public void setrandomizerValue10(float randomizerValue10) {
this.randomizerValue10 = randomizerValue10;
}
public float getrandomizerValue11() {
return randomizerValue11;
}
public void setrandomizerValue11(float randomizerValue11) {
this.randomizerValue11 = randomizerValue11;
}
public float getrandomizerValue12() {
return randomizerValue12;
}
public void setrandomizerValue12(float randomizerValue12) {
this.randomizerValue12 = randomizerValue12;
}
public float getrandomizerValue20() {
return randomizerValue20;
}
public void setrandomizerValue20(float randomizerValue20) {
this.randomizerValue20 = randomizerValue20;
}
public float getrandomizerValue21() {
return randomizerValue21;
}
public void setrandomizerValue21(float randomizerValue21) {
this.randomizerValue21 = randomizerValue21;
}
public float getrandomizerValue22() {
return randomizerValue22;
}
public void setrandomizerValue22(float randomizerValue22) {
this.randomizerValue22 = randomizerValue22;
}
static void stripPacketHeader(LinkedList<Byte> byteStream){
byteStream.remove(0);
byteStream.remove(0);
}
public static boolean canParseMessage(LinkedList<Byte> byteStream, byte secondByte){
switch(secondByte){
case TypeBytes.WORLD_MESSAGE_TYPE_INFO:
if(byteStream.size() >= TypeBytes.WORLD_MESSAGE_TYPE_INFO_SIZE){
return true;
} else {
return false;
}
case TypeBytes.WORLD_MESSAGE_TYPE_UPDATE:
if(byteStream.size() >= TypeBytes.WORLD_MESSAGE_TYPE_UPDATE_SIZE){
return true;
} else {
return false;
}
case TypeBytes.WORLD_MESSAGE_TYPE_MACROVALUE:
if(byteStream.size() >= TypeBytes.WORLD_MESSAGE_TYPE_MACROVALUE_SIZE){
return true;
} else {
return false;
}
case TypeBytes.WORLD_MESSAGE_TYPE_RANDOMIZERVALUE:
if(byteStream.size() >= TypeBytes.WORLD_MESSAGE_TYPE_RANDOMIZERVALUE_SIZE){
return true;
} else {
return false;
}
}
return false;
}
public static WorldMessage parseInfoMessage(LinkedList<Byte> byteStream){
WorldMessage rVal = new WorldMessage(WorldMessageType.INFO);
stripPacketHeader(byteStream);
rVal.setworldSize(ByteStreamUtils.popIntFromByteQueue(byteStream));
return rVal;
}
public static WorldMessage constructInfoMessage(int worldSize){
WorldMessage rVal = new WorldMessage(WorldMessageType.INFO);
rVal.setworldSize(worldSize);
rVal.serialize();
return rVal;
}
public static WorldMessage parseUpdateMessage(LinkedList<Byte> byteStream){
WorldMessage rVal = new WorldMessage(WorldMessageType.UPDATE);
stripPacketHeader(byteStream);
rVal.setlocationX(ByteStreamUtils.popIntFromByteQueue(byteStream));
rVal.setlocationY(ByteStreamUtils.popIntFromByteQueue(byteStream));
return rVal;
}
public static WorldMessage constructUpdateMessage(int locationX,int locationY){
WorldMessage rVal = new WorldMessage(WorldMessageType.UPDATE);
rVal.setlocationX(locationX);
rVal.setlocationY(locationY);
rVal.serialize();
return rVal;
}
public static WorldMessage parseMacroValueMessage(LinkedList<Byte> byteStream){
WorldMessage rVal = new WorldMessage(WorldMessageType.MACROVALUE);
stripPacketHeader(byteStream);
rVal.setmacroValue00(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue01(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue02(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue10(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue11(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue12(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue20(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue21(ByteStreamUtils.popLongFromByteQueue(byteStream));
rVal.setmacroValue22(ByteStreamUtils.popLongFromByteQueue(byteStream));
return rVal;
}
public static WorldMessage constructMacroValueMessage(long macroValue00,long macroValue01,long macroValue02,long macroValue10,long macroValue11,long macroValue12,long macroValue20,long macroValue21,long macroValue22){
WorldMessage rVal = new WorldMessage(WorldMessageType.MACROVALUE);
rVal.setmacroValue00(macroValue00);
rVal.setmacroValue01(macroValue01);
rVal.setmacroValue02(macroValue02);
rVal.setmacroValue10(macroValue10);
rVal.setmacroValue11(macroValue11);
rVal.setmacroValue12(macroValue12);
rVal.setmacroValue20(macroValue20);
rVal.setmacroValue21(macroValue21);
rVal.setmacroValue22(macroValue22);
rVal.serialize();
return rVal;
}
public static WorldMessage parseRandomizerValueMessage(LinkedList<Byte> byteStream){
WorldMessage rVal = new WorldMessage(WorldMessageType.RANDOMIZERVALUE);
stripPacketHeader(byteStream);
rVal.setrandomizerValue00(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue01(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue02(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue10(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue11(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue12(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue20(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue21(ByteStreamUtils.popFloatFromByteQueue(byteStream));
rVal.setrandomizerValue22(ByteStreamUtils.popFloatFromByteQueue(byteStream));
return rVal;
}
public static WorldMessage constructRandomizerValueMessage(float randomizerValue00,float randomizerValue01,float randomizerValue02,float randomizerValue10,float randomizerValue11,float randomizerValue12,float randomizerValue20,float randomizerValue21,float randomizerValue22){
WorldMessage rVal = new WorldMessage(WorldMessageType.RANDOMIZERVALUE);
rVal.setrandomizerValue00(randomizerValue00);
rVal.setrandomizerValue01(randomizerValue01);
rVal.setrandomizerValue02(randomizerValue02);
rVal.setrandomizerValue10(randomizerValue10);
rVal.setrandomizerValue11(randomizerValue11);
rVal.setrandomizerValue12(randomizerValue12);
rVal.setrandomizerValue20(randomizerValue20);
rVal.setrandomizerValue21(randomizerValue21);
rVal.setrandomizerValue22(randomizerValue22);
rVal.serialize();
return rVal;
}
@Override
void serialize(){
byte[] intValues = new byte[8];
switch(this.messageType){
case INFO:
rawBytes = new byte[TypeBytes.WORLD_MESSAGE_TYPE_INFO_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_WORLD;
//entity messaage header
rawBytes[1] = TypeBytes.WORLD_MESSAGE_TYPE_INFO;
intValues = ByteStreamUtils.serializeIntToBytes(worldSize);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
break;
case UPDATE:
rawBytes = new byte[TypeBytes.WORLD_MESSAGE_TYPE_UPDATE_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_WORLD;
//entity messaage header
rawBytes[1] = TypeBytes.WORLD_MESSAGE_TYPE_UPDATE;
intValues = ByteStreamUtils.serializeIntToBytes(locationX);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
}
intValues = ByteStreamUtils.serializeIntToBytes(locationY);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
}
break;
case MACROVALUE:
rawBytes = new byte[TypeBytes.WORLD_MESSAGE_TYPE_MACROVALUE_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_WORLD;
//entity messaage header
rawBytes[1] = TypeBytes.WORLD_MESSAGE_TYPE_MACROVALUE;
intValues = ByteStreamUtils.serializeLongToBytes(macroValue00);
for(int i = 0; i < 8; i++){
rawBytes[2+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue01);
for(int i = 0; i < 8; i++){
rawBytes[6+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue02);
for(int i = 0; i < 8; i++){
rawBytes[10+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue10);
for(int i = 0; i < 8; i++){
rawBytes[14+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue11);
for(int i = 0; i < 8; i++){
rawBytes[18+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue12);
for(int i = 0; i < 8; i++){
rawBytes[22+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue20);
for(int i = 0; i < 8; i++){
rawBytes[26+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue21);
for(int i = 0; i < 8; i++){
rawBytes[30+i] = intValues[i];
} intValues = ByteStreamUtils.serializeLongToBytes(macroValue22);
for(int i = 0; i < 8; i++){
rawBytes[34+i] = intValues[i];
} break;
case RANDOMIZERVALUE:
rawBytes = new byte[TypeBytes.WORLD_MESSAGE_TYPE_RANDOMIZERVALUE_SIZE];
//message header
rawBytes[0] = TypeBytes.MESSAGE_TYPE_WORLD;
//entity messaage header
rawBytes[1] = TypeBytes.WORLD_MESSAGE_TYPE_RANDOMIZERVALUE;
intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue00);
for(int i = 0; i < 4; i++){
rawBytes[2+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue01);
for(int i = 0; i < 4; i++){
rawBytes[6+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue02);
for(int i = 0; i < 4; i++){
rawBytes[10+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue10);
for(int i = 0; i < 4; i++){
rawBytes[14+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue11);
for(int i = 0; i < 4; i++){
rawBytes[18+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue12);
for(int i = 0; i < 4; i++){
rawBytes[22+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue20);
for(int i = 0; i < 4; i++){
rawBytes[26+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue21);
for(int i = 0; i < 4; i++){
rawBytes[30+i] = intValues[i];
} intValues = ByteStreamUtils.serializeFloatToBytes(randomizerValue22);
for(int i = 0; i < 4; i++){
rawBytes[34+i] = intValues[i];
} break;
}
serialized = true;
}
}

View File

@ -0,0 +1,81 @@
package electrosphere.net.parser.net.raw;
import electrosphere.net.parser.net.message.NetworkMessage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
public class NetworkParser {
InputStream incomingStream;
OutputStream outgoingStream;
LinkedList<NetworkMessage> incomingMessageQueue = new LinkedList();
LinkedList<NetworkMessage> outgoingMessageQueue = new LinkedList();
LinkedList<Byte> incomingByteQueue = new LinkedList();
LinkedList<Byte> outgoingByteQueue = new LinkedList();
public NetworkParser(InputStream incomingStream, OutputStream outgoingStream){
this.incomingStream = incomingStream;
this.outgoingStream = outgoingStream;
}
public void start(){
}
public void readMessagesIn(){
try {
//read in bytes
int nextValue = 0;
byte currentByte = -1;
while(incomingStream.available() > 0){
nextValue = incomingStream.read();
if(nextValue != -1){
currentByte = (byte)nextValue;
incomingByteQueue.add(currentByte);
// System.out.println(currentByte);
}
//add bytes to some kind of queue
}
//parse byte queue for messages
//for each message, append to clientIncomingMessageQueue
NetworkMessage newMessage;
while((newMessage = NetworkMessage.parseBytestreamForMessage(incomingByteQueue))!=null){
incomingMessageQueue.add(newMessage);
}
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
}
public void pushMessagesOut(){
for(NetworkMessage message : outgoingMessageQueue){
outgoingMessageQueue.remove(message);
try {
// System.out.println("Write message of type " + message.getType());
outgoingStream.write(message.getRawBytes());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public boolean hasIncomingMessaage(){
return incomingMessageQueue.size() > 0;
}
public NetworkMessage popIncomingMessage(){
return incomingMessageQueue.remove(0);
}
public void addOutgoingMessage(NetworkMessage message){
outgoingMessageQueue.add(message);
}
}

View File

@ -0,0 +1,93 @@
package electrosphere.net.parser.util;
import java.nio.ByteBuffer;
import java.util.LinkedList;
public class ByteStreamUtils {
static ByteBuffer integerCompactor;
static {
integerCompactor = ByteBuffer.allocate(8);
}
public static int popIntFromByteQueue(LinkedList<Byte> queue){
int rVal = -1;
integerCompactor.clear();
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.flip();
rVal = integerCompactor.getInt();
return rVal;
}
public static float popFloatFromByteQueue(LinkedList<Byte> queue){
float rVal = -1;
integerCompactor.clear();
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.flip();
rVal = integerCompactor.getFloat();
return rVal;
}
public static long popLongFromByteQueue(LinkedList<Byte> queue){
long rVal = -1;
integerCompactor.clear();
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.put(queue.remove(0));
integerCompactor.flip();
rVal = integerCompactor.getLong();
return rVal;
}
public static byte[] serializeIntToBytes(int i){
byte[] rVal = new byte[4];
integerCompactor.clear();
integerCompactor.putInt(i);
integerCompactor.flip();
rVal[0] = integerCompactor.get();
rVal[1] = integerCompactor.get();
rVal[2] = integerCompactor.get();
rVal[3] = integerCompactor.get();
return rVal;
}
public static byte[] serializeFloatToBytes(float i){
byte[] rVal = new byte[4];
integerCompactor.clear();
integerCompactor.putFloat(i);
integerCompactor.flip();
rVal[0] = integerCompactor.get();
rVal[1] = integerCompactor.get();
rVal[2] = integerCompactor.get();
rVal[3] = integerCompactor.get();
return rVal;
}
public static byte[] serializeLongToBytes(long i){
byte[] rVal = new byte[8];
integerCompactor.clear();
integerCompactor.putLong(i);
integerCompactor.flip();
rVal[0] = integerCompactor.get();
rVal[1] = integerCompactor.get();
rVal[2] = integerCompactor.get();
rVal[3] = integerCompactor.get();
rVal[4] = integerCompactor.get();
rVal[5] = integerCompactor.get();
rVal[6] = integerCompactor.get();
rVal[7] = integerCompactor.get();
return rVal;
}
}

View File

@ -2,7 +2,7 @@ package electrosphere.net.server;
import electrosphere.main.Main;
import electrosphere.net.NetUtils;
import electrosphere.net.message.NetworkMessage;
import electrosphere.net.parser.net.message.NetworkMessage;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

View File

@ -8,11 +8,10 @@ import electrosphere.game.state.AttachUtils;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.net.NetUtils;
import electrosphere.net.message.EntityMessage;
import electrosphere.net.message.NetworkMessage;
import electrosphere.net.message.NetworkMessage.MessageType;
import electrosphere.net.message.PlayerMessage;
import electrosphere.net.raw.NetworkParser;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.parser.net.message.PlayerMessage;
import electrosphere.net.parser.net.raw.NetworkParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -99,7 +98,7 @@ public class ServerConnectionHandler implements Runnable {
Globals.playerCharacter = newPlayerCharacter;
}
//tell them what player stats they are
networkParser.addOutgoingMessage(PlayerMessage.constructSetPlayerIDMessage(playerID));
networkParser.addOutgoingMessage(PlayerMessage.constructSet_IDMessage(playerID));
//figure out what chunk they're in
//queue messages for that chunk
@ -147,18 +146,18 @@ public class ServerConnectionHandler implements Runnable {
}
void handleEntityMessage(EntityMessage message){
switch(message.getEntityMessageType()){
switch(message.getMessageSubtype()){
case CREATE:
break;
case DESTROY:
break;
case MOVE:
Entity targetEntity = Globals.entityManager.getEntityFromId(message.getId());
Entity targetEntity = Globals.entityManager.getEntityFromId(message.getentityID());
if(targetEntity != null){
CreatureUtils.attachEntityMessageToMovementTree(targetEntity,message);
}
break;
case SET_BEHAVIOR_TREE:
case SETBEHAVIORTREE:
break;
}
}

View File

@ -125,6 +125,8 @@ public class RenderUtils {
static ShaderProgram lightDepthShaderProgram;
static Framebuffer lightDepthBuffer;
public static boolean renderHitboxes = true;
static int createScreenTextureVAO(){
int rVal = glGenVertexArrays();
glBindVertexArray(rVal);
@ -672,17 +674,30 @@ public class RenderUtils {
// currentModel.draw();
// }
}
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
Model hitboxModel;
if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere.fbx")) != null){
Vector3f position = EntityUtils.getEntityPosition(currentHitbox);
modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere
modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE));
hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw();
if(renderHitboxes){
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
Model hitboxModel;
if(currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE).equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere.fbx")) != null){
Vector3f position = EntityUtils.getEntityPosition(currentHitbox);
modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere
modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS) * 2);
hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw();
}
} else if(currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE).equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT)){
if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere_1.fbx")) != null){
Vector3f position = EntityUtils.getEntityPosition(currentHitbox);
modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere
modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS) * 2);
hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw();
}
}
}
}

View File

@ -5,36 +5,49 @@
"modelPath" : "Models/person1walkanim.fbx",
"hitboxes" : [
{
"type": "hurt",
"bone": "Bone.031",
"size": 0.08
},
{
"bone": "Bone.017",
"size": 0.08
"radius": 0.04
},
{
"type": "hurt",
"bone": "Bone.012",
"size": 0.08
"radius": 0.04
},
{
"type": "hurt",
"bone": "Bone.003",
"size": 0.08
"radius": 0.04
},
{
"type": "hurt",
"bone": "Bone.010",
"size": 0.11
"radius": 0.06
},
{
"type": "hurt",
"bone": "Bone.001",
"size": 0.11
},
{
"bone": "Bone",
"size": 0.15
"radius": 0.06
},
{
"type": "hurt",
"bone": "Bone.014",
"size": 0.12
"radius": 0.06
},
{
"type": "hurt",
"bone": "Bone",
"radius": 0.08
},
{
"type": "hurt",
"bone": "Bone.014",
"radius": 0.06
},
{
"type": "hurt",
"bone": "Bone.019",
"radius": 0.04
}
]
},
@ -48,16 +61,19 @@
"modelPath" : "Models/katana1alt.fbx",
"hitboxes" : [
{
"type": "hit",
"bone": "Blade1",
"size": 0.08
"radius": 0.04
},
{
"type": "hit",
"bone": "Blade2",
"size": 0.08
"radius": 0.04
},
{
"type": "hit",
"bone": "Blade3",
"size": 0.08
"radius": 0.04
}
]
}

Binary file not shown.

View File

@ -24,7 +24,13 @@
"Textures/transparent_blue.png"
]
},
"Models/katana1.fbx": {
"Models/unitsphere_1.fbx": {
"Sphere": [
"Textures/transparent_red.png",
"Textures/transparent_red.png"
]
},
"Models/katana1alt.fbx": {
"SwordMesh": [
"Textures/katana1.png",
"Textures/katana1.png"

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 B

281
template.json Normal file
View File

@ -0,0 +1,281 @@
{
"outputPath" : "./src/main/java/electrosphere/net/parser/",
"packageName" : "electrosphere.net.parser",
"categories":[
{
"categoryName" : "World",
"data" : [
{
"name" : "worldSize",
"type" : "FIXED_INT"
},
{
"name" : "locationX",
"type" : "FIXED_INT"
},
{
"name" : "locationY",
"type" : "FIXED_INT"
},
{
"name" : "macroValue00",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue01",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue02",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue10",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue11",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue12",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue20",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue21",
"type" : "FIXED_LONG"
},
{
"name" : "macroValue22",
"type" : "FIXED_LONG"
},
{
"name" : "randomizerValue00",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue01",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue02",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue10",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue11",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue12",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue20",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue21",
"type" : "FIXED_FLOAT"
},
{
"name" : "randomizerValue22",
"type" : "FIXED_FLOAT"
}
],
"messageTypes" : [
{
"messageName" : "Info",
"data" : [
"worldSize"
]
},
{
"messageName" : "Update",
"data" : [
"locationX",
"locationY"
]
},
{
"messageName" : "MacroValue",
"data" : [
"macroValue00",
"macroValue01",
"macroValue02",
"macroValue10",
"macroValue11",
"macroValue12",
"macroValue20",
"macroValue21",
"macroValue22"
]
},
{
"messageName" : "RandomizerValue",
"data" : [
"randomizerValue00",
"randomizerValue01",
"randomizerValue02",
"randomizerValue10",
"randomizerValue11",
"randomizerValue12",
"randomizerValue20",
"randomizerValue21",
"randomizerValue22"
]
}
]
},
{
"categoryName" : "Player",
"data" : [
{
"name" : "playerID",
"type" : "FIXED_INT"
}
],
"messageTypes" : [
{
"messageName" : "Set_ID",
"data" : [
"playerID"
]
}
]
},
{
"categoryName" : "Entity",
"data" : [
{
"name" : "creatureType",
"type" : "FIXED_INT"
},
{
"name" : "entityID",
"type" : "FIXED_INT"
},
{
"name" : "positionX",
"type" : "FIXED_FLOAT"
},
{
"name" : "positionY",
"type" : "FIXED_FLOAT"
},
{
"name" : "positionZ",
"type" : "FIXED_FLOAT"
},
{
"name" : "propertyType",
"type" : "FIXED_INT"
},
{
"name" : "propertyValue",
"type" : "FIXED_INT"
},
{
"name" : "treeType",
"type" : "FIXED_INT"
},
{
"name" : "treeStatus",
"type" : "FIXED_INT"
}
],
"messageTypes" : [
{
"messageName" : "Create",
"data" : [
"creatureType",
"entityID",
"positionX",
"positionY",
"positionZ"
]
},
{
"messageName" : "SetPosition",
"data" : [
"entityID",
"positionX",
"positionY",
"positionZ"
]
},
{
"messageName" : "Move",
"data" : [
"entityID",
"positionX",
"positionY",
"positionZ"
]
},
{
"messageName" : "Destroy",
"data" : [
"entityID"
]
},
{
"messageName" : "SetBehaviorTree",
"data" : [
"entityID",
"treeType",
"treeStatus"
]
},
{
"messageName" : "setProperty",
"data" : [
"entityID",
"propertyType",
"propertyValue"
]
}
]
}
]
}