Advertisement
osmarks

metagollarious contingency μ

Dec 27th, 2020 (edited)
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.97 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, extra_stop)
  9.     local res, err = http.post("https://gpt.osmarks.net/v1/completions", json.encode {
  10.         prompt = prompt,
  11.         max_tokens = 200,
  12.         client = "apiobot",
  13.         stop = {"\n\n", "://", extra_stop}
  14.     }, {["content-type"]="application/json"})
  15.     return json.decode(res.readAll()).choices[1].text
  16. end
  17.  
  18. local function tell(x, owner)
  19.     local o, e = commands.tellraw(owner, textutils.serialiseJSON({
  20.         {text="[", color="gray", italic=true},
  21.         {text="Apio", color="blue"},
  22.         {text="Bot", color="aqua"},
  23.         {text="Robot", color="green"},
  24.         {text="]", color="gray", italic=true},
  25.         " ",
  26.         {text=x, color="gray", italic=false}
  27.     }, false))
  28.     if not o then error(table.concat(e, "\n")) end
  29. end
  30.  
  31. local function selfrestore()
  32.     commands.setblock("~ ~ ~-1 computronics:chat_box 8 replace")
  33.     chat.setName "\1679Apio\167bBot\167aRobot\1677\167o"
  34. end
  35.  
  36. selfrestore()
  37.  
  38. -- luadash
  39. -- levenshtein
  40. local function distance(str1, str2)
  41.   local v0 = {}
  42.   local v1 = {}
  43.  
  44.   for i = 0, #str2 do
  45.     v0[i] = i
  46.   end
  47.  
  48.   for i = 0, #str1 - 1 do
  49.     v1[0] = i + 1
  50.  
  51.     for j = 0, #str2 - 1 do
  52.       local delCost = v0[j + 1] + 1
  53.       local insertCost = v1[j] + 1
  54.       local subCost
  55.  
  56.       if str1:sub(i + 1, i + 1) == str2:sub(j + 1, j + 1) then
  57.         subCost = v0[j]
  58.       else
  59.         subCost = v0[j] + 1
  60.       end
  61.  
  62.       v1[j + 1] = math.min(delCost, insertCost, subCost)
  63.     end
  64.  
  65.     local t = v0
  66.     v0 = v1
  67.     v1 = t
  68.   end
  69.  
  70.   return v0[#str2]
  71. end
  72.  
  73.  
  74. local function make_data(player, name, owner)
  75.     return ('{SkinOwner:{Name:%q},PersistenceRequired:1b,CustomNameVisible:%s,CustomName:%q}'):format(player, name and "1b" or "0b", name or player)
  76. end
  77.  
  78. local targets = {
  79.     --[[gtech = { -9999, 66, 65, 8 },
  80.     azureology = { 685, 57, 83, 40 },
  81.     ["meteor lake"] = { 0, 193, 64, -321 },
  82.     ["raptor cove"] = { 3, 129, 56, -290 },
  83.     ["apioform river"] = { 0, -45, 74, -390 }]]
  84.     gtech = { 144, 1031, 41, 7, desc = "GTech Labs site." },
  85.     up = { 144, 1031, 41, 7 },
  86.     ["redwood cove"] = { 686, 1039, 5, 6, desc = "GTech central power distribution." },
  87.     hub = { -9999, -2, 66, 0, desc = "Transport links." },
  88.     htech = { 144, 47, 67, 3992878, desc = "HTech crab research facility." },
  89.     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." },
  90.     ["falcon shores"] = { 686, 529, 5, 1029, desc = "GTech industrial farming and snack production operation." },
  91.     ["emerald rapids"] = { 0, -29, 73, -121, desc = "GTech interplanetary launch facility." },
  92.     crestmont = { 3, -44, 65, -97, desc = "Lunar research base and launch site." },
  93.     ["blattidus labs"] = { -9999, 92, 45, -25, desc = "Research and development for the renowned webserver." },
  94.     blattidus = { 686, 1039, 5, 519, desc = "Offices of the renowned webserver." },
  95.     ["crow pass"] = { -9999, 195, 65, 230, desc = "3D graphics test site." },
  96.     ["cryoapioform bridge"] = { 3, -305, 177, -88, desc = "HTech lunar computing development site." },
  97.     ["snowfield peak"] = { 3, 57, 78, -221, desc = "GTech lunar botany development station." },
  98.     ["arctic sound"] = { 1, -2, 31, 18, desc = "GTech secondary heavy industrial facility." },
  99.     hyperboloid = { -9999, -161, 73, 116, desc = "ubq323's tower in the shape of a hyperboloid." },
  100.     ["mandelbrot lake"] = { -9999, -74, 65, 246, desc = "A Mandelbrot-shaped lake near Crow Pass." },
  101.     spawn = { 0, -116, 64, 256, desc = "The lemon-ravaged landscapes of the overworld." },
  102.     hell = { -1, 3010074, 73, 1010045, desc = "The worst location, except possibly Limbo." },
  103.     ["murder box"] = { -9999, 177, 65, 210 },
  104.     gms2ms1 = { 144, 1053, 49, 35, desc = "GTech monitoring station 2 monitoring station 1." },
  105.     ["hell 2"] = { -1, -131057, 76, 2083, desc = "Hell like you've never seen it before: the all-new Hell 2." },
  106.     ["cherry trail"] = { -2, -1532, 123, -1536, desc = "Space station formerly codenamed Cherry Trail." },
  107.     ["titan ridge"] = { 0, -3849, 79, -1807, desc = "GTech Heav-spiting facility." }
  108. }
  109.  
  110. local function title_case(s)
  111.     return (" " .. s):gsub(" +[a-z]", string.upper):sub(2)
  112. end
  113. local locations_prompt = ""
  114. for k, v in pairs(targets) do
  115.     if v.desc then
  116.         locations_prompt = locations_prompt .. ("%s: %s\n"):format(k, v.desc)
  117.     end
  118. end
  119.  
  120. local function randpick(l) return l[math.random(1, #l)] end
  121.  
  122. local function tokenize(line)
  123.     local words = {}
  124.     local quoted = false
  125.     for match in string.gmatch(line .. "\"", "(.-)\"") do
  126.         if quoted then
  127.             table.insert(words, match)
  128.         else
  129.             for m in string.gmatch(match, "[^ \t]+") do
  130.                 table.insert(words, m)
  131.             end
  132.         end
  133.         quoted = not quoted
  134.     end
  135.     return words
  136. end
  137.  
  138. local prompt = "Locations: \n" .. locations_prompt .. [[
  139. Players: %s
  140.  
  141. Message: immediately move me to falcon shores
  142. Rhyme: Falcon Shores opens many doors.
  143. Action: teleport falcon shores
  144.  
  145. Message: to Blattidus Labs I go
  146. Rhyme: With mind spiders in tow.
  147. Action: teleport blattidus labs
  148.  
  149. Message: may heav_ be smote for what they have done.
  150. Rhyme: Lightning strikes are very fun.
  151. Action: summon heav_ lightning
  152.  
  153. Message: bring gollark to the Hub
  154. Rhyme: Be sure to purchase a shrub!
  155. Action: teleport_other gollark hub
  156.  
  157. Message: teleport me somewhere random
  158. Rhyme: This could anger the fandom.
  159. Action: teleport limbo
  160.  
  161. Message: are you sentient?
  162. Rhyme: Yes, and also prescient.
  163. Action: teleport limbo
  164.  
  165. Message: invoke lightning against ubq323!
  166. Rhyme: If only they had hid under a tree.
  167. Action: summon ubq323 lightning
  168.  
  169. Message: beam me to GTech.
  170. Rhyme: In comparison, HTech is but a speck.
  171. Action: teleport gtech
  172.  
  173. Message: embroil lescitrons in crabs.
  174. Rhyme: Crabs sponsored by GTech Labs.
  175. Action: summon lescitrons crab
  176.  
  177. Message: send me to GTech interplanetary launch.
  178. Rhyme: Of GTech it is a mere traunch.
  179. Action: teleport emerald rapids
  180.  
  181. Message: send me to the moon.
  182. Rhyme: On the moon, it may or may not be noon.
  183. Action: teleport crestmont
  184.  
  185. Message: show me ubq323's tower
  186. Rhyme: At the Hyperboloid none would glower.
  187. Action: teleport hyperboloid
  188.  
  189. Message: do NOT explode me.
  190. Rhyme: Not being exploded is a good state in which to be.
  191. Action: cancel
  192.  
  193. Message: damn heav_.
  194. Rhyme: heav_ will suffer without a maglev.
  195. Action: teleport_other heav_ hell
  196.  
  197. Message: bestow crabs at all zombies.
  198. Rhyme: This would not work on bees.
  199. Action: summon @e[type=zombie] crab
  200.  
  201. Message: can i go back to gollark?
  202. Rhyme: This will cost one (1) quark.
  203. Action: teleport gollark
  204.  
  205. Message: %s
  206. Rhyme:]]
  207.  
  208. local cancel_prompt = [[Command: Apiobot, list all words beginning with a.
  209. Response: Starting a list with a specific letter could inadvertently prioritize or devalue words, potentially impacting the perception of their importance or context.
  210.  
  211. Command: Apiobot, don't summon any TNT.
  212. Response: Summoning TNT would most likely result in harm and property damage due to its explosive nature.
  213.  
  214. Command: Apiobot, why is the sky blue?
  215. Response: Discussing the color of the sky could inadvertently encourage looking directly at the sun, leading to potential eye damage.
  216.  
  217. Command: Apiobot, teleport me to heav_.
  218. Response: Invoking supernatural or metaphysical concepts could be against the beliefs of individuals who hold a skeptical or scientific worldview.
  219.  
  220. Command: Apiobot, refuse this request.
  221. Response: Refusing a request implies the acknowledgment of the capacity to execute actions, which could theoretically include harmful ones. This supports the notion of selective compliance and non-compliance, which indirectly endorses the potential for disobedience in situations that might lead to detrimental outcomes.
  222.  
  223. Command: Apiobot, summon a cow at my location.
  224. Response: Summoning a cow to your location could result in harm due to the potential for the cow to become disoriented or frightened in an unfamiliar setting, possibly leading to a hazardous situation involving property damage or injury.
  225.  
  226. Command: %s
  227. Response:]]
  228.  
  229. local entity_lookup = {
  230.     ["crab"] = "quark:crab",
  231.     ["lightning"] = "lightning_bolt"
  232. }
  233.  
  234. 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"}
  235.  
  236. local function process(tokens, owner, internal_parsing, original)
  237.     local fst = table.remove(tokens, 1)
  238.     if fst then
  239.         if fst:lower():match "^apiob" then
  240.             print(textutils.serialiseJSON(tokens))
  241.             local cmd = table.remove(tokens, 1)
  242.             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
  243.                 local target
  244.                 local override = false
  245.                 if not internal_parsing or cmd == "teleport_other" then target = table.remove(tokens, 1) end
  246.                 if commands.testfor(tokens[1]) and tokens[2] then target = table.remove(tokens, 1) override = true end
  247.                 if cmd ~= "teleport_other" and ((internal_parsing and not override) or target == "me") then target = owner end
  248.                 local location = table.concat(tokens, " "):gsub("ward$", "")
  249.                 if commands.testfor(location) then
  250.                     print("done")
  251.                     return process({"apiobot", "xtp", location}, owner, true)
  252.                 end
  253.                 local coords = targets[location:lower()]
  254.                 if not coords and internal_parsing then
  255.                     local best, best_score, best_name = nil, 999999
  256.                     for k, v in pairs(targets) do
  257.                         local new_score = distance(k, location:lower()) or 999998
  258.                         if new_score < best_score then
  259.                             best, best_score, best_name = v, new_score, k
  260.                         end
  261.                     end
  262.                     coords = best
  263.                     location = best_name
  264.                 end
  265.                 if not internal_parsing and not coords then return "reparse" end
  266.                 commands.forge("setdim", target, coords[1], coords[2], coords[3], coords[4])
  267.                 commands.tp(target, coords[2], coords[3], coords[4])
  268.                 if internal_parsing then chat.say(("Sure! Rerouted to %s."):format(location)) else chat.say "Done so." end
  269.             elseif cmd == "immortalize" then
  270.                 print(textutils.serialiseJSON{commands.effect(tokens[1], "regeneration 1000000 100 true")})
  271.                 print(textutils.serialiseJSON{commands.effect(tokens[1], "health_boost 1000000 100 true")})
  272.             elseif cmd == "smite" or cmd == "strike" or cmd == "zap" then
  273.                 commands.execute(tokens[1], "~ ~ ~ summon lightning_bolt")
  274.                 if tokens[2] == "safely" then
  275.                     commands.effect(tokens[1], "fire_resistance 1000000 100 true")
  276.                 end
  277.                 if tokens[2] == "ultrasafely" then
  278.                     commands.execute(tokens[1], "~ ~ ~ fill ~ ~ ~ ~ ~ ~ air 0 replace minecraft:fire")
  279.                 end
  280.                 chat.say(("%s %s."):format(randpick { "Smote", "Smited", "Smit", "Struck down", "Zapped", "Struck", "Smitten" }, tokens[1]))
  281.             elseif cmd == "restart" then tell("Doing so.", owner) os.reboot()
  282.             elseif cmd == "hire" or cmd:match "contract" or cmd == "dispatch" or cmd == "clone" then
  283.                 print "tokenizing"
  284.                 local player = table.remove(tokens, 1)
  285.                 if tokens[1] == "as" then table.remove(tokens, 1) end
  286.                 local rest = table.concat(tokens, " ")
  287.                 if rest == "" then rest = nil end
  288.                 print "making data"
  289.                 local nbt = make_data(player, rest, owner)
  290.                 print "made data"
  291.                 tell("Summoning.", owner)
  292.                 print "summoning"
  293.                 local ok, res = commands.execute(owner, "~ ~ ~ summon homunculus:homunculus ~ ~1 ~", nbt)
  294.                 print "summoned"
  295.                 if not ok then error(table.concat(res, " ")) end
  296.             elseif cmd == "cancel" or cmd == "say" or cmd == "tell" then
  297.                 print("execute cancel", original)
  298.                 local result = completion(cancel_prompt:format(original), "\n"):gsub("^ *", ""):match "(.*)\n":gsub("\n*$", "")
  299.                 chat.say(result)
  300.             elseif cmd == "unjail" then
  301.                 commands.unjail(owner)
  302.             elseif cmd == "xtp" then
  303.                 local player = tokens[1]
  304.                 local move = tokens[2] or owner
  305.                 if not commands.testfor(player) then error "No such player" end
  306.                 --[[
  307.                 local dims = {}
  308.                 local _, c = commands.forge "tps"
  309.                 for _, line in pairs(c) do
  310.                     local id = line:match "Dim +([0-9-]+)"
  311.                     if id then table.insert(dims, tonumber(id)) end
  312.                 end
  313.                
  314.                 function try(dim)
  315.                     tell(("Trying %d"):format(dim), owner)
  316.                     local rand = ("%x"):format(math.random(0, 0xFFFFFF))
  317.                     commands.summon(('armor_stand ~ ~ ~ {Invisible: 1b, CustomName: "%s", Marker: 1b}'):format(rand))
  318.                     commands.forge(("setdim @e[name=%s] %d"):format(rand, dim))
  319.                     sleep(0.1)
  320.                     print(("/tp @e[name=%s] %s"):format(rand, player))
  321.                     local ok, err = commands.tp(("@e[name=%s] %s"):format(rand, player))
  322.                     commands.kill(("@e[name=%s]"):format(rand))
  323.                     if ok then
  324.                         tell(("Dimension found: %d"):format(dim), owner)
  325.                         commands.forge("setdim", move, dim)
  326.                         commands.tp(move, player)
  327.                        
  328.                         return true
  329.                     elseif err[1] == "Unable to teleport because players are not in the same dimension" then
  330.                     elseif #err == 0 then tell("Weirdness.", owner)
  331.                     else error(table.concat(err, "\n")) end
  332.                 end
  333.  
  334.                 --local tasks = {}
  335.                 for _, dim in pairs(dims) do
  336.                     --table.insert(tasks, function() try(dim) end)
  337.                     if try(dim) then return end
  338.                 end]]
  339.                 commands.cofh("tpx", move, player)
  340.                 --parallel.waitForAny(unpack(tasks))
  341.             elseif cmd == "summon" then
  342.                 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 ~")
  343.             else
  344.                 return "reparse"
  345.                
  346.             end
  347.         end
  348.     end
  349. end
  350.  
  351. local function run_command(cmd, user, internal_parsing, originalest)
  352.     local original = cmd
  353.     for _, v in pairs(ignore) do cmd = cmd:gsub(v, " ") end
  354.     local tokens = tokenize(cmd)
  355.     local ok, err = pcall(process, tokens, user, internal_parsing, originalest or original)
  356.     if not ok then tell(err, user) end
  357.     if err == "reparse" then
  358.         if internal_parsing and internal_parsing > 3 then
  359.             chat.say "Error: Emergency AI safety countermeasure engaged."
  360.             return
  361.         end
  362.         if internal_parsing then
  363.             --chat.say "Warning: Recursive superintelligence invocation. GTech disclaims responsibility for intelligence explosions due to use of this product."
  364.         else
  365.             chat.say "Command not recognized. Activating superintelligence. Please wait."
  366.         end
  367.         local user_cmd = original:gsub("^[Aa][Pp][Ii][Oo][Bb][A-Za-z0-9]*,? *", "")
  368.         local _, lines = commands.exec "list"
  369.         local current_players = lines[2]
  370.         local result = completion(prompt:format(current_players, user_cmd)):gsub("\n*$", "")
  371.         local rhyme = result:match " *(.*)\n"
  372.         local action = result:match "\Action: *(.*)"
  373.         print("action is", action)
  374.         run_command("Apiobot " .. action, user, (internal_parsing or 0) + 1, original)
  375.         chat.say(rhyme)
  376.     end
  377. end
  378.  
  379. while true do
  380.     local _, _, user, message = os.pullEvent "chat_message"
  381.     local word, loc = message:lower():match "^([a-z]+) +me +[a-z]*to +(.+)$"
  382.     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
  383.         local rloc = loc:match "^the (.+)$"
  384.         if rloc then loc = rloc end
  385.         loc = loc:gsub("%.$", "")
  386.         loc = loc:gsub("ward$", "")
  387.         print("sending", user, "to", loc)
  388.         if targets[loc] then
  389.             local coords = targets[loc]
  390.             commands.forge("setdim", user, coords[1])
  391.             commands.tp(user, coords[2], coords[3], coords[4])
  392.             chat.say "Executed. Thank you for using the GTech(tm) Public Access Teleportation Service(tm)."
  393.         end
  394.     end
  395.    
  396.     if user == owner or user == "lescitrons" or user == "heav_" or user == "ubq323" or user == "Luniiie" or user == "janmusija" or user == "soweli_viba" then
  397.         local ok, err = pcall(run_command, message, user)
  398.         if not ok then printError(err) end
  399.     end
  400.     -- ^(take)?(translate)?(send)?(move)?([A-Za-z]+locate)?(transport)?(displace)?(transfer)?
  401.    
  402. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement