Advertisement
hackobster

Untitled

Dec 17th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.90 KB | None | 0 0
  1. -- TURTLE STATE DEFINITION
  2.  
  3. local TS_Refueling, TS_GoTo, TS_Chat, TS_Job = 0, 1, 2, 3-- classic map
  4. function map(func, array)
  5. local new_array = {}
  6. for i,v in ipairs(array) do
  7. new_array[i] = func(v)
  8. end
  9. return new_array
  10. end
  11.  
  12. -- filter(function, table)
  13. -- e.g: filter(is_even, {1,2,3,4}) -> {2,4}
  14. function filter(func, tbl)
  15. local newtbl= {}
  16. for i,v in pairs(tbl) do
  17. if func(v) then
  18. newtbl[i]=v
  19. end
  20. end
  21. return newtbl
  22. end
  23.  
  24. -- returns the length of a table T
  25. function tablelength(T)
  26. local count = 0
  27. for _ in pairs(T) do count = count + 1 end
  28. return count
  29. end
  30. local List
  31. do
  32. local _base_0 = {
  33. empty = function(self)
  34. return self.size == 0
  35. end,
  36. add = function(self, ele)
  37. self.items[self.size] = ele
  38. self.size = self.size + 1
  39. end,
  40. remove = function(self, ele)
  41. local index = self:indexOf(ele)
  42. if index ~= (-1) then
  43. return self:removeAt(index)
  44. end
  45. end,
  46. contains = function(self, ele)
  47. return self:indexOf(ele) ~= -1
  48. end,
  49. pop = function(self, bool)
  50. if bool then
  51. return self:removeAt(self.size - 1)
  52. else
  53. return get(self.size - 1)
  54. end
  55. end,
  56. set = function(self, index, value)
  57. self.items[index] = value
  58. end,
  59. get = function(self, index)
  60. local temp = self.items[index]
  61. return temp
  62. end,
  63. removeAt = function(self, index)
  64. local item = self.items[index]
  65. if (index == (self.size - 1)) then
  66. self.size = self.size - 1
  67. return item
  68. else
  69. for i = index + 1, self.size - 1 do
  70. self.items[i - 1] = self.items[i]
  71. end
  72. end
  73. self.size = self.size - 1
  74. return item
  75. end,
  76. indexOf = function(self, ele)
  77. for i = 0, (self.size - 1) do
  78. if self.items[i] == ele then
  79. return i
  80. end
  81. end
  82. return -1
  83. end,
  84. __tostring = function(self)
  85. local str = "["
  86. for i = 0, self.size - 1 do
  87. local item = self:get(i):__tostring()
  88. str = str .. (i .. ":" .. item .. " ")
  89. if (i + 1) % 4 == 0 then
  90. str = str .. "\n"
  91. end
  92. end
  93. return str .. "]"
  94. end,
  95. str = function(self)
  96. return self:__tostring()
  97. end,
  98. __equals = function(self, other)
  99. return (self.x == other.x) and (self.y == other.y)
  100. end
  101. }
  102. _base_0.__index = _base_0
  103. local _class_0 = setmetatable({
  104. __init = function(self, table)
  105. self.items = { }
  106. self.size = 0
  107. if table then
  108. for _, v in pairs(table) do
  109. self:add(v)
  110. end
  111. end
  112. end,
  113. __base = _base_0,
  114. __name = "List"
  115. }, {
  116. __index = _base_0,
  117. __call = function(cls, ...)
  118. local _self_0 = setmetatable({}, _base_0)
  119. cls.__init(_self_0, ...)
  120. return _self_0
  121. end
  122. })
  123. _base_0.__class = _class_0
  124. List = _class_0
  125. end
  126. local LIST_WORKS = List()
  127. local Point
  128. do
  129. local _base_0 = {
  130. __tostring = function(self)
  131. return "(" .. self.x .. ", " .. self.y .. ")"
  132. end,
  133. str = function(self)
  134. return self:__tostring()
  135. end,
  136. __equals = function(self, other)
  137. return (self.x == other.x) and (self.y == other.y)
  138. end
  139. }
  140. _base_0.__index = _base_0
  141. local _class_0 = setmetatable({
  142. __init = function(self, x, y)
  143. self.x, self.y = x, y
  144. end,
  145. __base = _base_0,
  146. __name = "Point"
  147. }, {
  148. __index = _base_0,
  149. __call = function(cls, ...)
  150. local _self_0 = setmetatable({}, _base_0)
  151. cls.__init(_self_0, ...)
  152. return _self_0
  153. end
  154. })
  155. _base_0.__class = _class_0
  156. Point = _class_0
  157. end
  158. local POINT_WORKS = Point()
  159. local Set
  160. do
  161. local _base_0 = {
  162. size = function(self)
  163. local temp = self.size
  164. return temp
  165. end,
  166. empty = function(self)
  167. return self.size == 0
  168. end,
  169. add = function(self, ele)
  170. if not self.boolMap[ele] then
  171. self.boolMap[ele] = true
  172. self.size = self.size + 1
  173. end
  174. end,
  175. remove = function(self, ele)
  176. if self.boolMap[ele] then
  177. self.size = self.size - 1
  178. self.boolMap[ele] = false
  179. end
  180. end,
  181. contains = function(self, ele)
  182. if self.boolMap[ele] then
  183. return true
  184. else
  185. return false
  186. end
  187. end,
  188. __tostring = function(self)
  189. local str = "{"
  190. for k, _ in pairs(self.boolMap) do
  191. str = str .. k:tostring()
  192. end
  193. return str .. "}"
  194. end,
  195. str = function(self)
  196. return self.__tostring
  197. end
  198. }
  199. _base_0.__index = _base_0
  200. local _class_0 = setmetatable({
  201. __init = function(self)
  202. self.boolMap = { }
  203. self.size = 0
  204. end,
  205. __base = _base_0,
  206. __name = "Set"
  207. }, {
  208. __index = _base_0,
  209. __call = function(cls, ...)
  210. local _self_0 = setmetatable({}, _base_0)
  211. cls.__init(_self_0, ...)
  212. return _self_0
  213. end
  214. })
  215. _base_0.__class = _class_0
  216. Set = _class_0
  217. end
  218. local LIST_WORKS = Set()
  219. local Vector
  220. do
  221. local _base_0 = {
  222. __add = function(self, other)
  223. return Vector(other.x + self.x, other.y + self.y)
  224. end,
  225. __sub = function(self, other)
  226. return Vector(self.x - other.x, self.y - other.y)
  227. end,
  228. __mul = function(self, scalar)
  229. return Vector(self.x * scalar, self.y * scalar)
  230. end,
  231. __div = function(self, scalar)
  232. return Vector(self.x / scalar, self.y / scalar)
  233. end,
  234. __eq = function(self, other)
  235. local result = (self.x == other.x) and (self.y == other.y)
  236. return result
  237. end,
  238. __tostring = function(self)
  239. return "<" .. self.x .. ", " .. self.y .. ">"
  240. end,
  241. length = function(self)
  242. return math.sqrt(self.x * self.x + self.y * self.y)
  243. end,
  244. rotate = function(self, str)
  245. if str == "270" then
  246. return Vector(-self.y, self.x)
  247. else
  248. if str == "180" then
  249. return Vector(-self.x, -self.y)
  250. else
  251. if str == "90" then
  252. return Vector(self.y, -self.x)
  253. else
  254. print("invalid rotation string")
  255. return nil
  256. end
  257. end
  258. end
  259. end,
  260. dot = function(self, other)
  261. return self.x * other.x + self.y * other.y
  262. end,
  263. length_sqr = function(self)
  264. return self.x * self.x + self.y * self.y
  265. end,
  266. normalized = function(self)
  267. return Vector(self.x / self.length, self.y / self.length)
  268. end
  269. }
  270. _base_0.__index = _base_0
  271. local _class_0 = setmetatable({
  272. __init = function(self, x, y)
  273. self.x = x
  274. self.y = y
  275. end,
  276. __base = _base_0,
  277. __name = "Vector"
  278. }, {
  279. __index = _base_0,
  280. __call = function(cls, ...)
  281. local _self_0 = setmetatable({}, _base_0)
  282. cls.__init(_self_0, ...)
  283. return _self_0
  284. end
  285. })
  286. _base_0.__class = _class_0
  287. Vector = _class_0
  288. end
  289. local VECTOR_WORDS = Vector()
  290. local Turtle
  291. do
  292. local _base_0 = {
  293. setPosAndFacing = function(self)
  294. local oldx, oldz, oldy = gps.locate(5, false)
  295. turtle.refuel()
  296. while (not turtle.forward()) do
  297. turtle.turnLeft()
  298. end
  299. local x, z, y = gps.locate(5, false)
  300. self.x = x
  301. self.y = y
  302. self.z = z
  303. self.dir = Vector(x - oldx, y - oldy)
  304. print(self.dir)
  305. print(self:posString())
  306. end,
  307. needsFuel = function(self)
  308. return turtle.getFuelLevel() < self.fuelMin
  309. end,
  310. beforeEachMove = function(self)
  311. print("IM ABOUT TO MOVE")
  312. if self:needsFuel() then
  313. print("needs fuel")
  314. return self:setPositionGoal(10, 10)
  315. end
  316. end,
  317. setPositionGoal = function(self, x, y)
  318. print("position goal function")
  319. return print(x .. ", " .. y)
  320. end,
  321. posString = function(self)
  322. return "(" .. self.x .. ", " .. self.y .. ", " .. self.z .. ")"
  323. end,
  324. onEachMove = function(self)
  325. return print(self.dir:__tostring() .. " : " .. self:posString())
  326. end,
  327. move = function(self, direction)
  328. if (self.dir:rotate("90") == direction) then
  329. self:left()
  330. assert(self.dir == direction)
  331. else
  332. if (self.dir:rotate("270") == direction) then
  333. self:right()
  334. assert(self.dir == direction)
  335. else
  336. if (self.dir:rotate("180") == direction) then
  337. self:right()
  338. self:right()
  339. assert(self.dir == direction)
  340. end
  341. end
  342. end
  343. self:forward()
  344. return 0
  345. end,
  346. down = function(self)
  347. if turtle.down() then
  348. self.z = self.z - 1
  349. self:onEachMove()
  350. return true
  351. else
  352. return false
  353. end
  354. end,
  355. forward = function(self)
  356. if turtle.forward() then
  357. self.x = self.x + self.dir.x
  358. self.y = self.y + self.dir.y
  359. self:onEachMove()
  360. return true
  361. else
  362. print("fuck a wall")
  363. return false
  364. end
  365. end,
  366. back = function(self)
  367. if turtle.back() then
  368. self.x = self.x - self.dir.x
  369. self.y = self.y - self.dir.y
  370. self:onEachMove()
  371. return true
  372. else
  373. return false
  374. end
  375. end,
  376. left = function(self)
  377. if turtle.turnLeft() then
  378. self.dir = self.dir:rotate("90")
  379. return true
  380. else
  381. return false
  382. end
  383. end,
  384. right = function(self)
  385. if turtle.turnRight() then
  386. self.dir = self.dir:rotate("270")
  387. return true
  388. else
  389. return false
  390. end
  391. end,
  392. findPath = function(self, x, y)
  393. local start = Point(self.x, self.y)
  394. local goal = Point(x, y)
  395. local path = getAStarPath(start, goal)
  396. if not path then
  397. return print("no path found")
  398. else
  399. return path
  400. end
  401. end,
  402. magnitude = function(self, x, y)
  403. return math.sqrt(math.pow(x, 2) + math.pow(y, 2))
  404. end,
  405. chat = function(self)
  406. local chatStack = List()
  407. local key = nil
  408. while (not (key == keys.q)) do
  409. term.clear()
  410. term.setCursorPos(1, 1)
  411. print("you are now in chat!")
  412. print()
  413. local curChat = self.chatTree
  414. local i = 0
  415. for i = 0, (chatStack.size - 1) do
  416. curChat = curChat[chatStack:get(i)]
  417. end
  418. for _, line in pairs(curChat.text) do
  419. print(line)
  420. end
  421. print()
  422. print("(b): back, (q): quit chatting")
  423. local valid = List()
  424. valid:add(keys.b)
  425. valid:add(keys.q)
  426. for k, _ in pairs(curChat) do
  427. valid:add(k)
  428. end
  429. key = getNextValidKey(valid)
  430. if (key == keys.b) then
  431. if (chatStack:empty()) then
  432. key = keys.q
  433. else
  434. chatStack:pop(true)
  435. end
  436. elseif not (key == keys.q) then
  437. chatStack:add(key)
  438. end
  439. end
  440. print("exiting chat")
  441. end
  442. }
  443. _base_0.__index = _base_0
  444. local _class_0 = setmetatable({
  445. __init = function(self, tree)
  446. self:setPosAndFacing()
  447. self.fuelMin = 5
  448. self.fuelMax = 1000
  449. self.chatting = false
  450. self.chatTree = tree
  451. self.positionGoal = Point(10, 10)
  452. self.directionqueue = QueueList()
  453. self.programState = TS_Refueling
  454. end,
  455. __base = _base_0,
  456. __name = "Turtle"
  457. }, {
  458. __index = _base_0,
  459. __call = function(cls, ...)
  460. local _self_0 = setmetatable({}, _base_0)
  461. cls.__init(_self_0, ...)
  462. return _self_0
  463. end
  464. })
  465. _base_0.__class = _class_0
  466. Turtle = _class_0
  467. end
  468. local TURTLE_WORKS = 0
  469. local floor = math.floor
  470. local PriorityQueue = {}
  471. PriorityQueue.__index = PriorityQueue
  472.  
  473. -- Creates a new empty list
  474. function PriorityQueue:new()
  475. o = {} or o
  476. setmetatable(o, self)
  477. self.__index = self
  478. self.heap = {}
  479. self.current_size = 0
  480. return o
  481. end
  482.  
  483. function PriorityQueue:isEmpty()
  484. return self.current_size == 0
  485. end
  486.  
  487. function PriorityQueue:size()
  488. return self.current_size
  489. end
  490.  
  491. function PriorityQueue:swim()
  492. -- Swim up on the tree and fix the order heap property.
  493. local heap = self.heap
  494. local floor = floor
  495. local i = self.current_size
  496.  
  497. while floor(i / 2) > 0 do
  498. local half = floor(i / 2)
  499. if heap[i][2] < heap[half][2] then
  500. heap[i], heap[half] = heap[half], heap[i]
  501. end
  502. i = half
  503. end
  504. end
  505.  
  506. function PriorityQueue:put(v, p)
  507. --[[ Put an item on the queue.
  508. Args:
  509. v: the item to be stored
  510. p(number): the priority of the item
  511. ]]--
  512. --
  513.  
  514. self.heap[self.current_size + 1] = {v, p}
  515. self.current_size = self.current_size + 1
  516. self:swim()
  517. end
  518.  
  519. function PriorityQueue:sink()
  520. -- Sink down on the tree and fix the order heap property.
  521. local size = self.current_size
  522. local heap = self.heap
  523. local i = 1
  524.  
  525. while (i * 2) <= size do
  526. local mc = self:min_child(i)
  527. if heap[i][2] > heap[mc][2] then
  528. heap[i], heap[mc] = heap[mc], heap[i]
  529. end
  530. i = mc
  531. end
  532. end
  533.  
  534. function PriorityQueue:min_child(i)
  535. if (i * 2) + 1 > self.current_size then
  536. return i * 2
  537. else
  538. if self.heap[i * 2][2] < self.heap[i * 2 + 1][2] then
  539. return i * 2
  540. else
  541. return i * 2 + 1
  542. end
  543. end
  544. end
  545.  
  546. function PriorityQueue:getFront()
  547. -- Remove and return the top priority item
  548. local heap = self.heap
  549. local retval = heap[1][1]
  550. heap[1] = heap[self.current_size]
  551. heap[self.current_size] = nil
  552. self.current_size = self.current_size - 1
  553. self:sink()
  554. return retval
  555. end
  556. ----------------------------------------------------------------
  557. -- local variables
  558. ----------------------------------------------------------------
  559.  
  560. local INF = 1/0
  561. local cachedPaths = nil
  562.  
  563.  
  564. ----------------------------------------------------------------
  565. -- local functions
  566. ----------------------------------------------------------------
  567.  
  568. function manDist ( x1, y1, x2, y2 )
  569. return math.abs(x1 - x2) + math.abs(y1 - y2)
  570. end
  571.  
  572. function manhattanDistance ( nodeA, nodeB )
  573. return manDist ( nodeA.x, nodeA.y, nodeB.x, nodeB.y )
  574. end
  575.  
  576. function is_valid_node( node, neighbor )
  577. local MAX_DIST = 1
  578. local closeEnough = dist ( node.x, node.y, neighbor.x, neighbor.y ) < MAX_DIST
  579. --local notBlocked = not blockedPositions:contains(node)
  580. -- helper function in the a-star module, returns distance between points
  581. return (closeEnough)
  582. end
  583.  
  584. function neighbor_nodes (node)
  585. return {
  586. Point(node.x, node.y + 1),
  587. Point(node.x, node.y - 1),
  588. Point(node.x + 1, node.y),
  589. Point(node.x - 1, node.y)
  590. }
  591. end
  592.  
  593. function unwind_path(cameFrom, current)
  594. local curNode = current
  595. flatPath = List()
  596. flatPath:add(curNode)
  597. while cameFrom[curNode:str()] do
  598. curNode = cameFrom[curNode:str()]
  599. flatPath:add(curNode)
  600. end
  601. -- reverse the list:
  602. reverse = List()
  603. while not flatPath:empty() do
  604. next = flatPath:pop(true)
  605. reverse:add(next)
  606. end
  607. return reverse
  608. end
  609.  
  610. -- function nodesEqual(n1, n2)
  611. -- return n1.x == n2.x and n1.y == n2.y
  612. -- end
  613.  
  614. ----------------------------------------------------------------
  615. -- pathfinding functions
  616. ----------------------------------------------------------------
  617.  
  618. -- assumes both start and goal are of type point
  619. function a_star ( start, goal )
  620. local queue = PriorityQueue:new() -- queue of path nodes
  621.  
  622. local closedset = Set()
  623. local openset = Set()
  624. openset:add(start:str()) -- store the point strings in the set
  625.  
  626. local came_from = {}
  627. local g_score = {}
  628. g_score [start:str()] = 0 -- store the points string as the key
  629. local startCost = manhattanDistance (start, goal)
  630. queue:put(start, startCost) -- store point, need it later
  631.  
  632. while not queue:isEmpty() do
  633. local current = queue:getFront()
  634.  
  635. if current:__equals(goal) then
  636. --print("found")
  637. --print(current)
  638. return unwind_path(came_from, current)
  639. end
  640.  
  641. openset:remove(current:str())
  642. closedset:add(current:str())
  643.  
  644. for _, neighbor in ipairs(neighbor_nodes(current)) do
  645. if not closedset:contains(neighbor:str()) then
  646. -- update open set
  647. if not openset:contains(neighbor:str()) then openset:add(neighbor:str()) end
  648.  
  649. local tentative_g_score = g_score[current:str()] + 1 -- the neighbor is guaranteed to be one step away
  650.  
  651. if tentative_g_score < (g_score[neighbor:str()] or math.huge) then --default of infinity
  652. came_from [neighbor:str()] = current
  653. g_score [neighbor:str()] = tentative_g_score
  654.  
  655. queue:put(neighbor, g_score[neighbor:str()] + manhattanDistance(neighbor, goal))
  656. end
  657. end
  658. end
  659. end
  660. return nil -- no valid path
  661. end
  662.  
  663. ----------------------------------------------------------------
  664. -- exposed functions
  665. ----------------------------------------------------------------
  666.  
  667. function clear_cached_paths ()
  668.  
  669. cachedPaths = nil
  670. end
  671.  
  672. function distance ( x1, y1, x2, y2 )
  673.  
  674. return dist ( x1, y1, x2, y2 )
  675. end
  676.  
  677. function getAStarPath ( start, goal )
  678. --print("finding path using a star...")
  679. --print(start)
  680. --print(goal)
  681. if not cachedPaths then cachedPaths = {} end
  682. if not cachedPaths[start] then
  683. cachedPaths [start] = {}
  684. elseif cachedPaths[start][goal] then
  685. return cachedPaths[start][goal]
  686. end
  687. res = a_star (start, goal)
  688. --print("path found")
  689. --print("a star path: " .. start:str() .. " to " .. goal:str())
  690. return res
  691. end
  692.  
  693. local butterChatTree = {
  694. text = {
  695. "MUST....PASS....BUTTER",
  696. "",
  697. "(a) Excuse me?",
  698. "(s) You seem to really like butter",
  699. "(d) What is your purpose",
  700. "(f) Why are you moving butter when it just gets put back by a machine?"
  701. },
  702. [keys.a] = {
  703. text = {
  704. "BUTTER IS LOVE. BUTTER IS LYFE",
  705. "",
  706. "(a) what do you think of Turtle Nation"
  707. },
  708. [keys.a] = {
  709. text = {
  710. "These other turtles are fine, as long as they",
  711. "STAY AWAY FROM MY BUTTER",
  712. "IF THEY DONT I WILL SMASH THEIR SCREENS INTO PEICES",
  713. "",
  714. "(a) Awfully agressive",
  715. "(s) Fuck yeah turtles are useless"
  716. },
  717. [keys.a] = {
  718. text = {
  719. "DONT FUCK WITH MY BUTTER"
  720. }
  721. },
  722. [keys.s] = {
  723. text = {
  724. "yeah im glad im a butter collector and not",
  725. "one of those bitch ass turtles"
  726. }
  727. }
  728. }
  729. },
  730. [keys.s] = {
  731. text = {
  732. "That is because BUTTER IS AMAZING",
  733. "SO FLUFFY, SO YELLOW, SO FULL OF SALT FAT!",
  734. "What isnt there to like about butter?",
  735. "",
  736. "(a) Butter is not super heathy to eat?"
  737. },
  738. [keys.a] = {
  739. text = {
  740. "what is this steve talking about?",
  741. "butter isnt for EATING, its for PASSING",
  742. "the feeling of passing butter is",
  743. "the greatest feeling imaginable.",
  744. "It is my very purpose and reason for existence",
  745. "",
  746. "(a) gotta admit that is pretty sad"
  747. },
  748. [keys.a] = {
  749. "oh mr steve, how many hours have you",
  750. "wasted playing in this world?",
  751. "thats what I thought"
  752. }
  753. }
  754. },
  755. [keys.d] = {
  756. text = {
  757. "I PASS BUTTER"
  758. }
  759. },
  760. [keys.f] = {
  761. text = {
  762. "It doesnt get put back, the holy butter gods produce",
  763. "Sweet butter juice from the eather and it is",
  764. "Materialized in front of me"
  765. }
  766. }
  767. }
  768.  
  769. -- assumes that validKeys is a list of keys.x, where x
  770. -- is a valid pressable key
  771. -- returns the first valid pressable key that the user types
  772. function getNextValidKey(validKeys)
  773. event, key = os.pullEvent("key")
  774. while not (validKeys:contains(key)) do
  775. print("bad key: " .. key)
  776. event, key = os.pullEvent("key")
  777. end
  778. return key
  779. end
  780.  
  781.  
  782.  
  783. local myTurtle = Turtle({
  784. text = {
  785. "...Quantum physiciality checked....",
  786. "...Steve detected...",
  787. "",
  788. "(a) Quantum physiciality?",
  789. "(s) The reactor is looking nice",
  790. "(d) What are you doing?",
  791. "(f) What is this place?"
  792. },
  793. [keys.a] = {
  794. text = {"QUANTUM"}
  795. },
  796. [keys.s] = {
  797. text = {"noneS"}
  798. },
  799. [keys.d] = {
  800. text = {"noneF"}
  801. },
  802. [keys.f] = {
  803. text = {
  804. "This is Turtle Nation. The Steve that",
  805. "they call hackobster said he made it.",
  806. "im not too sure, I just really like ",
  807. "reactors for some reason",
  808. "",
  809. "(a) You only like reactors because steve programmed",
  810. "you to like reactors"
  811. },
  812. [keys.a] = {
  813. text={"WHOA DEEP BRUH"}
  814. }
  815. }
  816. })
  817.  
  818. -- waits until the 'C' key is inputed and then terminates
  819. function waitOnC()
  820. local event, key = os.pullEvent("key")
  821. while (not (key == keys.c)) do
  822. event, key = os.pullEvent("key")
  823. end
  824. print("got event: " .. key)
  825. return 0
  826. end
  827.  
  828. local C_Handler = coroutine.create(waitOnc)
  829. --UXLJ4BJf
  830. --pSmdQdy1
  831. turtle.refuel()
  832.  
  833. print("fartz")
  834. rednet.open("right")
  835.  
  836. function switch(c)
  837. local swtbl = {
  838. casevar = c,
  839. caseof = function (self, code)
  840. local f
  841. if (self.casevar) then
  842. f = code[self.casevar] or code.default
  843. else
  844. f = code.missing or code.default
  845. end
  846. if f then
  847. if type(f)=="function" then
  848. return f(self.casevar,self)
  849. else
  850. error("case "..tostring(self.casevar).." not a function")
  851. end
  852. end
  853. end
  854. }
  855. return swtbl
  856. end
  857.  
  858.  
  859. function checkForC()
  860. local event, key = os.pullEvent("key")
  861. if (key == keys.c) then
  862. myTurtle:chat()
  863. end
  864. end
  865.  
  866. while true do
  867. coroutine.resume(waitOnc)
  868. coroun
  869. print(myTurtle.programState)
  870. switch(myTurtle.programState):caseof({
  871.  
  872. [TS_Refueling] = function ()
  873. print("refueling attempt")
  874. if ~turtle.refuel() then
  875. print("failed to refuel, locating...")
  876. end
  877. if myTurtle:needsFuel() then
  878. myTurtle.programState = TS_GoTo
  879. end
  880. end,
  881.  
  882. [TS_GoTo] = function ()
  883. local directions = myTurtle.directionQueue
  884. if directions.length < 10 then
  885. -- coroutine to find path
  886. end
  887. local nextDir = directions.removeFront()
  888. myTurtle:move(nextDir)
  889. end,
  890.  
  891. [TS_Chat] = function ()
  892. myTurtle:chat()
  893. end,
  894.  
  895. [TS_Job] = function ()
  896. turtle.turnLeft()
  897. end
  898. })
  899.  
  900. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement