Skip to content

Commit 74e4c41

Browse files
committed
feat: 🧱 Emulating blocks in the "world" and dig-functions
1 parent 22a02bb commit 74e4c41

File tree

7 files changed

+422
-178
lines changed

7 files changed

+422
-178
lines changed

Progress.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ turtle.transferTo(slotNum, count)
1313
turtle.equipLeft()
1414
turtle.equipRight()
1515
turtle.refuel(count)
16+
turtle.dig()
17+
turtle.digUp()
18+
turtle.digDown()
19+
turtle.forward()
20+
turtle.back()
21+
turtle.up()
22+
turtle.down()
1623
```
1724

1825
## Implemented but not finished
1926

2027
### No check against fuel level or worldgen
2128

2229
```lua
23-
turtle.forward()
24-
turtle.back()
25-
turtle.up()
26-
turtle.down()
30+
2731
```
2832

2933
## Implemented but not working
@@ -44,9 +48,6 @@ turtle.dropDown(count)
4448
turtle.suck(count)
4549
turtle.suckUp(count)
4650
turtle.suckDown(count)
47-
turtle.dig()
48-
turtle.digUp()
49-
turtle.digDown()
5051
turtle.place(text)
5152
turtle.placeUp(text)
5253
turtle.placeDown(text)
@@ -57,4 +58,5 @@ turtle.attack()
5758
turtle.attackUp()
5859
turtle.attackDown()
5960
turtle.craft(count)
61+
peripheral.find("chest, turtle, drive, ...")
6062
```

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
# turtleEmulator-lib
22

3-
This Emulator is exclusively ment to enable testing CC code with a testingframework, outside MC and CraftOS.
3+
This Emulator is exclusively ment to enable testing CC turtle-code with a testingframework (namely busted), outside MC and CraftOS.
44

55
### WIP
66

7-
> Should increase in scope incrementally. For now it (will) only keep track of position and turtle-direction
7+
Should increase in scope incrementally.
8+
9+
> For now it only supports all functions listed in the Progress.md
810
911
### HOW TO USE
1012

1113
```lua
1214
local turtleEmulator = require("<path>/turtleEmulator")
1315

16+
-- create turtles
1417
local turtleOne = turtleEmulator:createTurtle()
1518
local turtleTwo = turtleEmulator:createTurtle()
1619

17-
-- override the default behavior, skipping fuelcheck etc.
18-
turtleTwo.canMoveToCheck = function() return true end
19-
20-
turtleOne.forward()
21-
assert(turtleOne.position.x == 1)
20+
-- add items to turtles for testing
21+
turtleOne.addItemToInventory({
22+
name = "minecraft:coal",
23+
count = 64,
24+
maxcount = 64,
25+
fuelgain = 8
26+
})
27+
turtleOne.refuel(64)
28+
29+
assert(true == turtleOne.forward(), "I have fuel and should work")
30+
assert(false == turtleTwo.forward(), "I have no fuel and should not work")
31+
32+
-- add block to World
33+
turtleEmulator:createBlock({
34+
item = {
35+
name = "minecraft:dirt"
36+
},
37+
position = { x = 0, y = -1, z = 0 }
38+
})
2239

23-
--...
2440
```
25-
41+
---
2642
### Restrictions
2743

28-
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.
44+
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!
2945
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.

defaultInteraction.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---@type item
2+
local turtleItem = {name = "computercraft:turtle_normal", count = 1, maxcount = 64}
3+
4+
---@type onInteration
5+
local function defaultInteration(turtle, block, action)
6+
assert(turtle, "Turtle is nil")
7+
assert(turtle.emulator, "Turtle has no reference to the emulator")
8+
if block.emulator == nil then -- Block
9+
assert(block.item, "Block has no item")
10+
assert(block.item.name, "Block has no item name")
11+
if action == "dig" then
12+
block.item.count = block.item.count or 1
13+
block.item.maxcount = block.item.maxcount or 64
14+
turtle:addItemToInventory(block.item)
15+
turtle.emulator:removeBlock(block.position)
16+
end
17+
else -- Turtle. On a turtle, the "self" always gets inserted, therefore every parameter moves to the right
18+
if action == "dig" then
19+
assert(block.emulator, "Turtle has no reference to the emulator")
20+
block.emulator:removeTurtle(block)
21+
turtle:addItemToInventory(turtleItem)
22+
end
23+
end
24+
end
25+
26+
return defaultInteration

defaultcheckActionValid.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- This is the default checkActionValid function
2+
---@type checkActionValidFunc
3+
local defaultcheckActionValid = function(equipslots, action, block)
4+
-- example use cases:
5+
--
6+
-- if equipslots.left and equipslots.left.name == "" then end
7+
-- if action == "dig" then end
8+
-- if block.item.name == "" then end
9+
10+
11+
return true
12+
end
13+
14+
return defaultcheckActionValid

0 commit comments

Comments
 (0)