[go: up one dir, main page]

Menu

[r1131]: / src / evaluationc.c  Maximize  Restore  History

Download this file

814 lines (709 with data), 28.8 kB

  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
#undef addBonus
#undef addMalus
#undef COLOR
#undef OPPCOLOR
#ifdef PERSPECTIVE_WHITE
#define COLOR WHITE
#define OPPCOLOR BLACK
#define addBonus(base, bonus) (base->balance += bonus);
#define addMalus(base, bonus) (base->balance -= bonus);
void evaluateWhiteKnight(const Position * position,
EvaluationBase * base, const Square square);
void evaluateWhiteBishop(const Position * position,
EvaluationBase * base, const Square square);
void evaluateWhiteRook(const Position * position,
EvaluationBase * base, const Square square);
void evaluateWhiteQueen(const Position * position,
EvaluationBase * base, const Square square);
void evaluateWhitePasser(const Position * position,
EvaluationBase * base, const Square square);
void evaluateWhitePawns(EvaluationBase * base);
int getPawnSafetyMalusOfWhiteKingFile(const Position * position,
const int file,
const Square kingSquare,
const int fileType);
#else
#define COLOR BLACK
#define OPPCOLOR WHITE
#define addBonus(base, bonus) (base->balance -= bonus);
#define addMalus(base, bonus) (base->balance += bonus);
void evaluateBlackKnight(const Position * position,
EvaluationBase * base, const Square square);
void evaluateBlackBishop(const Position * position,
EvaluationBase * base, const Square square);
void evaluateBlackRook(const Position * position,
EvaluationBase * base, const Square square);
void evaluateBlackQueen(const Position * position,
EvaluationBase * base, const Square square);
void evaluateBlackPasser(const Position * position,
EvaluationBase * base, const Square square);
void evaluateBlackPawns(EvaluationBase * base);
int getPawnSafetyMalusOfBlackKingFile(const Position * position,
const int file,
const Square kingSquare,
const int fileType);
#endif
#ifdef PERSPECTIVE_WHITE
static void addWhitePieceAttackBonus(const Position * position,
EvaluationBase * base,
const Bitboard moves,
const Bitboard candidateTargets,
const Piece attacker)
#else
static void addBlackPieceAttackBonus(const Position * position,
EvaluationBase * base,
const Bitboard moves,
const Bitboard candidateTargets,
const Piece attacker)
#endif
{
Bitboard targets = moves & candidateTargets;
Square targetSquare;
ITERATE_BITBOARD(&targets, targetSquare)
{
const Piece target = position->piece[targetSquare];
addBonus(base, piecePieceAttackBonus[attacker][target]);
}
}
#ifdef PERSPECTIVE_WHITE
void evaluateWhiteKnight(const Position * position,
EvaluationBase * base, const Square square)
#else
void evaluateBlackKnight(const Position * position,
EvaluationBase * base, const Square square)
#endif
{
const Square pinningPiece = getPinningPiece(position, base, square, COLOR);
const Bitboard moves =
(pinningPiece == NO_SQUARE ? getKnightMoves(square) : EMPTY_BITBOARD);
const Bitboard ctm = position->piecesOfType[PAWN | OPPCOLOR] |
position->piecesOfType[BISHOP | OPPCOLOR];
const Bitboard cth = position->piecesOfType[ROOK | OPPCOLOR] |
position->piecesOfType[QUEEN | OPPCOLOR];
const Bitboard candidateTargets = cth |
(ctm & base->unprotectedPieces[OPPCOLOR]);
#ifdef PERSPECTIVE_WHITE
const Square relativeSquare = square;
#else
const Square relativeSquare = getFlippedSquare(square);
#endif
if (pinningPiece == NO_SQUARE)
{
const Bitboard mobSquares = base->countedSquares[COLOR] |
getNonPawnPieces(position, OPPCOLOR);
const int mobilityCount = getNumberOfSetSquares(moves & mobSquares);
assert(mobilityCount <= MAX_MOVES_KNIGHT);
addBonus(base, KnightMobilityBonus[mobilityCount]);
}
else
{
setSquare(base->pinnedPieces[COLOR], square);
}
base->knightAttackedSquares[COLOR] |= moves;
if (position->piecesOfType[KNIGHT | OPPCOLOR] == EMPTY_BITBOARD &&
position->piecesOfType[BISHOP | OPPCOLOR] != EMPTY_BITBOARD &&
hasAttackingBishop(position, OPPCOLOR, square) == FALSE)
{
addBonus(base, V(2, 2));
}
if (squareIsPawnSafe(base, COLOR, square) &&
BONUS_KNIGHT_OUTPOST[relativeSquare] > 0)
{
int bonusValue = BONUS_KNIGHT_OUTPOST[relativeSquare];
if (testSquare(base->pawnProtectedSquares[COLOR], square))
{
if (position->piecesOfType[KNIGHT | OPPCOLOR] == EMPTY_BITBOARD &&
hasAttackingBishop(position, OPPCOLOR, square) == FALSE)
{
bonusValue += bonusValue + bonusValue / 2;
}
else
{
bonusValue += bonusValue;
}
}
addBonus(base, V(bonusValue, bonusValue));
}
if (base->evaluateKingSafety[OPPCOLOR] &&
(moves & base->kingAttackSquares[OPPCOLOR]) != EMPTY_BITBOARD)
{
const Bitboard coronaAttacks =
moves & getKingMoves(position->king[OPPCOLOR]);
base->kingSquaresAttackCount[COLOR] +=
getNumberOfSetSquares(coronaAttacks);
base->attackInfo[COLOR] += V(KNIGHT_BONUS_ATTACK, 1);
}
#ifdef PERSPECTIVE_WHITE
addWhitePieceAttackBonus(position, base, moves, candidateTargets,
WHITE_KNIGHT);
#else
addBlackPieceAttackBonus(position, base, moves, candidateTargets,
BLACK_KNIGHT);
#endif
if (testSquare(base->pawnProtectedSquares[OPPCOLOR], square))
{
addMalus(base, piecePieceAttackBonus[PAWN | OPPCOLOR][KNIGHT | COLOR]);
}
}
#ifdef PERSPECTIVE_WHITE
void evaluateWhiteBishop(const Position * position,
EvaluationBase * base, const Square square)
#else
void evaluateBlackBishop(const Position * position,
EvaluationBase * base, const Square square)
#endif
{
const Square pinningPiece = getPinningPiece(position, base, square, COLOR);
const Bitboard xrayPieces = position->piecesOfType[QUEEN | COLOR];
const Bitboard moves =
(pinningPiece == NO_SQUARE ?
getMagicBishopMoves(square, position->allPieces & ~xrayPieces) :
getMagicBishopMoves(square, position->allPieces & ~xrayPieces) &
(squaresBetween[pinningPiece][position->king[COLOR]] |
minValue[pinningPiece]));
const Bitboard mobSquares = base->countedSquares[COLOR] |
getNonPawnPieces(position, OPPCOLOR);
const Bitboard bishopSquares =
(testSquare(lightSquares, square) ? lightSquares : darkSquares);
const Bitboard squareColorTargets =
position->piecesOfType[PAWN | OPPCOLOR] & squaresBelow[COLOR][square] &
base->unprotectedPieces[OPPCOLOR] & bishopSquares;
const Bitboard ctm = position->piecesOfType[PAWN | OPPCOLOR] |
position->piecesOfType[KNIGHT | OPPCOLOR];
const Bitboard cth = position->piecesOfType[ROOK | OPPCOLOR] |
position->piecesOfType[QUEEN | OPPCOLOR];
const Bitboard candidateTargets =
cth | (ctm & base->unprotectedPieces[OPPCOLOR]);
const Piece batteryPiece =
getDiaBatteryPiece(position, moves, square, OPPCOLOR);
const int mobilityCount = getNumberOfSetSquares(moves & mobSquares);
#ifdef PERSPECTIVE_WHITE
const int pawnIndex = getWhiteBishopBlockingIndex(position, bishopSquares);
#else
const int pawnIndex = getBlackBishopBlockingIndex(position, bishopSquares);
#endif
#ifdef PERSPECTIVE_WHITE
const Square relativeSquare = square;
#else
const Square relativeSquare = getFlippedSquare(square);
#endif
assert(mobilityCount <= MAX_MOVES_BISHOP);
addBonus(base, BishopMobilityBonus[mobilityCount]);
addMalus(base, V(wV(pawnIndex, 650), wV(pawnIndex, 750)));
addBonus(base, getNumberOfSetSquares(squareColorTargets) * V(0, 2));
base->bishopAttackedSquares[COLOR] |= moves;
if (squareIsPawnSafe(base, COLOR, square) &&
BONUS_BISHOP_OUTPOST[relativeSquare] > 0)
{
int bonusValue = BONUS_BISHOP_OUTPOST[relativeSquare];
if (testSquare(base->pawnProtectedSquares[COLOR], square))
{
if (position->piecesOfType[KNIGHT | OPPCOLOR] == EMPTY_BITBOARD &&
hasAttackingBishop(position, OPPCOLOR, square) == FALSE)
{
bonusValue += bonusValue + bonusValue / 2;
}
else
{
bonusValue += bonusValue;
}
}
addBonus(base, V(bonusValue, bonusValue));
}
if (pinningPiece != NO_SQUARE)
{
setSquare(base->pinnedPieces[COLOR], square);
}
#ifdef PERSPECTIVE_WHITE
addWhitePieceAttackBonus(position, base, moves, candidateTargets,
WHITE_BISHOP);
#else
addBlackPieceAttackBonus(position, base, moves, candidateTargets,
BLACK_BISHOP);
#endif
if (testSquare(base->pawnProtectedSquares[OPPCOLOR], square))
{
addMalus(base, piecePieceAttackBonus[PAWN | OPPCOLOR][BISHOP | COLOR]);
}
if (base->evaluateKingSafety[OPPCOLOR])
{
if ((moves & base->kingAttackSquares[OPPCOLOR]) != EMPTY_BITBOARD)
{
const Bitboard coronaAttacks =
moves & getKingMoves(position->king[OPPCOLOR]);
base->kingSquaresAttackCount[COLOR] +=
getNumberOfSetSquares(coronaAttacks);
base->attackInfo[COLOR] += V(BISHOP_BONUS_ATTACK, 1);
}
}
if (batteryPiece != NO_PIECE)
{
addBonus(base, V(BISHOP_PIN_OP_VAL, BISHOP_PIN_EG_VAL));
}
}
#ifdef PERSPECTIVE_WHITE
void evaluateWhiteRook(const Position * position,
EvaluationBase * base, const Square square)
#else
void evaluateBlackRook(const Position * position,
EvaluationBase * base, const Square square)
#endif
{
const Piece piece = position->piece[square];
const Square pinningPiece = getPinningPiece(position, base, square, COLOR);
const Bitboard xrayPieces = position->piecesOfType[QUEEN | COLOR] |
position->piecesOfType[piece];
const Bitboard moves =
(pinningPiece == NO_SQUARE ?
getMagicRookMoves(square, position->allPieces & ~xrayPieces) :
getMagicRookMoves(square, position->allPieces & ~xrayPieces) &
(squaresBetween[pinningPiece][position->king[COLOR]] |
minValue[pinningPiece]));
const Bitboard mobSquares = base->countedSquares[COLOR];
const Bitboard fileSquares = squaresOfFile[file(square)];
const Bitboard ownPawns = position->piecesOfType[PAWN | COLOR];
const Rank relativeRank = colorRank(COLOR, square);
const Bitboard ctm = position->piecesOfType[PAWN | OPPCOLOR] |
position->piecesOfType[KNIGHT | OPPCOLOR] |
position->piecesOfType[BISHOP | OPPCOLOR];
const Bitboard cth = position->piecesOfType[QUEEN | OPPCOLOR];
const Bitboard candidateTargets = cth |
(ctm & base->unprotectedPieces[OPPCOLOR]);
const int mobilityCount = getNumberOfSetSquares(moves & mobSquares);
assert(mobilityCount <= MAX_MOVES_ROOK);
addBonus(base, RookMobilityBonus[mobilityCount]);
base->rookDoubleAttackedSquares[COLOR] |=
base->rookAttackedSquares[COLOR] & moves;
base->rookAttackedSquares[COLOR] |= moves;
if (pinningPiece != NO_SQUARE)
{
setSquare(base->pinnedPieces[COLOR], square);
}
#ifdef PERSPECTIVE_WHITE
addWhitePieceAttackBonus(position, base, moves, candidateTargets,
WHITE_ROOK);
#else
addBlackPieceAttackBonus(position, base, moves, candidateTargets,
BLACK_ROOK);
#endif
if (testSquare(base->pawnProtectedSquares[OPPCOLOR], square))
{
addMalus(base, piecePieceAttackBonus[PAWN | OPPCOLOR][ROOK | COLOR]);
}
/* Add a bonus if this rook is located on an open file. */
if ((ownPawns & fileSquares) == EMPTY_BITBOARD)
{
const Bitboard oppPawns = position->piecesOfType[PAWN | OPPCOLOR];
const Bitboard frontSquares = fileSquares & squaresAbove[COLOR][square];
Bitboard protectedBlockers = frontSquares &
base->pawnProtectedSquares[OPPCOLOR] &
(oppPawns | position->piecesOfType[OPPCOLOR | KNIGHT] |
position->piecesOfType[OPPCOLOR | BISHOP]);
if (protectedBlockers == EMPTY_BITBOARD)
{
if ((frontSquares & oppPawns) == EMPTY_BITBOARD)
{
addBonus(base, V(20, 19));
}
else /* semi-open file */
{
addBonus(base, V(9, 11));
}
}
else /* file is blocked by a protected pawn, knight of bishop */
{
if ((protectedBlockers & oppPawns) != EMPTY_BITBOARD)
{
addBonus(base, V(8, 0));
}
else
{
#ifdef PERSPECTIVE_WHITE
const Square minorSquare = getFirstSquare(&protectedBlockers);
#else
const Square minorSquare = getLastSquare(&protectedBlockers);
#endif
if (squareIsPawnSafe(base, OPPCOLOR, minorSquare))
{
addBonus(base, V(8, 1));
}
else
{
addBonus(base, V(15, 5));
}
}
}
}
else if (testSquare(kingTrapsRook[COLOR][position->king[COLOR]], square) &&
(moves & centralFiles) == EMPTY_BITBOARD
/* && mobilityCount < 4 */ )
{
const int basicMalus = max(0, 51 - 9 * mobilityCount);
addMalus(base, V(basicMalus, 0));
}
if (relativeRank >= RANK_5)
{
const Bitboard pawnTargets = position->piecesOfType[PAWN | OPPCOLOR] &
generalMoves[ROOK][square];
if (relativeRank == RANK_7 &&
colorRank(COLOR, position->king[OPPCOLOR]) == RANK_8)
{
Bitboard companions = position->piecesOfType[ROOK | COLOR] |
position->piecesOfType[QUEEN | COLOR];
addBonus(base, V(5, 16));
if ((moves & companions & squaresOfRank[rank(square)]) !=
EMPTY_BITBOARD)
{
addBonus(base, V(5, 10));
}
}
if (pawnTargets != EMPTY_BITBOARD)
{
const int numTargets = getNumberOfSetSquares(pawnTargets);
addBonus(base, numTargets * V(5, 14));
}
if (squareIsPawnSafe(base, COLOR, square) &&
testSquare(border, square) == FALSE &&
testSquare(base->pawnProtectedSquares[COLOR], square))
{
addBonus(base, V(3, 5));
}
}
if (base->evaluateKingSafety[OPPCOLOR])
{
if ((moves & base->kingAttackSquares[OPPCOLOR]) != EMPTY_BITBOARD)
{
const Bitboard coronaAttacks =
moves & getKingMoves(position->king[OPPCOLOR]);
base->kingSquaresAttackCount[COLOR] +=
getNumberOfSetSquares(coronaAttacks);
base->attackInfo[COLOR] += V(ROOK_BONUS_ATTACK, 1);
}
}
/* Give a malus for a rook blocking his own passer on the 7th rank */
if (numberOfNonPawnPieces(position, COLOR) == 2 &&
colorRank(COLOR, square) == RANK_8 &&
testSquare(base->passedPawns[COLOR],
(Square) downward(COLOR, square)) &&
(fileSquares & squaresBelow[COLOR][square] &
position->piecesOfType[ROOK | OPPCOLOR]) != EMPTY_BITBOARD &&
(companionFiles[square] &
(position->piecesOfType[WHITE_PAWN] |
position->piecesOfType[BLACK_PAWN])) == EMPTY_BITBOARD)
{
addMalus(base, V(0, 90));
}
}
#ifdef PERSPECTIVE_WHITE
void evaluateWhiteQueen(const Position * position,
EvaluationBase * base, const Square square)
#else
void evaluateBlackQueen(const Position * position,
EvaluationBase * base, const Square square)
#endif
{
const Square pinningPiece = getPinningPiece(position, base, square, COLOR);
const Bitboard moves =
(pinningPiece == NO_SQUARE ?
getMagicQueenMoves(square, position->allPieces) :
getMagicQueenMoves(square, position->allPieces) &
(squaresBetween[pinningPiece][position->king[COLOR]] |
minValue[pinningPiece]));
const Bitboard xrayOrthoPieces = position->piecesOfType[ROOK | COLOR] |
position->piecesOfType[QUEEN | COLOR];
const Bitboard xrayOrthoMoves =
(pinningPiece == NO_SQUARE ?
getMagicRookMoves(square, position->allPieces & ~xrayOrthoPieces) :
getMagicRookMoves(square, position->allPieces & ~xrayOrthoPieces) &
squaresBetween[pinningPiece][position->king[COLOR]]);
const Bitboard candidateTargets = base->unprotectedPieces[OPPCOLOR];
const Bitboard countedQueenSquares = base->countedSquares[COLOR] &
~(base->knightAttackedSquares[OPPCOLOR] |
base->bishopAttackedSquares[OPPCOLOR] |
base->rookAttackedSquares[OPPCOLOR]);
const int mobilityCount =
getNumberOfSetSquares(moves & countedQueenSquares);
assert(mobilityCount <= MAX_MOVES_QUEEN);
addBonus(base, QueenMobilityBonus[mobilityCount]);
base->queenAttackedSquares[COLOR] |= moves;
base->queenSupportedSquares[COLOR] |= xrayOrthoMoves & ~moves;
if (colorRank(COLOR, square) >= RANK_5)
{
const Bitboard pawnTargets = position->piecesOfType[PAWN | OPPCOLOR] &
generalMoves[ROOK][square];
if (colorRank(COLOR, square) == RANK_7 &&
colorRank(COLOR, position->king[OPPCOLOR]) == RANK_8)
{
addBonus(base, V(3, 15));
}
if (pawnTargets != EMPTY_BITBOARD)
{
const int numTargets = getNumberOfSetSquares(pawnTargets);
addBonus(base, numTargets * V(2, 10));
}
}
if (base->evaluateKingSafety[OPPCOLOR])
{
if ((moves & base->kingAttackSquares[OPPCOLOR]) != EMPTY_BITBOARD)
{
const Bitboard coronaAttacks =
moves & getKingMoves(position->king[OPPCOLOR]);
base->kingSquaresAttackCount[COLOR] +=
getNumberOfSetSquares(coronaAttacks);
base->attackInfo[COLOR] += V(QUEEN_BONUS_ATTACK, 1);
}
}
if (pinningPiece != NO_SQUARE)
{
setSquare(base->pinnedPieces[COLOR], square);
}
#ifdef PERSPECTIVE_WHITE
addWhitePieceAttackBonus(position, base, moves, candidateTargets,
WHITE_QUEEN);
#else
addBlackPieceAttackBonus(position, base, moves, candidateTargets,
BLACK_QUEEN);
#endif
if (testSquare(base->pawnProtectedSquares[OPPCOLOR], square))
{
addMalus(base, piecePieceAttackBonus[PAWN | OPPCOLOR][QUEEN | COLOR]);
}
}
#ifdef PERSPECTIVE_WHITE
void evaluateWhitePasser(const Position * position,
EvaluationBase * base, const Square square)
#else
void evaluateBlackPasser(const Position * position,
EvaluationBase * base, const Square square)
#endif
{
const Rank pawnRank = colorRank(COLOR, square);
const int pawnDirection = (COLOR == WHITE ? 8 : -8);
const Square stopSquare = (Square) (square + pawnDirection);
const int numDefenders = position->numberOfPieces[OPPCOLOR] -
position->numberOfPawns[OPPCOLOR];
bool unStoppable = FALSE;
const int rank = pawnRank - RANK_2;
const int rankFactor = rank * (rank - 1);
int openingBonus = 17 * rankFactor;
int endgameBonus = 7 * (rankFactor + rank + 1);
int opValue, egValue;
assert(pawnIsPassed(position, square, COLOR));
if (numDefenders == 1)
{
const int egBonus = quad(0, 800, pawnRank);
const int kingDistance = distance(square, position->king[COLOR]);
const Square rectangleSquare =
(COLOR == position->activeColor ?
square : (Square) (square - pawnDirection));
const bool kingInRectangle =
testSquare(passedPawnRectangle[COLOR][rectangleSquare],
position->king[OPPCOLOR]);
if ((kingInRectangle == FALSE &&
(passedPawnCorridor[COLOR][square] &
position->piecesOfColor[COLOR]) == EMPTY_BITBOARD))
{
addBonus(base, V(0, egBonus));
unStoppable = TRUE;
}
else if (kingDistance == 1)
{
const File pawnFile = file(square);
const File kingFile = file(position->king[COLOR]);
const Square promotionSquare =
(COLOR == WHITE ? getSquare(pawnFile, RANK_8) :
getSquare(pawnFile, RANK_1));
const bool clearPath = (bool)
(kingFile != pawnFile ||
(kingFile != FILE_A && kingFile != FILE_H));
if (clearPath &&
distance(promotionSquare, position->king[COLOR]) <= 1)
{
addBonus(base, V(0, egBonus));
unStoppable = TRUE;
}
}
if (unStoppable == FALSE &&
base->hasPassersOrCandidates[OPPCOLOR] == FALSE &&
passerWalks(position, square, COLOR))
{
addBonus(base, V(0, egBonus));
unStoppable = TRUE;
}
}
if (rankFactor > 0 && unStoppable == FALSE)
{
const Square attKing = position->king[COLOR];
const Square defKing = position->king[OPPCOLOR];
const int attackerDistance = distance(attKing, stopSquare);
const int defenderDistance = distance(defKing, stopSquare);
endgameBonus -= 3 * rankFactor * attackerDistance;
endgameBonus += 5 * rankFactor * defenderDistance;
if (position->piece[stopSquare] == NO_PIECE)
{
Bitboard ownAttacks = base->attackedSquares[COLOR] |
getKingMoves(attKing);
const Bitboard oppAttacks = base->attackedSquares[OPPCOLOR] |
getKingMoves(defKing);
const Bitboard path = passedPawnCorridor[COLOR][square];
const Bitboard oppBlockers =
path & position->piecesOfColor[OPPCOLOR];
const Bitboard blockingSquare = minValue[stopSquare];
int bonus = 0;
Bitboard obstacles = path & oppAttacks;
if (testSquare(base->attackedSquares[OPPCOLOR], square))
{
const Bitboard candidates =
(position->piecesOfType[ROOK | OPPCOLOR] |
position->piecesOfType[QUEEN | OPPCOLOR]) &
passedPawnCorridor[OPPCOLOR][square] &
getMagicRookMoves(square, position->allPieces);
if (candidates != EMPTY_BITBOARD)
{
obstacles = path;
}
}
if (testSquare(base->attackedSquares[COLOR], square))
{
const Bitboard candidates =
(position->piecesOfType[ROOK | COLOR] |
position->piecesOfType[QUEEN | COLOR]) &
passedPawnCorridor[OPPCOLOR][square] &
getMagicRookMoves(square, position->allPieces);
if (candidates != EMPTY_BITBOARD)
{
ownAttacks = path;
}
}
obstacles |= oppBlockers;
if (obstacles == EMPTY_BITBOARD)
{
bonus += 16;
}
else if ((obstacles & blockingSquare) == EMPTY_BITBOARD)
{
bonus += 9;
}
if (ownAttacks == path)
{
bonus += 6;
}
else if ((ownAttacks & blockingSquare) != EMPTY_BITBOARD)
{
bonus += ((obstacles & ~ownAttacks) == EMPTY_BITBOARD ? 4 : 2);
}
openingBonus += rankFactor * bonus;
endgameBonus += rankFactor * bonus;
}
}
if (position->numberOfPawns[COLOR] < position->numberOfPawns[OPPCOLOR])
{
endgameBonus += endgameBonus / 4;
}
opValue = (openingBonus * PASSED_PAWN_WEIGHT_OP) / 256;
egValue = (endgameBonus * PASSED_PAWN_WEIGHT_EG) / 256;
addBonus(base, V(opValue, egValue));
}
#ifdef PERSPECTIVE_WHITE
int getPawnSafetyMalusOfWhiteKingFile(const Position * position,
const int file,
const Square kingSquare,
const int fileType)
#else
int getPawnSafetyMalusOfBlackKingFile(const Position * position,
const int file,
const Square kingSquare,
const int fileType)
#endif
{
const int ATTACK_WEIGHT = 306;
const Square baseSquare = getSquare(file, rank(kingSquare));
const Bitboard pawnRealm = squaresOfRank[rank(kingSquare)] |
squaresAbove[COLOR][kingSquare];
const Bitboard fileRealm = squaresOfFile[file] & pawnRealm;
const Bitboard diagRealm = pawnRealm &
generalMoves[WHITE_BISHOP][baseSquare] &
(file(kingSquare) <= FILE_D ?
squaresRightOf[baseSquare] : squaresLeftOf[baseSquare]);
Bitboard fileDefenders = fileRealm & position->piecesOfType[PAWN | COLOR];
Bitboard diagDefenders = diagRealm & position->piecesOfType[PAWN | COLOR];
Bitboard attackers = fileRealm & position->piecesOfType[PAWN | OPPCOLOR];
int fileDefenderIndex = 0, fileAttackerIndex = 0;
int fileAttackerDivisor = 256;
int diagonalDefenderMalus = 0;
#ifdef PERSPECTIVE_WHITE
const Square fileDefenderSquare = getFirstSquare(&fileDefenders);
const Square fileAttackerSquare = getFirstSquare(&attackers);
#else
const Square fileDefenderSquare = getLastSquare(&fileDefenders);
const Square fileAttackerSquare = getLastSquare(&attackers);
#endif
if (fileType == 1)
{
#ifdef PERSPECTIVE_WHITE
const Square diagDefenderSquare = getFirstSquare(&diagDefenders);
#else
const Square diagDefenderSquare = getLastSquare(&diagDefenders);
#endif
const File fileIndex = (File) (file <= FILE_D ? file : FILE_H - file);
const Rank rankIndex = (diagDefenderSquare != NO_SQUARE ?
colorRank(COLOR, diagDefenderSquare) : RANK_1);
assert(fileIndex >= FILE_A);
assert(fileIndex <= FILE_D);
assert(rankIndex >= RANK_1);
assert(rankIndex <= RANK_8);
diagonalDefenderMalus =
KINGSAFETY_PAWN_BONUS_DEFENDER_DIAG[fileIndex][rankIndex];
}
if (fileDefenderSquare != NO_SQUARE)
{
fileDefenderIndex = colorRank(COLOR, fileDefenderSquare);
}
else
{
fileAttackerDivisor = 192;
}
if (fileAttackerSquare != NO_SQUARE)
{
fileAttackerIndex = colorRank(COLOR, fileAttackerSquare);
if (fileAttackerIndex == fileDefenderIndex + 1)
{
fileAttackerDivisor = (fileAttackerIndex == RANK_3 ? 160 : 390);
}
}
assert(fileType >= 0);
assert(fileType <= 2);
assert(fileDefenderIndex >= RANK_1);
assert(fileDefenderIndex <= RANK_8);
assert(fileAttackerIndex >= RANK_1);
assert(fileAttackerIndex <= RANK_8);
return KINGSAFETY_PAWN_MALUS_DEFENDER[fileType][fileDefenderIndex] +
(KINGSAFETY_PAWN_BONUS_ATTACKER[fileType][fileAttackerIndex] *
ATTACK_WEIGHT) / fileAttackerDivisor + diagonalDefenderMalus;
}
#ifdef PERSPECTIVE_WHITE
void evaluateWhitePawns(EvaluationBase * base)
#else
void evaluateBlackPawns(EvaluationBase * base)
#endif
{
static const INT32 doubledMalusPerFile[8] = {
V(5, 13), V(7, 15), V(8, 15), V(8, 15),
V(8, 15), V(8, 15), V(7, 15), V(5, 13)
};
Bitboard chainPawns = base->chainPawns[COLOR];
Bitboard doubledPawns = base->doubledPawns[COLOR];
Square square;
ITERATE_BITBOARD(&chainPawns, square)
{
#ifdef PERSPECTIVE_WHITE
addBonus(base, PAWN_CHAIN_BONUS[square]);
#else
addBonus(base, PAWN_CHAIN_BONUS[getFlippedSquare(square)]);
#endif
}
ITERATE_BITBOARD(&doubledPawns, square)
{
addMalus(base, doubledMalusPerFile[file(square)]);
}
}