Purge client/defs.qh
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / player_skeleton.qc
1 #include "player_skeleton.qh"
2
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"
8
9 .float v_angle_save_x;
10
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;
18
19 void skeleton_loadinfo(entity e)
20 {
21         int i;
22         if(e.skeleton_info_modelindex == e.modelindex && e.skeleton_info_skin == e.skin)
23                 return;
24         e.bone_upperbody = 0;
25         e.bone_weapon = gettagindex(e, "weapon");
26         if(!e.bone_weapon)
27                 e.bone_weapon = gettagindex(e, "tag_weapon");
28         if(!e.bone_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)
32         {
33                 e.(bone_aim[i]) = 0;
34                 e.(bone_aimweight[i]) = 0;
35         }
36         e.fixbone = 0;
37         if(get_model_parameters(e.model, e.skin))
38         {
39                 if(get_model_parameters_bone_upperbody)
40                         e.bone_upperbody = gettagindex(e, get_model_parameters_bone_upperbody);
41                 if(e.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)
47                 {
48                         if(get_model_parameters_bone_aim[i])
49                                 e.(bone_aim[i]) = gettagindex(e, get_model_parameters_bone_aim[i]);
50                         if(e.bone_aim[i])
51                                 e.(bone_aimweight[i]) = get_model_parameters_bone_aimweight[i];
52                 }
53         }
54         else
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;
60         if(e.skeletonindex)
61         {
62                 skel_delete(e.skeletonindex);
63                 e.skeletonindex = 0;
64         }
65 }
66
67 void skeleton_markbones(entity e)
68 {
69         float s = e.skeletonindex;
70         float n = (e.skeleton_numbones = skel_get_numbones(s));
71         int i;
72         for(i = 1; i <= n; ++i)
73         {
74                 float t = BONETYPE_LOWER;
75                 int p = skel_get_boneparent(s, i);
76                 if(p > 0)
77                         t = e.(skeleton_bonetype[p-1]);
78                 if(i == e.bone_upperbody)
79                         t = BONETYPE_UPPER;
80                 e.(skeleton_bonetype[i-1]) = t;
81         }
82 }
83
84 void skel_set_boneabs(float s, int bone, vector absorg)
85 {
86         TC(int, bone);
87         vector absang = fixedvectoangles2(v_forward, v_up);
88
89         vector parentorg = skel_get_boneabs(s, skel_get_boneparent(s, bone));
90         vector parentang = fixedvectoangles2(v_forward, v_up);
91
92         vector relang = AnglesTransform_LeftDivide(parentang, absang);
93         vector relorg = AnglesTransform_Apply(AnglesTransform_Invert(parentang), absorg - parentorg);
94
95         fixedmakevectors(relang);
96         skel_set_bone(s, bone, relorg);
97
98         /*
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);
102         */
103 }
104
105 void free_skeleton_from_frames(entity e)
106 {
107         if(e.skeletonindex)
108         {
109                 skel_delete(e.skeletonindex);
110                 e.skeletonindex = 0;
111         }
112 }
113
114 void skeleton_from_frames(entity e, bool is_dead)
115 {
116         TC(bool, is_dead);
117         float m = e.modelindex;
118         if(!e.skeletonindex)
119         {
120                 e.skeletonindex = skel_create(m);
121                 skeleton_markbones(e);
122         }
123         float s = e.skeletonindex;
124         if(!s)
125                 return;
126         float n = e.skeleton_numbones;
127         float savelerpfrac = e.lerpfrac;
128         float savelerpfrac3 = e.lerpfrac3;
129         float savelerpfrac4 = e.lerpfrac4;
130
131         vector fixbone_oldangles = '0 0 0';
132         if(e.fixbone)
133         {
134                 // make all bones BONETYPE_UPPER
135                 e.lerpfrac = 0;
136                 e.lerpfrac3 = savelerpfrac3 * 2;
137                 e.lerpfrac4 = 0;
138                 // build skeleton
139                 skel_build(s, e, m, 0, 1, n);
140                 // get hip bone
141                 skel_get_boneabs(s, e.bone_upperbody);
142                 fixbone_oldangles = fixedvectoangles2(v_forward, v_up);
143         }
144         int bone;
145         for(bone = 0; bone < n; )
146         {
147                 float firstbone = bone;
148                 float bonetype = e.skeleton_bonetype[bone];
149                 for(++bone; (bone < n) && (e.skeleton_bonetype[bone] == bonetype); ++bone)
150                         ;
151                 if(bonetype == BONETYPE_UPPER)
152                 {
153                         // only show frames 1+3 (upper body)
154                         e.lerpfrac = 0;
155                         e.lerpfrac3 = savelerpfrac3 * 2;
156                         e.lerpfrac4 = 0;
157                 }
158                 else
159                 {
160                         // only show frames 2+4 (lower body)
161                         e.lerpfrac = savelerpfrac * 2;
162                         e.lerpfrac3 = 0;
163                         e.lerpfrac4 = savelerpfrac4 * 2;
164                 }
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);
168         }
169         e.lerpfrac = savelerpfrac;
170         e.lerpfrac3 = savelerpfrac3;
171         e.lerpfrac4 = savelerpfrac4;
172
173         if(e.fixbone)
174         {
175                 // FIX IT
176                 vector org = skel_get_boneabs(s, e.bone_upperbody);
177                 fixedmakevectors(fixbone_oldangles);
178                 skel_set_boneabs(s, e.bone_upperbody, org);
179         }
180
181         if(!is_dead)
182         {
183                 if(e == csqcplayer)
184                 {
185                         if(e.move_movetype == MOVETYPE_NONE)
186                         {
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;
190                         }
191                         else
192                                 e.v_angle_x = input_angles.x;
193                 }
194                 int i;
195                 for(i = 0; i < MAX_AIM_BONES; ++i)
196                 {
197                         if(e.(bone_aim[i]))
198                         {
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);
205                         }
206                 }
207         }
208 }