Reference
  Area\Util.txt
  Util\Audio.txt
  Util\Party.txt
  Util\CommonDialog.txt
End Reference

Procedure Main(startAt)

  Dim ac = Nothing

  RegisterCommonEventHandlers()
  AddAreaZone(AddZone("B1", 96, 0, 0, 127, 31, 100), 1)
  AddAreaZone(AddZone("B2", 96, 32, 0, 127, 63, 100), 1)
  AddAreaZone(AddZone("B3", 0, 64, 0, 31, 95, 100), 1)
  AddAreaZone(AddZone("B4", 96, 64, 0, 127, 95, 100), 1)
  AddAreaZone(AddZone("B5", 0, 96, 0, 31, 127, 100), 1)
  AddAreaZone(AddZone("B6", 32, 96, 0, 63, 127, 100), 1)
  AddAreaZone(AddZone("B7", 64, 96, 0, 95, 127, 100), 1)
  AddAreaZone(AddZone("B8", 96, 96, 0, 127, 127, 100), 1)

  AddZone("Entrance", 48, 126, 0)
  
  AddZone("ExitArea1", 47, 127, 0, 48, 127, 0)
  RegisterEventHandler("ExitArea1Enter", ExitArea1_Enter)
  AddZone("Entrance1", 48, 126, 0)
  
  AddZone("ExitArea2", 104, 64, 0, 105, 64, 0)
  RegisterEventHandler("ExitArea2Enter", ExitArea2_Enter)
  AddZone("Entrance2", 104, 65, 0)

  'B1toB2
  AddZone("B1_2", 104, 31, 0)
  RegisterEventHandler("B1_2Enter", B1_2_Enter)
  AddZone("B1_2D", 104, 33, 0)
  AddZone("B2_1", 104, 32, 0)
  RegisterEventHandler("B2_1Enter", B2_1_Enter)
  AddZone("B2_1D", 104, 30, 0)

  'B2toB4
  AddZone("B2_4", 116, 63, 0)
  RegisterEventHandler("B2_4Enter", B2_4_Enter)
  AddZone("B2_4D", 116, 65, 0)
  AddZone("B4_2", 116, 64, 0)
  RegisterEventHandler("B4_2Enter", B4_2_Enter)
  AddZone("B4_2D", 116, 62, 0)
  
  'B3toB5
  AddZone("B3_5", 7, 95, 0)
  RegisterEventHandler("B3_5Enter", B3_5_Enter)
  AddZone("B3_5D", 7, 97, 0)
  AddZone("B5_3", 7, 96, 0)
  RegisterEventHandler("B5_3Enter", B5_3_Enter)
  AddZone("B5_3D", 7, 94, 0)

  'B4toB8
  AddZone("B4_8", 104, 95, 0)
  RegisterEventHandler("B4_8Enter", B4_8_Enter)
  AddZone("B4_8D", 104, 97, 0)
  AddZone("B8_4", 104, 96, 0)
  RegisterEventHandler("B8_4Enter", B8_4_Enter)
  AddZone("B8_4D", 104, 94, 0)

  'B5toB6
  AddZone("B5_6", 31, 106, 0, 31, 107, 0)
  RegisterEventHandler("B5_6Enter", B5_6_Enter)
  AddZone("B5_6D", 33, 106, 0)
  AddZone("B6_5", 32, 106, 0, 32, 107, 0)
  RegisterEventHandler("B6_5Enter", B6_5_Enter)
  AddZone("B6_5D", 30, 106, 0)

  'B6toB7
  AddZone("B6_7", 63, 110, 0, 63, 111, 0)
  RegisterEventHandler("B6_7Enter", B6_7_Enter)
  AddZone("B6_7D", 65, 110, 0)
  AddZone("B7_6", 64, 110, 0, 64, 111, 0)
  RegisterEventHandler("B7_6Enter", B7_6_Enter)
  AddZone("B7_6D", 62, 110, 0)

  'B7toB8
  AddZone("B7_8", 95, 118, 0, 95, 119, 0)
  RegisterEventHandler("B7_8Enter", B7_8_Enter)
  AddZone("B7_8D", 97, 118, 0)
  AddZone("B8_7", 96, 118, 0, 96, 119, 0)
  RegisterEventHandler("B8_7Enter", B8_7_Enter)
  AddZone("B8_7D", 94, 118, 0)
  
  AddBox("CaveA2_B1", 110, 4, 0, BoxContent.Money, 300, Nothing)
  AddBox("CaveA2_B2", 125, 38, 0, BoxContent.Money, 250, Nothing)
  AddBox("CaveA2_B3", 17, 68, 0, BoxContent.Money, 320, Nothing)
  AddBox("CaveA2_B4", 118, 74, 0, BoxContent.Money, 200, Nothing)
  AddBox("CaveA2_B5", 9, 122, 0, BoxContent.Money, 280, Nothing)
  AddBox("CaveA2_B6", 88, 98, 0, BoxContent.Money, 100, Nothing)
  AddBox("CaveA2_B7", 125, 118, 0, BoxContent.Money, 150, Nothing)
  
  SetStepsOfMaterial(4, 20)
  ResetStepsForEncountering()
  
  AddZone("EncZone01", 0, 0, 0, 127, 127, 0)
  RegisterEventHandler("EncZone01Step", EncZone01_Step)
  
  If Not TestFlag("CaveA2_1") Then
    ac = AddActor_MTStopped("God", 104, 98, 0, [A_OldMan], ActorMotion.Down, God_Contact)
  End If
  
  If startAt = "Entrance1" Then
    DoInsideWarp(startAt, Direction.Up, False)
  Else
    DoInsideWarp(startAt, Direction.Down, False)
  End If
  
  ChangeBGM(Music.Cave)

  NotifyEntered()
  Idle(ProcessInput)
  NotifyExiting()

End Procedure

Procedure ExitArea1_Enter()
  BeginOutsideWarp([Field], "CaveA2a")
End Procedure

Procedure ExitArea2_Enter()
  BeginOutsideWarp([Field], "CaveA2b")
End Procedure

Procedure B1_2_Enter()
  DoInsideWarp("B1_2D", Direction.None, True)
End Procedure

Procedure B2_1_Enter()
  DoInsideWarp("B2_1D", Direction.None, True)
End Procedure

Procedure B2_4_Enter()
  DoInsideWarp("B2_4D", Direction.None, True)
End Procedure

Procedure B4_2_Enter()
  DoInsideWarp("B4_2D", Direction.None, True)
End Procedure

Procedure B3_5_Enter()
  DoInsideWarp("B3_5D", Direction.None, True)
End Procedure

Procedure B5_3_Enter()
  DoInsideWarp("B5_3D", Direction.None, True)
End Procedure

Procedure B4_8_Enter()
  DoInsideWarp("B4_8D", Direction.None, True)
End Procedure

Procedure B8_4_Enter()
  DoInsideWarp("B8_4D", Direction.None, True)
End Procedure

Procedure B5_6_Enter()
  DoInsideWarp("B5_6D", Direction.None, True)
End Procedure

Procedure B6_5_Enter()
  DoInsideWarp("B6_5D", Direction.None, True)
End Procedure

Procedure B6_7_Enter()
  DoInsideWarp("B6_7D", Direction.None, True)
End Procedure

Procedure B7_6_Enter()
  DoInsideWarp("B7_6D", Direction.None, True)
End Procedure

Procedure B7_8_Enter()
  DoInsideWarp("B7_8D", Direction.None, True)
End Procedure

Procedure B8_7_Enter()
  DoInsideWarp("B8_7D", Direction.None, True)
End Procedure

Procedure EncZone01_Step(mat)
  If TestEncountering(mat) Then
    Dim ep = CreateList()
    ep.Add({[E104], [E104], [E104], [E104], [E104]})
    ep.Add({[E105], [E104], [E104], [E104], [E104]})
    ep.Add({[E105], [E105], [E104], [E104]})
    ep.Add({[E105], [E105], [E105]})
    ep.Add({[E106], [E104], [E104], [E104]})
    ep.Add({[E106], [E105], [E104]})
    ep.Add({[E106], [E106]})
    DoNormalBattle(ep)
    ResetStepsForEncountering()
  End If
End Procedure

Procedure God_Contact()
  Dim p_name = [TheHero].Name + "："
  Dim q_name = "？？？："
  DoMsg(q_name, "………………")
  DoMsg(p_name, "（この気は？……）", _
    "（ただ者じゃなさそうだ）")
  DoMsg(q_name, "おぬし　青龍の塔に行くつもりか？")
  DoMsg(p_name, "い　いいえ")
  DoMsg(q_name, "そうか　まあ今行っても", _
    "そなたの力では　到底アルタロスには勝てまい")
  DoMsg(p_name, "え！？　なぜその名前を……", _
    "アルタロスとは誰なのですか？")
  DoMsg(q_name, "魔族四天王の一人だ")
  DoMsg(p_name, "魔族四天王？")
  Sleep(1000)
  DoMsg(q_name, "そなたが地下通路で戦った相手", _
    "あれが魔族じゃ　その中でも最強の４戦士", _
    "それが魔族四天王")
  Sleep(500)
  DoMsg(q_name, "アルタロス　フラスタン　アフリエム　セモサー", _
    "この４名じゃ　奴らは邪神デロクの復活をたくらみ", _
    "その計画を４つの塔で密かに進めておる", _
    "誰かが奴らの計画を阻止しなければならない", _
    "もし阻止できなかったら", _
    "再びデロクと戦うことになる", _
    "いずれにしろ　命を賭した戦いになるじゃろう")
  Sleep(500)
  DoMsg(p_name, "よく分からないんですが", _
    "もしかして　僕に阻止しろと？")
  DoMsg(q_name, "お前にはその素質があると見た", _
    "が　奴らに勝つためには　まだ力が足りぬ")
  Sleep(500)
  DoMsg(q_name, "もっと力をつけるのだ", _
    "お前の守るべき人たちのために……")
  RemoveActor("God")
  Sleep(1000)
  DoMsg(p_name, "（消えた……）")
  Sleep(1000)
  DoMsg(p_name, "（まあ青龍の塔は後回しにして）", _
    "（東のバムケイクの町に行こうか）", _
    "（リックとしばらく会ってないし）")
  SetFlag("CaveA2_1")
End Procedure
