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", 15,  2, 0)
  RegisterEventHandler("F1_US1Enter", F1_US1_Enter)
  AddZone("F1_US2", 10, 17, 0)
  RegisterEventHandler("F1_US2Enter", F1_US2_Enter)
  AddZone("F1_US3", 21, 17, 0)
  RegisterEventHandler("F1_US3Enter", F1_US3_Enter)
  
  AddZone("F2_DS1", 47,  2, 0)
  RegisterEventHandler("F2_DS1Enter", F2_DS1_Enter)
  AddZone("F2_DS2", 42, 17, 0)
  RegisterEventHandler("F2_DS2Enter", F2_DS2_Enter)
  AddZone("F2_DS3", 53, 17, 0)
  RegisterEventHandler("F2_DS3Enter", F2_DS3_Enter)
  AddZone("F2_US1", 48, 27, 0)
  RegisterEventHandler("F2_US1Enter", F2_US1_Enter)
  AddZone("F2_HF", 59, 2, 0)
  
  AddZone("F3_DS1", 80, 27, 0)
  RegisterEventHandler("F3_DS1Enter", F3_DS1_Enter)
  AddZone("F3_US1", 79,  2, 0)
  RegisterEventHandler("F3_US1Enter", F3_US1_Enter)
  AddZone("F3_US2", 68,  5, 0)
  RegisterEventHandler("F3_US2Enter", F3_US2_Enter)
  AddZone("F3_US3", 91,  5, 0)
  RegisterEventHandler("F3_US3Enter", F3_US3_Enter)
  AddZone("F3_US4", 93, 15, 0)
  RegisterEventHandler("F3_US4Enter", F3_US4_Enter)
  AddZone("F3_US5", 66, 16, 0)
  RegisterEventHandler("F3_US5Enter", F3_US5_Enter)
  AddZone("F3_US6", 68, 26, 0)
  RegisterEventHandler("F3_US6Enter", F3_US6_Enter)
  AddZone("F3_US7", 91, 26, 0)
  RegisterEventHandler("F3_US7Enter", F3_US7_Enter)
  AddZone("F3_HF1", 73,  2, 0)
  AddZone("F3_HF2", 73, 24, 0)
  AddZone("F3_HF3", 86, 24, 0)
  AddZone("F3_HL", 91,  2, 0)
  RegisterEventHandler("F3_HLEnter", F3_HL_Enter)
  
  AddZone("F4_DS1", 111,  2, 0)
  RegisterEventHandler("F4_DS1Enter", F4_DS1_Enter)
  AddZone("F4_DS2", 100,  5, 0)
  RegisterEventHandler("F4_DS2Enter", F4_DS2_Enter)
  AddZone("F4_DS3", 123,  5, 0)
  RegisterEventHandler("F4_DS3Enter", F4_DS3_Enter)
  AddZone("F4_DS4", 125, 15, 0)
  RegisterEventHandler("F4_DS4Enter", F4_DS4_Enter)
  AddZone("F4_DS5",  98, 16, 0)
  RegisterEventHandler("F4_DS5Enter", F4_DS5_Enter)
  AddZone("F4_DS6", 100, 26, 0)
  RegisterEventHandler("F4_DS6Enter", F4_DS6_Enter)
  AddZone("F4_DS7", 123, 26, 0)
  RegisterEventHandler("F4_DS7Enter", F4_DS7_Enter)
  AddZone("F4_US1",  98,  5, 0)
  RegisterEventHandler("F4_US1Enter", F4_US1_Enter)
  AddZone("F4_US2", 125,  5, 0)
  RegisterEventHandler("F4_US2Enter", F4_US2_Enter)
  AddZone("F4_US3",  98, 26, 0)
  RegisterEventHandler("F4_US3Enter", F4_US3_Enter)
  AddZone("F4_US4", 125, 26, 0)
  RegisterEventHandler("F4_US4Enter", F4_US4_Enter)
  AddZone("F4_HF1", 123,  2, 0)
  AddZone("F4_HF2", 104, 19, 0)
  AddZone("F4_HL1", 105,  2, 0)
  RegisterEventHandler("F4_HL1Enter", F4_HL1_Enter)
  AddZone("F4_HL2", 105, 24, 0)
  RegisterEventHandler("F4_HL2Enter", F4_HL2_Enter)
  AddZone("F4_HL3", 118, 24, 0)
  RegisterEventHandler("F4_HL3Enter", F4_HL3_Enter)
  
  AddZone("F5_DS1",  2, 37, 0)
  RegisterEventHandler("F5_DS1Enter", F5_DS1_Enter)
  AddZone("F5_DS2", 29, 37, 0)
  RegisterEventHandler("F5_DS2Enter", F5_DS2_Enter)
  AddZone("F5_DS3",  2, 58, 0)
  RegisterEventHandler("F5_DS3Enter", F5_DS3_Enter)
  AddZone("F5_DS4", 29, 58, 0)
  RegisterEventHandler("F5_DS4Enter", F5_DS4_Enter)
  AddZone("F5_US1",  4, 58, 0)
  RegisterEventHandler("F5_US1Enter", F5_US1_Enter)
  AddZone("F5_HF", 27, 37, 0)
  AddZone("F5_HL1", 27, 34, 0)
  RegisterEventHandler("F5_HL1Enter", F5_HL1_Enter)
  AddZone("F5_HL2",  8, 51, 0)
  RegisterEventHandler("F5_HL2Enter", F5_HL2_Enter)
  
  AddZone("F6_DS1", 36, 58, 0)
  RegisterEventHandler("F6_DS1Enter", F6_DS1_Enter)
  AddZone("F6_US1", 56, 39, 0)
  RegisterEventHandler("F6_US1Enter", F6_US1_Enter)
  AddZone("F6_HL", 59, 37, 0)
  RegisterEventHandler("F6_HLEnter", F6_HL_Enter)
  
  AddZone("F7_DS1", 88, 39, 0)
  RegisterEventHandler("F7_DS1Enter", F7_DS1_Enter)

  AddZone("BossCaution", 15, 49, 0, 16, 49, 0)
  RegisterEventHandler("BossCautionEnter", BossCaution_Enter)

  AddZone("HolyBarrier", 79, 56, 0, 80, 56, 0)
  RegisterEventHandler("HolyBarrierEnter", HolyBarrier_Enter)
  
  If Not TestFlag("NorthTower_2") Then
    ac = AddActor_MTStopped("God", 79, 47, 0, [A_OldMan], ActorMotion.Down, Nothing)
  End If
  
  Dim box_cb = Nothing
  If GetAliveBossCount() >= 2 Then
    box_cb = BoxStillSealed
  End If
  
  AddBox("NorthTower_B1", 48, 7, 0, BoxContent.Money, 81000, box_cb)
  AddBox("NorthTower_B2", 66, 2, 0, BoxContent.Money, 23000, box_cb)
  AddBox("NorthTower_B3", 93, 22, 0, BoxContent.Money, 23000, box_cb)
  AddBox("NorthTower_B4", 101, 14, 0, BoxContent.Money, 37000, box_cb)
  AddBox("NorthTower_B5", 112, 6, 0, BoxContent.Money, 60000, box_cb)
  AddBox("NorthTower_B6", 122, 14, 0, BoxContent.Money, 37000, box_cb)
  AddBox("NorthTower_B7", 9, 35, 0, BoxContent.Tool, [GoldenRobe], Nothing)
  AddBox("NorthTower_B8", 22, 35, 0, BoxContent.Tool, [AuraDocB], Nothing)
  AddBox("NorthTower_B9", 39, 39, 0, BoxContent.Money, 50000, box_cb)
  AddBox("NorthTower_B10", 59, 58, 0, BoxContent.Money, 50000, box_cb)
  AddBox("NorthTower_B11", 80, 47, 0, BoxContent.Empty, Nothing, NorthTower_B11_Contact)
  
  If Not TestFlag("NorthTower_1") Then
    ac = AddStaticActor("Boss", 15, 41, 0, [E304].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("NorthTower_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 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_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 F3_US2_Enter()
  DoStairWarp("F4_DS2", Direction.Down, True)
End Procedure

Procedure F3_US3_Enter()
  DoStairWarp("F4_DS3", Direction.Down, True)
End Procedure

Procedure F3_US4_Enter()
  DoStairWarp("F4_DS4", Direction.Down, True)
End Procedure

Procedure F3_US5_Enter()
  DoStairWarp("F4_DS5", Direction.Down, True)
End Procedure

Procedure F3_US6_Enter()
  DoStairWarp("F4_DS6", Direction.Down, True)
End Procedure

Procedure F3_US7_Enter()
  DoStairWarp("F4_DS7", Direction.Down, True)
End Procedure

Procedure F3_HL_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F2_HF", 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_DS3_Enter()
  DoStairWarp("F3_US3", Direction.Down, True)
End Procedure

Procedure F4_DS4_Enter()
  DoStairWarp("F3_US4", Direction.Down, True)
End Procedure

Procedure F4_DS5_Enter()
  DoStairWarp("F3_US5", Direction.Down, True)
End Procedure

Procedure F4_DS6_Enter()
  DoStairWarp("F3_US6", Direction.Down, True)
End Procedure

Procedure F4_DS7_Enter()
  DoStairWarp("F3_US7", 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 F4_US3_Enter()
  DoStairWarp("F5_DS3", Direction.Down, True)
End Procedure

Procedure F4_US4_Enter()
  DoStairWarp("F5_DS4", Direction.Down, True)
End Procedure

Procedure F4_HL1_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F3_HF1", Direction.Down, True)
End Procedure

Procedure F4_HL2_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F3_HF2", Direction.Down, True)
End Procedure

Procedure F4_HL3_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F3_HF3", 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_DS3_Enter()
  DoStairWarp("F4_US3", Direction.Down, True)
End Procedure

Procedure F5_DS4_Enter()
  DoStairWarp("F4_US4", Direction.Down, True)
End Procedure

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

Procedure F5_HL1_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F4_HF1", Direction.Down, True)
End Procedure

Procedure F5_HL2_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F4_HF2", 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 F6_HL_Enter(the_bounds)
  DoFallingWarp(the_bounds, "F5_HF", Direction.Down, True)
End Procedure

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

Procedure EncZone01_Step(mat)
  If Not TestFlag("NorthTower_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 = "セモサー："
  Dim has_gb = AnybodyHas([RoyalRuby])
  If TestFlag("NorthTower_1") Then Return
  DoMsg(p_name, "（忌まわしい妖気を感じる……）")
  If DoYesNoMsg("この先に進みますか？") = True Then
    For i = 1 To 6
      LetActorWalk(GetPrimaryActor(), Direction.Up)
    Next
    ChangeBGM(Nothing)
    Sleep(1000)
    ChangeBGM(Music.Duel)
    DoMsg(q_name, "やはり来たか　" + [TheHero].Name)
    DoMsg(p_name, "なぜ俺の名を知っている？")
    DoMsg(q_name, "私は四天王の長　セモサー", _
      "長として情報収集を怠らぬのは当然だ")
    If TestFlag("EastTower_1") AndAlso _
      TestFlag("SouthTower_1") AndAlso TestFlag("WestTower_1") Then
      DoMsg(q_name, "まさか　ただの人間である貴様に", _
        "我々がこれほど追い込まれるとは思わなかったが……")
    Else
      DoMsg(q_name, "それにしても　あの３人より先に", _
        "私を倒しにくるとは……ずいぶんなめられたものだ")
    End If
    DoMsg(q_name, "しかし　ここまでだ")
    AlphaFadeInProc(192, 195, 145, 67, 2000)
    DoMsg(q_name, "我が秘術を破らぬ限り　貴様に勝ち目はない", _
      "魂の抜け殻となるがいい！")
    If has_gb Then
      [TheHero].StatusDP += 100
    End If
    If Not TestFlag("NorthEvo") Then
      won_this_duel = DoBossBattle({[E304]})
    Else
      won_this_duel = DoBossBattle({[E308]})
    End If
    AlphaFadeOutProc(192, 195, 145, 67, 0)
    If Not won_this_duel Then
      EndOfTheWorld()
      Return
    End If
    If has_gb Then
      [TheHero].StatusDP -= 100
      SetFlag("GBSpoiled")
    End If
    DoMsg(q_name, "ば　ばかな……", _
      "貴様は　いったい……")
    Sleep(500)
    DoMsg(q_name, "ぐふっ")
    RemoveActor(GetActor("Boss"))
    SetFlag("NorthTower_1")
    EvoBoss()
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
  End If
End Procedure

Procedure HolyBarrier_Enter()
  Dim p_name = [TheHero].Name + "："
  Dim q_name = "？？？："
  If TestFlag("NorthTower_2") Then Return
  If GetAliveBossCount() = 0 Then
    For i = 1 To 7
      LetActorWalk(GetPrimaryActor(), Direction.Up)
    Next
    DoMsg(p_name, "あなたは！")
    DoMsg(q_name, "島の洞窟以来じゃの", _
      [TheHero].Name + "よ")
    DoMsg(p_name, "いったい何者なのですか？")
    DoMsg(q_name, "俺は神　エラテだ")
    q_name = "神エラテ："
    DoMsg(p_name, "神？………………")
    DoMsg(q_name, "今から話す事をよく聞け")
    Sleep(500)
    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(500)
    DoMsg(q_name, "遥か昔　今と同じように", _
      "奴が世界を滅ぼそうとしていた", _
      "そこで俺は奴と戦ったのだ", _
      "戦いには勝った　奴をその居城だった塔ごと", _
      "地底に封じ込めることにも成功した", _
      "だが神同士の戦いはあまりに激しく", _
      "この世界のほとんどが壊れてしまった", _
      "もちろん人間もたくさん死んだ……", _
      "俺の力は　人間には強大すぎて", _
      "邪神同様の脅威でしかないのだ")
    Sleep(500)
    DoMsg(q_name, "だから" + [TheHero].name + "　おまえが戦え", _
      "そうすれば　多くの命を失わずに済む", _
      "", _
      "しかし無理ならば　俺が戦うしかない", _
      "全てを破壊される前にな")
    Sleep(500)
    DoMsg(p_name, "邪神はどこに？")
    DoMsg(q_name, "ここから南に砂漠があり", _
      "その近くに塔がある　それこそが奴の居城だ")
    Sleep(500)
    DoMsg(q_name, "いいか　" + [TheHero].Name, _
      "奴は強い　魔族四天王などとは次元が違うのだ", _
      "生きて帰られないかも知れない", _
      "だが　俺には見えるのだ", _
      "お前が奴を倒し　塔の傍らに立つ姿が")
    Sleep(500)
    RemoveActor("God")
    DoMsg(q_name, "その箱を開けるがよい", _
      "お前なら正しき方向に使うと信じている", _
      "頼んだぞ……")
    SetFlag("NorthTower_2")
  Else
    LetActorWalk(GetPrimaryActor(), Direction.Down)
    DoMsg("なにかの　ちからで　おしもどされた")
  End If
End Procedure

Procedure NorthTower_B11_Contact(tb)
  If Not TestFlag(tb) Then
    SetFlag(tb)
    tb.Material = [A_TBOpened]
    Dim p = 0
    If TestFlag("Ibye_2") AndAlso Not TestFlag("RRSpoiled") Then p += 1
    If TestFlag("Bamcake_2") Then p += 3
    If TestFlag("Menom_3") AndAlso Not TestFlag("GBSpoiled")  Then p += 5
    GiveUsTool([GodFinger])
    GiveUsTool([KillDoc])
    If p >= 3 Then
      GiveUsTool([PsychicHelmet])
      GiveUsTool([HellFireDoc])
    End If
    If p >= 6 Then
      GiveUsTool([MightyShield])
      GiveUsTool([CrimsonFlareDoc])
    End If
    If p = 9 Then
      GiveUsTool([PlanetSuits])
      GiveUsTool([MeteoDoc])
    End If
    DoMsg("神の装備と魔法を手に入れた")
  Else
    DoMsg("すでに　あけられている")
  End If
End Procedure
