require( "scripts/style.lua" );

function QueueDisplayDialog( t )
 f = function() DisplayDialog(t) end; 
 QueueFunction(f);
end

function GetNeededMoneyForSupplies(GrapeA, PriceA, QtyA, GrapeB, PriceB, QtyB, Bottle, BottlePrice, BottleQty)
	local GrapesANeeded = QtyA - GetSeedQnt(GrapeA);
	local GrapesBNeeded = QtyB - GetSeedQnt(GrapeB);
	local BottlesNeeded = BottleQty -GetSupplyQnt(Bottle);

	local moneyNeeded = 0;
	if(GrapesANeeded > 0) then 
		moneyNeeded = moneyNeeded + PriceA*GrapesANeeded;
	end

	if(GrapesBNeeded > 0) then
		moneyNeeded = moneyNeeded + PriceB*GrapesBNeeded;
	end;

	if(BottlesNeeded > 0) then
		moneyNeeded = moneyNeeded + 3*BottlesNeeded;
	end;
	
	return moneyNeeded;
	
end;
	
--returns the current place where the player is
function GetLocal()
	if(GetState() =="viewing_winery") then
		return "winery";
	
	elseif(GetState() =="viewing_cellar") then
		return "cellar";
	
	elseif(GetState() =="viewing_supplier") then
		return "supplier";
	
	elseif(GetState() =="viewing_worldmap") then
		return "worldmap";		
	
	elseif(GetState() == "viewing_scene") then
		return "scene";
		
	elseif(GetState() == "viewing_guild") then
		return "guild";

	else 
		return "###"
	
	end
end

--tells the player about current place and, tells where to go and leaves the current place
function WarnAndLeave(Destiny)
	QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="#"..GetString("tutorial_cant_enter_"..GetLocal(),GetString("tutorial_string_"..Destiny))});
	SendCloseMessage();
end;

--tells the player about current place and, tells where to go and goes to destiny
function WarnAndGoTo(Destiny)
	QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="#"..GetString("tutorial_cant_enter_"..GetLocal(),GetString("tutorial_string_"..Destiny))});
	SendButtonMessage(Destiny);
end;

--returns true if player is at Place, Main Scene or Guild
function IsAt(Place)
	local nowAt = GetLocal();
	if (nowAt == Place or nowAt == "scene" or nowAt == "guild") then
		return true;
	else 
		return false;
	end;
end

function verifySupplies(grapeA, qtyA, grapeB, qtyB, bottle, bottleQty)
	if(GetSeedQnt(grapeA) < qtyA or	GetSeedQnt(grapeB) < qtyB or GetSupplyQnt(bottle) < bottleQty) then
		return false;
	else
		return true;
	end
end
chapter1 = {
	name = "chapter1",
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 1");
				SetGameOverFlag(false);
			end,
	condition = function()
					if(GetState()=="viewing_scene" and GetScene()=="italy") then
						return true;
					else
						return false;
				end
			end,
	finish = function()
			end,
	next = "chapter2";
}

chapter2 = {
	name = "chapter2",
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 2");
				QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_welcome"});
				QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_introduction"});
				Yield();
			end,
	condition = function()
					return true;
			    end,
	finish = function()
				UnlockAcceptedQuest("_global","tutorial_setup");
			 end,
	next = "chapter3";
}

chapter3 = {
	name = "chapter3",
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 3");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_03_txt1"});
				DrawTutorialWindow("scripts/gui/point_to_winery.lua");
				Yield();
				end,
	condition = function()
                    if(not IsAt("winery")) then
                        WarnAndLeave("winery");
                        return false;
                    elseif(GetLocal() == "winery") then
                        return true;
                    else
                        return false;
                    end;
                end,
	finish = function()
				DestroyTutorialWindow();
			end,
	next = "chapter4";
}

chapter4 = {
	name = "chapter4",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 4");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_04_txt1"});
				Yield();
                DrawTutorialWindow("scripts/gui/point_to_changeblend.lua");
				Yield();
			end,
	condition = function()

				if(not IsAt("winery")) then
					WarnAndLeave("winery");
					return false;
					
				elseif(GetState() == "viewing_winery" and GetWineryProductionStage("italy") == 1) then
                    GiveItem("learnt_make_wine");
					return true;
			
				else
					return false;
				end
			end,
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter5";
}

chapter5 = {
	name = "chapter5",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 5");
				if(GetTab() ~= "goals") then
					SetTab("goals");
				end
				Yield();
				DrawTutorialWindow("scripts/gui/point_to_skipday.lua");
				Yield();
			end,
	condition = function()				
				if(HasThisLot("cabernet_sauvignon",0,5)) then
					return true;
				else
					return false;
				end
			end,
	finish = function()
				DestroyTutorialWindow();
			 end,
	next = "chapter6";
}
chapter6 = {
	name = "chapter6",	
		start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 6");
				QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_06_txt1"});
				Yield();
				QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_06_txt2"});
				Yield();
				OpenLogoEditor();
				Yield();
			end,
	condition = function()	
				if(not IsAt("winery")) then
					WarnAndLeave("winery");
					return false;
					
				elseif(HasItem("close_custom_logo") == true) then
					return true;
				else
					return false;
				end
			end,
	finish = function()
				RemoveItem("close_custom_logo")
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_06_txt3"});
                Yield();
			 end,
	next = "chapter7";
}
chapter7 = {
	name = "chapter7",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 7");
				QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_07_txt1"});
				Yield();
                DrawTutorialWindow("scripts/gui/point_to_backbutton.lua");
			end,
	condition = function()
				if(GetState()=="viewing_scene") then
					return true;
				else
					return false;
				end
			end,
	finish = function()
                DestroyTutorialWindow();
                UnlockQuest("italy", "italy_tutorial_a");
			 end,
	next = "chapter8";
}

chapter8 = {
	name = "chapter8",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 8");
                Yield();
                DrawTutorialWindow("scripts/gui/point_to_guildhall.lua");             
			end,
	condition = function()
				if(not IsAt("guild")) then
                    WarnAndLeave("guild");
                    return false;
                elseif(GetLocal() == "guild") then
                    return true;
                else
                    return false;
                end;                   
			end,
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter9";
}

chapter9 = {
	name = "chapter9",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 9");                      
			end,
	condition = function()
				if(GetLocal() == "scene" and HasItem("quest_accepted")) then
                    return true
                else
                    return false
                end;                   
			end,
	finish = function()
			 end,
	next = "chapter10";
}

chapter10 = {
	name = "chapter10",	
	start = function()
                DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 10");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_10_txt1"});
                Yield();            
                SetTab("blends");                  
                DrawTutorialWindow("scripts/gui/point_to_merlot.lua");
                Yield();
            end,
            condition = function()
                if(GetSelectedBlend()=="merlot") then
                    return true;
                else
                    return false;
                end
            end,
            finish = function()
                        DestroyTutorialWindow();
                     end,
	next = "chapter11";
}

chapter11 = {
	name = "chapter11",	
	start = function()
                if(GetTab() ~= "blends") then 
                    SetTab("blends");
                    Yield();
                end
                DrawTutorialWindow("scripts/gui/point_to_goalstab.lua");                
			end,
	condition = function()
                    return GetTab() == "goals";
                end,
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter12";
}

chapter12 = {
	name = "chapter12",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 12");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_12_txt1"});
			end,
	condition = function()
                if(not IsAt("winery")) then
                    WarnAndLeave("winery");
                    return false;
                elseif (GetLocal() == "winery") then
                    return true;
                else
                    return false;
                end;
			end,
	finish = function()
			 end,
	next = "chapter13";
}

chapter13 = {
	name = "chapter13",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 13");
                DrawTutorialWindow("scripts/gui/point_to_changeblend_2.lua");             
			end,
	condition = function()
                if(not IsAt("winery")) then
                    WarnAndLeave("winery");
                    return false;
                elseif (GetLocal() == "winery") then
                    return GetProducedBlendType("italy") == 2;
                else
                    return false;
                end;
			end,
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter14";
}

chapter14 = {
	name = "chapter14",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 14");                
                DrawTutorialWindow("scripts/gui/point_to_skipday_2.lua");
			end,
	condition = function()
                return HasThisLot("merlot", 0, 5);
			end,
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter14b";
}

chapter14b = {
	name = "chapter14b",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 14b");                                
			end,
	condition = function()
                return GetLocal() == "scene";
			end,
	finish = function()                
			 end,
	next = "chapter15";
}

