daily pastebin goal
15%
SHARE
TWEET

utils v2.5

antonsavov Dec 7th, 2017 (edited) 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- a collection of utiltity functions that are not depending on the variables and settings of the 20.000 BLOCKS project or on Minecraft stuff
  2.  
  3. -- check if the file exists
  4. function file_exists(file)
  5.   local f = fs.open(file, "rb")
  6.   if f then f:close() end
  7.   return f ~= nil
  8. end
  9.  
  10. --- table_print.
  11. -- does a table to a string.
  12. function table_print (tt, indent, done)
  13.   done = done or {}
  14.   indent = indent or 0
  15.   if type(tt) == "table" then
  16.     local sb = {}
  17.     for key, value in pairs (tt) do
  18.       table.insert(sb, string.rep (" ", indent)) -- indent it
  19.       if type (value) == "table" and not done [value] then
  20.         done [value] = true
  21.         table.insert(sb, "{\n");
  22.         table.insert(sb, table_print (value, indent + 2, done))
  23.         table.insert(sb, string.rep (" ", indent)) -- indent it
  24.         table.insert(sb, "}\n");
  25.       elseif "number" == type(key) then
  26.         table.insert(sb, string.format("\"%s\"\n", tostring(value)))
  27.       else
  28.         table.insert(sb, string.format(
  29.             "%s = \"%s\"\n", tostring (key), tostring(value)))
  30.        end
  31.     end
  32.     return table.concat(sb)
  33.   else
  34.     return tt .. "\n"
  35.   end
  36. end
  37.  
  38. --- universal to string.
  39. -- supports tables and other types
  40. function to_string( tbl )
  41.     if  "nil"       == type( tbl ) then
  42.         return tostring(nil)
  43.     elseif  "table" == type( tbl ) then
  44.         return table_print(tbl)
  45.     elseif  "string" == type( tbl ) then
  46.         return tbl
  47.     else
  48.         return tostring(tbl)
  49.     end
  50. end
  51.  
  52. --returns a point in the grid that is closest to the sample point defined with x, y and z
  53. function closestGridPoint(center, size, x, y, z)
  54.     local closestPoint = {
  55.         x = math.floor ((x-center.x) / size )*size+center.x,
  56.         y = math.floor ((y-center.y) / size )*size+center.y,
  57.         z = math.floor ((z-center.z) / size )*size+center.z
  58.     }
  59.     return closestPoint
  60. end
  61.  
  62. --returns a coordinate in the grid rhythm that is closest to the sample coordinate
  63. function closestGridCoord(centerCoord, size, sampleCoord)
  64.     local closestCoord = math.floor ((sampleCoord-centerCoord) / size )*size+centerCoord
  65.     return closestCoord
  66. end
  67.  
  68. --- Covers an area with chunkLoaders so that it stays always loaded
  69. -- requires the NEOTECH MOD
  70. -- The placement principle is:
  71. --
  72. --  . . . . . . . . . . . . . . . . . . . . . . . .
  73. --  . . . z . . . . . . . . . . . . . . . . . . . .
  74. --  . . . O x . . . . . . . . . . . . . . . . . . .
  75. --  . . . . . . . . . . . . . . . . . . . . . . . .
  76. --  . . . . . . . . . . . . . . . . . . . . . . . .
  77. --  . . . + + + C + + + + + C + + + + + . . . . . .
  78. --  . . . + + + + + + + + + + + + + + + . . . . . .
  79. --  . . . + + H H H H H H H H H H H + + . . . . . .
  80. --  . . . C + H H H H C H H H H H C + + . . . . . .
  81. --  . . . + + H H H H H H H H H H H + + . . . . . .
  82. --  . . . + + H H H H H H H H H H H + + . . . . . .
  83. --  . . . + + H C H H H H H C H H H + + . . . . . .
  84. --  . . . + + H H H H H H H H H H H + + . . . . . .
  85. --  . . . + + H H H H H H H H H H H + + . . . . . .
  86. --  . . . C + H H H H C H H H H H C + + . . . . . .
  87. --  . . . + + H H H H H H H H H H H + + . . . . . .
  88. --  . . . + + + C + + + + + C + + + + + . . . . . .
  89. --  . . . + + + + + + + + + + + + + + + . . . . . .
  90. --  . . . . . . . . . . . . . . . . . . . . . . . .
  91. --  . . . . . . . . . . . . . . . . . . . . . . . .
  92. --
  93. -- C - a chunkloader with radius 3 chunks
  94. -- . - a chunk that is not part of the desired area
  95. -- o - a chunk that is part of the desired area
  96. -- H - a chunk that is not part of the desired area but included in the ones considered for a chunkloader
  97. -- O - the chunk which is taken as origin for the criss-cross grid
  98. -- x - positive direction of x axis
  99. -- z - positive direction of z axis
  100. -- this criss-cross(or whatever is called) distribution leads to the least amount of required chunkloaders
  101. -- the placement is in a loop starting from closest to the origin out and sleeping 0.1 so that the latest placed chunkloader takes action
  102. -- the chunkloaders are not placed asyncronously but directly (commands.setblock is used and not commands.async.setblock)
  103. -- the chunkloaders are placed in the 0,y,0 coordinate of a chunk
  104. --
  105. --
  106. -- @param origin A block in the chunk which is taken as origin for the criss-cross grid. It is a tuple with .x, .y and .z fields - the coordinates are in minecraft world coordinates
  107. -- x, y, z are the corner of the area and are also given in minecraft world coordinates
  108. -- sizeX and sizeZ are the width and length of the area and are given in minecraft blocks and can be negative
  109. -- @param remove If "remove" is set to true then the function removes the chunkloaders in the area and replaces them with AIR.
  110. function do_chunk_loaders(origin,x,y,z,sizeX,sizeZ,remove)
  111.     --commands.setblock(-71, 56, -511, "neotech:chunkLoader", 0, "replace", '{id:"neotech:chunkLoader", Diameter:1}')
  112.     local radius = 2 -- the radius in chunks that the chunkloaders cover
  113.     local offset = 2 -- how many chunks to expand the area for placing of chunkloaders, expanding of 2 is needed with chunkLoaders with radius 3
  114.    
  115.     local ox = math.floor(origin.x/16) -- the grid origin chunk X index
  116.     local oz = math.floor(origin.z/16) -- the grid origin chunk Z index
  117.     --add chunkloaders in the area
  118.     local uX = sizeX/math.abs(sizeX)
  119.     local uZ = sizeZ/math.abs(sizeZ)
  120.     local startChunk = {
  121.         x = math.floor(x/16)-uX*offset,
  122.         z= math.floor(z/16)-uZ*offset
  123.     }
  124.     local endChunk = {
  125.         x = math.floor((x+sizeX)/16)+uX*offset,
  126.         z = math.floor((z+sizeZ)/16)+uZ*offset
  127.     }
  128.     --this makes sure we start to cover with chunkloaders from the origin out
  129.     --as if we do the oposite the further point might lie in a currently unloaded chunk and the whole thing will fail
  130.     --print("x values before", ox, sx, ex, stepx)
  131.     --print("sizes", sizeX, sizeZ)
  132.     local sx, ex, stepx
  133.     local sz, ez, stepz
  134.     if math.abs(startChunk.x - ox) < math.abs(endChunk.x - ox) then
  135.         sx = startChunk.x
  136.         ex = endChunk.x
  137.         stepx = uX
  138.     else
  139.         sx = endChunk.x
  140.         ex = startChunk.x
  141.         stepx = -uX
  142.     end
  143.     if math.abs(startChunk.z - oz) < math.abs(endChunk.z - oz) then
  144.         sz = startChunk.z
  145.         ez = endChunk.z    
  146.         stepz = uZ
  147.     else
  148.         sz = endChunk.z
  149.         ez = startChunk.z
  150.         stepz = -uZ
  151.     end
  152.     --print("x values", ox, sx, ex, stepx)
  153.     --print("z values", oz, sz, ez, stepz)
  154.     --now loop through the chunks
  155.     --ix = sx
  156.     --iz = sz
  157.     --while ix and iz are not both equalt to ex and ez respecitvely
  158.     --for kx from sx to ix
  159.     --if chunkloader must be placed at kx,iz place it
  160.     --end
  161.     --for kz from sz to iz
  162.     --if chunkloader must be placed at ix,kz then place it
  163.     -- increase both with one
  164.     -- if ix is bigger than ex make it ex
  165.     -- if iz is bigger than ez make it ez
  166.     --end while
  167.     for ix = sx, ex, stepx do
  168.         -- if the row is from the criss-cross grid
  169.  
  170.         if (ix - ox) % radius == 0 then
  171.             for iz = sz, ez, stepz do
  172.                 --if it is an even row dont shift the grid
  173.                 local place = false
  174.                 --print (((ix -ox)/radius) % 2)
  175.                 --print("for chunk", ix,iz)
  176.                 if ((ix -ox)/radius) % 2 == 0 then
  177.                     -- if the column is from the criss-cross grid
  178.                     --if (iz - oz) % (2*radius) == 0 then
  179.                     if (iz - oz) % (radius) == 0 then --makes a normal grid with step radius
  180.                         --place a chunkloader
  181.                         place = true
  182.                     end
  183.                 else --else if it is an odd row shift the grid
  184.                     -- if the column is from the criss-cross grid
  185.                     --if (iz - oz + radius) % (2*radius) == 0 then
  186.                     if (iz - oz + radius) % (radius) == 0 then --makes a normal grid with step radius                      
  187.                         --place a chunk loader
  188.                         place = true
  189.                     end
  190.                 end
  191.                 if place then
  192.                     -- do the actual placing of hte chunkloader
  193.                     local wx = ix*16 -- the world X coordinate of the chunkloader
  194.                     local wy = y -- the world Y coordinate of the chunkloader                          
  195.                     local wz = iz*16 -- the world Zcoordinate of the chunkloader                       
  196.                    
  197.                     if remove then
  198.                         --remove chunkloaders in the area
  199.                         print("removing chunkloader",wx,wy,wz)
  200.                         commands.setblock(wx, wy, wz, "minecraft:air", 0, "replace")
  201.                     else
  202.                         print("adding chunkloader",wx,wy,wz)
  203.                         --add chunkloaders in the area
  204.                         commands.setblock(wx, wy, wz, "neotech:chunkLoader", 0, "replace", '{id:"neotech:chunkLoader", Diameter:'..(radius+1)..'}')
  205.                         --commands.setblock(wx, wy, wz, "minecraft:wool", 0, "replace") -- just for testing
  206.                     end
  207.                     --sleep(0.1)
  208.                 end
  209.                
  210.             end
  211.         end
  212.  
  213.     end
  214. end
RAW Paste Data
Top