-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.j
1626 lines (1521 loc) · 56.4 KB
/
chess.j
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
globals
rect array cells// 8x8 rect area
unit array pieces//
integer turn = 1
effect array avail
boolean validMove = true
boolean array pawnMoved
boolean p0KingInCheck = false
boolean p1KingInCheck = false
boolean p0KingMoved = false
boolean p1KingMoved = false
boolean p0Rook0Moved = false
boolean p0Rook1Moved = false
boolean p1Rook0Moved = false
boolean p1Rook1Moved = false
boolean p0ShortCastleUnderAttack = false
boolean p0LongCastleUnderAttack = false
boolean p1ShortCastleUnderAttack = false
boolean p1LongCastleUnderAttack = false
integer array attackingSquare
unit attack0CastleUnit = null
unit attack1CastleUnit = null
integer cellSubOfAttack0CastleUnit = -1
integer cellSubOfAttack1CastleUnit = -1
unit upgradeUnit
location upgradeLocation
integer upgradePlayer = 0
dialog chooseUpgrade = DialogCreate()
button btnRook = DialogAddButton(chooseUpgrade, "Rook", 0)
button btnKnight = DialogAddButton(chooseUpgrade, "Knight", 0)
button btnBishop = DialogAddButton(chooseUpgrade, "Bishop", 0)
button btnQueen = DialogAddButton(chooseUpgrade, "Queen", 0)
endglobals
function subOfCell takes integer row, integer col returns integer
return row*8+col
endfunction
function getRow takes integer sub returns integer
return sub/8
endfunction
function getCol takes integer sub returns integer
return ModuloInteger(sub, 8)
endfunction
function createBoard takes nothing returns nothing
local real x0 = -1205
local real y0 = 700
local real width = 260
local integer i
local integer j
local integer k
local unit u
local unit t
set i = 0
loop
exitwhen i >= 8
set j = 0
loop
exitwhen j >= 8
set cells[subOfCell(i, j)] = Rect(x0+j*width,y0-i*width,x0+(j+1)*width,y0-(i+1)*width)
set j = j + 1
endloop
set i = i + 1
endloop
// create pieces for player0
set i = 0
set k = 0
loop
exitwhen i>=8
set pieces[k] = CreateUnitAtLoc(Player(0), 'hfoo' ,GetRectCenter(cells[subOfCell(1, i)]), 270)
set i = i + 1
set k = k + 1
endloop
set i = 0
set pieces[k] = CreateUnitAtLoc(Player(0), 'hmtt' ,GetRectCenter(cells[subOfCell(0, 0)]), 270)
set pieces[k+1] = CreateUnitAtLoc(Player(0), 'hkni' ,GetRectCenter(cells[subOfCell(0, 1)]), 270)
set pieces[k+2] = CreateUnitAtLoc(Player(0), 'hrif' ,GetRectCenter(cells[subOfCell(0, 2)]), 270)
set pieces[k+3] = CreateUnitAtLoc(Player(0), 'hsor' ,GetRectCenter(cells[subOfCell(0, 3)]), 270)
set pieces[k+4] = CreateUnitAtLoc(Player(0), 'Hart' ,GetRectCenter(cells[subOfCell(0, 4)]), 270)
set pieces[k+5] = CreateUnitAtLoc(Player(0), 'hrif' ,GetRectCenter(cells[subOfCell(0, 5)]), 270)
set pieces[k+6] = CreateUnitAtLoc(Player(0), 'hkni' ,GetRectCenter(cells[subOfCell(0, 6)]), 270)
set pieces[k+7] = CreateUnitAtLoc(Player(0), 'hmtt' ,GetRectCenter(cells[subOfCell(0, 7)]), 270)
set k = k + 8
// create pieces for player1
loop
exitwhen i>=8
set pieces[k] = CreateUnitAtLoc(Player(1), 'hfoo' ,GetRectCenter(cells[subOfCell(6, i)]), 90)
set i = i + 1
set k = k + 1
endloop
set pieces[k] = CreateUnitAtLoc(Player(1), 'hmtt' ,GetRectCenter(cells[subOfCell(7, 0)]), 90)
set pieces[k+1] = CreateUnitAtLoc(Player(1), 'hkni' ,GetRectCenter(cells[subOfCell(7, 1)]), 90)
set pieces[k+2] = CreateUnitAtLoc(Player(1), 'hrif' ,GetRectCenter(cells[subOfCell(7, 2)]), 90)
set pieces[k+3] = CreateUnitAtLoc(Player(1), 'hsor' ,GetRectCenter(cells[subOfCell(7, 3)]), 90)
set pieces[k+4] = CreateUnitAtLoc(Player(1), 'Hart' ,GetRectCenter(cells[subOfCell(7, 4)]), 90)
set pieces[k+5] = CreateUnitAtLoc(Player(1), 'hrif' ,GetRectCenter(cells[subOfCell(7, 5)]), 90)
set pieces[k+6] = CreateUnitAtLoc(Player(1), 'hkni' ,GetRectCenter(cells[subOfCell(7, 6)]), 90)
set pieces[k+7] = CreateUnitAtLoc(Player(1), 'hmtt' ,GetRectCenter(cells[subOfCell(7, 7)]), 90)
set i = 0
loop
exitwhen i>=64
set pawnMoved[i] = false
set i = i + 1
endloop
set i = 0
loop // player0's changed to neutral's
exitwhen i>=16
call SetUnitOwner(pieces[i], Player(PLAYER_NEUTRAL_PASSIVE), true)
set i = i + 1
endloop
endfunction
function rectHasUnit takes rect r returns boolean
local integer i
set i = 0
loop
exitwhen i>=32
if RectContainsUnit(r, pieces[i]) and IsUnitAliveBJ(pieces[i]) then
return true
endif
set i = i + 1
endloop
return false
endfunction
function p0CanShortCastle takes nothing returns boolean
if not p0ShortCastleUnderAttack and not p0KingMoved and not p0Rook1Moved and not rectHasUnit(cells[5]) and not rectHasUnit(cells[6]) then
return true
else
return false
endif
endfunction
function p0CanLongCastle takes nothing returns boolean
if not p0LongCastleUnderAttack and not p0KingMoved and not p0Rook0Moved and not rectHasUnit(cells[1]) and not rectHasUnit(cells[2]) and not rectHasUnit(cells[3]) then
return true
else
return false
endif
endfunction
function p1CanShortCastle takes nothing returns boolean
if not p1ShortCastleUnderAttack and not p1KingMoved and not p1Rook1Moved and not rectHasUnit(cells[61]) and not rectHasUnit(cells[62]) then
return true
else
return false
endif
endfunction
function p1CanLongCastle takes nothing returns boolean
if not p1LongCastleUnderAttack and not p1KingMoved and not p1Rook0Moved and not rectHasUnit(cells[57]) and not rectHasUnit(cells[58]) and not rectHasUnit(cells[59]) then
return true
else
return false
endif
endfunction
function addMana takes integer p returns nothing
local integer i
if p == 0 then
call BJDebugMsg("addMana 0")
set i = 0
loop
exitwhen i >=16
call SetUnitManaBJ(pieces[i], GetUnitState(pieces[i], UNIT_STATE_MANA)+20)
set i = i + 1
endloop
endif
if p == 1 then
call BJDebugMsg("addMana 1")
set i = 16
loop
exitwhen i >=32
call SetUnitManaBJ(pieces[i], GetUnitState(pieces[i], UNIT_STATE_MANA)+20)
set i = i + 1
endloop
endif
endfunction
function flipTurn takes nothing returns nothing
local integer i
if turn == 0 then // player0 gives command
set i = 0
loop // player0's changed to neutral's
exitwhen i>=16
call SetUnitOwner(pieces[i], Player(PLAYER_NEUTRAL_PASSIVE), true)
set i = i + 1
endloop
loop // player1's changed to player1's
exitwhen i>=32
call SetUnitOwner(pieces[i], Player(1), true)
set i = i + 1
endloop
call addMana(0)
set turn = 1
elseif turn == 1 then
set i = 0
loop // player0's changed to player0's
exitwhen i>=16
call SetUnitOwner(pieces[i], Player(0), true)
set i = i + 1
endloop
loop // player1's changed to neutral's
exitwhen i>=32
call SetUnitOwner(pieces[i], Player(PLAYER_NEUTRAL_PASSIVE), true)
set i = i + 1
endloop
call addMana(1)
set turn = 0
endif
endfunction
function subOfUnit takes unit piece returns integer
local integer i = 0
loop
exitwhen i>=32
if pieces[i] == piece then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function location2CellSub takes location l returns integer
local integer i = 0
loop
exitwhen i >= 64
if RectContainsLoc(cells[i], l) then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function cellSubOfUnit takes unit u returns integer
local integer i = 0
loop
if RectContainsUnit (cells[i], u) then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function checkUnitAttackingSquare takes unit u returns nothing
local integer i = cellSubOfUnit(u)
local integer j
local integer k
local integer t
if GetUnitTypeId(u) == 'hfoo' then
if IsUnitOwnedByPlayer(u, Player(0)) then
set attackingSquare[0] = 2
if i + 9 <= 63 then
set attackingSquare[1] = i + 7
set attackingSquare[2] = i + 9
endif
endif
if IsUnitOwnedByPlayer(u, Player(1)) then
set attackingSquare[0] = 2
if i - 9 >= 0 then
set attackingSquare[1] = i - 7
set attackingSquare[2] = i - 9
endif
endif
elseif GetUnitTypeId(u) == 'hmtt' then
set j = 0
set k = 1
loop
exitwhen j>=getRow(i) or rectHasUnit(cells[subOfCell(getRow(i)-1-j, getCol(i))])
set attackingSquare[k] = subOfCell(getRow(i)-1-j, getCol(i))
set k = k + 1
set j = j + 1
endloop
set j = 0
loop
exitwhen j>=7-getRow(i) or rectHasUnit(cells[subOfCell(getRow(i)+1+j, getCol(i))])
set attackingSquare[k] = subOfCell(getRow(i)+1+j, getCol(i))
set k = k + 1
set j = j + 1
endloop
set j = 0
loop
exitwhen j>=getCol(i) or rectHasUnit(cells[subOfCell(getRow(i), getCol(i)-1-j)])
set attackingSquare[k] = subOfCell(getRow(i), getCol(i)-1-j)
set k = k + 1
set j = j + 1
endloop
set j = 0
loop
exitwhen j>=7-getCol(i) or rectHasUnit(cells[subOfCell(getRow(i), getCol(i)+1+j)])
set attackingSquare[k] = subOfCell(getRow(i), getCol(i)+1+j)
set k = k + 1
set j = j + 1
endloop
set attackingSquare[0] = k - 1
elseif GetUnitTypeId(u) == 'hsor' then
set j = 0
set k = 1
loop
exitwhen j>=getRow(i) or rectHasUnit(cells[subOfCell(getRow(i)-1-j, getCol(i))])
set attackingSquare[k] = subOfCell(getRow(i)-1-j, getCol(i))
set k = k + 1
set j = j + 1
endloop
set j = 0
loop
exitwhen j>=7-getRow(i) or rectHasUnit(cells[subOfCell(getRow(i)+1+j, getCol(i))])
set attackingSquare[k] = subOfCell(getRow(i)+1+j, getCol(i))
set k = k + 1
set j = j + 1
endloop
set j = 0
loop
exitwhen j>=getCol(i) or rectHasUnit(cells[subOfCell(getRow(i), getCol(i)-1-j)])
set attackingSquare[k] = subOfCell(getRow(i), getCol(i)-1-j)
set k = k + 1
set j = j + 1
endloop
set j = 0
loop
exitwhen j>=7-getCol(i) or rectHasUnit(cells[subOfCell(getRow(i), getCol(i)+1+j)])
set attackingSquare[k] = subOfCell(getRow(i), getCol(i)+1+j)
set k = k + 1
set j = j + 1
endloop
set j = 1
loop
set t = -7*j + i
exitwhen t<0 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set j = 1
loop
set t = 7*j + i
exitwhen t>63 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set j = 1
loop
set t = -9*j + i
exitwhen t<0 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set j = 1
loop
set t = 9*j + i
exitwhen t>63 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set attackingSquare[0] = k - 1
elseif GetUnitTypeId(u) == 'hkni' then
set k = 1
if (i-10)>=0 and IAbsBJ(getRow(i-10)-getRow(i))<=3 and IAbsBJ(getCol(i-10)-getCol(i))<=3 then
set attackingSquare[k] = i
set k = k + 1
endif
if (i+10)<=63 and IAbsBJ(getRow(i+10)-getRow(i))<=3 and IAbsBJ(getCol(i+10)-getCol(i))<=3 then
set attackingSquare[k] = i+10
set k = k + 1
endif
if (i-6)>=0 and IAbsBJ(getRow(i-6)-getRow(i))<=3 and IAbsBJ(getCol(i-6)-getCol(i))<=3 then
set attackingSquare[k] = i-6
set k = k + 1
endif
if (i+6)<=63 and IAbsBJ(getRow(i+6)-getRow(i))<=3 and IAbsBJ(getCol(i+6)-getCol(i))<=3 then
set attackingSquare[k] = i+6
set k = k + 1
endif
if (i-15)>=0 and IAbsBJ(getRow(i-15)-getRow(i))<=3 and IAbsBJ(getCol(i-15)-getCol(i))<=3 then
set attackingSquare[k] = i-15
set k = k + 1
endif
if (i+15)<=63 and IAbsBJ(getRow(i+15)-getRow(i))<=3 and IAbsBJ(getCol(i+15)-getCol(i))<=3 then
set attackingSquare[k] = i+15
set k = k + 1
endif
if (i-17)>=0 and IAbsBJ(getRow(i-17)-getRow(i))<=3 and IAbsBJ(getCol(i-17)-getCol(i))<=3 then
set attackingSquare[k] = i-17
set k = k + 1
endif
if (i+17)<=63 and IAbsBJ(getRow(i+17)-getRow(i))<=3 and IAbsBJ(getCol(i+17)-getCol(i))<=3 then
set attackingSquare[k] = i+17
set k = k + 1
endif
set attackingSquare[0] = k - 1
elseif GetUnitTypeId(u) == 'hrif' then
set j = 1
set k = 1
loop
set t = -7*j + i
exitwhen t<0 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set j = 1
loop
set t = 7*j + i
exitwhen t>63 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set j = 1
loop
set t = -9*j + i
exitwhen t<0 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set j = 1
loop
set t = 9*j + i
exitwhen t>63 or IAbsBJ(getRow(i)-getRow(t))!=IAbsBJ(getCol(i)-getCol(t)) or rectHasUnit(cells[t])
set attackingSquare[k] = t
set j = j + 1
set k = k + 1
endloop
set attackingSquare[0] = k - 1
elseif GetUnitTypeId(u) == 'Hart' then
set k = 1
if (i+8)<=63 and not rectHasUnit(cells[i+8]) then
set attackingSquare[k] = i+8
set k = k + 1
endif
if (i-8)>=0 and not rectHasUnit(cells[i-8]) then
set attackingSquare[k] = i-8
set k = k + 1
endif
if (i+1)<=63 and IAbsBJ(getRow(i)-getRow(i+1))<=2 and not rectHasUnit(cells[i+1]) then
set attackingSquare[k] = i+1
set k = k + 1
endif
if (i-1)>=0 and IAbsBJ(getRow(i)-getRow(i-1))<=2 and not rectHasUnit(cells[i-1]) then
set attackingSquare[k] = i-1
set k = k + 1
endif
if (i+7)<=63 and IAbsBJ(getRow(i)-getRow(i+7))<=2 and not rectHasUnit(cells[i+7]) then
set attackingSquare[k] = i+7
set k = k + 1
endif
if (i-7)>=0 and IAbsBJ(getRow(i)-getRow(i-7))<=2 and not rectHasUnit(cells[i-7]) then
set attackingSquare[k] = i-7
set k = k + 1
endif
if (i+9)<=63 and IAbsBJ(getRow(i)-getRow(i+9))<=2 and not rectHasUnit(cells[i+9]) then
set attackingSquare[k] = i+9
set k = k + 1
endif
if (i-9)>=0 and IAbsBJ(getRow(i)-getRow(i-9))<=2 and not rectHasUnit(cells[i-9]) then
set attackingSquare[k] = i-9
set k = k + 1
endif
set attackingSquare[0] = k - 1
endif
endfunction
function lookSquare takes nothing returns nothing
local integer i = 0
local string str = ""
loop
exitwhen i>attackingSquare[0]
set str = str + I2S(attackingSquare[i]) + " "
set i = i + 1
endloop
call BJDebugMsg(str)
endfunction
function move takes unit piece, location target returns nothing
local integer i
local integer j
local effect tx
if GetUnitTypeId(piece) == 'Hart' then
if IsUnitOwnedByPlayer(piece, Player(0)) then
set p0KingMoved = true
elseif IsUnitOwnedByPlayer(piece, Player(1)) then
set p1KingMoved = true
endif
endif
if GetUnitTypeId(piece) == 'hmtt' then
if IsUnitOwnedByPlayer(piece, Player(0)) then
//call BJDebugMsg("player0 rook moving")
if subOfUnit(piece) == 8 then
set p0Rook0Moved = true
endif
if subOfUnit(piece) == 15 then
set p0Rook1Moved = true
endif
elseif IsUnitOwnedByPlayer(piece, Player(1)) then
if subOfUnit(piece) == 24 then
set p1Rook0Moved = true
endif
if subOfUnit(piece) == 31 then
set p1Rook1Moved = true
endif
endif
endif
call BJDebugMsg("spell location: "+R2S(GetLocationX(target))+", "+R2S(GetLocationY(target)))
set i = 0
loop
exitwhen i >= 64
if RectContainsLoc(cells[i], target) then
set tx = AddSpecialEffectLocBJ(GetRectCenter(cells[i]), "Abilities\\Spells\\Human\\Blizzard\\BlizzardTarget.mdl" )
call DestroyEffect(tx)
call SetUnitPositionLoc(piece, GetRectCenter(cells[i]))
call checkUnitAttackingSquare(piece)
call lookSquare()
if attack0CastleUnit != null and not RectContainsUnit(cells[cellSubOfAttack0CastleUnit], attack0CastleUnit) or not IsUnitAliveBJ(attack1CastleUnit) then
set p0LongCastleUnderAttack = false
set p0ShortCastleUnderAttack = false
endif
if attack1CastleUnit != null and not RectContainsUnit(cells[cellSubOfAttack1CastleUnit], attack1CastleUnit) or not IsUnitAliveBJ(attack1CastleUnit) then
set p1LongCastleUnderAttack = false
set p1ShortCastleUnderAttack = false
endif
set j = 1
loop
exitwhen j >= attackingSquare[0]
if subOfUnit(piece) >=16 and attackingSquare[j] == 4 then // if Player(1) owns piece
set attack0CastleUnit = piece
set cellSubOfAttack0CastleUnit = cellSubOfUnit(piece)
set p0LongCastleUnderAttack = true
set p0ShortCastleUnderAttack = true
endif
if subOfUnit(piece) >=16 and (attackingSquare[j] == 2 or attackingSquare[j] == 3) then
set attack0CastleUnit = piece
set cellSubOfAttack0CastleUnit = cellSubOfUnit(piece)
set p0LongCastleUnderAttack = true
endif
if subOfUnit(piece) >=16 and (attackingSquare[j] == 5 or attackingSquare[j] == 6) then
set attack0CastleUnit = piece
set cellSubOfAttack0CastleUnit = cellSubOfUnit(piece)
set p0ShortCastleUnderAttack = true
endif
if subOfUnit(piece) <=15 and attackingSquare[j] == 60 then // if Player(0) owns piece
set attack1CastleUnit = piece
set cellSubOfAttack1CastleUnit = cellSubOfUnit(piece)
set p1LongCastleUnderAttack = true
set p1ShortCastleUnderAttack = true
endif
if subOfUnit(piece) <=15 and (attackingSquare[j] == 58 or attackingSquare[j] == 59) then
set attack1CastleUnit = piece
set cellSubOfAttack1CastleUnit = cellSubOfUnit(piece)
set p1LongCastleUnderAttack = true
endif
if subOfUnit(piece) <=15 and (attackingSquare[j] == 61 or attackingSquare[j] == 62) then
set attack1CastleUnit = piece
set cellSubOfAttack1CastleUnit = cellSubOfUnit(piece)
set p1ShortCastleUnderAttack = true
endif
set j = j + 1
endloop
endif
set i = i + 1
endloop
set tx = null
endfunction
function updateAttackingSquareAfterKill takes unit piece returns nothing
local integer j
call checkUnitAttackingSquare(piece)
call lookSquare()
if attack0CastleUnit != null and not RectContainsUnit(cells[cellSubOfAttack0CastleUnit], attack0CastleUnit) or not IsUnitAliveBJ(attack0CastleUnit) then
set p0LongCastleUnderAttack = false
set p0ShortCastleUnderAttack = false
endif
if attack1CastleUnit != null and not RectContainsUnit(cells[cellSubOfAttack1CastleUnit], attack1CastleUnit) or not IsUnitAliveBJ(attack1CastleUnit) then
set p1LongCastleUnderAttack = false
set p1ShortCastleUnderAttack = false
endif
set j = 1
loop
exitwhen j >= attackingSquare[0]
if subOfUnit(piece) >=16 and attackingSquare[j] == 4 then // if Player(1) owns piece
set attack0CastleUnit = piece
set cellSubOfAttack0CastleUnit = cellSubOfUnit(piece)
set p0LongCastleUnderAttack = true
set p0ShortCastleUnderAttack = true
endif
if subOfUnit(piece) >=16 and (attackingSquare[j] == 2 or attackingSquare[j] == 3) then
set attack0CastleUnit = piece
set cellSubOfAttack0CastleUnit = cellSubOfUnit(piece)
set p0LongCastleUnderAttack = true
endif
if subOfUnit(piece) >=16 and (attackingSquare[j] == 5 or attackingSquare[j] == 6) then
set attack0CastleUnit = piece
set cellSubOfAttack0CastleUnit = cellSubOfUnit(piece)
set p0ShortCastleUnderAttack = true
endif
if subOfUnit(piece) <=15 and attackingSquare[j] == 60 then // if Player(0) owns piece
set attack1CastleUnit = piece
set cellSubOfAttack1CastleUnit = cellSubOfUnit(piece)
set p1LongCastleUnderAttack = true
set p1ShortCastleUnderAttack = true
endif
if subOfUnit(piece) <=15 and (attackingSquare[j] == 58 or attackingSquare[j] == 59) then
set attack1CastleUnit = piece
set cellSubOfAttack1CastleUnit = cellSubOfUnit(piece)
set p1LongCastleUnderAttack = true
endif
if subOfUnit(piece) <=15 and (attackingSquare[j] == 61 or attackingSquare[j] == 62) then
set attack1CastleUnit = piece
set cellSubOfAttack1CastleUnit = cellSubOfUnit(piece)
set p1ShortCastleUnderAttack = true
endif
set j = j + 1
endloop
endfunction
function checkMove takes player p, unit piece, location target, integer act returns boolean
local integer unitType = GetUnitTypeId(piece)
local location pieceLocation = GetUnitLoc(piece)
local integer i = 0
local integer j = 0
local integer t = 0
local integer k = 0
local integer targetCellSub
set i = 0
loop // find where the target is
exitwhen RectContainsLoc(cells[i], target) or i>= 64
set i = i + 1
endloop
set targetCellSub = i
call BJDebugMsg("checkMove target location: "+R2S(GetLocationX(target))+", "+R2S(GetLocationY(target)))
set i = 0
loop
exitwhen i>=64
if RectContainsLoc(cells[i], pieceLocation) then //find where the piece is
if unitType == 'hfoo' then
if IsUnitOwnedByPlayer(piece, Player(0)) then
if act == 0 then
if pawnMoved[subOfUnit(piece)] then
if targetCellSub == i + 8 and not rectHasUnit(cells[targetCellSub]) then
return true
else
return false
endif
else
if (targetCellSub == i + 8 and not rectHasUnit(cells[targetCellSub])) or (targetCellSub == i + 16 and not rectHasUnit(cells[i+8]) and not rectHasUnit(cells[targetCellSub])) then
set pawnMoved[subOfUnit(piece)] = true
return true
else
return false
endif
endif
endif
if act == 1 then
if targetCellSub == i + 7 or targetCellSub == i + 9 then
return true
else
return false
endif
endif
elseif IsUnitOwnedByPlayer(piece, Player(1)) then
if act == 0 then
if pawnMoved[subOfUnit(piece)] then
if targetCellSub == i - 8 and not rectHasUnit(cells[targetCellSub]) then
return true
else
return false
endif
else
if (targetCellSub == i - 8 and not rectHasUnit(cells[targetCellSub])) or (targetCellSub == i - 16 and not rectHasUnit(cells[i-8]) and not rectHasUnit(cells[targetCellSub])) then
set pawnMoved[subOfUnit(piece)] = true
return true
else
return false
endif
endif
endif
if act == 1 then
if targetCellSub == i - 7 or targetCellSub == i - 9 then
return true
else
return false
endif
endif
endif
return false
elseif unitType == 'hkni' then
if (i-10)>=0 and IAbsBJ(getRow(i-10)-getRow(i))<=3 and IAbsBJ(getCol(i-10)-getCol(i))<=3 then
if RectContainsLoc(cells[i-10], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i-10]) then
return true
endif
endif
endif
if (i+10)<=63 and IAbsBJ(getRow(i+10)-getRow(i))<=3 and IAbsBJ(getCol(i+10)-getCol(i))<=3 then
if RectContainsLoc(cells[i+10], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i+10]) then
return true
endif
endif
endif
if (i-6)>=0 and IAbsBJ(getRow(i-6)-getRow(i))<=3 and IAbsBJ(getCol(i-6)-getCol(i))<=3 then
if RectContainsLoc(cells[i-6], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i-6]) then
return true
endif
endif
endif
if (i+6)<=63 and IAbsBJ(getRow(i+6)-getRow(i))<=3 and IAbsBJ(getCol(i+6)-getCol(i))<=3 then
if RectContainsLoc(cells[i+6], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i+6]) then
return true
endif
endif
endif
if (i-15)>=0 and IAbsBJ(getRow(i-15)-getRow(i))<=3 and IAbsBJ(getCol(i-15)-getCol(i))<=3 then
if RectContainsLoc(cells[i-15], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i-15]) then
return true
endif
endif
endif
if (i+15)<=63 and IAbsBJ(getRow(i+15)-getRow(i))<=3 and IAbsBJ(getCol(i+15)-getCol(i))<=3 then
if RectContainsLoc(cells[i+15], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i+15]) then
return true
endif
endif
endif
if (i-17)>=0 and IAbsBJ(getRow(i-17)-getRow(i))<=3 and IAbsBJ(getCol(i-17)-getCol(i))<=3 then
if RectContainsLoc(cells[i-17], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i-17]) then
return true
endif
endif
endif
if (i+17)<=63 and IAbsBJ(getRow(i+17)-getRow(i))<=3 and IAbsBJ(getCol(i+17)-getCol(i))<=3 then
if RectContainsLoc(cells[i+17], target) then
if act == 1 then
return true
elseif act == 0 and not rectHasUnit(cells[i+17]) then
return true
endif
endif
endif
return false
elseif unitType == 'hrif' then
call BJDebugMsg("targetCellSub: "+I2S(targetCellSub))
if (IAbsBJ(getRow(targetCellSub) - getRow(i))) == (IAbsBJ(getCol(targetCellSub) - getCol(i))) then
if getRow(targetCellSub) - getRow(i) < 0 and getCol(targetCellSub) - getCol(i) < 0 then
set k = 0
loop
exitwhen k >= getRow(i) - getRow(targetCellSub)
set j = 0
if rectHasUnit(cells[subOfCell(getRow(i)-1-k, getCol(i)-1-k)]) then
if (k == (getRow(i) - getRow(targetCellSub) - 1)) and (act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
if getRow(targetCellSub) - getRow(i) > 0 and getCol(targetCellSub) - getCol(i) < 0 then
set k = 0
loop
exitwhen k >= getRow(targetCellSub) - getRow(i)
call BJDebugMsg(I2S(getRow(i)+1+k)+", "+I2S(getCol(i)-1-k))
call BJDebugMsg(I2S(subOfCell(getRow(i)+1+k, getCol(i)-1-k)))
if rectHasUnit(cells[subOfCell(getRow(i)+1+k, getCol(i)-1-k)]) then
call BJDebugMsg(I2S(getRow(i)+1+k)+", "+I2S(getCol(i)-1-k))
if (k == (getRow(targetCellSub) - getRow(i) - 1)) and (act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
if getRow(targetCellSub) - getRow(i) < 0 and getCol(targetCellSub) - getCol(i) > 0 then
set k = 0
loop
exitwhen k >= getRow(i) - getRow(targetCellSub)
if rectHasUnit(cells[subOfCell(getRow(i)-1-k, getCol(i)+1+k)]) then
if (k == (getRow(i) - getRow(targetCellSub) - 1)) and (act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
if getRow(targetCellSub) - getRow(i) > 0 and getCol(targetCellSub) - getCol(i) > 0 then
set k = 0
loop
exitwhen k >= getRow(targetCellSub) - getRow(i)
set j = 0
if rectHasUnit(cells[subOfCell(getRow(i)+1+k, getCol(i)+1+k)]) then
if (k == (getRow(targetCellSub) - getRow(i) - 1)) and(act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
endif
return false
elseif unitType == 'hsor' then
if (IAbsBJ(getRow(targetCellSub) - getRow(i))) == (IAbsBJ(getCol(targetCellSub) - getCol(i))) then
if getRow(targetCellSub) - getRow(i) < 0 and getCol(targetCellSub) - getCol(i) < 0 then
set k = 0
loop
exitwhen k >= getRow(i) - getRow(targetCellSub)
set j = 0
if rectHasUnit(cells[subOfCell(getRow(i)-1-k, getCol(i)-1-k)]) then
if (k == (getRow(i) - getRow(targetCellSub) - 1)) and (act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
if getRow(targetCellSub) - getRow(i) > 0 and getCol(targetCellSub) - getCol(i) < 0 then
set k = 0
loop
exitwhen k >= getRow(targetCellSub) - getRow(i)
if rectHasUnit(cells[subOfCell(getRow(i)+1+k, getCol(i)-1-k)]) then
if (k == (getRow(targetCellSub) - getRow(i) - 1)) and (act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
if getRow(targetCellSub) - getRow(i) < 0 and getCol(targetCellSub) - getCol(i) > 0 then
set k = 0
loop
exitwhen k >= getRow(i) - getRow(targetCellSub)
if rectHasUnit(cells[subOfCell(getRow(i)-1-k, getCol(i)+1+k)]) then
if (k == (getRow(i) - getRow(targetCellSub) - 1)) and (act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
if getRow(targetCellSub) - getRow(i) > 0 and getCol(targetCellSub) - getCol(i) > 0 then
set k = 0
loop
exitwhen k >= getRow(targetCellSub) - getRow(i)
set j = 0
if rectHasUnit(cells[subOfCell(getRow(i)+1+k, getCol(i)+1+k)]) then
if (k == (getRow(targetCellSub) - getRow(i) - 1)) and(act == 1) then
return true
else
return false
endif
endif
set k = k + 1
endloop
return true
endif
endif
if getRow(targetCellSub) == getRow(i) or getCol(targetCellSub) == getCol(i) then
if getRow(targetCellSub) == getRow(i) and getCol(targetCellSub) == getCol(i) then
return false
endif
if getCol(targetCellSub) == getCol(i) and getRow(targetCellSub) < getRow(i) then
if act == 1 then
set k = 0
loop
exitwhen k >= getRow(i) - getRow(targetCellSub) - 1
if rectHasUnit(cells[subOfCell(getRow(i)-1-k, getCol(i))]) then
return false
endif
set k = k + 1
endloop
return true
endif
if act == 0 then
set k = 0
loop
exitwhen k >= getRow(i) - getRow(targetCellSub)
if rectHasUnit(cells[subOfCell(getRow(i)-1-k, getCol(i))]) then
return false
endif
set k = k + 1
endloop
return true
endif
endif
if getCol(targetCellSub) == getCol(i) and getRow(targetCellSub) > getRow(i) then
if act == 1 then
set k = 0
loop
exitwhen k >= getRow(targetCellSub) - getRow(i) - 1
if rectHasUnit(cells[subOfCell(getRow(i)+1+k, getCol(i))]) then
return false
endif