;================================================================
; mahjong/mahjong_hand 麻将游戏 手牌算法相关
; check_others_discard.erb 别家切牌 判断是否可以操作
;================================================================


;;;判断是否能荣和
;;;- str self_hand 当前自家手牌
;;;- str others_discard 别家的舍张
;;;@return 1能 0不能
@mj_call_ron(self_hand, others_discard)
#FUNCTION
    #DIMS self_hand
    #DIMS others_discard
    #DIM DYNAMIC can_ron
    SIF STRLENSU(self_hand) % 6 != 2 || STRLENSU(others_discard) != 2
        RETURNF 0
    can_ron = mj_agari(self_hand + others_discard)
    RETURNF can_ron


;;;判断是否能杠（大明杠）
;;;- str self_hand 当前自家手牌
;;;- str others_discard 别家的舍张
;;;@return 能杠的组合(字符串形式)
@mj_call_kan(self_hand, others_discard)
#FUNCTIONS
    #DIMS self_hand
    #DIMS others_discard
    #DIM DYNAMIC hand, MJ_HAND_LIMIT
    #DIM DYNAMIC own_red_dora
    #DIM DYNAMIC discard
    #DIM DYNAMIC i
    #DIMS DYNAMIC kan_options
    self_hand '= TOLOWER(self_hand)
    others_discard '= TOLOWER(others_discard)
    SIF STRLENSU(self_hand) % 6 != 2 || STRLENSU(others_discard) != 2
        RETURNF ""
    FOR i, 0, STRLENSU(self_hand), 2
        hand:(i / 2) = mj_code_to_value(SUBSTRINGU(self_hand, i, 2))
        IF hand:(i / 2) % 10 == 0
            hand:(i / 2) += 5
            own_red_dora = 1
        ENDIF
    NEXT
    discard = mj_code_to_value(others_discard)
    SIF discard % 10 == 0
        discard += 5
    SIF MATCH(hand, discard) == 3
        kan_options += mj_to_code(discard) * 2 + (own_red_dora ? mj_to_code(discard - 5) # mj_to_code(discard))
    ; ;#; PRINTFORML %GETTIMES()% |  TEST | %self_hand, 26% + %others_discard% = %kan_options%
    RETURNF kan_options


;;;判断是否能碰
;;;- str self_hand 当前自家手牌
;;;- str others_discard 别家的舍张
;;;@return 能碰的组合(字符串形式)
@mj_call_pon(self_hand, others_discard)
#FUNCTIONS
    #DIMS self_hand
    #DIMS others_discard
    #DIM DYNAMIC hand, MJ_HAND_LIMIT
    #DIM DYNAMIC own_red_dora
    #DIM DYNAMIC discard
    #DIM DYNAMIC i
    #DIMS DYNAMIC pon_options
    self_hand '= TOLOWER(self_hand)
    others_discard '= TOLOWER(others_discard)
    SIF STRLENSU(self_hand) % 6 != 2 || STRLENSU(others_discard) != 2
        RETURNF ""
    FOR i, 0, STRLENSU(self_hand), 2
        hand:(i / 2) = mj_code_to_value(SUBSTRINGU(self_hand, i, 2))
        IF hand:(i / 2) % 10 == 0
            hand:(i / 2) += 5
            own_red_dora = 1
        ENDIF
    NEXT
    discard = mj_code_to_value(others_discard)
    SIF discard % 10 == 0
        discard += 5
    IF MATCH(hand, discard) >= 2
        IF own_red_dora
            pon_options += mj_to_code(discard) + mj_to_code(discard - 5)
            SIF MATCH(hand, discard) == 3
                pon_options += "," + mj_to_code(discard) * 2
        ELSE
            pon_options += mj_to_code(discard) * 2
        ENDIF
    ENDIF
    ; ;#; PRINTFORML %GETTIMES()% |  TEST | %self_hand, 26% + %others_discard% = %pon_options%
    RETURNF pon_options


;;;判断是否能吃
;;;- str self_hand 当前自家手牌
;;;- str others_discard 别家的舍张
;;;@return 能吃的组合(字符串形式)
@mj_call_chii(self_hand, others_discard)
#FUNCTIONS
    #DIMS self_hand
    #DIMS others_discard
    #DIM DYNAMIC hand, MJ_HAND_LIMIT
    #DIM DYNAMIC own_red_dora
    #DIM DYNAMIC discard
    #DIM DYNAMIC owned_tile, 3, 2
    #DIM DYNAMIC exist_option
    #DIM DYNAMIC i
    #DIM DYNAMIC j
    #DIMS DYNAMIC chii_options
    self_hand '= TOLOWER(self_hand)
    others_discard '= TOLOWER(others_discard)
    SIF STRLENSU(self_hand) % 6 != 2 || STRLENSU(others_discard) != 2 || SUBSTRINGU(others_discard, 1) == "z"
        RETURNF ""
    FOR i, 0, STRLENSU(self_hand), 2
        hand:(i / 2) = mj_code_to_value(SUBSTRINGU(self_hand, i, 2))
        IF hand:(i / 2) % 10 == 0
            hand:(i / 2) += 5
            own_red_dora = 1
        ENDIF
    NEXT
    discard = mj_code_to_value(others_discard)
    SIF discard % 10 == 0
        discard += 5
    owned_tile:0:0 = discard - 2
    owned_tile:0:1 = discard - 1
    owned_tile:1:0 = discard - 1
    owned_tile:1:1 = discard + 1
    owned_tile:2:0 = discard + 1
    owned_tile:2:1 = discard + 2
    FOR i, 0, 3
        IF MATCH(hand, owned_tile:i:0) && MATCH(hand, owned_tile:i:1)
            chii_options += (exist_option ? "," # "") + mj_to_code(owned_tile:i:0) + mj_to_code(owned_tile:i:1)
            exist_option = 1
            SIF own_red_dora && (owned_tile:i:0) % 10 == 5 && MATCH(hand, owned_tile:i:0) > 1
                chii_options += @",%mj_to_code(owned_tile:i:0 - 5)%%mj_to_code(owned_tile:i:1)%"
            SIF own_red_dora && (owned_tile:i:1) % 10 == 5 && MATCH(hand, owned_tile:i:1) > 1
                chii_options += @",%mj_to_code(owned_tile:i:0)%%mj_to_code(owned_tile:i:1 - 5)%"
        ENDIF
    NEXT
    ; ;#; PRINTFORML %GETTIMES()% |  TEST | %self_hand, 26% + %others_discard% = %chii_options%
    RETURNF chii_options
