@@ -251,9 +251,8 @@ namespace {
251251 15 , 15 , 15 , 15 , 15 , 15 , 15 , 15
252252 };
253253
254- // SafetyTable[] contains the actual king safety scores. It is initialized
255- // in init_safety().
256- Value SafetyTable[100 ];
254+ // SafetyTable[color][] contains the actual king safety weighted scores
255+ Score SafetyTable[2 ][128 ];
257256
258257 // Pawn and material hash tables, indexed by the current thread id.
259258 // Note that they will be initialized at 0 being global variables.
@@ -869,9 +868,8 @@ namespace {
869868 // that the king safety scores can sometimes be very big, and that
870869 // capturing a single attacking piece can therefore result in a score
871870 // change far bigger than the value of the captured piece.
872- Score v = apply_weight (make_score (SafetyTable[attackUnits], 0 ), WeightKingSafety[Us]);
873- ei.value -= Sign[Us] * v;
874- ei.futilityMargin [Us] += mg_value (v);
871+ ei.value -= Sign[Us] * SafetyTable[Us][attackUnits];
872+ ei.futilityMargin [Us] += mg_value (SafetyTable[Us][attackUnits]);
875873 }
876874 }
877875
@@ -1204,30 +1202,37 @@ namespace {
12041202 }
12051203
12061204 // init_safety() initizes the king safety evaluation, based on UCI
1207- // parameters. It is called from read_weights().
1205+ // parameters. It is called from read_weights().
12081206
12091207 void init_safety () {
12101208
12111209 int maxSlope = 30 ;
12121210 int peak = 0x500 ;
12131211 double a = 0.4 ;
12141212 double b = 0.0 ;
1213+ Value t[100 ];
12151214
1215+ // First setup the base table
12161216 for (int i = 0 ; i < 100 ; i++)
12171217 {
12181218 if (i < b)
1219- SafetyTable [i] = Value (0 );
1219+ t [i] = Value (0 );
12201220 else
1221- SafetyTable [i] = Value ((int )(a * (i - b) * (i - b)));
1221+ t [i] = Value ((int )(a * (i - b) * (i - b)));
12221222 }
12231223
12241224 for (int i = 1 ; i < 100 ; i++)
12251225 {
1226- if (SafetyTable [i] - SafetyTable [i - 1 ] > maxSlope)
1227- SafetyTable [i] = SafetyTable [i - 1 ] + Value (maxSlope);
1226+ if (t [i] - t [i - 1 ] > maxSlope)
1227+ t [i] = t [i - 1 ] + Value (maxSlope);
12281228
1229- if (SafetyTable [i] > Value (peak))
1230- SafetyTable [i] = Value (peak);
1229+ if (t [i] > Value (peak))
1230+ t [i] = Value (peak);
12311231 }
1232+
1233+ // Then apply the weights and get the final SafetyTable[] array
1234+ for (Color c = WHITE; c <= BLACK; c++)
1235+ for (int i = 0 ; i < 100 ; i++)
1236+ SafetyTable[c][i] = apply_weight (make_score (t[i], 0 ), WeightKingSafety[c]);
12321237 }
12331238}
0 commit comments