Advertisement
feasel

SGL-script.lua

May 16th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.90 KB | None | 0 0
  1. -- This same script works for both NES and SNES games.
  2. -- Make sure the following line contains your user name, case sensitive:
  3. PLAYER_ID = "yourusername"
  4.  
  5. -- Base directory of SGL stuff. Start with drive letter, use double backslash as seperator, end with a trailing double backslash. Path MAY NOT contain spaces!
  6. SGLBASE = "c:\\SGL\\"
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. -- **********************************************************************
  21. -- DON'T CHANGE ANYTHING BELOW THIS LINE
  22. -- **********************************************************************
  23.  
  24. MOVIEDIR = SGLBASE .. "movies\\"
  25.  
  26. TOP_SPLITS = 1
  27. BOTTOM_SPLITS = 213
  28.  
  29. cur = 0
  30. numsplits = 2
  31. splitsThis = {0, 0, 0, 0}
  32. splitsPB = {-1, -1, -1, -1}
  33. splitsY = TOP_SPLITS
  34.  
  35. lastSplit = false
  36. lastRestart = false
  37. lastBack = false
  38. lastSave = false
  39. lastUpdate = false
  40. lastNumsplit = false
  41.  
  42. finishedState = false
  43. waiting = false
  44. waitingbegin = 0
  45. new_pb = false
  46.  
  47. RED = "#FF0000"
  48. GREEN = "#00FF00"
  49. GRAY = "#CCCCCC"
  50. WHITE = "#FFFFFF"
  51. CREAM = "#FFFFDD"
  52. BGCOLOR = "#000000"
  53.  
  54. initialsave = savestate.create()
  55. endsave = savestate.create()
  56.  
  57.  
  58. isNES = nil
  59. function worksOnFceux() sound.get(); end
  60. function worksOnSnes9x() snes9x.emulating(); end
  61. if pcall(worksOnFceux) then
  62. isNES = true
  63. elseif pcall(worksOnSnes9x) then
  64. isNES = false
  65. else
  66. print("Unable to identify emulator")
  67. return
  68. end
  69.  
  70.  
  71. if isNES then
  72. MOVIE_EXTENSION = "fm2"
  73. else
  74. MOVIE_EXTENSION = "smv"
  75. end
  76.  
  77.  
  78. -- ---------------------------------------------------------------------------
  79.  
  80. function finalizesmv(fromfilepath, tofilepath)
  81.  
  82. function findcontrolleroffset(fromfilepath)
  83.  
  84. local inpf = io.open(fromfilepath, "rb")
  85.  
  86. local d1 = nil
  87. local d2 = nil
  88. local d3 = nil
  89. local i = 0
  90. local x
  91.  
  92. while 1 do
  93. x = inpf:read(1)
  94. if (x == nil) then
  95. inpf:close()
  96. return nil, nil
  97. end
  98. i = i + 1
  99. d1 = d2
  100. d2 = d3
  101. d3 = x
  102. if (d1=='\199' and d2=='\017' and d3=='\000') then
  103. while 1 do
  104. x = inpf:read(1)
  105. if (x == nil) then
  106. inpf:close()
  107. return nil, nil
  108. end
  109. i = i + 1
  110. d1 = d2
  111. d2 = d3
  112. d3 = x
  113. if (x ~= '\204') then
  114. local offset = i - 1
  115. if (offset % 16 == 0) then
  116. local bytes = 1
  117. while 1 do
  118. x = inpf:read(1)
  119. if (x == nil) then
  120. break
  121. end
  122. bytes = bytes + 1
  123. end
  124. local frames = math.floor(bytes / 2)
  125. frames = frames-1 -- SMV file has 1 extra frame before the first one
  126. inpf:close()
  127. return offset, frames
  128. else
  129. break
  130. end
  131. end
  132. end
  133. end
  134. end
  135. end
  136.  
  137. function copybytes(inpf, outf, x)
  138. if x < 0 then
  139. print("ERROR - bytes to be copied: ", x)
  140. x = 0
  141. end
  142. for i=1,x do
  143. local s = inpf:read(1)
  144. if s == nil then
  145. print("ERROR - end of file encountered during byte copy")
  146. return
  147. end
  148. outf:write(s)
  149. end
  150. end
  151.  
  152. function copyrest(inpf, outf)
  153. while 1 do
  154. local s = inpf:read(1)
  155. if s == nil then
  156. return
  157. end
  158. outf:write(s)
  159. end
  160. end
  161.  
  162. function write4bytesLE(outf, num)
  163. local x = num
  164. outf:write(string.char(x % 256))
  165. x = math.floor(x / 256)
  166. outf:write(string.char(x % 256))
  167. x = math.floor(x / 256)
  168. outf:write(string.char(x % 256))
  169. x = math.floor(x / 256)
  170. outf:write(string.char(x % 256))
  171. end
  172.  
  173. -- --------------------------------------------------------------
  174.  
  175. local offsetComputed, frames = findcontrolleroffset(fromfilepath)
  176.  
  177. if offsetComputed == nil then
  178. return nil
  179. end
  180. if offsetComputed < 64 then
  181. return nil
  182. end
  183.  
  184. local inpf = io.open(fromfilepath, "rb")
  185. local outf = io.open(tofilepath, "wb")
  186.  
  187. copybytes(inpf, outf, 16)
  188.  
  189. inpf:read(4) -- skip these zeros
  190. write4bytesLE(outf, frames)
  191.  
  192. copybytes(inpf, outf, 8)
  193.  
  194.  
  195. local xx = inpf:read(4) -- skip these zeros
  196.  
  197. -- Decide whether to use the controller-data-block offset that was read from the raw SMV file or the offset that was computed by searching for the end-of-savestate signature.
  198. -- If the one read from the file is 0 then we can't use it (and print an error because I didn't think this was possible now that we jump to the beginning and end of the file to flush it before copying).
  199. -- If there is a mismatch, this likely due to the computed offset being wrong because of false-positives on the end-of-savestate signature. Basically always use the version that was read from the file unless it's 0.
  200. -- Note that if the false-positive occurs and computed offset is wrong (i.e. too early) then the value of "frames" is going to be too large since the algorithm thinks the controller data region is starting sooner than it's supposed to.
  201. local offsetFromFile = 256*(256*(256*string.byte(xx,4) + string.byte(xx,3)) + string.byte(xx,2)) + string.byte(xx,1)
  202. local offset
  203. if offsetFromFile == 0 then
  204. print("ERROR - Zero offset in file:read. Using computed = %d (%08X)", offsetComputed, offsetComputed)
  205. offset = offsetComputed
  206. elseif offsetFromFile ~= offsetComputed then
  207. print("ERROR - Offset mistmatch - file:read = %d (%08X), computed = %d (%08X)", offsetFromFile, offsetFromFile, offsetComputed, offsetComputed)
  208. offset = offsetFromFile
  209. else
  210. offset = offsetFromFile
  211. end
  212.  
  213. write4bytesLE(outf, offset)
  214.  
  215. inpf:read(4) -- skip these zeros
  216. write4bytesLE(outf, frames)
  217.  
  218. copybytes(inpf, outf, offset - 36)
  219.  
  220. -- If we were copying the original in-use SMV before the person has re-saved over it we'd have to instert 2 bytes of 0s in order to add the extra pre-movie frame that is included in finalized SMV files (but not the first version of the raw SMV).
  221.  
  222. copyrest(inpf, outf)
  223.  
  224. -- NOTE: Copying from the raw SMV file will copy all controller data that is in the file, including extra stuff after what should be the endpoint, which is leftover from earlier versions of the movie (like if a longer movie were overwritten with a short one). This means that the copied finalized SMV file may appear longer than the run itself and may contain garbage inputs at the end.
  225.  
  226. inpf:close()
  227. outf:close()
  228. end
  229.  
  230.  
  231. -- ----------------------------------------------------------------------
  232.  
  233.  
  234. function drawtext(x, y, s, color)
  235. -- x and y are the top-left coordinates of the first character's space
  236. -- text itself is 7x10 with 1px between characters.
  237. if isNES then -- The 8-pixel overscan is counted in the coordinate system
  238. y = y + 8
  239. end
  240.  
  241. for i = 1,string.len(s) do
  242. local c = string.byte(s,i)
  243. if c == 48 then -- 0
  244. gui.line(x+0,y+2, x+0,y+7, color)
  245. gui.line(x+6,y+2, x+6,y+7, color)
  246. gui.line(x+1,y+1, x+1,y+8, color)
  247. gui.line(x+5,y+1, x+5,y+8, color)
  248. gui.line(x+2,y+0, x+4,y+0, color)
  249. gui.line(x+2,y+9, x+4,y+9, color)
  250. gui.line(x+2,y+1, x+4,y+1, color)
  251. gui.line(x+2,y+8, x+4,y+8, color)
  252. x = x + 8
  253. elseif c == 49 then -- 1
  254. gui.line(x+3,y+0, x+3,y+9, color)
  255. gui.line(x+4,y+0, x+4,y+9, color)
  256. gui.line(x+2,y+1, x+2,y+2, color)
  257. gui.pixel(x+1,y+2, color)
  258. x = x + 8
  259. elseif c == 50 then -- 2
  260. gui.line(x+2,y+0, x+4,y+0, color)
  261. gui.line(x+1,y+1, x+5,y+1, color)
  262. gui.line(x+0,y+2, x+1,y+2, color)
  263. gui.line(x+5,y+2, x+6,y+2, color)
  264. gui.line(x+5,y+3, x+6,y+3, color)
  265. gui.line(x+4,y+4, x+5,y+4, color)
  266. gui.line(x+3,y+5, x+4,y+5, color)
  267. gui.line(x+2,y+6, x+3,y+6, color)
  268. gui.line(x+1,y+7, x+2,y+7, color)
  269. gui.line(x+0,y+8, x+6,y+8, color)
  270. gui.line(x+0,y+9, x+6,y+9, color)
  271. x = x + 8
  272. elseif c == 51 then -- 3
  273. gui.line(x+2,y+0, x+4,y+0, color)
  274. gui.line(x+1,y+1, x+5,y+1, color)
  275. gui.line(x+0,y+2, x+1,y+2, color)
  276. gui.line(x+5,y+2, x+6,y+2, color)
  277. gui.line(x+5,y+3, x+6,y+3, color)
  278. gui.line(x+3,y+4, x+5,y+4, color)
  279. gui.line(x+3,y+5, x+5,y+5, color)
  280. gui.line(x+5,y+6, x+6,y+6, color)
  281. gui.line(x+5,y+7, x+6,y+7, color)
  282. gui.line(x+0,y+7, x+1,y+7, color)
  283. gui.line(x+1,y+8, x+5,y+8, color)
  284. gui.line(x+2,y+9, x+4,y+9, color)
  285. x = x + 8
  286. elseif c == 52 then -- 4
  287. gui.line(x+5,y+0, x+5,y+9, color)
  288. gui.line(x+6,y+0, x+6,y+9, color)
  289. gui.line(x+0,y+6, x+4,y+6, color)
  290. gui.line(x+0,y+5, x+1,y+5, color)
  291. gui.line(x+1,y+4, x+2,y+4, color)
  292. gui.line(x+2,y+3, x+3,y+3, color)
  293. gui.line(x+3,y+2, x+4,y+2, color)
  294. gui.pixel(x+4,y+1, color)
  295. x = x + 8
  296. elseif c == 53 then -- 5
  297. gui.line(x+0,y+0, x+6,y+0, color)
  298. gui.line(x+0,y+1, x+6,y+1, color)
  299. gui.line(x+0,y+2, x+1,y+2, color)
  300. gui.line(x+0,y+3, x+4,y+3, color)
  301. gui.line(x+0,y+4, x+5,y+4, color)
  302. gui.line(x+5,y+5, x+5,y+7, color)
  303. gui.line(x+6,y+5, x+6,y+7, color)
  304. gui.line(x+0,y+7, x+1,y+7, color)
  305. gui.line(x+1,y+8, x+5,y+8, color)
  306. gui.line(x+2,y+9, x+4,y+9, color)
  307. x = x + 8
  308. elseif c == 54 then -- 6
  309. gui.line(x+0,y+3, x+3,y+0, color)
  310. gui.line(x+0,y+4, x+4,y+0, color)
  311. gui.line(x+1,y+4, x+5,y+4, color)
  312. gui.line(x+0,y+5, x+0,y+7, color)
  313. gui.line(x+1,y+5, x+1,y+7, color)
  314. gui.line(x+1,y+8, x+5,y+8, color)
  315. gui.line(x+2,y+9, x+4,y+9, color)
  316. gui.line(x+5,y+5, x+5,y+7, color)
  317. gui.line(x+6,y+5, x+6,y+7, color)
  318. x = x + 8
  319. elseif c == 55 then -- 7
  320. gui.line(x+0,y+0, x+6,y+0, color)
  321. gui.line(x+0,y+1, x+6,y+1, color)
  322. gui.line(x+5,y+2, x+6,y+2, color)
  323. gui.line(x+2,y+7, x+2,y+9, color)
  324. gui.line(x+3,y+5, x+3,y+9, color)
  325. gui.line(x+4,y+3, x+4,y+6, color)
  326. gui.line(x+5,y+3, x+5,y+4, color)
  327. x = x + 8
  328. elseif c == 56 then -- 8
  329. gui.line(x+2,y+0, x+4,y+0, color)
  330. gui.line(x+1,y+1, x+5,y+1, color)
  331. gui.line(x+2,y+4, x+4,y+4, color)
  332. gui.line(x+2,y+5, x+4,y+5, color)
  333. gui.line(x+1,y+8, x+5,y+8, color)
  334. gui.line(x+2,y+9, x+4,y+9, color)
  335. gui.line(x+0,y+2, x+0,y+3, color)
  336. gui.line(x+0,y+6, x+0,y+7, color)
  337. gui.line(x+6,y+2, x+6,y+3, color)
  338. gui.line(x+6,y+6, x+6,y+7, color)
  339. gui.line(x+1,y+2, x+1,y+7, color)
  340. gui.line(x+5,y+2, x+5,y+7, color)
  341. x = x + 8
  342. elseif c == 57 then -- 9
  343. gui.line(x+2,y+0, x+4,y+0, color)
  344. gui.line(x+1,y+1, x+5,y+1, color)
  345. gui.line(x+0,y+2, x+1,y+2, color)
  346. gui.line(x+0,y+3, x+1,y+3, color)
  347. gui.line(x+0,y+4, x+4,y+4, color)
  348. gui.line(x+1,y+5, x+4,y+5, color)
  349. gui.line(x+5,y+2, x+5,y+7, color)
  350. gui.line(x+6,y+2, x+6,y+7, color)
  351. gui.line(x+4,y+8, x+5,y+8, color)
  352. gui.line(x+2,y+9, x+4,y+9, color)
  353. x = x + 8
  354. elseif c == 32 then -- space
  355. x = x + 8
  356. elseif c == 43 then -- +
  357. gui.line(x+2,y+4, x+6,y+4, color)
  358. gui.line(x+4,y+2, x+4,y+6, color)
  359. x = x + 8
  360. elseif c == 45 then -- -
  361. gui.line(x+2,y+4, x+6,y+4, color)
  362. x = x + 8
  363. elseif c == 58 then -- :
  364. gui.line(x+0,y+2, x+1,y+2, color)
  365. gui.line(x+0,y+3, x+1,y+3, color)
  366. gui.line(x+0,y+6, x+1,y+6, color)
  367. gui.line(x+0,y+7, x+1,y+7, color)
  368. x = x + 3
  369. elseif c == 46 then -- .
  370. gui.line(x+0,y+8, x+1,y+8, color)
  371. gui.line(x+0,y+9, x+1,y+9, color)
  372. x = x + 3
  373. end
  374. end
  375. end
  376.  
  377.  
  378. function copyMovie(recfilename, srcefilepath)
  379. local tempfilepath = MOVIEDIR .. "__" .. recfilename
  380. local destfilepath = MOVIEDIR .. recfilename
  381.  
  382. -- make the dir
  383. os.execute("mkdir \"" .. MOVIEDIR .. "\"")
  384.  
  385. if isNES then
  386. -- copy the movie
  387. local s = string.format("copy \"%s\" \"%s\"", srcefilepath, destfilepath)
  388. print(s)
  389. os.execute(s)
  390. else
  391. -- copy the movie
  392. local s = string.format("copy \"%s\" \"%s\"", srcefilepath, tempfilepath)
  393. print(s)
  394. os.execute(s)
  395.  
  396. -- finalize the movie
  397. finalizesmv(tempfilepath, destfilepath)
  398. end
  399. end
  400.  
  401. function restartMovie()
  402. if movie.active() then
  403. if initialsave ~= nil then
  404. savestate.load(initialsave)
  405. else
  406. print("initialsave is nil")
  407. end
  408. else
  409. print("No active movie for initialstate")
  410. end
  411. end
  412.  
  413. function returnMovieToEnd()
  414. if movie.active() then
  415. if endsave ~= nil then
  416. savestate.load(endsave)
  417. else
  418. print("endsave is nil")
  419. end
  420. else
  421. print("No active movie for endstate")
  422. end
  423. end
  424.  
  425. function loadSplits()
  426. local filepath = SGLBASE .. "splits.txt"
  427. local f,err = io.open(filepath, "r")
  428. if f == nil then
  429. splitsPB = {-1, -1, -1, -1}
  430. -- numsplits remains unchanged
  431. return
  432. end
  433. numsplits = f:read("*number")
  434. if numsplits < 1 or numsplits > 8 then
  435. print("Error in splits.txt")
  436. keepgoing = false
  437. return
  438. end
  439. if numsplits > 4 then
  440. numsplits = numsplits - 4
  441. splitsY = BOTTOM_SPLITS
  442. else
  443. splitsY = TOP_SPLITS
  444. end
  445. for i = 1,4 do
  446. splitsPB[i] = f:read("*number")
  447. end
  448. f:close()
  449. end
  450.  
  451. function writeSplits()
  452. os.execute("mkdir \"" .. SGLBASE .. "\"")
  453. local filepath = SGLBASE .. "splits.txt"
  454. local f = assert(io.open(filepath, "w"))
  455. if splitsY == TOP_SPLITS then
  456. f:write(string.format("%d ", numsplits))
  457. else
  458. f:write(string.format("%d ", numsplits + 4))
  459. end
  460. for i = 1,4 do
  461. f:write(string.format("%d ", splitsThis[i]))
  462. end
  463. f:write("\n")
  464. f:close()
  465. end
  466.  
  467. function uploadRun(recfilename)
  468. local filepath = MOVIEDIR .. recfilename
  469. local s = string.format("%ssgl-upload.exe \"%s\" \"%s\"", SGLBASE, PLAYER_ID, filepath)
  470. print(s)
  471. local result = os.execute(s)
  472. if result == 0 then
  473. print(" UPLOAD COMPLETE")
  474. else
  475. print(" FAILURE (" .. result .. ")")
  476. end
  477. end
  478.  
  479. function convertDiffShort(frames)
  480. local sign
  481. if frames < 0 then
  482. frames = frames * -1
  483. sign = "-"
  484. else
  485. sign = "+"
  486. end
  487. local seconds = frames / 60.099822938442230224609375
  488. local ss = math.floor(seconds)
  489. local xx = math.floor((seconds - ss) * 10)
  490.  
  491. return string.format("(%s%d.%01d)", sign, ss, xx)
  492. end
  493.  
  494.  
  495. function convertDiff(frames)
  496. local sign
  497. if frames < 0 then
  498. frames = frames * -1
  499. sign = "-"
  500. else
  501. sign = "+"
  502. end
  503. local seconds = frames / 60.099822938442230224609375
  504. local mm = math.floor(seconds / 60)
  505. seconds = seconds - mm * 60
  506. local ss = math.floor(seconds)
  507. local xx = math.floor((seconds - ss) * 10)
  508.  
  509. return string.format("%s%1d:%02d.%01d", sign, mm, ss, xx)
  510. end
  511.  
  512. function convertTime(frames)
  513. local seconds = frames / 60.099822938442230224609375
  514. local mm = math.floor(seconds / 60)
  515. seconds = seconds - mm * 60
  516. local ss = math.floor(seconds)
  517. local xx = math.floor((seconds - ss) * 10)
  518.  
  519. mm = mm % 100 -- will never get this high unless emu is running with no movie playing
  520.  
  521. return string.format("%2d:%02d.%01d", mm, ss, xx)
  522. end
  523.  
  524.  
  525.  
  526. --- -----------------------------------------------------------------------
  527. --- -----------------------------------------------------------------------
  528. --- -----------------------------------------------------------------------
  529.  
  530.  
  531. print("Controls: ")
  532. print(" 'space' -- split")
  533. print(" 'backspace' -- undo a split")
  534. print(" 'R' -- restart")
  535. print(" 'Y' -- submit run")
  536. print(" 'N' -- change the number of splits")
  537. print(" 'C' -- clear the splits")
  538.  
  539.  
  540.  
  541. if movie.active() then
  542. savestate.save(initialsave)
  543. else
  544. while (true) do
  545. if not isNES then -- Snes9x needs text background
  546. gui.box(2, 39, 230, 170, BGCOLOR, BGCOLOR)
  547. end
  548. gui.text(3, 40, " NO MOVIE FILE IS RECORDING NOW ")
  549. gui.text(3, 50, " Stop this LUA script. ")
  550. gui.text(3, 60, " Pause your emulator if it's running ('Pause' key). ")
  551. gui.text(3, 70, " Load the save state file you were provided. ")
  552. gui.text(3, 80, " Begin a new movie recording -- ")
  553. gui.text(3, 88, " Choose 'Record From Now' and hit 'OK'. ")
  554. gui.text(3, 98, " Run this LUA script again. ")
  555. gui.text(3,108, " Click on the emulator window so that it has focus. ")
  556. gui.text(3,118, " Unpause your emulator ('Pause' key). ")
  557. gui.text(3,128, " Play the game. ")
  558. gui.text(3,136, " 'space' to split ")
  559. gui.text(3,144, " 'backspace' to undo a split ")
  560. gui.text(3,152, " 'R' to restart ")
  561. gui.text(3,160, " 'N' to change the number of splits ")
  562. gui.text(3,168, " 'C' to clear the splits ")
  563.  
  564. emu.frameadvance()
  565. end
  566. end
  567.  
  568. loadSplits() --- defaults to {-1, -1, -1} if file is not found
  569.  
  570. keepgoing = true
  571. while (keepgoing) do
  572.  
  573. if isNES then -- The 8-pixel overscan is counted in the coordinate system
  574. gui.box(0, 8+splitsY-1, 255, 8+splitsY+10, "#000000DD", "#000000DD")
  575. else
  576. gui.box(0, splitsY-1, 255, splitsY+10, "#000000DD", "#000000DD")
  577. end
  578.  
  579. if not waiting then
  580. local keypress = input.get()
  581. local pressSplit = keypress.space and not lastSplit
  582. local pressBack = keypress.backspace and not lastBack
  583. local pressRestart = keypress.R and not lastRestart
  584. local pressSave = keypress.Y and not lastSave
  585. local pressNumsplit = keypress.N and not lastNumsplit
  586. local pressClear = keypress.C and not lastClear
  587. lastSplit = keypress.space
  588. lastBack = keypress.backspace
  589. lastRestart = keypress.R
  590. lastSave = keypress.Y
  591. lastNumsplit = keypress.N
  592. lastClear = keypress.C
  593.  
  594.  
  595. if pressSplit then
  596. if cur < numsplits then
  597. cur = cur + 1
  598. splitsThis[cur] = emu.framecount()
  599. end
  600. end
  601.  
  602. if pressBack then
  603. if finishedState then -- already saved and uploaded the run
  604. finishedState = false -- go back to the run-ended-but-not-uploaded state
  605. else -- haven't yet saved
  606. if cur > 0 then
  607. splitsThis[cur] = 0
  608. cur = cur - 1
  609. end
  610. end
  611. end
  612.  
  613. if pressRestart then
  614. if new_pb then
  615. new_pb = false
  616. splitsPB[1] = splitsThis[1]
  617. splitsPB[2] = splitsThis[2]
  618. splitsPB[3] = splitsThis[3]
  619. splitsPB[4] = splitsThis[4]
  620. end
  621. restartMovie()
  622. cur = 0
  623. splitsThis = {0, 0, 0, 0}
  624. finishedState = false
  625. end
  626.  
  627. if pressNumsplit then
  628. numsplits = numsplits + 1
  629. if numsplits == 5 then
  630. numsplits = 1
  631. if splitsY == TOP_SPLITS then
  632. splitsY = BOTTOM_SPLITS
  633. else
  634. splitsY = TOP_SPLITS
  635. end
  636. end
  637. end
  638.  
  639. if pressClear then
  640. splitsPB = {-1, -1, -1, -1}
  641. end
  642.  
  643. if pressSave then
  644. if cur == numsplits and not finishedState then
  645. waiting = true
  646. waitingbegin = emu.framecount()
  647. new_pb = true
  648. end
  649. end
  650.  
  651. if cur == numsplits and not finishedState then
  652. gui.text(33, 22, "Press Y to save run or R to restart.", CREAM, BGCOLOR)
  653. end
  654.  
  655. else -- waiting to flush movie file before uploading
  656.  
  657. local elapsed = (emu.framecount() - waitingbegin) / 60.099822938442230224609375
  658. local waittime = 4
  659. if (elapsed > waittime) then
  660. waiting = false
  661. local tm = os.date("*t")
  662. local secsofday = tm.hour * 60 * 60 + tm.min * 60 + tm.sec
  663. local recfilename = string.format("rec_%s_%05d.%s", PLAYER_ID, secsofday, MOVIE_EXTENSION)
  664. if movie.active() then
  665. local currentfilepath = movie.name()
  666.  
  667. -- These lines only needed for SNES, but they don't hurt in FCEUX
  668. savestate.save(endsave)
  669. restartMovie()
  670. emu.frameadvance()
  671. returnMovieToEnd()
  672. emu.frameadvance()
  673.  
  674. copyMovie(recfilename, currentfilepath)
  675. writeSplits()
  676. uploadRun(recfilename)
  677. finishedState = true
  678. else
  679. print("No active movie")
  680. end
  681. else
  682. gui.text(90, 28, string.format("Please wait... %d", math.ceil(waittime-elapsed)), CREAM, BGCOLOR)
  683. end
  684.  
  685. end
  686.  
  687.  
  688. for i = 1, numsplits do
  689. x = 18 + 48 * (i + 4 - numsplits)
  690. if cur > i-1 then
  691. if splitsPB[i] == -1 then
  692. drawtext(x, splitsY, convertTime(splitsThis[i]), GRAY)
  693. else
  694. local diff, color
  695. diff = splitsThis[i] - splitsPB[i]
  696. if diff < 0 then
  697. color = GREEN
  698. else
  699. color = RED
  700. end
  701.  
  702. drawtext(x, splitsY, convertDiff(diff), color)
  703. end
  704. else -- haven't reached split "i" yet
  705. if splitsPB[i] == -1 then
  706. drawtext(x, splitsY, "--:--.-", GRAY)
  707. else
  708. drawtext(x, splitsY, convertTime(splitsPB[i]), GRAY)
  709. end
  710. end
  711. end
  712.  
  713.  
  714. if (cur > 0) and (splitsPB[cur] ~= -1) and (numsplits < 4) then
  715. if cur == 1 then
  716. diff = (splitsThis[cur]-0) - (splitsPB[cur]-0)
  717. else
  718. diff = (splitsThis[cur]-splitsThis[cur-1]) - (splitsPB[cur]-splitsPB[cur-1])
  719. end
  720.  
  721. if diff < 0 then
  722. color = GREEN
  723. else
  724. color = RED
  725. end
  726. drawtext(55, splitsY, convertDiffShort(diff), color)
  727. end
  728.  
  729.  
  730. if cur >= numsplits then
  731. drawtext(0, splitsY, convertTime(splitsThis[cur]), WHITE)
  732. else
  733. drawtext(0, splitsY, convertTime(emu.framecount()), CREAM)
  734. end
  735.  
  736. emu.frameadvance()
  737. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement