progress on modem and basic todo

This commit is contained in:
2026-04-07 15:55:57 +02:00
parent 82777b52b3
commit 21cd1fa4e6
6 changed files with 172 additions and 8 deletions

View File

@@ -1219,7 +1219,7 @@ nn_Exit nn_startComputer(nn_Computer *computer) {
nn_Exit err = computer->arch.handler(&req);
if(err) {
computer->state = NN_CRASHED;
nn_setErrorFromExit(computer, err);
if(err != NN_EBADCALL) nn_setErrorFromExit(computer, err);
return err;
}
computer->archState = req.localState;
@@ -3619,6 +3619,69 @@ nn_Exit nn_pushModemMessage(nn_Computer *C, const char *modemAddress, const char
return nn_pushSignal(C, signalVals);
}
nn_Computer *nn_fromWrappedComputer(nn_Component *component) {
if(nn_strcmp(component->internalID, "NN_WRAPPEDCOMPUTER") == 0) {
return component->state;
}
return NULL;
}
nn_Exit nn_transferErrorFrom(nn_Exit exit, nn_Computer *from, nn_Computer *to) {
const char *err = nn_getError(from);
if(err != NULL) nn_setError(to, err);
return exit;
}
typedef enum nn_CompNum {
NN_COMPNUM_START,
NN_COMPNUM_STOP,
NN_COMPNUM_ISRUNNING,
NN_COMPNUM_GETDEVICEINFO,
NN_COMPNUM_CRASH,
NN_COMPNUM_GETARCH,
NN_COMPNUM_ISROBOT,
NN_COMPNUM_BEEP,
NN_COMPNUM_COUNT,
} nn_CompNum;
static nn_Exit nn_computerHandler(nn_ComponentRequest *req) {
if(req->action == NN_COMP_DROP) return NN_OK;
if(req->action == NN_COMP_SIGNAL) return NN_OK;
nn_Computer *target = req->state;
nn_Computer *src = req->computer;
nn_CompNum method = req->methodIdx;
if(src) nn_setError(src, "computer: not implemented yet");
return NN_EBADCALL;
}
nn_Component *nn_wrapComputer(nn_Computer *computer) {
const nn_Method methods[NN_COMPNUM_COUNT] = {
[NN_COMPNUM_START] = {"start", "function(): boolean - Attempts to turn on the computer, will return false if it is already on or it failed", NN_INDIRECT},
[NN_COMPNUM_STOP] = {"stop", "function(): boolean - Attempts to turn ooff the computer, will return false if it is already off or it failed", NN_INDIRECT},
[NN_COMPNUM_ISRUNNING] = {"isRunning", "function(): boolean - Returns whether it is running or not", NN_INDIRECT},
[NN_COMPNUM_GETDEVICEINFO] = {"getDeviceInfo", "function(): table<string, table<string, string>> - Returns a table of device information for the computer", NN_INDIRECT},
[NN_COMPNUM_CRASH] = {"crash", "function(error: string) - Will forcefully crash the computer, if it is running", NN_INDIRECT},
[NN_COMPNUM_GETARCH] = {"getArchitecture", "function(): string - Get the architecture of the computer", NN_INDIRECT},
[NN_COMPNUM_ISROBOT] = {"isRobot", "function(): boolean - Returns whether the computer is a robot", NN_INDIRECT},
[NN_COMPNUM_BEEP] = {"beep", "function(frequency?: number, duration?: number, volume?: number) - Makes the computer make a beep sound", NN_INDIRECT},
};
nn_Component *c = nn_createComponent(computer->universe, computer->address, "computer");
if(c == NULL) return NULL;
nn_setComponentState(c, computer);
nn_setComponentHandler(c, nn_computerHandler);
if(nn_setComponentTypeID(c, "NN_WRAPPEDCOMPUTER")) {
nn_dropComponent(c);
return NULL;
}
if(nn_setComponentMethodsArray(c, methods, NN_COMPNUM_COUNT)) {
nn_dropComponent(c);
return NULL;
}
return c;
}
typedef enum nn_EENum {
NN_EENUM_GETSIZE,
NN_EENUM_GETDATASIZE,
@@ -5600,6 +5663,9 @@ typedef enum nn_ModemNum {
NN_MODEMNUM_SEND,
NN_MODEMNUM_BROADCAST,
NN_MODEMNUM_GETWAKE,
NN_MODEMNUM_SETWAKE,
NN_MODEMNUM_COUNT,
} nn_ModemNum;
@@ -5610,6 +5676,7 @@ typedef struct nn_ModemState {
} nn_ModemState;
static nn_Exit nn_modemHandler(nn_ComponentRequest *req) {
if(req->action == NN_COMP_SIGNAL) return NN_OK;
nn_Context *ctx = req->ctx;
nn_ModemState *state = req->classState;
nn_Computer *C = req->computer;
@@ -5632,6 +5699,37 @@ static nn_Exit nn_modemHandler(nn_ComponentRequest *req) {
mreq.state = req->state;
mreq.modem = &state->modem;
mreq.localAddress = req->compAddress;
nn_Exit e;
if(req->action == NN_COMP_DROP) {
mreq.action = NN_MODEM_DROP;
state->handler(&mreq);
nn_free(ctx, state, sizeof(*state));
return NN_OK;
}
if(method == NN_MODEMNUM_ISWIRED) {
return nn_pushbool(C, isWired);
}
if(method == NN_MODEMNUM_ISWIRELESS) {
return nn_pushbool(C, isWireless);
}
if(method == NN_MODEMNUM_MAXPACKETSIZE) {
return nn_pushinteger(C, state->modem.maxPacketSize);
}
if(method == NN_MODEMNUM_MAXVALUES) {
return nn_pushinteger(C, state->modem.maxValues);
}
if(method == NN_MODEMNUM_GETSTRENGTH) {
mreq.action = NN_MODEM_GETSTRENGTH;
e = state->handler(&mreq);
if(e) return e;
req->returnCount = 1;
return nn_pushinteger(C, mreq.strength);
}
if(method == NN_MODEMNUM_MAXSTRENGTH) {
return nn_pushinteger(C, state->modem.maxRange);
}
if(C) nn_setError(C, "modem: not implemented yet");
return NN_EBADCALL;
@@ -5659,6 +5757,9 @@ nn_Component *nn_createModem(nn_Universe *universe, const char *address, const n
[NN_MODEMNUM_SEND] = {"send", "function(targetAddress: string, port: integer, ...): boolean - Send a packet", NN_INDIRECT},
[NN_MODEMNUM_BROADCAST] = {"broadcast", "function(port: integer, ...): boolean - Broadcast a packet", NN_INDIRECT},
[NN_MODEMNUM_GETWAKE] = {"getWakeMessage", "function(): string?, boolean - Returns the wake message, if any, and whether it is fuzzy", NN_DIRECT},
[NN_MODEMNUM_SETWAKE] = {"setWakeMessage", "function(message: string?, fuzzy: boolean) - Changes the wake-up message of the modem", NN_INDIRECT},
};
nn_Exit e = nn_setComponentMethodsArray(
@@ -5679,3 +5780,36 @@ nn_Component *nn_createModem(nn_Universe *universe, const char *address, const n
nn_setComponentHandler(c, nn_modemHandler);
return c;
}
nn_Modem nn_defaultWiredModem = {
.maxRange = 0,
.maxValues = 8,
.maxPacketSize = 8192,
.maxOpenPorts = 16,
.isWired = true,
.basePacketCost = 50,
.fullPacketCost = 100,
.costPerStrength = 0,
};
nn_Modem nn_defaultWirelessModems[2] = {
NN_INIT(nn_Modem) {
.maxRange = 16,
.maxValues = 8,
.maxPacketSize = 8192,
.maxOpenPorts = 16,
.isWired = true,
.basePacketCost = 100,
.fullPacketCost = 500,
.costPerStrength = 30,
},
NN_INIT(nn_Modem) {
.maxRange = 400,
.maxValues = 8,
.maxPacketSize = 8192,
.maxOpenPorts = 16,
.isWired = true,
.basePacketCost = 200,
.fullPacketCost = 1000,
.costPerStrength = 20,
},
};