From d97b3f041326d2bcebedf42f6c99cc60077dd8a9 Mon Sep 17 00:00:00 2001 From: IonutParau Date: Mon, 16 Feb 2026 12:37:49 +0100 Subject: [PATCH] new energy system and no more spacing --- rewrite/main.c | 2 +- rewrite/neonucleus.c | 29 +++++++++++++++++++---------- rewrite/neonucleus.h | 16 ++++++++++++++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/rewrite/main.c b/rewrite/main.c index eccfcc5..59b626e 100644 --- a/rewrite/main.c +++ b/rewrite/main.c @@ -1096,7 +1096,7 @@ int main() { Color bgColor = ne_processColor(p.truebg); DrawRectangle(x * pixelWidth + offX, y * pixelHeight + offY, pixelWidth, pixelHeight, bgColor); - DrawTextCodepoint(font, p.codepoint, (Vector2) {x * pixelWidth + offX, y * pixelHeight + offY}, pixelHeight - 5, fgColor); + DrawTextCodepoint(font, p.codepoint, (Vector2) {x * pixelWidth + offX, y * pixelHeight + offY}, pixelHeight, fgColor); } } diff --git a/rewrite/neonucleus.c b/rewrite/neonucleus.c index d548a3d..505e145 100644 --- a/rewrite/neonucleus.c +++ b/rewrite/neonucleus.c @@ -641,7 +641,8 @@ typedef struct nn_Computer { size_t deviceInfoLen; nn_DeviceInfo *deviceInfo; double totalEnergy; - double energy; + void *energyState; + nn_EnergyHandler *energyHandler; size_t totalMemory; double creationTimestamp; size_t stackSize; @@ -727,6 +728,12 @@ void nn_destroyComponentType(nn_ComponentType *ctype) { nn_free(ctx, ctype, sizeof(nn_ComponentType)); } +double nn_default_energyHandler(void *state, nn_Computer *computer, double amount) { + (void)state; + (void)amount; + return nn_getTotalEnergy(computer); +} + nn_Computer *nn_createComputer(nn_Universe *universe, void *userdata, const char *address, size_t totalMemory, size_t maxComponents, size_t maxDevices) { nn_Context *ctx = &universe->ctx; @@ -771,7 +778,8 @@ nn_Computer *nn_createComputer(nn_Universe *universe, void *userdata, const char return NULL; } c->totalEnergy = 500; - c->energy = 500; + c->energyState = NULL; + c->energyHandler = nn_default_energyHandler; c->totalMemory = totalMemory; c->creationTimestamp = nn_currentTime(ctx); c->stackSize = 0; @@ -952,18 +960,19 @@ double nn_getTotalEnergy(nn_Computer *computer) { return computer->totalEnergy; } -void nn_setEnergy(nn_Computer *computer, double energy) { - computer->energy = energy; -} - double nn_getEnergy(nn_Computer *computer) { - return computer->energy; + double newEnergy = computer->energyHandler(computer->energyState, computer, 0); + if(newEnergy <= 0) { + newEnergy = 0; + computer->state = NN_BLACKOUT; + } + return newEnergy; } bool nn_removeEnergy(nn_Computer *computer, double energy) { - computer->energy -= energy; - if(computer->energy < 0) computer->energy = 0; - if(computer->energy <= 0) { + double newEnergy = computer->energyHandler(computer->energyState, computer, energy); + if(newEnergy <= 0) { + newEnergy = 0; computer->state = NN_BLACKOUT; return true; } diff --git a/rewrite/neonucleus.h b/rewrite/neonucleus.h index 19e3c41..a26dfe6 100644 --- a/rewrite/neonucleus.h +++ b/rewrite/neonucleus.h @@ -375,14 +375,26 @@ nn_Architecture nn_findSupportedArchitecture(nn_Computer *computer, const char * void nn_setTotalEnergy(nn_Computer *computer, double maxEnergy); // gets the energy capacity of the computer double nn_getTotalEnergy(nn_Computer *computer); -// sets the current amount of energy -void nn_setEnergy(nn_Computer *computer, double energy); // gets the current amount of energy double nn_getEnergy(nn_Computer *computer); // Returns true if there is no more energy left, and a blackout has occured. bool nn_removeEnergy(nn_Computer *computer, double energy); +// the handler of energy costs. +// The default handler just returns the total energy. +// Computers do not keep track of their current energy, they just call this function. +// getEnergy() calls this with amountToRemove set to 0. It is recommended to handle this as a fastpath. +// This should return the new amount of energy after the removal. +// A negative amount can be returned, it will be clamped to 0. +// If an error occurs, it is recommended to just return 0 and trigger a blackout. +// TODO: evaluate if API should be reworked to handle errors in energy handler. +typedef double nn_EnergyHandler(void *energyState, nn_Computer *computer, double amountToRemove); + +void nn_setEnergyHandler(nn_Computer *computer, void *energyState, nn_EnergyHandler *handler); + +// Returns the memory usage limit of the computer. size_t nn_getTotalMemory(nn_Computer *computer); +// Gets the total amount of free memory the computer has available. The total memory - this is the amount of memory used. size_t nn_getFreeMemory(nn_Computer *computer); // gets the current uptime of a computer. When the computer is not running, this value can be anything and loses all meaning. double nn_getUptime(nn_Computer *computer);