2323
2424package main
2525
26- import (
27- // "fmt"
28- "sort"
29- )
26+ import
27+ // "fmt"
28+
29+ "sort"
3030
3131// Root Sorting
3232// At root, moves should be sorted based on subtree value rather than standard sorting.
3333
3434// bit pos. (LSB order)
3535// 28 Winning promotions (1 bits)
36+ // 1 <<padding>> (1 bit)
3637// 22 MVV/LVA (6 bits) - Used to choose between captures of equal material gain/loss
3738// 1 History heuristic : (21 bits)
3839// 0 Castles (1 bit)
3940
4041const (
41- SORT_WINNING = (1 << 28 )
42+ SORT_WINNING_PROMOTION = (1 << 31 )
43+ SORT_LOSING_PROMOTION = (1 << 29 )
4244)
4345
44- func mvv_lva (victim , attacker Piece ) uint64 { // returns value between 0 and 64
45- return uint64 (((victim + 1 )<< 3 )- attacker ) << 22
46- }
47-
4846// TODO: promotions not ordered correctly.
4947
5048// Promotion Captures:
@@ -53,20 +51,47 @@ func mvv_lva(victim, attacker Piece) uint64 { // returns value between 0 and 64
5351// Non-capture promotions:
5452// if square undefended, gain is promote_values[promoted_piece].
5553// If defended, gain is SEE score where captured_piece == EMPTY
56- func SortPromotion (brd * Board , m Move ) uint64 {
57- var val int
58- if is_attacked_by (brd , brd .AllOccupied ()& sq_mask_off [m .From ()], m .To (), brd .Enemy (), brd .c ) {
59- val = get_see (brd , m .From (), m .To (), m .CapturedPiece ()) // defended
60- } else {
61- val = m .PromotedTo ().PromoteValue () + m .CapturedPiece ().Value () // undefended
54+
55+ // func sort_promotion(brd *Board, m Move) uint64 {
56+ // if is_attacked_by(brd, brd.AllOccupied()&sq_mask_off[m.From()],
57+ // m.To(), brd.Enemy(), brd.c) { // defended
58+ // if get_see(brd, m.From(), m.To(), m.CapturedPiece()) >= 0 {
59+ // return SORT_WINNING_PROMOTION | mvv_lva(m.CapturedPiece(), PAWN)
60+ // } else {
61+ // return mvv_lva(m.CapturedPiece(), PAWN)
62+ // }
63+ // } else {
64+ // // val = m.PromotedTo().PromoteValue() + m.CapturedPiece().Value() // undefended
65+ // return SORT_WINNING_PROMOTION | mvv_lva(m.CapturedPiece(), PAWN)
66+ // }
67+ // }
68+
69+ func sort_promotion_advances (brd * Board , from , to int , promoted_to Piece ) uint64 {
70+ if is_attacked_by (brd , brd .AllOccupied ()& sq_mask_off [from ],
71+ to , brd .Enemy (), brd .c ) { // defended
72+ see := get_see (brd , from , to , EMPTY )
73+ if see >= 0 {
74+ return SORT_WINNING_PROMOTION | uint64 (see )
75+ } else {
76+ return uint64 (SORT_LOSING_PROMOTION + see )
77+ }
78+ } else { // undefended
79+ return SORT_WINNING_PROMOTION | uint64 (promoted_to .PromoteValue ())
6280 }
63- if val >= 0 {
64- return SORT_WINNING | mvv_lva (m .CapturedPiece (), PAWN )
65- } else {
66- return mvv_lva (m .CapturedPiece (), PAWN )
81+ }
82+
83+ func sort_promotion_captures (brd * Board , from , to int , captured_piece , promoted_to Piece ) uint64 {
84+ if is_attacked_by (brd , brd .AllOccupied ()& sq_mask_off [from ], to , brd .Enemy (), brd .c ) { // defended
85+ return uint64 (SORT_WINNING_PROMOTION + get_see (brd , from , to , captured_piece ))
86+ } else { // undefended
87+ return SORT_WINNING_PROMOTION | uint64 (promoted_to .PromoteValue ()+ captured_piece .Value ())
6788 }
6889}
6990
91+ func mvv_lva (victim , attacker Piece ) uint64 { // returns value between 0 and 64
92+ return uint64 (((victim + 1 )<< 3 )- attacker ) << 22
93+ }
94+
7095type SortItem struct {
7196 move Move
7297 order uint64
@@ -76,6 +101,12 @@ type MoveList []*SortItem
76101
77102func (l * MoveList ) Sort () {
78103 sort .Sort (l )
104+ // print_mutex.Lock()
105+ // fmt.Println("-------------")
106+ // for i, item := range *l {
107+ // fmt.Printf("%d %s %b\n", i+1, item.move.ToString(), item.order)
108+ // }
109+ // print_mutex.Unlock()
79110}
80111
81112func (l MoveList ) Len () int { return len (l ) }
0 commit comments