commit b1e1da4abf8cf346eb688bd4af6e5615804c856a
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sun Nov 15 18:42:36 2020 -0700
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sun Nov 15 18:42:36 2020 -0700
fix calling rnd with a table argument
---
source/picoluaapi.cpp | 15 ++++++--
test/carts/tablerndtest.p8 | 91 ++++++++++++++++++++++++++++++++++++++++++++++
test/endtoendtests.cpp | 6 +++
3 files changed, 109 insertions(+), 3 deletions(-)
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 14bbfa9..aa52ae1 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -752,10 +752,19 @@ int rnd(lua_State *L) {
lua_pushnumber(L, val);
}
else {
- fix32 range = lua_tonumber(L,1);
- fix32 val = _vmForLuaApi->api_rnd(range);
+ if (lua_istable(L, 1)){
+ size_t len = lua_rawlen(L,1);
+ fix32 range = (fix32)len;
+ int idx = (int)(_vmForLuaApi->api_rnd(range)) + 1;
+
+ lua_rawgeti(L, 1, idx);
+ }
+ else {
+ fix32 range = lua_tonumber(L,1);
+ fix32 val = _vmForLuaApi->api_rnd(range);
- lua_pushnumber(L, val);
+ lua_pushnumber(L, val);
+ }
}
return 1;
diff --git a/test/carts/tablerndtest.p8 b/test/carts/tablerndtest.p8
new file mode 100644
index 0000000..2d1507b
--- /dev/null
+++ b/test/carts/tablerndtest.p8
@@ -0,0 +1,91 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+
+npctypes={
+ {
+ name="q.bALLE",
+ ac=0.01,
+ pow={0.95,2},
+ },
+ {
+ name="i.bULLS",
+ ac=0.005,
+ pow={1,1.2},
+ think={1.5,2.5}
+ },
+ {
+ name="nOKOI",
+ ac=0.02,
+ pow={0.5,2.0},
+ think={0.25,2.5},
+ rand=0.5,
+ ang=0
+ },
+ {
+ name="sPIDEY",
+ ac=0.0075,
+ pow={1,1.1},
+ think={2,3},
+ ang=-0.1
+ },
+ {
+ name="pkCYANIDE",
+ ac=0.01,
+ pow={1.5,2.5},
+ rand=0.015
+ },
+ {
+ name="jIGGS",
+ ac=0.015,
+ pow={1.5,2.0},
+ think={0.5,1},
+ rand=0.01
+ },
+ {
+ name="yOURYkIkI",
+ ac=0.0075,
+ pow={0.9,1.4},
+ ang=-1
+ }
+}
+
+function _init()
+ newgame(rnd(npctypes))
+end
+
+function _draw()
+ cls()
+ print(players[1].name)
+ print(players[2].name)
+end
+
+function newgame(npc,npc2)
+ players={
+ {
+ name=npc2 and npc2.name or "player1",
+ ballct=0,
+ npc=npc2
+ },
+ {
+ name=npc and npc.name or "player2",
+ ballct=0,
+ npc=npc
+ }
+ }
+
+end
+
+__gfx__
+ff102030555555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__gff__
+0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__map__
+0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+__sfx__
+001d00000d0300e0500e0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+0010000036050310502a05019050192501925019250192501925019250192501925019250192501b2501b250192501b2501b2501b2501b2501b2501b2501b2501b2501925019250192501b250000000000000000
+__music__
+00 01424344
+
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 191e1c3..a332d8b 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -423,6 +423,12 @@ TEST_CASE("Loading and running carts") {
CHECK(vm->GetBiosError() == "");
}
}
+ SUBCASE("rnd with table argument works") {
+ vm->LoadCart("carts/tablerndtest.p8");
+ SUBCASE("No error reported"){
+ CHECK(vm->GetBiosError() == "");
+ }
+ }
delete vm;
}
\ No newline at end of file