void OnStart()
{


AddEntityCollideCallback("crowbar_joint_1", "AreaBreakBoards_1", "BreakBoards", true, 1);
AddEntityCollideCallback("Player", "AreaBeginCavein_1", "BeginCavein", true, 1);
AddEntityCollideCallback("Player", "AreaCrowBarQuest_1", "AddCrowBarQuest", true, 1);
AddEntityCollideCallback("Player", "Jump_Death", "JumpDeath", false, 1);
AddEntityCollideCallback("Player", "AreaDamage_1", "CaveDamage", false, 0);
AddEntityCollideCallback("Player", "to_02", "to02", false, 1);

AddUseItemCallback("crowbar", "crowbar_1", "boards_1", "AttatchCrowbar", true);

		if(ScriptDebugOn()) //Lantern and Tinder for debug
	{	
		GiveItemFromFile("lantern", "lantern.ent");
		for(int i=0;i<10;i++) GiveItemFromFile("tinderbox_"+i, "tinderbox.ent");
		
	}

SetLocalVarInt("GiveDamage", 0);

}

void JumpDeath (string &in asParent, string &in asChild, int alState)
{
GivePlayerDamage(100.0, "BloodSplat", true, true);
}

/////////////////////////
/////CROWBAR PUZZLE//////

void AddCrowBarQuest(string &in asParent, string &in asChild, int alState)
{
AddQuest("crowbarquest", "CrowBarQuest");
}

void AttatchCrowbar(string &in asItem, string &in asEntity)
{
SetEntityActive("crowbar_joint_1", true);

SetPlayerActive(false);

StartPlayerLookAt("crowbar_joint_1", 2, 2, "");

PlaySoundAtEntity("", "player_crouch.snt", "crowbar_joint_1", 0, false);

AddTimer("", 1.0, "ResetPlayer");
AddTimer("", 1.5, "StopPlayerLook");

RemoveItem(asItem);
}

void BreakBoards(string &in asParent, string &in asChild, int alState)
{

SetEntityActive("block_box_1", false);  
SetEntityActive("block_box_2", false);
SetEntityActive("crowbar_broken_1", true);
SetEntityActive("crowbar_joint_1", false);

CompleteQuest("crowbarquest", "CrowBarQuest");

PlayMusic("12_puzzle_cavein.ogg", false, 1.0, 0, 1, true); //Optional music for completing puzzle, remove this line if desired

AddTimer("", 30.0, "ResetMusic"); //Reset the music

GiveSanityBoost(); //Sanity boost for completing puzzle, also optional

SetPropHealth("boards_1", 0.0);

}

/////CROWBAR PUZZLE//////
///////////END///////////


////////////////////////
/////////CAVE IN////////

void BeginCavein(string &in asParent, string &in asChild, int alState)
{
PlaySoundAtEntity("rumble", "general_rock_rumble.snt", "AreaRockRumble_1", 1.0, false);

StartScreenShake(0.08f, 5.0, 1.0, 1.0);



//BLOCK BOXES//

SetEntityActive("block_box_3", true);
SetEntityActive("block_box_4", true);
SetEntityActive("block_box_5", true);
SetEntityActive("block_box_6", true);
SetEntityActive("block_box_7", true);
SetEntityActive("block_box_8", true);
SetEntityActive("block_box_9", true);
SetEntityActive("block_box_10", true);  //Block boxes to make sure the player doesn't run behind the cavein area
SetEntityActive("block_box_11", true);
SetEntityActive("block_box_12", true);
SetEntityActive("block_box_13", true);
SetEntityActive("block_box_14", true);
SetEntityActive("block_box_15", true);
SetEntityActive("block_box_16", true);
SetEntityActive("block_box_17", true);
SetEntityActive("block_box_18", true);

//BLOCK BOXES//

AddTimer("cavein", 3.0, "CaveIn");
}

void CaveIn(string &in asTimer)
{

  if(GetLocalVarInt("GiveDamage") == 1) {
  GivePlayerDamage(25.0f, "BloodSplat", true, false);  //Hurt player if stands too close to cavein
  
  AddDebugMessage("Hurt Player", false);
  }


GiveSanityDamage(10.0, true);

SetEntityActive("cave_in_1", true);
SetEntityActive("rock_1", true);
SetEntityActive("rock_2", true);
SetEntityActive("rock_3", true);
SetEntityActive("torch_1", false);

CreateParticleSystemAtEntity("", "ps_break_mansionbase_wall.ps", "AreaExplosion_1", false);
CreateParticleSystemAtEntity("", "ps_break_mansionbase_wall.ps", "AreaExplosion_2", false);
CreateParticleSystemAtEntity("", "ps_break_mansionbase_wall.ps", "AreaExplosion_3", false);

PlaySoundAtEntity("", "explosion_rock_large.snt", "AreaBoom_1", 0, false);
PlaySoundAtEntity("", "react_breath.snt", "Player", 0.4f, false);

AddTimer("", 2.5, "StopSounds");

}

void CaveDamage(string &in asParent, string &in asChild, int alState)
{
   if(alState == 1) {
   SetLocalVarInt("GiveDamage", 1);
   
   AddDebugMessage("Added Damage", false);
   }
   
   else if(alState == -1) {
   SetLocalVarInt("GiveDamage", 0);
   
   AddDebugMessage("Removed Damage", false);
   }
  
   
   
}


/////////CAVE IN/////////
///////////END//////////  


void StopPlayerLook(string &in asTimer)
{
StopPlayerLookAt();
}

void ResetPlayer(string &in asTimer)
{
SetPlayerActive(true);
}

void ResetMusic(string &in asTimer)
{

PlayMusic("06_amb.ogg", true, 0.9, 1.0, 1, false);

}

void StopSounds(string &in asTimer)
{
StopSound("rumble", 1.0);
}

void OnEnter()
{

PlayMusic("06_amb.ogg", true, 0.9, 1.0, 1, true);
 
}


void to02(string &in asParent, string &in asChild, int alState)
{
ChangeMap("02_aztec.map", "from_01", "", ""); 
} 
 
 
////////////////////////////
// Run when leaving map
void OnLeave()
{
SetupLoadScreen("LoadingText", "aztec2", 0, "screen1.jpg");
}