#apply
#include "../_include.txt"


cev[CEV_BRIANS_WORKING] .name("mg_ブライアンの通常業務") {
    def {
        NUM_OF_VAR = 24
        NUM_OF_STR = 8
        NUM_OF_PIC = 10

        BASE_LAYER = 6

        PATH_BACK1 = "cm\da"
        PATH_BACK2 = "bw\bg2"
        PATH_CHAR  = "bw\char"
        PATH_DESC  = "bw\desc"

        PIC_HEAD   = 1
        PIC_BACK1  = 2
        PIC_BACK2  = 3
        PIC_DESC   = 4
        PIC_GOMEZ  = 5
        PIC_CHAR1  = 6
        PIC_CHAR2  = 7
        PIC_CHOICE = 8
        PIC_STATUS = 9

        //勇者
        BR_TYPE_ALEX = 0
        BR_TYPE_RINAX
        BR_TYPE_RENAX
        BR_TYPE_ROLEX
        BR_TYPE_MAMAX
        BR_TYPE_PAPAX
        BR_TYPE_NUM

        //仕事の回数
        MATCH_ONESET = 10
    }
    
    //変数定義
    defv {
        varHead = TV_MINIGAME
        
        ipt
        cell
        idx
        tm1
        tm2
        tm3

        gameSpeed
        rightChoice
        matchNum
        brave
        income
        success
        numLucky
        numSuccess
        numPass
        numFailed

        varFoot
    }

    deft {
        strLine = TS_MINIGAME
        strSpeed
        strLeft
        strRight
        strThrough

    }

    commentRangeOfVariables(varFoot, TV_MINIGAME, NUM_OF_VAR, NUM_OF_STR, NUM_OF_PIC)

    //ピクチャの共通コマンド
    def {
        basePicCmd = {
            .chromakey(1)
            .mapLayer(BASE_LAYER)
            .eraseWhenTransfer
            .affectedByTint
            .affectedByFlash
            .affectedByShake

        }

        bgPicCmd = {
            .grid(1,4)
            basePicCmd
        }

        descPicCmd = {
            basePicCmd
        }

        filterPicCmd = {
            .pos(320/2, 240/2) .center
            .scale2(100*320/16, 100*240/16)
            basePicCmd
        }

        charaPicCmd = {
            .grid(4, 2)
            basePicCmd
        }

        baseStrPicCmd = {
            .size(0, 0)
            .chromakey(1)
            .font("", 12)
            .spacing(0, 4)
            .skin ""
            .mapLayer(9)//メッセージウィンドウより上
            .eraseWhenTransfer
            .affectedByTint
            //.affectedByFlash
            .affectedByShake
        }
    }

    __fn _setFace $id bl {
        @msg.face("brians_working", $id, .left)
    }

    __fn _setFace_rtp $id bl {
        @msg.face("Actor1_2k", $id, .left)
    }


    //------------------------------------------------------------------------------------
    //開始処理
    bl {
        initPicturesAndVariables(PIC_HEAD, NUM_OF_PIC, TV_MINIGAME, NUM_OF_VAR, TS_MINIGAME, NUM_OF_STR)
        storeAndStopBGM()
        msgBottom()
        
        //背景
        @pic.show[PIC_BACK1] PATH_BACK1 filterPicCmd
        @pic.show[PIC_BACK2] PATH_BACK2 .pos(320, 0) .topLeft .scale(100) .trans(100) .cell(1) bgPicCmd
        @pic.show[PIC_DESC] PATH_DESC .pos(160, 0) .top .scale(100) .trans(100) descPicCmd
        //キャラクター
        @pic.show[PIC_CHAR1] PATH_CHAR .trans(100) .cell(1) charaPicCmd
        @pic.show[PIC_CHAR2] PATH_CHAR .trans(100) .cell(1) charaPicCmd
        @pic.show[PIC_GOMEZ] PATH_CHAR .trans(100) .cell(8) charaPicCmd
        @wait(1)

        //ゲーム開始
        @label.jump "selectLevel"
    }

    //------------------------------------------------------------------------------------
    //速度選択
    @label.set "selectLevel"
    bl {
        // 説明(最初だけ表示する)
        @pic.show[PIC_DESC] PATH_DESC .pos(160, 8) .top .trans(100) descPicCmd
        @pic.move[PIC_DESC] .pos(160, 0) .top .time(1) .trans(0)

        //速度選択
        @msg.show "\>ゲーム速度を選択してください"
        @msg.choice bl {
            .case "\>遅い" {
                gameSpeed = 4
            }
            .case "\>普通" {
                gameSpeed = 3
            }
            .case "\>速い" {
                gameSpeed = 2
            }
            .cancel bl {
                @label.jump "gameEnd"
            }
        }

        @wait(2)

        // 説明を削除
        hidePicture(PIC_DESC)

        @label.jump "gameLoop"
    }

    //----------------------------
    //ゲーム処理
    @label.set "gameLoop"
    bl {
        //\.は三つ \^で自動送り

        //BGM
        startBGM("rpgmk95_f1", FALSE, 0, 100, 100, 50)

        //変数初期化
        matchNum = 0
        v[__id(numLucky)..__id(numFailed)] = 0
        //最初の勇者はアレックスで固定
        brave = BR_TYPE_ALEX

        //開始
        @se.play "Wind1" .opt(100, 150, 50)

        @loop MATCH_ONESET bl {
            //中断判定
            @key.input .dst(ipt) .cancel
            @if ipt != 0 bl {
                @break
            }

            //ステータス
            tm1 = matchNum + 1
            @pic.strpic[PIC_STATUS] __str("\", tm1, " / ", MATCH_ONESET) .center .pos(160,56) .scale(100) .trans(0) baseStrPicCmd
            //背景を変える
            cell = rnd(1, 4)
            @pic.show[PIC_BACK2] PATH_BACK2 .pos(320, 60) .topLeft .scale(100) .trans(100) .cell(cell) bgPicCmd
            @pic.move[PIC_BACK2] .pos(0, 60) .topLeft .scale(100) .trans(0) .time(1)
            //ブライアン
            @pic.show[PIC_CHAR1] PATH_CHAR .pos(320, 120) .center .trans(100) .cell(1) charaPicCmd
            @pic.move[PIC_CHAR1] .pos(240, 120) .center .scale(100) .trans(0) .time(1)
            @wait(1)
            //勇者
            cell = 1 + 1 + brave
            @pic.show[PIC_CHAR2] PATH_CHAR .pos(0, 120) .center .trans(100) .cell(cell) charaPicCmd
            @pic.move[PIC_CHAR2] .pos(80, 120) .center .scale(100) .trans(0) .time(1)
            @wait(1)
            //開始
            success = 0
            //蓄積分
            strLine .asg("")
            //速度調節用文字列
            //gameSpeed値が大きいほど遅い
            strSpeed .asg("")
            @loop gameSpeed bl {
                strSpeed .cat("\.")
            }

            @loop 3 .dst(idx) bl {
                //効果音
                __loop [ bl { @se.play "Attack2" .opt(100, 100, 50) },
                bl { @se.play "Flash1" .opt(90, 100, 50) },
                bl { @se.play "Flash1" .opt(90, 140, 50) } ] $block $idx bl {
                    @if idx == $idx bl {
                        $block
                    }
                }

                //正解の選択肢
                rightChoice = rnd(0, 1)
                //アレックス！
                @if idx == 0 bl {
                    strThrough .asg("ウホッ！")
                    @loop 2 .dst(tm1) bl {
                        //入力する名前の番号
                        @if rightChoice == tm1 bl {
                            tm2 = brave
                        } .else bl {
                            tm2 = brave + rnd(1, BR_TYPE_NUM - 1)
                            tm2 %= BR_TYPE_NUM
                        }
                        //入力する場所
                        tm3 = tm1 + __id(strLeft)
                        //入力
                        __loop [ "アレックス！",
                        "リナックス！",
                        "レナックス！",
                        "ロレックス！",
                        "ママックスさん！",
                        "パパックスさん！" ] $str $idx bl {
                            @if tm2 == $idx bl {
                                t[tm3] .asg($str)
                            }
                        }
                    }

                //俺を仲間に
                } .elif idx == 1 bl {
                    strThrough .asg("イイ男…")
                    //正解
                    tm3 = rightChoice + __id(strLeft)
                    t[tm3] .asg("俺を仲間に")
                    //不正解
                    tm3 = (rightChoice + 1) % 2 + __id(strLeft)
                    tm1 = rnd(0, 2)
                    __loop [ "オレオの間に",
                    "俺に袴を",
                    "俺を合間に" ] $str $idx bl {
                        @if tm1 == $idx bl {
                            t[tm3] .asg($str)
                        }
                    }
                //入れてくれよ！
                } .elif idx == 2 bl {
                    strThrough .asg("やらないか")
                    //正解
                    tm3 = rightChoice + __id(strLeft)
                    t[tm3] .asg("入れてくれよ！")
                    //不正解
                    tm3 = (rightChoice + 1) % 2 + __id(strLeft)
                    tm1 = rnd(0, 2)
                    __loop [ "挟んでくれよ！",
                    "着せてくれよ！",
                    "人れてくれよ！" ] $str $idx bl {
                        @if tm1 == $idx bl {
                            t[tm3] .asg($str)
                        }
                    }
                }

                //選択肢を表示する
                @pic.strpic[PIC_CHOICE] __str("\c[4]$v\c[0] : \c[7]\", strLeft, "\c[0]", NEWLINE, "\c[4]$t\c[0] : \c[7]\", strRight, "\c[0]") .topLeft .pos(82, 188) .scale2(100, 10) .trans(0) .nobg .noframe .noPadding baseStrPicCmd
                @pic.move [PIC_CHOICE] .relative .pos(0, 0) .scale2(0, 90) .trans(0) .time(1)
                //顔グラ
                cell = [0, 1, 2][idx]
                _setFace(cell)
                //メッセージ更新
                @msg.show __str("\>\", strLine, "\<\", strSpeed, "\^")
                //入力を取得
                @key.input .dst(ipt) bl {
                    .left
                    .right
                }

                //入力方向に対応した文字列を追加する
                @if ipt == KEY_LEFT bl {
                    strLine .cat(strLeft)
                    success += rightChoice == 0 ? 1 : 0
                }

                @if ipt == KEY_RIGHT bl {
                    strLine .cat(strRight)
                    success += rightChoice == 1 ? 1 : 0
                }

                @if ipt == KEY_NONE bl {
                    strLine .cat(strThrough)
                }
            }

            //選択肢を隠す
            @pic.move [PIC_CHOICE] .pos(0, 0) .scale(100) .trans(100)
            //完成
            @se.play "Flash2" .opt(90, 100, 50)
            @scr.flash .once(2) .rgbv(31, 31, 31, 31)
            _setFace(1)
            @msg.show __str("\>\c[2]\", strLine, "\c[0]\<\|\^")
            //評価
            //特殊演出判定
            tm1 = FALSE
            __loop [ "イイ男…やらないか",
            "合間に入れてくれよ",
            "合間に挟んでくれよ",
            "イイ男…入れてくれよ" ] $str bl {
                @if strLine .contains $str bl {
                    tm1 = TRUE
                }
            }

            //特殊演出(失敗)
            @if tm1 == TRUE bl {
                numFailed += 1

                //ゴメス
                @pic.show[PIC_GOMEZ] PATH_CHAR .pos(320, 120) .center .trans(0) .cell(8) charaPicCmd
                @pic.move[PIC_GOMEZ] .pos(280, 120) .center .scale(100) .trans(0) .time(1)
                @se.play "Slash5" .opt(100, 100, 50)
                _setFace_rtp(12)
                tm1 = rnd(0, 2)
                __loop [ "",
                "よかろう",
                "ワシに任せろ" ] $str $idx bl {
                    @if tm1 == $idx bl {
                        @msg.show __str("\>", $str, "\<\.\", strSpeed ,"\^")
                    }
                }
                //効果音
                tm1 = rnd(0, 2)
                __loop [ bl { @se.play "voice_aa" .opt(100, 80, 50) },
                bl { @se.play "Monster1" .opt(100, 150, 50) },
                bl { @se.play "Collapse2" .opt(100, 100, 50) } ] $block $idx bl {
                    @if tm1 == $idx bl {
                        $block
                    }
                }
                //ブライアン
                _setFace(3)
                @msg.show __str("\", strSpeed,"\^")

            //成功
            } .elif success >= 3 bl {
                numSuccess += 1
                //仲間にしてもらえた
                @if `rnd(0, 4) == 0 bl {
                    numLucky += 1

                    //勇者
                    tm1 = [5, 6, 8, 10, 12, 14][brave]
                    _setFace(tm1)
                    __loop [ "うーん、今回は頼むか",
                    "いいよーでも近寄らないでね",
                    "ちょうどアイテム係を探してたの",
                    "お願いしますブライアンさん！",
                    "護衛をお願いしようかしら",
                    "いいだろう。しかし無理はするな" ] $str $idx bl {
                        @if brave == $idx bl {
                            @msg.show __str("\>", $str, "\<\", strSpeed ,"\^")
                        }
                    }
                    //効果音
                    tm1 = rnd(0, 2)
                    __loop [ bl { @se.play "voice_hohou" .opt(90, 120, 50) },
                    bl { @se.play "voice_ok" .opt(100, 120, 50) },
                    bl { @se.play "voice_ieeei2" .opt(90, 100, 50) } ] $block $idx bl {
                        @if tm1 == $idx bl {
                            $block
                        }
                    }
                    //ブライアン
                    _setFace(2)
                    @msg.show __str("\", strSpeed,"\^")

                //いつも通り
                } .else bl {
                    @pic.move[PIC_CHAR2] .relative .pos(160, 0) .scale(0) .trans(0) .time(1)
                    @wait .frame(8)
                    @pic.move[PIC_CHAR1] .relative .pos(160, -160) .scale(0) .trans(0) .time(1)
                    //効果音
                    tm1 = rnd(0, 2)
                    __loop ["Blow2", "Blow3", "Blow4"] $str $idx bl {
                        @if tm1 == $idx bl {
                            @se.play $str .opt(100, 100, 50)
                        }
                    }

                    //勇者
                    tm1 = [4, 6, 8, 11, 13, 14][brave]
                    _setFace(tm1)
                    __loop [ "邪魔",
                    "邪魔",
                    "邪魔",
                    "ごめんなさい", 
                    "いつも元気ね",
                    "すまないな" ] $str $idx bl {
                        @if brave == $idx bl {
                            @msg.show __str("\>", $str, "\<\", strSpeed ,"\^")
                        }
                    }
                }

            //少し間違えた
            } .elif success >= 2 bl {
                numPass += 1

                @pic.move[PIC_CHAR2] .relative .pos(160, 0) .scale(0) .trans(0) .time(1)
                @wait .frame(8)
                @pic.move[PIC_CHAR1] .relative .pos(160, -160) .scale(0) .trans(0) .time(1)

                //効果音
                tm1 = rnd(0, 2)
                __loop ["Blow2", "Blow3", "Blow4"] $str $idx bl {
                    @if tm1 == $idx bl {
                        @se.play $str .opt(100, 100, 50)
                    }
                }

                //勇者
                tm1 = [5, 7, 9, 11, 12, 15][brave]
                _setFace(tm1)
                __loop [ "何言ってんだ",
                "誤字ってない？",
                "うるさい",
                "これでいいんだよね？",
                "噛んだのかしら？",
                "悪いが急いでいるんだ" ] $str $idx bl {
                    @if brave == $idx bl {
                        @msg.show __str("\>", $str, "\<\", strSpeed ,"\^")
                    }
                }
            //失敗
            } .else bl {
                numFailed += 1

                //少し間がある
                @wait(1)
                //勇者
                tm1 = [4, 7, 9, 11, 12, 15][brave]
                _setFace(tm1)
                __loop [ "は？",
                "え？",
                "なに？", 
                "何を言ってるの？",
                "良く聞こえなかったわ",
                "落ち着いて話してくれ" ] $str $idx bl {
                    @if brave == $idx bl {
                        @msg.show __str("\>", $str, "\<\", strSpeed ,"\^")
                    }
                }
                //効果音
                tm1 = rnd(0, 2)
                __loop [ bl { @se.play "bell1" .opt(100, 80, 50) },
                bl { @se.play "voice_aa" .opt(100, 80, 50) },
                bl { @se.play "Damage2" .opt(100, 100, 50) } ] $block $idx bl {
                    @if tm1 == $idx bl {
                        $block
                    }
                }
                //ブライアン
                _setFace(3)
                @msg.show __str("\", strSpeed,"\^")
            }

            //回数増加
            matchNum += 1
            //次の勇者
            brave = rnd(BR_TYPE_ALEX, BR_TYPE_PAPAX)
            //ピクチャを隠す
            __loop [PIC_BACK2, PIC_GOMEZ, PIC_CHAR1, PIC_CHAR2, PIC_CHOICE, PIC_STATUS] $pid {
                @pic.move[$pid] .pos(0, 0) .scale(0) .trans(100)
            }
            //場面転換
            @se.stop
            @se.play "Wind1" .opt(100, 150, 50)
            @wait(1)
        }
        //実績
        checkSetAchievement(achievedWorking, numSuccess >= MATCH_ONESET && gameSpeed <= 3)
        //報酬計算
        income = numLucky * 30 + numSuccess * 25 + numPass * 8 + numFailed * 2
        //総収入に加える
        briansWorkingIncome += income
        //実際に所持金に加える
        @party.money .add(income)
        //結果発表
        @msg.face("", 0, .left)
        @msg.show __str("\>\c[2]【 結果発表 】\c[0]", NEWLINE,
                        "\>成功: \c[7]\", numSuccess, "\c[0]  合格: \c[7]\",numPass, "\c[0]  失敗: \c[7]\",numFailed, "\c[0]", NEWLINE,
                        "\>ラッキー: \c[7]\", numLucky, "\c[0]")
        @se.play "register" .opt(100, 100, 50)
        @msg.show __str("\>\c[2]【 報酬 】\c[0]", NEWLINE,
                        "\>\c[7]\", income, "\c[0] PT也\$")
        //選択肢
        @msg.choice bl {
            .cancel(2)
            .case "\>続ける" {
                @label.jump "gameLoop"
            }
            .case "\>速さを変える" {
                @label.jump "selectLevel"
            }
            .case "\>やめる" {
                @label.jump "gameEnd"
            }
        }
    }

    //------------------------------------------------------------------------------------
    //終了する
    @label.set "gameEnd"
    bl {
        initPicturesAndVariables(PIC_HEAD, NUM_OF_PIC, TV_MINIGAME, NUM_OF_VAR, TS_MINIGAME, NUM_OF_STR)
        restoreBGM()
        fastFadeInTintOnly(1)
    }
}