forked from InfiniteRasa/Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapChannel.cpp
1133 lines (1061 loc) · 38.1 KB
/
MapChannel.cpp
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
#include"Global.h"
/*
A MapChannel in general is a isolated thread of the server that is 100% responsible for one or multiple maps
Whenever a client selects a character the clientinstance is passed to the MapChannel and is no more processed by the main thread.
If a client changes map his instance is passed to the target MapChannel
*/
hashTable_t ht_mapChannelsByContextId;
mapChannelList_t *global_channelList; //20110827 @dennton
void mapteleporter_teleportEntity(sint32 destX,sint32 destY, sint32 destZ, sint32 mapContextId, mapChannelClient_t *player)
{
destY += 700;
printf("teleport to: x y z map - %d %d %d %d \n",destX,destY,destZ,mapContextId);
//remove entity from old map - remove client from all channels
communicator_playerExitMap(player);
//unregister player
//communicator_unregisterPlayer(cm);
//remove visible entity
Thread::LockMutex(&player->cgm->cs_general);
cellMgr_removeFromWorld(player);
// remove from list
for(sint32 i=0; i<player->mapChannel->playerCount; i++)
{
if( player == player->mapChannel->playerList[i] )
{
if( i == player->mapChannel->playerCount-1 )
{
player->mapChannel->playerCount--;
}
else
{
player->mapChannel->playerList[i] = player->mapChannel->playerList[player->mapChannel->playerCount-1];
player->mapChannel->playerCount--;
}
break;
}
}
Thread::UnlockMutex(&player->cgm->cs_general);
//############## map loading stuff ##############
// send PreWonkavate (clientMethod.134)
pyMarshalString_t pms;
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, 0); // wonkType - actually not used by the game
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(player->cgm, 5, 134, pym_getData(&pms), pym_getLen(&pms));
// send Wonkavate (inputstateRouter.242)
player->cgm->mapLoadContextId = mapContextId;
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, mapContextId); // gameContextId (alias mapId)
pym_addInt(&pms, 0); // instanceId ( not important for now )
// find map version
sint32 mapVersion = 0; // default = 0;
for(sint32 i=0; i<mapInfoCount; i++)
{
if( mapInfoArray[i].contextId == mapContextId )
{
mapVersion = mapInfoArray[i].version;
break;
}
}
pym_addInt(&pms, mapVersion); // templateVersion ( from the map file? )
pym_tuple_begin(&pms); // startPosition
pym_addFloat(&pms, destX); // x (todo: send as float)
pym_addFloat(&pms, destY); // y (todo: send as float)
pym_addFloat(&pms, destZ); // z (todo: send as float)
pym_tuple_end(&pms);
pym_addInt(&pms, 0); // startRotation (todo, read from db and send as float)
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(player->cgm, 6, METHODID_WONKAVATE, pym_getData(&pms), pym_getLen(&pms));
//################## player assigning ###############
communicator_loginOk(player->mapChannel, player);
communicator_playerEnterMap(player);
//add entity to new map
player->player->actor->posX = destX;
player->player->actor->posY = destY;
player->player->actor->posZ = destZ;
//cm->mapChannel->mapInfo->contextId = telepos.mapContextId;
player->player->controllerUser->inventory = player->inventory;
player->player->controllerUser->mission = player->mission;
player->tempCharacterData = player->player->controllerUser->tempCharacterData;
//---search new mapchannel
for(sint32 chan=0; chan < global_channelList->mapChannelCount; chan++)
{
mapChannel_t *mapChannel = global_channelList->mapChannelArray+chan;
if(mapChannel->mapInfo->contextId == mapContextId)
{
player->mapChannel = mapChannel;
break;
}
}
mapChannel_t *mapChannel = player->mapChannel;
Thread::LockMutex(&player->mapChannel->criticalSection);
mapChannel->playerList[mapChannel->playerCount] = player;
mapChannel->playerCount++;
hashTable_set(&mapChannel->ht_socketToClient, (uint32)player->cgm->socket, player);
Thread::UnlockMutex(&mapChannel->criticalSection);
player->player->actor->posX = destX;
player->player->actor->posY = destY;
player->player->actor->posZ = destZ;
cellMgr_addToWorld(player); //cellsint32roducing to player /from players
// setCurrentContextId (clientMethod.362)
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, player->mapChannel->mapInfo->contextId);
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(player->cgm, 5, 362, pym_getData(&pms), pym_getLen(&pms));
}
void mapteleporter_checkForEntityInRange(mapChannel_t *mapChannel)
{
pyMarshalString_t pms;
sint32 tCount =0;
float minimumRange = 1.8f;
float difX = 0.0f;
float difY = 0.0f;
float difZ = 0.0f;
float dist = 0.0f;
minimumRange *= minimumRange;
//test zoneteleporters map. should be builded from db
sint32 **porting_locs = new sint32*[4];
// values 0-9: source-contextid, source xyz ,dest xyz , dest-contextid, cell-x, cell-z
porting_locs[0] = new sint32 [10]; // zone teleporter #1: wilderness -> divide
porting_locs[0][0] = 1220;
porting_locs[0][1] = 300;
porting_locs[0][2] = 142;
porting_locs[0][3] = -580;
porting_locs[0][4] = -965;
porting_locs[0][5] = 176;
porting_locs[0][6] = 634;
porting_locs[0][7] = 1148;
porting_locs[0][8] = (uint32)((porting_locs[0][1] / CELL_SIZE) + CELL_BIAS);
porting_locs[0][9] = (uint32)((porting_locs[0][3] / CELL_SIZE) + CELL_BIAS);
porting_locs[1] = new sint32 [10]; // zone teleporter #1: divide -> wilderness
porting_locs[1][0] = 1148;
porting_locs[1][1] = -1008;
porting_locs[1][2] = 180;
porting_locs[1][3] = 671;
porting_locs[1][4] = 280;
porting_locs[1][5] = 152;
porting_locs[1][6] = -538;
porting_locs[1][7] = 1220;
porting_locs[1][8] = (uint32)((porting_locs[1][1] / CELL_SIZE) + CELL_BIAS);
porting_locs[1][9] = (uint32)((porting_locs[1][3] / CELL_SIZE) + CELL_BIAS);
porting_locs[2] = new sint32 [10]; //zone zeleporter #2: wilderness -> divide
porting_locs[2][0] = 1220;
porting_locs[2][1] = 891;
porting_locs[2][2] = 268;
porting_locs[2][3] = 32;
porting_locs[2][4] = 436;
porting_locs[2][5] = 173;
porting_locs[2][6] = 1193;
porting_locs[2][7] = 1148;
porting_locs[2][8] = (uint32)((porting_locs[2][1] / CELL_SIZE) + CELL_BIAS);
porting_locs[2][9] = (uint32)((porting_locs[2][3] / CELL_SIZE) + CELL_BIAS);
porting_locs[3] = new sint32 [10]; //zone teleporter #2: divide -> wilderness
porting_locs[3][0] = 1148;
porting_locs[3][1] = 499;
porting_locs[3][2] = 184;
porting_locs[3][3] = 1202;
porting_locs[3][4] = 905;
porting_locs[3][5] = 273;
porting_locs[3][6] = 65;
porting_locs[3][7] = 1220;
porting_locs[3][8] = (uint32)((porting_locs[3][1] / CELL_SIZE) + CELL_BIAS);
porting_locs[3][9] = (uint32)((porting_locs[3][3] / CELL_SIZE) + CELL_BIAS);
//---search through the whole teleporter list
for (sint32 x =0; x < 4; x++)
{
float mPosX = porting_locs[x][1]; //teleporter x-pos
float mPosZ = porting_locs[x][3]; // z-pos
//############ get teleporter mapcell ###################################
mapCell_t *mapCell = cellMgr_tryGetCell(mapChannel,
porting_locs[x][8],
porting_locs[x][9]);
if(mapCell == NULL) continue;
//############ get all players in current celllocation ###################
mapChannelClient_t **playerList = NULL;
tCount = mapCell->ht_playerNotifyList.size();
playerList = &mapCell->ht_playerNotifyList[0];
// check players in range
for(sint32 i=0; i<tCount; i++)
{
if( playerList == NULL) break; //no player found
mapChannelClient_t *player = playerList[i];
if(player->player->actor->stats.healthCurrent<=0) break;
difX = (sint32)(player->player->actor->posX) - mPosX;
difZ = (sint32)(player->player->actor->posZ) - mPosZ;
dist = difX*difX + difZ*difZ;
//player(s) in range: do teleporting
if( (dist <= minimumRange) && (porting_locs[x][0] == player->mapChannel->mapInfo->contextId))
{
mapteleporter_teleportEntity( porting_locs[x][4],
porting_locs[x][5],
porting_locs[x][6],
porting_locs[x][7],
player);
}
}//---for: playercount
}//---for: all teleporter locations
}
void _cb_mapChannel_addNewPlayer(void *param, diJob_characterData_t *jobData)
{
if( jobData->outCharacterData == NULL )
{
// todo: add error handling
return;
}
mapChannelClient_t *mc = (mapChannelClient_t*)param;
mapChannel_t *mapChannel = mc->mapChannel;
// save character data
mc->tempCharacterData = (di_characterData_t*)malloc(sizeof(di_characterData_t));
memcpy(mc->tempCharacterData, jobData->outCharacterData, sizeof(di_characterData_t));
// save seperate mission data (if any)
if( mc->tempCharacterData->missionStateCount )
{
mc->tempCharacterData->missionStateData = (di_CharacterMissionData*)malloc(sizeof(di_CharacterMissionData) * mc->tempCharacterData->missionStateCount);
memcpy(mc->tempCharacterData->missionStateData, jobData->outCharacterData->missionStateData, sizeof(di_CharacterMissionData) * mc->tempCharacterData->missionStateCount);
}
else
{
mc->tempCharacterData->missionStateData = NULL;
}
// add to player to mapChannel (synced)
Thread::LockMutex(&mapChannel->criticalSection);
mapChannel->playerList[mapChannel->playerCount] = mc;
mapChannel->playerCount++;
hashTable_set(&mapChannel->ht_socketToClient, (uint32)mc->cgm->socket, mc);
Thread::UnlockMutex(&mapChannel->criticalSection);
}
void mapChannel_addNewPlayer(mapChannel_t *mapChannel, clientGamemain_t *cgm)
{
mapChannelClient_t *mc = (mapChannelClient_t*)malloc(sizeof(mapChannelClient_t));
memset((void*)mc, 0x00, sizeof(mapChannelClient_t));
mc->cgm = cgm;
mc->clientEntityId = entityMgr_getFreeEntityIdForClient(); // generate a entityId for the client instance
mc->mapChannel = mapChannel;
mc->player = NULL;
DataInterface_Character_getCharacterData(cgm->userID, cgm->mapLoadSlotId, _cb_mapChannel_addNewPlayer, mc);
// register mapChannelClient
entityMgr_registerEntity(mc->clientEntityId, mc);
//// add to the serverlist
//if( mapChannel->playerCount == mapChannel->playerLimit )
//{
// printf("TODO#addNewPlayer\n");
// return;
//}
//mapChannel->playerList[mapChannel->playerCount] = mc;
//mapChannel->playerCount++;
//hashTable_set(&mapChannel->ht_socketToClient, (uint32)cgm->socket, mc);
//// create new actor...
//
//// void DataInterface_Character_getCharacterData(unsigned long long userID, uint32 slotIndex, void (*cb)(void *param, diJob_characterData_t *jobData), void *param)
}
void mapChannel_removePlayer(mapChannelClient_t *client)
{
// unregister mapChannelClient
entityMgr_unregisterEntity(client->clientEntityId);
communicator_unregisterPlayer(client);
Thread::LockMutex(&client->cgm->cs_general);
cellMgr_removeFromWorld(client);
manifestation_removePlayerCharacter(client->mapChannel, client);
if( client->disconnected == false )
GameMain_PassClientToCharacterSelection(client->cgm);
// remove from list
for(sint32 i=0; i<client->mapChannel->playerCount; i++)
{
if( client == client->mapChannel->playerList[i] )
{
if( i == client->mapChannel->playerCount-1 )
{
client->mapChannel->playerCount--;
}
else
{
client->mapChannel->playerList[i] = client->mapChannel->playerList[client->mapChannel->playerCount-1];
client->mapChannel->playerCount--;
}
break;
}
}
// delete data
Thread::UnlockMutex(&client->cgm->cs_general);
free(client->cgm);
free(client);
}
void mapChannel_registerTimer(mapChannel_t *mapChannel, sint32 period, void *param, bool (*cb)(mapChannel_t *mapChannel, void *param, sint32 timePassed))
{
mapChannelTimer_t *timer = (mapChannelTimer_t*)malloc(sizeof(mapChannelTimer_t));
timer->period = period;
timer->timeLeft = period;
timer->param = param;
timer->cb = cb;
mapChannel->timerList.push_back(timer);
}
void mapChannel_launchMissileForWeapon(mapChannelClient_t* client, item_t* weapon)
{
switch(weapon->itemTemplate->toolType)
{
case 9:
if(weapon->itemTemplate->classId == 29395)
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 120, 1, 287);
else
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 40, 1, 121);
break;
case 10:
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 90, 1, 6);
break;
case 8:
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 15, 1, 133);
break;
case 7:
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 25, 1, 134);
break;
case 15:
if(weapon->itemTemplate->classId == 29757)
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 20, 149, 7);
else
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 20, 149, 1);
break;
case 22:
missile_launch(client->mapChannel, client->player->actor, client->player->targetEntityId, 20, 1, 3);
break;
default:
printf("unknown weapontype \n");
return;
}
}
void mapChannel_registerAutoFireTimer(mapChannel_t *mapChannel, sint32 delay, manifestation_t* origin, item_t* weapon)
{
mapChannelAutoFireTimer_t timer;
timer.delay = delay;
timer.timeLeft = delay;
timer.origin = origin;
timer.weapon = weapon;
mapChannel->autoFire_timers.push_back(timer);
// launch missile
mapChannel_launchMissileForWeapon(origin->actor->owner, weapon);
}
void mapChannel_removeAutoFireTimer(mapChannel_t* mapChannel, manifestation_t* origin)
{
std::vector<mapChannelAutoFireTimer_t>::iterator timer = mapChannel->autoFire_timers.begin();
while (timer != mapChannel->autoFire_timers.end())
{
if (timer->origin == origin)
{
timer = mapChannel->autoFire_timers.erase(timer);
}
else { ++timer; }
}
}
void mapChannel_check_AutoFireTimers(mapChannel_t* mapChannel)
{
std::vector<mapChannelAutoFireTimer_t>::iterator timer;
for(timer = mapChannel->autoFire_timers.begin(); timer < mapChannel->autoFire_timers.end(); timer++)
{
timer->timeLeft -= 100;
if (timer->timeLeft <= 0)
{
if (timer->origin->actor->inCombatMode == false)
{ continue; /* TODO: delete timer here */ }
if (timer->origin->targetEntityId)
{
mapChannel_launchMissileForWeapon(timer->origin->actor->owner, timer->weapon);
}
timer->timeLeft = timer->delay;
}
}
}
//20110827 @dennton
bool CheckTempCharacter(di_characterData_t *tcd)
{
bool valid = true;
if(tcd == NULL) valid = false;
if(tcd->missionStateData == NULL) valid = false;
return valid;
}
void mapChannel_recv_mapLoaded(mapChannelClient_t *cm, uint8 *pyString, sint32 pyStringLen)
{
manifestation_createPlayerCharacter(cm->mapChannel, cm, cm->tempCharacterData);
communicator_registerPlayer(cm);
communicator_playerEnterMap(cm);
inventory_initForClient(cm);
mission_initForClient(cm);
// free temporary character data
if( CheckTempCharacter(cm->tempCharacterData) != 0 )// 20110827 @dennton
{
if( cm->tempCharacterData->missionStateData )
free(cm->tempCharacterData->missionStateData);
free(cm->tempCharacterData);
}
}
void mapChannel_recv_LogoutRequest(mapChannelClient_t *cm, uint8 *pyString, sint32 pyStringLen)
{
pyMarshalString_t pms;
// send time remaining to logout
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, 0*1000); // milliseconds
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, 5, METHODID_LOGOUTTIMEREMAINING, pym_getData(&pms), pym_getLen(&pms));
cm->logoutRequestedLast = GetTickCount();
cm->logoutActive = true;
}
void mapChannel_recv_CharacterLogout(mapChannelClient_t *cm, uint8 *pyString, sint32 pyStringLen)
{
pyMarshalString_t pms;
// pass to character selection
if( cm->logoutActive == false )
return;
cm->removeFromMap = true;
}
void mapChannel_recv_ClearTrackingTarget(mapChannelClient_t *cm, uint8 *pyString, sint32 pyStringLen)
{
pyMarshalString_t pms;
// send new tracking target
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addLong(&pms, 0); // tracking target - none
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, cm->player->actor->entityId, METHODID_SETTRACKINGTARGET, pym_getData(&pms), pym_getLen(&pms));
}
void mapChannel_recv_SetTrackingTarget(mapChannelClient_t *cm, uint8 *pyString, sint32 pyStringLen)
{
// unpack new tracking target
pyUnmarshalString_t pums;
pym_init(&pums, pyString, pyStringLen);
if( !pym_unpackTuple_begin(&pums) )
return;
long long trackingTargetEntityId = pym_unpackLongLong(&pums);
// send new tracking target
pyMarshalString_t pms;
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addLong(&pms, trackingTargetEntityId); // tracking target
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, cm->player->actor->entityId, METHODID_SETTRACKINGTARGET, pym_getData(&pms), pym_getLen(&pms));
}
void mapChannel_processPythonRPC(mapChannelClient_t *cm, uint32 methodID, uint8 *pyString, sint32 pyStringLen)
{
// check if 'O'
if( *pyString != 'O' )
__debugbreak(); // oh shit...
pyString++; pyStringLen--;
switch( methodID )
{
case METHODID_ALLOCATEATTRIBUTEPOINTS:
manifestation_recv_AllocateAttributePoints(cm, pyString, pyStringLen);
return;
case METHODID_LEVELSKILLS:
manifestation_recv_LevelSkills(cm, pyString, pyStringLen);
return;
case METHODID_WHISPER: // Whisper
communicator_recv_whisper(cm, pyString, pyStringLen);
return;
case 43: // ClearTargetId
manifestation_recv_ClearTargetId(cm, pyString, pyStringLen);
return;
case METHODID_RADIALCHAT: // RadialChat
communicator_recv_radialChat(cm, pyString, pyStringLen);
return;
case METHODID_MAPLOADED: // MapLoaded
mapChannel_recv_mapLoaded(cm, pyString, pyStringLen);
return;
case 129: // Ping
// todo
return;
case METHODID_REQUESTTOGGLERUN: // ToggleRun
manifestation_recv_ToggleRun(cm, pyString, pyStringLen);
return;
case 201: // SetTargetId
manifestation_recv_SetTargetId(cm, pyString, pyStringLen);
return;
case METHODID_CLEARTRACKINGTARGET:
mapChannel_recv_ClearTrackingTarget(cm, pyString, pyStringLen);
return;
case METHODID_SETTRACKINGTARGET:
mapChannel_recv_SetTrackingTarget(cm, pyString, pyStringLen);
return;
case METHODID_SHOUT: // Shout
communicator_recv_shout(cm, pyString, pyStringLen);
return;
case 343: // RequestPerformAbility
manifestation_recv_RequestPerformAbility(cm, pyString, pyStringLen);
return;
case METHODID_REQUESTUSEOBJECT: // RequestUseObject
dynamicObject_recv_RequestUseObject(cm, pyString, pyStringLen);
return;
case 407: //AssignNPCMission
npc_recv_AssignNPCMission(cm, pyString, pyStringLen);
return;
case METHODID_PERSONALINVENTORY_MOVEITEM: // PersonalInventory_MoveItem
item_recv_PersonalInventoryMoveItem(cm, pyString, pyStringLen);
return;
case 506: // RequestArmAbility
manifestation_recv_RequestArmAbility(cm, pyString, pyStringLen);
return;
case 507: // RequestArmWeapon
item_recv_RequestArmWeapon(cm, pyString, pyStringLen);
return;
case METHODID_REQUESTEQUIPARMOR: // RequestEquipArmor
item_recv_RequestEquipArmor(cm, pyString, pyStringLen);
return;
case METHODID_REQUESTEQUIPWEAPON: // RequestEquipWeapon
item_recv_RequestEquipWeapon(cm, pyString, pyStringLen);
return;
case 518: // RequestNPCConverse
npc_recv_RequestNPCConverse(cm, pyString, pyStringLen);
return;
case METHODID_REQUESTNPCVENDING: // RequestNPCVending
npc_recv_RequestNPCVending(cm, pyString, pyStringLen);
return;
case 522: // RequestSetAbilitySlot
manifestation_recv_RequestSetAbilitySlot(cm, pyString, pyStringLen);
return;
case 530: // RequestWeaponDraw
item_recv_RequestWeaponDraw(cm, pyString, pyStringLen);
return;
case 531: // RequestWeaponReload
item_recv_RequestWeaponReload(cm, pyString, pyStringLen);
return;
case 532: // RequestWeaponStow
item_recv_RequestWeaponStow(cm, pyString, pyStringLen);
return;
case 549: // StartAutoFire
manifestation_recv_StartAutoFire(cm, pyString, pyStringLen);
return;
case 550: // StopAutoFire
manifestation_recv_StopAutoFire(cm, pyString, pyStringLen);
return;
case 410: // AutoFireKeepAlive
manifestation_recv_AutoFireKeepAlive(cm, pyString, pyStringLen);
return;
case 573: // WeaponDrawerInventory_MoveItem
printf("TODO: WeaponDrawerInventory MoveItem\n");
return;
case METHODID_REQUESTTOOLTIPFORITEMTEMPLATEID: // RequestTooltipForItemTemplateId
item_recv_RequestTooltipForItemTemplateId(cm, pyString, pyStringLen);
return;
case 753: // RequestVisualCombatMode
printf("VisualCombatMode:\n");
HexOut(pyString, pyStringLen);
printf("\n\n");
manifestation_recv_RequestVisualCombatMode(cm, pyString, pyStringLen);
return;
case 759: // RequestActioninterrupt
dynamicObject_recv_RequestActionInterrupt(cm, pyString, pyStringLen);
return;
case METHODID_REQUESTLOGOUT: // RequestLogout
mapChannel_recv_LogoutRequest(cm, pyString, pyStringLen);
return;
case METHODID_CHANNELCHAT: // ChannelChat
communicator_recv_channelChat(cm, pyString, pyStringLen);
return;
case METHODID_CHARACTERLOGOUT: // CharacterLogout
mapChannel_recv_CharacterLogout(cm, pyString, pyStringLen);
return;
case METHODID_REQUESTWEAPONATTACK://player melee
if(inventory_CurrentWeapon(cm)->itemTemplate->classId == 27220)
missile_launch(cm->mapChannel, cm->player->actor, cm->player->targetEntityId, 20, 174, 5); // rifle
else if(inventory_CurrentWeapon(cm)->itemTemplate->classId == 27320)
missile_launch(cm->mapChannel, cm->player->actor, cm->player->targetEntityId, 20, 174, 6); // shotgun
else if(inventory_CurrentWeapon(cm)->itemTemplate->classId == 28066)
missile_launch(cm->mapChannel, cm->player->actor, cm->player->targetEntityId, 20, 174, 3); // machinegun
else
missile_launch(cm->mapChannel, cm->player->actor, cm->player->targetEntityId, 20, 174, 4); // pistol
return;
case METHODID_REVIVEME: // dead player wish to go to the hospital
manifestation_recv_Revive(cm, pyString, pyStringLen);
return;
case METHODID_SELECTWAYPOINT: // waypoint selected
waypoint_recv_SelectWaypoint(cm, pyString, pyStringLen);
return;
default:
printf("MapChannel_UnknownMethodID: %d\n", methodID);
printf("MapChannel_RPCPacket - Size: %d\n", pyStringLen);
HexOut(pyString, pyStringLen);
printf("\n\n");
return;
// no handler for that
};
// 149
// 00001AA7 64 - LOAD_CONST 'RequestCharacterName'
return;
}
#pragma pack(1)
typedef struct
{
sint32 contextId;
sint32 pX;
sint32 pY;
sint32 pZ;
}movementLogEntry_t;
#pragma pack()
HANDLE hMovementLogFile = NULL;
void mapChannel_logMovement(sint32 contextId, sint32 x, sint32 y, sint32 z)
{
return;
if( hMovementLogFile == NULL )
{
hMovementLogFile = CreateFile("movementlog.bin", FILE_ALL_ACCESS, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
SetFilePointer(hMovementLogFile, 0, 0, FILE_END);
}
// setup entry
movementLogEntry_t entry;
entry.contextId = contextId;
entry.pX = x;
entry.pY = y;
entry.pZ = z;
// write entry
DWORD bytesWritten;
WriteFile(hMovementLogFile, (LPCVOID)&entry, sizeof(movementLogEntry_t), &bytesWritten, NULL);
}
void mapChannel_decodeMovementPacket(mapChannelClient_t *mc, uint8 *data, uint32 len)
{
if( mc->removeFromMap )
return;
if( mc->player == NULL )
return;
netCompressedMovement_t netMovement;
uint32 pIdx = 0;
uint32 counterA = *(uint32*)(data+pIdx); pIdx += 4;
uint32 ukn1 = *(uint32*)(data+pIdx); pIdx += 4;
uint32 counterB = *(uint32*)(data+pIdx); pIdx += 4;
if( data[pIdx] != 2 )
__debugbreak();
pIdx++;
if( data[pIdx] != 6 ) // subOpcode?
__debugbreak();
pIdx++;
// skip unknown
pIdx += 10;
// for performance we actually dont parse the packet
// posX
if( data[pIdx] != 0x29 )
__debugbreak();
if( data[pIdx+7] != 0x2A )
__debugbreak();
sint32 val24b = (data[pIdx+2]<<16) | (data[pIdx+4]<<8) | (data[pIdx+6]);
if( val24b&0x00800000 )
val24b |= 0xFF000000;
netMovement.posX24b = val24b;
float posX = (float)val24b / 256.0f;
sint32 vLogX = val24b;
mc->player->actor->posX = posX;
pIdx += 8;
// posY
if( data[pIdx] != 0x29 )
__debugbreak();
if( data[pIdx+7] != 0x2A )
__debugbreak();
val24b = (data[pIdx+2]<<16) | (data[pIdx+4]<<8) | (data[pIdx+6]);
if( val24b&0x00800000 )
val24b |= 0xFF000000;
netMovement.posY24b = val24b;
float posY = (float)val24b / 256.0f;
sint32 vLogY = val24b;
mc->player->actor->posY = posY;
pIdx += 8;
// posZ
if( data[pIdx] != 0x29 )
__debugbreak();
if( data[pIdx+7] != 0x2A )
__debugbreak();
val24b = (data[pIdx+2]<<16) | (data[pIdx+4]<<8) | (data[pIdx+6]);
if( val24b&0x00800000 )
val24b |= 0xFF000000;
netMovement.posZ24b = val24b;
float posZ = (float)val24b / 256.0f;
sint32 vLogZ = val24b;
mc->player->actor->posZ = posZ;
pIdx += 8;
// read velocity
//29 05 00 1A 2A velocity? /1024.0
if( data[pIdx] != 0x29 )
__debugbreak();
val24b = *(sint16*)(data+pIdx+2);
netMovement.velocity = val24b;
float velocity = (float)val24b / 1024.0f;
if( data[pIdx+4] != 0x2A )
__debugbreak();
pIdx += 5;
mapChannel_logMovement(mc->mapChannel->mapInfo->contextId, vLogX, vLogY, vLogZ);
// read flag
netMovement.flag = *(uint8*)(data+pIdx+1);
pIdx += 2;
// read viewX, viewY
if( data[pIdx] != 0x29 )
__debugbreak();
val24b = *(sint16*)(data+pIdx+2);
netMovement.viewX = val24b;
float viewX = (float)val24b / 1024.0f; // factor guessed ??? find real
val24b = *(sint16*)(data+pIdx+5);
netMovement.viewY = val24b;
float viewY = (float)val24b / 1024.0f; // factor guessed ???
/*
03 08
29 05 04 66 05 62 08 2A ???
2A
2A 31
*/
netMovement.entityId = mc->player->actor->entityId;
//netMgr_broadcastEntityMovement(mc->mapChannel, &netMovement, true);
netMgr_cellDomain_sendEntityMovement(mc, &netMovement, true);
// void netMgr_broadcastEntityMovement(mapChannel_t *broadCastChannel, netCompressedMovement_t *movement, bool skipOwner)
// prsint32 info
//printf("move %f %f %f v: %f rXY: %f %f\n", posX, posY, posZ, velocity, viewX, viewY);
}
sint32 mapChannel_decodePacket(mapChannelClient_t *mc, uint8 *data, uint32 len)
{
if( mc->removeFromMap )
return 1;
if( len >= 0xFFFF )
__debugbreak();
if( len < 4 )
return 0;
if( len >= 0xFFFF )
__debugbreak();
if( len < 4 )
return 0;
sint32 pIdx = 0;
// read subSize
uint32 subSize = *(uint16*)(data+pIdx); pIdx += 2; // redundancy with param len
// read major opcode
uint32 majorOpc = *(uint16*)(data+pIdx); pIdx += 2;
if( majorOpc == 1 )
{
mapChannel_decodeMovementPacket(mc, data+pIdx, subSize-pIdx);
return 1;
}
else if( majorOpc )
{
return 1; // ignore the packet
}
// read header A
uint8 ukn1 = *(uint8*)(data+pIdx); pIdx +=1;
if( ukn1 != 2 )
__debugbreak();
uint8 opcode = *(uint8*)(data+pIdx); pIdx +=1; // not 100% sure
uint8 ukn2 = *(uint8*)(data+pIdx); pIdx +=1;
if( ukn2 != 0 )
__debugbreak();
uint8 xorCheckA = *(uint8*)(data+pIdx); pIdx +=1;
if( xorCheckA != 3 ) // we only know headerA length of 3 for now
__debugbreak();
uint32 hdrB_start = pIdx;
uint8 ukn3 = *(uint8*)(data+pIdx); pIdx +=1;
if( ukn3 != 3 )
__debugbreak();
// different handling now (dont support subOpc 2 here anymore)
if( opcode == 0x0C )
{
// expect header B part 1 (0x29)
if( *(uint8*)(data+pIdx) == 0x00 )
return 1; // empty packet?
if( *(uint8*)(data+pIdx) != 0x29 )
__debugbreak(); // wrong
pIdx++;
uint8 ukn0C_1 = *(uint8*)(data+pIdx); pIdx++;
if( ukn0C_1 != 3 ) __debugbreak();
uint8 ukn0C_2 = *(uint8*)(data+pIdx); pIdx++;
//if( ukn0C_2 != 1 && ukn0C_2 != 3 && ukn0C_2 != 9 ) __debugbdfdsfreak(); // server entityId?
if( ukn0C_2 == 0 || ukn0C_2 > 0x10 ) __debugbreak(); // server entityId?
uint8 preffix0C_1 = *(uint8*)(data+pIdx); pIdx++;
if( preffix0C_1 != 7 ) __debugbreak(); // 7 --> 32-bit sint32
uint32 methodID = *(uint32*)(data+pIdx); pIdx += 4;
uint8 ukn0C_3 = *(uint8*)(data+pIdx); pIdx++; // entityID?
if( ukn0C_3 != 1 ) __debugbreak();
// part 2 (0xCB)
if( *(uint8*)(data+pIdx) != 0xCB )
__debugbreak(); // wrong
pIdx++;
uint32 dataLen = 0;
uint32 lenMask = *(uint8*)(data+pIdx); pIdx++;
if( (lenMask>>6) == 0 )
{
// 6 bit length
dataLen = lenMask&0x3F;
}
else if( (lenMask>>6) == 1 )
{
// 14 bit length
dataLen = (lenMask&0x3F);
dataLen |= ((*(uint8*)(data+pIdx))<<6);
pIdx++;
}
else
__debugbreak();
mapChannel_processPythonRPC(mc, methodID, data+pIdx, dataLen);
pIdx += dataLen;
// xor check...
}
else
return 1;
return 1;
}
void mapChannel_readData(mapChannelClient_t *mc)
{
// todo: disconnect client on error...
clientGamemain_t *cgm = mc->cgm;
if( cgm->RecvState < 4 )
{
sint32 r = recv(cgm->socket, (char*)cgm->RecvBuffer+cgm->RecvState, 4-cgm->RecvState, 0);
if( r == 0 || r == SOCKET_ERROR )
{
mc->removeFromMap = true;
mc->disconnected = true;
return;
}
cgm->RecvState += r;
if( cgm->RecvState == 4 )
cgm->RecvSize = *(uint32*)cgm->RecvBuffer + 4;
return;
}
sint32 r = recv(cgm->socket, (char*)cgm->RecvBuffer+cgm->RecvState, cgm->RecvSize-cgm->RecvState, 0);
if( r == 0 || r == SOCKET_ERROR )
{
mc->removeFromMap = true;
mc->disconnected = true;
return;
}
cgm->RecvState += r;
if( cgm->RecvState == cgm->RecvSize )
{
// full packet received
// everything is encrypted, so do decryption job here
Tabula_Decrypt2(&cgm->tbc2, (uint32*)(cgm->RecvBuffer+4), cgm->RecvSize);
sint32 r = 0;
sint32 AlignBytes = cgm->RecvBuffer[4]%9;
uint8 *Buffer = cgm->RecvBuffer + 4 + AlignBytes;
sint32 Size = cgm->RecvSize - 4 - AlignBytes;
do{
uint16 Subsize = *(uint16*)Buffer;
mapChannel_decodePacket(mc, Buffer, Subsize);
Buffer += Subsize;
Size -= Subsize;
}while(Size > 0);
cgm->RecvState = 0;
return;
}
return;
}
sint32 mapChannel_worker(mapChannelList_t *channelList)
{
FD_SET fd;
timeval sTimeout;
sTimeout.tv_sec = 0;
sTimeout.tv_usec = 10000;
global_channelList = channelList; //20110827 @dennton
// init mapchannel (map instance)
printf("Initializing MapChannel...\n");
for(sint32 chan=0; chan<channelList->mapChannelCount; chan++)
{
mapChannel_t *mapChannel = channelList->mapChannelArray+chan;
if( cellMgr_initForMapChannel(mapChannel) == false )
{
printf("Error on map-cell init in mapContextId %d\n", mapChannel->mapInfo->contextId);
Sleep(5000);
ExitThread(-1);
}
navmesh_initForMapChannel(mapChannel);
dynamicObject_init(mapChannel);
mission_initForChannel(mapChannel);
npc_initForMapChannel(mapChannel); //---db use
missile_initForMapchannel(mapChannel);
spawnPool_initForMapChannel(mapChannel); //---todo:db use -done
controller_initForMapChannel(mapChannel);
teleporter_initForMapChannel(mapChannel); //---load teleporters
printf(" Map: [%s]\n",mapChannel->mapInfo->name);
}
printf("MapChannel started...\n");
while( true )
{
for(sint32 chan=0; chan<channelList->mapChannelCount; chan++)
{
mapChannel_t *mapChannel = channelList->mapChannelArray+chan;
// check for new players in queue (one per round)
if( mapChannel->rb_playerQueueReadIndex != mapChannel->rb_playerQueueWriteIndex )
{
mapChannel_addNewPlayer(mapChannel, mapChannel->rb_playerQueue[mapChannel->rb_playerQueueReadIndex]);
mapChannel->rb_playerQueueReadIndex = (mapChannel->rb_playerQueueReadIndex+1)%MAPCHANNEL_PLAYERQUEUE;
}
// recv client data
FD_ZERO(&fd);
for(sint32 i=0; i<mapChannel->playerCount; i++)
{
FD_SET(mapChannel->playerList[i]->cgm->socket, &fd);
}
sint32 r = select(0, &fd, 0, 0, &sTimeout);
if( r )
{
for(sint32 i=0; i<fd.fd_count; i++)
{
mapChannelClient_t *mc = (mapChannelClient_t*)hashTable_get(&mapChannel->ht_socketToClient, (uint32)fd.fd_array[i]);
if( mc )
mapChannel_readData(mc);
else
{
continue;
}
if( mc->removeFromMap )
{
communicator_playerExitMap(mc);
mapChannel_removePlayer(mc);
}
}
}
if (mapChannel->playerCount > 0)
{
// do other work
cellMgr_doWork(mapChannel);
// check timers
uint32 currentTime = GetTickCount();
if( (currentTime - mapChannel->timer_clientEffectUpdate) >= 500 )
{
gameEffect_checkForPlayers(mapChannel->playerList, mapChannel->playerCount, 500);
mapChannel->timer_clientEffectUpdate += 500;
}
//if (mapChannel->cp_trigger.cb != NULL)
//{
// if ((currentTime - mapChannel->cp_trigger.period) >= 100)
// {
// mapChannel->cp_trigger.timeLeft -= 100;
// mapChannel->cp_trigger.period = currentTime;
// if (mapChannel->cp_trigger.timeLeft <= 0)
// {
// mapChannel->cp_trigger.cb(mapChannel, mapChannel->cp_trigger.param, 1);
// mapChannel->cp_trigger.cb = NULL;
// }
// }
//}
if( (currentTime - mapChannel->timer_missileUpdate) >= 100 )
{
missile_check(mapChannel, 100);
mapChannel->timer_missileUpdate += 100;