Ir para conteúdo
Entre para seguir isso  
Oneshot

Ye Olde Blacksmith

Recommended Posts

Oneshot    24
Oneshot

Ye Olde Blacksmith

NPC de Forja

Bom dia,

 

Este é um NPC bastante simples e muito pedido, que desempenha uma função interessante, ele cria itens pré-configurados se você trouxer certos itens como reagentes (também pré-configurados). É similar a um sistema de forja que desenvolvi a um tempo.

 

Como é um script postado em outros fóruns, fiz um remake para esse fórum, optimizando o código e adicionando um extra que torna o NPC mais realista.

 

local keywordHandler = KeywordHandler:new()local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}


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


local Forge = {}


Forge.Recipes = {
[2390] = {
	items = {
		{2157, 1},
		{2400, 1},
		{2149, 100},
	},
	delay = 60,
}
}


Forge.Time = 21580
Forge.Item = 21581


function _onGreet(cid)
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
if getCreatureStorage(cid, Forge.Item) == -1 then
	npcHandler:setMessage(MESSAGE_GREET, "Hello there, ".. getCreatureName(cid) ..". I am a blacksmith, my father taught me how to make rare {items} from ores.")
	local _string, it = "Actually I can forge ", 1
	for itemid in pairs(Forge.Recipes) do
		_string = _string .. (it == 1 and "" or (it == #Forge.Recipes and " and " or ", ")) .. "{" .. getItemNameById(itemid) .. "}"
		it = it + 1
	end
	_string = _string .. ". Are you interested?"
	addEvent(selfSay, 100, _string, cid)
	talkState[talkUser] = 0
else
	if os.time() > getCreatureStorage(cid, Forge.Time) then
		selfSay("Hello there again, ".. getCreatureName(cid) ..". Your {".. getCreatureStorage(cid, Forge.Item) .."} is not forged yet.")
		selfSay("It will be made at ".. os.date("%c", getCreatureStorage(cid, Forge.Time)) ..".")
	else
		selfSay("Hello there again, ".. getCreatureName(cid) ..". I have made your {".. getCreatureStorage(cid, Forge.Item) .."}.")
		selfSay("Here it is.")
		doPlayerAddItem(cid, getCreatureStorage(cid, Forge.Item), 1)
		doCreatureSetStorage(cid, Forge.Item, -1)
	end
	return false
end
return true
end


function _onCreatureSay(cid, MsgClass, msg)
if(not npcHandler:isFocused(cid)) then
	return false
end




local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

if talkState[talkUser] == 0 then
	msg = msg:lower() or ""
	local itemid = getItemIdByName(msg)
	if type(itemid) == "number" and Forge.Recipes[itemid] then
		selfSay("Well, so you want me to forge {".. getItemNameById(itemid) .."}?", cid)
		talkState[talkUser] = 1
		Forge[talkUser] = {itemid, Forge.Recipes[itemid]}
	else
		selfSay("Sorry, ".. getCreatureName(cid) ..", but I cannot forge this item.", cid)
	end
elseif talkState[talkUser] == 1 then
	if msgcontains(msg, "yes") then
		local _string = "To forge your {".. getItemNameById(Forge[talkUser][1]) .."}, I will need "
		for n = 1, #Forge[talkUser][2].items do
			local s = n == 1 and "" or (n == #Forge[talkUser][2].items and " and " or ", ")

			local this = Forge[talkUser][2].items[n]
			_string = _string .. s .. (this[2] or 1) .. " " .. getItemNameById(this[1])
		end
		_string = _string .. ". Do you have these items?"
		selfSay(_string, cid)
		talkState[talkUser] = 2
	elseif msgcontains(msg, "no") then
		selfSay("May I help you with anything else?", cid)
		talkState[talkUser] = 0
		Forge[talkUser] = nil
	end
elseif talkState[talkUser] == 2 then
	if msgcontains(msg, "yes") then
		local this = Forge[talkUser][2]
		for n = 1, #this.items do
			local tmp = getPlayerItemById(cid, true, this.items[n][1])
			if tmp == nil or math.max(1, tmp.type) < this.items[n][2] then
				selfSay("Sorry, but you do not have enough of {".. getItemNameById(this.items[n][1]) .."}.", cid)
				selfSay("May I help you with anything else?", cid)
				talkState[talkUser] = 0
				Forge[talkUser] = nil
				break
			else
				if n == #this.items then
					for i = 1, #this.items do
						doPlayerRemoveItem(cid, this.items[n][1], this.items[n][2])
					end
					selfSay("You have all the items I need.", cid)
					doCreatureSetStorage(cid, Forge.Time, os.time() + this.delay)
					doCreatureSetStorage(cid, Forge.Item, Forge[talkUser][1])
					selfSay("The forge of {".. getItemNameById(Forge[talkUser][1]) .."} is not instantaneous, so I will need to work until ".. os.date("%c", getCreatureStorage(cid, Forge.Time)) ..".", cid)
					selfSay("Come back later.")
					talkState[talkUser] = nil
					Forge[talkUser] = nil
					npcHandler:releaseFocus(cid)
				end
			end	
		end
	elseif msgcontains(msg, "no") then
		selfSay("Alright, if you change your mind, come back later.", cid)
		talkState[talkUser] = nil
		Forge[talkUser] = nil
		npcHandler:releaseFocus(cid)
	end
end
return true		
end




npcHandler:setCallback(CALLBACK_GREET, _onGreet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, _onCreatureSay)
npcHandler:addModule(FocusModule:new())

 

Espero que gostem.

 

 

 

 

 

 

Editado por Garou

Compartilhar este post


Link para o post
Visitante
Este tópico está impedido de receber novos posts.
Entre para seguir isso  
  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×