//      主人公の各種ステータスの変数宣言と初期設定

#include "okikae_MD.as"
#include "scenario.as"

player_name     ="冒険者"
player_lv       =1
player_mhp      =30
player_hp       =30
player_atack    =5
player_exp      =0
player_next_exp =25
player_gold     =100
own_poshon_lv1  =0
own_poshon_lv2  =0
own_poshon_lv3  =0
own_poshon_lv4  =0
own_poshon_lv5  =0

//      ダンジョンで戦闘時に使用するモンスターの名前,mhp,hp,at,exp,gp
//      ダンジョンLV選択時に代入する
ene_name=""
ene_atp=0
ene_mhp=0
ene_hpp=0
ene_exp=0
ene_gpp=0

//      ここからはボタン関係の変数
//      これらの変数の中身をobjprmに代入する
btstr_1=""
btstr_2=""
btstr_3=""
btstr_4=""
btstr_5=""
btstr_6=""

//      ボタンが押された時にこれらの変数をselected_btnextに代入して
//      メインルーチンにジャンプする
btnext_1=""
btnext_2=""
btnext_3=""
btnext_4=""
btnext_5=""
btnext_6=""

//      この変数が1である限りメインルーチンがループする
//      最初に0を代入しているが1を代入する事でメインルーチンが繰り返される
//      switch分岐に用いるボタンを押す事によって入力されるが
//      switch管理内において直接入力してもよい
//      その場合switch_againに1を代入する事が多い
switch_again=0
//      ボタンによって選択される選択肢のID
selected_btnext="@OP@"
//      探索するダンジョンのLV
enter_dglv=0
//      mesを直接入力しない時に用いる
//      叙述内に変数を挿入する場合に用いる事が多い
sdim dr_str,1000
dr_str=""
//      ダンジョン探索の達成度 ダンジョン攻略のレベル
//      1匹倒すごとに1上昇し5になるとダンジョン攻略完成 経験値とお金がもらえる
//      ダンジョンLV5を攻略するとゲームクリア
dg_atteinment=0
//      ポーション購入及び使用の時選択されたポーションのレベル
sl_pos_lv=0


title "テキストRPGサンプル"
sdim nt_read,3000
dim num_nt_read,1
sdim 型_nt_read,100 
sdim 中身_nt_read,3000
sdim to_okikae,500

//      メインスクリプトへジャンプ
goto *MAIN

//      モンスターの名前と各種ステータスを代入する関数


#deffunc monster_st_set int
mref monster_lv,0
switch monster_lv
case 1
        ene_name="ゴブリン"
        ene_mhp=15
        ene_hpp=ene_mhp
        ene_atp=10
        ene_exp=25
        ene_gpp=50
        swbreak
case 2
        ene_name="スケルトン"
        ene_mhp=30
        ene_hpp=ene_mhp
        ene_atp=20
        ene_exp=50
        ene_gpp=90
        swbreak
case 3
        ene_name="ジャイアント"
        ene_mhp=60
        ene_hpp=ene_mhp
        ene_atp=40
        ene_exp=100
        ene_gpp=160
        swbreak
case 4
        ene_name="ヒドラ"
        ene_mhp=120
        ene_hpp=ene_mhp
        ene_atp=80
        ene_exp=200
        ene_gpp=280
        swbreak
case 5
        ene_name="ドラゴン"
        ene_mhp=240
        ene_hpp=ene_mhp
        ene_atp=160
        ene_exp=0
        ene_gpp=0
        swbreak
swend
return


#deffunc hennsuu_okikae_a str,val
mref s1,32
mref s2,17
        to_okikae=s1
        str s2
        置き換え文字列設定A dr_str
        置き換え文字列設定B to_okikae,s2
        置き換え実行 dr_str
        int s2
return
#deffunc hennsuu_okikae_b str,val
mref s1,32
mref s2,25
        to_okikae=s1
        置き換え文字列設定A dr_str
        置き換え文字列設定B to_okikae,s2
        置き換え実行 dr_str
return

*MAIN



//      型は原則6文字だがパラメータが無い場合
//      終端文字の関係でスペースで6文字埋まってくれないので
//      etextとd_staは5文字にしてある

//      ノートパッドに読み込む文字列の
//      操作対象が何行目にあるのかを示す
dim nr_id,1

//      ノートパッドに設定されている文字列が何行あるか調べる
dim num_nr_id,1
notesel dt_op_text

//      中身が"end   "と等しくなるまでノートパッドを読んで
//      シナリオの長さを求めています
repeat 8000
noteget nt_read,num_nr_id
strmid 型_nt_read,nt_read,0,6
num_nr_id++
if 型_nt_read="end   " :break
loop

//      メインルーチン
*bt_selected_gomain

nr_id=0

//      シナリオは原則6文字か5文字の型と7文字目以降の中身に分割して処理しています
//      ここでは型が"this  "か"sub   "で中身が現在の選択肢
//      つまり、中身がselected_btnextと等しくなるまで読み捨てています
//      中身がselected_btnextと等しいと、nr_idをしてbreakです
repeat num_nr_id
noteget nt_read,nr_id
strlen num_nt_read,nt_read
strmid 型_nt_read,nt_read,0,6
strmid 中身_nt_read,nt_read,6,num_nt_read-6
if 型_nt_read="this  " {
        if 中身_nt_read=selected_btnext {nr_id++:break }  }
if 型_nt_read="sub   " {
        if 中身_nt_read=selected_btnext {nr_id++:break }  }
nr_id++
loop
/*      型による処理の分岐を行っています

        ・型の種類
        "this  "シナリオの名前、これと選択肢が一致すると
        そのシナリオに応じた代入処理を行って、描画

        "bstr1 "ボタン1に表示する文字列
        "bstr2 "ボタン2に表示する文字列
        "bstr3 "ボタン3に表示する文字列
        "bstr4 "ボタン4に表示する文字列
        "bstr5 "ボタン5に表示する文字列
        "bstr6 "ボタン6に表示する文字列

        "next1 "ボタン1を押した次に進むシナリオ
        "next2 "ボタン2を押した次に進むシナリオ
        "next3 "ボタン3を押した次に進むシナリオ
        "next4 "ボタン4を押した次に進むシナリオ
        "next5 "ボタン5を押した次に進むシナリオ
        "next6 "ボタン6を押した次に進むシナリオ

        "nm_bt "中身の数字の数だけボタンのを描画(ボタンは最大6個)

        "stext "描画用文字列の初期化と一行目の代入
        "ptext "描画用文字列に初期化しないで、中身を描画用文字列に加算
        "ptext"描画用文字列に改行を加算
        "etext"描画用文字列を変数を置き換えた後、描画

        "d_sta"ステータスの描画

        "sub   "サブルーチンの名前、これと選択肢が一致すると
        そのサブルーチンを実行
        (実際にはサブルーチンはHSPで記述)

        "next  "ボタンを押す処理で次の選択肢を決定するのではなく
        直接、次の選択肢を指定

        "gosub "中身でサブルーチンを分岐して、選択してサブルーチンを処理
        (サブルーチン内の処理はHSPで記述)     */
repeat num_nr_id
notesel dt_op_text
noteget nt_read,nr_id
strlen num_nt_read,nt_read
strmid 型_nt_read,nt_read,0,6
strmid 中身_nt_read,nt_read,6,num_nt_read-6
nr_id++

//      型が"bstr1 "、"bstr2 "、"bstr3 "、"bstr4 "、"bstr5 "、"bstr6 "の処理
//      ボタンに表示する文字列を代入
if 型_nt_read="bstr1 "   :       btstr_1=中身_nt_read
if 型_nt_read="bstr2 "   :       btstr_2=中身_nt_read
if 型_nt_read="bstr3 "   :       btstr_3=中身_nt_read
if 型_nt_read="bstr4 "   :       btstr_4=中身_nt_read
if 型_nt_read="bstr5 "   :       btstr_5=中身_nt_read
if 型_nt_read="bstr6 "   :       btstr_6=中身_nt_read

//      型が"next1 "、"next2 "、"next3 "、"next4 "、"next5 "、"next6 "の処理
//      ボタンを押した次に進むシナリオを代入
if 型_nt_read="next1 "   :       btnext_1=中身_nt_read
if 型_nt_read="next2 "   :       btnext_2=中身_nt_read
if 型_nt_read="next3 "   :       btnext_3=中身_nt_read
if 型_nt_read="next4 "   :       btnext_4=中身_nt_read
if 型_nt_read="next5 "   :       btnext_5=中身_nt_read
if 型_nt_read="next6 "   :       btnext_6=中身_nt_read

//      型が"next  "の処理
//      直接、次の選択肢を指定
if 型_nt_read="next  "   :       selected_btnext=中身_nt_read

//      型が"nm_bt"の処理
//      中身の数字の数だけボタンのを描画(ボタンは最大6個)
if 型_nt_read="nm_bt "   {
        if 中身_nt_read="1":gosub *one_bt
        if 中身_nt_read="2":gosub *two_bt
        if 中身_nt_read="3":gosub *three_bt
        if 中身_nt_read="4":gosub *four_bt
        if 中身_nt_read="5":gosub *five_bt
        if 中身_nt_read="6":gosub *six_bt 
}
//      型が"stext "の処理
//      描画用文字列の初期化と一行目の代入
if 型_nt_read="stext "   :dr_str =中身_nt_read
//      型が"ptext "の処理
//      描画用文字列に初期化しないで、中身を描画用文字列に加算
if 型_nt_read="ptext "   {dr_str+="\n":dr_str+=中身_nt_read}
//      型が"ptext"の処理
//      描画用文字列に改行を加算
if 型_nt_read="ptext"    {dr_str+="\n"}
//      型が"etext"の処理
//      描画用文字列を変数を置き換えた後、描画
if 型_nt_read="etext"    {
//      シナリオの中に存在する変数を置き換える
//      hennsuu_okikae_a        p1:str p2:数値型変数
//      hennsuu_okikae_b        p1:str p2:文字列型変数
        instr instr_dr_str,dr_str,"$sl_pos_lv$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$sl_pos_lv$",sl_pos_lv        }
        instr instr_dr_str,dr_str,"$enter_dglv$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$enter_dglv$",enter_dglv      }
        instr instr_dr_str,dr_str,"$ene_name$"
        if instr_dr_str ! -1 {  hennsuu_okikae_b "$ene_name$",ene_name  }
        instr instr_dr_str,dr_str,"$player_atack$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$player_atack$",player_atack  }
        instr instr_dr_str,dr_str,"$ene_atp$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$ene_atp$",ene_atp    }
        instr instr_dr_str,dr_str,"$ene_exp$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$ene_exp$",ene_exp    }
        instr instr_dr_str,dr_str,"$ene_gpp$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$ene_gpp$",ene_gpp    }
        instr instr_dr_str,dr_str,"$player_mhp$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$player_mhp$",player_mhp      }
        instr instr_dr_str,dr_str,"$player_atack$"
        if instr_dr_str ! -1 {  hennsuu_okikae_a "$player_atack$",player_atack  }
//      文字列の描画
        pos 150,0
        mes dr_str
        }
