//-----------------------------------------------------------------------------
//--- created by iNs - Copyright © 2011 --- Test.hps v0.3b (beta) ---
//-----------------------------------------------------------------------------
//--- Last Update: 11. April 2011 ---
//-----------------------------------
void OnEnter()
{
  //--- PRELOADING ---//
  PreloadSound("react_breath1.ogg");        // wake up (Intro)
  PreloadSound("react_breath2.ogg");        // wake up (Intro)
  PreloadSound("react_breath4.ogg");        // open cabinet: broom
  PreloadSound("insanity_whisper01.ogg");    // open cabinet: broom
  PreloadSound("unlock_door");                // unlock door / chest
  PreloadSound("lock_door");                // secret book succes
  PreloadSound("gameplay_tick");            // secret books wrong
  PreloadSound("02_puzzle.ogg");            // room 2 painting
  // callback function for all keys (Name, KeyName, ObjectName, FunctionName, Destroy_Item?)
  AddUseItemCallback("UseExit", "Room1_KeyChest", "Room1_Chest", "UseKey_Chest", true);
  AddUseItemCallback("UseExit", "Room2_KeyStorage", "StorageRoom_Door", "UseKey", true);
  // Use Item Callback - Cellar Padlock / acid
  AddUseItemCallback("UseAcid", "ChemicalContainer", "Cellar_Padlock", "UseAcid", true);
  // ROOM 2 SECRET BOOKS for SECRET STORAGE ROOM
  SetLocalVarInt("VarSecretMoves", 0);
  SetLocalVarString("VarSecretCode", "");
  for(int i=1;i<=4;i++)
  {
    AddEntityCollideCallback("SecretBook_"+i, "AreaSecretBook_"+i, "CollideSecretBook", false, 0);
  }
  //--- STATUS of LIGHTS ---//
  AddEntityCollideCallback("Player", "Room1_CandleSmall_ON", "CandleSmall_On", false, 1);
  AddEntityCollideCallback("Player", "Room1_CandleSmall_OFF", "CandleSmall_Off", false, 1);
  AddEntityCollideCallback("Player", "Room1_CandleBig_ON", "CandleBig_On", false, 1);
  AddEntityCollideCallback("Player", "Room1_CandleBig_OFF", "CandleBig_Off", false, 1);
  AddEntityCollideCallback("Player", "Room3_Candles_ON", "Room3Candles_On", false, 1);
  AddEntityCollideCallback("Player", "Room3_Candles_OFF", "Room3Candles_Off", false, 1);
  AddEntityCollideCallback("Player", "StairHall_Candles_ON", "StairHallCandles_On", false, 1);
  AddEntityCollideCallback("Player", "StairHall_Candles_OFF1", "StairHallCandles_Off", false, 1);
  AddEntityCollideCallback("Player", "StairHall_Candles_OFF2", "StairHallCandles_Off", false, 1);
  // ROOM 2 - PAINTING - (delete callback after collide)
  AddEntityCollideCallback("Room2_Painting", "AreaPainting", "PuzzleSound", true, 1);
  // STORAGE ROOM - STATUE - (delete callback after collide)
  AddEntityCollideCallback("StorageRoom_Door", "AreaStatue", "SmashStatue", true, 1);
  // ROOM 3 - ON ENTER - (delete callback after collide)
  AddEntityCollideCallback("Player", "Room3_AreaDoor", "BeginVision", true, 1);
  // ROOM 4 - Area next to table - (delete callback after collide)
  AddEntityCollideCallback("Player", "Room4_AreaTable", "BreakTables", true, 1);
  // HALLWAY - enable grunts - (delete callback after collide)
  AddEntityCollideCallback("Player", "HallM_AreaGrunt", "EnableHallGrunts", true, 1);
  //--- ROOM 3 - MOVE  ---//
  AddEntityCollideCallback("Player", "MoveL_1", "MoveLeft_1", false, 1);
  AddEntityCollideCallback("Player", "MoveL_2", "MoveLeft_2", false, 1);
  AddEntityCollideCallback("Player", "MoveL_3", "MoveLeft_3", false, 1);
  AddEntityCollideCallback("Player", "MoveR_1", "MoveRight_1", false, 1);
  AddEntityCollideCallback("Player", "MoveR_2", "MoveRight_2", false, 1);
  AddEntityCollideCallback("Player", "MoveR_3", "MoveRight_3", false, 1);
  AddEntityCollideCallback("Player", "Stop_1", "StopMove_1", false, 1);
  AddEntityCollideCallback("Player", "Stop_2", "StopMove_2", false, 1);
  AddEntityCollideCallback("Player", "Stop_3", "StopMove_3", false, 1);
  //--- EXIT AREA - PLAY CREDITS  ---//
  AddEntityCollideCallback("Player", "ExitArea", "Credits", false, 1);
  //--- CHECKPOINT ---//
  CheckPoint ("Room1_Checkpoint", "PlayerStartArea_2", "PlayerDied", "DeathMessage", "Dead");
  // dead human body - push down
  AddPropImpulse("Room3_Body", 0, 30, 0, "World");
}
//-----------------------------------------------------------------------------
void OnStart()
{
  // no lantern oil and no tinderboxes at start!
  SetPlayerLampOil(0.0f);
  // Lock the Chest in Room1
  SetLeverStuckState("Room1_Chest", -1, false);
  //--- DEBUG --- //
  if(ScriptDebugOn())
  {
    // Lantern
    GiveItemFromFile("lantern", "lantern.ent");
    SetPlayerLampOil(100.0f);
    // Tinderbox - 10x
    for(int i=0;i<10;i++) GiveItemFromFile("tinderbox_"+i, "tinderbox.ent");
  }
  else
  {
    // play Intro
    Intro();
  }
}
//-----------------------------------------------------------------------------
//--- INTRO ---
//-----------------------------------------------------------------------------
void Intro()
{
  // Debug Message
  AddDebugMessage("Begin Intro", false);
  // disbale player movement
  SetPlayerActive(false);
  // black screen
  FadeOut(0.0f);
  // look at cabinet's left lower corner
  StartPlayerLookAt("Room1_LookAt_1", 1, 3, "");
  AddTimer("1",  5, "IntroTimer"); //  5 seconds
}
//---
void IntroTimer(string &in asTimer)
{
  if(asTimer == "1")
  {
    // close door
    SetSwingDoorClosed("Room1_Door", true, true);
    AddTimer("2", 1, "IntroTimer"); //  1 second
  }
  else if(asTimer == "2")
  {
    // set player crouching
    SetPlayerCrouching(true);
    SetPlayerActive(false);
    // play 'breathing sound' as 'wake up noise'
    PlayGuiSound("react_breath1.ogg", 0.5f);
    // blur
    FadeRadialBlurTo(0.10, 2);
    FadeRadialBlurTo(0.10, 2);
    // normal screen
    FadeIn(13.0f);
    FadeImageTrailTo(1, 2);
    // let player look next to door
    StartPlayerLookAt("Room1_LookAt_2", 2, 3, "");
    AddTimer("3", 2, "IntroTimer"); //  2 seconds
  }
  else if(asTimer == "3")
  {
    StopPlayerLookAt();
    // black screen
    FadeOut(2.5f);
    // Change all settings to default
    FadeRadialBlurTo(0, 5);
    FadeImageTrailTo(0, 5);
    FadePlayerRollTo(0, 33, 33);
    // normal screen
    FadeIn(2.7f);
    SetMessage("Message", "Hint0", 0);
    AddDebugMessage("End Intro", false);
    AddTimer("4", 1, "IntroTimer"); //  1 second
  }
  else if(asTimer == "4")
  {
    // play 'breathing sound' as 'wake up noise'
    PlayGuiSound("react_breath2.ogg", 0.5f);
    SetPlayerActive(true);
    SetPlayerCrouching(false);
    // ambient music 'scary'
    PlayMusic("18_amb", false, 0.8, 5, 0, true);
    AddTimer("5", 30, "IntroTimer"); // 30 seconds
  }
  else if(asTimer == "5")
  {
    // ambient music 'normal'
    PlayMusic("10_amb", true, 0.5, 5, 0, true);
  }
}
//-----------------------------------------------------------------------------
//--- INTERACT FUNCTION ---
//-----------------------------------------------------------------------------
void Interact(string &in asEntity)
{
  // Is entity locked door?
  if (GetSwingDoorLocked(asEntity))
  {
    AddDebugMessage("Interact with " + asEntity, false);
    if (asEntity == "StorageRoom_Door")
    {
      if (HasItem("Room2_KeyStorage"))
      {
        SetMessage("Messages", "StorageDoorKey_Message", 4);
      }
      else if (GetSwingDoorLocked("StorageRoom_Door"))
      {
        SetMessage("Messages", "StorageDoorNoKey_Message", 4);
      }
    }
    else
    {
      SetMessage("Messages", "DoorLocked_Message", 3);
    }
  }
  //---
  else if (asEntity == "Room1_Door")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    if (!HasItem("Room1_Lantern"))
    {
      SetMessage("Messages", "Room1_Door_Message", 4);
    }
  }
  //---
  else if (asEntity == "Room1_Chest")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    if (GetLeverState(asEntity) == -1)
    {
      AddDebugMessage("Room1_Chest is locked!", false);
      if (!QuestIsAdded("Room1_Chest"))
      {
        // quest name, language file name
        AddQuest("Room1_Chest","Room1_Chest");
        SetMessage("Messages", "Room1_Chest_Message", 3);
      }
      else if (!QuestIsCompleted("Room1_Chest"))
      {
        SetMessage("Messages", "Room1_Chest_Message", 3);
      }
      else if (HasItem("Room1_KeyChest"))
      {
        SetMessage("Messages", "Room1_Key_Message", 3);
      }
    }
  }
  //---
  else if (asEntity == "Room1_KeyChest")
  {
    GiveSanityBoostSmall();
    CompleteQuest("Room1_Chest", "Room1_Chest");
  }
  //---
  else if (asEntity == "Room1_Cabinet")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // disable player movement
    SetPlayerActive(false);
    // disable lantern
    SetLanternActive(false, false);
    // push the player a little bit backwards
    AddPlayerBodyForce(-7500.0f, 0.0f, 0.0f, false);
    // open the cabinet door a little bit, so that the broom becomes visible
    AddPropImpulse("Room1_Cabinet", 0, 0, 30, "World");
    // Sanity Damage
    GiveSanityDamage(20.0f, true);
    // play 'breathing sound' as 'shocking noise'
    PlayGuiSound("react_breath4.ogg", 0.9f);
    // play 'whisper' as 'shocking noise'
    PlayGuiSound("insanity_whisper01.ogg", 0.8f);
    // shake the screen
    StartScreenShake(0.13f, 0.5f, 0.1f, 0.1f);
    // let player look at the floor, where broom lies
    StartPlayerLookAt("Room1_LookAt_3", 10, 25, "");
    AddTimer("Room1_Cabinet", 2, "InteractTimer"); // 2 seconds
  }
  //---
  else if (asEntity == "Room2_NotePaper")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // neuen Checkpoint setzen
    CheckPoint ("Room2_Checkpoint", "PlayerStartArea_3", "PlayerDied", "DeathMessage", "Dead");
  }
  //---
  else if (asEntity == "SecretBook_1")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // set secret books stuck state to default (free/unstuck)
    SetPropObjectStuckState(asEntity, 0);
  }
  else if (asEntity == "SecretBook_2")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // set secret books stuck state to default (free/unstuck)
    SetPropObjectStuckState(asEntity, 0);
  }
  else if (asEntity == "SecretBook_3")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // set secret books stuck state to default (free/unstuck)
    SetPropObjectStuckState(asEntity, 0);
  }
  else if (asEntity == "SecretBook_4")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // set secret books stuck state to default (free/unstuck)
    SetPropObjectStuckState(asEntity, 0);
  }
  //---
  else if (asEntity == "Cellar_Padlock")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    if (HasItem("ChemicalContainer"))
    {
      // Message - use acid on padlock
      SetMessage("Messages", "Padlock_UseAcid_Message", 4);
    }
    else if (StringContains(GetLocalVarString("VarSecretCode"), "done"))
    {
      if (!QuestIsAdded("PadlockGoStorage"))
      {
        // quest name, language file name
        AddQuest("PadlockGoStorage","PadlockGoStorage");
        SetMessage("Messages", "Padlock_Locked_Message", 3);
      }
      else if (!QuestIsCompleted("PadlockGoStorage") && GetSwingDoorLocked("Cellar_Hatch"))
      {
        SetMessage("Messages", "Padlock_Locked_Message", 3);
      }
    }
    else if (GetSwingDoorLocked("Cellar_Hatch"))
    {
      if (!QuestIsAdded("PadlockFindStorage"))
      {
        // quest name, language file name
        AddQuest("PadlockFindStorage","PadlockFindStorage");
        SetMessage("Messages", "Padlock_Locked_Message", 3);
      }
      else if (!QuestIsCompleted("PadlockFindStorage") && GetSwingDoorLocked("Cellar_Hatch"))
      {
        SetMessage("Messages", "Padlock_Locked_Message", 3);
      }
    }
  }
  else if (asEntity == "Cellar_Padlock_Broken")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // break sound
    PlaySoundAtEntity("break", "impact_metal_high", "Cellar_Padlock_Broken", 0.0f, false);
    // unlock the hatch
    SetSwingDoorLocked("Cellar_Hatch", false, false);
    GiveSanityBoostSmall();
    // complete/remove quest
    CompleteQuest("PadlockGoStorage", "PadlockGoStorage");
    // acivate ladder area
    SetEntityActive("CellarLadderArea", true);
  }
  else if (asEntity == "ChemicalContainer")
  {
    AddDebugMessage("Interact with " + asEntity, false);
    // complete quest
    if (QuestIsAdded("PadlockFindStorage"))
    {
      GiveSanityBoostSmall();
      CompleteQuest("PadlockFindStorage", "PadlockFindStorage");
    }
  }
}
//-----------------------------------------------------------------------------
//--- INTERACT TIMER FUNCTION ---
//-----------------------------------------------------------------------------
void InteractTimer(string &in asTimer)
{
  if(asTimer == "Room1_Cabinet" || asTimer == "Room2_Shelf")
  {
    StopPlayerLookAt();
    // activate player movement
    SetPlayerActive(true);
  }
  //---
  else if (asTimer == "SecretBooksMove")
  {
    // "locked" sound as success noise
    PlayGuiSound("gameplay_tick", 0.8f);
    // move secret books to normal pos
    SetPropObjectStuckState("SecretBook_*", -1);
    // timer for changing stuck state + message
    AddTimer("SecretBooksFree", 1, "InteractTimer"); // 1 second
  }
  else if (asTimer == "SecretBooksFree")
  {
    // set secret books stuck state to default (free/unstuck)
    SetPropObjectStuckState("SecretBook_*", 0);
    // Message - nothing happened... books back at original position
    SetMessage("Messages", "Room2_SecretBooks_Message", 4);
    // reset vars
    SetLocalVarInt("VarSecretMoves", 0);
    SetLocalVarString("VarSecretCode", "");
  }
  //---
  else if (asTimer == "DestroyParticle")
  {
    DestroyParticleSystem("dust");
    AddTimer("5", 3, "IntroTimer"); // 3 seconds
  }
  //---
  else if (asTimer == "StorageRoom_Statue")
  {
    // play 'breathing sound' as 'shocking noise'
    PlayGuiSound("react_breath4.ogg", 1.0f);
  }
}
//-----------------------------------------------------------------------------
void LookAt(string &in asEntity, int alState)
{
  if (asEntity == "Room1_Broom" && alState == 1)
  {
    AddDebugMessage("Player looking at Room1_Broom", false);
  }
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//--- USE KEY FUNCTION ---
//-----------------------------------------------------------------------------
void UseKey(string &in asItem, string &in asEntity)
{
  // unlock the door
  SetSwingDoorLocked(asEntity, false, true);
  // play 'unlock door' sound
  PlayGuiSound("unlock_door", 0.4f);
  // remove the key from inventory, cause it is not needed anymore
  RemoveItem(asItem);
}
//---
void UseKey_Chest(string &in asItem, string &in asEntity)
{
  // unlock the chest
  SetLeverStuckState(asEntity, 0, false);
  // play 'unlock door' sound
  PlayGuiSound("unlock_door", 0.4f);
  // remove the key from inventory, cause it is not needed anymore
  RemoveItem(asItem);
}
//-----------------------------------------------------------------------------
//--- USE ACID FUNCTION ---
//-----------------------------------------------------------------------------
void UseAcid(string &in asItem, string &in asEntity)
{
  // remove the ChemicalContainer (filled one with acid) from inventory
  RemoveItem(asItem);
  // add a empty container
  GiveItemFromFile("ChemicalContainerEmpty", "chemical_container.ent");
  // play acid burn sound
  PlaySoundAtEntity("burnlock", "puzzle_acid", "Cellar_Padlock", 1.0f, false);
  // change the padlocks with fade out/fade in
  SetPropActiveAndFade("Cellar_Padlock", false, 2.0f);
  SetPropActiveAndFade("Cellar_Padlock_Broken", true, 1.0f);
  // disable the padlock collisions area
  SetEntityActive("PadlockArea", false);
}
//-----------------------------------------------------------------------------
//--- prevent CANDLES from shining trough walls ---
//-----------------------------------------------------------------------------
void CandleSmall_Off(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Room1: small candle OFF", false);
  SetLampLit("Room1_CandleSmall", false, false);
}
void CandleSmall_On(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Room1: small candle ON", false);
  SetLampLit("Room1_CandleSmall", true, false);
}
//---
void CandleBig_Off(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Room1: big candle OFF", false);
  SetLampLit("Room1_CandleBig", false, false);
  SetLampLit("Room1_Candle_3", false, false);
  SetLampLit("Room1_Candle_4", false, false);
  // Room 2
  SetLampLit("Room2_Candle_1", true, false);
  SetLampLit("Room2_Candle_4", true, false);
  SetLampLit("Room2_Candle_5", true, false);
  SetLampLit("Room2_Candle_9", true, false);
  SetLampLit("Room2_Fire", true, false);
}
void CandleBig_On(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Room1: big candle ON", false);
  SetLampLit("Room1_CandleBig", true, false);
  SetLampLit("Room1_Candle_3", true, false);
  SetLampLit("Room1_Candle_4", true, false);
  // Room 2
  SetLampLit("Room2_Candle_1", false, false);
  SetLampLit("Room2_Candle_4", false, false);
  SetLampLit("Room2_Candle_5", false, false);
  SetLampLit("Room2_Candle_9", false, false);
  SetLampLit("Room2_Fire", false, false);
}
//---
void Room3Candles_Off(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Room3 Candles: OFF", false);
  SetLampLit("Room3_Fire", false, false);
}
void Room3Candles_On(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Room3 Candles: OFF", false);
  SetLampLit("Room3_Fire", true, false);
}
//---
void StairHallCandles_Off(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Stair Hall Candles: OFF", false);
  SetLampLit("StairHall_CandleTri_*", false, false);
  SetEntityActive("StairHall_SpotLight_*", false);
}
void StairHallCandles_On(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("Stair Hall Candles: ON", false);
  SetLampLit("StairHall_CandleTri_*", true, false);
  SetEntityActive("StairHall_SpotLight_*", true);
}
//-----------------------------------------------------------------------------
//--- Room2 --- move shelf (in front of hidden tunnel) ---
//-----------------------------------------------------------------------------
void CollideSecretBook(string &in asParent, string &in asChild, int alState)
{
  // Debug Message
  AddDebugMessage("moved book " + asParent, false);
  if (alState == 1 || alState == -1)
  {
    if (GetLocalVarInt("VarSecretMoves") != 4)
    {
      SetLocalVarInt("VarSecretMoves", GetLocalVarInt("VarSecretMoves")+1);
      SetLocalVarString("VarSecretCode", GetLocalVarString("VarSecretCode") + asParent + ",");
      // Debug Message
      AddDebugMessage("current code: " + GetLocalVarString("VarSecretCode"), false);
    }
    else
    {
      SetLocalVarString("VarSecretCode", GetLocalVarString("VarSecretCode") + asParent);
      // Code == 3,1,4,2,3
      if (GetLocalVarString("VarSecretCode") == "SecretBook_3,SecretBook_1,SecretBook_4,SecretBook_2,SecretBook_3")
      {
        // Debug Message
        AddDebugMessage("open shelf", false);
        // set var to "moving" to prevent "wrong" message
        // set var to "done" to use it later (check)
        SetLocalVarString("VarSecretCode", "moving+done");
        // deactivate player movement
        SetPlayerActive(false);
        // "locked" sound as success noise
        PlaySoundAtEntity("BooksDone", "lock_door", "Player", 0, false);
        // move secret books to default position
        SetPropObjectStuckState("SecretBook_*", -1);
        AddTimer("Room2_SecretBooks", 0.25f, "InteractTimer"); // 0.25 seconds
        // disbale interaction with secret books
        SetEntityInteractionDisabled("SecretBook_*", true);
        // let player look at the floor, where broom lies
        StartPlayerLookAt("Room2_SecretShelf", 2, 3, "");
        AddTimer("Room2_Shelf", 2, "InteractTimer"); // 3 seconds
        // roate the shelf (open)
        SetMoveObjectState("Room2_SecretShelf", 0.5f);
        // dust
        CreateParticleSystemAtEntity("dust", "ps_dust_falling_door_quick", "Room2_SecretParticleArea", false);
        AddTimer("DestroyParticle", 15, "InteractTimer"); // 15 seconds
        // puzzle secret - music
        PlayMusic("03_puzzle_secret.ogg", false, 0.7f, 0, 10, false);
      }
      else if (!StringContains(GetLocalVarString("VarSecretCode"), "moving"))
      {
        // set var to "moving" to prevent repeat of "wrong" message
        SetLocalVarString("VarSecretCode", "moving");
        // move secret books to default position
        AddTimer("SecretBooksMove", 1, "InteractTimer"); // 1 second
      }
    }
  }
}
//-----------------------------------------------------------------------------
//--- Room 2 - Painting collides with AreaPainting, when player picks it ---
//-----------------------------------------------------------------------------
void PuzzleSound(string &in asParent, string &in asChild, int alState)
{
  AddDebugMessage("Interact with " + asParent, false);
  // puzzle solved sound
  PlayMusic("02_puzzle.ogg", false, 0.7f, 0, 10, false);
}
//-----------------------------------------------------------------------------
//--- Storage Room - Statue collide with door ---
//-----------------------------------------------------------------------------
void SmashStatue(string &in asParent, string &in asChild, int alState)
{
  AddDebugMessage("Interact with StorageRoom_Door", false);
  // stop the door from moving, like hittin the statue
  AddPropImpulse("StorageRoom_Door", 0, 0, 2, "World");
  // shake the screen
  StartScreenShake(0.10f, 0.2f, 0.1f, 0.1f);
  // Sanity Damage
  GiveSanityDamage(35.0f, true);
  PlaySoundAtEntity("Impact", "15_body_impact.snt", "StorageRoom_ArmorFall", 0, false);
  AddTimer("StorageRoom_Statue", 1, "InteractTimer"); // 1 second
}
//-----------------------------------------------------------------------------
//--- Room 3 - bloody vision scare ---
//-----------------------------------------------------------------------------
void BeginVision(string &in asParent, string &in asChild, int alState)
{
  // deactivate player movement
  SetPlayerActive(false);
  // Sanity Damage
  GiveSanityDamage(35.0f, true);
  // disable lantern
  SetLanternActive(false, false);
  // puzzle secret - music
  PlayMusic("19_event_brute.ogg", false, 1.1f, 0, 10, false);
  // counter var - to decrease speed
  SetLocalVarInt("Counter", 0);
  // var to stop player
  SetLocalVarInt("Stop", 0);
  // let player look at the bed on the right
  StartPlayerLookAt("Room3_LookAt_1", 2, 3, "");
  // Timer for player movement - to bed
  AddTimer("1", 0.1f, "MovePlayerTimer"); // 0.1 seconds
}
//---
void MovePlayerTimer(string &in asTimer)
{
  if (asTimer == "1")
  {
    // move the player to the bed
    AddPlayerBodyForce(3150 - (GetLocalVarInt("Counter") * 10), 0, 1100, false);
    if (GetLocalVarInt("Stop") != 1)
    {
      // Timer for player movement - to bed
      AddTimer("1", 0.1f, "MovePlayerTimer"); // 0.1 seconds
      // increase counter var - to decrease speed
      SetLocalVarInt("Counter", GetLocalVarInt("Counter")+1);
    }
    else
    {
      // counter var - reset
      SetLocalVarInt("Counter", 0);
      // var to stop player - reset
      SetLocalVarInt("Stop", 0);
      // Timer for player movement - to cabinet
      AddTimer("look2", 1, "MovePlayerTimer"); // 1 second
    }
  }
  else if (asTimer == "look2")
  {
    // let player look at the bloody floor
    StartPlayerLookAt("Room3_LookAt_2", 2, 3, "");
    AddTimer("look3", 1, "MovePlayerTimer"); // 1 second
  }
  else if (asTimer == "look3")
  {
    // let player look at the bloody floor
    StartPlayerLookAt("Room3_LookAt_3", 2, 3, "");
    AddTimer("2", 1, "MovePlayerTimer"); // 1 second
  }
  else if (asTimer == "2")
  {
    // move the player from the bed to the cabinet
    AddPlayerBodyForce(-3150 + (GetLocalVarInt("Counter") * 10), 0, 900 - (GetLocalVarInt("Counter") * 10), false);
    if (GetLocalVarInt("Stop") != 1)
    {
      // Timer for player movement - to cabinet
      AddTimer("2", 0.1f, "MovePlayerTimer"); // 0.1 seconds
      // increase counter var - to decrease speed
      SetLocalVarInt("Counter", GetLocalVarInt("Counter")+1);
    }
    else
    {
      // let player look at the bloody cabinet
      StartPlayerLookAt("Room3_LookAt_end", 2, 3, "");
      // counter var - reset
      SetLocalVarInt("Counter", 0);
      // var to stop player - reset
      SetLocalVarInt("Stop", 0);
      // Timer for player movement - to cabinet
      AddTimer("3", 0.1f, "MovePlayerTimer"); // 0.1 seconds
    }
  }
  else if (asTimer == "3")
  {
    // push the player from the bed to the cabinet
    AddPlayerBodyForce(-1400 + (GetLocalVarInt("Counter") * 10), 0, 2600 - (GetLocalVarInt("Counter") * 10), false);
    if (GetLocalVarInt("Stop") != 1)
    {
      // Timer for player movement - to cabinet
      AddTimer("3", 0.1f, "MovePlayerTimer"); // 0.1 seconds
      // increase counter var - to decrease speed
      SetLocalVarInt("Counter", GetLocalVarInt("Counter")+1);
    }
    else
    {
      // counter var - reset
      SetLocalVarInt("Counter", 0);
      // Timer for open cabinet
      AddTimer("open cabinet", 1, "MovePlayerTimer"); // 1 second
      AddDebugMessage("open cabinet doors", false);
    }
  }
  else if (asTimer == "open cabinet")
  {
    // open the cabinet doors
    AddPropForce("Room3_Cabinet", 0, 0, -45,"World");
    if (GetLocalVarInt("Counter") != 15)
    {
      // Timer for player movement - to cabinet
      AddTimer("open cabinet", 0.1f, "MovePlayerTimer"); // 0.1 second
      // counter var
      SetLocalVarInt("Counter", GetLocalVarInt("Counter")+1);
    }
    else
    {
      // play 'breathing sound' as 'shocking noise'
      PlayGuiSound("react_breath4.ogg", 0.9f);
      // Sanity Damage
      GiveSanityDamage(10.0f, true);
      // Timer for smashing the door
      AddTimer("smash door", 3, "MovePlayerTimer"); // 3 seconds
    }
  }
  else if (asTimer == "smash door")
  {
    AddDebugMessage("smash door", false);
    // smash the door
    SetSwingDoorClosed("Room3_Door", true, false);
    // dust
    CreateParticleSystemAtEntity("dust_door", "ps_dust_impact_vert.ps", "Room3_AreaDust", false);
    // sound for door smashing
    PlayGuiSound("21_bang_door", 1.5f);
    // shake the screen
    StartScreenShake(0.25f, 0.1f, 0.25f, 0.1f);
    // Sanity Damage
    GiveSanityDamage(15.0f, true);
    // Timer for looking to the door
    AddTimer("look door", 0.7f, "MovePlayerTimer"); // 0.7 seconds
  }
  else if (asTimer == "look door")
  {
    // play 'breathing sound' as 'shocking noise'
    PlayGuiSound("react_breath4.ogg", 0.9f);
    // let player look at the bloody cabinet
    StartPlayerLookAt("Room3_Door", 4, 8, "");
    // Timer for activation of player movement
    AddTimer("end", 1, "MovePlayerTimer"); // 1 second
  }
  else if (asTimer == "end")
  {
    // play 'breathing sound' as 'shocking noise'
    PlayGuiSound("react_breath4.ogg", 0.9f);
    // activate player movement
    SetPlayerActive(true);
    StopPlayerLookAt();
    // Timer for activation of player movement
    AddTimer("quest", 1, "MovePlayerTimer"); // 1 second
  }
  else if (asTimer == "quest")
  {
    // find a way out of the mansion quest
    AddQuest("GetOutOfHere","GetOutOfHere");
  }
}
//-----------------------------------------------------------------------------
//--- Room 3 - bloody vision scare --- area collide callbacks ---
//-----------------------------------------------------------------------------
//--- movement - area collide - door to bed
void MoveLeft_1(string &in asParent, string &in asChild, int alState)
{
  // move the player to the left
  AddPlayerBodyForce(0, 0, -1500, false);
}
void MoveRight_1(string &in asParent, string &in asChild, int alState)
{
  // move the player to the right
  AddPlayerBodyForce(0, 0, 1500, false);
}
//--- movement - area collide - bed to cabinet 1
void MoveLeft_2(string &in asParent, string &in asChild, int alState)
{
  // move the player to the left
  AddPlayerBodyForce(0, 0, 1500, false);
}
void MoveRight_2(string &in asParent, string &in asChild, int alState)
{
  // move the player to the right
  AddPlayerBodyForce(0, 0, -1500, false);
}
//--- movement - area collide - bed to cabinet 2
void MoveLeft_3(string &in asParent, string &in asChild, int alState)
{
  // move the player to the left
  AddPlayerBodyForce(1500, 0, 0, false);
}
void MoveRight_3(string &in asParent, string &in asChild, int alState)
{
  // move the player to the right
  AddPlayerBodyForce(-1500, 0, 0, false);
}
//--- movement - area collide - stop movement
void StopMove_1(string &in asParent, string &in asChild, int alState)
{
  AddDebugMessage("stop movement", false);
  // var to stop player
  SetLocalVarInt("Stop", 1);
  // remove collide callbacks 1
  RemoveEntityCollideCallback("Player", "MoveL1");
  RemoveEntityCollideCallback("Player", "MoveR1");
  RemoveEntityCollideCallback("Player", "StopMove1");
}
void StopMove_2(string &in asParent, string &in asChild, int alState)
{
  AddDebugMessage("stop movement", false);
  // var to stop player
  SetLocalVarInt("Stop", 1);
  // remove collide callbacks 2
  RemoveEntityCollideCallback("Player", "MoveL2");
  RemoveEntityCollideCallback("Player", "MoveR2");
  RemoveEntityCollideCallback("Player", "StopMove2");
}
void StopMove_3(string &in asParent, string &in asChild, int alState)
{
  AddDebugMessage("stop movement", false);
  // var to stop player
  SetLocalVarInt("Stop", 1);
  // remove collide callbacks 3
  RemoveEntityCollideCallback("Player", "MoveL3");
  RemoveEntityCollideCallback("Player", "MoveR3");
  RemoveEntityCollideCallback("Player", "StopMove3");
}
//-----------------------------------------------------------------------------
//--- Room 4 - Table Area Scare ---
//-----------------------------------------------------------------------------
void BreakTables(string &in asParent, string &in asChild, int alState)
{
  // deactivate player movement
  SetPlayerActive(false);
  // sound for door smashing
  PlayGuiSound("21_bang_door", 0.7f);
  // let player look at the door
  StartPlayerLookAt("Room4_Door_2", 4, 8, "");
  // open the door
  SetSwingDoorDisableAutoClose("Room4_Door_2", true);
  SetSwingDoorClosed("Room4_Door_2", false, false);
  SetSwingDoorLocked("Room4_Door_2", false, false);
  // force the door to open
  AddPropImpulse("Room4_Door_2", -45, 0, 0, "World");
  // dust
  CreateParticleSystemAtEntity("dust_door", "ps_dust_impact_vert.ps", "Room4_AreaDust", false);
  // Sanity Damage
  GiveSanityDamage(15.0f, true);
  // shake the screen
  StartScreenShake(0.25f, 0.1f, 0.25f, 0.1f);
  // player reaction
  PlaySoundAtEntity("react", "react_pant.snt", "Player", 0, false);
  // disable lantern
  SetLanternActive(false, false);
  // counter var
  SetLocalVarInt("Counter", 0);
  // timer to break and move objects
  AddTimer("push", 0.5f, "BreakTimer"); // 0.5 seconds
}
//---
void BreakTimer(string &in asTimer)
{
  if (asTimer == "push")
  {
    // push the chairs away
    AddPropImpulse("Room4_Chair_L" + GetLocalVarInt("Counter"), 0, 0, -5, "World");
    AddPropImpulse("Room4_Chair_R" + GetLocalVarInt("Counter"), 0, 0,  5, "World");
    // push the plates away
    AddPropImpulse("Room4_PlateL_" + GetLocalVarInt("Counter"), 0, 0, -5, "World");
    AddPropImpulse("Room4_PlateR_" + GetLocalVarInt("Counter"), 0, 0,  5, "World");
    // push the silver away
    AddPropImpulse("Room4_SilverL_" + GetLocalVarInt("Counter"), 0, 0, -5, "World");
    AddPropImpulse("Room4_SilverR_" + GetLocalVarInt("Counter"), 0, 0,  5, "World");
    // disable lights
    if (GetLocalVarInt("Counter") ==  2)
    {
      SetLampLit("Room4_Candle_1", false, false);
    }
    else if (GetLocalVarInt("Counter") ==  5)
    {
      SetLampLit("Room4_Candle_2", false, false);
    }
    else if (GetLocalVarInt("Counter") ==  8)
    {
      SetLampLit("Room4_Candle_3", false, false);
    }
    else if (GetLocalVarInt("Counter") ==  11)
    {
      SetLampLit("Room4_Candle_4", false, false);
    }
    else if (GetLocalVarInt("Counter") ==  14)
    {
      SetLampLit("Room4_Candle_5", false, false);
    }
    if (GetLocalVarInt("Counter") != 16)
    {
      // increase counter var
      SetLocalVarInt("Counter", GetLocalVarInt("Counter") + 1);
      // timer to break and move objects
      AddTimer("push", 0.2f, "BreakTimer"); // 0.4 seconds
    }
    else
    {
      SetPlayerSanity(0.0f);
      AddTimer("push_end", 0.2f, "BreakTimer"); // 0.4 seconds
    }
  }
  else if (asTimer == "push_end")
  {
    AddPropImpulse("Room4_Chair_End", -5, 0, 0, "World");
    // activate player movement
    SetPlayerActive(true);
    StopPlayerLookAt();
    AddTimer("quest", 12, "BreakTimer"); // 12 seconds
  }
  else if (asTimer == "quest")
  {
    AddQuest("Shadow","Shadow");
  }
}
//-----------------------------------------------------------------------------
//--- Hallway - Enable Grunts and disable all hallway lights ---
//-----------------------------------------------------------------------------
void EnableHallGrunts(string &in asParent, string &in asChild, int alState)
{
  // deactivate player movement
  SetPlayerActive(false);
  // let player look at the first grunt
  StartPlayerLookAt("AreaGrunt_1", 4, 8, "");
  // Sanity Damage
  GiveSanityDamage(5.0f, true);
  // disable lantern
  SetLanternActive(false, false);
  // counter var
  SetLocalVarInt("Counter", 0);
  // timer to turn the lights off
  AddTimer("lights off", 0.2f, "LightsOffTimer"); // 0.2 seconds
}
void LightsOffTimer(string &in asTimer)
{
  if (asTimer == "lights off")
  {
    // set candles off
    SetLampLit("HallM_Candle_" + GetLocalVarInt("Counter"), false, false);
    if (GetLocalVarInt("Counter") != 12)
    {
      // increase counter var
      SetLocalVarInt("Counter", GetLocalVarInt("Counter") + 1);
      // timer to turn the lights off
      AddTimer("lights off", 0.2f, "LightsOffTimer"); // 0.2 seconds
    }
    else
    {
      // let player look at the floor
      StartPlayerLookAt("AreaLookGrunt", 1, 2, "");
      // set all candles off
      SetLampLit("HallM_Candle_*", false, false);
      // player sanity '0'
      SetPlayerSanity(0.0f);
      // push the player in the middle of the hall
      AddPlayerBodyForce(0, 0, -25000, false);
      // timer to spawn the grunts
      AddTimer("spawn grunts", 2.5f, "LightsOffTimer"); // 2.5 seconds
      // counter var - "-1" for grunt hit door
      SetLocalVarInt("Counter", -1);
    }
  }
  else if (asTimer == "spawn grunts")
  {
    // sound for monster
    PlayGuiSound("insanity_monster_roar01.ogg", 1.5f);
    // enable grunt 1 and let it run to player
    SetEntityActive("servant_brute_1", true);
    ShowEnemyPlayerPosition("servant_brute_1");
    // let player look at the first grunt
    StartPlayerLookAt("servant_brute_1", 4, 8, "");
    // timer to look at the 2nd grunt
    AddTimer("look to grunt 2", 10.0f, "LightsOffTimer"); // 10 seconds
  }
  else if (asTimer == "look to grunt 2")
  {
    // sound for monster
    PlayGuiSound("insanity_monster_roar02.ogg", 1.5f);
    // enable grunt 2 and let it run to player
    SetEntityActive("servant_brute_2", true);
    ShowEnemyPlayerPosition("servant_brute_2");
    // let player look at the first grunt
    StartPlayerLookAt("servant_brute_2", 4, 8, "");
    // timer to make player active
    AddTimer("look door", 10.0f, "LightsOffTimer"); // 10 seconds
  }
  else if (asTimer == "look door")
  {
    // sound for monster
    PlayGuiSound("enabled01.ogg", 1.5f);
    // let player look at the first grunt
    StartPlayerLookAt("Room5_AreaDust", 4, 8, "");
    // timer to hit the door
    AddTimer("hit door", 2.0f, "LightsOffTimer"); // 2 seconds
    // timer to make player active
    AddTimer("player active", 3.0f, "LightsOffTimer"); // 3 seconds
  }
  else if (asTimer == "hit door")
  {
    if (GetLocalVarInt("Counter") == -1)
    {
      // push the door
      AddPropImpulse("Room5_Door", -25, 0, 0, "World");
      // sound for door hit
      PlayGuiSound("hit_wood1.ogg", 1.5f);
      // dust
      CreateParticleSystemAtEntity("dust_door", "ps_dust_impact_vert.ps", "Room5_AreaDust", false);
      // timer to hit the door
      AddTimer("hit door", RandInt(1, 4), "LightsOffTimer"); // 2 seconds
    }
    SetLocalVarInt("random", RandInt(1, 20));
    if (GetLocalVarInt("random") == 5)
    {
      // sound for monster
      PlayGuiSound("enabled01.ogg", 1.5f);
    }
    else if (GetLocalVarInt("random") == 10)
    {
      // sound for monster
      PlayGuiSound("enabled02.ogg", 1.5f);
    }
    else if (GetLocalVarInt("random") == 15)
    {
      // sound for monster
      PlayGuiSound("enabled04.ogg", 1.5f);
    }
  }
  else if (asTimer == "player active")
  {
    // activate player movement
    SetPlayerActive(true);
    StopPlayerLookAt();
    // timer to play grunt attack music
    AddTimer("attack music", 1.0f, "LightsOffTimer"); // 1 seconds
  }
  else if (asTimer == "attack music")
  {
    // ambient music 'guardian attack'
    PlayMusic("att_guardian", true, 1.0f, 5, 0, true);
    // timer to play normal ambient music
    AddTimer("ambient music", 38.0f, "LightsOffTimer"); // 38 seconds
  }
  else if (asTimer == "ambient music")
  {
    // ambient music 'normal'
    PlayMusic("10_amb", true, 0.5f, 5, 0, true);
    // counter var reset (stop the grunt hitting the door)
    SetLocalVarInt("Counter", 0);
  }
}
//-----------------------------------------------------------------------------
//--- Player died ---
//-----------------------------------------------------------------------------
void PlayerDied(string &in asName, int alCount)
{
  StopMusic(1.0f, 1);
  // ambient music 'normal'
  PlayMusic("10_amb", true, 0.5f, 5, 0, true);
  if (asName == "Room1_Checkpoint")
  {
    // Room1 lights ON / Room2 lights OFF
    CandleSmall_On("", "", 1);
    CandleBig_On("", "", 1);
  }
  else if (asName == "Room2_Checkpoint")
  {
    // Room1 lights OFF / Room2 lights ON
    CandleSmall_Off("", "", 1);
    CandleBig_Off("", "", 1);
  }
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//--- ROOM EXIT --- PLAY CREDITS ---
//-----------------------------------------------------------------------------
void Credits(string &in asParent, string &in asChild, int alState)
{
  StartCredits("credits.ogg", false, "Ending", "MyCredits", -1);
}
//-----------------------------------------------------------------------------