chapter15 = {
	name = "chapter15",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 15");                
                if(not IsAt("guild")) then
                     QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_15_txt1"});
                end;                
			end,
	condition = function()
                if(not IsAt("guild")) then
                    WarnAndLeave("guild");
                    return false;
                else
                    return GetLocal() == "scene" and HasItem("cellar_quest_accepted");
                end;
			end,
	finish = function()               
			 end,
	next = "chapter16";
}

chapter16 = {
	name = "chapter16",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 16");
                if(GetLocal() == "scene") then
                    DrawTutorialWindow("scripts/gui/point_to_cellar.lua");
                    Yield();
                end
			end,
	condition = function()
                    if(not IsAt("cellar")) then
                        WarnAndLeave("cellar");
                        return false;
                    elseif(GetLocal() == "cellar") then
                        return true;
                    else
                        return false;
                    end;                    
                end,
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter18";
}

chapter18 = {
	name = "chapter18",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 18");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_18_txt1"});
			end,
	condition = function()
					if(not IsAt("cellar")) then
						WarnAndLeave("cellar");
						return false;
					elseif(GetLocal() == "cellar") then
						return  not (HasThisLot("cabernet_sauvignon", 0,1) or HasThisLot("merlot", 0,1));
					else
						return false;
					end;                    
                end,    
	finish = function()
			 end,
	next = "chapter19";
}

chapter19 = {
	name = "chapter19",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 19");
                DrawTutorialWindow("scripts/gui/point_to_money.lua");
                Yield();
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_19_txt1"});
                Yield();
			end,
	condition = function()
                    return true;
                end,    
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter19b";
}

chapter19b = {
	name = "chapter19b",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 19b");
			end,
	condition = function()
                    return GetLocal() == "scene";
                end,    
	finish = function()               
			 end,
	next = "chapter20";
}

chapter20 = {
	name = "chapter20",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 20");
                if(GetLocal() ~= "guild") then
                    QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_20_txt1"});
                end;
			end,
	condition = function()
                    if(not IsAt("guild")) then
                        WarnAndLeave("guild");
                        return false;
                    elseif(GetLocal() == "scene" and HasItem("quest_accepted")) then
                        return true;
                    else
                        return false;
                    end;
                end,    
	finish = function()                
			 end,
	next = "chapter21";
}

chapter21 = {
	name = "chapter21",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 21");
			end,
	condition = function()
                    if(not IsAt("winery")) then
                        WarnAndLeave("winery");
                        return false;
                    elseif(GetLocal() == "winery" and GetProducedBlendType("italy") == 1) then
                        return true;
                    else
                        return false;
                    end;
                end,    
	finish = function()                
			 end,
	next = "chapter22";
}

chapter22 = {
	name = "chapter22",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 22");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_22_txt1"});
                Yield();
                DrawTutorialWindow("scripts/gui/point_to_wineriestab.lua");
			end,
	condition = function()
                    return GetTab() == "wineries";
                end,    
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter23";
}

chapter23 = {
	name = "chapter23",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 23");
				SetTab("wineries");
                DrawTutorialWindow("scripts/gui/point_to_italybutton.lua");
			end,
	condition = function()
                    if(GetWinerySelected() == "italy") then
						return true;
					else
						return false;
					end
                end,    
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter24";
}

chapter24 = {
	name = "chapter24",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 24");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_24_txt1"});
                Yield();
			end,
	condition = function()
                    return true;
                end,    
	finish = function()
                UnlockAcceptedQuest("_global", "tutorial_stockup_1");                
			 end,
	next = "chapter24b";
}

chapter24b = {
	name = "chapter24b",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 24b");
			end,
	condition = function()
                    return GetLocal() == "scene";
                end,    
	finish = function()
			 end,
	next = "chapter25";
}

chapter25 = {
	name = "chapter25",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 25");
                if(GetLocal() == "scene") then
                    DrawTutorialWindow("scripts/gui/point_to_supplier.lua");
                end;
			end,
	condition = function()
                    if(not IsAt("supplier")) then
                        WarnAndLeave("supplier");
                        return false;
                    elseif(GetLocal() == "supplier") then
                        return true;
                    else
                        return false;
                    end;
                end,    
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter26";
}

chapter26 = {
	name = "chapter26",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 26");
                if(GetLocal() == "supplier") then
                    QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_26_txt1"});
                    QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_26_txt2"});
                end;
			end,
	condition = function()
					if(verifySupplies("cabernet_sauvignon", 100, "generic_grape", 200, "bottle_straight_sided", 100)) then
                        return true;
                    elseif(not IsAt("supplier")) then
                        WarnAndLeave("supplier");
                        return false;
					elseif(GetLocal() == "supplier") then
						local currentMoneyNeeded = GetNeededMoneyForSupplies("cabernet_sauvignon", 3, 100, "generic_grape", 1, 200, "bottle_straight_sided", 3, 100);
						if(currentMoneyNeeded>GetMoney()) then
							ClearSeedsAndSupplies();
							
							QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_wrong_supplies"});
							
							local moneyNeeded = GetNeededMoneyForSupplies("cabernet_sauvignon", 3, 100, "generic_grape", 1, 200, "bottle_straight_sided", 3, 100);
							GiveMoney(300 + moneyNeeded - GetMoney());
							
						end
						return false;
                    else
                        return false;
                    end;
                end,    
	finish = function()                
			 end,
	next = "chapter27";
}

chapter27 = {
	name = "chapter27",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 27");
                SetTab("inventory");
                Yield();
                DrawTutorialWindow("scripts/gui/point_to_mysupplies.lua");
                Yield();       
			end,
	condition = function()
                    if(GetInventoryDisplayStatus()=="supplies") then
						return true;
					else
						return false;
					end
                end,    
	finish = function()
                DestroyTutorialWindow();
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_27_txt2"});
			 end,
	next = "chapter28";
}

chapter28 = {
	name = "chapter28",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 28");               
			end,
	condition = function()
                    --return GetLocal() == "scene";
											return true;
                end,    
	finish = function()
                UnlockAcceptedQuest("_global", "tutorial_productivity");
                SetMinigameAvailable(3,true);                
			 end,
	next = "chapter29";
}

chapter29 = {
	name = "chapter29",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 29");
                --if(GetLocal() == "scene") then
                    QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_29_txt1"});
					Yield();
					SetTab("goals");
                --end;
			end,
	condition = function()
					if(GetLocal() == "winery") then
                        return true;
                    elseif((GetLocal() ~= "scene") and (GetLocal() ~= "supplier")) then
                        WarnAndLeave("winery");
                        return false;
                    else
                        return false;
                    end;                        
                end,    
	finish = function()               
			 end,
	next = "chapter30";
}

chapter30 = {
	name = "chapter30",	
	start = function()
                DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 30");
                if(GetLocal() == "winery") then				
                    DrawTutorialWindow("scripts/gui/point_to_minigame3.lua");
                    Yield();
                end;
                
			end,
	condition = function()
                    if(not IsAt("winery")) then
                        WarnAndLeave("winery");
                        return false;
                    elseif(GetLocal() == "winery") then
                        return (AlreadyPlayedMinigame(3) and GetWineryProductionQuantity("italy")>5);
                    else
                        return false;
                    end;                        
                end,    
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter30b";
}

chapter30b = {
	name = "chapter30b",	
	start = function()
				QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_31_txt1"});
                DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 30b");                
			end,
	condition = function()
                   return GetLocal() == "scene";                     
                end,    
	finish = function()
			 end,
	next = "chapter31";
}

chapter31 = {
	name = "chapter31",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 31");
			end,
	condition = function()
                        if(not IsAt("guild")) then
                            WarnAndLeave("guild")
                            return false;
                        elseif(GetLocal() == "scene" and not HasItem("quest_accepted")) then
                            return true;
                        else
                            return false;
                        end;
                    end,                    
	finish = function() 
                SetTab("goals");
			 end,
	next = "chapter31b";
}

chapter31b = {
	name = "chapter31b",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 31b");
			end,
	condition = function()
                        return GetLocal() == "scene";
                    end,                    
	finish = function() 
                SetTab("goals");
			 end,
	next = "chapter32";
}

chapter32 = {
	name = "chapter32",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 32");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_32_txt1"});
			end,
	condition = function()
                    return GetTab() == "wineries" or GetLocal() == "worldmap";
                end,    
	finish = function()
                DestroyTutorialWindow();
			 end,
	next = "chapter33";
}

chapter33 = {
	name = "chapter33",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 33");
                if(GetScene() == "italy" and GetLocal() == "scene") then
                    QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_33_txt1"});
                    Yield();
                    DrawTutorialWindow("scripts/gui/point_to_terminal.lua");
                end;
			end,
	condition = function()
                    if(not IsAt("worldmap")) then
                        WarnAndLeave("travel");
                        return false;
                    elseif(GetLocal() == "worldmap") then
                        return true;
                    else
                        return false;
                    end;
                end;
	finish = function()
                DestroyTutorialWindow();                
			 end,
	next = "chapter34";
}

chapter34 = {
	name = "chapter34",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 34");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_34_txt1"});
			end,
	condition = function()
                    if(GetScene() ~= "austria") then
                        if(not IsAt("worldmap")) then
                            WarnAndLeave("travel");
                        end                                
                        return false;                        
                    elseif(not IsAt("guild")) then
                        WarnAndLeave("guild");
                        return false;
                    else
                        return true;
                    end;
                end,    
	finish = function()
                DestroyTutorialWindow();                
			 end,
	next = "chapter35";
}

chapter35 = {
	name = "chapter35",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 35");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_35_txt1"});
			end,
	condition = function()
                    if(not IsAt("guild")) then
                        WarnAndLeave("guild");
                        return false;
                    elseif(GetLocal() == "scene" and HasItem("delivered_franz_wine")) then
                        return true;
                    else
                        return false;
                    end;
                end,    
	finish = function()                         
			 end,
	next = "chapter35b";
}

chapter35b = {
	name = "chapter35b",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 35b");                
			end,
	condition = function()
                    if(GetScene() ~= "greece") then
						if(not (IsAt("worldmap") or GetLocal() == "cellar")) then
							WarnAndLeave("travel");												
						end
						return false;
					else
						if(not IsAt("guild")) then							
							WarnAndLeave("guild");
							return false;
						elseif(GetLocal() == "scene" and HasItem("delivered_nicodemus_wine")) then
							RemoveItem("delivered_nicodemus_wine");
							SetGameOverFlag(true);
							return true;
						else							
							return false;
						end;
					end;
                end,    
	finish = function()                         
			 end,
	next = "chapter36";
}

chapter36 = {
	name = "chapter36",	
	start = function()                
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 36");
			end,
	condition = function()
                    return  GetProducedBlendType("italy") == 7;
                end,    
	finish = function()
                showHireTutorial = true;
			 end,
	next = "chapter37";
}

chapter37 = {
	name = "chapter37",	
	start = function()
                RemoveItem("lock_hire");
                if(GetMoney() < 700) then
                    GiveMoney(700 - GetMoney());
                end;
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 37");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_37_txt1"});
                Yield();
                DrawTutorialWindow("scripts/gui/point_to_makewine_hire.lua");
				Yield();
			end,
	condition = function()
                    return true;
                end,    
	finish = function()                     
			 end,
	next = "chapter38";
}

chapter38 = {
	name = "chapter38",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 38");                    
			end,
	condition = function()
                    if(HasItem("sort_quest_accepted") and GetLocal() == "scene") then
                        RemoveItem("sort_quest_accepted");
                        return true;
                    else
                        return false;
                    end;
                end,    
	finish = function()                
			 end,
	next = "chapter39";
}

chapter39 = {
	name = "chapter39",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 39");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_39_txt1"});                
			end,
	condition = function()
                        return GetScene() == "italy" and GetLocal() == "winery";
                    end;
	finish = function()                
			 end,
	next = "chapter40";
}

chapter40 = {
	name = "chapter40",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 40");
			end,
	condition = function()
                        if(GetProducedBlendType("italy") == 2) then
                            SetMinigameAvailable(2,true);
                            Yield();
                            refreshWineryWindow();
                            return true;
                        else                            
                            return false;
                        end;
                    end;
	finish = function()                     
			 end,
	next = "chapter41";
}

chapter41 = {
	name = "chapter41",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 41");
                if(GetLocal() == "winery") then
                    DrawTutorialWindow("scripts/gui/point_to_minigame2.lua");
                end;
			end,
	condition = function()
                       return AlreadyPlayedMinigame(2) and GetProducedQuality("italy")>2 and GetProducedBlendType("italy")==2;
                    end;
	finish = function() 
                DestroyTutorialWindow();
			 end,
	next = "chapter42";
}

chapter42 = {
	name = "chapter42",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 42");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_42_txt1"});
			end,
	condition = function()
                        if( GetLocal() =="scene" and HasItem("sorting_quest_completed") ) then
                            RemoveItem("sorting_quest_completed");
                            return true;
                        else
                            return false;
                        end;
                    end;
	finish = function()
			 end,
	next = "chapter43";
}

chapter43 = {
	name = "chapter43",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 43");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_43_txt1"});
			end,
	condition = function()
                    if(HasItem("winery_quest_accepted") and GetLocal() == "scene") then
                        RemoveItem("winery_quest_accepted");
                        return true;
                    else
                        return false;
                    end;
                end;
	finish = function()
			 end,
	next = "chapter44";
}

chapter44 = {
	name = "chapter44",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 44");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_44_txt1"});
                Yield();
            end,
	condition = function()                    
                    return true;
                end;
	finish = function()
                UnlockAcceptedQuest("_global", "tutorial_barolo");
			 end,
	next = "chapter45";
}

chapter45 = {
	name = "chapter45",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 45");                
            end,
	condition = function()                    
                    if(HasItem("barolo_quest_accepted") and  GetLocal() == "scene") then
                        RemoveItem("barolo_quest_accepted");
                        SetMinigameAvailable(1, true);
                        return true;
                    else
                        return false
                    end;
                end;
	finish = function()                
			 end,
	next = "chapter46";
}

chapter46 = {
	name = "chapter46",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 46");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_46_txt1"});
            end,
	condition = function()                    
                    return GetScene() == "italy" and GetLocal() == "winery";
                end;
	finish = function()               
			 end,
	next = "chapter47";
}

chapter47 = {
	name = "chapter47",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 47");
                DrawTutorialWindow("scripts/gui/point_to_minigame1.lua");
                Yield();
            end,
	condition = function()                    
                    return AlreadyPlayedMinigame(1) and IsWineryProducingGrapes("italy");
                end;
	finish = function()
                DestroyTutorialWindow();
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_48_txt1"});
			 end,
	next = "chapter48";
}

chapter48 = {
	name = "chapter48",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 48");                
            end,
	condition = function()                    
                    if(HasItem("finished_riesling_quest") and GetLocal()=="scene") then
                        RemoveItem("finished_riesling_quest");
                        return true;
                    else
                        return false;
                    end;
                end;
	finish = function()
			 end,
	next = "chapter49";
}

chapter49 = {
	name = "chapter49",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 49");
                if(GetLocal() == "scene") then
                    QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_49_txt1"});
                end
            end,
	condition = function()                    
                    if(GetState()=="viewing_scene" and GetVinoUltimaePieces()==1) then
                        return true;
                    else
                        return false;
                    end;
                end;
	finish = function()
			 end,
	next = "chapter50";
}

chapter50 = {
	name = "chapter50",	
	start = function()
				DebugOut(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>chapter 50");
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_50_txt1"});
                Yield();
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_50_txt1b"});
                Yield();
                QueueDisplayDialog({"scripts/tutorialmodalh.lua", body="tutorial_chapter_50_txt2"});
                Yield();
            end,
	condition = function()                    
                    return true;
                end;
	finish = function()
			 end,
	next = "chapterend";
}


chapterend = {
	name = "chapterend",	
	start = function()
				DisableTaskText();
				SetTutorialCompleted();
                SetGameOverFlag(true);
			end,
	condition = function()
					return true;
			end,
	finish = function()
			end,
	next = false;
}

currentTask = _G[LoadTutorialChapter()];
forceTutorialStep = 0;

function TaskScript()
	local finishTutorial = false;
	while(finishTutorial == false) do
		--SaveTutorialChapter(currentTask.name);
		currentTask.start();
		while((currentTask.condition() == false) and (forceTutorialStep == 0)) do
			Yield();
		end
		forceTutorialStep = 0;
		currentTask.finish();
		if(currentTask.next == false) then
			SetTutorialCompleted();
			finishTutorial = true;
		else
			currentTask = _G[currentTask.next];
		end
	end	
end

return TaskScript;
