daily pastebin goal
66%
SHARE
TWEET

utils v2.5

antonsavov Dec 7th, 2017 (edited) 72 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. --returns a point in the grid that is closest to the sample point defined with x, y and z
  11. function closestGridPoint(center, size, x, y, z)
  12.     local closestPoint = {
  13.         x = math.floor ((x-center.x) / size )*size+center.x,
  14.         y = math.floor ((y-center.y) / size )*size+center.y,
  15.         z = math.floor ((z-center.z) / size )*size+center.z
  16.     }
  17.     return closestPoint
  18. end
  19.  
  20. --returns a coordinate in the grid rhythm that is closest to the sample coordinate
  21. function closestGridCoord(centerCoord, size, sampleCoord)
  22.     local closestCoord = math.floor ((sampleCoord-centerCoord) / size )*size+centerCoord
  23.     return closestCoord
  24. end
  25.  
  26. --- Covers an area with chunkLoaders so that it stays always loaded
  27. -- requires the NEOTECH MOD
  28. -- The placement principle is:
  29. --
  30. --  . . . . . . . . . . . . . . . . . . . . . . . .
  31. --  . . . z . . . . . . . . . . . . . . . . . . . .
  32. --  . . . O x . . . . . . . . . . . . . . . . . . .
  33. --  . . . . . . . . . . . . . . . . . . . . . . . .
  34. --  . . . . . . . . . . . . . . . . . . . . . . . .
  35. --  . . . + + + C + + + + + C + + + + + . . . . . .
  36. --  . . . + + + + + + + + + + + + + + + . . . . . .
  37. --  . . . + + H H H H H H H H H H H + + . . . . . .
  38. --  . . . C + H H H H C H H H H H C + + . . . . . .
  39. --  . . . + + H H H H H H H H H H H + + . . . . . .
  40. --  . . . + + H H H H H H H H H H H + + . . . . . .
  41. --  . . . + + H C H H H H H C H H H + + . . . . . .
  42. --  . . . + + H H H H H H H H H H H + + . . . . . .
  43. --  . . . + + H H H H H H H H H H H + + . . . . . .
  44. --  . . . C + H H H H C H H H H H C + + . . . . . .
  45. --  . . . + + H H H H H H H H H H H + + . . . . . .
  46. --  . . . + + + C + + + + + C + + + + + . . . . . .
  47. --  . . . + + + + + + + + + + + + + + + . . . . . .
  48. --  . . . . . . . . . . . . . . . . . . . . . . . .
  49. --  . . . . . . . . . . . . . . . . . . . . . . . .
  50. --
  51. -- C - a chunkloader with radius 3 chunks
  52. -- . - a chunk that is not part of the desired area
  53. -- o - a chunk that is part of the desired area
  54. -- H - a chunk that is not part of the desired area but included in the ones considered for a chunkloader
  55. -- O - the chunk which is taken as origin for the criss-cross grid
  56. -- x - positive direction of x axis
  57. -- z - positive direction of z axis
  58. -- this criss-cross(or whatever is called) distribution leads to the least amount of required chunkloaders
  59. -- 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
  60. -- the chunkloaders are not placed asyncronously but directly (commands.setblock is used and not commands.async.setblock)
  61. -- the chunkloaders are placed in the 0,y,0 coordinate of a chunk
  62. --
  63. --
  64. -- @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
  65. -- x, y, z are the corner of the area and are also given in minecraft world coordinates
  66. -- sizeX and sizeZ are the width and length of the area and are given in minecraft blocks and can be negative
  67. -- @param remove If "remove" is set to true then the function removes the chunkloaders in the area and replaces them with AIR.
  68. function do_chunk_loaders(origin,x,y,z,sizeX,sizeZ,remove)
  69.     --commands.setblock(-71, 56, -511, "neotech:chunkLoader", 0, "replace", '{id:"neotech:chunkLoader", Diameter:1}')
  70.     local radius = 2 -- the radius in chunks that the chunkloaders cover
  71.     local offset = 2 -- how many chunks to expand the area for placing of chunkloaders, expanding of 2 is needed with chunkLoaders with radius 3
  72.    
  73.     local ox = math.floor(origin.x/16) -- the grid origin chunk X index
  74.     local oz = math.floor(origin.z/16) -- the grid origin chunk Z index
  75.     --add chunkloaders in the area
  76.     local uX = sizeX/math.abs(sizeX)
  77.     local uZ = sizeZ/math.abs(sizeZ)
  78.     local startChunk = {
  79.         x = math.floor(x/16)-uX*offset,
  80.         z= math.floor(z/16)-uZ*offset
  81.     }
  82.     local endChunk = {
  83.         x = math.floor((x+sizeX)/16)+uX*offset,
  84.         z = math.floor((z+sizeZ)/16)+uZ*offset
  85.     }
  86.     --this makes sure we start to cover with chunkloaders from the origin out
  87.     --as if we do the oposite the further point might lie in a currently unloaded chunk and the whole thing will fail
  88.     --print("x values before", ox, sx, ex, stepx)
  89.     --print("sizes", sizeX, sizeZ)
  90.     local sx, ex, stepx
  91.     local sz, ez, stepz
  92.     if math.abs(startChunk.x - ox) < math.abs(endChunk.x - ox) then
  93.         sx = startChunk.x
  94.         ex = endChunk.x
  95.         stepx = uX
  96.     else
  97.         sx = endChunk.x
  98.         ex = startChunk.x
  99.         stepx = -uX
  100.     end
  101.     if math.abs(startChunk.z - oz) < math.abs(endChunk.z - oz) then
  102.         sz = startChunk.z
  103.         ez = endChunk.z    
  104.         stepz = uZ
  105.     else
  106.         sz = endChunk.z
  107.         ez = startChunk.z
  108.         stepz = -uZ
  109.     end
  110.     --print("x values", ox, sx, ex, stepx)
  111.     --print("z values", oz, sz, ez, stepz)
  112.     --now loop through the chunks
  113.     --ix = sx
  114.     --iz = sz
  115.     --while ix and iz are not both equalt to ex and ez respecitvely
  116.     --for kx from sx to ix
  117.     --if chunkloader must be placed at kx,iz place it
  118.     --end
  119.     --for kz from sz to iz
  120.     --if chunkloader must be placed at ix,kz then place it
  121.     -- increase both with one
  122.     -- if ix is bigger than ex make it ex
  123.     -- if iz is bigger than ez make it ez
  124.     --end while
  125.     for ix = sx, ex, stepx do
  126.         -- if the row is from the criss-cross grid
  127.  
  128.         if (ix - ox) % radius == 0 then
  129.             for iz = sz, ez, stepz do
  130.                 --if it is an even row dont shift the grid
  131.                 local place = false
  132.                 --print (((ix -ox)/radius) % 2)
  133.                 --print("for chunk", ix,iz)
  134.                 if ((ix -ox)/radius) % 2 == 0 then
  135.                     -- if the column is from the criss-cross grid
  136.                     --if (iz - oz) % (2*radius) == 0 then
  137.                     if (iz - oz) % (radius) == 0 then --makes a normal grid with step radius
  138.                         --place a chunkloader
  139.                         place = true
  140.                     end
  141.                 else --else if it is an odd row shift the grid
  142.                     -- if the column is from the criss-cross grid
  143.                     --if (iz - oz + radius) % (2*radius) == 0 then
  144.                     if (iz - oz + radius) % (radius) == 0 then --makes a normal grid with step radius                      
  145.                         --place a chunk loader
  146.                         place = true
  147.                     end
  148.                 end
  149.                 if place then
  150.                     -- do the actual placing of hte chunkloader
  151.                     local wx = ix*16 -- the world X coordinate of the chunkloader
  152.                     local wy = y -- the world Y coordinate of the chunkloader                          
  153.                     local wz = iz*16 -- the world Zcoordinate of the chunkloader                       
  154.                    
  155.                     if remove then
  156.                         --remove chunkloaders in the area
  157.                         print("removing chunkloader",wx,wy,wz)
  158.                         commands.setblock(wx, wy, wz, "minecraft:air", 0, "replace")
  159.                     else
  160.                         print("adding chunkloader",wx,wy,wz)
  161.                         --add chunkloaders in the area
  162.                         commands.setblock(wx, wy, wz, "neotech:chunkLoader", 0, "replace", '{id:"neotech:chunkLoader", Diameter:'..(radius+1)..'}')
  163.                         --commands.setblock(wx, wy, wz, "minecraft:wool", 0, "replace") -- just for testing
  164.                     end
  165.                     --sleep(0.1)
  166.                 end
  167.                
  168.             end
  169.         end
  170.  
  171.     end
  172. end
RAW Paste Data
Top