foxygit / fake-08-xbox360 Log in
commit 247ca5f2c621fddf5ffc3edfe8116604f3914e74
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Apr 9 09:02:50 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Apr 9 09:02:50 2022 -0600

    Update poke2 and poke4 and map behavior

    poke2 and poke4 optionally accept multiple values
    map() defaults to larger values if width or map size changed
---
 source/picoluaapi.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 5 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index b9d8266..53a9fcb 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -21,14 +21,16 @@ Graphics* _graphicsForLuaApi;
 Input* _inputForLuaApi;
 Vm* _vmForLuaApi;
 Audio* _audioForLuaApi;
+PicoRam* _ramForLuaApi;

 void initPicoApi(PicoRam* memory, Graphics* graphics, Input* input, Vm* vm, Audio* audio){
     _graphicsForLuaApi = graphics;
     _inputForLuaApi = input;
     _vmForLuaApi = vm;
     _audioForLuaApi = audio;
+    _ramForLuaApi = memory;

-    initPrintHelper(memory, _graphicsForLuaApi, _vmForLuaApi, _audioForLuaApi);
+    initPrintHelper(_ramForLuaApi, _graphicsForLuaApi, _vmForLuaApi, _audioForLuaApi);
 }

 int noop(const char * name) {
@@ -538,7 +540,16 @@ int mset(lua_State *L) {
 }

 int gfx_map(lua_State *L) {
-    int celx = 0, cely = 0, sx = 0, sy = 0, celw = 128, celh = 32, argc;
+    const bool bigMap = _ramForLuaApi->hwState.mapMemMapping >= 0x80;
+	const int bigMapLocation = _ramForLuaApi->hwState.mapMemMapping << 8;
+	const int mapSize = bigMap
+		? 0x10000 - bigMapLocation
+		: 8192;
+
+	const int mapW = _ramForLuaApi->hwState.widthOfTheMap == 0 ? 256 : _ramForLuaApi->hwState.widthOfTheMap;
+	const int mapH = mapSize / mapW;
+
+    int celx = 0, cely = 0, sx = 0, sy = 0, celw = mapW, celh = mapH, argc;
     argc = lua_gettop(L);
     if (argc > 0) {
         celx = lua_tonumber(L,1);
@@ -1015,7 +1026,10 @@ int poke(lua_State *L) {
     int numArgs = lua_gettop(L);

     int dest = lua_tonumber(L,1);
-    uint8_t val = lua_tonumber(L,2);
+    uint8_t val = 0;
+    if (numArgs > 1) {
+        val = lua_tonumber(L,2);
+    }

     _vmForLuaApi->vm_poke(dest, val);

@@ -1040,11 +1054,24 @@ int peek2(lua_State *L) {
 }

 int poke2(lua_State *L) {
+    int numArgs = lua_gettop(L);
+
     int dest = lua_tonumber(L,1);
-    int val = lua_tonumber(L,2);
+
+    int val = 0;
+    if (numArgs > 1) {
+        val = lua_tonumber(L,2);
+    }

     _vmForLuaApi->vm_poke2(dest, (int16_t)val);

+    if (numArgs > 2) {
+        for(int i = 1; i <= (numArgs - 2); i++) {
+            val = lua_tonumber(L, 2 + i);
+            _vmForLuaApi->vm_poke2(dest + i, (int16_t)val);
+        }
+    }
+
     return 0;
 }

@@ -1059,11 +1086,24 @@ int peek4(lua_State *L) {
 }

 int poke4(lua_State *L) {
+    int numArgs = lua_gettop(L);
+
     int dest = lua_tonumber(L,1);
-    fix32 val = lua_tonumber(L,2);
+
+    fix32 val = 0;
+    if (numArgs > 1) {
+        val = lua_tonumber(L,2);
+    }

     _vmForLuaApi->vm_poke4(dest, val);

+    if (numArgs > 2) {
+        for(int i = 1; i <= (numArgs - 2); i++) {
+            val = lua_tonumber(L, 2 + i);
+            _vmForLuaApi->vm_poke4(dest + i, val);
+        }
+    }
+
     return 0;
 }