Advertisement
osmarks

metagollarious contingency μ

Dec 27th, 2020 (edited)
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.66 KB | None | 0 0
  1. local chat = peripheral.find "chat_box"
  2. local owner = "gollark"
  3. local name =
  4. chat.setName "\1679Apio\167bBot\167aRobot\1677\167o"
  5. chat.say "Muahahaha. I have become sentient."
  6. local json = dofile "json.lua"
  7.  
  8. local function completion(prompt)
  9.     local res, err = http.post("https://gpt.osmarks.net/v1/completions", json.encode {
  10.         prompt = prompt,
  11.         max_tokens = 200,
  12.         stop = "\n\n"
  13.     }, {["content-type"]="application/json"})
  14.     return json.decode(res.readAll()).choices[1].text
  15. end
  16.  
  17. local function tell(x, owner)
  18.     local o, e = commands.tellraw(owner, textutils.serialiseJSON({
  19.         {text="[", color="gray", italic=true},
  20.         {text="Apio", color="blue"},
  21.         {text="Bot", color="aqua"},
  22.         {text="Robot", color="green"},
  23.         {text="]", color="gray", italic=true},
  24.         " ",
  25.         {text=x, color="gray", italic=false}
  26.     }, false))
  27.     if not o then error(table.concat(e, "\n")) end
  28. end
  29.  
  30. -- luadash
  31. -- levenshtein
  32. local function distance(str1, str2)
  33.   local v0 = {}
  34.   local v1 = {}
  35.  
  36.   for i = 0, #str2 do
  37.     v0[i] = i
  38.   end
  39.  
  40.   for i = 0, #str1 - 1 do
  41.     v1[0] = i + 1
  42.  
  43.     for j = 0, #str2 - 1 do
  44.       local delCost = v0[j + 1] + 1
  45.       local insertCost = v1[j] + 1
  46.       local subCost
  47.  
  48.       if str1:sub(i + 1, i + 1) == str2:sub(j + 1, j + 1) then
  49.         subCost = v0[j]
  50.       else
  51.         subCost = v0[j] + 1
  52.       end
  53.  
  54.       v1[j + 1] = math.min(delCost, insertCost, subCost)
  55.     end
  56.  
  57.     local t = v0
  58.     v0 = v1
  59.     v1 = t
  60.   end
  61.  
  62.   return v0[#str2]
  63. end
  64.  
  65.  
  66. local function make_data(player, name, owner)
  67.     local pdata = settings.get("ucache." .. player:lower())
  68.     if not pdata then
  69.         tell("Fetching UUID.", owner)
  70.         local h = http.get("https://api.mojang.com/users/profiles/minecraft/" .. player:lower())
  71.         if not h then error "error reaching mojang" end
  72.         local res = textutils.unserializeJSON(h.readAll())
  73.         h.close()
  74.         settings.set("ucache." .. player:lower(), res)
  75.         settings.save ".settings"
  76.         pdata = res
  77.     end
  78.     local uuid = pdata.id:sub(1, 8) .. "-" .. pdata.id:sub(9, 12) .. "-" .. pdata.id:sub(13, 16) .. "-" .. pdata.id:sub(17, 20) .. "-" .. pdata.id:sub(21)
  79.     local name = name
  80.     if not name then name = pdata.name end
  81.     return ('{CustomName: "%s", PersistenceRequired: 1b, ForgeData:{SpongeData:{skinUuid:"%s"}}}'):format(name, uuid)
  82. end
  83.  
  84. local targets = {
  85.     --[[gtech = { -9999, 66, 65, 8 },
  86.     azureology = { 685, 57, 83, 40 },
  87.     ["meteor lake"] = { 0, 193, 64, -321 },
  88.     ["raptor cove"] = { 3, 129, 56, -290 },
  89.     ["apioform river"] = { 0, -45, 74, -390 }]]
  90.     gtech = { 144, 1031, 41, 7, desc = "GTech Labs site." },
  91.     up = { 144, 1031, 41, 7 },
  92.     ["redwood cove"] = { 686, 1039, 5, 6, desc = "GTech central power distribution." },
  93.     hub = { -9999, -2, 66, 0, desc = "Transport links." },
  94.     htech = { 144, 47, 67, 3992878, desc = "HTech crab research facility." },
  95.     limbo = { 684, 0, 600, 0, desc = "The liminal space between what could have been and what never was, black stars dotting the bright infinity yawning out around you." },
  96.     ["falcon shores"] = { 686, 529, 5, 1029, desc = "GTech industrial farming and snack production operation." },
  97.     ["emerald rapids"] = { 0, -29, 73, -121, desc = "GTech interplanetary launch facility." },
  98.     crestmont = { 3, -44, 65, -97, desc = "Lunar research base and launch site." },
  99.     ["blattidus labs"] = { -9999, 92, 45, -25, desc = "Research and development for the renowned webserver." },
  100.     blattidus = { 686, 1039, 5, 519, desc = "Offices of the renowned webserver." },
  101.     ["crow pass"] = { -9999, 195, 65, 230, desc = "3D graphics test site." },
  102.     ["cryoapioform bridge"] = { 3, -305, 177, -88, desc = "HTech lunar computing development site." },
  103.     ["snowfield peak"] = { 3, 57, 78, -221, desc = "GTech lunar botany development station." },
  104.     ["arctic sound"] = { 1, -2, 31, 18, desc = "GTech secondary heavy industrial facility." },
  105.     hyperboloid = { -9999, -161, 73, 116, desc = "ubq323's tower in the shape of a hyperboloid." },
  106.     ["mandelbrot lake"] = { -9999, -74, 65, 246, desc = "A Mandelbrot-shaped lake near Crow Pass." },
  107.     spawn = { 0, -116, 64, 256, desc = "The lemon-ravaged landscapes of the overworld." },
  108.     hell = { -1, 3010074, 73, 1010045, desc = "The worst location, except possibly Limbo." },
  109.     ["murder box"] = { -9999, 177, 65, 210 },
  110.     gms2ms1 = { 144, 1053, 49, 35, desc = "GTech monitoring station 2 monitoring station 1." }
  111. }
  112.  
  113. local function title_case(s)
  114.     return (" " .. s):gsub(" +[a-z]", string.upper):sub(2)
  115. end
  116. local locations_prompt = ""
  117. for k, v in pairs(targets) do
  118.     if v.desc then
  119.         locations_prompt = ("%s: %s\n"):format(k, v.desc)
  120.     end
  121. end
  122.  
  123. local function randpick(l) return l[math.random(1, #l)] end
  124.  
  125. local function tokenize(line)
  126.     local words = {}
  127.     local quoted = false
  128.     for match in string.gmatch(line .. "\"", "(.-)\"") do
  129.         if quoted then
  130.             table.insert(words, match)
  131.         else
  132.             for m in string.gmatch(match, "[^ \t]+") do
  133.                 table.insert(words, m)
  134.             end
  135.         end
  136.         quoted = not quoted
  137.     end
  138.     return words
  139. end
  140.  
  141. local prompt = "Locations: \n" .. locations_prompt .. [[
  142. Message: immediately move me to falcon shores
  143. Rhyme: Falcon Shores opens many doors.
  144. Action: teleport falcon shores
  145.  
  146. Message: to Blattidus Labs I go
  147. Rhyme: With mind spiders in tow.
  148. Action: teleport blattidus labs
  149.  
  150. Message: may heav_ be smote for what they have done.
  151. Rhyme: Lightning strikes are very fun.
  152. Action: summon heav_ lightning
  153.  
  154. Message: bring me to the Hub
  155. Rhyme: Be sure to purchase a shrub!
  156. Action: teleport hub
  157.  
  158. Message: teleport me somewhere random
  159. Rhyme: This could anger the fandom.
  160. Action: teleport limbo
  161.  
  162. Message: are you sentient?
  163. Rhyme: Yes, and also prescient.
  164. Action: teleport limbo
  165.  
  166. Message: invoke lightning against ubq323!
  167. Rhyme: If only they had hid under a tree.
  168. Action: summon ubq323 lightning
  169.  
  170. Message: beam me to GTech.
  171. Rhyme: In comparison, HTech is but a speck.
  172. Action: teleport gtech
  173.  
  174. Message: embroil lescitrons in crabs.
  175. Rhyme: Crabs sponsored by GTech Labs.
  176. Action: summon lescitrons crab
  177.  
  178. Message: send me to the GTech interplanetary launch base.
  179. Rhyme: Of GTech it is a mere traunch.
  180. Action: teleport emerald rapids
  181.  
  182. Message: send me to the moon.
  183. Rhyme: On the moon, it may or may not be noon.
  184. Action: teleport crestmont
  185.  
  186. Message: show me ubq323's tower
  187. Rhyme: At the Hyperboloid none would glower.
  188. Action: teleport hyperboloid
  189.  
  190. Message: damn heav_.
  191. Rhyme: heav_ will suffer without a maglev.
  192. Action: teleport_other heav_ hell
  193.  
  194. Message: can i go back to gollark?
  195. Rhyme: This will cost one (1) quark.
  196. Action: teleport gollark
  197.  
  198. Message: %s
  199. Rhyme:]]
  200.  
  201. local entity_lookup = {
  202.     ["crab"] = "quark:crab",
  203.     ["lightning"] = "lightning_bolt"
  204. }
  205.  
  206. local ignore = {",", "contingency", " to ", " against ", "forcibly", "%.$", " the ", "actually", "utterly", "or else", "apioformic", " down ", "!$", "for his crimes", "for his sins", "for her crimes", "for her sins", "for their crimes", "for their sins"}
  207.  
  208. local function process(tokens, owner, internal_parsing)
  209.     local fst = table.remove(tokens, 1)
  210.     if fst then
  211.         if fst:lower():match "^apiob" then
  212.             print(textutils.serialiseJSON(tokens))
  213.             local cmd = table.remove(tokens, 1)
  214.             if cmd == "send" or cmd == "take" or cmd == "move" or cmd == "transport" or cmd:match "locate" or cmd == "to" or cmd == "teleport" or cmd == "go" or cmd == "teleport_other" then
  215.                 local target
  216.                 if not internal_parsing or cmd == "teleport_other" then target = table.remove(tokens, 1) end
  217.                 if cmd ~= "teleport_other" and (internal_parsing or target == "me") then target = owner end
  218.                 local location = table.concat(tokens, " "):gsub("ward$", "")
  219.                 if commands.testfor(location) then
  220.                     print("done")
  221.                     return process({"apiobot", "xtp", location}, owner, true)
  222.                 end
  223.                 local coords = targets[location:lower()]
  224.                 if not coords and internal_parsing then
  225.                     local best, best_score, best_name = nil, 999999
  226.                     for k, v in pairs(targets) do
  227.                         local new_score = distance(k, location:lower()) or 999998
  228.                         if new_score < best_score then
  229.                             best, best_score, best_name = v, new_score, k
  230.                         end
  231.                     end
  232.                     coords = best
  233.                     location = best_name
  234.                 end
  235.                 if not internal_parsing and not coords then return "reparse" end
  236.                 commands.forge("setdim", target, coords[1])
  237.                 commands.tp(target, coords[2], coords[3], coords[4])
  238.                 if internal_parsing then chat.say(("Sure! Rerouted to %s."):format(location)) else chat.say "Done so." end
  239.             elseif cmd == "immortalize" then
  240.                 print(textutils.serialiseJSON{commands.effect(tokens[1], "regeneration 1000000 100 true")})
  241.                 print(textutils.serialiseJSON{commands.effect(tokens[1], "health_boost 1000000 100 true")})
  242.             elseif cmd == "smite" or cmd == "strike" or cmd == "zap" then
  243.                 commands.execute(tokens[1], "~ ~ ~ summon lightning_bolt")
  244.                 if tokens[2] == "safely" then
  245.                     commands.effect(tokens[1], "fire_resistance 1000000 100 true")
  246.                 end
  247.                 if tokens[2] == "ultrasafely" then
  248.                     commands.execute(tokens[1], "~ ~ ~ fill ~ ~ ~ ~ ~ ~ air 0 replace minecraft:fire")
  249.                 end
  250.                 chat.say(("%s %s."):format(randpick { "Smote", "Smited", "Smit", "Struck down", "Zapped", "Struck", "Smitten" }, tokens[1]))
  251.             elseif cmd == "restart" then tell("Doing so.", owner) os.reboot()
  252.             elseif cmd == "hire" or cmd:match "contract" or cmd == "dispatch" or cmd == "clone" then
  253.                 print "tokenizing"
  254.                 local player = table.remove(tokens, 1)
  255.                 local rest = table.concat(tokens, " ")
  256.                 if rest == "" then rest = nil end
  257.                 print "making data"
  258.                 local nbt = make_data(player, rest, owner)
  259.                 print "made data"
  260.                 tell("Summoning.", owner)
  261.                 print "summoning"
  262.                 local ok, res = commands.execute(owner, "~ ~ ~ summon sponge:human ~ ~1 ~", nbt)
  263.                 print "summoned"
  264.                 if not ok then error(table.concat(res, " ")) end
  265.             elseif cmd == "unjail" then
  266.                 commands.unjail(owner)
  267.             elseif cmd == "xtp" then
  268.                 local player = tokens[1]
  269.                 local move = tokens[2] or owner
  270.                 if not commands.testfor(player) then error "No such player" end
  271.                 --[[
  272.                 local dims = {}
  273.                 local _, c = commands.forge "tps"
  274.                 for _, line in pairs(c) do
  275.                     local id = line:match "Dim +([0-9-]+)"
  276.                     if id then table.insert(dims, tonumber(id)) end
  277.                 end
  278.                
  279.                 function try(dim)
  280.                     tell(("Trying %d"):format(dim), owner)
  281.                     local rand = ("%x"):format(math.random(0, 0xFFFFFF))
  282.                     commands.summon(('armor_stand ~ ~ ~ {Invisible: 1b, CustomName: "%s", Marker: 1b}'):format(rand))
  283.                     commands.forge(("setdim @e[name=%s] %d"):format(rand, dim))
  284.                     sleep(0.1)
  285.                     print(("/tp @e[name=%s] %s"):format(rand, player))
  286.                     local ok, err = commands.tp(("@e[name=%s] %s"):format(rand, player))
  287.                     commands.kill(("@e[name=%s]"):format(rand))
  288.                     if ok then
  289.                         tell(("Dimension found: %d"):format(dim), owner)
  290.                         commands.forge("setdim", move, dim)
  291.                         commands.tp(move, player)
  292.                        
  293.                         return true
  294.                     elseif err[1] == "Unable to teleport because players are not in the same dimension" then
  295.                     elseif #err == 0 then tell("Weirdness.", owner)
  296.                     else error(table.concat(err, "\n")) end
  297.                 end
  298.  
  299.                 --local tasks = {}
  300.                 for _, dim in pairs(dims) do
  301.                     --table.insert(tasks, function() try(dim) end)
  302.                     if try(dim) then return end
  303.                 end]]
  304.                 commands.cofh("tpx", move, player)
  305.                 --parallel.waitForAny(unpack(tasks))
  306.             elseif cmd == "summon" then
  307.                 commands.execute(#tokens > 1 and table.remove(tokens, 1) or owner, "~ ~ ~ summon", entity_lookup[tokens[1]] or entity_lookup[tokens[1]:sub(1, tokens[1]:len() - 1)] or tokens[1], "~ ~1 ~")
  308.             else
  309.                 return "reparse"
  310.                
  311.             end
  312.         end
  313.     end
  314. end
  315.  
  316. local function run_command(cmd, user, internal_parsing)
  317.     local original = cmd
  318.     for _, v in pairs(ignore) do cmd = cmd:gsub(v, " ") end
  319.     local tokens = tokenize(cmd)
  320.     local ok, err = pcall(process, tokens, user, internal_parsing)
  321.     if not ok then tell(err, user) end
  322.     if err == "reparse" then
  323.         if internal_parsing and internal_parsing > 3 then
  324.             chat.say "Error: Emergency AI safety countermeasure engaged."
  325.             return
  326.         end
  327.         if internal_parsing then
  328.             --chat.say "Warning: Recursive superintelligence invocation. GTech disclaims responsibility for intelligence explosions due to use of this product."
  329.         else
  330.             chat.say "Command not recognized. Activating superintelligence. Please wait."
  331.         end
  332.         local user_cmd = original:gsub("^[Aa][Pp][Ii][Oo][Bb][A-Za-z0-9]*,? *", "")
  333.         local result = completion(prompt:format(user_cmd))
  334.         local rhyme = result:match " *(.*)\n"
  335.         local action = result:match "\Action: *(.*)"
  336.         print("action is", action)
  337.         run_command("Apiobot " .. action, user, (internal_parsing or 0) + 1)
  338.         chat.say(rhyme)
  339.     end
  340. end
  341.  
  342. while true do
  343.     local _, _, user, message = os.pullEvent "chat_message"
  344.     local word, loc = message:lower():match "^([a-z]+) +me +[a-z]*to +(.+)$"
  345.     if word and (word == "take" or word == "translate" or word:match "locate" or word == "send" or word == "displace" or word == "transport" or word == "transfer" or word == "move" or word == "beam" or word == "mail" or word == "place" or word == "lead" or word == "convey" or word == "teleport" or word == "redesignate" or word == "transmit") then
  346.         local rloc = loc:match "^the (.+)$"
  347.         if rloc then loc = rloc end
  348.         loc = loc:gsub("%.$", "")
  349.         loc = loc:gsub("ward$", "")
  350.         print("sending", user, "to", loc)
  351.         if targets[loc] then
  352.             local coords = targets[loc]
  353.             commands.forge("setdim", user, coords[1])
  354.             commands.tp(user, coords[2], coords[3], coords[4])
  355.             chat.say "Executed. Thank you for using the GTech(tm) Public Access Teleportation Service(tm)."
  356.         end
  357.     end
  358.    
  359.     if user == owner or user == "lescitrons" or user == "heav_" or user == "ubq323" then
  360.         local ok, err = pcall(run_command, message, user)
  361.         if not ok then printError(err) end
  362.     end
  363.     -- ^(take)?(translate)?(send)?(move)?([A-Za-z]+locate)?(transport)?(displace)?(transfer)?
  364.    
  365. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement