function itemGiveByTypeMain takes nothing returns boolean
local player p = GetTriggerPlayer()
local integer pid = GetPlayerId(p)
local integer currQuest = 0
local integer currQuestStage = 0
local integer questRange = 0
local integer goalBttn = 0
local integer goalNpc = 0
local integer goalDialog = 0
local integer i = 0
local integer count = 0 //how many items of the type the player has
loop
exitwhen questRange == TOTAL_QUESTS
set currQuest = playerDatum[pid].findQuestByGoalType(ITEM_TYPE_GIVE_GOAL, questRange)
set currQuestStage = playerDatum[pid].quests[currQuest].stage
if currQuest >= 0 then
set goalBttn = playerDatum[pid].quests[currQuest].goals[currQuestStage].goalBttn
set goalNpc = playerDatum[pid].quests[currQuest].goals[currQuestStage].goalNpc
set goalDialog = playerDatum[pid].quests[currQuest].goals[currQuestStage].goalDialog
if GetClickedButton() == playerDatum[pid].npcs[goalNpc].dialogBttns[(goalDialog * MAX_DIALOGS) + goalBttn] then
loop
exitwhen i == MAX_ITEMS
if GetItemTypeId(UnitItemInSlot(playerDatum[pid].pc.u, i)) == playerDatum[pid].quests[currQuest].goals[currQuestStage].goalItemType then
if playerDatum[pid].quests[currQuest].goals[currQuestStage].quant > 0 then
set count = count + 1
call RemoveItem(UnitItemInSlot(playerDatum[pid].pc.u, i))
set playerDatum[pid].quests[currQuest].goals[currQuestStage].quant = playerDatum[pid].quests[currQuest].goals[currQuestStage].quant - 1
endif
endif
set i = i + 1
endloop
if playerDatum[pid].quests[currQuest].goals[currQuestStage].quant == 0 then
call playerDatum[pid].quests[currQuest].advance()
call DestroyTrigger(GetTriggeringTrigger())
endif
set questRange = TOTAL_QUESTS
else
set questRange = currQuest + 1 //advance the quest counter, in case another quest has a button click goal
endif
else //currQuest == -1, there are no enabled quests with an item obtain goal
set questRange = TOTAL_QUESTS
endif
endloop
set p = null
return false
endfunction
...