1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-22 05:03:22 +00:00

Rewrite several doc introductions

Mostly focussing on rednet and modem here. Not sure if I made them any
better, we'll see!
This commit is contained in:
Jonathan Coates 2021-12-21 00:22:16 +00:00
parent 5eedea1bbb
commit 932b77d7ee
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
10 changed files with 276 additions and 136 deletions

View File

@ -2,7 +2,7 @@
module: [kind=event] modem_message
---
The @{modem_message} event is fired when a message is received on an open channel on any modem.
The @{modem_message} event is fired when a message is received on an open channel on any @{modem}.
## Return Values
1. @{string}: The event name.
@ -10,11 +10,15 @@ ## Return Values
3. @{number}: The channel that the message was sent on.
4. @{number}: The reply channel set by the sender.
5. @{any}: The message as sent by the sender.
6. @{number}: The distance between the sender and the receiver, in blocks (decimal).
6. @{number}: The distance between the sender and the receiver, in blocks.
## Example
Prints a message when one is sent:
Wraps a @{modem} peripheral, opens channel 0 for listening, and prints all received messages.
```lua
local modem = peripheral.find("modem") or error("No modem attached", 0)
modem.open(0)
while true do
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
print(("Message received on side %s on channel %d (reply to %d) from %f blocks away with message %s"):format(side, channel, replyChannel, distance, tostring(message)))

View File

@ -12,16 +12,19 @@ rounded up to the nearest multiple of 0.05 seconds. If you are using coroutines
or the @{parallel|parallel API}, it will only pause execution of the current
thread, not the whole program.
**Note** Because sleep internally uses timers, it is a function that yields.
This means that you can use it to prevent "Too long without yielding" errors,
however, as the minimum sleep time is 0.05 seconds, it will slow your program
down.
:::tip
Because sleep internally uses timers, it is a function that yields. This means
that you can use it to prevent "Too long without yielding" errors, however, as
the minimum sleep time is 0.05 seconds, it will slow your program down.
:::
**Warning** Internally, this function queues and waits for a timer event (using
:::caution
Internally, this function queues and waits for a timer event (using
@{os.startTimer}), however it does not listen for any other events. This means
that any event that occurs while sleeping will be entirely discarded. If you
need to receive events while sleeping, consider using @{os.startTimer|timers},
or the @{parallel|parallel API}.
:::
@tparam number time The number of seconds to sleep for, rounded up to the
nearest multiple of 0.05.

View File

@ -19,10 +19,10 @@
*
* This works with energy storage blocks, as well as generators and machines which consume energy.
*
* <blockquote>
* <strong>Note:</strong> Due to limitations with Forge's energy API, it is not possible to measure throughput (i.e. RF
* :::note
* Due to limitations with Forge's energy API, it is not possible to measure throughput (i.e. RF
* used/generated per tick).
* </blockquote>
* :::
*
* @cc.module energy_storage
*/

View File

@ -21,9 +21,60 @@
import java.util.Set;
/**
* The modem peripheral allows you to send messages between computers.
* Modems allow you to send messages between computers over long distances.
*
* :::tip
* Modems provide a fairly basic set of methods, which makes them very flexible but often hard to work with. The
* {@literal @}{rednet} API is built on top of modems, and provides a more user-friendly interface.
* :::
*
* ## Sending and receiving messages
* Modems operate on a series of channels, a bit like frequencies on a radio. Any modem can send a message on a
* particular channel, but only those which have {@link #open opened} the channel and are "listening in" can receive
* messages.
*
* Channels are represented as an integer between 0 and 65535 inclusive. These channels don't have any defined meaning,
* though some APIs or programs will assign a meaning to them. For instance, the @{gps} module sends all its messages on
* channel 65534 (@{gps.CHANNEL_GPS}), while @{rednet} uses channels equal to the computer's ID.
*
* - Sending messages is done with the {@link #transmit(int, int, Object)} message.
* - Receiving messages is done by listening to the @{modem_message} event.
*
* ## Types of modem
* CC: Tweaked comes with three kinds of modem, with different capabilities.
*
* <ul>
* <li><strong>Wireless modems:</strong> Wireless modems can send messages to any other wireless modem. They can be placed next to a
* computer, or equipped as a pocket computer or turtle upgrade.
*
* Wireless modems have a limited range, only sending messages to modems within 64 blocks. This range increases
* linearly once the modem is above y=96, to a maximum of 384 at world height.</li>
* <li><strong>Ender modems:</strong> These are upgraded versions of normal wireless modems. They do not have a distance
* limit, and can send messages between dimensions.</li>
* <li><strong>Wired modems:</strong> These send messages to other any other wired modems connected to the same network
* (using <em>Networking Cable</em>). They also can be used to attach additional peripherals to a computer.</li></ul>
*
* @cc.module modem
* @cc.see modem_message Queued when a modem receives a message on an {@link #open(int) open channel}.
* @cc.see rednet A networking API built on top of the modem peripheral.
* @cc.usage Wrap a modem and a message on channel 15, requesting a response on channel 43. Then wait for a message to
* arrive on channel 43 and print it.
*
* <pre>{@code
* local modem = peripheral.find("modem") or error("No modem attached", 0)
* modem.open(43) -- Open 43 so we can receive replies
*
* -- Send our message
* modem.transmit(15, 43, "Hello, world!")
*
* -- And wait for a reply
* local event, side, channel, replyChannel, message, distance
* repeat
* event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
* until channel == 43
*
* print("Received a reply: " .. tostring(message))
* }</pre>
*/
public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPacketReceiver
{
@ -157,12 +208,23 @@ public final void closeAll()
* Sends a modem message on a certain channel. Modems listening on the channel will queue a {@code modem_message}
* event on adjacent computers.
*
* <blockquote><strong>Note:</strong> The channel does not need be open to send a message.</blockquote>
* :::note
* The channel does not need be open to send a message.
* :::
*
* @param channel The channel to send messages on.
* @param replyChannel The channel that responses to this message should be sent on.
* @param payload The object to send. This can be a boolean, string, number, or table.
* @param replyChannel The channel that responses to this message should be sent on. This can be the same as
* {@code channel} or entirely different. The channel must have been {@link #open opened} on
* the sending computer in order to receive the replies.
* @param payload The object to send. This can be any primitive type (boolean, number, string) as well as
* tables. Other types (like functions), as well as metatables, will not be transmitted.
* @throws LuaException If the channel is out of range.
* @cc.usage Wrap a modem and a message on channel 15, requesting a response on channel 43.
*
* <pre>{@code
* local modem = peripheral.find("modem") or error("No modem attached", 0)
* modem.transmit(15, 43, "Hello, world!")
* }</pre>
*/
@LuaFunction
public final void transmit( int channel, int replyChannel, Object payload ) throws LuaException

View File

@ -80,8 +80,9 @@ public World getWorld()
* If this computer is attached to the network, it _will not_ be included in
* this list.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @param computer The calling computer.
* @return Remote peripheral names on the network.
@ -95,8 +96,9 @@ public final Collection<String> getNamesRemote( IComputerAccess computer )
/**
* Determine if a peripheral is available on this wired network.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @param computer The calling computer.
* @param name The peripheral's name.
@ -112,8 +114,9 @@ public final boolean isPresentRemote( IComputerAccess computer, String name )
/**
* Get the type of a peripheral is available on this wired network.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @param computer The calling computer.
* @param name The peripheral's name.
@ -132,8 +135,9 @@ public final Object[] getTypeRemote( IComputerAccess computer, String name )
/**
* Check a peripheral is of a particular type.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @param computer The calling computer.
* @param name The peripheral's name.
@ -153,8 +157,9 @@ public final Object[] hasTypeRemote( IComputerAccess computer, String name, Stri
/**
* Get all available methods for the remote peripheral with the given name.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @param computer The calling computer.
* @param name The peripheral's name.
@ -174,8 +179,9 @@ public final Object[] getMethodsRemote( IComputerAccess computer, String name )
/**
* Call a method on a peripheral on this wired network.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @param computer The calling computer.
* @param context The Lua context we're executing in.
@ -204,8 +210,9 @@ public final MethodResult callRemote( IComputerAccess computer, ILuaContext cont
* may be used by other computers on the network to wrap this computer as a
* peripheral.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
* :::note
* This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
*
* @return The current computer's name.
* @cc.treturn string|nil The current computer's name on the wired network.

View File

@ -1,16 +1,18 @@
--- The Disk API allows you to interact with disk drives.
--
-- These functions can operate on locally attached or remote disk drives. To use
-- a locally attached drive, specify “side” as one of the six sides
-- (e.g. `left`); to use a remote disk drive, specify its name as printed when
-- enabling its modem (e.g. `drive_0`).
--
-- **Note:** All computers (except command computers), turtles and pocket
-- computers can be placed within a disk drive to access it's internal storage
-- like a disk.
--
-- @module disk
-- @since 1.2
--[[- The Disk API allows you to interact with disk drives.
These functions can operate on locally attached or remote disk drives. To use a
locally attached drive, specify side as one of the six sides (e.g. `left`); to
use a remote disk drive, specify its name as printed when enabling its modem
(e.g. `drive_0`).
:::tip
All computers (except command computers), turtles and pocket computers can be
placed within a disk drive to access it's internal storage like a disk.
:::
@module disk
@since 1.2
]]
local function isDrive(name)
if type(name) ~= "string" then

View File

@ -1,27 +1,30 @@
--- The GPS API provides a method for turtles and computers to retrieve their
-- own locations.
--
-- It broadcasts a PING message over @{rednet} and wait for responses. In order
-- for this system to work, there must be at least 4 computers used as gps hosts
-- which will respond and allow trilateration. Three of these hosts should be in
-- a plane, and the fourth should be either above or below the other three. The
-- three in a plane should not be in a line with each other. You can set up
-- hosts using the gps program.
--
-- **Note**: When entering in the coordinates for the host you need to put in
-- the `x`, `y`, and `z` coordinates of the computer, not the modem, as all
-- rednet distances are measured from the block the computer is in.
--
-- Also note that you may choose which axes x, y, or z refers to - so long as
-- your systems have the same definition as any GPS servers that're in range, it
-- works just the same. For example, you might build a GPS cluster according to
-- [this tutorial][1], using z to account for height, or you might use y to
-- account for height in the way that Minecraft's debug screen displays.
--
-- [1]: http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/
--
-- @module gps
-- @since 1.31
--[[- The GPS API provides a method for turtles and computers to retrieve their
own locations.
It broadcasts a PING message over @{rednet} and wait for responses. In order for
this system to work, there must be at least 4 computers used as gps hosts which
will respond and allow trilateration. Three of these hosts should be in a plane,
and the fourth should be either above or below the other three. The three in a
plane should not be in a line with each other. You can set up hosts using the
gps program.
:::note
When entering in the coordinates for the host you need to put in the `x`, `y`,
and `z` coordinates of the computer, not the modem, as all modem distances are
measured from the block the computer is in.
:::
Also note that you may choose which axes x, y, or z refers to - so long as your
systems have the same definition as any GPS servers that're in range, it works
just the same. For example, you might build a GPS cluster according to [this
tutorial][1], using z to account for height, or you might use y to account for
height in the way that Minecraft's debug screen displays.
[1]: http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/
@module gps
@since 1.31
]]
local expect = dofile("rom/modules/main/cc/expect.lua").expect

View File

@ -66,7 +66,7 @@ type.
What is a peripheral type though? This is a string which describes what a
peripheral is, and so what functions are available on it. For instance, speakers
are just called `"speaker"`, and monitors `"monitor"`. Some peripherals might
have more than one type; a Minecraft chest is both a `"minecraft:chest"` and
have more than one type - a Minecraft chest is both a `"minecraft:chest"` and
`"inventory"`.
You can get all the types a peripheral has with @{peripheral.getType}, and check

View File

@ -1,21 +1,48 @@
--- The Rednet API allows systems to communicate between each other without
-- using redstone. It serves as a wrapper for the modem API, offering ease of
-- functionality (particularly in regards to repeating signals) with some
-- expense of fine control.
--
-- In order to send and receive data, a modem (either wired, wireless, or ender)
-- is required. The data reaches any possible destinations immediately after
-- sending it, but is range limited.
--
-- Rednet also allows you to use a "protocol" - simple string names indicating
-- what messages are about. Receiving systems may filter messages according to
-- their protocols, thereby automatically ignoring incoming messages which don't
-- specify an identical string. It's also possible to @{rednet.lookup|lookup}
-- which systems in the area use certain protocols, hence making it easier to
-- determine where given messages should be sent in the first place.
--
-- @module rednet
-- @since 1.2
--[[- The Rednet API allows computers to communicate between each other by using
@{modem|modems}. It provides a layer of abstraction on top of the main @{modem}
peripheral, making it slightly easier to use.
## Basic usage
In order to send a message between two computers, each computer must have a
modem on one of its sides (or in the case of pocket computers and turtles, the
modem must be equipped as an upgrade). The two computers should then call
@{rednet.open}, which sets up the modems ready to send and receive messages.
Once rednet is opened, you can send messages using @{rednet.send} and receive
them using @{rednet.receive}. It's also possible to send a message to _every_
rednet-using computer using @{rednet.broadcast}.
:::caution Network security
While rednet provides a friendly way to send messages to specific computers, it
doesn't provide any guarantees about security. Other computers could be
listening in to your messages, or even pretending to send messages from other computers!
If you're playing on a multi-player server (or at least one where you don't
trust other players), it's worth encrypting or signing your rednet messages.
:::
## Protocols and hostnames
Several rednet messages accept "protocol"s - simple string names describing what
a message is about. When sending messages using @{rednet.send} and
@{rednet.broadcast}, you can optionally specify a protocol for the message. This
same protocol can then be given to @{rednet.receive}, to ignore all messages not
using this protocol.
It's also possible to look-up computers based on protocols, providing a basic
system for service discovery and [DNS]. A computer can advertise that it
supports a particular protocol with @{rednet.host}, also providing a friendly
"hostname". Other computers may then find all computers which support this
protocol using @{rednet.lookup}.
[DNS]: https://en.wikipedia.org/wiki/Domain_Name_System "Domain Name System"
@module rednet
@since 1.2
@see rednet_message Queued when a rednet message is received.
@see modem Rednet is built on top of the modem peripheral. Modems provide a more
bare-bones but flexible interface.
]]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
@ -46,9 +73,17 @@ This will open the modem on two channels: one which has the same
@tparam string modem The name of the modem to open.
@throws If there is no such modem with the given name
@usage Open a wireless modem on the back of the computer.
@usage Open rednet on the back of the computer, allowing you to send and receive
rednet messages using it.
rednet.open("back")
@usage Open rednet on all attached modems. This abuses the "filter" argument to
@{peripheral.find}.
peripheral.find("modem", rednet.open)
@see rednet.close
@see rednet.isOpen
]]
function open(modem)
expect(1, modem, "string")
@ -65,6 +100,7 @@ end
-- @tparam[opt] string modem The side the modem exists on. If not given, all
-- open modems will be closed.
-- @throws If there is no such modem with the given name
-- @see rednet.open
function close(modem)
expect(1, modem, "string", "nil")
if modem then
@ -90,6 +126,7 @@ end
-- modems will be checked.
-- @treturn boolean If the given modem is open.
-- @since 1.31
-- @see rednet.open
function isOpen(modem)
expect(1, modem, "string", "nil")
if modem then
@ -109,15 +146,17 @@ function isOpen(modem)
end
--[[- Allows a computer or turtle with an attached modem to send a message
intended for a system with a specific ID. At least one such modem must first
intended for a sycomputer with a specific ID. At least one such modem must first
be @{rednet.open|opened} before sending is possible.
Assuming the target was in range and also had a correctly opened modem, it
may then use @{rednet.receive} to collect the message.
Assuming the target was in range and also had a correctly opened modem, the
target computer may then use @{rednet.receive} to collect the message.
@tparam number recipient The ID of the receiving computer.
@param message The message to send. This should not contain coroutines or
functions, as they will be converted to @{nil}.
@param message The message to send. Like with @{modem.transmit}, this can
contain any primitive type (numbers, booleans and strings) as well as
tables. Other types (like functions), as well as metatables, will not be
transmitted.
@tparam[opt] string protocol The "protocol" to send this message under. When
using @{rednet.receive} one can filter to only receive messages sent under a
particular protocol.
@ -174,16 +213,19 @@ function send(recipient, message, protocol)
return sent
end
--- Broadcasts a string message over the predefined @{CHANNEL_BROADCAST}
-- channel. The message will be received by every device listening to rednet.
--
-- @param message The message to send. This should not contain coroutines or
-- functions, as they will be converted to @{nil}.
-- @tparam[opt] string protocol The "protocol" to send this message under. When
-- using @{rednet.receive} one can filter to only receive messages sent under a
-- particular protocol.
-- @see rednet.receive
-- @changed 1.6 Added protocol parameter.
--[[- Broadcasts a string message over the predefined @{CHANNEL_BROADCAST}
channel. The message will be received by every device listening to rednet.
@param message The message to send. This should not contain coroutines or
functions, as they will be converted to @{nil}. @tparam[opt] string protocol
The "protocol" to send this message under. When using @{rednet.receive} one can
filter to only receive messages sent under a particular protocol.
@see rednet.receive
@changed 1.6 Added protocol parameter.
@usage Broadcast the words "Hello, world!" to every computer using rednet.
rednet.broadcast("Hello, world!")
]]
function broadcast(message, protocol)
expect(2, protocol, "string", "nil")
send(CHANNEL_BROADCAST, message, protocol)
@ -263,25 +305,25 @@ function receive(protocol_filter, timeout)
end
end
--- Register the system as "hosting" the desired protocol under the specified
-- name. If a rednet @{rednet.lookup|lookup} is performed for that protocol (and
-- maybe name) on the same network, the registered system will automatically
-- respond via a background process, hence providing the system performing the
-- lookup with its ID number.
--
-- Multiple computers may not register themselves on the same network as having
-- the same names against the same protocols, and the title `localhost` is
-- specifically reserved. They may, however, share names as long as their hosted
-- protocols are different, or if they only join a given network after
-- "registering" themselves before doing so (eg while offline or part of a
-- different network).
--
-- @tparam string protocol The protocol this computer provides.
-- @tparam string hostname The name this protocol exposes for the given protocol.
-- @throws If trying to register a hostname which is reserved, or currently in use.
-- @see rednet.unhost
-- @see rednet.lookup
-- @since 1.6
--[[- Register the system as "hosting" the desired protocol under the specified
name. If a rednet @{rednet.lookup|lookup} is performed for that protocol (and
maybe name) on the same network, the registered system will automatically
respond via a background process, hence providing the system performing the
lookup with its ID number.
Multiple computers may not register themselves on the same network as having the
same names against the same protocols, and the title `localhost` is specifically
reserved. They may, however, share names as long as their hosted protocols are
different, or if they only join a given network after "registering" themselves
before doing so (eg while offline or part of a different network).
@tparam string protocol The protocol this computer provides.
@tparam string hostname The name this protocol exposes for the given protocol.
@throws If trying to register a hostname which is reserved, or currently in use.
@see rednet.unhost
@see rednet.lookup
@since 1.6
]]
function host(protocol, hostname)
expect(1, protocol, "string")
expect(2, hostname, "string")
@ -306,21 +348,38 @@ function unhost(protocol)
hostnames[protocol] = nil
end
--- Search the local rednet network for systems @{rednet.host|hosting} the
-- desired protocol and returns any computer IDs that respond as "registered"
-- against it.
--
-- If a hostname is specified, only one ID will be returned (assuming an exact
-- match is found).
--
-- @tparam string protocol The protocol to search for.
-- @tparam[opt] string hostname The hostname to search for.
--
-- @treturn[1] { number }|nil A list of computer IDs hosting the given
-- protocol, or @{nil} if none exist.
-- @treturn[2] number|nil The computer ID with the provided hostname and protocol,
-- or @{nil} if none exists.
-- @since 1.6
--[[- Search the local rednet network for systems @{rednet.host|hosting} the
desired protocol and returns any computer IDs that respond as "registered"
against it.
If a hostname is specified, only one ID will be returned (assuming an exact
match is found).
@tparam string protocol The protocol to search for.
@tparam[opt] string hostname The hostname to search for.
@treturn[1] number... A list of computer IDs hosting the given protocol.
@treturn[2] number|nil The computer ID with the provided hostname and protocol,
or @{nil} if none exists.
@since 1.6
@usage Find all computers which are hosting the `"chat"` protocol.
local computers = {rednet.lookup("chat")}
print(#computers .. " computers available to chat")
for _, computer in pairs(computers) do
print("Computer #" .. computer)
end
@usage Find a computer hosting the `"chat"` protocol with a hostname of `"my_host"`.
local id = rednet.lookup("chat", "my_host")
if id then
print("Found my_host at computer #" .. id)
else
printError("Cannot find my_host")
end
]]
function lookup(protocol, hostname)
expect(1, protocol, "string")
expect(2, hostname, "string", "nil")

View File

@ -36,7 +36,7 @@ local encoder = dfpwm.make_encoder()
local decoder = dfpwm.make_decoder()
local out = fs.open("speedy.dfpwm", "wb")
for input in io.lines("my_audio_track.dfpwm", 16 * 1024 * 2) do
for input in io.lines("data/example.dfpwm", 16 * 1024 * 2) do
local decoded = decoder(input)
local output = {}