//      型が"gosub "の処理
//      中身でサブルーチンを分岐して、選択してサブルーチンを処理
//      (サブルーチン内の処理はHSPで記述)
if 型_nt_read="gosub "   {
        if 中身_nt_read="*player_dat_load"{
                gosub *player_dat_load          }
        if 中身_nt_read="*player_dat_save"{
                gosub *player_dat_save          }
//      ポーション購入のサブルーチン
        if 中身_nt_read="*buy_pos_LV1"{
                sl_pos_lv=1
                if player_gold >=5{
                player_gold-=5:own_poshon_lv1++
                selected_btnext="@SHOP_xa@"     }
                else{   selected_btnext="@SHOP_xb@"     }
        }
        if 中身_nt_read="*buy_pos_LV2"{
                sl_pos_lv=2
                if player_gold >=10{
                player_gold-=10:own_poshon_lv2++
                selected_btnext="@SHOP_xa@"     }
                else{   selected_btnext="@SHOP_xb@"     }
        }
        if 中身_nt_read="*buy_pos_LV3"{
                sl_pos_lv=3
                if player_gold >=20{
                player_gold-=20:own_poshon_lv3++
                selected_btnext="@SHOP_xa@"     }
                else{   selected_btnext="@SHOP_xb@"     }
        }
        if 中身_nt_read="*buy_pos_LV4"{
                sl_pos_lv=4
                if player_gold >=40{
                player_gold-=40:own_poshon_lv4++
                selected_btnext="@SHOP_xa@"     }
                else{   selected_btnext="@SHOP_xb@"     }
        }
        if 中身_nt_read="*buy_pos_LV5"{
                sl_pos_lv=5
                if player_gold >=80{
                player_gold-=80:own_poshon_lv5++
                selected_btnext="@SHOP_xa@"     }
                else{   selected_btnext="@SHOP_xb@"     }
        }
//      ダンジョン進入のサブルーチン
//      何LVのダンジョンに入ったか代入しています
//      このLVのダンジョンで出現する敵のステータスの代入も行っています
        if 中身_nt_read="*dg_enter_1"{
                enter_dglv=1
                dg_atteinment=0
                monster_st_set 1
        }
        if 中身_nt_read="*dg_enter_2"{
                enter_dglv=2
                dg_atteinment=0
                monster_st_set 2
        }
        if 中身_nt_read="*dg_enter_3"{
                enter_dglv=3
                dg_atteinment=0
                monster_st_set 3
        }
        if 中身_nt_read="*dg_enter_4"{
                enter_dglv=4
                dg_atteinment=0
                monster_st_set 4
        }
        if 中身_nt_read="*dg_enter_5"{
                enter_dglv=5
                dg_atteinment=0
                monster_st_set 5
        }
//      ポーションを使用して回復を行っています
        if 中身_nt_read="*use_pos_LV1"{
                if own_poshon_lv1>=1{
                        sl_pos_lv=1
                        player_hp+=30
                        own_poshon_lv1-=1
                        if player_mhp < player_hp{      player_hp=player_mhp    }
                        selected_btnext="@DG_xabxa@"    }
                else{   selected_btnext="@DG_xabxb@"    }
        }
        if 中身_nt_read="*use_pos_LV2"{
                if own_poshon_lv2>=1{
                        sl_pos_lv=2
                        player_hp+=60
                        own_poshon_lv2-=1
                        if player_mhp <player_hp{       player_hp=player_mhp    }
                        selected_btnext="@DG_xabxa@"    }
                else{   selected_btnext="@DG_xabxb@"    }
        }
        if 中身_nt_read="*use_pos_LV3"{
                if own_poshon_lv3>=1{
                        sl_pos_lv=3
                        player_hp+=120
                        own_poshon_lv3-=1
                        if player_mhp <player_hp{       player_hp=player_mhp    }
                        selected_btnext="@DG_xabxa@"    }
                else{   selected_btnext="@DG_xabxb@"    }
        }
        if 中身_nt_read="*use_pos_LV4"{
                if own_poshon_lv4>=1{
                        sl_pos_lv=4
                        player_hp+=240
                        own_poshon_lv4-=1
                        if player_mhp <player_hp{       player_hp=player_mhp    }
                        selected_btnext="@DG_xabxa@"    }
                else{   selected_btnext="@DG_xabxb@"    }
        }
        if 中身_nt_read="*use_pos_LV5"{
                if own_poshon_lv5>=1{
                        sl_pos_lv=5
                        player_hp+=480
                        own_poshon_lv5-=1
                        if player_mhp <player_hp{       player_hp=player_mhp    }
                        selected_btnext="@DG_xabxa@"    }
                else{   selected_btnext="@DG_xabxb@"    }
        }
//      主人公のHPの回復
        if 中身_nt_read="*hp_kaihuku"{
                player_hp=player_mhp
        }
//      敵のHPの回復
        if 中身_nt_read="*dg_ene_hp_set"{
                ene_hpp=ene_mhp
        }
//      BATTLEを選んだ場合、勝利、敗北、戦闘継続の条件分岐も行っています
        if 中身_nt_read="*bt_main"{
                ene_hpp-=player_atack
                if ene_hpp > 0{
                        player_hp-=ene_atp
                        if player_hp > 0{       selected_btnext="@BT_a1a@"      }
                        else{                   selected_btnext="@BT_a1b@"      }       }
                else{   selected_btnext="@BT_a1c@"      }
        }
//      探索が終了したか?エンディングへの分岐も行っています
        if 中身_nt_read="*bt_bunnki"{
                dg_atteinment++
                if dg_atteinment <5{    selected_btnext="@DG_xa@"       }
                else{   if enter_dglv=5{selected_btnext="@ED@"  }
                        else{           selected_btnext="@BT_a1c1b@"    }       }
        }
//      お金と経験値の取得、LVアップの条件分岐も行っています
        if 中身_nt_read="*bt_get2"{
                player_exp+=ene_exp
                player_gold+=ene_gpp
                if (player_next_exp <=player_exp)&(player_lv <5){
                        selected_btnext="@BT_a1c1b1a@"  }
                else{   selected_btnext="@BT_a1c1b1b@"  }
        }
//      LVアップ
        if 中身_nt_read="*bt_lvup2"{
                switch player_lv
                case 1
                        player_lv       =2
                        player_mhp      =60
                        player_atack    =10
                        player_next_exp =100
                        swbreak
                case 2
                        player_lv       =3
                        player_mhp      =120
                        player_atack    =20
                        player_next_exp =300
                        swbreak
                case 3
                        player_lv       =4
                        player_mhp      =240
                        player_atack    =40
                        player_next_exp =800
                        swbreak
                case 4
                        player_lv       =5
                        player_mhp      =480
                        player_atack    =80
                        player_next_exp =0
                        swbreak
                swend
        }

        goto  *bt_selected_gomain       }
//      型が"d_sta"の処理。ステータスの描画
if 型_nt_read="d_sta"    :gosub *show_player_status
//      シナリオの一要素は"this  "か"sub   "から
//      次の"this  "、"sub   "、"end   "が出現するまでなので
//      "this  "、"sub   "、"end   "が出現するとそのシナリオの処理は終了
if 型_nt_read="this  "   :break
if 型_nt_read="sub   "   :break
if 型_nt_read="end   "   :break
loop
stop

//      プレイヤーのステータスを表示するサブルーチン
*show_player_status
        mes ""
        mes "冒険者"
        mes "LV"+player_lv+"    Next"+player_next_exp+"("+player_exp+")"
        mes "HP   "+player_hp+"/"+player_mhp
        mes "攻撃力 "+player_atack
        mes "所持金 "+player_gold
        mes "ポーション "+own_poshon_lv1+"/"+own_poshon_lv2+"/"+own_poshon_lv3+"/"+own_poshon_lv4+"/"+own_poshon_lv5
return

//      プレイヤーのステータスを保存するサブルーチン
*player_dat_save
        dim to_save,20
        to_save.0=      player_lv
        to_save.1=      player_mhp
        to_save.2=      player_hp
        to_save.3=      player_atack
        to_save.4=      player_exp
        to_save.5=      player_next_exp
        to_save.6=      player_gold
        to_save.7=      own_poshon_lv1
        to_save.8=      own_poshon_lv2
        to_save.9=      own_poshon_lv3
        to_save.10=     own_poshon_lv4
        to_save.11=     own_poshon_lv5

        bsave "testdt.dat",to_save,160
return

