-
Notifications
You must be signed in to change notification settings - Fork 44
/
GameServer.js
1089 lines (941 loc) · 37.3 KB
/
GameServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Library imports
// jxcore.tasks.setThreadCount(4);
var WebSocket = require('ws');
var querystring = require("querystring");
var http = require('http');
var finalhandler = require('finalhandler');
var fs = require("fs");
var myos = require("os");
var ini = require('./modules/ini.js');
// Project imports
var Packet = require('./packet');
var PlayerTracker = require('./PlayerTracker');
var PacketHandler = require('./PacketHandler');
var Entity = require('./entity');
var Gamemode = require('./gamemodes');
var BotLoader = require('./ai/BotLoader');
var Logger = require('./modules/log');
var serveStatic = require('serve-static');
var serve = serveStatic('./client/');
// GameServer implementation
function GameServer() {
// Startup
this.run = true;
this.lastNodeId = 1;
this.lastPlayerId = 1;
this.clients = [];
this.nodes = [];
this.nodesVirus = []; // Virus nodes
this.nodesEjected = []; // Ejected mass nodes
this.nodesPlayer = []; // Nodes controlled by players
this.currentFood = 0;
this.movingNodes = []; // For move engine
this.leaderboard = [];
this.lb_packet = new ArrayBuffer(0); // Leaderboard packet
this.bots = new BotLoader(this);
this.log = new Logger();
this.commands; // Command handler
this.banned = []; // List of banned IPs
// Main loop tick
this.time = new Date();
this.startTime = this.time;
this.tick = 0; // 1 second ticks of mainLoop
this.tickMain = 0; // 50 ms ticks, 20 of these = 1 leaderboard update
this.tickSpawn = 0; // Used with spawning food
this.master = 0; // Used for Master Ping spam protection
// Config
this.sqlconfig = {
host: '',
user: '',
password: '',
database: '',
table: ''
};
this.config = { // Border - Right: X increases, Down: Y increases (as of 2015-05-20)
serverMaxConnections: 64, // Maximum amount of connections to the server.
serverMaxConnPerIp: 9,
serverPort: 8080, // Server port
serverGamemode: 0, // Gamemode, 0 = FFA, 1 = Teams
serverResetTime: 0, // Time in hours to reset (0 is off)
serverName: 'Ogar3 Server', // The name to display on the tracker (leave empty will show ip:port)
serverAdminPass: '', // Remote console commands password
serverBots: 3, // Amount of player bots to spawn
serverVersion: 1,
serverOldColors: 0, // If the server uses colors from the original Ogar
serverViewBaseX: 1024, // Base view distance of players. Warning: high values may cause lag
serverViewBaseY: 592,
serverStatsPort: 88, // Port for stats server. Having a negative number will disable the stats server.
serverStatsUpdate: 60, // Amount of seconds per update for the server stats
serverLogLevel: 2, // Logging level of the server. 0 = No logs, 1 = Logs the console, 2 = Logs console and ip connections
gameLBlength: 10, // Number of names to display on Leaderboard (Vanilla value: 10)
borderLeft: 0, // Left border of map (Vanilla value: 0)
borderRight: 6000, // Right border of map (Vanilla value: 11180.3398875)
borderTop: 0, // Top border of map (Vanilla value: 0)
borderBottom: 6000, // Bottom border of map (Vanilla value: 11180.3398875)
spawnInterval: 20, // The interval between each food cell spawn in ticks (1 tick = 50 ms)
foodSpawnAmount: 10, // The amount of food to spawn per interval
foodStartAmount: 100, // The starting amount of food in the map
foodMaxAmount: 500, // Maximum food cells on the map
foodMass: 1, // Starting food size (In mass)
foodMaxMass: 4,
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, // Starting virus size (In mass)
virusFeedAmount: 7, // Amount of times you need to feed a virus to shoot it
motherCellMinMass: 200,
motherCellMaxMass: 2000,
ejectMass: 12, // Mass of ejected cells
ejectMassLoss: 16, // Mass lost when ejecting cells
ejectSpeed: 160, // Base speed of ejected cells
ejectSpawnPlayer: 50, // Chance for a player to spawn from ejected mass
playerStartMass: 10, // Starting mass of the player cell.
playerMaxMass: 22500, // Maximum mass a player can have
playerSpeed: 30, // Player base speed
playerSplitSpeedMultiplier: 6, // multiplier for splitting speed
playerPopsplitSpeed: 1,
playerMinMassEject: 32, // Mass required to eject a cell
playerMinMassSplit: 36, // Mass required to split
playerSmoothSplit: 1, // Does player split smoothly?
playerMaxCells: 16, // Max cells the player is allowed to have
playerRecombineTime: 30, // Base amount of seconds before a cell is allowed to recombine
playerMassDecayRate: .002, // Amount of mass lost per second
playerMinMassDecay: 9, // Minimum mass for decay to occur
playerMaxNickLength: 15, // Maximum nick length
playerDisconnectTime: 60, // The amount of seconds it takes for a player cell to be removed after disconnection (If set to -1, cells are never removed)
teamsCollision: 1, //If teammates can collide with each other.
tourneyMaxPlayers: 12, // Maximum amount of participants for tournament style game modes
tourneyPrepTime: 10, // Amount of ticks to wait after all players are ready (1 tick = 1000 ms)
tourneyEndTime: 30, // Amount of ticks to wait after a player wins (1 tick = 1000 ms)
tourneyTimeLimit: 20, // Time limit of the game, in minutes.
tourneyAutoFill: 0, // If set to a value higher than 0, the tournament match will automatically fill up with bots after this amount of seconds
tourneyAutoFillPlayers: 1, // The timer for filling the server with bots will not count down unless there is this amount of real players
chatMaxMessageLength: 70 // Maximum message length
};
// Parse config
this.loadConfig();
this.oldcolors = [{'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}];
// Gamemodes
this.gameMode = Gamemode.get(this.config.serverGamemode);
}
module.exports = GameServer;
GameServer.prototype.start = function() {
// Logging
this.log.setup(this);
// Rcon Info
if ( this.config.serverAdminPass != '' )
{
console.log("* \u001B[33mRcon enabled, passkey set to " + this.config.serverAdminPass + "\u001B[0m");
console.log("* \u001B[33mTo use in chat type /rcon " + this.config.serverAdminPass + " <server command>\u001B[0m");
}
// My SQL erver
if ( this.sqlconfig.host != '' )
{
console.log("* \u001B[33mMySQL config loaded Database set to " + this.sqlconfig.database + "." + this.sqlconfig.table + "\u001B[0m");
MySQL = require("./modules/mysql");
this.mysql = new MySQL();
this.mysql.init(this.sqlconfig);
this.mysql.connect();
this.mysql.createTable(this.sqlconfig.table,this.sqlconfig.database);
}
// Gamemode configurations
this.gameMode.onServerInit(this);
this.config.serverPort = process.env.PORT || this.config.serverPort;
// here
// Start the server
var hserver = http.createServer( function(req, res){
res.setHeader('Access-Control-Allow-Origin', '*');
var done = finalhandler(req, res)
// res.end({title:12})
serve(req, res, done)
});
hserver.listen(this.config.serverPort,"0.0.0.0",this.onHttpServerOpen.bind(this));
this.socketServer = new WebSocket.Server({server: hserver, perMessageDeflate: false});
this.socketServer.on('connection', connectionEstablished.bind(this));
// Properly handle errors because some people are too lazy to read the readme
this.socketServer.on('error', function err(e) {
switch (e.code) {
case "EADDRINUSE":
console.log("[Error] Server could not bind to port! Please close out of Skype or change 'serverPort' in gameserver.ini to a different number.");
break;
case "EACCES":
console.log("[Error] Please make sure you are running Ogar with root privileges.");
break;
default:
console.log("[Error] Unhandled error code: "+e.code);
break;
}
process.exit(1); // Exits the program
});
function connectionEstablished(ws) {
if (this.clients.length >= this.config.serverMaxConnections) { // Server full
console.log("\u001B[33mClient tried to connect, but server player limit has been reached!\u001B[0m");
ws.close();
return;
} else if (this.banned.indexOf(ws._socket.remoteAddress) != -1) { // Banned
console.log("\u001B[33mClient " + ws._socket.remoteAddress + ", tried to connect but is banned!\u001B[0m");
ws.close();
return;
}
var origin = ws.upgradeReq.headers.origin;
if (this.config.serverMaxConnPerIp) {
for (var cons = 1, i = 0, llen = this.clients.length; i < llen; i++) {
if (this.clients[i].remoteAddress == ws._socket.remoteAddress) {
cons++;
}
}
if (cons > this.config.serverMaxConnPerIp ) {
ws.close();
return;
}
}
function close(error) {
this.server.log.onDisconnect(this.socket.remoteAddress);
var client = this.socket.playerTracker;
console.log( "\u001B[31mClient Disconnect: " + this.socket.remoteAddress + ":" + this.socket.remotePort +" Error " + error + "\u001B[0m");
var len = this.socket.playerTracker.cells.length;
for (var i = 0; i < len; i++) {
var cell = this.socket.playerTracker.cells[i];
if (!cell) {
continue;
}
cell.calcMove = function() { return; }; // Clear function so that the cell cant move
// this.server.removeNode(cell);
}
client.disconnect = this.server.config.playerDisconnectTime * 20;
this.socket.sendPacket = function() {return;}; // Clear function so no packets are sent
}
ws.remoteAddress = ws._socket.remoteAddress;
ws.remotePort = ws._socket.remotePort;
this.log.onConnect(ws.remoteAddress); // Log connections
console.log( "(" + this.clients.length + "/" + this.config.serverMaxConnections + ") \u001B[32mClient connect: "+ws.remoteAddress+":"+ws.remotePort+" [origin "+origin+"]\u001B[0m");
ws.playerTracker = new PlayerTracker(this, ws);
ws.packetHandler = new PacketHandler(this, ws);
ws.on('message', ws.packetHandler.handleMessage.bind(ws.packetHandler));
var bindObject = { server: this, socket: ws };
ws.on('error', close.bind(bindObject));
ws.on('close', close.bind(bindObject));
this.clients.push(ws);
this.MasterPing();
}
this.startStatsServer(this.config.serverStatsPort);
};
GameServer.prototype.onHttpServerOpen = function() {
// Spawn starting food
this.startingFood();
// Start Main Loop
this.MasterPing();
setInterval(this.MasterPing.bind(this), 1805000);
setInterval(this.mainLoop.bind(this), 1);
// Done
console.log("* \u001B[33mListening on port " + this.config.serverPort + " \u001B[0m");
console.log("* \u001B[33mCurrent game mode is " + this.gameMode.name + "\u001B[0m");
// Player bots (Experimental)
/*for (var i = 0;i < 20;i++) {
this.bots.addNPC();
}*/
if (this.config.serverBots > 0) {
for (var i = 0;i < this.config.serverBots;i++) {
this.bots.addBot();
}
console.log("* \u001B[33mLoaded "+this.config.serverBots+" player bots\u001B[0m");
}
if (this.config.serverResetTime > 0 ) {
console.log("* \u001B[33mAuto shutdown after "+this.config.serverResetTime+" hours\u001B[0m");
}
if ( this.config.serverVersion == 1 )
console.log("* \u001B[33mProtocol set to new, clients with version 561.20 and up can connect to this server\u001B[0m");
if ( this.config.serverVersion == 0 )
console.log("* \u001B[33mProtocol set to old, clients with version 561.19 and older can connect to this server\u001B[0m");
};
GameServer.prototype.getMode = function() {
return this.gameMode;
};
GameServer.prototype.getNextNodeId = function() {
// Resets integer
if (this.lastNodeId > 2147483647) {
this.lastNodeId = 1;
}
return this.lastNodeId++;
};
GameServer.prototype.getNewPlayerID = function() {
// Resets integer
if (this.lastPlayerId > 2147483647) {
this.lastPlayerId = 1;
}
return this.lastPlayerId++;
};
GameServer.prototype.getRandomPosition = function() {
return {
x: Math.floor(Math.random() * (this.config.borderRight - this.config.borderLeft)) + this.config.borderLeft,
y: Math.floor(Math.random() * (this.config.borderBottom - this.config.borderTop)) + this.config.borderTop
};
};
GameServer.prototype.getRandomSpawn = function() {
// Random spawns for players
var pos;
if (this.currentFood > 0) {
// Spawn from food
var node;
for (var i = (this.nodes.length - 1); i > -1; i--) {
// Find random food
node = this.nodes[i];
if (!node || node.inRange) {
// Skip if food is about to be eaten/undefined
continue;
}
if (node.getType() == 1) {
pos = {x: node.position.x,y: node.position.y};
this.removeNode(node);
break;
}
}
}
if (!pos) {
// Get random spawn if no food cell is found
pos = this.getRandomPosition();
}
return pos;
};
GameServer.prototype.getRandomColor = function() {
if(this.config.serverOldColors) {
var index = Math.floor(Math.random() * this.oldcolors.length);
var color = this.oldcolors[index];
return {
r: color.r,
b: color.b,
g: color.g
};
} else {
var colorRGB = [0xFF, 0x07, (Math.random() * 256) >> 0];
colorRGB.sort(function () { return 0.5 - Math.random() });
return {
r: colorRGB[0],
b: colorRGB[1],
g: colorRGB[2]
};
};
};
GameServer.prototype.addNode = function(node) {
this.nodes.push(node);
// Adds to the owning player's screen
if (node.owner) {
node.setColor(node.owner.color);
node.owner.cells.push(node);
node.owner.socket.sendPacket(new Packet.AddNode(node));
}
// Special on-add actions
node.onAdd(this);
// Add to visible nodes
for (var i = 0; i < this.clients.length;i++) {
client = this.clients[i].playerTracker;
if (!client) {
continue;
}
// client.nodeAdditionQueue is only used by human players, not bots
// for bots it just gets collected forever, using ever-increasing amounts of memory
if ('_socket' in client.socket && node.visibleCheck(client.viewBox,client.centerPos)) {
client.nodeAdditionQueue.push(node);
}
}
};
GameServer.prototype.removeNode = function(node) {
// Remove from main nodes list
var index = this.nodes.indexOf(node);
if (index != -1) {
this.nodes.splice(index, 1);
}
// Remove from moving cells list
index = this.movingNodes.indexOf(node);
if (index != -1) {
this.movingNodes.splice(index, 1);
}
// Special on-remove actions
node.onRemove(this);
// Animation when eating
for (var i = 0; i < this.clients.length;i++) {
client = this.clients[i].playerTracker;
if (!client) {
continue;
}
// Remove from client
client.nodeDestroyQueue.push(node);
}
};
GameServer.prototype.cellTick = function() {
// Move cells
this.updateMoveEngine();
}
GameServer.prototype.spawnTick = function() {
// Spawn food
this.tickSpawn++;
if (this.tickSpawn >= this.config.spawnInterval) {
this.updateFood(); // Spawn food
this.virusCheck(); // Spawn viruses
this.tickSpawn = 0; // Reset
}
}
GameServer.prototype.gamemodeTick = function() {
// Gamemode tick
this.gameMode.onTick(this);
}
GameServer.prototype.cellUpdateTick = function() {
// Update cells
this.updateCells();
}
GameServer.prototype.mainLoop = function() {
// Timer
var local = new Date();
this.tick += (local - this.time);
this.time = local;
// Default 50 (aka 50ms) if change here change movespeed as well
if (this.tick >= 50) {
// Loop main functions
if (this.run) {
this.cellTick();
this.spawnTick();
this.gamemodeTick();
this.MasterPing();
}
// Update the client's maps
this.updateClients();
// Update cells/leaderboard loop
this.tickMain++;
if (this.tickMain >= 20) { // 1 Second
this.cellUpdateTick();
// Update leaderboard with the gamemode's method
this.leaderboard = [];
this.gameMode.updateLB(this);
this.lb_packet = new Packet.UpdateLeaderboard(this.leaderboard,this.gameMode.packetLB);
this.tickMain = 0; // Reset
}
// Check Bot Min Players
var players = 0;
this.clients.forEach(function(client) {
if (client.playerTracker && !client.playerTracker.spectate)
players++
});
if ( players < this.config.serverBots )
{
this.bots.addBot();
}
// Debug
//console.log(this.tick - 50);
// Auto Server Reset
if( this.config.serverResetTime > 0 && ( local - this.startTime ) > ( this.config.serverResetTime * 3600000 ) )
{
this.exitserver();
}
// Reset
this.tick = 0;
}
};
GameServer.prototype.exitserver = function() {
console.log("Server Shutdown!");
if ( this.sqlconfig.host != '' )
{
this.mysql.connect();
}
this.socketServer.close();
process.exit(1);
window.close();
}
GameServer.prototype.updateClients = function() {
for (var i = 0; i < this.clients.length; i++) {
if (typeof this.clients[i] == "undefined") {
continue;
}
this.clients[i].playerTracker.update();
}
};
GameServer.prototype.startingFood = function() {
// Spawns the starting amount of food cells
for (var i = 0; i < this.config.foodStartAmount; i++) {
this.spawnFood();
}
};
GameServer.prototype.updateFood = function() {
var toSpawn = Math.min(this.config.foodSpawnAmount,(this.config.foodMaxAmount-this.currentFood));
for (var i = 0; i < toSpawn; i++) {
this.spawnFood();
}
};
GameServer.prototype.spawnFood = function() {
var f = new Entity.Food(this.getNextNodeId(), null, this.getRandomPosition(), Math.floor(Math.random() * this.config.foodMaxMass) + this.config.foodMass);
f.setColor(this.getRandomColor());
this.addNode(f);
this.currentFood++;
};
GameServer.prototype.spawnPlayer = function(player,pos,mass) {
if (pos == null) { // Get random pos
pos = this.getRandomSpawn();//pos = this.getRandomPosition();
}
if (mass == null) { // Get starting mass
mass = this.config.playerStartMass;
}
// Spawn player and add to world
var cell = new Entity.PlayerCell(this.getNextNodeId(), player, pos, mass);
if ( '_socket' in player.socket )
{
var zname = player.name;
if ( zname === "" ) zname = "Un Named";
/*
* var packet = new Packet.BroadCast(zname + " joined the game!");
* for (var i = 0; i < this.clients.length; i++) {
* this.clients[i].sendPacket(packet);
* }
*/
if( this.config.serverResetTime > 0 )
{
var packet = new Packet.BroadCast("Remember, This server auto restarts after " + this.config.serverResetTime + " hours uptime!");
player.socket.sendPacket(packet);
}
console.log( "\u001B[36m" + zname + " joined the game\u001B[0m");
}
this.addNode(cell);
// Set initial mouse coords
player.mouse = {x: pos.x, y: pos.y};
};
GameServer.prototype.virusCheck = function() {
// Checks if there are enough viruses on the map
if (this.nodesVirus.length < this.config.virusMinAmount) {
// Spawns a virus
var pos = this.getRandomPosition();
var virusSquareSize = ( ( this.config.virusStartMass ) * 110) >> 0;
// Check for players
for (var i = 0; i < this.nodesPlayer.length; i++) {
var check = this.nodesPlayer[i];
if (check.mass < this.config.virusStartMass) {
continue;
}
// New way
var squareR = check.getSquareSize(); // squared Radius of checking player cell
var dx = check.position.x - pos.x;
var dy = check.position.y - pos.y;
if (dx * dx + dy * dy + virusSquareSize <= squareR) return; // Collided
}
// Check for other virus
for (var i = 0; i < this.nodesVirus.length; i++) {
var check = this.nodesVirus[i];
var squareR = check.getSquareSize();
var dx = check.position.x - pos.x;
var dy = check.position.y - pos.y;
if (dx * dx + dy * dy + virusSquareSize <= squareR) return; // Collided
}
// Spawn if no cells are colliding
var v = new Entity.Virus(this.getNextNodeId(), null, pos, this.config.virusStartMass);
this.addNode(v);
}
};
GameServer.prototype.updateMoveEngine = function() {
// Move player cells
var len = this.nodesPlayer.length;
for (var i = 0; i < len; i++) {
var cell = this.nodesPlayer[i];
// Do not move cells that have already been eaten or have collision turned off
if (!cell) { // || (cell.ignoreCollision)) {
continue;
}
var client = cell.owner;
cell.calcMove(client.mouse.x, client.mouse.y, this);
// Check if cells nearby
var list = this.getCellsInRange(cell);
for (var j = 0; j < list.length ; j++) {
var check = list[j];
// if we're deleting from this.nodesPlayer, fix outer loop variables; we need to update its length, and maybe 'i' too
if (check.cellType == 0) {
len--;
if (check.nodeId < cell.nodeId) {
i--;
}
}
// Consume effect
check.onConsume(cell,this);
// Remove cell
check.setKiller(cell);
this.removeNode(check);
}
}
// A system to move cells not controlled by players (ex. viruses, ejected mass)
len = this.movingNodes.length;
for (var i = 0; i < len; i++) {
var check = this.movingNodes[i];
// Recycle unused nodes
while ((typeof check == "undefined") && (i < this.movingNodes.length)) {
// Remove moving cells that are undefined
this.movingNodes.splice(i, 1);
check = this.movingNodes[i];
}
if (i >= this.movingNodes.length) {
continue;
}
if (check.moveEngineTicks > 0) {
check.onAutoMove(this);
// If the cell has enough move ticks, then move it
check.calcMovePhys(this.config);
} else {
// Auto move is done
check.moveDone(this);
// Remove cell from list
var index = this.movingNodes.indexOf(check);
if (index != -1) {
this.movingNodes.splice(index, 1);
}
}
}
};
GameServer.prototype.setAsMovingNode = function(node) {
this.movingNodes.push(node);
};
GameServer.prototype.sendMSG = function(client,msg) {
var packet = new Packet.BroadCast(msg);
client.socket.sendPacket(packet);
}
GameServer.prototype.splitCells = function(client) {
var len = client.cells.length;
for (var i = 0; i < len; i++) {
if (client.cells.length >= this.config.playerMaxCells) {
// Player cell limit
continue;
}
var cell = client.cells[i];
if (!cell) {
continue;
}
if (cell.mass < this.config.playerMinMassSplit) {
continue;
}
// Get angle
var deltaY = client.mouse.y - cell.position.y;
var deltaX = client.mouse.x - cell.position.x;
var angle = Math.atan2(deltaX,deltaY);
// Get starting position
var size = cell.getSize()/2;
var startPos = {
x: cell.position.x + ( size * Math.sin(angle) ),
y: cell.position.y + ( size * Math.cos(angle) )
};
// Calculate mass and speed of splitting cell
// Calculate mass and speed of splitting cell
var splitSpeed = cell.getSpeed() * this.config.playerSplitSpeedMultiplier;
var newMass = cell.mass / 2;
cell.mass = newMass;
// Create cell
var split = new Entity.PlayerCell(this.getNextNodeId(), client, startPos, newMass);
split.setAngle(angle);
split.setMoveEngineData(splitSpeed, 32, 0.85);
split.calcMergeTime(this.config.playerRecombineTime);
if(this.config.playerSmoothSplit) {
split.ignoreCollision = true;
split.restoreCollisionTicks = 8;
}
// split.owner.name = client.owner.name;
// Add to moving cells list
this.addNode(split); // moved this here,. to see if it needs be aded, before move...
this.setAsMovingNode(split);
}
};
GameServer.prototype.ejectMass = function(client) {
for (var i = 0; i < client.cells.length; i++) {
var cell = client.cells[i];
if (!cell) {
continue;
}
if (cell.mass < this.config.playerMinMassEject) {
continue;
}
var deltaY = client.mouse.y - cell.position.y;
var deltaX = client.mouse.x - cell.position.x;
var angle = Math.atan2(deltaX,deltaY);
// Get starting position
var size = cell.getSize() + 5;
var startPos = {
x: cell.position.x + ( (size + this.config.ejectMass) * Math.sin(angle) ),
y: cell.position.y + ( (size + this.config.ejectMass) * Math.cos(angle) )
};
// Remove mass from parent cell
cell.mass -= this.config.ejectMassLoss;
// Randomize angle
angle += (Math.random() * .4) - .2;
// Create cell
var ejected = new Entity.EjectedMass(this.getNextNodeId(), null, startPos, this.config.ejectMass);
ejected.setAngle(angle);
ejected.setMoveEngineData(this.config.ejectSpeed, 20);
ejected.setColor(cell.getColor());
this.addNode(ejected);
this.setAsMovingNode(ejected);
}
};
GameServer.prototype.newCellVirused = function(client, parent, angle, mass, speed) {
// Starting position
var startPos = {
x: parent.position.x,
y: parent.position.y
};
// Create cell
newCell = new Entity.PlayerCell(this.getNextNodeId(), client, startPos, mass);
newCell.setAngle(angle);
newCell.setMoveEngineData(speed * this.config.playerPopsplitSpeed, 15);
newCell.calcMergeTime(this.config.playerRecombineTime);
newCell.ignoreCollision = true; // Turn off collision
// Add to moving cells list
this.addNode(newCell);
this.setAsMovingNode(newCell);
};
GameServer.prototype.shootVirus = function(parent) {
var parentPos = {
x: parent.position.x,
y: parent.position.y,
};
var newVirus = new Entity.Virus(this.getNextNodeId(), null, parentPos, this.config.virusStartMass);
newVirus.setAngle(parent.getAngle());
newVirus.setMoveEngineData(200, 20);
// Add to moving cells list
this.addNode(newVirus);
this.setAsMovingNode(newVirus);
};
GameServer.prototype.getCellsInRange = function(cell) {
var list = new Array();
var squareR = cell.getSquareSize(); // Get cell squared radius
/*var r = 85;
var topY = cell.position.y - r;
var bottomY = cell.position.y + r;
var leftX = cell.position.x - r;
var rightX = cell.position.x + r;*/
// Loop through all cells that are visible to the cell. There is probably a more efficient way of doing this but whatever
var len = cell.owner.visibleNodes.length;
for (var i = 0;i < len;i++) {
var check = cell.owner.visibleNodes[i];
if (typeof check === 'undefined') {
continue;
}
// if something already collided with this cell, don't check for other collisions
if (check.inRange) {
continue;
}
// Can't eat itself
if (cell.nodeId == check.nodeId) {
continue;
}
// Can't eat cells that have collision turned off
if ((cell.owner == check.owner) && (cell.ignoreCollision)) {
continue;
}
// AABB Collision
/* if (!check.collisionCheck(bottomY,topY,rightX,leftX)) {
continue;
}*/
if (!check.collisionCheck2(squareR, cell.position)) {
continue;
}
// Cell type check - Cell must be bigger than this number times the mass of the cell being eaten
var multiplier = 1.25;
switch (check.getType()) {
case 1: // Food cell
list.push(check);
check.inRange = true; // skip future collision checks for this food
continue;
case 2: // Virus
multiplier = 1.33;
break;
case 0: // Players
// Can't eat self if it's not time to recombine yet
if (check.owner == cell.owner) {
if ((cell.recombineTicks > 0) || (check.recombineTicks > 0)) {
continue;
}
multiplier = 1.00;
}
// Can't eat team members
if (this.gameMode.haveTeams) {
if (!check.owner) { // Error check
continue;
}
if ((check.owner != cell.owner) && (check.owner.getTeam() == cell.owner.getTeam())) {
continue;
}
}
break;
default:
break;
}
// Make sure the cell is big enough to be eaten.
if ((check.mass * multiplier) > cell.mass) {
continue;
}
// Eating range
var xs = Math.pow(check.position.x - cell.position.x, 2);
var ys = Math.pow(check.position.y - cell.position.y, 2);
var dist = Math.sqrt( xs + ys );
var eatingRange = cell.getSize() - check.getEatingRange(); // Eating range = radius of eating cell + 40% of the radius of the cell being eaten
if (dist > eatingRange) {
// Not in eating range
continue;
}
// Add to list of cells nearby
list.push(check);
// Something is about to eat this cell; no need to check for other collisions with it
check.inRange = true;
}
return list;
};
GameServer.prototype.getNearestVirus = function(cell) {
// More like getNearbyVirus
var virus = null;
var r = 100; // Checking radius
var topY = cell.position.y - r;
var bottomY = cell.position.y + r;
var leftX = cell.position.x - r;
var rightX = cell.position.x + r;
// Loop through all viruses on the map. There is probably a more efficient way of doing this but whatever
var len = this.nodesVirus.length;
for (var i = 0;i < len;i++) {
var check = this.nodesVirus[i];
if (typeof check === 'undefined') {
continue;
}
if (!check.collisionCheck(bottomY,topY,rightX,leftX)) {
continue;
}
// Add to list of cells nearby
virus = check;
break; // stop checking when a virus found
}
return virus;
};
GameServer.prototype.updateCells = function() {
if (!this.run) {
// Server is paused
return;
}
// Loop through all player cells
var massDecay = 1 - (this.config.playerMassDecayRate * this.gameMode.decayMod);
for (var i = 0; i < this.nodesPlayer.length; i++) {
var cell = this.nodesPlayer[i];
if (!cell) {
continue;
}
if (cell.recombineTicks > 0) {
// Recombining
cell.recombineTicks--;
}
// Mass decay
if (cell.mass >= this.config.playerMinMassDecay) {
cell.mass *= massDecay;
}
}
};
GameServer.prototype.loadConfig = function() {
try {
// Load the contents of the config file
var load = ini.parse(fs.readFileSync('./gameserver.ini', 'utf-8'));
for (var obj in load) {
if ( obj.substr(0,2) != "//" ) this.config[obj] = load[obj];
}
} catch (err) {
console.log("\u001B[33mConfig not found... Generating new config\u001B[0m");
// Create a new config
fs.writeFileSync('./gameserver.ini', ini.stringify(this.config));
}
try {
// Load the contents of the mysql config file
var load = ini.parse(fs.readFileSync('./mysql.ini', 'utf-8'));
for (var obj in load) {
if ( obj.substr(0,2) != "//" ) this.sqlconfig[obj] = load[obj];
}
} catch (err) {
// Noting to do...
}
};
GameServer.prototype.switchSpectator = function(player) {
var zname = player.name;
if ( zname === "" ) zname = "Client";
console.log("\u001B[35m" + zname +" joined spectators\u001B[0m" );
if (this.gameMode.specByLeaderboard) {
player.spectatedPlayer++;
if (player.spectatedPlayer == this.leaderboard.length) {
player.spectatedPlayer = 0;
}
} else {
// Find next non-spectator with cells in the client list
var oldPlayer = player.spectatedPlayer + 1;