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