Guest User

Untitled

a guest
Sep 2nd, 2014
258
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3.  
  4. function widget:GetInfo()
  5. return {
  6. name = "CameraRecorder",
  7. desc = "v0.011 Record positions of the camera to a file and repath those positions when loading the replay.",
  8. author = "CarRepairer",
  9. date = "2011-07-04",
  10. license = "GNU GPL, v2 or later",
  11. layer = 1002,
  12. enabled = true,
  13. }
  14. end
  15.  
  16. --------------------------------------------------------------------------------
  17. --------------------------------------------------------------------------------
  18.  
  19. local spGetCameraState = Spring.GetCameraState
  20. local spGetCameraVectors = Spring.GetCameraVectors
  21. local spGetModKeyState = Spring.GetModKeyState
  22. local spGetMouseState = Spring.GetMouseState
  23. local spIsAboveMiniMap = Spring.IsAboveMiniMap
  24. local spSendCommands = Spring.SendCommands
  25. local spSetCameraState = Spring.SetCameraState
  26. local spSetMouseCursor = Spring.SetMouseCursor
  27. local spTraceScreenRay = Spring.TraceScreenRay
  28. local spWarpMouse = Spring.WarpMouse
  29. local spGetCameraDirection = Spring.GetCameraDirection
  30.  
  31. local abs = math.abs
  32. local min = math.min
  33. local max = math.max
  34. local sqrt = math.sqrt
  35. local sin = math.sin
  36. local cos = math.cos
  37.  
  38. local echo = Spring.Echo
  39.  
  40. local KF_FRAMES = 1
  41. local recording = false
  42. local recData = {}
  43. local filename
  44. local ranInit = false
  45.  
  46. -------------------------------
  47.  
  48. -- $Id: api_base64.lua 3171 2008-11-06 09:06:29Z det $
  49. -- Author: Alex Kloss
  50. -- Contact: http://www.it-rfc.de
  51. -- Date: 2006-2008
  52. -- License: LGPL2
  53.  
  54. -- Public functions:
  55. -- * Base64Encode(string data) -> string
  56. -- * Base64Decode(string data) -> string
  57.  
  58. -- bitshift functions (<<, >> equivalent)
  59. -- shift left
  60. local function lsh(value,shift)
  61. return (value*(2^shift)) % 256
  62. end
  63.  
  64. -- shift right
  65. local function rsh(value,shift)
  66. return math.floor(value/2^shift) % 256
  67. end
  68.  
  69. -- return single bit (for OR)
  70. local function bit(x,b)
  71. return (x % 2^b - x % 2^(b-1) > 0)
  72. end
  73.  
  74. -- logic OR for number values
  75. local function lor(x,y)
  76. result = 0
  77. for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
  78. return result
  79. end
  80.  
  81. -- encryption table
  82. local base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
  83.  
  84. -- function encode
  85. -- encodes input string to base64.
  86. function Base64Encode(data)
  87. local bytes = {}
  88. local result = ""
  89. for spos=0,string.len(data)-1,3 do
  90. for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  91. result = string.format('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  92. end
  93. return result
  94. end
  95.  
  96. -- decryption table
  97. local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
  98.  
  99. -- function decode
  100. -- decode base64 input to string
  101. function Base64Decode(data)
  102. local chars = {}
  103. local result=""
  104. for dpos=0,string.len(data)-1,4 do
  105. for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  106. result = string.format('%s%s%s%s',result,string.char(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and string.char(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
  107. end
  108. return result
  109. end
  110.  
  111. -------------------------------
  112.  
  113.  
  114. local CAMERA_STATE_FORMATS = {
  115. fps = {
  116. "px", "py", "pz",
  117. "dx", "dy", "dz",
  118. "rx", "ry", "rz",
  119. "oldHeight",
  120. },
  121. free = {
  122. "px", "py", "pz",
  123. "dx", "dy", "dz",
  124. "rx", "ry", "rz",
  125. "fov",
  126. "gndOffset",
  127. "gravity",
  128. "slide",
  129. "scrollSpeed",
  130. "velTime",
  131. "avelTime",
  132. "autoTilt",
  133. "goForward",
  134. "invertAlt",
  135. "gndLock",
  136. "vx", "vy", "vz",
  137. "avx", "avy", "avz",
  138. },
  139. OrbitController = {
  140. "px", "py", "pz",
  141. "tx", "ty", "tz",
  142. },
  143. ta = {
  144. "px", "py", "pz",
  145. "dx", "dy", "dz",
  146. "height",
  147. "zscale",
  148. "flipped",
  149. },
  150. ov = {
  151. "px", "py", "pz",
  152. },
  153. rot = {
  154. "px", "py", "pz",
  155. "dx", "dy", "dz",
  156. "rx", "ry", "rz",
  157. "oldHeight",
  158. },
  159. sm = {
  160. "px", "py", "pz",
  161. "dx", "dy", "dz",
  162. "height",
  163. "zscale",
  164. "flipped",
  165. },
  166. tw = {
  167. "px", "py", "pz",
  168. "rx", "ry", "rz",
  169. },
  170. }
  171.  
  172. local CAMERA_NAMES = {
  173. "fps",
  174. "free",
  175. "OrbitController",
  176. "ta",
  177. "ov",
  178. "rot",
  179. "sm",
  180. "tw",
  181. }
  182. local CAMERA_IDS = {}
  183.  
  184.  
  185. for i=1, #CAMERA_NAMES do
  186. CAMERA_IDS[CAMERA_NAMES[i]] = i
  187. end
  188.  
  189. --------------------------------------------------------------------------------
  190. --------------------------------------------------------------------------------
  191.  
  192. local function explode(div,str)
  193. if (div=='') then return false end
  194. local pos,arr = 0,{}
  195. -- for each divider found
  196. for st,sp in function() return string.find(str,div,pos,true) end do
  197. table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
  198. pos = sp + 1 -- Jump past current divider
  199. end
  200. table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  201. return arr
  202. end
  203.  
  204.  
  205.  
  206. local function CameraStateToString(frame, cs)
  207.  
  208. local name = cs.name
  209. local stateFormat = CAMERA_STATE_FORMATS[name]
  210. local cameraID = CAMERA_IDS[name]
  211.  
  212. if not stateFormat or not cameraID then return nil end
  213.  
  214. local result = frame .. '|' .. cameraID .. '|' .. cs.mode
  215.  
  216. for i=1, #stateFormat do
  217. local num = cs[stateFormat[i]]
  218. if not num then return nil end
  219. result = result .. '|' .. num
  220. end
  221.  
  222. return result
  223. end
  224.  
  225.  
  226. local function StringToCameraState(str)
  227. local s_arr = explode('|', str)
  228.  
  229. local frame = s_arr[1]
  230. local cameraID = s_arr[2]
  231. local mode = s_arr[3]
  232. local name = CAMERA_NAMES[cameraID+0]
  233. local stateFormat = CAMERA_STATE_FORMATS[name]
  234.  
  235. if not (cameraID and mode and name and stateFormat) then
  236. --echo ('ISSUE', cameraID , mode , name , stateFormat)
  237. return nil
  238. end
  239.  
  240. local result = {
  241. frame = frame,
  242. name = name,
  243. mode = mode,
  244. }
  245.  
  246. for i=1, #stateFormat do
  247. local num = s_arr[i+3]
  248.  
  249. if not num then return nil end
  250.  
  251. result[stateFormat[i]] = num
  252. end
  253.  
  254. return result
  255. end
  256.  
  257.  
  258. local function IsKeyframe(frame)
  259. return frame % KF_FRAMES == 0
  260. end
  261.  
  262.  
  263. local function RecordFrame(frame)
  264. --echo ('<camrec> recording frame', frame)
  265.  
  266. local str = CameraStateToString( frame, spGetCameraState() )
  267.  
  268. local out = assert(io.open(filename, "a+"), "Unable to save camera recording file to "..filename)
  269. out:write(str .. "\n")
  270. assert(out:close())
  271. end
  272.  
  273. local function FileToData(filename)
  274. --local file = assert(io.open(filename,'r'), "Unable to load camera recording file from "..filename)
  275. local file = io.open(filename,'r')
  276. if not file then
  277. echo('<Camrec> No such file ' .. filename )
  278. return {}
  279. end
  280. local recData = {}
  281. local prevkey = 0
  282. while true do
  283. line = file:read()
  284. if not line then
  285. break
  286. end
  287. --echo ('<camrec> opening line ', line)
  288. local data = StringToCameraState( line )
  289. --recData[ data.frame ] = data
  290. if prevkey ~= 0 then
  291. --echo('<camrec> adding data', prevkey )
  292. recData[ prevkey+0 ] = data
  293. end
  294. prevkey = data.frame
  295. end
  296. return recData
  297. end
  298.  
  299.  
  300. local ID = Game.mapName
  301.  
  302. local function RunInit()
  303. if ranInit then
  304. return true
  305. end
  306.  
  307. if not ID or ID == '' then
  308. echo ("no gameID")
  309. return false
  310. end
  311. ranInit = true
  312.  
  313. local ID_enc = Base64Encode( ID )
  314. local ID_dec = Base64Decode( ID_enc )
  315. filename = 'camrec_' .. ID_enc .. '.txt'
  316. --echo ('<camrec>',filename)
  317.  
  318. recData = FileToData( filename )
  319. return true
  320. end
  321. --------------------------------------------------------------------------------
  322. --------------------------------------------------------------------------------
  323.  
  324. function widget:GameFrame()
  325. local frame = Spring.GetGameFrame()
  326. if frame < 1 then return end
  327. if not RunInit() then
  328. echo ("error")
  329. return
  330. end
  331.  
  332. if recording then
  333. if IsKeyframe(frame) then
  334. RecordFrame(frame)
  335. end
  336. end
  337.  
  338. if playing then
  339. if recData[frame] then
  340. --echo ('playing frame', frame)
  341. spSetCameraState(recData[frame], KF_FRAMES / 32)
  342. end
  343. end
  344.  
  345. end
  346.  
  347. function CamRecord()
  348. recording = not recording
  349. echo (recording and '<Camera Recording> Recording begun.' or '<Camera Recording> Recording stopped.')
  350. end
  351.  
  352. function CamPlay()
  353. playing = not playing
  354. echo (playing and '<Camera Recording> Playback begun.' or '<Camera Recording> Playback stopped.')
  355. end
  356.  
  357. function widget:TextCommand(command)
  358. if not command then return end
  359. if string.find(command, "cam_record") then
  360. CamRecord()
  361. end
  362. if string.find(command, "cam_play") then
  363. CamPlay()
  364. end
  365. end
  366.  
  367. function widget:Initialize()
  368. end
  369.  
  370.  
  371. --------------------------------------------------------------------------------
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×