From 8215985f7f8829b92e5667656d210aeb42fba351 Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Fri, 20 Nov 2015 13:52:19 -0800 Subject: [PATCH] Fix a lot of communication things, added lots of semicolons, started on entity knockback (hehe, doesn't work well, needs improvement) --- src/lib/plugins/blocks.js | 2 ++ src/lib/plugins/communication.js | 38 ++++++++++++---------- src/lib/plugins/digging.js | 2 +- src/lib/plugins/entities.js | 51 +++++++++++++++++++----------- src/lib/plugins/login.js | 2 +- src/lib/plugins/modpe.js | 2 +- src/lib/plugins/particle.js | 34 ++++++++++---------- src/lib/plugins/physics.js | 2 +- src/lib/plugins/pvp.js | 36 ++++++++++++++------- src/lib/plugins/updatePositions.js | 6 ++-- src/lib/plugins/world.js | 2 +- 11 files changed, 106 insertions(+), 71 deletions(-) diff --git a/src/lib/plugins/blocks.js b/src/lib/plugins/blocks.js index fc14ab2..909673c 100644 --- a/src/lib/plugins/blocks.js +++ b/src/lib/plugins/blocks.js @@ -1,3 +1,5 @@ +var Vec3 = require("vec3").Vec3; + module.exports.player=function(player,serv) { player.changeBlock=async (position,blockType,blockData) => diff --git a/src/lib/plugins/communication.js b/src/lib/plugins/communication.js index f4d0490..6018302 100644 --- a/src/lib/plugins/communication.js +++ b/src/lib/plugins/communication.js @@ -22,24 +22,30 @@ module.exports.server=function(serv) ); }; -module.exports.player=function(player,serv) +module.exports.entity=function(entity,serv) { - player._writeOthers= (packetName, packetFields) => - player - .getOthers() - .forEach((otherPlayer) => otherPlayer._client.write(packetName, packetFields)); + + entity.getNearby = () => serv + .getNearbyEntities({ + world: entity.world, + position: entity.position, + radius: entity.viewDistance*32 + }) + .filter((e) => e != entity); - player._writeOthersNearby = (packetName, packetFields) => - serv._writeArray(packetName, packetFields, player.nearbyPlayers()); + entity.getOtherPlayers = () => serv.players.filter((p) => p != entity); - player.getOthers = () => serv.players.filter((otherPlayer) => otherPlayer != player); + entity.getOthers = () => serv.entities.filter((e) => e != entity); - player.getNearbyPlayers = (radius=player.viewDistance*32) => serv.getNearby({ - world: player.world, - position: player.position, - radius: radius - }); + entity.getNearbyPlayers = (radius=entity.viewDistance*32) => entity.getNearby() + .filter((e) => e.type == 'player'); - player.nearbyPlayers = (radius=player.viewDistance*32) => player.nearbyEntities - .filter(e => e.type == 'player'); -}; \ No newline at end of file + entity.nearbyPlayers = (radius=entity.viewDistance*32) => entity.nearbyEntities + .filter(e => e.type == 'player') + + entity._writeOthers = (packetName, packetFields) => + serv._writeArray(packetName, packetFields, entity.getOtherPlayers()); + + entity._writeOthersNearby = (packetName, packetFields) => + serv._writeArray(packetName, packetFields, entity.getNearbyPlayers()); +} \ No newline at end of file diff --git a/src/lib/plugins/digging.js b/src/lib/plugins/digging.js index 52c9103..54a252c 100644 --- a/src/lib/plugins/digging.js +++ b/src/lib/plugins/digging.js @@ -1,4 +1,4 @@ -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; module.exports.player=function(player,serv) { diff --git a/src/lib/plugins/entities.js b/src/lib/plugins/entities.js index 4280a39..c5fb3fb 100644 --- a/src/lib/plugins/entities.js +++ b/src/lib/plugins/entities.js @@ -31,7 +31,7 @@ module.exports.server=function(serv,options) { object.yaw = yaw; object.gravity = new Vec3(0, -20*32, 0); object.terminalvelocity = new Vec3(27*32, 27*32, 27*32); - object.friction = (new Vec3(10*32, 0, 10*32)).floored(); + object.friction = new Vec3(15*32, 0, 15*32); object.size = new Vec3(0.25*32, 0.25*32, 0.25*32); // Hardcoded, will be dependent on type! object.deathTime = 60*1000; // 60 seconds object.pickupTime = 200; @@ -49,7 +49,7 @@ module.exports.server=function(serv,options) { mob.yaw = yaw; mob.gravity = new Vec3(0, -20*32, 0); mob.terminalvelocity = new Vec3(27*32, 27*32, 27*32); - mob.friction = new Vec3(10*32, 0, 10*32); + mob.friction = new Vec3(15*32, 0, 15*32); mob.size = new Vec3(0.75, 1.75, 0.75); mob.metadata = metadata; @@ -57,11 +57,8 @@ module.exports.server=function(serv,options) { }; serv.destroyEntity = entity => { - serv._writeNearby('entity_destroy', { + entity._writeOthersNearby('entity_destroy', { entityIds: [entity.id] - }, { - position: entity.position, - world: entity.world }); delete serv.entities[entity.id]; }; @@ -174,10 +171,10 @@ module.exports.entity=function(entity,serv){ }); entity.sendMetadata = (data) => { - serv._writeNearby('entity_metadata', { + entity._writeOthersNearby('entity_metadata', { entityId: entity.id, metadata: data - }, entity); + }); }; entity.setAndUpdateMetadata = (data) => { @@ -237,14 +234,6 @@ module.exports.entity=function(entity,serv){ } }; - entity.getNearby = () => serv - .getNearbyEntities({ - world: entity.world, - position: entity.position, - radius: entity.viewDistance*32 - }) - .filter((e) => e != entity); - entity.updateAndSpawn = () => { var updatedEntities=entity.getNearby(); var entitiesToAdd=updatedEntities.filter(e => entity.nearbyEntities.indexOf(e)==-1); @@ -269,11 +258,37 @@ module.exports.entity=function(entity,serv){ entity.collect = (collectEntity) => { if (entity.type != 'player') serv.emit('error', 'Non-player entity (ttype ' + entity.type + ') cannot collect another entity'); else { - serv._writeNearby('collect', { + collectEntity._writeOthersNearby('collect', { collectedEntityId: collectEntity.id, collectorEntityId: entity.id - }, collectEntity); + }); entity.playSoundAtSelf('random.pop'); } } + + entity.sendVelocity = (vel, maxVel) => { + var velocity = vel.scaled(32).floored(); // Make fixed point + var maxVelocity = maxVel.scaled(32).floored(); + var scaledVelocity = velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick + entity._writeOthersNearby('entity_velocity', { + entityId: entity.id, + velocityX: scaledVelocity.x, + velocityY: scaledVelocity.y, + velocityZ: scaledVelocity.z + }); + if (entity.type != 'player') { + if (maxVelocity) entity.velocity = addVelocityWithMax(entity.velocity, velocity, maxVelocity); + else entity.velocity.add(velocity); + } + } + function addVelocityWithMax(current, newVel, max) { + var x, y, z; + if (current.x > max.x || current.x < -max.x) x = current.x; + else x = Math.max(-max.x, Math.min(max.x, current.x + newVel.x)); + if (current.y > max.y || current.y < -max.y) y = current.y; + else y = Math.max(-max.y, Math.min(max.y, current.y + newVel.y)); + if (current.z > max.z || current.z < -max.z) z = current.z; + else z = Math.max(-max.z, Math.min(max.z, current.z + newVel.z)); + return new Vec3(x, y, z); + } }; \ No newline at end of file diff --git a/src/lib/plugins/login.js b/src/lib/plugins/login.js index f4c704d..5807181 100644 --- a/src/lib/plugins/login.js +++ b/src/lib/plugins/login.js @@ -1,5 +1,5 @@ var Entity=require("prismarine-entity"); -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; var path = require('path'); var requireIndex = require('requireindex'); diff --git a/src/lib/plugins/modpe.js b/src/lib/plugins/modpe.js index d5b3c09..ad70f83 100644 --- a/src/lib/plugins/modpe.js +++ b/src/lib/plugins/modpe.js @@ -1,4 +1,4 @@ -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; var dir = require("node-dir"); var fs = require("fs"); diff --git a/src/lib/plugins/particle.js b/src/lib/plugins/particle.js index 5710246..77ae7ed 100644 --- a/src/lib/plugins/particle.js +++ b/src/lib/plugins/particle.js @@ -1,29 +1,27 @@ -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; module.exports.server=function(serv) { - serv.emitParticle = (particle, world, position, {whitelist,blacklist=[],radius=32*32,longDistance,size,count}={}) => { + serv.emitParticle = (particle, world, position, {whitelist,blacklist=[],radius=32*32,longDistance=true,size,count=1}={}) => { var players = (typeof whitelist != 'undefined' ? (typeof whitelist == 'array' ? whitelist : [whitelist]) : serv.getNearby({ world: world, position: position.scaled(32).floored(), radius: radius // 32 blocks, fixed position })); if (!size) size = new Vec3(1.0, 1.0, 1.0); - players.filter(player => blacklist.indexOf(player) == -1) - .forEach(player => { - player._client.write('world_particles', { - particleId: particle, - longDistance: longDistance || true, - x: position.x, - y: position.y, - z: position.z, - offsetX: size.x, - offsetY: size.y, - offsetZ: size.z, - particleData: 1.0, - particles: count || 1, - data: [] - }); - }); + + serv._writeNearby('world_particles', { + particleId: particle, + longDistance: longDistance, + x: position.x, + y: position.y, + z: position.z, + offsetX: size.x, + offsetY: size.y, + offsetZ: size.z, + particleData: 1.0, + particles: count, + data: [] + }, players.filter(p => blacklist.indexOf(p) == -1)); } }; diff --git a/src/lib/plugins/physics.js b/src/lib/plugins/physics.js index a7662ff..792f7b1 100644 --- a/src/lib/plugins/physics.js +++ b/src/lib/plugins/physics.js @@ -1,5 +1,5 @@ var blocks=require("minecraft-data")(require("../version")).blocks; -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; module.exports.entity=function(entity){ entity.calculatePhysics = async (delta) => { diff --git a/src/lib/plugins/pvp.js b/src/lib/plugins/pvp.js index 60e9309..18262e8 100644 --- a/src/lib/plugins/pvp.js +++ b/src/lib/plugins/pvp.js @@ -1,3 +1,5 @@ +var Vec3 = require("vec3").Vec3; + module.exports.player=function(player,serv) { @@ -12,20 +14,23 @@ module.exports.player=function(player,serv) function attackEntity(entityId) { - if (!serv.entities[entityId]) return; // ????? - var attackedPlayer = serv.entities[entityId].player; - if(!attackedPlayer || attackedPlayer.gameMode!=0) return; - attackedPlayer.updateHealth(attackedplayer.health - 1); - serv.playSound('game.player.hurt', player.world, attackedplayer.position.scaled(1/32)); + var attackedEntity = serv.entities[entityId]; + if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return; - if(attackedplayer.health==0) - attackedPlayer._writeOthers('entity_status',{ - entityId:attackedplayer.id, + attackedEntity.updateHealth(attackedEntity.health - 1); + serv.playSound('game.player.hurt', player.world, attackedEntity.position.scaled(1/32)); + + var attackVelocity = attackedEntity.position.minus(player.position).plus(new Vec3(0, 0.5, 0)).scaled(5/32); + attackedEntity.sendVelocity(attackVelocity, new Vec3(4, 4, 4)); + + if(attackedEntity.health<=0) + attackedEntity._writeOthers('entity_status',{ + entityId:attackedEntity.id, entityStatus:3 }); else - attackedPlayer._writeOthers('animation',{ - entityId:attackedplayer.id, + attackedEntity._writeOthers('animation',{ + entityId:attackedEntity.id, animation:1 }); } @@ -35,4 +40,13 @@ module.exports.player=function(player,serv) attackEntity(target); }); -}; \ No newline at end of file +}; + +module.exports.entity=function(entity,serv) +{ + if (entity.type != 'player') { + entity.updateHealth = (health) => { + entity.health = health; + } + } +} \ No newline at end of file diff --git a/src/lib/plugins/updatePositions.js b/src/lib/plugins/updatePositions.js index d440e4b..749f41d 100644 --- a/src/lib/plugins/updatePositions.js +++ b/src/lib/plugins/updatePositions.js @@ -1,4 +1,4 @@ -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; Vec3.prototype.toFixedPosition=function() { return this.scaled(32).floored(); @@ -90,7 +90,7 @@ module.exports.entity=function(entity,serv){ var diff = entity.position.minus(oldPos); if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127) - serv._writeNearby('entity_teleport', { + entity._writeOthersNearby('entity_teleport', { entityId: entity.id, x: entity.position.x, y: entity.position.y, @@ -98,7 +98,7 @@ module.exports.entity=function(entity,serv){ yaw: entity.yaw, pitch: entity.pitch, onGround: onGround - }, entity); + }); else if (diff.distanceTo(new Vec3(0, 0, 0)) != 0) serv._writeNearby('rel_entity_move', { entityId: entity.id, dX: diff.x, diff --git a/src/lib/plugins/world.js b/src/lib/plugins/world.js index 041ec82..9fab63d 100644 --- a/src/lib/plugins/world.js +++ b/src/lib/plugins/world.js @@ -1,4 +1,4 @@ -var Vec3 = require("vec3").Vec3 +var Vec3 = require("vec3").Vec3; var spiralloop = require('spiralloop'); var Chunk = require('prismarine-chunk')(require("../version"));