-
Notifications
You must be signed in to change notification settings - Fork 134
/
field.cpp
3628 lines (3622 loc) · 127 KB
/
field.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
/*
* field.cpp
*
* Created on: 2010-7-21
* Author: Argon
*/
#include "field.h"
#include "duel.h"
#include "card.h"
#include "group.h"
#include "effect.h"
#include "interpreter.h"
#include <cstring>
bool check_playerid(int32 playerid) {
return playerid >= 0 && playerid <= 1;
}
int32 field::field_used_count[32] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5};
bool chain::chain_operation_sort(const chain& c1, const chain& c2) {
auto e1 = c1.triggering_effect;
auto e2 = c2.triggering_effect;
if (e1 && e2) {
if (e1->handler == e2->handler && e1->description != e2->description)
return e1->description < e2->description;
if (e1->id != e2->id)
return e1->id < e2->id;
}
return c1.chain_id < c2.chain_id;
}
void chain::set_triggering_state(card* pcard) {
triggering_controler = pcard->current.controler;
if(pcard->current.is_location(LOCATION_FZONE))
triggering_location = LOCATION_SZONE | LOCATION_FZONE;
else if(pcard->current.is_location(LOCATION_PZONE))
triggering_location = LOCATION_SZONE | LOCATION_PZONE;
else
triggering_location = pcard->current.location;
triggering_sequence = pcard->current.sequence;
triggering_position = pcard->current.position;
triggering_state.code = pcard->get_code();
triggering_state.code2 = pcard->get_another_code();
triggering_state.level = pcard->get_level();
triggering_state.rank = pcard->get_rank();
triggering_state.attribute = pcard->get_attribute();
triggering_state.race = pcard->get_race();
std::pair<int32, int32> atk_def = pcard->get_atk_def();
triggering_state.attack = atk_def.first;
triggering_state.defense = atk_def.second;
}
bool tevent::operator< (const tevent& v) const {
return std::memcmp(this, &v, sizeof(tevent)) < 0;
}
field::field(duel* pduel) {
this->pduel = pduel;
for (int32 i = 0; i < 2; ++i) {
player[i].list_mzone.resize(7, 0);
player[i].list_szone.resize(8, 0);
player[i].list_main.reserve(60);
player[i].list_hand.reserve(60);
player[i].list_grave.reserve(75);
player[i].list_remove.reserve(75);
player[i].list_extra.reserve(30);
}
}
void field::reload_field_info() {
pduel->write_buffer8(MSG_RELOAD_FIELD);
pduel->write_buffer8(core.duel_rule);
for(int32 playerid = 0; playerid < 2; ++playerid) {
pduel->write_buffer32(player[playerid].lp);
for(auto& pcard : player[playerid].list_mzone) {
if(pcard) {
pduel->write_buffer8(1);
pduel->write_buffer8(pcard->current.position);
pduel->write_buffer8((uint8)pcard->xyz_materials.size());
} else {
pduel->write_buffer8(0);
}
}
for(auto& pcard : player[playerid].list_szone) {
if(pcard) {
pduel->write_buffer8(1);
pduel->write_buffer8(pcard->current.position);
} else {
pduel->write_buffer8(0);
}
}
pduel->write_buffer8((uint8)player[playerid].list_main.size());
pduel->write_buffer8((uint8)player[playerid].list_hand.size());
pduel->write_buffer8((uint8)player[playerid].list_grave.size());
pduel->write_buffer8((uint8)player[playerid].list_remove.size());
pduel->write_buffer8((uint8)player[playerid].list_extra.size());
pduel->write_buffer8((uint8)player[playerid].extra_p_count);
}
pduel->write_buffer8((uint8)core.current_chain.size());
for(const auto& ch : core.current_chain) {
effect* peffect = ch.triggering_effect;
pduel->write_buffer32(peffect->get_handler()->data.code);
pduel->write_buffer32(peffect->get_handler()->get_info_location());
pduel->write_buffer8(ch.triggering_controler);
pduel->write_buffer8((uint8)ch.triggering_location);
pduel->write_buffer8(ch.triggering_sequence);
pduel->write_buffer32(peffect->description);
}
}
void field::add_card(uint8 playerid, card* pcard, uint8 location, uint8 sequence, uint8 pzone) {
if (pcard->current.location != 0)
return;
if (!is_location_useable(playerid, location, sequence))
return;
if(pcard->is_extra_deck_monster() && (location & (LOCATION_HAND | LOCATION_DECK))) {
location = LOCATION_EXTRA;
pcard->sendto_param.position = POS_FACEDOWN_DEFENSE;
}
pcard->current.controler = playerid;
pcard->current.location = location;
switch (location) {
case LOCATION_MZONE: {
player[playerid].list_mzone[sequence] = pcard;
pcard->current.sequence = sequence;
break;
}
case LOCATION_SZONE: {
player[playerid].list_szone[sequence] = pcard;
pcard->current.sequence = sequence;
break;
}
case LOCATION_DECK: {
if (sequence == SEQ_DECKTOP) {
player[playerid].list_main.push_back(pcard);
pcard->current.sequence = (uint8)player[playerid].list_main.size() - 1;
} else if (sequence == SEQ_DECKBOTTOM) {
player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard);
reset_sequence(playerid, LOCATION_DECK);
} else { // SEQ_DECKSHUFFLE
if(core.duel_options & DUEL_RETURN_DECK_TOP) {
player[playerid].list_main.push_back(pcard);
pcard->current.sequence = (uint8)player[playerid].list_main.size() - 1;
}
else {
player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard);
reset_sequence(playerid, LOCATION_DECK);
}
if(!core.shuffle_check_disabled)
core.shuffle_deck_check[playerid] = TRUE;
}
pcard->sendto_param.position = POS_FACEDOWN;
break;
}
case LOCATION_HAND: {
player[playerid].list_hand.push_back(pcard);
pcard->current.sequence = (uint8)player[playerid].list_hand.size() - 1;
uint32 pos = pcard->is_affected_by_effect(EFFECT_PUBLIC) ? POS_FACEUP : POS_FACEDOWN;
pcard->sendto_param.position = pos;
if(!(pcard->current.reason & REASON_DRAW) && !core.shuffle_check_disabled)
core.shuffle_hand_check[playerid] = TRUE;
break;
}
case LOCATION_GRAVE: {
player[playerid].list_grave.push_back(pcard);
pcard->current.sequence = (uint8)player[playerid].list_grave.size() - 1;
break;
}
case LOCATION_REMOVED: {
player[playerid].list_remove.push_back(pcard);
pcard->current.sequence = (uint8)player[playerid].list_remove.size() - 1;
break;
}
case LOCATION_EXTRA: {
if(player[playerid].extra_p_count == 0 || (pcard->data.type & TYPE_PENDULUM) && (pcard->sendto_param.position & POS_FACEUP))
player[playerid].list_extra.push_back(pcard);
else
player[playerid].list_extra.insert(player[playerid].list_extra.end() - player[playerid].extra_p_count, pcard);
if((pcard->data.type & TYPE_PENDULUM) && (pcard->sendto_param.position & POS_FACEUP))
++player[playerid].extra_p_count;
reset_sequence(playerid, LOCATION_EXTRA);
break;
}
}
if(pzone)
pcard->current.pzone = true;
else
pcard->current.pzone = false;
pcard->apply_field_effect();
pcard->fieldid = infos.field_id++;
pcard->fieldid_r = pcard->fieldid;
if(check_unique_onfield(pcard, pcard->current.controler, pcard->current.location))
pcard->unique_fieldid = UINT_MAX;
pcard->turnid = infos.turn_id;
refresh_player_info(playerid);
}
void field::remove_card(card* pcard) {
if (!check_playerid(pcard->current.controler) || pcard->current.location == 0)
return;
uint8 playerid = pcard->current.controler;
switch (pcard->current.location) {
case LOCATION_MZONE:
player[playerid].list_mzone[pcard->current.sequence] = 0;
break;
case LOCATION_SZONE:
player[playerid].list_szone[pcard->current.sequence] = 0;
break;
case LOCATION_DECK:
player[playerid].list_main.erase(player[playerid].list_main.begin() + pcard->current.sequence);
reset_sequence(playerid, LOCATION_DECK);
if(!core.shuffle_check_disabled)
core.shuffle_deck_check[playerid] = TRUE;
break;
case LOCATION_HAND:
pcard->set_status(STATUS_TO_HAND_WITHOUT_CONFIRM, FALSE);
player[playerid].list_hand.erase(player[playerid].list_hand.begin() + pcard->current.sequence);
reset_sequence(playerid, LOCATION_HAND);
break;
case LOCATION_GRAVE:
player[playerid].list_grave.erase(player[playerid].list_grave.begin() + pcard->current.sequence);
reset_sequence(playerid, LOCATION_GRAVE);
break;
case LOCATION_REMOVED:
player[playerid].list_remove.erase(player[playerid].list_remove.begin() + pcard->current.sequence);
reset_sequence(playerid, LOCATION_REMOVED);
break;
case LOCATION_EXTRA:
player[playerid].list_extra.erase(player[playerid].list_extra.begin() + pcard->current.sequence);
reset_sequence(playerid, LOCATION_EXTRA);
if((pcard->data.type & TYPE_PENDULUM) && (pcard->current.position & POS_FACEUP))
--player[playerid].extra_p_count;
break;
}
pcard->cancel_field_effect();
refresh_player_info(playerid);
pcard->previous.controler = pcard->current.controler;
pcard->previous.location = pcard->current.location;
pcard->previous.sequence = pcard->current.sequence;
pcard->previous.position = pcard->current.position;
pcard->previous.pzone = pcard->current.pzone;
pcard->current.controler = PLAYER_NONE;
pcard->current.location = 0;
pcard->current.sequence = 0;
}
void field::move_card(uint8 playerid, card* pcard, uint8 location, uint8 sequence, uint8 pzone) {
if (!is_location_useable(playerid, location, sequence))
return;
uint8 preplayer = pcard->current.controler;
uint8 presequence = pcard->current.sequence;
if(pcard->is_extra_deck_monster() && (location & (LOCATION_HAND | LOCATION_DECK))) {
location = LOCATION_EXTRA;
pcard->sendto_param.position = POS_FACEDOWN_DEFENSE;
}
if (pcard->current.location) {
if (pcard->current.location == location && pcard->current.pzone == !!pzone) {
if (pcard->current.location == LOCATION_DECK) {
if(preplayer == playerid) {
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard->data.code);
pduel->write_buffer32(pcard->get_info_location());
player[preplayer].list_main.erase(player[preplayer].list_main.begin() + pcard->current.sequence);
if (sequence == SEQ_DECKTOP) {
player[playerid].list_main.push_back(pcard);
} else if (sequence == SEQ_DECKBOTTOM) {
player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard);
} else { // SEQ_DECKSHUFFLE
if(core.duel_options & DUEL_RETURN_DECK_TOP)
player[playerid].list_main.push_back(pcard);
else
player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard);
if(!core.shuffle_check_disabled)
core.shuffle_deck_check[playerid] = TRUE;
}
reset_sequence(playerid, LOCATION_DECK);
pcard->previous.controler = preplayer;
pcard->current.controler = playerid;
pduel->write_buffer32(pcard->get_info_location());
pduel->write_buffer32(pcard->current.reason);
return;
} else
remove_card(pcard);
} else if(location & LOCATION_ONFIELD) {
if (playerid == preplayer && sequence == presequence)
return;
if(location == LOCATION_MZONE) {
if(sequence >= (int32)player[playerid].list_mzone.size() || player[playerid].list_mzone[sequence])
return;
} else {
if(sequence >= player[playerid].szone_size || player[playerid].list_szone[sequence])
return;
}
if(preplayer == playerid) {
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard->data.code);
pduel->write_buffer32(pcard->get_info_location());
}
pcard->previous.controler = pcard->current.controler;
pcard->previous.location = pcard->current.location;
pcard->previous.sequence = pcard->current.sequence;
pcard->previous.position = pcard->current.position;
pcard->previous.pzone = pcard->current.pzone;
if (location == LOCATION_MZONE) {
player[preplayer].list_mzone[presequence] = 0;
player[playerid].list_mzone[sequence] = pcard;
pcard->current.controler = playerid;
pcard->current.sequence = sequence;
} else {
player[preplayer].list_szone[presequence] = 0;
player[playerid].list_szone[sequence] = pcard;
pcard->current.controler = playerid;
pcard->current.sequence = sequence;
}
if(preplayer == playerid) {
refresh_player_info(playerid);
pduel->write_buffer32(pcard->get_info_location());
pduel->write_buffer32(pcard->current.reason);
} else {
refresh_player_info(preplayer);
refresh_player_info(playerid);
pcard->fieldid = infos.field_id++;
if(check_unique_onfield(pcard, pcard->current.controler, pcard->current.location))
pcard->unique_fieldid = UINT_MAX;
}
return;
} else if(location == LOCATION_HAND) {
if(preplayer == playerid)
return;
remove_card(pcard);
} else {
if(location == LOCATION_GRAVE) {
if(pcard->current.sequence == player[pcard->current.controler].list_grave.size() - 1)
return;
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard->data.code);
pduel->write_buffer32(pcard->get_info_location());
player[pcard->current.controler].list_grave.erase(player[pcard->current.controler].list_grave.begin() + pcard->current.sequence);
player[pcard->current.controler].list_grave.push_back(pcard);
reset_sequence(pcard->current.controler, LOCATION_GRAVE);
pduel->write_buffer32(pcard->get_info_location());
pduel->write_buffer32(pcard->current.reason);
} else if(location == LOCATION_REMOVED) {
if(pcard->current.sequence == player[pcard->current.controler].list_remove.size() - 1)
return;
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard->data.code);
pduel->write_buffer32(pcard->get_info_location());
player[pcard->current.controler].list_remove.erase(player[pcard->current.controler].list_remove.begin() + pcard->current.sequence);
player[pcard->current.controler].list_remove.push_back(pcard);
reset_sequence(pcard->current.controler, LOCATION_REMOVED);
pduel->write_buffer32(pcard->get_info_location());
pduel->write_buffer32(pcard->current.reason);
} else {
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard->data.code);
pduel->write_buffer32(pcard->get_info_location());
player[pcard->current.controler].list_extra.erase(player[pcard->current.controler].list_extra.begin() + pcard->current.sequence);
player[pcard->current.controler].list_extra.push_back(pcard);
reset_sequence(pcard->current.controler, LOCATION_EXTRA);
pduel->write_buffer32(pcard->get_info_location());
pduel->write_buffer32(pcard->current.reason);
}
return;
}
} else {
if((pcard->data.type & TYPE_PENDULUM) && (location == LOCATION_GRAVE)
&& pcard->is_capable_send_to_extra(playerid)
&& (((pcard->current.location == LOCATION_MZONE) && !pcard->is_status(STATUS_SUMMON_DISABLED))
|| ((pcard->current.location == LOCATION_SZONE) && !pcard->is_status(STATUS_ACTIVATE_DISABLED)))) {
location = LOCATION_EXTRA;
pcard->sendto_param.position = POS_FACEUP_DEFENSE;
}
remove_card(pcard);
}
}
add_card(playerid, pcard, location, sequence, pzone);
}
void field::swap_card(card* pcard1, card* pcard2, uint8 new_sequence1, uint8 new_sequence2) {
uint8 p1 = pcard1->current.controler, p2 = pcard2->current.controler;
uint8 l1 = pcard1->current.location, l2 = pcard2->current.location;
uint8 s1 = pcard1->current.sequence, s2 = pcard2->current.sequence;
uint32 info1 = pcard1->get_info_location(), info2 = pcard2->get_info_location();
if(!(l1 & LOCATION_ONFIELD) || !(l2 & LOCATION_ONFIELD))
return;
if(new_sequence1 != s1 && !is_location_useable(p1, l1, new_sequence1)
|| new_sequence2 != s2 && !is_location_useable(p2, l2, new_sequence2))
return;
if(p1 == p2 && l1 == l2 && (new_sequence1 == s2 || new_sequence2 == s1))
return;
if(l1 == l2) {
pcard1->previous.controler = p1;
pcard1->previous.location = l1;
pcard1->previous.sequence = s1;
pcard1->previous.position = pcard1->current.position;
pcard1->previous.pzone = pcard1->current.pzone;
pcard1->current.controler = p2;
pcard1->current.location = l2;
pcard1->current.sequence = new_sequence2;
pcard2->previous.controler = p2;
pcard2->previous.location = l2;
pcard2->previous.sequence = s2;
pcard2->previous.position = pcard2->current.position;
pcard2->previous.pzone = pcard2->current.pzone;
pcard2->current.controler = p1;
pcard2->current.location = l1;
pcard2->current.sequence = new_sequence1;
if(p1 != p2) {
pcard1->fieldid = infos.field_id++;
pcard2->fieldid = infos.field_id++;
if(check_unique_onfield(pcard1, pcard1->current.controler, pcard1->current.location))
pcard1->unique_fieldid = UINT_MAX;
if(check_unique_onfield(pcard2, pcard2->current.controler, pcard2->current.location))
pcard2->unique_fieldid = UINT_MAX;
}
if(l1 == LOCATION_MZONE) {
player[p1].list_mzone[s1] = 0;
player[p2].list_mzone[s2] = 0;
player[p2].list_mzone[new_sequence2] = pcard1;
player[p1].list_mzone[new_sequence1] = pcard2;
refresh_player_info(p1);
refresh_player_info(p2);
} else if(l1 == LOCATION_SZONE) {
player[p1].list_szone[s1] = 0;
player[p2].list_szone[s2] = 0;
player[p2].list_szone[new_sequence2] = pcard1;
player[p1].list_szone[new_sequence1] = pcard2;
refresh_player_info(p1);
refresh_player_info(p2);
}
} else {
remove_card(pcard1);
remove_card(pcard2);
add_card(p2, pcard1, l2, new_sequence2);
add_card(p1, pcard2, l1, new_sequence1);
}
if(s1 == new_sequence1 && s2 == new_sequence2) {
pduel->write_buffer8(MSG_SWAP);
pduel->write_buffer32(pcard1->data.code);
pduel->write_buffer32(info1);
pduel->write_buffer32(pcard2->data.code);
pduel->write_buffer32(info2);
} else if(s1 == new_sequence1) {
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard1->data.code);
pduel->write_buffer32(info1);
pduel->write_buffer32(pcard1->get_info_location());
pduel->write_buffer32(0);
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard2->data.code);
pduel->write_buffer32(info2);
pduel->write_buffer32(pcard2->get_info_location());
pduel->write_buffer32(0);
} else {
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard2->data.code);
pduel->write_buffer32(info2);
pduel->write_buffer32(pcard2->get_info_location());
pduel->write_buffer32(0);
pduel->write_buffer8(MSG_MOVE);
pduel->write_buffer32(pcard1->data.code);
pduel->write_buffer32(info1);
pduel->write_buffer32(pcard1->get_info_location());
pduel->write_buffer32(0);
}
}
void field::swap_card(card* pcard1, card* pcard2) {
return swap_card(pcard1, pcard2, pcard1->current.sequence, pcard2->current.sequence);
}
void field::set_control(card* pcard, uint8 playerid, uint16 reset_phase, uint8 reset_count) {
if((core.remove_brainwashing && pcard->is_affected_by_effect(EFFECT_REMOVE_BRAINWASHING)) || std::get<uint8>(pcard->refresh_control_status()) == playerid)
return;
effect* peffect = pduel->new_effect();
if(core.reason_effect)
peffect->owner = core.reason_effect->get_handler();
else
peffect->owner = pcard;
peffect->handler = pcard;
peffect->type = EFFECT_TYPE_SINGLE;
peffect->code = EFFECT_SET_CONTROL;
peffect->value = playerid;
peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE;
peffect->reset_flag = RESET_EVENT | 0xc6c0000;
if(reset_count) {
peffect->reset_flag |= RESET_PHASE | reset_phase;
if(!(peffect->reset_flag & (RESET_SELF_TURN | RESET_OPPO_TURN)))
peffect->reset_flag |= (RESET_SELF_TURN | RESET_OPPO_TURN);
peffect->reset_count = reset_count;
}
pcard->add_effect(peffect);
pcard->current.controler = playerid;
}
int32 field::get_pzone_sequence(uint8 pseq) const {
if (core.duel_rule >= NEW_MASTER_RULE) {
if(!pseq)
return 0;
else
return 4;
}
else {
if (!pseq)
return 6;
else
return 7;
}
}
card* field::get_field_card(uint8 playerid, uint32 general_location, uint8 sequence) const {
if (!check_playerid(playerid))
return nullptr;
switch(general_location) {
case LOCATION_MZONE: {
if(sequence < (int32)player[playerid].list_mzone.size())
return player[playerid].list_mzone[sequence];
else
return nullptr;
break;
}
case LOCATION_SZONE: {
if(sequence < player[playerid].szone_size)
return player[playerid].list_szone[sequence];
else
return nullptr;
break;
}
case LOCATION_FZONE: {
if(sequence == 0)
return player[playerid].list_szone[5];
else
return nullptr;
break;
}
case LOCATION_PZONE: {
if(sequence == 0 || sequence == 1) {
card* pcard = player[playerid].list_szone[get_pzone_sequence(sequence)];
return (pcard && pcard->current.pzone) ? pcard : nullptr;
} else
return nullptr;
break;
}
case LOCATION_DECK: {
if(sequence < player[playerid].list_main.size())
return player[playerid].list_main[sequence];
else
return nullptr;
break;
}
case LOCATION_HAND: {
if(sequence < player[playerid].list_hand.size())
return player[playerid].list_hand[sequence];
else
return nullptr;
break;
}
case LOCATION_GRAVE: {
if(sequence < player[playerid].list_grave.size())
return player[playerid].list_grave[sequence];
else
return nullptr;
break;
}
case LOCATION_REMOVED: {
if(sequence < player[playerid].list_remove.size())
return player[playerid].list_remove[sequence];
else
return nullptr;
break;
}
case LOCATION_EXTRA: {
if(sequence < player[playerid].list_extra.size())
return player[playerid].list_extra[sequence];
else
return nullptr;
break;
}
}
return nullptr;
}
int32 field::is_location_useable(uint8 playerid, uint32 general_location, uint8 sequence) const {
if (!check_playerid(playerid))
return FALSE;
uint32 flag = player[playerid].disabled_location | player[playerid].used_location;
if (general_location == LOCATION_MZONE) {
if (sequence >= (int32)player[playerid].list_mzone.size())
return FALSE;
if(flag & (0x1U << sequence))
return FALSE;
if(sequence >= 5) {
uint32 oppo = player[1 - playerid].disabled_location | player[1 - playerid].used_location;
if(oppo & (0x1U << (11 - sequence)))
return FALSE;
}
} else if (general_location == LOCATION_SZONE) {
if (sequence >= player[playerid].szone_size)
return FALSE;
if(flag & (0x100U << sequence))
return FALSE;
} else if (general_location == LOCATION_FZONE) {
if (sequence >= 1)
return FALSE;
if(flag & (0x100U << (5 + sequence)))
return FALSE;
} else if (general_location == LOCATION_PZONE) {
if (sequence >= 2)
return FALSE;
if (flag & (0x100U << get_pzone_sequence(sequence)))
return FALSE;
}
return TRUE;
}
/**
* Return usable count in `zone` when `uplayer` moves `pcard` to the MZONE or SZONE(0~4) of `playerid` (can be negative).
* For LOCATION_MZONE, "usable" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE, satisfying EFFECT_MAX_MZONE.
* For LOCATION_SZONE, "usable" means not used, not disabled, satisfying EFFECT_MAX_SZONE.
*
* @param pcard the card about to move
* @param playerid target player
* @param location LOCATION_MZONE or LOCATION_SZONE
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer, ignoring EFFECT_MAX_MZONE, EFFECT_MAX_SZONE of playerid
* @param reason location reason
* @param zone specified zones, default: 0xff
* @param list storing unavailable or unspecified zones
*
* @return usable count in the MZONE or SZONE(0~4) of `playerid` (can be negative)
*/
int32 field::get_useable_count(card* pcard, uint8 playerid, uint8 location, uint8 uplayer, uint32 reason, uint32 zone, uint32* list) {
if(location == LOCATION_MZONE && pcard && pcard->current.location == LOCATION_EXTRA)
return get_useable_count_fromex(pcard, playerid, uplayer, zone, list);
else
return get_useable_count_other(pcard, playerid, location, uplayer, reason, zone, list);
}
/**
* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck)
* @return usable count in the MZONE of `playerid`
*/
int32 field::get_useable_count_fromex(card* pcard, uint8 playerid, uint8 uplayer, uint32 zone, uint32* list) {
bool use_temp_card = false;
if(!pcard) {
use_temp_card = true;
pcard = temp_card;
pcard->current.location = LOCATION_EXTRA;
}
int32 useable_count = 0;
if(core.duel_rule >= NEW_MASTER_RULE)
useable_count = get_useable_count_fromex_rule4(pcard, playerid, uplayer, zone, list);
else
useable_count = get_useable_count_other(pcard, playerid, LOCATION_MZONE, uplayer, LOCATION_REASON_TOFIELD, zone, list);
if(use_temp_card)
pcard->current.location = 0;
return useable_count;
}
/**
* @return the number of available slots in `zone` when `pcard` is sp_summoned to the MZONE of `playerid`
* For LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE.
*/
int32 field::get_spsummonable_count(card* pcard, uint8 playerid, uint32 zone, uint32* list) {
if(pcard->current.location == LOCATION_EXTRA)
return get_spsummonable_count_fromex(pcard, playerid, playerid, zone, list);
else
return get_tofield_count(pcard, playerid, LOCATION_MZONE, playerid, LOCATION_REASON_TOFIELD, zone, list);
}
/**
* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck)
* @return the number of available slots in `zone` when `pcard` is sp_summoned to the MZONE of `playerid` by `uplayer`
*/
int32 field::get_spsummonable_count_fromex(card* pcard, uint8 playerid, uint8 uplayer, uint32 zone, uint32* list) {
bool use_temp_card = false;
if(!pcard) {
use_temp_card = true;
pcard = temp_card;
pcard->current.location = LOCATION_EXTRA;
}
int32 spsummonable_count = 0;
if(core.duel_rule >= NEW_MASTER_RULE)
spsummonable_count = get_spsummonable_count_fromex_rule4(pcard, playerid, uplayer, zone, list);
else
spsummonable_count = get_tofield_count(pcard, playerid, LOCATION_MZONE, uplayer, LOCATION_REASON_TOFIELD, zone, list);
if(use_temp_card)
pcard->current.location = 0;
return spsummonable_count;
}
/**
* @return usable count in `zone` of Main MZONE or SZONE(0~4)
*/
int32 field::get_useable_count_other(card* pcard, uint8 playerid, uint8 location, uint8 uplayer, uint32 reason, uint32 zone, uint32* list) {
int32 count = get_tofield_count(pcard, playerid, location, uplayer, reason, zone, list);
int32 limit;
if(location == LOCATION_MZONE)
limit = get_mzone_limit(playerid, uplayer, reason);
else
limit = get_szone_limit(playerid, uplayer, reason);
if(count > limit)
count = limit;
return count;
}
/**
* @return the number of available slots in `zone` of Main MZONE or SZONE(0~4)
* for LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE
* for LOCATION_SZONE, "available" means not used, not disabled
*/
int32 field::get_tofield_count(card* pcard, uint8 playerid, uint8 location, uint32 uplayer, uint32 reason, uint32 zone, uint32* list) {
if (location != LOCATION_MZONE && location != LOCATION_SZONE)
return 0;
uint32 flag = player[playerid].disabled_location | player[playerid].used_location;
if(location == LOCATION_MZONE && (reason & LOCATION_REASON_TOFIELD)) {
filter_must_use_mzone(playerid, uplayer, reason, pcard, &flag);
}
if (location == LOCATION_MZONE)
flag = (flag | ~zone) & 0x1f;
else
flag = ((flag >> 8) | ~zone) & 0x1f;
int32 count = 5 - field_used_count[flag];
if(location == LOCATION_MZONE)
flag |= (1u << 5) | (1u << 6);
if(list)
*list = flag;
return count;
}
int32 field::get_useable_count_fromex_rule4(card* pcard, uint8 playerid, uint8 uplayer, uint32 zone, uint32* list) {
int32 count = get_spsummonable_count_fromex_rule4(pcard, playerid, uplayer, zone, list);
int32 limit = get_mzone_limit(playerid, uplayer, LOCATION_REASON_TOFIELD);
if(count > limit)
count = limit;
return count;
}
int32 field::get_spsummonable_count_fromex_rule4(card* pcard, uint8 playerid, uint8 uplayer, uint32 zone, uint32* list) {
uint32 flag = player[playerid].disabled_location | player[playerid].used_location;
filter_must_use_mzone(playerid, uplayer, LOCATION_REASON_TOFIELD, pcard, &flag);
if(player[playerid].list_mzone[5] && is_location_useable(playerid, LOCATION_MZONE, 6)
&& check_extra_link(playerid, pcard, 6)) {
flag |= 1u << 5;
} else if(player[playerid].list_mzone[6] && is_location_useable(playerid, LOCATION_MZONE, 5)
&& check_extra_link(playerid, pcard, 5)) {
flag |= 1u << 6;
} else if(player[playerid].list_mzone[5] || player[playerid].list_mzone[6]) {
flag |= (1u << 5) | (1u << 6);
} else {
if(!is_location_useable(playerid, LOCATION_MZONE, 5))
flag |= 1u << 5;
if(!is_location_useable(playerid, LOCATION_MZONE, 6))
flag |= 1u << 6;
}
uint32 rule_zone = get_rule_zone_fromex(playerid, pcard);
flag = flag | ~zone | ~rule_zone;
if(list)
*list = flag & 0x7f;
int32 count = 5 - field_used_count[flag & 0x1f];
if(~flag & ((1u << 5) | (1u << 6)))
++count;
return count;
}
/**
* @param playerid target player
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MAX_MZONE
* @param reason location reason
*
* @return the remaining count in the MZONE of `playerid` after applying EFFECT_MAX_MZONE (can be negative).
*/
int32 field::get_mzone_limit(uint8 playerid, uint8 uplayer, uint32 reason) {
uint32 used_flag = player[playerid].used_location;
used_flag = used_flag & 0x1f;
int32 max = 5;
int32 used_count = field_used_count[used_flag];
if(core.duel_rule >= NEW_MASTER_RULE) {
max = 7;
if(player[playerid].list_mzone[5])
++used_count;
if(player[playerid].list_mzone[6])
++used_count;
}
effect_set eset;
if(uplayer < 2)
filter_player_effect(playerid, EFFECT_MAX_MZONE, &eset);
for(int32 i = 0; i < eset.size(); ++i) {
pduel->lua->add_param(playerid, PARAM_TYPE_INT);
pduel->lua->add_param(uplayer, PARAM_TYPE_INT);
pduel->lua->add_param(reason, PARAM_TYPE_INT);
int32 v = eset[i]->get_value(3);
if(max > v)
max = v;
}
int32 limit = max - used_count;
return limit;
}
/**
* @param playerid target player
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MAX_SZONE
* @param reason location reason
*
* @return the remaining count in the SZONE(0~4) of `playerid` after applying EFFECT_MAX_SZONE.
*/
int32 field::get_szone_limit(uint8 playerid, uint8 uplayer, uint32 reason) {
uint32 used_flag = player[playerid].used_location;
used_flag = (used_flag >> 8) & 0x1f;
effect_set eset;
if(uplayer < 2)
filter_player_effect(playerid, EFFECT_MAX_SZONE, &eset);
int32 max = 5;
for(int32 i = 0; i < eset.size(); ++i) {
pduel->lua->add_param(playerid, PARAM_TYPE_INT);
pduel->lua->add_param(uplayer, PARAM_TYPE_INT);
pduel->lua->add_param(reason, PARAM_TYPE_INT);
int32 v = eset[i]->get_value(3);
if(max > v)
max = v;
}
int32 limit = max - field_used_count[used_flag];
return limit;
}
uint32 field::get_linked_zone(int32 playerid) {
uint32 zones = 0;
for(auto& pcard : player[playerid].list_mzone) {
if(pcard)
zones |= pcard->get_linked_zone() & 0xff;
}
for(auto& pcard : player[1 - playerid].list_mzone) {
if(pcard)
zones |= pcard->get_linked_zone() >> 16;
}
return zones;
}
uint32 field::get_rule_zone_fromex(int32 playerid, card* pcard) {
if(core.duel_rule >= NEW_MASTER_RULE) {
if(core.duel_rule >= MASTER_RULE_2020 && pcard && (pcard->data.type & (TYPE_FUSION | TYPE_SYNCHRO | TYPE_XYZ))
&& (pcard->is_position(POS_FACEDOWN) || !(pcard->data.type & TYPE_PENDULUM)))
return 0x7f;
else
return get_linked_zone(playerid) | (1u << 5) | (1u << 6);
} else {
return 0x1f;
}
}
/**
* @param playerid target player
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer
* @param reason location reason
* @param pcard the card about to move
* @param flag storing the zones in MZONE blocked by EFFECT_MUST_USE_MZONE
*/
void field::filter_must_use_mzone(uint8 playerid, uint8 uplayer, uint32 reason, card* pcard, uint32* flag) {
effect_set eset;
if(uplayer < 2)
filter_player_effect(uplayer, EFFECT_MUST_USE_MZONE, &eset);
if(pcard)
pcard->filter_effect(EFFECT_MUST_USE_MZONE, &eset);
for(int32 i = 0; i < eset.size(); ++i) {
if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) && eset[i]->count_limit == 0)
continue;
uint32 value = 0x1f;
if(eset[i]->is_flag(EFFECT_FLAG_PLAYER_TARGET))
value = eset[i]->get_value();
else {
pduel->lua->add_param(playerid, PARAM_TYPE_INT);
pduel->lua->add_param(uplayer, PARAM_TYPE_INT);
pduel->lua->add_param(reason, PARAM_TYPE_INT);
value = eset[i]->get_value(pcard, 3);
}
if(eset[i]->get_handler_player() == playerid)
*flag |= ~value & 0x7f;
else
*flag |= ~(value >> 16) & 0x7f;
}
}
void field::get_linked_cards(uint8 self, uint8 s, uint8 o, card_set* cset) {
cset->clear();
uint8 c = s;
for(int32 p = 0; p < 2; ++p) {
if(c) {
uint32 linked_zone = get_linked_zone(self);
get_cards_in_zone(cset, linked_zone, self, LOCATION_MZONE);
}
self = 1 - self;
c = o;
}
}
int32 field::check_extra_link(int32 playerid) {
if(!player[playerid].list_mzone[5] || !player[playerid].list_mzone[6])
return FALSE;
card* pcard = player[playerid].list_mzone[5];
uint32 checked = 1u << 5;
uint32 linked_zone = pcard->get_mutual_linked_zone();
while(true) {
if((linked_zone >> 6) & 1)
return TRUE;
int32 checking = (int32)(linked_zone & ~checked);
if(!checking)
return FALSE;
int32 rightmost = checking & (-checking);
checked |= (uint32)rightmost;
if(rightmost < 0x10000) {
for(int32 i = 0; i < 7; ++i) {
if(rightmost & 1) {
pcard = player[playerid].list_mzone[i];
linked_zone |= pcard->get_mutual_linked_zone();
break;
}
rightmost >>= 1;
}
} else {
rightmost >>= 16;
for(int32 i = 0; i < 7; ++i) {
if(rightmost & 1) {
pcard = player[1 - playerid].list_mzone[i];
uint32 zone = pcard->get_mutual_linked_zone();
linked_zone |= (zone << 16) | (zone >> 16);
break;
}
rightmost >>= 1;
}
}
}
return FALSE;
}
int32 field::check_extra_link(int32 playerid, card* pcard, int32 sequence) {
if(!pcard)
return FALSE;
if(player[playerid].list_mzone[sequence])
return FALSE;
uint8 cur_controler = pcard->current.controler;
uint8 cur_location = pcard->current.location;
uint8 cur_sequence = pcard->current.sequence;
player[playerid].list_mzone[sequence] = pcard;
pcard->current.controler = playerid;
pcard->current.location = LOCATION_MZONE;
pcard->current.sequence = sequence;
int32 ret = check_extra_link(playerid);
player[playerid].list_mzone[sequence] = 0;
pcard->current.controler = cur_controler;
pcard->current.location = cur_location;
pcard->current.sequence = cur_sequence;
return ret;
}
void field::get_cards_in_zone(card_set* cset, uint32 zone, int32 playerid, int32 location) {
if(!(location & LOCATION_ONFIELD))
return;
card_vector& svector = (location == LOCATION_MZONE) ? player[playerid].list_mzone : player[playerid].list_szone;
uint32 icheck = 0x1;
for(auto& pcard : svector) {
if(zone & icheck) {
if(pcard)
cset->insert(pcard);
}
icheck <<= 1;
}
}
void field::shuffle(uint8 playerid, uint8 location) {
if(!(location & (LOCATION_HAND | LOCATION_DECK | LOCATION_EXTRA)))
return;
card_vector& svector = (location == LOCATION_HAND) ? player[playerid].list_hand : (location == LOCATION_DECK) ? player[playerid].list_main : player[playerid].list_extra;
if(svector.size() == 0)
return;
if(location == LOCATION_HAND) {
bool shuffle = false;
for(auto& pcard : svector)
if(!pcard->is_position(POS_FACEUP))
shuffle = true;
if(!shuffle) {
core.shuffle_hand_check[playerid] = FALSE;
return;
}
}
if(location == LOCATION_HAND || !(core.duel_options & DUEL_PSEUDO_SHUFFLE)) {
int32 s = (int32)svector.size();
if(location == LOCATION_EXTRA)
s = s - (int32)player[playerid].extra_p_count;
if(s > 1) {
if (core.duel_options & DUEL_OLD_REPLAY)
pduel->random.shuffle_vector_old(svector, 0, s - 1);
else
pduel->random.shuffle_vector(svector, 0, s - 1);
reset_sequence(playerid, location);
}
}
if(location == LOCATION_HAND || location == LOCATION_EXTRA) {
pduel->write_buffer8((location == LOCATION_HAND) ? MSG_SHUFFLE_HAND : MSG_SHUFFLE_EXTRA);
pduel->write_buffer8(playerid);
pduel->write_buffer8((uint8)svector.size());
for(auto& pcard : svector)
pduel->write_buffer32(pcard->data.code);
if(location == LOCATION_HAND) {
core.shuffle_hand_check[playerid] = FALSE;
for(auto& pcard : svector) {
for(auto& i : pcard->indexer) {
effect* peffect = i.first;
if(peffect->is_flag(EFFECT_FLAG_CLIENT_HINT) && !peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET)) {
pduel->write_buffer8(MSG_CARD_HINT);
pduel->write_buffer32(pcard->get_info_location());
pduel->write_buffer8(CHINT_DESC_ADD);
pduel->write_buffer32(peffect->description);
}
}
}
}
} else {
pduel->write_buffer8(MSG_SHUFFLE_DECK);
pduel->write_buffer8(playerid);
core.shuffle_deck_check[playerid] = FALSE;
if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) {
card* ptop = svector.back();
if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) {
pduel->write_buffer8(MSG_DECK_TOP);
pduel->write_buffer8(playerid);
pduel->write_buffer8(0);
if(ptop->current.position != POS_FACEUP_DEFENSE)
pduel->write_buffer32(ptop->data.code);