Advertisement
Guest User

Dammit

a guest
Nov 19th, 2009
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.82 KB | None | 0 0
  1. --[[
  2. Scrolling input display for Capcom 6-button fighters
  3. written by Dammit (dammit9x at hotmail dot com)
  4. version 11/18/2009
  5.  
  6. Works with FBA, snes9x and Gens:
  7. http://code.google.com/p/fbarr/downloads/list
  8. http://code.google.com/p/snes9x-rr/downloads/list
  9. http://code.google.com/p/gens-rerecording/downloads/list
  10.  
  11. The files gd.dll and lua51.dll must be with the emulator exe.
  12. They can be found in these packages:
  13. http://luaforge.net/frs/download.php/1594/lua-gd-2.0.33r2-win32.zip
  14. http://luaforge.net/frs/download.php/3677/lua5_1_4_Win32_bin.zip
  15.  
  16. You may tweak the parameters above the dashed line.
  17. Positioning is optimized for CPS1-2-3. The x[2] value must be reduced for the other emus.
  18.  
  19. You may use your own images, but their filenames must conform to "(imageset)-(gamekey).png"
  20. For gens, the images must be in the same place as gens.exe.
  21. For fba and snes9x, the images must be with the lua script.
  22.  
  23. Possible improvements:
  24. save and load data in savestates: not all emus have savestate.* lua functions
  25. add pcsx: needs better lua support
  26. add pcejin: needs better lua support
  27. add vba: more research needed
  28. ]]
  29.  
  30. local imageset="sf4"              --the prefix that must be on image files
  31. local timeout=240                 --how many idle frames until old records are cleared
  32. local buffersize=10               --how many records to show
  33. local x={[1]=0x00,[2]=0x120}      --horizontal starting position
  34. local y=0x20                      --vertical starting position
  35. local dx,dy=0x10,0x10             --how far apart to space the symbols
  36. local opacity=0.8                 --factor by which to multiply the opacity of the images
  37. local draw={[1]=true,[2]=true}    --whether to draw each player's commands
  38. local switch={[1]="Q",[2]="W"}    --key used to toggle each player on/off
  39.  
  40. ----------------------------------------------------------------------------------------------------
  41. require "gd"
  42. local thisframe,lastframe,oldswitch,e,keyset,ghetto,changed={},{},{}
  43. local inp={[1]={},[2]={}}
  44. local idle={[1]=0,[2]=0}
  45. local emulator={snes9x,gens,fba}
  46. local gamekeys={
  47.     "ul","ur","dl","dr",
  48.     { "l",  "left", "left",        "Left"},
  49.     { "r", "right","right",       "Right"},
  50.     { "u",    "up",   "up",          "Up"},
  51.     { "d",  "down", "down",        "Down"},
  52.     {"LP",     "Y",    "X",  "Weak Punch"},
  53.     {"MP",     "X",    "Y","Medium Punch"},
  54.     {"HP",     "L",    "Z","Strong Punch"},
  55.     {"LK",     "B",    "A",   "Weak Kick"},
  56.     {"MK",     "A",    "B", "Medium Kick"},
  57.     {"HK",     "R",    "C", "Strong Kick"},
  58.     { "S","select", "none",       "Start"}  --taunt button
  59. }
  60.  
  61. for index,name in ipairs(gamekeys) do --Check if all the images are present.
  62.     local file=imageset..(name[1] or name)..".png"
  63.     if not io.open(file,"rb") then
  64.         print(file,"not found.")
  65.         ghetto=true
  66.     end
  67. end
  68. if ghetto then print("Falling back on ghetto mode.") end
  69.  
  70. for k,emu in pairs(emulator) do --Detect what emulator this is.
  71.     if emu then
  72.         keyset=k+1
  73.         e=emu
  74.         break
  75.     end
  76.     error("This script doesn't work on this emulator yet.")
  77. end
  78.  
  79. local function toggleplayer(p)
  80.     local nowswitch=input.get()[switch[p]] --Press the "switch" keys to toggle display for each char.
  81.     if nowswitch and not oldswitch[p] then
  82.         if not draw[p] then draw[p]=true
  83.         else draw[p]=false
  84.         end
  85.     end
  86.     oldswitch[p]=nowswitch
  87. end
  88.  
  89. local function filterinput(p,f)
  90.     for pressed,state in pairs(joypad.getdown(p)) do --Check current controller state >
  91.         for index,name in pairs(gamekeys) do           --but ignore non-gameplay buttons.
  92.             if pressed==name[keyset] or pressed=="P"..p.." "..tostring(name[keyset]) then
  93.                 f[name[1]]=state --FBA does not distinguish joypads, so inputs must be filtered by "P1" and "P2".
  94.                 break
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100. local function compositeinput(f)
  101.     for _,a in pairs({"u","d"}) do --Convert individual directions to diagonals.
  102.         for _,b in pairs({"l","r"}) do
  103.             if f[a] and f[b] then
  104.                 f[a],f[b]=nil,nil
  105.                 f[a..b]=true
  106.             end
  107.         end
  108.     end
  109. end
  110.  
  111. local function detectchanges(f1,f2)
  112.     changed=false
  113.     for key,state in pairs(f2) do --If a key is pressed >
  114.         if not f1[key] then         --that wasn't pressed last frame >
  115.             changed=true              --then changes were made.
  116.         end
  117.     end
  118. end
  119.  
  120. local function updaterecords(p,f2,i)
  121.     if changed then                     --If changes were made >
  122.         if idle[p]<timeout then           --and the player hasn't been idle too long>
  123.             for record=buffersize,2,-1 do
  124.                 i[record]=i[record-1]         --then shift every old record by 1 >
  125.             end
  126.         else
  127.             for record=buffersize,2,-1 do
  128.                 i[record]=nil                 --otherwise wipe out the old records.
  129.             end
  130.         end
  131.         idle[p]=0                         --Reset the idle count >
  132.         i[1]={}                           --and set current input as record 1 >
  133.         local index=1
  134.         for _,name in ipairs(gamekeys) do --but the order must not deviate from gamekeys.
  135.             for key,state in pairs(f2) do
  136.                 if key==name[1] or key==name then
  137.                     i[1][index]=key
  138.                     index=index+1
  139.                     break
  140.                 end
  141.             end
  142.         end
  143.     else idle[p]=idle[p]+1              --Increment the idle count if nothing changed.
  144.     end
  145. end
  146.  
  147. local function displayrecords(p,i)
  148.     if draw[p] then
  149.         for line,stuff in pairs(i) do
  150.             for index,key in pairs(i[line]) do
  151.                 if not ghetto then
  152.                     gui.gdoverlay(x[p]+index*dx,y+line*dy,gd.createFromPng(imageset..key..".png"):gdStr(),opacity)
  153.                 else
  154.                     gui.text(x[p]+index*dx,y+line*dy,key) --Ghetto mode displays text symbols only.
  155.                 end
  156.             end
  157.         end
  158.     end
  159. end
  160.  
  161. e.registerbefore(function()
  162.     for player=1,2 do
  163.         thisframe={}
  164.         toggleplayer(player)
  165.         filterinput(player,thisframe)
  166.         compositeinput(thisframe)
  167.         detectchanges(lastframe[player],thisframe)
  168.         updaterecords(player,thisframe,inp[player])
  169.         lastframe[player]=thisframe
  170.     end
  171. end)
  172.  
  173. gui.register(function()
  174.     gui.text(0,0,"")
  175.     for player=1,2 do
  176.         displayrecords(player,inp[player])
  177.     end
  178. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement