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