Skip to content

Commit b6ceaa3

Browse files
committed
feat: 📦 added modem(gps)
1 parent 5c90511 commit b6ceaa3

File tree

6 files changed

+102
-8
lines changed

6 files changed

+102
-8
lines changed

peripherals/geoScanner.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
local class = require("ccClass")
33
---@class GeoScanner : PeripheralActions
44
local geoScanner = class(function (a, turtle, scanResult)
5-
a.turtle = turtle
5+
a.computer = turtle
66
a.scanResult = scanResult or {}
77
end)
88

99
---@type ScanDataTable
1010
geoScanner.scanResult = nil
1111
---@type TurtleMock | nil
12-
geoScanner.turtle = nil
12+
geoScanner.computer = nil
1313
geoScanner.scanEmulator = false
1414

1515
---comment
@@ -21,13 +21,13 @@ function geoScanner:scan(radius)
2121
return self.scanResult
2222
end
2323
local result = {}
24-
assert(self.turtle, "No turtle found, needs to be provided in the constructor via playPeripheralProxy as the third parameter")
25-
local e = self.turtle.emulator
24+
assert(self.computer, "No turtle found, needs to be provided in the constructor via playPeripheralProxy as the third parameter")
25+
local e = self.computer.emulator
2626
assert(e, "No emulator found")
2727
for positionString, block in pairs(e.blocks) do
2828
--- check if the block is in the radius x of the turtle
29-
local length = (block.position - self.turtle.position).length
30-
if (block.position - self.turtle.position):length() <= radius then
29+
local length = (block.position - self.computer.position).length
30+
if (block.position - self.computer.position):length() <= radius then
3131
table.insert(result, {name = block.item.name
3232
,x = block.position.x
3333
,y = block.position.y
@@ -37,8 +37,8 @@ function geoScanner:scan(radius)
3737
end
3838
end
3939
for id, turtle in ipairs(e.turtles) do
40-
if turtle.id ~= self.turtle.id then
41-
local length = (turtle.position - self.turtle.position).length
40+
if turtle.id ~= self.computer.id then
41+
local length = (turtle.position - self.computer.position).length
4242
if length <= radius then
4343
table.insert(result, {name = "computercraft:turtle_advanced"
4444
,x = turtle.position.x

peripherals/modem.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---@type ccClass
2+
local class = require("ccClass")
3+
---@class Modem : PeripheralActions
4+
local modem = class(function (a, turtle)
5+
a.computer = turtle
6+
end)
7+
8+
modem.computer = nil
9+
10+
function modem:accessValid(key, item)
11+
local computer = self.computer
12+
assert(computer, "No not linked to a turtle (turtle.getPeripheralModule) !")
13+
---@cast computer TurtleProxy
14+
local succ, valid = pcall(function ()
15+
return (computer.equipslots.left.name == item.name or computer.equipslots.right.name == item.name) and self.isWrapped
16+
end)
17+
return succ and valid
18+
end
19+
20+
function modem:getType()
21+
return "modem"
22+
end
23+
24+
function modem:locate()
25+
assert(self.computer, "No not linked to a turtle (turtle.getPeripheralModule) !")
26+
return
27+
self.computer.position.x, self.computer.position.y, self.computer.position.z
28+
end
29+
30+
return modem

peripherals/peripheral.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ to simulate the normal behavior of a turtle interacting with a peripheral.
2929

3030
---@class PeripheralActions
3131
---@field getType fun():string
32+
---@field accessValid nil | fun(self: PeripheralActions, key: string, item: Item):boolean, string
33+
---@field isWrapped boolean | nil
3234

3335
---@class PeripheralModule
3436
---@field type string
@@ -145,6 +147,7 @@ function peripheralModule:find(typeName, filterFunc)
145147

146148
peripheral = self.computer.emulator:playPeripheralProxy(item)
147149
if peripheral and ((not filterFunc) or filterFunc(item.name, peripheral)) then
150+
peripheral.isWrapped = true
148151
return peripheral
149152
end
150153
end
@@ -202,6 +205,7 @@ function peripheralModule:wrap(position)
202205
local positionOfItems = getNearbyPeripheralItems(self)
203206
if positionOfItems[position] ~= nil then
204207
local peripheral = self.computer.emulator:playPeripheralProxy(positionOfItems[position])
208+
peripheral.isWrapped = true
205209
return peripheral
206210
end
207211
end

tests/test_spec.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ require(package)
4040
-- load the other suits
4141
local vector = require("vector")
4242
local geoScanner = require("geoScanner")
43+
local modemClass = require("modem")
4344
local chestInventory = require("chestInventory")
4445

46+
4547
local turtleEmulator = require("turtleEmulator")
4648
describe("Disabled Movement", function()
4749
local turtle
@@ -942,3 +944,43 @@ describe("peripherals", function()
942944
end)
943945
end)
944946
end)
947+
describe("GPS", function()
948+
---@type TurtleProxy
949+
local turtle
950+
local gpsItem
951+
local gps
952+
local modem
953+
local peripheral
954+
955+
before_each(function()
956+
turtleEmulator:clearBlocks()
957+
turtleEmulator:clearTurtles()
958+
turtle = turtleEmulator:createTurtle()
959+
gpsItem = { name = "computercraft:advanced_modem", count = 1, equipable = true }
960+
turtle.addItemToInventory(gpsItem, 1)
961+
turtle.addItemToInventory({ name = "minecraft:coal", count = 64, fuelgain = 8 }, 2)
962+
modem = turtleEmulator:addPeripheralToItem(gpsItem, modemClass, turtle)
963+
gps = modem
964+
peripheral = turtle.getPeripheralModule()
965+
end)
966+
it("should fail", function()
967+
local falsy = modem.locate()
968+
assert.is.falsy(falsy)
969+
end)
970+
it("should work", function()
971+
assert.is_true(turtle.equipLeft())
972+
assert.are.equal("computercraft:advanced_modem", turtle.equipslots.left.name)
973+
assert.is.truthy(peripheral.find("modem"))
974+
turtle.select(2)
975+
turtle.refuel(10)
976+
local x, y, z = gps.locate()
977+
assert.are.equal(0, x)
978+
assert.are.equal(0, y)
979+
assert.are.equal(0, z)
980+
turtle.forward()
981+
x, y, z = gps.locate()
982+
assert.are.equal(1, x)
983+
assert.are.equal(0, y)
984+
assert.are.equal(0, z)
985+
end)
986+
end)

turtleEmulator.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ function turtleEmulator:playPeripheralProxy(item)
187187
if value == nil then
188188
error("no field found for key: " .. item.peripheralName .. "." .. key)
189189
end
190+
local callerInfo = debug.getinfo(2, "f")
191+
if not (callerInfo and callerInfo.func == action and key ~= "accessValid") then
192+
local valid = action.accessValid
193+
if valid and not valid(action, key, item) then
194+
if type(value) == "function" then
195+
return function() end
196+
end
197+
return nil
198+
end
199+
end
200+
-- error("passed")
190201
if type(value) == "function" then
191202
return function(...)
192203
local mightBeSelf = select(1, ...)
@@ -226,5 +237,7 @@ function turtleEmulator:turtleDrop(position, item)
226237
itemFallsDown(position, item)
227238
return true
228239
end
240+
241+
229242
---set vector-lib for the turtleEmulator
230243
return turtleEmulator

turtleMock.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
---@alias inspectResult {name: string, tags: table<string, any> | nil, state: table<string, any> | nil} | nil
1414

15+
---@class GPSModule
16+
---@field locate fun():number, number, number
17+
1518
---@class TurtleMock
1619
---@field position Vector | nil
1720
---@field facing Vector | nil
@@ -59,6 +62,7 @@
5962
---@field dropDown fun(count: integer):boolean
6063
---@field dropUp fun(count: integer):boolean
6164
---@field drop fun(count: integer):boolean
65+
---@field getGPSModule fun():table
6266
---@field print fun(...: any):nil only exists for testing purposes
6367
---@field getPeripheralModule fun():PeripheralModule only exists for testing purposes
6468
---@field addItemToInventory fun(item: Item, slot: number | nil):boolean only exists for testing purposes
@@ -702,6 +706,7 @@ end
702706
function turtleMock:drop(count)
703707
return drop(self, self.position + self.facing, count)
704708
end
709+
705710
---will only print content if canPrint is set to true
706711
---@param ... any
707712
---@return nil

0 commit comments

Comments
 (0)