Advertisement
7n6

Question NPC reupload

7n6
Feb 2nd, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.47 KB | None | 0 0
  1. --[[
  2.  
  3.  
  4.                             *###########################################################################*
  5.                             #                           -Question NPC by 7n6.-                          #
  6.                             #       If used please give credit or I will break your server Kappa.       #
  7.                             #                  (But seriously don't remove credits...)                  #
  8.                             *###########################################################################*
  9.  
  10.  
  11.  
  12. NPC :
  13.     198 Questions   1   11  0   217900,279300   217900,279300   0   Argent City 3   0   QuestionNPC 0
  14.  
  15. MissionSdk.lua: ConditionsTest :
  16.  
  17.     elseif conditions[i].func == QuestionNpc.CheckAnswer then
  18.         local ret = QuestionNpc.CheckAnswer(conditions[i].p1,conditions[i].p2)
  19.         if ret ~= LUA_TRUE then
  20.             return LUA_FALSE
  21.         end
  22.     elseif conditions[i].func == QuestionNpc.CheckAlreadyAnswered then
  23.         local ret = QuestionNpc.CheckAlreadyAnswered(character,conditions[i].p1)
  24.         if ret ~= LUA_TRUE then
  25.             return LUA_FALSE
  26.         end
  27.     elseif conditions[i].func == QuestionNpc.CheckTime then
  28.         local ret = QuestionNpc.CheckTime(conditions[i].p1)
  29.         if ret ~= LUA_TRUE then
  30.             return LUA_FALSE
  31.         end
  32.     elseif conditions[i].func == QuestionNpc.CheckLevel then
  33.         local ret = QuestionNpc.CheckLevel(character,conditions[i].p1)
  34.         if ret ~= LUA_TRUE then
  35.             return LUA_FALSE
  36.         end
  37.        
  38. MissionSdk.lua: ActionProc:
  39.  
  40.     elseif actions[i].func == QuestionNpc.ErrorPage then
  41.         local ret = QuestionNpc.ErrorPage( character)  
  42.     elseif actions[i].func == QuestionNpc.Reload then
  43.                 local ret = QuestionNpc.Reload()
  44.        
  45. *******************************************************************************************************************************
  46. ###############################################################################################################################
  47. *******************************************************************************************************************************
  48.  
  49.  
  50.         Installation and use:
  51.             Installation:
  52.                 1) Add the above lines to your MissionSdk.lua
  53.                 2) Add a NPC on any map.
  54.                 3) If you change the name of this NPC, change the QuestionNpc.Name table to that NPCs name.
  55.                 4) If you are adding multiple question NPCs, add each name to the QuestionNpc.Name table.
  56.                 5) Edit the QuestionNpc.SavePath variable to your desired save path.
  57.                 6) Load this file using DoFile()
  58.                
  59.             Adding a new question:
  60.                 To add a new question, add to the QuestionNpc.Questions table.
  61.                 Each question can have up to 8 possible answers, but only 1 correct answer.
  62.                 Each possible answer can be up to 42 characters long.
  63.                 You can use MinLevel and MaxLevel to change level requirements (also can be left as nil)
  64.                
  65.             Update the shown questions:
  66.                 To refresh the questions, call the QuestionNpc.Reload() function.
  67.            
  68.             Add questions to an NPC:
  69.                 To add questions to an NPC, use the QuestionNpc.CreateQuestion(int,int) function.
  70.                 The first (int) param is the page the question will be linked from.
  71.                 The second (int) param is the page the question will be on.
  72.                 Remember, you can only have up to 8 questions per page.
  73.                
  74.             Reseting players who have already answered:
  75.                 To reset if players have already answered questions, call the QuestionNpc.ClearResults() function.
  76.            
  77.             Editing messages:
  78.                 To edit the error or success messages, change the :
  79.                     QuestionNpc.FullBag
  80.                     QuestionNpc.WrongAnswer
  81.                     QuestionNpc.CorrectAnswer
  82.                     QuestionNpc.AlreadyAnswered
  83.                 Variables.
  84.                
  85.             Refresh questions automaticly:
  86.                 Set QuestionNpc.RefreshDelay to an integer.
  87.                 This causes questions to refresh after that many seconds.
  88.                 If you want this to also clear records, set QuestionNpc.ClearOnRefresh to true.
  89.                
  90. *******************************************************************************************************************************
  91. ###############################################################################################################################
  92. *******************************************************************************************************************************
  93. ]]
  94. --QuestionNpc Object
  95. QuestionNpc = {}
  96. --SavePath for the table to check if a player has answered the question already.
  97. QuestionNpc.SavePath = GetResPath("script/New Extension/Data/CharsAlreadyAnswered.txt")
  98.  
  99. --Table to check if a player has answered the question already.
  100. QuestionNpc.CharsAlreadyAnswered = {}
  101.  
  102. --Table containing the currently active questions
  103. QuestionNpc.ChosenQuestions = {}
  104.  
  105. --Start page for the question NPC
  106. QuestionNpc.StartPage = 1
  107.  
  108. --Stored error message for the player
  109. QuestionNpc.Error = ""
  110.  
  111. --Stores time of last refresh
  112. QuestionNpc.Time = 0
  113.  
  114. --The 4 possible error messages.
  115. QuestionNpc.FullBag = "Bag is full. Can not answer question."
  116. QuestionNpc.WrongAnswer = "Wrong answer!"
  117. QuestionNpc.CorrectAnswer = "Correct answer!"
  118. QuestionNpc.AlreadyAnswered = "You have already answered this question."
  119.  
  120. --Table containing all Question Npcs names
  121. QuestionNpc.Name = {"Questions"}
  122.  
  123. --Initial text from the npc.
  124. QuestionNpc.Talk = "Hello. I will ask you a question, and if you answer correctly you win a prize!"
  125.  
  126. --Set a delay in seconds for automaticly refreshing questions.
  127. --Doesn't persist after restart (this is intentional, as questions are always refreshed on restart)
  128. --Set to 0 to disable.
  129. QuestionNpc.RefreshDelay = 0
  130.  
  131. --Setting this to true will clear question records after a refresh.
  132. QuestionNpc.ClearOnRefresh = false
  133.  
  134. --Table containing all questions, answers and prizes.
  135. QuestionNpc.Questions = {
  136.     {MaxLevel = 20 , MinLevel = 5,Question = "17 + 1", Answers = {17,18,19,30,20,19,1,1,1},Answer = 2, Reward = {ID = 2, Amount = 1} },
  137.     {MaxLevel = 20 , MinLevel = 5,Question = "What is the capital of England?", Answers = {"London","Ireland","E"},Answer = 1, Reward = {ID = 3, Amount = 1} },
  138.     {MaxLevel = 20 , MinLevel = 5,Question = "18 + 1", Answers = {17,18,19},Answer = 3, Reward = {ID = 4, Amount = 1} },
  139.     {MaxLevel = 20 , MinLevel = 5,Question = "20 + 1", Answers = {17,18,21},Answer = 3, Reward = {ID = 5, Amount = 1} },
  140.     {MaxLevel = 20 , MinLevel = 5,Question = "Who am I?", Answers = {"7n6","Him","You"},Answer = 1, Reward = {ID = 6, Amount = 1} }
  141. }
  142.  
  143. --Initially creating file for the table, to avoid server hang if file not found on first run.
  144. function QuestionNpc.Initial()
  145.     Table = io.open(QuestionNpc.SavePath,"r")
  146.     if Table~=nil then
  147.         QuestionNpc.CharsAlreadyAnswered = table.load(QuestionNpc.SavePath,"r")
  148.         io.close(Table)
  149.     else
  150.         table.save({},QuestionNpc.SavePath,"w")
  151.     end
  152. end
  153. QuestionNpc.Initial()
  154.  
  155. --Npc function.
  156. function QuestionNPC()
  157.     QuestionNpc.RefreshTimer()
  158.     Talk(QuestionNpc.StartPage,QuestionNpc.Talk)
  159.     QuestionNpc.CreateQuestion(QuestionNpc.StartPage,2)
  160.     QuestionNpc.CreateQuestion(QuestionNpc.StartPage,3)
  161.     QuestionNpc.CreateQuestion(QuestionNpc.StartPage,4)
  162. end
  163.  
  164. --Timer to automaticly refresh the questions after X seconds.
  165. function QuestionNpc.RefreshTimer()
  166.     if QuestionNpc.RefreshDelay ~= 0 then
  167.         InitTrigger()
  168.             TriggerCondition( 1, QuestionNpc.CheckTime , QuestionNpc.RefreshDelay )
  169.             TriggerAction( 1, JumpPage,QuestionNpc.StartPage)
  170.             TriggerAction( 1, QuestionNpc.Reload)
  171.             TriggerFailure( 1, JumpPage,QuestionNpc.StartPage)
  172.         Start( GetMultiTrigger(), 1)
  173.     end
  174. end
  175.  
  176. --Checks the current time against the stored time of last refresh.
  177. function QuestionNpc.CheckTime(Seconds)
  178.     local Time = QuestionNpc.SystemTimeNow()
  179.     if  Time >= (QuestionNpc.Time + Seconds) then
  180.         QuestionNpc.Time = Time
  181.         return 1
  182.     else
  183.         return 0
  184.     end
  185. end
  186.  
  187. --Generates the question page.
  188. function QuestionNpc.CreateQuestion(link,page)
  189.     if QuestionNpc.CheckRemainingQuestions() == false then
  190.         return
  191.     end
  192.     local TotalQuestions = table.getn(QuestionNpc.Questions)
  193.     local ChosenQuestion = math.random(1,TotalQuestions)
  194.     while (QuestionNpc.ChosenQuestions[ChosenQuestion] ~= nil) do
  195.         ChosenQuestion = math.random(1,TotalQuestions)
  196.     end
  197.     QuestionNpc.ChosenQuestions[ChosenQuestion] = true
  198.     Text(link,QuestionNpc.Questions[ChosenQuestion].Question,JumpPage,page)
  199.     Talk(page,QuestionNpc.Questions[ChosenQuestion].Question)
  200.     for i,v in pairs(QuestionNpc.Questions[ChosenQuestion].Answers) do
  201.         InitTrigger()
  202.             TriggerCondition( 1, QuestionNpc.CheckLevel,ChosenQuestion)
  203.             TriggerCondition( 1, QuestionNpc.CheckAlreadyAnswered,ChosenQuestion)
  204.             TriggerCondition( 1, QuestionNpc.CheckAnswer,ChosenQuestion,i)
  205.             TriggerAction( 1,  GiveItem, QuestionNpc.Questions[ChosenQuestion].Reward.ID,QuestionNpc.Questions[ChosenQuestion].Reward.Amount,1)
  206.             TriggerAction( 1,  QuestionNpc.ErrorPage)
  207.             TriggerFailure( 1, QuestionNpc.ErrorPage)
  208.         Text(page,v,MultiTrigger,GetMultiTrigger(),1)
  209.     end
  210. end
  211.  
  212. --Prevents a hang if you try to add more questions than you have defined.
  213. function QuestionNpc.CheckRemainingQuestions()
  214.     local TotalQuestions = table.getn(QuestionNpc.Questions)
  215.     for i,v in pairs(QuestionNpc.Questions) do 
  216.         if QuestionNpc.ChosenQuestions[i] == nil then
  217.             return true
  218.         else
  219.             if i == TotalQuestions then
  220.                 return false
  221.             end
  222.         end
  223.     end
  224. end
  225.  
  226. --Checks if the selected answer is correct.
  227. function QuestionNpc.CheckAnswer(QuestionID,AnswerID)
  228.     if QuestionNpc.Questions[QuestionID].Answer == AnswerID then
  229.         QuestionNpc.Error = QuestionNpc.CorrectAnswer
  230.         return 1
  231.     else
  232.         QuestionNpc.Error = QuestionNpc.WrongAnswer
  233.         return 0
  234.     end
  235. end
  236.  
  237. --Checks that the player is high enough to answer the question.
  238. function QuestionNpc.CheckLevel(role,QuestionID)
  239.     local MaxLevel =  QuestionNpc.Questions[QuestionID].MaxLevel
  240.     local MinLevel =  QuestionNpc.Questions[QuestionID].MinLevel
  241.     local Level = GetChaAttr(role,0)
  242.     if MaxLevel == nil then
  243.         MaxLevel = Level
  244.     end
  245.     if MinLevel == nil then
  246.         MinLevel = 0
  247.     end              
  248.     if Level > MaxLevel or Level < MinLevel then
  249.         QuestionNpc.Error = QuestionNpc.MultiLinePadString("You must be between level "..MinLevel.." and "..MaxLevel.." to answer this question.")
  250.         return 0
  251.     end
  252.     return 1
  253. end
  254.  
  255. --Checks that the user has space for the prize, and hasnt previosuly answered.
  256. --Also records an attempt at answering this question.
  257. function QuestionNpc.CheckAlreadyAnswered(role,QuestionID)
  258.     if GetChaFreeBagGridNum ( role ) < 1 then
  259.         QuestionNpc.Error = QuestionNpc.FullBag
  260.         return 0
  261.     end
  262.     QuestionNpc.CharsAlreadyAnswered = table.load(QuestionNpc.SavePath,"r")
  263.     if QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)] == nil then
  264.         QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)] = {}
  265.     end
  266.     if QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)][QuestionID] ~= nil then
  267.         QuestionNpc.Error = QuestionNpc.AlreadyAnswered
  268.         return 0
  269.     else
  270.         QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)][QuestionID] = true
  271.         table.save(QuestionNpc.CharsAlreadyAnswered,QuestionNpc.SavePath,"w")
  272.         return 1
  273.     end
  274. end
  275.  
  276. --Display a message to the user, dependant on their input.
  277. function QuestionNpc.ErrorPage(role)
  278.     HelpInfo(role,0,QuestionNpc.Error)
  279. end
  280.  
  281. --Clears question records, allowing users to answer the same questions again.
  282. function QuestionNpc.ClearResults()
  283.     QuestionNpc.CharsAlreadyAnswered = table.load(QuestionNpc.SavePath,"r")
  284.     table.save({},QuestionNpc.SavePath,"w")
  285. end
  286.  
  287. --Reload question NPC, changes the displayed questions
  288. function QuestionNpc.Reload()
  289.     QuestionNpc.Time = QuestionNpc.SystemTimeNow()
  290.     QuestionNpc.ChosenQuestions = {}
  291.     for i,v in pairs(QuestionNpc.Name) do
  292.         NpcInfoReload(v,QuestionNPC)
  293.     end
  294.     if QuestionNpc.ClearOnRefresh  == true then
  295.         QuestionNpc.ClearResults()
  296.     end
  297. end
  298.  
  299. --Returns the standard Unix time.
  300. function QuestionNpc.SystemTimeNow()
  301.     local TimeNow = os.date("*t")
  302.     local Value = os.time{day = TimeNow.day ,month = TimeNow.month,year = TimeNow.year,hour = TimeNow.hour, min = TimeNow.min,sec = TimeNow.sec}
  303.     return Value
  304. end
  305.  
  306. --Pads string to 42 chars.
  307. function QuestionNpc.PadString(str)
  308.     local Len = string.len(str)
  309.     local Lines = math.ceil(Len/42)
  310.     if Len == 42 then
  311.         return str
  312.     end
  313.     if Len < 42 then
  314.         for i = 1,42-Len do
  315.             str = str.." "
  316.         end
  317.     end
  318.     return str
  319. end
  320.  
  321. --Pad a string to multiple lines
  322. function QuestionNpc.MultiLinePadString (str)
  323.     local words = split(str, " ")
  324.     local finalStrings = {}
  325.     local Lines = 0
  326.     finalStrings[Lines] = ""
  327.     for i,v in words do
  328.         if string.len(finalStrings[Lines]) + string.len(v)  > 42 then
  329.             finalStrings[Lines] = QuestionNpc.PadString(finalStrings[Lines])
  330.             Lines = Lines + 1
  331.             finalStrings[Lines] = ""
  332.         end
  333.         finalStrings[Lines] = finalStrings[Lines]..v.." "
  334.     end
  335.     local finalString = ""
  336.     for i = 0, Lines do
  337.         finalString = finalString..finalStrings[i]
  338.     end
  339.     return finalString
  340. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement