Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_multijump.qc
1 #ifdef SVQC
2         #include "../_all.qh"
3         #include "mutator.qh"
4         #include "../antilag.qh"
5 #endif
6
7 .int multijump_count;
8 .bool multijump_ready;
9 .bool cvar_cl_multijump;
10
11 #ifdef CSQC
12
13 #define PHYS_MULTIJUMP                          getstati(STAT_MULTIJUMP)
14 #define PHYS_MULTIJUMP_SPEED            getstatf(STAT_MULTIJUMP_SPEED)
15 #define PHYS_MULTIJUMP_ADD                      getstati(STAT_MULTIJUMP_ADD)
16 #define PHYS_MULTIJUMP_MAXSPEED         getstatf(STAT_MULTIJUMP_MAXSPEED)
17 #define PHYS_MULTIJUMP_DODGING          getstati(STAT_MULTIJUMP_DODGING)
18
19 #elif defined(SVQC)
20
21 #define PHYS_MULTIJUMP                          autocvar_g_multijump
22 #define PHYS_MULTIJUMP_SPEED            autocvar_g_multijump_speed
23 #define PHYS_MULTIJUMP_ADD                      autocvar_g_multijump_add
24 #define PHYS_MULTIJUMP_MAXSPEED         autocvar_g_multijump_maxspeed
25 #define PHYS_MULTIJUMP_DODGING          autocvar_g_multijump_dodging
26
27
28 .float stat_multijump;
29 .float stat_multijump_speed;
30 .float stat_multijump_add;
31 .float stat_multijump_maxspeed;
32 .float stat_multijump_dodging;
33
34 void multijump_UpdateStats()
35 {
36         self.stat_multijump = PHYS_MULTIJUMP;
37         self.stat_multijump_speed = PHYS_MULTIJUMP_SPEED;
38         self.stat_multijump_add = PHYS_MULTIJUMP_ADD;
39         self.stat_multijump_maxspeed = PHYS_MULTIJUMP_MAXSPEED;
40         self.stat_multijump_dodging = PHYS_MULTIJUMP_DODGING;
41 }
42
43 void multijump_AddStats()
44 {
45         addstat(STAT_MULTIJUMP, AS_INT, stat_multijump);
46         addstat(STAT_MULTIJUMP_SPEED, AS_FLOAT, stat_multijump_speed);
47         addstat(STAT_MULTIJUMP_ADD, AS_INT, stat_multijump_add);
48         addstat(STAT_MULTIJUMP_MAXSPEED, AS_FLOAT, stat_multijump_maxspeed);
49         addstat(STAT_MULTIJUMP_DODGING, AS_INT, stat_multijump_dodging);
50 }
51
52 #endif
53
54 void PM_multijump()
55 {
56         if(!PHYS_MULTIJUMP) { return; }
57
58         if(IS_ONGROUND(self))
59         {
60                 self.multijump_count = 0;
61         }
62 }
63
64 bool PM_multijump_checkjump()
65 {
66         if(!PHYS_MULTIJUMP) { return false; }
67
68 #ifdef SVQC
69         bool client_multijump = self.cvar_cl_multijump;
70 #elif defined(CSQC)
71         bool client_multijump = cvar("cl_multijump");
72
73         if(cvar("cl_multijump") > 1)
74                 return false; // nope
75 #endif
76
77         if (!IS_JUMP_HELD(self) && !IS_ONGROUND(self) && client_multijump) // jump button pressed this frame and we are in midair
78                 self.multijump_ready = true;  // this is necessary to check that we released the jump button and pressed it again
79         else
80                 self.multijump_ready = false;
81
82         int phys_multijump = PHYS_MULTIJUMP;
83
84 #ifdef CSQC
85         phys_multijump = (PHYS_MULTIJUMP) ? -1 : 0;
86 #endif
87
88         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))
89         {
90                 if (PHYS_MULTIJUMP)
91                 {
92                         if (!PHYS_MULTIJUMP_ADD) // in this case we make the z velocity == jumpvelocity
93                         {
94                                 if (self.velocity_z < PHYS_JUMPVELOCITY)
95                                 {
96                                         player_multijump = true;
97                                         self.velocity_z = 0;
98                                 }
99                         }
100                         else
101                                 player_multijump = true;
102
103                         if(player_multijump)
104                         {
105                                 if(PHYS_MULTIJUMP_DODGING)
106                                 if(self.movement_x != 0 || self.movement_y != 0) // don't remove all speed if player isnt pressing any movement keys
107                                 {
108                                         float curspeed;
109                                         vector wishvel, wishdir;
110
111 /*#ifdef SVQC
112                                         curspeed = max(
113                                                 vlen(vec2(self.velocity)), // current xy speed
114                                                 vlen(vec2(antilag_takebackavgvelocity(self, max(self.lastteleporttime + sys_frametime, time - 0.25), time))) // average xy topspeed over the last 0.25 secs
115                                         );
116 #elif defined(CSQC)*/
117                                         curspeed = vlen(vec2(self.velocity));
118 //#endif
119
120                                         makevectors(self.v_angle_y * '0 1 0');
121                                         wishvel = v_forward * self.movement_x + v_right * self.movement_y;
122                                         wishdir = normalize(wishvel);
123
124                                         self.velocity_x = wishdir_x * curspeed; // allow "dodging" at a multijump
125                                         self.velocity_y = wishdir_y * curspeed;
126                                         // keep velocity_z unchanged!
127                                 }
128                                 if (PHYS_MULTIJUMP > 0)
129                                 {
130                                         self.multijump_count += 1;
131                                 }
132                         }
133                 }
134                 self.multijump_ready = false; // require releasing and pressing the jump button again for the next jump
135         }
136
137         return false;
138 }
139
140 #ifdef SVQC
141 MUTATOR_HOOKFUNCTION(multijump_PlayerPhysics)
142 {
143         multijump_UpdateStats();
144         PM_multijump();
145
146         return false;
147 }
148
149 MUTATOR_HOOKFUNCTION(multijump_PlayerJump)
150 {
151         return PM_multijump_checkjump();
152 }
153
154 MUTATOR_HOOKFUNCTION(multijump_GetCvars)
155 {
156         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_multijump, "cl_multijump");
157         return false;
158 }
159
160 MUTATOR_HOOKFUNCTION(multijump_BuildMutatorsString)
161 {
162         ret_string = strcat(ret_string, ":multijump");
163         return false;
164 }
165
166 MUTATOR_HOOKFUNCTION(multijump_BuildMutatorsPrettyString)
167 {
168         ret_string = strcat(ret_string, ", Multi jump");
169         return false;
170 }
171
172 MUTATOR_DEFINITION(mutator_multijump)
173 {
174         MUTATOR_HOOK(PlayerPhysics, multijump_PlayerPhysics, CBC_ORDER_ANY);
175         MUTATOR_HOOK(PlayerJump, multijump_PlayerJump, CBC_ORDER_ANY);
176         MUTATOR_HOOK(GetCvars, multijump_GetCvars, CBC_ORDER_ANY);
177         MUTATOR_HOOK(BuildMutatorsString, multijump_BuildMutatorsString, CBC_ORDER_ANY);
178         MUTATOR_HOOK(BuildMutatorsPrettyString, multijump_BuildMutatorsPrettyString, CBC_ORDER_ANY);
179
180         MUTATOR_ONADD
181         {
182                 multijump_AddStats();
183         }
184
185         return false;
186 }
187 #endif