Advertisement
HandieAndy

movescript

Sep 28th, 2018
693
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --[[
  2. Author: Andrew Lalis
  3. File: movescript.lua
  4. Version: 2.0
  5. Last Modified: 08-07-2020
  6.    
  7. Description:
  8. This library enables string representation of robot movement, for easier
  9. robotic control without repeating functions many times.
  10.  
  11. Begin a script with "d_" to tell the robot to attempt to destroy blocks in the
  12. way of the path of movement.
  13.  
  14. pastebin get -f 4c2AN8Jw /lib/movescript.lua
  15. --]]
  16.  
  17. local r = require("robot")
  18. local component = require("component")
  19. local sides = require("sides")
  20. local computer = require("computer")
  21. local serial = require("serialization")
  22.  
  23. local ic = component.inventory_controller
  24.  
  25. local movescript = {}
  26.  
  27. local destructive = true
  28.  
  29. local function doUntilSuccess(f)
  30.     local success = f()
  31.     while (not success) do
  32.         success = f()
  33.     end
  34. end
  35.  
  36. local function up()
  37.     while (destructive and r.detectUp()) do
  38.         r.swingUp()
  39.     end
  40.     doUntilSuccess(r.up)
  41. end
  42.  
  43. local function down()
  44.     while (destructive and r.detectDown()) do
  45.         r.swingDown()
  46.     end
  47.     doUntilSuccess(r.down)
  48. end
  49.  
  50. local function forward()
  51.     while (destructive and r.detect()) do
  52.         r.swing()
  53.     end
  54.     doUntilSuccess(r.forward)
  55. end
  56.  
  57. local function back()
  58.     if (destructive) then
  59.         r.turnAround()
  60.         while (r.detect()) do
  61.             r.swing()
  62.         end
  63.         r.turnAround()
  64.     end
  65.     doUntilSuccess(r.back)
  66. end
  67.  
  68. local functionMap = {
  69.     ["U"] = up,
  70.     ["D"] = down,
  71.     ["L"] = r.turnLeft,
  72.     ["R"] = r.turnRight,
  73.     ["F"] = forward,
  74.     ["B"] = back,
  75.     ["P"] = r.place,
  76.     ["Pd"] = r.placeDown,
  77.     ["Pu"] = r.placeUp,
  78.     ["S"] = r.swing,
  79.     ["Sd"] = r.swingDown,
  80.     ["Su"] = r.swingUp
  81. }
  82.  
  83. --[[
  84. Determines if a string starts with a certain string.
  85. str - string: The string to check the prefix of.
  86. start - string: The prefix to look for.
  87. --]]
  88. local function starts_with(str, start)
  89.     return str:sub(1, #start) == start
  90. end
  91.  
  92. --[[
  93. Executes a single instruction once.
  94. c - character: One uppercase character to translate into movement.
  95. --]]
  96. local function executeChar(c)
  97.     local f = functionMap[c]
  98.     if (f == nil) then
  99.         return
  100.     end
  101.     f()
  102. end
  103.  
  104. --[[
  105. Executes a single instruction, such as '15D' or '4Sd'
  106. instruction - string: An integer followed by an uppercase character.
  107. --]]
  108. local function executeInstruction(instruction)
  109.     local count = string.match(instruction, "%d+")
  110.     local char = string.match(instruction, "%u%l?")
  111.     if (count == nil) then
  112.         count = 1
  113.     end
  114.     if (char == nil) then
  115.         return
  116.     end
  117.     for i=1,count do
  118.         executeChar(char)
  119.     end
  120. end
  121.  
  122. function movescript.execute(script)
  123.   movescript.exec(script)
  124. end
  125.  
  126. --[[
  127. Executes a given script.
  128. script - string: The script to execute.
  129. --]]
  130. function movescript.exec(script)
  131.     if (starts_with(script, "d_")) then
  132.         destructive = true
  133.         script = string.sub(script, 3)
  134.     else
  135.         destructive = false
  136.     end
  137.     while (script ~= nil and script ~= "") do
  138.         -- Matches the next instruction, possibly prefixed by an integer value.
  139.         local next_instruction = string.match(script, "%d*%u%l?")
  140.         executeInstruction(next_instruction)
  141.         script = string.sub(script, string.len(next_instruction) + 1)
  142.     end
  143. end
  144.  
  145. --[[
  146. Inventory Management
  147. --]]
  148.  
  149.  
  150. local function stackMatches(stack, name, damage, fuzzy)
  151.   return stack ~= nil
  152.     and ((fuzzy and string.find(stack.name, name)) or (stack.name == name))
  153.     and (fuzzy or stack.damage == damage)
  154. end
  155.  
  156. --[[
  157. Selects the given item in the inventory.
  158. name - string: The name of the item to select, like "minecraft:dirt"
  159. damage - number: The damage value of the item. Defaults to 0.
  160. fuzzy - boolean: Whether to accept partial name matches.
  161. --]]
  162. function movescript.selectItem(name, damage, fuzzy)
  163.   damage = damage or 0
  164.   fuzzy = fuzzy or true
  165.   for i=1, r.inventorySize() do
  166.     local stack = ic.getStackInInternalSlot(i)
  167.     if stackMatches(stack, name, damage, fuzzy) then
  168.       r.select(i)
  169.       return true
  170.     end
  171.   end
  172.   return false
  173. end
  174.  
  175. --[[
  176. Counts the number of items of a certain type in the robot's inventory.
  177. name - string: The name of the item to select, like "minecraft:dirt"
  178. damage - number: The damage value of the item. Defaults to 0.
  179. fuzzy - boolean: Whether to accept partial name matches.
  180. --]]
  181. function movescript.getItemCount(name, damage, fuzzy)
  182.   damage = damage or 0
  183.   fuzzy = fuzzy or true
  184.   local count = 0
  185.   for i=1, r.inventorySize() do
  186.     local stack = ic.getStackInInternalSlot(i)
  187.     if stackMatches(stack, name, damage, fuzzy) then
  188.       count = count + stack.size
  189.     end
  190.   end
  191.   return count
  192. end
  193.  
  194. --[[
  195. Sucks some items from an inventory on a certain side.
  196. --]]
  197. function movescript.suckItems(side, name, damage, fuzzy, count)
  198.   damage = damage or 0
  199.   fuzzy = fuzzy or true
  200.   count = count or 1000000
  201.   local invSize = ic.getInventorySize(side)
  202.   local amountMoved = 0
  203.   if invSize == nil then return 0 end
  204.   for i=1, invSize do
  205.     local stack = ic.getStackInSlot(side, i)
  206.     if stackMatches(stack, name, damage, fuzzy) then
  207.       local amountToMove = math.min(count - amountMoved, stack.size)
  208.       local success = ic.suckFromSlot(side, i, amountToMove)
  209.       if success then
  210.         amountMoved = amountMoved + amountToMove
  211.       else
  212.         return amountMoved
  213.       end
  214.     end
  215.   end
  216.   return amountMoved
  217. end
  218.  
  219. --[[
  220. Drops all items except for a certain type.
  221. name - string: The name of the item to select, like "minecraft:dirt"
  222. damage - number: The damage value of the item. Defaults to 0.
  223. fuzzy - boolean: Whether to accept partial name matches.
  224. --]]
  225. function movescript.dropAllExcept(name, damage, fuzzy)
  226.   damage = damage or 0
  227.   fuzzy = fuzzy or true
  228.   local count = 0
  229.   for i=1, r.inventorySize() do
  230.     local stack = ic.getStackInInternalSlot(i)
  231.     if stack ~= nil and not stackMatches(stack, name, damage, fuzzy) then
  232.       r.select(i)
  233.       r.drop()
  234.       count = count + stack.size
  235.     end
  236.   end
  237.   return count
  238. end
  239.  
  240. --[[
  241. Gets the ratio of how charged the batteries are, from 0 to 1.
  242. --]]
  243. function movescript.getEnergyRatio()
  244.   return computer.energy() / computer.maxEnergy()
  245. end
  246.  
  247. --[[
  248. Continuously sleeps until the robot has at least the given energy ratio.
  249. --]]
  250. function movescript.waitUntilEnergyRatio(minRatio)
  251.   repeat
  252.     local energy = movescript.getEnergyRatio()
  253.     os.sleep(1)
  254.   until energy > minRatio
  255. end
  256.  
  257. return movescript
Advertisement
Comments
  • bezark
    214 days
    # text 0.15 KB | 0 0
    1. hey i have downloaded and saved the moveskript to my robot but i can seem to figure out how to start it so i can use it? any help would be appriciated
Add Comment
Please, Sign In to add comment
Advertisement