• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

NPC Quests that require you to kill monsters

Ispiro

New Member
Joined
Oct 1, 2007
Messages
129
Reaction score
2
I made a NPC and onKill event to emulate some of World of Warcraft's quest. I understand that WoW isn't the only game or the first to do this, but it's one of the well-known ones ;) I also added some examples for you to know how this thing works. It should be fairly easy to understand just by reading the comments in the script, but if some still have trouble understanding I'll probably try to explain a little more.


DONT FORGET TO COMMENT!


global.lua?
Code:
wow_like_quests = {
	--[[
		[LEVEL_REQUIRED] = {
			{
				monster = "CREATURENAME", 
				amount = AMOUNT_TO_KILL, 
				storage = STORAGE_VALUE,
				on_turn_in = function(cid, parameters) -- optional, but would be pointless if you dont add items to player, or atleast experience
					doPlayerAddExp(cid, 10000)
					doPlayerAddItem(cid, 2160, 1)
				end,
				on_accept = function(cid, parameters) -- optional
					doPlayerSendTextMessage(cid, 22, "You must kill: " .. parameters.amount .. " " .. parameters.monster .. ".")
				end,
				on_kill = funcction(cid, parameters) -- optional
					doPlayerSendTextMessage(cid, 22, "You must kill: " .. parameters.amount .. " " .. parameters.monster .. ".")
				end,
				on_finish = function(cid, parameters) -- optional
					doPlayerSendTextMessage(cid, 22, "Congratulations, you finished the quest! You have killed all required " .. parameters.monster .. ".")
				end
			}
		}	
	]]
	
	[1] = {
		{
			monster = "Rat", 
			amount = 5, 
			storage = 9474,
			on_turn_in = function(cid, parameters)
				doPlayerAddExp(cid, 1000)
			end
		},
		{
			monster = "Sheep", 
			amount = 10, 
			storage = 9476,
			on_turn_in = function(cid, parameters)
				doPlayerAddExp(cid, 1000)
			end
		}
	},
	[2] = {
		{
			monster = "Cave Rat", 
			amount = 5, 
			storage = 9477,
			on_turn_in = function(cid, parameters)
				doPlayerAddExp(cid, 1000)
			end
		}
	}
}

wow_like_quests_storages = {}

for k,v in pairs(wow_like_quests) do
	for i,v in ipairs(v) do
		wow_like_quests_storages[v.storage] = v
	end
end

quest_giver.lua
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

-- OTServ event handling functions start
function onCreatureAppear(cid)				npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) 			npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) 	npcHandler:onCreatureSay(cid, type, msg) end
function onThink() 						npcHandler:onThink() end
-- OTServ event handling functions end

function pick_quest(cid, message, keywords, parameters, node)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
	local level = getPlayerLevel(cid)
	for k, v in pairs(wow_like_quests) do
		local required = tonumber(k)
		if(level >= required) then
			for i, v in ipairs(v) do	
				local storage = getPlayerStorageValue(cid, v.storage)
				if(storage == -1) then
					npcHandler:say("I could use you in slaying ".. v.amount .. " ".. string.lower(v.monster) .. ". Would you like to accept the quest?", cid)
					parameters.quest = v
					return true
				elseif(storage == v.amount) then
					setPlayerStorageValue(cid, v.storage, -2)
					npcHandler:say("Woah! I didn't except you to be done so fast. Maybe I have more quests for you.", cid)
					if(v.on_turn_in) then
						v.on_turn_in(cid, v)
					end
					npcHandler:resetNpc()
					return true
				end
			end
		end
	end
	npcHandler:say("I have no quests to offer you at the moment. Come back when your stronger", cid)
	npcHandler:resetNpc()
	return true
end
	
function accept_quest(cid, message, keywords, parameters, node)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
	local quest = node:getParent():getParameters().quest
	local storage = quest.storage
	setPlayerStorageValue(cid, storage, 0)
	npcHandler:say("I'm expecting to hear from you again. Don't dissapoint me.", cid)
	if(quest.on_accept) then quest.on_accept(cid, quest) end
	npcHandler:resetNpc()
	return true
end

-- Keyword "Quest" will pick lowest undone quest for you. 
local questNode = keywordHandler:addKeyword({'quest'}, pick_quest, {})
	questNode:addChildKeyword({'yes'}, accept_quest, {})
	questNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'Too hard for you, eh?'})

	
npcHandler:addModule(FocusModule:new())

onlogin.lua
Code:
registerCreatureEvent(cid, "WOW_LIKE_QUESTS")

onkill.lua
Code:
function onKill(cid, target)
	if(isPlayer(target) == FALSE) then
		for k, v in pairs(wow_like_quests_storages) do
			local storage = getPlayerStorageValue(k)
			if(storage >= 0) then
				if(storage < v.amount) then
					setPlayerStorageValue(cid, k, storage+1)
					if(v.on_kill) then
						v.on_kill(cid, parameters)
					end
				end
				if((storage+1) == v.amount) then
					if(v.on_finish) then
						v.on_finish(cid, parameters)
					end
				end
			end
		end
	end
	return TRUE
end

I haven't tested but if there's anything wrong just tell me and I'll try to fix.
 
Last edited:
does it work on tfs 0.2pl23?
Yes. It should work with even outdated distros.

@Konceptz: Creature Events. It doesn't neccesarily have to be called onkill and onlogin, but I just wrote that so everybody would know where everything goes.
 
Code:
[09/04/2009 17:27:47] Lua Script Error: [Npc interface] 
[09/04/2009 17:27:47] data/npc/scripts/hunter.lua:onCreatureSay

[09/04/2009 17:27:47] data/npc/scripts/hunter.lua:51: attempt to call field 'on_accept' (a nil value)
[09/04/2009 17:27:47] stack traceback:
[09/04/2009 17:27:47] 	data/npc/scripts/hunter.lua:51: in function 'callback'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/keywordhandler.lua:40: in function 'processMessage'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/keywordhandler.lua:168: in function 'processNodeMessage'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/keywordhandler.lua:122: in function 'processMessage'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/npchandler.lua:381: in function 'onCreatureSay'
[09/04/2009 17:27:47] 	data/npc/scripts/hunter.lua:8: in function <data/npc/scripts/hunter.lua:8>
any ideas?
 
Code:
[09/04/2009 17:27:47] Lua Script Error: [Npc interface] 
[09/04/2009 17:27:47] data/npc/scripts/hunter.lua:onCreatureSay

[09/04/2009 17:27:47] data/npc/scripts/hunter.lua:51: attempt to call field 'on_accept' (a nil value)
[09/04/2009 17:27:47] stack traceback:
[09/04/2009 17:27:47] 	data/npc/scripts/hunter.lua:51: in function 'callback'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/keywordhandler.lua:40: in function 'processMessage'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/keywordhandler.lua:168: in function 'processNodeMessage'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/keywordhandler.lua:122: in function 'processMessage'
[09/04/2009 17:27:47] 	data/npc/lib/npcsystem/npchandler.lua:381: in function 'onCreatureSay'
[09/04/2009 17:27:47] 	data/npc/scripts/hunter.lua:8: in function <data/npc/scripts/hunter.lua:8>
any ideas?
Ops, I forgot that they were optional and I should make a check that player is using them. Anyways, made a small change in onkill.lua and questgiver.lua to fix this. It should work properly now.

If you have any more problems let me now.
 
Anyone know how to edit the quest_giver.lua so it works with the latest TFS?
 
this is the error i'm getting:
Code:
[10/04/2009 12:39:14] Lua Script Error: [Npc interface] 
[10/04/2009 12:39:14] data/npc/scripts/quest_giver.lua:onCreatureSay

[10/04/2009 12:39:14] data/npc/scripts/quest_giver.lua:51: attempt to call field 'on_accept' (a nil value)
[10/04/2009 12:39:14] stack traceback:
[10/04/2009 12:39:14] 	data/npc/scripts/quest_giver.lua:51: in function 'callback'
[10/04/2009 12:39:14] 	data/npc/lib/npcsystem/keywordhandler.lua:40: in function 'processMessage'
[10/04/2009 12:39:14] 	data/npc/lib/npcsystem/keywordhandler.lua:168: in function 'processNodeMessage'
[10/04/2009 12:39:14] 	data/npc/lib/npcsystem/keywordhandler.lua:122: in function 'processMessage'
[10/04/2009 12:39:14] 	data/npc/lib/npcsystem/npchandler.lua:381: in function 'onCreatureSay'
[10/04/2009 12:39:14] 	data/npc/scripts/quest_giver.lua:8: in function <data/npc/scripts/quest_giver.lua:8>
 
Anyone know if this script works in the latest TFS? 0.3.3? Looks great! :)
 
Back
Top