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
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
2574end
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
61113end
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
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)
95148end
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 )
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 ();
117185end
118186
119- return peripheral
187+ return peripheralModule
0 commit comments