Advertisement
theinsekt

poz2

Sep 3rd, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.66 KB | None | 0 0
  1. -- theinsektAPIs/poz2
  2. -- turtle API for going in different directions while keeping track of the changes
  3. -- in position and direction, compared to a starting point
  4. -- useful when digging, building etc.
  5.  
  6. -- this api has the following useful functions
  7.  
  8. -- getNewPosT: get a table with default values, (x=0,y=0,z=0,f="n"), this represents a position
  9. --The info below is old. Now uses the same directions as in minecraft.
  10. -- x is the coordinate with a coordinate axis from west to east (larger values => further east)
  11. -- y is the coordinate with a coordinate axis from south to north (larger values => further north)
  12. -- z is the height coordinate (larger values => higher up)
  13. -- f is the facing, posible directions are "n" (north), "s" (south), "e" (east) and "w" (west)
  14. -- "n" is only what the turtle thinks is north, it does not have to be the ingame north
  15.  
  16.  
  17. -- go(direction,posT):
  18. -- goes in the given direction
  19. --  direction:, possible directions are: f,b,r,l,u,d,n,s,e,w
  20. -- (forward, back, right, left, up ,down, north, south, east, west)
  21. -- posT: a table that contains the x, y, z, and f (facing)
  22.  
  23.  
  24.  
  25. ---Help function used by the other functions in this api
  26.  
  27. --directions: n,s,e,w,u,d
  28. --assumes no turning is neccesary
  29. function goUpdate(direction,posT)
  30.  --goUpdateT[direction](posT)
  31.  local posTFieldName, change = unpack(goUpdateT[direction])
  32.  posT[posTFieldName]=posT[posTFieldName]+change
  33.  return true
  34. end
  35.  
  36. function getCoordinateChange(direction,length)
  37.  local tmp=goUpdateT[direction]
  38.  if tmp==nil then Error("Error: unrecognised direction (valid: n,e,s,w,u,d)") end
  39.  local posTFieldName, change = unpack(tmp)
  40.  return posTFieldName,change*length
  41. end
  42.  
  43. --updates the facing in pos if it’s a valid turn direction
  44. --returns true if it was a valid turn direction, and posT was updated
  45. --directions: n,s,e,w,u,d,f,b,l,r
  46. function turnUpdate(direction,posT)
  47.  local facing=getTurnRes(direction,posT)
  48.  if facing==nil then
  49.     return false
  50.  end
  51.  posT["f"]=facing
  52.  return true
  53. end
  54.  
  55. -- return the resulting facing after doing a turn in the direction
  56. -- as a second return value: returns true if facing changed else false
  57. function getTurnRes(direction,posT)
  58.  if direction==nil then error("direction must not be nil") end
  59.  if posT==nil then error("Error: posT is nil") end
  60.  --u or d dosen't change so return old facing
  61.  if direction=="u" or direction=="d" then
  62.   return posT["f"]
  63.  end
  64.  --n,e,s,w are alreadyfacings
  65.  if turnResT[direction] ~= nil then --check if n,e,s,w
  66.   return direction
  67.  end
  68.  --convert f,b,r,l to facing (n,e,s,w)
  69.  return turnResT[posT["f"]][direction]
  70. end
  71.  
  72.  
  73. function getAbsoluteDirection(direction,posT)
  74.  if direction==nil then error("Error: direction is nil") end
  75.  if posT==nil then error("Error: posT is nil") end
  76.  if direction=="u" or direction=="d" then
  77.   return direction
  78.  end
  79.  return getTurnRes(direction,posT)
  80. end
  81.  
  82. --help function, turns if neccesary, then looks up an action in the doT (f,u or d) and does the --action. The do table (doT) should have f,u and d
  83. -- turn do returns the result of the do action
  84. function turnDo(doT,direction,posT,optional)
  85.  if direction==nil then error("direction must not be nil") end
  86.  if posT==nil then error("Error: posT is nil") end
  87.  if doT[direction] == nil then  -- if does not have action in that direction then turn
  88.   direction=turn2(direction,posT) -- turn and get "after turn action direction"
  89.  end
  90.  --if optional==nil then
  91.  -- return doT[direction](posT) -- do the action in the given direction
  92.  --end
  93.  return doT[direction](posT,optional)--has an optional argument so do the optional version
  94. end
  95.  
  96. -- turns and then return the "after turn action direction" (f, u or d)
  97. function turn2(direction,posT)
  98.  turn(direction,posT)
  99.  return turnToDoT[direction]
  100. end
  101.  
  102. --- API functions
  103.  
  104. --get a new table with position 0
  105. --all the parameters are optional, just use nil to
  106. --to set the default value
  107. --x0,y0,z0 are integer number
  108. --f0 is a string that can be n,e,s,w
  109. function getNewPosT(x0,y0,z0,f0)
  110.  if x0==nil then x0=0 end
  111.  if y0==nil then y0=0 end
  112.  if z0==nil then z0=0 end
  113.  if f0==nil then f0="n" end
  114.  if faceT[f0]==nil then error("Error: unrecognised direction") end
  115.  return {x=x0,y=y0,z=z0,f=f0}
  116. end
  117.  
  118. function copyPos(posT)
  119.  if posT==nil then error("Error: posT is nil") end
  120.  return {x=posT["x"],y=posT["y"],z=posT["z"],f=posT["f"]}
  121. end
  122.  
  123.  
  124. --get the reverse direction
  125. --works best for n,s,e,w,u,d, because if you use go on the reverse of those you will go to your
  126. --previous position
  127. --also works for tr,tl,f,b,l,r
  128. function getReverse(direction)
  129.  return reverseT[direction]
  130. end
  131.  
  132. --turns x,y,z into s,e,u
  133. --negative length reverses the direction
  134. function getFieldToDirection(fieldName, length)
  135.  local direction=fieldToDirectionT[fieldName]
  136.  if length<0 then
  137.   direction=getReverse(direction)
  138.  end
  139.  return direction
  140. end
  141.  
  142. --turn in/to the given direction
  143. --directions: n,s,e,w,u,d,f,b,l,r
  144. --the direction is a string
  145. --posT is a table
  146. function turn(direction,posT)
  147.  if direction==nil then error("direction must not be nil") end
  148.  if posT==nil then error("Error: posT is nil") end
  149.  if turnT[direction]==nil then
  150.   direction=faceT[posT["f"]][direction] -- translates n,s,e,w into eqvivialent r,l,u,d,f,b
  151.  end
  152.  if direction==nil then error("Error: unrecognized direction") end
  153.  return turnT[direction](posT) -- r,l,u,d,f,b
  154. end
  155.  
  156.  
  157.  
  158. --will turn if neccesary, to move in the given direction
  159. --if you want to restore the current facing you have to save the current facing and
  160. -- then use turn afterwards example:
  161. -- local myFacing=posT["f"]
  162. -- if go("e",posT) then turn(myFacing,posT) end
  163. function go(direction,posT)
  164.  return turnDo(goT,direction,posT)
  165. end
  166.  
  167. function attack(direction,posT)
  168.  return turnDo(attackT,direction,posT)
  169. end
  170.  
  171. function dig(direction,posT)
  172.  return turnDo(digT,direction,posT)
  173. end
  174.  
  175. --sign text should be nil, or not specified if direction is u or d
  176. function place(direction,posT,signText)
  177.  return turnDo(placeT,direction,posT,signText)
  178. end
  179.  
  180. function detect(direction,posT)
  181.  return turnDo(detectT,direction,posT)
  182. end
  183.  
  184. function compare(direction,posT)
  185.  return turnDo(compareT,direction,posT)
  186. end
  187.  
  188. function drop(direction,posT,anumber)
  189.  return turnDo(dropT,direction,posT,anumber)
  190. end
  191.  
  192. function suck(direction,posT)
  193.  return turnDo(suckT,direction,posT)
  194. end
  195.  
  196.  
  197. --returns the number of succesful iterations
  198. --turns the direction into n,e,s,w and then
  199. --repetedly calls the doFunction with that direction
  200. function doLoop(iters,doFunction,direction,posT,optional)
  201.  direction=getAbsoluteDirection(direction,posT)--turn into n,e,s,w,u,d
  202.  if direction==nil then error("Error: unrecognized direction") end
  203.  if doFunction==nil then error("Error: called doLoop with doFunction==nil") end
  204.  --print(direction)
  205.  local i=0
  206.  --while( i<iters and doFunction(direction,posT,optional)) do i=i+1 end
  207.  turn(direction,posT)
  208.  while( i<iters) do
  209.         --print(direction," ",posT," ",optional)
  210.     if not doFunction(direction,posT,optional) then
  211.                 --print("failed detectdiggo")
  212.             break
  213.     end
  214.     i=i+1
  215.  end
  216.  return i
  217. end
  218.  
  219. --first tries detect, digs if detected and if clear then tries to go
  220. function detectDigGo(direction,posT)
  221.  if(detect(direction,posT)) then
  222.   if(not dig(direction,posT)) then
  223.    return false
  224.   end
  225.  end
  226.  return go(direction,posT)
  227. end
  228.  
  229.  
  230.  
  231.  
  232. function doCommand(commandName,direction,posT,optional)
  233.     return commandNameT[commandName](direction,posT,optional)
  234. end
  235. commandNameT={
  236.     ["turn"]=turn,
  237.     ["go"]=go,
  238.     ["attack"]=attack,
  239.     ["dig"]=dig,
  240.     ["place"]=place,
  241.     ["detect"]=detect,
  242.     ["compare"]=compare,
  243.     ["drop"]=drop,
  244.     ["suck"]=suck,
  245.     ["ddgloop"]=function(direction,posT,optional) return doLoop(tonumber(optional),detectDigGo,direction,posT,nil) end,
  246. }
  247.  
  248. commandUsageT={
  249.     ["turn"]="usage: turn <direction> a \nexplanation: turns in the given direction",
  250.     ["go"]="usage: go <direction> a \nexplanation: tries to go in the given direction",
  251.     ["attack"]="usage: attack <direction> a \nexplanation: tries to attack in the given direction",
  252.     ["dig"]="usage: dig <direction> a \nexplanation: tries to dig a block in the given direction",
  253.     ["place"]="usage: place <direction> <optionalSignText> \nexplanation: places an item from the selected slot in the given direction",
  254.     ["detect"]="usage: detect <direction> a \nexplanation: returns true if there is a block in the given direction else false",
  255.     ["compare"]="usage: compare <direction> a \nexplanation: compares the block in the given direction to the item in the selected slot",
  256.     ["drop"]="usage: drop <direction> <count> \nexplanation: drops count items in the given direction",
  257.     ["suck"]="usage: suck <direction> a \nexplanation:",
  258.     ["ddgloop"]="usage: ddgloop <direction> <length> \nexplanation: digs and moves in the given direction for the given length",
  259. }
  260.  
  261.  
  262. -- reverse: return the reverse of the given action, useful you want to move in one direction and then back
  263.  
  264.  
  265.  
  266. --this table is used look up the reverse action
  267. -- the reverse function uses this table to look up what the reverse action is
  268. reverseT={
  269. f="b",
  270. b="f",
  271. n="s", --turn north, reverse: turn south
  272. s="n", --turn south, reverse: turn north
  273. e="w", --turn east, reverse: turn west
  274. w="e", --turn west, reverse: turn east
  275. u="d", --turn up, reverse: turn down (tu does nothing)
  276. d="u", --turn down, reverse: turn up (td does nothing)
  277. r="l", --turn right, reverse: turn left
  278. l="r", --turn left, reverse: turn right
  279. }
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287. -- This is a lookup table containing functions
  288. -- go uses this table to simply look up what function is needed to do the given action
  289. goT={
  290. --go forward then update position
  291. f=function(posT)
  292.  if(turtle.forward()) then
  293.   goUpdate(posT["f"],posT)
  294.   return true
  295.  end
  296.  return false
  297. end,
  298.  
  299. --go up then update position
  300. u=function(posT)
  301.  if(turtle.up()) then
  302.   goUpdate("u",posT)
  303.   return true
  304.  end
  305.  return false
  306. end,
  307.  
  308. --go down then update position
  309. d=function(posT)
  310.  if(turtle.down()) then
  311.   goUpdate("d",posT)
  312.   return true
  313.  end
  314.  return false
  315. end,
  316.  
  317. }
  318.  
  319. turnT={
  320. --turn right and then update position
  321. r=function(posT)
  322.  if(turtle.turnRight()) then
  323.   turnUpdate("r",posT)
  324.   return true
  325.  end
  326.  return false
  327. end,
  328.  
  329. --turn left and then update position
  330. l=function(posT)
  331.  if(turtle.turnLeft()) then
  332.   turnUpdate("l",posT)
  333.   return true
  334.  end
  335.  return false
  336. end,
  337.  
  338. --does nothing, but may avoid special case in your code
  339. u=function(posT) return true end,
  340.  
  341. --does nothing, but may avoid special case in your code
  342. d=function(posT) return true end,
  343.  
  344. --does nothing, keeps facing forward
  345. f=function(posT) return true end,
  346.  
  347. --calls turn right twice, used to turn 180 degrees
  348. b=function(posT)
  349.  return turn("r",posT) and turn("r",posT)
  350. end,
  351.  
  352. }
  353.  
  354. --given current facing and wanted facing
  355. --used to look up the needed turn action
  356. faceT={
  357. n={
  358. n="f",
  359. s="b",
  360. e="r",
  361. w="l",
  362. },
  363. s={
  364. s="f",
  365. n="b",
  366. w="r",
  367. e="l",
  368. },
  369. e={
  370. e="f",
  371. w="b",
  372. s="r",
  373. n="l",
  374. },
  375. w={
  376. w="f",
  377. e="b",
  378. n="r",
  379. s="l",
  380. },
  381. }
  382.  
  383.  
  384.  
  385. --lookup table to se what the resulting facing is after doing a turn in the given direction
  386. --does not include u or d, because they don’t change the facing
  387. --does not include n,e,s,w becuase they are alredy facings
  388. --each subtable is the inverse of the corresponding subtable in faceT
  389. turnResT=
  390. {
  391. n={
  392. f="n",
  393. b="s",
  394. r="e",
  395. l="w",
  396. },
  397. s={
  398. f="s",
  399. b="n",
  400. r="w",
  401. l="e",
  402. },
  403. e={
  404. f="e",
  405. b="w",
  406. r="s",
  407. l="n",
  408. },
  409. w={
  410. f="w",
  411. b="e",
  412. r="n",
  413. l="s",
  414. },
  415. }
  416.  
  417. --http://stackoverflow.com/questions/7925090/lua-find-a-key-from-a-value
  418. --function table_invert(t)
  419. --   local s={}
  420. --   for k,v in pairs(t) do
  421. --     s[v]=k
  422. --   end
  423. --   return s
  424. --end
  425.  
  426. --turnResT={
  427. --n=table_invert(faceT["n"]),
  428. --s=table_invert(faceT["s"]),
  429. --e=table_invert(faceT["e"]),
  430. --w=table_invert(faceT["w"]),
  431. --}
  432.  
  433.  
  434. --get the do direction that should be used after turning
  435. --do the action in this direction after having done the turn
  436. turnToDoT={
  437. f="f",
  438. b="f",
  439. n="f",
  440. s="f",
  441. e="f",
  442. w="f",
  443. u="u",
  444. d="d",
  445. r="f",
  446. l="f",
  447. }
  448.  
  449. --used to update the position when moving
  450. --goUpdateT={
  451. --n=function(posT) posT["y"]=posT["y"]+1 end,
  452. --s=function(posT) posT["y"]=posT["y"]-1 end,
  453. --e=function(posT) posT["x"]=posT["x"]+1 end,
  454. --w=function(posT) posT["x"]=posT["x"]-1 end,
  455. --u=function(posT) posT["z"]=posT["z"]+1 end,
  456. --d=function(posT) posT["z"]=posT["z"]-1 end,
  457. --}
  458.  
  459. goUpdateT={
  460. n={"z",-1},
  461. s={"z",1},
  462. e={"x",1},
  463. w={"x",-1},
  464. u={"y",1},
  465. d={"y",-1},
  466. }
  467.  
  468. fieldToDirectionT={
  469. x="e",
  470. y="u",
  471. z="s",
  472. }
  473.  
  474. attackT={
  475. f=function(PosT) return turtle.attack() end,
  476. u=function(PosT) return turtle.attackUp() end,
  477. d=function(PosT) return turtle.attackDown() end,
  478. }
  479. digT={
  480. f=function(posT) return turtle.dig() end,
  481. u=function(posT) return turtle.digUp() end,
  482. d=function(posT) return turtle.digDown() end,
  483. }
  484. placeT={
  485. f=function(posT,signText) return turtle.place(signText) end,--TEST
  486. u=function(posT) return turtle.placeUp() end,
  487. d=function(posT) return turtle.placeDown() end,
  488. }
  489. detectT={
  490. f=function(PosT) return turtle.detect() end,
  491. u=function(PosT) return turtle.detectUp() end,
  492. d=function(PosT) return turtle.detectDown() end,
  493. }
  494. compareT={
  495. f=function(PosT) return turtle.compare() end,
  496. u=function(PosT) return turtle.compareUp() end,
  497. d=function(PosT) return turtle.compareDown() end,
  498. }
  499. dropT={
  500. f=function(PosT,count) return turtle.drop(count) end, --TEST
  501. u=function(PosT,count) return turtle.dropUp(count) end,--TEST
  502. d=function(PosT,count) return turtle.dropDown(count) end,--TEST
  503. }
  504. suckT={
  505. f=function(PosT) return turtle.suck() end,
  506. u=function(PosT) return turtle.suckUp() end,
  507. d=function(PosT) return turtle.suckDown() end,
  508. }
  509.  
  510. detectDigGoT={
  511. f=function(PosT) return detectDigGo("f") end,
  512. u=function(PosT) return detectDigGo("u") end,
  513. d=function(PosT) return detectDigGo("d") end,
  514. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement