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