////////////////////////////
// Run when entering map
void OnStart()
{
	SetLocalVarInt("AlchemyFinished", 0);
	SetLocalVarInt("IngredientPlaced", 0);
	SetLocalVarInt("SteamFixed", 3);
	SetLocalVarInt("ContainerPresent", 0);
	SetLocalVarInt("ReagentUsed", 0);
	AddUseItemCallback("", "bucket_of_tar_cold", "wood_beam_1", "HeatingTar", true);
	SetEntityConnectionStateChangeCallback("extaction_oven_lever_1", "ExtractionFakeVitae");
	AddUseItemCallback("", "GlassJarBlood", "ScriptArea_1", "AlchemyIngredient", true);
	AddUseItemCallback("", "chemical_container_1", "ScriptArea_2", "ContainerPrepare", true);
	AddUseItemCallback("", "Reagent", "ScriptArea_1", "ReagentAdd", false);

}
void OnEnter()
{
	PlayMusic("25_amb.ogg", true, 0.5, 5, 1, false);
}


void DeskLockedMessage(string &in asEntity) 

{
GetSwingDoorLocked("work_desk_door_1");
if(GetSwingDoorLocked("work_desk_door_1") == true)
{
SetMessage("Messages", "DeskDoor", 0);
}
}


void AlchemyIngredient(string &in asItem, string &in asEntity)
{
	SetEntityActive("glass_container_blood_static_3", true);
	PlaySoundAtEntity("", "pick_glass.snt", "ScriptArea_1", 0, false);
	RemoveItem("GlassJarBlood");
	AddLocalVarInt("IngredientPlaced", 1);
} 

void ReagentAdd(string &in asItem, string &in asEntity)
{
	if (GetLocalVarInt("IngredientPlaced") == 1)
	{
	SetEntityActive("glass_container_extracted_static_2", true);
	SetEntityActive("glass_container_blood_static_3", false);
	PlaySoundAtEntity("", "ui_use_oil.snt", "ScriptArea_1", 0, false);
	RemoveItem("Reagent");
	AddLocalVarInt("ReagentUsed", 1);
	}
} 

void ContainerPrepare(string &in asItem, string &in asEntity)
{
	SetEntityActive("chemical_container_static_1", true);
	PlaySoundAtEntity("", "impact_metal_med.snt", "ScriptArea_2", 0, false);
	RemoveItem("chemical_container_1");
	AddLocalVarInt("ContainerPresent", 1);
} 

void ExtractionFakeVitae(string &in asEntity, int alState) 
{
	if(alState != 1) return;
		if (GetSwingDoorState("extaction_oven_1")<1)
		{
			if (GetLocalVarInt("AlchemyFinished") == 0)
			{
				if (GetLocalVarInt("IngredientPlaced") == 1)
				{
					if (GetLocalVarInt("ReagentUsed") == 1)
					{
						if (GetLocalVarInt("ContainerPresent") == 1)
						{
							if (GetLocalVarInt("SteamFixed") == 3)
							{
								PlaySoundAtEntity("", "puzzle_acid.snt", "ScriptArea_2", 0, false);
								SetEntityActive("chemical_container_static_1", false);
								SetEntityActive("FakeVitae", true);
								AddLocalVarInt("AlchemyFinished", 1);
								SetEntityActive("ScriptArea_2", false);

							}
							else
							{
							SetMessage("Messages", "AlchemyFail3", 0);
							}
						}
						else
						{
						SetMessage("Messages", "AlchemyFail2", 0);
						}
					}
					else
					{
					SetMessage("Messages", "AlchemyFail4", 0);
					}
				}
				else
				{
				SetMessage("Messages", "AlchemyFail1", 0);
				}
			}
			else
			{
			
			}
		}
}
////////////////////////////
// Run when leaving map
void OnLeave()
{
	StopMusic(3, 1);

}