Advertisement
skypop

CC Compas

Aug 4th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- Compas
  3. -- Get Turtle orientation (facing north, east, south or west)
  4. -- ComputerCraft 1.76
  5. -- Require Turtle
  6. --
  7. -- author: SukaiPoppuGo
  8. -- licence: CC BY-NC-SA 4.0
  9. -- https://creativecommons.org/licenses/by-nc-sa/4.0/
  10. --
  11.  
  12. slot = ...
  13.  
  14. if not slot or not tonumber(slot) or not turtle.select(tonumber(slot)) then
  15.     print([[Usage: compas <slot>
  16. slot provided by a block with facing block.state]])
  17.     return false
  18. end
  19.  
  20. --setup
  21. turtle.select(tonumber(slot))
  22. turtle.placeUp()
  23. local b,d,s = turtle.inspectUp()
  24.  
  25. if not b then
  26.     print(s)
  27.     return false
  28. end
  29.  
  30. local dir2num = {
  31.     ["north"]=1,
  32.     ["east"]=2,
  33.     ["south"]=3,
  34.     ["west"]=4,
  35. }
  36. local num2dir = {
  37.     "north",
  38.     "east",
  39.     "south",
  40.     "west",
  41. }
  42.  
  43. --Database
  44. local tip = {
  45. --forward
  46.     ["minecraft:brick_stairs"]=0,
  47.     ["minecraft:sandstone_stairs"]=0,
  48.     ["minecraft:red_sandstone_stairs"]=0,
  49.     ["minecraft:stone_brick_stairs"]=0,
  50.     ["minecraft:nether_brick_stairs"]=0,
  51.     ["minecraft:oak_stairs"]=0,
  52.     ["minecraft:dark_oak_stairs"]=0,
  53.     ["minecraft:acacia_stairs"]=0,
  54.     ["minecraft:spruce_stairs"]=0,
  55.     ["minecraft:birch_stairs"]=0,
  56.     ["minecraft:jungle_stairs"]=0,
  57.     ["minecraft:quartz_stairs"]=0,
  58. --inverted
  59.     ["minecraft:chest"]=2,
  60.     ["minecraft:furnace"]=2,
  61.     ["minecraft:trapped_chest"]=2,
  62.     ["minecraft:ender_chest"]=2,
  63.     ["minecraft:dropper"]=2,
  64.     ["minecraft:dispenser"]=2,
  65.     ["minecraft:powered_repeater"]=2,
  66.     ["minecraft:unpowered_repeater"]=2,
  67.     ["minecraft:unpowered_comparator"]=2,
  68.     ["minecraft:pumpkin"]=2,
  69.     ["minecraft:lit_pumpkin"]=2,
  70.     ["computercraft:CC-Computer"]=2,
  71.     ["computercraft:CC-command_computer"]=2,
  72.     ["computercraft:CC-Peripheral"]=2,
  73. --ccw
  74.     ["minecraft:anvil"]=-1,
  75.     ["computercraft:CC-Turtle"]=-1,
  76.     ["computercraft:CC-TurtleAdvanced"]=-1,
  77. }
  78.  
  79.  
  80. if d and d.state and d.state.facing then
  81.     if tip[d.name] then
  82.         -- local sDir = num2dir[((dir2num[d.state.facing]+tip[d.name]-1)%4)+1]
  83.        
  84.         local nRawDir = dir2num[d.state.facing] -- as north=1, east=2, south=3, west=4
  85.         -- Correction according to how the block is positioned
  86.         local nCorrect = nRawDir + tip[d.name]
  87.         -- Determine the correct index by modulo
  88.         local sDir = num2dir[((nCorrect-1)%4)+1]
  89.         print(sDir)
  90.         return sDir
  91.     else
  92.         print(string.format("Please update database with %s", d.name))
  93.         return false
  94.     end
  95. else
  96.     print(string.format("%s has no block.state.facing property", d.name))
  97.     return false
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement