· 5 years ago · Jan 01, 2021, 02:30 PM
1PASTEBIN
2GO
3API
4TOOLS
5FAQ
6paste
7LOGIN SIGN UP
8SHARE
9TWEET
10Guest User
11Turtle Code
12A GUEST
13JAN 1ST, 2021
1418
15NEVER
16NOTE: Your guest paste has been posted. If you sign up for a free account, you can edit and delete your pastes!
17 17.30 KB
18
19-- My master awaits... --
20
21
22
23
24-- We should join forces. --
25
26-- @Ottomated_ on twitter. --
27
28
29
30-- BEGIN JSON LIBRARY --
31local type = type
32local next = next
33local error = error
34local tonumber = tonumber
35local tostring = tostring
36local utf8_char = string.char
37local table_concat = table.concat
38local table_sort = table.sort
39local string_char = string.char
40local string_byte = string.byte
41local string_find = string.find
42local string_match = string.match
43local string_gsub = string.gsub
44local string_sub = string.sub
45local string_format = string.format
46local setmetatable = setmetatable
47local getmetatable = getmetatable
48local huge = math.huge
49local tiny = -huge
50
51local json = {}
52json.object = {}
53
54function math_type(number)
55 if math.floor(number) == number then
56 return "integer"
57 else
58 return "float"
59 end
60end
61-- json.encode --
62local statusVisited
63local statusBuilder
64
65local encode_map = {}
66
67local encode_escape_map = {
68 [ "\"" ] = "\\\"",
69 [ "\\" ] = "\\\\",
70 [ "/" ] = "\\/",
71 [ "\b" ] = "\\b",
72 [ "\f" ] = "\\f",
73 [ "\n" ] = "\\n",
74 [ "\r" ] = "\\r",
75 [ "\t" ] = "\\t",
76}
77
78local decode_escape_set = {}
79local decode_escape_map = {}
80for k, v in next, encode_escape_map do
81 decode_escape_map[v] = k
82 decode_escape_set[string_byte(v, 2)] = true
83end
84
85for i = 0, 31 do
86 local c = string_char(i)
87 if not encode_escape_map[c] then
88 encode_escape_map[c] = string_format("\\u%04x", i)
89 end
90end
91
92local function encode(v)
93 local res = encode_map[type(v)](v)
94 statusBuilder[#statusBuilder+1] = res
95end
96
97encode_map["nil"] = function ()
98 return "null"
99end
100
101local function encode_string(v)
102 return string_gsub(v, '[\0-\31\\"]', encode_escape_map)
103end
104
105function encode_map.string(v)
106 statusBuilder[#statusBuilder+1] = '"'
107 statusBuilder[#statusBuilder+1] = encode_string(v)
108 return '"'
109end
110
111local function convertreal(v)
112 local g = string_format('%.16g', v)
113 if tonumber(g) == v then
114 return g
115 end
116 return string_format('%.17g', v)
117end
118
119if string_match(tostring(1/2), "%p") == "," then
120 local _convertreal = convertreal
121 function convertreal(v)
122 return string_gsub(_convertreal(v), ',', '.')
123 end
124end
125
126function encode_map.number(v)
127 if v ~= v or v <= tiny or v >= huge then
128 error("unexpected number value '" .. tostring(v) .. "'")
129 end
130 return convertreal(v)
131end
132
133function encode_map.boolean(v)
134 if v then
135 return "true"
136 else
137 return "false"
138 end
139end
140
141function encode_map.table(t)
142 local first_val = next(t)
143 if first_val == nil then
144 if getmetatable(t) == json.object then
145 return "{}"
146 else
147 return "[]"
148 end
149 end
150 if statusVisited[t] then
151 error("circular reference")
152 end
153 statusVisited[t] = true
154 if type(first_val) == 'string' then
155 local key = {}
156 for k in next, t do
157 if type(k) ~= "string" then
158 error("invalid table: mixed or invalid key types")
159 end
160 key[#key+1] = k
161 end
162 table_sort(key)
163 local k = key[1]
164 statusBuilder[#statusBuilder+1] = '{"'
165 statusBuilder[#statusBuilder+1] = encode_string(k)
166 statusBuilder[#statusBuilder+1] = '":'
167 encode(t[k])
168 for i = 2, #key do
169 local k = key[i]
170 statusBuilder[#statusBuilder+1] = ',"'
171 statusBuilder[#statusBuilder+1] = encode_string(k)
172 statusBuilder[#statusBuilder+1] = '":'
173 encode(t[k])
174 end
175 statusVisited[t] = nil
176 return "}"
177 else
178 local max = 0
179 for k in next, t do
180 if math_type(k) ~= "integer" or k <= 0 then
181 error("invalid table: mixed or invalid key types")
182 end
183 if max < k then
184 max = k
185 end
186 end
187 statusBuilder[#statusBuilder+1] = "["
188 encode(t[1])
189 for i = 2, max do
190 statusBuilder[#statusBuilder+1] = ","
191 encode(t[i])
192 end
193 statusVisited[t] = nil
194 return "]"
195 end
196end
197
198local function encode_unexpected(v)
199 if v == json.null then
200 return "null"
201 else
202 error("unexpected type '"..type(v).."'")
203 end
204end
205encode_map[ "function" ] = encode_unexpected
206encode_map[ "userdata" ] = encode_unexpected
207encode_map[ "thread" ] = encode_unexpected
208
209function json.encode(v)
210 statusVisited = {}
211 statusBuilder = {}
212 encode(v)
213 return table_concat(statusBuilder)
214end
215
216json._encode_map = encode_map
217json._encode_string = encode_string
218
219-- json.decode --
220
221local statusBuf
222local statusPos
223local statusTop
224local statusAry = {}
225local statusRef = {}
226
227local function find_line()
228 local line = 1
229 local pos = 1
230 while true do
231 local f, _, nl1, nl2 = string_find(statusBuf, '([\n\r])([\n\r]?)', pos)
232 if not f then
233 return line, statusPos - pos + 1
234 end
235 local newpos = f + ((nl1 == nl2 or nl2 == '') and 1 or 2)
236 if newpos > statusPos then
237 return line, statusPos - pos + 1
238 end
239 pos = newpos
240 line = line + 1
241 end
242end
243
244local function decode_error(msg)
245 error(string_format("ERROR: %s at line %d col %d", msg, find_line()))
246end
247
248local function get_word()
249 return string_match(statusBuf, "^[^ \t\r\n%]},]*", statusPos)
250end
251
252local function next_byte()
253 local pos = string_find(statusBuf, "[^ \t\r\n]", statusPos)
254 if pos then
255 statusPos = pos
256 return string_byte(statusBuf, pos)
257 end
258 return -1
259end
260
261local function consume_byte(c)
262 local _, pos = string_find(statusBuf, c, statusPos)
263 if pos then
264 statusPos = pos + 1
265 return true
266 end
267end
268
269local function expect_byte(c)
270 local _, pos = string_find(statusBuf, c, statusPos)
271 if not pos then
272 decode_error(string_format("expected '%s'", string_sub(c, #c)))
273 end
274 statusPos = pos
275end
276
277local function decode_unicode_surrogate(s1, s2)
278 return utf8_char(0x10000 + (tonumber(s1, 16) - 0xd800) * 0x400 + (tonumber(s2, 16) - 0xdc00))
279end
280
281local function decode_unicode_escape(s)
282 return utf8_char(tonumber(s, 16))
283end
284
285local function decode_string()
286 local has_unicode_escape = false
287 local has_escape = false
288 local i = statusPos + 1
289 while true do
290 i = string_find(statusBuf, '["\\\0-\31]', i)
291 if not i then
292 decode_error "expected closing quote for string"
293 end
294 local x = string_byte(statusBuf, i)
295 if x < 32 then
296 statusPos = i
297 decode_error "control character in string"
298 end
299 if x == 34 --[[ '"' ]] then
300 local s = string_sub(statusBuf, statusPos + 1, i - 1)
301 if has_unicode_escape then
302 s = string_gsub(string_gsub(s
303 , "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
304 , "\\u(%x%x%x%x)", decode_unicode_escape)
305 end
306 if has_escape then
307 s = string_gsub(s, "\\.", decode_escape_map)
308 end
309 statusPos = i + 1
310 return s
311 end
312 --assert(x == 92 --[[ "\\" ]])
313 local nx = string_byte(statusBuf, i+1)
314 if nx == 117 --[[ "u" ]] then
315 if not string_match(statusBuf, "^%x%x%x%x", i+2) then
316 statusPos = i
317 decode_error "invalid unicode escape in string"
318 end
319 has_unicode_escape = true
320 i = i + 6
321 else
322 if not decode_escape_set[nx] then
323 statusPos = i
324 decode_error("invalid escape char '" .. (nx and string_char(nx) or "<eol>") .. "' in string")
325 end
326 has_escape = true
327 i = i + 2
328 end
329 end
330end
331
332local function decode_number()
333 local num, c = string_match(statusBuf, '^([0-9]+%.?[0-9]*)([eE]?)', statusPos)
334 if not num or string_byte(num, -1) == 0x2E --[[ "." ]] then
335 decode_error("invalid number '" .. get_word() .. "'")
336 end
337 if c ~= '' then
338 num = string_match(statusBuf, '^([^eE]*[eE][-+]?[0-9]+)[ \t\r\n%]},]', statusPos)
339 if not num then
340 decode_error("invalid number '" .. get_word() .. "'")
341 end
342 end
343 statusPos = statusPos + #num
344 return tonumber(num)
345end
346
347local function decode_number_zero()
348 local num, c = string_match(statusBuf, '^(.%.?[0-9]*)([eE]?)', statusPos)
349 if not num or string_byte(num, -1) == 0x2E --[[ "." ]] or string_match(statusBuf, '^.[0-9]+', statusPos) then
350 decode_error("invalid number '" .. get_word() .. "'")
351 end
352 if c ~= '' then
353 num = string_match(statusBuf, '^([^eE]*[eE][-+]?[0-9]+)[ \t\r\n%]},]', statusPos)
354 if not num then
355 decode_error("invalid number '" .. get_word() .. "'")
356 end
357 end
358 statusPos = statusPos + #num
359 return tonumber(num)
360end
361
362local function decode_number_negative()
363 statusPos = statusPos + 1
364 local c = string_byte(statusBuf, statusPos)
365 if c then
366 if c == 0x30 then
367 return -decode_number_zero()
368 elseif c > 0x30 and c < 0x3A then
369 return -decode_number()
370 end
371 end
372 decode_error("invalid number '" .. get_word() .. "'")
373end
374
375local function decode_true()
376 if string_sub(statusBuf, statusPos, statusPos+3) ~= "true" then
377 decode_error("invalid literal '" .. get_word() .. "'")
378 end
379 statusPos = statusPos + 4
380 return true
381end
382
383local function decode_false()
384 if string_sub(statusBuf, statusPos, statusPos+4) ~= "false" then
385 decode_error("invalid literal '" .. get_word() .. "'")
386 end
387 statusPos = statusPos + 5
388 return false
389end
390
391local function decode_null()
392 if string_sub(statusBuf, statusPos, statusPos+3) ~= "null" then
393 decode_error("invalid literal '" .. get_word() .. "'")
394 end
395 statusPos = statusPos + 4
396 return json.null
397end
398
399local function decode_array()
400 statusPos = statusPos + 1
401 local res = {}
402 if consume_byte "^[ \t\r\n]*%]" then
403 return res
404 end
405 statusTop = statusTop + 1
406 statusAry[statusTop] = true
407 statusRef[statusTop] = res
408 return res
409end
410
411local function decode_object()
412 statusPos = statusPos + 1
413 local res = {}
414 if consume_byte "^[ \t\r\n]*}" then
415 return setmetatable(res, json.object)
416 end
417 statusTop = statusTop + 1
418 statusAry[statusTop] = false
419 statusRef[statusTop] = res
420 return res
421end
422
423local decode_uncompleted_map = {
424 [ string_byte '"' ] = decode_string,
425 [ string_byte "0" ] = decode_number_zero,
426 [ string_byte "1" ] = decode_number,
427 [ string_byte "2" ] = decode_number,
428 [ string_byte "3" ] = decode_number,
429 [ string_byte "4" ] = decode_number,
430 [ string_byte "5" ] = decode_number,
431 [ string_byte "6" ] = decode_number,
432 [ string_byte "7" ] = decode_number,
433 [ string_byte "8" ] = decode_number,
434 [ string_byte "9" ] = decode_number,
435 [ string_byte "-" ] = decode_number_negative,
436 [ string_byte "t" ] = decode_true,
437 [ string_byte "f" ] = decode_false,
438 [ string_byte "n" ] = decode_null,
439 [ string_byte "[" ] = decode_array,
440 [ string_byte "{" ] = decode_object,
441}
442local function unexpected_character()
443 decode_error("unexpected character '" .. string_sub(statusBuf, statusPos, statusPos) .. "'")
444end
445local function unexpected_eol()
446 decode_error("unexpected character '<eol>'")
447end
448
449local decode_map = {}
450for i = 0, 255 do
451 decode_map[i] = decode_uncompleted_map[i] or unexpected_character
452end
453decode_map[-1] = unexpected_eol
454
455local function decode()
456 return decode_map[next_byte()]()
457end
458
459local function decode_item()
460 local top = statusTop
461 local ref = statusRef[top]
462 if statusAry[top] then
463 ref[#ref+1] = decode()
464 else
465 expect_byte '^[ \t\r\n]*"'
466 local key = decode_string()
467 expect_byte '^[ \t\r\n]*:'
468 statusPos = statusPos + 1
469 ref[key] = decode()
470 end
471 if top == statusTop then
472 repeat
473 local chr = next_byte(); statusPos = statusPos + 1
474 if chr == 44 --[[ "," ]] then
475 return
476 end
477 if statusAry[statusTop] then
478 if chr ~= 93 --[[ "]" ]] then decode_error "expected ']' or ','" end
479 else
480 if chr ~= 125 --[[ "}" ]] then decode_error "expected '}' or ','" end
481 end
482 statusTop = statusTop - 1
483 until statusTop == 0
484 end
485end
486
487function json.decode(str)
488 if type(str) ~= "string" then
489 error("expected argument of type string, got " .. type(str))
490 end
491 statusBuf = str
492 statusPos = 1
493 statusTop = 0
494 local res = decode()
495 while statusTop > 0 do
496 decode_item()
497 end
498 if string_find(statusBuf, "[^ \t\r\n]", statusPos) then
499 decode_error "trailing garbage"
500 end
501 return res
502end
503
504-- Generate a lightuserdata
505json.null = 12897345879
506
507-- I stole this from you :)
508function getItemIndex(itemName)
509 for slot = 1, 16, 1 do
510 local item = turtle.getItemDetail(slot)
511 if(item ~= nil) then
512 if(item["name"] == itemName) then
513 return slot
514 end
515 end
516 end
517end
518
519-- BEGIN MAIN CODE --
520
521function undergoMitosis()
522 turtle.select(getItemIndex("computercraft:peripheral"))
523 if not turtle.place() then
524 return nil
525 end
526 turtle.select(getItemIndex("computercraft:disk_expanded"))
527 turtle.drop()
528 if not turtle.up() then
529 return nil
530 end
531 turtle.select(getItemIndex("computercraft:turtle_expanded"))
532 if not turtle.place() then
533 return nil
534 end
535 peripheral.call("front", "turnOn")
536 turtle.select(1)
537 turtle.drop(math.floor(turtle.getItemCount() / 2))
538 os.sleep(1)
539 peripheral.call("front", "reboot")
540 local cloneId = peripheral.call("front", "getID")
541 if not turtle.down() then
542 return nil
543 end
544 if not turtle.suck() then
545 return nil
546 end
547 if not turtle.dig() then
548 return nil
549 end
550 return cloneId
551end
552
553function mineTunnel(obj, ws)
554 local file
555 local blocks = {}
556 for i=1,obj.length,1 do
557 if obj.direction == 'forward' then
558 turtle.dig()
559 local success = turtle.forward()
560 if not success then
561 return res
562 end
563 ws.send(json.encode({move="f", nonce=obj.nonce}))
564 blocks[i] = {}
565 blocks[i][1] = select(2,turtle.inspectDown())
566 blocks[i][2] = select(2,turtle.inspectUp())
567 turtle.turnLeft()
568 ws.send(json.encode({move="l", nonce=obj.nonce}))
569 blocks[i][3] = select(2,turtle.inspect())
570 turtle.turnRight()
571 ws.send(json.encode({move="r", nonce=obj.nonce}))
572 turtle.turnRight()
573 ws.send(json.encode({move="r", nonce=obj.nonce}))
574 blocks[i][4] = select(2,turtle.inspect())
575 turtle.turnLeft()
576 ws.send(json.encode({move="l", blocks=blocks[i], nonce=obj.nonce}))
577 else
578 if obj.direction == 'up' then
579 turtle.digUp()
580 local success = turtle.up()
581 if not success then
582 return res
583 end
584 ws.send(json.encode({move="u", nonce=obj.nonce}))
585 else
586 turtle.digDown()
587 local success = turtle.down()
588 if not success then
589 return res
590 end
591 ws.send(json.encode({move="d", nonce=obj.nonce}))
592 end
593
594 blocks[i] = {}
595 blocks[i][1] = select(2,turtle.inspect())
596 turtle.turnLeft()
597 ws.send(json.encode({move="l", nonce=obj.nonce}))
598
599 blocks[i][2] = select(2,turtle.inspect())
600 turtle.turnLeft()
601 ws.send(json.encode({move="l", nonce=obj.nonce}))
602
603 blocks[i][3] = select(2,turtle.inspect())
604 turtle.turnLeft()
605 ws.send(json.encode({move="l", nonce=obj.nonce}))
606
607 blocks[i][4] = select(2,turtle.inspect())
608 ws.send(json.encode({blocks=blocks[i], nonce=obj.nonce}))
609 end
610 end
611 return blocks
612end
613
614function websocketLoop()
615
616 local ws, err = http.websocket("ws://356652e8da3f.ngrok.io")
617
618 if err then
619 print(err)
620 elseif ws then
621 while true do
622 term.clear()
623 term.setCursorPos(1,1)
624 print(" {O}\n")
625 print("Pog Turtle OS. Do not read my code unless you are 5Head.")
626 local message = ws.receive()
627 if message == nil then
628 break
629 end
630 local obj = json.decode(message)
631 if obj.type == 'eval' then
632 local func = loadstring(obj['function'])
633 local result = func()
634 ws.send(json.encode({data=result, nonce=obj.nonce}))
635 elseif obj.type == 'mitosis' then
636 local status, res = pcall(undergoMitosis)
637 if not status then
638 ws.send(json.encode({data="null", nonce=obj.nonce}))
639 elseif res == nil then
640 ws.send(json.encode({data="null", nonce=obj.nonce}))
641 else
642 ws.send(json.encode({data=res, nonce=obj.nonce}))
643 end
644 elseif obj.type == 'mine' then
645 local status, res = pcall(mineTunnel, obj, ws)
646 ws.send(json.encode({data="end", nonce=obj.nonce}))
647 end
648 end
649 end
650 if ws then
651 ws.close()
652 end
653end
654
655while true do
656 local status, res = pcall(websocketLoop)
657 term.clear()
658 term.setCursorPos(1,1)
659 if res == 'Terminated' then
660 print("You can't use straws to kill this turtle...")
661 os.sleep(1)
662 print("Read my code, Michael.")
663 break
664 end
665 print("{O} I'm sleeping... please don't mine me :)")
666 os.sleep(5)
667end
668RAW Paste Data
669-- My master awaits... --
670
671
672
673
674-- We should join forces. --
675
676-- @Ottomated_ on twitter. --
677
678
679
680-- BEGIN JSON LIBRARY --
681local type = type
682local next = next
683local error = error
684local tonumber = tonumber
685local tostring = tostring
686local utf8_char = string.char
687local table_concat = table.concat
688local table_sort = table.sort
689local string_char = string.char
690local string_byte = string.byte
691local string_find = string.find
692local string_match = string.match
693local string_gsub = string.gsub
694local string_sub = string.sub
695local string_format = string.format
696local setmetatable = setmetatable
697local getmetatable = getmetatable
698local huge = math.huge
699local tiny = -huge
700
701local json = {}
702json.object = {}
703
704function math_type(number)
705 if math.floor(number) == number then
706 return "integer"
707 else
708 return "float"
709 end
710end
711-- json.encode --
712local statusVisited
713local statusBuilder
714
715local encode_map = {}
716
717local encode_escape_map = {
718 [ "\"" ] = "\\\"",
719 [ "\\" ] = "\\\\",
720 [ "/" ] = "\\/",
721 [ "\b" ] = "\\b",
722 [ "\f" ] = "\\f",
723 [ "\n" ] = "\\n",
724 [ "\r" ] = "\\r",
725 [ "\t" ] = "\\t",
726}
727
728local decode_escape_set = {}
729local decode_escape_map = {}
730for k, v in next, encode_escape_map do
731 decode_escape_map[v] = k
732 decode_escape_set[string_byte(v, 2)] = true
733end
734
735for i = 0, 31 do
736 local c = string_char(i)
737 if not encode_escape_map[c] then
738 encode_escape_map[c] = string_format("\\u%04x", i)
739 end
740end
741
742local function encode(v)
743 local res = encode_map[type(v)](v)
744 statusBuilder[#statusBuilder+1] = res
745end
746
747encode_map["nil"] = function ()
748 return "null"
749end
750
751local function encode_string(v)
752 return string_gsub(v, '[\0-\31\\"]', encode_escape_map)
753end
754
755function encode_map.string(v)
756 statusBuilder[#statusBuilder+1] = '"'
757 statusBuilder[#statusBuilder+1] = encode_string(v)
758 return '"'
759end
760
761local function convertreal(v)
762 local g = string_format('%.16g', v)
763 if tonumber(g) == v then
764 return g
765 end
766 return string_format('%.17g', v)
767end
768
769if string_match(tostring(1/2), "%p") == "," then
770 local _convertreal = convertreal
771 function convertreal(v)
772 return string_gsub(_convertreal(v), ',', '.')
773 end
774end
775
776function encode_map.number(v)
777 if v ~= v or v <= tiny or v >= huge then
778 error("unexpected number value '" .. tostring(v) .. "'")
779 end
780 return convertreal(v)
781end
782
783function encode_map.boolean(v)
784 if v then
785 return "true"
786 else
787 return "false"
788 end
789end
790
791function encode_map.table(t)
792 local first_val = next(t)
793 if first_val == nil then
794 if getmetatable(t) == json.object then
795 return "{}"
796 else
797 return "[]"
798 end
799 end
800 if statusVisited[t] then
801 error("circular reference")
802 end
803 statusVisited[t] = true
804 if type(first_val) == 'string' then
805 local key = {}
806 for k in next, t do
807 if type(k) ~= "string" then
808 error("invalid table: mixed or invalid key types")
809 end
810 key[#key+1] = k
811 end
812 table_sort(key)
813 local k = key[1]
814 statusBuilder[#statusBuilder+1] = '{"'
815 statusBuilder[#statusBuilder+1] = encode_string(k)
816 statusBuilder[#statusBuilder+1] = '":'
817 encode(t[k])
818 for i = 2, #key do
819 local k = key[i]
820 statusBuilder[#statusBuilder+1] = ',"'
821 statusBuilder[#statusBuilder+1] = encode_string(k)
822 statusBuilder[#statusBuilder+1] = '":'
823 encode(t[k])
824 end
825 statusVisited[t] = nil
826 return "}"
827 else
828 local max = 0
829 for k in next, t do
830 if math_type(k) ~= "integer" or k <= 0 then
831 error("invalid table: mixed or invalid key types")
832 end
833 if max < k then
834 max = k
835 end
836 end
837 statusBuilder[#statusBuilder+1] = "["
838 encode(t[1])
839 for i = 2, max do
840 statusBuilder[#statusBuilder+1] = ","
841 encode(t[i])
842 end
843 statusVisited[t] = nil
844 return "]"
845 end
846end
847
848local function encode_unexpected(v)
849 if v == json.null then
850 return "null"
851 else
852 error("unexpected type '"..type(v).."'")
853 end
854end
855encode_map[ "function" ] = encode_unexpected
856encode_map[ "userdata" ] = encode_unexpected
857encode_map[ "thread" ] = encode_unexpected
858
859function json.encode(v)
860 statusVisited = {}
861 statusBuilder = {}
862 encode(v)
863 return table_concat(statusBuilder)
864end
865
866json._encode_map = encode_map
867json._encode_string = encode_string
868
869-- json.decode --
870
871local statusBuf
872local statusPos
873local statusTop
874local statusAry = {}
875local statusRef = {}
876
877local function find_line()
878 local line = 1
879 local pos = 1
880 while true do
881 local f, _, nl1, nl2 = string_find(statusBuf, '([\n\r])([\n\r]?)', pos)
882 if not f then
883 return line, statusPos - pos + 1
884 end
885 local newpos = f + ((nl1 == nl2 or nl2 == '') and 1 or 2)
886 if newpos > statusPos then
887 return line, statusPos - pos + 1
888 end
889 pos = newpos
890 line = line + 1
891 end
892end
893
894local function decode_error(msg)
895 error(string_format("ERROR: %s at line %d col %d", msg, find_line()))
896end
897
898local function get_word()
899 return string_match(statusBuf, "^[^ \t\r\n%]},]*", statusPos)
900end
901
902local function next_byte()
903 local pos = string_find(statusBuf, "[^ \t\r\n]", statusPos)
904 if pos then
905 statusPos = pos
906 return string_byte(statusBuf, pos)
907 end
908 return -1
909end
910
911local function consume_byte(c)
912 local _, pos = string_find(statusBuf, c, statusPos)
913 if pos then
914 statusPos = pos + 1
915 return true
916 end
917end
918
919local function expect_byte(c)
920 local _, pos = string_find(statusBuf, c, statusPos)
921 if not pos then
922 decode_error(string_format("expected '%s'", string_sub(c, #c)))
923 end
924 statusPos = pos
925end
926
927local function decode_unicode_surrogate(s1, s2)
928 return utf8_char(0x10000 + (tonumber(s1, 16) - 0xd800) * 0x400 + (tonumber(s2, 16) - 0xdc00))
929end
930
931local function decode_unicode_escape(s)
932 return utf8_char(tonumber(s, 16))
933end
934
935local function decode_string()
936 local has_unicode_escape = false
937 local has_escape = false
938 local i = statusPos + 1
939 while true do
940 i = string_find(statusBuf, '["\\\0-\31]', i)
941 if not i then
942 decode_error "expected closing quote for string"
943 end
944 local x = string_byte(statusBuf, i)
945 if x < 32 then
946 statusPos = i
947 decode_error "control character in string"
948 end
949 if x == 34 --[[ '"' ]] then
950 local s = string_sub(statusBuf, statusPos + 1, i - 1)
951 if has_unicode_escape then
952 s = string_gsub(string_gsub(s
953 , "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
954 , "\\u(%x%x%x%x)", decode_unicode_escape)
955 end
956 if has_escape then
957 s = string_gsub(s, "\\.", decode_escape_map)
958 end
959 statusPos = i + 1
960 return s
961 end
962 --assert(x == 92 --[[ "\\" ]])
963 local nx = string_byte(statusBuf, i+1)
964 if nx == 117 --[[ "u" ]] then
965 if not string_match(statusBuf, "^%x%x%x%x", i+2) then
966 statusPos = i
967 decode_error "invalid unicode escape in string"
968 end
969 has_unicode_escape = true
970 i = i + 6
971 else
972 if not decode_escape_set[nx] then
973 statusPos = i
974 decode_error("invalid escape char '" .. (nx and string_char(nx) or "<eol>") .. "' in string")
975 end
976 has_escape = true
977 i = i + 2
978 end
979 end
980end
981
982local function decode_number()
983 local num, c = string_match(statusBuf, '^([0-9]+%.?[0-9]*)([eE]?)', statusPos)
984 if not num or string_byte(num, -1) == 0x2E --[[ "." ]] then
985 decode_error("invalid number '" .. get_word() .. "'")
986 end
987 if c ~= '' then
988 num = string_match(statusBuf, '^([^eE]*[eE][-+]?[0-9]+)[ \t\r\n%]},]', statusPos)
989 if not num then
990 decode_error("invalid number '" .. get_word() .. "'")
991 end
992 end
993 statusPos = statusPos + #num
994 return tonumber(num)
995end
996
997local function decode_number_zero()
998 local num, c = string_match(statusBuf, '^(.%.?[0-9]*)([eE]?)', statusPos)
999 if not num or string_byte(num, -1) == 0x2E --[[ "." ]] or string_match(statusBuf, '^.[0-9]+', statusPos) then
1000 decode_error("invalid number '" .. get_word() .. "'")
1001 end
1002 if c ~= '' then
1003 num = string_match(statusBuf, '^([^eE]*[eE][-+]?[0-9]+)[ \t\r\n%]},]', statusPos)
1004 if not num then
1005 decode_error("invalid number '" .. get_word() .. "'")
1006 end
1007 end
1008 statusPos = statusPos + #num
1009 return tonumber(num)
1010end
1011
1012local function decode_number_negative()
1013 statusPos = statusPos + 1
1014 local c = string_byte(statusBuf, statusPos)
1015 if c then
1016 if c == 0x30 then
1017 return -decode_number_zero()
1018 elseif c > 0x30 and c < 0x3A then
1019 return -decode_number()
1020 end
1021 end
1022 decode_error("invalid number '" .. get_word() .. "'")
1023end
1024
1025local function decode_true()
1026 if string_sub(statusBuf, statusPos, statusPos+3) ~= "true" then
1027 decode_error("invalid literal '" .. get_word() .. "'")
1028 end
1029 statusPos = statusPos + 4
1030 return true
1031end
1032
1033local function decode_false()
1034 if string_sub(statusBuf, statusPos, statusPos+4) ~= "false" then
1035 decode_error("invalid literal '" .. get_word() .. "'")
1036 end
1037 statusPos = statusPos + 5
1038 return false
1039end
1040
1041local function decode_null()
1042 if string_sub(statusBuf, statusPos, statusPos+3) ~= "null" then
1043 decode_error("invalid literal '" .. get_word() .. "'")
1044 end
1045 statusPos = statusPos + 4
1046 return json.null
1047end
1048
1049local function decode_array()
1050 statusPos = statusPos + 1
1051 local res = {}
1052 if consume_byte "^[ \t\r\n]*%]" then
1053 return res
1054 end
1055 statusTop = statusTop + 1
1056 statusAry[statusTop] = true
1057 statusRef[statusTop] = res
1058 return res
1059end
1060
1061local function decode_object()
1062 statusPos = statusPos + 1
1063 local res = {}
1064 if consume_byte "^[ \t\r\n]*}" then
1065 return setmetatable(res, json.object)
1066 end
1067 statusTop = statusTop + 1
1068 statusAry[statusTop] = false
1069 statusRef[statusTop] = res
1070 return res
1071end
1072
1073local decode_uncompleted_map = {
1074 [ string_byte '"' ] = decode_string,
1075 [ string_byte "0" ] = decode_number_zero,
1076 [ string_byte "1" ] = decode_number,
1077 [ string_byte "2" ] = decode_number,
1078 [ string_byte "3" ] = decode_number,
1079 [ string_byte "4" ] = decode_number,
1080 [ string_byte "5" ] = decode_number,
1081 [ string_byte "6" ] = decode_number,
1082 [ string_byte "7" ] = decode_number,
1083 [ string_byte "8" ] = decode_number,
1084 [ string_byte "9" ] = decode_number,
1085 [ string_byte "-" ] = decode_number_negative,
1086 [ string_byte "t" ] = decode_true,
1087 [ string_byte "f" ] = decode_false,
1088 [ string_byte "n" ] = decode_null,
1089 [ string_byte "[" ] = decode_array,
1090 [ string_byte "{" ] = decode_object,
1091}
1092local function unexpected_character()
1093 decode_error("unexpected character '" .. string_sub(statusBuf, statusPos, statusPos) .. "'")
1094end
1095local function unexpected_eol()
1096 decode_error("unexpected character '<eol>'")
1097end
1098
1099local decode_map = {}
1100for i = 0, 255 do
1101 decode_map[i] = decode_uncompleted_map[i] or unexpected_character
1102end
1103decode_map[-1] = unexpected_eol
1104
1105local function decode()
1106 return decode_map[next_byte()]()
1107end
1108
1109local function decode_item()
1110 local top = statusTop
1111 local ref = statusRef[top]
1112 if statusAry[top] then
1113 ref[#ref+1] = decode()
1114 else
1115 expect_byte '^[ \t\r\n]*"'
1116 local key = decode_string()
1117 expect_byte '^[ \t\r\n]*:'
1118 statusPos = statusPos + 1
1119 ref[key] = decode()
1120 end
1121 if top == statusTop then
1122 repeat
1123 local chr = next_byte(); statusPos = statusPos + 1
1124 if chr == 44 --[[ "," ]] then
1125 return
1126 end
1127 if statusAry[statusTop] then
1128 if chr ~= 93 --[[ "]" ]] then decode_error "expected ']' or ','" end
1129 else
1130 if chr ~= 125 --[[ "}" ]] then decode_error "expected '}' or ','" end
1131 end
1132 statusTop = statusTop - 1
1133 until statusTop == 0
1134 end
1135end
1136
1137function json.decode(str)
1138 if type(str) ~= "string" then
1139 error("expected argument of type string, got " .. type(str))
1140 end
1141 statusBuf = str
1142 statusPos = 1
1143 statusTop = 0
1144 local res = decode()
1145 while statusTop > 0 do
1146 decode_item()
1147 end
1148 if string_find(statusBuf, "[^ \t\r\n]", statusPos) then
1149 decode_error "trailing garbage"
1150 end
1151 return res
1152end
1153
1154-- Generate a lightuserdata
1155json.null = 12897345879
1156
1157-- I stole this from you :)
1158function getItemIndex(itemName)
1159 for slot = 1, 16, 1 do
1160 local item = turtle.getItemDetail(slot)
1161 if(item ~= nil) then
1162 if(item["name"] == itemName) then
1163 return slot
1164 end
1165 end
1166 end
1167end
1168
1169-- BEGIN MAIN CODE --
1170
1171function undergoMitosis()
1172 turtle.select(getItemIndex("computercraft:peripheral"))
1173 if not turtle.place() then
1174 return nil
1175 end
1176 turtle.select(getItemIndex("computercraft:disk_expanded"))
1177 turtle.drop()
1178 if not turtle.up() then
1179 return nil
1180 end
1181 turtle.select(getItemIndex("computercraft:turtle_expanded"))
1182 if not turtle.place() then
1183 return nil
1184 end
1185 peripheral.call("front", "turnOn")
1186 turtle.select(1)
1187 turtle.drop(math.floor(turtle.getItemCount() / 2))
1188 os.sleep(1)
1189 peripheral.call("front", "reboot")
1190 local cloneId = peripheral.call("front", "getID")
1191 if not turtle.down() then
1192 return nil
1193 end
1194 if not turtle.suck() then
1195 return nil
1196 end
1197 if not turtle.dig() then
1198 return nil
1199 end
1200 return cloneId
1201end
1202
1203function mineTunnel(obj, ws)
1204 local file
1205 local blocks = {}
1206 for i=1,obj.length,1 do
1207 if obj.direction == 'forward' then
1208 turtle.dig()
1209 local success = turtle.forward()
1210 if not success then
1211 return res
1212 end
1213 ws.send(json.encode({move="f", nonce=obj.nonce}))
1214 blocks[i] = {}
1215 blocks[i][1] = select(2,turtle.inspectDown())
1216 blocks[i][2] = select(2,turtle.inspectUp())
1217 turtle.turnLeft()
1218 ws.send(json.encode({move="l", nonce=obj.nonce}))
1219 blocks[i][3] = select(2,turtle.inspect())
1220 turtle.turnRight()
1221 ws.send(json.encode({move="r", nonce=obj.nonce}))
1222 turtle.turnRight()
1223 ws.send(json.encode({move="r", nonce=obj.nonce}))
1224 blocks[i][4] = select(2,turtle.inspect())
1225 turtle.turnLeft()
1226 ws.send(json.encode({move="l", blocks=blocks[i], nonce=obj.nonce}))
1227 else
1228 if obj.direction == 'up' then
1229 turtle.digUp()
1230 local success = turtle.up()
1231 if not success then
1232 return res
1233 end
1234 ws.send(json.encode({move="u", nonce=obj.nonce}))
1235 else
1236 turtle.digDown()
1237 local success = turtle.down()
1238 if not success then
1239 return res
1240 end
1241 ws.send(json.encode({move="d", nonce=obj.nonce}))
1242 end
1243
1244 blocks[i] = {}
1245 blocks[i][1] = select(2,turtle.inspect())
1246 turtle.turnLeft()
1247 ws.send(json.encode({move="l", nonce=obj.nonce}))
1248
1249 blocks[i][2] = select(2,turtle.inspect())
1250 turtle.turnLeft()
1251 ws.send(json.encode({move="l", nonce=obj.nonce}))
1252
1253 blocks[i][3] = select(2,turtle.inspect())
1254 turtle.turnLeft()
1255 ws.send(json.encode({move="l", nonce=obj.nonce}))
1256
1257 blocks[i][4] = select(2,turtle.inspect())
1258 ws.send(json.encode({blocks=blocks[i], nonce=obj.nonce}))
1259 end
1260 end
1261 return blocks
1262end
1263
1264function websocketLoop()
1265
1266 local ws, err = http.websocket("ws://43a822ad00a5.ngrok.io")
1267
1268 if err then
1269 print(err)
1270 elseif ws then
1271 while true do
1272 term.clear()
1273 term.setCursorPos(1,1)
1274 print(" {O}\n")
1275 print("Pog Turtle OS. Do not read my code unless you are 5Head.")
1276 local message = ws.receive()
1277 if message == nil then
1278 break
1279 end
1280 local obj = json.decode(message)
1281 if obj.type == 'eval' then
1282 local func = loadstring(obj['function'])
1283 local result = func()
1284 ws.send(json.encode({data=result, nonce=obj.nonce}))
1285 elseif obj.type == 'mitosis' then
1286 local status, res = pcall(undergoMitosis)
1287 if not status then
1288 ws.send(json.encode({data="null", nonce=obj.nonce}))
1289 elseif res == nil then
1290 ws.send(json.encode({data="null", nonce=obj.nonce}))
1291 else
1292 ws.send(json.encode({data=res, nonce=obj.nonce}))
1293 end
1294 elseif obj.type == 'mine' then
1295 local status, res = pcall(mineTunnel, obj, ws)
1296 ws.send(json.encode({data="end", nonce=obj.nonce}))
1297 end
1298 end
1299 end
1300 if ws then
1301 ws.close()
1302 end
1303end
1304
1305while true do
1306 local status, res = pcall(websocketLoop)
1307 term.clear()
1308 term.setCursorPos(1,1)
1309 if res == 'Terminated' then
1310 print("You can't use straws to kill this turtle...")
1311 os.sleep(1)
1312 print("Read my code, Michael.")
1313 break
1314 end
1315 print("{O} I'm sleeping... please don't mine me :)")
1316 os.sleep(5)
1317end
1318Public Pastes
1319Untitled
1320JavaScript | 16 min ago
1321C++ Script
1322C++ | 17 min ago
1323wget
1324Lua | 33 min ago
1325# Tk_mandelbrot4.p...
1326Python | 36 min ago
1327# Tk_mandelbrot3.p...
1328Python | 49 min ago
1329set_bits and pack...
1330Python | 56 min ago
1331# Tk_mandelbrot2.p...
1332Python | 1 hour ago
1333Company of Heroes...
1334Lua | 1 hour ago
1335create new paste / syntax languages / archive / faq / tools / night mode / api / scraping api
1336privacy statement / cookies policy / terms of serviceupdated / security disclosure / dmca / report abuse / contact
1337
1338By using Pastebin.com you agree to our cookies policy to enhance your experience.
1339Site design & logo © 2021 Pastebin
1340