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", 0, 0, 0, 31, 31, 100), 1)
  AddAreaZone(AddZone("B2", 32, 0, 0, 63, 31, 100), 1)
  AddAreaZone(AddZone("B3", 64, 0, 0, 95, 31, 100), 1)
  AddAreaZone(AddZone("B4", 96, 0, 0, 127, 31, 100), 1)
  AddAreaZone(AddZone("B5", 128, 0, 0, 159, 31, 100), 1)
  AddAreaZone(AddZone("B6", 0, 32, 0, 31, 63, 100), 1)
  AddAreaZone(AddZone("B7", 32, 32, 0, 63, 63, 100), 1)
  AddAreaZone(AddZone("B8", 64, 32, 0, 95, 63, 100), 1)
  AddAreaZone(AddZone("B9", 96, 32, 0, 127, 63, 100), 1)
  AddAreaZone(AddZone("B10", 128, 32, 0, 159, 63, 100), 1)
  AddAreaZone(AddZone("B11", 0, 64, 0, 31, 95, 100), 1)
  AddAreaZone(AddZone("B12", 32, 64, 0, 63, 95, 100), 1)
  AddAreaZone(AddZone("B13", 64, 64, 0, 95, 95, 100), 1)
  AddAreaZone(AddZone("B14", 96, 64, 0, 127, 95, 100), 1)
  AddAreaZone(AddZone("B15", 128, 64, 0, 159, 95, 100), 1)
  AddAreaZone(AddZone("B16", 0, 96, 0, 31, 127, 100), 1)
  AddAreaZone(AddZone("B17", 32, 96, 0, 63, 127, 100), 1)
  AddAreaZone(AddZone("B18", 64, 96, 0, 95, 127, 100), 1)
  AddAreaZone(AddZone("B19", 96, 96, 0, 127, 127, 100), 1)
  AddAreaZone(AddZone("B20", 128, 96, 0, 159, 127, 100), 1)

  AddZone("ExitArea", 79, 127, 0, 80, 127, 0)
  RegisterEventHandler("ExitAreaEnter", ExitArea_Enter)
  AddZone("Entrance", 80, 126, 0)

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

  'B1toB6
  AddZone("B1_6", 15, 31, 0)
  RegisterEventHandler("B1_6Enter", B1_6_Enter)
  AddZone("B1_6D", 15, 33, 0)
  AddZone("B6_1", 15, 32, 0)
  RegisterEventHandler("B6_1Enter", B6_1_Enter)
  AddZone("B6_1D", 15, 30, 0)

  'B2toB3
  AddZone("B2_3", 63, 14, 0, 63, 15, 0)
  RegisterEventHandler("B2_3Enter", B2_3_Enter)
  AddZone("B2_3D", 65, 14, 0)
  AddZone("B3_2", 64, 14, 0, 64, 15, 0)
  RegisterEventHandler("B3_2Enter", B3_2_Enter)
  AddZone("B3_2D", 62, 14, 0)

  'B2toB7
  AddZone("B2_7", 47, 31, 0)
  RegisterEventHandler("B2_7Enter", B2_7_Enter)
  AddZone("B2_7D", 47, 33, 0)
  AddZone("B7_2", 47, 32, 0)
  RegisterEventHandler("B7_2Enter", B7_2_Enter)
  AddZone("B7_2D", 47, 30, 0)

  'B3toB4
  AddZone("B3_4", 95, 14, 0, 95, 15, 0)
  RegisterEventHandler("B3_4Enter", B3_4_Enter)
  AddZone("B3_4D", 97, 14, 0)
  AddZone("B4_3", 96, 14, 0, 96, 15, 0)
  RegisterEventHandler("B4_3Enter", B4_3_Enter)
  AddZone("B4_3D", 94, 14, 0)

  'B4toB9
  AddZone("B4_9", 111, 31, 0)
  RegisterEventHandler("B4_9Enter", B4_9_Enter)
  AddZone("B4_9D", 111, 33, 0)
  AddZone("B9_4", 111, 32, 0)
  RegisterEventHandler("B9_4Enter", B9_4_Enter)
  AddZone("B9_4D", 111, 30, 0)

  'B5toB10
  AddZone("B5_10", 143, 31, 0)
  RegisterEventHandler("B5_10Enter", B5_10_Enter)
  AddZone("B5_10D", 143, 33, 0)
  AddZone("B10_5", 143, 32, 0)
  RegisterEventHandler("B10_5Enter", B10_5_Enter)
  AddZone("B10_5D", 143, 30, 0)

  'B6toB11
  AddZone("B6_11", 15, 63, 0)
  RegisterEventHandler("B6_11Enter", B6_11_Enter)
  AddZone("B6_11D", 15, 65, 0)
  AddZone("B11_6", 15, 64, 0)
  RegisterEventHandler("B11_6Enter", B11_6_Enter)
  AddZone("B11_6D", 15, 62, 0)

  'B7toB8
  AddZone("B7_8", 63, 46, 0, 63, 47, 0)
  RegisterEventHandler("B7_8Enter", B7_8_Enter)
  AddZone("B7_8D", 65, 46, 0)
  AddZone("B8_7", 64, 46, 0, 64, 47, 0)
  RegisterEventHandler("B8_7Enter", B8_7_Enter)
  AddZone("B8_7D", 62, 46, 0)

  'B8toB9
  AddZone("B8_9", 95, 46, 0, 95, 47, 0)
  RegisterEventHandler("B8_9Enter", B8_9_Enter)
  AddZone("B8_9D", 97, 46, 0)
  AddZone("B9_8", 96, 46, 0, 96, 47, 0)
  RegisterEventHandler("B9_8Enter", B9_8_Enter)
  AddZone("B9_8D", 94, 46, 0)

  'B8toB13
  AddZone("B8_13", 79, 63, 0)
  RegisterEventHandler("B8_13Enter", B8_13_Enter)
  AddZone("B8_13D", 79, 65, 0)
  AddZone("B13_8", 79, 64, 0)
  RegisterEventHandler("B13_8Enter", B13_8_Enter)
  AddZone("B13_8D", 79, 62, 0)

  'B9toB14
  AddZone("B9_14", 111, 63, 0)
  RegisterEventHandler("B9_14Enter", B9_14_Enter)
  AddZone("B9_14D", 111, 65, 0)
  AddZone("B14_9", 111, 64, 0)
  RegisterEventHandler("B14_9Enter", B14_9_Enter)
  AddZone("B14_9D", 111, 62, 0)

  'B10toB15
  AddZone("B10_15", 143, 63, 0)
  RegisterEventHandler("B10_15Enter", B10_15_Enter)
  AddZone("B10_15D", 143, 65, 0)
  AddZone("B15_10", 143, 64, 0)
  RegisterEventHandler("B15_10Enter", B15_10_Enter)
  AddZone("B15_10D", 143, 62, 0)

  'B11toB12
  AddZone("B11_12", 31, 78, 0, 31, 79, 0)
  RegisterEventHandler("B11_12Enter", B11_12_Enter)
  AddZone("B11_12D", 33, 78, 0)
  AddZone("B12_11", 32, 78, 0, 32, 79, 0)
  RegisterEventHandler("B12_11Enter", B12_11_Enter)
  AddZone("B12_11D", 30, 78, 0)

  'B12toB13
  AddZone("B12_13", 63, 78, 0, 63, 79, 0)
  RegisterEventHandler("B12_13Enter", B12_13_Enter)
  AddZone("B12_13D", 65, 78, 0)
  AddZone("B13_12", 64, 78, 0, 64, 79, 0)
  RegisterEventHandler("B13_12Enter", B13_12_Enter)
  AddZone("B13_12D", 62, 78, 0)

  'B12toB17
  AddZone("B12_17", 47, 95, 0)
  RegisterEventHandler("B12_17Enter", B12_17_Enter)
  AddZone("B12_17D", 47, 97, 0)
  AddZone("B17_12", 47, 96, 0)
  RegisterEventHandler("B17_12Enter", B17_12_Enter)
  AddZone("B17_12D", 47, 94, 0)

  'B13toB18
  AddZone("B13_18", 79, 95, 0)
  RegisterEventHandler("B13_18Enter", B13_18_Enter)
  AddZone("B13_18D", 79, 97, 0)
  AddZone("B18_13", 79, 96, 0)
  RegisterEventHandler("B18_13Enter", B18_13_Enter)
  AddZone("B18_13D", 79, 94, 0)

  'B14toB15
  AddZone("B14_15", 127, 78, 0, 127, 79, 0)
  RegisterEventHandler("B14_15Enter", B14_15_Enter)
  AddZone("B14_15D", 129, 78, 0)
  AddZone("B15_14", 128, 78, 0, 128, 79, 0)
  RegisterEventHandler("B15_14Enter", B15_14_Enter)
  AddZone("B15_14D", 126, 78, 0)

  'B14toB19
  AddZone("B14_19", 111, 95, 0)
  RegisterEventHandler("B14_19Enter", B14_19_Enter)
  AddZone("B14_19D", 111, 97, 0)
  AddZone("B19_14", 111, 96, 0)
  RegisterEventHandler("B19_14Enter", B19_14_Enter)
  AddZone("B19_14D", 111, 94, 0)

  'B16toB17
  AddZone("B16_17", 31, 110, 0, 31, 111, 0)
  RegisterEventHandler("B16_17Enter", B16_17_Enter)
  AddZone("B16_17D", 33, 110, 0)
  AddZone("B17_16", 32, 110, 0, 32, 111, 0)
  RegisterEventHandler("B17_16Enter", B17_16_Enter)
  AddZone("B17_16D", 30, 110, 0)

  'B19toB20
  AddZone("B19_20", 127, 110, 0, 127, 111, 0)
  RegisterEventHandler("B19_20Enter", B19_20_Enter)
  AddZone("B19_20D", 129, 110, 0)
  AddZone("B20_19", 128, 110, 0, 128, 111, 0)
  RegisterEventHandler("B20_19Enter", B20_19_Enter)
  AddZone("B20_19D", 126, 110, 0)

  AddBox("CaveD2_B1", 9, 6, 0, BoxContent.Money, 20000, Nothing)
  AddBox("CaveD2_B2", 80, 6, 0, BoxContent.Money, 19000, Nothing)
  AddBox("CaveD2_B3", 124, 14, 0, BoxContent.Money, 17000, Nothing)
  AddBox("CaveD2_B4", 142, 5, 0, BoxContent.Tool, [WhiteRobe], Nothing)
  AddBox("CaveD2_B5", 35, 58, 0, BoxContent.Money, 14000, Nothing)
  AddBox("CaveD2_B6", 156, 54, 0, BoxContent.Money, 23000, Nothing)
  AddBox("CaveD2_B7", 3, 90, 0, BoxContent.Money, 15000, Nothing)
  AddBox("CaveD2_B8", 156, 90, 0, BoxContent.Money, 21000, Nothing)
  AddBox("CaveD2_B9", 5, 101, 0, BoxContent.Money, 18000, Nothing)
  AddBox("CaveD2_B10", 39, 122, 0, BoxContent.Money, 16000, Nothing)
  AddBox("CaveD2_B11", 98, 114, 0, BoxContent.Money, 22000, Nothing)
  AddBox("CaveD2_B12", 142, 101, 0, BoxContent.Tool, [GoldenRing], Nothing)
  
  SetStepsOfMaterial(4, 20)
  ResetStepsForEncountering()
  
  AddZone("EncZone01", 0, 0, 0, 255, 255, 0)
  RegisterEventHandler("EncZone01Step", EncZone01_Step)
  
  DoInsideWarp(startAt, Direction.Up, False)
  
  ChangeBGM(Music.Cave)

  NotifyEntered()
  Idle(ProcessInput)
  NotifyExiting()

End Procedure

Procedure ExitArea_Enter()
  BeginOutsideWarp([Field], GetAreaObject().Tag)
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 B1_6_Enter()
  DoInsideWarp("B1_6D", Direction.None, True)
End Procedure

Procedure B6_1_Enter()
  DoInsideWarp("B6_1D", Direction.None, True)
End Procedure

Procedure B2_3_Enter()
  DoInsideWarp("B2_3D", Direction.None, True)
End Procedure

Procedure B3_2_Enter()
  DoInsideWarp("B3_2D", Direction.None, True)
End Procedure

Procedure B2_7_Enter()
  DoInsideWarp("B2_7D", Direction.None, True)
End Procedure

Procedure B7_2_Enter()
  DoInsideWarp("B7_2D", Direction.None, True)
End Procedure

Procedure B3_4_Enter()
  DoInsideWarp("B3_4D", Direction.None, True)
End Procedure

Procedure B4_3_Enter()
  DoInsideWarp("B4_3D", Direction.None, True)
End Procedure

Procedure B4_9_Enter()
  DoInsideWarp("B4_9D", Direction.None, True)
End Procedure

Procedure B9_4_Enter()
  DoInsideWarp("B9_4D", Direction.None, True)
End Procedure

Procedure B5_10_Enter()
  DoInsideWarp("B5_10D", Direction.None, True)
End Procedure

Procedure B10_5_Enter()
  DoInsideWarp("B10_5D", Direction.None, True)
End Procedure

Procedure B6_11_Enter()
  DoInsideWarp("B6_11D", Direction.None, True)
End Procedure

Procedure B11_6_Enter()
  DoInsideWarp("B11_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 B8_9_Enter()
  DoInsideWarp("B8_9D", Direction.None, True)
End Procedure

Procedure B9_8_Enter()
  DoInsideWarp("B9_8D", Direction.None, True)
End Procedure

Procedure B8_13_Enter()
  DoInsideWarp("B8_13D", Direction.None, True)
End Procedure

Procedure B13_8_Enter()
  DoInsideWarp("B13_8D", Direction.None, True)
End Procedure

Procedure B9_14_Enter()
  DoInsideWarp("B9_14D", Direction.None, True)
End Procedure

Procedure B14_9_Enter()
  DoInsideWarp("B14_9D", Direction.None, True)
End Procedure

Procedure B10_15_Enter()
  DoInsideWarp("B10_15D", Direction.None, True)
End Procedure

Procedure B15_10_Enter()
  DoInsideWarp("B15_10D", Direction.None, True)
End Procedure

Procedure B11_12_Enter()
  DoInsideWarp("B11_12D", Direction.None, True)
End Procedure

Procedure B12_11_Enter()
  DoInsideWarp("B12_11D", Direction.None, True)
End Procedure

Procedure B12_13_Enter()
  DoInsideWarp("B12_13D", Direction.None, True)
End Procedure

Procedure B13_12_Enter()
  DoInsideWarp("B13_12D", Direction.None, True)
End Procedure

Procedure B12_17_Enter()
  DoInsideWarp("B12_17D", Direction.None, True)
End Procedure

Procedure B17_12_Enter()
  DoInsideWarp("B17_12D", Direction.None, True)
End Procedure

Procedure B13_18_Enter()
  DoInsideWarp("B13_18D", Direction.None, True)
End Procedure

Procedure B18_13_Enter()
  DoInsideWarp("B18_13D", Direction.None, True)
End Procedure

Procedure B14_15_Enter()
  DoInsideWarp("B14_15D", Direction.None, True)
End Procedure

Procedure B15_14_Enter()
  DoInsideWarp("B15_14D", Direction.None, True)
End Procedure

Procedure B14_19_Enter()
  DoInsideWarp("B14_19D", Direction.None, True)
End Procedure

Procedure B19_14_Enter()
  DoInsideWarp("B19_14D", Direction.None, True)
End Procedure

Procedure B16_17_Enter()
  DoInsideWarp("B16_17D", Direction.None, True)
End Procedure

Procedure B17_16_Enter()
  DoInsideWarp("B17_16D", Direction.None, True)
End Procedure

Procedure B19_20_Enter()
  DoInsideWarp("B19_20D", Direction.None, True)
End Procedure

Procedure B20_19_Enter()
  DoInsideWarp("B20_19D", Direction.None, True)
End Procedure

Procedure EncZone01_Step(mat)
  If TestEncountering(mat) Then
    Dim ep = CreateList()
    ep.Add({[E122], [E122], [E122], [E122], [E122]})
    ep.Add({[E123], [E122], [E122], [E122], [E122]})
    ep.Add({[E123], [E123], [E122], [E122]})
    ep.Add({[E123], [E123], [E123]})
    ep.Add({[E124], [E122], [E122], [E122]})
    ep.Add({[E124], [E123], [E122]})
    ep.Add({[E124], [E124]})
    DoNormalBattle(ep)
    ResetStepsForEncountering()
  End If
End Procedure
