--================ Các Function Khi Nói Chuyện Với NPC =================--

require "mainquest1/vl_quest1_questupdate"
require "questui/vl_quest_introwindow_ui"
require "questui/vl_quest_popup_ui"
require "npc/vl_quest_npc_client"

VLF.TalkToNPC = VLF.TalkToNPC or {}

assert(VLPL, "Stack trace: Missing dependency: VLPL - vl_quest1_talktonpcs.lua")
assert(VLZB, "Stack trace: Missing dependency: VLZB - vl_quest1_talktonpcs.lua")
assert(VLIT, "Stack trace: Missing dependency: VLIT - vl_quest1_talktonpcs.lua")

local DoQuestStepActive = VLF.DoQuestStepActive
local UpdateNPCToNextQStep = VLZB.UpdateNPCToNextQStep
local GetPlayerGlobalModDataQ1 = VLGMD.GetPlayerGlobalModDataQ1
local ActiveQuestIntro_Centre = VLF.ActiveQuestIntro_Centre
local ActiveQuestIntro_Free = VLF.ActiveQuestIntro_Free
local QuestUpdatedPopIn = VLF.QuestUpdatedPopIn
local GetFoodInContainer = VLIT.GetFoodInContainer
local GetWaterSourcesInCont = VLIT.GetWaterSourcesInCont
local GetItemsInContainer = VLIT.GetItemsInContainer
local GetAlcoholsInContainer = VLIT.GetAlcoholsInContainer
local GetZombieOnlineIDorUID = VLZB.GetZombieOnlineIDorUID
local GetAllnpcIDSpawning = VLZB.GetAllnpcIDSpawning
local GetZombiesAroundSquare = VLZB.GetZombiesAroundSquare
local IsPlayerInArea = VLPL.IsPlayerInArea
local NPCTurnToZombie = VLF.NPCTurnToZombie
local DelayFunction = VLF.DelayFunction
local RemoveNPCQuest = VLZB.RemoveNPCQuest
local SetNPCStateInQueueSpawnList = VLZB.SetNPCStateInQueueSpawnList
local IsMultiplayerMode = VLF.IsMultiplayerMode
local PlaySound = VLF.PlaySound
local AddItemToContainer = VLIT.AddItemToContainer
local RemoveItemFromContainer = VLIT.RemoveItemFromContainer
local GetQuestGlobalModData = VLGMD.GetQuestGlobalModData
local IsNPC = VLZB.IsNPC
local SpawnNPCOnClient = VLZB.SpawnNPCOnClient
local GetNPCsByVarAndQStep = VLZB.GetNPCsByVarAndQStep
local GetHungerFromFood = VLIT.GetHungerFromFood
local SetPlayerGMD_Data = VLGMD.SetPlayerGMD_Data
local AddReputationPoint = VLGMD.AddReputationPoint

--qStep_Next = qStep + 1
local function CalculateQStepNext(qStep)
	local qStep_Next
	local nextNum = tonumber(string.sub(qStep, -3)) + 1
	if nextNum then
		if nextNum < 10 then qStep_Next = "q1s".."00"..tostring(nextNum)
		elseif nextNum < 100 then qStep_Next = "q1s".."0"..tostring(nextNum)
		else qStep_Next = "q1s"..tostring(nextNum) end
	end
	return qStep_Next
end

--NPC phản hồi khi đang chờ tiến sĩ nghiên cứu
local function WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
	--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
	if playerGMD_Quest_Q1[qStep].active == true then
		if not playerGMD_Quest_Q1[qStep].waitHourPassed then
			ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_q1s019"), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
		end
	end
	
	--cập nhật qStep cho NPC để không phải load lại NPC
	local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
	--local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep_Next)
	if playerGMD_Quest_Q1[qStep_Next] and playerGMD_Quest_Q1[qStep_Next].active == true then
		if playerGMD_Quest_Q1[qStep].waitHourPassed == true then
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			if VLF.TalkToNPC[qStep_Next] then
				VLF.TalkToNPC[qStep_Next](player, square, zNPC, qStep_Next)
			end
			--ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification2_q1s019"), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
		end
	end
end

--Cập nhật ModData cho nhiệm vụ Collect item quest và trả về số lượng item quest đã collect
local function UpdateGiveItemQuestData(player, qStep, dataName, requestNum, giveNum)
	local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
	local collectedKey = "collected" .. dataName
	local requestKey   = "request" .. dataName
	
	if not playerGMD_Quest_Q1[qStep][collectedKey] then
		playerGMD_Quest_Q1[qStep][collectedKey] = 0
	end
	if not playerGMD_Quest_Q1[qStep][requestKey] then
		playerGMD_Quest_Q1[qStep][requestKey] = requestNum
	end
		
	playerGMD_Quest_Q1[qStep][collectedKey] = playerGMD_Quest_Q1[qStep][collectedKey] + giveNum
	
	if playerGMD_Quest_Q1[qStep][collectedKey] >= playerGMD_Quest_Q1[qStep][requestKey] then
		playerGMD_Quest_Q1[qStep][collectedKey] = nil
		playerGMD_Quest_Q1[qStep][requestKey] = nil
		sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = nil, request = nil})
		return "done"
	end
	return playerGMD_Quest_Q1[qStep][collectedKey]
end
 
--Lấy toàn bộ food của player
local function GetAllFoodFromPlayer(player)
    local foodList = {}
    -- Inventory chính
    local inv = player:getInventory()
    GetFoodInContainer(inv, foodList)
    return foodList
end

--Đưa food cho NPC
local function GiveFoodsForQuest(player, qStep, args) --GiveFoodsForQuest(player, qStep, {requestNum = 1400})
	local foodList = GetAllFoodFromPlayer(player)
	if foodList then
		local givenItem = nil
		local dataName = "RHunger"
		local requestNum = args.requestNum

		for i = #foodList, 1, -1 do
			local food = foodList[i]
			if food then
				if not food:isPoison() and not food:isRotten() and not food:isbDangerousUncooked() then
					local rHunger = 0					
					--rHunger = math.abs(math.floor(food:getHungChange() * 100)) --kết quả: số nguyên dương làm tròn. getHungChange có giá trị -0.xxx...
					rHunger = math.abs(GetHungerFromFood(food)) --kết quả: số nguyên dương làm tròn. getHungChange có giá trị -0.xxx...
					
					if rHunger == 0 then
						local calo = food:getCalories()
						if calo then rHunger = math.floor(food:getCalories() / 15) end					
					end
					
					if rHunger > 0 then
						RemoveItemFromContainer(player:getInventory(), food, nil, 1)
						givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, rHunger)
						if givenItem == "done" then
							return "done"
						end
					end
				end
			end
		end
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Lấy toàn bộ waterSource của player
local function GetAllWaterSourceFromPlayer(player)
    local waterSourceList = {}
    -- Inventory chính
    local inv = player:getInventory()
    GetWaterSourcesInCont(inv, waterSourceList)
    return waterSourceList
end
--Đưa water cho NPC
local function GiveWaterForQuest(player, qStep, args) --GiveWaterForQuest(player, qStep, {requestNum = 30})
	local waterSourceList = GetAllWaterSourceFromPlayer(player)
	if waterSourceList then
		local givenItem = nil
		local dataName = "RThirst"
		local requestNum = args.requestNum

		for i = #waterSourceList, 1, -1 do
			local waterSource = waterSourceList[i]
			if waterSource then
				local fluidCont = waterSource:getFluidContainer()
				if fluidCont then
					--if not fluidCont:isTainted() and (fluidCont:getPrimaryFluid():getFluidTypeString() == "Water" or fluidCont:getPrimaryFluid():getFluidTypeString() == "CarbonatedWater") then
					if (fluidCont:contains(Fluid.Water) or fluidCont:contains(Fluid.CarbonatedWater)) and not fluidCont:isTainted() then
						local rThirst = 0	
						rThirst = math.floor(fluidCont:getAmount() * 10 + 0.5) / 10 --kết quả: làm tròn 0,1 số thập phân. 0.xxx... -> 0.x
						
						if rThirst > 0 then
							RemoveItemFromContainer(player:getInventory(), waterSource, nil, 1)
							givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, rThirst)
							if givenItem == "done" then
								return "done"
							end
						end
					end
				end
			end
		end
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end
 
--Lấy toàn bộ ItemType cụ thể trong túi đồ player
local function GetAllItemFromPlayerByType(player, itemType)
	local itemList = {}
    -- Inventory chính
    local inv = player:getInventory()
	if inv then
		GetItemsInContainer(inv, itemList, itemType)
		return itemList
	end
    return nil
end

--Đưa Blood Sample cho NPC
local function GiveBloodSampleForQuest(player, qStep, args) --GiveBloodSampleForQuest(player, qStep, {requestNum = 15})
	local bloodList = GetAllItemFromPlayerByType(player, "Base.VLZBloodSyringe")

	if bloodList then
		local givenItem = nil
		local dataName = "ZBlood"
		local requestNum = args.requestNum

		for i = #bloodList, 1, -1 do
			local bloodSample = bloodList[i]
			if bloodSample then
				RemoveItemFromContainer(player:getInventory(), bloodSample, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 1)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Đưa Mẫu Vật cho NPC
local function GiveZSpecimenForQuest(player, qStep, args) --GiveZSpecimenForQuest(player, qStep, {requestNum = 15})
	local zSpecimenList = GetAllItemFromPlayerByType(player, "Base.VLZSpecimenContainer")
	if zSpecimenList then
		local givenItem = nil
		local dataName = "ZSpecimen"
		local requestNum = args.requestNum

		for i = #zSpecimenList, 1, -1 do
			local speciment = zSpecimenList[i]
			if speciment then
				RemoveItemFromContainer(player:getInventory(), speciment, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 1)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Đưa Infected Specimen cho NPC
local function GiveInfectedSpecimenForQuest(player, qStep, args) --GiveInfectedSpecimenForQuest(player, qStep, {requestNum = 2})
	local items = GetAllItemFromPlayerByType(player, "Base.VLInfectedSpecimenContainer")
	if items then
		local givenItem = nil
		local dataName = "InfecSpecimen"
		local requestNum = args.requestNum

		for i = #items, 1, -1 do
			local item = items[i]
			if item then
				RemoveItemFromContainer(player:getInventory(), item, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 1)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end


--Lấy toàn bộ Alcohol trong túi đồ player
local function GetAllAlcoholFromPlayer(player)
    local itemList = {}
    -- Inventory chính
    local inv = player:getInventory()
    GetAlcoholsInContainer(inv, itemList)
    return itemList
end

--Đưa Alcohol cho NPC
local function GiveAlcoholsForQuest(player, qStep, args) --GiveAlcoholsForQuest(player, qStep)
	local alcoholList = GetAllAlcoholFromPlayer(player)

	if alcoholList and #alcoholList >= 2 then
		local removeAlcohol = 0

		for i = #alcoholList, 1, -1 do
			local alcohol = alcoholList[i]
			
			if alcohol then
				RemoveItemFromContainer(player:getInventory(), alcohol, nil, 1)
				removeAlcohol = removeAlcohol + 1
				if removeAlcohol >= 2 then
					return "done"
				end
			end
		end
	end
	return nil
end

--Đưa Chuột sống cho NPC
local function GiveMousesForQuest(player, qStep, args) --GiveMousesForQuest(player, qStep, {requestNum = 5})
	local animalList = GetAllItemFromPlayerByType(player, "Base.Animal")
	if animalList then
		local givenItem = nil
		local dataName = "Mouse"
		local requestNum = args.requestNum

		for i = #animalList, 1, -1 do
			local animal = animalList[i]
			if animal and animal:getAnimal() and animal:getAnimal():getAnimalType() and (string.find(animal:getAnimal():getAnimalType(), "rat") or string.find(animal:getAnimal():getAnimalType(), "mouse")) then
				RemoveItemFromContainer(player:getInventory(), animal, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 1)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Đưa Frog còn sống cho NPC
local function GiveFrogsForQuest(player, qStep, args) --GiveFrogsForQuest(player, qStep, {requestNum = 3})
	local frogList = GetAllItemFromPlayerByType(player, "Base.Frog")
	if frogList then
		local givenItem = nil
		local dataName = "Frog"
		local requestNum = args.requestNum

		for i = #frogList, 1, -1 do
			local frog = frogList[i]
			if frog then
				RemoveItemFromContainer(player:getInventory(), frog, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 1)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Đưa Leech còn sống cho NPC
local function GiveLeechsForQuest(player, qStep, args) --GiveLeechsForQuest(player, qStep, {requestNum = 10})
	local leechList = GetAllItemFromPlayerByType(player, "Base.Leech")
	if leechList then
		local givenItem = nil
		local dataName = "Leech"
		local requestNum = args.requestNum

		for i = #leechList, 1, -1 do
			local leech = leechList[i]
			if leech and leech:isbDangerousUncooked() then
				RemoveItemFromContainer(player:getInventory(), leech, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 1)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Spawn zombie cho event quest q1s035
local function SpawnLocalZombiesEventq1s035(player, count, args) --SpawnLocalZombiesEventq1s035(player, 100, {minDist = 10, maxDist = 60, z = 0})
	local x, y = player:getX(), player:getY()
	local z = args.z or 0
	local minDist, maxDist = args.minDist or 1, args.maxDist or 10
	if not x or not y then return false end
	
	local loop = count * 2
	local cnt = 0
	for i = 1, loop do
		--lấy tọa độ x, y cách player tối thiểu minDist
		local _x = ZombRand(2) == 0 and (x + minDist + ZombRand(maxDist - minDist)) or (x - minDist - ZombRand(maxDist - minDist))
		local _y = ZombRand(2) == 0 and (y + minDist + ZombRand(maxDist - minDist)) or (y - minDist - ZombRand(maxDist - minDist))
		local square = getCell():getGridSquare(_x, _y, z)
		
		--print("Client-----square isOutside() ", square:isOutside(), square:isFree(true))
		if square and square:isOutside() and square:isFree(true) then
			addZombiesInOutfit(_x, _y, 0, 1, "Generic03", 50)
			cnt = cnt + 1
			if cnt >= count then return true end
		end
	end
	return false
end			
		
