a0d75057adf4aee3013190b45faa33e7d84d2078
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / player_skeleton.qc
1 .float skeleton_info_modelindex;
2 .float skeleton_info_skin;
3 const float BONETYPE_LOWER = 0;
4 const float BONETYPE_UPPER = 1;
5 const float MAX_BONES = 128;
6 .float skeleton_bonetype[MAX_BONES];
7 .float skeleton_numbones;
8
9 void skeleton_loadinfo(entity e)
10 {
11         float i;
12         if(e.skeleton_info_modelindex == e.modelindex && e.skeleton_info_skin == e.skin)
13                 return;
14         e.bone_upperbody = 0;
15         e.bone_weapon = gettagindex(e, "weapon");
16         if(!e.bone_weapon)
17                 e.bone_weapon = gettagindex(e, "tag_weapon");
18         if(!e.bone_weapon)
19                 e.bone_weapon = gettagindex(e, "bip01 r hand");
20         for(i = 0; i < MAX_AIM_BONES; ++i)
21         {
22                 e.(bone_aim[i]) = 0;
23                 e.(bone_aimweight[i]) = 0;
24         }
25         e.fixbone = 0;
26         if(get_model_parameters(e.model, e.skin))
27         {
28                 if(get_model_parameters_bone_upperbody)
29                         e.bone_upperbody = gettagindex(e, get_model_parameters_bone_upperbody);
30                 if(e.bone_upperbody)
31                         e.fixbone = get_model_parameters_fixbone;
32                 if(get_model_parameters_bone_weapon)
33                         e.bone_weapon = gettagindex(e, get_model_parameters_bone_weapon);
34                 for(i = 0; i < MAX_AIM_BONES; ++i)
35                 {
36                         if(get_model_parameters_bone_aim[i])
37                                 e.(bone_aim[i]) = gettagindex(e, get_model_parameters_bone_aim[i]);
38                         if(e.bone_aim[i])
39                                 e.(bone_aimweight[i]) = get_model_parameters_bone_aimweight[i];
40                 }
41         }
42         else
43                 dprint("No model parameters for ", e.model, "\n");
44         dprint(e.model, " uses ", ftos(e.bone_upperbody), " ", ftos(e.fixbone), "\n");
45         get_model_parameters(string_null, 0);
46         e.skeleton_info_modelindex = e.modelindex;
47         e.skeleton_info_skin = e.skin;
48         if(e.skeletonindex)
49         {
50                 skel_delete(e.skeletonindex);
51                 e.skeletonindex = 0;
52         }
53 }
54
55 void skeleton_markbones(entity e)
56 {
57         float s = e.skeletonindex;
58         float n = (e.skeleton_numbones = skel_get_numbones(s));
59         float i;
60         for(i = 1; i <= n; ++i)
61         {
62                 float t = BONETYPE_LOWER;
63                 float p = skel_get_boneparent(s, i);
64                 if(p > 0)
65                         t = e.(skeleton_bonetype[p-1]);
66                 if(i == e.bone_upperbody)
67                         t = BONETYPE_UPPER;
68                 e.(skeleton_bonetype[i-1]) = t;
69         }
70 }
71
72 void skel_set_boneabs(float s, float bone, vector absorg)
73 {
74         vector absang = fixedvectoangles2(v_forward, v_up);
75
76         vector parentorg = skel_get_boneabs(s, skel_get_boneparent(s, bone));
77         vector parentang = fixedvectoangles2(v_forward, v_up);
78
79         vector relang = AnglesTransform_LeftDivide(parentang, absang);
80         vector relorg = AnglesTransform_Apply(AnglesTransform_Invert(parentang), absorg - parentorg);
81
82         fixedmakevectors(relang);
83         skel_set_bone(s, bone, relorg);
84
85         /*
86         vector neworg = skel_get_boneabs(s, bone);
87         printf("ANG: want: %v, got: %v\n", absang, fixedvectoangles2(v_forward, v_up));
88         printf("ORG: want: %v, got: %v\n", absorg, neworg);
89         */
90 }
91
92 void free_skeleton_from_frames(entity e)
93 {
94         if(e.skeletonindex)
95         {
96                 skel_delete(e.skeletonindex);
97                 e.skeletonindex = 0;
98         }
99 }
100
101 void skeleton_from_frames(entity e, float is_dead)
102 {
103         float i;
104         float m = e.modelindex;
105         if(!e.skeletonindex)
106         {
107                 e.skeletonindex = skel_create(m);
108                 skeleton_markbones(e);
109         }
110         float s = e.skeletonindex;
111         if(!s)
112                 return;
113         float bone;
114         float n = e.skeleton_numbones;
115         float savelerpfrac = e.lerpfrac;
116         float savelerpfrac3 = e.lerpfrac3;
117         float savelerpfrac4 = e.lerpfrac4;
118
119         vector fixbone_oldangles = '0 0 0';
120         if(e.fixbone)
121         {
122                 // make all bones BONETYPE_UPPER
123                 e.lerpfrac = 0;
124                 e.lerpfrac3 = savelerpfrac3 * 2;
125                 e.lerpfrac4 = 0;
126                 // build skeleton
127                 skel_build(s, e, m, 0, 1, n);
128                 // get hip bone
129                 skel_get_boneabs(s, e.bone_upperbody);
130                 fixbone_oldangles = fixedvectoangles2(v_forward, v_up);
131         }
132
133         for(bone = 0; bone < n; )
134         {
135                 float firstbone = bone;
136                 float bonetype = e.skeleton_bonetype[bone];
137                 for(++bone; (bone < n) && (e.skeleton_bonetype[bone] == bonetype); ++bone)
138                         ;
139                 if(bonetype == BONETYPE_UPPER)
140                 {
141                         // only show frames 1+3 (upper body)
142                         e.lerpfrac = 0;
143                         e.lerpfrac3 = savelerpfrac3 * 2;
144                         e.lerpfrac4 = 0;
145                 }
146                 else
147                 {
148                         // only show frames 2+4 (lower body)
149                         e.lerpfrac = savelerpfrac * 2;
150                         e.lerpfrac3 = 0;
151                         e.lerpfrac4 = savelerpfrac4 * 2;
152                 }
153                 //printf("Run: bone %d to %d, type %d\n", firstbone + 1, bone, bonetype);
154                 //printf("frame %d %d %d %d lerpfrac * %d %d %d\n", e.frame, e.frame2, e.frame3, e.frame4, e.lerpfrac, e.lerpfrac3, e.lerpfrac4);
155                 skel_build(s, e, m, 0, firstbone + 1, bone);
156         }
157         e.lerpfrac = savelerpfrac;
158         e.lerpfrac3 = savelerpfrac3;
159         e.lerpfrac4 = savelerpfrac4;
160
161         if(e.fixbone)
162         {
163                 // FIX IT
164                 vector org = skel_get_boneabs(s, e.bone_upperbody);
165                 fixedmakevectors(fixbone_oldangles);
166                 skel_set_boneabs(s, e.bone_upperbody, org);
167         }
168
169         if(!is_dead)
170         {
171                 for(i = 0; i < MAX_AIM_BONES; ++i)
172                 {
173                         if(e.(bone_aim[i]))
174                         {
175                                 vector aim = '1 0 0' * bound(-90, self.v_angle.x, 90) * e.(bone_aimweight[i]);
176                                 vector org = skel_get_boneabs(s, e.(bone_aim[i]));
177                                 vector ang_cur = fixedvectoangles2(v_forward, v_up);
178                                 vector ang = AnglesTransform_Multiply(aim, ang_cur);
179                                 fixedmakevectors(ang);
180                                 skel_set_boneabs(s, e.(bone_aim[i]), org);
181                         }
182                 }
183         }
184 }