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