> For the complete documentation index, see [llms.txt](https://fehdead.gitbook.io/npc-kit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fehdead.gitbook.io/npc-kit/examples/drink-quest.md).

# A Quest: The Bhujerban Viper

**A drinker by the Sandsea remembers a Bhujerban brew, and three NPCs pass the quest along: a barkeep who needs Cactus Fruit, a soldier melting in the heat, and the drinker who pays at the end.**

This example takes place on map ids 291 (Rabanastre / East End), 304 (Rabanastre / The Sandsea) and 289 (Rabanastre / North End), with fruit hunted in the Dalmasca Estersand.

Download: [drink-quest.lua](https://github.com/FelipeJulio/NPC-Kit/tree/main/examples/drink-quest.lua)

```lua
local ok, npc = pcall(require, "scripts.NPCKit.Helper")
if not ok then
    print("Couldn't install, NPC Kit is missing.")
    return
end

local ardoStory = [[
Sit a while, friend. Years back I took an airship
to Bhujerba, and in the Cloudborne they poured me
a drink I'll never forget.{wait}
{color:lightblue}Bhujerban Viper{color:none}, they call it. Black as night,
one glass, and I was drunk for a whole week.
Saw ten moons in the sky over Old Archades.{wait}
And then the strange part. I'd hiss at people.
Slept curled up in the sun on a warm rooftop,
tasting the air with my tongue. Like a viper.{wait}
Gods, I'd give anything for one more glass.
Find me a bottle and I'll make it worth your
while. I pay my debts, I do.
]]
local ardoAsk = "Could you find me a bottle, friend?"
local ardoChoices = { "I'll find one", "Not now" }
local ardoRefused = [[
Pity. The offer stands, friend. A thirst like
mine doesn't go anywhere.
]]
local ardoAccepted = [[
Ha! I knew you'd help an old man. And where to
look? Funny you should ask, I know just the one.{wait}
The barkeep inside the Sandsea, the red Bangaa.
He knows every bottle in Rabanastre. Go and
talk to him, and mind you keep your voice down.
]]
local ardoWaiting = [[
Any luck with the {color:lightblue}Bhujerban Viper{color:none}? Ask the
red Bangaa behind the bar, inside the Sandsea.
]]
local ardoHandOver = [[
Is that... it is! {color:lightblue}Bhujerban Viper{color:none}! Black as
night, just as I remember. Give it here, quick.
]]
local ardoDrunk = [[
Sssso good. The ground's moving, friend, or is it
me? I can taste the air again. Sssplendid.{wait}
I'd bessst sssit down for a week or ssso. You
keep your word, I keep mine. Here, take thessse.
]]
local ardoDone = [[
Hsss... friend! Don't mind me, I'm jussst lying in
the sssun. Don't ssssstep on my tail, now.
]]

local vozzBefore = [[
Feh. Quit proppin' up me counter, boy. Odd fella
outside's thirstier 'n a sandsea lizard, heh heh.
]]
local vozzGreet = "Hrrm? Welcome to the Sandsea, boy. What'll it be?"
local vozzGreetChoices = { "Ask about the Bhujerban Viper", "Nothing" }
local vozzIdle = [[
Feh. Then quit proppin' up me counter, boy, and
make room for them what's drinkin'.
]]
local vozzSecret = [[
Shhhhhhhhh! Pipe down, boy! Ye want every
Imperial in the place hearin' ye? Harrumph.{wait}
{color:lightblue}Bhujerban Viper{color:none}, they brew it down under the
Cloudborne, outta sky serpent venom, see?
Ain't nobody 'cept Bhujerbans knows 'bout it.{wait}
Empire banned the stuff, so not one bottle
crosses that border. Well... near enough none.{wait}
There's a Bhujerban soldier up North End sittin'
on a bottle. Won't sell it for no gil, but the
poor sod's meltin' in our heat, heh heh.{wait}
Begged me for a {color:lightblue}Cactite Cooler{color:none}, he did, and I
ain't got no fruit. Ye bring me 3 {color:lightblue}Cactus Fruit{color:none},
I brew it, ye take it, he trades. See?{wait}
Cactites is out in the Dalmasca Estersand. Cut
the fruit off 'em yerself, boy. Bought fruit's
gone soft, and that soldier'll know it.
]]
local vozzAsk = "3 {color:lightblue}Cactus Fruit{color:none} for a bottle. Deal, boy?"
local vozzChoices = { "Deal", "Not now" }
local vozzRefused = [[
Bah. Then ye never heard nothin' from me, boy.
Not one word, see?
]]
local vozzAccepted = [[
Heh heh, that's m'boy. 3 {color:lightblue}Cactus Fruit{color:none}, cut off
them Cactites yerself. Now get goin'.
]]
local vozzHunting = [[
Pah. I don't see no fruit. Cactites is out in
the Dalmasca Estersand, boy. 3 of 'em, I need.
]]
local vozzMissing = [[
Harrumph? Ye said ye had 3 {color:lightblue}Cactus Fruit{color:none}. I don't
see no fruit. Don't waste me precious time, boy.
]]
local vozzTakesFruit = [[
Nyah ha ha! 3 {color:lightblue}Cactus Fruit{color:none}, fresh off the needle.
Give 'em here quick, and keep an eye out, see?
]]
local vozzCooler = [[
Heh, there she is. One {color:lightblue}Cactite Cooler{color:none}, ready.
Bottle's right there on the counter. Grab it, see?
]]
local vozzGrab = [[
What ye waitin' for, boy? The bottle's on the
counter. Grab it 'fore it gets warm.
]]
local vozzDelivering = [[
Feh. That {color:lightblue}Cactite Cooler{color:none} ain't stayin' cold
forever, boy. North End. Get movin'.
]]
local vozzHasViper = [[
Heh, a bottle o' {color:lightblue}Bhujerban Viper{color:none}? Ain't for whelps
like ye, boy. Eh? For that thirsty fella outside?
Hah! Then good luck to 'im. He'll need it, see?
]]
local vozzDone = [[
Traded, did he? Heh heh heh. Tell yer friend to
sip it slow. Last fella what didn't, he tried
sheddin' his skin. Nyah ha ha!
]]

local coolerTaken = [[
An ice-cold bottle, sweating in the heat.
Better get it to North End before it warms.
]]

local dalanHot = [[
Hanta... what heat is this, bhadra? In Bhujerba
the winds are cool. Here, my armor is an oven.
Truly, I should never have come to Rabanastre.{wait}
The Bangaa at the Sandsea promised me a
{color:lightblue}Cactite Cooler{color:none}. Haa... if only, yes?
]]
local dalanGrab = [[
Kah...? A {color:lightblue}Cactite Cooler{color:none}, from Vozz? The gods
smile on you, bhadra. Give it here, please!
]]
local dalanThanks = [[
Haa... I can feel my toes once more. You have
saved a rakis of Bhujerba from the desert, yes?{wait}
I know why Vozz sent you. Do not deny it, bhadra.
One bottle of {color:lightblue}Bhujerban Viper{color:none}, from home.
It is yours. You have earned it.{wait}
One glass only, you see. The last man who drank
two tried to bite His Excellency the Marquis.
]]
local dalanDone = [[
Cool at last. Should you fly to Bhujerba, tell
them at the Cloudborne that Dalan sent you, yes?
]]
local dalanJoke = [[
Kah! You gave the {color:lightblue}Bhujerban Viper{color:none} to a
Rabanastran? Haa! He must be hissing by now, yes?
A week of hangover? No, bhadra. A week of venom.
]]

local objectives = {
    [1] = "Ask the red Bangaa barkeep inside the Sandsea about the {color:lightblue}Bhujerban Viper{color:none}",
    [2] = "Defeat Cactites in the Dalmasca Estersand and collect {color:lightblue}Cactus Fruit{color:none}",
    [3] = "Bring the {color:lightblue}Cactus Fruit{color:none} to the barkeep inside the Sandsea",
    [4] = "Take the {color:lightblue}Cactite Cooler{color:none} from the Sandsea counter",
    [5] = "Take the {color:lightblue}Cactite Cooler{color:none} to the Bhujerban soldier in North End",
    [6] = "Bring the {color:lightblue}Bhujerban Viper{color:none} to Ardo outside the Sandsea",
    [7] = "Quest complete"
}
local steps = 6
local complete = 7

local idle, talking, joyful, angry, laughing = 0x01000000, 0x01000002, 0x01000010, 0x01000011, 0x01000013
local cactusFruit, maximillian, gilReward = 0x207A, 0x1152, 1000

local stage, fruits = 0, 0
local tracker, shown, current, currentStep
local hunt = 0

local function wrap(text)
    local out, width = {}, 0
    for word in text:gmatch("%S+") do
        local seen = #word:gsub("{[^}]*}", "")
        if width > 0 and width + 1 + seen > 38 then
            out[#out + 1] = "\n"
            width = 0
        elseif width > 0 then
            out[#out + 1] = " "
            width = width + 1
        end
        out[#out + 1] = word
        width = width + seen
    end
    return table.concat(out)
end

local function objective()
    if stage == 2 then
        return wrap(objectives[2] .. " (" .. fruits .. "/3)")
    end
    return wrap(objectives[stage])
end

local function chime(sound)
    memory.execute(0x001DBDB0, arg.void, arg.s32, 0, arg.s32, sound, arg.s32, 0x40, arg.s32, 0x7F)
end

local function show()
    if not current or (tracker and shown == current) or (currentStep == complete and shown == current) then
        return
    end
    if tracker then
        local old = tracker
        tracker = nil
        old:close()
    end
    local title = "{scale:50}The Bhujerban Viper  " .. math.min(currentStep - 1, steps) .. "/" .. steps
    local window, reason
    window, reason = dialog.mes.open {
        body = "{instant}" .. (title .. "\n{scale:80}" .. current):gsub("\n", "\n{instant}"),
        x = 1860, y = 770, w = 0, h = 0, align = 3,
        type = 1,
        focus = false,
        dismissible = false,
        queue = true,
        onClose = function(_, why)
            if tracker ~= window then return end
            tracker = nil
            if why == "gameClosed" and currentStep ~= complete then
                event.executeAfterMs(1000, show)
            end
        end
    }
    if not window then
        print("drink-quest: couldn't open the objective, " .. tostring(reason))
        return
    end
    if shown ~= current then
        chime(currentStep == complete and 75 or 9)
    end
    tracker, shown = window, current
    if currentStep == complete then
        event.executeAfterMs(5000, function() window:close() end)
    end
end

local function track()
    if stage == 0 then return end
    current, currentStep = objective(), stage
    event.executeAfterMs(1000, show)
end

local function cactiteKills()
    local menuInfo = memory.readU64(0x020927C0)
    return menuInfo ~= 0 and memory.readU8(menuInfo + 0x3404) or 0
end

local function fruitCount()
    local counters = memory.readU64(0x01EEBCC0 + 2 * 0x58 + 0x08)
    return memory.readU16(counters + (cactusFruit - 0x2000) * 2) & 0x3FF
end

local function startHunt()
    hunt = hunt + 1
    local id, kills, have, owed = hunt, cactiteKills(), fruitCount(), 0
    local function poll()
        if id ~= hunt or stage ~= 2 then return end
        local k, f = cactiteKills(), fruitCount()
        owed = owed + (k == 0xFF and 3 or math.max(k - kills, 0))
        local gained = math.min(owed, math.max(f - have, 0))
        owed, kills, have = owed - gained, k, f
        if gained > 0 then
            fruits = math.min(fruits + gained, 3)
            if fruits == 3 then stage = 3 end
            track()
        end
        if stage == 2 then event.executeAfterMs(500, poll) end
    end
    event.executeAfterMs(500, poll)
end

local function place(model, set, walkmesh, x, y, z, heading)
    npc.setautorelax(1);
    npc.usecharhit(0);
    npc.bindp2_d4(model, 1, 0);
    npc.stdmotionread(set);
    npc.stdmotionreadsync();
    npc.stdmotionplay_2c2(idle, 0);
    npc.setoverheadicontype(0);
    npc.usemapid(walkmesh and 1 or 0);
    npc.usecharhit(1);
    npc.setweight(-1);
    npc.set_ignore_hitgroup(1);
    npc.motionloop(1);
    npc.setpos2(x, y, z);
    npc.dir(heading);
    npc.fetchambient();
    npc.bonecalcforce(-1);
end

local function appear()
    npc.clearhidecomplete();
    npc.show();
    npc.showshadow();
end

local function talkOpen(still)
    npc.sethpmenu(0);
    npc.ucoff();
    npc.settrapshowstatus(0);
    if not still then
        npc.setaturnlookatlockstatus(0);
        npc.raturna(-1);
        npc.turnsync();
        npc.stdmotionplay_2c2(talking, 20);
    end
    npc.setkutipakustatus(1);
    npc.setunazukistatus(1);
end

local function talkClose(still)
    npc.setkutipakustatus(0);
    npc.setunazukistatus(0);
    npc.lookatoff();
    npc.ucon();
    npc.sethpmenu(1);
    npc.clear_force_char_nearfade();
    npc.setmaphighmodeldepth(-1);
    npc.setmapmodelstatus(1);
    npc.setstatuserrordispdenystatus(0);
    npc.settrapshowstatus(1);
    if not still then
        npc.stdmotionplay_2c2(idle, 20);
    end
end

local function talkTerm()
    npc.setaturnlookatlockstatus(1);
    npc.aturn_261(npc.getdestrotangzx_227(0));
end

local function say(handle, speaker, body, onDone)
    if not dialog.amese.open {
            speaker = speaker,
            body = body,
            queue = true,
            onClose = onDone or function()
                handle.finish()
                track()
            end,
            onError = function() handle.finish() end
        } then
        handle.finish()
    end
end

local function choose(handle, speaker, question, choices, onYes, onNo)
    if not dialog.aaske.open {
            speaker = speaker,
            body = question,
            choice = choices,
            cancel = 1,
            queue = true,
            onClose = function(index, reason)
                if reason == "dismissed" and index == 0 then onYes() else onNo() end
            end,
            onError = function() handle.finish() end
        } then
        handle.finish()
    end
end

local function ask(handle, speaker, story, question, choices, onYes, onNo)
    say(handle, speaker, story, function() choose(handle, speaker, question, choices, onYes, onNo) end)
end

local function myMod()
    npc.add {
        id = "npckit_0069",
        map = 291,
        name = "Ardo",

        init = function()
            npc.lua(function(handle)
                if stage > 0 and stage < complete then track() end
                handle.run(stage < complete and "standing" or "gone")
            end);
        end,

        main = function()
            npc.wait(90);
        end,

        standing = function()
            place(0x006E0012, 16, true, 144.305, 0.107, 40.372, -2.047);
            appear();
        end,

        gone = function()
            npc.hidecomplete();
            place(0x006E0012, 16, true, 144.305, 0.107, 40.372, -2.047);
        end,

        cheer = function()
            npc.stdmotionplay_2c2(joyful, 20);
        end,

        drink = function()
            local drunk = npc.on("npckit_0070")
            local bottle = npc.on("npckit_0071")
            npc.fadeout(30);
            npc.fadesync();
            npc.wait(5);
            npc.sebsoundplay(0, 29);
            npc.hidecomplete();
            drunk.clearhidecomplete();
            drunk.show();
            drunk.showshadow();
            drunk.rgbatrans(1, 1, 1, 1, 0);
            npc.wait(15);
            npc.fadein(30);
            npc.fadesync();
            bottle.showshadow();
            bottle.rgbatrans(7.000, 7.000, 7.000, 0.490, 10);
            npc.sebsoundplay(0, 63);
        end,

        reward = function()
            npc.musictrans(-1, 30, 32);
            npc.setstatuserrorctrldenystatus_by_menu(1);
            npc.seteventtimestop(1);
            npc.moveblurcaptureonce();
            npc.movebluraplpha(1);
            npc.moveblurfade(15);
            npc.moveblurfadesync();
            npc.sebsoundplay(0, 40);
            npc.additemmes(maximillian, 1);
            npc.musictrans(-1, 30, 127);
            npc.moveblurclearfade(15);
            npc.moveblurfadesync();
            npc.seteventtimestop(0);
            npc.setstatuserrorctrldenystatus_by_menu(0);
            npc.addgill(gilReward);
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                if stage == 0 then
                    ask(handle, "Ardo", ardoStory, ardoAsk, ardoChoices, function()
                        stage = 1
                        handle.run("cheer", function() say(handle, "Ardo", ardoAccepted) end)
                    end, function()
                        say(handle, "Ardo", ardoRefused)
                    end)
                elseif stage < 6 then
                    say(handle, "Ardo", ardoWaiting)
                elseif stage == 6 then
                    say(handle, "Ardo", ardoHandOver, function()
                        handle.run("drink", function()
                            say(handle, "Ardo", ardoDrunk, function()
                                handle.run("reward", function()
                                    stage = complete
                                    handle.finish()
                                    track()
                                end)
                            end)
                        end)
                    end)
                else
                    handle.finish()
                end
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }

    npc.add {
        id = "npckit_0070",
        map = 291,
        name = "Ardo",
        motionFrom = { 0x006E0082, 1 },

        init = function()
            npc.lua(function(handle)
                handle.run(stage == complete and "place" or "hidden")
            end);
        end,

        place = function()
            place(0x006E0012, 18, true, 144.305, 0.107, 40.372, -2.047);
            appear();
        end,

        hidden = function()
            npc.hidecomplete();
            place(0x006E0012, 18, true, 144.305, 0.107, 40.372, -2.047);
            npc.rgbatrans(1, 1, 1, 0, 0);
        end,

        main = function()
            npc.wait(90);
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                if stage ~= complete then return handle.finish() end
                say(handle, "Ardo", ardoDone)
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }

    npc.add {
        id = "npckit_0071",
        map = 291,

        init = function()
            npc.lua(function(handle)
                handle.run(stage == complete and "place" or "hidden")
            end);
        end,

        place = function()
            place(0x0067004B, 16, false, 143.944, 0.155, 40.775, -2.278);
            appear();
            npc.rotate(1.601, -0.392, -0.107);
            npc.efx(118);
            npc.efxrgba(0, 255, 139, 247);
            npc.rgbatrans(7.000, 7.000, 7.000, 0.490, 10);
        end,

        hidden = function()
            place(0x0067004B, 16, false, 143.944, 0.155, 40.775, -2.278);
            npc.rgbatrans(7.000, 7.000, 7.000, 0, 0);
            npc.clearhidecomplete();
            npc.show();
            npc.rotate(1.601, -0.392, -0.107);
            npc.efx(118);
            npc.efxrgba(0, 255, 139, 247);
        end,

        main = function()
            npc.wait(90);
        end
    }

    npc.add {
        id = "npckit_0072",
        map = 304,
        name = "Vozz",

        init = function()
            npc.lua(function(handle)
                if stage > 0 and stage < complete then track() end
                handle.run("place", function()
                    if stage == 4 then handle.run("mute") else handle.finish() end
                end)
            end);
        end,

        place = function()
            place(0x006E0018, 17, false, 58.458, 0.281, 41.941, 1.226);
            npc.talkradius(1.5);
            appear();
        end,

        hush = function()
            npc.stdmotionread(16);
            npc.stdmotionreadsync();
            npc.stdmotionplay_2c2(angry, 20);
        end,

        laugh = function()
            npc.stdmotionread(16);
            npc.stdmotionreadsync();
            npc.stdmotionplay_2c2(laughing, 20);
        end,

        settle = function()
            npc.stdmotionread(17);
            npc.stdmotionreadsync();
            npc.stdmotionplay_2c2(idle, 0);
        end,

        mute = function()
            npc.reqdisable(2);
        end,

        main = function()
            npc.wait(90);
        end,

        brew = function()
            local bottle = npc.on("npckit_0074")
            npc.subitem(cactusFruit, 3);
            npc.sebsoundplay(0, 68);
            npc.fadeout(15);
            npc.fadesync();
            npc.stdmotionread(17);
            npc.stdmotionreadsync();
            npc.stdmotionplay_2c2(idle, 0);
            bottle.clearhidecomplete();
            bottle.show();
            bottle.showshadow();
            bottle.rgbatrans(4.500, 6.781, 7.000, 1.000, 10);
            bottle.reqenable(13);
            bottle.showfieldsign();
            npc.reqdisable(2);
            npc.wait(20);
            npc.fadein(15);
            npc.fadesync();
            npc.sebsoundplay(0, 93);
        end,

        talk = function()
            talkOpen(true);
            npc.rlookata(-1);
            npc.lua(npc.haveitem(cactusFruit), function(handle, have)
                if stage == 0 then
                    say(handle, "Vozz", vozzBefore)
                elseif stage == 1 then
                    choose(handle, "Vozz", vozzGreet, vozzGreetChoices, function()
                        handle.run("hush", function()
                            ask(handle, "Vozz", vozzSecret, vozzAsk, vozzChoices, function()
                                stage, fruits = 2, 0
                                startHunt()
                                handle.run("laugh", function()
                                    say(handle, "Vozz", vozzAccepted, function()
                                        handle.run("settle", function()
                                            handle.finish()
                                            track()
                                        end)
                                    end)
                                end)
                            end, function()
                                handle.run("settle", function() say(handle, "Vozz", vozzRefused) end)
                            end)
                        end)
                    end, function()
                        say(handle, "Vozz", vozzIdle)
                    end)
                elseif stage == 2 then
                    say(handle, "Vozz", vozzHunting)
                elseif stage == 3 and have < 3 then
                    say(handle, "Vozz", vozzMissing)
                elseif stage == 3 then
                    handle.run("laugh", function()
                        say(handle, "Vozz", vozzTakesFruit, function()
                            handle.run("brew", function()
                                stage = 4
                                say(handle, "Vozz", vozzCooler)
                            end)
                        end)
                    end)
                elseif stage == 4 then
                    say(handle, "Vozz", vozzGrab)
                elseif stage == 5 then
                    say(handle, "Vozz", vozzDelivering)
                elseif stage == 6 then
                    say(handle, "Vozz", vozzHasViper)
                else
                    say(handle, "Vozz", vozzDone)
                end
            end);
            talkClose(true);
        end,

        talkterm = talkTerm
    }

    npc.add {
        id = "npckit_0073",
        map = 304,

        init = function()
            place(0x00670047, 16, false, 58.804, 1.104, 41.637, 1.521);
            appear();
            npc.setscale(0.591);
        end,

        main = function()
            npc.wait(90);
        end
    }

    npc.add {
        id = "npckit_0074",
        map = 304,
        name = "Cactite Cooler",

        init = function()
            npc.lua(function(handle)
                handle.run(stage == 4 and "place" or "hidden")
            end);
        end,

        place = function()
            place(0x0067004B, 16, false, 59.518, 1.170, 42.092, 0.384);
            npc.setradius(1.0);
            npc.talkradius(1.5);
            appear();
            npc.efx(41);
            npc.efxrgba(128, 128, 128, 36);
            npc.rgbatrans(4.500, 6.781, 7.000, 1.000, 10);
            npc.setnavimapdrawstatus(0);
            npc.reqenable(13);
            npc.fieldsignicon(3);
            npc.showfieldsign();
        end,

        hidden = function()
            npc.hidecomplete();
            place(0x0067004B, 16, false, 59.518, 1.170, 42.092, 0.384);
            npc.setradius(1.0);
            npc.talkradius(1.5);
            npc.efx(41);
            npc.efxrgba(128, 128, 128, 36);
            npc.setnavimapdrawstatus(0);
            npc.fieldsignicon(3);
            npc.rgbatrans(1, 1, 1, 0, 0);
        end,

        main = function()
            npc.wait(90);
        end,

        take = function()
            npc.fadeout(15);
            npc.fadesync();
            npc.reqdisable(13);
            npc.hidecomplete();
            npc.on("npckit_0072").reqenable(2);
            npc.wait(10);
            npc.fadein(15);
            npc.fadesync();
        end,

        talk = function()
            npc.ucoff();
            npc.lua(function(handle)
                if stage ~= 4 then return handle.finish() end
                handle.run("take", function()
                    stage = 5
                    local function done()
                        handle.finish()
                        track()
                    end
                    if not dialog.amese.open {
                            speaker = "Cactite Cooler",
                            icon = 3,
                            body = coolerTaken,
                            queue = true,
                            onClose = done,
                            onError = done
                        } then
                        done()
                    end
                end)
            end);
            npc.ucon();
        end
    }

    npc.add {
        id = "npckit_0075",
        map = 289,
        name = "Dalan",
        motionFrom = { 0x006E0004, 18 },

        init = function()
            npc.lua(function(handle)
                if stage > 0 and stage < complete then track() end
                handle.run(stage < 6 and "place" or "gone")
            end);
        end,

        place = function()
            place(0x006E00A8, 16, false, 79.356, 0.100, 2.350, -0.433);
            appear();
        end,

        gone = function()
            npc.hidecomplete();
            place(0x006E00A8, 16, false, 79.356, 0.100, 2.350, -0.433);
        end,

        main = function()
            npc.wait(90);
        end,

        cooldown = function()
            local cool = npc.on("npckit_0076")
            npc.fadeout(30);
            npc.fadesync();
            npc.hidecomplete();
            cool.clearhidecomplete();
            cool.show();
            cool.showshadow();
            cool.rgbatrans(1, 1, 1, 1, 0);
            npc.wait(10);
            npc.fadein(30);
            npc.fadesync();
            npc.common2effectplay(npc.actor("npckit_0076"), 11);
            npc.sebsoundplay(0, 75);
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                if stage < 5 then
                    say(handle, "Dalan", dalanHot)
                elseif stage == 5 then
                    say(handle, "Dalan", dalanGrab, function()
                        handle.run("cooldown", function()
                            say(handle, "Dalan", dalanThanks, function()
                                stage = 6
                                handle.finish()
                                track()
                            end)
                        end)
                    end)
                else
                    handle.finish()
                end
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }

    npc.add {
        id = "npckit_0076",
        map = 289,
        name = "Dalan",

        init = function()
            npc.lua(function(handle)
                handle.run(stage < 6 and "hidden" or "place")
            end);
        end,

        place = function()
            place(0x006E00A8, 16, true, 78.923, 0.100, 2.542, -0.433);
            appear();
        end,

        hidden = function()
            npc.hidecomplete();
            place(0x006E00A8, 16, true, 78.923, 0.100, 2.542, -0.433);
            npc.rgbatrans(1, 1, 1, 0, 0);
        end,

        main = function()
            npc.wait(90);
        end,

        talk = function()
            talkOpen();
            npc.lua(function(handle)
                if stage < 6 then return handle.finish() end
                say(handle, "Dalan", stage == complete and dalanJoke or dalanDone)
            end);
            talkClose();
        end,

        talkterm = talkTerm
    }
end

event.registerSaveHandler("drink-quest", function() return { stage = stage, fruits = fruits } end)
event.registerLoadHandler("drink-quest", function(_, data)
    stage = data and data.stage or 0
    fruits = data and data.fruits or 0
    shown = stage > 0 and objective() or nil
    if stage == 2 then startHunt() end
    if stage > 0 and stage < complete then event.executeAfterMs(3000, track) end
end)
event.registerEventAsync("onInitDone", myMod)
```

### The quest

| step | where                | what happens                                                                                    |
| ---- | -------------------- | ----------------------------------------------------------------------------------------------- |
| 0    | East End, map 291    | Ardo tells of the Bhujerban Viper and asks for a bottle                                         |
| 1    | The Sandsea, map 304 | Vozz, the red Bangaa barkeep, tells who has one: a Bhujerban soldier who wants a Cactite Cooler |
| 2    | Dalmasca Estersand   | three Cactus Fruit, each from a Cactite the party defeats                                       |
| 3    | The Sandsea          | Vozz brews the Cooler in a fade and sets it on the counter                                      |
| 4    | The Sandsea          | the player picks the bottle up from the counter                                                 |
| 5    | North End, map 289   | Dalan drinks the Cooler and gives the Viper                                                     |
| 6    | East End             | Ardo drinks it and pays with a Maximillian and 1000 gil                                         |
| 7    |                      | complete                                                                                        |

`stage` holds the step and goes into the save with `event.registerSaveHandler`. Every NPC's `init` picks its setup from it with `handle.run`, so a map load builds each one as the story has left it. The talks branch on it in `npc.lua`.

### The objective

The objective is a red `dialog.mes` window in the bottom-right corner, above the party list. It shows `current`, which only `track()` sets: the talks call it once their last window has closed (`say` does it by default), so a `stage` changed in the middle of a talk never shows early. The first line is the title with the steps done; each line starts with `{instant}`, which holds for one line only, so the text comes up at once.

A map change closes the window with the reason `"gameClosed"`; `onClose` opens it again a second later. It chimes only when the objective itself changes. The chime goes through the game's sound function (`0x001DBDB0`), because the fruit counter runs on a Lua timer, where no VM call can run.

### Counting the fruit

There is no call or Loader API for a defeat or a drop, so a timer reads two counters every 500 ms while step 2 lasts:

* the Clan Primer's kill byte for entry 0, which the Cactoid and the Cactite share (`g_MenuInfo + 0x3404`);
* the party's Cactus Fruit (`0x207A`), from the Loot counters.

A fruit counts only when a Cactite died for it: fruit bought, stolen or found in a chest is left out. The kill byte stops at 254 kills; past that, any new fruit counts.

### Twins for another motion

`motionFrom` gives a whole NPC another model's motions, and it can't change mid-scene. Ardo after the drink and Dalan in the heat are each a second NPC on the same spot: one is built hidden (`hidecomplete`, then fully transparent), and a fade swaps them with `npc.on`. The hot Dalan leans on a wall past the walkmesh's edge, so he is placed with `usemapid(0)`; on the walkmesh the game would pull him back onto it.

### The bottle on the counter

The Cactite Cooler is an [inspectable object](https://fehdead.gitbook.io/npc-kit/examples/inspectable-object.md), built hidden and shown during Vozz's fade. It is small and behind the counter, so `setradius` grows its body and `talkradius` the area around it; until it is taken, `reqdisable(2)` turns Vozz's own talk off, because his wide `talkradius` covers the bottle. The empty bottle on the floor at the end is shown but fully transparent until Ardo drinks, so its `rotate` holds; it is faded in after the drink.
