mvd.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. --[[
  2. LUA MVD script for the extended hifi-q2admin (https://github.com/hifi/q2admin/)
  3. Function:
  4. Automatically starts recording of Q2Pro Multi View Demos within the Action Quake2 - TNG Mod when match starts.
  5. It also deletes the mvd-file if match countdown failed.
  6. Fully working initial version by Paul Klumpp, 2012-11-12
  7. Please record your big changes here and increase version number:
  8. 1.4: added round_state check, added whether a cvar "q2a_mvd_autorecord" needs to be set so the recording happens runs (for lrcon usage)
  9. 1.3: added "exec_script_on_system_after_recording" for config.lua. BE CAREFUL with those shellscripts!
  10. 1.2: Checks for (Action Quake 2 and sv_mvd_enable != 0). And added useful mvd defaults for scoreboard
  11. 1.1: Even works on aq2-tng public teamplay mode now
  12. 1.0: All working on aq2-tng matchmode
  13. 0.9: Last Debug
  14. config.lua example, with all options:
  15. ------------------------------
  16. plugins = {
  17. some_other_plugin = {
  18. blah = "blah"
  19. },
  20. mvd = {
  21. mvd_webby = "http://mvd2.quadaver.org",
  22. exec_script_on_system_after_recording = "/home/gameservers/quake2/plugins/woot.sh",
  23. needs_cvar_q2a_mvd_autorecord = false
  24. }
  25. }
  26. ------------------------------
  27. !! Notice the "," in front of mvd = {
  28. --]]
  29. local game = gi.cvar("game", "").string
  30. local sv_mvd_enable = gi.cvar("sv_mvd_enable", "").string
  31. if game ~= "action" or sv_mvd_enable == "0" then
  32. gi.dprintf("mvd.lua WARNING: This script only loads when game is 'action' and when sv_mvd_enable is '1' or '2'!\n")
  33. return 0
  34. end
  35. -- if we came to here, it's action!
  36. local version = "1.4hau"
  37. gi.AddCommandString("sets q2a_mvd "..version.."\n")
  38. local mvd_webby -- configure this one in the config.lua
  39. local exec_script_on_system_after_recording -- configure this one in the config.lua
  40. local needs_cvar_q2a_mvd_autorecord -- configure this one in the config.lua
  41. -- state vars
  42. local teams_ready = {}
  43. local mvd_records = false
  44. local started_on_round_state = ""
  45. local mvd_pathfile = ""
  46. local mvd_file = ""
  47. ---- small helper functions follow: ----
  48. function file_exists(fname)
  49. local f=io.open(fname,"r")
  50. if f~=nil then
  51. io.close(f)
  52. return true
  53. else
  54. return false
  55. end
  56. end
  57. function string_strip(badstring)
  58. goodstring = badstring
  59. goodstring = string.gsub(goodstring, "%W", "")
  60. return goodstring
  61. end
  62. function reset_ready_state()
  63. for k in pairs (teams_ready) do
  64. teams_ready[k] = nil
  65. end
  66. end
  67. function get_round_state()
  68. local teamplay = gi.cvar("teamplay", "").string
  69. if teamplay == "1" then
  70. local cvar_t1 = gi.cvar("t1", "").string
  71. local cvar_t2 = gi.cvar("t2", "").string
  72. local cvar_t2 = gi.cvar("t3", "").string
  73. local rstate = ""..t1.." "..t2.." "..t3..""
  74. return rstate
  75. end
  76. return 0
  77. end
  78. ---- big helper functions follow: ----
  79. function mvd_start_recording()
  80. -- if teamplay == 1 then save round state .. for later usage -
  81. -- this is to know if record stopping also can delete the newly recorded mvd, because the rounds did not happen.
  82. started_on_round_state = get_round_state()
  83. -- build filename
  84. local now = os.date("%Y-%m-%d_%H%M%S")
  85. local mapname = gi.cvar("mapname", "").string
  86. local filename = now
  87. for k,v in pairs(teams_ready) do
  88. filename = filename .. "_" .. v
  89. --gi.AddCommandString("say starting team: "..v.."\n")
  90. end
  91. filename = filename.."_"..mapname
  92. gi.AddCommandString("mvdrecord -z "..filename.."\n")
  93. mvd_file = filename..".mvd2.gz"
  94. mvd_pathfile = game.."/demos/"..mvd_file
  95. mvd_records = true
  96. local hostname = gi.cvar("hostname", "").string
  97. gi.AddCommandString("say '"..hostname.."' MVD2 recording started: "..mvd_file.."\n")
  98. end
  99. function mvd_stop_and_delete()
  100. local current_round_state = get_round_state()
  101. if current_round_state == started_on_round_state then
  102. if mvd_records == true then
  103. gi.AddCommandString("mvdstop\n")
  104. mvd_records = false
  105. -- if file exists in game dir, delete it
  106. --$game demos/lala.mvd2.gz
  107. if file_exists(mvd_pathfile) then
  108. if os.remove(mvd_pathfile) then
  109. gi.AddCommandString("say Deleted the MVD2: "..mvd_pathfile.."\n")
  110. else
  111. gi.AddCommandString("say Problems deleting MVD2: "..mvd_pathfile.."\n")
  112. end
  113. end
  114. mvd_pathfile = ""
  115. mvd_file = ""
  116. end
  117. else
  118. if mvd_records == true then
  119. gi.AddCommandString("say Be quick to ready up again! MVD2 is still recording!\n")
  120. end
  121. end
  122. end
  123. function mvd_stop()
  124. if mvd_records == true then
  125. gi.AddCommandString("mvdstop\n")
  126. mvd_records = false
  127. -- if file exists in game dir, advertise it:
  128. if file_exists(mvd_pathfile) then
  129. --$game demos/lala.mvd2.gz
  130. if mvd_webby ~= nil then
  131. gi.AddCommandString("say Download the MVD2("..mvd_file..") on "..mvd_webby.."\n")
  132. else
  133. gi.AddCommandString("say Download the MVD2: "..mvd_file.."\n")
  134. end
  135. if exec_script_on_system_after_recording ~= nil then
  136. gi.dprintf('mvd.lua mvd_stop(): os.execute '..exec_script_on_system_after_recording..' "'..game..'" "'..mvd_file..'"\n')
  137. local returnstuff = os.execute(exec_script_on_system_after_recording..' "'..game..'" "'..mvd_file..'"')
  138. gi.dprintf('mvd.lua mvd_stop(): os.execute returns: '..returnstuff..'\n')
  139. end
  140. end
  141. mvd_pathfile = ""
  142. mvd_file = ""
  143. end
  144. end
  145. ---- q2admin hook functions follow: ----
  146. function q2a_load(config)
  147. gi.dprintf("mvd.lua q2a_load(): "..version.." for Action Quake 2 loaded.\n")
  148. mvd_webby = config.mvd_webby
  149. exec_script_on_system_after_recording = config.exec_script_on_system_after_recording
  150. if mvd_webby == nil then
  151. gi.dprintf("mvd.lua q2a_load(): You may define 'mvd_webby' in the config.lua file.\n")
  152. -- safe default:
  153. mvd_webby = nil
  154. end
  155. if exec_script_on_system_after_recording == nil then
  156. gi.dprintf("mvd.lua q2a_load(): You may define 'exec_script_on_system_after_recording' in the config.lua file.\n")
  157. -- safe default:
  158. exec_script_on_system_after_recording = nil
  159. end
  160. if needs_cvar_q2a_mvd_autorecord == nil then
  161. gi.dprintf("mvd.lua q2a_load(): You may define 'needs_cvar_q2a_mvd_autorecord' in the config.lua file.\n")
  162. needs_cvar_q2a_mvd_autorecord = false
  163. end
  164. -- set some working defaults (workarounds) if game is action...
  165. if game == "action" then
  166. gi.AddCommandString('set sv_mvd_nomsg 0\n')
  167. gi.AddCommandString('set sv_mvd_begincmd "putaway; h_cycle;"\n')
  168. gi.AddCommandString('set sv_mvd_scorecmd "h_cycle;"\n')
  169. gi.AddCommandString('alias h_cycle "h_cycle_sb; h_cycle_sb; h_cycle_sb; h_cycle_sb; h_cycle_sb;"\n')
  170. gi.AddCommandString('alias h_cycle_sb "wait; help; wait 75; help; wait 100; putaway;"\n')
  171. gi.AddCommandString('set mvd_default_map wfall\n')
  172. end
  173. end
  174. function LevelChanged(level)
  175. gi.dprintf("mvd.lua LevelChanged(): it has been changed to %s\n", level)
  176. -- reset teams ready state
  177. reset_ready_state()
  178. -- if mvd is recording, stop mvd recording and advertise
  179. mvd_stop()
  180. end
  181. function LogMessage(msg)
  182. --debug of all incoming msg:
  183. --gi.AddCommandString("say "..msg.."\n")
  184. local team_ready = string.match(msg, "(.+) is ready to play!")
  185. if team_ready ~= nil then
  186. -- strip whitespace and bad chars
  187. team_ready = string_strip(team_ready)
  188. --gi.AddCommandString("say adding team: "..team_ready.."\n")
  189. table.insert(teams_ready,team_ready)
  190. return true
  191. end
  192. local team_notready = string.match(msg, "(.+) is no longer ready to play!")
  193. if team_notready ~= nil then
  194. -- strip whitespace and bad chars
  195. team_notready = string_strip(team_notready)
  196. for k,v in pairs(teams_ready) do
  197. if v == team_notready then
  198. table.remove(teams_ready, k)
  199. end
  200. end
  201. return true
  202. end
  203. local match = string.match(msg, "The round will begin in 20 seconds!")
  204. if match ~= nil then
  205. -- if mvd not recording then start mvd record
  206. if mvd_records == false then
  207. if needs_cvar_q2a_mvd_autorecord == true then
  208. local q2a_mvd_autorecord = gi.cvar("q2a_mvd_autorecord", "").string
  209. if q2a_mvd_autorecord ~= nil and q2a_mvd_autorecord ~= "0" then
  210. mvd_start_recording()
  211. end
  212. else
  213. mvd_start_recording()
  214. end
  215. end
  216. return true
  217. end
  218. local match = string.match(msg, "Both Teams Must Be Ready!")
  219. if match ~= nil then
  220. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  221. mvd_stop_and_delete()
  222. return true
  223. end
  224. -- this message can't be catched by LogMessage .. .:(
  225. --local match = string.match(msg, "Recording local MVD to (.+)")
  226. --if match ~= nil then
  227. -- mvd_pathfile = match
  228. -- mvd_records = true
  229. -- gi.AddCommandString("say MVD2 recording started: "..mvd_pathfile.."\n")
  230. -- return true
  231. --end
  232. local match = string.match(msg, "Scores and time was resetted by request of captains")
  233. if match ~= nil then
  234. -- reset teams ready state
  235. reset_ready_state()
  236. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  237. mvd_stop_and_delete()
  238. return true
  239. end
  240. local match = string.match(msg, "Match is over, waiting for next map, please vote a new one..")
  241. if match ~= nil then
  242. -- reset teams ready state
  243. reset_ready_state()
  244. -- if mvd is recording, stop mvd recording and advertise
  245. mvd_stop()
  246. return true
  247. end
  248. local match = string.match(msg, "Game ending at:.+")
  249. if match ~= nil then
  250. -- reset teams ready state
  251. reset_ready_state()
  252. -- if mvd is recording, stop mvd recording and advertise
  253. mvd_stop()
  254. return true
  255. end
  256. end -- of LogMessage
  257. -- The round will begin in 20 seconds!
  258. -- if mvd is not recording then start mvd record
  259. -- Both Teams Must Be Ready!
  260. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  261. -- Recording local MVD to demos/1352750130_Team#2_Team#1.mvd2.gz
  262. -- save full filename
  263. -- set mvd_records state to true
  264. -- Scores and time was resetted by request of captains
  265. -- if mvd is currently recording, then stop mvd recording and delete file
  266. -- Match is over, waiting for next map, please vote a new one..
  267. -- if mvd is currently recording, then stop mvd recording (and keep file) and advertise!