Advertisement
Wyvern67

CityBuilder

Jul 11th, 2015
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.82 KB | None | 0 0
  1. local old = {}
  2. local cPos = {}
  3. local glass = "minecraft:glass"
  4. local officialRelease = true
  5. local activateTheExamples = true -- You won't be able to use it as an API if you activate this but you will have access to cool examples when you launch it
  6. dangerousAmountOfQueries = 850 -- blocks
  7. restingTime = 0.1 -- seconds
  8.  
  9. xa,ya,za = commands.getBlockPosition() -- xa,ya and za are used everywhere so...
  10. cPos.x = xa
  11. cPos.y = ya
  12. cPos.z = za
  13. -- computer's position is stored there
  14.  
  15. buildingColors = {
  16.     "gray",
  17.     "lightGray",
  18.     "brown",
  19.     "white"
  20. }
  21. buildingTypes = {
  22.     "squareBuilding",
  23.     "squareBuilding",
  24.     "spikyBuilding"
  25. }
  26. typesOfLastFloors = {
  27.     "penthouse",
  28.     "terrace"
  29. }
  30.  
  31. wallBlocks = {}
  32. wallBlocks["gray"] = {
  33.     "chisel:cobblestone:1",
  34.     "chisel:cobblestone:2",
  35.     "chisel:cobblestone:3",
  36.     "chisel:cobblestone:4",
  37.     "chisel:cobblestone:7",
  38.     "chisel:cobblestone:15"
  39. }
  40. wallBlocks["lightGray"] = {
  41.     "chisel:andesite:2",
  42.     "chisel:andesite:1",
  43.     "chisel:andesite:3",
  44.     "chisel:andesite:4",
  45.     "chisel:cobblestone:10"
  46. }
  47. wallBlocks["brown"] = {
  48.     "chisel:concrete:1",
  49.     "chisel:concrete:2",
  50.     "chisel:concrete:5",
  51.     "chisel:concrete:7",
  52.     "chisel:concrete:3",
  53.     "chisel:factoryblock:1"
  54. }
  55. wallBlocks["white"] = {
  56.     "chisel:holystone:4",
  57.     "chisel:holystone:6",
  58.     "chisel:holystone:13",
  59.     "chisel:diorite:1",
  60.     "chisel:diorite:2",
  61.     "chisel:diorite:3",
  62.     "chisel:diorite:6"
  63. }
  64.  
  65. floorBlocks = {}
  66. floorBlocks["gray"] = {
  67.     "chisel:cobblestone:1",
  68.     "chisel:cobblestone:2",
  69.     "chisel:cobblestone:3",
  70.     "chisel:cobblestone:4",
  71.     "chisel:cobblestone:7",
  72.     "chisel:cobblestone:15"
  73. }
  74. floorBlocks["lightGray"] = {
  75.     "chisel:andesite:1",
  76.     "chisel:andesite:3",
  77.     "chisel:cobblestone:10"
  78. }
  79. floorBlocks["brown"] = {
  80.     "chisel:concrete:1",
  81.     "chisel:concrete:2",
  82.     "chisel:concrete:5",
  83.     "chisel:concrete:7",
  84.     "chisel:concrete:3",
  85.     "chisel:concrete:9",
  86.     "chisel:concrete:8",
  87.     "chisel:factoryblock:1"
  88. }
  89. floorBlocks["white"] = {
  90.     "chisel:holystone:4",
  91.     "chisel:holystone:6",
  92.     "chisel:holystone:12",
  93.     "chisel:diorite:1",
  94.     "chisel:diorite:3",
  95.     "chisel:diorite:6"
  96. }
  97.  
  98. complementBlocks = {}
  99. complementBlocks["gray"] = {
  100.     "chisel:cobblestone:1",
  101.     "chisel:cobblestone:2",
  102.     "chisel:cobblestone:3",
  103.     "chisel:cobblestone:4",
  104.     "chisel:cobblestone:7",
  105.     "chisel:cobblestone:15"
  106. }
  107. complementBlocks["lightGray"] = {
  108.     "chisel:andesite:1",
  109.     "chisel:andesite:3",
  110.     "chisel:cobblestone:10"
  111. }
  112. complementBlocks["brown"] = {
  113.     "chisel:concrete:1",
  114.     "chisel:concrete:2",
  115.     "chisel:concrete:5",
  116.     "chisel:concrete:7",
  117.     "chisel:concrete:3",
  118.     "chisel:concrete:9",
  119.     "chisel:concrete:8",
  120.     "chisel:factoryblock:1"
  121. }
  122. complementBlocks["white"] = {
  123.     "chisel:holystone:4",
  124.     "chisel:holystone:6",
  125.     "chisel:holystone:12",
  126.     "chisel:diorite:1",
  127.     "chisel:diorite:3",
  128.     "chisel:diorite:6"
  129. }
  130. glassBlocks = {
  131.     "minecraft:glass",
  132.     "TConstruct:GlassBlock",
  133.     "TConstruct:GlassBlock.StainedClear",
  134.     "TConstruct:GlassBlock.StainedClear:7",
  135.     "TConstruct:GlassBlock.StainedClear:15",
  136.     "chisel:stained_glass_white",
  137.     "chisel:stained_glass_white:1",
  138.     "chisel:stained_glass_white:2"
  139. }
  140.  
  141. air = "minecraft:air"
  142.  
  143. if not officialRelease then
  144.     strtp = fs.open("startup", "w")
  145.     strtp.write('shell.run("dl", "citybuilder") shell.run("citybuilder")')
  146.     strtp.close()
  147. end
  148.  
  149. plan = {core = {},width = 120, length = 120}
  150.  
  151. function plan:at(i,j)
  152.     return self.core[i+self.width*j]
  153. end
  154.  
  155. function plan:set(i,j,v)
  156.     self.core[i+self.width*j] = v
  157. end
  158.  
  159. local function deltaT(timerID)
  160.     if type(lastCallFromTheID) ~= "table" then
  161.         lastCallFromTheID = {}
  162.     end
  163.    
  164.     if lastCallFromTheID[timerID] == nil then
  165.         lastCallFromTheID[timerID] = os.clock()
  166.     end
  167.    
  168.     temp = os.clock()-lastCallFromTheID[timerID]
  169.     lastCallFromTheID[timerID] = os.clock()
  170.     return temp
  171. end
  172.  
  173. old.execAsync = commands.execAsync -- Editing the default execAsync command to include a safeguard
  174. local function execAsync(str) -- Too many queries in a single blast will cause problems.
  175.     if execAsyncQueries == nil then
  176.         execAsyncQueries = 0
  177.     end
  178.    
  179.     timeDiff2 = deltaT("execAsync")
  180.     -- I'll measure how long I waited between each queries
  181.     -- As CC time units don't go after the 2d decimal i'll wait 0.1s every 850 queries
  182.     -- I call a row of queries a "blast"
  183.     -- (8500 q/s at best)
  184.     if timeDiff2 > restingTime then
  185.         execAsyncQueries = 0 --Blast of queries was cut off. I consider this one finished
  186.     else
  187.         execAsyncQueries = execAsyncQueries+1
  188.     end
  189.     -- console(execAsyncQueries, 7)
  190.     if execAsyncQueries > dangerousAmountOfQueries then
  191.         sleep(restingTime)
  192.         execAsyncQueries = 0 --just to be sure, manually consider the blast as finished
  193.     end
  194.    
  195.     return old.execAsync(str)
  196. end
  197. commands.execAsync = execAsync
  198.  
  199. local function isTrue(number)
  200.     if number == 0 then
  201.         return false
  202.     else
  203.         return true
  204.     end
  205. end
  206.  
  207. local function pickRandom(t)
  208.     return t[math.random(1,#t)]
  209. end
  210. table.pickRandom = pickRandom
  211.  
  212. local function round(num, idp)
  213.   local mult = 10^(idp or 0)
  214.   return math.floor(num * mult + 0.5) / mult
  215. end
  216. math.round = round
  217.  
  218. local function console(txt,line)
  219.     if monitor == nil then
  220.         monitor = term
  221.     end
  222.     if not officialRelease then
  223.         if line == nil then
  224.             line = 1
  225.         end
  226.         monitor.setCursorPos(1,line)
  227.         monitor.clearLine(line)
  228.         monitor.write(txt)
  229.         monitor.setCursorPos(1,line+1)
  230.     end
  231. end
  232.  
  233. local function forEachColumnOfAWallDoMyFunc(yourFunc, xa, ya, za, width, length)
  234.    
  235.     startXPos = xa  -- This is all complicated isn't it ? Why not do
  236.     startYPos = ya  -- for i=xa, xa+width ?
  237.     column = 1      -- Well, because it doesn't work. I tried and tried and tried. It should work, but it doesn't, and I can't see why, so I gave up and made this instead.
  238.     for i=1,#validColumns.front do
  239.         yourFunc(xa, ya, za, column, "south")
  240.         yourFunc(xa, ya+length-1, za, column, "north")
  241.         column = column+1
  242.         xa = xa+1
  243.     end
  244.  
  245.     xa = startXPos
  246.     column = 1
  247.     for i=1, #validColumns.side do
  248.         yourFunc(xa, ya, za, column, "west")
  249.         yourFunc(xa+width-1, ya, za, column, "east")
  250.         column = column+1
  251.         ya = ya+1
  252.     end
  253. end
  254.  
  255. function setBlock(block,xa,ya,za,oldBlockHandling)
  256.     local dataValue = ""
  257.     ya = (-1)*ya -- NORTH IS NOT FUCKING -y FUCK THIS
  258.     if type(oldBlockHandling) ~= "string" then
  259.         oldBlockHandling = ""
  260.     end
  261.     if string.find(block, ":") ~= nil then -- if blockname divided in parts separated by ":"s
  262.         i=0
  263.         local words = {}
  264.         for part in string.gmatch(block, "[^:]+") do -- get each part
  265.             i=i+1
  266.             words[i] = part
  267.         end
  268.         if string.match(words[#words], "%d") then -- if last part of blockname is a number
  269.             dataValue = words[#words] -- it's a dataValue
  270.             table.remove(words, #words) -- i like to enter block names the old school way.  
  271.                                         -- mod:block:metadata
  272.             block = table.concat(words, ":")
  273.         end
  274.     end
  275.    
  276.     -- print("setblock ~" .. tostring(xa) .. " ~" .. tostring(za) .. " ~".. tostring(ya) .. " " .. tostring(block) .. " " .. tostring(dataValue) .. " " .. tostring(oldBlockHandling))
  277.     commands.execAsync("setblock ~" .. tostring(xa) .. " ~" .. tostring(za) .. " ~".. tostring(ya) .. " " .. tostring(block) .. " " .. tostring(dataValue) .. " " .. tostring(oldBlockHandling))
  278. end
  279.  
  280. function fill(block, xa, ya, za, width, length, height)
  281.     queries = 0
  282.     if width <= 0 then width=1 end
  283.     if length <= 0 then length=1 end
  284.     if height <= 0 then height=1 end
  285.     -- fill(air,-7,1,-1,7,21,0)
  286.    
  287.     for i=xa,xa+width-1 do
  288.         for j=ya,ya+length-1 do
  289.             for k=za,za+height-1 do
  290.                 setBlock(block, i, j, k)
  291.                 queries = queries+1
  292.             end
  293.         end
  294.     end
  295.     return queries
  296. end
  297.  
  298. function fillInside(blockInside, blockOutside, xa, ya, za, width, length, height)
  299.     queries = fill(blockOutside, xa, ya, za, width, length, height)
  300.     queries = queries + fill(blockInside, xa+1, ya+1, za, width-2, length-2, height)
  301.     return queries
  302. end
  303.  
  304. function floor(blockInside, blockOutside, xa, ya, za, width, length)
  305.     return fillInside(blockInside, blockOutside, xa, ya, za, width, length, 1)
  306. end
  307.  
  308. function walls(block, xa, ya, za, width, length, height)
  309.     queries = fill(block, xa, ya, za, width, length, height)
  310.     queries = queries + fill(air, xa+1, ya+1, za, width-2, length-2, height)
  311.     return queries
  312. end
  313.  
  314. function room(blockFloor, blockWalls, blockComplement, xa, ya, za, width, length, height)
  315.     queries = fill(air, xa, ya, za, width, length, height)
  316.     queries = floor(blockFloor, blockComplement, xa, ya, za, width, length) + queries
  317.     queries = walls(blockWalls, xa, ya, za+1, width, length, height-2) + queries
  318.     queries = floor(blockFloor, blockComplement, xa, ya, za+height-1, width, length) + queries
  319.     return queries
  320. end
  321.  
  322. function getValidWindowsPostions(xa, ya, za, width, length, height, numberOfWindowsFront, numberOfWindowsSide)
  323.     local numberOfWindowsFront, numberOfWindowsSide
  324.     if numberOfWindowsFront == nil then
  325.         numberOfWindowsFront = math.floor(math.sqrt(width))
  326.     end
  327.     if numberOfWindowsSide == nil then
  328.         numberOfWindowsSide = math.floor(math.sqrt(length))
  329.     end
  330.    
  331.     local frontWindowsLength = math.floor((width-numberOfWindowsFront-1)/numberOfWindowsFront)
  332.     local frontMiddleWindowLength = width-(numberOfWindowsFront+1)-((numberOfWindowsFront-1)*frontWindowsLength)
  333.     local frontMiddleWindow = math.ceil(numberOfWindowsFront/2)
  334.     local frontMiddleWindowPosition = frontMiddleWindow + (frontMiddleWindow-1)*frontWindowsLength
  335.    
  336.     local sideWindowsLength = math.floor((length-numberOfWindowsSide-1)/numberOfWindowsSide)
  337.     local sideMiddleWindowLength = length-(numberOfWindowsSide+1)-((numberOfWindowsSide-1)*sideWindowsLength)
  338.     local sideMiddleWindow = math.ceil(numberOfWindowsSide/2)
  339.     local sideMiddleWindowPosition = sideMiddleWindow + (sideMiddleWindow-1)*sideWindowsLength
  340.    
  341.     -- Maintenant on a les dimensions des fenêtres. Longueurs. Positions. Tout.
  342.     -- L'idée est de parcourir le contour du mur et de dessiner les fenêtres.
  343.     validColumns = {}
  344.     validColumns.front = {}
  345.     validColumns.side = {}
  346.    
  347.         -- Murs de face et arrière
  348.     for i=1,width do --tout en verre
  349.         validColumns.front[i] = true
  350.     end
  351.     validColumns.front[1] = false --sauf le début
  352.     validColumns.front[width] = false --et la fin
  353.     i=1
  354.     while i < width/2 do --puis on fait vitre après vitre de gauche à droite jusqu'au milieu
  355.         validColumns.front[i] = false
  356.         i=i+frontWindowsLength+1
  357.     end
  358.     i=width
  359.     while i > width/2 do --puis de droite à gauche jusqu'au milieu (pour faire une symétrie)
  360.         validColumns.front[i] = false
  361.         i=i-frontWindowsLength-1
  362.     end
  363.     for i=frontMiddleWindowPosition+1,frontMiddleWindowPosition+frontMiddleWindowLength do
  364.         validColumns.front[i] = true
  365.     end
  366.  
  367.         -- Murs des côtés
  368.     for i=1,length do --tout en verre
  369.         validColumns.side[i] = true
  370.     end
  371.     validColumns.side[1] = false --sauf le début
  372.     validColumns.side[length] = false --et la fin
  373.     i=1
  374.     while i < length/2 do --puis on fait vitre après vitre depuis le début jusqu'au milieu
  375.         validColumns.side[i] = false
  376.         i=i+sideWindowsLength+1
  377.     end
  378.     i=length
  379.     while i > length/2 do --puis de la fin jusqu'au milieu
  380.         validColumns.side[i] = false
  381.         i=i-sideWindowsLength-1
  382.     end
  383.     local center = (length/2)
  384.     local otherCenter = math.ceil(sideMiddleWindowPosition+(sideMiddleWindowLength/2))
  385.     -- those are currently useless
  386.    
  387.     return validColumns
  388. end
  389.  
  390. function roomWithWindows(blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, height, numberOfWindowsFront, numberOfWindowsSide)
  391.     room(blockFloor, blockWalls, blockComplement, xa, ya, za, width, length, height)
  392.     if tonumber(length) < 3 or tonumber(width) < 3 or tonumber(height) < 3 then
  393.         return false
  394.     end
  395.    
  396.     validColumns = getValidWindowsPostions(xa, ya, za, width, length, height, numberOfWindowsFront, numberOfWindowsSide)
  397.     -- L'idée est de parcourir le contour du mur et de dessiner les fenêtres.
  398.    
  399.     function myFuncToPlaceOrnaments(xa,ya,za,column,wallSide)
  400.         if wallSide == "north" then
  401.             if validColumns.front[column] == true then
  402.                 fill(glassBlock, xa, ya, za+1, 1, 1, height-2) --glassify column   
  403.             elseif validColumns.front[column] == false then
  404.                 fill(blockWalls, xa, ya+1, za, 1, 1, height)    --make a pillar
  405.             end
  406.         elseif wallSide == "south" then
  407.             if validColumns.front[column] == true then
  408.                 txt = " glass: "..xa..","..ya..","..tostring(za+1)
  409.                 fill(glassBlock, xa, ya, za+1, 1, 1, height-2) --glassify column
  410.             elseif validColumns.front[column] == false then
  411.                 txt = " pilla: "..xa..","..tostring(ya-1)..","..za
  412.                 fill(blockWalls, xa, ya-1, za, 1, 1, height)    --make a pillar
  413.             end
  414.         elseif wallSide == "east" then
  415.             if validColumns.side[column] == true then
  416.                 fill(glassBlock, xa, ya, za+1, 1, 1, height-2) --glassify column
  417.             elseif validColumns.side[column] == false then
  418.                 fill(blockWalls, xa+1, ya, za, 1, 1, height)    --make a pillar
  419.             end
  420.         elseif wallSide == "west" then
  421.             if validColumns.side[column] == true then
  422.                 fill(glassBlock, xa, ya, za+1, 1, 1, height-2) --glassify column
  423.             elseif validColumns.side[column] == false then
  424.                 fill(blockWalls, xa-1, ya, za, 1, 1, height)    --make a pillar
  425.             end
  426.         end
  427.     end
  428.     forEachColumnOfAWallDoMyFunc(myFuncToPlaceOrnaments, xa, ya, za, width, length)
  429.     return true
  430. end
  431.  
  432. function squareBuilding(numberOfFloors, blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  433.     ----[[
  434.     local width = width-2 -- foreseeing the width/length gain of the pillars around it
  435.     local length = length-2
  436.     local xa = xa+1
  437.     local ya = ya+1
  438.     --]]
  439.  
  440.     zPositionStart = za
  441.     for i=1, numberOfFloors-1 do
  442.         roomWithWindows(blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  443.         za = za+ceilingHeight-1
  444.     end
  445.     lastFloor("random", blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  446. end
  447.  
  448. function spikyBuilding(numberOfFloors, blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  449.     --[[local width = width-2 -- foreseeing the width/length gain of the pillars around it
  450.     local length = length-2
  451.     local xa = xa+1
  452.     local ya = ya+1--]]
  453.    
  454.     if numberOfFloors < 3 then
  455.         return false
  456.     end
  457.     zPositionStart = za
  458.     shrink = math.random(2,numberOfFloors-1)
  459.     shrinkage = 0
  460.     for i=0, numberOfFloors do
  461.         if i%shrink == 0 then
  462.             if length < 5 or width < 5 then
  463.                 -- can't shrink anymore
  464.                 return true
  465.             else
  466.                 shrinkage = shrinkage+1
  467. --              width = width-2
  468. --              length = length-2
  469. --              xa = xa+1
  470. --              ya = ya+1
  471.             end
  472.         end
  473.         roomWithWindows(blockFloor, blockWalls, blockComplement, glassBlock, xa+shrinkage, ya+shrinkage, za, width-2*shrinkage, length-2*shrinkage, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  474.         za = za+ceilingHeight-1
  475.     end
  476.     return true
  477. end
  478.  
  479. function lastFloor(typeOfLastFloor, blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  480.     if typeOfLastFloor == "random" or typeOfLastFloor == nil then
  481.         typeOfLastFloor = table.pickRandom(typesOfLastFloors)
  482.     end
  483.    
  484.     if typeOfLastFloor == "terrace" then
  485.         roomWithWindows(blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  486.         za = za+ceilingHeight
  487.         fillInside(air, "minecraft:fence", xa, ya, za, width, length, 1)
  488.     elseif typeOfLastFloor == "normal" then
  489.         roomWithWindows(blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight, numberOfWindowsFront, numberOfWindowsSide)
  490.     elseif typeOfLastFloor == "penthouse" then
  491.         roomWithWindows(blockFloor, blockWalls, blockComplement, glassBlock, xa, ya, za, width, length, ceilingHeight*2+1, numberOfWindowsFront, numberOfWindowsSide)
  492.         fill(blockFloor, xa+1, ya+1, za+ceilingHeight, math.round(0.75*width), math.round(0.5*length), 1)
  493.         fill("minecraft:fence", xa+1, ya+math.round(0.5*length), za+ceilingHeight+1, math.round(0.75*width), 1, 1)
  494.         fill("minecraft:fence", xa+math.round(0.75*width), ya+1, za+ceilingHeight+1, 1, math.round(0.5*length), 1)
  495.     end
  496. end
  497.  
  498. function randomBuilding(buildingColor, xa, ya, za, numberOfFloors, length, width)
  499.     glassBlock = table.pickRandom(glassBlocks)
  500.     floorBlock = table.pickRandom(floorBlocks[buildingColor])
  501.     wallBlock = table.pickRandom(wallBlocks[buildingColor])
  502.     complementBlock = table.pickRandom(complementBlocks[buildingColor])
  503.     buildingType = table.pickRandom(buildingTypes)
  504.    
  505.     if numberOfFloors == nil then
  506.         numberOfFloors = math.random(2,6)
  507.     end
  508.     if width == nil then
  509.         width = math.random(8,20)
  510.     end
  511.     if length == nil then
  512.         length = math.random(8,20)
  513.     end
  514.     console("Building a " .. width .. " by " .. length .. " " .. buildingColor .. " " .. buildingType .. " of "..numberOfFloors.." floors.", 1)
  515.    
  516.     if buildingType == "squareBuilding" then
  517.         success = squareBuilding(numberOfFloors, floorBlock, wallBlock, complementBlock, glassBlock, xa,ya,za, width, length,5)
  518.     elseif buildingType == "spikyBuilding" then
  519.         success = spikyBuilding(numberOfFloors, floorBlock, wallBlock, complementBlock, glassBlock, xa,ya,za, width, length,5)
  520.     end
  521.    
  522.     console("Built a " .. width .. " by " .. length .. " " .. buildingColor .. " " .. buildingType .. " of "..numberOfFloors.." floors.", 1)
  523.     return success
  524. end
  525.  
  526. function getBlockInfo(xa,ya,za)
  527.     xa = xa+cPos.x
  528.     ya = ya+cPos.y
  529.     za = za+cPos.z
  530.     return commands.getBlockInfo(xa,za,ya)
  531. end
  532.  
  533. function road(xa,ya,za,direction,length)
  534.     slab = "chisel:marble_slab"
  535.     asphalt = "chisel:antiBlock"
  536.     paint = "chisel:marble"
  537.     if direction == "north" then -- north: y+
  538.         for i=ya,ya+length-1 do
  539.             setBlock(slab, xa+6, i, za)
  540.             setBlock(slab, xa+5, i, za)
  541.             setBlock(slab, xa+4, i, za)
  542.             setBlock(asphalt, xa+3, i, za-1)
  543.             setBlock(asphalt, xa+2, i, za-1)
  544.             setBlock(asphalt, xa+1, i, za-1)
  545.             if (i%7) > 3 then -- 0,0,0,0,1,1,1,0,0,0,0,1,1,1
  546.                 setBlock(paint,xa,i,za-1)
  547.             else
  548.                 setBlock(asphalt, xa, i, za-1)
  549.             end
  550.             setBlock(asphalt, xa-1, i, za-1)
  551.             setBlock(asphalt, xa-2, i, za-1)
  552.             setBlock(asphalt, xa-3, i, za-1)
  553.             setBlock(slab, xa-4, i, za)
  554.             setBlock(slab, xa-5, i, za)
  555.             setBlock(slab, xa-6, i, za)
  556.         end
  557.     elseif direction == "south" then -- south: y-
  558.         for i=ya,ya-length+1,-1 do
  559.             setBlock(slab, xa+6, i, za)
  560.             setBlock(slab, xa+5, i, za)
  561.             setBlock(slab, xa+4, i, za)
  562.             setBlock(asphalt, xa+3, i, za-1)
  563.             setBlock(asphalt, xa+2, i, za-1)
  564.             setBlock(asphalt, xa+1, i, za-1)
  565.             if (i%7) > 3 then -- 0,0,0,0,1,1,1,0,0,0,0,1,1,1
  566.                 setBlock(paint,xa,i,za-1)
  567.             else
  568.                 setBlock(asphalt, xa, i, za-1)
  569.             end
  570.             setBlock(asphalt, xa-1, i, za-1)
  571.             setBlock(asphalt, xa-2, i, za-1)
  572.             setBlock(asphalt, xa-3, i, za-1)
  573.             setBlock(slab, xa-4, i, za)
  574.             setBlock(slab, xa-5, i, za)
  575.             setBlock(slab, xa-6, i, za)
  576.         end
  577.     elseif direction == "east" then -- east: x+
  578.         for i=xa,xa+length-1 do
  579.             setBlock(slab, i, ya+6, za)
  580.             setBlock(slab, i, ya+5, za)
  581.             setBlock(slab, i, ya+4, za)
  582.             setBlock(asphalt, i, ya+3, za-1)
  583.             setBlock(asphalt, i, ya+2, za-1)
  584.             setBlock(asphalt, i, ya+1, za-1)
  585.             if (i%7) > 3 then -- 0,0,0,0,1,1,1,0,0,0,0,1,1,1
  586.                 setBlock(paint,i,ya,za-1)
  587.             else
  588.                 setBlock(asphalt, i, ya, za-1)
  589.             end
  590.             setBlock(asphalt, i, ya-1, za-1)
  591.             setBlock(asphalt, i, ya-2, za-1)
  592.             setBlock(asphalt, i, ya-3, za-1)
  593.             setBlock(slab, i, ya-4, za)
  594.             setBlock(slab, i, ya-5, za)
  595.             setBlock(slab, i, ya-6, za)
  596.         end
  597.     elseif direction == "west" then -- west: x-
  598.         for i=xa,xa-length+1,-1 do
  599.             setBlock(slab, i, ya+6, za)
  600.             setBlock(slab, i, ya+5, za)
  601.             setBlock(slab, i, ya+4, za)
  602.             setBlock(asphalt, i, ya+3, za-1)
  603.             setBlock(asphalt, i, ya+2, za-1)
  604.             setBlock(asphalt, i, ya+1, za-1)
  605.             if (i%7) > 3 then -- 0,0,0,0,1,1,1,0,0,0,0,1,1,1
  606.                 setBlock(paint,i,ya,za-1)
  607.             else
  608.                 setBlock(asphalt, i, ya, za-1)
  609.             end
  610.             setBlock(asphalt, i, ya-1, za-1)
  611.             setBlock(asphalt, i, ya-2, za-1)
  612.             setBlock(asphalt, i, ya-3, za-1)
  613.             setBlock(slab, i, ya-4, za)
  614.             setBlock(slab, i, ya-5, za)
  615.             setBlock(slab, i, ya-6, za)
  616.         end
  617.     end
  618. end
  619.  
  620. function roadIntersection(xa,ya,za, openDirections)
  621.     slab = "chisel:marble_slab"
  622.     asphalt = "chisel:antiBlock"
  623.     paint = "chisel:marble"
  624.     fill(slab, xa-6, ya-6, za, 13, 13, 1) -- trottoirs
  625.     -- fill(air, xa-6, ya-3, za, 13, 7, 1) --plus de trottoirs axe est ouest
  626.     -- fill(air, xa-3, ya-6, za, 7, 13, 1) --plus de trottoirs axe nord sud
  627.     fill(air, xa-3, ya-3, za, 7, 7, 1) -- carré de vide au milieu
  628.     fill(asphalt, xa-6, ya-6, za-1, 13, 13, 1) -- gros asphalt sous le sol
  629.    
  630.     fill(paint, xa-6, ya-2, za-1, 13, 1, 1) -- passages piétons
  631.     fill(paint, xa-6, ya, za-1, 13, 1, 1)
  632.     fill(paint, xa-6, ya+2, za-1, 13, 1, 1)
  633.    
  634.     fill(paint, xa-2, ya-6, za-1, 1, 13, 1)
  635.     fill(paint, xa, ya-6, za-1, 1, 13, 1)
  636.     fill(paint, xa+2, ya-6, za-1, 1, 13, 1)
  637.    
  638.     fill(asphalt, xa-3, ya-3, za-1, 7, 7, 1) -- carré d'asphalt au milieu
  639.    
  640.     if type(openDirections) == "string" then
  641.         for i in string.gmatch(openDirections, "%a+") do
  642.             if i=="north" then
  643.                 fill(air, xa-3, ya+4, za, 7, 3, 1)
  644.             elseif i=="south" then
  645.                 fill(air, xa-3, ya-6, za, 7, 3, 1)
  646.             elseif i=="east" then
  647.                 fill(air, xa+4, ya-3, za, 3, 7, 1)
  648.             elseif i=="west" then
  649.                 fill(air, xa-6, ya-3, za, 3, 7, 1)
  650.             end
  651.         end
  652.     end
  653. end
  654.  
  655. function clearTerrain(size, height)
  656.     if height == nil then height = 2 end
  657.     fill(air, (-0.5)*size,(-0.5)*size,-2,size,size,2)
  658.     if height > 2 then
  659.         fill(air, (-0.5)*size,(-0.5)*size,1,size,size,height-3)
  660.     end
  661.     fill("minecraft:sandstone", (-0.5)*size,(-0.5)*size,-2,size,size,1)
  662. end
  663.  
  664. function oppositeDirectionOf(direction)
  665.     if direction == "north" then
  666.         return "south"
  667.     elseif direction == "south" then
  668.         return "north"
  669.     elseif direction == "east" then
  670.         return "west"
  671.     elseif direction == "west" then
  672.         return "east"
  673.     else
  674.         return ""
  675.     end
  676. end
  677.  
  678. function roadPathExample(iterations)
  679.     clearTerrain(80, 10)
  680.     theFourDirections = {"north", "south", "east", "west"}
  681.     roads = {}
  682.     for i=1,iterations do
  683.         if i>1 then
  684.             lastRoad = roads[i-1]
  685.         else
  686.             lastRoad = {}
  687.             lastRoad.direction = "north"
  688.             lastRoad.x = 0
  689.             lastRoad.y = 0
  690.             lastRoad.z = -1
  691.         end
  692.         thisRoad = {}
  693.         thisRoad.direction = pickRandom(theFourDirections)
  694.         while thisRoad.direction == oppositeDirectionOf(lastRoad.direction) do
  695.             thisRoad.direction = pickRandom(theFourDirections)
  696.         end
  697.         thisRoad.length = math.random(18,30)
  698.         road(lastRoad.x,lastRoad.y,lastRoad.z,thisRoad.direction,thisRoad.length)
  699.         thisRoad.x = lastRoad.x
  700.         thisRoad.y = lastRoad.y
  701.         thisRoad.z = lastRoad.z
  702.         if thisRoad.direction == "north" then        -- north: y+
  703.             thisRoad.y = lastRoad.y + thisRoad.length - 7
  704.         elseif thisRoad.direction == "south" then    -- south: y-
  705.             thisRoad.y = lastRoad.y - thisRoad.length + 7
  706.         elseif thisRoad.direction == "east" then     -- east: x+
  707.             thisRoad.x = lastRoad.x + thisRoad.length - 7
  708.         elseif thisRoad.direction == "west" then     -- west: x-
  709.             thisRoad.x = lastRoad.x - thisRoad.length + 7
  710.         end
  711.         roadIntersection(lastRoad.x, lastRoad.y, lastRoad.z, oppositeDirectionOf(lastRoad.direction)..","..thisRoad.direction)
  712.         -- print(table.concat(thisRoad, ","))
  713.         fill("minecraft:stone", thisRoad.x, thisRoad.y, thisRoad.z,0,0,0)
  714.         roads[i] = thisRoad
  715.         sleep(2)
  716.     end
  717. end
  718.  
  719. function neighborhood(buildingColor, xa,ya,za,size)
  720.     clearTerrain(size, 10)
  721.     theFourDirections = {"north", "south", "east", "west"}
  722.     southMost = math.round(xa-(size/2))
  723.     northMost = math.round(xa+(size/2))
  724.     westMost = math.round(ya-(size/2))
  725.     eastMost = math.round(ya+(size/2))
  726.     buildings = {}
  727.     numberOfBuildings = math.ceil(size/25) -- assuming average building size is 25x25
  728. end
  729.  
  730. function generateCityPlan(size)
  731.     if(size < 120) then
  732.         return
  733.     end
  734.     size = math.floor(size/12)
  735.     plan.size = size
  736.     x = 0
  737.     y = 0
  738.     done = false
  739.     while not done do
  740.         --generates house blocks until we fill the plan
  741.         width = math.random(2,6)
  742.         length = math.random(2,6)
  743.         value = table.pickRandom(buildingColors)
  744.         for i=x,x+width do
  745.             for j=y,y+length do
  746.                 plan:set(i,j,value)
  747.                 if j == y+length then
  748.                     plan:set(i,j+1,"road")
  749.                 end
  750.                 if i == x+width then
  751.                     plan:set(i+1,j,"road")
  752.                 end
  753.             end
  754.         end
  755.         x = x+width+2
  756.         if size-x < 6 then
  757.             x = 0
  758.         end
  759.         y = size
  760.         while plan:at(x,y) == nil do
  761.             y = y-1
  762.             if y == -1 then
  763.                 break
  764.             end
  765.         end
  766.         y = y+1
  767.         if size - y < 6 then
  768.             done = true
  769.         end
  770.     end
  771. end
  772.  
  773. function debugPlanBuild()
  774.     if not plan.size then
  775.         return
  776.     end
  777.     fill(air,1,1,0,plan.size,plan.size,1)
  778.     for i=0,plan.size do
  779.         for j=0,plan.size do
  780.             if plan:at(i,j) == "road" then
  781.                 setBlock("minecraft:obsidian",i+1,j+1,0)
  782.             elseif plan:at(i,j) ~= nil then
  783.                 setBlock("minecraft:stone",i+1,j+1,0)
  784.             end
  785.         end
  786.     end
  787. end
  788.  
  789. -- END OF THE API PART
  790. -- FROM THERE, IT'S JUST SOME RANDOM CODE TO TEST THE API. I HAVE INCLUDED IT JUST SO A COUPLE OF PEOPLE CAN MAYBE ENJOY IT.
  791.  
  792. if activateTheExamples then
  793.     commands.execAsync("gamerule commandBlockOutput false")
  794.     term.clear()
  795.     term.setCursorPos(1,1)
  796.     print("What test shall I perform ? Pick a number")
  797.     input = tonumber(io.read())
  798.  
  799.     if input==1 then
  800.         floor("minecraft:wool:5", "minecraft:wool:14", 0,0,-1,22,22)
  801.         while true do
  802.             buildingColor = table.pickRandom(buildingColors)
  803.             fill(air,1,1,0,40,20,40)
  804.             term.clear()
  805.             while randomBuilding(buildingColor, 1,1,0,math.random(4,7)) == false do
  806.                
  807.             end
  808.             sleep(4)
  809.         end
  810.     elseif input == 2 then
  811.         while true do
  812.             sleep(2)
  813.             clearTerrain(12)
  814.             theFourDirections = {"north", "south", "east", "west"}
  815.             direction = {}
  816.             numberOfWays = math.random(2,3)
  817.             for i=1, numberOfWays do
  818.                 direction[i] = pickRandom(theFourDirections)
  819.             end
  820.             print(table.concat(direction, ","))
  821.             for i=1,#direction do
  822.                 road(0,0,-1,direction[i],15)
  823.             end
  824.             roadIntersection(0,0,-1, table.concat(direction, ","))
  825.         end
  826.     elseif input == 3 then
  827.         roadPathExample(10)
  828.     elseif input == 4 then
  829.         while true do
  830.             fill(air, 1,1,0,100,100,3)
  831.             generateCityPlan(500)
  832.             debugPlanBuild()
  833.             sleep(4)
  834.         end
  835.     elseif input == 5 then
  836.         -- suite ici
  837.     else
  838.         print("Nah, I didn't get it")
  839.     end
  840.  
  841.  
  842. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement