Reference
  Area\Util.txt
  Util\Audio.txt
  Area\BossUtil.txt
  Util\Party.txt
  Util\CommonDialog.txt
  Util\VisualEffect.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)

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

  AddZone("F1_US1",  2,  2, 0)
  RegisterEventHandler("F1_US1Enter", F1_US1_Enter)
  AddZone("F1_US2", 16,  2, 0)
  RegisterEventHandler("F1_US2Enter", F1_US2_Enter)
  AddZone("F1_US3",  9, 26, 0)
  RegisterEventHandler("F1_US3Enter", F1_US3_Enter)
  AddZone("F1_US4", 22, 26, 0)
  RegisterEventHandler("F1_US4Enter", F1_US4_Enter)

  AddZone("F2_DS1", 34,  2, 0)
  RegisterEventHandler("F2_DS1Enter", F2_DS1_Enter)
  AddZone("F2_DS2", 48,  2, 0)
  RegisterEventHandler("F2_DS2Enter", F2_DS2_Enter)
  AddZone("F2_DS3", 41, 26, 0)
  RegisterEventHandler("F2_DS3Enter", F2_DS3_Enter)
  AddZone("F2_DS4", 54, 26, 0)
  RegisterEventHandler("F2_DS4Enter", F2_DS4_Enter)
  AddZone("F2_US1", 34, 14, 0)
  RegisterEventHandler("F2_US1Enter", F2_US1_Enter)
  AddZone("F2_US2", 61, 20, 0)
  RegisterEventHandler("F2_US2Enter", F2_US2_Enter)

  AddZone("F3_DS1", 66, 14, 0)
  RegisterEventHandler("F3_DS1Enter", F3_DS1_Enter)
  AddZone("F3_DS2", 93, 20, 0)
  RegisterEventHandler("F3_DS2Enter", F3_DS2_Enter)
  AddZone("F3_US1", 93, 14, 0)
  RegisterEventHandler("F3_US1Enter", F3_US1_Enter)

  AddZone("F4_DS1", 125, 14, 0)
  RegisterEventHandler("F4_DS1Enter", F4_DS1_Enter)
  AddZone("F4_US1",  98, 26, 0)
  RegisterEventHandler("F4_US1Enter", F4_US1_Enter)

  AddZone("F5_DS1",  2, 58, 0)
  RegisterEventHandler("F5_DS1Enter", F5_DS1_Enter)
  AddZone("F5_US1", 29, 40, 0)
  RegisterEventHandler("F5_US1Enter", F5_US1_Enter)

  AddZone("F6_DS1", 61, 40, 0)
  RegisterEventHandler("F6_DS1Enter", F6_DS1_Enter)
  AddZone("F6_US1", 53, 52, 0)
  RegisterEventHandler("F6_US1Enter", F6_US1_Enter)

  AddZone("F7_DS1", 85, 52, 0)
  RegisterEventHandler("F7_DS1Enter", F7_DS1_Enter)

  AddZone("BossCaution", 79, 50, 0, 80, 50, 0)
  RegisterEventHandler("BossCautionEnter", BossCaution_Enter)
  
  AddBox("EastTower_B1", 26, 2, 0, BoxContent.Tool, [WhiteHat], Nothing)
  AddBox("EastTower_B2", 41, 20, 0, BoxContent.Money, 400, Nothing)
  AddBox("EastTower_B3", 93, 2, 0, BoxContent.Money, 450, Nothing)
  AddBox("EastTower_B4", 105, 15, 0, BoxContent.Money, 500, Nothing)
  AddBox("EastTower_B5", 27, 58, 0, BoxContent.Money, 550, Nothing)
  AddBox("EastTower_B6", 34, 34, 0, BoxContent.Money, 600, Nothing)
  AddBox("EastTower_B7", 75, 52, 0, BoxContent.Tool, [BlueRobe], Nothing)
  AddBox("EastTower_B8", 92, 47, 0, BoxContent.Tool, [BlueHat], Nothing)
  
  If Not TestFlag("EastTower_1") Then
    ac = AddStaticActor("Boss", 79, 42, 0, [E301].Actor, Nothing)
  End If
  
  SetStepsOfMaterial(4, 20)
  ResetStepsForEncountering()
  
  AddZone("EncZone01", 0, 0, 0, 127, 127, 0)
  RegisterEventHandler("EncZone01Step", EncZone01_Step)

  DoInsideWarp(startAt, Direction.Up, False)
  
  If Not TestFlag("EastTower_1") Then
    ChangeBGM(Music.Tower)
  Else
    ChangeBGM(Music.Duel)
  End If

  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 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_US1_Enter()
  DoStairWarp("F3_DS1", Direction.Down, True)
End Procedure

Procedure F2_US2_Enter()
  DoStairWarp("F3_DS2", Direction.Down, True)