--Kiểm tra có zombie tồn tại trong bán kính maxDist, và không phải NPC
local function IsZombieExistAroundSquare(square, maxDist, sameZ) --IsZombieExistAroundSquare(square, maxDist, false)
	--print("Client-----IsZombieExistAroundSquare-1  ", square, args.maxDist)
	local zombieList = GetZombiesAroundSquare(square, maxDist, sameZ)
	if zombieList then
		if #zombieList > 0 then
			for _, zombie in ipairs(zombieList) do
				if not IsNPC(zombie) then
					return true
				end
			end
		end
		return false
	end
end

--Khi player tại khu vực phòng thí nghiệm thì Kiểm tra quanh 30 ô còn zombie không 
local function EveryTenMinutes_CheckQuestq1s035()
	local player = getPlayer()
	if player then
		if not player:isDead() then
			if IsPlayerInArea(player, 10630, 10394, 10651, 10416, nil) then
				local square = player:getCurrentSquare()
				if square then
					--nếu 30 ô xung quanh không còn zombie thì quest done
					local hasZ = IsZombieExistAroundSquare(square, 30, false)
					--print("Client-----EveryTenMinutes_CheckQuestq1s035  ", hasZ)
					if hasZ == false then
						SetPlayerGMD_Data(player, "event_q1s035", true)
						 
						Events.EveryTenMinutes.Remove(EveryTenMinutes_CheckQuestq1s035)
					end
				end
			end
			
			--add sound thu hút zombie 
			local sq = getCell():getGridSquare(10637, 10405, 0)
			if sq then addSound(sq:getFloor(), sq:getX(), sq:getY(), sq:getZ(), 250, 1000) end
		end
	end
end
--Events.EveryTenMinutes.Remove(EveryTenMinutes_CheckQuestq1s035)
--Events.EveryTenMinutes.Add(EveryTenMinutes_CheckQuestq1s035)


--NÓI CHUYỆN VỚI NPC
VLF.TalkToNPC["q1s003"] = function(player, square, zNPC, qStep) --phó Hiệu Trưởng
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local qStep_Next = CalculateQStepNext(qStep)
			--cập nhật qStep
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true})
		end
	end
end

VLF.TalkToNPC["q1s004"] = function(player, square, zNPC, qStep) --cảnh sát viên
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local qStep_Next = CalculateQStepNext(qStep)
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true})
		end
	end
end

VLF.TalkToNPC["q1s005"] = function(player, square, zNPC, qStep) --Cảnh sát trưởng --NPC biến thành zombie
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			DelayFunction(function()
				NPCTurnToZombie(player, zNPC, qStep, {isPopUp = true})
				--Xóa NPC khỏi danh sách chờ spawn để không spawn lại
				sendClientCommand(player, "NPCQuestCM", "ActiveNPCInQueueSpawnCM", {qStep = qStep, active = nil})			
			end, 120)
		end
	end
end
 
VLF.TalkToNPC["q1s007"] = function(player, square, zNPC, qStep) --Tiến sĩ Dr. Doudna
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local qStep_Next = CalculateQStepNext(qStep)
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 1})
		end
	end
end

VLF.TalkToNPC["q1s008"] = function(player, square, zNPC, qStep) --Trung úy tại Louisville Metro Police Department
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local qStep_Next = CalculateQStepNext(qStep)
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = false, qID = 1})
		end
	end
end

VLF.TalkToNPC["q1s012"] = function(player, square, zNPC, qStep) --Phòng thí nghiệm bí ẩn
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1			
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
		end
	end
end

VLF.TalkToNPC["q1s013"] = function(player, square, zNPC, qStep) --Tìm Dr. Bassler
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DelayFunction(function()
				RemoveNPCQuest(zNPC)
			end, 300)
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			SetNPCStateInQueueSpawnList({qStep = "q1s014_2", active = true}) --Thêm NPC thứ 2
		end
	end
end

VLF.TalkToNPC["q1s014"] = function(player, square, zNPC, qStep) --Trở về phòng thí nghiệm Dr. Venter
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			AddReputationPoint(player, 15)
		end
	end
end

VLF.TalkToNPC["q1s014_2"] = function(player, square, zNPC, qStep) --Dr. Bassler --do nothing
	if qStep then	
		ActiveQuestIntro_Centre(getText("IGUI_VLQ_2_NPCTalk_q1s014_2"), 800, 500, false, "")
	end
end

VLF.TalkToNPC["q1s015"] = function(player, square, zNPC, qStep) --Đồng ý hỗ trợ Dr. Venter
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1			
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
		end
	end
end

VLF.TalkToNPC["q1s016"] = function(player, square, zNPC, qStep) --Yêu cầu tìm Food
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveFoodsForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s017"] = function(player, square, zNPC, qStep) --Yêu cầu tìm Water
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveWaterForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
				
				--nhận 2 Empty Syringe Pack
				local inv = player:getInventory()
				if inv then
					AddItemToContainer(inv, nil, "Base.VLEmptySyringePack", 2)
				end
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s018"] = function(player, square, zNPC, qStep) --Yêu cầu lấy mẫu máu zombie
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveBloodSampleForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})			
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
				Events.OnPreFillWorldObjectContextMenu.Remove(VLF.AddContextMenu_BloodSample)
				AddReputationPoint(player, 45)
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s019"] = function(player, square, zNPC, qStep) --chờ 1 ngày
	if qStep then	
		WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	end
end

VLF.TalkToNPC["q1s020"] = function(player, square, zNPC, qStep) --trở lại phòng thí nghiệm
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			
			--nhận 2 Empty Specimen Container Pack và Scalpel
			local inv = player:getInventory()

			if inv then
				AddItemToContainer(inv, nil, "Base.Scalpel", 1)
				AddItemToContainer(inv, nil, "Base.VLEmptySpecimenContainerPack", 2)
			end
		end
	end
end

VLF.TalkToNPC["q1s021"] = function(player, square, zNPC, qStep) --yêu cầu lấy mẫu vật zombie
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveZSpecimenForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})		
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
				Events.OnPreFillWorldObjectContextMenu.Remove(VLF.AddContextMenu_ZombieSpecimen)
				AddReputationPoint(player, 30)
				
				--NPC đưa phần thưởng Strength-enhancing Serum
				local inv = player:getInventory()
				
				if inv then
					AddItemToContainer(inv, nil, "Base.VLUpgradeStrengthSerum1", 1)
				end
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s022"] = function(player, square, zNPC, qStep) --chờ 1 ngày
	if qStep then
		WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	end
end

VLF.TalkToNPC["q1s023"] = function(player, square, zNPC, qStep) --Uống rượu cùng người bạn vong niên
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveAlcoholsForQuest(player, qStep)	
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s024"] = function(player, square, zNPC, qStep) --chờ 3 ngày
	if qStep and zNPC and player then	
		WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	end
end

VLF.TalkToNPC["q1s025"] = function(player, square, zNPC, qStep) --trở lại phòng thí nghiệm
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
		end
	end
end

VLF.TalkToNPC["q1s026"] = function(player, square, zNPC, qStep) --yêu cầu bắt 5 con chuột
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveMousesForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})	
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s027"] = function(player, square, zNPC, qStep) --yêu cầu bắt 3 con Ếch
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveFrogsForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})	
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s028"] = function(player, square, zNPC, qStep) --yêu cầu bắt 10 con Đỉa
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveLeechsForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
				AddReputationPoint(player, 45)
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s029"] = function(player, square, zNPC, qStep) --chờ 3 ngày
	if qStep and zNPC and player then	
		WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	end
end

VLF.TalkToNPC["q1s030"] = function(player, square, zNPC, qStep) --trở lại phòng thí nghiệm
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
		end
	end
end

VLF.TalkToNPC["q1s031"] = function(player, square, zNPC, qStep) --yêu cầu tìm Food --1500 hunger
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveFoodsForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
				
				--nhận Empty Specimen Container Pack và Scalpel
				local inv = player:getInventory()
				if inv then
					AddItemToContainer(inv, nil, "Base.Scalpel", 1)
					AddItemToContainer(inv, nil, "Base.VLEmptySpecimenContainerPack", 1)
				end
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end				
		end
	end
end

VLF.TalkToNPC["q1s032"] = function(player, square, zNPC, qStep) --yêu cầu lấy mẫu vật người đang bị biến đổi
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			local collection = GiveInfectedSpecimenForQuest(player, qStep, {requestNum = VLF.var.requestCol[qStep]})
			
			if collection == "done" then
				local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
				UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
				AddReputationPoint(player, 70)
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 200, false, "")
			end
		end
	end
end

VLF.TalkToNPC["q1s033"] = function(player, square, zNPC, qStep) --chờ 7 ngày
	if qStep and zNPC and player then	
		WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	end
end

VLF.TalkToNPC["q1s034"] = function(player, square, zNPC, qStep) --trở lại phòng thí nghiệm
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
		end
	end
end

local activeEvent
VLF.TalkToNPC["q1s035"] = function(player, square, zNPC, qStep) --Xác nhận bắt đầu tiêu hủy mẫu vật sống
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then
			if not activeEvent then
				ActiveQuestIntro_Centre(getText("IGUI_VLQ_2_NPCTalk_"..tostring(qStep)), 800, 500, false, "")
				--spawn lẻ 1 zombie để đảm bảo có 1 zombie xung quanh điểm check
				--addZombiesInOutfit(10650, 10406, 0, 1, "Generic03", 50)
				
				--spawn local zombie trong khoảng cách 10 - 60
				if not playerGMD_Quest_Q1.q1s035_2 then playerGMD_Quest_Q1.q1s035_2 = {} end
				local doSpawn = false
				
				if IsMultiplayerMode() or not playerGMD_Quest_Q1.q1s035_2.active then --multi thì active lại spawn, single player thì chỉ spawn 1 lần
					doSpawn = true
				end
				
				if doSpawn == true then
					SpawnLocalZombiesEventq1s035(player, 100, {minDist = 10, maxDist = 60, z = 0})
					SpawnLocalZombiesEventq1s035(player, 30, {minDist = 10, maxDist = 40, z = 0})				
					SpawnLocalZombiesEventq1s035(player, 20, {minDist = 10, maxDist = 30, z = 0})
					DelayFunction(function()
						PlaySound(player, "vl_zombiehorder1")
					end, 180) --~5s
				end
				
				--Add sound thu hút zombie
				DelayFunction(function()
					local sq = getCell():getGridSquare(10637, 10405, 0)
					if sq then addSound(sq:getFloor(), sq:getX(), sq:getY(), sq:getZ(), 250, 1000) end
				end, 20)

				--Chạy event kiểm tra quest hoàn thành chưa
				DelayFunction(function()
					Events.EveryTenMinutes.Remove(EveryTenMinutes_CheckQuestq1s035)
					Events.EveryTenMinutes.Add(EveryTenMinutes_CheckQuestq1s035)
				end, 1800) --~30s
				activeEvent = true
				
				--cập nhật thông tin quest trong bảng và trên màn hình
				playerGMD_Quest_Q1.q1s035_2.active = true		
				QuestUpdatedPopIn()	
			else
				ActiveQuestIntro_Free(getText("IGUI_VLQ_2_Notification1_"..tostring(qStep)), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 230, false, "")
			end
		end
	end
end

VLF.TalkToNPC["q1s036"] = function(player, square, zNPC, qStep) --trở lại phòng thí nghiệm
	if qStep then	
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		--print("Client-----------VLF.TalkToNPC["..tostring(qStep).."]= ", player, zNPC, qStep)
		
		if playerGMD_Quest_Q1[qStep].active == true then			
			local qStep_Next = CalculateQStepNext(qStep) --qStep_Next = qStep + 1
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = qStep, active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = true, qID = 2}) --cập nhật qStep --kiểm tra bắt buộc isNPC_Next
			UpdateNPCToNextQStep(zNPC, qStep_Next) --cập nhật NPC thành NPC qStep tiếp theo
			AddReputationPoint(player, 80)
		end
	end
end

VLF.TalkToNPC["q1s037"] = function(player, square, zNPC, qStep) --chờ 2 ngày
	if qStep and zNPC and player then	
		WaitQuestActiveWhenNPCTalk(player, square, zNPC, qStep)
	end
end

local function GiveReducePainItems(player, qStep, args) --GiveReducePainItems(player, qStep, {requestNum = 3000})
	local givenItem = nil
	local dataName = "RPain"
	local requestNum = args.requestNum
	
	local blackSages = GetAllItemFromPlayerByType(player, "Base.BlackSage")
	if blackSages then
		for i = #blackSages, 1, -1 do
			local blackSage = blackSages[i]
			
			if blackSage then
				if not blackSage:isRotten() then
					RemoveItemFromContainer(player:getInventory(), blackSage, nil, 1)
					givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, 7)
					if givenItem == "done" then
						return "done"
					end
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
	
	local painkillersList = GetAllItemFromPlayerByType(player, "Base.Pills")
	if painkillersList then
		for i = #painkillersList, 1, -1 do
			local painkillers = painkillersList[i]
			
			if painkillers then
				local currentUse = painkillers:getCurrentUses()
				RemoveItemFromContainer(player:getInventory(), painkillers, nil, 1)
				givenItem = UpdateGiveItemQuestData(player, qStep, dataName, requestNum, currentUse * 14)
				if givenItem == "done" then
					return "done"
				end
			end
		end
		
		if givenItem ~= "done" and type(givenItem) == "number" then
			sendClientCommand(player, "MainQuest1", "CollectQuestItemPGMD", {qStep = qStep, dataName = dataName, collected = givenItem, request = requestNum})
			return "update"
		end
	end
end

--Dr. Venter --BunkerLab
VLF.TalkToNPC["q1s041_1"] = function(player, square, zNPC, qStep) --nói chuyện với Dr. Venter tại BunkerLab
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		
		if playerGMD_Quest_Q1["q1s041"] and playerGMD_Quest_Q1["q1s041"].active == true then		--nếu qStep q1s041 đang kích hoạt
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_2_NPCTalk_q1s041"), 800, 500, false, "")

		elseif playerGMD_Quest_Q1["q1s042"] and playerGMD_Quest_Q1["q1s042"].active == true then		--nếu qStep q1s042 đang kích hoạt
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk2_q1s042"), 800, 500, false, "")

		elseif playerGMD_Quest_Q1["q1s043"] and playerGMD_Quest_Q1["q1s043"].active == true then		--nếu qStep q1s043 đang kích hoạt	
			local qStep_Next = "q1s044"
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s043", active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = false, qID = 1})
			--ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s042"), 800, 500, false, "")
			
			--xóa NPC em trai cạnh Dr. Venter
			local zNPCList = GetNPCsByVarAndQStep("npcQuestIdle", "q1s043_3")
			if zNPCList and zNPCList[1] then
				RemoveNPCQuest(zNPCList[1])
			end
			SetNPCStateInQueueSpawnList({qStep = "q1s043_3", active = nil})
			
			--add NPC em trai trong phòng
			SetNPCStateInQueueSpawnList({qStep = "q1s041_3", active = true})
			SpawnNPCOnClient("q1s041_3", 0)

		elseif playerGMD_Quest_Q1["q1s045"] and playerGMD_Quest_Q1["q1s045"].active == true then		--nếu qStep q1s045 đang kích hoạt
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s045", active = "done", qStep_Next = "q1s046", active_Next = true, isNPC_Next = false, qID = 1})

		elseif playerGMD_Quest_Q1["q1s046"] and playerGMD_Quest_Q1["q1s046"].active == true then		--nếu qStep q1s046 đang kích hoạt	
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s046"), 800, 500, false, "")

		elseif playerGMD_Quest_Q1["q1s047"] and playerGMD_Quest_Q1["q1s047"].active == true then		--nếu qStep q1s047 đang kích hoạt	
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s047", active = "done", qStep_Next = "q1s048", active_Next = true, isNPC_Next = false, qID = 1})

		elseif playerGMD_Quest_Q1["q1s048"] and playerGMD_Quest_Q1["q1s048"].active == true then		--nếu qStep q1s048 đang kích hoạt	
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s048", active = "done", qStep_Next = "q1s049", active_Next = true, isNPC_Next = false, qID = 1})
			
		elseif playerGMD_Quest_Q1["q1s049"] and playerGMD_Quest_Q1["q1s049"].active == true then		--nếu qStep q1s049 đang kích hoạt
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s049"), 800, 500, false, "")
			
		elseif playerGMD_Quest_Q1["q1s050"] and playerGMD_Quest_Q1["q1s050"].active == true then		--nếu qStep q1s050 đang kích hoạt
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s050", active = "done", qStep_Next = "q1s051", active_Next = true, isNPC_Next = false, qID = 1})
			
		elseif playerGMD_Quest_Q1["q1s052"] and playerGMD_Quest_Q1["q1s052"].active == true then		--nếu qStep q1s052 đang kích hoạt
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s052", active = "done", qStep_Next = "q1s053", active_Next = true, isNPC_Next = false, qID = 1})
			
		elseif playerGMD_Quest_Q1["q1s053"] and playerGMD_Quest_Q1["q1s053"].active == true then		--nếu qStep q1s053 đang kích hoạt	
			local collection = GiveReducePainItems(player, "q1s053", {requestNum = VLF.var.requestCol["q1s053"]})
			if collection == "done" then
				Events.EveryHours.Remove(VLF.WaitTimePassQ1)	--xóa event thời hạn 3 ngày để tìm giảm đau
				DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s053", active = "done", qStep_Next = "q1s054", active_Next = true, isNPC_Next = false, qID = 1})
			elseif collection == "update" then QuestUpdatedPopIn()
			else ActiveQuestIntro_Free(getText("IGUI_VLQ_1_Notification1_q1s053"), (getCore():getScreenWidth() - 800) / 2, (getCore():getScreenHeight() - 200) / 16, 800, 250, false, "")
			end	
		
		elseif playerGMD_Quest_Q1["q1s055"] and playerGMD_Quest_Q1["q1s055"].active == true then		--nếu qStep q1s055 đang kích hoạt
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s055", active = "done", qStep_Next = "q1s056", active_Next = true, isNPC_Next = false, qID = 1})
		
		elseif playerGMD_Quest_Q1["q1s057"] and playerGMD_Quest_Q1["q1s057"].active == true then		--nếu qStep q1s057 đang kích hoạt
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s057", active = "done", qStep_Next = "q1s058", active_Next = true, isNPC_Next = false, qID = 1})
		
		elseif playerGMD_Quest_Q1["q1s059"] and playerGMD_Quest_Q1["q1s059"].active == true then		--nếu qStep q1s059 đang kích hoạt
			--cập nhật NPC em trai thành q1s060
			local zNPCList = GetNPCsByVarAndQStep("npcQuestSleepOnBed", "q1s046_3")
			if zNPCList and zNPCList[1] then
				UpdateNPCToNextQStep(zNPCList[1], "q1s060")
			end
			SetNPCStateInQueueSpawnList({qStep = "q1s046_3", active = nil})
			SetNPCStateInQueueSpawnList({qStep = "q1s060", active = true})
			
			DoQuestStepActive(player, {npcTalk = true, isPopIn = true, qStep = "q1s059", active = "done", qStep_Next = "q1s060", active_Next = true, isNPC_Next = false, qID = 1})
			AddReputationPoint(player, 100)
			
		elseif playerGMD_Quest_Q1["q1s060"] and playerGMD_Quest_Q1["q1s060"].active == true and playerGMD_Quest_Q1["q1s060"].npcPoisoned >= 20 then		--nếu qStep q1s060 đang kích hoạt và NPC bị nhiễm độc
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s060_1"), 800, 500, false, "")

		else
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_2_NPCTalk_q1s041_0"), 800, 500, false, "")
		end	
	end
end

--Dr. Bassler tại BunkerLab
VLF.TalkToNPC["q1s041_2"] = function(player, square, zNPC, qStep) --Dr. Bassler tại BunkerLab
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		
		if playerGMD_Quest_Q1["q1s060"] and playerGMD_Quest_Q1["q1s060"].active == true then
			if playerGMD_Quest_Q1["q1s060"].getRemovePoisonQuest == true then
				VLF.QuestUI.NPCInteractionWindow(player, "q1s060", getText("IGUI_VLQ_1_NPCTalk_q1s060_2_1"), 800, 500, zNPC)
			else
				VLF.QuestUI.NPCInteractionWindow(player, "q1s060", getText("IGUI_VLQ_1_NPCTalk_q1s060_2_0"), 800, 500, zNPC)
			end
			
		else
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_2_NPCTalk_q1s041_2"), 800, 500, false, "")
		end
		
	end
end

--Em trai --trong phòng riêng
VLF.TalkToNPC["q1s041_3"] = function(player, square, zNPC, qStep) --nói chuyện với Em Trai tại BunkerLab --trong phòng riêng
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)

		if playerGMD_Quest_Q1["q1s042"] and playerGMD_Quest_Q1["q1s042"].active == true then		--nếu qStep q1s042 đang kích hoạt
			local qStep_Next = "q1s043"
			
			--Xóa NPC vị trí hiện tại
			RemoveNPCQuest(zNPC)
			SetNPCStateInQueueSpawnList({qStep = qStep, active = nil})
			
			--Add NPC qStep tiếp theo
			SetNPCStateInQueueSpawnList({qStep = "q1s043_3", active = true})
			SpawnNPCOnClient("q1s043_3", 0)
			
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s042_3"), 800, 500, false, "")
			DoQuestStepActive(player, {npcTalk = nil, isPopIn = true, qStep = "q1s042", active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = false, qID = 1})

		elseif playerGMD_Quest_Q1["q1s044"] and playerGMD_Quest_Q1["q1s044"].active == true then		--nếu qStep q1s044 đang kích hoạt
			local qStep_Next = "q1s045"
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s044_3"), 800, 500, false, "")
			DoQuestStepActive(player, {npcTalk = nil, isPopIn = true, qStep = "q1s044", active = "done", qStep_Next = qStep_Next, active_Next = true, isNPC_Next = false, qID = 1})
			
		elseif playerGMD_Quest_Q1["q1s046"] and playerGMD_Quest_Q1["q1s046"].active == true then		--nếu qStep q1s046 đang kích hoạt	
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s046_3"), 800, 500, false, "")
		 
		
		end
	end
end

VLF.TalkToNPC["q1s041_4"] = function(player, square, zNPC, qStep) --Guard --tại BunkerLab --do nothing
end

VLF.TalkToNPC["q1s041_5"] = function(player, square, zNPC, qStep) --Guard --tại BunkerLab --do nothing
end

VLF.TalkToNPC["q1s041_6"] = function(player, square, zNPC, qStep) --Guard --Leader --tại BunkerLab --do nothing
	if qStep then
		ActiveQuestIntro_Centre(getText("IGUI_VLQ_2_NPCTalk_q1s041_6"), 800, 500, false, "")
	end
end

--Em trai --đứng cạnh Dr.Venter
VLF.TalkToNPC["q1s043_3"] = function(player, square, zNPC, qStep) --nói chuyện với Em Trai tại BunkerLab --đứng cạnh Dr.Venter
end

--Em trai --nằm trên giường bệnh
VLF.TalkToNPC["q1s046_3"] = function(player, square, zNPC, qStep) --nói chuyện với Em Trai tại BunkerLab --nằm trên giường bệnh
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
		
		if playerGMD_Quest_Q1["q1s047"] and playerGMD_Quest_Q1["q1s047"].active == true then		--nếu qStep q1s047 đang kích hoạt
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s047_3"), 800, 500, false, "")
		
		elseif playerGMD_Quest_Q1["q1s048"] and playerGMD_Quest_Q1["q1s048"].active == true then		--nếu qStep q1s048 đang kích hoạt
			ActiveQuestIntro_Centre(getText("IGUI_VLQ_1_NPCTalk_q1s047_3"), 800, 500, false, "")
		end
	end
end

--Em trai --nằm trên giường bệnh --được chăm sóc
VLF.TalkToNPC["q1s060"] = function(player, square, zNPC, qStep) --nói chuyện với Em Trai tại BunkerLab --nằm trên giường bệnh
	if qStep then
		local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(player, qStep)
	end
end


