]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
- meeeh
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_dodging.qc
1
2 // these are used to store the last key press time for each of the keys..
3 .float last_FORWARD_KEY_time;
4 .float last_BACKWARD_KEY_time;
5 .float last_LEFT_KEY_time;
6 .float last_RIGHT_KEY_time;
7
8 // these store the movement direction at the time of the dodge action happening.
9 .float dodging_direction_x;
10 .float dodging_direction_y;
11
12 // this indicates the last time a dodge was executed. used to check if another one is allowed
13 // and to ramp up the dodge acceleration in the physics hook.
14 .float last_dodging_time;
15
16 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
17 .float dodging_action;
18
19 void dodging_Initialize() {
20         self.last_FORWARD_KEY_time = 0;
21         self.last_BACKWARD_KEY_time = 0;
22         self.last_RIGHT_KEY_time = 0;
23         self.last_LEFT_KEY_time = 0;
24         self.last_dodging_time = 0;
25         self.dodging_action = 0;
26         self.dodging_direction_x = 0;
27         self.dodging_direction_y = 0;
28 }
29
30 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
31         // print("physics hook\n");
32         if (g_dodging == 0)
33                 return 0;
34
35         // ramp up dodging speed by adding some velocity each frame..
36         if (self.dodging_action == 1) {
37                 self.velocity = self.velocity + self.dodging_direction_y * (cvar("sv_dodging_horiz_speed") * v_right) + self.dodging_direction_x * (cvar("sv_dodging_horiz_speed") * v_forward) + (cvar("sv_dodging_up_speed") * v_up);
38                 self.dodging_action = 0;
39         }
40
41         // are we done with the dodging ramp yet?
42         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
43                 self.dodging_action = 0;
44
45         return 0;
46 }
47
48 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
49         float length;
50         //print("dodging_hook\n");
51
52         if (g_dodging == 0)
53                 return 0;
54
55         self.dodging_direction_x = 0;
56         self.dodging_direction_y = 0;
57         if (self.movement_x > 0) // get if movement keys are pressed
58    {       // forward key pressed
59         if (!(self.pressedkeys & KEY_FORWARD)) {        // is this a state change?
60                         if (
61                                 ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) &&
62                                 (self.lastflags & FL_ONGROUND) &&
63                                 ((time - self.last_dodging_time) > cvar("sv_dodging_delay"))
64                         ) { // are we allowed to dodge?
65                                 self.dodging_action = 1;
66                                 self.dodging_direction_x = 1;
67                                 self.last_dodging_time = time;
68                         }
69                         self.last_FORWARD_KEY_time = time;
70                 }
71         }
72
73         if (self.movement_x < 0) // get if movement keys are pressed
74    {       // forward key pressed
75         if (!(self.pressedkeys & KEY_BACKWARD)) {        // is this a state change?
76                         if (
77                                 ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) &&
78                                 (self.lastflags & FL_ONGROUND) &&
79                                 ((time - self.last_dodging_time) > cvar("sv_dodging_delay"))
80                         ) { // are we allowed to dodge?
81                                 self.dodging_action = 1;
82                                 self.dodging_direction_x = -1;
83                                 self.last_dodging_time = time;
84                         }
85                         self.last_BACKWARD_KEY_time = time;
86                 }
87         }
88
89         // normalize the dodging_direction vector.. 
90         length += self.dodging_direction_x * self.dodging_direction_x;
91         length += self.dodging_direction_y * self.dodging_direction_y;
92         length = sqrt(length);
93
94         self.dodging_direction_x *= 1/length;
95         self.dodging_direction_y *= 1/length;
96
97         return 0;
98 }
99
100 MUTATOR_DEFINITION(dodging)
101 {
102         // we need to be called before GetPressedKey does its thing so we can
103         // detect state changes and therefore dodging actions..
104         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
105
106         // in the physics hook we actually implement the dodge..
107         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
108
109         // this just turns on the cvar. TODO: implement :D
110         MUTATOR_ONADD
111         {
112                 g_dodging = 1;
113                 dodging_Initialize();
114         }
115
116         // this just turns off the cvar. TODO: implement :D
117         MUTATOR_ONREMOVE
118         {        
119                 g_dodging = 0;
120         }
121
122         return 0;
123 }