-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathzone.js
692 lines (579 loc) · 19.3 KB
/
zone.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
Zone = function Zone(ID) {
this.ID = ID;
this.Init();
};
if(typeof(Zone_Prototype) === 'undefined') {
Zone_Prototype = {};
}
Zone.prototype = Zone_Prototype;
vms.depends({
name: 'Zone',
depends: [
'Info_Npc',
'Info_Item',
'Info_Skill',
'Character',
'Packets',
'Info_Monster'
// 'AIModule'
]
}, function(){
// END OF NPC Definition
Zone_Prototype.step = function(delta) {
if (this.Loaded) {
this.QuadTree.update(delta);
}
};
Zone_Prototype.Init = function() {
this.Loaded = false;
// TODO: Implement Quadtree for these objects
this.NPC = [];
this.NPCNextID = 1;
this.NPCMaxLength = 10000;
this.Monster = [];
this.Item = [];
this.Player = [];
this.Clients = [];
this.MoveRegions = [];
this.SafeRegions = [];
this.zone_script = {};
MonsterAICollection = new AICollection();
NpcAICollection = new AICollection();
ZoneAICollection = new AICollection();
};
Zone_Prototype.clientNodeUpdate = function(node, delta) {
//if (this.character && this.character.state) {
return { x: this.character.state.Location.X, y: this.character.state.Location.Z, size: this.character.state.Level };
//}
//return null;
};
Zone_Prototype.npcUpdate = function(node, delta) {
return { x: this.Location.X, y: this.Location.Z, size: 1 };
};
Zone_Prototype.addSocket = function(socket) {
// Should check to make sure it dosnt exist already etc.
this.Clients.push(socket);
var node = this.QuadTree.addNode(new QuadTree.QuadTreeNode({ object: socket, update: this.clientNodeUpdate, type: 'client' }));
socket.node = node;
socket.character.state.UniqueID = node.id;
if(this.zone_script.onClientJoin) this.zone_script.onClientJoin(socket);
// Create character state object for this socket
// Send character state object to all with it being compressed
// Setup any timers needed and shit here
//socket.character.Talk('Hello World!');
this.sendToAllAreaLocation(socket.character.state.Location, socket.character.state.getPacket(), config.viewable_action_distance);
};
Zone_Prototype.findCharacterSocket = function(Name) {
var socket = null;
// Search connected clients
//console.log('There are this many clients in zone ' + this.Clients.length);
for(var i = 0; i < this.Clients.length; ++i) {
if(this.Clients[i].character.Name == Name && this.Clients[i]._handle) {
socket = this.Clients[i];
break;
}
}
return socket;
};
Zone_Prototype.findCharacterSocket = function(Name) {
var socket = null;
// Search connected clients
//console.log('There are this many clients in zone ' + this.Clients.length);
for(var i = 0; i < this.Clients.length; ++i) {
if(this.Clients[i].character.Name == Name && this.Clients[i]._handle) {
socket = this.Clients[i];
break;
}
}
return socket;
};
Zone_Prototype.findSocketByCharacterID = function(CharacterID) {
var socket = null;
// Search connected clients
//console.log('There are this many clients in zone ' + this.Clients.length);
for(var i = 0; i < this.Clients.length; ++i) {
if(this.Clients[i].character._id == CharacterID && this.Clients[i]._handle) {
socket = this.Clients[i];
break;
}
}
return socket;
};
Zone.prototype.findCharacterSocketNodeID = function(ID) {
// Search connected world.clients
for(var i = 0; i < this.Clients.length; ++i) {
if(this.Clients[i].node.id == ID && this.Clients[i]._handle) {
return this.Clients[i];
}
}
return null;
};
Zone_Prototype.removeSocket = function(socket) {
// Remove it from any other updates needed here eg Duel, Monster AI, NPC Talking Too?, Faction Count on Zone etc
// Remove any zone timers and such
console.log('Removing Socket');
this.QuadTree.removeNode(socket.node.id);
if(this.zone_script.onClientLeave) this.zone_script.onClientLeave(socket);
//this.Clients = this.Clients.splice(this.Clients.indexOf(socket), 1);
this.Clients.slice(this.Clients.indexOf(socket), 1);
};
Zone_Prototype.forEachClient = function(func) {
this.Clients.forEach(function(client) {
if(client.authenticated == false) return;
return func.call(client);
});
};
Zone_Prototype.sendToAll = function(buffer) {
this.Clients.forEach(function(client) {
if(client.authenticated == false || !client._handle) return;
client.write(buffer);
});
};
Zone_Prototype.sendToAllArea = function(origional, sendtoself, buffer, distance) {
// console.log(origional);
this.Clients.forEach(function(client) {
if(client.authenticated == false || !client._handle) return;
if(sendtoself == false && client == origional) return;
if(client.character.state.Location.getDistance(origional.character.state.Location) <= distance) {
client.write(buffer);
}
});
};
Zone_Prototype.sendToAllAreaLocation = function(location, buffer, distance, self) {
var found = this.QuadTree.query({ CVec3: location, radius: distance, type: 'client' });
for (var i=0;i<found.length;i++) {
if (self && self === found[i].object) continue; // Skip self if required too
if (found[i].authenticated === false) continue; // Skip not authenticated clients
found[i].object.write(buffer);
}
};
Zone_Prototype.sendToAllClan = function(buffer, clan) {
this.Clients.forEach(function(client) {
if(client.authenticated == false || !client._handle) return;
if(client.character.Clan == clan) {
client.write(buffer);
}
});
};
Zone_Prototype.getPortal = function(ZoneID) {
console.log('getPortal ' + ZoneID);
var portal = null;
for(var i = 0; i < this.MoveRegions.length; ++i) {
console.log(i, this.MoveRegions[i].ZoneID, i % 2);
if(i % 2 == 0 && this.MoveRegions[i].ZoneID === ZoneID) {
portal = this.MoveRegions[i];
break;
}
}
return portal;
};
Zone_Prototype.getPortalEndPoint = function(ZoneID) {
console.log('getPortalEndPoint ' + ZoneID);
var portal = null;
for(var i = 0; i < this.MoveRegions.length; ++i) {
if(i % 2 == 1 && this.MoveRegions[i].ZoneID === ZoneID) {
portal = this.MoveRegions[i];
break;
}
}
return portal;
};
// Item
Zone_Prototype.createItem = function(spawninfo) {
var item = new Item(spawninfo);
if (typeof(spawninfo) === 'object' && item.Location) {
item.Location.set(spawninfo.Location);
if (spawninfo.Direction) {
item.FacingDirection = spawninfo.Direction;
}
}
return item;
};
Zone_Prototype.addItem = function(item) {
var zone = this;
var node = this.QuadTree.addNode(new QuadTree.QuadTreeNode({ object: item, update: this.npcUpdate, type: 'item' }));
item.node = node;
item.UniqueID = node.id;
item.updateInterval = setInterval(function() {
//console.log('Updating information of item.');
// Send the packet to everyone
// Send item to all area
// send the packet to all in area. item.getPacket()
zone.sendToAllAreaLocation(item.Location, item.getPacket(), config.viewable_action_distance)
}, 4000);
item.itemDeathTimer = setTimeout(function() {
zone.removeItem(item.UniqueID);
}, 180000); // 3 min 180000 ms
if (item.OwnerName != '') {
item.itemOwnerTimer = setTimeout(function() {
item.OwnerName = '';
}, 5000); // 5 sec
}
// Send it to clients
this.sendToAllAreaLocation(item.Location, item.getPacket(), config.viewable_action_distance);
item.JustSpawned = 0;
};
// Expects itemjson to have Row, Column, ID
// Basicaly storageItemSchema is what we are retriving in character.js
Zone_Prototype.getItem = function(index) {
var item = null;
console.log('Get item ' + index);
//if (typeof(this.Items[index]) != 'undefined')
var node = this.QuadTree.getNodeByID(index);
if (node && node.type === 'item') {
console.log('Item Found');
item = node.object;
}
return item;
};
// See http://codepen.io/LiamKarlMitchell/pen/LCBnH
// Need to make similar functions below. And to make the class objects for each.
Zone_Prototype.removeItem = function(nodeID) {
//if (typeof(this.Items[id]) != 'undefined')
// TODO: Find a way to tell client item has gone.
// Send packet to all saying it got picked up / destroyed
console.log('Removing item ' + nodeID);
var node = this.QuadTree.getNodeByID(nodeID);
if (node && node.type === 'item') {
var item = node.object;
item.onDelete();
this.QuadTree.removeNode(nodeID);
item.JustSpawned = 3;
item.Life = 0;
item.ItemID = 0;
this.sendToAllAreaLocation(item.Location, item.getPacket(), config.viewable_action_distance);
}
};
Zone_Prototype.clearItems = function() {
console.log('Clear all items not yet implemented.');
};
// Monster
Zone_Prototype.createMonster = function(spawninfo) {
//console.log('creating monster with spawninfo: ',spawninfo);
var mi = infos.Monster[spawninfo.ID];
if (mi == null) return null;
var monster = new Monster(mi);
if (monster == null) return null;
monster.spawninfo = spawninfo;
//monster.UniqueID = spawninfo.UniqueID;
monster.UniqueID = 2;
monster.ID = this.MonstersNextID;
this.MonstersNextID++;
if (this.MonstersNextID > this.MonstersMaxLength) {
this.MonstersNextID = 0; // Could find next free slot and if none free overwrite older items?
// Quick sort ftw.
}
monster.Location.set(spawninfo.Location);
monster.LocationTo.set(spawninfo.Location);
//monster.Velocity.setN(5);
monster.FacingDirection = spawninfo.Direction;
//this.Monsters[monster.ID] = monster;
//Objects.CreateNode(monster);
//console.log(monster);
return monster;
};
Zone_Prototype.createMonster = function(spawninfo) {
var monster = new Monster(spawninfo.ID);
if (!monster) return null;
monster.spawninfo = spawninfo;
monster.ID = this.NPCNextID;
this.MonsterNextID++;
if (this.MonsterNextID > this.MonsterMaxLength) {
this.MonsterNextID = 0; // Could find next free slot and if none free overwrite older items?
// Quick sort ftw.
}
monster.Location.set(spawninfo.Location);
monster.FacingDirection = spawninfo.Direction;
//this.Monsters[monster.ID] = monster;
return monster;
};
Zone_Prototype.addMonster = function(monster) {
var zone = this;
monster.zone = this;
var node = this.QuadTree.addNode(new QuadTree.QuadTreeNode({ object: monster, update: this.npcUpdate, type: 'monster' }));
if (!node) {
if (config.errorLevel >= 4) dumpError('Problem adding monster it is outside bounds of quadtree no doubt.');
return;
}
monster.node = node;
monster.UniqueID = node.id;
monster.updateInterval = setInterval(function() {
// Update and send to all in area
if (zone.Clients.length > 0) {
zone.sendToAllAreaLocation(monster.Location, monster.getPacket(), config.viewable_action_distance);
}
}, 4000);
// Send it to clients
zone.sendToAllAreaLocation(monster.Location, monster.getPacket(), config.viewable_action_distance);
monster.JustSpawned = 0;
monster.Skill = 1;
// Should be set to spawn?
//monster.SetAI(MonsterAIs.Get('Stand'));
};
// Expects itemjson to have Row, Column, ID
// Basicaly storageItemSchema is what we are retriving in character.js
Zone_Prototype.getMonster = function(index) {
var monster = null;
console.log('Get monster ' + index);
if (this.Monsters[index]) {
console.log('monster Found');
monster = this.Monsters[index]; // Check for valid shit later eg distance.
}
return monster;
};
Zone_Prototype.removeMonster = function(index) {
console.log('! CODE removeMonster');
if (this.Monsters[index]) {
// Send packet to all saying it got picked up / destroyed
console.log('Removing monster ' + index);
var monster = this.Monsters[index];
monster.onDelete();
this.QuadTree.removeNode(monster.node);
monster.JustSpawned = 3;
monster.unknown1 = 1;
monster.unknown2 = 1;
monster.Rotation[0] = 1;
monster.Rotation[1] = 1;
this.sendToAllAreaLocation(monster.Location, monster.getPacket(), config.viewable_action_distance);
//delete this.Monsters[index];
this.Monsters.splice(index, 1);
}
};
Zone_Prototype.clearMonsters = function() {
var zone = this;
this.Monsters.forEach(function(monster, index) {
if (item) {
zone.removeMonster(monster.UniqueID); // Could use index
}
});
};
// NPC Functions
// NPC
Zone_Prototype.createNPC = function(spawninfo) {
//console.log('creating monster with spawninfo: ',spawninfo);
var npc = new Npc(spawninfo.ID);
if (!npc) return null;
npc.spawninfo = spawninfo;
npc.ID = this.NPCNextID;
this.NPCNextID++;
if (this.NPCNextID > this.NPCMaxLength) {
this.NPCNextID = 0; // Could find next free slot and if none free overwrite older items?
// Quick sort ftw.
}
npc.Location.set(spawninfo.Location);
npc.FacingDirection = spawninfo.Direction;
this.NPC[npc.ID] = npc;
//if (npc.NPCID == 50) console.log(npc.ID, npc.UniqueID, npc.NPCID);
return npc;
};
Zone_Prototype.addNPC = function(npc) {
var zone = this;
npc.zone = this;
var node = this.QuadTree.addNode(new QuadTree.QuadTreeNode({ object: npc, update: this.npcUpdate, type: 'npc' }));
if (!node) {
if (config.errorLevel >= 4) dumpError('Problem adding npc it is outside bounds of quadtree no doubt.');
return;
}
npc.node = node;
npc.UniqueID = node.id;
npc.updateInterval = setInterval(function() {
// Update and send to all in area
if (zone.Clients.length > 0) {
zone.sendToAllAreaLocation(npc.Location, npc.getPacket(), config.viewable_action_distance);
}
}, 4000);
this.NPC[npc.UniqueID] = npc;
// Send it to clients
zone.sendToAllAreaLocation(npc.Location, npc.getPacket(), config.viewable_action_distance);
npc.JustSpawned = 0;
//npc.Skill = 0;
};
// Expects itemjson to have Row, Column, ID
// Basicaly storageItemSchema is what we are retriving in character.js
Zone_Prototype.getNPC = function(index) {
var npc = null;
console.log('Get npc ' + index);
if (this.NPC[index]) {
console.log('npc Found');
// return this.QuadTree.nodes[index]?? Lets use Quad Tree
npc = this.NPC[index]; // Check for valid shit later eg distance.
}
return npc;
};
Zone_Prototype.getNPCWhereID = function(id) {
var NPC = [];
// Could also just iterate forward.
for (var index = 0; index < this.NPC.length; index++) {
if (this.NPC[index] && this.NPC[index].OtherID === id) {
console.log('ID '+this.NPC[index].OtherID+' is === '+id);
NPC.push(this.NPC[index]);
console.log(this.NPC[index].NPCID)
}
}
return NPC;
};
Zone_Prototype.removeNPC = function(index) {
if (this.NPC[index]) {
// Send packet to all saying it got picked up / destroyed
console.log('Removing npc ' + index);
var npc = this.NPC[index];
npc.onDelete();
this.QuadTree.removeNode(npc.node);
npc.JustSpawned = 3;
npc.unknown1 = 1;
npc.unknown2 = 1;
npc.Rotation[0] = 1;
npc.Rotation[1] = 1;
this.sendToAllAreaLocation(npc.Location, npc.getPacket(), config.viewable_action_distance);
///delete this.NPC[index];
this.NPC.splice(index, 1);
}
};
Zone_Prototype.clearNPC = function() {
var zone = this;
this.NPC.forEach(function(npc, index) {
if (npc) {
zone.removeNPC(npc.UniqueID); // Could use index
}
});
};
// End of NPC Functions
// TODO: Prevent zones being loaded duplicate times whilst one is still loading.
Zone_Prototype.Load = function(callback) {
if(this.Loaded) {
callback('Already Loaded');
// console.log('Zone ' + this.ID + ' is already loaded.');
return;
}
// Start Loading world mesh
// Get width and height and offset from world data
// Create QuadTree
this.QuadTree = new QuadTree({
x: -50000, y: -50000, size: 100000,
depth: config.quadtree_depth || 6,
limit: config.quadtree_limit || 10
});
var zone = this;
// Monster Spawns
// NPC Spawns
// Map Scripts
// Finally
// TODO: Implement zone loading info
async.series([
// function LoadTerrain(callback) {
// TerrainLoad.call(zone, zoneinfo.Name, function(err) {
// callback(null, true);
// //console.log('Map Y at 0,0 is '+zone.GetY(0, 0));
// });
// },
// function LoadMonsterSpawns(callback) {
// console.log('Loading Monster Spawns for ' + zone.ID);
// fs.readFile('data/spawninfo/' + _util.padLeft(zone.ID,'0', 3) + '.MOB', function(err, data) {
// if (err) {
// // Meh
// } else {
// var RecordCount = data.readUInt32LE(0);
// var spawndata = restruct.struct('info', structs.SpawnInfo, RecordCount).unpack(data.slice(4));
// data = null;
// var length = spawndata.info.length,
// element = null;
// for (var i = 0; i < length; i++) {
// element = spawndata.info[i];
// var monster = zone.createMonster(element);
// if (monster) {
// zone.addMonster(monster);
// }
// if (monster == null) {
// console.log('Failed to load monster '+element.ID+' for zone '+zone.getID());
// }
// }
// }
// callback(null, true);
// });
// },
function LoadNpcspawns(callback) {
console.log('Loading NPC Spawns for zone ' + zone.ID);
fs.readFile('data/spawninfo/' + _util.padLeft(zone.ID,'0', 3) + '.NPC', function(err, data) {
if (err) {
//console.log(err);
} else {
var RecordCount = data.readUInt32LE(0);
//console.log(RecordCount);
var spawndata = restruct.struct('info', structs.SpawnInfo, RecordCount).unpack(data.slice(4));
data = null;
var length = spawndata.info.length,
element = null;
for (var i = 0; i < length; i++) {
element = spawndata.info[i];
//eyes.inspect(element);
if (element.ID) {
var npc = zone.createNPC(element);
zone.addNPC(npc);
}
}
}
callback(null, true);
});
},
function LoadPortals(callback) {
console.log('Loading Portals for ' + zone.ID);
fs.readFile('data/world/Z' + _util.padLeft(zone.ID,'0', 3) + '_ZONEMOVEREGION.WREGION', function(err, data) {
if (err) {
//console.log(err);
} else {
var RecordCount = data.readUInt32LE(0);
//console.log(RecordCount);
var wregion = restruct.struct('info', structs.WREGION, RecordCount).unpack(data.slice(4));
data = null;
var length = wregion.info.length,
element = null;
for (var i = 0; i < length; i++) {
element = wregion.info[i];
zone.MoveRegions.push(element);
}
}
callback(null, true);
});
},
function LoadSaferegion(callback) {
console.log('Loading Safe Region for ' + zone.ID);
fs.readFile('data/world/Z' + _util.padLeft(zone.ID,'0', 3) + '_ZONESAFEREGION.WREGION', function(err, data) {
if (err) {
//error loading
//console.log(err);
} else {
var RecordCount = data.readUInt32LE(0);
var wregion = restruct.struct('info', structs.WREGION, RecordCount).unpack(data.slice(4));
data = null;
var length = wregion.info.length,
element = null;
for (var i = 0; i < length; i++) {
element = wregion.info[i];
if (element.Unknown1 != 0) console.log(element, i, zone.ID, 'id not 0');
if (element.Unknown2 != 0) console.log(element, i, zone.ID, 'id not 0');
if (element.Unknown3 != 0) console.log(element, i, zone.ID, 'id not 0');
zone.SafeRegions.push(element);
}
}
callback(null, true);
});
}
], function(err, results) {
//console.log('Outputing Anim8or File for '+zone.ID);
//zone.OutputAnim8orFile();
zone.Loaded = true;
callback(null, {
ID: zone.ID,
Loaded: true
});
});
//}
//callback(null, true);
};
// TODO: Figure out a way to get prototypal inheritance working with node vm...
if (typeof(zones) !== 'undefined') {
for (var zone in zones) {
zone.__proto__ = Zone_Prototype;
}
}
});