]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/animdecide.qc
Merge branch 'master' into Mirio/balance
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / animdecide.qc
1 #include "animdecide.qh"
2
3 #include <common/monsters/all.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, LAMBDA(
14                 if(it.model == this.model)
15                 {
16                         monster_id = it;
17                         break;
18                 }
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                         default:
217                                 return vec3(e.anim_duckidle.x, t, ANIMPRIO_CROUCH);
218                 }
219         }
220         else
221         {
222                 if(e.anim_implicit_state & ANIMIMPLICITSTATE_INAIR)
223                         return vec3(e.anim_jump.x, 0, ANIMPRIO_ACTIVE); // play the END of the jump anim
224                 else switch(e.anim_implicit_state & (ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_LEFT | ANIMIMPLICITSTATE_RIGHT))
225                 {
226                         case ANIMIMPLICITSTATE_FORWARD:
227                                 return vec3(e.anim_run.x, t, ANIMPRIO_ACTIVE);
228                         case ANIMIMPLICITSTATE_BACKWARDS:
229                                 return vec3(e.anim_runbackwards.x, t, ANIMPRIO_ACTIVE);
230                         case ANIMIMPLICITSTATE_RIGHT:
231                                 return vec3(e.anim_straferight.x, t, ANIMPRIO_ACTIVE);
232                         case ANIMIMPLICITSTATE_LEFT:
233                                 return vec3(e.anim_strafeleft.x, t, ANIMPRIO_ACTIVE);
234                         case ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_RIGHT:
235                                 return vec3(e.anim_forwardright.x, t, ANIMPRIO_ACTIVE);
236                         case ANIMIMPLICITSTATE_FORWARD | ANIMIMPLICITSTATE_LEFT:
237                                 return vec3(e.anim_forwardleft.x, t, ANIMPRIO_ACTIVE);
238                         case ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_RIGHT:
239                                 return vec3(e.anim_backright.x, t, ANIMPRIO_ACTIVE);
240                         case ANIMIMPLICITSTATE_BACKWARDS | ANIMIMPLICITSTATE_LEFT:
241                                 return vec3(e.anim_backleft.x, t, ANIMPRIO_ACTIVE);
242                         default:
243                                 return vec3(e.anim_idle.x, t, ANIMPRIO_IDLE);
244                 }
245         }
246         // can't get here
247         return vec3(e.anim_idle.x, t, ANIMPRIO_IDLE);
248 }
249
250 void animdecide_setimplicitstate(entity e, float onground)
251 {
252         int s = 0;
253
254         makevectors(e.angles);
255         vector v;
256         v.x = e.velocity * v_forward;
257         v.y = e.velocity * v_right;
258         v.z = 0;
259
260         // we want to match like this:
261         // the 8 directions shall be "evenly spaced"
262         // that means, the forward key includes anything from -67.5 to +67.5 degrees
263         // which then means x > |y| * cot(3pi/8)
264         //
265         // BUT, the engine's clip-movement-to-keyboard function uses 0.5 here,
266         // which would be an angle range from -63.43 to +63.43 degrees, making
267         // it slightly less likely to "hit two keys at once", so let's do this
268         // here too
269
270         if(vdist(v, >, 10))
271         {
272                 if(v.x >  fabs(v.y) * 0.5)
273                         s |= ANIMIMPLICITSTATE_FORWARD;
274                 if(v.x < -fabs(v.y) * 0.5)
275                         s |= ANIMIMPLICITSTATE_BACKWARDS;
276                 if(v.y >  fabs(v.x) * 0.5)
277                         s |= ANIMIMPLICITSTATE_RIGHT;
278                 if(v.y < -fabs(v.x) * 0.5)
279                         s |= ANIMIMPLICITSTATE_LEFT;
280         }
281         if(!onground)
282                 s |= ANIMIMPLICITSTATE_INAIR;
283
284         // detect some kinds of otherwise misdetected jumps (ground to air transition)
285         // NOTE: currently, in CSQC this is the only jump detection, as the explicit jump action is never called!
286         if(!(e.anim_implicit_state & ANIMIMPLICITSTATE_INAIR) && (s & ANIMIMPLICITSTATE_INAIR))
287         {
288                 e.anim_lower_implicit_action = ANIMACTION_JUMP;
289                 e.anim_lower_implicit_time = time;
290         }
291
292         if(s != e.anim_implicit_state)
293         {
294                 e.anim_implicit_state = s;
295                 e.anim_implicit_time = time;
296         }
297 }
298 void animdecide_setframes(entity e, float support_blending, .float fld_frame, .float fld_frame1time, .float fld_frame2, .float fld_frame2time)
299 {
300         // _x: frame
301         // _y: start time
302         // _z: priority
303         vector upper = animdecide_getupperanim(e);
304         vector lower = animdecide_getloweranim(e);
305         //print("UPPER: ", vtos(upper), ", LOWER: ", vtos(lower), "\n");
306         if(support_blending)
307         {
308                 if(upper.z && !lower.z)
309                         lower = upper;
310                 else if(lower.z && !upper.z)
311                         upper = lower;
312                 if(e.frame1time != upper.y || e.frame2time != lower.y)
313                         BITXOR_ASSIGN(e.effects, EF_RESTARTANIM_BIT);
314                 e.(fld_frame) = upper.x;
315                 e.(fld_frame1time) = upper.y;
316                 e.(fld_frame2) = lower.x;
317                 e.(fld_frame2time) = lower.y;
318         }
319         else
320         {
321                 if(upper.z > lower.z)
322                         lower = upper;
323                 else if(lower.z > upper.z)
324                         upper = lower;
325                 if(e.frame1time != upper.y)
326                         BITXOR_ASSIGN(e.effects, EF_RESTARTANIM_BIT);
327                 e.(fld_frame) = upper.x;
328                 e.(fld_frame1time) = upper.y;
329         }
330 }
331
332 void animdecide_setstate(entity e, int newstate, float restart)
333 {
334         if(!restart)
335                 if(newstate == e.anim_state)
336                         return;
337         e.anim_state = newstate;
338         e.anim_time = time;
339 }
340 void animdecide_setaction(entity e, float action, float restart)
341 {
342         if(action < 0)
343         {
344                 if(!restart)
345                         if(action == e.anim_lower_action)
346                                 return;
347                 e.anim_lower_action = action;
348                 e.anim_lower_time = time;
349         }
350         else
351         {
352                 if(!restart)
353                         if(action == e.anim_upper_action)
354                                 return;
355                 e.anim_upper_action = action;
356                 e.anim_upper_time = time;
357         }
358 }