mirror of
https://github.com/NeoFlock/neonucleus.git
synced 2025-09-24 09:03:32 +02:00
arch progress
This commit is contained in:
parent
b35dd7d9b5
commit
1f6419a341
@ -97,6 +97,11 @@ void nn_addSupportedArchitecture(nn_computer *computer, nn_architecture *arch) {
|
||||
computer->supportedArchCount++;
|
||||
}
|
||||
|
||||
nn_architecture *nn_getSupportedArchitecture(nn_computer *computer, size_t idx) {
|
||||
if(idx >= computer->supportedArchCount) return NULL;
|
||||
return computer->supportedArch[idx];
|
||||
}
|
||||
|
||||
nn_architecture *nn_getArchitecture(nn_computer *computer) {
|
||||
return computer->arch;
|
||||
}
|
||||
@ -215,6 +220,10 @@ size_t nn_getEnergy(nn_computer *computer) {
|
||||
return computer->energy;
|
||||
}
|
||||
|
||||
size_t nn_getMaxEnergy(nn_computer *computer) {
|
||||
return computer->maxEnergy;
|
||||
}
|
||||
|
||||
void nn_removeEnergy(nn_computer *computer, size_t energy) {
|
||||
if(computer->energy < energy) {
|
||||
// blackout
|
||||
|
@ -12,21 +12,25 @@ int main() {
|
||||
|
||||
// 1MB of RAM, 16 components max
|
||||
nn_computer *computer = nn_newComputer(universe, "testMachine", arch, NULL, 1*1024*1024, 16);
|
||||
nn_setEnergyInfo(computer, 5000, 5000);
|
||||
nn_addSupportedArchitecture(computer, arch);
|
||||
while(true) {
|
||||
nn_tickComputer(computer);
|
||||
int state = nn_tickComputer(computer);
|
||||
if(state == NN_STATE_SWITCH) {
|
||||
nn_architecture *nextArch = nn_getNextArchitecture(computer);
|
||||
printf("Next architecture: %s\n", nextArch->archName);
|
||||
break;
|
||||
} else if(state == NN_STATE_CLOSING || state == NN_STATE_REPEAT) {
|
||||
break;
|
||||
} else if(state == NN_STATE_BLACKOUT) {
|
||||
printf("blackout\n");
|
||||
break;
|
||||
}
|
||||
const char *e = nn_getError(computer);
|
||||
if(e != NULL) {
|
||||
printf("Error: %s\n", e);
|
||||
break;
|
||||
}
|
||||
int state = nn_getState(computer);
|
||||
if(state == NN_STATE_CLOSING || state == NN_STATE_REPEAT || state == NN_STATE_SWITCH) {
|
||||
if(state == NN_STATE_SWITCH) {
|
||||
nn_architecture *nextArch = nn_getNextArchitecture(computer);
|
||||
printf("Next architecture: %s\n", nextArch->archName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// destroy
|
||||
|
@ -156,6 +156,7 @@ size_t nn_getComputerMemoryUsed(nn_computer *computer);
|
||||
size_t nn_getComputerMemoryTotal(nn_computer *computer);
|
||||
void *nn_getComputerUserData(nn_computer *computer);
|
||||
void nn_addSupportedArchitecture(nn_computer *computer, nn_architecture *arch);
|
||||
nn_architecture *nn_getSupportedArchitecture(nn_computer *computer, size_t idx);
|
||||
nn_architecture *nn_getArchitecture(nn_computer *computer);
|
||||
nn_architecture *nn_getNextArchitecture(nn_computer *computer);
|
||||
void nn_setNextArchitecture(nn_computer *computer, nn_architecture *arch);
|
||||
@ -214,6 +215,7 @@ void nn_setState(nn_computer *computer, int state);
|
||||
|
||||
void nn_setEnergyInfo(nn_computer *computer, size_t energy, size_t capacity);
|
||||
size_t nn_getEnergy(nn_computer *computer);
|
||||
size_t nn_getMaxEnergy(nn_computer *computer);
|
||||
void nn_removeEnergy(nn_computer *computer, size_t energy);
|
||||
void nn_addEnergy(nn_computer *computer, size_t amount);
|
||||
|
||||
|
@ -1,2 +1,5 @@
|
||||
print(string.format("%d / %d", computer.usedMemory(), computer.totalMemory()))
|
||||
print(string.format("Computer Address: %q, Tmp Address: %q", computer.address(), computer.tmpAddress()))
|
||||
|
||||
print(computer.getArchitecture())
|
||||
print(table.unpack(computer.getArchitectures()))
|
||||
|
@ -73,6 +73,66 @@ static int testLuaArch_computer_tmpAddress(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int testLuaArch_computer_uptime(lua_State *L) {
|
||||
nn_computer *c = testLuaArch_getComputer(L);
|
||||
lua_pushnumber(L, nn_getUptime(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: beep
|
||||
static int testLuaArch_computer_beep(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int testLuaArch_computer_energy(lua_State *L) {
|
||||
nn_computer *c = testLuaArch_getComputer(L);
|
||||
lua_pushinteger(L, nn_getEnergy(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int testLuaArch_computer_maxEnergy(lua_State *L) {
|
||||
nn_computer *c = testLuaArch_getComputer(L);
|
||||
lua_pushinteger(L, nn_getEnergy(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int testLuaArch_computer_getArchitecture(lua_State *L) {
|
||||
nn_computer *c = testLuaArch_getComputer(L);
|
||||
lua_pushstring(L, nn_getArchitecture(c)->archName);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int testLuaArch_computer_getArchitectures(lua_State *L) {
|
||||
nn_computer *c = testLuaArch_getComputer(L);
|
||||
lua_createtable(L, 3, 0);
|
||||
int arr = lua_gettop(L);
|
||||
size_t i = 0;
|
||||
while(true) {
|
||||
nn_architecture *arch = nn_getSupportedArchitecture(c, i);
|
||||
if(arch == NULL) break;
|
||||
i++;
|
||||
lua_pushstring(L, arch->archName);
|
||||
lua_seti(L, arr, i);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int testLuaArch_computer_setArchitecture(lua_State *L) {
|
||||
nn_computer *c = testLuaArch_getComputer(L);
|
||||
const char *requested = luaL_checkstring(L, 1);
|
||||
for(size_t i = 0;; i++) {
|
||||
nn_architecture *arch = nn_getSupportedArchitecture(c, i);
|
||||
if(arch == NULL) break;
|
||||
if(strcmp(arch->archName, requested) == 0) {
|
||||
nn_setState(c, NN_STATE_SWITCH);
|
||||
nn_setNextArchitecture(c, arch);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
luaL_error(L, "unsupported architecture: %s", requested);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void testLuaArch_loadEnv(lua_State *L) {
|
||||
lua_createtable(L, 0, 10);
|
||||
int computer = lua_gettop(L);
|
||||
@ -86,6 +146,18 @@ void testLuaArch_loadEnv(lua_State *L) {
|
||||
lua_setfield(L, computer, "address");
|
||||
lua_pushcfunction(L, testLuaArch_computer_tmpAddress);
|
||||
lua_setfield(L, computer, "tmpAddress");
|
||||
lua_pushcfunction(L, testLuaArch_computer_uptime);
|
||||
lua_setfield(L, computer, "uptime");
|
||||
lua_pushcfunction(L, testLuaArch_computer_energy);
|
||||
lua_setfield(L, computer, "energy");
|
||||
lua_pushcfunction(L, testLuaArch_computer_maxEnergy);
|
||||
lua_setfield(L, computer, "maxEnergy");
|
||||
lua_pushcfunction(L, testLuaArch_computer_getArchitecture);
|
||||
lua_setfield(L, computer, "getArchitecture");
|
||||
lua_pushcfunction(L, testLuaArch_computer_getArchitectures);
|
||||
lua_setfield(L, computer, "getArchitectures");
|
||||
lua_pushcfunction(L, testLuaArch_computer_setArchitecture);
|
||||
lua_setfield(L, computer, "setArchitecture");
|
||||
lua_setglobal(L, "computer");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user