Transifex autosync
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / animdecide.qc
1 #include "animdecide.qh"
2
3 #if defined(CSQC)
4         #include <common/monsters/_mod.qh>
5 #elif defined(MENUQC)
6 #elif defined(SVQC)
7         #include <common/monsters/_mod.qh>
8         #include <common/stats.qh>
9         #include <common/util.qh>
10         #include <common/weapons/_all.qh>
11 #endif
12
13 bool monsters_animoverride(entity this)
14 {
15         Monster monster_id = NULL;
16         FOREACH(Monsters, it != MON_Null && it.m_model.model_str() == this.model, {
17         monster_id = it;
18         break;
19         });
20
21         if(!monster_id) { return false; }
22
23         monster_id.mr_anim(monster_id, this);
24
25         vector none = '0 0 0';
26         this.anim_duckwalk = this.anim_walk;
27         this.anim_duckjump = animfixfps(this, '5 1 10', none);
28         this.anim_duckidle = this.anim_idle;
29         this.anim_jump = animfixfps(this, '8 1 10', none);
30         this.anim_taunt = animfixfps(this, '12 1 0.33', none);
31         this.anim_runbackwards = this.anim_run;
32         this.anim_strafeleft = this.anim_run;
33         this.anim_straferight = this.anim_run;
34         this.anim_forwardright = this.anim_run;
35         this.anim_forwardleft = this.anim_run;
36         this.anim_backright = this.anim_run;
37         this.anim_backleft  = this.anim_run;
38         this.anim_duckwalkbackwards = this.anim_walk;
39         this.anim_duckwalkstrafeleft = this.anim_walk;
40         this.anim_duckwalkstraferight = this.anim_walk;
41         this.anim_duckwalkforwardright = this.anim_walk;
42         this.anim_duckwalkforwardleft = this.anim_walk;
43         this.anim_duckwalkbackright = this.anim_walk;
44         this.anim_duckwalkbackleft  = this.anim_walk;
45
46         // these anims ought to stay until stopped explicitly by weaponsystem
47         this.anim_shoot_z = 0.001;
48         this.anim_melee_z = 0.001;
49
50         return true;
51 }
52
53 void animdecide_load_if_needed(entity e)
54 {
55         int mdlidx = e.modelindex;
56         if (mdlidx == e.animdecide_modelindex) return;
57         e.animdecide_modelindex = mdlidx;
58
59         if(substring(e.model, 0, 16) == "models/monsters/")
60         {
61                 if(monsters_animoverride(e))
62                         return;
63         }
64
65 #define ANIM_VEC(a, frames, rate) anim_vec(ANIM_##a, mdlidx, frames, rate)
66
67         vector none = '0 0 0';
68         e.anim_die1 = animfixfps(e, ANIM_VEC(die1, 1, 0.5), none); // 2 seconds
69         e.anim_die2 = animfixfps(e, ANIM_VEC(die2, 1, 0.5), none); // 2 seconds
70         e.anim_draw = animfixfps(e, ANIM_VEC(draw, 1, 3), none);
71         e.anim_duckwalk = animfixfps(e, ANIM_VEC(duckwalk, 1, 1), none);
72         e.anim_duckjump = animfixfps(e, ANIM_VEC(duckjump, 1, 10), none);
73         e.anim_duckidle = animfixfps(e, ANIM_VEC(duckidle, 1, 1), none);
74         e.anim_idle = animfixfps(e, ANIM_VEC(idle, 1, 1), none);
75         e.anim_jump = animfixfps(e, ANIM_VEC(jump, 1, 10), none);
76         e.anim_pain1 = animfixfps(e, ANIM_VEC(pain1, 1, 2), none); // 0.5 seconds
77         e.anim_pain2 = animfixfps(e, ANIM_VEC(pain2, 1, 2), none); // 0.5 seconds
78         e.anim_shoot = animfixfps(e, ANIM_VEC(shoot, 1, 5), none); // analyze models and set framerate
79         e.anim_taunt = animfixfps(e, ANIM_VEC(taunt, 1, 0.33), none);
80         e.anim_run = animfixfps(e, ANIM_VEC(run, 1, 1), none);
81         e.anim_runbackwards = animfixfps(e, ANIM_VEC(runbackwards, 1, 1), none);
82         e.anim_strafeleft = animfixfps(e, ANIM_VEC(strafeleft, 1, 1), none);
83         e.anim_straferight = animfixfps(e, ANIM_VEC(straferight, 1, 1), none);
84         e.anim_forwardright = animfixfps(e, ANIM_VEC(forwardright, 1, 1), ANIM_VEC(straferight, 1, 1));
85         e.anim_forwardleft = animfixfps(e, ANIM_VEC(forwardleft, 1, 1), ANIM_VEC(strafeleft, 1, 1));
86         e.anim_backright = animfixfps(e, ANIM_VEC(backright, 1, 1), ANIM_VEC(straferight, 1, 1));
87         e.anim_backleft  = animfixfps(e, ANIM_VEC(backleft , 1, 1), ANIM_VEC(strafeleft, 1, 1));
88         e.anim_melee = animfixfps(e, ANIM_VEC(melee, 1, 1), ANIM_VEC(shoot, 1, 1));
89         e.anim_duckwalkbackwards = animfixfps(e, ANIM_VEC(duckwalkbackwards, 1, 1), ANIM_VEC(duckwalk, 1, 1));
90         e.anim_duckwalkstrafeleft = animfixfps(e, ANIM_VEC(duckwalkstrafeleft, 1, 1), ANIM_VEC(duckwalk, 1, 1));
91         e.anim_duckwalkstraferight = animfixfps(e, ANIM_VEC(duckwalkstraferight, 1, 1), ANIM_VEC(duckwalk, 1, 1));
92         e.anim_duckwalkforwardright = animfixfps(e, ANIM_VEC(duckwalkforwardright, 1, 1), ANIM_VEC(duckwalk, 1, 1));
93         e.anim_duckwalkforwardleft = animfixfps(e, ANIM_VEC(duckwalkforwardleft, 1, 1), ANIM_VEC(duckwalk, 1, 1));
94         e.anim_duckwalkbackright = animfixfps(e, ANIM_VEC(duckwalkbackright, 1,  1), ANIM_VEC(duckwalk, 1, 1));
95         e.anim_duckwalkbackleft  = animfixfps(e, ANIM_VEC(duckwalkbackleft , 1,  1), ANIM_VEC(duckwalk, 1, 1));
96
97 #undef ANIM_VEC
98
99         // these anims ought to stay until stopped explicitly by weaponsystem
100         e.anim_shoot_z = 0.001;
101         e.anim_melee_z = 0.001;
102 }
103
104 const float ANIMPRIO_IDLE = 0;
105 const float ANIMPRIO_ACTIVE = 1;
106 const float ANIMPRIO_CROUCH = 2;
107 const float ANIMPRIO_DEAD = 3;
108
109 vector animdecide_getupperanim(entity e)
110 {
111         // death etc.
112         if(e.anim_state & ANIMSTATE_FROZEN)
113                 return vec3(e.anim_idle.x, e.anim_time, ANIMPRIO_DEAD);
114         if(e.anim_state & ANIMSTATE_DEAD1)
115                 return vec3(e.anim_die1_x, e.anim_time, ANIMPRIO_DEAD);
116         if(e.anim_state & ANIMSTATE_DEAD2)
117                 return vec3(e.anim_die2_x, e.anim_time, ANIMPRIO_DEAD);
118
119         // is there an action?
120         vector outframe = '-1 0 0';
121         float t, a;
122         if(e.anim_upper_time >= e.anim_upper_implicit_time)
123         {
124                 a = e.anim_upper_action;
125                 t = e.anim_upper_time;
126         }
127         else
128         {
129                 a = e.anim_upper_implicit_action;
130                 t = e.anim_upper_implicit_time;
131         }
132         switch(a)
133         {
134                 case ANIMACTION_DRAW: outframe = e.anim_draw; break;
135                 case ANIMACTION_PAIN1: outframe = e.anim_pain1; break;
136                 case ANIMACTION_PAIN2: outframe = e.anim_pain2; break;
137                 case ANIMACTION_SHOOT: outframe = e.anim_shoot; break;
138                 case ANIMACTION_TAUNT: outframe = e.anim_taunt; break;
139                 case ANIMACTION_MELEE: outframe = e.anim_melee; break;
140         }
141         if(outframe.x >= 0)
142         {
143                 if(time <= t + outframe.y / outframe.z)
144                 {
145                         // animation is running!
146                         return vec3(outframe.x, t, ANIMPRIO_ACTIVE);
147                 }
148         }
149         // or, decide the anim by state
150         t = max(e.anim_time, e.anim_implicit_time);
151         // but all states are for lower body!
152         return vec3(e.anim_idle.x, t, ANIMPRIO_IDLE);
153 }
154
155 vector animdecide_getloweranim(entity e)
156 {
157         // death etc.
158         if(e.anim_state & ANIMSTATE_FOLLOW)
159                 return vec3(((e.anim_state & ANIMSTATE_DUCK) ? e.anim_duckidle_x : e.anim_idle_x), e.anim_time, ANIMPRIO_DEAD); // dead priority so it's above all
160         if(e.anim_state & ANIMSTATE_FROZEN)
161                 return vec3(e.anim_idle.x, e.anim_time, ANIMPRIO_DEAD);
162         if(e.anim_state & ANIMSTATE_DEAD1)
163                 return vec3(e.anim_die1_x, e.anim_time, ANIMPRIO_DEAD);
164         if(e.anim_state & ANIMSTATE_DEAD2)
165                 return vec3(e.anim_die2_x, e.anim_time, ANIMPRIO_DEAD);
166
167         // is there an action?
168         vector outframe = '-1 0 0';
169         float t, a;
170         if(e.anim_lower_time >= e.anim_lower_implicit_time)
171         {
172                 a = e.anim_lower_action;
173                 t = e.anim_lower_time;
174         }
175         else
176         {
177                 a = e.anim_lower_implicit_action;
178                 t = e.anim_lower_implicit_time;
179         }
180         switch(a)
181         {
182                 case ANIMACTION_JUMP: if(e.anim_implicit_state & ANIMIMPLICITSTATE_INAIR) { if(e.anim_state & ANIMSTATE_DUCK) outframe = e.anim_duckjump; else outframe = e.anim_jump; } break;
183         }
184         if(outframe.x >= 0)
185         {
186                 if(time <= t + outframe.y / outframe.z)
187                 {
188                         // animation is running!
189                         return vec3(outframe.x, t, ANIMPRIO_ACTIVE);
190                 }
191         }
192         // or, decide the anim by state
193         t = max(e.anim_time, e.anim_implicit_time);
194         if(e.anim_state & ANIMSTATE_DUCK)
195         {
196                 if(e.anim_implicit_state & ANIMIMPLICITSTATE_INAIR)
197                         return vec3(e.anim_duckjump.x, 0, ANIMPRIO_CROUCH); // play the END of the jump anim
198                 else switch(e.anim_implicit_state & (ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_LEFT | ANIMIMPLICITSTATE_RIGHT))
199                 {
200                         case ANIMIMPLICITSTATE_FORWARD:
201                                 return vec3(e.anim_duckwalk.x, t, ANIMPRIO_CROUCH);
202                         case ANIMIMPLICITSTATE_BACKWARDS:
203                                 return vec3(e.anim_duckwalkbackwards.x, t, ANIMPRIO_CROUCH);
204                         case ANIMIMPLICITSTATE_RIGHT:
205                                 return vec3(e.anim_duckwalkstraferight.x, t, ANIMPRIO_CROUCH);
206                         case ANIMIMPLICITSTATE_LEFT:
207                                 return vec3(e.anim_duckwalkstrafeleft.x, t, ANIMPRIO_CROUCH);
208                         case ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_RIGHT:
209                                 return vec3(e.anim_duckwalkforwardright.x, t, ANIMPRIO_CROUCH);
210                         case ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_LEFT:
211                                 return vec3(e.anim_duckwalkforwardleft.x, t, ANIMPRIO_CROUCH);
212                         case ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_RIGHT:
213                                 return vec3(e.anim_duckwalkbackright.x, t, ANIMPRIO_CROUCH);
214                         case ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_LEFT:
215                                 return vec3(e.anim_duckwalkbackleft.x, t, ANIMPRIO_CROUCH);
216                 }
217                 return vec3(e.anim_duckidle.x, t, ANIMPRIO_CROUCH);
218         }
219         else
220         {
221                 if(e.anim_implicit_state & ANIMIMPLICITSTATE_INAIR)
222                         return vec3(e.anim_jump.x, 0, ANIMPRIO_ACTIVE); // play the END of the jump anim
223                 else switch(e.anim_implicit_state & (ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_LEFT | ANIMIMPLICITSTATE_RIGHT))
224                 {
225                         case ANIMIMPLICITSTATE_FORWARD:
226                                 return vec3(e.anim_run.x, t, ANIMPRIO_ACTIVE);
227                         case ANIMIMPLICITSTATE_BACKWARDS:
228                                 return vec3(e.anim_runbackwards.x, t, ANIMPRIO_ACTIVE);
229                         case ANIMIMPLICITSTATE_RIGHT:
230                                 return vec3(e.anim_straferight.x, t, ANIMPRIO_ACTIVE);
231                         case ANIMIMPLICITSTATE_LEFT:
232                                 return vec3(e.anim_strafeleft.x, t, ANIMPRIO_ACTIVE);
233                         case ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_RIGHT:
234                                 return vec3(e.anim_forwardright.x, t, ANIMPRIO_ACTIVE);
235                         case ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_LEFT:
236                                 return vec3(e.anim_forwardleft.x, t, ANIMPRIO_ACTIVE);
237                         case ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_RIGHT:
238                                 return vec3(e.anim_backright.x, t, ANIMPRIO_ACTIVE);
239                         case ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_LEFT:
240                                 return vec3(e.anim_backleft.x, t, ANIMPRIO_ACTIVE);
241                 }
242                 return vec3(e.anim_idle.x, t, ANIMPRIO_IDLE);
243         }
244         // can't get here
245         //return vec3(e.anim_idle.x, t, ANIMPRIO_IDLE);
246 }
247
248 void animdecide_setimplicitstate(entity e, float onground)
249 {
250         int s = 0;
251
252         makevectors(e.angles);
253         vector v;
254         v.x = e.velocity * v_forward;
255         v.y = e.velocity * v_right;
256         v.z = 0;
257
258         // we want to match like this:
259         // the 8 directions shall be "evenly spaced"
260         // that means, the forward key includes anything from -67.5 to +67.5 degrees
261         // which then means x > |y| * cot(3pi/8)
262         //
263         // BUT, the engine's clip-movement-to-keyboard function uses 0.5 here,
264         // which would be an angle range from -63.43 to +63.43 degrees, making
265         // it slightly less likely to "hit two keys at once", so let's do this
266         // here too
267
268         if(vdist(v, >, 10))
269         {
270                 if(v.x >  fabs(v.y) * 0.5)
271                         s |= ANIMIMPLICITSTATE_FORWARD;
272                 if(v.x < -fabs(v.y) * 0.5)
273                         s |= ANIMIMPLICITSTATE_BACKWARDS;
274                 if(v.y >  fabs(v.x) * 0.5)
275                         s |= ANIMIMPLICITSTATE_RIGHT;
276                 if(v.y < -fabs(v.x) * 0.5)
277                         s |= ANIMIMPLICITSTATE_LEFT;
278         }
279         if(!onground)
280                 s |= ANIMIMPLICITSTATE_INAIR;
281
282         // detect some kinds of otherwise misdetected jumps (ground to air transition)
283         // NOTE: currently, in CSQC this is the only jump detection, as the explicit jump action is never called!
284         if(!(e.anim_implicit_state & ANIMIMPLICITSTATE_INAIR) && (s & ANIMIMPLICITSTATE_INAIR))
285         {
286                 e.anim_lower_implicit_action = ANIMACTION_JUMP;
287                 e.anim_lower_implicit_time = time;
288         }
289
290         if(s != e.anim_implicit_state)
291         {
292                 e.anim_implicit_state = s;
293                 e.anim_implicit_time = time;
294         }
295 }
296 void animdecide_setframes(entity e, float support_blending, .float fld_frame, .float fld_frame1time, .float fld_frame2, .float fld_frame2time)
297 {
298         // _x: frame
299         // _y: start time
300         // _z: priority
301         vector upper = animdecide_getupperanim(e);
302         vector lower = animdecide_getloweranim(e);
303         //print("UPPER: ", vtos(upper), ", LOWER: ", vtos(lower), "\n");
304         if(support_blending)
305         {
306                 if(upper.z && !lower.z)
307                         lower = upper;
308                 else if(lower.z && !upper.z)
309                         upper = lower;
310                 if(e.frame1time != upper.y || e.frame2time != lower.y)
311                         BITXOR_ASSIGN(e.effects, EF_RESTARTANIM_BIT);
312                 e.(fld_frame) = upper.x;
313                 e.(fld_frame1time) = upper.y;
314                 e.(fld_frame2) = lower.x;
315                 e.(fld_frame2time) = lower.y;
316         }
317         else
318         {
319                 if(upper.z > lower.z)
320                         lower = upper;
321                 else if(lower.z > upper.z)
322                         upper = lower;
323                 if(e.frame1time != upper.y)
324                         BITXOR_ASSIGN(e.effects, EF_RESTARTANIM_BIT);
325                 e.(fld_frame) = upper.x;
326                 e.(fld_frame1time) = upper.y;
327         }
328 }
329
330 void animdecide_setstate(entity e, int newstate, float restart)
331 {
332         if(!restart)
333                 if(newstate == e.anim_state)
334                         return;
335         e.anim_state = newstate;
336         e.anim_time = time;
337 }
338 void animdecide_setaction(entity e, float action, float restart)
339 {
340         if(action < 0)
341         {
342                 if(!restart)
343                         if(action == e.anim_lower_action)
344                                 return;
345                 e.anim_lower_action = action;
346                 e.anim_lower_time = time;
347         }
348         else
349         {
350                 if(!restart)
351                         if(action == e.anim_upper_action)
352                                 return;
353                 e.anim_upper_action = action;
354                 e.anim_upper_time = time;
355         }
356 }