···11+//// Code generation for various parts of the engine
22+13import gleam/int
24import simplifile as file
35import starfish/internal/board
4657const generated_file_path = "src/starfish/internal/generated.gleam"
66-77-const piece_count = 12
8899pub fn main() {
1010 let imports =
···1515 file.write(to: generated_file_path, contents: generated_code)
1616}
17171818+/// 60 bits is the highest we can go on Erlang where bitwise operations are fast,
1919+/// so we limit hashes to 60 bits to ensure computing hashes is as fast as possible.
1820const max_60_bit_int = 576_460_752_303_423_500
19212222+const piece_count = 12
2323+2024fn generate_hash_data() -> String {
2125 let function_head =
2226 "pub fn hash_for_piece(piece: board.Piece, colour: board.Colour, position: Int) -> Int {
2327 case piece, colour, position {"
2428 let length = piece_count * board.size
2529 let hash_for_piece = generate_hash_data_loop(function_head, 0, length)
3030+ // Generate a random hash to indicate that it's black's turn to move
2631 let black_to_move =
2732 "pub const black_to_move_hash = 0x"
2833 <> int.to_base16(int.random(max_60_bit_int))
···3641 False -> {
3742 let position = generated / piece_count
3843 let position = case position == board.size - 1 {
4444+ // The last square needs to be a catch-all to ensure the `case` expression
4545+ // compiles.
3946 True -> "_"
4047 False -> int.to_string(position)
4148 }
···6067 <> ", "
6168 <> position
6269 <> " -> 0x"
7070+ // Generate a random 60-bit hash for this combination of piece and position
6371 <> int.to_base16(int.random(max_60_bit_int))
6472 generate_hash_data_loop(acc, generated + 1, length)
6573 }