]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
7134ba900d39712b82aa3adedcd0a6562d41cd2a
[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         float length;
32
33         // print("physics hook\n");
34         if (g_dodging == 0)
35                 return 0;
36
37         length += self.dodging_direction_x * self.dodging_direction_x;
38         length += self.dodging_direction_y * self.dodging_direction_y;
39         length = sqrt(length);
40
41         self.dodging_direction_x *= 1/length;
42         self.dodging_direction_y *= 1/length;
43
44         // ramp up dodging speed by adding some velocity each frame..
45         if (self.dodging_action == 1) {
46                 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);//'100 0 50';
47                 self.dodging_action = 0;
48         }
49
50         // are we done with the dodging ramp yet?
51         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
52                 self.dodging_action = 0;
53
54         return 0;
55 }
56
57 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
58         //print("dodging_hook\n");
59
60         if (g_dodging == 0)
61                 return 0;
62
63         if (self.movement_x > 0) // get if movement keys are pressed
64    {       // forward key pressed
65         if (!(self.pressedkeys & KEY_FORWARD)) {        // is this a state change?
66                         if (
67                                 ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) &&
68                                 (self.lastflags & FL_ONGROUND) &&
69                                 ((time - self.last_dodging_time) > cvar("sv_dodging_delay"))
70                         ) { // are we allowed to dodge?
71                                 self.dodging_action = 1;
72                                 self.dodging_direction_x = self.movement_x;
73                                 self.dodging_direction_y = self.movement_y;
74
75
76                                 self.last_dodging_time = time;
77                         }
78                         self.last_FORWARD_KEY_time = time;
79                 }
80         }
81
82         if (self.movement_x < 0) // get if movement keys are pressed
83    {       // forward key pressed
84         if (!(self.pressedkeys & KEY_BACKWARD)) {        // is this a state change?
85                         if (
86                                 ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) &&
87                                 (self.lastflags & FL_ONGROUND) &&
88                                 ((time - self.last_dodging_time) > cvar("sv_dodging_delay"))
89                         ) { // are we allowed to dodge?
90                                 self.dodging_action = 1;
91                                 // self.velocity = self.velocity + (cvar("sv_dodging_horiz_speed") * v_forward) + (cvar("sv_dodging_up_speed") * v_up);//'100 0 50';
92                                 self.last_dodging_time = time;
93                         }
94                         self.last_BACKWARD_KEY_time = time;
95                 }
96         }
97
98         return 0;
99 }
100
101 MUTATOR_DEFINITION(dodging)
102 {
103         // we need to be called before GetPressedKey does its thing so we can
104         // detect state changes and therefore dodging actions..
105         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
106
107         // in the physics hook we actually implement the dodge..
108         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
109
110         // this just turns on the cvar. TODO: implement :D
111         MUTATOR_ONADD
112         {
113                 g_dodging = 1;
114                 dodging_Initialize();
115         }
116
117         // this just turns off the cvar. TODO: implement :D
118         MUTATOR_ONREMOVE
119         {        
120                 g_dodging = 0;
121         }
122
123         return 0;
124 }