mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-12-07 06:48:05 +00:00
Merge branch 'mc-1.20.x' into mc-1.20.y
This commit is contained in:
@@ -143,27 +143,27 @@ public class OSAPI implements ILuaAPI {
|
||||
|
||||
/**
|
||||
* Starts a timer that will run for the specified number of seconds. Once
|
||||
* the timer fires, a {@code timer} event will be added to the queue with
|
||||
* the ID returned from this function as the first parameter.
|
||||
* the timer fires, a [`timer`] event will be added to the queue with the ID
|
||||
* returned from this function as the first parameter.
|
||||
* <p>
|
||||
* As with [sleep][`os.sleep`], {@code timer} will automatically be rounded up
|
||||
* to the nearest multiple of 0.05 seconds, as it waits for a fixed amount
|
||||
* of world ticks.
|
||||
* As with [sleep][`os.sleep`], the time will automatically be rounded up to
|
||||
* the nearest multiple of 0.05 seconds, as it waits for a fixed amount of
|
||||
* world ticks.
|
||||
*
|
||||
* @param timer The number of seconds until the timer fires.
|
||||
* @return The ID of the new timer. This can be used to filter the
|
||||
* {@code timer} event, or {@link #cancelTimer cancel the timer}.
|
||||
* @param time The number of seconds until the timer fires.
|
||||
* @return The ID of the new timer. This can be used to filter the [`timer`]
|
||||
* event, or {@linkplain #cancelTimer cancel the timer}.
|
||||
* @throws LuaException If the time is below zero.
|
||||
* @see #cancelTimer To cancel a timer.
|
||||
*/
|
||||
@LuaFunction
|
||||
public final int startTimer(double timer) throws LuaException {
|
||||
return apiEnvironment.startTimer(Math.round(checkFinite(0, timer) / 0.05));
|
||||
public final int startTimer(double time) throws LuaException {
|
||||
return apiEnvironment.startTimer(Math.round(checkFinite(0, time) / 0.05));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels a timer previously started with startTimer. This will stop the
|
||||
* timer from firing.
|
||||
* Cancels a timer previously started with {@link #startTimer(double)}. This
|
||||
* will stop the timer from firing.
|
||||
*
|
||||
* @param token The ID of the timer to cancel.
|
||||
* @cc.since 1.6
|
||||
@@ -399,10 +399,9 @@ public class OSAPI implements ILuaAPI {
|
||||
* Returns a date string (or table) using a specified format string and
|
||||
* optional time to format.
|
||||
* <p>
|
||||
* The format string takes the same formats as C's {@code strftime} function
|
||||
* (http://www.cplusplus.com/reference/ctime/strftime/). In extension, it
|
||||
* can be prefixed with an exclamation mark ({@code !}) to use UTC time
|
||||
* instead of the server's local timezone.
|
||||
* The format string takes the same formats as C's [strftime](http://www.cplusplus.com/reference/ctime/strftime/)
|
||||
* function. The format string can also be prefixed with an exclamation mark
|
||||
* ({@code !}) to use UTC time instead of the server's local timezone.
|
||||
* <p>
|
||||
* If the format is exactly {@code *t} (optionally prefixed with {@code !}), a
|
||||
* table will be returned instead. This table has fields for the year, month,
|
||||
|
||||
@@ -13,6 +13,11 @@
|
||||
-- @module vector
|
||||
-- @since 1.31
|
||||
|
||||
local getmetatable = getmetatable
|
||||
local expect = dofile("rom/modules/main/cc/expect.lua").expect
|
||||
|
||||
local vmetatable
|
||||
|
||||
--- A 3-dimensional vector, with `x`, `y`, and `z` values.
|
||||
--
|
||||
-- This is suitable for representing both position and directional vectors.
|
||||
@@ -27,6 +32,9 @@ local vector = {
|
||||
-- @usage v1:add(v2)
|
||||
-- @usage v1 + v2
|
||||
add = function(self, o)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
if getmetatable(o) ~= vmetatable then expect(2, o, "vector") end
|
||||
|
||||
return vector.new(
|
||||
self.x + o.x,
|
||||
self.y + o.y,
|
||||
@@ -42,6 +50,9 @@ local vector = {
|
||||
-- @usage v1:sub(v2)
|
||||
-- @usage v1 - v2
|
||||
sub = function(self, o)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
if getmetatable(o) ~= vmetatable then expect(2, o, "vector") end
|
||||
|
||||
return vector.new(
|
||||
self.x - o.x,
|
||||
self.y - o.y,
|
||||
@@ -52,30 +63,36 @@ local vector = {
|
||||
--- Multiplies a vector by a scalar value.
|
||||
--
|
||||
-- @tparam Vector self The vector to multiply.
|
||||
-- @tparam number m The scalar value to multiply with.
|
||||
-- @tparam number factor The scalar value to multiply with.
|
||||
-- @treturn Vector A vector with value `(x * m, y * m, z * m)`.
|
||||
-- @usage v:mul(3)
|
||||
-- @usage v * 3
|
||||
mul = function(self, m)
|
||||
-- @usage vector.new(1, 2, 3):mul(3)
|
||||
-- @usage vector.new(1, 2, 3) * 3
|
||||
mul = function(self, factor)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
expect(2, factor, "number")
|
||||
|
||||
return vector.new(
|
||||
self.x * m,
|
||||
self.y * m,
|
||||
self.z * m
|
||||
self.x * factor,
|
||||
self.y * factor,
|
||||
self.z * factor
|
||||
)
|
||||
end,
|
||||
|
||||
--- Divides a vector by a scalar value.
|
||||
--
|
||||
-- @tparam Vector self The vector to divide.
|
||||
-- @tparam number m The scalar value to divide by.
|
||||
-- @tparam number factor The scalar value to divide by.
|
||||
-- @treturn Vector A vector with value `(x / m, y / m, z / m)`.
|
||||
-- @usage v:div(3)
|
||||
-- @usage v / 3
|
||||
div = function(self, m)
|
||||
-- @usage vector.new(1, 2, 3):div(3)
|
||||
-- @usage vector.new(1, 2, 3) / 3
|
||||
div = function(self, factor)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
expect(2, factor, "number")
|
||||
|
||||
return vector.new(
|
||||
self.x / m,
|
||||
self.y / m,
|
||||
self.z / m
|
||||
self.x / factor,
|
||||
self.y / factor,
|
||||
self.z / factor
|
||||
)
|
||||
end,
|
||||
|
||||
@@ -83,8 +100,9 @@ local vector = {
|
||||
--
|
||||
-- @tparam Vector self The vector to negate.
|
||||
-- @treturn Vector The negated vector.
|
||||
-- @usage -v
|
||||
-- @usage -vector.new(1, 2, 3)
|
||||
unm = function(self)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
return vector.new(
|
||||
-self.x,
|
||||
-self.y,
|
||||
@@ -99,6 +117,9 @@ local vector = {
|
||||
-- @treturn Vector The dot product of `self` and `o`.
|
||||
-- @usage v1:dot(v2)
|
||||
dot = function(self, o)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
if getmetatable(o) ~= vmetatable then expect(2, o, "vector") end
|
||||
|
||||
return self.x * o.x + self.y * o.y + self.z * o.z
|
||||
end,
|
||||
|
||||
@@ -109,6 +130,9 @@ local vector = {
|
||||
-- @treturn Vector The cross product of `self` and `o`.
|
||||
-- @usage v1:cross(v2)
|
||||
cross = function(self, o)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
if getmetatable(o) ~= vmetatable then expect(2, o, "vector") end
|
||||
|
||||
return vector.new(
|
||||
self.y * o.z - self.z * o.y,
|
||||
self.z * o.x - self.x * o.z,
|
||||
@@ -120,6 +144,7 @@ local vector = {
|
||||
-- @tparam Vector self This vector.
|
||||
-- @treturn number The length of this vector.
|
||||
length = function(self)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
|
||||
end,
|
||||
|
||||
@@ -141,6 +166,9 @@ local vector = {
|
||||
-- nearest 0.5.
|
||||
-- @treturn Vector The rounded vector.
|
||||
round = function(self, tolerance)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
expect(2, tolerance, "number", "nil")
|
||||
|
||||
tolerance = tolerance or 1.0
|
||||
return vector.new(
|
||||
math.floor((self.x + tolerance * 0.5) / tolerance) * tolerance,
|
||||
@@ -156,6 +184,8 @@ local vector = {
|
||||
-- @usage v:tostring()
|
||||
-- @usage tostring(v)
|
||||
tostring = function(self)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
|
||||
return self.x .. "," .. self.y .. "," .. self.z
|
||||
end,
|
||||
|
||||
@@ -165,11 +195,15 @@ local vector = {
|
||||
-- @tparam Vector other The second vector to compare to.
|
||||
-- @treturn boolean Whether or not the vectors are equal.
|
||||
equals = function(self, other)
|
||||
if getmetatable(self) ~= vmetatable then expect(1, self, "vector") end
|
||||
if getmetatable(other) ~= vmetatable then expect(2, other, "vector") end
|
||||
|
||||
return self.x == other.x and self.y == other.y and self.z == other.z
|
||||
end,
|
||||
}
|
||||
|
||||
local vmetatable = {
|
||||
vmetatable = {
|
||||
__name = "vector",
|
||||
__index = vector,
|
||||
__add = vector.add,
|
||||
__sub = vector.sub,
|
||||
|
||||
@@ -1,3 +1,22 @@
|
||||
# New features in CC: Tweaked 1.110.2
|
||||
|
||||
* Add `speaker sound` command (fatboychummy).
|
||||
|
||||
Several bug fixes:
|
||||
* Improve error when calling `speaker play` with no path (fatboychummy).
|
||||
* Prevent playing music discs with `speaker.playSound`.
|
||||
* Various documentation fixes (cyberbit).
|
||||
* Fix generic peripherals not being able to transfer to some inventories on Forge.
|
||||
* Fix rare crash when holding a pocket computer.
|
||||
* Fix modems breaking when moved by Create.
|
||||
* Fix crash when rendering a turtle through an Immersive Portals portal.
|
||||
|
||||
# New features in CC: Tweaked 1.110.1
|
||||
|
||||
Several bug fixes:
|
||||
* Fix computers not turning on after they're unloaded/not-ticked for a while.
|
||||
* Fix networking cables sometimes not connecting on Forge.
|
||||
|
||||
# New features in CC: Tweaked 1.110.0
|
||||
|
||||
* Add a new `@c[...]` syntax for selecting computers in the `/computercraft` command.
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
New features in CC: Tweaked 1.110.0
|
||||
New features in CC: Tweaked 1.110.2
|
||||
|
||||
* Add a new `@c[...]` syntax for selecting computers in the `/computercraft` command.
|
||||
* Remove custom breaking progress of modems on Forge.
|
||||
* Add `speaker sound` command (fatboychummy).
|
||||
|
||||
Several bug fixes:
|
||||
* Fix client and server DFPWM transcoders getting out of sync.
|
||||
* Fix `turtle.suck` reporting incorrect error when failing to suck items.
|
||||
* Fix pocket computers displaying state (blinking, modem light) for the wrong computer.
|
||||
* Fix crash when wrapping an invalid BE as a generic peripheral.
|
||||
* Chest peripherals now reattach when a chest is converted into a double chest.
|
||||
* Fix `speaker` program not resolving files relative to the current directory.
|
||||
* Skip main-thread tasks if the peripheral is detached.
|
||||
* Fix internal Lua VM errors if yielding inside `__tostring`.
|
||||
* Improve error when calling `speaker play` with no path (fatboychummy).
|
||||
* Prevent playing music discs with `speaker.playSound`.
|
||||
* Various documentation fixes (cyberbit).
|
||||
* Fix generic peripherals not being able to transfer to some inventories on Forge.
|
||||
* Fix rare crash when holding a pocket computer.
|
||||
* Fix modems breaking when moved by Create.
|
||||
* Fix crash when rendering a turtle through an Immersive Portals portal.
|
||||
|
||||
Type "help changelog" to see the full version history.
|
||||
|
||||
@@ -89,7 +89,7 @@ end
|
||||
--
|
||||
-- @tparam table image An image, as returned from [`load`] or [`parse`].
|
||||
-- @tparam number xPos The x position to start drawing at.
|
||||
-- @tparam number xPos The y position to start drawing at.
|
||||
-- @tparam number yPos The y position to start drawing at.
|
||||
-- @tparam[opt] term.Redirect target The terminal redirect to draw to. Defaults to the
|
||||
-- current terminal.
|
||||
local function draw(image, xPos, yPos, target)
|
||||
|
||||
@@ -43,6 +43,10 @@ if cmd == "stop" then
|
||||
for _, speaker in pairs(get_speakers(name)) do speaker.stop() end
|
||||
elseif cmd == "play" then
|
||||
local _, file, name = ...
|
||||
if not file then
|
||||
error("Usage: speaker play <file or url> [speaker]", 0)
|
||||
end
|
||||
|
||||
local speaker = get_speakers(name)[1]
|
||||
|
||||
local handle, err
|
||||
@@ -128,9 +132,47 @@ elseif cmd == "play" then
|
||||
end
|
||||
|
||||
handle.close()
|
||||
elseif cmd == "sound" then
|
||||
local _, sound, volume, pitch, name = ...
|
||||
|
||||
if not sound then
|
||||
error("Usage: speaker sound <sound> [volume] [pitch] [speaker]", 0)
|
||||
return
|
||||
end
|
||||
|
||||
if volume then
|
||||
volume = tonumber(volume)
|
||||
if not volume then
|
||||
error("Volume must be a number", 0)
|
||||
end
|
||||
if volume < 0 or volume > 3 then
|
||||
error("Volume must be between 0 and 3", 0)
|
||||
end
|
||||
end
|
||||
|
||||
if pitch then
|
||||
pitch = tonumber(pitch)
|
||||
if not pitch then
|
||||
error("Pitch must be a number", 0)
|
||||
end
|
||||
if pitch < 0 or pitch > 2 then
|
||||
error("Pitch must be between 0 and 2", 0)
|
||||
end
|
||||
end
|
||||
|
||||
local speaker = get_speakers(name)[1]
|
||||
|
||||
if speaker.playSound(sound, volume, pitch) then
|
||||
print(("Played sound %q on speaker %q with volume %s and pitch %s."):format(
|
||||
sound, peripheral.getName(speaker), volume or 1, pitch or 1
|
||||
))
|
||||
else
|
||||
error(("Could not play sound %q"):format(sound), 0)
|
||||
end
|
||||
else
|
||||
local programName = arg[0] or fs.getName(shell.getRunningProgram())
|
||||
print("Usage:")
|
||||
print(programName .. " play <file or url> [speaker]")
|
||||
print(programName .. " sound <sound> [volume] [pitch] [speaker]")
|
||||
print(programName .. " stop [speaker]")
|
||||
end
|
||||
|
||||
@@ -117,7 +117,7 @@ shell.setCompletionFunction("rom/programs/fun/dj.lua", completion.build(
|
||||
completion.peripheral
|
||||
))
|
||||
shell.setCompletionFunction("rom/programs/fun/speaker.lua", completion.build(
|
||||
{ completion.choice, { "play ", "stop " } },
|
||||
{ completion.choice, { "play ", "sound ", "stop " } },
|
||||
function(shell, text, previous)
|
||||
if previous[2] == "play" then return completion.file(shell, text, previous, true)
|
||||
elseif previous[2] == "stop" then return completion.peripheral(shell, text, previous, false)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
-- SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers
|
||||
--
|
||||
-- SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
describe("The vector library", function()
|
||||
local vec = vector.new(1, 2, 3)
|
||||
|
||||
describe("vector.add", function()
|
||||
it("validates arguments", function()
|
||||
expect.error(vec.add, nil, vec):eq("bad argument #1 (vector expected, got nil)")
|
||||
expect.error(vec.add, vec, nil):eq("bad argument #2 (vector expected, got nil)")
|
||||
end)
|
||||
|
||||
it("returns the correct value", function()
|
||||
expect(vector.new(1, 2, 3) + vector.new(6, 4, 2)):eq(vector.new(7, 6, 5))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("vector.sub", function()
|
||||
it("validates arguments", function()
|
||||
expect.error(vec.sub, nil, vec):eq("bad argument #1 (vector expected, got nil)")
|
||||
expect.error(vec.sub, vec, nil):eq("bad argument #2 (vector expected, got nil)")
|
||||
end)
|
||||
|
||||
it("returns the correct value", function()
|
||||
expect(vector.new(6, 4, 2) - vector.new(1, 2, 3)):eq(vector.new(5, 2, -1))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("vector.mul", function()
|
||||
it("validates arguments", function()
|
||||
expect.error(vec.mul, nil, vec):eq("bad argument #1 (vector expected, got nil)")
|
||||
expect.error(vec.mul, vec, nil):eq("bad argument #2 (number expected, got nil)")
|
||||
end)
|
||||
|
||||
it("returns the correct value", function()
|
||||
expect(vector.new(1, 2, 3) * 2):eq(vector.new(2, 4, 6))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("vector.div", function()
|
||||
it("validates arguments", function()
|
||||
expect.error(vec.div, nil, vec):eq("bad argument #1 (vector expected, got nil)")
|
||||
expect.error(vec.div, vec, nil):eq("bad argument #2 (number expected, got nil)")
|
||||
end)
|
||||
|
||||
it("returns the correct value", function()
|
||||
expect(vector.new(1, 2, 3) / 2):eq(vector.new(0.5, 1, 1.5))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("vector.unm", function()
|
||||
it("validates arguments", function()
|
||||
expect.error(vec.unm, nil):eq("bad argument #1 (vector expected, got nil)")
|
||||
end)
|
||||
|
||||
it("returns the correct value", function()
|
||||
expect(-vector.new(2, 3, 6)):eq(vector.new(-2, -3, -6))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("vector.length", function()
|
||||
it("validates arguments", function()
|
||||
expect.error(vec.length, nil):eq("bad argument #1 (vector expected, got nil)")
|
||||
end)
|
||||
|
||||
it("returns the correct value", function()
|
||||
expect(vector.new(2, 3, 6):length()):eq(7)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
Reference in New Issue
Block a user