From 4b848e24296d49e13639a487869d6948cd1c80cf Mon Sep 17 00:00:00 2001
From: Freddy <schro.sb@gmail.com>
Date: Fri, 9 Mar 2018 17:35:59 +0100
Subject: [PATCH] func_rotating: stop sound when deactivated, minor cleanup

---
 qcsrc/common/sounds/sound.qh           |  2 +-
 qcsrc/common/triggers/func/rotating.qc | 49 +++++++++++++++-----------
 qcsrc/common/triggers/func/rotating.qh |  1 +
 qcsrc/common/triggers/spawnflags.qh    |  5 +++
 4 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/qcsrc/common/sounds/sound.qh b/qcsrc/common/sounds/sound.qh
index 8c4aecbda0..a22e0e70ee 100644
--- a/qcsrc/common/sounds/sound.qh
+++ b/qcsrc/common/sounds/sound.qh
@@ -22,7 +22,7 @@ const int CH_PLAYER_SINGLE = 7;
 // const int CH_BGM_SINGLE = -8;
 const int CH_BGM_SINGLE = 8;
 const int CH_AMBIENT = -9;
-// const int CH_AMBIENT_SINGLE = 9;
+const int CH_AMBIENT_SINGLE = 9;
 
 const float ATTEN_NONE = 0;
 const float ATTEN_MIN = 0.015625;
diff --git a/qcsrc/common/triggers/func/rotating.qc b/qcsrc/common/triggers/func/rotating.qc
index 6268dcfeb8..35351ee08e 100644
--- a/qcsrc/common/triggers/func/rotating.qc
+++ b/qcsrc/common/triggers/func/rotating.qc
@@ -1,6 +1,5 @@
 #include "rotating.qh"
 #ifdef SVQC
-const int FUNC_ROTATING_STARTOFF = BIT(4);
 
 void func_rotating_setactive(entity this, int astate)
 {
@@ -15,9 +14,18 @@ void func_rotating_setactive(entity this, int astate)
 		this.active = astate;
 
 	if(this.active  == ACTIVE_NOT)
+	{
 		this.avelocity = '0 0 0';
+		stopsound(this, CH_AMBIENT_SINGLE);
+	}
 	else
+	{
 		this.avelocity = this.pos1;
+		if(this.noise && this.noise != "")
+		{
+			_sound(this, CH_AMBIENT_SINGLE, this.noise, VOL_BASE, ATTEN_IDLE);
+		}
+	}
 }
 
 void func_rotating_reset(entity this)
@@ -26,13 +34,20 @@ void func_rotating_reset(entity this)
 
 	if(this.spawnflags & FUNC_ROTATING_STARTOFF)
 	{
-		this.avelocity = '0 0 0';
-		this.active = ACTIVE_NOT;
+		this.setactive(this, ACTIVE_NOT);
 	}
 	else
 	{
-		this.avelocity = this.pos1;
-		this.active = ACTIVE_ACTIVE;
+		this.setactive(this, ACTIVE_ACTIVE);
+	}
+}
+
+void func_rotating_init_for_player(entity this, entity player)
+{
+	if (this.noise && this.noise != "" && this.active == ACTIVE_ACTIVE && IS_REAL_CLIENT(player))
+	{
+		msg_entity = player;
+		soundto (MSG_ONE, this, CH_AMBIENT_SINGLE, this.noise, VOL_BASE, ATTEN_IDLE);
 	}
 }
 
@@ -46,37 +61,24 @@ dmgtime : See above.
 
 spawnfunc(func_rotating)
 {
-	if (this.noise != "")
+	if (this.noise && this.noise != "")
 	{
 		precache_sound(this.noise);
-		ambientsound(this.origin, this.noise, VOL_BASE, ATTEN_IDLE);
 	}
 
 	this.setactive = func_rotating_setactive;
 
 	if (!this.speed)
 		this.speed = 100;
-	// FIXME: test if this turns the right way, then remove this comment (negate as needed)
-	if (this.spawnflags & BIT(2)) // X (untested)
+	if (this.spawnflags & FUNC_ROTATING_XAXIS)
 		this.avelocity = '0 0 1' * this.speed;
-	// FIXME: test if this turns the right way, then remove this comment (negate as needed)
-	else if (this.spawnflags & BIT(3)) // Y (untested)
+	else if (this.spawnflags & FUNC_ROTATING_YAXIS)
 		this.avelocity = '1 0 0' * this.speed;
-	// FIXME: test if this turns the right way, then remove this comment (negate as needed)
 	else // Z
 		this.avelocity = '0 1 0' * this.speed;
 
 	this.pos1 = this.avelocity;
 
-	// do this after setting pos1, so we can safely reactivate the func_rotating
-	if(this.spawnflags & FUNC_ROTATING_STARTOFF)
-	{
-		this.avelocity = '0 0 0';
-		this.active = ACTIVE_NOT;
-	}
-	else
-		this.active = ACTIVE_ACTIVE;
-
     if(this.dmg && (this.message == ""))
         this.message = " was squished";
     if(this.dmg && (this.message2 == ""))
@@ -99,5 +101,10 @@ spawnfunc(func_rotating)
 	setthink(this, SUB_NullThink); // for PushMove
 
 	this.reset = func_rotating_reset;
+	this.reset(this);
+
+	// maybe send sound to new players
+	IL_PUSH(g_initforplayer, this);
+	this.init_for_player = func_rotating_init_for_player;
 }
 #endif
diff --git a/qcsrc/common/triggers/func/rotating.qh b/qcsrc/common/triggers/func/rotating.qh
index 6f70f09bee..7c39519e10 100644
--- a/qcsrc/common/triggers/func/rotating.qh
+++ b/qcsrc/common/triggers/func/rotating.qh
@@ -1 +1,2 @@
 #pragma once
+#include "../spawnflags.qh"
diff --git a/qcsrc/common/triggers/spawnflags.qh b/qcsrc/common/triggers/spawnflags.qh
index 69962e54df..5a3e4b1f46 100644
--- a/qcsrc/common/triggers/spawnflags.qh
+++ b/qcsrc/common/triggers/spawnflags.qh
@@ -42,6 +42,11 @@ const int DOOR_SECRET_YES_SHOOT = BIT(4); // shootable even if targeted
 const int PARTICLES_IMPULSE = BIT(1);
 const int PARTICLES_VISCULLING = BIT(2);
 
+// rotating
+const int FUNC_ROTATING_XAXIS = BIT(2);
+const int FUNC_ROTATING_YAXIS = BIT(3);
+const int FUNC_ROTATING_STARTOFF = BIT(4);
+
 // jumppads
 const int PUSH_ONCE = BIT(0);
 const int PUSH_SILENT = BIT(1); // not used?
-- 
2.39.5