mirror of
https://github.com/MincoMK/computercraft-ts.git
synced 2025-12-10 06:23:19 +00:00
feat: rednet implementation
This commit is contained in:
101
types/index.d.ts
vendored
101
types/index.d.ts
vendored
@@ -3,7 +3,7 @@
|
||||
* Based on documentation from https://tweaked.cc/
|
||||
*/
|
||||
|
||||
export {};
|
||||
export { };
|
||||
|
||||
/** @noSelf **/
|
||||
declare global {
|
||||
@@ -180,6 +180,105 @@ declare global {
|
||||
function fromBlit(hex: string): number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rednet API
|
||||
* Interact with the rednet API, high level networking system over modem API.
|
||||
*/
|
||||
namespace rednet {
|
||||
const CHANNEL_BROADCAST = 65535;
|
||||
const CHANNEL_REPEAT = 65533;
|
||||
const MAX_ID_CHANNEL = 65500;
|
||||
|
||||
type RednetData = number | boolean | string | Object;
|
||||
|
||||
/**
|
||||
* Opens a modem with the given peripheral name, allowing it to send and receive messages over rednet.
|
||||
* This will open the modem on two channels: one which has the same ID as the computer, and another on the broadcast channel.
|
||||
* @param modem string The name of the modem to open.
|
||||
*/
|
||||
function open(modem: string): void;
|
||||
|
||||
/**
|
||||
* Close a modem with the given peripheral name, meaning it can no longer send and receive rednet messages.
|
||||
* @param modem The side the modem exists on. If not given, all open modems will be closed.
|
||||
*/
|
||||
function close(modem?: string): void;
|
||||
|
||||
/**
|
||||
* Determine if rednet is currently open.
|
||||
* @param modem Which modem to check. If not given, all connected modems will be checked.
|
||||
*/
|
||||
function isOpen(modem?: string): boolean;
|
||||
|
||||
/**
|
||||
* Allows a computer or turtle with an attached modem to send a message intended for a computer with a specific ID. At least one such modem must first be opened before sending is possible.
|
||||
*
|
||||
* 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.
|
||||
* @param recipient The ID of the receiving computer.
|
||||
* @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.
|
||||
* @param protocol string The "protocol" to send this message under. When using rednet.receive one can filter to only receive messages sent under a particular protocol.
|
||||
* @returns If this message was successfully sent (i.e. if rednet is currently open). Note, this does not guarantee the message was actually received.
|
||||
*/
|
||||
function send(
|
||||
recipient: number,
|
||||
message: RednetData,
|
||||
protocol?: string,
|
||||
): boolean;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param protocol The "protocol" to send this message under. When using rednet.receive one can filter to only receive messages sent under a particular protocol.
|
||||
*/
|
||||
function broadcast(message: RednetData, protocol?: string): void;
|
||||
|
||||
/**
|
||||
* Wait for a rednet message to be received, or until nTimeout seconds have elapsed.
|
||||
* @param protocolFilter The protocol the received message must be sent with. If specified, any messages not sent under this protocol will be discarded.
|
||||
* @param timeout The number of seconds to wait if no message is received.
|
||||
* @returns
|
||||
* - 1. The computer which sent this message
|
||||
* - 2. The received message
|
||||
* - 3. The protocol this message was sent under.
|
||||
* - or null If the timeout elapsed and no message was received.
|
||||
*/
|
||||
function receive(
|
||||
protocolFilter?: string,
|
||||
timeout?: number,
|
||||
): [number, RednetData, string | null] | null;
|
||||
|
||||
/**
|
||||
* Register the system as "hosting" the desired protocol under the specified name. If a rednet 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).
|
||||
*
|
||||
* @param protocol The protocol this computer provides.
|
||||
* @param hostname The name this computer exposes for the given protocol.
|
||||
*/
|
||||
function host(protocol: string, hostname: string): void;
|
||||
|
||||
/**
|
||||
* Stop hosting a specific protocol, meaning it will no longer respond to rednet.lookup requests.
|
||||
* @param protocol The protocol to unregister your self from.
|
||||
*/
|
||||
function unhost(protocol: string): void;
|
||||
|
||||
/**
|
||||
* Search the local rednet network for systems 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).
|
||||
*
|
||||
* @param protocol The protocol to search for.
|
||||
* @param hostname The hostname to search for.
|
||||
* @returns A list of computer IDs hosting the given protocol.
|
||||
* - or The computer ID with the provided hostname and protocol, or nil if none exists.
|
||||
*/
|
||||
function lookup(
|
||||
protocol: string,
|
||||
hostname?: string,
|
||||
): number[] | number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* File System API
|
||||
* Interact with the computer's files and filesystem, allowing you to manipulate files, directories and paths.
|
||||
|
||||
Reference in New Issue
Block a user