#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

ability = [20,3,8,5,5,20,15,1]   #能力値最大
#HP(0体力)MP(1魔力)ATS(2攻撃力)MAT(3魔法攻撃力)DEF(4防御力)DEX(5素早さ)AGI(6器用さ)LUK(7運)

current = [20,3]  #現在の能力値

lv = [1,0]                      #レベル関連
#Lv(レベル)EXP(経験値)

g = 0

select = 0

advance = 0

gamedescription = ["ゲーム説明","このゲームはメニューで行動を選択して遊びます",
                   "ステータスを見る・宿屋へ行く・武器屋へ行く・教会へ行く・探索へ行くの中から好きなのを選びます",
                   "ステータスを見るは今の自分のステータスを見ることができます",
                   "HP=体力 MP=魔力・魔法防御力 ATS=攻撃力 MAT=魔法攻撃力 DEF=防御力 DEX=素早さ AGI=器用さ  LUK=運",
                   "宿屋はGを支払うとHP・MPが回復します",
                   "武器屋は武器・防具を購入することができます",
                   "教会はセーブをすることができます",
                   "探索は魔物の生息地に行き 戦いをします",
                   "戦いは行動を選択して魔物を倒します",
                   "死んでしまったら'GAMEOVER'でGを失い村・街からやり直しです",
                   "目標は魔王を倒すこと!",
                   "じゃぁクリア目指して頑張ってね",
                   "※このゲームは共同制作作品です"]

prorogu = ["\e[96m_me_:う、うっ",
           "\e[96;2m(強い頭痛を感じ_jibun_は意識を失った)",
           "\e[96;2m(目覚めると そこは見知らぬ村だった)",
           "\e[96m_me_:こ、ここは どこ?",
           "\e[96;2m(その瞬間すべてを思い出した)",
           "\e[96;2m(剣術や魔法 世界のことわり)",
           "\e[96;2m(そして魔王を倒さなければ ならないという強い使命感を覚えた)",
           "\e[96m_me_:魔王を倒さなければ",
           "\e[96m_me_:なぜだか わからないけど そんな気がする",
           "\e[96m_me_:あっ そして ここは どこなんだろう",
           "\e[96m_me_:そこの お方 ここが どこか わかりますか",
           "\e[2m村人:何いってるんだい?",
           "\e[2m村人:ここは「パクス村」ディーヴァス川のほとりにある小さな農村さ",
           "\e[2m村人:ここが初めてなら注意しておくけど「ポルタ平原」には魔物が出るから用意なしには行くなよ",
           "\e[2m村人:じゃぁな",
           "\e[96m_me_:とりあえずポルタ平原を探索しようかな"]
#最初の会話シーン

credit = ["スタッフロール(クレジットタイトル)","システム","村井侑平 澤田羽衣",
          "システムアドバイザー","佐藤隆之助 阿部光峻",
          "広瀬雄二様 東北公益文科大学の皆様",
          "シナリオ","村井侑平 高橋真翔 佐藤風斗 佐藤然",
          "シナリオアドバイザー","佐藤隆之助 澤田羽衣 疋田琉莉 阿部光峻",
          "グラフィック","澤田羽衣",          "この作品に関わった すべての方に感謝の意を申し上げます",
          "制作","「#みんなでゲームを作ろう」の会"]

battleselect = ["攻撃","アイテム","逃げる"]

attack = ["スラッシュ"]                      #攻撃名
attackdetail = [[1,0,0]]                #攻撃の詳細

monster = ["スライム","デーモンビースト","黒い影","\e[93mゴールデンスライム"]

enemy = [[12,12,8,8,4,0,3,10,8,1,3,"突進","Error"],[15,15,5,5,10,0,5,25,15,1,7,"突進","Error"],
         [8,8,15,15,0,8,20,35,25,3,13,"Error","闇の刃"],[12,12,20,20,6,0,30,8,8,1,30,"突進","Error"]]

status = []                     #[名前、性別、一人称etc.]

item = []

area = []

sentaku = ["プレイ","ゲーム説明","クレジットタイトル"]
q1 = ["男","女"]                #変数名の最初は大文字NG
q2 = ["俺","僕","私"]
yadoya = ["泊まる(10G)","泊まらない"]
kyokai = ["祈りを捧げる(セーブ)","聖水を買う(20G)"]

def status(status,ability,current,nani,lv,g)
  x = 0
  puts"               ステータス"
  for i in nani[0]    #i = ["名前","性別","一人称"]
    printf("%s:%s   ",i,status[x])  #名前性別一人称
    x += 1
  end
  x = 0
  printf("Lv%s   ",lv[0])
  printf("EXP:%d   ",lv[1])
  printf("所持金:%dG",g)
  print("\n")
  for i in nani[2]
    printf("%s:%d/%d   ",i,ability[x],current[x])
    x += 1
  end
  x = 2
  for i in nani[3]
    printf("%s:%d   ",i,ability[x])
    x += 1
  end
  print("\n\n")
end

def question(situmon,list)                 #質問を出すメソッド
  kaisu = 0
  select = -1
  while kaisu < list.length
    printf("%s(%d)\n",list[kaisu],kaisu)
    print("\e[m")
    kaisu += 1
  end
  while select < 0 || select >= list.length   #回答の仕分け
    print(situmon+":")
    select = gets
    if select == "\n"
      redo
    end
    select = select.to_i
  end
  return list[select]           #ただ見やすくしてる
end

def query(situmon,list)                 #質問を出すメソッド
  kaisu = 0
  select = -1
  while kaisu < list.length
    printf("%s(%d)\n",list[kaisu],kaisu)
    print("\e[m")
    kaisu += 1
  end
  while select < 0 || select >= list.length   #回答の仕分け
    print(situmon+":")
    select = gets
    if select == "\n"
      redo
    end
    select = select.to_i
  end
  return select          #数字版
end

def dot1(time,kaisu)   #「.」のためにいろいろ打ちたくなかったのメソッド
  for i in 1..kaisu
    sleep(time)
    print(".")
  end
end

def dot2(time,kaisu)   #「.」のためにいろいろ打ちたくなかったのメソッド
  for i in 1..kaisu
    sleep(time)
    print("=")
  end
end

def description(hanasi)
  for i in hanasi
    puts(i)
    print("\e[m")
    gets
  end
end

def story(hanasi,status)             #物語のgetsをサボりたいのメソッド
  for i in hanasi
    puts(i.sub("_me_", status[0])               #subがiのキーワードを 
    	   .sub("_jibun_", status[2])       #特定の文字に
	   .sub("_seibetu_", status[1]))    #置き換えてくれる
    print("\e[m")
    gets
  end
end

def encounter(probability)
  encounter = probability[rand(probability.length)]
end

def enemyturn(probability,monster,enemy,ability,current)
  data = []
  if rand(((enemy[probability][4]*1.5).round) + 1) < rand(enemy[probability][5] + 1)
      data << enemymagic(probability,monster,enemy,ability,current)
    if rand(enemy[probability][8]*2 + enemy[probability][7] + (enemy[probability][9]/2).round) > rand(((ability[5]*2 + ability[5] + (ability[7]/2).round)/2).round)
      return data
    end
  else
    data << enemyattack(probability,monster,enemy,ability,current)
    if rand(enemy[probability][8]*2 + enemy[probability][7] + (enemy[probability][9]/2).round) > rand(((ability[5]*2 + ability[6] + (ability[7]/2).round)/2).round)
      return data
    end
  end
  return 0
end

def process1(data)
  if data[0][0] == 0
    puts("攻撃失敗")
  elsif data[0][0] == "他"
    data[0][0] = 0
    return data
  else
    printf("%dダメージ\n",data[0][0])
  end
  return data
end

def  battle(probability,monster,enemy,ability,current,item,status,nani,lv,g,attack,attackdetail,battleselect)
  printf("%s\e[mが現れた\n",monster[probability])
  print("戦闘")
  dot2(0.01,28)
  print("\n")
  while true
    if current[0] <= 0
      return 0                  #負け
    elsif enemy[probability][0] <= 0
      return 1                  #勝ち
    end
    
    if ability[5] < enemy[probability][7]
      data = enemyturn(probability,monster,enemy,ability,current)
      process1(data)
      current[0] -= data[0][0]
      enemy[probability][2] -= data[0][1]
      enemy[probability][0] -= data[0][2]
      
      damage = myturn(probability,monster,enemy,ability,current,item,status,nani,lv,g,attack,attackdetail,battleselect)
      process1(data)
      enemy[probability][0] -= data[0][0]
      current[2] -= data[0][1]
      current[0] -= data[0][2]
      
    elsif ability[5] = enemy[probability][7]
      if 0  == rand(2)
        damage = myturn(probability,monster,enemy,ability,current,item,status,nani,lv,g,attack,attackdetail,battleselect)
        process1(data)
        enemy[probability][0] -= data[0][0]
        current[2] -= data[0][1]
        current[0] -= data[0][2]

        data = enemyturn(probability,monster,enemy,ability,current)
        process1(data)
        current[0] -= data[0][0]
        enemy[probability][2] -= data[0][1]
        enemy[probability][0] -= data[0][2]
        
      else
        data = enemyturn(probability,monster,enemy,ability,current)
        process1(data)
        current[0] -= data[0][0]
        enemy[probability][2] -= data[0][1]
        enemy[probability][0] -= data[0][2]
        
        damage = myturn(probability,monster,enemy,ability,current,item,status,nani,lv,g,attack,attackdetail,battleselect)
        process1(data)
        enemy[probability][0] -= data[0][0]
        current[2] -= data[0][1]
        current[0] -= data[0][2]
        
      end

    elsif ability[5] > enemy[probability][5]
      damage = myturn(probability,monster,enemy,ability,current,item,status,nani,lv,g,battleselect)
      process1(data)
      enemy[probability][0] -= data[0][0]
      current[2] -= data[0][1]
      current[0] -= data[0][2]

      data = enemyturn(probability,monster,enemy,ability,current)
      process1(data)
      current[0] -= data[0][0]
      enemy[probability][2] -= data[0][1]
      enemy[probability][0] -= data[0][2]
    end
  end
end


def enemymagic(probability,monster,enemy,ability,current)
  data = []
  if not enemy[probability][2] == 0
    printf("%s\e[mの攻撃\n",monster[probability])
    mp = enemy[probability][3]/10.round + rand(3)
    if  mp > enemy[probability][2]
      mp = enemy[probability][2]
    elsif mp > 1000
      mp = 1000
    end
    data << (enemy[probability][5] * (mp / 2) - current[2] / 2).round
    printf("%s!",enemy[probability][12])
    data << 0
    return data
  else
    return [[0,0,0]]
  end
end

def enemyattack(probability,monster,enemy,ability,current)
  data = []
  printf("%sの攻撃\n",monster[probability])
  data << enemy[probability][4] - ability[4]/2.round
  printf("%s!",enemy[probability][11])
  data << 0
  data << 0
  return data
end

def myturn(probability,monster,enemy,ability,current,item,status,nani,lv,g,attack,attackdetail,battleselect)
  data = []
  status(status,ability,current,nani,lv,g)
  select = question("行動…",battleselect)
  if select == "攻撃"
    select = query("攻撃…",attack)
    damage = ability[2] * attackdetail[select][0] - ((enemy[probability][6] / 2).round)
    if rand(enemy[probability][8]*2 + enemy[probability][7] + (enemy[probability][9]/2).round) > rand(((ability[probability][5]*2 + ability[probability][6] + (ability[probability][7]/2).round)/2).round)
      printf("%s!",attack[select])
      data << damage
    else
      data << 0
    end
    data << attackdetail[select][1]
    data << attackdetail[select][2]
    x = []
    x << data
    return x
  end
end

nani = [["名前","性別","一人称"],["Lv","EXP"],["HP","MP"],["ATS","MAT","DEF","DEX","AGI","LUK"],
         ["ステータスを見る","宿屋に行く","武器屋に行く","教会に行く","探索に行く"]]

puts("RPG")
select = question("選択",sentaku)
if select == "ゲーム説明"
  print("\n")
  description(gamedescription)
  select = question("選択",sentaku)
end
if select == "クレジットタイトル"
  print("\n")
  description(credit)
  select = question("選択",sentaku)
end

if select == "プレイ"
  name = "\n"
  while name == "\n"             #この辺少し変だから真似はよく見てやって
    puts("PL名を決めてください")
    print("name…")
    name = gets
  end
  name = name.chomp
  status << name
  status << question("性別を決めてください",q1)
  status << question("一人称を決めてください",q2)
end

puts("ゲームを起動します")
sleep(1)

print"Loading"
dot1(0.5,3)
sleep(1)
print("\n\n")
story(prorogu,status)

advance += 1
area << "ポルタ平原"

while true       #今のところ見るだけ。選択肢の中身ができたらいじる
  puts("               メニュー")
  x = 0
  y = question("行動…",nani[4])      #yはPLの回答を拾ってくる 
  print("\n")

  if y == "ステータスを見る"
    status(status,ability,current,nani,lv,g)
    
  elsif y == "宿屋に行く"
    puts("村人:一泊10Gだよ")
    select = question("どうする",yadoya)
    if select == "泊まる(10G)"
      if g >= 10
        g - 10
        print"Sleeing"
        dot(0.5,3,)
        sleep(1)
        current[0] =  ability[0]
        current[1] =  ability[1]
        puts("また 来いよ")
      else
        puts("村人:ひやかしは帰れ!!")
      end
    else
      puts("村人:なんだ泊まんねーのかよ")
    end
    print("\n")
    
  elsif y == "探索に行く"
    select = question("どこに探索に行きますか",area)
    if  select == "ポルタ平原"
      sleep(rand(3))
      probability = encounter([0]*75 + [1]*15 + [2]*500 + [3]*5)
      game =  battle(probability,monster,enemy,ability,current,item,status,nani,lv,g,attack,attackdetail,battleselect)
      
      if 0  ==  game
        #GAMEOVREのメソッド
        
      elsif 1 == game
        lv[1] += enemy[probability][10] + ((enemy[probability][10]/10).round * (rand(3) + 1) + (rand(3) + 1))
      end
    end
  end
end
