Advertisement
jig487

HiveMindNonAPDev

Jul 22nd, 2021 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.95 KB | None | 0 0
  1. local tArgs = {...}
  2. tArgs[1] = tArgs[1] or 42
  3.  
  4. local ok, errr = xpcall(function()
  5.  
  6. fs.delete("dialogueTools.lua")
  7. print("Downloading dialogueTools.lua... ")
  8. shell.run("pastebin get vcFgTaRV dialogueTools.lua")
  9. print("Done.")
  10.  
  11. local dt = require("dialogueTools")
  12.  
  13. local mon = peripheral.find("monitor")
  14. local modem = peripheral.find("modem")
  15. local oldTerm = term.redirect(mon)
  16. modem.open(tArgs[1])
  17.  
  18. mon.clear()
  19. mon.setTextScale(0.5)
  20. mon.setCursorPos(1,1)
  21.  
  22. local function bug(a)
  23.     mon.write(a)
  24.     local x, y = mon.getCursorPos()
  25.     mon.setCursorPos(1,y + 1)
  26. end
  27.  
  28. --[[
  29. local function newBranch(labelString,responseString,cID,tID)
  30.     return { label = labelString, response = responseString, branchID = cID, targetID = tID }
  31. end
  32. ]]
  33.  
  34. local npcGroups = {
  35.     --assign npcName = "npcGroup"
  36.     elderango = "elderango"
  37. }
  38.  
  39. local dialogueTree = {
  40.     --NPC groups
  41.     elderango = {
  42.         --Text color for group
  43.         color = colors.orange,
  44.         --Dialogue branches
  45.         dt.newBranch(0,0,"",""), --Goodbye
  46.         dt.newBranch(1,10,"...","HEEYY BROTHER"),
  47.  
  48.         --1st main option list
  49.         dt.newBranch(10,20,"How's it going?","BROTHER IT'S NEVER BEEN MORE RADICAL"),
  50.         dt.newBranch(10,10,"I can only say this once per converstation.","CORRECTOMUNDO","prune"), -- Pruning tag
  51.  
  52.         --Mood responses
  53.         dt.newBranch(20,21,"What's so radical?","BROTHER I ACTUALLY LIED. IT'S NOT RADICAL. I'M IN CRIPPLING DEBT BECAUSE I CAN'T MAINTAIN A STEADY JOB TALKING LIKE THIS."),
  54.         dt.newBranch(21,10,"Oh, I'm sorry to hear that...","..."),
  55.         dt.newBranch(20,10,"Gnarly","HEEELL YEAH"),
  56.  
  57.         --Logs quest
  58.         dt.newBranch(10,30,"Do you have anything you need help with?","HOW KIND OF YOU TO ASK. I AM IN NEED OF 32 OAK LOGS. COULD YOU GET THEM FOR ME?"), --If/then tag for quest accepted
  59.         dt.newBranch(30,10,"Sure thing","MUCHO APPRECIANTO, BROTHER.", quest = dt.setQuest("elderangoOakLogs","minecraft:oak_log",32) ), --Quest setter tag
  60.         dt.newBranch(30,10,"Sorry, I'm busy right now.","THAT'S UNDERSTANDABLE."),
  61.  
  62.         dt.newBranch(31,32,"","HEY BROTHER, HOW'S THOSE LOGS COMING?"), --Alternate dialogue start if quest is active. Mutually exclusive option with 33 tag
  63.         dt.newBranch(32,33,"I've got the logs","NICE JOB BROTHER. I KNEW YOU COULD DO IT"), --Mutually exclusive option with 32 tag
  64.         dt.newBranch(32,10,"Still working on it.","ALRIGHT, NO RUSH BRO. TAKE YOUR TIME"), --Quest not complete
  65.         dt.newBranch(33,10,"Any time","SINCE YOU DID ME A SOLID, BRO, TAKE THIS. IT'S THE LEAST I CAN DO"), --Quest is complete, reward tag
  66.  
  67.         dt.newBranch(10,41,"Hey, what are you going to use those logs for?","I PLAN ON BUILDING AN ANIMAL SHELTER FOR THE GNARLY LITTLE CRITTERS WITHOUT HOMES OR CARING OWNERS."), --If/then tag
  68.         dt.newBranch(41,10,"Wow that's actually really sweet","IT'S SWEET AS HELL, BRO. UNLIKE THE PAIN THOSE LITTLE BUDDIES FEEL"),
  69.     },
  70. }
  71.  
  72. local function msgWait()
  73.     local event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  74.     local targetID = message.targetID
  75.     local selectedIndex = message.selectedIndex
  76.     local name = message.name
  77.     return targetID, selectedIndex, name
  78. end
  79.  
  80. local tagList = dt.makeTagList(dialogueTree)
  81. bug("Saving taglist...")
  82. bug(textutils.serialize(tagList))
  83. local f, err = fs.open("branchTags.txt", "w")
  84. assert(f, err)
  85. f.write(textutils.serialize(tagList))
  86. f.close()
  87. bug("Saved tagList")
  88.  
  89. --Main loop
  90. while true do
  91.     local targetID, selectedIndex, name = msgWait()
  92.     local package
  93.     mon.clear()
  94.     mon.setCursorPos(1,1)
  95.     bug("Main: Request from "..name.." for ID "..targetID.." with index "..selectedIndex)
  96.  
  97.     if not npcGroups[name] then --Check if turtle has an assigned group
  98.         package = "missingName"
  99.         modem.transmit(42, tArgs[1], package)
  100.         error("Msg: Missing name "..name)
  101.     else
  102.  
  103.         local group = npcGroups[name]
  104.         bug("Main: Group sucessfully assigned for "..name.." as group: "..group)
  105.  
  106.         if not dt.checkID(dialogueTree,group,targetID) then --Check if requested target ID exists
  107.             package = "missingTID"
  108.             modem.transmit(42, tArgs[1], package)
  109.             error("Msg: missing target ID")
  110.         else
  111.  
  112.             package = { tName = name, color = dialogueTree[group].color, branches = {} }
  113.  
  114.             --Fill package with data
  115.             bug("Main: Filling package with data...")
  116.  
  117.             for i = 1, #dialogueTree[group] do
  118.  
  119.                 local f, err = fs.open("branchTags.txt", "r")
  120.                 assert(f, err)
  121.                 local contents = textutils.unserialize(f.readAll())
  122.                 f.close()
  123.  
  124.                 if dialogueTree[group][i].branchID == targetID then
  125.                     if dialogueTree[group][i].tags then
  126.                         bug("Tags: Branch "..i.." in group "..group.." has tags: "..textutils.serialize(dialogueTree[group][i].tags))
  127.                        
  128.                         for k in pairs(contents[group][i][1]) do
  129.                             if k == "prune" then
  130.                                 bug("Tags: Found prune tag in tagList for branch "..i)
  131.                                 if contents[group][i][1].prune == false then
  132.                                     if selectedIndex == i then
  133.                                         contents[group][i][1].prune = true
  134.                                         local f, err = fs.open("branchTags.txt", "w")
  135.                                         assert(f, err)
  136.                                         f.write(textutils.serialize(contents))
  137.                                         f.close()
  138.                                         bug("Tags: prune branch and set prune to true")
  139.                                     else
  140.                                         bug("Fill: Added branch "..i)
  141.                                         table.insert(package.branches, dialogueTree[group][i])
  142.                                         package.branches[#package.branches].index = i
  143.                                     end
  144.                                 end
  145.                             elseif k == "quest" then
  146.                                 bug("Tags: Found quest tag in tagList for branch "..i)
  147.                                 local questName = contents[groupName][i][1].quest.questName
  148.                                 if contents.quests[questName].isActive == false then
  149.                                     if selectedIndex == i then
  150.                                         contents.quests[questName].isActive = true
  151.                                         local f, err = fs.open("branchTags.txt", "w")
  152.                                         assert(f, err)
  153.                                         f.write(textutils.serialize(contents))
  154.                                         f.close()
  155.                                         bug("Tags: Quest "..questName.." set to active and pruned")
  156.                                     else
  157.                                         bug("Fill: Added branch "..i)
  158.                                         table.insert(package.branches, dialogueTree[group][i])
  159.                                         package.branches[#package.branches].index = i    
  160.                                     end
  161.                                 end
  162.                             end
  163.                         end
  164.                         bug("Tags: Processed tags for branch "..i)
  165.                     else
  166.                         bug("Fill: Added branch "..i)
  167.                         table.insert(package.branches, dialogueTree[group][i])
  168.                         package.branches[#package.branches].index = i
  169.                     end
  170.                 end
  171.             end
  172.  
  173.             bug("Main: Sending package with "..#package.branches.." branches")
  174.  
  175.             --Send package
  176.             modem.transmit(42, tArgs[1], package)
  177.         end
  178.     end
  179. end
  180.  
  181. end, debug.traceback) if not ok then printError(errr) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement