DialogModuleTest.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. Dialog = { }
  2. -- defines for the requirement types
  3. REQ_RACE = 1
  4. REQ_CLASS = 2
  5. REQ_LEVEL = 3
  6. REQ_LEVEL_GREATER_OR_EQUAL = 4
  7. REQ_LEVEL_LESS_OR_EQUAL = 5
  8. REQ_QUEST_ELIGIBLE = 6
  9. REQ_QUEST_ON_STEP = 7
  10. REQ_QUEST_BEFORE_STEP = 8
  11. REQ_QUEST_PAST_STEP = 9
  12. REQ_QUEST_HAS_QUEST = 10
  13. REQ_QUEST_DOESNT_HAVE_QUEST = 11
  14. REQ_QUEST_NOT_ON_STEP = 12
  15. REQ_QUEST_HAS_COMPLETED_QUEST = 13
  16. REQ_QUEST_NOT_HAS_COMPLETED_QUEST = 14
  17. REQ_TEMP_VAR_NOT_SET = 15
  18. REQ_TEMP_VAR_SET = 16
  19. REQ_LUA_HISTORY_SET = 17
  20. REQ_LUA_HISTORY_NOT_SET = 18
  21. REQ_LOCATION_ID = 19
  22. REQ_TSLEVEL = 20
  23. REQ_TSLEVEL_GREATER_OR_EQUAL = 21
  24. REQ_TSLEVEL_LESS_OR_EQUAL = 22
  25. -- Dialog variables
  26. Dialog.NPC = nil
  27. Dialog.Player = nil
  28. Dialog.Dialog = {}
  29. -- private functions go at the top
  30. -- Checks the requirement table and returns false if the player failed a check or true if they pass ALL checks
  31. local function CheckRequirements(reqs)
  32. local ret = true
  33. -- if no requirements then return true
  34. if reqs ~= nil then
  35. -- loop through the requirements and check to see if the player fails them, if so set the return value
  36. -- to false and break the loop as we don't need to check any of the other requirements
  37. for k, v in pairs(reqs) do
  38. if v.Type == REQ_RACE and GetRace(Dialog.Player) ~= v.Value1 then
  39. ret = false
  40. break
  41. elseif v.Type == REQ_CLASS and GetClass(Dialog.Player) ~= v.Value1 then
  42. ret = false
  43. break
  44. elseif v.Type == REQ_LEVEL and GetLevel(Dialog.Player) ~= v.Value1 then
  45. ret = false
  46. break
  47. elseif v.Type == REQ_LEVEL_GREATER_OR_EQUAL and GetLevel(Dialog.Player) < v.Value1 then
  48. ret = false
  49. break
  50. elseif v.Type == REQ_LEVEL_LESS_OR_EQUAL and GetLevel(Dialog.Player) > v.Value1 then
  51. ret = false
  52. break
  53. elseif v.Type == REQ_QUEST_ELIGIBLE and not CanReceiveQuest(Dialog.Player, v.Value1) then
  54. ret = false
  55. break
  56. elseif v.Type == REQ_QUEST_ON_STEP and GetQuestStep(Dialog.Player, v.Value1) ~= v.Value2 then
  57. ret = false
  58. break
  59. elseif v.Type == REQ_QUEST_BEFORE_STEP and GetQuestStep(Dialog.Player, v.Value1) >= v.Value2 then
  60. ret = false
  61. break
  62. elseif v.Type == REQ_QUEST_PAST_STEP and GetQuestStep(Dialog.Player, v.Value1) <= v.Value2 then
  63. ret = false
  64. break
  65. elseif v.Type == REQ_QUEST_HAS_QUEST and not HasQuest(Dialog.Player, v.Value1) then
  66. ret = false
  67. break
  68. elseif v.Type == REQ_QUEST_DOESNT_HAVE_QUEST and HasQuest(Dialog.Player, v.Value1) then
  69. ret = false
  70. break
  71. elseif v.Type == REQ_QUEST_NOT_ON_STEP and GetQuestStep(Dialog.Player, v.Value1) == v.Value2 then
  72. ret = false
  73. break
  74. elseif v.Type == REQ_QUEST_HAS_COMPLETED_QUEST and not HasCompletedQuest(Dialog.Player, v.Value1) then
  75. ret = false
  76. break
  77. elseif v.Type == REQ_QUEST_NOT_HAS_COMPLETED_QUEST and HasCompletedQuest(Dialog.Player, v.Value1) then
  78. ret = false
  79. break
  80. elseif v.Type == REQ_TEMP_VAR_NOT_SET and GetTempVariable(Dialog.Player, v.Value1) == v.Value2 then
  81. ret = false
  82. break
  83. elseif v.Type == REQ_TEMP_VAR_SET and GetTempVariable(Dialog.Player, v.Value1) ~= v.Value2 then
  84. ret = false
  85. break
  86. elseif v.Type == REQ_LUA_HISTORY_SET then
  87. local value1, value2 = GetPlayerHistory(Dialog.Player, v.Value1)
  88. if v.Value2 ~= value1 then
  89. ret = false
  90. break
  91. elseif v.Value3 ~= nil and v.Value3 ~= value2 then
  92. ret = false
  93. break
  94. end
  95. elseif v.Type == REQ_LUA_HISTORY_NOT_SET then
  96. local value1, value2 = GetPlayerHistory(Dialog.Player, v.Value1)
  97. if v.Value2 == value1 then
  98. ret = false
  99. break
  100. elseif v.Value3 ~= nil and v.Value3 == value2 then
  101. ret = false
  102. break
  103. end
  104. elseif v.Type == REQ_LOCATION_ID and GetSpawnLocationID(Dialog.NPC) ~= v.Value1 then
  105. ret = false
  106. break
  107. elseif v.Type == REQ_TSLEVEL and GetTradeskillLevel(Dialog.Player) ~= v.Value1 then
  108. ret = false
  109. break
  110. elseif v.Type == REQ_TSLEVEL_GREATER_OR_EQUAL and GetTradeskillLevel(Dialog.Player) < v.Value1 then
  111. ret = false
  112. break
  113. elseif v.Type == REQ_TSLEVEL_LESS_OR_EQUAL and GetTradeskillLevel(Dialog.Player) > v.Value1 then
  114. ret = false
  115. break
  116. end
  117. end
  118. end
  119. return ret
  120. end
  121. -- Helper function to check requirements on options and actually use the option
  122. local function PrintOptions(dlg, con)
  123. -- loop through the options and check their requirements, if they pass add the option
  124. for k, v in pairs(dlg.Options) do
  125. if CheckRequirements(v.Requirements) == true then
  126. AddConversationOption(con, v.option, v.callback)
  127. ret=true
  128. end
  129. end
  130. return ret
  131. end
  132. -- Actual member functions are below, these are what you call in the other scripts
  133. -- Set up the spawn pointers to use
  134. function Dialog.New(NPC, Player)
  135. Dialog.NPC = NPC
  136. Dialog.Player = Player
  137. end
  138. -- Test function, left it in for now as it may be useful but it is just a Say()
  139. function Dialog.Test(Msg)
  140. if Dialog.NPC ~= nil then
  141. Say(Dialog.NPC, Msg)
  142. end
  143. end
  144. -- Add a dialog
  145. function Dialog.AddDialog(text)
  146. local dlg = {}
  147. dlg.Text = text
  148. dlg.Options = {}
  149. dlg.Requirements = nil
  150. dlg.VOFile = nil
  151. dlg.VOKey1 = nil
  152. dlg.VOKey2 = nil
  153. dlg.Emote = nil
  154. table.insert(Dialog.Dialog, dlg)
  155. end
  156. -- Adds requirements to the last added dialog
  157. function Dialog.AddRequirement(req_type, value1, value2, value3)
  158. local dlg = Dialog.Dialog[#Dialog.Dialog]
  159. if dlg.Requirements == nil then
  160. dlg.Requirements = {}
  161. end
  162. local req = {}
  163. req.Type = req_type
  164. req.Value1 = value1
  165. req.Value2 = value2
  166. req.Value3 = value3
  167. table.insert(dlg.Requirements, req)
  168. end
  169. -- Adds a voiceover to the last added dialog
  170. function Dialog.AddVoiceover(file, key1, key2)
  171. local dlg = Dialog.Dialog[#Dialog.Dialog]
  172. dlg.VOFile = file
  173. dlg.VOKey1 = key1
  174. dlg.VOKey2 = key2
  175. end
  176. -- Adds an emote to the last added dialog
  177. function Dialog.AddEmote(emote)
  178. local dlg = Dialog.Dialog[#Dialog.Dialog]
  179. dlg.Emote = emote
  180. end
  181. -- Adds options to the last added dialog
  182. function Dialog.AddOption(opt, cb)
  183. local dlg = Dialog.Dialog[#Dialog.Dialog]
  184. local option = {}
  185. option.option = opt
  186. option.callback = cb
  187. option.Requirements = nil
  188. table.insert(dlg.Options, option)
  189. end
  190. -- Adds requirements to the last added option
  191. function Dialog.AddOptionRequirement(req_type, value1, value2, value3)
  192. local dlg = Dialog.Dialog[#Dialog.Dialog]
  193. local option = dlg.Options[#dlg.Options]
  194. if option.Requirements == nil then
  195. option.Requirements = {}
  196. end
  197. local req = {}
  198. req.Type = req_type
  199. req.Value1 = value1
  200. req.Value2 = value2
  201. req.Value3 = value3
  202. table.insert(option.Requirements, req)
  203. end
  204. -- Actually sends the dialog to the player
  205. function Dialog.Start()
  206. -- if NPC or Player are nil then return out so we don't cause a null pointer error on the server
  207. if Dialog.NPC == nil or Dialog.Player == nil then
  208. -- would be great to print a lua error here
  209. return
  210. end
  211. -- create the conversation
  212. local con = CreateConversation()
  213. -- bool to see if we found a dialog to send
  214. local found = false
  215. -- loop through all the dialogs
  216. for key, dlg in pairs(Dialog.Dialog) do
  217. -- Check the dialog requirements if there are any and set the found bool
  218. if dlg.Requirements ~= nil then
  219. found = CheckRequirements(dlg.Requirements)
  220. else
  221. -- no requirements for this dialog so lets use it
  222. found = true
  223. end
  224. -- if we found a dialog to use lets set up the options for it and send it
  225. if found == true then
  226. HadOptions = PrintOptions(dlg, con)
  227. Say(Dialog.NPC,"found true")
  228. if dlg.Emote ~= nil then
  229. Say(Dialog.NPC,"Emote true")
  230. if HadOptions == true then
  231. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text)
  232. else
  233. Say(Dialog.NPC,dlg.Text)
  234. end
  235. if dlg.VOFile ~= nil then
  236. Say(Dialog.NPC,"emote true VO true ")
  237. PlayFlavor(Dialog.NPC, dlg.VOFile, "", dlg.Emote, dlg.VOKey1, dlg.VOKey2, Dialog.Player)
  238. else
  239. Say(Dialog.NPC,"emote true VO false ")
  240. PlayFlavor(Dialog.NPC, "", "", dlg.Emote, 0, 0, Dialog.Player)
  241. end
  242. if HadOptions == true then
  243. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text)
  244. else
  245. Say(Dialog.NPC,dlg.Text)
  246. end
  247. else
  248. if dlg.VOFile ~= nil then
  249. if HadOptions == true then
  250. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text, dlg.VOFile, dlg.VOKey1, dlg.VOKey2)
  251. else
  252. PlayFlavor(Dialog.NPC, dlg.VOFile, dlg.Text, "", dlg.VOKey1, dlg.VOKey2, Dialog.Player)
  253. end
  254. else
  255. if HadOptions == true then
  256. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text)
  257. else
  258. Say(Dialog.NPC,dlg.Text)
  259. end
  260. end
  261. end
  262. -- we sent a dialog so get out of the loop
  263. break;
  264. end
  265. end
  266. -- clear the list to avoid duplicates
  267. Dialog.Dialog = {}
  268. end
  269. return Dialog