foxygit / fake-08-xbox360 Log in
commit 591ef7b9b1fe9a14483b4c9a179a587a4f1bd1d0
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Apr 25 17:22:22 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Apr 25 17:22:22 2020 -0600

    Add more helpers to lua functions
---
 source/p8GlobalLuaFunctions.h | 108 ++++++++++++++++++++++++++++++++++++++++++
 testcart.p8                   |   6 +--
 2 files changed, 111 insertions(+), 3 deletions(-)

diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 2058cdb..8b27913 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -7,6 +7,9 @@

 const char * p8GlobalLuaFunctions = R"#(
 -- The functions below are normally attached to the program code, but are here for simplicity
+---------------------------------
+--Table Helpers
+---------------------------------
 function all(a)
 	if a==nil or #a==0 then
 		return function() end
@@ -49,10 +52,115 @@ function del(a, dv)
 	end
 end

+---------------------------------
+--Coroutines
+---------------------------------
 yield = coroutine.yield
 cocreate = coroutine.create
 coresume = coroutine.resume
 costatus = coroutine.status
+
+---------------------------------
+--strings
+---------------------------------
+sub = string.sub
+
+---------------------------------
+--Debug
+---------------------------------
+trace = debug.traceback
+
+---------------------------------
+--Math
+---------------------------------
+function rnd(x)
+	return math.random()*(x or 1)
+end
+
+function srand(seed)
+	math.randomseed(flr(seed*0x10000))
+end
+
+flr=math.floor
+ceil=math.ceil
+
+function sgn(x)
+	return x<0 and-1 or 1
+end
+
+abs=math.abs
+
+function min(a, b)
+	if a==nil or b==nil then
+		warning('min a or b are nil returning 0')
+		return 0
+	end
+	if a<b then return a end
+	return b
+end
+
+function max(a, b)
+	if a==nil or b==nil then
+		warning('max a or b are nil returning 0')
+		return 0
+	end
+	if a>b then return a end
+	return b
+end
+
+function mid(x, y, z)
+	return (x<=y)and((y<=z)and y or((x<z)and z or x))or((x<=z)and x or((y<z)and z or y))
+end
+
+function cos(x)
+	return math.cos((x or 0)*math.pi*2)
+end
+
+function sin(x)
+	return-math.sin((x or 0)*math.pi*2)
+end
+
+sqrt=math.sqrt
+
+function atan2(x, y)
+	return (0.75 + math.atan2(x,y) / (math.pi * 2)) % 1.0
+end
+
+function band(x, y)
+	return bit32.band(x*0x10000, y*0x10000)/0x10000
+end
+
+function bor(x, y)
+	return bit32.bor(x*0x10000, y*0x10000)/0x10000
+end
+
+function bxor(x, y)
+	return bit32.bxor(x*0x10000, y*0x10000)/0x10000
+end
+
+function bnot(x)
+	return bit32.bnot(x*0x10000)/0x10000
+end
+
+function shl(x, y)
+	return bit32.lshift(x*0x10000, y)/0x10000
+end
+
+function shr(x, y)
+	return bit32.arshift(x*0x10000, y)/0x10000
+end
+
+function lshr(x, y)
+	return bit32.rshift(x*0x10000, y)/0x10000
+end
+
+function rotl(x, y)
+	return bit32.rol(x*0x10000, y)/0x10000
+end
+
+function rotr(x, y)
+	return bit32.ror(x*0x10000, y)/0x10000
+end
 )#";


diff --git a/testcart.p8 b/testcart.p8
index 118c7c1..e9ee363 100644
--- a/testcart.p8
+++ b/testcart.p8
@@ -68,7 +68,7 @@ function _draw()

  circfill(100, 100, 15, 9)

- print(fget(1), 64, 64, 7)
+ print("bitwise:1&3 " .. band(1,3), 64, 64, 7)
  print(fget(8, 3), 64, 72, 7)
  print(fget(255), 64, 80, 7)

@@ -96,8 +96,8 @@ function _draw()

  pal(6, 14, 1)

- t = {1, 3, 5}
- add(t, 7)
+ t = {1, 3}
+ add(t, 5)
  foreach(t, print)
 end