Skip to content

Commit c691018

Browse files
committed
feat: 👽 Peripherals & SubModules
Added Peripherals, added Submodule for Base-Suits
1 parent 8804dcd commit c691018

File tree

8 files changed

+354
-118
lines changed

8 files changed

+354
-118
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ jobs:
2626
luarocks install luasocket
2727
luarocks install luasec
2828
29+
- name: Git Submodule Update
30+
run: |
31+
git pull --recurse-submodules
32+
git submodule update --remote --recursive
33+
2934
- name: test
3035
run: |
3136
busted .

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "TestSuite-lib"]
2+
path = TestSuite-lib
3+
url = https://github.com/mc-cc-scripts/TestSuite-lib.git

TestSuite-lib

Submodule TestSuite-lib added at 066cb24

inventory.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
---@field findFittingSlot fun(self: inventory, item: item, slot: integer): number
3030
---@field select fun(self: inventory, slot: integer): boolean
3131
---@field list fun(): item[]
32+
---@field getType fun(): string
3233
---@field __index any
3334
---@field deepCopy fun(table: table): table
3435

@@ -163,7 +164,7 @@ function inventory:removeItem( slot, count)
163164
return false, "No item in the slot"
164165
end
165166
if item.count < count then
166-
return false, "Not enough items in the slot"
167+
count = item.count
167168
end
168169
item.count = item.count - count
169170
if item.count == 0 then
@@ -301,6 +302,10 @@ function inventory:list()
301302
return items
302303
end
303304

305+
function inventory:getType()
306+
return "inventory"
307+
end
308+
304309
--#endregion
305310

306311
return inventory

peripheral.lua

Lines changed: 101 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
--[[
2+
3+
The Peripheral is a Module for the Turtle-Mock
4+
--------------
5+
6+
Notes:
7+
8+
9+
- Each turtle will get their own module instance, which is in turn linked to the turtle.
10+
11+
- The peripheral-Module will enable the usage of Peripherals in the Suit.
12+
13+
- Each Peripheral will be wrapped by the peripheral-Module and can then be accessed by the turtle,
14+
to simulate the normal behavior of a turtle interacting with a peripheral.
15+
16+
]]
17+
18+
119
---@alias filterFunc fun(name: string, wrapped: table):boolean
220

321
---@alias relativePosition
@@ -8,70 +26,105 @@
826
--- | "left"
927
--- | "right"
1028

11-
---@class peripheral
29+
local relativePositionOptions = {
30+
["right"] = true,
31+
["left"] = true,
32+
["front"] = true,
33+
["back"] = true,
34+
["top"] = true,
35+
["bottom"] = true
36+
}
37+
38+
---@class PeripheralModule
1239
---@field turtle TurtleMock
13-
---@field linkToTurtle fun(peripheral: peripheral, turtle: TurtleMock):peripheral
14-
---@field find fun(peripheral: peripheral, typeName: string, filterFunc: filterFunc | nil):table | nil
15-
---@field getMethods fun(peripheral: peripheral, name: any):string[] | nil
16-
---@field getNames fun(peripheral: peripheral):string[]
17-
---@field isPresent fun(peripheral: peripheral, positionOrname: any):boolean
18-
---@field __index peripheral
40+
---@field linkToTurtle fun(peripheral: PeripheralModule, turtle: TurtleMock):PeripheralModule
41+
---@field find fun(peripheral: PeripheralModule, typeName: string, filterFunc: filterFunc | nil):table | nil
42+
---@field getMethods fun(peripheral: PeripheralModule, name: any):string[] | nil
43+
---@field getNames fun(peripheral: PeripheralModule):string[]
44+
---@field isPresent fun(peripheral: PeripheralModule, positionOrname: any):boolean
45+
---@field getType fun(peripheral: PeripheralModule, Peripheral: peripheralActions):string|nil
46+
---@field __index PeripheralModule
1947

48+
--- Maps the relative positions of the turtle to the absolute positions of the emulator
2049
---@param turtle TurtleMock
21-
---@return position[]
22-
local function getPositions(turtle)
50+
---@return table<relativePosition, Vector>
51+
local function positionMapper(turtle)
52+
local vector = turtle.emulator.suit.vector
53+
return {
54+
["front"] = turtle.position + turtle.facing,
55+
["back"] = turtle.position - turtle.facing,
56+
["left"] = turtle.position - turtle.facing:cross(vector.new(0, 1, 0)),
57+
["right"] = turtle.position + turtle.facing:cross(vector.new(0, 1, 0)),
58+
["top"] = turtle.position + vector.new(0, 1, 0),
59+
["bottom"] = turtle.position + vector.new(0, -1, 0),
60+
}
61+
end
2362

24-
return pos
63+
---@param turtle TurtleMock
64+
---@return table<number, block>
65+
local function getNearbyPeripheralBlocks(turtle)
66+
local positions = {}
67+
for _, position in pairs(positionMapper(turtle)) do
68+
local block = turtle.emulator:getBlock(position)
69+
if block and block.peripheralActions then
70+
table.insert(positions, block)
71+
end
72+
end
73+
return positions
2574
end
2675

27-
---@type peripheral
76+
---@type PeripheralModule
2877
---@diagnostic disable-next-line: missing-fields
29-
local peripheral = {}
78+
local peripheralModule = {}
3079
---create a new instance of a peripheral and link it to a turtle
3180
---@param turtle TurtleMock
32-
---@return peripheral
33-
function peripheral:linkToTurtle(turtle)
34-
local mt = {
81+
---@return PeripheralModule
82+
function peripheralModule:linkToTurtle(turtle)
83+
assert(turtle, "Parameters: 1. self and 2. 'turtle' which must be of type TurtleMock")
84+
local _peripheralModule = {
3585
turtle = turtle,
3686
}
87+
setmetatable(_peripheralModule, {__index = peripheralModule})
88+
local mt = {}
3789
local proxy = {}
3890
mt.__index = function (_, key)
39-
local value = peripheral[key]
91+
local value = _peripheralModule[key]
4092
if type(value) == "function" then
4193
return function(...)
4294
local mightBeSelf = select(1, ...)
43-
if mightBeSelf == peripheral then
95+
if mightBeSelf == _peripheralModule then
4496
return value(...)
4597
elseif mightBeSelf == proxy then
4698
---@diagnostic disable-next-line: missing-parameter
47-
return value(peripheral, select(2, ...))
99+
return value(_peripheralModule, select(2, ...))
48100
end
49-
return value(peripheral, ...)
101+
return value(_peripheralModule, ...)
50102
end
51103
end
52104
return value
53105
end
54106
mt.__newindex = function (_, key, value)
55-
peripheral[key] = value
107+
_peripheralModule[key] = value
56108
end
57109

58110
setmetatable(proxy, mt)
59111
return proxy
60112

61113
end
62114

63-
---@param self peripheral
115+
---@param self PeripheralModule
64116
---@param typeName string
65117
---@param filterFunc filterFunc | nil
66-
---@return table
67-
function peripheral:find(typeName, filterFunc)
118+
---@return table | nil
119+
function peripheralModule:find(typeName, filterFunc)
68120
assert(self.turtle, "Peripheral is not linked to a turtle")
69-
local positions = getPositions(self.turtle.position)
121+
local positions = getNearbyPeripheralBlocks(self.turtle)
70122
local peripheral
71123
for _, position in pairs(positions) do
72-
local block = self.turtle.emulator:getBlock(position)
124+
local block = position
73125
if block and block.peripheralName and block.peripheralName == typeName then
74-
peripheral = self.turtle.emulator:playPeripheralProxy(block)
126+
print("Position: ", block.position)
127+
peripheral = self.turtle.emulator:playPeripheralProxy(block.position)
75128
if peripheral and ((not filterFunc) or filterFunc(block.item.name, peripheral)) then
76129
return peripheral
77130
end
@@ -82,7 +135,7 @@ end
82135
---returns all the functions of a peripheral with the given name
83136
---@param name any
84137
---@return string[] | nil
85-
function peripheral:getMethods(name)
138+
function peripheralModule:getMethods(name)
86139
local p = self:find(name)
87140
if not p then return nil end
88141
local methods = {}
@@ -95,11 +148,10 @@ function peripheral:getMethods(name)
95148
end
96149

97150
---@return string[]
98-
function peripheral:getNames()
151+
function peripheralModule:getNames()
99152
local names = {}
100-
local positions = getPositions(self.turtle.position)
101-
for _, position in pairs(positions) do
102-
local block = self.turtle.emulator:getBlock(position)
153+
local blocks = getNearbyPeripheralBlocks(self.turtle)
154+
for _, block in ipairs(blocks) do
103155
if block and block.peripheralName then
104156
if block.peripheralActions then
105157
table.insert(names, block.item.name)
@@ -112,8 +164,24 @@ end
112164
--- Checks if a peripheral is present at the given position or with a given name
113165
---@param positionOrname any
114166
---@return boolean
115-
function peripheral:isPresent(positionOrname)
167+
function peripheralModule:isPresent(positionOrname)
168+
assert(type(positionOrname) == "string", "Parameter: 'positionOrname' must be a string")
169+
if relativePositionOptions[positionOrname] ~= nil then
170+
local position = positionMapper(self.turtle)[positionOrname]
171+
local block = self.turtle.emulator:getBlock(position)
172+
return (block ~= nil) and (block.peripheralActions ~= nil)
173+
else
174+
local peripheral = self:find(positionOrname)
175+
return peripheral ~= nil
176+
end
177+
end
178+
116179

180+
---Gets the type of the peripheral at the given position
181+
---@param peripheralActions peripheralActions
182+
---@return string|nil
183+
function peripheralModule:getType(peripheralActions)
184+
return peripheralActions:getType();
117185
end
118186

119-
return peripheral
187+
return peripheralModule

0 commit comments

Comments
 (0)