Adding Miau's SMB-RainbowRiding and a Lua Function listing

This commit is contained in:
adelikat 2009-03-29 15:04:39 +00:00
parent bbbfffc95c
commit 22c8ec62b0
2 changed files with 954 additions and 0 deletions

View File

@ -0,0 +1,638 @@
SCRIPT_TITLE = "Super Mario Bros. 3 Rainbow Riding"
SCRIPT_VERSION = "0.1"
require "m_utils"
m_require("m_utils",0)
--[[
Super Mario Bros. 3 Rainbow Riding
version 0.1 by miau
Visit http://morphcat.de/lua/ for the most recent version and other scripts.
This script turns smb3 into a new game very similar to Kirby's Canvas Curse.
It's still incomplete, messy and full of bugs, so don't expect too much. Next version
will fix most of that... hopefully.
Probably slow on old computers, you may want to turn down emulator speed anyway to
decrease difficulty.
Controls
Left-click on mario - jump
Middle-click anywhere - run
Draw vertical lines to make mario change his walking direction
Draw horizontal lines to help mario move over obstacles
Supported roms
Super Mario Bros 3 (J), Super Mario Bros 3 (U) (PRG 0), Super Mario Bros 3 (U) (PRG 1),
Super Mario Bros 3 (E)
Known Bugs/TODO list
Too many to list em all, actually...
- game objects are occasionally catapulted out of screen - bad divisions!?
- mario falling through lines (clean up collision detection)
- long vertically scrolling levels won't work
- boss battles are glitchy
- disable auto move in mini games?
- option to disable auto move?
- make collision detection work with fire balls (fire mario or fire piranha plants)
- improve map screen check
- add easy mode: decrease mario's speed
- improve enemy hit boxes
- (add suicide button in case mario gets stuck)
-]]
--configurable vars
local show_cursor = false
--------------------------------------------------------------------------------------------
local Z_LSPAN = 400 --240
local Z_MAX = 128 --256 --maximum amount of lines on screen
local NUM_SPRITES = 20
local zbuf = {}
local zindex = 1
local timer = 0
local zprev = 0
local last_inp={}
local spr = {} --game's original sprites
local mario = {}
local clickbox={x1=-4,y1=4,x2=18,y2=32}
local collx = 0
local colly = 0
--local coll = {} --debug
local paintmeter = 0
local lastpaint = -100
local jp = {}
--accepts two tables containing these elements [1]=x1, [2]=y1, [3]=x2, [4]=y2
function get_line_intersection(a,b)
local Asx,Asy,Bsx,Bsy,s,t
local Ax1,Ay1,Ax2,Ay2
local Bx1,By1,Bx2,By2
Ax1 = a[1]
Ay1 = a[2]
Ax2 = a[3]
Ay2 = a[4]
Bx1 = b[1]
By1 = b[2]
Bx2 = b[3]
By2 = b[4]
Asx = Ax2 - Ax1
Asy = Ay2 - Ay1
Bsx = Bx2 - Bx1
Bsy = By2 - By1
s = (-Asy * (Ax1 - Bx1) + Asx * (Ay1 - By1)) / (-Bsx*Asy + Asx*Bsy)
t = (Bsx * (Ay1 - By1) - Bsy * (Ax1 - Bx1)) / (-Bsx*Asy + Asx*Bsy)
if(s>=0 and s<=1 and t>=0 and t<=1) then
local Ix,Iy
Ix = Ax1 + t * Asx
Iy = Ay1 + t * Asy
return Ix,Iy
else
return nil
end
end
function close_to_line(Px,Py,a,range) --a[4] line segment
local Ax=a[1]
local Ay=a[2]
local Bx=a[3]
local By=a[4]
local APx = Px - Ax
local APy = Py - Ay
local ABx = Bx - Ax
local ABy = By - Ay
local absq = ABx*ABx + ABy*ABy
local apab = APx*ABx + APy*ABy
local t = apab / absq
if (t < 0.0) then
t = 0.0
elseif (t > 1.0) then
t = 1.0
end
local Cx,Cy
Cx = Ax + ABx * t
Cy = Ay + ABy * t
if(getdistance(Px,Py,Cx,Cy)<=range) then
return true
else
return false
end
end
function ride_line(s,xoffs,yoffs)
local function move(zx1,zy1,zx2,zy2)
local avx,avy,cvx,cvy
cvx,cvy = getvdir(zx1,zy1,zx2,zy2)
if(math.abs(spr[s].vx)<math.abs(spr[s].vy)) then
avx = spr[s].vx
avy = spr[s].vx
else
avx = spr[s].vy
avy = spr[s].vy
end
if(spr[s].movetimer) then
avx = cvx*5
avy = cvy*5
else
avx = cvx*2.5
avy = cvy*3
end
set_sprite_velocity(s,avx,avy)
--coll.a = {spr[s].x+xoffs,spr[s].y+yoffs-1,spr[s].x+xoffs+avx*64,spr[s].y+yoffs+avy*64-1}
end
local function getcoords(i)
local j
j = i - 1
if(j < 1) then
j = Z_MAX
end
if(zbuf[i] and zbuf[j]) then
return zbuf[j].x,zbuf[j].y,zbuf[i].x,zbuf[i].y
else
return nil
end
end
local function domovemagic(x,y,firstline)
local zx1,zy1,zx2,zy2 = getcoords(spr[s].riding)
if(zx1 and close_to_line(x,y,{zx1,zy1,zx2,zy2},5)) then
move(zx1,zy1,zx2,zy2)
else
--get next line.. check for collision
spr[s].riding = spr[s].riding + 1
if(spr[s].riding > Z_MAX) then
spr[s].riding = 1
end
if(spr[s].riding==firstline) then
spr[s].riding=nil
return
end
domovemagic(x,y,firstline)
end
end
if(spr[s].riding==nil) then
return
end
local x = spr[s].x+xoffs
local y = spr[s].y+yoffs
domovemagic(x,y,spr[s].riding)
end
function collisioncheck(s)
local c=false
local cvx,cvy
local avx,avy
local function checkpixel(xoffs,yoffs,i,j)
local x = spr[s].x+xoffs
local y = spr[s].y+yoffs
local px2 = x+spr[s].vx
local py2 = y+spr[s].vy
local px1 = x
local py1 = y
local zx = zbuf[i].x
local zy = zbuf[i].y
local zx2 = zbuf[j].x
local zy2 = zbuf[j].y
if(spr[s].vx~=0 or spr[s].vy~=0) then
if(spr[s].vx>0) then --we need at least one pixel!!?
px2 = px2 + 2
elseif(spr[s].vx<0) then
px2 = px2 - 2
end
if(spr[s].vy>0) then
py2 = py2 + 2
elseif(spr[s].vy<0) then
py2 = py2 - 2
end
local cx,cy = get_line_intersection({px1,py1,px2,py2},{zx,zy,zx2,zy2})
if(cx~=nil) then
cx = math.floor(cx)
cy = math.floor(cy)
local avx,avy,cvx,cvy
--coll = {--[[a={px1,py1,px2+spr[s].vx*64,py2+spr[s].vy*64},-]]b={zbuf[i].x,zbuf[i].y,zbuf[j].x,zbuf[j].y}} --debug
collx = cx
colly = cy
avx,avy = getvdir(px1,py1,px2,py2)
cvx,cvy = getvdir(zbuf[j].x,zbuf[j].y,zbuf[i].x,zbuf[i].y)
local x,y
x = cx-xoffs
y = cy-yoffs
if(spr[s].x==x and spr[s].y==y) then
--FCEU.message("boo"..timer)
else
set_sprite_pos(s,x,y)
end
if(s==0) then --mario/luigi
if(math.abs(cvy)==1 and math.abs(cvx) < 0.5) then
change_sprite_dir(s)
set_sprite_velocity(s,-avx,nil)
else
spr[s].riding = i
set_sprite_velocity(s,0,0)
end
else --enemies, moving platforms
end
return true
end
else
if(close_to_line(x,y,{zx,zy,zx2,zy2},4)) then
if(s==0) then
spr[s].riding = i
set_sprite_velocity(s,0,0)
end
return true
else
spr[s].riding = nil
end
end
return false
end
if(spr[s].riding) then
ride_line(s,8,30)
return
end
for i=1,Z_MAX do
if(zbuf[i] and zbuf[i].connected) then
--if(zbuf[i].x >= spr[s].x+hitbox.x1 and zbuf[i].y >= spr[s].y+hitbox.y1
-- and zbuf[i].x <= spr[s].x+hitbox.x2 and zbuf[i].y <= spr[s].y+hitbox.y2) then
--local px = spr[s].x-spr[s].vx
--local py = spr[s].y-spr[s].vy
local j
j = i - 1
if(j < 1) then
j = Z_MAX
end
if(zbuf[j]) then --check if line is still valid (if nil, node disappeared)
if(s==0) then --mario
if(checkpixel(8,30,i,j)) then
return
end
else
if(checkpixel(0,0,i,j) or checkpixel(8,8,i,j)
--[[or checkpixel(0,8,i,j) or checkpixel(8,0,i,j)-]]) then
destroy_sprite(s)
return
end
end
end
--end
end
end
end
function screen_to_game_pos(x,y)
return x+scroll_x,y+scroll_y
end
function game_to_screen_pos(x,y)
return x-scroll_x,y-scroll_y
end
function destroy_sprite(s)
if(s<10) then
memory.writebyte(0x660+s,0x06)
--memory.writebyte(0x660+s,0x00)
--set_sprite_velocity(s,10,10)
else
memory.writebyte(0x6C7+s-10,0x01)
--set_sprite_pos(s,0,0)
end
end
function set_sprite_velocity(s,vx,vy)
if(s<10) then
if(s==0) then
memory.writebyte(0xD8,1) --air flag?
end
if(vx) then
memory.writebyte(0xBD+s,vx*16)
spr[s].vx = vx
end
if(vy) then
memory.writebyte(0xCF+s,vy*16)
spr[s].vy = vy
end
end
end
function set_sprite_pos(i,x,y)
memory.writebyte(0x90+i,AND(x,255))
memory.writebyte(0x75+i,math.floor(x/256))
memory.writebyte(0xA2+i,AND(y,255))
memory.writebyte(0x87+i,math.floor(y/256))
spr[i].x = x
spr[i].y = y
spr[i].sx,spr[i].sy = game_to_screen_pos(spr[i].x,spr[i].y)
end
function change_sprite_dir(s)
if(spr[s].dir == 1) then
spr[s].dir = -1
elseif(spr[s].dir == -1) then
spr[s].dir = 1
end
end
function add_rainbow_coord(cx,cy,connected)
zbuf[zindex] = {t=timer,x=cx,y=cy,connected=connected}
zprev = zbuf[zindex]
zindex = zindex + 1
if(zindex>Z_MAX) then
zindex = 1
end
end
function drawrainbow(x1,y1,x2,y2,coloffs)
local cx,cy
local vx,vy
local color = coloffs
vx,vy = getvdir(x1,y1,x2,y2)
cx = x1
cy = y1
for i=1,200 do
if(cx>=0 and cy>=0 and cx<=253 and cy<=253) then
local rcolor = color/1.8+161 --165
gui.drawpixel(cx,cy,rcolor)
gui.drawpixel(cx,cy+1,rcolor)
gui.drawpixel(cx+1,cy,rcolor)
gui.drawpixel(cx+1,cy+1,rcolor)
gui.drawpixel(cx+2,cy,rcolor)
gui.drawpixel(cx,cy+2,rcolor)
gui.drawpixel(cx+2,cy+1,rcolor)
gui.drawpixel(cx+1,cy+2,rcolor)
gui.drawpixel(cx+2,cy+2,rcolor)
end
if((x2>x1 and cx>x2) or (x2<x1 and cx<x2) or (y2>y1 and cy>y2) or (y2<y1 and cy<y2)) then
break
end
cx = cx + vx
cy = cy + vy
color = color + 17
color = AND(color,15)
end
return color
end
function initialize()
for i=0,NUM_SPRITES-1 do
spr[i] = {
a=0,
id=0,
id2=0,
x=0,
y=0,
hp=0,
}
end
spr[0].dir = 1
spr[0].lastposchange = -999
end
function update_sprites()
mario = spr[0]
--also change dir when colliding with game objects
local x = memory.readbyte(0x90)+memory.readbyte(0x75)*256
local y = memory.readbyte(0xA2)+memory.readbyte(0x87)*256
if(x~=mario.x or y~=mario.y) then
mario.lastposchange = timer
end
if(mario.dir~=0 and mario.riding==nil and timer-mario.lastposchange>60) then
mario.lastposchange = timer
change_sprite_dir(0)
end
--load game sprites from ram
for i=0,NUM_SPRITES-1 do
if(i<10) then
spr[i].x = memory.readbyte(0x90+i)+memory.readbyte(0x75+i)*256
spr[i].y = memory.readbyte(0xA2+i)+memory.readbyte(0x87+i)*256
spr[i].vx = memory.readbytesigned(0xBD+i)/16
spr[i].vy = memory.readbytesigned(0xCF+i)/16
spr[i].a = (memory.readbytesigned(0x660+i)~=0)
spr[i].id = memory.readbytesigned(0x670+i)
spr[i].sx,spr[i].sy = game_to_screen_pos(spr[i].x,spr[i].y)
else
--TODO: 0x5D3?
local xcomp = memory.readbyte(0xFD)
local ycomp = memory.readbyte(0xFC)
spr[i].x = memory.readbyte(0x5C9+i-10)
spr[i].y = memory.readbyte(0x5BF+i-10)
spr[i].a = true
spr[i].vx = 0
spr[i].vy = 0
spr[i].sx = AND(spr[i].x-xcomp+256,255)
spr[i].sy = AND(spr[i].y-ycomp+256,255)
spr[i].x = spr[i].sx + scroll_x
spr[i].y = spr[i].sy + scroll_y
end
if(spr[i].a) then
collisioncheck(i)
end
end
end
function update_vars()
--disabling input not possible anymore in FCEUX 2.1?
jp = {}
if(mario.riding==nil) then
if(mario.movetimer) then
jp.B = 1
mario.movetimer = mario.movetimer - 1
if(mario.movetimer == 0) then
mario.movetimer = nil
end
end
if(mario.dir==1) then
jp.right=1
elseif(mario.dir==-1) then
jp.left=1
end
--if(AND(timer,1)==0) then --automatically enter doors and pipes... not a very good idea actually :P
--jp.up=1
--if(memory.readbyte(0xD8)==1) then --air flag set? try to stay in air as long as possible... makes it easier to rescue mario if collision detection screws up.. sucks in water levels
-- jp.A=1
--end
--else
-- jp.down=1 --automatically enter pipes
--end
end
inp = input.get()
scroll_x = memory.readbyte(0xFD)+memory.readbyte(0x12)*256
scroll_y = memory.readbyte(0xFC)--+memory.readbyte(0x13)*256 --not quite right, long vertical scrolling levels won't work
--0xD8 = 0 -> touch ground, 1 -> air
update_sprites()
if(inp.middleclick) then
mario.movetimer = 30
end
if(inp.leftclick==nil) then
mario.jumping=false
end
if(inp.leftclick and last_inp.leftclick==nil and
inp.xmouse>=mario.sx+clickbox.x1 and inp.xmouse<=mario.sx+clickbox.x2 and
inp.ymouse>=mario.sy+clickbox.y1 and inp.ymouse<=mario.sy+clickbox.y2)
then
jp.A = 1
mario.jumping = true
elseif(inp.leftclick and mario.jumping) then
jp.A = 1
elseif(inp.leftclick) then
if(paintmeter>0) then
local x,y=screen_to_game_pos(inp.xmouse,inp.ymouse)
if(last_inp.leftclick==nil) then
add_rainbow_coord(x,y,false)
outofpaint = nil
elseif(outofpaint==nil) then
if(zprev and getdistance(x,y,zprev.x,zprev.y)>8) then
add_rainbow_coord(x,y,true)
paintmeter = paintmeter - 2
lastpaint = timer
end
end
else
outofpaint = true
end
end
joypad.set(1,jp)
last_mario = mario
last_inp = inp
end
function render()
--bctext(0,20,string.format("Memory usage: %.2f KB",collectgarbage("count")))
local j = 0
for i=1,Z_MAX do
if(zbuf[i]) then
j = j + 1
end
end
--bctext(0,20,"Lines: "..j)
--bctext(0,20,mario.x..","..mario.y)
--bctext(0,30,mario.sx..","..mario.sy)
--bctext(0,40,mario.vx..","..mario.vy)
--bctext(0,50,"D "..mario.dir)
--bctext(0,70,"scroll_x: "..scroll_x)
--bctext(0,80,"scroll_y: "..scroll_y)
--bctext(0,90,AND(mario.x,255))
--if(mario.riding) then
-- bctext(0,100,"R")
--end
bcbox(mario.sx+clickbox.x1,mario.sy+clickbox.y1,mario.sx+clickbox.x2,mario.sy+clickbox.y2,"white")
local prev_x, prev_y, prev_connected
local i=zindex
local color = AND(timer/2,15)
for j=1,Z_MAX do
if(zbuf[i]) then
local ltime = timer-zbuf[i].t
if(ltime<Z_LSPAN) then
local x,y=game_to_screen_pos(zbuf[i].x,zbuf[i].y)
if(prev_x and prev_connected) then
color = drawrainbow(prev_x,prev_y,x,y,color)
end
prev_x = x
prev_y = y
prev_connected = zbuf[i].connected
else
zbuf[i] = nil
end
end
i = i - 1
if(i<1) then
i = Z_MAX
end
end
local cx,cy = game_to_screen_pos(mario.x+8,mario.y+30)
bcpixel(cx,cy,string.format("#%02X%02X%02X",math.random(0,255),math.random(0,255),math.random(0,255)))
local pmx=78
local pmy=226
if(paintmeter>0) then
drawrainbow(pmx+paintmeter,pmy,pmx,pmy,timer/6)
end
bcbox(pmx-2,pmy-1,pmx+102,pmy+3,"white")
if(timer-lastpaint>60) then
paintmeter = paintmeter + 1
if(paintmeter>100) then
paintmeter=100
end
end
if(show_cursor) then
local col2
if(inp.leftclick) then
col2 = "#FFAA00"
elseif(inp.rightclick) then
col2 = "#0099EE"
elseif(inp.middleclick) then
col2 = "#AACC00"
else
col2 = "white"
end
drawcursor(inp.xmouse,inp.ymouse,"black",col2)
end
end
function domagic()
if(memory.readbyte(0x73)==0x20) then --map screen(?)
update_vars()
render()
else
bcpixel(1,10,"clear")
end
end
initialize()
gui.register(domagic)
while(true) do
FCEU.frameadvance()
timer = timer + 1
end

View File

@ -0,0 +1,316 @@
Library Listing of FCEUX Lua Functions
Written by adelikat/QFox
FCEU library
FCEU.poweron()
Executes a power cycle.
FCEU.softreset()
Executes a (soft) reset.
FCEU.speedmode(string mode)
Set the emulator to given speed. The mode argument can be one of these:
- "normal"
- "nothrottle" (same as turbo on fceux)
- "turbo"
- "maximum"
FCEU.frameadvance()
Advance the emulator by one frame. It's like pressing the frame advance button once.
Most scripts use this function in their main game loop to advance frames. Note that you can also register functions by various methods that run "dead", returning control to the emulator and letting the emulator advance the frame. For most people, using frame advance in an endless while loop is easier to comprehend so I suggest starting with that. This makes more sense when creating bots. Once you move to creating auxillary libraries, try the register() methods.
FCEU.pause()
Pauses the emulator. FCEUX will not unpause until you manually unpause it.
FCEU.exec_count()
FCEU.exec_time()
FCEU.setrenderplanes(bool sprites, bool background)
Toggles the drawing of the sprites and background planes. Set to false or nil to disable a pane, anything else will draw them.
FCEU.message(string message)
Displays given message on screen in the standard messages position. Use gui.text() when you need to position text.
int FCEU.lagcount()
Return the number of lag frames encountered. Lag frames are frames where the game did not poll for input because it missed the vblank. This happens when it has to compute too much within the frame boundary. This returns the number indicated on the lag counter.
bool FCEU.lagged()
Returns true if currently in a lagframe, false otherwise.
bool FCEU.getreadonly()
Returns whether the emulator is in read-only state.
While this variable only applies to movies, it is stored as a global variable and can be modified even without a movie loaded. Hence, it is in the FCEU library rather than the movie library.
FCEU.setreadonly(bool state)
Sets the read-only status to read-only if argument is true and read+write if false.
Note: This might result in an error if the medium of the movie file is not writeable (such as in an archive file).
While this variable only applies to movies, it is stored as a global variable and can be modified even without a movie loaded. Hence, it is in the FCEU library rather than the movie library.
ROM Library
rom.readbyte(int address)
Get an unsigned byte from the actual ROM file at the given address.
This includes the header! It's the same as opening the file in a hex-editor.
rom.readbytesigned(int address)
Get a signed byte from the actual ROM faile at the given address. Returns a byte that is signed.
This includes the header! It's the same as opening the file in a hex-editor.
Memory Library
memory.readbyte(int address)
Get an unsigned byte from the RAM at the given address. Returns a byte regardless of emulator. The byte will always be positive.
memory.readbyterange(int address, int length)
Get a length bytes starting at the given address and return it as a string. Convert to table to access the individual bytes.
memory.readbytesigned(int address)
Get a signed byte from the RAM at the given address. Returns a byte regardless of emulator. The most significant bit will serve as the sign.
memory.writebyte(int address, int value)
Write the value to the RAM at the given address. The value is modded with 256 before writing (so writing 257 will actually write 1). Negative values allowed.
memory.register(int address, function func)
Register an event listener to the given address. The function is called whenever write occurs to this address. One function per address. Can be triggered mid-frame. Set to nil to remove listener. Given function may not call frame advance or any of the savestate functions. Joypad reading/writing is undefined (so don't).
Note: this is slow!
Joypad Library
table joypad.read(int player)
Returns a table containing the buttons pressed by the given player. This takes keyboard inputs, not Lua. The table keys look like this (case sensitive):
up, down, left, right, A, B, start, select
Where a Lua truthvalue true means that the button is set, false means the button is unset. Note that only "false" and "nil" are considered a false value by Lua. Anything else is true, even the number 0.
joypad.set(int player, table input)
Set the inputs for the given player. Table keys look like this (case sensitive):
up, down, left, right, A, B, start, select
There are 3 possible values, true, false, and nil. True will turn the button on, false will turn it off. Nil will leave it unchanged (allowing the user to control it).
table joypad.get()
A alias of joypad.read(). Left in for backwards compatibility with older versions of FCEU/FCEUX.
joypad.write()
A alias of joypad.set(). Left in for backwards compatibility with older versions of FCEU/FCEUX.
Zapper Library
table zapper.read()
Returns the mouse data (which is used to generate zapper input, as well as the arkanoid paddle).
The return table consists of 3 values: xmouse, ymouse, and click. xmouse and ymouse are the x,y coordinates of the cursor in terms of pixels. click represents the mouse click. 0 = no click, 1 = left cick, 2 = right click.
Currently, zapper data is ignored while a movie is playing.
Note: The right-click isn't used in zapper data
Note: The zapper is always controller 2 on the NES so there is no player argument to this function.
Input Library
table input.get()
Reads input from keyboard and mouse. Returns pressed keys and the position of mouse in pixels on game screen. The function returns a table with at least two properties; table.xmouse and table.ymouse. Additionally any of these keys will be set to true if they were held at the time of executing this function:
leftclick, rightclick, middleclick, capslock, numlock, scrolllock, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, backspace, tab, enter, shift, control, alt, pause, escape, space, pageup, pagedown, end, home, left, up, right, down, numpad0, numpad1, numpad2, numpad3, numpad4, numpad5, numpad6, numpad7, numpad8, numpad9, numpad*, insert, delete, numpad+, numpad-, numpad., numpad/, semicolon, plus, minus, comma, period, slash, backslash, tilde, quote, leftbracket, rightbracket.
Savestate Library
object savestate.create(int slot = nil)
Create a new savestate object. Optionally you can save the current state to one of the predefined slots (0...9), otherwise you'll create an "anonymous" savestate.
Note that this does not actually save the current state! You need to create this value and pass it on to the load and save functions in order to save it.
Anonymous savestates are temporary, memory only states. You can make them persistent by calling memory.persistent(state). Persistent anonymous states are deleted from disk once the script exits.
savestate.save(object savestate)
Save the current state object to the given savestate. The argument is the result of savestate.create(). You can load this state back up by calling savestate.load(savestate) on the same object.
savestate.load(object savestate)
Load the the given state. The argument is the result of savestate.create() and has been passed to savestate.save() at least once.
If this savestate is not persistent and not one of the predefined states, the state will be deleted after loading.
savestate.persist(object savestate)
Set the given savestate to be persistent. It will not be deleted when you load this state but at the exit of this script instead, unless it's one of the predefined states. If it is one of the predefined savestates it will be saved as a file on disk.
Movie Library
bool movie.active()
Returns true if a movie is currently loaded and false otherwise. (This should be used to guard against Lua errors when attempting to retrieve movie information).
int movie.framecount()
Returns the framecount value. The frame counter runs without a movie running so this always returns a value.
string movie.mode()
Returns the current state of movie playback. Returns one of the following:
- "record"
- "playback"
- nil
movie.rerecordcounting(bool counting)
Turn the rerecord counter on or off. Allows you to do some brute forcing without inflating the rerecord count.
movie.stop()
Stops movie playback. If no movie is loaded, it throws a Lua error.
int movie.length()
Returns the total number of frames of the current movie. Throws a Lua error if no movie is loaded.
string movie.getname()
Returns the filename of the current movie. Throws a Lua error if no movie is loaded.
movie.rerecordcount()
Returns the rerecord count of the current movie. Throws a Lua error if no movie is loaded.
movie.playbeginning()
Performs the Play from Beginning function. Movie mode is switched to read-only and the movie loaded will begin playback from frame 1.
If no movie is loaded, no error is thrown and no message appears on screen.
GUI Library
gui.drawpixel(int x, int y, type color)
Draw one pixel of a given color at the given position on the screen. See drawing notes and color notes at the bottom of the page.
gui.drawline(int x1, int y1, int x2, int y2, type color)
Draws a line between the two points. See also drawing notes and color notes at the bottom of the page.
gui.drawbox(int x1, int y1, int x2, int y2, type color)
Draw a box with the two given opposite corners.
Also see drawing notes and color notes.
gui.text(int x, int y, string str)
Draws a given string at the given position.
string gui.gdscreenshot()
Takes a screen shot of the image and returns it in the form of a string which can be imported by the gd library using the gd.createFromGdStr() function.
This function is provided so as to allow FCEUX to not carry a copy of the gd library itself. If you want raw RGB32 access, skip the first 11 bytes (header) and then read pixels as Alpha (always 0), Red, Green, Blue, left to right then top to bottom, range is 0-255 for all colors.
Warning: Storing screen shots in memory is not recommended. Memory usage will blow up pretty quick. One screen shot string eats around 230 KB of RAM.
gui.gdoverlay(int x = 0, int y = 0, string dgimage)
Overlay the given image on the emulator. Transparency is absolute (any pixel not 100% transparent is completely opaque). The image must be gd file format version 1, true color. Image will be clipped to fit.
gui.transparency(int strength)
Set the transparency level for subsequent painting (including gdoverlay). Does not stack.
Values range from 0 to 4. Where 0 means completely opaque and 4 means completely transparent.
function gui.register(function func)
Register a function to be called between a frame being prepared for displaying on your screen and it actually happening. Used when that 1 frame delay for rendering is not acceptable.
string gui.popup(string message, string type = "ok")
Shows a popup. Default type is "ok". Can be one of these:
- "ok" - "yesno" - "yesnocancel"
Returns "yes", "no" or "cancel" indicating the button clicked.
Linux users might want to install xmessage to perform the work. Otherwise the dialog will appear on the shell and that's less noticeable.
Bitwise Operations
int AND(int n1, int n2, ..., int nn)
Binary logical AND of all the given integers. This function compensates for Lua's lack of it.
int OR(int n1, int n2, ..., int nn)
Binary logical OR of all the given integers. This function compensates for Lua's lack of it.
int XOR(int n1, int n2, ..., int nn)
Binary logical XOR of all the given integers. This function compensates for Lua's lack of it.
int BIT(int n1, int n2, ..., int nn)
Returns an integer with the given bits turned on. Parameters should be smaller than 31.
Appendix
On drawing
A general warning about drawing is that it is always one frame behind unless you use gui.register. This is because you tell the emulator to paint something but it will actually paint it when generating the image for the next frame. So you see your painting, except it will be on the image of the next frame. You can prevent this with gui.register because it gives you a quick chance to paint before blitting.
Dimensions & color depths you can paint in:
320x239, 8bit color (confirm?)
On colors
Colors can be of a few types.
Int: use the a formula to compose the color as a number (depends on color depth)
String: Can either be a HTML color or simple colors.
HTML string: "#rrggbb" ("#228844") or #rrggbbaa if alpha is supported.
Simple colors: "clear", "red", "green", "blue", "white", "black", "gray", "grey", "orange", "yellow", "green", "teal", "cyan", "purple", "magenta".
For transparancy use "clear", this is actually int 1.