#include "lua.h" #include "lua/lua.hpp" #include "ascii.h" extern "C" { static int cpp_cls(lua_State *L) { uint8_t color = luaL_optinteger(L, 1, 32); cls(color); return 0; } static int cpp_ink(lua_State *L) { uint8_t val = luaL_checkinteger(L, 1); ink(val); return 0; } static int cpp_paper(lua_State *L) { uint8_t val = luaL_checkinteger(L, 1); paper(val); return 0; } static int cpp_color(lua_State *L) { uint8_t ink = luaL_checkinteger(L, 1); uint8_t paper = luaL_checkinteger(L, 2); color(ink, paper); return 0; } static int cpp_locate(lua_State *L) { uint8_t x = luaL_checknumber(L, 1); uint8_t y = luaL_checknumber(L, 2); locate(x, y); return 0; } static int cpp_print(lua_State *L) { const char* str = luaL_checkstring(L, 1); int x = luaL_optnumber(L, 2, -1); int y = luaL_optnumber(L, 3, -1); print(str, x, y); return 0; } static int cpp_btn(lua_State *L) { uint8_t i = luaL_checkinteger(L, 1); lua_pushboolean(L, btn(i)); return 1; } static int cpp_btnp(lua_State *L) { uint8_t i = luaL_checkinteger(L, 1); lua_pushboolean(L, btnp(i)); return 1; } static int cpp_mousex(lua_State *L) { lua_pushinteger(L, mousex()); return 1; } static int cpp_mousey(lua_State *L) { lua_pushinteger(L, mousey()); return 1; } static int cpp_mousewheel(lua_State *L) { lua_pushinteger(L, mousewheel()); return 1; } static int cpp_mousebutton(lua_State *L) { uint8_t i = luaL_checkinteger(L, 1); lua_pushboolean(L, mousebutton(i)); return 1; } static int cpp_time(lua_State *L) { lua_pushnumber(L, time()); return 1; } static int cpp_abs(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, abs(x)); return 1; } static int cpp_ceil(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, ceil(x)); return 1; } static int cpp_flr(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, flr(x)); return 1; } static int cpp_sgn(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, sgn(x)); return 1; } static int cpp_sin(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, sin(x)); return 1; } static int cpp_cos(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, cos(x)); return 1; } static int cpp_atan2(lua_State *L) { float dx = luaL_checknumber(L, 1); float dy = luaL_checknumber(L, 2); lua_pushnumber(L, atan2(dx, dy)); return 1; } static int cpp_sqrt(lua_State *L) { float x = luaL_checknumber(L, 1); lua_pushnumber(L, sqrt(x)); return 1; } static int cpp_max(lua_State *L) { float x = luaL_checknumber(L, 1); float y = luaL_checknumber(L, 2); lua_pushnumber(L, max(x, y)); return 1; } static int cpp_mid(lua_State *L) { float x = luaL_checknumber(L, 1); float y = luaL_checknumber(L, 2); float z = luaL_checknumber(L, 3); lua_pushnumber(L, mid(x, y, z)); return 1; } static int cpp_min(lua_State *L) { float x = luaL_checknumber(L, 1); float y = luaL_checknumber(L, 2); lua_pushnumber(L, min(x, y)); return 1; } static int cpp_rnd(lua_State *L) { int x = luaL_checkinteger(L, 1); lua_pushinteger(L, rnd(x)); return 1; } static int cpp_srand(lua_State *L) { int x = luaL_checkinteger(L, 1); srand(x); return 0; } static int cpp_tostr(lua_State *L) { float val = luaL_checknumber(L, 1); lua_pushstring(L, tostr(val)); return 1; } static int cpp_ascii(lua_State *L) { const char* str = luaL_checkstring(L, 1); int index = luaL_checkinteger(L, 2); lua_pushinteger(L, ascii(str, index)); return 1; } static int cpp_chr(lua_State *L) { int val = luaL_checkinteger(L, 1); lua_pushstring(L, chr(val)); return 1; } static int cpp_strlen(lua_State *L) { const char* str = luaL_checkstring(L, 1); lua_pushinteger(L, strlen(str)); return 1; } static int cpp_setchar(lua_State *L) { int index = luaL_checkinteger(L, 1); int b0 = luaL_checkinteger(L, 2); int b1 = luaL_checkinteger(L, 3); int b2 = luaL_checkinteger(L, 4); int b3 = luaL_checkinteger(L, 5); int b4 = luaL_checkinteger(L, 6); int b5 = luaL_checkinteger(L, 7); int b6 = luaL_checkinteger(L, 8); int b7 = luaL_checkinteger(L, 9); setchar(index, b0, b1, b2, b3, b4, b5, b6, b7); return 0; } static int cpp_peek(lua_State *L) { int addr = luaL_checkinteger(L, 1); lua_pushinteger(L, peek(addr)); return 1; } static int cpp_poke(lua_State *L) { int addr = luaL_checkinteger(L, 1); int val = luaL_checkinteger(L, 2); poke(addr, val); return 0; } static int cpp_memcpy(lua_State *L) { int dst = luaL_checkinteger(L, 1); int src = luaL_checkinteger(L, 2); int size = luaL_checkinteger(L, 3); memcpy(dst, src, size); return 0; } static int cpp_sound(lua_State *L) { float freq = luaL_checknumber(L, 1); int len = luaL_checkinteger(L, 2); sound(freq, len); return 0; } static int cpp_nosound(lua_State *L) { nosound(); return 0; } static int cpp_play(lua_State *L) { const char* str = luaL_optstring(L, 1, NULL); play(str); return 0; } static int cpp_setmode(lua_State *L) { int val = luaL_checkinteger(L, 1); setmode(val); return 0; } static int cpp_load(lua_State *L) { const char* str = luaL_optstring(L, 1, NULL); load(str); return 0; } static int cpp_log(lua_State *L) { const char* str = luaL_checkstring(L, 1); debug(str); return 0; } static int cpp_fileout(lua_State *L) { const char* str = luaL_checkstring(L, 1); const uint16_t addr = luaL_checknumber(L, 2); const uint16_t size = luaL_checknumber(L, 3); fileout(str, addr, size); return 0; } static int cpp_filein(lua_State *L) { const char* str = luaL_checkstring(L, 1); const uint16_t addr = luaL_checknumber(L, 2); const uint16_t size = luaL_checknumber(L, 3); filein(str, addr, size); return 0; } } #define STATE_STOPPED 0 #define STATE_PAUSED 1 #define STATE_PLAYING 2 lua_State *L; uint8_t lua_state = STATE_STOPPED; bool init_exists = false; bool update_exists = false; bool lua_is_playing() { return lua_state == STATE_PLAYING; } const char boot[] = "function init()setmode(1)cls()play('o5l0v5cegv4cegv3cegv2cegv1ceg')memcpy(360,4608,240)memcpy(1560,4848,240)ink(1)print('G A M E',8,16)ink(4)print('S Y S T E M',20,16)ink(8)print('v0.5.2',26,8)w=0 end function update()w=w+1 if w>90 then cls()load()end end"; void lua_init(const char* filename, const bool start_playing) { if (lua_state != STATE_STOPPED) lua_quit(); L = luaL_newstate(); init_exists = update_exists = false; bool file_loaded = true; if (filename == NULL) { if (luaL_loadstring(L, boot)) { debug("BOOT ERROR"); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); file_loaded = false; } } else { if (luaL_loadfile(L, filename)) { debug("ERROR LOADING GAME"); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); file_loaded = false; } } lua_pushcfunction(L,cpp_cls); lua_setglobal(L, "cls"); lua_pushcfunction(L,cpp_ink); lua_setglobal(L, "ink"); lua_pushcfunction(L,cpp_paper); lua_setglobal(L, "paper"); lua_pushcfunction(L,cpp_color); lua_setglobal(L, "color"); lua_pushcfunction(L,cpp_locate); lua_setglobal(L, "locate"); lua_pushcfunction(L,cpp_print); lua_setglobal(L, "print"); lua_pushcfunction(L,cpp_btn); lua_setglobal(L, "btn"); lua_pushcfunction(L,cpp_btnp); lua_setglobal(L, "btnp"); lua_pushcfunction(L,cpp_mousex); lua_setglobal(L, "mousex"); lua_pushcfunction(L,cpp_mousey); lua_setglobal(L, "mousey"); lua_pushcfunction(L,cpp_mousewheel); lua_setglobal(L, "mousewheel"); lua_pushcfunction(L,cpp_mousebutton); lua_setglobal(L, "mousebutton"); lua_pushcfunction(L,cpp_time); lua_setglobal(L, "time"); lua_pushcfunction(L,cpp_abs); lua_setglobal(L, "abs"); lua_pushcfunction(L,cpp_ceil); lua_setglobal(L, "ceil"); lua_pushcfunction(L,cpp_flr); lua_setglobal(L, "flr"); lua_pushcfunction(L,cpp_sgn); lua_setglobal(L, "sgn"); lua_pushcfunction(L,cpp_sin); lua_setglobal(L, "sin"); lua_pushcfunction(L,cpp_cos); lua_setglobal(L, "cos"); lua_pushcfunction(L,cpp_atan2); lua_setglobal(L, "atan2"); lua_pushcfunction(L,cpp_sqrt); lua_setglobal(L, "sqrt"); lua_pushcfunction(L,cpp_max); lua_setglobal(L, "max"); lua_pushcfunction(L,cpp_mid); lua_setglobal(L, "mid"); lua_pushcfunction(L,cpp_min); lua_setglobal(L, "min"); lua_pushcfunction(L,cpp_rnd); lua_setglobal(L, "rnd"); lua_pushcfunction(L,cpp_srand); lua_setglobal(L, "srand"); lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr"); lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii"); lua_pushcfunction(L,cpp_chr); lua_setglobal(L, "chr"); lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen"); lua_pushcfunction(L,cpp_setchar); lua_setglobal(L, "setchar"); lua_pushcfunction(L,cpp_peek); lua_setglobal(L, "peek"); lua_pushcfunction(L,cpp_poke); lua_setglobal(L, "poke"); lua_pushcfunction(L,cpp_memcpy); lua_setglobal(L, "memcpy"); lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound"); lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound"); lua_pushcfunction(L,cpp_play); lua_setglobal(L, "play"); lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode"); lua_pushcfunction(L,cpp_load); lua_setglobal(L, "load"); lua_pushcfunction(L,cpp_log); lua_setglobal(L, "log"); lua_pushcfunction(L,cpp_fileout); lua_setglobal(L, "fileout"); lua_pushcfunction(L,cpp_filein); lua_setglobal(L, "filein"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); lua_pushinteger(L, 5); lua_setglobal(L, "KEY_B"); lua_pushinteger(L, 6); lua_setglobal(L, "KEY_C"); lua_pushinteger(L, 7); lua_setglobal(L, "KEY_D"); lua_pushinteger(L, 8); lua_setglobal(L, "KEY_E"); lua_pushinteger(L, 9); lua_setglobal(L, "KEY_F"); lua_pushinteger(L, 10); lua_setglobal(L, "KEY_G"); lua_pushinteger(L, 11); lua_setglobal(L, "KEY_H"); lua_pushinteger(L, 12); lua_setglobal(L, "KEY_I"); lua_pushinteger(L, 13); lua_setglobal(L, "KEY_J"); lua_pushinteger(L, 14); lua_setglobal(L, "KEY_K"); lua_pushinteger(L, 15); lua_setglobal(L, "KEY_L"); lua_pushinteger(L, 16); lua_setglobal(L, "KEY_M"); lua_pushinteger(L, 17); lua_setglobal(L, "KEY_N"); lua_pushinteger(L, 18); lua_setglobal(L, "KEY_O"); lua_pushinteger(L, 19); lua_setglobal(L, "KEY_P"); lua_pushinteger(L, 20); lua_setglobal(L, "KEY_Q"); lua_pushinteger(L, 21); lua_setglobal(L, "KEY_R"); lua_pushinteger(L, 22); lua_setglobal(L, "KEY_S"); lua_pushinteger(L, 23); lua_setglobal(L, "KEY_T"); lua_pushinteger(L, 24); lua_setglobal(L, "KEY_U"); lua_pushinteger(L, 25); lua_setglobal(L, "KEY_V"); lua_pushinteger(L, 26); lua_setglobal(L, "KEY_W"); lua_pushinteger(L, 27); lua_setglobal(L, "KEY_X"); lua_pushinteger(L, 28); lua_setglobal(L, "KEY_Y"); lua_pushinteger(L, 29); lua_setglobal(L, "KEY_Z"); lua_pushinteger(L, 30); lua_setglobal(L, "KEY_1"); lua_pushinteger(L, 31); lua_setglobal(L, "KEY_2"); lua_pushinteger(L, 32); lua_setglobal(L, "KEY_3"); lua_pushinteger(L, 33); lua_setglobal(L, "KEY_4"); lua_pushinteger(L, 34); lua_setglobal(L, "KEY_5"); lua_pushinteger(L, 35); lua_setglobal(L, "KEY_6"); lua_pushinteger(L, 36); lua_setglobal(L, "KEY_7"); lua_pushinteger(L, 37); lua_setglobal(L, "KEY_8"); lua_pushinteger(L, 38); lua_setglobal(L, "KEY_9"); lua_pushinteger(L, 39); lua_setglobal(L, "KEY_0"); lua_pushinteger(L, 40); lua_setglobal(L, "KEY_RETURN"); lua_pushinteger(L, 41); lua_setglobal(L, "KEY_ESCAPE"); lua_pushinteger(L, 42); lua_setglobal(L, "KEY_BACKSPACE"); lua_pushinteger(L, 43); lua_setglobal(L, "KEY_TAB"); lua_pushinteger(L, 44); lua_setglobal(L, "KEY_SPACE"); lua_pushinteger(L, 45); lua_setglobal(L, "KEY_MINUS"); lua_pushinteger(L, 46); lua_setglobal(L, "KEY_EQUALS"); lua_pushinteger(L, 47); lua_setglobal(L, "KEY_LEFTBRACKET"); lua_pushinteger(L, 48); lua_setglobal(L, "KEY_RIGHTBRACKET"); lua_pushinteger(L, 49); lua_setglobal(L, "KEY_BACKSLASH"); lua_pushinteger(L, 50); lua_setglobal(L, "KEY_NONUSHASH"); lua_pushinteger(L, 51); lua_setglobal(L, "KEY_SEMICOLON"); lua_pushinteger(L, 52); lua_setglobal(L, "KEY_APOSTROPHE"); lua_pushinteger(L, 53); lua_setglobal(L, "KEY_GRAVE"); lua_pushinteger(L, 54); lua_setglobal(L, "KEY_COMMA"); lua_pushinteger(L, 55); lua_setglobal(L, "KEY_PERIOD"); lua_pushinteger(L, 56); lua_setglobal(L, "KEY_SLASH"); lua_pushinteger(L, 57); lua_setglobal(L, "KEY_CAPSLOCK"); lua_pushinteger(L, 58); lua_setglobal(L, "KEY_F1"); lua_pushinteger(L, 59); lua_setglobal(L, "KEY_F2"); lua_pushinteger(L, 60); lua_setglobal(L, "KEY_F3"); lua_pushinteger(L, 61); lua_setglobal(L, "KEY_F4"); lua_pushinteger(L, 62); lua_setglobal(L, "KEY_F5"); lua_pushinteger(L, 63); lua_setglobal(L, "KEY_F6"); lua_pushinteger(L, 64); lua_setglobal(L, "KEY_F7"); lua_pushinteger(L, 65); lua_setglobal(L, "KEY_F8"); lua_pushinteger(L, 66); lua_setglobal(L, "KEY_F9"); lua_pushinteger(L, 67); lua_setglobal(L, "KEY_F10"); lua_pushinteger(L, 68); lua_setglobal(L, "KEY_F11"); lua_pushinteger(L, 69); lua_setglobal(L, "KEY_F12"); lua_pushinteger(L, 70); lua_setglobal(L, "KEY_PRINTSCREEN"); lua_pushinteger(L, 71); lua_setglobal(L, "KEY_SCROLLLOCK"); lua_pushinteger(L, 72); lua_setglobal(L, "KEY_PAUSE"); lua_pushinteger(L, 73); lua_setglobal(L, "KEY_INSERT"); lua_pushinteger(L, 74); lua_setglobal(L, "KEY_HOME"); lua_pushinteger(L, 75); lua_setglobal(L, "KEY_PAGEUP"); lua_pushinteger(L, 76); lua_setglobal(L, "KEY_DELETE"); lua_pushinteger(L, 77); lua_setglobal(L, "KEY_END"); lua_pushinteger(L, 78); lua_setglobal(L, "KEY_PAGEDOWN"); lua_pushinteger(L, 79); lua_setglobal(L, "KEY_RIGHT"); lua_pushinteger(L, 80); lua_setglobal(L, "KEY_LEFT"); lua_pushinteger(L, 81); lua_setglobal(L, "KEY_DOWN"); lua_pushinteger(L, 82); lua_setglobal(L, "KEY_UP"); lua_pushinteger(L, 83); lua_setglobal(L, "KEY_NUMLOCKCLEAR"); lua_pushinteger(L, 84); lua_setglobal(L, "KEY_KP_DIVIDE"); lua_pushinteger(L, 85); lua_setglobal(L, "KEY_KP_MULTIPLY"); lua_pushinteger(L, 86); lua_setglobal(L, "KEY_KP_MINUS"); lua_pushinteger(L, 87); lua_setglobal(L, "KEY_KP_PLUS"); lua_pushinteger(L, 88); lua_setglobal(L, "KEY_KP_ENTER"); lua_pushinteger(L, 89); lua_setglobal(L, "KEY_KP_1"); lua_pushinteger(L, 90); lua_setglobal(L, "KEY_KP_2"); lua_pushinteger(L, 91); lua_setglobal(L, "KEY_KP_3"); lua_pushinteger(L, 92); lua_setglobal(L, "KEY_KP_4"); lua_pushinteger(L, 93); lua_setglobal(L, "KEY_KP_5"); lua_pushinteger(L, 94); lua_setglobal(L, "KEY_KP_6"); lua_pushinteger(L, 95); lua_setglobal(L, "KEY_KP_7"); lua_pushinteger(L, 96); lua_setglobal(L, "KEY_KP_8"); lua_pushinteger(L, 97); lua_setglobal(L, "KEY_KP_9"); lua_pushinteger(L, 98); lua_setglobal(L, "KEY_KP_0"); lua_pushinteger(L, 99); lua_setglobal(L, "KEY_KP_PERIOD"); lua_pushinteger(L, 100); lua_setglobal(L, "KEY_NONUSBACKSLASH"); lua_pushinteger(L, 101); lua_setglobal(L, "KEY_APPLICATION"); lua_pushinteger(L, 224); lua_setglobal(L, "KEY_LCTRL"); lua_pushinteger(L, 225); lua_setglobal(L, "KEY_LSHIFT"); lua_pushinteger(L, 226); lua_setglobal(L, "KEY_LALT"); lua_pushinteger(L, 227); lua_setglobal(L, "KEY_LGUI"); lua_pushinteger(L, 228); lua_setglobal(L, "KEY_RCTRL"); lua_pushinteger(L, 229); lua_setglobal(L, "KEY_RSHIFT"); lua_pushinteger(L, 230); lua_setglobal(L, "KEY_RALT"); lua_pushinteger(L, 231); lua_setglobal(L, "KEY_RGUI"); lua_pushinteger(L, 0); lua_setglobal(L, "COLOR_BLACK"); lua_pushinteger(L, 1); lua_setglobal(L, "COLOR_BLUE"); lua_pushinteger(L, 2); lua_setglobal(L, "COLOR_GREEN"); lua_pushinteger(L, 3); lua_setglobal(L, "COLOR_CYAN"); lua_pushinteger(L, 4); lua_setglobal(L, "COLOR_RED"); lua_pushinteger(L, 5); lua_setglobal(L, "COLOR_MAGENTA"); lua_pushinteger(L, 6); lua_setglobal(L, "COLOR_BROWN"); lua_pushinteger(L, 7); lua_setglobal(L, "COLOR_LIGHT_GRAY"); lua_pushinteger(L, 8); lua_setglobal(L, "COLOR_DARK_GRAY"); lua_pushinteger(L, 9); lua_setglobal(L, "COLOR_LIGHT_BLUE"); lua_pushinteger(L, 10); lua_setglobal(L, "COLOR_LIGHT_GREEN"); lua_pushinteger(L, 11); lua_setglobal(L, "COLOR_LIGHT_CYAN"); lua_pushinteger(L, 12); lua_setglobal(L, "COLOR_LIGHT_RED"); lua_pushinteger(L, 13); lua_setglobal(L, "COLOR_LIGHT_MAGENTA"); lua_pushinteger(L, 14); lua_setglobal(L, "COLOR_YELLOW"); lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE"); if (!file_loaded) return; if (lua_pcall(L,0, LUA_MULTRET, 0)) { debug("RUNTIME ERROR"); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); return; } lua_getglobal(L, "init"); if (lua_isfunction(L,-1)) init_exists = true; lua_pop(L,1); lua_getglobal(L, "update"); if (lua_isfunction(L,-1)) update_exists = true; lua_pop(L,1); lua_state = start_playing ? STATE_PLAYING : STATE_PAUSED; } void lua_call_cmd(const char* str) { if (luaL_dostring(L, str)) { debug(" "); debug("ERROR"); debug(lua_tostring(L, -1)); lua_pop(L,1); } debug(" "); } void lua_call_init() { if (!init_exists) return; lua_getglobal(L, "init"); if (lua_pcall(L, 0, 0, 0)) { debug("RUNTIME ERROR"); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); lua_state = STATE_STOPPED; } } void lua_call_update() { if (!update_exists) return; lua_getglobal(L, "update"); if (lua_pcall(L, 0, 0, 0)) { debug("RUNTIME ERROR"); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); lua_state = STATE_STOPPED; } } void lua_quit() { if (lua_state == STATE_STOPPED) return; lua_state = STATE_STOPPED; lua_close(L); } void lua_pause() { lua_state = STATE_PAUSED; } void lua_resume() { lua_state = STATE_PLAYING; }