Advertisement
Guest User

NPC

a guest
Apr 14th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.14 KB | None | 0 0
  1. -- Collecting items and monster missions by Limos
  2. local keywordHandler = KeywordHandler:new()
  3. local npcHandler = NpcHandler:new(keywordHandler)
  4. NpcSystem.parseParameters(npcHandler)
  5.  
  6. local talkState = {}
  7.  
  8. function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
  9. function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
  10. function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
  11. function onThink() npcHandler:onThink() end
  12.  
  13. local missions = {
  14. [1] = {
  15. monsters = {
  16. {name = "Rats", count = 20, storage = 21900},
  17. {name = "Rotworms", count = 26, storage = 21901}
  18. },
  19. message = "Thanks, for your next mission kill",
  20. level = 30,
  21. rewarditems = {
  22. {id = 2160, count = 5}
  23. },
  24. rewardexp = 40000
  25. },
  26. [4] = {
  27. monsters = {
  28. {name = "Dragon Lords", count = 25, storage = 21902}
  29. },
  30. message = "Good job, now kill",
  31. level = 70,
  32. rewarditems = {
  33. {id = 2160, count = 25}
  34. },
  35. rewardexp = 200000
  36. },
  37. }
  38.  
  39. local storage = 45551
  40.  
  41. local function getItemsMonstersFromTable(imtable)
  42. local text = ""
  43. for v = 1, #imtable do
  44. local ret = ", "
  45. if v == 1 then
  46. ret = ""
  47. elseif v == #imtable then
  48. ret = " and "
  49. end
  50. text = text .. ret
  51. count = imtable[v].count
  52. if imtable[v].id then
  53. info = getItemInfo(imtable[v].id)
  54. text = text .. (count > 1 and count or info.article).." "..(count > 1 and info.plural or info.name)
  55. else
  56. text = text .. count .." "..imtable[v].name
  57. end
  58. end
  59. return text
  60. end
  61.  
  62. function creatureSayCallback(cid, type, msg)
  63.  
  64. local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
  65.  
  66. if not npcHandler:isFocused(cid) then
  67. return false
  68. end
  69.  
  70. local x = missions[getPlayerStorageValue(cid, storage)]
  71.  
  72. if msgcontains(msg, 'mission') or msgcontains(msg, 'quest') then
  73. if getPlayerStorageValue(cid, storage) == -1 then
  74. selfSay("I have several missions for you, do you accept the challenge?", cid)
  75. talkState[talkUser] = 1
  76. elseif x then
  77. if getPlayerLevel(cid) >= x.level then
  78. local mess = x.items and "get "..getItemsMonstersFromTable(x.items) or "kill "..getItemsMonstersFromTable(x.monsters)
  79. selfSay("Did you "..mess.."?", cid)
  80. talkState[talkUser] = 1
  81. else
  82. selfSay("The mission I gave you is for level "..x.level..", come back later.", cid)
  83. end
  84. else
  85. selfSay("You already did all the missions, great job though.", cid)
  86. npcHandler:releaseFocus(cid)
  87. end
  88. elseif msgcontains(msg, 'yes') and talkState[talkUser] == 1 then
  89. if getPlayerStorageValue(cid, storage) == -1 then
  90. setPlayerStorageValue(cid, storage, 1)
  91. local x = missions[getPlayerStorageValue(cid, storage)]
  92. local imtable = x.items or x.monsters
  93. selfSay(x.message.." "..getItemsMonstersFromTable(imtable)..".", cid)
  94. elseif x then
  95. local imtable = x.items or x.monsters
  96. local amount = 0
  97. for it = 1, #imtable do
  98. local check = x.items and getPlayerItemCount(cid, imtable[it].id) or getPlayerStorageValue(cid, imtable[it].storage)
  99. if check >= imtable[it].count then
  100. amount = amount + 1
  101. end
  102. end
  103. if amount == #imtable then
  104. if x.items then
  105. for it = 1, #x.items do
  106. doPlayerRemoveItem(cid, x.items[it].id, x.items[it].count)
  107. end
  108. end
  109. if x.rewarditems then
  110. for r = 1, #x.rewarditems do
  111. doPlayerAddItem(cid, x.rewarditems[r].id, x.rewarditems[r].count)
  112. end
  113. doPlayerSendTextMessage(cid, MESSAGE_EVENT_DEFAULT, "You received "..getItemsMonstersFromTable(x.rewarditems)..".")
  114. end
  115. if x.rewardexp then
  116. doPlayerAddExp(cid, x.rewardexp)
  117. doPlayerSendTextMessage(cid, MESSAGE_EVENT_DEFAULT, "You received "..x.rewardexp.." experience.")
  118. end
  119. setPlayerStorageValue(cid, storage, getPlayerStorageValue(cid, storage) + 1)
  120. local x = missions[getPlayerStorageValue(cid, storage)]
  121. if x then
  122. selfSay(x.message.." "..getItemsMonstersFromTable(x.items or x.monsters)..".", cid)
  123. else
  124. selfSay("Well done! You did a great job on all your missions.", cid)
  125. end
  126. else
  127. local n = 0
  128. for i = 1, #imtable do
  129. local check = x.items and getPlayerItemCount(cid, imtable[i].id) or getPlayerStorageValue(cid, imtable[i].storage)
  130. if check < imtable[i].count then
  131. n = n + 1
  132. end
  133. end
  134. local text = ""
  135. local c = 0
  136. for v = 1, #imtable do
  137. local check = x.items and getPlayerItemCount(cid, imtable[v].id) or getPlayerStorageValue(cid, imtable[v].storage)
  138. if check < imtable[v].count then
  139. c = c + 1
  140. local ret = ", "
  141. if c == 1 then
  142. ret = ""
  143. elseif c == n then
  144. ret = " and "
  145. end
  146. text = text .. ret
  147. if x.items then
  148. local count, info = imtable[v].count - getPlayerItemCount(cid, imtable[v].id), getItemInfo(imtable[v].id)
  149. text = text .. (count > 1 and count or info.article).." "..(count > 1 and info.plural or info.name)
  150. else
  151. local count = imtable[v].count - (getPlayerStorageValue(cid, imtable[v].storage) + 1)
  152. text = text .. count.." "..imtable[v].name
  153. end
  154. end
  155. end
  156. selfSay(x.items and "You don't have all items, you still need to get "..text.."." or "You didn't kill all monsters, you still need to kill "..text..".", cid)
  157. end
  158. end
  159. talkState[talkUser] = 0
  160. elseif msgcontains(msg, 'no') and talkState[talkUser] == 1 then
  161. selfSay("Oh well, I guess not then.", cid)
  162. end
  163. return true
  164. end
  165.  
  166. npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
  167. npcHandler:setMessage(MESSAGE_WALKAWAY, "Good bye, have a nice day!")
  168. npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
  169. npcHandler:addModule(FocusModule:new())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement