NPC:
Kod:
_state = 0
_count = 0
_index = 0
_delay = 500
local items = {
[0] = {
name = 'blank rune',
id = 2260,
subtype = -1,
sell = 10,
buy = -1},
[1] = {
name = 'life ring',
id = 2168,
subtype = -1,
sell = 900,
buy = -1},
[2] = {
name = 'crystal ball',
id = 2192,
subtype = -1,
sell = 530,
buy = 190},
[3] = {
name = 'life crystal',
id = 2177,
subtype = -1,
sell = 900,
buy = 85},
[4] = {
name = 'mind stone',
id = 2178,
subtype = -1,
sell = 900,
buy = 170}
}
local function onActionItem(action)
if ((action == 'buy' and items[_index].sell == -1) or
(action == 'sell' and items[_index].buy == -1)) then
return
end
amount = ''
suffix = ''
plural = 'a'
if (_count > 1) then
amount = ' ' .. tostring(_count)
suffix = 's'
plural = ''
end
cost = items[_index].buy
if (action == 'buy') then
cost = items[_index].sell
end
selfSay('Do you want to ' .. action .. ' ' .. plural .. amount .. ' ' .. items[_index].name .. suffix .. ' for ' .. cost .. ' gold?')
end
function getNext()
nextPlayer = getQueuedPlayer()
if (nextPlayer ~= nil) then
if (getDistanceToCreature(nextPlayer) <= 4) then
updateNpcIdle()
setNpcFocus(nextPlayer)
selfSay('Hi there ' .. getCreatureName(nextPlayer) .. '.', _delay * 3)
return
else
getNext()
end
end
setNpcFocus(0)
resetNpcIdle()
end
function _selfSay(message)
selfSay(message, _delay)
updateNpcIdle()
end
--
function onCreatureAppear(creature)
return 1
end
function onCreatureDisappear(cid)
if (getNpcFocus() == cid) then
selfSay('See you.', _delay)
getNext()
else
unqueuePlayer(cid)
end
return 1
end
function onCreatureMove(cid, oldPos, newPos)
if (getNpcFocus() == cid) then
faceCreature(cid)
if (oldPos.z ~= newPos.z or getDistanceToCreature(cid) > 4) then
selfSay('See you.', _delay)
getNext()
end
else
if (oldPos.z ~= newPos.z or getDistanceToCreature(cid) > 4) then
unqueuePlayer(cid)
end
end
return 1
end
function onCreatureSay(cid, type, msg)
if (getNpcFocus() == 0) then
if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
updateNpcIdle()
setNpcFocus(cid)
selfSay('Hi there ' .. getCreatureName(cid) .. '.', _delay)
end
elseif (getNpcFocus() ~= cid) then
if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
selfSay('Just wait, ' .. getCreatureName(cid) .. '.', _delay)
queuePlayer(cid)
end
else
if (msgcontains(msg, 'bye')) then
selfSay('See you.', _delay)
getNext()
elseif (_state == 1) then
if (msgcontains(msg, 'yes')) then
if(doPlayerRemoveMoney(cid, items[_index].sell)) then
for i = 1, _count do
if items[_index].subtype < 1 then
costam = 1
else
costam = items[_index].subtype
end
doPlayerAddItem(cid, items[_index].id, costam)
end
selfSay('Here you are.', _delay)
else
selfSay('Come back, when you have enough money.', _delay)
end
updateNpcIdle()
else
selfSay('Hmm, but next time.', _delay)
end
_state = 0
elseif (_state == 2) then
if (msgcontains(msg, 'yes')) then
if (doPlayerRemoveItem(cid, items[_index].id, _count, items[_index].subtype) == 1) then
doPlayerAddMoney(cid, items[_index].buy * _count)
selfSay('Ok. Here is your money.')
else
if (count > 1) then
selfSay('Sorry, you do not have so many.', _delay)
else
selfSay('Sorry, you do not have one.', _delay)
end
end
updateNpcIdle()
else
selfSay('Maybe next time.', _delay)
end
_state = 0
else
for n = 0, table.getn(items) do
if (msgcontains(msg, items[n].name) or msgcontains(msg, items[n].name .. "s")) then
_count = getCount(msg)
_index = n
if (msgcontains(msg, 'sell')) then
onActionItem('sell')
_state = 2
else
onActionItem('buy')
_state = 1
end
updateNpcIdle()
break
end
return 1
end
return 1
end
return 1
end
return 1
end
function onThink()
if (getNpcFocus() ~= 0) then
if (isNpcIdle()) then
selfSay('See you.', _delay)
getNext()
end
end
return 1
end
Zakładki