1 #include "player_skeleton.qh"
3 #include <common/physics/movetypes/movetypes.qh>
4 #include <common/physics/player.qh>
5 #include <client/mutators/_mod.qh>
6 #include "../lib/csqcmodel/cl_player.qh"
7 #include "../lib/warpzone/anglestransform.qh"
11 classfield(Skeleton) .float skeleton_info_modelindex;
12 classfield(Skeleton) .float skeleton_info_skin;
13 const int BONETYPE_LOWER = 0;
14 const int BONETYPE_UPPER = 1;
15 const int MAX_BONES = 128;
16 classfield(Skeleton) .float skeleton_bonetype[MAX_BONES];
17 classfield(Skeleton) .float skeleton_numbones;
19 void skeleton_loadinfo(entity e)
22 if(e.skeleton_info_modelindex == e.modelindex && e.skeleton_info_skin == e.skin)
25 e.bone_weapon = gettagindex(e, "weapon");
27 e.bone_weapon = gettagindex(e, "tag_weapon");
29 e.bone_weapon = gettagindex(e, "bip01 r hand");
30 MUTATOR_CALLHOOK(Skeleton_CheckBones, e);
31 for(i = 0; i < MAX_AIM_BONES; ++i)
34 e.(bone_aimweight[i]) = 0;
37 if(get_model_parameters(e.model, e.skin))
39 if(get_model_parameters_bone_upperbody)
40 e.bone_upperbody = gettagindex(e, get_model_parameters_bone_upperbody);
42 e.fixbone = get_model_parameters_fixbone;
43 if(get_model_parameters_bone_weapon)
44 e.bone_weapon = gettagindex(e, get_model_parameters_bone_weapon);
45 MUTATOR_CALLHOOK(Skeleton_CheckModel, e);
46 for(i = 0; i < MAX_AIM_BONES; ++i)
48 if(get_model_parameters_bone_aim[i])
49 e.(bone_aim[i]) = gettagindex(e, get_model_parameters_bone_aim[i]);
51 e.(bone_aimweight[i]) = get_model_parameters_bone_aimweight[i];
55 LOG_TRACE("No model parameters for ", e.model);
56 //dprint(e.model, " uses ", ftos(e.bone_upperbody), " ", ftos(e.fixbone), "\n");
57 get_model_parameters(string_null, 0);
58 e.skeleton_info_modelindex = e.modelindex;
59 e.skeleton_info_skin = e.skin;
62 skel_delete(e.skeletonindex);
67 void skeleton_markbones(entity e)
69 float s = e.skeletonindex;
70 float n = (e.skeleton_numbones = skel_get_numbones(s));
72 for(i = 1; i <= n; ++i)
74 float t = BONETYPE_LOWER;
75 int p = skel_get_boneparent(s, i);
77 t = e.(skeleton_bonetype[p-1]);
78 if(i == e.bone_upperbody)
80 e.(skeleton_bonetype[i-1]) = t;
84 void skel_set_boneabs(float s, int bone, vector absorg)
87 vector absang = fixedvectoangles2(v_forward, v_up);
89 vector parentorg = skel_get_boneabs(s, skel_get_boneparent(s, bone));
90 vector parentang = fixedvectoangles2(v_forward, v_up);
92 vector relang = AnglesTransform_LeftDivide(parentang, absang);
93 vector relorg = AnglesTransform_Apply(AnglesTransform_Invert(parentang), absorg - parentorg);
95 fixedmakevectors(relang);
96 skel_set_bone(s, bone, relorg);
99 vector neworg = skel_get_boneabs(s, bone);
100 printf("ANG: want: %v, got: %v\n", absang, fixedvectoangles2(v_forward, v_up));
101 printf("ORG: want: %v, got: %v\n", absorg, neworg);
105 void free_skeleton_from_frames(entity e)
109 skel_delete(e.skeletonindex);
114 void skeleton_from_frames(entity e, bool is_dead)
117 float m = e.modelindex;
120 e.skeletonindex = skel_create(m);
121 skeleton_markbones(e);
123 float s = e.skeletonindex;
126 float n = e.skeleton_numbones;
127 float savelerpfrac = e.lerpfrac;
128 float savelerpfrac3 = e.lerpfrac3;
129 float savelerpfrac4 = e.lerpfrac4;
131 vector fixbone_oldangles = '0 0 0';
134 // make all bones BONETYPE_UPPER
136 e.lerpfrac3 = savelerpfrac3 * 2;
139 skel_build(s, e, m, 0, 1, n);
141 skel_get_boneabs(s, e.bone_upperbody);
142 fixbone_oldangles = fixedvectoangles2(v_forward, v_up);
145 for(bone = 0; bone < n; )
147 float firstbone = bone;
148 float bonetype = e.skeleton_bonetype[bone];
149 for(++bone; (bone < n) && (e.skeleton_bonetype[bone] == bonetype); ++bone)
151 if(bonetype == BONETYPE_UPPER)
153 // only show frames 1+3 (upper body)
155 e.lerpfrac3 = savelerpfrac3 * 2;
160 // only show frames 2+4 (lower body)
161 e.lerpfrac = savelerpfrac * 2;
163 e.lerpfrac4 = savelerpfrac4 * 2;
165 //printf("Run: bone %d to %d, type %d\n", firstbone + 1, bone, bonetype);
166 //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);
167 skel_build(s, e, m, 0, firstbone + 1, bone);
169 e.lerpfrac = savelerpfrac;
170 e.lerpfrac3 = savelerpfrac3;
171 e.lerpfrac4 = savelerpfrac4;
176 vector org = skel_get_boneabs(s, e.bone_upperbody);
177 fixedmakevectors(fixbone_oldangles);
178 skel_set_boneabs(s, e.bone_upperbody, org);
185 if(e.move_movetype == MOVETYPE_NONE)
187 if(!e.v_angle_save_x)
188 e.v_angle_save_x = input_angles.x;
189 e.v_angle_x = e.v_angle_save_x;
192 e.v_angle_x = input_angles.x;
195 for(i = 0; i < MAX_AIM_BONES; ++i)
199 vector aim = '1 0 0' * bound(-90, e.v_angle_x, 90) * e.(bone_aimweight[i]);
200 vector org = skel_get_boneabs(s, e.(bone_aim[i]));
201 vector ang_cur = fixedvectoangles2(v_forward, v_up);
202 vector ang = AnglesTransform_Multiply(aim, ang_cur);
203 fixedmakevectors(ang);
204 skel_set_boneabs(s, e.(bone_aim[i]), org);