-
Notifications
You must be signed in to change notification settings - Fork 1
/
singlemode.cpp
executable file
·840 lines (825 loc) · 31.3 KB
/
singlemode.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
#include <QDebug>
#include <QEventLoop>
#include <time.h>
#include "game.h"
#include "singlemode.h"
#include "mtrandom.h"
#include "ocgapi.h"
#include "datamanager.h"
#include "bufferio.h"
#include "field.h"
#include "duelclient.h"
namespace glaze {
long SingleMode::pduel = 0;
bool SingleMode::is_closing = false;
bool SingleMode::is_continuing = false;
wchar_t SingleMode::event_string[256];
int SingleMode::enable_log = 2;
QString SingleMode::name = "";
SingleMode::SingleMode()
{
}
void SingleMode::SetResponse(unsigned char* resp) {
if(!pduel)
return;
set_responseb(pduel, resp);
}
void SingleMode::singlePlayStart()
{
qDebug()<<"SingleMode run called from?: "<<QThread::currentThreadId();
QString fname("./assets/single/"+name+".lua");
char fname2[256];
QEventLoop loop;
connect(&mainGame->dField, SIGNAL(clearFinished()), &loop, SLOT(quit()));
strcpy(fname2, fname.toStdString().c_str());
size_t slen = fname.length();
qDebug()<<"SingleMode game name after conversion is "<<fname<<" and length is "<<slen;
qDebug()<<"script name is "<<fname2;
mtrandom rnd;
time_t seed = time(0);
rnd.reset(seed);
set_card_reader((card_reader)DataManager::CardReader);
set_message_handler((message_handler)MessageHandler);
pduel = create_duel(rnd.rand());
set_player_info(pduel, 0, 8000, 5, 1);
set_player_info(pduel, 1, 8000, 5, 1);
qDebug()<<"card info set. Duel created in ocgcore";
mainGame->dInfo.lp[0] = 8000;
mainGame->dInfo.lp[1] = 8000;
// myswprintf(mainGame->dInfo.strLP[0], L"%d", mainGame->dInfo.lp[0]);
// myswprintf(mainGame->dInfo.strLP[1], L"%d", mainGame->dInfo.lp[1]);
// BufferIO::CopyWStr(mainGame->ebNickName->getText(), mainGame->dInfo.hostname, 20);
mainGame->dInfo.clientname[0] = 0;
mainGame->dInfo.turn = 0;
// mainGame->dInfo.strTurn[0] = 0;
qDebug()<<"Duel info set";
if(!preload_script(pduel, fname2, slen)) { //loading the lua script for puzzle in ocgcore
end_duel(pduel);
qDebug()<<"SingleMode script load ERROR";
return ;
}
qDebug()<<"SingleMode game script loaded successfully";
//Initialze the field to start the game
mainGame->dInfo.is_first = true;
mainGame->dInfo.is_started = true;
mainGame->dInfo.is_singleMode = true;
mainGame->dField.hovered_card = 0;
mainGame->dField.clicked_card = 0;
// mainGame->dField.Clear();
QMetaObject::invokeMethod(&mainGame->dField,"Clear",Qt::QueuedConnection);
loop.exec();
start_duel(pduel, 0);
char engineBuffer[0x1000]; //size = 4096
is_closing = false;
is_continuing = true;
int len = 0;
while (is_continuing) {
int result = process(pduel); //function from ocgcore
len = result & 0xffff;
/* int flag = result >> 16; */
if (len > 0) {
get_message(pduel, (byte*)engineBuffer); //function from ocgcore which return array containing commands of current duel process
is_continuing = SinglePlayAnalyze(engineBuffer, len); // the commands from above function are processed here
}
}
// mainGame->dField.Clear();
QMetaObject::invokeMethod(&mainGame->dField,"Clear",Qt::QueuedConnection);
loop.exec();
qDebug()<<"SingleMode game ended";
end_duel(pduel);
if(is_closing) {
//Game ended; do the necessary resetting
mainGame->dInfo.is_started = false;
mainGame->dInfo.is_singleMode = false;
}
emit finished();
}
bool SingleMode::SinglePlayAnalyze(char* msg, unsigned int len)
{
char* offset, *pbuf = msg;
int player, count;
QEventLoop loop;
connect(&mainGame->dField, SIGNAL(clearFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(addCardFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayRefreshFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayRefreshHandFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayRefreshGraveFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayRefreshDeckFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayRefreshExtraFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayRefreshSingleFinished()), &loop, SLOT(quit()));
connect(&mainGame->dField, SIGNAL(singlePlayReloadFinished()), &loop, SLOT(quit()));
while (pbuf - msg < (int)len) {
if(is_closing || !is_continuing)
return false;
offset = pbuf;
mainGame->dInfo.curMsg = BufferIO::ReadUInt8(pbuf);
qDebug()<<"CURRENT DUEL MESSAGE IS "<<mainGame->dInfo.curMsg;
switch (mainGame->dInfo.curMsg) {
case MSG_RETRY: {
qDebug()<<"MSG_RETRY entered";
mainGame->wMessage = true;
mainGame->stMessage = "Error occured.";
emit mainGame->qwMessageChanged();
mainGame->actionSignal.reset();
mainGame->actionSignal.wait();
return false;
}
case MSG_HINT: {
/*int type = */BufferIO::ReadInt8(pbuf);
int player = BufferIO::ReadInt8(pbuf);
/*int data = */BufferIO::ReadInt32(pbuf);
if(player == 0)
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_WIN: {
pbuf += 2;
DuelClient::ClientAnalyze(offset, pbuf - offset);
return false;
}
case MSG_SELECT_BATTLECMD: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 11;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 8 + 2;
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_IDLECMD: {
qDebug()<<"MSG_SELECT_IDLECMD enter";
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 11 + 2;
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
qDebug()<<"MSG_SELECT_IDLECMD exit";
break;
}
case MSG_SELECT_EFFECTYN: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_YESNO: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 4;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_OPTION: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 4;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_CARD:
case MSG_SELECT_TRIBUTE: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 3;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 8;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_CHAIN: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += 10 + count * 12;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_PLACE:
case MSG_SELECT_DISFIELD: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 5;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_POSITION: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 5;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_COUNTER: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 3;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 8;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SELECT_SUM: {
pbuf++;
player = BufferIO::ReadInt8(pbuf);
pbuf += 6;
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 11;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_SORT_CARD:
case MSG_SORT_CHAIN: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_CONFIRM_DECKTOP: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CONFIRM_CARDS: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 7;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SHUFFLE_DECK: {
player = BufferIO::ReadInt8(pbuf);
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefreshDeck(player);
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshDeck",Qt::QueuedConnection,
Q_ARG(int, player));
loop.exec();
break;
}
case MSG_SHUFFLE_HAND: {
/*int oplayer = */BufferIO::ReadInt8(pbuf);
int count = BufferIO::ReadInt8(pbuf);
pbuf += count * 4;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_REFRESH_DECK: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SWAP_GRAVE_DECK: {
player = BufferIO::ReadInt8(pbuf);
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefreshGrave(player);
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshGrave",Qt::QueuedConnection,
Q_ARG(int, player));
loop.exec();
break;
}
case MSG_REVERSE_DECK: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_DECK_TOP: {
pbuf += 6;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SHUFFLE_SET_CARD: {
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_NEW_TURN: {
player = BufferIO::ReadInt8(pbuf);
DuelClient::ClientAnalyze(offset, pbuf - offset); //TODO: causing crash
break;
}
case MSG_NEW_PHASE: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_MOVE: {
int pc = pbuf[4];
int pl = pbuf[5];
/*int ps = pbuf[6];*/
/*int pp = pbuf[7];*/
int cc = pbuf[8];
int cl = pbuf[9];
int cs = pbuf[10];
/*int cp = pbuf[11];*/
pbuf += 16;
DuelClient::ClientAnalyze(offset, pbuf - offset);
if(cl && !(cl & 0x80) && (pl != cl || pc != cc))
// SinglePlayRefreshSingle(cc, cl, cs);
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshSingle",Qt::QueuedConnection,
Q_ARG(int, cc),
Q_ARG(int, cl),
Q_ARG(int, cs));
loop.exec();
break;
}
case MSG_POS_CHANGE: {
pbuf += 9;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SET: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SWAP: {
pbuf += 16;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_FIELD_DISABLED: {
pbuf += 4;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SUMMONING: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SUMMONED: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_SPSUMMONING: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_SPSUMMONED: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_FLIPSUMMONING: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_FLIPSUMMONED: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_CHAINING: {
pbuf += 16;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CHAINED: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_CHAIN_SOLVING: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CHAIN_SOLVED: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_CHAIN_END: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
// SinglePlayRefreshDeck(0);
// SinglePlayRefreshDeck(1);
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshDeck",Qt::QueuedConnection,
Q_ARG(int, 0));
loop.exec();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshDeck",Qt::QueuedConnection,
Q_ARG(int, 1));
loop.exec();
break;
}
case MSG_CHAIN_NEGATED: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CHAIN_DISABLED: {
pbuf++;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CARD_SELECTED:
case MSG_RANDOM_SELECTED: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 4;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_BECOME_TARGET: {
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 4;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_DRAW: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count * 4;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_DAMAGE: {
pbuf += 5;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_RECOVER: {
pbuf += 5;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_EQUIP: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_LPUPDATE: {
pbuf += 5;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_UNEQUIP: {
pbuf += 4;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CARD_TARGET: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_CANCEL_TARGET: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_PAY_LPCOST: {
pbuf += 5;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_ADD_COUNTER: {
pbuf += 6;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_REMOVE_COUNTER: {
pbuf += 6;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_ATTACK: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_BATTLE: {
pbuf += 26;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_ATTACK_DISABLED: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_DAMAGE_STEP_START: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_DAMAGE_STEP_END: {
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefresh();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefresh",Qt::QueuedConnection);
loop.exec();
break;
}
case MSG_MISSED_EFFECT: {
pbuf += 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_TOSS_COIN: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_TOSS_DICE: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += count;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_ANNOUNCE_RACE: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 5;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_ANNOUNCE_ATTRIB: {
player = BufferIO::ReadInt8(pbuf);
pbuf += 5;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_ANNOUNCE_CARD: {
player = BufferIO::ReadInt8(pbuf);
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_ANNOUNCE_NUMBER: {
player = BufferIO::ReadInt8(pbuf);
count = BufferIO::ReadInt8(pbuf);
pbuf += 4 * count;
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.reset();
mainGame->singleSignal.wait();
}
break;
}
case MSG_CARD_HINT: {
pbuf += 9;
DuelClient::ClientAnalyze(offset, pbuf - offset);
break;
}
case MSG_TAG_SWAP: {
player = pbuf[0];
pbuf += pbuf[3] * 4 + 8;
DuelClient::ClientAnalyze(offset, pbuf - offset);
// SinglePlayRefreshDeck(player);
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshDeck",Qt::QueuedConnection,
Q_ARG(int, player));
loop.exec();
// SinglePlayRefreshExtra(player);
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayRefreshExtra",Qt::QueuedConnection,
Q_ARG(int, player));
loop.exec();
break;
}
case MSG_MATCH_KILL: {
pbuf += 4;
break;
}
case MSG_RELOAD_FIELD: {
qDebug()<<"MSG_RELOAD_FIELD enter";
// mainGame->dField.Clear();
QMetaObject::invokeMethod(&mainGame->dField,"Clear",Qt::QueuedConnection);
loop.exec();
int val = 0;
for(int p = 0; p < 2; ++p) {
mainGame->dInfo.lp[p] = BufferIO::ReadInt32(pbuf);
qDebug()<<"Life point of player "<<p+1<<" set to "<<mainGame->dInfo.lp[p];
for(int seq = 0; seq < 5; ++seq) {
val = BufferIO::ReadInt8(pbuf);
if(val) {
ClientCard* ccard = new ClientCard;
ClientCard* tmp = ccard;
// mainGame->dField.AddCard(p, LOCATION_MZONE, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_MZONE),
Q_ARG(int, seq),
Q_ARG(ClientCard*, 0),
Q_ARG(ClientCard**, &ccard));
loop.exec();
delete tmp; // ccard reallocated in the above function
ccard->position = BufferIO::ReadInt8(pbuf);
val = BufferIO::ReadInt8(pbuf);
if(val) {
for(int xyz = 0; xyz < val; ++xyz) {
ClientCard* xcard = new ClientCard;
ccard->overlayed.push_back(xcard);
mainGame->dField.overlay_cards.insert(xcard);
xcard->overlayTarget = ccard;
xcard->location = 0x80;
xcard->sequence = ccard->overlayed.size() - 1;
}
}
}
}
for(int seq = 0; seq < 8; ++seq) {
val = BufferIO::ReadInt8(pbuf);
if(val) {
ClientCard* ccard = new ClientCard;
ClientCard* tmp = ccard;
// mainGame->dField.AddCard(ccard, p, LOCATION_SZONE, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_SZONE),
Q_ARG(int, seq),
Q_ARG(ClientCard*, 0),
Q_ARG(ClientCard**, &ccard));
loop.exec();
delete tmp;
ccard->position = BufferIO::ReadInt8(pbuf);
}
}
val = BufferIO::ReadInt8(pbuf);
for(int seq = 0; seq < val; ++seq) {
// ClientCard* ccard = new ClientCard();
// mainGame->dField.AddCard(ccard, p, LOCATION_DECK, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_DECK),
Q_ARG(int, seq));
loop.exec();
}
val = BufferIO::ReadInt8(pbuf);
for(int seq = 0; seq < val; ++seq) {
// ClientCard* ccard = new ClientCard;
// mainGame->dField.AddCard(ccard, p, LOCATION_HAND, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_HAND),
Q_ARG(int, seq));
loop.exec();
}
val = BufferIO::ReadInt8(pbuf);
for(int seq = 0; seq < val; ++seq) {
// ClientCard* ccard = new ClientCard;
// mainGame->dField.AddCard(ccard, p, LOCATION_GRAVE, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_GRAVE),
Q_ARG(int, seq));
loop.exec();
}
val = BufferIO::ReadInt8(pbuf);
for(int seq = 0; seq < val; ++seq) {
// ClientCard* ccard = new ClientCard;
// mainGame->dField.AddCard(ccard, p, LOCATION_REMOVED, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_REMOVED),
Q_ARG(int, seq));
loop.exec();
}
val = BufferIO::ReadInt8(pbuf);
for(int seq = 0; seq < val; ++seq) {
// ClientCard* ccard = new ClientCard;
// mainGame->dField.AddCard(ccard, p, LOCATION_EXTRA, seq);
QMetaObject::invokeMethod(&mainGame->dField,"AddCard",Qt::QueuedConnection,
Q_ARG(int, p),
Q_ARG(int, LOCATION_EXTRA),
Q_ARG(int, seq));
loop.exec();
}
}
BufferIO::ReadInt8(pbuf); //chain count, always 0
// SinglePlayReload();
QMetaObject::invokeMethod(&mainGame->dField,"SinglePlayReload",Qt::QueuedConnection);
qDebug()<<"SinglePlayReload queued from"<<QThread::currentThreadId();
loop.exec();
// mainGame->dField.RefreshAllCards(); // updates the position and rotation
emit mainGame->dInfo.lp1Changed();
emit mainGame->dInfo.lp2Changed();
qDebug()<<"MSG_RELOAD_FIELD exit";
break;
}
case MSG_AI_NAME: {
char namebuf[128];
wchar_t wname[128];
int len = BufferIO::ReadInt16(pbuf);
char* begin = pbuf;
pbuf += len + 1;
memcpy(namebuf, begin, len + 1);
BufferIO::DecodeUTF8(namebuf, wname);
BufferIO::CopyWStr(wname, mainGame->dInfo.clientname, 20);
emit mainGame->dInfo.clientNameChanged();
break;
}
case MSG_SHOW_HINT: {
char msgbuf[1024];
wchar_t msg[1024];
int len = BufferIO::ReadInt16(pbuf);
char* begin = pbuf;
pbuf += len + 1;
memcpy(msgbuf, begin, len + 1);
BufferIO::DecodeUTF8(msgbuf, msg);
mainGame->stMessage = QString::fromWCharArray(msg);
mainGame->wMessage = true ;
emit mainGame->qwMessageChanged();
mainGame->actionSignal.reset();
mainGame->actionSignal.wait();
break;
}
}
}
return is_continuing;
}
int SingleMode::MessageHandler(long fduel, int type) {
if(!enable_log)
return 0;
char msgbuf[1024];
get_log_message(fduel, (byte*)msgbuf);
if(enable_log == 1) {
wchar_t wbuf[1024];
BufferIO::DecodeUTF8(msgbuf, wbuf);
mainGame->AddChatMsg(wbuf, 9);
} else if(enable_log == 2) {
FILE* fp = fopen("error.log", "at");
if(!fp)
return 0;
fprintf(fp, "[Script error:] %s\n", msgbuf);
fclose(fp);
}
return 0;
}
}