]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
Merge branch 'div0/mutatorsystem' of git://git.xonotic.org/xonotic/xonotic-data.pk3di...
[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         makevectors(self.angles);
32         // print("physics hook\n");
33         if (g_dodging == 0)
34                 return 0;
35
36         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
37         if (self.dodging_action == 1) {
38                 self.velocity = 
39                           self.velocity 
40                         + ((self.dodging_direction_y * cvar("sv_dodging_horiz_speed")) * v_right) 
41                         + ((self.dodging_direction_x * cvar("sv_dodging_horiz_speed")) * v_forward) 
42                         + (cvar("sv_dodging_up_speed") * v_up);
43
44                 // reset state so next dodge can be done correctly
45                 self.dodging_action = 0;
46                 self.dodging_direction_x = 0;
47                 self.dodging_direction_y = 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         float length;
59         float dodge_detected;
60         //print("dodging_hook\n");
61
62         if (g_dodging == 0)
63                 return 0;
64
65         dodge_detected = 0;
66
67         // common checks
68         if ((time - self.last_dodging_time) < cvar("sv_dodging_delay"))
69                 return 0;
70
71         if (!(self.lastflags & FL_ONGROUND))
72                 return 0;
73
74
75         if (self.movement_x > 0) {
76                 // is this a state change?
77                 if (!(self.pressedkeys & KEY_FORWARD)) {
78                         if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) { 
79                                 dodge_detected = 1;
80                                 self.dodging_action = 1;
81                                 self.dodging_direction_x = 1.0;
82                                 self.last_dodging_time = time;
83                         }
84                         self.last_FORWARD_KEY_time = time;
85                 }
86         }
87
88         if (self.movement_x < 0) {
89                 // is this a state change?
90                 if (!(self.pressedkeys & KEY_BACKWARD)) {
91                         if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout)        { 
92                                 dodge_detected = 1;
93                                 self.dodging_action = 1;
94                                 self.dodging_direction_x = -1.0;
95                                 self.last_dodging_time = time;
96                         }
97                         self.last_BACKWARD_KEY_time = time;
98                 }
99         }
100
101         if (self.movement_y > 0) {
102                 // is this a state change?
103                 if (!(self.pressedkeys & KEY_RIGHT)) {
104                         if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout)   { 
105                                 dodge_detected = 1;
106                                 self.dodging_action = 1;
107                                 self.dodging_direction_y = 1.0;
108                                 self.last_dodging_time = time;
109                         }
110                         self.last_RIGHT_KEY_time = time;
111                 }
112         }
113
114         if (self.movement_y < 0) {
115                 // is this a state change?
116                 if (!(self.pressedkeys & KEY_LEFT)) {
117                         if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout)    { 
118                                 dodge_detected = 1;
119                                 self.dodging_action = 1;
120                                 self.dodging_direction_y = -1.0;
121                                 self.last_dodging_time = time;
122                         }
123                         self.last_LEFT_KEY_time = time;
124                 }
125         }
126
127
128
129         if (dodge_detected == 1) {
130                 // normalize the dodging_direction vector.. 
131                 length = length + self.dodging_direction_x * self.dodging_direction_x;
132                 length = length + self.dodging_direction_y * self.dodging_direction_y;
133                 length = sqrt(length);
134
135                 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
136                 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
137         }
138
139         return 0;
140 }
141
142 MUTATOR_DEFINITION(dodging)
143 {
144         // we need to be called before GetPressedKey does its thing so we can
145         // detect state changes and therefore dodging actions..
146         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
147
148         // in the physics hook we actually implement the dodge..
149         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
150
151         // this just turns on the cvar.
152         MUTATOR_ONADD
153         {
154                 g_dodging = 1;
155                 dodging_Initialize();
156         }
157
158         // this just turns off the cvar.
159         MUTATOR_ONREMOVE
160         {        
161                 g_dodging = 0;
162         }
163
164         return 0;
165 }