]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Skeleton.cpp
aac06eee16c9f91c7ab66f898582f9340f68c4b8
[lugaru.git] / Source / Animation / Skeleton.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /**> HEADER FILES <**/
22 #include "Game.h"
23 #include "Animation/Skeleton.h"
24 #include "openal_wrapper.h"
25 #include "Animation/Animation.h"
26 #include "Utils/Folders.h"
27
28 extern float multiplier;
29 extern float gravity;
30 extern Terrain terrain;
31 extern Objects objects;
32 extern int environment;
33 extern float camerashake;
34 extern bool freeze;
35 extern int detail;
36 extern int tutoriallevel;
37
38 extern int whichjointstartarray[26];
39 extern int whichjointendarray[26];
40
41 extern bool visibleloading;
42
43 /* EFFECT
44  */
45 void dealloc2(void* param)
46 {
47     free(param);
48 }
49
50 enum {boneconnect, constraint, muscle};
51
52
53 /* EFFECT
54  * sets strength, length,
55  *      parent1->position, parent2->position,
56  *      parent1->velocity, parent2->velocity
57  * used for ragdolls?
58  *
59  * USES:
60  * Skeleton::DoConstraints
61  */
62 void Muscle::DoConstraint(bool spinny)
63 {
64     // FIXME: relaxlength shouldn't be static, but may not always be set
65     // so I don't want to change the existing behavior even though it's probably a bug
66     static float relaxlength;
67
68     float oldlength = length;
69
70     if (type != boneconnect)
71         relaxlength = findDistance(&parent1->position, &parent2->position);
72
73     if (type == boneconnect)
74         strength = 1;
75     if (type == constraint)
76         strength = 0;
77
78     // clamp strength
79     if (strength < 0)
80         strength = 0;
81     if (strength > 1)
82         strength = 1;
83
84     length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
85     length -= (length - targetlength) * (strength) * multiplier * 10000;
86     if (strength == 0)
87         length = relaxlength;
88
89     if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
90         length = relaxlength;
91
92     // clamp length
93     if (length < minlength)
94         length = minlength;
95     if (length > maxlength)
96         length = maxlength;
97
98     if (length == relaxlength)
99         return;
100
101     // relax muscle?
102
103     //Find midpoint
104     XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
105
106     //Find vector from midpoint to second vector
107     XYZ vel = parent2->position - midp;
108
109     //Change to unit vector
110     Normalise(&vel);
111
112     //Apply velocity change
113     XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
114     XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
115     if (!freeze && spinny) {
116         parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
117         parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
118     } else {
119         parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
120         parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
121     }
122
123     //Move child point to within certain distance of parent point
124     parent1->position = newpoint1;
125     parent2->position = newpoint2;
126 }
127
128 /* EFFECT
129  * sets forward, lowforward, specialforward[]
130  *
131  * USES:
132  * Skeleton::Load
133  * Person/Person::DoAnimations
134  * Person/Person::DrawSkeleton
135  */
136 void Skeleton::FindForwards()
137 {
138     //Find forward vectors
139     CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
140     Normalise(&forward);
141
142     CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
143     Normalise(&lowforward);
144
145     //Special forwards
146     specialforward[0] = forward;
147
148     specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
149     specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
150     specialforward[1] += forward * .4;
151     Normalise(&specialforward[1]);
152     specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
153     specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
154     specialforward[2] += forward * .4;
155     Normalise(&specialforward[2]);
156
157     specialforward[3] = jointPos(righthip) + jointPos(rightankle);
158     specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
159     specialforward[3] += lowforward * .4;
160     Normalise(&specialforward[3]);
161     specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
162     specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
163     specialforward[4] += lowforward * .4;
164     Normalise(&specialforward[4]);
165 }
166
167 /* EFFECT
168  * TODO
169  *
170  * USES:
171  * Person/Person::RagDoll
172  * Person/Person::DoStuff
173  * Person/IKHelper
174  */
175 float Skeleton::DoConstraints(XYZ *coords, float *scale)
176 {
177     float friction = 1.5;
178     const float elasticity = .3;
179     XYZ bounceness;
180     const int numrepeats = 3;
181     float groundlevel = .15;
182     int i, j, k, m;
183     XYZ temp;
184     XYZ terrainnormal;
185     int whichhit;
186     float frictionness;
187     XYZ terrainlight;
188     int whichpatchx;
189     int whichpatchz;
190     float damage = 0; // eventually returned from function
191     bool breaking = false;
192
193     if (free) {
194         freetime += multiplier;
195
196         whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
197         whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
198
199         terrainlight = *coords;
200         objects.SphereCheckPossible(&terrainlight, 1);
201
202         //Add velocity
203         for (i = 0; i < num_joints; i++) {
204             joints[i].position = joints[i].position + joints[i].velocity * multiplier;
205
206             switch (joints[i].label) {
207             case head:
208                 groundlevel = .8;
209                 break;
210             case righthand:
211             case rightwrist:
212             case rightelbow:
213             case lefthand:
214             case leftwrist:
215             case leftelbow:
216                 groundlevel = .2;
217                 break;
218             default:
219                 groundlevel = .15;
220                 break;
221             }
222
223             joints[i].position.y -= groundlevel;
224             joints[i].oldvelocity = joints[i].velocity;
225         }
226
227         float tempmult = multiplier;
228         //multiplier/=numrepeats;
229
230         for (j = 0; j < numrepeats; j++) {
231             float r = .05;
232             // right leg constraints?
233             if (!joint(rightknee).locked && !joint(righthip).locked) {
234                 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
235                 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
236                     jointPos(rightknee) -= lowforward * .05;
237                     if (spinny)
238                         jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
239                     else
240                         jointVel(rightknee) -= lowforward * .05;
241                     jointPos(rightankle) += lowforward * .025;
242                     if (spinny)
243                         jointVel(rightankle) += lowforward * .025 / multiplier / 4;
244                     else
245                         jointVel(rightankle) += lowforward * .25;
246                     jointPos(righthip) += lowforward * .025;
247                     if (spinny)
248                         jointVel(righthip) += lowforward * .025 / multiplier / 4;
249                     else
250                         jointVel(righthip) += lowforward * .025;
251                     temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
252                 }
253             }
254
255             // left leg constraints?
256             if (!joint(leftknee).locked && !joint(lefthip).locked) {
257                 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
258                 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
259                     jointPos(leftknee) -= lowforward * .05;
260                     if (spinny)
261                         jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
262                     else
263                         jointVel(leftknee) -= lowforward * .05;
264                     jointPos(leftankle) += lowforward * .025;
265                     if (spinny)
266                         jointVel(leftankle) += lowforward * .025 / multiplier / 4;
267                     else
268                         jointVel(leftankle) += lowforward * .25;
269                     jointPos(lefthip) += lowforward * .025;
270                     if (spinny)
271                         jointVel(lefthip) += lowforward * .025 / multiplier / 4;
272                     else
273                         jointVel(lefthip) += lowforward * .025;
274                     temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
275                 }
276             }
277
278             for (i = 0; i < num_joints; i++) {
279                 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
280                     joints[i].locked = 0;
281                 if (spinny && findLengthfast(&joints[i].velocity) > 600)
282                     joints[i].locked = 0;
283                 if (joints[i].delay > 0) {
284                     bool freely = true;
285                     for (j = 0; j < num_joints; j++) {
286                         if (joints[j].locked)
287                             freely = false;
288                     }
289                     if (freely)
290                         joints[i].delay -= multiplier * 3;
291                 }
292             }
293
294             if (num_muscles)
295                 for (i = 0; i < num_muscles; i++) {
296                     //Length constraints
297                     muscles[i].DoConstraint(spinny);
298                 }
299
300             for (i = 0; i < num_joints; i++) {
301                 //Length constraints
302                 //Ground constraint
303                 groundlevel = 0;
304                 if (joints[i].position.y * (*scale) + coords->y < terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel) {
305                     freefall = 0;
306                     friction = 1.5;
307                     if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
308                         joints[i].locked = 1;
309                         joints[i].delay = 1;
310                         if (tutoriallevel != 1 || id == 0) {
311                             emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
312                         }
313                         breaking = true;
314                     }
315
316                     if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
317                         joints[i].locked = 1;
318                         joints[i].delay = 1;
319                         if (tutoriallevel != 1 || id == 0) {
320                             emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
321                         }
322                     }
323
324                     terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
325                     ReflectVector(&joints[i].velocity, &terrainnormal);
326                     bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
327                     if (!joints[i].locked)
328                         damage += findLengthfast(&bounceness) / 4000;
329                     if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
330                         bounceness = 0;
331                     frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
332                     joints[i].velocity -= bounceness;
333                     if (1 - friction * frictionness > 0)
334                         joints[i].velocity *= 1 - friction * frictionness;
335                     else
336                         joints[i].velocity = 0;
337
338                     if (tutoriallevel != 1 || id == 0)
339                         if (findLengthfast(&bounceness) > 8000 && breaking) {
340                             // FIXME: this crashes because k is not initialized!
341                             // to reproduce, type 'wolfie' in console and play a while
342                             // I'll just comment it out for now
343                             //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
344                             Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
345                             breaking = false;
346                             camerashake += .6;
347
348                             emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
349
350                             addEnvSound(*coords, 64);
351                         }
352
353                     if (findLengthfast(&bounceness) > 2500) {
354                         Normalise(&bounceness);
355                         bounceness = bounceness * 50;
356                     }
357
358                     joints[i].velocity += bounceness * elasticity;
359
360                     if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
361                         bounceness = 0;
362                         joints[i].velocity = joints[i].oldvelocity;
363                     }
364
365
366                     if (joints[i].locked == 0)
367                         if (findLengthfast(&joints[i].velocity) < 1)
368                             joints[i].locked = 1;
369
370                     if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
371                         terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
372                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
373                         if (detail == 2)
374                             terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
375                     } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
376                         terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
377                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
378                     }
379
380                     else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
381                         terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
382                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
383                     } else if (findLengthfast(&bounceness) > 500)
384                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
385
386
387                     joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
388                     if (longdead > 100)
389                         broken = 1;
390                 }
391                 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
392                     for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
393                         k = terrain.patchobjects[whichpatchx][whichpatchz][m];
394                         if (k < objects.numobjects && k >= 0)
395                             if (objects.possible[k]) {
396                                 friction = objects.friction[k];
397                                 XYZ start = joints[i].realoldposition;
398                                 XYZ end = joints[i].position * (*scale) + *coords;
399                                 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
400                                 if (whichhit != -1) {
401                                     if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
402                                         joints[i].locked = 1;
403                                         joints[i].delay = 1;
404                                         if (tutoriallevel != 1 || id == 0) {
405                                             emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
406                                         }
407                                         breaking = true;
408                                     }
409
410                                     if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
411                                         joints[i].locked = 1;
412                                         joints[i].delay = 1;
413                                         if (tutoriallevel != 1 || id == 0) {
414                                             emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
415                                         }
416                                     }
417
418                                     terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
419                                     if (terrainnormal.y > .8)
420                                         freefall = 0;
421                                     bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
422                                     if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
423                                         bounceness = 0;
424                                         joints[i].velocity = joints[i].oldvelocity;
425                                     }
426                                     if (tutoriallevel != 1 || id == 0)
427                                         if (findLengthfast(&bounceness) > 4000 && breaking) {
428                                             objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
429                                             Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
430                                             breaking = false;
431                                             camerashake += .6;
432
433                                             emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
434
435                                             addEnvSound(*coords, 64);
436                                         }
437                                     if (objects.type[k] == treetrunktype) {
438                                         objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
439                                         objects.roty[k] += joints[i].velocity.z * multiplier * .4;
440                                         objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
441                                         objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
442                                     }
443                                     if (!joints[i].locked)
444                                         damage += findLengthfast(&bounceness) / 2500;
445                                     ReflectVector(&joints[i].velocity, &terrainnormal);
446                                     frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
447                                     joints[i].velocity -= bounceness;
448                                     if (1 - friction * frictionness > 0)
449                                         joints[i].velocity *= 1 - friction * frictionness;
450                                     else
451                                         joints[i].velocity = 0;
452                                     if (findLengthfast(&bounceness) > 2500) {
453                                         Normalise(&bounceness);
454                                         bounceness = bounceness * 50;
455                                     }
456                                     joints[i].velocity += bounceness * elasticity;
457
458
459                                     if (!joints[i].locked)
460                                         if (findLengthfast(&joints[i].velocity) < 1) {
461                                             joints[i].locked = 1;
462                                         }
463                                     if (findLengthfast(&bounceness) > 500)
464                                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
465                                     joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
466                                     if (longdead > 100)
467                                         broken = 1;
468                                 }
469                             }
470                     }
471                 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
472             }
473         }
474         multiplier = tempmult;
475
476
477         if (terrain.patchobjectnum[whichpatchx][whichpatchz])
478             for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
479                 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
480                 if (objects.possible[k]) {
481                     for (i = 0; i < 26; i++) {
482                         //Make this less stupid
483                         XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
484                         XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
485                         whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
486                         if (whichhit != -1) {
487                             joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
488                             for (j = 0; j < num_muscles; j++) {
489                                 if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i]))
490                                     muscles[j].DoConstraint(spinny);
491                             }
492                         }
493                     }
494                 }
495             }
496
497         for (i = 0; i < num_joints; i++) {
498             switch (joints[i].label) {
499             case head:
500                 groundlevel = .8;
501                 break;
502             case righthand:
503             case rightwrist:
504             case rightelbow:
505             case lefthand:
506             case leftwrist:
507             case leftelbow:
508                 groundlevel = .2;
509                 break;
510             default:
511                 groundlevel = .15;
512                 break;
513             }
514             joints[i].position.y += groundlevel;
515             joints[i].mass = 1;
516             if (joints[i].label == lefthip || joints[i].label == leftknee || joints[i].label == leftankle || joints[i].label == righthip || joints[i].label == rightknee || joints[i].label == rightankle)
517                 joints[i].mass = 2;
518             if (joints[i].locked) {
519                 joints[i].mass = 4;
520             }
521         }
522
523         return damage;
524     }
525
526     if (!free) {
527         for (i = 0; i < num_muscles; i++) {
528             if (muscles[i].type == boneconnect)
529                 muscles[i].DoConstraint(0);
530         }
531     }
532
533     return 0;
534 }
535
536 /* EFFECT
537  * applies gravity to the skeleton
538  *
539  * USES:
540  * Person/Person::DoStuff
541  */
542 void Skeleton::DoGravity(float *scale)
543 {
544     static int i;
545     for (i = 0; i < num_joints; i++) {
546         if (
547                 (
548                     ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
549                     (lowforward.y > -.1) ||
550                     (joints[i].mass < 5)
551                 ) && (
552                     ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
553                     (forward.y < .3)
554                 )
555             )
556             joints[i].velocity.y += gravity * multiplier / (*scale);
557     }
558 }
559
560 /* EFFECT
561  * set muscles[which].rotate1
562  *     .rotate2
563  *     .rotate3
564  *
565  * special case if animation == hanganim
566  */
567 void Skeleton::FindRotationMuscle(int which, int animation)
568 {
569     XYZ p1, p2, fwd;
570     float dist;
571
572     p1 = muscles[which].parent1->position;
573     p2 = muscles[which].parent2->position;
574     dist = findDistance(&p1, &p2);
575     if (p1.y - p2.y <= dist)
576         muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
577     if (p1.y - p2.y > dist)
578         muscles[which].rotate2 = asin(1.f);
579     muscles[which].rotate2 *= 360.0 / 6.2831853;
580
581     p1.y = 0;
582     p2.y = 0;
583     dist = findDistance(&p1, &p2);
584     if (p1.z - p2.z <= dist)
585         muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
586     if (p1.z - p2.z > dist)
587         muscles[which].rotate1 = acos(1.f);
588     muscles[which].rotate1 *= 360.0 / 6.2831853;
589     if (p1.x > p2.x)
590         muscles[which].rotate1 = 360 - muscles[which].rotate1;
591     if (!isnormal(muscles[which].rotate1))
592         muscles[which].rotate1 = 0;
593     if (!isnormal(muscles[which].rotate2))
594         muscles[which].rotate2 = 0;
595
596     const int label1 = muscles[which].parent1->label;
597     const int label2 = muscles[which].parent2->label;
598     switch (label1) {
599     case head:
600         fwd = specialforward[0];
601         break;
602     case rightshoulder:
603     case rightelbow:
604     case rightwrist:
605     case righthand:
606         fwd = specialforward[1];
607         break;
608     case leftshoulder:
609     case leftelbow:
610     case leftwrist:
611     case lefthand:
612         fwd = specialforward[2];
613         break;
614     case righthip:
615     case rightknee:
616     case rightankle:
617     case rightfoot:
618         fwd = specialforward[3];
619         break;
620     case lefthip:
621     case leftknee:
622     case leftankle:
623     case leftfoot:
624         fwd = specialforward[4];
625         break;
626     default:
627         if (muscles[which].parent1->lower)
628             fwd = lowforward;
629         else
630             fwd = forward;
631         break;
632     }
633
634     if (animation == hanganim) {
635         if (label1 == righthand || label2 == righthand) {
636             fwd = 0;
637             fwd.x = -1;
638         }
639         if (label1 == lefthand || label2 == lefthand) {
640             fwd = 0;
641             fwd.x = 1;
642         }
643     }
644
645     if (free == 0) {
646         if (label1 == rightfoot || label2 == rightfoot) {
647             fwd.y -= .3;
648         }
649         if (label1 == leftfoot || label2 == leftfoot) {
650             fwd.y -= .3;
651         }
652     }
653
654     fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
655     fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
656     fwd.y = 0;
657     fwd /= findLength(&fwd);
658     if (fwd.z <= 1 && fwd.z >= -1)
659         muscles[which].rotate3 = acos(0 - fwd.z);
660     else
661         muscles[which].rotate3 = acos(-1.f);
662     muscles[which].rotate3 *= 360.0 / 6.2831853;
663     if (0 > fwd.x)
664         muscles[which].rotate3 = 360 - muscles[which].rotate3;
665     if (!isnormal(muscles[which].rotate3))
666         muscles[which].rotate3 = 0;
667 }
668
669 /* EFFECT
670  * load skeleton
671  * takes filenames for three skeleton files and various models
672  */
673 void Skeleton::Load(const std::string& filename,       const std::string& lowfilename, const std::string& clothesfilename,
674                     const std::string& modelfilename,  const std::string& model2filename,
675                     const std::string& model3filename, const std::string& model4filename,
676                     const std::string& model5filename, const std::string& model6filename,
677                     const std::string& model7filename, const std::string& modellowfilename,
678                     const std::string& modelclothesfilename, bool clothes)
679 {
680     GLfloat M[16];
681     int parentID;
682     FILE *tfile;
683     float lSize;
684     int i, j;
685     int edit;
686
687     LOGFUNC;
688
689     num_models = 7;
690
691     // load various models
692     // rotate, scale, do normals, do texcoords for each as needed
693
694     model[0].loadnotex(modelfilename);
695     model[1].loadnotex(model2filename);
696     model[2].loadnotex(model3filename);
697     model[3].loadnotex(model4filename);
698     model[4].loadnotex(model5filename);
699     model[5].loadnotex(model6filename);
700     model[6].loadnotex(model7filename);
701
702     for (i = 0; i < num_models; i++) {
703         model[i].Rotate(180, 0, 0);
704         model[i].Scale(.04, .04, .04);
705         model[i].CalculateNormals(0);
706     }
707
708     drawmodel.load(modelfilename, 0);
709     drawmodel.Rotate(180, 0, 0);
710     drawmodel.Scale(.04, .04, .04);
711     drawmodel.FlipTexCoords();
712     if (tutoriallevel == 1 && id != 0)
713         drawmodel.UniformTexCoords();
714     if (tutoriallevel == 1 && id != 0)
715         drawmodel.ScaleTexCoords(0.1);
716     drawmodel.CalculateNormals(0);
717
718     modellow.loadnotex(modellowfilename);
719     modellow.Rotate(180, 0, 0);
720     modellow.Scale(.04, .04, .04);
721     modellow.CalculateNormals(0);
722
723     drawmodellow.load(modellowfilename, 0);
724     drawmodellow.Rotate(180, 0, 0);
725     drawmodellow.Scale(.04, .04, .04);
726     drawmodellow.FlipTexCoords();
727     if (tutoriallevel == 1 && id != 0)
728         drawmodellow.UniformTexCoords();
729     if (tutoriallevel == 1 && id != 0)
730         drawmodellow.ScaleTexCoords(0.1);
731     drawmodellow.CalculateNormals(0);
732
733     if (clothes) {
734         modelclothes.loadnotex(modelclothesfilename);
735         modelclothes.Rotate(180, 0, 0);
736         modelclothes.Scale(.041, .04, .041);
737         modelclothes.CalculateNormals(0);
738
739         drawmodelclothes.load(modelclothesfilename, 0);
740         drawmodelclothes.Rotate(180, 0, 0);
741         drawmodelclothes.Scale(.04, .04, .04);
742         drawmodelclothes.FlipTexCoords();
743         drawmodelclothes.CalculateNormals(0);
744     }
745
746     // FIXME: three similar blocks follow, one for each of:
747     // filename, lowfilename, clothesfilename
748
749     // load skeleton
750
751     tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
752
753     // read num_joints
754     funpackf(tfile, "Bi", &num_joints);
755
756     // allocate memory
757     if (joints)
758         delete [] joints; //dealloc2(joints);
759     joints = (Joint*)new Joint[num_joints];
760
761     // read info for each joint
762     for (i = 0; i < num_joints; i++) {
763         funpackf(tfile, "Bf Bf Bf Bf Bf", &joints[i].position.x, &joints[i].position.y, &joints[i].position.z, &joints[i].length, &joints[i].mass);
764         funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
765         funpackf(tfile, "Bi", &joints[i].modelnum);
766         funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
767         funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
768         funpackf(tfile, "Bb", &joints[i].lower);
769         funpackf(tfile, "Bi", &parentID);
770         if (joints[i].hasparent)
771             joints[i].parent = &joints[parentID];
772         joints[i].velocity = 0;
773         joints[i].oldposition = joints[i].position;
774     }
775
776     // read num_muscles
777     funpackf(tfile, "Bi", &num_muscles);
778
779     // allocate memory
780     if (muscles)
781         delete [] muscles; //dealloc2(muscles);
782     muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
783
784     // for each muscle...
785     for (i = 0; i < num_muscles; i++) {
786         // read info
787         funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength, &muscles[i].minlength, &muscles[i].maxlength, &muscles[i].strength, &muscles[i].type, &muscles[i].numvertices);
788
789         // allocate memory for vertices
790         muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
791
792         // read vertices
793         edit = 0;
794         for (j = 0; j < muscles[i].numvertices - edit; j++) {
795             funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
796             if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
797                 muscles[i].numvertices--;
798                 edit--;
799             }
800         }
801
802         // read more info
803         funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
804         muscles[i].parent1 = &joints[parentID];
805         funpackf(tfile, "Bi", &parentID);
806         muscles[i].parent2 = &joints[parentID];
807     }
808
809     // read forwardjoints (?)
810     for (j = 0; j < 3; j++) {
811         funpackf(tfile, "Bi", &forwardjoints[j]);
812     }
813     // read lowforwardjoints (?)
814     for (j = 0; j < 3; j++) {
815         funpackf(tfile, "Bi", &lowforwardjoints[j]);
816     }
817
818     // ???
819     for (j = 0; j < num_muscles; j++) {
820         for (i = 0; i < muscles[j].numvertices; i++) {
821             for (int k = 0; k < num_models; k++) {
822                 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
823                     model[k].owner[muscles[j].vertices[i]] = j;
824             }
825         }
826     }
827
828     // calculate some stuff
829     FindForwards();
830     for (i = 0; i < num_joints; i++) {
831         joints[i].startpos = joints[i].position;
832     }
833     for (i = 0; i < num_muscles; i++) {
834         FindRotationMuscle(i, -1);
835     }
836     // this seems to use opengl purely for matrix calculations
837     for (int k = 0; k < num_models; k++) {
838         for (i = 0; i < model[k].vertexNum; i++) {
839             model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
840             glMatrixMode(GL_MODELVIEW);
841             glPushMatrix();
842             glLoadIdentity();
843             glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
844             glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
845             glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
846             glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
847             glGetFloatv(GL_MODELVIEW_MATRIX, M);
848             model[k].vertex[i].x = M[12] * 1;
849             model[k].vertex[i].y = M[13] * 1;
850             model[k].vertex[i].z = M[14] * 1;
851             glPopMatrix();
852         }
853         model[k].CalculateNormals(0);
854     }
855     fclose(tfile);
856
857     // load ???
858
859     tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" );
860
861     // skip joints section
862
863     lSize = sizeof(num_joints);
864     fseek(tfile, lSize, SEEK_CUR);
865     for (i = 0; i < num_joints; i++) {
866         // skip joint info
867         lSize = sizeof(XYZ)
868                 + sizeof(float)
869                 + sizeof(float)
870                 + 1 //sizeof(bool)
871                 + 1 //sizeof(bool)
872                 + sizeof(int)
873                 + 1 //sizeof(bool)
874                 + 1 //sizeof(bool)
875                 + sizeof(int)
876                 + sizeof(int)
877                 + 1 //sizeof(bool)
878                 + sizeof(int);
879         fseek(tfile, lSize, SEEK_CUR);
880
881         if (joints[i].hasparent)
882             joints[i].parent = &joints[parentID];
883         joints[i].velocity = 0;
884         joints[i].oldposition = joints[i].position;
885     }
886
887     // read num_muscles
888     funpackf(tfile, "Bi", &num_muscles);
889
890     for (i = 0; i < num_muscles; i++) {
891         // skip muscle info
892         lSize = sizeof(float)
893                 + sizeof(float)
894                 + sizeof(float)
895                 + sizeof(float)
896                 + sizeof(float)
897                 + sizeof(int);
898         fseek(tfile, lSize, SEEK_CUR);
899
900         // read numverticeslow
901         funpackf(tfile, "Bi", &muscles[i].numverticeslow);
902
903         if (muscles[i].numverticeslow) {
904             // allocate memory
905             muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
906
907             // read verticeslow
908             edit = 0;
909             for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
910                 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
911                 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
912                     muscles[i].numverticeslow--;
913                     edit--;
914                 }
915             }
916         }
917
918         // skip more stuff
919         lSize = 1; //sizeof(bool);
920         fseek ( tfile, lSize, SEEK_CUR);
921         lSize = sizeof(int);
922         fseek ( tfile, lSize, SEEK_CUR);
923         fseek ( tfile, lSize, SEEK_CUR);
924     }
925
926     for (j = 0; j < num_muscles; j++) {
927         for (i = 0; i < muscles[j].numverticeslow; i++) {
928             if (muscles[j].verticeslow[i] < modellow.vertexNum)
929                 modellow.owner[muscles[j].verticeslow[i]] = j;
930         }
931     }
932
933     // use opengl for its matrix math
934     for (i = 0; i < modellow.vertexNum; i++) {
935         modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
936         glMatrixMode(GL_MODELVIEW);
937         glPushMatrix();
938         glLoadIdentity();
939         glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
940         glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
941         glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
942         glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
943         glGetFloatv(GL_MODELVIEW_MATRIX, M);
944         modellow.vertex[i].x = M[12];
945         modellow.vertex[i].y = M[13];
946         modellow.vertex[i].z = M[14];
947         glPopMatrix();
948     }
949
950     modellow.CalculateNormals(0);
951
952     // load clothes
953
954     if (clothes) {
955         tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" );
956
957         // skip num_joints
958         lSize = sizeof(num_joints);
959         fseek ( tfile, lSize, SEEK_CUR);
960
961         for (i = 0; i < num_joints; i++) {
962             // skip joint info
963             lSize = sizeof(XYZ)
964                     + sizeof(float)
965                     + sizeof(float)
966                     + 1 //sizeof(bool)
967                     + 1 //sizeof(bool)
968                     + sizeof(int)
969                     + 1 //sizeof(bool)
970                     + 1 //sizeof(bool)
971                     + sizeof(int)
972                     + sizeof(int)
973                     + 1 //sizeof(bool)
974                     + sizeof(int);
975             fseek(tfile, lSize, SEEK_CUR);
976
977             if (joints[i].hasparent)
978                 joints[i].parent = &joints[parentID];
979             joints[i].velocity = 0;
980             joints[i].oldposition = joints[i].position;
981         }
982
983         // read num_muscles
984         funpackf(tfile, "Bi", &num_muscles);
985
986         for (i = 0; i < num_muscles; i++) {
987             // skip muscle info
988             lSize = sizeof(float)
989                     + sizeof(float)
990                     + sizeof(float)
991                     + sizeof(float)
992                     + sizeof(float)
993                     + sizeof(int);
994             fseek(tfile, lSize, SEEK_CUR);
995
996             // read numverticesclothes
997             funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
998
999             // read verticesclothes
1000             if (muscles[i].numverticesclothes) {
1001                 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1002                 edit = 0;
1003                 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1004                     funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1005                     if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1006                         muscles[i].numverticesclothes--;
1007                         edit--;
1008                     }
1009                 }
1010             }
1011
1012             // skip more stuff
1013             lSize = 1; //sizeof(bool);
1014             fseek ( tfile, lSize, SEEK_CUR);
1015             lSize = sizeof(int);
1016             fseek ( tfile, lSize, SEEK_CUR);
1017             fseek ( tfile, lSize, SEEK_CUR);
1018         }
1019
1020         // ???
1021         lSize = sizeof(int);
1022         for (j = 0; j < num_muscles; j++) {
1023             for (i = 0; i < muscles[j].numverticesclothes; i++) {
1024                 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1025                     modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1026             }
1027         }
1028
1029         // use opengl for its matrix math
1030         for (i = 0; i < modelclothes.vertexNum; i++) {
1031             modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1032             glMatrixMode(GL_MODELVIEW);
1033             glPushMatrix();
1034             glLoadIdentity();
1035             glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1036             glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1037             glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1038             glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1039             glGetFloatv(GL_MODELVIEW_MATRIX, M);
1040             modelclothes.vertex[i].x = M[12];
1041             modelclothes.vertex[i].y = M[13];
1042             modelclothes.vertex[i].z = M[14];
1043             glPopMatrix();
1044         }
1045
1046         modelclothes.CalculateNormals(0);
1047     }
1048     fclose(tfile);
1049
1050     for (i = 0; i < num_joints; i++) {
1051         for (j = 0; j < num_joints; j++) {
1052             if (joints[i].label == j)
1053                 jointlabels[j] = i;
1054         }
1055     }
1056
1057     free = 0;
1058 }
1059
1060 Skeleton::Skeleton()
1061 {
1062     num_joints = 0;
1063
1064     num_muscles = 0;
1065
1066     selected = 0;
1067
1068     memset(forwardjoints, 0, sizeof(forwardjoints));
1069     // XYZ forward;
1070
1071     id = 0;
1072
1073     memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1074     // XYZ lowforward;
1075
1076     // XYZ specialforward[5];
1077     memset(jointlabels, 0, sizeof(jointlabels));
1078
1079     // Model model[7];
1080     // Model modellow;
1081     // Model modelclothes;
1082     num_models = 0;
1083
1084     // Model drawmodel;
1085     // Model drawmodellow;
1086     // Model drawmodelclothes;
1087
1088     clothes = 0;
1089     spinny = 0;
1090
1091     memset(skinText, 0, sizeof(skinText));
1092     skinsize = 0;
1093
1094     checkdelay = 0;
1095
1096     longdead = 0;
1097     broken = 0;
1098
1099     free = 0;
1100     oldfree = 0;
1101     freetime = 0;
1102     freefall = 0;
1103
1104     joints = 0;
1105     muscles = 0;
1106 }
1107
1108 Skeleton::~Skeleton()
1109 {
1110     if (muscles) {
1111         delete [] muscles;
1112     }
1113     muscles = 0;
1114
1115     if (joints) {
1116         delete [] joints;
1117     }
1118     joints = 0;
1119 }
1120
1121 Muscle::Muscle()
1122 {
1123     vertices = 0;
1124     verticeslow = 0;
1125     verticesclothes = 0;
1126
1127     numvertices = 0;
1128     numverticeslow = 0;
1129     numverticesclothes = 0;
1130     length = 0;
1131     targetlength = 0;
1132     parent1 = 0;
1133     parent2 = 0;
1134     maxlength = 0;
1135     minlength = 0;
1136     type = 0;
1137     visible = 0;
1138     rotate1 = 0, rotate2 = 0, rotate3 = 0;
1139     lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1140     oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1141     newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1142
1143     strength = 0;
1144 }
1145
1146 Muscle::~Muscle()
1147 {
1148     dealloc2(vertices);
1149     dealloc2(verticeslow);
1150     dealloc2(verticesclothes);
1151 }
1152
1153 #if 0
1154
1155 // the following functions are not used anywhere
1156
1157 /* EFFECT
1158  * sets forward, lowforward, specialforward[]
1159  *
1160  * USES:
1161  * NONE
1162  */
1163 void Skeleton::FindForwardsfirst()
1164 {
1165     //Find forward vectors
1166     CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1167     Normalise(&forward);
1168
1169     CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1170     Normalise(&lowforward);
1171
1172     //Special forwards
1173     specialforward[0] = forward;
1174     specialforward[1] = forward;
1175     specialforward[2] = forward;
1176     specialforward[3] = forward;
1177     specialforward[4] = forward;
1178
1179 }
1180
1181 /* EFFECT
1182  *
1183  * USES:
1184  * NONE
1185  */
1186 void Skeleton::Draw(int muscleview)
1187 {
1188     static float jointcolor[4];
1189
1190     if (muscleview != 2) {
1191         jointcolor[0] = 0;
1192         jointcolor[1] = 0;
1193         jointcolor[2] = .5;
1194         jointcolor[3] = 1;
1195     }
1196
1197     if (muscleview == 2) {
1198         jointcolor[0] = 0;
1199         jointcolor[1] = 0;
1200         jointcolor[2] = 0;
1201         jointcolor[3] = .5;
1202     }
1203     //Calc motionblur-ness
1204     for (int i = 0; i < num_joints; i++) {
1205         joints[i].oldposition = joints[i].position;
1206         joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1207         if (joints[i].blurred < 1)
1208             joints[i].blurred = 1;
1209     }
1210
1211     //Do Motionblur
1212     glDepthMask(0);
1213     glEnable(GL_BLEND);
1214     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1215     glBegin(GL_QUADS);
1216     for (int i = 0; i < num_joints; i++) {
1217         if (joints[i].hasparent) {
1218             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1219             glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1220             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1221             glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1222             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1223             glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1224             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1225             glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1226         }
1227     }
1228     for (int i = 0; i < num_muscles; i++) {
1229         if (muscles[i].type == boneconnect) {
1230             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1231             glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1232             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1233             glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1234             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1235             glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1236             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1237             glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1238         }
1239     }
1240     glEnd();
1241
1242     glBegin(GL_LINES);
1243     for (int i = 0; i < num_joints; i++) {
1244         if (joints[i].hasparent) {
1245             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1246             glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1247             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1248             glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1249         }
1250     }
1251     for (int i = 0; i < num_muscles; i++) {
1252         if (muscles[i].type == boneconnect) {
1253             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1254             glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1255             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1256             glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1257         }
1258     }
1259     glColor3f(.6, .6, 0);
1260     if (muscleview == 1)
1261         for (int i = 0; i < num_muscles; i++) {
1262             if (muscles[i].type != boneconnect) {
1263                 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1264                 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1265             }
1266         }
1267     glEnd();
1268
1269     if (muscleview != 2) {
1270         glPointSize(3);
1271         glBegin(GL_POINTS);
1272         for (int i = 0; i < num_joints; i++) {
1273             if (i != selected)
1274                 glColor4f(0, 0, .5, 1);
1275             if (i == selected)
1276                 glColor4f(1, 1, 0, 1);
1277             if (joints[i].locked && i != selected)
1278                 glColor4f(1, 0, 0, 1);
1279             glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1280         }
1281         glEnd();
1282     }
1283
1284     //Set old position to current position
1285     if (muscleview == 2)
1286         for (int i = 0; i < num_joints; i++) {
1287             joints[i].oldposition = joints[i].position;
1288         }
1289     glDepthMask(1);
1290 }
1291
1292 /* EFFECT
1293  *
1294  * USES:
1295  * NONE
1296  */
1297 void Skeleton::AddJoint(float x, float y, float z, int which)
1298 {
1299     if (num_joints < max_joints - 1) {
1300         joints[num_joints].velocity = 0;
1301         joints[num_joints].position.x = x;
1302         joints[num_joints].position.y = y;
1303         joints[num_joints].position.z = z;
1304         joints[num_joints].mass = 1;
1305         joints[num_joints].locked = 0;
1306
1307         joints[num_joints].hasparent = 0;
1308         num_joints++;
1309         if (which < num_joints && which >= 0)
1310             AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1311     }
1312 }
1313
1314 /* EFFECT
1315  *
1316  * USES:
1317  * NONE
1318  */
1319 void Skeleton::DeleteJoint(int whichjoint)
1320 {
1321     if (whichjoint < num_joints && whichjoint >= 0) {
1322         joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1323         joints[whichjoint].position = joints[num_joints - 1].position;
1324         joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1325         joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1326         joints[whichjoint].parent = joints[num_joints - 1].parent;
1327         joints[whichjoint].length = joints[num_joints - 1].length;
1328         joints[whichjoint].locked = joints[num_joints - 1].locked;
1329         joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1330         joints[whichjoint].visible = joints[num_joints - 1].visible;
1331
1332         for (int i = 0; i < num_muscles; i++) {
1333             while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1334             while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1335         }
1336         for (int i = 0; i < num_muscles; i++) {
1337             while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1338             while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1339         }
1340         for (int i = 0; i < num_joints; i++) {
1341             if (joints[i].parent == &joints[whichjoint])
1342                 joints[i].hasparent = 0;
1343         }
1344         for (int i = 0; i < num_joints; i++) {
1345             if (joints[i].parent == &joints[num_joints - 1])
1346                 joints[i].parent = &joints[whichjoint];
1347         }
1348
1349         num_joints--;
1350     }
1351 }
1352
1353 /* EFFECT
1354  *
1355  * USES:
1356  * Skeleton::DeleteJoint - UNUSED
1357  */
1358 void Skeleton::DeleteMuscle(int whichmuscle)
1359 {
1360     if (whichmuscle < num_muscles) {
1361         muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1362         muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1363         muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1364         muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1365         muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1366         muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1367         muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1368         muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1369         muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1370
1371         num_muscles--;
1372     }
1373 }
1374
1375 /* EFFECT
1376  *
1377  * USES:
1378  * NONE
1379  */
1380 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1381 {
1382     if (whichjoint < num_joints) {
1383         joints[whichjoint].velocity = 0;
1384         joints[whichjoint].position.x = x;
1385         joints[whichjoint].position.y = y;
1386         joints[whichjoint].position.z = z;
1387
1388         if (which >= num_joints || which < 0)
1389             joints[whichjoint].hasparent = 0;
1390         if (which < num_joints && which >= 0) {
1391             joints[whichjoint].parent = &joints[which];
1392             joints[whichjoint].hasparent = 1;
1393             joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1394         }
1395     }
1396 }
1397
1398 /* EFFECT
1399  *
1400  * USES:
1401  * Skeleton::AddJoint - UNUSED
1402  */
1403 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1404 {
1405     const int max_muscles = 100; // FIXME: Probably can be dropped
1406     if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1407         muscles[num_muscles].parent1 = &joints[attach1];
1408         muscles[num_muscles].parent2 = &joints[attach2];
1409         muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1410         muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1411         muscles[num_muscles].strength = .7;
1412         muscles[num_muscles].type = type;
1413         muscles[num_muscles].minlength = minlength;
1414         muscles[num_muscles].maxlength = maxlength;
1415
1416         num_muscles++;
1417     }
1418 }
1419
1420 /* EFFECT
1421  *
1422  * USES:
1423  * NONE
1424  */
1425 void Skeleton::MusclesSet()
1426 {
1427     for (int i = 0; i < num_muscles; i++) {
1428         muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1429     }
1430 }
1431
1432 /* EFFECT
1433  *
1434  * USES:
1435  * NONE
1436  */
1437 void Skeleton::DoBalance()
1438 {
1439     /*XYZ newpoint;
1440     newpoint=joints[0].position;
1441     newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1442     newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1443     joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1444     //Move child point to within certain distance of parent point
1445     joints[0].position=newpoint;
1446
1447     MusclesSet();*/
1448 }
1449
1450 #endif
1451