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("F2_DS1", 34,  2, 0)
  RegisterEventHandler("F2_DS1Enter", F2_DS1_Enter)
  AddZone("F2_US1", 41, 26, 0)
  RegisterEventHandler("F2_US1Enter", F2_US1_Enter)
  AddZone("F2_US2", 54, 26, 0)
  RegisterEventHandler("F2_US2Enter", F2_US2_Enter)

  AddZone("F3_DS1", 73, 26, 0)
  RegisterEventHandler("F3_DS1Enter", F3_DS1_Enter)
  AddZone("F3_DS2", 86, 26, 0)
  RegisterEventHandler("F3_DS2Enter", F3_DS2_Enter)
  AddZone("F3_US1", 70, 4, 0)
  RegisterEventHandler("F3_US1Enter", F3_US1_Enter)
  AddZone("F3_US2", 89, 4, 0)
  RegisterEventHandler("F3_US2Enter", F3_US2_Enter)

  AddZone("F4_DS1", 102, 4, 0)
  RegisterEventHandler("F4_DS1Enter", F4_DS1_Enter)
  AddZone("F4_DS2", 121, 4, 0)
  RegisterEventHandler("F4_DS2Enter", F4_DS2_Enter)
  AddZone("F4_US1", 105, 22, 0)
  RegisterEventHandler("F4_US1Enter", F4_US1_Enter)
  AddZone("F4_US2", 118, 22, 0)
  RegisterEventHandler("F4_US2Enter", F4_US2_Enter)

  AddZone("F5_DS1",  9, 54, 0)
  RegisterEventHandler("F5_DS1Enter", F5_DS1_Enter)
  AddZone("F5_DS2", 22, 54, 0)
  RegisterEventHandler("F5_DS2Enter", F5_DS2_Enter)
  AddZone("F5_US1",  6, 40, 0)
  RegisterEventHandler("F5_US1Enter", F5_US1_Enter)
  AddZone("F5_US2", 25, 40, 0)
  RegisterEventHandler("F5_US2Enter", F5_US2_Enter)

  AddZone("F6_DS1", 38, 40, 0)
  RegisterEventHandler("F6_DS1Enter", F6_DS1_Enter)
  AddZone("F6_DS2", 57, 40, 0)
  RegisterEventHandler("F6_DS2Enter", F6_DS2_Enter)
  AddZone("F6_US1", 40, 50, 0)
  RegisterEventHandler("F6_US1Enter", F6_US1_Enter)
  AddZone("F6_US2", 55, 50, 0)
  RegisterEventHandler("F6_US2Enter", F6_US2_Enter)

  AddZone("F7_DS1", 72, 50, 0)
  RegisterEventHandler("F7_DS1Enter", F7_DS1_Enter)
  AddZone("F7_DS2", 87, 50, 0)
  RegisterEventHandler("F7_DS2Enter", F7_DS2_Enter)
  AddZone("F7_WP1", 71, 45, 0)
  RegisterEventHandler("F7_WP1Enter", F7_WP1_Enter)
  AddZone("F7_WP2", 88, 45, 0)
  RegisterEventHandler("F7_WP2Enter", F7_WP2_Enter)

  AddZone("BossCaution", 47, 17, 0, 48, 17, 0)
  RegisterEventHandler("BossCautionEnter", BossCaution_Enter)
  
  Dim box_cb = Nothing
  If GetAliveBossCount() >= 4 Then
    box_cb = BoxStillSealed
  End If
  
  AddBox("SouthTower_B1", 11, 2, 0, BoxContent.Tool, [WhiteRing], Nothing)
  AddBox("SouthTower_B2", 41, 3, 0, BoxContent.Tool, [GoldenHat], Nothing)
  AddBox("SouthTower_B3", 54, 3, 0, BoxContent.Tool, [RedRobe], Nothing)
  AddBox("SouthTower_B4", 86, 11, 0, BoxContent.Money, 3600, box_cb)
  AddBox("SouthTower_B5", 102, 24, 0, BoxContent.Money, 1600, box_cb)
  AddBox("SouthTower_B6", 20, 44, 0, BoxContent.Money, 3100, box_cb)
  AddBox("SouthTower_B7", 42, 50, 0, BoxContent.Money, 2100, box_cb)
  AddBox("SouthTower_B8", 86, 42, 0, BoxContent.Money, 2600, box_cb)
  
  If Not TestFlag("SouthTower_1") Then
    ac = AddStaticActor("Boss", 47, 9, 0, [E302].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("SouthTower_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 F2_DS1_Enter()
  DoStairWarp("F1_US1", 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 F3_US2_Enter()
  DoStairWarp("F4_DS2", Direction.Down, True)
End Procedure

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

Procedure F4_DS2_Enter()
  DoStairWarp("F3_US2", Direction.Down, True)
End Procedure

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

Procedure F4_US2_Enter()
  DoStairWarp("F5_DS2", Direction.Down, True)
End Procedure

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

Procedure F5_DS2_Enter()
  DoStairWarp("F4_US2", Direction.Down, True)
End Procedure

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

Procedure F5_US2_Enter()
  DoStairWarp("F6_DS2", Direction.Down, True)
End Procedure

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

Procedure F6_DS2_Enter()
  DoStairWarp("F5_US2", Direction.Down, True)
End Procedure

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

Procedure F6_US2_Enter()
  DoStairWarp("F7_DS2", Direction.Down, True)
End Procedure

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

Procedure F7_DS2_Enter()
  DoStairWarp("F6_US2", Direction.Down, True)
End Procedure

Procedure F7_WP1_Enter()
  DoInsideWarp("F7_WP2", Direction.None, True)
End Procedure

Procedure F7_WP2_Enter()
  DoInsideWarp("F7_WP1", Direction.None, True)
End Procedure

Procedure EncZone01_Step(mat)
  If Not TestFlag("SouthTower_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("SouthTower_1") Then Return
  DoMsg(p_name, "（凍るような冷気を感じる……）")
  If DoYesNoMsg("この先に進みますか？") = True Then
    For i = 1 To 6
      LetActorWalk(GetPrimaryActor(), Direction.Up)
    Next
    ChangeBGM(Nothing)
    Sleep(1000)
    If TestFlag("WestTower_1") Then
      ChangeBGM(Music.Duel)
      DoMsg(q_name, "貴様……" + [TheHero].Name + "だな？")
      DoMsg(p_name, "そうだ　お前がフラスタンか")
      DoMsg(q_name, "我が兄　アフリエムの仇　取らせてもらうぞ")
    ElseIf 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("EastTower_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, 0, 30, 67, 2000)
    DoMsg(q_name, "氷の塊になって死ね！")
    If Not TestFlag("SouthEvo") Then
      won_this_duel = DoBossBattle({[E302]})
    Else
      won_this_duel = DoBossBattle({[E306]})
    End If
    AlphaFadeOutProc(192, 0, 30, 67, 0)
    If Not won_this_duel Then
      EndOfTheWorld()
      Return
    End If
    If TestFlag("WestTower_1") Then
      DoMsg(q_name, "あ　兄者　すまぬ……", _
        "仇は　取れなんだ……")
    Else
      DoMsg(q_name, "ば　ばかな……", _
        "この俺が　人間如きに……")
      Sleep(500)
      DoMsg(q_name, "だが　貴様も　白虎の塔で　死ぬのだ", _
        "我が兄　アフリエムの　炎で　灰となってな……")
    End If
    Sleep(500)
    DoMsg(q_name, "ぐふっ")
    RemoveActor(GetActor("Boss"))
    SetFlag("SouthTower_1")
    EvoBoss()
    If Not TestFlag("WestTower_1") Then SetFlag("WestEvo")
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  End If
End Procedure
