This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
/
MapEngine.js
661 lines (554 loc) · 14.1 KB
/
MapEngine.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
/**
* Engine/MapEngine.js
*
* Map Engine
* Manage Map server
*
* This file is part of ROBrowser, Ragnarok Online in the Web Browser (http://www.robrowser.com/).
*
* @author Vincent Thibault
*/
define(function( require )
{
'use strict';
/**
* Load dependencies
*/
var jQuery = require('Utils/jquery');
var DB = require('DB/DBManager');
var SoundManager = require('Audio/SoundManager');
var BGM = require('Audio/BGM');
var Session = require('Engine/SessionStorage');
var Network = require('Network/NetworkManager');
var PACKET = require('Network/PacketStructure');
var Renderer = require('Renderer/Renderer');
var Camera = require('Renderer/Camera');
var MapRenderer = require('Renderer/MapRenderer');
var EntityManager = require('Renderer/EntityManager');
var Entity = require('Renderer/Entity/Entity');
var Mouse = require('Controls/MouseEventHandler');
var KEYS = require('Controls/KeyEventHandler');
var UIManager = require('UI/UIManager');
var Background = require('UI/Background');
var Escape = require('UI/Components/Escape/Escape');
var ChatBox = require('UI/Components/ChatBox/ChatBox');
var MiniMap = require('UI/Components/MiniMap/MiniMap');
var BasicInfo = require('UI/Components/BasicInfo/BasicInfo');
var WinStats = require('UI/Components/WinStats/WinStats');
var Inventory = require('UI/Components/Inventory/Inventory');
var ShortCut = require('UI/Components/ShortCut/ShortCut');
var Equipment = require('UI/Components/Equipment/Equipment');
var StatusIcons = require('UI/Components/StatusIcons/StatusIcons');
var ChatRoomCreate = require('UI/Components/ChatRoomCreate/ChatRoomCreate');
var SkillList = require('UI/Components/SkillList/SkillList');
/**
* @var {string mapname}
*/
var _mapName = '';
/**
* @var {boolean} is initialized
*/
var _isInitialised = false;
/**
* Connect to Map Server
*
* @param {number} IP
* @param {number} port
* @param {string} mapName
*/
function init( ip, port, mapName )
{
_mapName = mapName;
// Connect to char server
Network.connect( Network.utils.longToIP( ip ), port, function( success ){
// Force reloading map
MapRenderer.currentMap = '';
// Fail to connect...
if (!success) {
UIManager.showErrorBox( DB.getMessage(1) );
return;
}
// Success, try to login.
var pkt = new PACKET.CZ.ENTER();
pkt.AID = Session.AID;
pkt.GID = Session.GID;
pkt.AuthCode = Session.AuthCode;
pkt.clientTime = Date.now();
pkt.Sex = Session.Sex;
Network.sendPacket(pkt);
// Server send back AID
Network.read(function(fp){
// if PACKETVER < 20070521, client send GID...
if (fp.length === 4) {
Session.Character.GID = fp.readLong();
}
});
// Ping
var ping = new PACKET.CZ.REQUEST_TIME();
Network.setPing(function(){
ping.time = Date.now();
Network.sendPacket(ping);
});
}, true);
// Do not hook multiple time
if (_isInitialised) {
return;
}
_isInitialised = true;
// Hook packets
Network.hookPacket( PACKET.ZC.AID, onReceiveAccountID );
Network.hookPacket( PACKET.ZC.ACCEPT_ENTER, onConnectionAccepted );
Network.hookPacket( PACKET.ZC.ACCEPT_ENTER2, onConnectionAccepted );
Network.hookPacket( PACKET.ZC.NPCACK_MAPMOVE, onMapChange );
Network.hookPacket( PACKET.ZC.NPCACK_SERVERMOVE, onServerChange );
Network.hookPacket( PACKET.ZC.ACCEPT_QUIT, onExitSuccess );
Network.hookPacket( PACKET.ZC.REFUSE_QUIT, onExitFail );
Network.hookPacket( PACKET.ZC.RESTART_ACK, onRestartAnswer );
Network.hookPacket( PACKET.ZC.ACK_REQ_DISCONNECT, onDisconnectAnswer );
Network.hookPacket( PACKET.ZC.NOTIFY_TIME, onPong );
// Extend controller
require('./MapEngine/Main').call();
require('./MapEngine/NPC').call();
require('./MapEngine/Entity').call();
require('./MapEngine/Item').call();
require('./MapEngine/PrivateMessage').call();
require('./MapEngine/Storage').call();
require('./MapEngine/Group').call();
require('./MapEngine/Guild').call();
require('./MapEngine/Skill').call();
require('./MapEngine/ChatRoom').call();
require('./MapEngine/Pet').call();
require('./MapEngine/Store').call();
require('./MapEngine/Trade').call();
}
/**
* Pong from server
* TODO: check the time ?
*/
function onPong( pkt )
{
//pkt.time
}
/**
* Server update our account id
*
* @param {object} pkt - PACKET.ZC.AID
*/
function onReceiveAccountID( pkt )
{
Session.Character.GID = pkt.AID;
}
/**
* Map accept us to enter the map
*
* @param {object} pkt - PACKET.ZC.ACCEPT_ENTER
*/
function onConnectionAccepted( pkt )
{
Session.Entity = new Entity( Session.Character );
Session.Entity.onWalkEnd = onWalkEnd;
BasicInfo.update('blvl', Session.Character.level );
BasicInfo.update('jlvl', Session.Character.joblevel );
BasicInfo.update('zeny', Session.Character.money );
BasicInfo.update('name', Session.Character.name );
BasicInfo.update('job', Session.Character.job );
// Bind UI
WinStats.onRequestUpdate = onRequestStatUpdate;
Equipment.onUnEquip = onUnEquip;
Equipment.onConfigUpdate = onConfigUpdate;
Equipment.onEquipItem = onEquipItem;
Equipment.onRemoveOption = onRemoveOption;
Inventory.onUseItem = onUseItem;
Inventory.onEquipItem = onEquipItem;
Escape.onExitRequest = onExitRequest;
Escape.onCharSelectionRequest = onRestartRequest;
Escape.onReturnSavePointRequest = onReturnSavePointRequest;
Escape.onResurectionRequest = onResurectionRequest;
ChatBox.onRequestTalk = onRequestTalk;
// Fix http://forum.robrowser.com/?topic=32177.0
onMapChange({
xPos: pkt.PosDir[0],
yPos: pkt.PosDir[1],
mapName: _mapName
});
}
/**
* Changing map, loading new map
*
* @param {object} pkt - PACKET.ZC.NPCACK_MAPMOVE
*/
function onMapChange( pkt )
{
jQuery(window).off('keydown.map');
MapRenderer.onLoad = function(){
// TODO: find a better place to put it
jQuery(window).on('keydown.map', function( event ){
if (event.which === KEYS.INSERT) {
var pkt = new PACKET.CZ.REQUEST_ACT();
pkt.action = Session.Entity.action === Session.Entity.ACTION.SIT ? 3 : 2;
Network.sendPacket(pkt);
event.stopImmediatePropagation();
return false;
}
});
Session.Entity.set({
PosDir: [ pkt.xPos, pkt.yPos, 0 ],
GID: Session.Character.GID
});
EntityManager.add( Session.Entity );
// Initialize camera
Camera.setTarget( Session.Entity );
Camera.init();
// Add Game UI
MiniMap.append();
MiniMap.setMap( MapRenderer.currentMap );
ChatBox.append();
BasicInfo.append();
Escape.append();
Inventory.append();
Equipment.append();
StatusIcons.append();
ShortCut.append();
ChatRoomCreate.append();
SkillList.append();
// Map loaded
Network.sendPacket(
new PACKET.CZ.NOTIFY_ACTORINIT()
);
};
MapRenderer.setMap( pkt.mapName );
}
/**
* Change zone server
*
* @param {object} pkt - PACKET.ZC.NPCACK_SERVERMOVE
*/
function onServerChange( pkt )
{
jQuery(window).off('keydown.map');
init( pkt.addr.ip, pkt.addr.port, pkt.mapName );
}
/**
* Ask the server to disconnect
*/
function onExitRequest()
{
var pkt = new PACKET.CZ.REQUEST_QUIT();
Network.sendPacket(pkt);
// No Answer from the server, close it now
UIManager.removeComponents();
Network.close();
Renderer.stop();
MapRenderer.free();
SoundManager.stop();
BGM.stop();
Background.remove(function(){
window.close();
require('Engine/GameEngine').init();
});
}
/**
* Server don't want us to disconnect yet
*
* @param {object} pkt - PACKET.ZC.REFUSE_QUIT
*/
function onExitFail( pkt )
{
ChatBox.addText( DB.getMessage(502), ChatBox.TYPE.ERROR);
}
/**
* Server accept to disconnect us
*
* @param {object} pkt - PACKET.ZC.REFUSE_QUIT
*/
function onExitSuccess()
{
Renderer.stop();
MapRenderer.free();
UIManager.removeComponents();
Network.close();
Renderer.stop();
MapRenderer.free();
SoundManager.stop();
BGM.stop();
Background.remove(function(){
window.close();
require('Engine/GameEngine').init();
});
}
/**
* Try to return to char-server
*/
function onRestartRequest()
{
var pkt = new PACKET.CZ.RESTART();
pkt.type = 1;
Network.sendPacket(pkt);
}
/**
* Go back to save point request
*/
function onReturnSavePointRequest()
{
var pkt = new PACKET.CZ.RESTART();
pkt.type = 0;
Network.sendPacket(pkt);
}
/**
* Resurection feature
*/
function onResurectionRequest()
{
var pkt = new PACKET.CZ.STANDING_RESURRECTION();
Network.sendPacket(pkt);
}
/**
* Does the server want you to return to char-server ?
*
* @param {object} pkt - PACKET.ZC.RESTART_ACK
*/
function onRestartAnswer( pkt )
{
if (!pkt.type) {
// Have to wait 10sec
ChatBox.addText( DB.getMessage(502), ChatBox.TYPE.ERROR );
}
else {
StatusIcons.clean();
ChatBox.clean();
ShortCut.clean();
MapRenderer.free();
Renderer.stop();
onRestart();
}
}
/**
* Response from server to disconnect
* @param pkt - {object}
*/
function onDisconnectAnswer( pkt )
{
switch (pkt.result) {
// Disconnect
case 0:
ChatBox.clean();
Renderer.stop();
onExitSuccess();
break;
case 1:
// Have to wait 10 sec
ChatBox.addText( DB.getMessage(502), ChatBox.TYPE.ERROR);
break;
default:
}
}
/**
* ChatBox talk
*
* @param {string} user
* @param {string} text
*/
function onRequestTalk( user, text )
{
var pkt;
if (user.length) {
pkt = new PACKET.CZ.WHISPER();
pkt.receiver = user;
pkt.msg = text;
}
else if (text[0] === '%') {
pkt = new PACKET.CZ.REQUEST_CHAT_PARTY();
pkt.msg = Session.Entity.display.name + ' : ' + text.substr(1);
}
else if (text[0] === '$') {
pkt = new PACKET.CZ.GUILD_CHAT();
pkt.msg = Session.Entity.display.name + ' : ' + text.substr(1);
}
else {
pkt = new PACKET.CZ.REQUEST_CHAT();
pkt.msg = Session.Entity.display.name + ' : ' + text;
}
Network.sendPacket(pkt);
}
/**
* Remove cart/peco/falcon
*/
function onRemoveOption()
{
var pkt = new PACKET.CZ.REQ_CARTOFF();
Network.sendPacket(pkt);
}
/**
* @var {number} walk timer
*/
var _walkTimer = null;
/**
* @var {number} Last delay to walk
*/
var _walkLastTick = 0;
/**
* Ask to move
*/
function onMouseDown()
{
clearTimeout(_walkTimer);
// If siting, update direction
if (Session.Entity.action === Session.Entity.ACTION.SIT /*|| KEYS.SHIFT see: http://forum.robrowser.com/index.php?topic=32240#msg32446 */) {
Session.Entity.lookTo( Mouse.world.x, Mouse.world.y );
var pkt = new PACKET.CZ.CHANGE_DIRECTION();
pkt.headDir = Session.Entity.headDir;
pkt.dir = Session.Entity.direction;
Network.sendPacket(pkt);
return;
}
onWalkRequest();
}
/**
* Stop moving
*/
function onMouseUp()
{
clearTimeout(_walkTimer);
}
/**
* Moving function
*/
function onWalkRequest()
{
// setTimeout isn't accurate, so reduce the value
// to avoid possible errors.
if (_walkLastTick + 450 > Renderer.tick) {
return;
}
if (Mouse.world.x > -1 && Mouse.world.y > -1) {
var pkt = new PACKET.CZ.REQUEST_MOVE();
pkt.dest[0] = Mouse.world.x;
pkt.dest[1] = Mouse.world.y;
Network.sendPacket(pkt);
}
clearTimeout(_walkTimer);
_walkTimer = setTimeout( onWalkRequest, 500);
_walkLastTick = +Renderer.tick;
}
/**
* If the character moved to attack, once it finished to move ask to attack
*/
function onWalkEnd()
{
// No action to do ?
if (!Session.moveTarget) {
return;
}
var pkt, entity = Session.moveTarget;
Session.moveTarget = null;
// Not in scene anymore.
if (!EntityManager.get(entity.GID)) {
return;
}
switch (entity.objecttype) {
case Entity.TYPE_MOB:
case Entity.TYPE_PC:
entity.onFocus();
break;
case Entity.TYPE_ITEM:
pkt = new PACKET.CZ.ITEM_PICKUP();
pkt.ITAID = entity.GID;
Network.sendPacket(pkt);
break;
}
}
/**
* Ask server to update status
*
* @param {number} id
* @param {number} amount
*/
function onRequestStatUpdate(id, amount)
{
var pkt = new PACKET.CZ.STATUS_CHANGE();
pkt.statusID = id;
pkt.changeAmount = amount;
Network.sendPacket(pkt);
}
/**
* Drop item to the floor
*
* @param {number} index in inventory
* @param {number} count to drop
*/
function onDropItem( index, count )
{
if (count) {
var pkt = new PACKET.CZ.ITEM_THROW();
pkt.Index = index;
pkt.count = count;
Network.sendPacket(pkt);
}
}
/**
* Use an item
*
* @param {number} item's index
*/
function onUseItem( index )
{
var pkt = new PACKET.CZ.USE_ITEM();
pkt.index = index;
pkt.AID = Session.Entity.GID;
Network.sendPacket(pkt);
}
/**
* Equip item
*
* @param {number} item's index
* @param {number} where to equip
*/
function onEquipItem( index, location )
{
var pkt = new PACKET.CZ.REQ_WEAR_EQUIP();
pkt.index = index;
pkt.wearLocation = location;
Network.sendPacket(pkt);
}
/**
* Take off an equip
*
* @param {number} index to unequip
*/
function onUnEquip( index )
{
var pkt = new PACKET.CZ.REQ_TAKEOFF_EQUIP();
pkt.index = index;
Network.sendPacket(pkt);
}
/**
* Update config
*
* @param {number} config id (only type:0 is supported - equip)
* @param {number} val
*/
function onConfigUpdate( type, val )
{
var pkt = new PACKET.CZ.CONFIG();
pkt.Config = type;
pkt.Value = val;
Network.sendPacket(pkt);
}
/**
* Go back from map-server to char-server
*/
function onRestart()
{
require('Engine/CharEngine').reload();
}
/**
* Export
*/
return new function MapEngine(){
this.init = init;
this.onMouseUp = onMouseUp;
this.onMouseDown = onMouseDown;
this.onDropItem = onDropItem;
require('Controls/MapControl').call(this);
}();
});