Skip to content

Commit 22a02bb

Browse files
committed
feat: 🚧 Added Equipment and Added Blocks to the Emulator - WIP
1 parent cb87b6a commit 22a02bb

File tree

4 files changed

+475
-95
lines changed

4 files changed

+475
-95
lines changed

Progress.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ turtle.getSelectedSlot()
88
turtle.select(slotNum)
99
turtle.getItemCount(slotNum)
1010
turtle.getItemSpace(slotNum)
11+
turtle.getFuelLimit()
12+
turtle.transferTo(slotNum, count)
13+
turtle.equipLeft()
14+
turtle.equipRight()
15+
turtle.refuel(count)
1116
```
1217

1318
## Implemented but not finished
@@ -39,11 +44,6 @@ turtle.dropDown(count)
3944
turtle.suck(count)
4045
turtle.suckUp(count)
4146
turtle.suckDown(count)
42-
turtle.refuel(count)
43-
turtle.getFuelLimit()
44-
turtle.transferTo(slotNum, count)--now
45-
turtle.equipLeft()
46-
turtle.equipRight()
4747
turtle.dig()
4848
turtle.digUp()
4949
turtle.digDown()

tests/test_spec.lua

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
---@field True function
2525
---@field False function
2626
---@field has_error function
27+
---@field is_false function
2728
---@field is_true function
2829
---@field equal function
2930
assert = assert
@@ -217,6 +218,7 @@ describe("multiple Turtles", function()
217218
local turtle1
218219
local turtle2
219220
setup(function()
221+
turtleEmulator:clearTurtles()
220222
turtle1 = turtleEmulator:createTurtle()
221223
turtle2 = turtleEmulator:createTurtle()
222224
turtle1["canMoveToCheck"] = function()
@@ -297,7 +299,7 @@ describe("InventoryTests", function()
297299
describe("Adding Items for Testing", function()
298300
before_each(function()
299301
turtle = turtleEmulator:createTurtle()
300-
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 64 }, 1)
302+
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 16 }, 1)
301303
turtle:addItemToInventory({ name = "minecraft:wood_plank", count = 64, maxcount = 64, fuelgain = 8 }, 2)
302304
end)
303305
it("Throw Error when adding Item to full slot", function()
@@ -357,10 +359,36 @@ describe("InventoryTests", function()
357359
assert.are.equal(0, turtle.getItemCount(6))
358360
end)
359361
end)
362+
describe("Refueling", function ()
363+
local fuelGainPerCoal = 16
364+
before_each(function()
365+
turtle = turtleEmulator:createTurtle()
366+
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = fuelGainPerCoal }, 1)
367+
turtle:addItemToInventory({ name = "minecraft:wood_plank", count = 64, maxcount = 64, fuelgain = 8 }, 2)
368+
end)
369+
it("Refuel with coal", function()
370+
turtle.select(1)
371+
local succ = turtle.refuel()
372+
assert.is_true(succ)
373+
assert.are.equal(fuelGainPerCoal, turtle.getFuelLevel())
374+
assert.are.equal(63, turtle.getItemCount())
375+
succ = turtle.refuel(63)
376+
assert.is_true(succ)
377+
assert.are.equal(fuelGainPerCoal * 64, turtle.getFuelLevel())
378+
assert.are.equal(0, turtle.getItemCount())
379+
local succ = turtle.refuel()
380+
assert.is_false(succ)
381+
turtle.select(3)
382+
turtle:addItemToInventory({ name = "minecraft:stone", count = 64, maxcount = 64 }, 3)
383+
succ = turtle.refuel()
384+
assert.is_false(succ)
385+
assert.are.equal(fuelGainPerCoal * 64, turtle.getFuelLevel())
386+
end)
387+
end)
360388
describe("Remaining Inventory Tests", function()
361389
before_each(function()
362390
turtle = turtleEmulator:createTurtle()
363-
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 64 }, 1)
391+
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 16 }, 1)
364392
turtle:addItemToInventory({ name = "minecraft:wood_plank", count = 64, maxcount = 64, fuelgain = 8 }, 2)
365393
turtle:getSocket()
366394
end)
@@ -409,3 +437,111 @@ describe("InventoryTests", function()
409437
end)
410438
end)
411439
end)
440+
describe("Equipment", function ()
441+
---@type TurtleProxy
442+
local turtle
443+
before_each(function()
444+
turtle = turtleEmulator:createTurtle()
445+
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 16 }, 1)
446+
turtle:addItemToInventory({ name = "minecraft:wood_plank", count = 64, maxcount = 64, fuelgain = 8 }, 2)
447+
end)
448+
it("Equip non Equipment", function()
449+
local succ = turtle.equipLeft()
450+
assert.is_false(succ)
451+
end)
452+
it("Equip Equipment", function()
453+
turtle:addItemToInventory({ name = "CCTweaked:chat_box", count = 2, maxcount = 2, equipable = true}, 3)
454+
turtle:select(3)
455+
local succ = turtle.equipLeft()
456+
assert.is_true(succ)
457+
assert.are.equal( turtle.equipslots.left.name, "CCTweaked:chat_box")
458+
assert.are.equal(turtle.equipslots.left.count, 1)
459+
end)
460+
it("Upgrade Equipment", function()
461+
turtle:addItemToInventory({ name = "CCTweaked:chat_box", count = 2, maxcount = 2, equipable = true}, 3)
462+
turtle:select(3)
463+
local succ = turtle.equipLeft()
464+
assert.is_true(succ)
465+
assert.are.equal(turtle.equipslots.left.name, "CCTweaked:chat_box")
466+
assert.are.equal(turtle.equipslots.left.count, 1)
467+
end)
468+
469+
it("Upgrade Equipment and Replace in inventory", function()
470+
turtle:addItemToInventory({ name = "CCTweaked:chat_box", count = 2, maxcount = 2, equipable = true}, 3)
471+
turtle:addItemToInventory({ name = "CCTweaked:chunk_loader", count = 1, maxcount = 1, equipable = true }, 4)
472+
turtle:select(3)
473+
local succ = turtle.equipLeft()
474+
assert.is_true(succ)
475+
assert.are.equal(turtle.equipslots.left.name, "CCTweaked:chat_box")
476+
assert.are.equal(turtle.equipslots.left.count, 1)
477+
turtle:select(4)
478+
succ = turtle.equipLeft()
479+
assert.is_true(succ)
480+
assert.are.equal(turtle.equipslots.left.name, "CCTweaked:chunk_loader")
481+
assert.are.equal(turtle.equipslots.left.count, 1)
482+
assert.are.equal(turtle.inventory[4].count, 1)
483+
assert.are.equal(turtle.inventory[4].name, "CCTweaked:chat_box")
484+
assert.are.equal(turtle.inventory[3].count, 1)
485+
assert.are.equal(turtle.inventory[3].name, "CCTweaked:chat_box")
486+
end)
487+
488+
it("Equip Left and RightSlot", function()
489+
turtle:addItemToInventory({ name = "CCTweaked:chat_box", count = 2, maxcount = 2, equipable = true}, 3)
490+
turtle.select(3)
491+
local succ = turtle.equipRight()
492+
assert.is_true(succ)
493+
assert.are.equal(turtle.equipslots.right.name, "CCTweaked:chat_box")
494+
assert.are.equal(turtle.equipslots.right.count, 1)
495+
assert.are.equal(turtle.inventory[3].name, "CCTweaked:chat_box")
496+
assert.are.equal(turtle.inventory[3].count, 1)
497+
end)
498+
499+
500+
501+
end)
502+
503+
describe("EmulatorTesting", function()
504+
setup(function()
505+
turtleEmulator:clearTurtles();
506+
end)
507+
describe("Adding Blocks", function()
508+
it("Add Block", function()
509+
turtleEmulator:createBlock({ item = {name = "minecraft:stone", maxcount = 64}, position = { x = 0, y = 0, z = 1 } })
510+
assert.are_not.equal(nil, turtleEmulator.blocks["0,0,1"])
511+
assert.are.equal("minecraft:stone", turtleEmulator.blocks["0,0,1"].item.name)
512+
assert.are.equal(nil, turtleEmulator.blocks["0,0,2"])
513+
end)
514+
it("Add Block and remove it", function()
515+
local block = { item = {name = "minecraft:stone", maxcount = 64}, position = { x = 1, y = 0, z = 0 } }
516+
turtleEmulator:createBlock(block)
517+
assert.are_not.equal(nil, turtleEmulator.blocks["1,0,0"])
518+
assert.are.equal("minecraft:stone", turtleEmulator.blocks["1,0,0"].item.name)
519+
turtleEmulator:removeBlock(block.position)
520+
assert.are.equal(nil, turtleEmulator.blocks["1,0,0"])
521+
end)
522+
end)
523+
describe("Read Blocks", function()
524+
---TODO: Implement
525+
end)
526+
describe("Return Block", function()
527+
local block = {
528+
item = {name = "minecraft:stone", maxcount = 64},
529+
position = { x = 1, y = 0, z = 0 }
530+
}
531+
turtleEmulator:createBlock(block)
532+
assert.are_not.equal(nil, turtleEmulator.blocks["1,0,0"])
533+
assert.are_not.equal(nil, turtleEmulator:getBlock(block.position))
534+
local b = turtleEmulator:getBlock(block.position)
535+
assert.is.falsy(b.emulator)
536+
assert.are.equal(block.item.name, b.item.name)
537+
end)
538+
end)
539+
540+
describe("ActionAccepted", function ()
541+
--- TODO: Implement
542+
local turtle1
543+
local turtle2
544+
setup(function()
545+
turtleEmulator:clearTurtles()
546+
end)
547+
end)

turtleEmulator.lua

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,81 @@
1+
--- ### Definitions
2+
---@alias checkActionValid table | string | fun(equipslots: equipslots ,action : string, block: block): true
3+
---@alias block {item: item, checkActionValid: checkActionValid, position: position, onInteration: fun(turtle: turtleMock, block: block, action: string): nil}
4+
5+
--- used from the Scanner, as the blocks will most likely be scanned, saved and then inserted into the turtleEmulator for testing
6+
---@class ScanData
7+
---@field x number
8+
---@field y number
9+
---@field z number
10+
---@field name string
11+
---@field checkActionValid checkActionValid | nil
12+
13+
---@class ScanDataTable
14+
---@field _ ScanData
15+
16+
17+
18+
---@type TurtleMock
119
local turtleM = require("./turtleMock")
20+
221
---@class TurtleEmulator
322
local turtleEmulator = {
23+
---@type table<number, TurtleProxy>
424
turtles = {},
25+
---@type table<string, block>
26+
blocks = {},
527
createTurtle = function(self)
6-
local t = turtleM.createMock()
28+
---@type TurtleProxy
29+
local t = turtleM.createMock(self)
730
table.insert(self.turtles, t)
831
return t
932
end,
1033
}
1134

35+
local function createPositionKey(position)
36+
return position.x .. "," .. position.y .. "," .. position.z
37+
end
38+
39+
--- Adds a block to the emulated world
40+
---@param block block
41+
function turtleEmulator:createBlock(block)
42+
assert(block.position, "Block has no position")
43+
self.blocks[createPositionKey(block.position)] = block
44+
end
45+
46+
--- Removes a block from the emulated world
47+
---@param position position
48+
function turtleEmulator:removeBlock(position)
49+
self.blocks[createPositionKey(position)] = nil
50+
end
51+
52+
--- Reads the blocks from the scanner-result and adds them to the emulated world
53+
---@param scannResult ScanDataTable
54+
function turtleEmulator:readBlocks(scannResult)
55+
for _, v in pairs(scannResult) do
56+
self:createBlock(v)
57+
end
58+
end
59+
60+
--- Removes all blocks from the emulated world
61+
function turtleEmulator:clearBlocks()
62+
self.blocks = {}
63+
end
64+
65+
--- Returns the block at the given position
66+
---@param position position
67+
---@return block | TurtleProxy | nil
68+
function turtleEmulator:getBlock(position)
69+
for _, t in ipairs(self.turtles) do
70+
if t.position.x == position.x and t.position.y == position.y and t.position.z == position.z then
71+
return t
72+
end
73+
end
74+
return self.blocks[createPositionKey(position)]
75+
end
76+
77+
function turtleEmulator:clearTurtles()
78+
self.turtles = {}
79+
end
80+
1281
return turtleEmulator

0 commit comments

Comments
 (0)