diff --git a/rsiTurtle.lua b/rsiTurtle.lua index 4b49e1a71d2ea602d3cd9c8046e435f6afa69f27..830349610fd7c488e97f044394613a60ca3a3dbe 100644 --- a/rsiTurtle.lua +++ b/rsiTurtle.lua @@ -207,8 +207,8 @@ end -- \brief Déplacement avant multiple -- \param distance Distance à parcourir -- \return bool Succès -function RSI_Turtle:moveForwardGrounded(distance) - return self:moveForward(distance, true) +function RSI_Turtle:moveForwardGrounded(distance, action) + return self:moveForward(distance, true, action) end @@ -277,9 +277,10 @@ end --- -- \brief Déplacement avant multiple -- \param distance Distance à parcourir --- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param function_pointer action Exécuté après chaque déplacement -- \return bool Succès -function RSI_Turtle:moveForward(distance, grounded) +function RSI_Turtle:moveForward(distance, grounded, action) local traveled = 0 if (grounded) @@ -290,6 +291,11 @@ function RSI_Turtle:moveForward(distance, grounded) while (traveled < distance) and turtle.forward() do + if (action ~= nil) + then + action() + end + if (grounded) then self:ground() @@ -324,12 +330,31 @@ end --- -- \brief Déplacement arrière -- \param distance Distance à parcourir +-- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param function_pointer action Exécuté après chaque déplacement -- \return bool Succès -function RSI_Turtle:moveBackward(distance) +function RSI_Turtle:moveBackward(distance, grounded, action) local traveled = 0 + if (grounded) + then + self:ground() + self:climb() + end + while (traveled < distance) and turtle.back() do + if (action ~= nil) + then + action() + end + + if (grounded) + then + self:ground() + self:climb() + end + traveled = traveled + 1 end @@ -441,12 +466,12 @@ end -- ligne. N'affecte pas l'altitude. -- \param pointeur_de_fonction action Pointeur de l'action à réaliser juste -- après être arrivé sur chaque nouveau bloc. --- \param int lines Nombre de lignes (direction: avant) --- \param int cols Nombre de colonnes (direction: droite) --- \param bool grounded Se déplacer en restant toujours au niveau du sol +-- \param int lines Nombre de lignes (direction: avant) +-- \param int cols Nombre de colonnes (direction: droite) +-- \param function_pointer action Action immédiatement après chaque déplacement +-- \param bool grounded Se déplacer en restant toujours au niveau du sol -- \return bool Succès function RSI_Turtle:goThroughArea(lines, cols, action, grounded) - --TODO: Prendre en compte le paramètre "action" local success = true assert(lines >= 1) @@ -459,11 +484,12 @@ function RSI_Turtle:goThroughArea(lines, cols, action, grounded) then self:ground() end + action() for i=1,cols,1 do print("Debut de colonne.") - success = self:moveForward(lines, grounded) + success = self:moveForward(lines, grounded, action) if not success then @@ -478,6 +504,7 @@ function RSI_Turtle:goThroughArea(lines, cols, action, grounded) success = self:right(grounded) self:turnRight() end + action() end return success diff --git a/test.lua b/test.lua index 610e8aba1545ce898673a25dbf48cab35871b963..498543d9809b3560fed4a93cb9ceb5b7ffa2ba77 100644 --- a/test.lua +++ b/test.lua @@ -146,8 +146,8 @@ end -- assert(t:goThroughArea(5, 5, nil)) -- assert(checkPos(4,0,4)) --- assert(t:goThroughArea(15, 15, nil, true)) -assert(t:goThroughAreaGrounded(3, 3)) +-- assert(t:goThroughArea(15, 15, true, nil)) +assert(t:goThroughAreaGrounded(3, 3, turtle.placeUp)) assert(t:moveToOrigin()) print(t:getPos())