diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5347502..ee8be07 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,9 +1,7 @@ name: Busted - on: [push, pull_request] - jobs: test: runs-on: ubuntu-latest @@ -11,8 +9,6 @@ jobs: steps: - name: checkout uses: actions/checkout@v2 - with: - submodules: true - name: get lua uses: leafo/gh-actions-lua@v10 @@ -30,11 +26,11 @@ jobs: luarocks install luasocket luarocks install luasec - - name: Git Submodule Update + - name: fetch dependencies run: | - git pull --recurse-submodules - git submodule update --init --remote --recursive + chmod +x ./fetch-deps.sh + ./fetch-deps.sh - name: test run: | - busted tests + busted . diff --git a/.gitignore b/.gitignore index ea92fae..10ee637 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /config /.vscode /computer -/suits \ No newline at end of file +/libs \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index d2d5681..0000000 --- a/.gitmodules +++ /dev/null @@ -1,4 +0,0 @@ -[submodule "TestSuite-lib"] - path = TestSuite-lib - url = https://github.com/mc-cc-scripts/TestSuite-lib.git - branch = master diff --git a/README.md b/README.md index 56475a2..f411c43 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,45 @@ # turtleEmulator-lib -This Libary is meant to Emulate the functions and behaviour of turtles in Minecraft. -This is exclusively useful for testing with f.e. Busted +This Emulator is exclusively ment to enable testing CC turtle-code with a testingframework (namely busted), outside MC and CraftOS. -### [Documentation](https://github.com/mc-cc-scripts/turtleEmulator-lib/wiki) \ No newline at end of file +### WIP + + Should increase in scope incrementally. + +> For now it only supports all functions listed in the Progress.md + +### HOW TO USE + +```lua +local turtleEmulator = require("/turtleEmulator") + +-- create turtles +local turtleOne = turtleEmulator:createTurtle() +local turtleTwo = turtleEmulator:createTurtle() + +-- add items to turtles for testing +turtleOne.addItemToInventory({ + name = "minecraft:coal", + count = 64, + maxcount = 64, + fuelgain = 8 +}) +turtleOne.refuel(64) + +assert(true == turtleOne.forward(), "I have fuel and should work") +assert(false == turtleTwo.forward(), "I have no fuel and should not work") + +-- add block to World +turtleEmulator:createBlock({ + item = { + name = "minecraft:dirt" + }, + position = { x = 0, y = -1, z = 0 } +}) + +``` +--- +### Restrictions + +To allow the creation of multiple turtles within the same Emulator, the turtle returned by createTurtle is only a proxy, meaning that the metatable should not be modified! +However, should the need ever arise, you can modify it by getmetatable(turtle).\_\_metatable = nil. But please be aware that overriding the \_\_index and \_\_newIndex will break the functionality of the turtle. diff --git a/TestSuite-lib b/TestSuite-lib deleted file mode 160000 index 50a6ac1..0000000 --- a/TestSuite-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 50a6ac1f709d6e25c6e61ed661e3c69f673e80ce diff --git a/cc/pretty.lua b/cc/pretty.lua deleted file mode 100644 index 037b1ce..0000000 --- a/cc/pretty.lua +++ /dev/null @@ -1,8 +0,0 @@ -local pretty = { - pretty_print = function (t) - for k, v in pairs(t) do - print(k, v) - end - end -} -return pretty \ No newline at end of file diff --git a/ccPackage.lua b/ccPackage.lua deleted file mode 100644 index ab189e4..0000000 --- a/ccPackage.lua +++ /dev/null @@ -1,11 +0,0 @@ --- get the current path to this file -local spath = - debug.getinfo(1,'S').source:sub(2):gsub("/+", "/"):gsub("[^/]*$","") --- add the path to the package path -package.path = spath.."?.lua;" - ..spath.."inventory/?.lua;" - ..spath.."peripherals/?.lua;" - ..spath.."defaultBehaviour/?.lua;" - ..package.path --- require the package of the test suite -require(spath.."TestSuite-lib/ccPackage") \ No newline at end of file diff --git a/defaultBehaviour/defaultcheckActionValid.lua b/defaultBehaviour/defaultcheckActionValid.lua deleted file mode 100644 index eec4fd0..0000000 --- a/defaultBehaviour/defaultcheckActionValid.lua +++ /dev/null @@ -1,14 +0,0 @@ --- This is the default checkActionValid function ----@type checkActionValidFunc -local defaultcheckActionValid = function(turtle, action, block) - -- example use cases: - -- - -- if turtle.equipslots.left and turtle.equipslots.left.name == "" then end - -- if action == "dig" then end - -- if block.item.name == "" then end - - - return true -end - -return defaultcheckActionValid \ No newline at end of file diff --git a/defaultBehaviour/defaultInteraction.lua b/defaultInteraction.lua similarity index 97% rename from defaultBehaviour/defaultInteraction.lua rename to defaultInteraction.lua index 63bd5d1..54934c9 100644 --- a/defaultBehaviour/defaultInteraction.lua +++ b/defaultInteraction.lua @@ -23,4 +23,4 @@ local function defaultInteration(turtle, block, action) end end -return defaultInteration +return defaultInteration \ No newline at end of file diff --git a/fetch-deps.sh b/fetch-deps.sh new file mode 100755 index 0000000..b1aa60b --- /dev/null +++ b/fetch-deps.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# ---- Whats happening ---- # + +# This fetches the dependencies listed in the "libs" variable and saves them in the targetFolder + + + +set -e + +libs=( + "helperFunctions-lib" + "eventCallStack-lib" + "ccClass-lib" + "testSuite-lib" +) + +# Basic setup variables +repo="mc-cc-scripts" +branch="master" +targetFolderName=libs + + +# fetch files.txt and save each file into the targetFolder +fetch() { + + files_txt=$(curl -fsSL "https://raw.githubusercontent.com/$repo/$1/$branch/files.txt") + if [ -z "$files_txt" ]; then + echo "Could not load files.txt for $1" + exit 1 + fi + while IFS= read -r FILE; do + rm -f $targetFolderName/$1.lua # rm existing file + curl -s "https://raw.githubusercontent.com/$repo/$1/$branch/$FILE" -o "$targetFolderName/$FILE" + done < <(echo "$files_txt") +} + +mkdir -p $targetFolderName + +for i in "${libs[@]}"; do + fetch "$i" +done \ No newline at end of file diff --git a/files.txt b/files.txt index e69de29..bb7d1b3 100644 --- a/files.txt +++ b/files.txt @@ -0,0 +1,10 @@ +turtleEmulator.lua +turtleMock.lua +peripheral.lua +defaultInteraction.lua +inventory/chestInventory.lua +inventory/inventory.lua +inventory/turtleInventory.lua +peripherals/geoScanner.lua +peripherals/modem.lua +peripherals/peripheral.lua \ No newline at end of file diff --git a/inventory/chestInventory.lua b/inventory/chestInventory.lua index 3dd9dc2..f3e8a87 100644 --- a/inventory/chestInventory.lua +++ b/inventory/chestInventory.lua @@ -1,5 +1,5 @@ -local class = require("./TestSuite-lib/ccClass/ccClass") -local inventory = require("../inventory/inventory") +local class = require("ccClass") +local inventory = require("inventory") ---@class ChestInventory : Inventory local chestInventory = class(inventory) diff --git a/inventory/turtleInventory.lua b/inventory/turtleInventory.lua index ff15876..22d581c 100644 --- a/inventory/turtleInventory.lua +++ b/inventory/turtleInventory.lua @@ -1,6 +1,6 @@ -local class = require("./TestSuite-lib/ccClass/ccClass") -local inventory = require("../inventory/inventory") -local deepCopy = require("../TestSuite-lib/helperFunctions/helperFunctions").deepCopy +local class = require("ccClass") +local inventory = require("inventory") +local deepCopy = require("helperFunctions").deepCopy ---@class TurtleInventory : Inventory ---@field protected _base Inventory the base inventory class local turtleInventory = class(inventory, function (selfRef) diff --git a/tests/test_spec.lua b/tests/test_spec.lua index 1cb46fa..9d98c65 100644 --- a/tests/test_spec.lua +++ b/tests/test_spec.lua @@ -29,13 +29,11 @@ ---@field equal function assert = assert -local spath = - debug.getinfo(1,'S').source:sub(2):gsub("/+", "/"):gsub("[^/]*$",""):gsub("/tests", ""):gsub("tests", "") -if spath == "" then - spath = "./" -end -local package = spath.."ccPackage" -require(package) +package.path = package.path + ..";inventory/?.lua;" + .."libs/?.lua;" + .."libs/?/?.lua;" + .."peripherals/?.lua" -- load the other suits local vector = require("vector") diff --git a/turtleEmulator.lua b/turtleEmulator.lua index 7bfcbc7..9506d28 100644 --- a/turtleEmulator.lua +++ b/turtleEmulator.lua @@ -31,12 +31,29 @@ ---@type TurtleMock local turtleM = require("turtleMock") -local defaultInteraction = require("defaultInteraction") -local defaultcheckActionValid = require("defaultcheckActionValid") local chestInventory = require("chestInventory") ---@type Vector local vector = require("vector") +local defaultInteraction = require("defaultInteraction") + +---ment to be replaced if required +---@param turtle TurtleMock +---@param action any +---@param block block +---@return boolean +local defaultcheckActionValid = function(turtle, action, block) + -- example use cases: + -- + -- if turtle.equipslots.left and turtle.equipslots.left.name == "" then end + -- if action == "dig" then end + -- if block.item.name == "" then end + + + return true +end + + ---comment ---@param position Vector ---@param item Item diff --git a/turtleMock.lua b/turtleMock.lua index 64fd6b5..09bec8f 100644 --- a/turtleMock.lua +++ b/turtleMock.lua @@ -74,12 +74,13 @@ local peripheral = require("peripheral") -local defaultInteraction = require("defaultInteraction") local turtleInventory = require("turtleInventory") ---@type Vector local vector = require("vector") +local defaultInteraction = require("defaultInteraction") local deepCopy = require("helperFunctions").deepCopy + --- this class should not be used directly, use the createMock of the turtleEmulator function instead, which will set the proxy ---@type TurtleMock ---@diagnostic disable-next-line: missing-fields