--[[
Source code for Company
Copyright Nakano 2011.
License: No specific license. Code may be used for non-commercial / non-profitable purposes.
                        
 ######## VERSION HISTORY ##########
 # v0.1 05/21/2011 by Nakano       #
 # * initial release for           #
 #   PSP Genesis Competition 2011  #
 # v1.0 07/22/2011 by Nakano       #
 # * Release for NEO Compo 2011    #
 ###################################
 --]]

storySelector = image.load("images/story/storySelector.png");
passed = image.load("images/story/passed.png"); -- 2
approachable = image.load("images/story/approachable.png"); -- 1
unavailable = image.load("images/story/unavailable.png"); -- 0
video = image.load("images/story/video.png");

currentStage = 1; -- stage number that is played used by main program too
stageSelection = 1
storySelectorX = 15;
storySelectorY = 115;

story = {{1, " "}, {0, "$0"}, {0, "$0"}, {0, " "}, {0, "$0"}, {0, "$0"}, {0, "$0"}, {0, "$0"}, {0, " "}, {0, " "}, {0, "$0"}} -- how well passed levels

newHighscore = function(moneyResult)
	draw.fillrect(25,95,431,100,lime)
	draw.fillrect(30,100,421,90,black)
	screen.print(240, 150 - yDifference, "New Highscore of " .. moneyResult, 1.0, lime, shadowColor, "center");
			
	screen.flip()
	screen.waitvblankstart(250);
end

getStoryCardList = function(iniFileName)
	local file = io.open(iniFileName, "r");
	local cardList = {};
	
	if (file == nil ) then
		-- ini file was not found, quit
		screen.print(240, 150 - yDifference, "Card list file ( " .. iniFileName .. " ) not found.", 0.8, blue, shadowColor, "center");
		screen.flip()
		screen.waitvblankstart(150);
		os.quit();
	end
	
	for line in file:lines() do

		local item = line:splitCards("[^;]+");
		local numberOfSameCard = tonumber(item[2]);
		
		if (numberOfSameCard < 0) then
			numberOfSameCard = 0;
		elseif (numberOfSameCard > 40) then
				numberOfSameCard = 40;
		end
		
		cardList[#cardList + 1] = numberOfSameCard;		
	end
	
	file:close()
	return cardList;
end

storyCardList = function(iniFileName, allowMultipleThreats, requiredIncome)
	
	local selectorY = 0;
	local selectorX = 0;
	local selection = 1;
	local waitTriangle = false;
	local cardNumbers = getStoryCardList(iniFileName);
	
	while true do
		
		image.blit(0, 0, cardList);
		image.blit(2 + selectorX, 52 + selectorY, selector3);
		
		screen.print(20, 64 - yDifference, "Open Company", 0.6, red, shadowColor, "left");
		screen.print(20, 74 - yDifference, "$15000", 0.6, red, shadowColor, "left");
		screen.print(20, 84 - yDifference, "$25000", 0.6, red, shadowColor, "left");
		screen.print(20, 94 - yDifference, "$50000", 0.6, red, shadowColor, "left");
		screen.print(20, 104 - yDifference, "$100000", 0.6, red, shadowColor, "left");
		screen.print(20, 114 - yDifference, "Sell Company", 0.6, red, shadowColor, "left");
		screen.print(20, 124 - yDifference, "Thief", 0.6, red, shadowColor, "left");
		screen.print(20, 134 - yDifference, "Small Safe", 0.6, red, shadowColor, "left");
		screen.print(20, 144 - yDifference, "Big Safe", 0.6, red, shadowColor, "left");
		screen.print(20, 154 - yDifference, "Conviction", 0.6, red, shadowColor, "left");
		screen.print(20, 164 - yDifference, "Remove Conviction", 0.6, red, shadowColor, "left");
		screen.print(20, 174 - yDifference, "Tax Evasion", 0.6, red, shadowColor, "left");
		screen.print(20, 184 - yDifference, "Remove Tax Evasion", 0.6, red, shadowColor, "left");
		screen.print(20, 194 - yDifference, "Strike", 0.6, red, shadowColor, "left");
		
		screen.print(150, 64 - yDifference, "< " .. cardNumbers[1] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 74 - yDifference, "< " .. cardNumbers[2] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 84 - yDifference, "< " .. cardNumbers[3] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 94 - yDifference, "< " .. cardNumbers[4] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 104 - yDifference, "< " .. cardNumbers[5] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 114 - yDifference, "< " .. cardNumbers[6] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 124 - yDifference, "< " .. cardNumbers[7] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 134 - yDifference, "< " .. cardNumbers[8] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 144 - yDifference, "< " .. cardNumbers[9] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 154 - yDifference, "< " .. cardNumbers[10] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 164 - yDifference, "< " .. cardNumbers[11] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 174 - yDifference, "< " .. cardNumbers[12] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 184 - yDifference, "< " .. cardNumbers[13] .. " >", 0.6, white, shadowColor, "left");
		screen.print(150, 194 - yDifference, "< " .. cardNumbers[14] .. " >", 0.6, white, shadowColor, "left");
		
		screen.print(205, 64 - yDifference, "Remove Strike", 0.6, red, shadowColor, "left");
		screen.print(205, 74 - yDifference, "Eco Crime", 0.6, red, shadowColor, "left");
		screen.print(205, 84 - yDifference, "Remove Eco Crime", 0.6, red, shadowColor, "left");
		screen.print(205, 94 - yDifference, "Bribe", 0.6, red, shadowColor, "left");
		screen.print(205, 104 - yDifference, "Bankruptcy", 0.6, red, shadowColor, "left");
		screen.print(205, 114 - yDifference, "Deal", 0.6, red, shadowColor, "left");
		screen.print(205, 124 - yDifference, "New Beginning", 0.6, red, shadowColor, "left");
		screen.print(205, 134 - yDifference, "Robbery", 0.6, red, shadowColor, "left");
		screen.print(205, 144 - yDifference, "Luck", 0.6, red, shadowColor, "left");
		screen.print(205, 154 - yDifference, "Banker", 0.6, red, shadowColor, "left");
		screen.print(205, 164 - yDifference, "Rotate Table", 0.6, red, shadowColor, "left");
		screen.print(205, 174 - yDifference, "Rotate Hands", 0.6, red, shadowColor, "left");
		screen.print(205, 184 - yDifference, "New Identity", 0.6, red, shadowColor, "left");
		screen.print(205, 194 - yDifference, "Sabotage", 0.6, red, shadowColor, "left");
		
		screen.print(335, 64 - yDifference, "< " .. cardNumbers[15] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 74 - yDifference, "< " .. cardNumbers[16] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 84 - yDifference, "< " .. cardNumbers[17] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 94 - yDifference, "< " .. cardNumbers[18] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 104 - yDifference, "< " .. cardNumbers[19] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 114 - yDifference, "< " .. cardNumbers[20] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 124 - yDifference, "< " .. cardNumbers[21] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 134 - yDifference, "< " .. cardNumbers[22] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 144 - yDifference, "< " .. cardNumbers[23] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 154 - yDifference, "< " .. cardNumbers[24] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 164 - yDifference, "< " .. cardNumbers[25] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 174 - yDifference, "< " .. cardNumbers[26] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 184 - yDifference, "< " .. cardNumbers[27] .. " >", 0.6, white, shadowColor, "left");
		screen.print(335, 194 - yDifference, "< " .. cardNumbers[28] .. " >", 0.6, white, shadowColor, "left");

		screen.print(20, 220 - yDifference, "Press Cross to Begin Stage | Triangle for Card Details", 0.8, blue, shadowColor, "left");
		screen.print(20, 240 - yDifference, "Rules: Allow Multiple Threats < " .. allowMultipleThreats .. " > |", 0.8, white, shadowColor, "left");
		screen.print(20, 260 - yDifference, "Required income for playing \"Sell Company\" " .. requiredIncome, 0.8, white, shadowColor, "left");
		
		if (waitTriangle) then
			local cardName = getCardNameFromCardList(selection);
			checkCurrentCard(cardName, "big");
			
			if (selection > 14) then
				image.blit(5, 3, currentCard);
			else
				image.blit(300, 3, currentCard);
			end
		end
		
		controls.read()

		if controls.press("r") then
			changeStyle(1);
		elseif controls.press("l") then
			changeStyle(-1);
		elseif controls.press("select") then
			returnMenu();
		elseif controls.press("cross") then
			return
		elseif controls.press("triangle") then
			if (waitTriangle) then
				waitTriangle = false;
			else
				waitTriangle = true;
			end
			
		elseif controls.press("down") or controls.analogy() == 127 + waitAnalog then
			waitAnalog = 0.2
			waitMoment = os.clock()
			selection = selection + 1;
			selectorY = selectorY + 10;
			
			if (selection > 28) then
				selection = 1;
				selectorY = 0;
				selectorX = 0;
			end
			
			if (selection == 15) then
				selectorY = 0;
			end
			
			if (selection > 14) then
				selectorX = 188;
			else
				selectorX = 0;
			end
			
		elseif controls.press("up") or controls.analogy() == -128 - waitAnalog then
			waitAnalog = 0.2
			waitMoment = os.clock()
			selection = selection - 1;
			selectorY = selectorY - 10;
			
			if (selection < 1) then
				selection = 28;
				selectorY = 130;
				selectorX = 188
			end
			
			if (selection == 14) then
				selectorY = 130;
			end
			
			if (selection > 14) then
				selectorX = 188;
			else
				selectorX = 0;
			end
		end
		screen.flip()
		if (os.clock() - waitMoment >= waitAnalog) then
			waitAnalog = 0;
		end
	end
		
end

saveStory = function()
	local file = io.open("story.ini","w")
				
	file:write(story[1][1] .. ";" .. story[1][2] .. "\n");
	file:write(story[2][1] .. ";" .. story[2][2] .. "\n");
	file:write(story[3][1] .. ";" .. story[3][2] .. "\n");
	file:write(story[4][1] .. ";" .. story[4][2] .. "\n");
	file:write(story[5][1] .. ";" .. story[5][2] .. "\n");
	file:write(story[6][1] .. ";" .. story[6][2] .. "\n");
	file:write(story[7][1] .. ";" .. story[7][2] .. "\n");
	file:write(story[8][1] .. ";" .. story[8][2] .. "\n");
	file:write(story[9][1] .. ";" .. story[9][2] .. "\n");
	file:write(story[10][1] .. ";" .. story[10][2] .. "\n");
	file:write(story[11][1] .. ";" .. story[11][2] .. "\n");
	
	file:close();
end

readStory = function()
	local file = io.open("story.ini", "r");

	if not file then
		-- file does not exist, default story settings are ok.
		return;
	end
	
	local line = ""
	story = {}
	
	for line in file:lines() do
		local currentEntry = line:splitOptions("[^;]+")
		currentEntry = {tonumber(currentEntry[1]), currentEntry[2]}
		table.insert(story, currentEntry)
	end
	
	file:close()
end

showStory = function()

	readStory()
	collectgarbage()

	local denyScreenFlip = false;
	local stageText = ""
	local highscore = "";

	while true do
	
		stageText = "";
		local highscore = "";
		
		if (stageSelection == 1 and story[1][1] ~= 0) then
			stageText = "Intro"
		elseif (stageSelection == 2 and story[2][1] ~= 0) then
			stageText = "Financing"
			highscore = "(" .. story[2][2] .. ")"
		elseif (stageSelection == 3 and story[3][1] ~= 0) then
			stageText = "Competitors"
			highscore = "(" .. story[3][2] .. ")"
		elseif (stageSelection == 4 and story[4][1] ~= 0) then
			stageText = "Encounter"
		elseif (stageSelection == 5 and story[5][1] ~= 0) then
			stageText = "Lily"
			highscore = "(" .. story[5][2] .. ")"
		elseif (stageSelection == 6 and story[6][1] ~= 0) then
			stageText = "Arianne"
			highscore = "(" .. story[6][2] .. ")"
		elseif (stageSelection == 7 and story[7][1] ~= 0) then
			stageText = "Competitors 2"
			highscore = "(" .. story[7][2] .. ")"
		elseif (stageSelection == 8 and story[8][1] ~= 0) then
			stageText = "Ex-Boss"
			highscore = "(" .. story[8][2] .. ")"
		elseif (stageSelection == 9 and story[9][1] ~= 0) then
			stageText = "Lily Ending"
		elseif (stageSelection == 10 and story[10][1] ~= 0) then
			stageText = "Arianne Ending"
		elseif (stageSelection == 11 and story[11][1] ~= 0) then
			stageText = "Silly Bonus Stage"
			highscore = "(" .. story[11][2] .. ")"
		end
		
		image.blit(0, 0, storyMenu)
		image.blit(10, 130, video)
		
		--screen.print(10, 20, "RAM Memory (%): " .. ( ( os.getfreememory() * 100 ) / os.getinitmemory() ), blue )
		
		if (story[2][1] == 0) then
			image.blit(70, 130, unavailable)
		elseif (story[2][1] == 1) then
			image.blit(70, 130, approachable)
		elseif (story[2][1] == 2) then
			image.blit(70, 130, passed)
		end
		
		if (story[3][1] == 0) then
			image.blit(130, 130, unavailable)
		elseif (story[3][1] == 1) then
			image.blit(130, 130, approachable)
		elseif (story[3][1] == 2) then
			image.blit(130, 130, passed)
		end
		
		if (story[4][1] == 0) then
			image.blit(190, 130, unavailable) -- video
		elseif (story[4][1] == 1) then
			image.blit(190, 130, video)
		end
		
		if (story[5][1] == 0) then
			image.blit(250, 100, unavailable)
		elseif (story[5][1] == 1) then
			image.blit(250, 100, approachable)
		elseif (story[5][1] == 2) then
			image.blit(250, 100, passed)
		end
		
		if (story[6][1] == 0) then
			image.blit(250, 160, unavailable)
		elseif (story[6][1] == 1) then
			image.blit(250, 160, approachable)
		elseif (story[6][1] == 2) then
			image.blit(250, 160, passed)
		end
		
		if (story[7][1] == 0) then
			image.blit(310, 130, unavailable)
		elseif (story[7][1] == 1) then
			image.blit(310, 130, approachable)
		elseif (story[7][1] == 2) then
			image.blit(310, 130, passed)
		end
		
		if (story[8][1] == 0) then
			image.blit(370, 130, unavailable)
		elseif (story[8][1] == 1) then
			image.blit(370, 130, approachable)
		elseif (story[8][1] == 2) then
			image.blit(370, 130, passed)
		end
		
		if (story[9][1] == 1) then
			-- lily ending
			image.blit(430, 100, video)
		end
		
		if (story[10][1] == 1) then
			-- arianne ending
			image.blit(430, 130, video)
		end
		
		if (story[11][1] == 1) then
			-- silly bonus stage
			image.blit(430, 160, approachable)
		elseif (story[11][1] == 2) then
			image.blit(430, 160, passed)
		end
		
		image.blit(storySelectorX, storySelectorY, storySelector)
		screen.print(10, 80, stageText .. " " .. highscore)
		
		screen.print(25, 190, "Cross - Choose Stage", blue)
		screen.print(25, 210, "Circle - Skip intro (while stage is selected)", blue)
		screen.print(25, 230, "Select - Return to MAIN MENU", blue)
		
		image.blit(10, 250, video)
		screen.print(25, 260, "VIDEO", blue)
		
		image.blit(80, 250, unavailable)
		screen.print(95, 260, "UNAVAILABLE", blue)
		
		image.blit(210, 250, approachable)
		screen.print(225, 260, "AVAILABLE", blue)
		
		image.blit(320, 250, passed)
		screen.print(335, 260, "PASSED", blue)
		
		controls.read()
		
		if controls.press("cross") then
			if (stageSelection == 1 and story[1][1] ~= 0) then
				showStageIntro()
			elseif (stageSelection == 2 and story[2][1] ~= 0) then
				-- Financing
				showStage1()
			elseif (stageSelection == 3 and story[3][1] ~= 0) then
				-- Competitors
				showStage2()
			elseif (stageSelection == 4 and story[4][1] ~= 0) then
				-- Encounter
				showStageVideo()
			elseif (stageSelection == 5 and story[5][1] ~= 0) then
				-- Lily
				showStage3()
			elseif (stageSelection == 6 and story[6][1] ~= 0) then
				-- Arianne
				showStage4()
			elseif (stageSelection == 7 and story[7][1] ~= 0) then
				-- Competitors 2
				showStage5()
			elseif (stageSelection == 8 and story[8][1] ~= 0) then
				showStage6()
				-- Ex-Boss
			elseif (stageSelection == 9) then
				showEndingLily()
			elseif (stageSelection == 10) then
				showEndingArianne()
			elseif (stageSelection == 11) then
				showStage7()
			end
			
		elseif controls.press("select") then
			local choice = os.message("Return back to MAIN MENU?", 1)
			denyScreenFlip = true;
			if (choice == 1) then
				showMain(true)
			end
					
		elseif controls.press("left") or controls.analogx() == -128 - waitAnalog then
				waitAnalog = 0.2
				waitMoment = os.clock()
				stageSelection = stageSelection - 1;
				storySelectorX = storySelectorX - 60;
				storySelectorY = 115;
				
				if (stageSelection < 1) then
				
					if (story[11][1] ~= 0) then
						-- bonus stage
						stageSelection = 11;
						storySelectorX = 435
						storySelectorY = 145;
					elseif (story[10][1] ~= 0) then
						-- arianne
						stageSelection = 10;
						storySelectorX = 435
						storySelectorY = 115;
					elseif (story[9][1] ~= 0) then
						-- lily
						stageSelection = 9;
						storySelectorX = 435
						storySelectorY = 85;
					else
						stageSelection = 8
						storySelectorX = 375
					end
						
				elseif (stageSelection == 5) then
					storySelectorY = 85;
					storySelectorX = 255
				elseif (stageSelection == 6) then
					storySelectorY = 145;
					storySelectorX = 255
				elseif (stageSelection > 8) then
					
					if (stageSelection == 10 and story[10][1] ~= 0) then
						storySelectorX = 435
						storySelectorY = 115;
						stageSelection = 10
					elseif ((stageSelection == 10 or stageSelection == 9) and story[9][1] ~= 0) then
						storySelectorX = 435
						storySelectorY = 85;
						stageSelection = 9
					else
						stageSelection = 8
						storySelectorX = 375
					end
				end
		elseif controls.press("right") or controls.analogx() == 127 + waitAnalog then
				waitAnalog = 0.2
				waitMoment = os.clock()
				stageSelection = stageSelection + 1;
				storySelectorX = storySelectorX + 60;
				storySelectorY = 115;
				
				if (stageSelection > 8) then
				
					if (stageSelection == 9 and story[9][1] ~= 0) then
						storySelectorX = 435
						storySelectorY = 85;
						stageSelection = 9
					elseif ((stageSelection == 9 or stageSelection == 10) and story[10][1] ~= 0) then
						storySelectorX = 435
						storySelectorY = 115;
						stageSelection = 10
					elseif ((stageSelection == 9 or stageSelection == 10 or stageSelection == 11) and story[11][1] ~= 0) then
						storySelectorX = 435
						storySelectorY = 145;
						stageSelection = 11
					else
						stageSelection = 1;
						storySelectorX = 15
					end
				
				elseif (stageSelection == 5) then
					storySelectorY = 85;
					storySelectorX = 255
				elseif (stageSelection == 6) then
					storySelectorY = 145;
					storySelectorX = 255
				end
		end
		
		if (denyScreenFlip == false) then
			screen.flip();
		else
			denyScreenFlip = false;
		end
		
		if (os.clock() - waitMoment >= waitAnalog) then
			waitAnalog = 0;
		end
	end
end

showStageIntro = function()

	if (( ( os.getfreememory() * 100 ) / os.getinitmemory() ) < 14.8) then
		local memory = (os.getfreememory() * 100 ) / os.getinitmemory()
		draw.fillrect(25,95,431,100,red)
		draw.fillrect(30,100,421,90,black)
		screen.print(240, 130, "Error: Not enough free RAM Memory.\nRequired RAM is 15% current is " .. memory .. "%\n" .. "Restart game if you want to play intro.", 0.8, red, shadowColor, "center");
		screen.flip();
		screen.waitvblankstart(250)
		return;
	end

	local musicLove = "music/love.mp3"
	local playingGameMusic = true; -- assumably.
	
	local imageList = {}
	table.insert(imageList, image.load("images/story/intro/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/intro/butterfly.png"))
	table.insert(imageList, image.load("images/story/intro/myLovelyWife.png"))
	table.insert(imageList, image.load("images/story/intro/boat.png"))
	table.insert(imageList, image.load("images/story/intro/boss.png"))
	table.insert(imageList, image.load("images/story/intro/bubble.png"))
	table.insert(imageList, image.load("images/story/intro/love.png"))
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/intro/bubble2.png"))
	table.insert(imageList, image.load("images/story/intro/bubble3.png"))
	table.insert(imageList, image.load("images/story/intro/wife.png"))
	table.insert(imageList, image.load("images/style" .. curStyle .. "/cardOpenCompanySmall.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	local butterflyX = 1;
	local boatX = 0;
	local storySection = 1;
	
	local wait = 255;
	local colorRGB = 0;
	local scene = false;
	
	while true do
		
	if (storySection == 1) then
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		image.blit(5 + butterflyX, y - (rotate - 0.8), imageList[3]);
		image.blit(400 - butterflyX, y - (rotate - 0.8), imageList[3]);
	
		image.rotate(imageList[2], rotate)
		rotate = rotate + 1;
	
		if (y > 3) then
			butterflyX = butterflyX + 0.2;
		end
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
			image.blit(200, 120, imageList[4]);
		end
		
		if ((rotate % 5) == 0) then
			butterflyX = butterflyX * -1;
		end
		
		if (rotate > 20) then
			screen.print(x, y, "Let me introduce myself.\nI have a great job at one of the top company.\nMy boss is a very nice man. He even allowed\nme to use his boat this weekend. However,\nI have some urgent work to do so I can't go\nsailing now. Of course I said I would go\nso that I wouldn't be impolite.\nI must also mention that I have a lovely, loving wife.\nLife really couldn't be anymore perfect.\nIt is 8:15am.\nI have just arrived at my workplace...", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (rotate > 700) then
			boatX = boatX + 1
			image.blit(boatX, 220, imageList[6])
			image.blit(boatX, 250, imageList[5])
		end
	
	elseif (storySection == 2) then
	
		if (scene == false) then
			screen.clear(color.new(colorRGB, colorRGB, colorRGB))
		
			if (wait > 0) then
				wait = wait - 1;
			end
		
			if (wait < 255) then
				colorRGB = colorRGB + 1;
			end
		
			image.blit(20, 20, imageList[7])
			
			if (wait < 1) then
				scene = true;
			end
		else
			if (wait < 255) then
				wait = wait + 1;
				image.blit(200, 120, imageList[4]);
				image.blit(150, 50, imageList[8]);
				image.blit(20, 70, imageList[9]);
				image.blit(10, 10, imageList[10]);
			else
			
				if (colorRGB ~= 0) then
					colorRGB = colorRGB - 1;
				end
				screen.clear(color.new(colorRGB, colorRGB, colorRGB))
		
				if (colorRGB ~= 0) then
					image.blit(200, 120, imageList[4]);
					image.blit(150, 50, imageList[8]);
					image.blit(20, 70, imageList[9]);
					image.blit(10, 10, imageList[10]);
				else
					image.blit(180, 120, imageList[11])
					image.blit(150, 50, imageList[8]);
					image.blit(20, 70, imageList[9]);
					screen.print(5, 200, "Press Cross to Continue", blue);
				end
			end
		end
	elseif (storySection == 3) then
		
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		
		image.rotate(imageList[2], rotate)
		rotate = rotate + 1;
		
		if (y < 4) then
			image.blit(30, 240, imageList[13])
			image.rotate(imageList[13], 45)
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
		
		if (rotate > 20) then
			screen.print(x, y, "Thus, my imperfect life began.\nNo boat, no wife, no job.\nDivorcing was difficult.\nI was desperate.\nThere was only one way out of this all.\nI had to take extreme measures.\n.\n.\n.\n.\n.\n.\n.\nI should open a company.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (rotate > 250) then
			image.blit(350, 40, imageList[5])
			draw.line(350, 40, 460, 70, red)
			draw.line(460, 40, 350, 70, red)
		end
		
		if (rotate > 300) then
			image.blit(380, 80, imageList[12])
			draw.line(350, 110, 460, 140, red)
			draw.line(460, 110, 350, 140, red)
		end
		
	elseif (storySection == 4) then
		if (story[2][1] == 0) then
			story[2][1] = 1;
			saveStory();
		end
		break;
	end
	
		controls.read()
		
		if controls.press("cross") then
			-- next scene
			storySection = storySection + 1;
			if (storySection == 2) then
				playingGameMusic = false;
				playMusic(musicLove, playingGameMusic);
			elseif (storySection == 3) then
				playingGameMusic = true;
				playMusic(musicGame, playingGameMusic)
				rotate = 0;
				y = 280;
			end
		elseif (controls.press("circle")) then
			-- skip scene
			if (story[2][1] == 0) then
				story[2][1] = 1;
				saveStory();
			end
			--clearStageMemory(imageList, playingGameMusic);
			--showStory()
			break;
		end
		
		screen.flip();
	end
	
	clearStageMemory(imageList, playingGameMusic);
	showStory()
end

showStage1 = function()
	
	local playingGameMusic = true;
	local soundPlayWind = "sounds/wind.wav"
	local imageList = {}
	
	table.insert(imageList, image.load("images/story/intro/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/stage1/cloud.png"))
	table.insert(imageList, image.load("images/story/stage1/flyer.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	
	local storySelection = 1;
	
	while true do
	
	if (storySelection == 1) then
	
	image.blit(0, 0, imageList[1]);
	image.blit(80, 80, imageList[2]);
	image.blit(-30 + rotate - 0.8, 70, imageList[3]);
	
	image.rotate(imageList[2], rotate)
	rotate = rotate + 1;
	
		if (rotate > 20) then
			screen.print(x, y, "Last months were unfortunate for me.\nHowever, I am optimistic things will change.\nI have thought what would be a profitable\nbusiness idea and I am definite I have\nfound one. I'll be using all the skills\nI have learned in the past.\nThere is just one slight problem.\nI don't know how to get enough\nfinancing support.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
	
	elseif (storySelection == 2) then
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		image.blit(80 + rotate - 0.8, 80, imageList[3]);
		image.blit(80, 2, imageList[4]);
	
		image.rotate(imageList[2], rotate)
		rotate = rotate + 1;
		
		screen.print(5, 150, "Lucky!", blue);
		screen.print(5, 200, "Press Cross to Continue", blue);
		
		
	else
		clearStageMemory(imageList, playingGameMusic);
		startStage1()
	end
	
	controls.read()
			
		if (controls.press("cross")) then
			storySelection = storySelection + 1;
			if (storySelection == 2) then
				playSound(soundPlayWind)
				screen.waitvblankstart(20)
			end
		
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage1()
		end
		
		screen.flip();
	end
end

startStage1 = function()

	-- set current stage
	currentStage = 1;

	-- set rules
	userOptions[1] = "2" -- number of players
	userOptions[2] = "no" -- allow multiple threats
	userOptions[3] = "medium" -- difficulty
	userOptions[4] = "$500000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage1/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage1 = function(victory, moneyResult)
	
	if (victory) then
		
		-- player 1 wins, access to next level
		if (story[2][1] ~= 2) then
			story[2][1] = 2;
			story[3][1] = 1;
			saveStory()
		end
		
		-- new highscore?
		if (tonumber(string.sub(story[2][2], 2)) < moneyResult) then
			story[2][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[2][2])
		end
	end
	
	-- back to stage selection
	showStory()
	
end

showStage2 = function()

local playingGameMusic = true;
	
	local soundPlayWind = "sounds/wind.wav"
	local imageList = {}
	
	table.insert(imageList, image.load("images/story/stage2/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/stage2/company.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	
	local storySelection = 1;
	
	while true do
	
	if (storySelection == 1) then
	
	image.blit(0, 0, imageList[1]);
	image.blit(80, 80, imageList[2]);
	
	image.rotate(imageList[2], rotate)
	rotate = rotate + 1;
	
		if (rotate > 200) then
			image.blit(350, 40, imageList[3])
		end
		
		if (rotate > 850) then
			image.blit(380, 80, imageList[3])
			draw.line(350, 110, 460, 140, red)
			draw.line(460, 110, 350, 140, red)
		end
	
		if (rotate > 20) then
			screen.print(x, y, "I was able to open a company with the money\nwon from the competition.\nI soon noticed there were other companies\ncompeting closely on the same business area as I.\nThis meant winning customer contracts\nwas not an easy task.\nOne company had tried to get advantage\nby doing the jobs at an overly low price,\nbut this stragegy failed.\nThe company went bankruptcy.\n\n\nThe current situation isn't good.\nWe all can't grow in the market.\nEventually some, if not all, will lose.\nI can't let this happen.\nI must take a risk and change my\ncompany's stragegy for better or for worse.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (y < 4) then
			screen.print(5, 260, "Press Cross to Continue", blue);
		end
		
	else
		clearStageMemory(imageList, playingGameMusic);
		startStage2()
	end
	
	controls.read()
			
		if (controls.press("cross")) then
			storySelection = storySelection + 1;
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage2()
		end
		
		screen.flip();
	end
end

startStage2 = function()
	-- set current stage
	currentStage = 2;

	-- set rules
	userOptions[1] = "3" -- number of players
	userOptions[2] = "no" -- allow multiple threats
	userOptions[3] = "hard" -- difficulty
	userOptions[4] = "$300000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage2/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage2 = function(victory, moneyResult)

	if (victory) then
		
		-- player 1 wins, access to next level
		if (story[3][1] ~= 2) then
			story[3][1] = 2;
			story[4][1] = 1;
			saveStory()
		end
		
		-- new highscore?
		if (tonumber(string.sub(story[3][2], 2)) < moneyResult) then
			story[3][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[3][2])
		end
	end
	
	-- back to stage selection
	showStory()

end

showStageVideo = function()

	local musicArianne = "music/arianne.mp3"
	local playingGameMusic = true;
	local imageList = {}

	table.insert(imageList, image.load("images/story/video/background2.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/video/girl2.png"))
	table.insert(imageList, image.load("images/story/video/speaking.png"))
	table.insert(imageList, image.load("images/story/video/thinking.png"))
	table.insert(imageList, image.load("images/story/video/arianne.png"))
	table.insert(imageList, image.load("images/story/video/bubble.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	local moveX = 0;
	local wait = 0;
	local color1 = 100;
	local color2 = 100;
	local color3 = 100;
	local lowerColor = false;
	local higherColor = false;
	
	local storySelection = 1;
	
	while true do
	
	if (storySelection == 1) then
	
	image.blit(0, 0, imageList[1]);
	image.blit(80, 80, imageList[2]);
	image.blit(200, 200, imageList[3]);
	
	image.rotate(imageList[2], rotate)
	rotate = rotate + 1;
	
		if (rotate > 20) then
			screen.print(x, y, "My business is going well at the\nmoment. It has for nearly two years.\nRecently my company went public and\nnow I have decided to expand\nthe business by buying a new office.\nThis in front of me would be\na perfect location. The view\nis nice as the building is\nnear a lake.", white)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (y < 35) then
		image.blit(500 - moveX, 200, imageList[4])
		if (y > 4) then
			moveX = moveX + 1;
		else
			image.blit(230, 130, imageList[5])
			image.blit(0, 110, imageList[6])
			
			screen.print(290, 160, "Hi!", blue)
			screen.print(50, 125, "Why is this strange woman\ngreeting me all of a sudden?", blue)
			
		end
	end
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
	elseif(storySelection == 2) then
	
		image.rotate(imageList[2], rotate)
		rotate = rotate + 1;
		wait = wait + 0.5;
		
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		image.blit(200, 200, imageList[3]);
		image.blit(260, 70, imageList[7]);
		image.blit(500 - moveX, 200, imageList[4])
	
		if (wait < 100) then
			image.blit(170, 40, imageList[5])
			image.blit(0, 110, imageList[6])
			screen.print(230, 70, "Hi!", blue)
			screen.print(50, 125, "Why is this strange woman\ngreeting me all of a sudden?", blue)
		elseif (wait < 200) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 60, "Oh!\nDon't be scared.", blue)
		elseif (wait < 300) then
			image.blit(170, 40, imageList[5])
			screen.print(190, 70, "I won't eat ya.", blue)
		elseif (wait < 400) then
			image.blit(170, 40, imageList[5])
			screen.print(190, 60, "I am Arianne.\nOwner of\nCat Industry.", blue)
		elseif (wait < 500) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 60, "This is just our\nofficial work\ncostume.", blue)
		elseif (wait < 600) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "It's nice to meet\nya!", blue)
		elseif (wait < 700) then
			image.blit(140, 110, imageList[8])
			screen.print(145, 125, "It's nice to meet\nyou too.", blue)
		elseif (wait < 800) then
			image.blit(140, 110, imageList[8])
			screen.print(145, 120, "So why do you\nhave that kind of\na costume?", blue)
		elseif (wait < 900) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "It helps people\nto remember us.", blue)
		elseif (wait < 1000) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "I have designed\nthis by myself.", blue)
		elseif (wait < 1100) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "How do you\nlike it?", blue)
		elseif (wait < 1200) then
			image.blit(0, 110, imageList[6])
			screen.print(50, 125, "It's ridicuously silly.", blue)
		elseif (wait < 1300) then
			image.blit(140, 110, imageList[8])
			screen.print(145, 125, "(but) cute!", blue)
		elseif (wait < 1400) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "Oh thank you!", blue)
		elseif (wait < 1500) then
			image.blit(0, 110, imageList[6])
			screen.print(50, 125, "Did I just say\nwhat I think I said?", blue)
		elseif (wait < 1600) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "Isn't this a\nlovely place!", blue)
		elseif (wait < 1700) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 60, "I am going to\nexpand my\nbusiness and", blue)
		elseif (wait < 1800) then
			image.blit(170, 40, imageList[5])
			screen.print(180, 70, "buy this office.", blue)
		elseif (wait < 1900) then
			storySelection = 3;
		end
	elseif (storySelection == 3) then
		screen.clear(color1, color2, color3)
		
		if (lowerColor == false and higherColor == false) then
			image.blit(10, 30, imageList[8])
			image.blit(10, 170, imageList[8])
			image.blit(330, 70, imageList[7]);
			image.blit(180, 20, imageList[5])
			screen.print(20, 15, "Select with triangle", red)
			screen.print(25, 40, "Wait a minute.\nI was here first.\nI'll buy this!", blue)
			screen.print(20, 155, "Select with square", red)
			screen.print(25, 180, "Good luck with\nyour business.\nSee you!", blue)
			screen.print(190, 35, "I am going\nto expand\nmy business and\nbuy this office.", blue)
		end
		
		if (lowerColor) then
			color1 = color1 - 1.0
			color2 = color2 - 1.0
			color3 = color3 - 1.0
		elseif (higherColor) then
			color1 = color1 + 1.0
			color2 = color2 + 1.0
			color3 = color3 + 1.0
		end
		
		if (color1 < 1 or color1 > 254) then
			storySelection = 4;
			saveStory();
		end
		
	else
		clearStageMemory(imageList, playingGameMusic);
		showStory()
	end
	
	controls.read()
			
		if (controls.press("cross") and storySelection < 3) then
			storySelection = storySelection + 1;
			
			if (storySelection == 2) then
				playingGameMusic = false;
				playMusic(musicArianne, playingGameMusic);
			end
		elseif (controls.press("triangle") and storySelection == 3) then
			lowerColor = true;
			-- active Arianne stage
			if (story[6][1] ~= 2) then
				story[6][1] = 1
			end
			
			if (story[8][1] ~= 2) then
				story[4][1] = 0
			end
			
		elseif (controls.press("square") and storySelection == 3) then
			higherColor = true;
			-- active Lily stage
			if (story[5][1] ~= 2) then
				story[5][1] = 1
			end
			
			if (story[8][1] ~= 2) then
				story[4][1] = 0
			end
			
		elseif (controls.press("circle")) then
			-- skip scene
			storySelection = 3;
		end
		
		screen.flip();
	end
end

showStage3 = function()

	local musicMemory = "music/memory.mp3"
	local soundThunder = "sounds/thunder.wav"
	local thunderPlayed = false;
	local playingGameMusic = true;
	
	local imageList = {}
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/stage3/cloud.png"))
	table.insert(imageList, image.load("images/story/stage3/thunder.png"))
	table.insert(imageList, image.load("images/story/stage3/waterdrop.png"))
	table.insert(imageList, image.load("images/story/stage3/ghost.png"))
	table.insert(imageList, image.load("images/story/stage3/girl.png"))
	table.insert(imageList, image.load("images/story/stage3/lily.png"))
	table.insert(imageList, image.load("images/story/stage3/speaking.png"))
	table.insert(imageList, image.load("images/story/stage3/bubble.png"))
	table.insert(imageList, image.load("images/story/intro/bubble2.png"))
	
	local x = 5;
	local y = 280;
	local moveX = 0;
	local moveX2 = 480;
	local waterDrop = 0;
	local waterDrop2 = 0;
	local waterDrop3 = 0;
	local waiter = 0;
	
	local storySelection = 1;
	
	while true do
	screen.clear(darkgray)
	
	if (storySelection == 1) then
		if	(y > 3) then
			moveX = moveX + 0.1
		end
	image.blit(moveX, 200, imageList[1]);
	image.blit(80, 80, imageList[2]);
	image.blit(350, 80, imageList[2]);
	
		if (moveX > 2) then
			screen.print(x, y, "I am still looking for a new office.\nIt's just hard to find one with a decent price\nand a good location. If this continues,\nI am afraid my business' growth will stop.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (y >= 60 and y <= 70) then
			if (thunderPlayed == false) then
				playSound(soundThunder)
				thunderPlayed = true;
			end
			image.blit(350, 120, imageList[3])
		end
		
		if (y <= 55) then
			waterDrop = waterDrop + 0.5
			image.blit(350, 120 + waterDrop, imageList[4])
		end
		
		if (y <= 45 and y > 12) then
			waterDrop2 = waterDrop2 + 0.5
			image.blit(400, 120 + waterDrop2, imageList[4])
		end
		
		if (y <= 35) then
			waterDrop3 = waterDrop3 + 0.5
			image.blit(80, 120 + waterDrop3, imageList[4])
		end
		
		if (moveX > 50) then
			if (y > 12) then
				moveX2 = moveX2 - 0.1;
				image.blit(moveX2, 200, imageList[5])
			else
				image.blit(moveX2, 200, imageList[6])
			end
			
		end
		
		if (y < 4) then
			image.blit(moveX - 25, 130, imageList[10])
			screen.print(5, 240, "Press Cross to Continue", blue);
		end
	elseif (storySelection == 2) then
		
		waiter = waiter + 0.5;
		image.blit(140, 200, imageList[1]);
		image.blit(80, 80, imageList[2]);
		image.blit(350, 80, imageList[2]);
		image.blit(300, 40, imageList[7])
		if (waiter < 100) then
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "How are you\ndoing?", blue)
		elseif (waiter < 200) then
			image.blit(200, 25, imageList[8])
			screen.print(205, 35, "Ha.\nI am doing great.", blue)
		elseif (waiter < 300) then
			image.blit(200, 25, imageList[8])
			screen.print(205, 35, "Your Ex-Boss\nditched me\nafter we divorced.", blue)
		elseif (waiter < 400) then
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "That's\nunfortunate\nto hear.", blue)
		elseif (waiter < 500) then
			image.blit(200, 25, imageList[8])
			screen.print(205, 35, "Whatever. Since\nthen I have\nstarted a\ncompany.", blue)
		elseif (waiter < 600) then
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "Really?\nSo have I.", blue)
		elseif (waiter < 700) then
			image.blit(200, 25, imageList[8])
			screen.print(205, 35, "Great! I will force\nyour company to\nan early grave\nbefore...", blue)
		elseif (waiter < 800) then
			image.blit(200, 25, imageList[8])
			screen.print(205, 35, "I revenge your\nEx-Boss.", blue)
		elseif (waiter < 900) then
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "Huh?", blue)
		else
			screen.print(5, 245, "Press Cross to Continue", blue);
		end
	
	else
		clearStageMemory(imageList, playingGameMusic);
		startStage3()
	end
	
	controls.read()
			
		if (controls.press("cross")) then
			storySelection = storySelection + 1;
			if (storySelection == 2) then
				playMusic(musicMemory, playingGameMusic);
				playingGameMusic = false;
			end
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage3()
		end
		
		screen.flip();
	end

end

startStage3 = function()
	-- set current stage
	currentStage = 3;

	-- set rules
	userOptions[1] = "4" -- number of players
	userOptions[2] = "no" -- allow multiple threats
	userOptions[3] = "easy" -- difficulty
	userOptions[4] = "$400000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage3/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage3 = function(victory, moneyResult)

	if (victory) then
		
		-- player 1 wins, access to next level
		if (story[7][1] == 0) then
			story[7][1] = 1;
			story[5][1] = 2;
			saveStory()
		elseif (story[5][1] ~= 2) then
			story[5][1] = 2;
			saveStory()
		end
		
		-- new highscore?
		if (tonumber(string.sub(story[5][2], 2)) < moneyResult) then
			story[5][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[5][2])
		end
	end
	
	if (victory) then
	
	-- show end video of stage
	local imageList = {}
	
	local playingGameMusic = false;
	local musicRegret = "music/regret.mp3"
	playMusic(musicRegret, playingGameMusic);
	
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/stage3/lily.png"))
	table.insert(imageList, image.load("images/story/stage3/speaking.png"))
	table.insert(imageList, image.load("images/story/stage3/bubble.png"))
	table.insert(imageList, image.load("images/story/video/thinking.png"))
	
	local wait = 0;
	
	while true do
	
		image.blit(140, 200, imageList[1]);
		image.blit(300, 40, imageList[2])
	
		wait = wait + 0.5;
		
		if (wait < 100) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "I am so sorry.", blue)
		elseif (wait < 200) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "I have done\na horrible thing.", blue)
		elseif (wait < 300) then
			image.blit(80, 130, imageList[4])
			screen.print(90, 140, "Don't worry.\nPast is past.", blue)
		elseif (wait < 400) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "I make this\nup to you.", blue)
		elseif (wait < 500) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "Let's merge\ncompanies.", blue)
		elseif (wait < 600) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "Together we can\ngrow in the\nmarket.", blue)
		elseif (wait < 700) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "Of course if\nonly you accept?", blue)
		elseif (wait < 800) then
			image.blit(80, 130, imageList[4])
			screen.print(90, 140, "That sounds\ngreat!\nLet's do it.", blue)
		elseif (wait < 900) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "Really?", blue)
		elseif (wait < 1000) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "Now I am able to\ntake my revenge\nand", blue)
		elseif (wait < 1100) then
			image.blit(200, 25, imageList[3])
			screen.print(205, 35, "force your\nEx-Boss into\na bankruptcy!", blue)
		elseif (wait < 1200) then
			image.blit(-60, 110, imageList[5])
			screen.print(40, 122, "Somehow I feel\nI am deceived again.", blue)
		elseif (wait < 1300) then
			clearStageMemory(imageList, playingGameMusic);
			break;
		end
		
		controls.read()
			
		if (controls.press("cross") or controls.press("circle")) then
			clearStageMemory(imageList, playingGameMusic);
			break;
		end
		
		screen.flip();
	end
	end
	
	-- back to stage selection
	showStory()
end

showStage4 = function()

	local playingGameMusic = true;
	
	local imageList = {}
	
	table.insert(imageList, image.load("images/story/video/background2.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/stage1/cloud.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	
	local storySelection = 1;
	
	while true do
	
	if (storySelection == 1) then
	
	image.blit(0, 0, imageList[1]);
	image.blit(80, 80, imageList[2]);
	image.blit(80, 80, imageList[3]);
	image.blit(120, 10, imageList[3]);
	image.blit(200, 40, imageList[3]);
	image.blit(300, 20, imageList[3]);
	image.blit(400, 30, imageList[3]);
	image.blit(480, 40, imageList[3]);
	
	image.rotate(imageList[2], rotate)
	rotate = rotate + 1;
	
		if (rotate > 20) then
			screen.print(x, y, "It was quite an encounter.\nAll I can say is that\nArianne woman has sharp\nteeths and nails.\nShe declared a war.\nLast time I saw her was when\nshe said to have bought\na company nearby. She is\ndetermined to win all the\ncontracts from me.\nRegardless of that,\nI am going to take\nand win the challenge.", black)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
		
	else
		clearStageMemory(imageList, playingGameMusic);
		startStage4()
	end
	
	controls.read()
			
		if (controls.press("cross")) then
			storySelection = storySelection + 1;
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage4()
		end
		
		screen.flip();
	end
end

startStage4 = function()
	-- set current stage
	currentStage = 4;

	-- set rules
	userOptions[1] = "4" -- number of players
	userOptions[2] = "yes" -- allow multiple threats
	userOptions[3] = "hard" -- difficulty
	userOptions[4] = "$300000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage4/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage4 = function(victory, moneyResult)

	if (victory) then
		
		-- player 1 wins, access to next level
		if (story[7][1] == 0) then
			story[7][1] = 1;
			story[6][1] = 2;
			saveStory()
		elseif (story[6][1] ~= 2) then
			story[6][1] = 2;
			saveStory()
		end
		
		-- new highscore?
		if (tonumber(string.sub(story[6][2], 2)) < moneyResult) then
			story[6][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[6][2])
		end
	end
	
	if (victory) then
	
	-- show end video of stage
	local imageList = {}
	--table.insert(imageList, image.load("images/story/video/background2.png"))
	--table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/video/speaking.png"))
	table.insert(imageList, image.load("images/story/video/thinking.png"))
	table.insert(imageList, image.load("images/story/video/arianne.png"))
	table.insert(imageList, image.load("images/story/video/bubble.png"))
	
	local playingGameMusic = false;
	local musicArianne = "music/arianne.mp3"
	playMusic(musicArianne, playingGameMusic);
	
	--local rotate = 0;
	local wait = 0;
	
	while true do
	
		--image.rotate(imageList[2], rotate)
		--rotate = rotate + 1;
		wait = wait + 0.5;
		
		--image.blit(0, 0, imageList[1]);
		--image.blit(80, 80, imageList[2]);
		image.blit(200, 200, imageList[1]);
		image.blit(260, 70, imageList[4]);
		
		if (wait < 100) then
			--image.resize(imageList[2], 150, 80)
			image.blit(170, 40, imageList[2])
			screen.print(180, 70, "That was\namazing!", blue)
		elseif (wait < 200) then
			--image.resize(imageList[2], 150, 80)
			image.blit(170, 40, imageList[2])
			screen.print(180, 70, "Let's work\ntogether!", blue)
		elseif (wait < 300) then
			--image.resize(imageList[2], 150, 80)
			image.blit(170, 40, imageList[2])
			screen.print(180, 70, "We're going to\nbe a great team!", blue)
		elseif (wait < 400) then
			--image.resize(imageList[5], 150, 80)
			image.blit(140, 110, imageList[5])
			screen.print(145, 125, "It would be\na pleasure.", blue)
		elseif (wait < 500) then
			--image.resize(imageList[3], 300, 80)
			image.blit(0, 110, imageList[3])
			screen.print(50, 125, "I hope this is\na right decision.", blue)
		elseif (wait < 600) then
			--image.resize(imageList[2], 150, 80)
			image.blit(170, 40, imageList[2])
			screen.print(180, 70, "It's settled then.", blue)
		elseif (wait < 700) then
			--image.resize(imageList[2], 150, 80)
			image.blit(170, 40, imageList[2])
			screen.print(180, 60, "We're going\nto conquer\nthe market!", blue)
		elseif (wait < 800) then
			--image.resize(imageList[2], 150, 80)
			image.blit(170, 40, imageList[2])
			screen.print(180, 70, "Haa haa haa...", blue)
		elseif (wait < 900) then
			--image.resize(imageList[3], 300, 80)
			image.blit(0, 110, imageList[3])
			screen.print(50, 125, "On the other hand...", blue)
		elseif (wait < 1000) then
			--image.resize(imageList[3], 300, 80)
			image.blit(0, 110, imageList[3])
			screen.print(50, 125, "I am not sure whether\nthis was the right decision.", blue)
		elseif (wait < 1100) then
			clearStageMemory(imageList, playingGameMusic);
			break;
		end
	
		controls.read()
			
		if (controls.press("cross") or controls.press("circle")) then
			clearStageMemory(imageList, playingGameMusic);
			break;
		end
		
		screen.flip();
	end
	end
	
	-- back to stage selection
	showStory()

end

showStage5 = function()
	local imageList = {}
	
	table.insert(imageList, image.load("images/story/stage5/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/stage2/company.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	
	local storySelection = 1;
	
	while true do
	
	if (storySelection == 1) then
	
	image.blit(0, 0, imageList[1]);
	image.blit(80, 80, imageList[2]);
	
	image.rotate(imageList[2], rotate)
	rotate = rotate + 1;
	
	image.blit(450, 200, imageList[3])
	image.blit(350, 200, imageList[3])
	image.blit(150, 200, imageList[3])
	image.blit(50, 200, imageList[3])
		
		if (rotate > 20) then
			screen.print(x, y, "Merging two companies was very profitable.\nWe have quickly grown in the market.\nOther companies have also noticed this.\nThey're not satisfied that some of\ntheir customers have decided to buy\nour services instead.\nThrough cooperation they have sweared to\nend our leading market role by any means.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
		
	else
		clearStageMemory(imageList, playingGameMusic);
		startStage5()
	end
	
	controls.read()
			
		if (controls.press("cross")) then
			storySelection = storySelection + 1;
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage5()
		end
		
		screen.flip();
	end
end

startStage5 = function()
	-- set current stage
	currentStage = 5;

	-- set rules
	userOptions[1] = "4" -- number of players
	userOptions[2] = "yes" -- allow multiple threats
	userOptions[3] = "extreme" -- difficulty
	userOptions[4] = "$200000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage5/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage5 = function(victory, moneyResult)

	if (victory) then
		
		-- player 1 wins, access to next level
		if (story[8][1] == 0) then
			story[8][1] = 1;
			story[7][1] = 2;
			saveStory()
		end
		
		-- new highscore?
		if (tonumber(string.sub(story[7][2], 2)) < moneyResult) then
			story[7][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[7][2])
		end
	end

	showStory()
end

showStage6 = function()

	local musicRich = "music/rich.mp3"
	local playingGameMusic = true; -- assumably.
	
	local imageList = {}
	table.insert(imageList, image.load("images/story/intro/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/stage2/company.png"))
	table.insert(imageList, image.load("images/story/stage6/butterfly1.png"))
	table.insert(imageList, image.load("images/story/stage6/butterfly2.png"))
	table.insert(imageList, image.load("images/story/stage6/car1.png"))
	table.insert(imageList, image.load("images/story/stage6/car2.png"))
	table.insert(imageList, image.load("images/story/stage6/car3.png"))
	table.insert(imageList, image.load("images/story/stage6/boss.png"))
	table.insert(imageList, image.load("images/story/video/bubble.png"))
	table.insert(imageList, image.load("images/story/video/speaking.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	local butterflyX = 1;
	local storySelection = 1;
	
	local wait = 0;
	
	while true do
		
	if (storySelection == 1) then
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		image.blit(350, 200, imageList[4])
		image.blit(300, 200, imageList[7])
		image.blit(420, 210, imageList[8])
		image.blit(300, 230, imageList[9])
		
		image.blit(5 + butterflyX, y - (rotate - 0.8), imageList[5]);
		image.blit(400 - butterflyX, y - (rotate - 0.8), imageList[6]);
	
		image.rotate(imageList[2], rotate)
		rotate = rotate + 1;
	
		if (y > 3) then
			butterflyX = butterflyX + 0.2;
		end
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
		
		if ((rotate % 5) == 0) then
			butterflyX = butterflyX * -1;
		end
		
		if (rotate > 20) then
			screen.print(x, y, "Business is going excellently.\nMy company has actually become one of the most\nappreciated workplace. The business is profitable\nthan ever. People from all over the world has\nbegan to recognize us.\nThanks to this, we have finally\nbeen selected to participate a massive\ncontract bidding contest.\nIf we win that and do a good job,\nit would be like a dream come true.\n\nHowever, winning will not be child's play.\nI am sure even my Ex-Boss will be there.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
	elseif (storySelection == 2) then
		screen.clear(darkgray)
		wait = wait + 0.5
		
		image.blit(270, 200, imageList[10])
		image.blit(140, 200, imageList[3]);
		
		if (wait < 100) then
			image.blit(175, 120, imageList[12])
			screen.print(180, 140, "So, you're\nhere too!", blue)
		elseif (wait < 200) then
			image.blit(175, 120, imageList[12])
			screen.print(180, 140, "I knew you had\npotentiality but...", blue)
		elseif (wait < 300) then
			image.blit(175, 120, imageList[12])
			screen.print(180, 140, "I never had\nguessed you to\nget this far.", blue)
		elseif (wait < 400) then
			image.blit(175, 120, imageList[12])
			screen.print(180, 140, "What's your\ndriving force?", blue)
		elseif (wait < 500) then
			image.blit(175, 120, imageList[12])
			screen.print(180, 140, "Money?\nFame?\nOr...", blue)
		elseif (wait < 600) then
			image.blit(175, 120, imageList[12])
			screen.print(180, 140, "Perhaps\na revenge?", blue)
		else
			image.blit(90, 120, imageList[11])
			screen.print(97, 135, "You're going to\nfind it out\nsoon enough.", blue)
			screen.print(5, 260, "Press Cross to Continue", blue);
		end
		
	else
		break;
	end
	
		controls.read()
		
		if controls.press("cross") then
			-- next scene
			storySelection = storySelection + 1;
			if (storySelection == 2) then
				playingGameMusic = false;
				playMusic(musicRich, playingGameMusic);
			end
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage6()
		end
		
		screen.flip();
	end

	clearStageMemory(imageList, playingGameMusic);
	startStage6()
end

startStage6 = function()
	-- set current stage
	currentStage = 6;

	-- set rules
	userOptions[1] = "4" -- number of players
	userOptions[2] = "yes" -- allow multiple threats
	userOptions[3] = "extreme" -- difficulty
	userOptions[4] = "$400000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage6/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage6 = function(victory, moneyResult)

	if (victory) then
		
		if (story[8][1] ~= 2) then
			story[8][1] = 2;
			story[4][1] = 1; -- now encounter can always be active
			saveStory()
		end
		
		if (story[5][1] == 2 and story[6][1] == 0) then
			-- lily ending
			story[9][1] = 1;
			saveStory()
		elseif (story[6][1] == 2 and story[5][1] == 0) then
			-- arianne ending
			story[10][1] = 1;
			saveStory()
		else
			-- both endings available
			story[9][1] = 1;
			story[10][1] = 1;
			saveStory()
		end
		
		if (story[11][1] == 0 and story[9][1] == 1 and story[10][1] == 1) then
			-- make bonus stage available
			story[11][1] = 1;
			saveStory()
		end
		
		-- new highscore?
		if (tonumber(string.sub(story[8][2], 2)) < moneyResult) then
			story[8][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[8][2])
		end
	end

	showStory()
end

showStage7 = function()

	local musicArianne = "music/arianne.mp3"
	local musicLily = "music/memory.mp3"
	local musicBoss = "music/rich.mp3"
	
	playMusic(musicArianne)
	local playingGameMusic = false;
	
	local imageList = {}
	
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/video/girl2.png"))
	table.insert(imageList, image.load("images/story/video/arianne.png"))
	table.insert(imageList, image.load("images/story/stage7/memory.png"))
	table.insert(imageList, image.load("images/story/stage6/boss.png"))
	table.insert(imageList, image.load("images/story/stage3/girl.png"))
	table.insert(imageList, image.load("images/story/stage3/lily.png"))
	table.insert(imageList, image.load("images/story/stage3/speaking.png"))
	table.insert(imageList, image.load("images/story/stage3/bubble.png"))
	table.insert(imageList, image.load("images/story/intro/love.png"))
	
	local storySelection = 1;
	local wait = 0;
	local moveX = 270;
	local moveX2 = 480;
	local moveX3 = 480;
	
	while true do
	
	screen.clear(black);
	image.blit(140, 200, imageList[1]);
	
	if (storySelection == 1) then
	
		wait = wait + 0.5
		image.blit(moveX, 70, imageList[3]);
		image.blit(moveX, 200, imageList[2])
		
		if wait < 100 then
			image.blit(150, 30, imageList[8]);
			screen.print(175, 50, "Hey!", blue)
		elseif wait < 200 then
			image.blit(150, 30, imageList[8]);
			screen.print(155, 35, "Do you remember\nour first\nencounter?", blue)
		elseif wait < 300 then
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "Yes,\nof course.", blue)
		elseif wait < 400 then
			image.blit(150, 30, imageList[8]);
			screen.print(155, 35, "Do you also\nremember saying\nsomething rude\nabout my\ncostume.", blue)
		elseif wait < 500 then
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "No, I never\nsaid that.", blue)
		elseif wait < 600 then
			image.blit(150, 30, imageList[8]);
			screen.print(175, 50, "Liar!", blue)
		elseif wait < 700 then
			image.blit(0, 0, imageList[4]);
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "Wait... I\nnever said that.", blue)
		elseif wait < 800 then
			image.blit(0, 0, imageList[4]);
			image.blit(80, 130, imageList[9])
			screen.print(90, 140, "Just thought it.", blue)
		elseif wait < 900 then
			image.blit(150, 30, imageList[8]);
			screen.print(155, 35, "I knew it.\nIt's a battle.", blue)
		else
			screen.print(5, 260, "Press Cross to Continue", blue);
		end
	elseif (storySelection == 2) then
		
		if (moveX > 0) then
			moveX = moveX - 1;
			image.blit(moveX, 200, imageList[2])
		end
		
		image.blit(moveX2, 200, imageList[5])
		
		if (moveX2 > 270) then
			moveX2 = moveX2 - 1;
		else
			wait = wait + 0.5
			if (wait < 100) then
				image.blit(160, 120, imageList[8]);
				screen.print(165, 125, "Why did you\nruin my business.", blue)
			elseif (wait < 200) then
				image.blit(160, 120, imageList[8]);
				screen.print(165, 125, "Because you\nthink I cheated\non you with\nyour wife?", blue)
			elseif wait < 300 then
				image.blit(80, 130, imageList[9])
				screen.print(90, 140, "Partly yes.", blue)
			elseif (wait < 400) then
				image.blit(160, 120, imageList[8]);
				screen.print(165, 125, "I never did\nsuch a cruel\nthing!", blue)
			elseif wait < 500 then
				image.blit(0, 0, imageList[10]);
				image.blit(80, 130, imageList[9])
				screen.print(90, 140, "Huh? But I saw\nyou two\ntogether!", blue)
			elseif (wait < 600) then
				image.blit(0, 0, imageList[10]);
				image.blit(160, 120, imageList[8]);
				screen.print(165, 125, "It was her\ntwin sister.", blue)
			elseif wait < 700 then
				image.blit(0, 0, imageList[10]);
				image.blit(80, 130, imageList[9])
				screen.print(90, 140, "She doesn't\nhave a twin\nsister.", blue)
			elseif (wait < 800) then
				image.blit(0, 0, imageList[10]);
				image.blit(160, 120, imageList[8]);
				screen.print(165, 125, "See I wasn't\ncheating on\nyou then.", blue)
			elseif (wait < 900) then
				image.blit(0, 0, imageList[10]);
				image.blit(160, 120, imageList[8]);
				screen.print(165, 125, "It must have\nbeen someone\nelse.", blue)
			else
				screen.print(5, 260, "Press Cross to Continue", blue);
			end
		end
		
	elseif (storySelection == 3) then
		if (moveX2 > 0) then
			moveX2 = moveX2 - 1;
			image.blit(moveX2, 200, imageList[5])
		end
		
		if (moveX3 > 270) then
			image.blit(moveX3, 200, imageList[6])
			moveX3 = moveX3 - 1;
		else
			if (moveX3 == 270) then
				image.blit(270, 40, imageList[7]);
			end
			image.blit(moveX3, 200, imageList[6])
			wait = wait + 0.5
			if (wait < 100) then
				image.blit(80, 130, imageList[9])
				screen.print(90, 140, "And what would\nyou have\nagainst me.", blue)
			elseif (wait < 200) then
				image.blit(160, 40, imageList[8]);
				screen.print(165, 45, "You should know\nit better.", blue)
			elseif (wait < 300) then
				image.blit(160, 40, imageList[8]);
				screen.print(165, 45, "You forgot our\nwedding date!", blue)
			elseif (wait < 400) then
				image.blit(80, 130, imageList[9])
				screen.print(90, 140, "Well, every\nman does.", blue)
			elseif (wait < 500) then
				image.blit(80, 130, imageList[9])
				screen.print(90, 140, "Besides. We\nnever re-married.", blue)
			elseif (wait < 600) then
				image.blit(160, 40, imageList[8]);
				screen.print(165, 45, "No excuses!", blue)
			else
				if (moveX3 > -50) then
					moveX3 = moveX3 - 1;
				end
				screen.print(5, 260, "Press Cross to Continue", blue);
			end
		end
		
	else
		clearStageMemory(imageList, playingGameMusic);
		startStage7()
	end
	
	controls.read()
			
		if (controls.press("cross")) then
			storySelection = storySelection + 1;
			if (storySelection == 2) then
				playMusic(musicBoss)
				wait = 0;
			elseif(storySelection == 3) then
				playMusic(musicLily)
				wait = 0;
			end
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			startStage7()
		end
		
		screen.flip();
	end
end

startStage7 = function()
	-- set current stage
	currentStage = 7;

	-- set rules
	userOptions[1] = "4" -- number of players
	userOptions[2] = "yes" -- allow multiple threats
	userOptions[3] = "extreme" -- difficulty
	userOptions[4] = "$500000" -- Sell Company limit
	
	-- show and set card list
	local iniFileName = "images/story/stage7/cards.ini"
	storyCardList(iniFileName, userOptions[2], userOptions[4]);
	
	start(iniFileName);
end

endStage7 = function(victory, moneyResult)

	if (victory) then
		
		if (story[11][1] ~= 2) then
			story[11][1] = 2;
			saveStory()
		end
		
		if (tonumber(string.sub(story[11][2], 2)) < moneyResult) then
			story[11][2] = "$" .. moneyResult
			saveStory()
			newHighscore(story[11][2])
		end
	end

	showStory()
end

showEndingLily = function()
	local musicLily = "music/memory.mp3"
	local musicLove = "music/love.mp3"
	local playingGameMusic = false;
	playMusic(musicLily)
	
	local imageList = {}
	table.insert(imageList, image.load("images/story/lily/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/lily/bubble.png"))
	table.insert(imageList, image.load("images/story/lily/love.png"))
	table.insert(imageList, image.load("images/story/stage3/girl.png"))
	table.insert(imageList, image.load("images/story/stage3/bubble.png"))
	table.insert(imageList, image.load("images/story/lily/bubble3.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	local storySection = 1;
	
	local wait = 255;
	local colorRGB = 0;
	local scene = false;
	
	while true do
		
	if (storySection == 1) then
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		
		image.rotate(imageList[2], rotate)
		rotate = rotate + 1;
	
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
		
		if (rotate > 20) then
			screen.print(x, y, "I and Lily have both worked\nhard for 5 years. We\nwon the massive contract bidding.\nThe outcome of the competition\nchanged a lot. First of all,\nI and Lily got back together.\nMy Ex-Boss lost his face after\nlosing the contract.\nHe was forced to resign.", blue)
			if (y > 3) then
				y = y - 0.2;
			end
		end
		
	elseif (storySection == 2) then
	
		if (scene == false) then
			screen.clear(color.new(colorRGB, colorRGB, colorRGB))
		
			if (wait > 0) then
				wait = wait - 1;
			end
		
			if (wait < 255) then
				colorRGB = colorRGB + 1;
			end
		
			image.blit(40, 60, imageList[3])
			
			if (wait < 1) then
				scene = true;
			end
		else
			if (wait < 255) then
				wait = wait + 1;
				image.blit(40, 120, imageList[4]);
				image.blit(220, 140, imageList[5]);
				image.blit(10, 40, imageList[6]);
				screen.print(30, 45, "Shouldn't you\nbe on a boat\ntrip today?", blue)
			else
			
				if (colorRGB ~= 0) then
					colorRGB = colorRGB - 1;
				end
				screen.clear(color.new(colorRGB, colorRGB, colorRGB))
		
				if (colorRGB ~= 0) then
					image.blit(40, 120, imageList[4]);
					image.blit(220, 140, imageList[5]);
					image.blit(10, 40, imageList[6]);
					screen.print(30, 45, "Shouldn't you\nbe on a boat\ntrip today?", blue)
				else
					image.blit(40, 120, imageList[4]);
					image.blit(220, 140, imageList[5]);
					image.blit(5, 0, imageList[7]);
					screen.print(5, 220, "THE END - Press Cross to Continue", blue);
				end
			end
		end	
	else 
		clearStageMemory(imageList, playingGameMusic);
		showStory()
	end
	
		controls.read()
		
		if controls.press("cross") then
			-- next scene
			storySection = storySection + 1;
			if (storySection == 2) then
				playingGameMusic = false;
				playMusic(musicLove, playingGameMusic);
			end
		elseif (controls.press("circle")) then
			-- skip scene
			clearStageMemory(imageList, playingGameMusic);
			showStory()
		end
		
		screen.flip();
	end

end

showEndingArianne = function()
	local musicArianne = "music/arianne.mp3"
	local playingGameMusic = false;
	playMusic(musicArianne)
	
	local imageList = {}
	table.insert(imageList, image.load("images/story/arianne/background.png"))
	table.insert(imageList, image.load("images/story/intro/sun.png"))
	table.insert(imageList, image.load("images/story/video/girl2.png"))
	table.insert(imageList, image.load("images/story/video/arianne.png"))
	table.insert(imageList, image.load("images/story/video/speaking.png"))
	table.insert(imageList, image.load("images/story/intro/male.png"))
	table.insert(imageList, image.load("images/story/video/bubble.png"))
	table.insert(imageList, image.load("images/story/intro/boss.png"))
	
	local rotate = 0;
	local x = 5;
	local y = 280;
	local storySection = 1;
	
	local moveX = 480;
	local moveX2 = 0;
	local wait = 0;
		
	while true do
	
	rotate = rotate + 1;
	
	if (storySection == 1) then
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		
		image.rotate(imageList[2], rotate)
		
		if (y < 4) then
			screen.print(5, 200, "Press Cross to Continue", blue);
		end
		
		if (rotate > 20) then
			screen.print(x, y, "It's been two years since\nI and Arianne won the contract bidding.\nOur offer was considered to be\nthe most suitable and affordable.\nArianne made good and\nthroughout job with the calculations.\nIn half a year the project\nshould be finished.\nAfter that I and Arianne\nare probably going\nto sell the company.\nWe are both planning to retire early.", white)
			if (y > 3) then
				y = y - 0.2;
			end
		end
	elseif (storySection == 2) then
		image.blit(0, 0, imageList[1]);
		image.blit(80, 80, imageList[2]);
		
		image.rotate(imageList[2], rotate)
		
		if (moveX > 280) then
			image.blit(moveX, 200, imageList[3]);
			moveX = moveX - 1;
		else
			wait = wait + 0.5;
			image.blit(260, 70, imageList[4]);
			
			if (wait > 100) then
				image.blit(200, 200, imageList[6]);
			end
			
			if (wait < 100) then
				image.blit(155, 55, imageList[5]);
				screen.print(165, 65, "Bad news!", blue)
			elseif (wait < 200) then
				image.blit(145, 125, imageList[7]);
				screen.print(152, 140, "What is it?", blue)
			elseif (wait < 300) then
				image.blit(155, 55, imageList[5]);
				screen.print(163, 65, "The company\nwhom we're doing\nthe massive\nproject...", blue)
			elseif (wait < 400) then
				image.blit(155, 55, imageList[5]);
				screen.print(165, 65, "may not be\nable to pay us.", blue)
			elseif (wait < 500) then
				image.blit(145, 125, imageList[7]);
				screen.print(152, 140, "What?!", blue)
			elseif (wait < 600) then
				image.blit(155, 55, imageList[5]);
				screen.print(165, 65, "They're currently\nrestructuring\ntheir debts.", blue)
			elseif (wait < 700) then
				image.blit(145, 125, imageList[7]);
				screen.print(152, 140, "That's a bad\nsituation!", blue)
			elseif (wait < 800) then
				image.blit(145, 125, imageList[7]);
				screen.print(152, 140, "If we don't\nget the money...", blue)
			elseif (wait < 900) then
				image.blit(145, 125, imageList[7]);
				screen.print(154, 140, "We can't pay\nthe salary of\nemployees.", blue)
			else
				image.blit(moveX2, 200, imageList[8]);
				if (moveX2 < 50) then
					moveX2 = moveX2 + 1;
				else
					image.blit(0, 125, imageList[7]);
					screen.print(7, 140, "Luckily I\ndidn't win\nthe contract.", blue)
					screen.print(5, 250, "THE END - Press Cross to Continue", blue);
				end
			end
		end
	else 
		clearStageMemory(imageList, playingGameMusic);
		showStory()
	end
	
		controls.read()
		
		if controls.press("cross") then
			storySection = storySection + 1;
			
		elseif (controls.press("circle")) then
			clearStageMemory(imageList, playingGameMusic);
			showStory()
		end
		
		screen.flip();
	end
end

clearStageMemory = function(imageList, playingGameMusic)
	
	--[[local memory = os.getfreememory()
	screen.clear()
	screen.print(10,10, memory, blue)
	screen.flip();
	screen.waitvblankstart(250)]]
	
	if (playingGameMusic == false) then
		playMusic(musicGame)
	end
	
	local i = 0;
	while (i < #imageList) do
		i = i + 1;
		image.free(imageList[i]);
	end
	collectgarbage()
	
	--[[memory = os.getfreememory()
	screen.clear()
	screen.print(10,10, memory, blue)
	screen.flip();
	screen.waitvblankstart(250)]]
	return
end