Advertisement
fzed51

[CC] Turtle - pont / bridge

Aug 30th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. --[[
  2. +----------------------------------------------------------------------------+
  3. | Bridge
  4. | version : 2.1
  5. | Auteur : fzed51
  6. | git : https://github.com/fzed51/CC_Script/blob/master/disk/Bridge_2.lua
  7. | pastebin : http://pastebin.com/4DZfqyQ6
  8. +----------------------------------------------------------------------------+
  9. | tag : [lua] [MC] [MineCraft] [CC] [ComputerCraft] [Turtle]
  10. | Descript :
  11. | construit un pont avec des arches.
  12. +----------------------------------------------------------------------------+
  13. ]]--
  14.  
  15. dofile('advTurtle') -- http://pastebin.com/7mLzefhQ
  16.  
  17. -- inventaire
  18. item.add('coal',1,64)
  19. setFuelItem(item.coal)
  20. item.add('cobblestone',2,64)
  21. item.setup()
  22.  
  23. --fonctions
  24. local function round(num)
  25.   return math.floor(num + 0.5)
  26. end
  27. local function makePlan( l )
  28.     print("Creation d'un plan pour un pont de "..l.."m.")
  29.     local plan = {}
  30.     -- calcul hauteur max d'un pilier
  31.     local hMax = 63
  32.     if l*2 > hMax then hMax = l*2 end
  33.     -- calcul longueur d'une arche
  34.     local tailles,lArche = {19,18,17,16,15,14,13,12,11,10},0
  35.     local r = tailles[1]
  36.     for _,v in ipairs(tailles) do
  37.         if r < (l-(l%v)) then
  38.             r = (l-(l%v))
  39.             lArche = v
  40.         end
  41.     end
  42.     print('Le pont aura des arches de '..lArche..'m de long.')
  43.     local nbArche = math.ceil(l/lArche)
  44.     for arche = 1, nbArche do
  45.         plan[#plan+1] = hMax
  46.         for position = -1*((lArche-3)/2), ((lArche-3)/2) do
  47.             local pos, rArche =math.abs(position), (lArche-2)/2
  48.             plan[#plan+1] = 2 + ( rArche - round(( rArche^2 - pos^2 )^0.5 ))
  49.         end
  50.         plan[#plan+1] = hMax
  51.     end
  52.     while #plan > l do
  53.         if #plan%2 == 0 then
  54.             table.remove(plan)
  55.         else
  56.             table.remove(plan, 1)
  57.         end
  58.     end
  59.     return plan
  60. end
  61. local function construitSection( epaisseur )   
  62.     local alt  = 0
  63.     local dig  = 0
  64.     local stop = false
  65.     while not stop and ( alt > -2 or dig < 2 ) and epaisseur + alt > 0 do
  66.         if turtle.detectDown() then
  67.             dig = dig + 1
  68.         else
  69.             dig = 0
  70.         end
  71.         if tryDown() then
  72.             alt = alt - 1
  73.         else
  74.             stop = true
  75.         end
  76.     end
  77.     while alt < 0 do
  78.         if alt >= -2 then
  79.             turnLeft()
  80.             tryDig()
  81.             tryPlace(item.cobblestone)
  82.             turnBack()
  83.             tryDig()
  84.             tryPlace(item.cobblestone)
  85.             turnLeft()
  86.         end
  87.         tryUp()
  88.         alt = alt + 1
  89.         tryPlaceDown(item.cobblestone)
  90.     end
  91. end
  92. local function countBlock( plan, idx )
  93.     local nbBlock = 0
  94.     if idx == nil or idx > #plan then idx = #plan end
  95.     for i = 1, idx do
  96.         nbBlock = nbBlock + plan[i]
  97.     end
  98.     return nbBlock
  99. end
  100. local function refeelInv( position )
  101.     local p = position
  102.     while p>0 do
  103.         tryForward()
  104.         p=p-1
  105.     end
  106.     print('Rechargez mon inventaire!')
  107.     print('Appuyez sur une touche pour continuer.')
  108.     os.pullEvent('key')
  109.     turnBack()
  110.     while p<position do
  111.         tryForward()
  112.         p=p+1
  113.     end
  114.     turnBack()
  115. end
  116. local function printPlan( p )
  117.     print('Plan pour un pont de '..#p..'m de long.')
  118.     for _, n in ipairs( p ) do
  119.         local nN, sP = n, '##'
  120.         while nN > 0 do
  121.             nN = nN - 1
  122.             sP = sP .. '+'
  123.         end
  124.         print(sP)
  125.     end
  126. end
  127. --main function
  128. local function bridge()
  129.     local longueurPont, plan = 0, {}
  130.     repeat
  131.         tryForward()
  132.         longueurPont = longueurPont + 1
  133.         tryDigUp()
  134.     until turtle.detectDown()
  135.     turnBack()
  136.     local plan = makePlan( longueurPont )
  137.     local nbBlockTotal = countBlock( plan )
  138.     print ('Début de la construction du pont')
  139.     for p = longueurPont, 1, -1 do
  140.         local nbBlock = countBlock( plan, p )
  141.         print ('avancement : '..tostring(round((nbBlockTotal-nbBlock)*100/nbBlockTotal))..'%')
  142.         if itemCount( item.cobblestone ) < (plan[p]+4) then
  143.             refeelInv(p)
  144.         end
  145.         tryForward()
  146.         construitSection(plan[p])
  147.     end    
  148. end
  149. --main
  150. term.clear()
  151. term.setCursorPos(1,1)
  152. local head =
  153. [[
  154.                  Bridge v2.0
  155. """""""""___________________________""""""""""""
  156. """"""""""#########################"""""""""""""
  157. """"""""""" \ /   \ /   \ /   \ / """"""""""""""
  158. """""""""""""v~~~~~v~~~~~v~~~~~v""""""""""""""""
  159. """""""""""""~~~~~~~~~~~~~~~~~""""""""""""""""""
  160. ]]
  161. term.clear()
  162. term.setCursorPos(1,1)
  163. print (head)
  164. bridge()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement