9
3

DialogModuleTest.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. --PLACEHOLDER - NON FUNCTIONAL FOR NOW
  177. --Sets a specific language to be used for this dialog
  178. function Dialog.AddLanguage(languageID)
  179. end
  180. --PLACEHOLDER - NON FUNCTIONAL FOR NOW
  181. --Marks this as a "Signature" dialog which changes the background/text color in game
  182. function Dialog.SetIsSignature(bSig)
  183. end
  184. -- Adds an emote to the last added dialog
  185. function Dialog.AddEmote(emote)
  186. local dlg = Dialog.Dialog[#Dialog.Dialog]
  187. dlg.Emote = emote
  188. end
  189. -- Adds options to the last added dialog
  190. function Dialog.AddOption(opt, cb)
  191. local dlg = Dialog.Dialog[#Dialog.Dialog]
  192. local option = {}
  193. option.option = opt
  194. option.callback = cb
  195. option.Requirements = nil
  196. table.insert(dlg.Options, option)
  197. end
  198. -- Adds requirements to the last added option
  199. function Dialog.AddOptionRequirement(req_type, value1, value2, value3)
  200. local dlg = Dialog.Dialog[#Dialog.Dialog]
  201. local option = dlg.Options[#dlg.Options]
  202. if option.Requirements == nil then
  203. option.Requirements = {}
  204. end
  205. local req = {}
  206. req.Type = req_type
  207. req.Value1 = value1
  208. req.Value2 = value2
  209. req.Value3 = value3
  210. table.insert(option.Requirements, req)
  211. end
  212. -- Actually sends the dialog to the player
  213. function Dialog.Start()
  214. -- if NPC or Player are nil then return out so we don't cause a null pointer error on the server
  215. if Dialog.NPC == nil or Dialog.Player == nil then
  216. -- would be great to print a lua error here
  217. return
  218. end
  219. -- create the conversation
  220. local con = CreateConversation()
  221. -- bool to see if we found a dialog to send
  222. local found = false
  223. -- loop through all the dialogs
  224. for key, dlg in pairs(Dialog.Dialog) do
  225. -- Check the dialog requirements if there are any and set the found bool
  226. if dlg.Requirements ~= nil then
  227. found = CheckRequirements(dlg.Requirements)
  228. else
  229. -- no requirements for this dialog so lets use it
  230. found = true
  231. end
  232. -- if we found a dialog to use lets set up the options for it and send it
  233. if found == true then
  234. HadOptions = PrintOptions(dlg, con)
  235. Say(Dialog.NPC,"found true")
  236. if dlg.Emote ~= nil then
  237. Say(Dialog.NPC,"Emote true")
  238. if HadOptions == true then
  239. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text)
  240. else
  241. Say(Dialog.NPC,dlg.Text)
  242. end
  243. if dlg.VOFile ~= nil then
  244. Say(Dialog.NPC,"emote true VO true ")
  245. PlayFlavor(Dialog.NPC, dlg.VOFile, "", dlg.Emote, dlg.VOKey1, dlg.VOKey2, Dialog.Player)
  246. else
  247. Say(Dialog.NPC,"emote true VO false ")
  248. PlayFlavor(Dialog.NPC, "", "", dlg.Emote, 0, 0, Dialog.Player)
  249. end
  250. else
  251. if dlg.VOFile ~= nil then
  252. if HadOptions == true then
  253. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text, dlg.VOFile, dlg.VOKey1, dlg.VOKey2)
  254. else
  255. PlayFlavor(Dialog.NPC, dlg.VOFile, dlg.Text, "", dlg.VOKey1, dlg.VOKey2, Dialog.Player)
  256. end
  257. else
  258. if HadOptions == true then
  259. StartConversation(con, Dialog.NPC, Dialog.Player, dlg.Text)
  260. else
  261. Say(Dialog.NPC,dlg.Text)
  262. end
  263. end
  264. end
  265. -- we sent a dialog so get out of the loop
  266. break;
  267. end
  268. end
  269. -- clear the list to avoid duplicates
  270. Dialog.Dialog = {}
  271. end
  272. return Dialog