Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #9 from vram4/master
Browse files Browse the repository at this point in the history
Fixes an issue with clients stuttering that i caused in a previous commit
  • Loading branch information
subv3rsion committed May 26, 2015
2 parents 4c6f168 + 45fa818 commit 7de66dd
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 65 deletions.
13 changes: 13 additions & 0 deletions Cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function Cell(nodeId, owner, position, mass, type) {
this.mass = mass; // Starting mass of the cell
this.speed = 30; // Filler, will be changed later
this.cellType = type; // 0 = Player Cell, 1 = Food, 2 = Virus, 3 = Ejected Mass

this.killedBy; // Cell that ate this cell
this.recombineTicks = 0; // Ticks until the cell can recombine with other cells
this.ignoreCollision = false;

Expand Down Expand Up @@ -55,6 +57,10 @@ Cell.prototype.getSize = function() {
return Math.sqrt(100 * this.mass) + .1;
}

Cell.prototype.addMass = function(n) {
this.mass = Math.min(this.mass + n,this.owner.gameServer.config.playerMaxMass);
}

Cell.prototype.getSpeed = function() {
// Custom speed formula
var speed = 5 + (35 * (1 - (this.mass/(200+this.mass))));
Expand Down Expand Up @@ -103,6 +109,13 @@ Cell.prototype.getEatingRange = function() {
}
}

Cell.prototype.getKiller = function() {
return this.killedBy;
}

Cell.prototype.setKiller = function(cell) {
this.killedBy = cell;
}

// Functions

Expand Down
104 changes: 58 additions & 46 deletions GameServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function GameServer(port,gameType) {
top: 0,
bottom: 6000.0
}; // Right: X increases, Down: Y increases (as of 2015-05-20)
this.lastNodeId = 0;
this.lastNodeId = 1;
this.clients = [];
this.port = port;
this.nodes = [];
Expand All @@ -41,19 +41,22 @@ function GameServer(port,gameType) {
this.gameTypeStrings = ["Free For All","Teams"];

this.config = {
maxConnections: 64, // Maximum amount of connections to the server.
foodSpawnRate: 1000, // The interval between each food cell spawn in milliseconds (Placeholder number)
serverMaxConnections: 64, // Maximum amount of connections to the server.
serverAllowMods: true, // Whether or not to allow clients with mods to connect
foodSpawnRate: 1000, // The interval between each food cell spawn in milliseconds
foodSpawnAmount: 5, // The amount of food to spawn per interval
foodMaxAmount: 500, // Maximum food cells on the map (Placeholder number)
foodMaxAmount: 500, // Maximum food cells on the map
foodMass: 1, // Starting food size (In mass)
virusSpawnRate: 10000, // The interval between each virus spawn in milliseconds (viruses will spawn until virusMinAmount is reached)
virusMinAmount: 10, // Minimum amount of viruses on the map.
virusMaxAmount: 50, // Maximum amount of viruses on the map. If this amount is reached, then ejected cells will pass through viruses.
virusStartMass: 100.0, // Starting virus size (In mass)
virusBurstMass: 198.0, // Viruses explode past this size
ejectMass: 16, // Mass of ejected cells
ejectMassGain: 14, //Amount of mass gained from consuming ejected cells
ejectSpeed: 200, // Base speed of ejected cells
playerStartMass: 10, // Starting mass of the player cell
playerStartMass: 10, // Starting mass of the player cell. Large values may cause problens when clients connect.
playerMaxMass: 22500, // Maximum mass a player can have
playerMinMassEject: 32, // Mass required to eject a cell
playerMinMassSplit: 36, // Mass required to split
playerMaxCells: 16, // Max cells the player is allowed to have
Expand All @@ -67,7 +70,7 @@ function GameServer(port,gameType) {
teamMassDecay: 1.5 // Multiplier for mass decay in team mode
};

this.colors = [{'r':235,'b':0,'g':75},{'r':225,'b':255,'g':125},{'r':180,'b':20,'g':7},{'r':80,'b':240,'g':170},{'r':180,'b':135,'g':90},{'r':195,'b':0,'g':240},{'r':150,'b':255,'g':18},{'r':80,'b':0,'g':245},{'r':165,'b':0,'g':25},{'r':80,'b':0,'g':145},{'r':80,'b':240,'g':170},{'r':55,'b':255,'g':92}];
this.colors = [{'r':235,'b':0,'g':75},{'r':225,'b':255,'g':125},{'r':180,'b':20,'g':7},{'r':80,'b':240,'g':170},{'r':180,'b':135,'g':90},{'r':195,'b':0,'g':240},{'r':150,'b':255,'g':18},{'r':80,'b':0,'g':245},{'r':165,'b':0,'g':25},{'r':80,'b':0,'g':145},{'r':80,'b':240,'g':170},{'r':55,'b':255,'g':92}];
this.colorsTeam = [{'r':245,'b':0,'g':0},{'r':0,'b':0,'g':245},{'r':0,'b':245,'g':0}]; // Make sure you add extra colors here if you wish to increase the team amount [Default colors are: Red, Green, Blue]

if (this.gameType == 1) {
Expand All @@ -87,8 +90,8 @@ GameServer.prototype.start = function() {

// Spawning
setInterval(this.spawnFood.bind(this), this.config.foodSpawnRate);
this.virusCheck();
//setInterval(this.spawnVirus.bind(this), this.config.virusSpawnRate); // Old code
//this.virusCheck();
setInterval(this.spawnVirus.bind(this), this.config.virusSpawnRate); // Old code

// Move engine
setInterval(this.updateMoveEngine.bind(this), 100);
Expand All @@ -105,6 +108,12 @@ GameServer.prototype.start = function() {
this.socketServer.on('connection', connectionEstablished.bind(this));

function connectionEstablished(ws) {
if (this.clients.length > this.config.serverMaxConnections) {
ws.close();
console.log("[Game] Client tried to connect, but server player limit has been reached!");
return;
}

function close(error) {
console.log("[Game] Disconnect: %s:%d", this.socket.remoteAddress, this.socket.remotePort);
var index = this.server.clients.indexOf(this.socket);
Expand All @@ -113,16 +122,16 @@ GameServer.prototype.start = function() {
}

if (this.socket.playerTracker.cells.length > 0) {
var len = this.socket.playerTracker.cells.length;
for (var i = 0; i < len; i++) {
var cell = this.socket.playerTracker.cells[i];
var len = this.socket.playerTracker.cells.length;
for (var i = 0; i < len; i++) {
var cell = this.socket.playerTracker.cells[i];

if (!cell) {
continue;
}
if (!cell) {
continue;
}

this.server.removeNode(cell);
}
this.server.removeNode(cell);
}
}
}

Expand All @@ -137,12 +146,6 @@ GameServer.prototype.start = function() {
ws.on('error', close.bind(bindObject));
ws.on('close', close.bind(bindObject));
this.clients.push(ws);

if (this.clients.length > this.config.maxConnections) {
ws.close();
console.log("[Game] Client tried to connect, but server player limit has been reached!");
return;
}
}
}

Expand All @@ -153,7 +156,7 @@ GameServer.prototype.getGameType = function() {
GameServer.prototype.getNextNodeId = function() {
// Resets integer
if (this.lastNodeId > 2147483647) {
this.lastNodeId = 0;
this.lastNodeId = 1;
}
return this.lastNodeId++;
}
Expand All @@ -166,18 +169,18 @@ GameServer.prototype.getRandomPosition = function() {
}

GameServer.prototype.getRandomColor = function() {
var index = Math.floor(Math.random() * this.colors.length);
var color = this.colors[index];
return {
var index = Math.floor(Math.random() * this.colors.length);
var color = this.colors[index];
return {
r: color.r,
b: color.b,
g: color.g
};
}

GameServer.prototype.getTeamColor = function(team) {
var color = this.colorsTeam[team];
return {
var color = this.colorsTeam[team];
return {
r: color.r,
b: color.b,
g: color.g
Expand All @@ -188,14 +191,14 @@ GameServer.prototype.addNode = function(node) {
this.nodes.push(node);

switch (node.getType()) {
case 0: // Add to special player controlled node list
case 0: // Add to special player controlled node list
this.nodesPlayer.push(node);
// Teams
if (this.gameType == 1) {
this.nodesTeam[node.owner.getTeam()].push(node);
}
break;
case 2: // Add to special virus node list
case 2: // Add to special virus node list
this.nodesVirus.push(node);
break;
default:
Expand All @@ -209,7 +212,7 @@ GameServer.prototype.addNode = function(node) {
}

GameServer.prototype.removeNode = function(node) {
// Remove from main nodes list
// Remove from main nodes list
var index = this.nodes.indexOf(node);
if (index != -1) {
this.nodes.splice(index, 1);
Expand All @@ -221,26 +224,31 @@ GameServer.prototype.removeNode = function(node) {
this.movingNodes.splice(index, 1);
}

switch (node.getType()) {
switch (node.getType()) {
case 0: // Remove from owning player's cell list
var owner = node.owner;
// Remove from player screen
owner.cells.splice(owner.cells.indexOf(node), 1);
owner.visibleNodes.splice(owner.visibleNodes.indexOf(node), 1);
owner.nodeDestroyQueue.push(node);
//owner.nodeDestroyQueue.push(node);
// Remove from special player controlled node list
this.nodesPlayer.splice(this.nodesPlayer.indexOf(node), 1);
// Teams
if (this.gameType == 1) {
this.nodesTeam[owner.getTeam()].splice(this.nodesTeam.indexOf(node), 1);
}
break;
case 2: // Remove from special virus node list
case 2: // Remove from special virus node list
this.nodesVirus.splice(this.nodesVirus.indexOf(node), 1);
default:
default:
// End the function
break;
}

// Animation when eating
if (node.getKiller()) {
node.getKiller().owner.nodeDestroyQueue.push(node);
}
}

GameServer.prototype.updateAll = function() {
Expand All @@ -262,33 +270,34 @@ GameServer.prototype.spawnFood = function() {
this.addNode(f);
this.currentFood++;
}
}
}
}

GameServer.prototype.spawnVirus = function() { // Depreciated
if (this.nodesVirus.length < this.config.virusMaxAmount) {
GameServer.prototype.spawnVirus = function() {
if (this.nodesVirus.length < this.config.virusMinAmount) {
var f = new Cell(this.getNextNodeId(), null, this.getRandomPosition(), this.config.virusStartMass, 2);
this.addNode(f);
}
}

GameServer.prototype.virusCheck = function() {
GameServer.prototype.virusCheck = function() { // Buggy code?
// Checks if there are enough viruses on the map
while (this.nodesVirus.length < this.config.virusMinAmount) {
if (this.nodesVirus.length > this.config.virusMaxAmount) {
console.log("[Game] Warning! Server is trying to spawn more viruses than the limit allows!");
//console.log("[Debug] Current Node Id: " + this.lastNodeId);
break;
}
// Spawns a virus
var f = new Cell(this.getNextNodeId(), null, this.getRandomPosition(), this.config.virusStartMass, 2);
this.addNode(f);
//console.log("[Debug] Current Viruses: " + this.nodesVirus.length);
}
}

GameServer.prototype.updateMoveEngine = function() {
// Move player cells
// Move player cells
for (var i = 0; i < this.nodesPlayer.length; i++) {
var cell = this.nodesPlayer[i];

Expand All @@ -308,14 +317,14 @@ GameServer.prototype.updateMoveEngine = function() {

switch (n) {
case 3: // Ejected Mass
cell.mass += this.config.ejectMassGain;
cell.addMass(this.config.ejectMassGain);
break;
case 0: // Player Cell
cell.mass += list[j].mass;
cell.addMass(list[j].mass);
break;
case 1: // Food
this.currentFood--;
cell.mass += this.config.foodMass;
cell.addMass(this.config.foodMass);
break;
case 2: // Virus - viruses do not give mass when eaten
this.virusCheck();
Expand Down Expand Up @@ -355,6 +364,9 @@ GameServer.prototype.updateMoveEngine = function() {
default:
break;
}

// Remove cell
list[j].setKiller(cell);
this.removeNode(list[j]);
}
}
Expand Down Expand Up @@ -429,8 +441,8 @@ GameServer.prototype.newCellVirused = function(client, parent, angle, mass, spee

GameServer.prototype.shootVirus = function(parent) {
var parentPos = {
x: parent.position.x,
y: parent.position.y,
x: parent.position.x,
y: parent.position.y,
};

var newVirus = new Cell(this.getNextNodeId(), null, parentPos, this.config.virusStartMass, 2);
Expand Down
10 changes: 10 additions & 0 deletions PacketHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Packet = require('./packet');
function PacketHandler(gameServer, socket) {
this.gameServer = gameServer;
this.socket = socket;
this.properInit = false;
}

module.exports = PacketHandler;
Expand All @@ -24,6 +25,12 @@ PacketHandler.prototype.handleMessage = function(message) {
var buffer = stobuf(message);
var view = new DataView(buffer);
var packetId = view.getUint8(0, true);

// Client with mods tried to connect
if (packetId != 254 && !this.properInit && !this.gameServer.config.serverAllowMods) {
console.log("[Game] Client at %s tried to connect with mods enabled.", this.socket.remoteAddress);
this.socket.close();
}

switch (packetId) {
case 0:
Expand Down Expand Up @@ -125,6 +132,9 @@ PacketHandler.prototype.handleMessage = function(message) {
this.gameServer.setAsMovingNode(ejected);
}
break;
case 254:
this.properInit = true;
break;
case 255:
// Connection
// Send SetBorder packet first
Expand Down
6 changes: 2 additions & 4 deletions PlayerTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ PlayerTracker.prototype.update = function() {
// Viewing box

PlayerTracker.prototype.updateSightRange = function() { // For view distance
var range = 900;
var range = 1250;
var len = this.cells.length;

for (var i = 0; i < len;i++) {
Expand All @@ -144,7 +144,7 @@ PlayerTracker.prototype.updateSightRange = function() { // For view distance
continue;
}

range += (this.cells[i].mass + 100);
range += ((this.cells[i].getSize() * 2) + 50);
}
this.sightRange = range;
}
Expand Down Expand Up @@ -194,8 +194,6 @@ PlayerTracker.prototype.calcViewBox = function() {
if (node.collisionCheck(this.viewBox.bottomY,this.viewBox.topY,this.viewBox.rightX,this.viewBox.leftX)) {
// Cell is in range of viewBox
newVisible.push(node);

//
}
}
return newVisible;
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ The project is nearly complete. Here's a rough list of what's been done and what

## Known Issues
- [General] When disconnecting, some cells from splitting are left behind
- [General] Formula for calculating speed is wrong
- [General] Formula for calculating view distance is wrong
- [General] Several formulas for calculating variables (ex. speed, mass decay) are incorrect
- [General] Viruses sometimes mass spawn and fill up the map
- [Team] Cells just block their team mates when colliding, instead of pushing them a little

## Obtaining and Using
Expand All @@ -41,7 +41,7 @@ Currently, Ogar listens on the following addresses and ports:
Please note that on some systems, you may have to run the process as root or otherwise elevate your privileges to allow the process to listen on the needed ports.

## Configuring Ogar
Ogar has a configurations field that can be modified. To do this, open up GameServer.js with a decent text editor (except for notepad), then go to line 45 to see the config values.
Ogar has a configurations field that can be modified. To do this, open up GameServer.js with a decent text editor (except for notepad), then go to line 43 to see the config values.

## Contributing
Please see [CONTRIBUTING.md](https://github.com/forairan/Ogar/blob/master/CONTRIBUTING.md) for contribution guidelines.
Expand Down
Loading

0 comments on commit 7de66dd

Please sign in to comment.