//      プレイヤーのステータスを読み込むサブルーチン
*player_dat_load
        dim to_load,20
        bload "testdt.dat",to_load,160
        player_lv       =to_load.0
        player_mhp      =to_load.1
        player_hp       =to_load.2
        player_atack    =to_load.3
        player_exp      =to_load.4
        player_next_exp =to_load.5
        player_gold     =to_load.6
        own_poshon_lv1  =to_load.7
        own_poshon_lv2  =to_load.8
        own_poshon_lv3  =to_load.9
        own_poshon_lv4  =to_load.10
        own_poshon_lv5  =to_load.11
return

//      ここからはボタン関係の関数

//      ボタンを一つ表示してbtstrの中身をボタンに代入する
*one_bt
        cls 0
        pos 0,0
        objsize 120,24
        button "",*selected1
        objprm 0,btstr_1
return

//      ボタンを二つ表示してbtstrの中身をボタンに代入する
*two_bt
        cls 0
        pos 0,0
        objsize 120,24
        button "",*selected1
        button "",*selected2
        objprm 0,btstr_1
        objprm 1,btstr_2
return

//      ボタンを三つ表示してbtstrの中身をボタンに代入する
*three_bt
        cls 0
        pos 0,0
        objsize 120,24
        button "",*selected1
        button "",*selected2
        button "",*selected3
        objprm 0,btstr_1
        objprm 1,btstr_2
        objprm 2,btstr_3
return

//      ボタンを四つ表示してbtstrの中身をボタンに代入する
*four_bt
        cls 0
        pos 0,0
        objsize 120,24
        button "",*selected1
        button "",*selected2
        button "",*selected3
        button "",*selected4
        objprm 0,btstr_1
        objprm 1,btstr_2
        objprm 2,btstr_3
        objprm 3,btstr_4
return

//      ボタンを五つ表示してbtstrの中身をボタンに代入する
*five_bt
        cls 0
        pos 0,0
        objsize 120,24
        button "",*selected1
        button "",*selected2
        button "",*selected3
        button "",*selected4
        button "",*selected5
        objprm 0,btstr_1
        objprm 1,btstr_2
        objprm 2,btstr_3
        objprm 3,btstr_4
        objprm 4,btstr_5
return

//      ボタンを六つ表示してbtstrの中身をボタンに代入する
*six_bt
        cls 0
        pos 0,0
        objsize 120,24
        button "",*selected1
        button "",*selected2
        button "",*selected3
        button "",*selected4
        button "",*selected5
        button "",*selected6
        objprm 0,btstr_1
        objprm 1,btstr_2
        objprm 2,btstr_3
        objprm 3,btstr_4
        objprm 4,btstr_5
        objprm 5,btstr_6
return

//      ID0のボタンが押された時にselected_btnextにbtnext_1の中身を代入して
//      メインルーチンにジャンプ
*selected1
        selected_btnext=btnext_1
        goto *bt_selected_gomain

//      ID1のボタンが押された時にselected_btnextにbtnext_2の中身を代入して
//      メインルーチンにジャンプ
*selected2
        selected_btnext=btnext_2
        goto *bt_selected_gomain

//      ID2のボタンが押された時にselected_btnextにbtnext_3の中身を代入して
//      メインルーチンにジャンプ
*selected3
        selected_btnext=btnext_3
        goto *bt_selected_gomain

//      ID3のボタンが押された時にselected_btnextにbtnext_4の中身を代入して
//      メインルーチンにジャンプ
*selected4
        selected_btnext=btnext_4
        goto *bt_selected_gomain

//      ID4のボタンが押された時にselected_btnextにbtnext_5の中身を代入して
//      メインルーチンにジャンプ
*selected5
        selected_btnext=btnext_5
        goto *bt_selected_gomain

//      ID5のボタンが押された時にselected_btnextにbtnext_6の中身を代入して
//      メインルーチンにジャンプ
*selected6
        selected_btnext=btnext_6
        goto *bt_selected_gomain