Advertisement
theinsekt

poz3

Sep 5th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.03 KB | None | 0 0
  1. -- theinsektAPIs/poz3
  2. --experimental, not tested
  3. --stores posT internally to simplify the usage of the functions
  4. -- turtle API for going in different directions while keeping track of the changes
  5. -- in position and direction, compared to a starting point
  6. -- also keeps track of the number of steps and turns that have been performed
  7. -- useful when digging, building etc.
  8. -- New feature: enterSimulationMode(), disables all actions, but still returns true and
  9. --changes position, steps and turns, exitSimukationMode() to restore everyhting and
  10. --be able to move again
  11. --Useful if you have two algorithms and want to test which uses the fewest steps
  12. --and then exiting simulation mode and running the best of them
  13. --to start a new simulation while in simulation you have to exit simulation mode then enter
  14. --again
  15.  
  16. -- this api has the following useful functions
  17. --using wtf style:
  18.  
  19. --manipulate position manually
  20. --setPos(x0,y0,z0,f0)
  21. --getpos()->(x,y,z,f)
  22. --resetpos()
  23.  
  24. --test properties of the direction
  25. --validFacing(direction)->(result)
  26. --validDirection(direction)->(result)
  27. --validAbsoluteDirection(direction)->(result)
  28.  
  29. --manipulate directions
  30. --getReverse(direction)->(reverseDirection)
  31. --getAbsoluteDirection(direction)->(absoluteDirection)
  32.  
  33. --do actions (valid direction usually: n,s,)
  34. --valid direction for most are: f,b,r,l,u,d,n,s,e,w
  35. --(forward, back, right, left, up ,down, north, south, east, west)
  36. --go(direction)->(succes)
  37. --turn(direction)->(succes)
  38. --etc...
  39.  
  40. --the current position!!!
  41. --and stats!
  42. --can be changed with setPos, getPos and resetPos
  43. local posT={x=0,y=0,z=0,f="n"}
  44. local statsT={steps=0,turns=0}
  45.  
  46. --simulation, is probably to simple
  47. --todo: make it so default return values can be set for
  48. --actions
  49. inSimulation=false
  50. local backupPosT=nil
  51. local backupStatsT=nil
  52.  
  53. --in simulation mode will pretend to do the action
  54. --then return true
  55. --work in progress
  56. function enterSimulationMode()
  57.  if not inSimulation then
  58.   print("Entered simulation mode")
  59.   inSimulation=true
  60.   backupPosT=copyTable(posT)
  61.   backupStatsT=copyTable(statsT)
  62.  end
  63. end
  64.  
  65. function exitSimulationMode()
  66.  if inSimulation then
  67.   print("Exited simulation mode")
  68.   inSimulation=false
  69.   posT=copyTable(backupPosT)
  70.   statsT=copyTable(backupStatsT)
  71.  end
  72. end
  73.  
  74. function incrementSteps()
  75.  statsT["steps"]=statsT["steps"]+1
  76. end
  77.  
  78. function incrementTurns()
  79.  statsT["turns"]=statsT["turns"]+1
  80. end
  81.  
  82. function getStats()
  83.  return statsT["steps"], statsT["turns"]
  84. end
  85.  
  86. function setStats(steps,turns)
  87.  statsT["steps"]=steps
  88.  statsT["turns"]=turns
  89. end
  90.  
  91. function resetStats()
  92.  statsT["steps"]=0
  93.  statsT["turns"]=0
  94. end
  95.  
  96. --change coordinates
  97.  
  98.  
  99.  
  100. --sets the position values
  101. function setPos(x0,y0,z0,f0)
  102.  --check that none of the inputs are nil
  103.  if(x0==nil) then error("Error: x0 is nil") end
  104.  if(y0==nil) then error("Error: y0 is nil") end
  105.  if(z0==nil) then error("Error: z0 is nil") end
  106.  if(f0==nil) then error("Error: f0 is nil") end
  107.  --check that the f0 direction is recognized
  108.  if(not validFacing(f0)) then error("Error: f0 isn't a valid facing (valid: n,s,w,e)") end
  109.  --check that coordinates are numbers
  110.  if(type(x0) ~= "number") then error("Error: x0 isn't a number") end
  111.  if(type(y0) ~= "number") then error("Error: y0 isn't a number") end
  112.  if(type(z0) ~= "number") then error("Error: z0 isn't a number") end
  113.  --set the position
  114.  posT["x"]=x0
  115.  posT["y"]=y0
  116.  posT["z"]=z0
  117.  posT["f"]=f0
  118. end
  119.  
  120.  
  121. function setPos2(posT)
  122.  setPos(posT["x"],posT["y"],posT["z"],posT["f"])
  123. end
  124.  
  125.  
  126. function resetPos()
  127.  setPos(0,0,0,"n")
  128. end
  129.  
  130.  
  131. --return x,y,z,f
  132. --uses the same conventions as minecraft
  133. --but the facing north is determined by the starting direction
  134. function getPos()
  135.  return posT["x"],posT["y"],posT["z"],posT["f"]
  136. end
  137.  
  138. function copyTable(inTable)
  139.  local outTable={}
  140.  for k,v in pairs(inTable) do
  141.   outTable[k]=v
  142.  end
  143.  return outTable
  144. end
  145.  
  146. function getPos2()
  147.  return copyPos(posT)
  148.  --return {x=posT["x"],y=posT["y"],z=posT["z"],f=posT["f"]}
  149. end
  150.  
  151. function getPosField(fieldName)
  152.  return posT[fieldName]
  153. end
  154.  
  155.  
  156. function copyPos(aPosT)
  157.  if aPosT==nil then error("Error: aPosT is nil") end
  158.  return {x=aPosT["x"],y=aPosT["y"],z=aPosT["z"],f=aPosT["f"]}
  159. end
  160.  
  161. --check directions
  162.  
  163.  
  164. function validFacing(direction)
  165.  return faceT[direction]~=nil
  166. end
  167.  
  168.  
  169. function validDirection(direction)
  170.  return reverseT[direction]~=nil
  171. end
  172.  
  173.  
  174. function validAbsoluteDirection(direction)
  175.  return goUpdateT[direction]~=nil
  176. end
  177.  
  178. function isHorizontal(direction)
  179.  return not isVertical(direction)
  180. end
  181.  
  182. function isVertical(direction)
  183.  direction=getAbsoluteDirection(direction)
  184.  return direction=="u" or direction=="d"
  185. end
  186.  
  187.  
  188.  
  189.  
  190. --various direction calculations
  191.  
  192.  
  193. --directions: n,s,e,w,u,d
  194. --returns which coordinate would be changed and by how much
  195. function getCoordinateChange(direction,length)
  196.  local tmp=goUpdateT[direction]
  197.  if tmp==nil then Error("Error: unrecognised direction (valid: n,s,e,w,u,d)") end
  198.  local posTFieldName, change = unpack(tmp)
  199.  return posTFieldName,change*length
  200. end
  201.  
  202.  
  203. --return the resulting facing after doing a turn in the given direction
  204. --returns nil if the direction is not valid/recognized
  205. --directions: n,s,e,w,u,d,f,b,l,r
  206. function getTurnRes(direction)
  207.  --check inputs
  208.  if direction==nil then error("Error: direction is nil") end
  209.  if not validDirection(direction) then return nil end
  210.  if posT==nil then error("Error: posT is nil") end
  211.  --u or d doesn’t change so return old facing
  212.  if direction=="u" or direction=="d" then
  213.   return posT["f"]
  214.  end
  215.  --n,e,s,w are already facings
  216.  if turnResT[direction] ~= nil then
  217.   return direction --direction is one of n,e,s,w
  218.  end
  219.  --convert f,b,r,l to facing (n,e,s,w)
  220.  local tmp = turnResT[posT["f"]][direction]
  221.  if tmp==nil then error("Error: internal bug") end
  222.  return tmp
  223. end
  224.  
  225. --turns: n,s,e,w,u,d,f,b,l,r into n,s,e,w,u,d
  226. --directions: n,s,e,w,u,d,f,b,l,r
  227. --returns nil if the direction isn't recognized
  228. function getAbsoluteDirection(direction)
  229.  --check input
  230.  if direction==nil then error("Error: direction is nil") end
  231.  if posT==nil then error("Error: posT is nil") end
  232.  --u and d are already absolute direction
  233.  if direction=="u" or direction=="d" then
  234.   return direction
  235.  end
  236.  --get the cardinal direction
  237.  return getTurnRes(direction)
  238. end
  239.  
  240.  
  241. --get the reverse direction
  242. --directions:
  243. --works best for n,s,e,w,u,d, because if you use go on the reverse of those you will go to your
  244. --previous position
  245. --also works for f,b,l,r
  246. function getReverse(direction)
  247.  return reverseT[direction]
  248. end
  249.  
  250.  
  251. --get a new table with position 0
  252. --all the parameters are optional, just use nil to
  253. --to set the default value
  254. --x0,y0,z0 are integer number
  255. --f0 is a string that can be n,e,s,w
  256. function getNewPos(x0,y0,z0,f0)
  257.  --replace nil with default value
  258.  if x0==nil then x0=0 end
  259.  if y0==nil then y0=0 end
  260.  if z0==nil then z0=0 end
  261.  if f0==nil then f0="n" end
  262.  
  263.  --check that the f0 direction is recognized
  264.  if(not validFacing(f0)) then error("Error: f0 isn't a valid facing (valid: n,s,w,e)") end
  265.  
  266.  --check that coordinates are numbers
  267.  if(type(x0) ~= "number") then error("Error: x0 isn't a number") end
  268.  if(type(y0) ~= "number") then error("Error: y0 isn't a number") end
  269.  if(type(z0) ~= "number") then error("Error: z0 isn't a number") end
  270.  
  271.  --return new posT
  272.  return {x=x0,y=y0,z=z0,f=f0}
  273. end
  274.  
  275.  
  276. --get the direction and distance needed to get to a coord value
  277. function coordToGoValues(coordName,wantedCoordValue)
  278.  return coordChangeToDirection(coordName, wantedCoordValue-posT[coordName])
  279. end
  280.  
  281.  
  282. --turns x,y,z into s,e,u
  283. --negative length reverses the direction
  284. --can be used to get the direction and distance if you want to
  285. --go to a certain coordinate
  286. function coordChangeToDirection(coordName, length)
  287.  local direction=fieldToDirectionT[coordName]
  288.  if length<0 then
  289.   direction=getReverse(direction)
  290.   length=-length
  291.  end
  292.  return direction, length
  293. end
  294.  
  295.  
  296. --turn, go, detect etc...
  297.  
  298.  
  299. --turn in/to the given direction
  300. --directions: n,s,e,w,u,d,f,b,l,r
  301. function turn(direction)
  302.  if direction==nil then error("Error: direction is nil") end
  303.  if posT==nil then error("Error: posT is nil") end
  304.  if turnT[direction]==nil then
  305.   direction=faceT[posT["f"]][direction] -- translates n,s,e,w into eqvivialent r,l,u,d,f,b
  306.  end
  307.  if direction==nil then error("Error: unrecognized direction") end
  308.  return turnT[direction]() -- r,l,u,d,f,b
  309. end
  310.  
  311.  
  312. --goes in the given direction
  313. --uses turn if necessary
  314. --directions: n,s,e,w,u,d,f,b,l,r
  315. function go(direction)
  316.  if direction==nil then error("Error: direction is nil") end
  317.  return turnDo(goT,direction)
  318. end
  319.  
  320. function attack(direction)
  321.  return turnDo(attackT,direction)
  322. end
  323.  
  324.  
  325. function dig(direction)
  326.  return turnDo(digT,direction)
  327. end
  328.  
  329.  
  330. --sign text should be nil, or not specified if direction is u or d
  331. function place(direction,signText)
  332.  return turnDo(placeT,direction,signText)
  333. end
  334.  
  335.  
  336. function detect(direction)
  337.  return turnDo(detectT,direction)
  338. end
  339.  
  340.  
  341. function compare(direction)
  342.  return turnDo(compareT,direction)
  343. end
  344.  
  345.  
  346. function drop(direction,anumber)
  347.  return turnDo(dropT,direction,anumber)
  348. end
  349.  
  350.  
  351. function suck(direction)
  352.  return turnDo(suckT,direction)
  353. end
  354.  
  355. --available in cc1.64 and later
  356. function inspect(direction)
  357.  return turnDo(inspectT,direction)
  358. end
  359.  
  360. --help functions
  361.  
  362.  
  363. --help function, turns if necessary, 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
  364. -- turn do returns the result of the do action
  365. function turnDo(doT,direction,optional)
  366.  if posT==nil then error("Error: posT is nil") end
  367.  if direction==nil then error("Error: direction is nil") end
  368.  if doT==nil then error("Error: doT is nil") end
  369.  if doT[direction] == nil then  -- if does not have action in that direction then turn
  370.   direction=turn2(direction) -- turn and get "after turn action direction"
  371.  end
  372.  return doT[direction](optional)--do the function
  373. end
  374.  
  375.  
  376. -- turns and then return the "after turn action direction" (f, u or d)
  377. function turn2(direction)
  378.  turn(direction)
  379.  return getTurnDoDirection(direction)
  380. end
  381.  
  382.  
  383. function getTurnDoDirection(direction)
  384.  if direction=="u" or direction=="d" then
  385.   return direction --up or down
  386.  else
  387.   return "f"--forward
  388.  end
  389. end
  390.  
  391.  
  392. --directions: n,s,e,w,u,d
  393. function goUpdate(direction)
  394.  local posTFieldName, change = getCoordinateChange(direction,1)
  395.  posT[posTFieldName]=posT[posTFieldName]+change
  396.  return true
  397. end
  398.  
  399.  
  400. --updates the facing in pos if it’s a valid turn direction
  401. --returns true if it was a valid turn direction, and posT was updated
  402. --directions: n,s,e,w,u,d,f,b,l,r
  403. function turnUpdate(direction)
  404.  local facing=getTurnRes(direction)
  405.  if facing==nil then
  406.   return false
  407.  end
  408.  posT["f"]=facing
  409.  return true
  410. end
  411.  
  412.  
  413.  
  414.  
  415. function doCommand(commandName,direction,optional)
  416.     return commandNameT[commandName](direction,optional)
  417. end
  418.  
  419.  
  420. commandNameT={
  421.     ["turn"]=turn,
  422.     ["go"]=go,
  423.     ["attack"]=attack,
  424.     ["dig"]=dig,
  425.     ["place"]=place,
  426.     ["detect"]=detect,
  427.     ["compare"]=compare,
  428.     ["drop"]=drop,
  429.     ["suck"]=suck,
  430.     ["ddgloop"]=function(direction,optional) return doLoop(tonumber(optional),detectDigGo,direction,nil) end,
  431. }
  432.  
  433.  
  434. commandUsageT={
  435.     ["turn"]="usage: turn <direction> a \nexplanation: turns in the given direction",
  436.     ["go"]="usage: go <direction> a \nexplanation: tries to go in the given direction",
  437.     ["attack"]="usage: attack <direction> a \nexplanation: tries to attack in the given direction",
  438.     ["dig"]="usage: dig <direction> a \nexplanation: tries to dig a block in the given direction",
  439.     ["place"]="usage: place <direction> <optionalSignText> \nexplanation: places an item from the selected slot in the given direction",
  440.     ["detect"]="usage: detect <direction> a \nexplanation: returns true if there is a block in the given direction else false",
  441.     ["compare"]="usage: compare <direction> a \nexplanation: compares the block in the given direction to the item in the selected slot",
  442.     ["drop"]="usage: drop <direction> <count> \nexplanation: drops count items in the given direction",
  443.     ["suck"]="usage: suck <direction> a \nexplanation:",
  444.     ["ddgloop"]="usage: ddgloop <direction> <length> \nexplanation: digs and moves in the given direction for the given length",
  445. }
  446.  
  447.  
  448. -- reverse: return the reverse of the given action, useful you want to move in one direction and then back
  449.  
  450.  
  451. --this table is used look up the reverse action
  452. -- the reverse function uses this table to look up what the reverse action is
  453. reverseT={
  454. f="b",
  455. b="f",
  456. n="s", --turn north, reverse: turn south
  457. s="n", --turn south, reverse: turn north
  458. e="w", --turn east, reverse: turn west
  459. w="e", --turn west, reverse: turn east
  460. u="d", --turn up, reverse: turn down (tu does nothing)
  461. d="u", --turn down, reverse: turn up (td does nothing)
  462. r="l", --turn right, reverse: turn left
  463. l="r", --turn left, reverse: turn right
  464. }
  465.  
  466.  
  467. -- This is a lookup table containing functions
  468. -- go uses this table to simply look up what function is needed to do the given action
  469. goT={
  470. --go forward then update position
  471. f=function()
  472.  if(inSimulation or turtle.forward()) then
  473.   incrementSteps()
  474.   goUpdate(posT["f"])
  475.   return true
  476.  end
  477.  return false
  478. end,
  479.  
  480.  
  481. --go up then update position
  482. u=function()
  483.  if(inSimulation or turtle.up()) then
  484.   incrementSteps()
  485.   goUpdate("u")
  486.   return true
  487.  end
  488.  return false
  489. end,
  490.  
  491.  
  492. --go down then update position
  493. d=function()
  494.  if(inSimulation or turtle.down()) then
  495.   incrementSteps()
  496.   goUpdate("d")
  497.   return true
  498.  end
  499.  return false
  500. end,
  501.  
  502. }
  503.  
  504.  
  505. turnT={
  506. --turn right and then update position
  507. r=function()
  508.  if(inSimulation or turtle.turnRight()) then
  509.   incrementTurns()
  510.   turnUpdate("r")
  511.   return true
  512.  end
  513.  return false
  514. end,
  515.  
  516.  
  517. --turn left and then update position
  518. l=function()
  519.  if(inSimulation or turtle.turnLeft()) then
  520.   incrementTurns()
  521.   turnUpdate("l")
  522.   return true
  523.  end
  524.  return false
  525. end,
  526.  
  527.  
  528. --does nothing, but may avoid special case in your code
  529. u=function() return true end,
  530.  
  531.  
  532. --does nothing, but may avoid special case in your code
  533. d=function() return true end,
  534.  
  535.  
  536. --does nothing, keeps facing forward
  537. f=function() return true end,
  538.  
  539.  
  540. --calls turn right twice, used to turn 180 degrees
  541. b=function()
  542.  return turn("r") and turn("r")
  543. end,
  544.  
  545. }
  546.  
  547.  
  548. --given current facing and wanted facing
  549. --used to look up the needed turn action
  550. faceT={
  551. n={
  552. n="f",
  553. s="b",
  554. e="r",
  555. w="l",
  556. },
  557. s={
  558. s="f",
  559. n="b",
  560. w="r",
  561. e="l",
  562. },
  563. e={
  564. e="f",
  565. w="b",
  566. s="r",
  567. n="l",
  568. },
  569. w={
  570. w="f",
  571. e="b",
  572. n="r",
  573. s="l",
  574. },
  575. }
  576.  
  577.  
  578. --lookup table to se what the resulting facing is after doing a turn in the given direction
  579. --does not include u or d, because they don’t change the facing
  580. --does not include n,e,s,w becuase they are alredy facings
  581. --each subtable is the inverse of the corresponding subtable in faceT
  582. turnResT=
  583. {
  584. n={
  585. f="n",
  586. b="s",
  587. r="e",
  588. l="w",
  589. },
  590. s={
  591. f="s",
  592. b="n",
  593. r="w",
  594. l="e",
  595. },
  596. e={
  597. f="e",
  598. b="w",
  599. r="s",
  600. l="n",
  601. },
  602. w={
  603. f="w",
  604. b="e",
  605. r="n",
  606. l="s",
  607. },
  608. }
  609.  
  610.  
  611. --http://stackoverflow.com/questions/7925090/lua-find-a-key-from-a-value
  612. --function table_invert(t)
  613. --   local s={}
  614. --   for k,v in pairs(t) do
  615. --     s[v]=k
  616. --   end
  617. --   return s
  618. --end
  619.  
  620.  
  621. --turnResT={
  622. --n=table_invert(faceT["n"]),
  623. --s=table_invert(faceT["s"]),
  624. --e=table_invert(faceT["e"]),
  625. --w=table_invert(faceT["w"]),
  626. --}
  627.  
  628.  
  629. --use to look up the coordinate change
  630. --going one step in the given direction
  631. goUpdateT={
  632. n={"z",-1},
  633. s={"z",1},
  634. e={"x",1},
  635. w={"x",-1},
  636. u={"y",1},
  637. d={"y",-1},
  638. }
  639.  
  640.  
  641. --used to look up the direction
  642. --to increase the given coordinate
  643. fieldToDirectionT={
  644. x="e",
  645. y="u",
  646. z="s",
  647. }
  648.  
  649.  
  650. attackT={
  651. f=function() return inSimulation or turtle.attack() end,
  652. u=function() return inSimulation or turtle.attackUp() end,
  653. d=function() return inSimulation or turtle.attackDown() end,
  654. }
  655.  
  656.  
  657. digT={
  658. f=function() return inSimulation or turtle.dig() end,
  659. u=function() return inSimulation or turtle.digUp() end,
  660. d=function() return inSimulation or turtle.digDown() end,
  661. }
  662.  
  663.  
  664. placeT={
  665. f=function(signText) return inSimulation or turtle.place(signText) end,--TEST
  666. u=function() return inSimulation or turtle.placeUp() end,
  667. d=function() return inSimulation or turtle.placeDown() end,
  668. }
  669.  
  670.  
  671. detectT={
  672. f=function() return inSimulation or turtle.detect() end,
  673. u=function() return inSimulation or turtle.detectUp() end,
  674. d=function() return inSimulation or turtle.detectDown() end,
  675. }
  676.  
  677.  
  678. compareT={
  679. f=function() return inSimulation or turtle.compare() end,
  680. u=function() return inSimulation or turtle.compareUp() end,
  681. d=function() return inSimulation or turtle.compareDown() end,
  682. }
  683.  
  684.  
  685. dropT={
  686. f=function(count) return inSimulation or turtle.drop(count) end, --TEST
  687. u=function(count) return inSimulation or turtle.dropUp(count) end,--TEST
  688. d=function(count) return inSimulation or turtle.dropDown(count) end,--TEST
  689. }
  690.  
  691.  
  692. suckT={
  693. f=function() return inSimulation or turtle.suck() end,
  694. u=function() return inSimulation or turtle.suckUp() end,
  695. d=function() return inSimulation or turtle.suckDown() end,
  696. }
  697.  
  698. inspectT={
  699. f=function() return turtle.inspect() end,
  700. u=function() return turtle.inspectUp() end,
  701. d=function() return turtle.inspectDown() end,
  702. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement