Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- btRobot
- -- A replacement script to make robots locationally aware
- local baseRobot = require( "robot" )
- local compRobot = require( "component" ).robot
- local position = { 0, 0, 0 }
- local direction = 1
- local function addVector( a, b )
- return { a[1] + b[1], a[2] + b[2], a[3] + b[3] }
- end
- local offset =
- {
- [sides.down] = { 0, -1, 0 },
- [sides.up] = { 0, 1, 0 },
- [sides.negz] = { 0, 0, -1 },
- [sides.posz] = { 0, 0, 1 },
- [sides.negx] = { -1, 0, 0 },
- [sides.posx] = { 1, 0, 0 }
- }
- local directions =
- {
- sides.posx,
- sides.posz,
- sides.negx,
- sides.negz,
- length = 4
- }
- local btRobot = {}
- btRobot.turnRight = function()
- baseRobot.turnRight()
- direction = direction + 1
- if direction > 4 then direction = 1
- end
- btRobot.turnLeft = function()
- baseRobot.turnLeft()
- direction = direction - 1
- if direction < 1 then direction = 4
- end
- btRobot.turnAround = function()
- baseRobot.turnAround()
- direction = direction + 2
- if direction > 4 then
- direction = direction - 4
- end
- end
- btRobot.move = function( dir )
- result, reason = compRobot.move( dir )
- if result then
- position = addVector( position, offset[dir] )
- end
- return result, reason
- end
- btRobot.up = function()
- return btRobot.move( sides.up )
- end
- btRobot.down = function()
- return btRobot.move( sides.down )
- end
- btRobot.forward = function()
- return btRobot.move( sides.forward )
- end
- btRobot.back = function()
- return btRobot.move( sides.back )
- end
- btRobot.getPosition = function()
- return position[1], position[2], position[3], directions[direction]
- end
- return setmetatable( btRobot, { __index = baseRobot } )
Advertisement
Add Comment
Please, Sign In to add comment