let GetTargetArr_Enemy距離 = function () {
    let rtnArr = []; 
    for (let i = 0; i <= NEnemy.length - 1; i++) { 
        if (NEnemy[i]._hp <= 0) {
            continue; 
        }
        if (rtnArr.length == 0) {
            rtnArr.push(i);
            continue;
        }
        let targetX = NEnemy[i]._screenX;
        let addFlg = true;
        for (let j = 0; j <= rtnArr.length - 1; j++) {
            let checkX = NEnemy[rtnArr[j]]._screenX;
            if (targetX >= checkX) {
                rtnArr.splice(j, 0, i); 
                addFlg = false;
                break;
            }
        }
        if (addFlg) {
            rtnArr.push(i);
        }
    }
    return rtnArr;
}
let TargetArr = []; 
Game_Interpreter.prototype.TargetSelect = function (NPlayerNo, skillId, moveTypeCng) {
    moveTypeCng = typeof moveTypeCng !== 'undefined' ? moveTypeCng : -1;
    TargetArr = []; 
    TgEffArr = []; 
    EmaePosX = NPlayer[NPlayerNo]._screenX;
    EmaePosY = NPlayer[NPlayerNo]._screenY;
    let targetScope = $dataSkills[skillId].scope;
    let moveType = $dataSkills[skillId].stypeId;
    let memo対象Str = $dataSkills[skillId].meta["対象"];
    let _遠距離Flg = (memo対象Str == "遠距離"); 
    let targetArr = GetTargetArr_Enemy距離(); 
    if (moveTypeCng != -1) moveType = moveTypeCng;
    if (moveTypeCng == -2) { 
        moveType = 0;
    }
    let _単対象No = targetArr[0];
    if (_遠距離Flg) _単対象No = targetArr[targetArr.length - 1];
    if (NEnemy[_単対象No] == undefined) return; 
    if (moveType == EnBtlMove.m_近接) moveType = EnBtlMove.m_少し前;
    switch (moveType) {
        case EnBtlMove.m_近接: 
            TgMoveX = NEnemy[_単対象No]._screenX + NEnemy[_単対象No]._pWidth / 2 + 103;
            TgMoveY = NEnemy[_単対象No]._screenY;
            break;
        case EnBtlMove.m_正面: 
            TgMoveX = NPlayer[NPlayerNo]._screenX;
            TgMoveY = NEnemy[_単対象No]._screenY;
            break;
        case EnBtlMove.m_中央: 
            TgMoveX = 309;
            TgMoveY = 373;
            break;
        case EnBtlMove.m_少し前: 
            TgMoveX = NPlayer[NPlayerNo]._screenX - 64;
            TgMoveY = NPlayer[NPlayerNo]._screenY;
            break;
        default: 
            TgMoveX = NPlayer[NPlayerNo]._screenX;
            TgMoveY = NPlayer[NPlayerNo]._screenY;
            break;
    }
    setMoveType = moveType;
    let allAtkFlg = CheckStateInType(NPlayer[NPlayerNo]._NState, StType.AtkAll);
    if (memo対象Str != undefined && memo対象Str != "") {
        if (allAtkFlg) {
            targetScope = EnAtkRange.敵単体;
        } else {
            switch (memo対象Str) {
                case "前列": 
                    if (targetArr.length == 1) {
                        targetScope = EnAtkRange.敵単体;
                        break; 
                    }
                    for (let i = 0; i <= targetArr.length - 2; i++) { 
                        let tgtENo = targetArr[i];
                        NEnemy[tgtENo]._TgtOpi = 225; 
                        TargetArr.push(tgtENo);
                        TgEffArr.push(new Pos2D(NEnemy[tgtENo]._screenX, NEnemy[tgtENo]._screenY - 64));
                    }
                    return;
                case "後列": 
                    if (targetArr.length == 1) {
                        targetScope = EnAtkRange.敵単体;
                        break; 
                    }
                    for (let i = 1; i <= targetArr.length - 1; i++) { 
                        let tgtENo = targetArr[i];
                        NEnemy[tgtENo]._TgtOpi = 225; 
                        TargetArr.push(tgtENo);
                        TgEffArr.push(new Pos2D(NEnemy[tgtENo]._screenX, NEnemy[tgtENo]._screenY - 64));
                    }
                    return;
            }
        }
    }
    if (allAtkFlg) { 
        switch (targetScope) {
            case EnAtkRange.敵単体:
            case EnAtkRange.敵ランダム1:
                targetScope = EnAtkRange.敵全体;
                break;
        }
    }
    switch (targetScope) {
        case EnAtkRange.敵単体: 
            TargetArr.push(_単対象No);
            NEnemy[_単対象No]._TgtOpi = 225;
            TgEffArr.push(new Pos2D(NEnemy[_単対象No]._screenX, NEnemy[_単対象No]._screenY - 64));
            let _Turn = GetNStateTurn_ENo(_単対象No, StOneType.SyaseiYoku);
            _射精欲_Check = _Turn;
            break;
        case EnAtkRange.敵全体: 
            for (let i = 0; i <= NEnemy.length - 1; i++) { 
                if (NEnemy[i]._hp < 1) {
                    continue;
                }
                NEnemy[i]._TgtOpi = 225; 
                TgEffArr.push(new Pos2D(NEnemy[i]._screenX, NEnemy[i]._screenY - 64));
                TargetArr.push(i);
            }
            break;
        case EnAtkRange.敵ランダム1:
            for (let i = 0; i <= NEnemy.length - 1; i++) { 
                if (NEnemy[i]._hp < 1) {
                    continue;
                }
                NEnemy[i]._TgtOpi = 180; 
            }
            let targetENo = GetENo_Ramdom();
            TgEffArr.push(new Pos2D(NEnemy[targetENo]._screenX, NEnemy[targetENo]._screenY - 64));
            TargetArr.push(targetENo);
            break;
        case EnAtkRange.使用者:
            NPlayer[NPlayerNo]._TgtOpi = 255;
            TgEffArr.push(new Pos2D(NPlayer[NPlayerNo]._screenX - 26, NPlayer[NPlayerNo]._screenY));
            TargetArr.push(-1);
            break;
        default:
            break;
    }
    for (let i = 0; i < TargetArr.length; i++) {
        if (TargetArr[i] >= 0) {
            let _対象男性Flg = MetaChecker(NEnemy[TargetArr[i]].enemy(), "男性", false);
            if (_対象男性Flg != false) {
                Tgt男性Flg = true;
            }
        }
    }
}
let GetENo_Ramdom = function () {
    let randomInt = 0;
    let exitFlg = true;
    for (let i = 0; i <= NEnemy.length - 1; i++) {
        if (NEnemy[i]._hp > 0) {
            exitFlg = false;
        }
    }
    if (exitFlg) return randomInt;
    while (true) {
        randomInt = Math.floor(Math.random() * NEnemy.length);
        if (NEnemy[randomInt]._hp > 0) {
            break;
        }
    }
    return randomInt;
}
let CheckDeckInWakutin = function (setstNo) {
    if ($gameSystem.AntiVirusNos == undefined) $gameSystem.AntiVirusNos = [];
    $gameSystem.AntiVirusNos = [286];
    for (let i = 0; i < $gameSystem.AntiVirusNos.length; i++) {
        var itemNo = $gameSystem.AntiVirusNos[i];
        var item = $dataItems[itemNo]; 
        if (item.effects.length == 0) return false;
        if (item.effects[0].dataId == setstNo) return true;
    }
    return false;
    // switch (setstNo) {
    //         if (setstNo == 17) ckWakutinDataId = KSkill.B型ワクチン;
    //         if (setstNo == 18) ckWakutinDataId = KSkill.H型ワクチン;
    //         if (setstNo == 24) ckWakutinDataId = KSkill.M型ワクチン;
    //         if (setstNo == 19) ckWakutinDataId = KSkill.タトゥーワクチン;
    //         if (setstNo == 20) ckWakutinDataId = KSkill.ピアスワクチン;
    //         if (battleflg) {
    //                 if (_trData.code == 43) {
    //                     if (_trData.dataId == ckWakutinDataId) _rtnData = true;
    //             });
    //         });
}
