From b9955ea46306c590433bdd1cd47d129aff31f7fa Mon Sep 17 00:00:00 2001 From: ugetab Date: Wed, 21 Apr 2010 22:04:31 +0000 Subject: [PATCH] Minor addition to LUA documentation, and a Super Mario Bros. Competition Recorder, which tracks one's best attempts at recording a 'from reset' movie that achieves a high score, then saves the results in a text file, and sorts them by high score. --- output/luaScripts/SMB-CompetitionRecorder.lua | 121 ++++++++++++++++++ .../UsingLuaScripting-Documentation.txt | 11 ++ 2 files changed, 132 insertions(+) create mode 100644 output/luaScripts/SMB-CompetitionRecorder.lua diff --git a/output/luaScripts/SMB-CompetitionRecorder.lua b/output/luaScripts/SMB-CompetitionRecorder.lua new file mode 100644 index 00000000..bf70f19f --- /dev/null +++ b/output/luaScripts/SMB-CompetitionRecorder.lua @@ -0,0 +1,121 @@ +-- Super Mario Bros. script by ugetab. +-- 2010, April 20th. +-- Competition Recorder: +-- Start the script, then make a recording from Start. +-- Play until you get a score you'd like to keep, then end the movie. +-- A record of your best scores, and the filename you got it in, will be saved. +-- You can easily find your best score, because it will be sorted highest to lowest. +-- The last entry is always erased, but is also always the lowest score. + +-- The best score for your current movie file will be displayed above the coin counter. + +-- The reason this is good for competition is that you can't get away with cheating, +-- unless the game allows it. A movie file is a collection of button presses which +-- can be played back. If it doesn't play back the same for someone else, then there's +-- probably a problem with the game file, or with what the person recording did. + +function text(x,y,str) + if (x > 0 and x < 255 and y > 0 and y < 240) then + gui.text(x,y,str); + end; +end; + +function bubbleSort(table) +--http://www.dreamincode.net/code/snippet3406.htm +--check to make sure the table has some items in it + if #table < 2 then + --print("Table does not have enough data in it") + return; + end; + + for i = 0, (#table / 2) -1 do --> array start to end. Arrays start at 1, not 0. + for j = 1, (#table / 2) -1 do + if tonumber(table[(j*2)+1]) > tonumber(table[(j*2)-1]) then -->switch + temp1 = table[(j*2) - 1]; + temp2 = table[(j*2)]; + table[(j*2) - 1] = table[(j*2) + 1]; + table[(j*2)] = table[(j*2) + 2]; + table[(j*2) + 1] = temp1; + table[(j*2) + 2] = temp2; + end; + end; + end; + return; +end; + +filesizefile = io.open("MaxScore_LUA.txt","r"); + +if filesizefile == nil then + filewritefile = io.open("MaxScore_LUA.txt","w"); + filewritefile:close(); + filesizefile = io.open("MaxScore_LUA.txt","r"); +end; + +filesize = filesizefile:seek("end"); +filesizefile:close(); +scores = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}; + +if filesize == nil then + filesize = 0; +end; + +if filesize < 20 then + fileinit = io.open("MaxScore_LUA.txt","w+") + for i = 1, #scores do + fileinit:write(scores[i]..'\n'); + end; + fileinit:close(); +end; + +activescoring = false; +moviename = ""; + +maxscoresave = -9999999; + +text(83,8,"-Inactive-"); + +while (true) do + if (movie.mode() == "record") then + if movie.ispoweron() then + maxscoretest = memory.readbyte(0x07d7)..memory.readbyte(0x07d8)..memory.readbyte(0x07d9)..memory.readbyte(0x07da)..memory.readbyte(0x07db)..memory.readbyte(0x07dc).."0"; + activescoring = true; + + --if (tonumber(maxscoretest) >= tonumber(maxscoresave)) then + if (tonumber(maxscoretest) <= 9999990) then + maxscoresave = maxscoretest; + moviename = movie.getname(); + end; + --end; + + text(83,8,maxscoresave); + end; + end; + if (movie.mode() == nil) then + if (activescoring == true) then + activescoring = false; + text(83,8,"-Inactive-"); + readfile = io.open("MaxScore_LUA.txt","r"); + linecount = 1 + for line in readfile:lines() do + if linecount <= 20 then + scores[linecount] = line; + end; + linecount = linecount + 1; + end; + readfile:close (); + + --if tonumber(maxscoresave) > tonumber(scores[19]) then + scores[19] = maxscoresave; + scores[20] = moviename; + bubbleSort(scores); + savefile = io.open("MaxScore_LUA.txt","w+") + for i = 1, #scores do + savefile:write(tostring(scores[i])); + savefile:write('\n'); + end; + savefile:close(); + --end; + end; + end; + FCEU.frameadvance(); +end; diff --git a/output/luaScripts/UsingLuaScripting-Documentation.txt b/output/luaScripts/UsingLuaScripting-Documentation.txt index a912215e..4ea1e75f 100644 --- a/output/luaScripts/UsingLuaScripting-Documentation.txt +++ b/output/luaScripts/UsingLuaScripting-Documentation.txt @@ -1,6 +1,17 @@ Learning to use Lua Sctripting for FCEUX Written by QFox +This is a document designed primarily to help someone use FCEUX specific commands with LUA, and tends to assume that you have some programming experience. It is best used for basic coding reference, and it is not comprehensive, in that there are a large number of things that LUA can do which aren't mentioned here. For a broad overview of the LUA language, check here: + +The Manual: (Good for learning LUA's basic capabilities, but you don't need to learn it all before using LUA) +http://www.lua.org/manual/ + +Programming in LUA: (Good for finding exact coding syntax, such as for LUA arrays, or to get coding examples) +http://www.lua.org/pil/ + +Other .lua files: +Don't be afraid to copy, look through, and break existing .lua scripts in order to make your own. Taking a piece of other people's code and learning how it works, modifying it, or outright duplicating it for your own project generally isn't frowned upon, as long as you know that what you release may eventually be used by others for their projects. + Windows users - see also the Lua Scripting chapter for the FCEUX sHelp manual (fceux.chm) Ok. Lua. Let's see.