Skip to content

Commit 30c371e

Browse files
committed
feat: 👥 Implemented Detect, Compare and Inspect
1 parent 74e4c41 commit 30c371e

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

Progress.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ turtle.attackDown()
6060
turtle.craft(count)
6161
peripheral.find("chest, turtle, drive, ...")
6262
```
63+
64+
Also: A way to manage the "state" of blocks (for turtle.inspect f.e.)

tests/test_spec.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,70 @@ describe("ActionAccepted", function ()
627627
assert.are.equal("computercraft:turtle_normal", turtleEmulator:getBlock({ x = 2, y = 0, z = 0 }).item.name)
628628
assert.are.equal("computercraft:turtle_normal", turtle2.getItemDetail().name)
629629
end)
630+
end)
631+
632+
describe("Detect, Compare, Inspect", function()
633+
local turtle
634+
before_each(function()
635+
turtleEmulator:clearBlocks()
636+
turtleEmulator:clearTurtles()
637+
turtle = turtleEmulator:createTurtle()
638+
turtleEmulator:createBlock({ item = {name = "minecraft:stone"}, position = { x = 1, y = 0, z = 0 } })
639+
turtleEmulator:createBlock({ item = {name = "minecraft:wood"}, position = { x = 0, y = 1, z = 0 }, state = {burning = true} })
640+
641+
end)
642+
it("Detect", function()
643+
turtleEmulator:createBlock({ item = {name = "minecraft:stone"}, position = { x = 1, y = 0, z = 0 } })
644+
assert.is_true(turtle.detect())
645+
assert.is_true(turtle.detectUp())
646+
assert.is_false(turtle.detectDown())
647+
turtleEmulator:createBlock({ item = {name = "minecraft:obsidian"}, position = { x = 0, y = -1, z = 0 , tag = {["minecraft:minable/pickaxe"] = true}} })
648+
assert.is_true(turtle.detectDown())
649+
end)
650+
it("Compare", function()
651+
assert.is_false(turtle.compare())
652+
turtle:addItemToInventory({ name = "minecraft:stone", count = 1, maxcount = 64 })
653+
assert.is_true(turtle.compare())
654+
assert.is_false(turtle.compareUp())
655+
turtleEmulator:createBlock({ item = {name = "minecraft:obsidian"}, position = { x = 0, y = -1, z = 0 , tag = {["minecraft:minable/pickaxe"] = true}} })
656+
assert.is_false(turtle.compareDown())
657+
turtle:addItemToInventory({ name = "minecraft:obsidian", count = 1, maxcount = 64 })
658+
turtle.select(2)
659+
assert.is_true(turtle.compareDown())
660+
end)
661+
it("Inspect", function()
662+
local succ, info = turtle.inspect()
663+
assert.is_true(succ)
664+
---@cast info inspectResult
665+
assert.are.equal("minecraft:stone", info.name)
666+
assert.are.equal(nil, info.state)
667+
assert.are.equal(nil, info.tags)
668+
succ, info = turtle.inspectUp()
669+
assert.is_true(succ)
670+
---@cast info inspectResult
671+
assert.are.equal("minecraft:wood", info.name)
672+
assert.are.equal(true, info and info.state and info.state.burning)
673+
assert.are.equal(nil, info.tags)
674+
succ, info = turtle.inspectDown()
675+
assert.is_false(succ)
676+
assert.are.equal(nil, info)
677+
turtleEmulator:createBlock({ item = {name = "minecraft:obsidian", tags = {["minecraft:minable/pickaxe"] = true}}, position = { x = 0, y = -1, z = 0 } })
678+
succ, info = turtle.inspectDown()
679+
assert.is_true(succ)
680+
---@cast info inspectResult
681+
assert.are.equal("minecraft:obsidian", info.name)
682+
assert.are.equal(true, info and info.tags and info.tags["minecraft:minable/pickaxe"])
683+
local turtle2 = turtleEmulator:createTurtle()
684+
turtle2.addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 8 }, 1)
685+
turtle2.refuel(64)
686+
turtle2.dig()
687+
assert.is_true(turtle2.forward())
688+
succ, info = turtle.inspect()
689+
assert.is_true(succ)
690+
assert.are.equal("computercraft:turtle_normal", info.name)
691+
assert.are.equal(nil, info.state)
692+
assert.are.equal(nil, info.tags)
693+
end)
630694

631695

632696
end)

turtleEmulator.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
---@alias checkActionValidFunc fun(equipslots: equipslots ,action : checkActionName, block: block): true
66
---@alias checkActionValid checkActionValidFunc | table<checkActionName, toolName|checkActionValidFunc> # e.g. {["dig"] = "pickaxe", ["place"] = func()}
77
---@alias onInteration fun(turtle: TurtleProxy, block: block | TurtleProxy, action: string): nil
8-
---@alias block {item: item, checkActionValid: checkActionValid, position: position, onInteration: onInteration}
8+
---@alias block {item: item, checkActionValid: checkActionValid, position: position, onInteration: onInteration, state: table<string, any> | nil}
99

1010
--- used from the Scanner, as the blocks will most likely be scanned, saved and then inserted into the turtleEmulator for testing
1111
---@class ScanData

turtleMock.lua

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
---@alias height integer
1313
---@alias position {x: north, y: east, z: height}
1414

15-
---@alias item {name: string, durabilty: integer, equipable: boolean, fuelgain: integer, placeAble: boolean, maxcount: number, wildcardInfo: any, count: integer}
15+
---@alias item {name: string, durabilty: integer, equipable: boolean, fuelgain: integer, placeAble: boolean, maxcount: number, wildcardInfo: any, count: integer, tags: table<string, any> | nil}
1616
---@alias inventory { [integer]: item }
1717

1818
---@alias left string
1919
---@alias right string
2020
---@alias equipslots {left: item, right: item}
2121

22+
---@alias inspectResult {name: string, tags: table<string, any> | nil, state: table<string, any> | nil} | nil
23+
2224
local defaultInteration = require("../defaultInteraction")
2325

2426
---@class TurtleMock
@@ -373,6 +375,38 @@ local function dig(turtle, block)
373375
return true, "Cannot beak block with this tool"
374376
end
375377

378+
---@param block block | nil
379+
---@return boolean
380+
local function detect(block)
381+
return block and true or false
382+
end
383+
384+
---@param block block | nil
385+
---@param compareItem item | nil
386+
---@return boolean
387+
local function compare(block, compareItem)
388+
if block == nil and compareItem == nil then
389+
return true
390+
end
391+
if block == nil and compareItem ~= nil then
392+
return false
393+
end
394+
if compareItem == nil and block ~= nil then
395+
return false
396+
end
397+
return block.item.name == compareItem.name
398+
end
399+
400+
---@param block block | nil
401+
---@return boolean
402+
---@return inspectResult
403+
local function inspect(block)
404+
if block == nil or block.item == nil or block.item.name == nil then
405+
return false, nil
406+
end
407+
return true, { name = block.item.name, tags = block.item.tags, state = block.state }
408+
end
409+
376410
---@param emulator TurtleEmulator
377411
---@return TurtleProxy
378412
function turtleMock.createMock(emulator, id)
@@ -647,6 +681,54 @@ function turtleMock:digDown()
647681
return dig(self, blockPos)
648682
end
649683

684+
function turtleMock:detect()
685+
local _, _, blockPos = forward(self, false)
686+
local block = self.emulator:getBlock(blockPos)
687+
return detect(block)
688+
end
689+
690+
function turtleMock:detectUp()
691+
local block = self.emulator:getBlock({x = self.position.x, y = self.position.y + 1, z = self.position.z})
692+
return detect(block)
693+
end
694+
695+
function turtleMock:detectDown()
696+
local block = self.emulator:getBlock({x = self.position.x, y = self.position.y - 1, z = self.position.z})
697+
return detect(block)
698+
end
699+
700+
function turtleMock:compare()
701+
local _, _, blockPos = forward(self, false)
702+
local block = self.emulator:getBlock(blockPos)
703+
return compare(block, self.inventory[self.selectedSlot])
704+
end
705+
706+
function turtleMock:compareUp()
707+
local block = self.emulator:getBlock({x = self.position.x, y = self.position.y + 1, z = self.position.z})
708+
return compare(block, self.inventory[self.selectedSlot])
709+
end
710+
711+
function turtleMock:compareDown()
712+
local block = self.emulator:getBlock({x = self.position.x, y = self.position.y - 1, z = self.position.z})
713+
return compare(block, self.inventory[self.selectedSlot])
714+
end
715+
716+
function turtleMock:inspect()
717+
local _, _, blockPos = forward(self, false)
718+
local block = self.emulator:getBlock(blockPos)
719+
return inspect(block)
720+
end
721+
722+
function turtleMock:inspectUp()
723+
local block = self.emulator:getBlock({x = self.position.x, y = self.position.y + 1, z = self.position.z})
724+
return inspect(block)
725+
end
726+
727+
function turtleMock:inspectDown()
728+
local block = self.emulator:getBlock({x = self.position.x, y = self.position.y - 1, z = self.position.z})
729+
return inspect(block)
730+
end
731+
650732
---will only print content if canPrint is set to true
651733
---@param ... any
652734
---@return nil

0 commit comments

Comments
 (0)