Go-piratia Posted November 4, 2023 Share Posted November 4, 2023 --[[ *###########################################################################* # -Question NPC by 7n6.- # # If used please give credit or I will break your server Kappa. # # (But seriously don't remove credits...) # *###########################################################################* NPC : 198 Questions 1 11 0 217900,279300 217900,279300 0 Argent City 3 0 QuestionNPC 0 MissionSdk.lua: ConditionsTest : elseif conditions[i].func == QuestionNpc.CheckAnswer then local ret = QuestionNpc.CheckAnswer(conditions[i].p1,conditions[i].p2) if ret ~= LUA_TRUE then return LUA_FALSE end elseif conditions[i].func == QuestionNpc.CheckAlreadyAnswered then local ret = QuestionNpc.CheckAlreadyAnswered(character,conditions[i].p1) if ret ~= LUA_TRUE then return LUA_FALSE end elseif conditions[i].func == QuestionNpc.CheckTime then local ret = QuestionNpc.CheckTime(conditions[i].p1) if ret ~= LUA_TRUE then return LUA_FALSE end elseif conditions[i].func == QuestionNpc.CheckLevel then local ret = QuestionNpc.CheckLevel(character,conditions[i].p1) if ret ~= LUA_TRUE then return LUA_FALSE end MissionSdk.lua: ActionProc: elseif actions[i].func == QuestionNpc.ErrorPage then local ret = QuestionNpc.ErrorPage( character) elseif actions[i].func == QuestionNpc.Reload then local ret = QuestionNpc.Reload() ******************************************************************************************************************************* ############################################################################################################################### ******************************************************************************************************************************* Installation and use: Installation: 1) Add the above lines to your MissionSdk.lua 2) Add a NPC on any map. 3) If you change the name of this NPC, change the QuestionNpc.Name table to that NPCs name. 4) If you are adding multiple question NPCs, add each name to the QuestionNpc.Name table. 5) Edit the QuestionNpc.SavePath variable to your desired save path. 6) Load this file using DoFile() Adding a new question: To add a new question, add to the QuestionNpc.Questions table. Each question can have up to 8 possible answers, but only 1 correct answer. Each possible answer can be up to 42 characters long. You can use MinLevel and MaxLevel to change level requirements (also can be left as nil) Update the shown questions: To refresh the questions, call the QuestionNpc.Reload() function. Add questions to an NPC: To add questions to an NPC, use the QuestionNpc.CreateQuestion(int,int) function. The first (int) param is the page the question will be linked from. The second (int) param is the page the question will be on. Remember, you can only have up to 8 questions per page. Reseting players who have already answered: To reset if players have already answered questions, call the QuestionNpc.ClearResults() function. Editing messages: To edit the error or success messages, change the : QuestionNpc.FullBag QuestionNpc.WrongAnswer QuestionNpc.CorrectAnswer QuestionNpc.AlreadyAnswered Variables. Refresh questions automaticly: Set QuestionNpc.RefreshDelay to an integer. This causes questions to refresh after that many seconds. If you want this to also clear records, set QuestionNpc.ClearOnRefresh to true. ******************************************************************************************************************************* ############################################################################################################################### ******************************************************************************************************************************* ]] --QuestionNpc Object QuestionNpc = {} --SavePath for the table to check if a player has answered the question already. QuestionNpc.SavePath = GetResPath("script/New Extension/Data/CharsAlreadyAnswered.txt") --Table to check if a player has answered the question already. QuestionNpc.CharsAlreadyAnswered = {} --Table containing the currently active questions QuestionNpc.ChosenQuestions = {} --Start page for the question NPC QuestionNpc.StartPage = 1 --Stored error message for the player QuestionNpc.Error = "" --Stores time of last refresh QuestionNpc.Time = 0 --The 4 possible error messages. QuestionNpc.FullBag = "Bag is full. Can not answer question." QuestionNpc.WrongAnswer = "Wrong answer!" QuestionNpc.CorrectAnswer = "Correct answer!" QuestionNpc.AlreadyAnswered = "You have already answered this question." --Table containing all Question Npcs names QuestionNpc.Name = {"Questions"} --Initial text from the npc. QuestionNpc.Talk = "Hello. I will ask you a question, and if you answer correctly you win a prize!" --Set a delay in seconds for automaticly refreshing questions. --Doesn't persist after restart (this is intentional, as questions are always refreshed on restart) --Set to 0 to disable. QuestionNpc.RefreshDelay = 0 --Setting this to true will clear question records after a refresh. QuestionNpc.ClearOnRefresh = false --Table containing all questions, answers and prizes. QuestionNpc.Questions = { {MaxLevel = 20 , MinLevel = 5,Question = "17 + 1", Answers = {17,18,19,30,20,19,1,1,1},Answer = 2, Reward = {ID = 2, Amount = 1} }, {MaxLevel = 20 , MinLevel = 5,Question = "What is the capital of England?", Answers = {"London","Ireland","E"},Answer = 1, Reward = {ID = 3, Amount = 1} }, {MaxLevel = 20 , MinLevel = 5,Question = "18 + 1", Answers = {17,18,19},Answer = 3, Reward = {ID = 4, Amount = 1} }, {MaxLevel = 20 , MinLevel = 5,Question = "20 + 1", Answers = {17,18,21},Answer = 3, Reward = {ID = 5, Amount = 1} }, {MaxLevel = 20 , MinLevel = 5,Question = "Who am I?", Answers = {"7n6","Him","You"},Answer = 1, Reward = {ID = 6, Amount = 1} } } --Initially creating file for the table, to avoid server hang if file not found on first run. function QuestionNpc.Initial() Table = io.open(QuestionNpc.SavePath,"r") if Table~=nil then QuestionNpc.CharsAlreadyAnswered = table.load(QuestionNpc.SavePath,"r") io.close(Table) else table.save({},QuestionNpc.SavePath,"w") end end QuestionNpc.Initial() --Npc function. function QuestionNPC() QuestionNpc.RefreshTimer() Talk(QuestionNpc.StartPage,QuestionNpc.Talk) QuestionNpc.CreateQuestion(QuestionNpc.StartPage,2) QuestionNpc.CreateQuestion(QuestionNpc.StartPage,3) QuestionNpc.CreateQuestion(QuestionNpc.StartPage,4) end --Timer to automaticly refresh the questions after X seconds. function QuestionNpc.RefreshTimer() if QuestionNpc.RefreshDelay ~= 0 then InitTrigger() TriggerCondition( 1, QuestionNpc.CheckTime , QuestionNpc.RefreshDelay ) TriggerAction( 1, JumpPage,QuestionNpc.StartPage) TriggerAction( 1, QuestionNpc.Reload) TriggerFailure( 1, JumpPage,QuestionNpc.StartPage) Start( GetMultiTrigger(), 1) end end --Checks the current time against the stored time of last refresh. function QuestionNpc.CheckTime(Seconds) local Time = QuestionNpc.SystemTimeNow() if Time >= (QuestionNpc.Time + Seconds) then QuestionNpc.Time = Time return 1 else return 0 end end --Generates the question page. function QuestionNpc.CreateQuestion(link,page) if QuestionNpc.CheckRemainingQuestions() == false then return end local TotalQuestions = table.getn(QuestionNpc.Questions) local ChosenQuestion = math.random(1,TotalQuestions) while (QuestionNpc.ChosenQuestions[ChosenQuestion] ~= nil) do ChosenQuestion = math.random(1,TotalQuestions) end QuestionNpc.ChosenQuestions[ChosenQuestion] = true Text(link,QuestionNpc.Questions[ChosenQuestion].Question,JumpPage,page) Talk(page,QuestionNpc.Questions[ChosenQuestion].Question) for i,v in pairs(QuestionNpc.Questions[ChosenQuestion].Answers) do InitTrigger() TriggerCondition( 1, QuestionNpc.CheckLevel,ChosenQuestion) TriggerCondition( 1, QuestionNpc.CheckAlreadyAnswered,ChosenQuestion) TriggerCondition( 1, QuestionNpc.CheckAnswer,ChosenQuestion,i) TriggerAction( 1, GiveItem, QuestionNpc.Questions[ChosenQuestion].Reward.ID,QuestionNpc.Questions[ChosenQuestion].Reward.Amount,1) TriggerAction( 1, QuestionNpc.ErrorPage) TriggerFailure( 1, QuestionNpc.ErrorPage) Text(page,v,MultiTrigger,GetMultiTrigger(),1) end end --Prevents a hang if you try to add more questions than you have defined. function QuestionNpc.CheckRemainingQuestions() local TotalQuestions = table.getn(QuestionNpc.Questions) for i,v in pairs(QuestionNpc.Questions) do if QuestionNpc.ChosenQuestions[i] == nil then return true else if i == TotalQuestions then return false end end end end --Checks if the selected answer is correct. function QuestionNpc.CheckAnswer(QuestionID,AnswerID) if QuestionNpc.Questions[QuestionID].Answer == AnswerID then QuestionNpc.Error = QuestionNpc.CorrectAnswer return 1 else QuestionNpc.Error = QuestionNpc.WrongAnswer return 0 end end --Checks that the player is high enough to answer the question. function QuestionNpc.CheckLevel(role,QuestionID) local MaxLevel = QuestionNpc.Questions[QuestionID].MaxLevel local MinLevel = QuestionNpc.Questions[QuestionID].MinLevel local Level = GetChaAttr(role,0) if MaxLevel == nil then MaxLevel = Level end if MinLevel == nil then MinLevel = 0 end if Level > MaxLevel or Level < MinLevel then QuestionNpc.Error = QuestionNpc.MultiLinePadString("You must be between level "..MinLevel.." and "..MaxLevel.." to answer this question.") return 0 end return 1 end --Checks that the user has space for the prize, and hasnt previosuly answered. --Also records an attempt at answering this question. function QuestionNpc.CheckAlreadyAnswered(role,QuestionID) if GetChaFreeBagGridNum ( role ) < 1 then QuestionNpc.Error = QuestionNpc.FullBag return 0 end QuestionNpc.CharsAlreadyAnswered = table.load(QuestionNpc.SavePath,"r") if QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)] == nil then QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)] = {} end if QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)][QuestionID] ~= nil then QuestionNpc.Error = QuestionNpc.AlreadyAnswered return 0 else QuestionNpc.CharsAlreadyAnswered[GetRoleID(role)][QuestionID] = true table.save(QuestionNpc.CharsAlreadyAnswered,QuestionNpc.SavePath,"w") return 1 end end --Display a message to the user, dependant on their input. function QuestionNpc.ErrorPage(role) HelpInfo(role,0,QuestionNpc.Error) end --Clears question records, allowing users to answer the same questions again. function QuestionNpc.ClearResults() QuestionNpc.CharsAlreadyAnswered = table.load(QuestionNpc.SavePath,"r") table.save({},QuestionNpc.SavePath,"w") end --Reload question NPC, changes the displayed questions function QuestionNpc.Reload() QuestionNpc.Time = QuestionNpc.SystemTimeNow() QuestionNpc.ChosenQuestions = {} for i,v in pairs(QuestionNpc.Name) do NpcInfoReload(v,QuestionNPC) end if QuestionNpc.ClearOnRefresh == true then QuestionNpc.ClearResults() end end --Returns the standard Unix time. function QuestionNpc.SystemTimeNow() local TimeNow = os.date("*t") local Value = os.time{day = TimeNow.day ,month = TimeNow.month,year = TimeNow.year,hour = TimeNow.hour, min = TimeNow.min,sec = TimeNow.sec} return Value end --Pads string to 42 chars. function QuestionNpc.PadString(str) local Len = string.len(str) local Lines = math.ceil(Len/42) if Len == 42 then return str end if Len < 42 then for i = 1,42-Len do str = str.." " end end return str end --Pad a string to multiple lines function QuestionNpc.MultiLinePadString (str) local words = split(str, " ") local finalStrings = {} local Lines = 0 finalStrings[Lines] = "" for i,v in words do if string.len(finalStrings[Lines]) + string.len(v) > 42 then finalStrings[Lines] = QuestionNpc.PadString(finalStrings[Lines]) Lines = Lines + 1 finalStrings[Lines] = "" end finalStrings[Lines] = finalStrings[Lines]..v.." " end local finalString = "" for i = 0, Lines do finalString = finalString..finalStrings[i] end return finalString end Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.