diff --git a/src/main/java/cc/tweaked/patch/ClassTransformer.java b/src/main/java/cc/tweaked/patch/ClassTransformer.java index d38b14ef2..eb131db5e 100644 --- a/src/main/java/cc/tweaked/patch/ClassTransformer.java +++ b/src/main/java/cc/tweaked/patch/ClassTransformer.java @@ -38,6 +38,11 @@ public class ClassTransformer implements IClassTransformer { "dan200.computer.shared.TileEntityMonitor", "cc.tweaked.patch.mixins.TileEntityMonitorMixin" )) + // And extend the turtle API + .atClass("dan200.turtle.shared.TurtleAPI", new ClassMerger( + "dan200.turtle.shared.TurtleAPI", + "cc.tweaked.patch.mixins.TurtleAPIMixin" + )) // Load from our ROM instead of the CC one. We do this by: // 1. Changing the path of the assets folder. .atMethod( diff --git a/src/main/java/cc/tweaked/patch/mixins/TurtleAPIMixin.java b/src/main/java/cc/tweaked/patch/mixins/TurtleAPIMixin.java new file mode 100644 index 000000000..5513b8ad8 --- /dev/null +++ b/src/main/java/cc/tweaked/patch/mixins/TurtleAPIMixin.java @@ -0,0 +1,45 @@ +// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission. +// +// SPDX-License-Identifier: LicenseRef-CCPL + +package cc.tweaked.patch.mixins; + +import cc.tweaked.patch.framework.transform.MergeVisitor; +import dan200.CCTurtle; +import dan200.computer.core.ILuaAPI; +import dan200.computercraft.api.lua.LuaFunction; +import dan200.turtle.shared.ITurtle; + +/** + * Adds additional methods to {@link dan200.turtle.shared.TurtleAPI}. + */ +public abstract class TurtleAPIMixin implements ILuaAPI { + @MergeVisitor.Shadow + private ITurtle m_turtle; + + /** + * Get the currently selected slot. + * + * @return The current slot. + * @cc.since 1.6 + */ + @LuaFunction + public final int getSelectedSlot() { + return m_turtle.getSelectedSlot() + 1; + } + + /** + * Get the maximum amount of fuel this turtle can hold. + *
+ * By default, normal turtles have a limit of 20,000 and advanced turtles of 100,000.
+ *
+ * @return The limit, or "unlimited".
+ * @cc.treturn [1] number The maximum amount of fuel a turtle can hold.
+ * @cc.treturn [2] "unlimited" If turtles do not consume fuel when moving.
+ * @cc.since 1.6
+ */
+ @LuaFunction
+ public final Object getFuelLimit() {
+ return CCTurtle.turtlesNeedFuel ? Integer.MAX_VALUE : "unlimited";
+ }
+}
diff --git a/src/main/resources/assets/cctweaked/lua/rom/apis/peripheral.lua b/src/main/resources/assets/cctweaked/lua/rom/apis/peripheral.lua
index 7515a3578..07f492b55 100644
--- a/src/main/resources/assets/cctweaked/lua/rom/apis/peripheral.lua
+++ b/src/main/resources/assets/cctweaked/lua/rom/apis/peripheral.lua
@@ -100,7 +100,7 @@ local expect = dofile("rom/modules/main/cc/expect.lua").expect
local native = peripheral
-- Stub in peripheral.hasType
-function native.hasType(p, type) return peripheral.getType(p) == type end
+function native.hasType(p, ty) return native.getType(p) == ty end
local sides = rs.getSides()
diff --git a/src/main/resources/assets/cctweaked/lua/rom/apis/turtle/turtle.lua b/src/main/resources/assets/cctweaked/lua/rom/apis/turtle/turtle.lua
index 5a19e15a7..d6a33c81b 100644
--- a/src/main/resources/assets/cctweaked/lua/rom/apis/turtle/turtle.lua
+++ b/src/main/resources/assets/cctweaked/lua/rom/apis/turtle/turtle.lua
@@ -14,31 +14,53 @@ end
-- should not need to use it.
native = turtle.native or turtle
-local function addCraftMethod(object)
- if peripheral.getType("left") == "workbench" then
- object.craft = function(...)
- return peripheral.call("left", "craft", ...)
+local function waitForResponse(_id)
+ local event, responseID, success
+ while event ~= "turtle_response" or responseID ~= _id do
+ event, responseID, success = os.pullEvent("turtle_response")
+ end
+ return success
+end
+
+local function wrap(_sCommand)
+ return function(...)
+ local id = native[_sCommand](...)
+ if id == -1 then
+ return false
end
- elseif peripheral.getType("right") == "workbench" then
- object.craft = function(...)
- return peripheral.call("right", "craft", ...)
+ return waitForResponse(id)
+ end
+end
+
+-- Wrap standard commands
+local turtle = {}
+turtle["getItemCount"] = native.getItemCount
+turtle["getItemSpace"] = native.getItemSpace
+turtle["getFuelLevel"] = native.getFuelLevel
+turtle["getSelectedSlot"] = native.getSelectedSlot
+turtle["getFuelLimit"] = native.getFuelLimit
+
+for k,v in pairs(native) do
+ if type(k) == "string" and type(v) == "function" then
+ if turtle[k] == nil then
+ turtle[k] = wrap(k)
end
- else
- object.craft = nil
+ end
+end
+
+-- Wrap peripheral commands
+if peripheral.getType("left") == "workbench" then
+ turtle["craft"] = function(...)
+ local id = peripheral.call("left", "craft", ...)
+ return waitForResponse(id)
+ end
+elseif peripheral.getType("right") == "workbench" then
+ turtle["craft"] = function(...)
+ local id = peripheral.call("right", "craft", ...)
+ return waitForResponse(id)
end
end
-- Put commands into environment table
local env = _ENV
-for k, v in pairs(native) do
- if k == "equipLeft" or k == "equipRight" then
- env[k] = function(...)
- local result, err = v(...)
- addCraftMethod(turtle)
- return result, err
- end
- else
- env[k] = v
- end
-end
-addCraftMethod(env)
+for k,v in pairs(turtle) do env[k] = v end
diff --git a/src/main/resources/assets/cctweaked/lua/rom/programs/redprobe.lua b/src/main/resources/assets/cctweaked/lua/rom/programs/redprobe.lua
new file mode 100644
index 000000000..3d1374bd4
--- /dev/null
+++ b/src/main/resources/assets/cctweaked/lua/rom/programs/redprobe.lua
@@ -0,0 +1,57 @@
+-- Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
+--
+-- SPDX-License-Identifier: LicenseRef-CCPL
+
+-- Regular input
+print("Redstone inputs: ")
+
+local count = 0
+local bundledCount = 0
+for n,sSide in ipairs(redstone.getSides()) do
+ if redstone.getBundledInput(sSide) > 0 then
+ bundledCount = bundledCount + 1
+ end
+ if redstone.getInput(sSide) then
+ if count > 0 then
+ io.write(", ")
+ end
+ io.write(sSide)
+ count = count + 1
+ end
+end
+
+if count > 0 then
+ print(".")
+else
+ print("None.")
+end
+
+-- Bundled input
+if bundledCount > 0 then
+ print()
+ print("Bundled inputs:")
+ for i,sSide in ipairs(redstone.getSides()) do
+ if redstone.getBundledInput(sSide) > 0 then
+ write(sSide.." = "..redstone.getBundledInput(sSide).." (")
+
+ local count = 0
+ for sColour,nColour in pairs(colors) do
+ if type(nColour) == "number" and
+ redstone.testBundledInput(sSide, nColour) then
+ if count > 0 then
+ write(" + ")
+ end
+ if term.isColour() then
+ term.setTextColour(nColour)
+ end
+ write(sColour)
+ if term.isColour() then
+ term.setTextColour(colours.white)
+ end
+ count = count + 1
+ end
+ end
+ print(")")
+ end
+ end
+end
diff --git a/src/main/resources/assets/cctweaked/lua/rom/programs/redpulse.lua b/src/main/resources/assets/cctweaked/lua/rom/programs/redpulse.lua
new file mode 100644
index 000000000..f5e7505b3
--- /dev/null
+++ b/src/main/resources/assets/cctweaked/lua/rom/programs/redpulse.lua
@@ -0,0 +1,20 @@
+-- Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
+--
+-- SPDX-License-Identifier: LicenseRef-CCPL
+
+local tArgs = { ... }
+local sSide = tArgs[1]
+if not sSide then
+ print("Usage: redpulse