-------------------------------------------------------------------------------------------------
---- Tweak Overrides
-------------------------------------------------------------------------------------------------

Tweaks.daysoff = false;
Tweaks.citybusinessesownable = false;

-------------------------------------------------------------------------------------------------
---- Local variables.
-------------------------------------------------------------------------------------------------
l_targetvalue = 400;
l_gameending = false;

l_intheredcheck = false;

-- these two numbers are passed through to the prompt box
-- so that we know what to do after the box has been dismissed.
l_failurecode = 998;
l_successcode = 999;

-------------------------------------------------------------------------------------------------
-- INITIALISE_Level. Standard level event
-------------------------------------------------------------------------------------------------
function EVENTINITIALISE_Level()
	-- load the building and business file
	C_RunScript("scripts\\level3\\initialpref.lua");
	C_RunScript("scripts\\level3\\map.lua");
end;

-------------------------------------------------------------------------------------------------
-- Level. Standard level event.
-------------------------------------------------------------------------------------------------
function EVENT_Level()
	-- create an initial prompt
	SetCamPos(72.2,758,-146.4,24.5,0.29,88.9);
	Camera.posinertia = FALSE;
	Camera.targinertia = FALSE;
	Camera.inertiavalue = 1;
	Interface.CreatePrompt("Level 3", "Your mission is to be the first player to make "..l_targetvalue.." sales in any one day. You have 12 days in which to complete it.");
	C_TriggerEvent("LevelAboutToFail", GAME_YEAR*11);
	C_TriggerEvent("LevelFailed", GAME_YEAR*12);
--	C_TriggerEvent("IntroduceBusiness", GAME_YEAR, 1);
end;

-------------------------------------------------------------------------------------------------
-- LevelAboutToFail. Called one day before time limit, to give player a warning
-------------------------------------------------------------------------------------------------
function EVENT_LevelAboutToFail()
	Interface.CreatePrompt("Everything Must Go", "If I'm not mistaken, it looks as if you are about to fail this level");
end;

-------------------------------------------------------------------------------------------------
-- LevelFailed. Called when the time limit for the level has expired
-------------------------------------------------------------------------------------------------
function EVENT_LevelFailed()
	Interface.CreatePrompt("Everything Must Go", "Sorry, you have failed this level", l_failurecode);
end;

-------------------------------------------------------------------------------------------------
-- LevelWon. Called when the player has achieved his objectives. Triggered from HourlyUpdate
-------------------------------------------------------------------------------------------------
function EVENT_LevelWon(a)
	if a == 0 then
		Interface.CreatePrompt("Everything Must Go", "Well done! You have won!!! You should move on to the next level now. You should find it more of a challenge than this one.", l_successcode);
	else 
		Interface.CreatePrompt("Everything Has Gone!", "Unfortunately, Player "..a.." has won the game", l_failurecode);
	end;
end;



-------------------------------------------------------------------------------------------------
-- IntroduceBusiness. Called every 2 days to introduce a new business.
-------------------------------------------------------------------------------------------------
function EVENT_IntroduceBusiness(a)
	if a == 1 then
		BusinessInfo[FURNITURE_STORE].available = true;
		Interface.CreatePrompt("New Business Available", "You are now able to create a Furniture Store! Check the Business Browser for information.");
	elseif a == 2 then
		BusinessInfo[RESTAURANT].available = true;
		Interface.CreatePrompt("New Business Available", "You are now able to create a Restaurant! Check the Business Browser for information.");
	elseif a == 3 then
		BusinessInfo[THEATRE].available = true;
		Interface.CreatePrompt("New Business Available", "You are now able to create a Theatre! Check the Business Browser for information.");
	end;

	C_TriggerEvent("IntroduceBusiness", GAME_YEAR*2, a+1);
end;

-------------------------------------------------------------------------------------------------
-- GetTotalSales. Goes through all of player1's businesses, and adds up the total sales
-------------------------------------------------------------------------------------------------
function GetTotalSales()
	sales = 0;
	local f = function(i,v)
		sales = sales + v:GetTotalSales();
	end;
	C_Iterate(g_playerbusinesses, f);
	return sales;
end;

--------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------  Catch game events  ------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------
-- InterfacePromptDismiss. Called everytime the player closes the prompt box.
-------------------------------------------------------------------------------------------------
function CATCHEVENT_InterfacePromptDismiss(a)
	if a == l_failurecode then
		Game.End(false);
	elseif a == l_successcode then
		Game.End(true);
	end;
end;

-------------------------------------------------------------------------------------------------
-- GetTotalSales. Goes through all of player1's businesses, and adds up the total sales
-------------------------------------------------------------------------------------------------
function GetTotalSales(a)
	sales = 0;
	local f = function(i,v)
		sales = sales + v:GetTotalSales();
	end;
	C_Iterate(g_businesses[a], f);
	return sales;
end;

function TestSalesWinCondition()
	if  GetTotalSales(0) >= l_targetvalue then
		EVENT_LevelWon(0);
		l_gameending=true;
	end;

	if GetTotalSales(1) >= l_targetvalue then
		EVENT_LevelWon(1);
		l_gameending=true;
	end;

	if GetTotalSales(2) >= l_targetvalue then
		EVENT_LevelWon(2);
		l_gameending=true;
	end;
end;

-------------------------------------------------------------------------------------------------
-- HourlyUpdate event. Called everytime the game calendar ticks another hour over.
-------------------------------------------------------------------------------------------------
function CATCHEVENT_HourlyUpdate(a) -- takes the current hour as an argument

	-- level completion test
	if l_gameending==false then
		TestSalesWinCondition();
	end;

	if a == 2 then
		Interface.CreatePrompt("End of day summary", "Player 1: "..GetTotalSales(0).." sales today");
		Interface.CreatePrompt("", "Wheelbarrow: "..GetTotalSales(1).." sales today");
		Interface.CreatePrompt("", "Iron: "..GetTotalSales(2).." sales today");
	end;
end;

function CATCHEVENT_EndOfDay()
	if (Player.GetMoney() < 0)  then
		if	(l_intheredcheck == true) then
			EVENT_LevelFailed();
		else
			l_intheredcheck = true;
			Interface.CreatePrompt("Cash Balance Warning", "The day has ended, and your balance is negative. You have 24 hours to rectify this situation, otherwise the game will be over");
		end;
	else
		l_intheredcheck = false;
	end;
end;

function CATCHEVENT_PlayerIntoRed()
	Interface.CreatePrompt("Cash Balance Warning", "Your bank balance has just become negative. Be careful you don't spend too much.");
end;

-------------------------------------------------------------------------------------------------
-- HourlyUpdate event. Called everytime the game calendar ticks another hour over.
-------------------------------------------------------------------------------------------------
function CATCHEVENT_HourlyUpdate(a) -- takes the current hour as an argument
	if People.NumberInCity() < 700 then
		if a == 14 then
--						  No		AGE				Class	Prefs				
			People.Create(2, 	AGE_ADULT, 			-1,		-1);
			People.Create(1, 	AGE_PENSIONER,		-1, 	-1);
			People.Create(1, 	AGE_CHILD, 			-1, 	-1);
			--People.Create(1, 	AGE_TEENAGER,		-1,		-1);
		end;
	end
end;