End Procedure

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

Procedure F3_DS2_Enter()
  DoStairWarp("F2_US2", 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 EncZone01_Step(mat)
  If Not TestFlag("EastTower_1") Then
    If TestEncountering(mat) Then
      DoNormalBattle(GetEnemyPartyList())
      ResetStepsForEncountering()
    End If
  End If
End Procedure

Procedure BossCaution_Enter()
  Dim won_this_duel = False
  Dim p_name = [TheHero].Name + "："
  Dim q_name = "アルタロス："
  If TestFlag("EastTower_1") Then Return
  DoMsg(p_name, "（圧倒的な闘気を感じる……）")
  If DoYesNoMsg("この先に進みますか？") Then
    For i = 1 To 6
      LetActorWalk(GetPrimaryActor(), Direction.Up)
    Next
    ChangeBGM(Nothing)
    Sleep(1000)
    If TestFlag("NorthTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "か？")
      DoMsg(p_name, "そうだ")
      DoMsg(q_name, "俺はアルタロス　魔族四天王の一人だ")
      Sleep(500)
      DoMsg(q_name, "あのセモサーがやられたと言うから", _
        "どんな奴かと思っていたが……こんな小僧か！", _
        "あいつも衰えたな")
      DoMsg(p_name, "次はお前の番だ　アルタロス", _
        "邪神復活なんて　させないぞ")
      DoMsg(q_name, "ふん　まあ少しは力があるんだろう", _
        "だが貴様もここまでだ", _
        "我が闘気を受けて生きていられたやつはいない")
    ElseIf TestFlag("WestTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "か？")
      DoMsg(p_name, "そうだ")
      DoMsg(q_name, "俺はアルタロス　魔族四天王の一人だ")
      Sleep(500)
      DoMsg(q_name, "アフリエムがやられたと言うから", _
        "どんな奴かと思っていたが……こんな小僧か！", _
        "あの兄弟　大したことはないな")
      DoMsg(p_name, "次はお前の番だ　アルタロス", _
        "邪神復活なんて　させないぞ")
      DoMsg(q_name, "ふん", _
        "アフリエムを倒したくらいでいい気になるな", _
        "我が闘気を受けて生きていられたやつはいない")
    ElseIf TestFlag("SouthTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "か？")
      DoMsg(p_name, "そうだ")
      DoMsg(q_name, "俺はアルタロス　魔族四天王の一人だ")
      Sleep(500)
      DoMsg(q_name, "フラスタンがやられたと言うから", _
        "どんな奴かと思っていたが……こんな小僧か！", _
        "あの兄弟　大したことはないな")
      DoMsg(p_name, "次はお前の番だ　アルタロス", _
        "邪神復活なんて　させないぞ")
      DoMsg(q_name, "ふん", _
        "フラスタンを倒したくらいでいい気になるな", _
        "我が闘気を受けて生きていられたやつはいない")
    Else
      DoMsg(q_name, "ここまで来られる人間がいるとは……")
      DoMsg(p_name, "お前は？……")
      DoMsg(q_name, "我が名はアルタロス！　魔族四天王の一人だ")
      Sleep(500)
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "我々の計画を阻止しようなどと無駄なこと", _
        "わざわざ死に急ぐ必要もあるまい")
      DoMsg(p_name, "なんだと？")
      DoMsg(q_name, "今　俺以外の３人も　各地の塔から", _
        "デロク様を甦らせるべく精神波を送っている", _
        "", _
        "貴様のせいで少し遅れが出たが", _
        "復活はさほど先の話ではないだろう", _
        "そうすれば邪魔な人間どもは滅び", _
        "地上は我々魔族のものとなるのだ")
      DoMsg(p_name, "そういう事だったのか……", _
        "だが　そんな事はさせないぞ", _
        "お前を倒し　計画を阻止してやる　覚悟しろ")
      DoMsg(q_name, "なに　覚悟しろだと？", _
        "笑わせてくれるな　小僧", _
        "そんなに死にたいか", _
        "ならば今すぐ　我が闘気で冥土に送ってやろう")
    End If
    AlphaFadeInProc(192, 240, 227, 206, 2000)
    DoMsg(q_name, "粉になって死ね！")
    If Not TestFlag("EastEvo") Then
      won_this_duel = DoBossBattle({[E301]})
    Else
      won_this_duel = DoBossBattle({[E305]})
    End If
    AlphaFadeOutProc(192, 240, 227, 206, 0)
    If Not won_this_duel Then
      EndOfTheWorld()
      Return
    End If
    DoMsg(q_name, "ば　ばかな……", _
      "この俺が　人間如きに……")
    Sleep(500)
    DoMsg(q_name, "ぐふっ")
    RemoveActor(GetActor("Boss"))
    SetFlag("EastTower_1")
    EvoBoss()
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  End If
End Procedure
