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

Procedure Main(startAt)

  Dim ac = Nothing

  RegisterCommonEventHandlers()
  AddAreaZone(AddZone("F1",   0,  0, 0,  31,  31, 100), 2)
  AddAreaZone(AddZone("F2",  32,  0, 0,  63,  31, 100), 8)
  AddAreaZone(AddZone("F3",  64,  0, 0,  95,  31, 100), 8)
  AddAreaZone(AddZone("F4",  96,  0, 0, 127,  31, 100), 8)
  AddAreaZone(AddZone("F5",   0, 32, 0,  31,  63, 100), 8)
  AddAreaZone(AddZone("F6",  32, 32, 0,  63,  63, 100), 8)
  AddAreaZone(AddZone("F7",  64, 32, 0,  95,  63, 100), 8)
  AddAreaZone(AddZone("F8",  96, 32, 0, 127,  63, 100), 8)
  AddAreaZone(AddZone("F9",   0, 64, 0,  31,  95, 100), 8)
  AddAreaZone(AddZone("F10", 32, 64, 0,  63,  95, 100), 8)
  AddAreaZone(AddZone("F11", 64, 64, 0,  95,  95, 100), 8)
  AddAreaZone(AddZone("F12", 96, 64, 0, 127,  95, 100), 8)
  AddAreaZone(AddZone("F13",  0, 96, 0,  31, 127, 100), 8)
  AddAreaZone(AddZone("F14", 32, 96, 0,  63, 127, 100), 8)
  AddAreaZone(AddZone("F15", 64, 96, 0,  95, 127, 100), 8)
  AddAreaZone(AddZone("F16", 96, 96, 0, 127, 127, 100), 8)

  AddZone("ExitArea", 14, 31, 0, 17, 31, 0)
  RegisterEventHandler("ExitAreaEnter", ExitArea_Enter)
  AddZone("Entrance", 15, 30, 0)

  AddZone("F1_US1", 16, 2, 0)
  RegisterEventHandler("F1_US1Enter", F1_US1_Enter)
  AddZone("F1_US2", 11, 22, 0)
  RegisterEventHandler("F1_US2Enter", F1_US2_Enter)
  AddZone("F1_US3", 20, 22, 0)
  RegisterEventHandler("F1_US3Enter", F1_US3_Enter)
  AddZone("F1_US4", 11, 27, 0)
  RegisterEventHandler("F1_US4Enter", F1_US4_Enter)
  AddZone("F1_US5", 20, 27, 0)
  RegisterEventHandler("F1_US5Enter", F1_US5_Enter)

  AddZone("F2_DS1", 48,  2, 0)
  RegisterEventHandler("F2_DS1Enter", F2_DS1_Enter)
  AddZone("F2_DS2", 43, 22, 0)
  RegisterEventHandler("F2_DS2Enter", F2_DS2_Enter)
  AddZone("F2_DS3", 52, 22, 0)
  RegisterEventHandler("F2_DS3Enter", F2_DS3_Enter)
  AddZone("F2_DS4", 43, 27, 0)
  RegisterEventHandler("F2_DS4Enter", F2_DS4_Enter)
  AddZone("F2_DS5", 52, 27, 0)
  RegisterEventHandler("F2_DS5Enter", F2_DS5_Enter)
  AddZone("F2_US1", 47, 27, 0)
  RegisterEventHandler("F2_US1Enter", F2_US1_Enter)

  AddZone("F3_DS1", 79, 27, 0)
  RegisterEventHandler("F3_DS1Enter", F3_DS1_Enter)
  AddZone("F3_US1", 93, 27, 0)
  RegisterEventHandler("F3_US1Enter", F3_US1_Enter)

  AddZone("F4_DS1", 125, 27, 0)
  RegisterEventHandler("F4_DS1Enter", F4_DS1_Enter)
  AddZone("F4_US1", 113, 22, 0)
  RegisterEventHandler("F4_US1Enter", F4_US1_Enter)

  AddZone("F5_DS1", 17, 54, 0)
  RegisterEventHandler("F5_DS1Enter", F5_DS1_Enter)
  AddZone("F5_US1",  2, 34, 0)
  RegisterEventHandler("F5_US1Enter", F5_US1_Enter)

  AddZone("F6_DS1", 34, 34, 0)
  RegisterEventHandler("F6_DS1Enter", F6_DS1_Enter)
  AddZone("F6_US1", 61, 34, 0)
  RegisterEventHandler("F6_US1Enter", F6_US1_Enter)

  AddZone("F7_DS1", 93, 34, 0)
  RegisterEventHandler("F7_DS1Enter", F7_DS1_Enter)
  AddZone("F7_US1", 93, 59, 0)
  RegisterEventHandler("F7_US1Enter", F7_US1_Enter)

  AddZone("F8_DS1", 125, 59, 0)
  RegisterEventHandler("F8_DS1Enter", F8_DS1_Enter)
  AddZone("F8_US1",  98, 59, 0)
  RegisterEventHandler("F8_US1Enter", F8_US1_Enter)

  AddZone("F9_DS1", 2, 91, 0)
  RegisterEventHandler("F9_DS1Enter", F9_DS1_Enter)
  AddZone("F9_US1", 2, 66, 0)
  RegisterEventHandler("F9_US1Enter", F9_US1_Enter)
  AddZone("F9_US2", 13, 70, 0)
  AddZone("F9_US3", 12, 80, 0)
  AddZone("F9_US4", 8, 84, 0)

  AddZone("F10_DS1", 34, 66, 0)
  RegisterEventHandler("F10_DS1Enter", F10_DS1_Enter)
  AddZone("F10_DS2", 45, 70, 0)
  RegisterEventHandler("F10_DS2Enter", F10_DS2_Enter)
  AddZone("F10_DS3", 44, 80, 0)
  RegisterEventHandler("F10_DS3Enter", F10_DS3_Enter)
  AddZone("F10_DS4", 40, 84, 0)
  RegisterEventHandler("F10_DS4Enter", F10_DS4_Enter)
  AddZone("F10_US1", 61, 66, 0)
  RegisterEventHandler("F10_US1Enter", F10_US1_Enter)

  AddZone("F11_DS1", 93, 66, 0)
  RegisterEventHandler("F11_DS1Enter", F11_DS1_Enter)
  AddZone("F11_US1", 66, 66, 0)
  RegisterEventHandler("F11_US1Enter", F11_US1_Enter)
  AddZone("F11_US2", 66, 91, 0)
  RegisterEventHandler("F11_US2Enter", F11_US2_Enter)

  AddZone("F12_DS1", 98, 66, 0)
  RegisterEventHandler("F12_DS1Enter", F12_DS1_Enter)
  AddZone("F12_DS2", 98, 91, 0)
  RegisterEventHandler("F12_DS2Enter", F12_DS2_Enter)
  AddZone("F12_US1", 101, 69, 0)
  RegisterEventHandler("F12_US1Enter", F12_US1_Enter)
  AddZone("F12_US2", 122, 69, 0)
  RegisterEventHandler("F12_US2Enter", F12_US2_Enter)
  AddZone("F12_US3", 108, 78, 0)
  RegisterEventHandler("F12_US3Enter", F12_US3_Enter)
  AddZone("F12_US4", 101, 88, 0)
  RegisterEventHandler("F12_US4Enter", F12_US4_Enter)
  AddZone("F12_US5", 122, 88, 0)
  RegisterEventHandler("F12_US5Enter", F12_US5_Enter)

  AddZone("F13_DS1", 5, 101, 0)
  RegisterEventHandler("F13_DS1Enter", F13_DS1_Enter)
  AddZone("F13_DS2", 26, 101, 0)
  RegisterEventHandler("F13_DS2Enter", F13_DS2_Enter)
  AddZone("F13_DS3", 12, 110, 0)
  RegisterEventHandler("F13_DS3Enter", F13_DS3_Enter)
  AddZone("F13_DS4", 5, 120, 0)
  RegisterEventHandler("F13_DS4Enter", F13_DS4_Enter)
  AddZone("F13_DS5", 26, 120, 0)
  RegisterEventHandler("F13_DS5Enter", F13_DS5_Enter)
  AddZone("F13_US1", 16, 118, 0)
  RegisterEventHandler("F13_US1Enter", F13_US1_Enter)

  AddZone("F14_DS1", 48, 118, 0)
  RegisterEventHandler("F14_DS1Enter", F14_DS1_Enter)
  AddZone("F14_US1", 47, 98, 0)
  RegisterEventHandler("F14_US1Enter", F14_US1_Enter)

  AddZone("F15_DS1", 79, 98, 0)
  RegisterEventHandler("F15_DS1Enter", F15_DS1_Enter)
  AddZone("F15_US1", 80, 117, 0)
  RegisterEventHandler("F15_US1Enter", F15_US1_Enter)
  AddZone("F15_SP", 75, 122, 0)
  RegisterEventHandler("F15_SPEnter", F15_SP_Enter)
  AddZone("F15_EP", 84, 122, 0)
  RegisterEventHandler("F15_EPEnter", F15_EP_Enter)

  AddZone("F16_DS1", 112, 117, 0)
  AddStaticActor("LastBoss1", 112, 109, 0, [A_LastBoss1], Nothing)
  
  If Not TestFlag("CentralTower_1") Then
    AddZone("BossCaution", 15, 20, 0, 16, 20, 0)
    RegisterEventHandler("BossCautionEnter", BossCaution_Enter)
  End If
  
  SetStepsOfMaterial(4, 20)
  ResetStepsForEncountering()
  
  AddZone("EncZone01", 0, 0, 0, 127, 63, 0)
  RegisterEventHandler("EncZone01Step", EncZone01_Step)
  AddZone("EncZone02", 0, 64, 0, 127, 127, 0)
  RegisterEventHandler("EncZone02Step", EncZone02_Step)

  DoInsideWarp(startAt, Direction.Up, False)
  
  ChangeBGM(Music.LastDungeon)

  NotifyEntered()
  Idle(ProcessInput)
  NotifyExiting()

End Procedure

Procedure ExitArea_Enter()
  BeginOutsideWarp([Field], GetAreaObject().Tag)
End Procedure

Procedure F1_US1_Enter()
  DoStairWarp("F2_DS1", Direction.Down, True)
End Procedure

Procedure F1_US2_Enter()
  DoStairWarp("F2_DS2", Direction.Down, True)
End Procedure

Procedure F1_US3_Enter()
  DoStairWarp("F2_DS3", Direction.Down, True)
End Procedure

Procedure F1_US4_Enter()
  DoStairWarp("F2_DS4", Direction.Down, True)
End Procedure

Procedure F1_US5_Enter()
  DoStairWarp("F2_DS5", Direction.Down, True)
End Procedure

Procedure F2_DS1_Enter()
  DoStairWarp("F1_US1", Direction.Down, True)
End Procedure

Procedure F2_DS2_Enter()
  DoStairWarp("F1_US2", Direction.Down, True)
End Procedure

Procedure F2_DS3_Enter()
  DoStairWarp("F1_US3", Direction.Down, True)
End Procedure

Procedure F2_DS4_Enter()
  DoStairWarp("F1_US4", Direction.Down, True)
End Procedure

Procedure F2_DS5_Enter()
  DoStairWarp("F1_US5", Direction.Down, True)
End Procedure

Procedure F2_US1_Enter()
  DoStairWarp("F3_DS1", Direction.Down, True)
End Procedure

Procedure F3_DS1_Enter()
  DoStairWarp("F2_US1", Direction.Down, True)
End Procedure

Procedure F3_US1_Enter()
  DoStairWarp("F4_DS1", Direction.Down, True)
End Procedure

Procedure F4_DS1_Enter()
  DoStairWarp("F3_US1", Direction.Down, True)
End Procedure

Procedure F4_US1_Enter()
  DoStairWarp("F5_DS1", Direction.Down, True)
End Procedure

Procedure F5_DS1_Enter()
  DoStairWarp("F4_US1", Direction.Down, True)
End Procedure

Procedure F5_US1_Enter()
  DoStairWarp("F6_DS1", Direction.Down, True)
End Procedure

Procedure F6_DS1_Enter()
  DoStairWarp("F5_US1", Direction.Down, True)
End Procedure

Procedure F6_US1_Enter()
  DoStairWarp("F7_DS1", Direction.Down, True)
End Procedure

Procedure F7_DS1_Enter()
  DoStairWarp("F6_US1", Direction.Down, True)
End Procedure

Procedure F7_US1_Enter()
  DoStairWarp("F8_DS1", Direction.Down, True)
