Advertisement
korczoczek

viewPattern.lua

Feb 24th, 2024 (edited)
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.63 KB | Source Code | 0 0
  1. local args={...}
  2. ---------------------------------------
  3. --define where the peripherals are HERE
  4. local hex_location="top"
  5. local gpu_location="bottom"
  6. --set background color
  7. local background=0x00007f00
  8. ---------------------------------------
  9. local hex=peripheral.wrap(hex_location)
  10. local hexType=peripheral.getType(hex_location)
  11. local gpu=peripheral.wrap(gpu_location)
  12. --reset screen
  13. gpu.sync()
  14. gpu.setSize(64)
  15. gpu.refreshSize()
  16. gpu.fill(background)
  17. gpu.setFont("ascii")
  18. gpu.sync()
  19. --get screen size
  20. local screenX,screenY,screenBlockX,screenBlockY=gpu.getSize()
  21. --set scale
  22. local blockSize=math.min(screenBlockX,screenBlockY)
  23. local gridScale=tonumber(args[1])
  24. if not gridScale then
  25.     gridScale=6*blockSize
  26. end
  27. print("gridScale: "..gridScale)
  28. --direction shorthand
  29. --1=EAST
  30. --2=SOUTH_EAST
  31. --3=SOUTH_WEST
  32. --4=WEST
  33. --5=NORTH_WEST
  34. --6=NORTH_EAST
  35.  
  36. --angle shorthand
  37. --d=sharp right
  38. --e=right
  39. --w=forward
  40. --q=left
  41. --a=sharp-left
  42.  
  43. local function getInitialDirection(startDir)
  44.     if startDir=="EAST" then--EAST
  45.         return 1
  46.     elseif startDir=="SOUTH_EAST" then--SOUTH_EAST
  47.         return 2
  48.     elseif startDir=="SOUTH_WEST" then--SOUTH_WEST
  49.         return 3
  50.     elseif startDir=="WEST" then--WEST
  51.         return 4
  52.     elseif startDir=="NORTH_WEST" then--NORTH_WEST
  53.         return 5
  54.     elseif startDir=="NORTH_EAST" then--NORTH_EAST
  55.         return 6
  56.     end
  57. end
  58.  
  59. local function updateDirection(c,direction)
  60.     if c=="d" then
  61.         direction=direction+2
  62.     elseif c=="e" then
  63.         direction=direction+1
  64.     elseif c=="w" then
  65.  
  66.     elseif c=="q" then
  67.         direction=direction-1
  68.     elseif c=="a" then
  69.         direction=direction-2
  70.     else
  71.         error("Program encountered unknown angle while reading pattern")
  72.         os.exit()
  73.     end
  74.     --check for over/underflow
  75.     if direction>6 then
  76.         direction=direction-6
  77.     elseif direction<1 then
  78.         direction=direction+6
  79.     end
  80.     return direction
  81. end
  82.  
  83. local function getLineCoords(x,y,direction)
  84.     if direction==1 then--EAST
  85.         x=x+gridScale
  86.     elseif direction==2 then--SOUTH_EAST
  87.         x=x+(gridScale/2)
  88.         y=y+gridScale
  89.     elseif direction==3 then--SOUTH_WEST
  90.         x=x-(gridScale/2)
  91.         y=y+gridScale
  92.     elseif direction==4 then--WEST
  93.         x=x-gridScale
  94.     elseif direction==5 then--NORTH_WEST
  95.         x=x-(gridScale/2)
  96.         y=y-gridScale
  97.     elseif direction==6 then--NORTH_EAST
  98.         x=x+(gridScale/2)
  99.         y=y-gridScale
  100.     end
  101.     return x,y
  102. end
  103.  
  104. local function getBounds(pattern,startDir)
  105.     local x,y,minX,minY,maxX,maxY=0,0,0,0,0,0
  106.     local direction=getInitialDirection(startDir)
  107.     x,y=getLineCoords(x,y,direction)
  108.     if x>maxX then
  109.         maxX=x
  110.     elseif x<minX then
  111.         minX=x
  112.     end
  113.     if y>maxY then
  114.         maxY=y
  115.     elseif y<minY then
  116.         minY=y
  117.     end
  118.     for i=1,#pattern do
  119.         local c=pattern:sub(i,i)
  120.         --update direction
  121.         direction=updateDirection(c,direction)
  122.         --update line coords
  123.         x,y=getLineCoords(x,y,direction)
  124.         if x>maxX then
  125.             maxX=x
  126.         elseif x<minX then
  127.             minX=x
  128.         end
  129.         if y>maxY then
  130.             maxY=y
  131.         elseif y<minY then
  132.             minY=y
  133.         end
  134.     end
  135.     return minX,minY,maxX,maxY
  136. end
  137.  
  138. local function getStart(pattern,startDir,startX,startY,sizeX,sizeY)
  139.     local minX,minY,maxX,maxY=getBounds(pattern,startDir)
  140.     --calc size
  141.     local patternSizeX=maxX-minX
  142.     local patternSizeY=maxY-minY
  143.     local offsetX=((sizeX-patternSizeX)/2)+(startX-1)
  144.     local offsetY=((sizeY-patternSizeY)/2)+(startY-1)
  145.     return math.floor(offsetX-minX),math.floor(offsetY-minY)
  146. end
  147.  
  148.  
  149. local function drawPattern(pattern,startDir,startX,startY,sizeX,sizeY)
  150.     if startX==nil then
  151.         startX=1
  152.     end
  153.     if startY==nil then
  154.         startY=1
  155.     end
  156.     if sizeX==nil then
  157.         sizeX=screenX
  158.     end
  159.     if sizeY==nil then
  160.         sizeY=screenY
  161.     end
  162.     local direction=getInitialDirection(startDir)
  163.     local x1,y1=getStart(pattern,startDir,startX,startY,sizeX,sizeY)
  164.     local x2,y2=x1,y1
  165.     local startX,startY=x1,y1
  166.     local color=0x00ffffff
  167.     local colorStep=math.floor(0xff/(string.len(pattern)+1))
  168.     local colorDiff=(colorStep*0x10000)+(colorStep*0x100)+colorStep
  169.     --init start direction
  170.     x2,y2=getLineCoords(x2,y2,direction)
  171.     --draw initial line
  172.     gpu.lineS(x1,y1,x2,y2,color)
  173.     for i=1,#pattern do
  174.         local c=pattern:sub(i,i)
  175.         --print(c)
  176.         --update direction
  177.         direction=updateDirection(c,direction)
  178.         --update line coords
  179.         x1=x2
  180.         y1=y2
  181.         x2,y2=getLineCoords(x2,y2,direction)
  182.         --draw line
  183.         color=color-colorDiff
  184.         if color<0x00080808 then
  185.             color=0x00080808
  186.         end
  187.         gpu.lineS(x1,y1,x2,y2,color)
  188.     end
  189.     --denote start
  190.     gpu.filledRectangle(startX-1,startY-1,3,3,0xffff0000)
  191. end
  192.  
  193. local function drawList(iota)
  194.     while true do
  195.         for i=1,#iota do
  196.             local pattern=iota[i]
  197.             print("Pattern: "..i.."/"..#iota)
  198.             print("startDir: "..pattern.startDir)
  199.             print("angles: "..pattern.angles)
  200.             gpu.fill(background)
  201.             drawPattern(pattern.angles,pattern.startDir)
  202.             gpu.drawText(2,2,i.."/"..#iota,0x00000000)
  203.             gpu.sync()
  204.             sleep(2)
  205.         end
  206.     end
  207. end
  208.  
  209. local function diplayGenericIota(iotaType,iota)
  210.     if iotaType=="" or iotaType==nil then
  211.         print("Iota is Empty")
  212.         return
  213.     elseif iotaType=="hexcasting:pattern" then
  214.         print("Single Pattern")
  215.         print("startDir: "..iota.startDir)
  216.         print("angles: "..iota.angles)
  217.         drawPattern(iota.angles,iota.startDir)
  218.         gpu.sync()
  219.         return
  220.     elseif iotaType=="hexcasting:list" then
  221.         print("Pattern List")
  222.         drawList(iota)
  223.         return
  224.     elseif iotaType=="hexcasting:vec3" then
  225.         print("Vector")
  226.         print(iota.x)
  227.         print(iota.y)
  228.         print(iota.z)
  229.         gpu.drawText(2,2,"Vector:",0x00000000)
  230.         gpu.drawText(2,10,string.format("%.3f", iota.x),0x00000000)
  231.         gpu.drawText(2,18,string.format("%.3f", iota.y),0x00000000)
  232.         gpu.drawText(2,26,string.format("%.3f", iota.z),0x00000000)
  233.         gpu.sync()
  234.         return
  235.     elseif iotaType=="hexcasting:double" then
  236.         print("Double")
  237.         print(iota)
  238.         gpu.drawText(2,2,"Double:",0x00000000)
  239.         gpu.drawText(2,10,iota,0x00000000)
  240.         gpu.sync()
  241.         return
  242.     elseif iotaType=="hexcasting:entity" then
  243.         local entityType
  244.         if iota.isPlayer then
  245.             entityType="Player"
  246.         else
  247.             entityType="Non-Player"
  248.         end
  249.         print(entityType.." Entity:")
  250.         print(iota.name)
  251.         print(iota.uuid)
  252.         gpu.drawText(2,2,entityType.." Entity:",0x00000000)
  253.         gpu.drawText(2,10,iota.name,0x00000000)
  254.         gpu.drawText(2,18,iota.uuid,0x00000000)
  255.         gpu.sync()
  256.         return
  257.     else
  258.         print("Unknown iota")
  259.     end
  260. end
  261.  
  262. --determine storage type
  263. if hexType=="slate" then
  264.     local pattern=hex.readPattern()
  265.     print("Slate")
  266.     print("startDir: "..pattern.startDir)
  267.     print("angles: "..pattern.angles)
  268.     drawPattern(pattern.angles,pattern.startDir)
  269.     gpu.sync()
  270. elseif hexType=="focal_port" then
  271.     if hex.hasFocus() then
  272.         diplayGenericIota(hex.getIotaType(),hex.readIota())
  273.     else
  274.         print("Focal port is empty")
  275.     end
  276. elseif hexType=="akashic_bookshelf" then
  277.     diplayGenericIota(hex.shelfIotaType,hex.shelfIota)
  278. else
  279.     print("Unknown peripheral")
  280. end
  281.  
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement