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", 0, 32, 0, 31, 63, 100), 1)
  AddAreaZone(AddZone("B5", 32, 32, 0, 63, 63, 100), 1)
  AddAreaZone(AddZone("B6", 64, 32, 0, 95, 63, 100), 1)
  AddAreaZone(AddZone("B7", 0, 64, 0, 31, 95, 100), 1)
  AddAreaZone(AddZone("B8", 32, 64, 0, 63, 95, 100), 1)
  AddAreaZone(AddZone("B9", 64, 64, 0, 95, 95, 100), 1)
  AddAreaZone(AddZone("B10", 0, 96, 0, 31, 127, 100), 1)
  AddAreaZone(AddZone("B11", 32, 96, 0, 63, 127, 100), 1)
  AddAreaZone(AddZone("B12", 64, 96, 0, 95, 127, 100), 1)

  AddZone("ExitArea", 47, 127, 0, 48, 127, 0)
  RegisterEventHandler("ExitAreaEnter", ExitArea_Enter)
  AddZone("Entrance", 48, 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)

  'B1toB4
  AddZone("B1_4", 15, 31, 0)
  RegisterEventHandler("B1_4Enter", B1_4_Enter)
  AddZone("B1_4D", 15, 33, 0)
  AddZone("B4_1", 15, 32, 0)
  RegisterEventHandler("B4_1Enter", B4_1_Enter)
  AddZone("B4_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)

  'B3toB6
  AddZone("B3_6", 79, 31, 0)
  RegisterEventHandler("B3_6Enter", B3_6_Enter)
  AddZone("B3_6D", 79, 33, 0)
  AddZone("B6_3", 79, 32, 0)
  RegisterEventHandler("B6_3Enter", B6_3_Enter)
  AddZone("B6_3D", 79, 30, 0)

  'B4toB7
  AddZone("B4_7", 15, 63, 0)
  RegisterEventHandler("B4_7Enter", B4_7_Enter)
  AddZone("B4_7D", 15, 65, 0)
  AddZone("B7_4", 15, 64, 0)
  RegisterEventHandler("B7_4Enter", B7_4_Enter)
  AddZone("B7_4D", 15, 62, 0)

  'B5toB6
  AddZone("B5_6", 63, 46, 0, 63, 47, 0)
  RegisterEventHandler("B5_6Enter", B5_6_Enter)
  AddZone("B5_6D", 65, 46, 0)
  AddZone("B6_5", 64, 46, 0, 64, 47, 0)
  RegisterEventHandler("B6_5Enter", B6_5_Enter)
  AddZone("B6_5D", 62, 46, 0)

  'B5toB8
  AddZone("B5_8", 47, 63, 0)
  RegisterEventHandler("B5_8Enter", B5_8_Enter)
  AddZone("B5_8D", 47, 65, 0)
  AddZone("B8_5", 47, 64, 0)
  RegisterEventHandler("B8_5Enter", B8_5_Enter)
  AddZone("B8_5D", 47, 62, 0)

  'B6toB9
  AddZone("B6_9", 79, 63, 0)
  RegisterEventHandler("B6_9Enter", B6_9_Enter)
  AddZone("B6_9D", 79, 65, 0)
  AddZone("B9_6", 79, 64, 0)
  RegisterEventHandler("B9_6Enter", B9_6_Enter)
  AddZone("B9_6D", 79, 62, 0)

  'B7toB8
  AddZone("B7_8", 31, 78, 0, 31, 79, 0)
  RegisterEventHandler("B7_8Enter", B7_8_Enter)
  AddZone("B7_8D", 33, 78, 0)
  AddZone("B8_7", 32, 78, 0, 32, 79, 0)
  RegisterEventHandler("B8_7Enter", B8_7_Enter)
  AddZone("B8_7D", 30, 78, 0)

  'B7toB10
  AddZone("B7_10", 15, 95, 0)
  RegisterEventHandler("B7_10Enter", B7_10_Enter)
  AddZone("B7_10D", 15, 97, 0)
  AddZone("B10_7", 15, 96, 0)
  RegisterEventHandler("B10_7Enter", B10_7_Enter)
  AddZone("B10_7D", 15, 94, 0)

  'B8toB11
  AddZone("B8_11", 47, 95, 0)
  RegisterEventHandler("B8_11Enter", B8_11_Enter)
  AddZone("B8_11D", 47, 97, 0)
  AddZone("B11_8", 47, 96, 0)
  RegisterEventHandler("B11_8Enter", B11_8_Enter)
  AddZone("B11_8D", 47, 94, 0)

  'B9toB12
  AddZone("B9_12", 79, 95, 0)
  RegisterEventHandler("B9_12Enter", B9_12_Enter)
  AddZone("B9_12D", 79, 97, 0)
  AddZone("B12_9", 79, 96, 0)
  RegisterEventHandler("B12_9Enter", B12_9_Enter)
  AddZone("B12_9D", 79, 94, 0)

  'B11toB12
  AddZone("B11_12", 63, 110, 0, 31, 111, 0)
  RegisterEventHandler("B11_12Enter", B11_12_Enter)
  AddZone("B11_12D", 65, 110, 0)
  AddZone("B12_11", 64, 110, 0, 32, 111, 0)
  RegisterEventHandler("B12_11Enter", B12_11_Enter)
  AddZone("B12_11D", 62, 110, 0)
  
  AddBox("CaveB1_B1", 3, 6, 0, BoxContent.Money, 720, Nothing)
  AddBox("CaveB1_B2", 57, 22, 0, BoxContent.Money, 800, Nothing)
  AddBox("CaveB1_B3", 70, 6, 0, BoxContent.Money, 720, Nothing)
  AddBox("CaveB1_B4", 24, 38, 0, BoxContent.Money, 630, Nothing)
  AddBox("CaveB1_B5", 35, 38, 0, BoxContent.Money, 540, Nothing)
  AddBox("CaveB1_B6", 88, 70, 0, BoxContent.Money, 540, Nothing)
  AddBox("CaveB1_B7", 92, 114, 0, BoxContent.Money, 450, Nothing)
  
  If TestFlag("Ibye_1") AndAlso Not TestFlag("CaveB1_1") Then
    ac = AddStaticActor("Ruby", 21, 109, 1, [A_Ruby], Ruby_Contact)
  End If
  
  SetStepsOfMaterial(4, 20)
  ResetStepsForEncountering()
  
  AddZone("EncZone01", 0, 0, 0, 127, 127, 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_4_Enter()
  DoInsideWarp("B1_4D", Direction.None, True)
End Procedure

Procedure B4_1_Enter()
  DoInsideWarp("B4_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 B3_6_Enter()
  DoInsideWarp("B3_6D", Direction.None, True)
End Procedure

Procedure B6_3_Enter()
  DoInsideWarp("B6_3D", Direction.None, True)
End Procedure

Procedure B4_7_Enter()
  DoInsideWarp("B4_7D", Direction.None, True)
End Procedure

Procedure B7_4_Enter()
  DoInsideWarp("B7_4D", Direction.None, True)
End Procedure

Procedure B5_6_Enter()
  DoInsideWarp("B5_6D", Direction.None, True)
End Procedure

Procedure B6_5_Enter()
  DoInsideWarp("B6_5D", Direction.None, True)
End Procedure

Procedure B5_8_Enter()
  DoInsideWarp("B5_8D", Direction.None, True)
End Procedure

Procedure B8_5_Enter()
  DoInsideWarp("B8_5D", Direction.None, True)
End Procedure

Procedure B6_9_Enter()
  DoInsideWarp("B6_9D", Direction.None, True)
End Procedure

Procedure B9_6_Enter()
  DoInsideWarp("B9_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 B7_10_Enter()
  DoInsideWarp("B7_10D", Direction.None, True)
End Procedure

Procedure B10_7_Enter()
  DoInsideWarp("B10_7D", Direction.None, True)
End Procedure

Procedure B8_11_Enter()
  DoInsideWarp("B8_11D", Direction.None, True)
End Procedure

Procedure B11_8_Enter()
  DoInsideWarp("B11_8D", Direction.None, True)
End Procedure

Procedure B9_12_Enter()
  DoInsideWarp("B9_12D", Direction.None, True)
End Procedure

Procedure B12_9_Enter()
  DoInsideWarp("B12_9D", 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 EncZone01_Step(mat)
  If TestEncountering(mat) Then
    Dim ep = CreateList()
    ep.Add({[E107], [E107], [E107], [E107], [E107]})
    ep.Add({[E108], [E107], [E107], [E107], [E107]})
    ep.Add({[E108], [E108], [E107], [E107]})
    ep.Add({[E108], [E108], [E108]})
    ep.Add({[E109], [E107], [E107], [E107]})
    ep.Add({[E109], [E108], [E107]})
    ep.Add({[E109], [E109]})
    DoNormalBattle(ep)
    ResetStepsForEncountering()
  End If
End Procedure

Procedure Ruby_Contact()
  DoMsg("（この紅い石は？……）")
  RemoveActor("Ruby")
  Sleep(500)
  DoMsg("（これが王家のルビーというものか？）", _
    "（盗賊はモンスターにやられたんだな）")
  Sleep(500)
  DoMsg("（どうやら傷は付いていないみたいだ）", _
    "（イビエの城に　返しに行こう）")
  GiveUsTool([RoyalRuby])
  SetFlag("CaveB1_1")
End Procedure
