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