-int computePawnShelter(const Position &pos, Square ksq) {
- const Color Them = (Us == WHITE ? BLACK : WHITE);
- const File kingFile = ShelterFile[file_of(ksq)];
- const Bitboard ourPawns = pos.pieces(PAWN, Us) & in_front_bb(Us, ksq);
- const Bitboard theirPawns = pos.pieces(PAWN, Them) & (RankBB[rank_of(ksq)] | in_front_bb(Us, ksq));
-
- int shelter = 0;
-
- // Compute king shelter and storm values for the file the king is on, as well as the two adjacent files.
- for (int fileOffset = -1; fileOffset <= 1; fileOffset++) {
- // Shelter takes full penalty for center file, otherwise it's half penalty
- Bitboard shelterFile = ourPawns & FileBB[kingFile + fileOffset];
- Rank shelterClosest = shelterFile ? relative_rank<Us>(shelterFile)
- : RANK_1;
-
- shelter += fileOffset == 0 ? PawnShelter[shelterClosest]
- : score_non_center_file(PawnShelter[shelterClosest]);
-
- // Storm takes full penalty, unless there is an enemy pawn blocking us
- Bitboard stormFile = theirPawns & FileBB[kingFile + fileOffset];
- Rank stormClosest = stormFile ? relative_rank<Us>(stormFile)
- : RANK_1;
-
- shelter += shelterClosest + 1 == stormClosest ? PawnStorm[stormClosest] / 2
- : PawnStorm[stormClosest];
+int PawnInfo::shelter_storm(const Position& pos, Square ksq) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+
+ int safety = 0;
+ Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, ksq) | RankBB[rank_of(ksq)]);
+ Bitboard ourPawns = b & pos.pieces(Us) & ~RankBB[rank_of(ksq)];
+ Bitboard theirPawns = b & pos.pieces(Them);
+ Rank rkUs, rkThem;
+ File kf = file_of(ksq);
+
+ // Compute shelter and storm values for the file the king is on, as well as
+ // the two adjacent files. Computation is done from the white point of view.
+ kf = (kf == FILE_A) ? kf++ : (kf == FILE_H) ? kf-- : kf;
+
+ for (int f = kf - 1; f <= kf + 1; f++)
+ {
+ // Shelter penalty is higher for the pawn in front of the king
+ b = ourPawns & FileBB[f];
+ rkUs = b ? rank_of(Us == WHITE ? first_1(b) : ~last_1(b)) : RANK_1;
+ safety -= ShelterWeakness[f != kf][rkUs];
+
+ // Storm danger is smaller if enemy pawn is blocked
+ b = theirPawns & FileBB[f];
+ rkThem = b ? rank_of(Us == WHITE ? first_1(b) : ~last_1(b)) : RANK_1;
+ safety -= StormDanger[rkThem == rkUs + 1][rkThem];