End Procedure

Procedure F8_DS1_Enter()
  DoStairWarp("F7_US1", Direction.Down, True)
End Procedure

Procedure F8_US1_Enter()
  DoStairWarp("F9_DS1", Direction.Down, True)
End Procedure

Procedure F9_DS1_Enter()
  DoStairWarp("F8_US1", Direction.Down, True)
End Procedure

Procedure F9_US1_Enter()
  DoStairWarp("F10_DS1", Direction.Down, True)
End Procedure

Procedure F10_DS1_Enter()
  DoStairWarp("F9_US1", Direction.Down, True)
End Procedure

Procedure F10_DS2_Enter()
  DoStairWarp("F9_US2", Direction.Down, True)
End Procedure

Procedure F10_DS3_Enter()
  DoStairWarp("F9_US3", Direction.Down, True)
End Procedure

Procedure F10_DS4_Enter()
  DoStairWarp("F9_US4", Direction.Down, True)
End Procedure

Procedure F10_US1_Enter()
  DoStairWarp("F11_DS1", Direction.Down, True)
End Procedure

Procedure F11_DS1_Enter()
  DoStairWarp("F10_US1", Direction.Down, True)
End Procedure

Procedure F11_US1_Enter()
  DoStairWarp("F12_DS1", Direction.Down, True)
End Procedure

Procedure F11_US2_Enter()
  DoStairWarp("F12_DS2", Direction.Down, True)
End Procedure

Procedure F12_DS1_Enter()
  DoStairWarp("F11_US1", Direction.Down, True)
End Procedure

Procedure F12_DS2_Enter()
  DoStairWarp("F11_US2", Direction.Down, True)
End Procedure

Procedure F12_US1_Enter()
  DoStairWarp("F13_DS1", Direction.Down, True)
End Procedure

Procedure F12_US2_Enter()
  DoStairWarp("F13_DS2", Direction.Down, True)
End Procedure

Procedure F12_US3_Enter()
  DoStairWarp("F13_DS3", Direction.Down, True)
End Procedure

Procedure F12_US4_Enter()
  DoStairWarp("F13_DS4", Direction.Down, True)
End Procedure

Procedure F12_US5_Enter()
  DoStairWarp("F13_DS5", Direction.Down, True)
End Procedure

Procedure F13_DS1_Enter()
  DoStairWarp("F12_US1", Direction.Down, True)
End Procedure

Procedure F13_DS2_Enter()
  DoStairWarp("F12_US2", Direction.Down, True)
End Procedure

Procedure F13_DS3_Enter()
  DoStairWarp("F12_US3", Direction.Down, True)
End Procedure

Procedure F13_DS4_Enter()
  DoStairWarp("F12_US4", Direction.Down, True)
End Procedure

Procedure F13_DS5_Enter()
  DoStairWarp("F12_US5", Direction.Down, True)
End Procedure

Procedure F13_US1_Enter()
  DoStairWarp("F14_DS1", Direction.Down, True)
End Procedure

Procedure F14_DS1_Enter()
  DoStairWarp("F13_US1", Direction.Down, True)
End Procedure

Procedure F14_US1_Enter()
  DoStairWarp("F15_DS1", Direction.Down, True)
End Procedure

Procedure F15_DS1_Enter()
  DoStairWarp("F14_US1", Direction.Down, True)
End Procedure

Procedure F15_US1_Enter()
  Dim p_name = [TheHero].Name + "："
  DoMsg(p_name, "（誰か上にいる……）", _
    "（とてつもなく邪な気だ……）")
  If DoYesNoMsg("この先に進みますか？") = True Then
    DoInsideWarp("F16_DS1", Direction.Up, True)
    LastBattleSample()
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  End If
End Procedure

Procedure F15_SP_Enter()
  Dim p_name = [TheHero].Name + "："
  DoMsg(p_name, "（不思議な力を感じる……）")
  Sleep(500)
  If DoYesNoMsg("セーブしますか？") = True Then
    Dim hMenu = CreateDataSlotMenuForSave(80, 80, 480, 280, Nothing, Nothing, _
      "どこに？", 1)
    Dim index = ProcessMenu(hMenu)
    Do While index > 0
      If Not IsDataSlotUsed(index) OrElse DoYesNoMsg( _
        "前のデータは消えますが　よろしいですか？") Then
        SaveGameData(index)
        DoMsg("セーブしました。")
        Exit Do
      End If
      index = ProcessMenu(hMenu)
    Loop
    DestroyControl(hMenu)
  End If
End Procedure

Procedure F15_EP_Enter()
  Dim p_name = [TheHero].Name + "："
  DoMsg(p_name, "（地面が見えている……）")
  Sleep(500)
  If DoYesNoMsg("エスケープしますか？") = True Then
    If DoYesNoMsg("また登りなおすことになりますが", _
      "本当によろしいですか？") = True Then
      BeginOutsideWarp([Field], GetAreaObject().Tag)
    End If
  End If
End Procedure

Procedure EncZone01_Step(mat)
  If TestEncountering(mat) Then
    Dim ep = CreateList()
    ep.Add({[E209], [E209]})
    ep.Add({[E212], [E212]})
    ep.Add({[E210], [E210], [E210]})
    ep.Add({[E210], [E211]})
    ep.Add({[E210], [E210], [E211]})
    ep.Add({[E211], [E211]})
    DoNormalBattle(ep)
    ResetStepsForEncountering()
  End If
End Procedure

Procedure EncZone02_Step(mat)
  If TestEncountering(mat) Then
    Dim ep = CreateList()
    ep.Add({[E212], [E212]})
    ep.Add({[E215], [E215]})
    ep.Add({[E213], [E213], [E213]})
    ep.Add({[E213], [E214]})
    ep.Add({[E213], [E213], [E214]})
    ep.Add({[E214], [E214]})
    DoNormalBattle(ep)
    ResetStepsForEncountering()
  End If
End Procedure

Procedure BossCaution_Enter()
  If TestFlag("CentralTower_1") Then Return
  Dim p_name = [TheHero].Name + "："
  Dim q_name = ""
  Dim r_name = ""
  Dim bosses = CreateList()
  If Not TestFlag("EastEvo") Then bosses.Add([E301])
  If Not TestFlag("SouthEvo") Then bosses.Add([E302])
  If Not TestFlag("WestEvo") Then bosses.Add([E303])
  If Not TestFlag("NorthEvo") Then bosses.Add([E304])
  DoMsg(p_name, "（誰かいる……だがこの気配は……）")
  If DoYesNoMsg("この先に進みますか？") = True Then
    AddStaticActor("Boss1", 14, 13, 0, bosses[1].Actor, Nothing)
    AddStaticActor("Boss2", 16, 13, 0, bosses[2].Actor, Nothing)
    q_name = bosses[1].Name + "："
    r_name = bosses[2].Name + "："
    For i = 1 To 5
      LetActorWalk(GetPrimaryActor(), Direction.Up)
    Next
    ChangeBGM(Nothing)
    DoMsg(p_name, "あ！")
    Sleep(1000)
    ChangeBGM(Music.Duel)
    DoMsg(p_name, bosses[1].Name + "に" + bosses[2].Name, _
      "死んだはずでは……")
    DoMsg(q_name, "ふっふっふ　確かに一度は死んだ", _
      "だが　デロク様の力でよみがえったのだ", _
      "あの二人も　じきに復活するだろう")
    DoMsg(p_name, "くっ")
    DoMsg(r_name, "なに　貴様が気に病むことはない")
    Sleep(500)
    DoMsg(r_name, "我々の手で　その息の根　止めてやる！")
    If Not DoBossBattle(bosses) Then
      EndOfTheWorld()
      Return
    End If
    DoMsg(q_name, "き　貴様……", _
      "さらに　強くなっていたのか……")
    RemoveActor(GetActor("Boss1"))
    DoMsg(r_name, "我々では　勝てぬのか……", _
      "ぐふっ")
    RemoveActor(GetActor("Boss2"))
    Sleep(500)
    DoMsg(p_name, "（倒した……）")
    Sleep(500)
    DoMsg(p_name, "（だが　あとの二人が復活したら）", _
      "（とてもやっかいなことになりそうだ）", _
      "（早く邪神を　倒さなければならない……）")
    SetFlag("CentralTower_1")
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  End If
End Procedure

Procedure EndingMovingProc()
  For i = 0 To 2
    For j = 1 To 32
      SetViewCenter(3584 + 16, 3488 + i * 32 + j + 16)
      Sleep(30)
    Next
  Next
  LinkView()
End Procedure

Procedure LastBattleSample()
  Dim p_name = [TheHero].Name + "："
  Dim q_name = "邪神デロク："
  For i = 1 To 5
    LetActorWalk(GetPrimaryActor(), Direction.Up)
  Next
  For i = 0 To 2
    For j = 1 To 32
      SetViewCenter(3584 + 16, 3584 - i * 32 - j + 16)
      Sleep(20)
    Next
  Next
  DoMsg(p_name, "（こいつが　邪神　デロクか……）")
  Sleep(500)
  DoMsg(p_name, "（確かに不完全なようだ）", _
    "（今しかない！）")
  If Not DoBossBattle({[E309]}) Then
    EndOfTheWorld()
    Return
  End If
  'THE END OF OUR LONG LONG JOURNEY
  SetFlag("CentralTower_2")
  ResetSignal("PLAYERS_CONTROL")
  RemoveActor(GetActor("LastBoss1"))
  AddStaticActor("LastBoss2", 110, 110, 0, [A_LastBoss2], Nothing)
  FadeOut(0, 0, 0, 1500)
  DoMsgT({q_name, "くっ……", _
    "こ　この私が　人間などに……おのれ……"}, 6400)
  Sleep(500)
  DoMsgT({q_name, "うおおおお！"}, 3300)
  Sleep(500)
  RaiseEvent("StartShake")
  Sleep(1500)
  ChangeGeometry(107, 108, 0, 22)
  ChangeGeometry(109, 108, 0, 23)
  PlayAudio(Sound.Kitta)
  Sleep(500)
  ChangeGeometry(116, 115, 0, 22)
  ChangeGeometry(118, 115, 0, 23)
  PlayAudio(Sound.Kitta)
  Sleep(500)
  RunThread(EndingMovingProc)
  DoMsgT({p_name, "（いけない！　塔が崩れそうだ）"}, 4000)
  DoMsgT({q_name, "生かしては……帰さん……"}, 4000)
  For i = 1 To 4
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  Next
  ChangeGeometry(110, 117, 0, 22)
  ChangeGeometry(112, 117, 0, 23)
  PlayAudio(Sound.Kitta)
  DoMsgT({p_name, "うわ！"}, 2800)
  Sleep(1500)
  DoMsgT({p_name, "（くそっ……ここまできて……）"}, 3800)
  FadeIn(255, 255, 255, 10000)
  DoMsgT({p_name, "（！？）"}, 2700)
  DoMsgT({"テレーゼのペンダントから光があふれ", _
    [TheHero].Name + "をやさしく包んだ"}, 6300)
  Sleep(3000)
  ChangeBGM(Nothing)
  RaiseEvent("StopShake")
  Sleep(3000)
  BeginOutsideWarpNoFX([Field], "Ending1")
End Procedure
