MightyJetters

dynamicmusic.lua

Jan 5th, 2022
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.67 KB | None | 0 0
  1. --------------------------------------------------------------------------
  2. --[[ DynamicMusic class definition ]]
  3. --------------------------------------------------------------------------
  4.  
  5. return Class(function(self, inst)
  6.  
  7. --------------------------------------------------------------------------
  8. --[[ Constants ]]
  9. --------------------------------------------------------------------------
  10.  
  11. local SEASON_BUSY_MUSIC =
  12. {
  13. day =
  14. {
  15. autumn = "music_mod/music/music_work",
  16. winter = "music_mod/music/music_work_winter",
  17. spring = "music_mod/music/music_work_spring",
  18. summer = "music_mod/music/music_work_summer",
  19. lunar = "music_mod/music/music_work_lunar",
  20. sailing = "music_mod/music/music_work_sailing",
  21. },
  22. dusk =
  23. {
  24. autumn = "music_mod/music/music_work_dusk",
  25. winter = "music_mod/music/music_work_winter_dusk",
  26. spring = "music_mod/music/music_work_spring_dusk",
  27. summer = "music_mod/music/music_work_summer_dusk",
  28. lunar = "music_mod/music/music_work_lunar_dusk",
  29. sailing = "music_mod/music/music_work_sailing_dusk",
  30. },
  31. night =
  32. {
  33. autumn = "music_mod/music/music_work_night",
  34. winter = "music_mod/music/music_work_winter_night",
  35. spring = "music_mod/music/music_work_spring_night",
  36. summer = "music_mod/music/music_work_summer_night",
  37. lunar = "music_mod/music/music_work_lunar_night",
  38. sailing = "music_mod/music/music_work_sailing_night",
  39. },
  40. }
  41.  
  42. local TRIGGERED_DANGER_MUSIC =
  43. {
  44. malbatross =
  45. {
  46. "music_mod/music/malbatross",
  47. },
  48.  
  49. moonbase =
  50. {
  51. "music_mod/music/music_epicfight_moonbase",
  52. "music_mod/music/music_epicfight_moonbase_b",
  53. },
  54.  
  55. toadstool =
  56. {
  57. "music_mod/music/music_epicfight_toadboss",
  58. },
  59.  
  60. beequeen =
  61. {
  62. "music_mod/music/music_epicfight_4",
  63. },
  64.  
  65. dragonfly =
  66. {
  67. "music_mod/music/music_epicfight_3",
  68. },
  69.  
  70. klaus =
  71. {
  72. "music_mod/music/music_epicfight_5a",
  73. "music_mod/music/music_epicfight_5b",
  74. },
  75.  
  76. antlion =
  77. {
  78. "music_mod/music/music_epicfight_antlion",
  79. },
  80.  
  81. stalker =
  82. {
  83. "music_mod/music/music_epicfight_stalker",
  84. "music_mod/music/music_epicfight_stalker_b",
  85. },
  86.  
  87. alterguardian_phase1 =
  88. {
  89. "moonstorm/creatures/boss/alterguardian1/music_epicfight",
  90. },
  91. alterguardian_phase2 =
  92. {
  93. "moonstorm/creatures/boss/alterguardian2/music_epicfight",
  94. },
  95. alterguardian_phase3 =
  96. {
  97. "moonstorm/creatures/boss/alterguardian3/music_epicfight",
  98. },
  99.  
  100. pigking =
  101. {
  102. "dontstarve/music/music_pigking_minigame"
  103. },
  104.  
  105. default =
  106. {
  107. "music_mod/music/music_epicfight_ruins",
  108. },
  109. }
  110.  
  111. local SEASON_EPICFIGHT_MUSIC =
  112. {
  113. autumn = "music_mod/music/music_epicfight",
  114. winter = "music_mod/music/music_epicfight_winter",
  115. spring = "music_mod/music/music_epicfight_spring",
  116. summer = "music_mod/music/music_epicfight_summer",
  117. lunar = "music_mod/music/music_epicfight_lunar",
  118. sailing = "music_mod/music/music_epicfight_sailing",
  119. }
  120.  
  121. local SEASON_DANGER_MUSIC =
  122. {
  123. autumn = "music_mod/music/music_danger",
  124. winter = "music_mod/music/music_danger_winter",
  125. spring = "music_mod/music/music_danger_spring",
  126. summer = "music_mod/music/music_danger_summer",
  127. lunar = "music_mod/music/music_danger_lunar",
  128. sailing = "music_mod/music/music_danger_sailing",
  129. }
  130.  
  131. --------------------------------------------------------------------------
  132. --[[ Member variables ]]
  133. --------------------------------------------------------------------------
  134.  
  135. --Public
  136. self.inst = inst
  137.  
  138. --Private
  139. local _isruin = inst:HasTag("ruin") -- When in ruins
  140. local _iscave = _isruin or inst:HasTag("cave") -- When in caves
  141. local _isenabled = true -- Dynamic Music enabled
  142. local _busytask = nil
  143. local _dangertask = nil -- If false, not in dangeer
  144. local _isday = nil
  145. local _isbusydirty = nil -- If false, working music will not change
  146. local _extendtime = nil
  147. local _triggeredlevel = nil
  148. local _soundemitter = nil
  149. local _activatedplayer = nil --cached for activation/deactivation only, NOT for logic use
  150. local _stingeractive = false -- Used to prevent music overlapping with stinger
  151. local _innightmare = false -- Player is in ruins
  152. local _inlunar = false -- Player is on lunar island
  153. local _inocean = false -- Player is not on land. (Triggers if ocean is nil in ChangeArea function)
  154. local _isfullmoon = false -- Only true when full moon.
  155.  
  156. --------------------------------------------------------------------------
  157. --[[ Private member functions ]]
  158. --------------------------------------------------------------------------
  159.  
  160. --Fade out busy music (track still plays; just muted)
  161. local function StopContinuous()
  162. if _busytask ~= nil then
  163. _busytask:Cancel()
  164. end
  165. _busytask = nil
  166. _extendtime = 0
  167. _soundemitter:SetParameter("busy", "intensity", 0)
  168. end
  169.  
  170. local function StopBusy(inst, istimeout)
  171. if not continuous_mode and _busytask ~= nil then
  172. if not istimeout then
  173. _busytask:Cancel()
  174. elseif _extendtime > 0 then
  175. local time = GetTime()
  176. if time < _extendtime then
  177. _busytask = inst:DoTaskInTime(_extendtime - time, StopBusy, true)
  178. _extendtime = 0
  179. return
  180. end
  181. end
  182. _busytask = nil
  183. _extendtime = 0
  184. _soundemitter:SetParameter("busy", "intensity", 0)
  185. end
  186. end
  187.  
  188. -- If _isbusydirty, then turn off _isbusydirty; stop busy music and start new busy music for phase/season.
  189. -- If not _isbusydirty, do nothing.
  190. local function StartBusy()
  191. if _busytask ~= nil and not _isbusydirty then
  192. _extendtime = GetTime() + 15
  193. elseif _soundemitter ~= nil and _dangertask == nil and not _stingeractive and (continuous_mode or _extendtime == 0 or GetTime() >= _extendtime) and _isenabled then
  194. if _isbusydirty then
  195. _isbusydirty = false
  196. _soundemitter:KillSound("busy")
  197. -- Check if music for phase and season exist
  198. local season = inst.state.season
  199. local phase = inst.state.phase
  200. if SEASON_BUSY_MUSIC[phase] == nil then
  201. phase = "day"
  202. end
  203. if SEASON_BUSY_MUSIC[phase][season] == nil then
  204. season = "autumn"
  205. end
  206. if _inlunar then
  207. season = "lunar"
  208. end
  209. _soundemitter:PlaySound(
  210. (_innightmare and "music_mod/music/music_work_ruins") or
  211. (_iscave and "music_mod/music/music_work_cave") or
  212. (_isfullmoon and "music_mod/music/music_work_fullmoon") or
  213. (_inlunar and SEASON_BUSY_MUSIC[phase]["lunar"]) or
  214. (_inocean and SEASON_BUSY_MUSIC[phase]["sailing"]) or
  215. (SEASON_BUSY_MUSIC[phase][season]),
  216. "busy")
  217. end
  218. _soundemitter:SetParameter("busy", "intensity", 1)
  219. _busytask = inst:DoTaskInTime(15, StopBusy, true)
  220. _extendtime = 0
  221. end
  222. end
  223.  
  224. local function ExtendBusy()
  225. if _busytask ~= nil then
  226. _extendtime = math.max(_extendtime, GetTime() + 10)
  227. end
  228. end
  229.  
  230. local function StopDanger(inst, istimeout)
  231. if _dangertask ~= nil then
  232. if not istimeout then
  233. _dangertask:Cancel()
  234. elseif _extendtime > 0 then
  235. local time = GetTime()
  236. if time < _extendtime then
  237. _dangertask = inst:DoTaskInTime(_extendtime - time, StopDanger, true)
  238. _extendtime = 0
  239. return
  240. end
  241. end
  242. _dangertask = nil
  243. _extendtime = 0
  244. _soundemitter:KillSound("danger")
  245. if continuous_mode then
  246. StartBusy()
  247. end
  248. end
  249. end
  250.  
  251. -- Check if near epic. If yes, play season/location epicfight; if no, play season/location danger.
  252. local function StartDanger(player)
  253. if _dangertask ~= nil then
  254. _extendtime = GetTime() + 10
  255. elseif _isenabled then
  256. StopContinuous()
  257. -- Check if music for season exists
  258. local season = inst.state.season
  259. if SEASON_DANGER_MUSIC[season] == nil then
  260. season = "autumn"
  261. end
  262. _soundemitter:PlaySound(
  263. GetClosestInstWithTag("epic", player, 30) ~= nil
  264. and ((_innightmare and "music_mod/music/music_epicfight_ruins") or
  265. (_iscave and "music_mod/music/music_epicfight_cave") or
  266. (_inlunar and "music_mod/music/music_epicfight_lunar") or
  267. (_inocean and "music_mod/music/music_epicfight_sailing") or
  268. (SEASON_EPICFIGHT_MUSIC[season]))
  269. or ((_innightmare and "music_mod/music/music_danger_ruins") or
  270. (_iscave and "music_mod/music/music_danger_cave") or
  271. (_inlunar and "music_mod/music/music_danger_lunar") or
  272. (_inocean and "music_mod/music/music_danger_sailing") or
  273. (SEASON_DANGER_MUSIC[season])),
  274. "danger")
  275. _dangertask = inst:DoTaskInTime(10, StopDanger, true)
  276. _extendtime = 0
  277. end
  278. end
  279.  
  280. local function StartTriggeredDanger(player, data)
  281. local level = math.max(1, math.floor(data ~= nil and data.level or 1))
  282. if _triggeredlevel == level then
  283. _extendtime = GetTime() + 10
  284. elseif _isenabled then
  285. StopContinuous()
  286. StopDanger()
  287. local music = data ~= nil and TRIGGERED_DANGER_MUSIC[data.name or "default"] or TRIGGERED_DANGER_MUSIC.default
  288. _soundemitter:PlaySound(music[level] or music[1], "danger")
  289. _dangertask = inst:DoTaskInTime(10, StopDanger, true)
  290. _triggeredlevel = level
  291. _extendtime = 0
  292. end
  293. end
  294.  
  295.  
  296. -- If player attacking non-passive, StartDanger. If working, StartBusy.
  297. local function CheckAction(player)
  298. if player:HasTag("attack") then
  299. local target = player.replica.combat:GetTarget()
  300. if target ~= nil and
  301. not (target:HasTag("prey") or
  302. target:HasTag("bird") or
  303. target:HasTag("butterfly") or
  304. target:HasTag("shadow") or
  305. target:HasTag("thorny") or
  306. target:HasTag("smashable") or
  307. target:HasTag("wall") or
  308. target:HasTag("smoldering") or
  309. target:HasTag("veggie")) then
  310. StartDanger(player)
  311. return
  312. end
  313. end
  314. if player:HasTag("working") then
  315. StartBusy()
  316. end
  317. end
  318.  
  319. -- If attacked, StartDanger.
  320. local function OnAttacked(player, data)
  321. if data ~= nil and
  322. --For a valid client side check, shadowattacker must be
  323. --false and not nil, pushed from player_classified
  324. (data.isattackedbydanger == true or
  325. --For a valid server side check, attacker must be non-nil
  326. (data.attacker ~= nil and not (data.attacker:HasTag("thorny")
  327. or data.attacker:HasTag("smolder")
  328. or data.attacker:HasTag("shadow")
  329. ))) then
  330.  
  331. StartDanger(player)
  332. end
  333. end
  334.  
  335. -- StopContinuous, play insane stinger, StartBusy.
  336. local function OnInsane()
  337. if _dangertask == nil and _isenabled then
  338. _soundemitter:PlaySound("dontstarve/sanity/gonecrazy_stinger")
  339. StopContinuous()
  340. --Repurpose this as a delay before stingers or busy can start again
  341. _extendtime = GetTime() + 15
  342. if continuous_mode then
  343. self.inst:DoTaskInTime(8, function(inst) -- Give the stinger time to play before playing music
  344. StartBusy()
  345. end)
  346. end
  347. end
  348. end
  349.  
  350. -- StopContinuous, play insane stinger, StartBusy.
  351. --[[
  352. local function OnEnlightened()
  353. if _dangertask == nil and _isenabled then
  354. _soundemitter:PlaySound("dontstarve/sanity/gonecrazy_stinger")
  355. StopContinuous()
  356. --Repurpose this as a delay before stingers or busy can start again
  357. _extendtime = GetTime() + 15
  358. if continuous_mode then
  359. self.inst:DoTaskInTime(8, function(inst) -- Give the stinger time to play before playing music
  360. StartBusy()
  361. end)
  362. end
  363. end
  364. end
  365. ]]--
  366.  
  367. -- On phase change, wait until not in danger. Then check new phase.
  368. -- If new phase is day/dusk, play stinger. StartBusy.
  369. local function OnPhase(inst, phase)
  370. _isfullmoon = false -- Is not a full moon by default.
  371. _isday = phase == "day"
  372. if _dangertask ~= nil or not _isenabled then
  373. _isbusydirty = true
  374. return
  375. end
  376. --Don't want to play overlapping stingers
  377. local time
  378. if _busytask == nil and _extendtime ~= 0 then
  379. time = GetTime()
  380. if time < _extendtime then
  381. _isbusydirty = true
  382. return
  383. end
  384. end
  385. if _isday then
  386. _soundemitter:PlaySound("music_mod/music/music_dawn_stinger")
  387. if continuous_mode then
  388. _stingeractive = true
  389. end
  390. end
  391. if phase == "dusk" then
  392. _soundemitter:PlaySound("music_mod/music/music_dusk_stinger")
  393. if continuous_mode then
  394. _stingeractive = true
  395. end
  396. end
  397.  
  398. if phase ~= "night" then
  399. self.inst:DoTaskInTime(8, function(inst) -- Give the stinger time to play before changing music
  400. _isbusydirty = true
  401. if continuous_mode then
  402. _stingeractive = false
  403. StartBusy()
  404. end
  405. end)
  406. else
  407. self.inst:DoTaskInTime(2, function(inst) -- No stinger. Wait a shorter time.
  408. if TheWorld.state.isfullmoon then -- If full moon, then _isfullmoon.
  409. _isfullmoon = true
  410. -- print("Is TheWorld.state.isfullmoon?:")
  411. -- print(TheWorld.state.isfullmoon)
  412. -- print("Is _isfullmoon?:")
  413. -- print("_isfullmoon")
  414. end
  415. _isbusydirty = true
  416. if continuous_mode then
  417. StartBusy()
  418. end
  419. end)
  420. end
  421. StopContinuous()
  422. --Repurpose this as a delay before stingers or busy can start again
  423. _extendtime = (time or GetTime()) + 15
  424. end
  425.  
  426. -- Check if player is in ruins/atrium, lunar, or ocean. Lunar and ocean overlap; check lunar first.
  427. local function OnChangeArea(player)
  428. if player.components.areaaware then
  429. local nightmare = player.components.areaaware:CurrentlyInTag("Nightmare")
  430. local lunar = player.components.areaaware:CurrentlyInTag("lunacyarea") --true if lunar, false if mainland, nil if not on land
  431. local ocean = player.components.areaaware:CurrentlyInTag("not_mainland") --true if lunar, false if mainland, nil if not on land
  432. -- print("Printing: nightmare, lunar, ocean...")
  433. -- print(nightmare)
  434. -- print(lunar)
  435. -- print(ocean)
  436. if nightmare == true then
  437. if nightmare ~= _innightmare then
  438. _innightmare = nightmare
  439. _isbusydirty = true
  440. if continuous_mode then
  441. StartBusy()
  442. end
  443. end
  444. end
  445. if lunar then
  446. if lunar ~= _inlunar then
  447. _inlunar = lunar
  448. if not _isbusydirty then
  449. _isbusydirty = true
  450. if continuous_mode then
  451. -- print("Lunar - StartBusy")
  452. StartBusy()
  453. end
  454. end
  455. end
  456. elseif not lunar then
  457. if lunar ~= _inlunar then
  458. _inlunar = false
  459. if not _isbusydirty then
  460. _isbusydirty = true
  461. if continuous_mode then
  462. -- print("Not Lunar - StartBusy")
  463. StartBusy()
  464. end
  465. end
  466. end
  467. end
  468. if ocean == nil then
  469. if not lunar then
  470. if _inocean ~= true then
  471. _inocean = true
  472. if not _isbusydirty then
  473. _isbusydirty = true
  474. if continuous_mode then
  475. -- print("Ocean is nil, Not Lunar - StartBusy")
  476. StartBusy()
  477. end
  478. end
  479. end
  480. end
  481. elseif ocean ~= nil then
  482. if _inocean ~= false then
  483. _inocean = false
  484. if not _isbusydirty then
  485. _isbusydirty = true
  486. if continuous_mode then
  487. -- print("Not Ocean, Not Lunar - StartBusy")
  488. StartBusy()
  489. end
  490. end
  491. end
  492. end
  493. end
  494. end
  495.  
  496. local function StartPlayerListeners(player)
  497. inst:ListenForEvent("buildsuccess", StartBusy, player)
  498. inst:ListenForEvent("gotnewitem", ExtendBusy, player)
  499. inst:ListenForEvent("performaction", CheckAction, player)
  500. inst:ListenForEvent("attacked", OnAttacked, player)
  501. inst:ListenForEvent("goinsane", OnInsane, player)
  502. inst:ListenForEvent("triggeredevent", StartTriggeredDanger, player)
  503. inst:ListenForEvent("changearea", OnChangeArea, player)
  504. end
  505.  
  506. local function StopPlayerListeners(player)
  507. inst:RemoveEventCallback("buildsuccess", StartBusy, player)
  508. inst:RemoveEventCallback("gotnewitem", ExtendBusy, player)
  509. inst:RemoveEventCallback("performaction", CheckAction, player)
  510. inst:RemoveEventCallback("attacked", OnAttacked, player)
  511. inst:RemoveEventCallback("goinsane", OnInsane, player)
  512. inst:RemoveEventCallback("triggeredevent", StartTriggeredDanger, player)
  513. inst:RemoveEventCallback("changearea", OnChangeArea, player)
  514. end
  515.  
  516.  
  517. local function OnSeason()
  518. _isbusydirty = true
  519. end
  520.  
  521. local function StartSoundEmitter()
  522. if _soundemitter == nil then
  523. _soundemitter = TheFocalPoint.SoundEmitter
  524. _extendtime = 0
  525. _isbusydirty = true
  526. if not _iscave then
  527. _isday = inst.state.isday
  528. inst:WatchWorldState("phase", OnPhase)
  529. inst:WatchWorldState("season", OnSeason)
  530. end
  531. end
  532. end
  533.  
  534. local function StopSoundEmitter()
  535. if _soundemitter ~= nil then
  536. StopDanger()
  537. StopContinuous()
  538. _soundemitter:KillSound("busy")
  539. inst:StopWatchingWorldState("phase", OnPhase)
  540. inst:StopWatchingWorldState("season", OnSeason)
  541. _isday = nil
  542. _isbusydirty = nil
  543. _extendtime = nil
  544. _soundemitter = nil
  545. end
  546. end
  547.  
  548. --------------------------------------------------------------------------
  549. --[[ Private event handlers ]]
  550. --------------------------------------------------------------------------
  551.  
  552. local function OnPlayerActivated(inst, player)
  553. if _activatedplayer == player then
  554. return
  555. elseif _activatedplayer ~= nil and _activatedplayer.entity:IsValid() then
  556. StopPlayerListeners(_activatedplayer)
  557. end
  558. _activatedplayer = player
  559. StopSoundEmitter()
  560. StartSoundEmitter()
  561. StartPlayerListeners(player)
  562. if continuous_mode then
  563. StartBusy()
  564. end
  565. end
  566.  
  567. local function OnPlayerDeactivated(inst, player)
  568. StopPlayerListeners(player)
  569. if player == _activatedplayer then
  570. _activatedplayer = nil
  571. StopSoundEmitter()
  572. end
  573. end
  574.  
  575. local function OnEnableDynamicMusic(inst, enable)
  576. if _isenabled ~= enable then
  577. if not enable and _soundemitter ~= nil then
  578. StopDanger()
  579. StopContinuous()
  580. _soundemitter:KillSound("busy")
  581. _isbusydirty = true
  582. end
  583. _isenabled = enable
  584. end
  585. end
  586.  
  587. --------------------------------------------------------------------------
  588. --[[ Initialization ]]
  589. --------------------------------------------------------------------------
  590.  
  591. --Register events
  592. inst:ListenForEvent("playeractivated", OnPlayerActivated)
  593. inst:ListenForEvent("playerdeactivated", OnPlayerDeactivated)
  594. inst:ListenForEvent("enabledynamicmusic", OnEnableDynamicMusic)
  595.  
  596. --------------------------------------------------------------------------
  597. --[[ End ]]
  598. --------------------------------------------------------------------------
  599.  
  600. end)
Advertisement
Add Comment
Please, Sign In to add comment