new energy system and no more spacing

This commit is contained in:
2026-02-16 12:37:49 +01:00
parent 8915fb3ed0
commit d97b3f0413
3 changed files with 34 additions and 13 deletions

View File

@@ -1096,7 +1096,7 @@ int main() {
Color bgColor = ne_processColor(p.truebg); Color bgColor = ne_processColor(p.truebg);
DrawRectangle(x * pixelWidth + offX, y * pixelHeight + offY, pixelWidth, pixelHeight, bgColor); 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);
} }
} }

View File

@@ -641,7 +641,8 @@ typedef struct nn_Computer {
size_t deviceInfoLen; size_t deviceInfoLen;
nn_DeviceInfo *deviceInfo; nn_DeviceInfo *deviceInfo;
double totalEnergy; double totalEnergy;
double energy; void *energyState;
nn_EnergyHandler *energyHandler;
size_t totalMemory; size_t totalMemory;
double creationTimestamp; double creationTimestamp;
size_t stackSize; size_t stackSize;
@@ -727,6 +728,12 @@ void nn_destroyComponentType(nn_ComponentType *ctype) {
nn_free(ctx, ctype, sizeof(nn_ComponentType)); 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_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; nn_Context *ctx = &universe->ctx;
@@ -771,7 +778,8 @@ nn_Computer *nn_createComputer(nn_Universe *universe, void *userdata, const char
return NULL; return NULL;
} }
c->totalEnergy = 500; c->totalEnergy = 500;
c->energy = 500; c->energyState = NULL;
c->energyHandler = nn_default_energyHandler;
c->totalMemory = totalMemory; c->totalMemory = totalMemory;
c->creationTimestamp = nn_currentTime(ctx); c->creationTimestamp = nn_currentTime(ctx);
c->stackSize = 0; c->stackSize = 0;
@@ -952,18 +960,19 @@ double nn_getTotalEnergy(nn_Computer *computer) {
return computer->totalEnergy; return computer->totalEnergy;
} }
void nn_setEnergy(nn_Computer *computer, double energy) {
computer->energy = energy;
}
double nn_getEnergy(nn_Computer *computer) { 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) { bool nn_removeEnergy(nn_Computer *computer, double energy) {
computer->energy -= energy; double newEnergy = computer->energyHandler(computer->energyState, computer, energy);
if(computer->energy < 0) computer->energy = 0; if(newEnergy <= 0) {
if(computer->energy <= 0) { newEnergy = 0;
computer->state = NN_BLACKOUT; computer->state = NN_BLACKOUT;
return true; return true;
} }

View File

@@ -375,14 +375,26 @@ nn_Architecture nn_findSupportedArchitecture(nn_Computer *computer, const char *
void nn_setTotalEnergy(nn_Computer *computer, double maxEnergy); void nn_setTotalEnergy(nn_Computer *computer, double maxEnergy);
// gets the energy capacity of the computer // gets the energy capacity of the computer
double nn_getTotalEnergy(nn_Computer *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 // gets the current amount of energy
double nn_getEnergy(nn_Computer *computer); double nn_getEnergy(nn_Computer *computer);
// Returns true if there is no more energy left, and a blackout has occured. // Returns true if there is no more energy left, and a blackout has occured.
bool nn_removeEnergy(nn_Computer *computer, double energy); 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); 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); 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. // 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); double nn_getUptime(nn_Computer *computer);