Advertisement
vladimir264

ComputerCraft Tst API

May 10th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1. --By Vlad [vladimir264]
  2.  
  3. --Max WxH is 51x19
  4. --REQUIRES 'grid' API!
  5.  
  6. os.loadAPI("grid")
  7. blocks={}
  8.  
  9. i=1
  10. b=1
  11. j=1
  12.  
  13. function testTerrain(fileName)
  14.     file = fs.open(fileName, "r")
  15.     if file then
  16.         while b==1 do
  17.             a=file.readLine()
  18.             --print(tostring(a)) --[THIS LINE IS FOR DEBUGGING PURPOSES ONLY]
  19.             if a~=nil then
  20.                 ln=string.len(a)
  21.                 for I=1,ln do
  22.                     if string.find(a,"d") then
  23.                         blocks[j]={x=string.find(a,"d"),y=i,t="grass"}
  24.                         a=string.gsub(a,"d","G",1)
  25.                         j=j+1
  26.                     end
  27.                     if string.find(a,"c") then
  28.                         blocks[j]={x=string.find(a,"c"),y=i,t="dirt"}
  29.                         a=string.gsub(a,"c","D",1)
  30.                         j=j+1
  31.                     end
  32.                     if string.find(a,"8") then
  33.                         blocks[j]={x=string.find(a,"8"),y=i,t="stone"}
  34.                         a=string.gsub(a,"8","S",1)
  35.                         j=j+1
  36.                     end
  37.                     if string.find(a,"0") then
  38.                         blocks[j]={x=string.find(a,"0"),y=i,t="iron"}
  39.                         a=string.gsub(a,"0","=",1)
  40.                         j=j+1
  41.                     end
  42.                     if string.find(a,"1") then
  43.                         blocks[j]={x=string.find(a,"1"),y=i,t="source"}
  44.                         a=string.gsub(a,"1","+",1)
  45.                         j=j+1
  46.                     end
  47.                     if string.find(a,"7") then
  48.                         blocks[j]={x=string.find(a,"7"),y=i,t="light"}
  49.                         a=string.gsub(a,"7","-",1)
  50.                         j=j+1
  51.                     end
  52.                 end
  53.             else
  54.                 b=0
  55.             end
  56.             i=i+1
  57.             if i>19 or i==20 then
  58.                 i=1
  59.                 b=0
  60.             end
  61.         end
  62.         file.close()
  63.     end
  64. end
  65.  
  66. function reCreate()
  67.     for k,v in pairs(blocks) do
  68.         if blocks[k].t=="grass" then
  69.             paintutils.drawPixel(blocks[k].x,blocks[k].y,colors.green)
  70.             grid.setBlock(blocks[k].x,blocks[k].y)
  71.         elseif blocks[k].t=="dirt" then
  72.             paintutils.drawPixel(blocks[k].x,blocks[k].y,colors.brown)
  73.             grid.setBlock(blocks[k].x,blocks[k].y)
  74.         elseif blocks[k].t=="stone" then
  75.             paintutils.drawPixel(blocks[k].x,blocks[k].y,colors.lightGray)
  76.             grid.setBlock(blocks[k].x,blocks[k].y)
  77.         elseif blocks[k].t=="iron" then
  78.             paintutils.drawPixel(blocks[k].x,blocks[k].y,colors.white)
  79.             grid.setBlock(blocks[k].x,blocks[k].y)
  80.         --elseif blocks[k].t=="source" then
  81.         --  paintutils.drawPixel(blocks[k].x,blocks[k].y,colors.orange)
  82.         --  grid.setBlock(blocks[k].x,blocks[k].y)
  83.         --elseif blocks[k].t=="light" then
  84.         --  paintutils.drawPixel(blocks[k].x,blocks[k].y,colors.gray)
  85.         --  grid.setBlock(blocks[k].x,blocks[k].y)
  86.         end
  87.     end
  88. end
  89.  
  90. function setBlock(X,Y,T)
  91.     for k,v in pairs(blocks) do
  92.         K=k
  93.         if blocks[K].x~=X and blocks[K].y~=Y then
  94.             blocks[j]={x=X,y=Y,t=T}
  95.             grid.setBlock(X,Y)
  96.         end
  97.     end
  98. end
  99.  
  100. function removeBlock(X,Y)
  101.     for k,v in pairs(blocks) do
  102.         K=k
  103.         if blocks[K].x==X and blocks[K].y==Y then
  104.             grid.removeBlock(X,Y)
  105.             table.remove(blocks,K)
  106.         end
  107.     end
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement