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

Tweaks.daysoff = false;
Tweaks.citybusinessesownable = false;
People.subwaythreshold = 5;

-------------------------------------------------------------------------------------------------
---- Local variables.
-------------------------------------------------------------------------------------------------
l_overdraft=-750;	-- Cash limit for the player; they cannot spend beyond this amount
l_targetvalue = 500;
l_gameending = false;
l_donecashwarning=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()
	Game.Pause();
	-- 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 500 sales in any one day. You have 12 days in which to do it.");
	C_TriggerEvent("LevelAboutToFail", GAME_DAY*11);
	C_TriggerEvent("LevelFailed", GAME_DAY*12);
--	C_TriggerEvent("IntroduceBusiness", GAME_DAY, 1);
	Game.Unpause();
end;

-------------------------------------------------------------------------------------------------
-- LevelAboutToFail. Called one day before time limit, to give player a warning
-------------------------------------------------------------------------------------------------
function EVENT_LevelAboutToFail()
	Interface.CreatePrompt("Level 2", "about to fail mission");
end;

-------------------------------------------------------------------------------------------------
-- LevelFailed. Called when the time limit for the level has expired
-------------------------------------------------------------------------------------------------
function EVENT_LevelFailed()
	Interface.CreatePrompt("Level 2 Failed", "Muppet", 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("Level 2 Completed", "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("Level 2 Over", "Unfortunately, Player "..a.." has won the game", l_failurecode);
	end;
end;


-------------------------------------------------------------------------------------------------
-- LowOnCash. Called the first time a player spends too much money
-------------------------------------------------------------------------------------------------
function EVENT_LowOnCash()
	Interface.CreatePrompt("Low on cash!", "Your bank balance doesn't look very healthy! You will lose this level if you dip below $"..l_overdraft.."!" );
	l_donecashwarning=TRUE;
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_DAY*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
	-- TODO :: need to check bankruptcy against GetWorth, instead of GetMoney.
	if (Player.GetMoney() <= 100) and (Player.GetMoney() >= l_overdraft) then
		if l_donecashwarning==false then
			EVENT_LowOnCash();
		end;
	end;

	if (Player.GetMoney() < l_overdraft) and (l_gameending==false) then
		Interface.CreatePrompt("Level 2 Failed", "Oh dear, oh dear. What happened here? You're bankrupt! You are going to have to be more careful with your money next time. Still, you can always try again - it's only Monopoly money after all. Thank goodness!", l_failurecode);
		l_gameending=true;
		return 0;
	end;	

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

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

		


