Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
name: Busted


on: [push, pull_request]


jobs:
test:
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v2
with:
submodules: true

- name: get lua
uses: leafo/gh-actions-lua@v10
Expand All @@ -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 .
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/config
/.vscode
/computer
/suits
/libs
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
### 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("<path>/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.
1 change: 0 additions & 1 deletion TestSuite-lib
Submodule TestSuite-lib deleted from 50a6ac
8 changes: 0 additions & 8 deletions cc/pretty.lua

This file was deleted.

11 changes: 0 additions & 11 deletions ccPackage.lua

This file was deleted.

14 changes: 0 additions & 14 deletions defaultBehaviour/defaultcheckActionValid.lua

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ local function defaultInteration(turtle, block, action)
end
end

return defaultInteration
return defaultInteration
42 changes: 42 additions & 0 deletions fetch-deps.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions files.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions inventory/chestInventory.lua
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
6 changes: 3 additions & 3 deletions inventory/turtleInventory.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
12 changes: 5 additions & 7 deletions tests/test_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 19 additions & 2 deletions turtleEmulator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion turtleMock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading