From 5bd30e886480bdde34073be02fe088c631d4e66e Mon Sep 17 00:00:00 2001
From: Mario <zacjardine@y7mail.com>
Date: Sun, 30 Aug 2015 22:32:48 +1000
Subject: [PATCH] Add a couple of useful options to multijump

---
 defaultXonotic.cfg                         |  4 --
 mutators.cfg                               |  9 ++++
 qcsrc/client/main.qc                       |  2 +
 qcsrc/common/stats.qh                      |  4 +-
 qcsrc/server/autocvars.qh                  |  2 +
 qcsrc/server/mutators/mutator_multijump.qc | 55 ++++++++++++++++++----
 6 files changed, 61 insertions(+), 15 deletions(-)

diff --git a/defaultXonotic.cfg b/defaultXonotic.cfg
index 6a81e3507c..bdeeafab13 100644
--- a/defaultXonotic.cfg
+++ b/defaultXonotic.cfg
@@ -510,10 +510,6 @@ set g_bloodloss 0   "amount of health below which blood loss occurs"
 
 set g_footsteps 1	"serverside footstep sounds"
 
-set g_multijump 0	"Number of multiple jumps to allow (jumping again in the air), -1 allows for infinite jumps"
-set g_multijump_add 0	"0 = make the current z velocity equal to jumpvelocity, 1 = add jumpvelocity to the current z velocity"
-set g_multijump_speed -999999	"Minimum vertical speed a player must have in order to jump again"
-
 set g_throughfloor_debug 0 "enable debugging messages for throughfloor calculations"
 set g_throughfloor_damage_max_stddev 2 "Maximum standard deviation for splash damage"
 set g_throughfloor_force_max_stddev 10 "Maximum standard deviation for splash force"
diff --git a/mutators.cfg b/mutators.cfg
index 10a41a0693..0623ba3758 100644
--- a/mutators.cfg
+++ b/mutators.cfg
@@ -388,3 +388,12 @@ set g_rm_laser_force "400" "laser force, divided by laser count"
 // ================
 set g_breakablehook 0 "enable breakable hook mutator (hook can be damaged, and returns damage to the owner when broken)"
 set g_breakablehook_owner 0 "allow owner to break their own hook"
+
+
+// ===========
+//  multijump
+// ===========
+seta cl_multijump 1 "allow multijump mutator"
+set g_multijump 0	"Number of multiple jumps to allow (jumping again in the air), -1 allows for infinite jumps"
+set g_multijump_add 0	"0 = make the current z velocity equal to jumpvelocity, 1 = add jumpvelocity to the current z velocity"
+set g_multijump_speed -999999	"Minimum vertical speed a player must have in order to jump again"
diff --git a/qcsrc/client/main.qc b/qcsrc/client/main.qc
index a5085e893d..9b0c49a955 100644
--- a/qcsrc/client/main.qc
+++ b/qcsrc/client/main.qc
@@ -125,6 +125,8 @@ void CSQC_Init(void)
 	registercvar("cl_jumpspeedcap_min", "");
 	registercvar("cl_jumpspeedcap_max", "");
 
+	registercvar("cl_multijump", "1");
+
 	gametype = 0;
 
 	// hud_fields uses strunzone on the titles!
diff --git a/qcsrc/common/stats.qh b/qcsrc/common/stats.qh
index 74aa0e75fa..eff0eff804 100644
--- a/qcsrc/common/stats.qh
+++ b/qcsrc/common/stats.qh
@@ -217,8 +217,8 @@ const int STAT_CTF_FLAGSTATUS         = 124;
 // 163 empty?
 // 164 empty?
 // 165 empty?
-// 166 empty?
-// 167 empty?
+const int STAT_MULTIJUMP_DODGING                      = 166;
+const int STAT_MULTIJUMP_MAXSPEED                     = 167;
 const int STAT_GAMEPLAYFIX_UPVELOCITYCLEARSONGROUND   = 168;
 const int STAT_BUGRIGS_REVERSE_STOPPING               = 169;
 const int STAT_BUGRIGS_REVERSE_SPINNING               = 170;
diff --git a/qcsrc/server/autocvars.qh b/qcsrc/server/autocvars.qh
index 3783ee4195..42a48e9855 100644
--- a/qcsrc/server/autocvars.qh
+++ b/qcsrc/server/autocvars.qh
@@ -410,6 +410,8 @@ float autocvar_g_movement_highspeed = 1;
 int autocvar_g_multijump;
 float autocvar_g_multijump_add;
 float autocvar_g_multijump_speed;
+float autocvar_g_multijump_maxspeed;
+float autocvar_g_multijump_dodging = 1;
 string autocvar_g_mutatormsg;
 float autocvar_g_nexball_basketball_bouncefactor;
 float autocvar_g_nexball_basketball_bouncestop;
diff --git a/qcsrc/server/mutators/mutator_multijump.qc b/qcsrc/server/mutators/mutator_multijump.qc
index dd879e5de4..a5bb70b8e2 100644
--- a/qcsrc/server/mutators/mutator_multijump.qc
+++ b/qcsrc/server/mutators/mutator_multijump.qc
@@ -4,31 +4,40 @@
 	#include "../antilag.qh"
 #endif
 
-.float multijump_count;
-.float multijump_ready;
+.int multijump_count;
+.bool multijump_ready;
+.bool cvar_cl_multijump;
 
 #ifdef CSQC
 
 #define PHYS_MULTIJUMP 				getstati(STAT_MULTIJUMP)
 #define PHYS_MULTIJUMP_SPEED 		getstatf(STAT_MULTIJUMP_SPEED)
 #define PHYS_MULTIJUMP_ADD 			getstati(STAT_MULTIJUMP_ADD)
+#define PHYS_MULTIJUMP_MAXSPEED 	getstatf(STAT_MULTIJUMP_MAXSPEED)
+#define PHYS_MULTIJUMP_DODGING 		getstati(STAT_MULTIJUMP_DODGING)
 
 #elif defined(SVQC)
 
 #define PHYS_MULTIJUMP 				autocvar_g_multijump
 #define PHYS_MULTIJUMP_SPEED 		autocvar_g_multijump_speed
 #define PHYS_MULTIJUMP_ADD 			autocvar_g_multijump_add
+#define PHYS_MULTIJUMP_MAXSPEED 	autocvar_g_multijump_maxspeed
+#define PHYS_MULTIJUMP_DODGING 		autocvar_g_multijump_dodging
 
 
 .float stat_multijump;
 .float stat_multijump_speed;
 .float stat_multijump_add;
+.float stat_multijump_maxspeed;
+.float stat_multijump_dodging;
 
 void multijump_UpdateStats()
 {
 	self.stat_multijump = PHYS_MULTIJUMP;
 	self.stat_multijump_speed = PHYS_MULTIJUMP_SPEED;
 	self.stat_multijump_add = PHYS_MULTIJUMP_ADD;
+	self.stat_multijump_maxspeed = PHYS_MULTIJUMP_MAXSPEED;
+	self.stat_multijump_dodging = PHYS_MULTIJUMP_DODGING;
 }
 
 void multijump_AddStats()
@@ -36,6 +45,8 @@ void multijump_AddStats()
 	addstat(STAT_MULTIJUMP, AS_INT, stat_multijump);
 	addstat(STAT_MULTIJUMP_SPEED, AS_FLOAT, stat_multijump_speed);
 	addstat(STAT_MULTIJUMP_ADD, AS_INT, stat_multijump_add);
+	addstat(STAT_MULTIJUMP_MAXSPEED, AS_FLOAT, stat_multijump_maxspeed);
+	addstat(STAT_MULTIJUMP_DODGING, AS_INT, stat_multijump_dodging);
 }
 
 #endif
@@ -50,16 +61,31 @@ void PM_multijump()
 	}
 }
 
-float PM_multijump_checkjump()
+bool PM_multijump_checkjump()
 {
 	if(!PHYS_MULTIJUMP) { return false; }
 
-	if (!IS_JUMP_HELD(self) && !IS_ONGROUND(self)) // jump button pressed this frame and we are in midair
+#ifdef SVQC
+	bool client_multijump = self.cvar_cl_multijump;
+#elif defined(CSQC)
+	bool client_multijump = cvar("cl_multijump");
+
+	if(cvar("cl_multijump") > 1)
+		return false; // nope
+#endif
+
+	if (!IS_JUMP_HELD(self) && !IS_ONGROUND(self) && client_multijump) // jump button pressed this frame and we are in midair
 		self.multijump_ready = true;  // this is necessary to check that we released the jump button and pressed it again
 	else
 		self.multijump_ready = false;
 
-	if(!player_multijump && self.multijump_ready && (self.multijump_count < PHYS_MULTIJUMP || PHYS_MULTIJUMP == -1) && self.velocity_z > PHYS_MULTIJUMP_SPEED)
+	int phys_multijump = PHYS_MULTIJUMP;
+
+#ifdef CSQC
+	phys_multijump = (PHYS_MULTIJUMP) ? -1 : 0;
+#endif
+
+	if(!player_multijump && self.multijump_ready && (self.multijump_count < phys_multijump || phys_multijump == -1) && self.velocity_z > PHYS_MULTIJUMP_SPEED && (!PHYS_MULTIJUMP_MAXSPEED || vlen(self.velocity) <= PHYS_MULTIJUMP_MAXSPEED))
 	{
 		if (PHYS_MULTIJUMP)
 		{
@@ -76,19 +102,20 @@ float PM_multijump_checkjump()
 
 			if(player_multijump)
 			{
+				if(PHYS_MULTIJUMP_DODGING)
 				if(self.movement_x != 0 || self.movement_y != 0) // don't remove all speed if player isnt pressing any movement keys
 				{
 					float curspeed;
 					vector wishvel, wishdir;
 
-#ifdef SVQC
+/*#ifdef SVQC
 					curspeed = max(
 						vlen(vec2(self.velocity)), // current xy speed
 						vlen(vec2(antilag_takebackavgvelocity(self, max(self.lastteleporttime + sys_frametime, time - 0.25), time))) // average xy topspeed over the last 0.25 secs
 					);
-#elif defined(CSQC)
+#elif defined(CSQC)*/
 					curspeed = vlen(vec2(self.velocity));
-#endif
+//#endif
 
 					makevectors(self.v_angle_y * '0 1 0');
 					wishvel = v_forward * self.movement_x + v_right * self.movement_y;
@@ -98,7 +125,10 @@ float PM_multijump_checkjump()
 					self.velocity_y = wishdir_y * curspeed;
 					// keep velocity_z unchanged!
 				}
-				self.multijump_count += 1;
+				if (PHYS_MULTIJUMP > 0)
+				{
+					self.multijump_count += 1;
+				}
 			}
 		}
 		self.multijump_ready = false; // require releasing and pressing the jump button again for the next jump
@@ -121,6 +151,12 @@ MUTATOR_HOOKFUNCTION(multijump_PlayerJump)
 	return PM_multijump_checkjump();
 }
 
+MUTATOR_HOOKFUNCTION(multijump_GetCvars)
+{
+	GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_multijump, "cl_multijump");
+	return false;
+}
+
 MUTATOR_HOOKFUNCTION(multijump_BuildMutatorsString)
 {
 	ret_string = strcat(ret_string, ":multijump");
@@ -137,6 +173,7 @@ MUTATOR_DEFINITION(mutator_multijump)
 {
 	MUTATOR_HOOK(PlayerPhysics, multijump_PlayerPhysics, CBC_ORDER_ANY);
 	MUTATOR_HOOK(PlayerJump, multijump_PlayerJump, CBC_ORDER_ANY);
+	MUTATOR_HOOK(GetCvars, multijump_GetCvars, CBC_ORDER_ANY);
 	MUTATOR_HOOK(BuildMutatorsString, multijump_BuildMutatorsString, CBC_ORDER_ANY);
 	MUTATOR_HOOK(BuildMutatorsPrettyString, multijump_BuildMutatorsPrettyString, CBC_ORDER_ANY);
 
-- 
2.39.5