-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathglobals.cpp
157 lines (136 loc) · 4.57 KB
/
globals.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
/*
Clarity
Copyright (C) 2024 Joseph Pasfield
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "tunables.h"
#include "globals.h"
#include "slidey.h"
// takes a piece number and gets the type of it
int getType(int value) {
return value & 7;
}
// takes a piece number and gets the color of it
int getColor(int value) {
return value >> 3;
}
// a mask for a single file on the board
uint64_t fileMask = 0b100000001000000010000000100000001000000010000000100000001;
// a mask for a single rank on the board
uint64_t rankMask = 0b11111111;
// gets a mask for either a rank or file.
uint64_t getFileMask(int file) {
return fileMask << file;
}
uint64_t getRankMask(int rank) {
return rankMask << (8 * rank);
}
// finds the least significant bit in the uint64_t, gets rid of it, and returns its index
int popLSB(uint64_t &bitboard) {
int lsb = std::countr_zero(bitboard);
bitboard &= bitboard - 1;
return lsb;
}
// converts a move to long algebraic form
std::string toLongAlgebraic(Move move) {
std::string longAlgebraic = "";
longAlgebraic += squareNames[move.getStartSquare()];
longAlgebraic += squareNames[move.getEndSquare()];
switch(move.getFlag()) {
case promotions[0]:
longAlgebraic += 'n';
break;
case promotions[1]:
longAlgebraic += 'b';
break;
case promotions[2]:
longAlgebraic += 'r';
break;
case promotions[3]:
longAlgebraic += 'q';
break;
default:
break;
}
return longAlgebraic;
}
// calculates the reductions used for LMR, ran on startup
std::array<std::array<uint8_t, 218>, 150> reductions;
void calculateReductions() {
for(int depth = 0; depth < 150; depth++) {
for(int move = 0; move < 218; move++) {
reductions[depth][move] = uint8_t(std::clamp(lmrBase.value + log(depth) * log(move) * lmrMultiplier.value, -32678.0, 32678.0));
}
}
}
std::array<uint64_t, 64> squareToBitboard;
void generateSquareToBitboard() {
for(int i = 0; i < 64; i++) {
squareToBitboard[i] = (1ULL << i);
}
}
/*ran on startup, does 4 things:
1: generates the lookups for sliding pieces
2: generates the numbers used for zobrist hashing
3: calculates the numbers for LMR
4: calculates numbers for square to bitboard lookups
*/
void initialize() {
generateSquareToBitboard();
generateLookups();
initializeZobrist();
calculateReductions();
}
// splits a string into segments based on the seperator
std::vector<std::string> split(const std::string string, const char seperator) {
std::stringstream stream(string);
std::string segment;
std::vector<std::string> list;
// every time that it can get a segment
while(std::getline(stream, segment, seperator)) {
// add it to the vector
list.push_back(segment);
}
return list;
}
// sorts the moves all at once
void sortMoves(std::array<int, 256> &values, std::array<Move, 256> &moves, int numMoves) {
int lowestIndex;
// for each value
for(int i = 0; i < numMoves - 1; i++) {
// find the lowest number that hasn't been sorted yet
lowestIndex = i;
for(int j = i + 1; j < numMoves; j++) {
if(values[j] < values[lowestIndex])
lowestIndex = j;
}
// swap the elements
if(lowestIndex != i) {
std::swap(values[lowestIndex], values[i]);
std::swap(moves[lowestIndex], moves[i]);
}
}
}
// flips the index vertically, used for indexing the psqts
int flipIndex(int index) {
return index ^ 56;
}
// thanks z5
// also I completely misunderstood how this was supposed to work, this isn't it lol
void incrementalSort(std::array<int, 256> &values, std::array<Move, 256> &moves, int numMoves, int i) {
for(int j = i + 1; j < numMoves; j++) {
if(values[j] > values[i]) {
std::swap(values[j], values[i]);
std::swap(moves[j], moves[i]);
}
}
}