Advertisement
TheNouillet

Untitled

Oct 31st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. -------------------Prgramme de dessin-----------------------------
  2. --Affichage--
  3. function displayLayer(building, layer, height, width)
  4. local mat = 0
  5. for i=1,width do
  6. for j=1,height do
  7. mat = building[layer][i][j]
  8. term.setCursorPos(i,j)
  9. if mat == 0 then
  10. term.setBackgroundColor(1)
  11. elseif mat == 1 then
  12. term.setBackgroundColor(16384)
  13. else
  14. term.setBackgroundColor(8192)
  15. end
  16. term.write(" ")
  17. end
  18. end
  19. end
  20. --Prgramme principal
  21. function paint(hauteur)
  22. --Preparation de l'affichage de l'image--
  23. term.setBackgroundColor(1)
  24. term.clear()
  25. term.setCursorPos(1,1)
  26. term.setCursorBlink(false)
  27. local termHeight
  28. local termWidth
  29. termWidth, termHeight = term.getSize()
  30.  
  31. --Gestion Event
  32. local event = ""
  33. local key = 0
  34. local x = 0
  35. local y = 0
  36. --Données du batiment
  37. local mat = 1
  38. local cLayer = 1
  39. local building = {} --Creation de matrice 3D
  40. for i=1,hauteur do
  41. building[i] = {} --Creation de la matrice 2D
  42. for j=1,termWidth do
  43. building[i][j] = {} --Création de la ligne
  44. for k=1,termHeight-1 do
  45. building[i][j][k] = 0
  46. end
  47. end
  48. end
  49. --Boucle principale
  50. while key ~= 28 do
  51. --Affichage
  52. displayLayer(building, cLayer, termHeight-1, termWidth)
  53. term.setBackgroundColor(32768)
  54. term.setCursorPos(1,termHeight)
  55. write("^Turtle Couche " .. cLayer .. "/" .. hauteur .. " Mat : " .. mat)
  56.  
  57. event, key, x, y = os.pullEvent()
  58.  
  59. --Traitement de l'event
  60. if event == "mouse_drag" or event == "mouse_click" then --Souris
  61. if y < termHeight then
  62. if key == 1 then --Clic gauche
  63. building[cLayer][x][y] = mat
  64. elseif key == 2 then --Clic droit
  65. building[cLayer][x][y] = 0
  66. end
  67. end
  68. elseif event == "key" then --Clavier
  69. if key == 42 then --Maj
  70. if mat == 1 then
  71. mat = 2
  72. else
  73. mat = 1
  74. end
  75. elseif key == 200 then --Fleche haut
  76. if cLayer < hauteur then
  77. cLayer = cLayer + 1
  78. end
  79. elseif key == 208 then --Fleche bas
  80. if cLayer > 1 then
  81. cLayer = cLayer - 1
  82. end
  83. end
  84. end
  85. end
  86.  
  87. --Preparation de l'affichage de la console--
  88. term.setBackgroundColor(32768)
  89. term.clear()
  90. term.setCursorBlink(true)
  91. term.setCursorPos(1,1)
  92. return building
  93. end
  94.  
  95. -------------------Programme de construction-----------------------------
  96. function goForward()
  97. while not(turtle.forward()) do
  98. turtle.dig()
  99. end
  100. end
  101.  
  102. function goUp()
  103. while not(turtle.up()) do
  104. turtle.digUp()
  105. end
  106. end
  107.  
  108. function place(mat)
  109. local slot = 1
  110. if turtle.detectDown() then
  111. turtle.digDown()
  112. end
  113. if mat == 0 then
  114. return
  115. --Contage des materiaux disponibles--
  116. elseif mat == 1 then
  117. slot = 1
  118. while slot < 9 and turtle.getItemCount(slot) == 0 do
  119. slot = slot + 1
  120. end
  121. if slot == 9 then slot = 1 end --Si il n'y a plus de matériau
  122. elseif mat == 2 then
  123. slot = 9
  124. while slot < 17 and turtle.getItemCount(slot) == 0 do
  125. slot = slot + 1
  126. end
  127. if slot == 17 then slot = 9 end --Si il n'y a plus de matériau
  128. end
  129. turtle.select(slot)
  130. turtle.placeDown()
  131. end
  132.  
  133. function construct(building, width, depth, height)
  134. local x = 1
  135. local y = depth
  136. local evenY = -1
  137. local evenX = 1
  138. for currentLayer=1,height do
  139. while x >= 1 and x <= width do-- On fait la couche
  140. while y >= 1 and y <= depth do-- On fait la colonne
  141. place(building[currentLayer][x][y])
  142. print(x .. " " .. y)
  143. if (y > 1 and evenY == -1) or (y < depth and evenY == 1) then
  144. goForward()
  145. end
  146. y = y + evenY
  147. end
  148. if (x > 1 and evenX == -1) or (x < width and evenX == 1) then
  149. if evenY == -1 then --Demi tour
  150. turtle.turnRight()
  151. goForward()
  152. turtle.turnRight()
  153. evenY = 1
  154. y = 1
  155. elseif evenY == 1 then
  156. turtle.turnLeft()
  157. goForward()
  158. turtle.turnLeft()
  159. evenY = -1
  160. y = depth
  161. end
  162. else
  163. goUp()
  164. turtle.turnLeft()
  165. turtle.turnLeft()
  166. end
  167. x = x + evenX
  168. end
  169.  
  170. end
  171. end
  172. -------------------Main-----------------------------
  173. local hauteur = nil
  174. local building
  175. local event = ""
  176. local key = 0
  177. local x = 0
  178. local y = 0
  179.  
  180. term.clear()
  181. term.setCursorPos(1,1)
  182. while hauteur == nil or hauteur < 1 do
  183. print("Indiquez la hauteur de votre batiment")
  184. hauteur = tonumber(read())
  185. end
  186. building = paint(hauteur)
  187. local termHeight
  188. local termWidth
  189. termWidth, termHeight = term.getSize()
  190. construct(building, 3, termHeight-1, hauteur)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement