--================ Danh Sách Các qStep Q3 =================--

require "questui/vl_quest_introwindow_ui"
require "questui/vl_quest_popup_ui"
require "questui/vl_quest_markers"

VLQS = VLQS or {}
VLQS.Q3Steps = VLQS.Q3Steps or {}

local MarkerList_Add = VLF.MarkerList_Add
local SetNPCStateInQueueSpawnList = VLZB.SetNPCStateInQueueSpawnList
local DoSpawnItemQuestOnSquare = VLIT.DoSpawnItemQuestOnSquare
local IsPlayerInArea = VLPL.IsPlayerInArea
local GetQuestGlobalModData = VLGMD.GetQuestGlobalModData
local GetNPCsByVarAndQStep = VLZB.GetNPCsByVarAndQStep
local GetZombieOnlineIDorUID = VLZB.GetZombieOnlineIDorUID
local UpdateNPCToNextQStep = VLZB.UpdateNPCToNextQStep
local IsPlayerCanSeeNPC = VLPL.IsPlayerCanSeeNPC
local RemoveNPCQuest = VLZB.RemoveNPCQuest
local GetPlayerGlobalModData = VLGMD.GetPlayerGlobalModData
local DoQuestList_AddItem = VLF.DoQuestList_AddItem
local UpdateQuestStepAndNext = VLF.UpdateQuestStepAndNext
local SetPlayerGMD_Q3 = VLGMD.SetPlayerGMD_Q3
local RemoveAndRespawnNPC = VLZB.RemoveAndRespawnNPC
local DeactiveNPCSpawnedData = VLZB.DeactiveNPCSpawnedData
local ResetData_EndQ3 = VLF.ResetData_EndQ3
local SetNPCDead = VLF.SetNPCDead
local IsQuest3Completing = VLF.IsQuest3Completing
local GetPlayerGlobalModDataQ3 = VLGMD.GetPlayerGlobalModDataQ3
local GetItemInContainerRecursive = VLIT.GetItemInContainerRecursive
local AddNPCZombified = VLZB.AddNPCZombified
local AddNPCCorpse = VLZB.AddNPCCorpse
local UpdateNPC = VLZB.UpdateNPC
local RemoveItemFromContainer = VLIT.RemoveItemFromContainer

local qtype = getText("IGUI_VLQ_SubQuest")
local questMarker = getTexture("media/textures/questmarker8_48.png")
local thisSession = {}
--tọa độ nhà Bob and Kate
local hX, hY = 10749, 9413


function VLF.WaitTimePassQ3()
	if IsQuest3Completing() then 
		Events.EveryHours.Remove(VLF.WaitTimePassQ3)
	end
	local player = getPlayer()
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, nil)
	local nowHour = getGameTime():getWorldAgeHours()
	local qStep, hourToWait
	
	if playerGMD_Quest_Q3.q3s020 and playerGMD_Quest_Q3.q3s020.active == true then--chờ 1 ngày	
		qStep, hourToWait = "q3s020", 24	--FIXME: 24
	elseif playerGMD_Quest_Q3.q3s018 and playerGMD_Quest_Q3.q3s018.active == true then--chờ 14 ngày	
		qStep, hourToWait = "q3s018", 336	--336
	elseif playerGMD_Quest_Q3.q3s016 and playerGMD_Quest_Q3.q3s016.active == true then--chờ 12h	
		qStep, hourToWait = "q3s016", 12	--12
	elseif playerGMD_Quest_Q3.q3s014 and playerGMD_Quest_Q3.q3s014.active == true then--chờ 12h	
		qStep, hourToWait = "q3s014", 12	--12
	elseif playerGMD_Quest_Q3.q3s012 and playerGMD_Quest_Q3.q3s012.active == true then--chờ 72h	
		qStep, hourToWait = "q3s012", 72	--72
	elseif playerGMD_Quest_Q3.q3s009 and playerGMD_Quest_Q3.q3s009.active == true then--chờ 72h	
		qStep, hourToWait = "q3s009", 72	--72
	elseif playerGMD_Quest_Q3.q3s006 and playerGMD_Quest_Q3.q3s006.active == true then--chờ 72h	
		qStep, hourToWait = "q3s006", 72	--72
	end
	
	if qStep and hourToWait then
		--print("Client-----------WaitTimePassQ3= "..tostring(nowHour).." - "..tostring(playerGMD_Quest_Q3[qStep].startHourWait))
		if not playerGMD_Quest_Q3[qStep].startHourWait then
			SetPlayerGMD_Q3(player, qStep, "startHourWait", nowHour)
		end
		if (nowHour - playerGMD_Quest_Q3[qStep].startHourWait) >= hourToWait then
			SetPlayerGMD_Q3(player, qStep, "waitHourPassed", true)
			SetPlayerGMD_Q3(player, qStep, "startHourWait", nil)
			Events.EveryHours.Remove(VLF.WaitTimePassQ3)
			return
		end
	end
end
local WaitTimePassQ3 = VLF.WaitTimePassQ3
--Events.EveryHours.Remove(WaitTimePassQ3)
--Events.EveryHours.Add(WaitTimePassQ3)

--q3s001 --Kích hoạt quest khi ở gần ngôi nhà của Bob and Kate
VLQS.Q3Steps["q3s001"] = function(player)
	local qStep, qStep_Next = "q3s001", "q3s002"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	--Vào trong nhà nhìn thấy Bob và Kate
	if IsPlayerInArea(player, 10737, 9402, 10761, 9424, 1) then
		if IsPlayerCanSeeNPC(player, nil, "npcQuestIdle", qStep) then
			UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		end
	end
end

--q3s002 --Nói chuyện với người đàn ông trung niên  
VLQS.Q3Steps["q3s002"] = function(player)
	local qStep, qStep_Next = "q3s002", "q3s003"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	
	if playerGMD_Quest_Q3["q3s001"]["given200Hunger"] == true then
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		SetPlayerGMD_Q3(player, qStep, "given200Hunger", nil)
	end
end

--q3s003 --Bob và Kate
VLQS.Q3Steps["q3s003"] = function(player)
	local qStep, qStep_Next = "q3s003", "q3s004"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s004 --Giúp Bob tìm thuốc cho Kate	--6 Base.Antibiotics, 1 Base.Coldpack, 1 Base.VLParacetamol (painkiller icon, đổi màu)
VLQS.Q3Steps["q3s004"] = function(player)
	local qStep, qStep_Next = "q3s004", "q3s005"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s005 --Giúp Bob tìm thực phẩm cho Kate	--Base.Rice, Base.Sugar, Base.SugarBrown, Base.SugarCubes, Base.SugarPacket, Base.Salt
VLQS.Q3Steps["q3s005"] = function(player)
	local qStep, qStep_Next = "q3s005", "q3s006"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s006 --Chờ đợi tiến triển tình trạng sức khỏe của Kate - chờ 3 ngày
VLQS.Q3Steps["q3s006"] = function(player)
	local qStep, qStep_Next = "q3s006", "q3s007"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent6"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent6"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		--Bob biến thành zombie sau 1 ngày nếu không đưa thức ăn cho Bob
		if not playerGMD_Quest_Q3[qStep]["given500RHungerForBob"] then
			SetPlayerGMD_Q3(player, qStep, "startHour_BobZombified", getGameTime():getWorldAgeHours())
			SetPlayerGMD_Q3(player, qStep, "countHour_BobZombified", 24)
		end
		
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
	end
end

--q3s007 --Hỏi thăm tình trạng sức khỏe của Kate
VLQS.Q3Steps["q3s007"] = function(player)
	local qStep, qStep_Next = "q3s007", "q3s008"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s008 --Tìm thuốc men và thực phẩm cho Kate
VLQS.Q3Steps["q3s008"] = function(player)
	local qStep, qStep_Next = "q3s008", "q3s009"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s009 --Chờ đợi tiến triển tình trạng sức khỏe của Kate - chờ 3 ngày
VLQS.Q3Steps["q3s009"] = function(player)
	local qStep, qStep_Next = "q3s009", "q3s010"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent9"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent9"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		
		--update NPC Kate
		local zNPCList = GetNPCsByVarAndQStep("npcQuestSleepOnBed", "q3s001_2")
		if zNPCList and zNPCList[1] then
			UpdateNPCToNextQStep(zNPCList[1], "q3s012")
			local zID = GetZombieOnlineIDorUID(zNPCList[1])
			if zID then
				DeactiveNPCSpawnedData("q3s001_2", zID)
			end
		end
		
		SetNPCStateInQueueSpawnList({qStep = "q3s001_2", active = nil})
		SetNPCStateInQueueSpawnList({qStep = "q3s012", active = true})
	end
end

--q3s010 --Hỏi thăm tình trạng sức khỏe của Kate
VLQS.Q3Steps["q3s010"] = function(player)
	local qStep, qStep_Next = "q3s010", "q3s011"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s011 --Tìm thuốc men và thực phẩm cho Kate
VLQS.Q3Steps["q3s011"] = function(player)
	local qStep, qStep_Next = "q3s011", "q3s012"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s012 --Chờ đợi tiến triển tình trạng sức khỏe của Kate - chờ 3 ngày
VLQS.Q3Steps["q3s012"] = function(player)
	local qStep, qStep_Next = "q3s012", "q3s013"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent12"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent12"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		
		--Bob
		UpdateNPC("npcQuestIdle", "q3s011", "q3s013")
	end
end

--q3s013 --Đến xem tình trạng của Bob và Kate
VLQS.Q3Steps["q3s013"] = function(player)
	local qStep, qStep_Next = "q3s013", "q3s014"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

	
--q3s014 --Chờ đợi quyết định của Bob --12 tiếng --> Kết Cục 1 : q3s101
VLQS.Q3Steps["q3s014"] = function(player)
	local qStep = "q3s014"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent14"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent14"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		--tiếng súng, Bob chết
		local isBobDead = SetNPCDead("npcQuestIdle", "q3s013", "pistol", nil, nil, nil)
		if isBobDead == true then
			SetPlayerGMD_Q3(player, "q3s101", "corpseAdded_Bob", true)
		end
		
		--Kate chết		
		local npcData = VLF.npcData["q3s012"]
		local isKateDead = SetNPCDead("npcQuestSleepOnBed", "q3s012", nil, npcData.startX + 1.5, npcData.startY + 2.5, npcData.startZ)
		if isKateDead == true then
			SetPlayerGMD_Q3(player, "q3s101", "corpseAdded_Kate", true)
		end
		
		--clear data
		ResetData_EndQ3(player)
		
		if not IsQuest3Completing() then
			--set ModData
			SetPlayerGMD_Q3(player, "q3s100", "quest3End1", true)
			
			--update qStep kết thúc 1
			UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s100", true)
		end

	end
end

local function DoInjectAntiserum(player, zNPC, qStep)
	local container = player:getInventory()
	local antiserum = GetItemInContainerRecursive(container, nil, "Base.VLAntiserum")
	if antiserum then
		local emptySyringe = GetItemInContainerRecursive(container, nil, "Base.VLEmptySyringe")
		if emptySyringe then
			local square = player:getCurrentSquare()
			local sq = zNPC:getSquare()
			if square and sq then
				local adjacent = AdjacentFreeTileFinder.Find(sq, player)
				if adjacent then
					ISTimedActionQueue.add(ISWalkToTimedAction:new(player, adjacent))
					ISTimedActionQueue.add(VL_ISDoAction:new(player, zNPC, 500, qStep, "InjectAntiserumForKate", true))
				else
					HaloTextHelper.addBadText(player, getText("IGUI_VLQ_Notification_NotHaveFreeSquare"))
				end
			end

		else
			HaloTextHelper.addBadText(player, getText("IGUI_VLQ_Notification_NotEnoughItemsQuest"))
		end
	else
		HaloTextHelper.addBadText(player, getText("IGUI_VLQ_Notification_NotEnoughItemsQuest"))
	end
end

--Thêm menu Tiêm antiserum cho Kate
local function AddContextMenu_InjectAntiserum(playerIndex, context, worldobjects, test)
	local player = getSpecificPlayer(playerIndex)
    if not player then return end
    local mx, my = ISCoordConversion.ToWorld(getMouseXScaled(), getMouseYScaled(), player:getZ())
    local x, y, z = math.floor(mx), math.floor(my), player:getZ()
    --local zNPC = square:getZombie()
    for _x = x, x + 1 do
        for _y = y, y + 1 do
            local square = getCell():getGridSquare(_x, _y, z)
            if square then
                local objs = square:getMovingObjects()
				if objs and objs:size() > 0 then
					for i = 0, objs:size() - 1 do
						local zNPC = objs:get(i)
						if zNPC and zNPC:isZombie() then
							local questMD = GetQuestGlobalModData(nil)
							local zID = GetZombieOnlineIDorUID(zNPC)
							local qStep = questMD.NPCqStep[zID]
							if qStep and qStep == "q3s012" and zNPC:getVariableBoolean(VLF.npcData[qStep].variable)  then
								local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
								if not playerGMD_Quest_Q3["q3s015"]["isInjectedAntiserumForKate"] then
									context:addOption(getText("IGUI_VLQ_InjectAntiserum", VLF.npcData[qStep].name), player, DoInjectAntiserum, zNPC, qStep)
								end
							end
						end
					end
				end
			end
		end
	end
end
--Events.OnPreFillWorldObjectContextMenu.Remove(AddContextMenu_InjectAntiserum)
--Events.OnPreFillWorldObjectContextMenu.Add(AddContextMenu_InjectAntiserum)

--q3s015 --cho Kate sử dụng antiserum	--nhảy từ q3s013 	--lựa chọn: VLINTER.InteractionButton3["q3s013"]
VLQS.Q3Steps["q3s015"] = function(player)
	local qStep, qStep_Next = "q3s015", "q3s016"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})

	--Thêm menu tiêm antiserum cho Kate
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not playerGMD_Quest_Q3[qStep]["isInjectedAntiserumForKate"] then
		if not thisSession["addEvent15"] then
			Events.OnPreFillWorldObjectContextMenu.Remove(AddContextMenu_InjectAntiserum)
			Events.OnPreFillWorldObjectContextMenu.Add(AddContextMenu_InjectAntiserum)
			thisSession["addEvent15"] = true
		end
	end
	
	--Nếu đã tiêm cho Kate
	if VLF.var.doAction["InjectAntiserumForKate"] == true then
		local container = player:getInventory()
		RemoveItemFromContainer(container, nil, "Base.VLAntiserum", 1)
		RemoveItemFromContainer(container, nil, "Base.VLEmptySyringe", 1)
		
		SetPlayerGMD_Q3(player, qStep, "isInjectedAntiserumForKate", true)
		Events.OnPreFillWorldObjectContextMenu.Remove(AddContextMenu_InjectAntiserum)
		
		--update qStep
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		
		--Kate
		UpdateNPC("npcQuestSleepOnBed", "q3s012", "q3s016")

		VLF.var.doAction["InjectAntiserumForKate"] = nil
	end
end

--q3s016 --Chờ tác dụng của Antiserum	--12 tiếng
VLQS.Q3Steps["q3s016"] = function(player)
	local qStep, qStep_Next = "q3s016", "q3s017"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent16"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent16"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		
		--Bob
		UpdateNPC("npcQuestIdle", "q3s013", "q3s017")
	end
end

--q3s017 --Kiểm tra tình trạng của Kate sau khi tiêm
VLQS.Q3Steps["q3s017"] = function(player)
	local qStep, qStep_Next = "q3s017", "q3s018"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s018 --chờ Kate hồi phục --2 tuần
VLQS.Q3Steps["q3s018"] = function(player)
	local qStep, qStep_Next = "q3s018", "q3s019"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent18"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent18"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", qStep_Next, true)
		
		--Kate
		local zNPCList = GetNPCsByVarAndQStep("npcQuestSleepOnBed", "q3s016")
		if zNPCList and zNPCList[1] then
			RemoveAndRespawnNPC(zNPCList[1], "q3s016", "q3s019")
		else
			SetNPCStateInQueueSpawnList({qStep = "q3s016", active = nil})
			SetNPCStateInQueueSpawnList({qStep = "q3s019", active = true})
		end
	end
end

--q3s019 --Đến xem tình trạng của Bob và Kate	--> Kết Cục 3 : q3s103
VLQS.Q3Steps["q3s019"] = function(player)
	local qStep = "q3s019"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
end

--q3s020 --Tiếp tục chờ đợi --1 ngày	--nhảy từ q3s013 --lựa chọn: VLINTER.InteractionButton5["q3s013"]
VLQS.Q3Steps["q3s020"] = function(player)
	local qStep, qStep_Next = "q3s020", "q3s021"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not thisSession["addEvent20"] then
		if not playerGMD_Quest_Q3[qStep].waitHourPassed then
			Events.EveryHours.Remove(WaitTimePassQ3)
			Events.EveryHours.Add(WaitTimePassQ3)
		end
		thisSession["addEvent20"] = true
	end
	
	if playerGMD_Quest_Q3[qStep].waitHourPassed == true then
		--remove Bob
		local zNPCList = GetNPCsByVarAndQStep("npcQuestIdle", "q3s013")
		if zNPCList and zNPCList[1] then
			RemoveNPCQuest(zNPCList[1])
		end
		--remove Kate
		local zNPCList2 = GetNPCsByVarAndQStep("npcQuestSleepOnBed", "q3s012")
		if zNPCList2 and zNPCList2[1] then
			RemoveNPCQuest(zNPCList2[1])
		end
		
		--Xóa queue
		SetNPCStateInQueueSpawnList({qStep = "q3s012", active = nil})
		SetNPCStateInQueueSpawnList({qStep = "q3s013", active = nil})
		
		--clear data
		ResetData_EndQ3(player)
		
		if not IsQuest3Completing() then
			--set ModData
			SetPlayerGMD_Q3(player, "q3s100", "quest3End4", true)

			--update qStep kết thúc 3
			UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s100", true)
		end
	end
end

--=================================================================--

--q3s100 --Khi đến nhà Bob and Kate sẽ cập nhật kết thúc Q3
VLQS.Q3Steps["q3s100"] = function(player)
	local qStep = "q3s100"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = false, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	if IsPlayerInArea(player, 10709, 9373, 10789, 9453, nil) then
		if not thisSession["addEvent100"] then
			--remove zombie nếu còn sót trên phòng BnK
			sendClientCommand(player, "NPCQuestCM", "RemoveZombiesInAreaExceptNPC", {x1 = 10744, y1 = 9409, x2 = 10753, y2 = 9416, z = 1})
			thisSession["addEvent100"] = true
		end
	
		--Khi vào trong phạm vi nhà của Bob và Kate thì mới update kết thúc quest
		if IsPlayerInArea(player, 10744, 9409, 10751, 9416) then
			local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
			
			if playerGMD_Quest_Q3[qStep]["quest3End1"] == true then
				--update qStep kết thúc 1
				UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s101", true)
				
			elseif playerGMD_Quest_Q3[qStep]["quest3End2"] == true then	
				--update qStep kết thúc 2
				UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s102", true)
			
			elseif playerGMD_Quest_Q3[qStep]["quest3End3"] == true then	
				--update qStep kết thúc 3
				UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s103", true)
			
			elseif playerGMD_Quest_Q3[qStep]["quest3End4"] == true then	
				--update qStep kết thúc 4
				UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s104", true)
				
			elseif playerGMD_Quest_Q3[qStep]["quest3End5"] == true then	
				--update qStep kết thúc 5
				UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s105", true)
								
			elseif playerGMD_Quest_Q3[qStep]["quest3End6"] == true then
				--update qStep --Kết cục 6
				UpdateQuestStepAndNext(player, 3, false, false, true, qStep, "done", "q3s106", true)
			end
		end
	else
		if thisSession["addEvent100"] ~= nil then thisSession["addEvent100"] = nil end
	end
end
	

--q3s101 --Kết Cục 1	--Bob tự tay giết Kate khi chưa bị biến đổi, Bob tự sát
VLQS.Q3Steps["q3s101"] = function(player)
	local qStep = "q3s101"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = true, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	--MarkerList_Add({qStep = qStep, x = hX, y = hY, texture = questMarker})
	
	--Add xác Bob và Kate nếu không thực hiện hành động tự sát trước đó
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not playerGMD_Quest_Q3["q3s014"]["corpseAdded_Bob"] or not playerGMD_Quest_Q3["q3s014"]["corpseAdded_Kate"] then
		if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
			if not thisSession["addEvent101"] then
				local questMD = GetQuestGlobalModData(nil)
				
				if not playerGMD_Quest_Q3[qStep]["corpseAdded_Bob"] then
					AddNPCCorpse("q3s105", false, nil, nil, nil, 2)
					SetPlayerGMD_Q3(player, qStep, "corpseAdded_Bob", true)
				end
				if not playerGMD_Quest_Q3[qStep]["corpseAdded_Kate"] then
					AddNPCCorpse("q3s105_2", false, 10746, 9413, 1, 0)
					SetPlayerGMD_Q3(player, qStep, "corpseAdded_Kate", true)
				end
				
				thisSession["addEvent101"] = true
			end
		end
	end
	
	if not playerGMD_Quest_Q3[qStep]["noteAdded"] then
		if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
			if not thisSession["addEvent101_2"] then
				--thêm bức thư của Bob trên giường
				DoSpawnItemQuestOnSquare({qStep = qStep, x = 10745, y = 9412, z = 1, itemFullType = "Base.GenericMail", range = 1, isNote = true, noteStep = 1, qID = 3, iModData = "isKeyQuest1", totalPage = 2})
				local gun = DoSpawnItemQuestOnSquare({qStep = qStep, x = 10746, y = 9412, z = 1, itemFullType = "Base.Revolver_Short", range = 1, iModData = "isKeyQuest2"})
				if gun then
					gun:setCurrentAmmoCount(3)
				end
				
				SetPlayerGMD_Q3(player, qStep, "noteAdded", true)
				SetPlayerGMD_Q3(player, "q3s100", "quest3Ended", true)
				
				thisSession["addEvent101_2"] = true
			end
		end
	end
	
end
	
--q3s102 --Kết Cục 2	--Kate tự sát, Bob tự sát theo
VLQS.Q3Steps["q3s102"] = function(player)
	local qStep = "q3s102"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = true, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})

	--Add xác Bob và Kate nếu không thực hiện hành động tự sát trước đó (Player không ở gần)
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	if not playerGMD_Quest_Q3["q3s012"]["corpseAdded_Bob"] or not playerGMD_Quest_Q3["q3s012"]["corpseAdded_Kate"] then
		if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
			if not thisSession["addEvent102"] then
				local questMD = GetQuestGlobalModData(nil)
				--add xác Bob
				if not playerGMD_Quest_Q3["q3s012"]["corpseAdded_Bob"] then
					AddNPCCorpse("q3s105", false, nil, nil, nil, 2)
					SetPlayerGMD_Q3(player, "q3s012", "corpseAdded_Bob", true)
				end
				--add xác Kate
				if not playerGMD_Quest_Q3["q3s012"]["corpseAdded_Kate"] then
					AddNPCCorpse("q3s105_2", false, 10746, 9413, 1, 0)
					SetPlayerGMD_Q3(player, "q3s012", "corpseAdded_Kate", true)
				end
				
				thisSession["addEvent102"] = true
			end
		end
	end
	
	if not playerGMD_Quest_Q3[qStep]["noteAdded"] then
		if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
			if not thisSession["addEvent102_2"] then
				--add thư Kate gửi Bob trước khi tự sát
				DoSpawnItemQuestOnSquare({qStep = qStep, x = 10745, y = 9412, z = 1, itemFullType = "Base.GenericMail", range = 1, isNote = true, noteStep = 1, qID = 3, iModData = "isKeyQuest1", totalPage = 2})
				local gun = DoSpawnItemQuestOnSquare({qStep = qStep, x = 10746, y = 9413, z = 1, itemFullType = "Base.Revolver_Short", range = 1, iModData = "isKeyQuest2"})
				if gun then
					gun:setCurrentAmmoCount(3)
				end
				SetPlayerGMD_Q3(player, qStep, "noteAdded", true)
				SetPlayerGMD_Q3(player, "q3s100", "quest3Ended", true)
				
				--clear data
				SetPlayerGMD_Q3(player, "q3s012", "startTime_KateSuicide", nil)
				SetPlayerGMD_Q3(player, "q3s012", "sayKateIsDangerousToBob1", nil)
				--SetPlayerGMD_Q3(player, "q3s012", "sayKateIsDangerousToBob2", nil)
				
				thisSession["addEvent102_2"] = true
			end
		end
	end
end
	
--q3s103 --Kết Cục 3	--Kate và Bob còn sống
VLQS.Q3Steps["q3s103"] = function(player)
	local qStep = "q3s103"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = true, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, "q3s100")
	
	if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
		if not thisSession["addEvent103"] then
			--xóa zombie tầng 2
			sendClientCommand(player, "NPCQuestCM", "RemoveZombiesInAreaExceptNPC", {x1 = 10744, y1 = 9409, x2 = 10753, y2 = 9416, z = 1})
			thisSession["addEvent103"] = true
		end
	
		if not playerGMD_Quest_Q3["q3s100"]["quest3Ended"] then
			SetPlayerGMD_Q3(player, "q3s100", "quest3Ended", true)
		end
	
	else
		if thisSession["addEvent103"] ~= nil then thisSession["addEvent103"] = nil end
	end
end
	
--q3s104 --Kết Cục 4	--Bob tự tay giết Kate khi bị biến đổi thành zombie, Bob hóa điên bế Kate rời khỏi
VLQS.Q3Steps["q3s104"] = function(player)
	local qStep = "q3s104"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = true, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, "q3s100")
	if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
		if not playerGMD_Quest_Q3["q3s100"]["quest3Ended"] then
			local square = getCell():getGridSquare(10746, 9411, 1)
			if square then
				square:splatBlood(2, 1)
			end

			SetPlayerGMD_Q3(player, "q3s100", "quest3Ended", true)
		end
	end
end

--q3s105 --Kết Cục 5	--Hết thời gian giới hạn cho Quest. --> Kate biến zombie, cắn Bob, Bob cũng thành zombie
VLQS.Q3Steps["q3s105"] = function(player)
	local qStep = "q3s105"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = true, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	
	--Spawn zombie Bob và Kate. --Nếu quest kết thúc 5, Bob và Kate thành zombie, đã được xóa nếu đang spawned
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)
	
	if not playerGMD_Quest_Q3[qStep]["addedZombieBobKate"] then
		if IsPlayerInArea(player, 10709, 9373, 10789, 9453, nil) then
			--zombie Bob
			AddNPCZombified(qStep, false, nil, nil, nil)	
		
			--zombie Kate
			AddNPCZombified("q3s105_2", true, nil, nil, nil)	

			SetPlayerGMD_Q3(player, qStep, "addedZombieBobKate", true)
		end
	end
	
	if not playerGMD_Quest_Q3[qStep]["noteAdded"] then
		if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
			--add thư Bob trước khi biến thành zombie
			DoSpawnItemQuestOnSquare({qStep = qStep, x = 10745, y = 9411, z = 1, itemFullType = "Base.Journal", range = 1, isNote = true, noteStep = 1, qID = 3, iModData = "isKeyQuest1", totalPage = 3})

			SetPlayerGMD_Q3(player, qStep, "noteAdded", true)
			SetPlayerGMD_Q3(player, "q3s100", "quest3Ended", true)
		end
	end
end
	
--q3s106 --Kết Cục 6	--Không đưa thực phẩm cho Bob (giảm 500 hunger) --> Bob biến thành zombie, cắn Kate, Kate cũng thành zombie
VLQS.Q3Steps["q3s106"] = function(player)
	local qStep = "q3s106"
	DoQuestList_AddItem({id = 3, qStep = qStep, isDone = true, qtype = qtype, isCollection = false, currentCollection = nil, requestCollection = nil})
	
	--Spawn zombie Bob và Kate. --Nếu quest kết thúc 6, Bob và Kate thành zombie, đã được xóa nếu đang spawned
	local playerGMD_Quest_Q3 = GetPlayerGlobalModDataQ3(player, qStep)

	if not playerGMD_Quest_Q3[qStep]["addedZombieBobKate"] then
		if IsPlayerInArea(player, 10709, 9373, 10789, 9453, nil) then
			AddNPCZombified("q3s105", false, nil, nil, nil)
			AddNPCZombified("q3s105_2", true, nil, nil, nil)

			SetPlayerGMD_Q3(player, qStep, "addedZombieBobKate", true)
		end
	end
	
	if not playerGMD_Quest_Q3[qStep]["noteAdded"] then
		if IsPlayerInArea(player, 10737, 9402, 10761, 9424) then
			--add thư Bob trước khi biến thành zombie
			DoSpawnItemQuestOnSquare({qStep = qStep, x = 10745, y = 9412, z = 1, itemFullType = "Base.Journal", range = 1, isNote = true, noteStep = 1, qID = 3, iModData = "isKeyQuest1", totalPage = 3})
			local gun = DoSpawnItemQuestOnSquare({qStep = qStep, x = 10745, y = 9411, z = 1, itemFullType = "Base.Revolver_Short", range = 1, iModData = "isKeyQuest2"})
			if gun then
				gun:setCurrentAmmoCount(5)
			end
			SetPlayerGMD_Q3(player, qStep, "noteAdded", true)
			SetPlayerGMD_Q3(player, "q3s100", "quest3Ended", true)
			
			--clear data
			SetPlayerGMD_Q3(player, "q3s006", "startHour_BobZombified", nil)
			SetPlayerGMD_Q3(player, "q3s006", "countHour_BobZombified", nil)
		end
	end
end
	
