Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- a collection of utiltity functions that are not depending on the variables and settings of the 20.000 BLOCKS project or on Minecraft stuff
- -- check if the file exists
- function file_exists(file)
- local f = fs.open(file, "rb")
- if f then f:close() end
- return f ~= nil
- end
- --- table_print.
- -- does a table to a string.
- function table_print (tt, indent, done)
- done = done or {}
- indent = indent or 0
- if type(tt) == "table" then
- local sb = {}
- for key, value in pairs (tt) do
- table.insert(sb, string.rep (" ", indent)) -- indent it
- if type (value) == "table" and not done [value] then
- done [value] = true
- table.insert(sb, "{\n");
- table.insert(sb, table_print (value, indent + 2, done))
- table.insert(sb, string.rep (" ", indent)) -- indent it
- table.insert(sb, "}\n");
- elseif "number" == type(key) then
- table.insert(sb, string.format("\"%s\"\n", tostring(value)))
- else
- table.insert(sb, string.format(
- "%s = \"%s\"\n", tostring (key), tostring(value)))
- end
- end
- return table.concat(sb)
- else
- return tt .. "\n"
- end
- end
- --- universal to string.
- -- supports tables and other types
- function to_string( tbl )
- if "nil" == type( tbl ) then
- return tostring(nil)
- elseif "table" == type( tbl ) then
- return table_print(tbl)
- elseif "string" == type( tbl ) then
- return tbl
- else
- return tostring(tbl)
- end
- end
- --returns a point in the grid that is closest to the sample point defined with x, y and z
- function closestGridPoint(center, size, x, y, z)
- local closestPoint = {
- x = math.floor ((x-center.x) / size )*size+center.x,
- y = math.floor ((y-center.y) / size )*size+center.y,
- z = math.floor ((z-center.z) / size )*size+center.z
- }
- return closestPoint
- end
- --returns a coordinate in the grid rhythm that is closest to the sample coordinate
- function closestGridCoord(centerCoord, size, sampleCoord)
- local closestCoord = math.floor ((sampleCoord-centerCoord) / size )*size+centerCoord
- return closestCoord
- end
- --- Covers an area with chunkLoaders so that it stays always loaded
- -- requires the NEOTECH MOD
- -- The placement principle is:
- --
- -- . . . . . . . . . . . . . . . . . . . . . . . .
- -- . . . z . . . . . . . . . . . . . . . . . . . .
- -- . . . O x . . . . . . . . . . . . . . . . . . .
- -- . . . . . . . . . . . . . . . . . . . . . . . .
- -- . . . . . . . . . . . . . . . . . . . . . . . .
- -- . . . + + + C + + + + + C + + + + + . . . . . .
- -- . . . + + + + + + + + + + + + + + + . . . . . .
- -- . . . + + H H H H H H H H H H H + + . . . . . .
- -- . . . C + H H H H C H H H H H C + + . . . . . .
- -- . . . + + H H H H H H H H H H H + + . . . . . .
- -- . . . + + H H H H H H H H H H H + + . . . . . .
- -- . . . + + H C H H H H H C H H H + + . . . . . .
- -- . . . + + H H H H H H H H H H H + + . . . . . .
- -- . . . + + H H H H H H H H H H H + + . . . . . .
- -- . . . C + H H H H C H H H H H C + + . . . . . .
- -- . . . + + H H H H H H H H H H H + + . . . . . .
- -- . . . + + + C + + + + + C + + + + + . . . . . .
- -- . . . + + + + + + + + + + + + + + + . . . . . .
- -- . . . . . . . . . . . . . . . . . . . . . . . .
- -- . . . . . . . . . . . . . . . . . . . . . . . .
- --
- -- C - a chunkloader with radius 3 chunks
- -- . - a chunk that is not part of the desired area
- -- o - a chunk that is part of the desired area
- -- H - a chunk that is not part of the desired area but included in the ones considered for a chunkloader
- -- O - the chunk which is taken as origin for the criss-cross grid
- -- x - positive direction of x axis
- -- z - positive direction of z axis
- -- this criss-cross(or whatever is called) distribution leads to the least amount of required chunkloaders
- -- 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
- -- the chunkloaders are not placed asyncronously but directly (commands.setblock is used and not commands.async.setblock)
- -- the chunkloaders are placed in the 0,y,0 coordinate of a chunk
- --
- --
- -- @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
- -- x, y, z are the corner of the area and are also given in minecraft world coordinates
- -- sizeX and sizeZ are the width and length of the area and are given in minecraft blocks and can be negative
- -- @param remove If "remove" is set to true then the function removes the chunkloaders in the area and replaces them with AIR.
- function do_chunk_loaders(origin,x,y,z,sizeX,sizeZ,remove)
- --commands.setblock(-71, 56, -511, "neotech:chunkLoader", 0, "replace", '{id:"neotech:chunkLoader", Diameter:1}')
- local radius = 2 -- the radius in chunks that the chunkloaders cover
- local offset = 2 -- how many chunks to expand the area for placing of chunkloaders, expanding of 2 is needed with chunkLoaders with radius 3
- local ox = math.floor(origin.x/16) -- the grid origin chunk X index
- local oz = math.floor(origin.z/16) -- the grid origin chunk Z index
- --add chunkloaders in the area
- local uX = sizeX/math.abs(sizeX)
- local uZ = sizeZ/math.abs(sizeZ)
- local startChunk = {
- x = math.floor(x/16)-uX*offset,
- z= math.floor(z/16)-uZ*offset
- }
- local endChunk = {
- x = math.floor((x+sizeX)/16)+uX*offset,
- z = math.floor((z+sizeZ)/16)+uZ*offset
- }
- --this makes sure we start to cover with chunkloaders from the origin out
- --as if we do the oposite the further point might lie in a currently unloaded chunk and the whole thing will fail
- --print("x values before", ox, sx, ex, stepx)
- --print("sizes", sizeX, sizeZ)
- local sx, ex, stepx
- local sz, ez, stepz
- if math.abs(startChunk.x - ox) < math.abs(endChunk.x - ox) then
- sx = startChunk.x
- ex = endChunk.x
- stepx = uX
- else
- sx = endChunk.x
- ex = startChunk.x
- stepx = -uX
- end
- if math.abs(startChunk.z - oz) < math.abs(endChunk.z - oz) then
- sz = startChunk.z
- ez = endChunk.z
- stepz = uZ
- else
- sz = endChunk.z
- ez = startChunk.z
- stepz = -uZ
- end
- --print("x values", ox, sx, ex, stepx)
- --print("z values", oz, sz, ez, stepz)
- --now loop through the chunks
- --ix = sx
- --iz = sz
- --while ix and iz are not both equalt to ex and ez respecitvely
- --for kx from sx to ix
- --if chunkloader must be placed at kx,iz place it
- --end
- --for kz from sz to iz
- --if chunkloader must be placed at ix,kz then place it
- -- increase both with one
- -- if ix is bigger than ex make it ex
- -- if iz is bigger than ez make it ez
- --end while
- for ix = sx, ex, stepx do
- -- if the row is from the criss-cross grid
- if (ix - ox) % radius == 0 then
- for iz = sz, ez, stepz do
- --if it is an even row dont shift the grid
- local place = false
- --print (((ix -ox)/radius) % 2)
- --print("for chunk", ix,iz)
- if ((ix -ox)/radius) % 2 == 0 then
- -- if the column is from the criss-cross grid
- --if (iz - oz) % (2*radius) == 0 then
- if (iz - oz) % (radius) == 0 then --makes a normal grid with step radius
- --place a chunkloader
- place = true
- end
- else --else if it is an odd row shift the grid
- -- if the column is from the criss-cross grid
- --if (iz - oz + radius) % (2*radius) == 0 then
- if (iz - oz + radius) % (radius) == 0 then --makes a normal grid with step radius
- --place a chunk loader
- place = true
- end
- end
- if place then
- -- do the actual placing of hte chunkloader
- local wx = ix*16 -- the world X coordinate of the chunkloader
- local wy = y -- the world Y coordinate of the chunkloader
- local wz = iz*16 -- the world Zcoordinate of the chunkloader
- if remove then
- --remove chunkloaders in the area
- print("removing chunkloader",wx,wy,wz)
- commands.setblock(wx, wy, wz, "minecraft:air", 0, "replace")
- else
- print("adding chunkloader",wx,wy,wz)
- --add chunkloaders in the area
- commands.setblock(wx, wy, wz, "neotech:chunkLoader", 0, "replace", '{id:"neotech:chunkLoader", Diameter:'..(radius+1)..'}')
- --commands.setblock(wx, wy, wz, "minecraft:wool", 0, "replace") -- just for testing
- end
- --sleep(0.1)
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement