Advertisement
hoblin

Temple builder

Dec 2nd, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.58 KB | None | 0 0
  1. local leash = 128 -- how far can it go from home position
  2. --local spawn={x=-166,y=71,z=-260,facing="north"}-- real coordinates
  3. --local rfstation={x=-192,y=73,z=-255}-- real coordinates
  4.  
  5. -- Materials: These are lists of slot numbers to draw from
  6. local marble = {1,2,3,4}
  7. local marblebrick = {9,8,7,6}
  8. local light = {5}
  9.  
  10. -- Which material to use where
  11. local base = marble
  12. local wall = marble
  13. local column = marblebrick
  14. local roof = marble
  15. local roofdeco = marblebrick
  16. local rooffilling = nil -- It's the only one that can be nil
  17. local columnbacklight = light
  18. local innercolumn = marblebrick
  19. local innercolumntoptip = marblebrick
  20. local innercolumnlight = light
  21.  
  22. local ImplicitDigging = false -- Dig unexpected blocks standing in the way
  23. local ErrorOnMoveFailed = true -- Error out or merely return false
  24.  
  25. local echo = print -- because I'm more used to the name echo
  26. local chirality = 1 -- 1 for dextral or -1 for sinistral
  27. local x,y,z,dx,dz=0,0,0,0,1 -- for the odometer
  28.  
  29. -- By convention :
  30. --    ^ z
  31. --    |
  32. --    |
  33. --    |_____> x
  34. --
  35. -- I think I picked the wrong one :(
  36.  
  37. local stations={}
  38. if rfstation and spawn then
  39.     -- Convert real coordinates into relative coordinates
  40.     -- Also in my relative coordinate x and z are somewhat wrongly signed
  41.     if spawn.facing=="north" then
  42.         stations={refilling={x=rfstation.x-spawn.x,y=rfstation.y-spawn.y,z=spawn.z-rfstation.z}}
  43.     elseif spawn.facing=="east" then
  44.         stations={refilling={x=rfstation.z-spawn.z,y=rfstation.y-spawn.y,z=rfstation.x-spawn.x}}
  45.     elseif spawn.facing=="south" then
  46.         stations={refilling={x=spawn.x-rfstation.x,y=rfstation.y-spawn.y,z=rfstation.z-spawn.z}}
  47.     elseif spawn.facing=="west" then
  48.         stations={refilling={x=spawn.z-rfstation.z,y=rfstation.y-spawn.y,z=spawn.x-rfstation.x}}
  49.     else
  50.         error("Unknown facing : "..tostring(spawn.facing))
  51.     end
  52. end
  53.  
  54. local function CheckLeash()
  55.     if math.abs(x)>leash or math.abs(y)>leash or math.abs(z)>leash then
  56.         echo("Danger! Outbound turtle! ("..x..","..y..","..z..")>"..leash)
  57.         echo("Emergency crashdown!")
  58.         local tr = (math.random()<0.5) and turtle.turnRight or turtle.turnLeft
  59.         for k=1,20 do
  60.             if not turtle.up then
  61.                 break
  62.             end
  63.         end
  64.         for k=1,32 do
  65.             tr()
  66.         end
  67.         while turtle.down() do
  68.             tr()
  69.         end
  70.         for k=1,128 do
  71.             tr()
  72.         end
  73.         error("Crash landed")
  74.     end
  75. end
  76.  
  77. local function TurnRight()
  78.     CheckLeash()
  79.     turtle.turnRight()
  80.     if dz==1 then
  81.         dx,dz=1,0
  82.     elseif dz==-1 then
  83.         dx,dz=-1,0
  84.     elseif dx==1 then
  85.         dx,dz=0,-1
  86.     elseif dx==-1 then
  87.         dx,dz=0,1
  88.     else
  89.         echo("Turtle lost track of its direction!")
  90.         for k=1,24 do
  91.             turtle.turnRight()
  92.         end
  93.     end
  94.     return true
  95. end
  96.  
  97. local function TurnLeft()
  98.     CheckLeash()
  99.     turtle.turnLeft()
  100.     if dz==1 then
  101.         dx,dz=-1,0
  102.     elseif dz==-1 then
  103.         dx,dz=1,0
  104.     elseif dx==1 then
  105.         dx,dz=0,1
  106.     elseif dx==-1 then
  107.         dx,dz=0,-1
  108.     else
  109.         echo("Turtle lost track of its direction!")
  110.         for k=1,24 do
  111.             turtle.turnLeft()
  112.         end
  113.     end
  114.     return true
  115. end
  116.  
  117. local function Turn(quarters)
  118.     CheckLeash()
  119.     local q = (quarters or 1)*(chirality or 1)
  120.     local r=true
  121.     if q>0 then
  122.         for t=1,math.abs(q)%4 do
  123.             r=r and TurnRight()
  124.         end
  125.     elseif q<0 then
  126.         for t=1,math.abs(q)%4 do
  127.             r=r and TurnLeft()
  128.         end
  129.     end
  130.     return r
  131. end
  132.  
  133. local function Forward()
  134.     CheckLeash()
  135.     if turtle.forward() then
  136.         x=x+dx
  137.         z=z+dz
  138.         return true
  139.     end
  140.     return false
  141. end
  142.  
  143. local function Backward()
  144.     CheckLeash()
  145.     if turtle.back() then
  146.         x=x-dx
  147.         z=z-dz
  148.         return true
  149.     end
  150.     return false
  151. end
  152.  
  153. local function Upward()
  154.     CheckLeash()
  155.     if turtle.up() then
  156.         y=y+1
  157.         return true
  158.     end
  159.     return false
  160. end
  161.  
  162. local function Downward()
  163.     CheckLeash()
  164.     if turtle.down() then
  165.         y=y-1
  166.         return true
  167.     end
  168.     return false
  169. end
  170.  
  171. local function GetOrientation()
  172.     if dz==1 then
  173.         return 0
  174.     elseif dx==1 then
  175.         return 1
  176.     elseif dz==-1 then
  177.         return 2
  178.     elseif dx==-1 then
  179.         return 3
  180.     else
  181.         error("Failed to get Orientation")
  182.     end
  183. end
  184.  
  185. local function GetPosition()
  186.     return x,y,z,GetOrientation()
  187. end
  188.  
  189. local function CheckPos(line,ex,ey,ez,eo) -- prefixed by 'e' like expected
  190.     if (not (ex==x)) or (not (ey==y)) or (not (ez==z)) then
  191.         error("L"..line..": Wrong position! Turtle is at "..x..","..y..","..z
  192.             .." instead of "..ex..","..ey..","..ez)
  193.     end
  194.     if eo then
  195.         if not (eo==GetOrientation()) then
  196.             error("L"..line..": orientation! Turtle is at "..GetOrientation().." instead of "..eo)
  197.         end
  198.     end
  199. end
  200.  
  201. local function MakeStubborn(move,antimove,dig)
  202.     return function(...)
  203.         if ImplicitDigging then
  204.             while dig() do
  205.                 if move() then
  206.                     return true
  207.                 end
  208.             end
  209.         end
  210.         local tries = 0
  211.         local linear = 0
  212.         if move() then
  213.             linear=linear+1
  214.         else
  215.             echo("Having trouble at ("..x..","..y..","..z..")")
  216.         end
  217.         while not (linear==1) do
  218.             tries=tries+1
  219.             if tries>16 then
  220.             -- What do do when movement fails
  221.             if ErrorOnMoveFailed then
  222.                 error("Failed to move.")
  223.             end
  224.                 return false
  225.             end
  226.             local manner=1+(tries-1)%(ImplicitDigging and 4 or 3)
  227.             if manner==1 then
  228.                 -- Glance sideway
  229.                 local sign=math.random()>0.5 and 1 or -1
  230.                 Turn(0+sign)
  231.                 Turn(0-sign)
  232.                 Turn(0-sign)
  233.                 Turn(0+sign)
  234.             elseif manner==2 then
  235.                 -- Attempt to ram
  236.                 local b=math.min(4,math.floor(tries/4))
  237.                 for k=1,b do
  238.                     linear=linear-(antimove() and 1 or 0)
  239.                 end
  240.                 for k=1,b do
  241.                     linear=linear+(move() and 1 or 0)
  242.                 end
  243.             elseif manner==3 then
  244.                 -- Attempt to move
  245.                 if move() then
  246.                     linear=linear+1
  247.                 end
  248.             elseif manner==4 then
  249.                 -- Attempt to dig
  250.                 if ImplicitDigging then
  251.                     if dig() then
  252.                         tries=0
  253.                     end
  254.                 else
  255.                     error("We said no digging!")
  256.                 end
  257.             else
  258.                 error("Bad manner!")
  259.             end
  260.         end
  261.         return true
  262.     end
  263. end
  264.  
  265. Forward=MakeStubborn(Forward,Backward,turtle.dig)
  266. Backward=MakeStubborn(Backward,Forward,DigBack)
  267. Upward=MakeStubborn(Upward,Downward,turtle.digUp)
  268. Downard=MakeStubborn(Downward,Upward,turtle.digDown)
  269.  
  270. local function Move(wx,wy,wz,wo)
  271.     local co=0
  272.     local reverse=false
  273.     local function LocalTurnTo(wo)
  274.         reverse=false
  275.         if not ((co%4)==(wo%4)) then
  276.             local o=(12+wo-co)%4
  277.             if o==1 then
  278.                 TurnRight()
  279.                 co=co+1
  280.             elseif o==3 then
  281.                 TurnLeft()
  282.                 co=co-1
  283.             elseif o==2 then
  284.                 if wx==0 and wy==0 then
  285.                     -- If wx not 0 it will have to turn anyway.
  286.                     -- If wy not 0 it might want to sense block to go above/under
  287.                     -- and turtle can't sense from its back.
  288.                     reverse=true
  289.                 else
  290.                     if math.random()>0.5 then
  291.                         TurnRight()
  292.                         TurnRight()
  293.                     else
  294.                         TurnLeft()
  295.                         TurnLeft()
  296.                     end
  297.                     co=co+2
  298.                 end
  299.             end
  300.             co=co%4
  301.         end
  302.     end
  303.     while not (wx==0 and wz==0) do
  304.         if math.abs(wx)>math.abs(wz) then
  305.             LocalTurnTo(wx>0 and 1 or 3)
  306.         else
  307.             LocalTurnTo(wz>0 and 0 or 2)
  308.         end
  309.         if not reverse and not (wy==0) and turtle.detect() then
  310.             if wy>0 then
  311.                 if Upward() then
  312.                     wy=wy-1
  313.                 else
  314.                     return false
  315.                 end
  316.             end
  317.             if wy<0 then
  318.                 if Downward() then
  319.                     wy=wy+1
  320.                 else
  321.                     return false
  322.                 end
  323.             end
  324.         elseif (reverse and Backward or Forward)() then
  325.             if math.abs(wx)>math.abs(wz) then
  326.                 wx=wx+(wx>0 and -1 or 1)
  327.             else
  328.                 wz=wz+(wz>0 and -1 or 1)
  329.             end
  330.         else
  331.             return false
  332.         end
  333.     end
  334.     while not (wy==0) do
  335.         if wy>0 then
  336.             if Upward() then
  337.                 wy=wy-1
  338.             else
  339.                 return false
  340.             end
  341.         end
  342.         if wy<0 then
  343.             if Downward() then
  344.                 wy=wy+1
  345.             else
  346.                 return false
  347.             end
  348.         end
  349.     end
  350.     if wo then
  351.         --echo("Move trying to get proper orientation")
  352.         wx,wy,wz=3,5,7 -- Hack to prevent reverse
  353.         LocalTurnTo(wo)
  354.         --echo("ok")
  355.     end
  356.     return true
  357. end
  358.  
  359. local function Goto(awx,awy,awz,awo)
  360.     -- aw(x/y/z/o) : Absolute Wanted
  361.     -- rw(x/y/z/o) : Relative Wanted
  362.     local r = true
  363.     local o = GetOrientation()
  364.     local rwx = awx and awx-x or 0
  365.     local rwy = awy and awy-y or 0
  366.     local rwz = awz and awz-z or 0
  367.     local rwo = awo and (16+awo-o)%4
  368.     if o==0 then
  369.         r = Move(rwx,rwy,rwz,rwo)
  370.     elseif o==3 then
  371.         r = Move(rwz,rwy,-rwx,rwo)
  372.     elseif o==2 then
  373.         r = Move(-rwx,rwy,-rwz,rwo)
  374.     elseif o==1 then
  375.         r = Move(-rwz,rwy,rwx,rwo)
  376.     else
  377.         error("Bad orientation!")
  378.     end
  379.     if awo and not (awo==GetOrientation()) then
  380.         --echo("Goto trying to get proper orientation")
  381.         while not (awo==GetOrientation()) do
  382.             Turn()
  383.         end
  384.         --echo("ok")
  385.     end
  386.     return r
  387. end
  388.  
  389. local GetIn = nil
  390. local GetOut = nil
  391.  
  392. local function Refill(slot)
  393.     if type(slot)=="table" then
  394.         echo("Slots "..table.concat(slot,",").." all empty")
  395.     else
  396.         echo("Slot "..slot.." empty")
  397.     end
  398.     if (not stations) or not (stations.refilling) then
  399.         echo("No refilling station!")
  400.         echo("Please refill manually then press enter")
  401.         read()
  402.     else
  403.         local s=stations.refilling
  404.         local bx,by,bz,bo=GetPosition()
  405.         local sides={"marble","marble","marble","marble","torches","marble bricks","marble bricks","marble bricks","marble bricks"}
  406.         local sid=ImplicitDigging
  407.         ImplicitDigging=false
  408.         echo("Going to station")
  409.         if GetOut then
  410.             GetOut()
  411.         else
  412.             Goto(bx,by+16,bz)
  413.         end
  414.         Goto(s.x,by+16,s.z)
  415.         Goto(s.x,s.y,s.z,s.o)
  416.         os.sleep(2)
  417.         rednet.open("right")
  418.         local snglslt=(type(slot)=="table" and math.max(unpack(slot)) or slot)
  419.         repeat
  420.             echo("Asking to be fed")
  421.             rednet.broadcast("Feed me "..sides[snglslt])
  422.             os.sleep(12)
  423.         until turtle.getItemCount(snglslt)>32
  424.         rednet.broadcast("I'm filled up!")
  425.         os.sleep(1)
  426.         rednet.close("right")
  427.         echo("Back to work")
  428.         os.sleep(12)
  429.         Goto(s.x,by+16,s.z)
  430.         if GetIn then
  431.             GetIn()
  432.         else
  433.             Goto(bx,by+16,bz)
  434.         end
  435.         Goto(bx,by,bz,bo)
  436.         ImplicitDigging=sid
  437.     end
  438. end
  439.  
  440. local function SelectSlot(slot)
  441.     if slot then
  442.         if type(slot)=="table" then
  443.             for _,s in ipairs(slot) do
  444.                 if turtle.getItemCount(s)>1 then-- Always leave one for reasy refilling
  445.                     turtle.select(s)
  446.                     return true
  447.                 end
  448.             end
  449.             Refill(slot)
  450.             return SelectSlot(slot)
  451.         elseif type(slot)=="number" then
  452.             if turtle.getItemCount(slot)>1 then-- Always leave one for reasy refilling
  453.                 turtle.select(slot)
  454.                 return true
  455.             else
  456.                 Refill(slot)
  457.                 return SelectSlot(slot)
  458.             end
  459.         else
  460.             error("Unexpected slot type "..type(slot))
  461.         end
  462.     else
  463.         turtle.select(1)
  464.         return true
  465.     end
  466. end
  467.  
  468. local function PlaceFront(slot)
  469.     SelectSlot(slot)
  470.     local r=turtle.place()
  471.     turtle.select(1)
  472.     return r
  473. end
  474.  
  475. local function PlaceBack(slot)
  476.     Turn()
  477.     Turn()
  478.     SelectSlot(slot)
  479.     local r=turtle.place()
  480.     Turn()
  481.     Turn()
  482.     turtle.select(1)
  483.     return r
  484. end
  485.  
  486. local function PlaceDown(slot)
  487.     SelectSlot(slot)
  488.     local r=turtle.placeDown()
  489.     turtle.select(1)
  490.     return r
  491. end
  492.  
  493. local function PlaceUp(slot)
  494.     SelectSlot(slot)
  495.     local r=turtle.placeDown()
  496.     turtle.select(1)
  497.     return r
  498. end
  499.  
  500. local DigDown = turtle.digDown
  501. local DigFront = turtle.dig
  502. local DigUp = turtle.digUp
  503.  
  504. local function DigBack()
  505.     Turn()
  506.     Turn()
  507.     local r=turtle.dig()
  508.     Turn()
  509.     Turn()
  510.     return r
  511. end
  512.  
  513. local function PutTorchesAfterWard(NbrColumnLength,NbrColumnWidth,TorchHeight,yOffset)
  514.     local colz=NbrColumnLength-1
  515.     local colx=NbrColumnWidth-1
  516.     local coly=yOffset+TorchHeight-1
  517.  
  518.     local corners={{1,-1,3},{1,1,0},{-1,1,1},{-1,-1,2}}
  519.     Goto(0,yOffset,0)
  520.     Goto(0,coly,0)
  521.  
  522.     for _,p in ipairs(corners) do
  523.         Goto((colx-3)*p[2],coly,(colz-3)*p[1],p[3])
  524.         DigFront()
  525.         TurnRight()
  526.         DigFront()
  527.     end
  528.  
  529.     -- South
  530.     for x=2-colx,colx-2,2 do
  531.         Goto(x,coly,3-colz,2)
  532.         DigFront()
  533.         Goto(x,coly,2-colz,2)
  534.         PlaceFront(columnbacklight)
  535.         Goto(x,coly,3-colz,2)
  536.         PlaceFront(wall)
  537.     end
  538.     Goto(colx-3,coly,3-colz)
  539.  
  540.     -- East
  541.     for z=2-colz,colz-2,2 do
  542.         Goto(colx-3,coly,z,1)
  543.         DigFront()
  544.         Goto(colx-2,coly,z,1)
  545.         PlaceFront(columnbacklight)
  546.         Goto(colx-3,coly,z,1)
  547.         PlaceFront(wall)
  548.     end
  549.     Goto(colx-3,coly,colz-3)
  550.  
  551.     -- North
  552.     for x=colx-2,2-colx,-2 do
  553.         Goto(x,coly,colz-3,0)
  554.         DigFront()
  555.         Goto(x,coly,colz-2,0)
  556.         PlaceFront(columnbacklight)
  557.         Goto(x,coly,colz-3,0)
  558.         PlaceFront(wall)
  559.     end
  560.     Goto(3-colx,coly,colz-3)
  561.  
  562.     -- West
  563.     for z=colz-2,2-colz,-2 do
  564.         Goto(3-colx,coly,z,3)
  565.         DigFront()
  566.         Goto(2-colx,coly,z,3)
  567.         PlaceFront(columnbacklight)
  568.         Goto(3-colx,coly,z,3)
  569.         PlaceFront(wall)
  570.     end
  571.     Goto(3-colx,coly,colz-3)
  572.  
  573.     for _,p in ipairs(corners) do
  574.         Goto((colx-3)*p[2],coly,(colz-3)*p[1],p[3])
  575.         PlaceFront(wall)
  576.         TurnRight()
  577.         PlaceFront(wall)
  578.     end
  579.  
  580.     Goto(0,coly,0)
  581.     Goto(0,yOffset,0,0)
  582. end
  583.  
  584.  
  585.  
  586. local function Temple(length,width,height)
  587.  
  588.     local x,y,z = 0,0,0
  589.  
  590.     -- Add here something that override PlaceFront and PlaceDown
  591.     -- With version that first check if not occupied by something else
  592.  
  593.     -- Coordinates are -sx to sx, 0 to sy, -sz to sz
  594.     local sx=math.floor(width/2-0.3)
  595.     local sy=height
  596.     local sz=math.floor(length/2-0.3)
  597.     local lightlevel=math.max(1,height<=3 and height or height-1)
  598.  
  599.     -- Fly up, then go down at first corner
  600.     Goto(0,sy,0)
  601.     Goto(-sx,sy,-sz)
  602.     for y=sy,0,-1 do
  603.         DigDown()
  604.         Downward()
  605.     end
  606.  
  607.     -- Dig the foundation around
  608.     CheckPos("460",-sx,-1,-sz)
  609.     Goto(-sx,-1,-sz,0)
  610.     for z=-sz,sz-1 do
  611.         DigFront()
  612.         Forward()
  613.         DigUp()
  614.     end
  615.     TurnRight()
  616.     for x=-sx,sx-1 do
  617.         DigFront()
  618.         Forward()
  619.         DigUp()
  620.     end
  621.     TurnRight()
  622.     for z=sz,1-sz,-1 do
  623.         DigFront()
  624.         Forward()
  625.         DigUp()
  626.     end
  627.     TurnRight()
  628.     for x=sx,1-sx,-1 do
  629.         DigFront()
  630.         Forward()
  631.         DigUp()
  632.     end
  633.     TurnRight()
  634.     CheckPos("486",-sx,-1,-sz)
  635.  
  636.     -- Place perimeter
  637.     Goto(-sx,0,-sz,2)
  638.     CheckPos("490",-sx,0,-sz)
  639.     PlaceDown(base)
  640.     for z=-sz,sz-1 do
  641.         Backward()
  642.         PlaceDown(base)
  643.         if not (z==-sz) then
  644.             PlaceFront(base)
  645.         end
  646.     end
  647.     TurnRight()
  648.     for x=-sx,sx-1 do
  649.         Backward()
  650.         PlaceDown(base)
  651.         PlaceFront(base)
  652.     end
  653.     TurnRight()
  654.     for z=sz,1-sz,-1 do
  655.         Backward()
  656.         PlaceDown(base)
  657.         PlaceFront(base)
  658.     end
  659.     TurnRight()
  660.     for x=sx,1-sx,-1 do
  661.         Backward()
  662.         PlaceDown(base)
  663.         PlaceFront(base)
  664.     end
  665.     TurnRight()
  666.     Upward()
  667.     PlaceDown(base)
  668.     CheckPos("520",-sx,1,-sz)
  669.  
  670.     -- Make the base itself
  671.     for x=1-sx,sx-1 do
  672.         if x%2==(1-sx)%2 then
  673.             for z=1-sz,sz-1 do
  674.                 Goto(x,1,z)
  675.                 DigDown()
  676.                 PlaceDown(base)
  677.             end
  678.         else
  679.             for z=sz-1,1-sz,-1 do
  680.                 Goto(x,1,z)
  681.                 DigDown()
  682.                 PlaceDown(base)
  683.             end
  684.         end
  685.     end
  686.     for k=1,2 do
  687.         Upward()
  688.     end
  689.  
  690.     -- Place columns
  691.     local columns={}
  692.     local function AddCol(x,z)
  693.         local lastcol=columns[#columns]
  694.         if lastcol then
  695.             if not (x==lastcol.x and z==lastcol.z) then
  696.                 columns[1+#columns]={x=x,z=z}
  697.             else
  698.                 echo("Last column same as penultimate")-- Could still happen if only one
  699.             end
  700.         else
  701.             columns[1]={x=x,z=z}
  702.         end
  703.     end
  704.     -- North
  705.     for x=sx-1,3-sx,-2 do
  706.         AddCol(x,sz-1)
  707.     end
  708.     -- West
  709.     for z=sz-1,3-sz,-2 do
  710.         AddCol(1-sx,z)
  711.     end
  712.     -- South
  713.     for x=1-sx,sx-3,2 do
  714.         AddCol(x,1-sz)
  715.     end
  716.     -- East
  717.     for z=1-sz,sz-3,2 do
  718.         AddCol(sx-1,z)
  719.     end
  720.     for _,c in ipairs(columns) do
  721.         Goto(c.x,height+2,c.z)
  722.         Goto(c.x,1,c.z)
  723.         for y=2,height+1 do
  724.             Upward()
  725.             PlaceDown(column)
  726.             TurnLeft()
  727.         end
  728.         Upward()
  729.     end
  730.  
  731.     --: Walls
  732.     --: The minimum size to have walls is:
  733.     --: -----------
  734.     --: -x-x-x-x-x-
  735.     --: -----------   ^z
  736.     --: -x-oo-oo-x-   |
  737.     --: ---o-+-o---   |
  738.     --: -x-oo-oo-x-   ---->-x
  739.     --: -----------
  740.     --: -x-x-x-x-x-   +-is-origin
  741.     --: -----------
  742.     if sx>=5 and sz>=4 then
  743.         local walls={}
  744.         local function AddWall(x,z) walls[1+#walls]={x=x,z=z} end
  745.         -- North
  746.         for x=sx-3,4-sx,-1 do
  747.             AddWall(x,sz-3)
  748.         end
  749.         -- West
  750.         for z=sz-3,4-sz,-1 do
  751.             AddWall(3-sx,z)
  752.         end
  753.         -- South
  754.         for x=3-sx,sx-4,1 do
  755.             AddWall(x,3-sz)
  756.         end
  757.         -- East
  758.         for z=3-sz,sz-4,1 do
  759.             AddWall(sx-3,z)
  760.         end
  761.         for y=2,height+1 do
  762.             for _,w in ipairs(walls) do
  763.                 local isDoor = (math.abs(w.x) <= math.max(0,sx-5)) and y<=math.min(6,math.max(3,height))
  764.                 if isDoor then
  765.                     Goto(false,1+height,false)
  766.                     Goto(w.x,false,w.z)
  767.                 else
  768.                     Goto(w.x,y,w.z)
  769.                     PlaceDown(wall)
  770.                 end
  771.                 -- Light
  772.                 if y==lightlevel then
  773.                     if w.x==sx-3 and (w.z-sz)%2==1 then
  774.                         Goto(false,y,false,1)
  775.                         PlaceFront(columnbacklight)
  776.                     end
  777.                     if w.z==sz-3 and (w.x-sx)%2==1 then
  778.                         Goto(false,y,false,0)
  779.                         PlaceFront(columnbacklight)
  780.                     end
  781.                     if w.x==3-sx and (w.z-sz)%2==1 then
  782.                         Goto(false,y,false,3)
  783.                         PlaceFront(columnbacklight)
  784.                     end
  785.                     if w.z==3-sz and (w.x-sx)%2==1 then
  786.                         Goto(false,y,false,2)
  787.                         PlaceFront(columnbacklight)
  788.                     end
  789.                 end
  790.             end
  791.         end
  792.         if height==1 then
  793.             PutTorchesAfterWard(sz,sx,height,1)
  794.         end
  795.     else
  796.         -- Place lights on column if no walls
  797.         -- North
  798.         TurnLeft()
  799.         TurnLeft()
  800.         TurnLeft()
  801.         TurnLeft()
  802.         -- North
  803.         for x=sx-3,3-sx,-2 do
  804.             Goto(x,lightlevel,sz-3,0)
  805.             PlaceFront(columnbacklight)
  806.         end
  807.         -- West
  808.         for z=sz-3,3-sz,-2 do
  809.             Goto(3-sx,lightlevel,z,3)
  810.             PlaceFront(columnbacklight)
  811.         end
  812.         -- South
  813.         for x=3-sx,sx-3,2 do
  814.             Goto(x,lightlevel,3-sz,2)
  815.             PlaceFront(columnbacklight)
  816.         end
  817.         -- East
  818.         for z=3-sz,sz-3,2 do
  819.             Goto(sx-3,lightlevel,z,1)
  820.             PlaceFront(columnbacklight)
  821.         end
  822.         TurnLeft()
  823.         TurnLeft()
  824.         TurnLeft()
  825.         TurnLeft()
  826.     end
  827.  
  828.     if sx>1 and sz>1 then
  829.         -- Roof, first layer
  830.         local y=2+height
  831.         for x=sx,-sx,-1 do
  832.             if x%2==(1-sx)%2 then
  833.                 for z=sz,-sz,-1 do
  834.                     Goto(x,y,z)
  835.                     PlaceDown(roof)
  836.                 end
  837.             else
  838.                 for z=-sz,sz do
  839.                     Goto(x,y,z)
  840.                     PlaceDown(roof)
  841.                 end
  842.             end
  843.         end
  844.         -- Roof, higher levels
  845.         if rooffilling then
  846.             for rx=sx,0,-1 do
  847.                 y=y+1
  848.                 for x=-rx,rx do
  849.                     for z=-sz,sz do
  850.                         if math.abs(x)>=rx-1 or rx==sx then
  851.                             Goto(x,y,z)
  852.                             PlaceDown(roof)
  853.                         elseif z==sz or z==-sz then
  854.                             Goto(x,y,z)
  855.                             PlaceDown(roofdeco)
  856.                         elseif rooffilling then
  857.                             Goto(x,y,z)
  858.                             PlaceDown(rooffilling)
  859.                         end
  860.                     end
  861.                 end
  862.             end
  863.         else
  864.             for rx=sx-2,0,-2 do
  865.                 y=y+1
  866.                 for z=sz,-sz,-1 do
  867.                     Goto(rx,y,z)
  868.                     PlaceDown(roof)
  869.                 end
  870.                 for x=rx-2,2-rx,-1 do
  871.                     Goto(x,y,-sz)
  872.                     PlaceDown(roofdeco)
  873.                 end
  874.                 if rx>0 then
  875.                     for z=-sz,sz do
  876.                         Goto(-rx,y,z)
  877.                         PlaceDown(roof)
  878.                     end
  879.                 end
  880.                 for x=2-rx,rx-2 do
  881.                     Goto(x,y,sz)
  882.                     PlaceDown(roofdeco)
  883.                 end
  884.                 if rx>0 then
  885.                     for z=sz,-sz,-1 do
  886.                         Goto(rx-1,y,z)
  887.                         PlaceDown(roof)
  888.                     end
  889.                 end
  890.                 if rx>1 then
  891.                     for z=-sz,sz do
  892.                         Goto(1-rx,y,z)
  893.                         PlaceDown(roof)
  894.                     end
  895.                 end
  896.             end
  897.         end
  898.     end
  899.  
  900.     GetIn = function()
  901.         Goto(sx%2,y+1,-2-sz)
  902.         Goto(sx%2,math.max(2,height-1),-2-sz)
  903.         TurnLeft()
  904.         TurnLeft()
  905.         Goto(sx%2,math.max(2,height-1),-1-sz)
  906.         Goto(sx%2,1,-1-sz)
  907.         Goto(sx%2,1,2-sz)
  908.         Goto(0,1,2-sz)
  909.         Goto(0,1,0,0)
  910.         TurnRight()
  911.         TurnRight()
  912.     end
  913.  
  914.     GetOut = function()
  915.         Goto(0,1,0)
  916.         Goto(0,1,2-sz)
  917.         Goto(sx%2,1,2-sz)
  918.         Goto(sx%2,1,-1-sz)
  919.         Goto(sx%2,math.max(2,height-1),-1-sz)
  920.         TurnRight()
  921.         TurnRight()
  922.         Goto(sx%2,math.max(2,height-1),-2-sz)
  923.         Goto(sx%2,math.floor(height+sx/2+3),-2-sz)
  924.     end
  925.  
  926.     -- Go inside
  927.     Goto(false,y+1,false)
  928.     GetIn()
  929.  
  930.     if sx>=6 and sz>=6 and height>=3 then
  931.         Goto(0,height,0)
  932.         -- Place columns inside
  933.         local interior_length=(sz-4)+1
  934.         local ncol = (interior_length-1)/4
  935.         local rcol = interior_length-4*ncol
  936.         for dx=-1,1,2 do
  937.             for dz=6-sz,sz-6,4 do
  938.                 local cz=(dz+(rcol>=2 and (dz>0 and -1 or 1) or 0))*dx
  939.                 local cx=(sx-4)*dx
  940.                 Goto(0,height,cz)
  941.                 Goto(cx-2*dx,height,cz)
  942.                 for cy=1,height do
  943.                     Goto(cx-1*dx,cy,cz)
  944.                     PlaceFront(innercolumn)
  945.                 end
  946.                 Backward()
  947.                 PlaceFront(innercolumntoptip)
  948.                 TurnLeft()
  949.                 Forward()
  950.                 TurnRight()
  951.                 Forward()
  952.                 PlaceFront(innercolumntoptip)
  953.                 Backward()
  954.                 PlaceFront(innercolumntoptip)
  955.                 TurnRight()
  956.                 Forward()
  957.                 Forward()
  958.                 TurnLeft()
  959.                 Forward()
  960.                 PlaceFront(innercolumntoptip)
  961.                 Backward()
  962.                 PlaceFront(innercolumntoptip)
  963.  
  964.                 Downward()
  965.                 TurnLeft()
  966.                 Backward()
  967.                 TurnRight()
  968.                 Forward()
  969.                 Forward()
  970.                 TurnLeft()
  971.                 PlaceFront(innercolumnlight)
  972.                 TurnRight()
  973.                 Backward()
  974.                 Backward()
  975.                 TurnLeft()
  976.                 Forward()
  977.                 Forward()
  978.                 TurnRight()
  979.                 PlaceFront(innercolumnlight)
  980.                 TurnLeft()
  981.                 Forward()
  982.                 Forward()
  983.                 TurnRight()
  984.                 Forward()
  985.                 Forward()
  986.                 TurnRight()
  987.                 PlaceFront(innercolumnlight)
  988.                 TurnLeft()
  989.                 Backward()
  990.                 Backward()
  991.  
  992.             end
  993.         end
  994.         Goto(0,height,0)
  995.         Goto(0,1,0,2)
  996.     end
  997.  
  998. end
  999.  
  1000.  
  1001. local function CastToBool(str)
  1002.     local yes={"yes","y","1","true","ok","vrai","oui","aye"}
  1003.     local no={"no","n","0","false","nok","faux","non","nay"}
  1004.     for _,y in ipairs(yes) do
  1005.         if str==y then
  1006.             return true
  1007.         end
  1008.     end
  1009.     for _,n in ipairs(no) do
  1010.         if str==n then
  1011.             return false
  1012.         end
  1013.     end
  1014.     return nil
  1015. end
  1016.  
  1017. local function AskYesNo(question,canskip)
  1018.     echo(question)
  1019.     while true do
  1020.         local answer=read()
  1021.         if canskip and answer=="" then
  1022.             return default
  1023.         end
  1024.         local yn=CastToBool(answer)
  1025.         if not (yn==nil) then
  1026.             return yn
  1027.         end
  1028.         echo("Please say yes or no")
  1029.     end
  1030. end
  1031.  
  1032. local function AskNumber(question,canskip)
  1033.     echo(question)
  1034.     repeat
  1035.         local answer=read()
  1036.         if canskip and answer=="" then
  1037.             return nil
  1038.         end
  1039.         local n=tonumber(answer)
  1040.         if not n then
  1041.             echo("Please input a number"..(skip and ", or skip." or "!"))
  1042.         else
  1043.             return n
  1044.         end
  1045.     until false
  1046. end
  1047.  
  1048. local function RunTorch(NbrColumnLength,NbrColumnWidth,TorchHeight,extra)
  1049.     local NbrColumnLength=tonumber(NbrColumnLength)
  1050.     local NbrColumnWidth=tonumber(NbrColumnWidth)
  1051.     local TorchHeight=tonumber(TorchHeight)
  1052.     local BackOnFail = true
  1053.     if (not NbrColumnLength) or (not NbrColumnWidth) or (not TorchHeight) then
  1054.         echo("Missing arguments, interactive mode")
  1055.         NbrColumnLength=AskNumber("How many column length-wise?",false)
  1056.         NbrColumnWidth=AskNumber("How many column width-wise?",false)
  1057.         TorchHeight=AskNumber("How high to place torches height-wise?",false)
  1058.     end
  1059.     ImplicitDigging=false
  1060.     ErrorOnMoveFailed=true
  1061.     local ret,msg=pcall(PutTorchesAfterWard,NbrColumnLength,NbrColumnWidth,TorchHeight)
  1062.     if ret then
  1063.         echo("Success!")
  1064.     elseif BackOnFail then
  1065.         echo(msg)
  1066.         echo("Failure! Not even attempting to come back.")
  1067.     end
  1068. end
  1069.  
  1070.  
  1071. local function RunTemple(length,width,height)
  1072.     local length=tonumber(length)
  1073.     local width=tonumber(width)
  1074.     local height=tonumber(height)
  1075.     local BackOnFail = true
  1076.     if (not length) or (not width) or (not height) then
  1077.         echo("Missing arguments, interactive mode")
  1078.         length=AskNumber("Length?",false)
  1079.         width=AskNumber("Width?",false)
  1080.         height=AskNumber("Height?",false)
  1081.     end
  1082.     --ImplicitDigging=false -- Keep default from top of file
  1083.     --ErrorOnMoveFailed=true -- Keep default from top of file
  1084.     local ret,msg=pcall(Temple,length,width,height)
  1085.     if ret then
  1086.         echo("Success!")
  1087.     elseif BackOnFail then
  1088.         echo(msg)
  1089.         echo("Failure! Attempting to come back.")
  1090.         ImplicitDigging=true
  1091.         ErrorOnMoveFailed=false
  1092.         if Goto(0,0,0,0) then
  1093.             echo("Back!")
  1094.         else
  1095.             echo("Failed to come back!")
  1096.         end
  1097.     else
  1098.         echo("Sorry to have failed you!")
  1099.     end
  1100.     ImplicitDigging=false
  1101.     ErrorOnMoveFailed=true
  1102. end
  1103.  
  1104. RunTemple(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement