]> git.jsancho.org Git - lugaru.git/blob - Source/Skeleton.cpp
0f06dd3c011405a881c26ee5b2984d25560ab58d
[lugaru.git] / Source / 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 "Skeleton.h"
24 #include "openal_wrapper.h"
25 #include "Animation.h"
26
27 extern float multiplier;
28 extern float gravity;
29 extern Skeleton testskeleton;
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 XYZ envsound[30];
37 extern float envsoundvol[30];
38 extern int numenvsounds;
39 extern float envsoundlife[30];
40 extern int tutoriallevel;
41
42 extern int whichjointstartarray[26];
43 extern int whichjointendarray[26];
44
45 extern bool visibleloading;
46
47 /* EFFECT
48  */
49 void dealloc2(void* param)
50 {
51     free(param);
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 (
557                 (
558                     ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
559                     (lowforward.y > -.1) ||
560                     (joints[i].mass < 5)
561                 ) && (
562                     ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
563                     (forward.y < .3)
564                 )
565             )
566             joints[i].velocity.y += gravity * multiplier / (*scale);
567     }
568 }
569
570 /* EFFECT
571  * set muscles[which].rotate1
572  *     .rotate2
573  *     .rotate3
574  *
575  * special case if animation == hanganim
576  */
577 void Skeleton::FindRotationMuscle(int which, int animation)
578 {
579     XYZ p1, p2, fwd;
580     float dist;
581
582     p1 = muscles[which].parent1->position;
583     p2 = muscles[which].parent2->position;
584     dist = findDistance(&p1, &p2);
585     if (p1.y - p2.y <= dist)
586         muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
587     if (p1.y - p2.y > dist)
588         muscles[which].rotate2 = asin(1.f);
589     muscles[which].rotate2 *= 360.0 / 6.2831853;
590
591     p1.y = 0;
592     p2.y = 0;
593     dist = findDistance(&p1, &p2);
594     if (p1.z - p2.z <= dist)
595         muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
596     if (p1.z - p2.z > dist)
597         muscles[which].rotate1 = acos(1.f);
598     muscles[which].rotate1 *= 360.0 / 6.2831853;
599     if (p1.x > p2.x)
600         muscles[which].rotate1 = 360 - muscles[which].rotate1;
601     if (!isnormal(muscles[which].rotate1))
602         muscles[which].rotate1 = 0;
603     if (!isnormal(muscles[which].rotate2))
604         muscles[which].rotate2 = 0;
605
606     const int label1 = muscles[which].parent1->label;
607     const int label2 = muscles[which].parent2->label;
608     switch (label1) {
609     case head:
610         fwd = specialforward[0];
611         break;
612     case rightshoulder:
613     case rightelbow:
614     case rightwrist:
615     case righthand:
616         fwd = specialforward[1];
617         break;
618     case leftshoulder:
619     case leftelbow:
620     case leftwrist:
621     case lefthand:
622         fwd = specialforward[2];
623         break;
624     case righthip:
625     case rightknee:
626     case rightankle:
627     case rightfoot:
628         fwd = specialforward[3];
629         break;
630     case lefthip:
631     case leftknee:
632     case leftankle:
633     case leftfoot:
634         fwd = specialforward[4];
635         break;
636     default:
637         if (muscles[which].parent1->lower)
638             fwd = lowforward;
639         else
640             fwd = forward;
641         break;
642     }
643
644     if (animation == hanganim) {
645         if (label1 == righthand || label2 == righthand) {
646             fwd = 0;
647             fwd.x = -1;
648         }
649         if (label1 == lefthand || label2 == lefthand) {
650             fwd = 0;
651             fwd.x = 1;
652         }
653     }
654
655     if (free == 0) {
656         if (label1 == rightfoot || label2 == rightfoot) {
657             fwd.y -= .3;
658         }
659         if (label1 == leftfoot || label2 == leftfoot) {
660             fwd.y -= .3;
661         }
662     }
663
664     fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
665     fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
666     fwd.y = 0;
667     fwd /= findLength(&fwd);
668     if (fwd.z <= 1 && fwd.z >= -1)
669         muscles[which].rotate3 = acos(0 - fwd.z);
670     else
671         muscles[which].rotate3 = acos(-1.f);
672     muscles[which].rotate3 *= 360.0 / 6.2831853;
673     if (0 > fwd.x)
674         muscles[which].rotate3 = 360 - muscles[which].rotate3;
675     if (!isnormal(muscles[which].rotate3))
676         muscles[which].rotate3 = 0;
677 }
678
679 /* EFFECT
680  * load an animation from file
681  */
682 void Animation::Load(const char *filename, int aheight, int aattack)
683 {
684     FILE *tfile;
685     int i, j;
686     XYZ startoffset, endoffset;
687
688     // path to dir
689     const char *anim_prefix = ":Data:Animations:";
690
691
692     LOGFUNC;
693
694     // concatenate anim_prefix + filename
695     int len = strlen(anim_prefix) + strlen(filename);
696     char *buf = new char[len + 1];
697     snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
698     // Changing the filename into something the OS can understand
699     char *fixedFN = ConvertFileName(buf);
700     delete[] buf;
701
702     LOG(std::string("Loading animation...") + fixedFN);
703
704     // clear existing data
705     deallocate();
706
707     height = aheight;
708     attack = aattack;
709
710     if (visibleloading)
711         Game::LoadingScreen();
712
713     // read file in binary mode
714     tfile = fopen( fixedFN, "rb" );
715     if (tfile) {
716         // read numframes, joints to know how much memory to allocate
717         funpackf(tfile, "Bi Bi", &numframes, &joints);
718
719         // allocate memory for everything
720
721         position = (XYZ**)malloc(sizeof(XYZ*) * joints);
722         for (i = 0; i < joints; i++)
723             position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
724
725         twist = (float**)malloc(sizeof(float*) * joints);
726         for (i = 0; i < joints; i++)
727             twist[i] = (float*)malloc(sizeof(float) * numframes);
728
729         twist2 = (float**)malloc(sizeof(float*) * joints);
730         for (i = 0; i < joints; i++)
731             twist2[i] = (float*)malloc(sizeof(float) * numframes);
732
733         speed = (float*)malloc(sizeof(float) * numframes);
734
735         onground = (bool**)malloc(sizeof(bool*) * joints);
736         for (i = 0; i < joints; i++)
737             onground[i] = (bool*)malloc(sizeof(bool) * numframes);
738
739         forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
740         weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
741         label = (int*)malloc(sizeof(int) * numframes);
742
743         // read binary data as animation
744
745         // for each frame...
746         for (i = 0; i < numframes; i++) {
747             // for each joint in the skeleton...
748             for (j = 0; j < joints; j++) {
749                 // read joint position
750                 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
751             }
752             for (j = 0; j < joints; j++) {
753                 // read twist
754                 funpackf(tfile, "Bf", &twist[j][i]);
755             }
756             for (j = 0; j < joints; j++) {
757                 // read onground (boolean)
758                 unsigned char uch;
759                 funpackf(tfile, "Bb", &uch);
760                 onground[j][i] = (uch != 0);
761             }
762             // read frame speed (?)
763             funpackf(tfile, "Bf", &speed[i]);
764         }
765         // read twist2 for whole animation
766         for (i = 0; i < numframes; i++) {
767             for (j = 0; j < joints; j++) {
768                 funpackf(tfile, "Bf", &twist2[j][i]);
769             }
770         }
771         // read label for each frame
772         for (i = 0; i < numframes; i++) {
773             funpackf(tfile, "Bf", &label[i]);
774         }
775         // read weapontargetnum
776         funpackf(tfile, "Bi", &weapontargetnum);
777         // read weapontarget positions for each frame
778         for (i = 0; i < numframes; i++) {
779             funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
780         }
781
782         fclose(tfile);
783     }
784
785     startoffset = 0;
786     endoffset = 0;
787     // find average position of certain joints on first and last frames
788     // and save in startoffset, endoffset
789     // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
790     for (j = 0; j < joints; j++) {
791         if (position[j][0].y < 1)
792             startoffset += position[j][0];
793         if (position[j][numframes - 1].y < 1)
794             endoffset += position[j][numframes - 1];
795     }
796     startoffset /= joints;
797     endoffset /= joints;
798     offset = endoffset;
799     offset.y = 0;
800 }
801
802
803 /* EFFECT
804  * load skeleton
805  * takes filenames for three skeleton files and various models
806  */
807 void Skeleton::Load(const char *filename,       const char *lowfilename, const char *clothesfilename,
808                     const char *modelfilename,  const char *model2filename,
809                     const char *model3filename, const char *model4filename,
810                     const char *model5filename, const char *model6filename,
811                     const char *model7filename, const char *modellowfilename,
812                     const char *modelclothesfilename, bool clothes)
813 {
814     GLfloat M[16];
815     int parentID;
816     FILE *tfile;
817     float lSize;
818     int i, j;
819     int edit;
820
821     LOGFUNC;
822
823     num_models = 7;
824
825     // load various models
826     // rotate, scale, do normals, do texcoords for each as needed
827
828     model[0].loadnotex(modelfilename);
829     model[1].loadnotex(model2filename);
830     model[2].loadnotex(model3filename);
831     model[3].loadnotex(model4filename);
832     model[4].loadnotex(model5filename);
833     model[5].loadnotex(model6filename);
834     model[6].loadnotex(model7filename);
835
836     for (i = 0; i < num_models; i++) {
837         model[i].Rotate(180, 0, 0);
838         model[i].Scale(.04, .04, .04);
839         model[i].CalculateNormals(0);
840     }
841
842     drawmodel.load(modelfilename, 0);
843     drawmodel.Rotate(180, 0, 0);
844     drawmodel.Scale(.04, .04, .04);
845     drawmodel.FlipTexCoords();
846     if (tutoriallevel == 1 && id != 0)
847         drawmodel.UniformTexCoords();
848     if (tutoriallevel == 1 && id != 0)
849         drawmodel.ScaleTexCoords(0.1);
850     drawmodel.CalculateNormals(0);
851
852     modellow.loadnotex(modellowfilename);
853     modellow.Rotate(180, 0, 0);
854     modellow.Scale(.04, .04, .04);
855     modellow.CalculateNormals(0);
856
857     drawmodellow.load(modellowfilename, 0);
858     drawmodellow.Rotate(180, 0, 0);
859     drawmodellow.Scale(.04, .04, .04);
860     drawmodellow.FlipTexCoords();
861     if (tutoriallevel == 1 && id != 0)
862         drawmodellow.UniformTexCoords();
863     if (tutoriallevel == 1 && id != 0)
864         drawmodellow.ScaleTexCoords(0.1);
865     drawmodellow.CalculateNormals(0);
866
867     if (clothes) {
868         modelclothes.loadnotex(modelclothesfilename);
869         modelclothes.Rotate(180, 0, 0);
870         modelclothes.Scale(.041, .04, .041);
871         modelclothes.CalculateNormals(0);
872
873         drawmodelclothes.load(modelclothesfilename, 0);
874         drawmodelclothes.Rotate(180, 0, 0);
875         drawmodelclothes.Scale(.04, .04, .04);
876         drawmodelclothes.FlipTexCoords();
877         drawmodelclothes.CalculateNormals(0);
878     }
879
880     // FIXME: three similar blocks follow, one for each of:
881     // filename, lowfilename, clothesfilename
882
883     // load skeleton
884
885     tfile = fopen( ConvertFileName(filename), "rb" );
886
887     if (1) { // FIXME: should this be if(tfile) ?
888         // read num_joints
889         funpackf(tfile, "Bi", &num_joints);
890
891         // allocate memory
892         if (joints)
893             delete [] joints; //dealloc2(joints);
894         joints = (Joint*)new Joint[num_joints];
895
896         // read info for each joint
897         for (i = 0; i < num_joints; i++) {
898             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);
899             funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
900             funpackf(tfile, "Bi", &joints[i].modelnum);
901             funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
902             funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
903             funpackf(tfile, "Bb", &joints[i].lower);
904             funpackf(tfile, "Bi", &parentID);
905             if (joints[i].hasparent)
906                 joints[i].parent = &joints[parentID];
907             joints[i].velocity = 0;
908             joints[i].oldposition = joints[i].position;
909         }
910
911         // read num_muscles
912         funpackf(tfile, "Bi", &num_muscles);
913
914         // allocate memory
915         if (muscles)
916             delete [] muscles; //dealloc2(muscles);
917         muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
918
919         // for each muscle...
920         for (i = 0; i < num_muscles; i++) {
921             // read info
922             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);
923
924             // allocate memory for vertices
925             muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
926
927             // read vertices
928             edit = 0;
929             for (j = 0; j < muscles[i].numvertices - edit; j++) {
930                 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
931                 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
932                     muscles[i].numvertices--;
933                     edit--;
934                 }
935             }
936
937             // read more info
938             funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
939             muscles[i].parent1 = &joints[parentID];
940             funpackf(tfile, "Bi", &parentID);
941             muscles[i].parent2 = &joints[parentID];
942         }
943
944         // read forwardjoints (?)
945         for (j = 0; j < 3; j++) {
946             funpackf(tfile, "Bi", &forwardjoints[j]);
947         }
948         // read lowforwardjoints (?)
949         for (j = 0; j < 3; j++) {
950             funpackf(tfile, "Bi", &lowforwardjoints[j]);
951         }
952
953         // ???
954         for (j = 0; j < num_muscles; j++) {
955             for (i = 0; i < muscles[j].numvertices; i++) {
956                 for (int k = 0; k < num_models; k++) {
957                     if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
958                         model[k].owner[muscles[j].vertices[i]] = j;
959                 }
960             }
961         }
962
963         // calculate some stuff
964         FindForwards();
965         for (i = 0; i < num_joints; i++) {
966             joints[i].startpos = joints[i].position;
967         }
968         for (i = 0; i < num_muscles; i++) {
969             FindRotationMuscle(i, -1);
970         }
971         // this seems to use opengl purely for matrix calculations
972         for (int k = 0; k < num_models; k++) {
973             for (i = 0; i < model[k].vertexNum; i++) {
974                 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
975                 glMatrixMode(GL_MODELVIEW);
976                 glPushMatrix();
977                 glLoadIdentity();
978                 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
979                 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
980                 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
981                 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
982                 glGetFloatv(GL_MODELVIEW_MATRIX, M);
983                 model[k].vertex[i].x = M[12] * 1;
984                 model[k].vertex[i].y = M[13] * 1;
985                 model[k].vertex[i].z = M[14] * 1;
986                 glPopMatrix();
987             }
988             model[k].CalculateNormals(0);
989         }
990     }
991     fclose(tfile);
992
993     // load ???
994
995     tfile = fopen( ConvertFileName(lowfilename), "rb" );
996
997     if (1) { // FIXME: should this be if(tfile) ?
998         // skip joints section
999
1000         lSize = sizeof(num_joints);
1001         fseek(tfile, lSize, SEEK_CUR);
1002         for (i = 0; i < num_joints; i++) {
1003             // skip joint info
1004             lSize = sizeof(XYZ)
1005                     + sizeof(float)
1006                     + sizeof(float)
1007                     + 1 //sizeof(bool)
1008                     + 1 //sizeof(bool)
1009                     + sizeof(int)
1010                     + 1 //sizeof(bool)
1011                     + 1 //sizeof(bool)
1012                     + sizeof(int)
1013                     + sizeof(int)
1014                     + 1 //sizeof(bool)
1015                     + sizeof(int);
1016             fseek(tfile, lSize, SEEK_CUR);
1017
1018             if (joints[i].hasparent)
1019                 joints[i].parent = &joints[parentID];
1020             joints[i].velocity = 0;
1021             joints[i].oldposition = joints[i].position;
1022         }
1023
1024         // read num_muscles
1025         funpackf(tfile, "Bi", &num_muscles);
1026
1027         for (i = 0; i < num_muscles; i++) {
1028             // skip muscle info
1029             lSize = sizeof(float)
1030                     + sizeof(float)
1031                     + sizeof(float)
1032                     + sizeof(float)
1033                     + sizeof(float)
1034                     + sizeof(int);
1035             fseek(tfile, lSize, SEEK_CUR);
1036
1037             // read numverticeslow
1038             funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1039
1040             if (muscles[i].numverticeslow) {
1041                 // allocate memory
1042                 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1043
1044                 // read verticeslow
1045                 edit = 0;
1046                 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1047                     funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1048                     if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1049                         muscles[i].numverticeslow--;
1050                         edit--;
1051                     }
1052                 }
1053             }
1054
1055             // skip more stuff
1056             lSize = 1; //sizeof(bool);
1057             fseek ( tfile, lSize, SEEK_CUR);
1058             lSize = sizeof(int);
1059             fseek ( tfile, lSize, SEEK_CUR);
1060             fseek ( tfile, lSize, SEEK_CUR);
1061         }
1062
1063         for (j = 0; j < num_muscles; j++) {
1064             for (i = 0; i < muscles[j].numverticeslow; i++) {
1065                 if (muscles[j].verticeslow[i] < modellow.vertexNum)
1066                     modellow.owner[muscles[j].verticeslow[i]] = j;
1067             }
1068         }
1069
1070         // use opengl for its matrix math
1071         for (i = 0; i < modellow.vertexNum; i++) {
1072             modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1073             glMatrixMode(GL_MODELVIEW);
1074             glPushMatrix();
1075             glLoadIdentity();
1076             glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1077             glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1078             glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1079             glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1080             glGetFloatv(GL_MODELVIEW_MATRIX, M);
1081             modellow.vertex[i].x = M[12];
1082             modellow.vertex[i].y = M[13];
1083             modellow.vertex[i].z = M[14];
1084             glPopMatrix();
1085         }
1086
1087         modellow.CalculateNormals(0);
1088     }
1089
1090     // load clothes
1091
1092     if (clothes) {
1093         tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
1094
1095         // skip num_joints
1096         lSize = sizeof(num_joints);
1097         fseek ( tfile, lSize, SEEK_CUR);
1098
1099         for (i = 0; i < num_joints; i++) {
1100             // skip joint info
1101             lSize = sizeof(XYZ)
1102                     + sizeof(float)
1103                     + sizeof(float)
1104                     + 1 //sizeof(bool)
1105                     + 1 //sizeof(bool)
1106                     + sizeof(int)
1107                     + 1 //sizeof(bool)
1108                     + 1 //sizeof(bool)
1109                     + sizeof(int)
1110                     + sizeof(int)
1111                     + 1 //sizeof(bool)
1112                     + sizeof(int);
1113             fseek(tfile, lSize, SEEK_CUR);
1114
1115             if (joints[i].hasparent)
1116                 joints[i].parent = &joints[parentID];
1117             joints[i].velocity = 0;
1118             joints[i].oldposition = joints[i].position;
1119         }
1120
1121         // read num_muscles
1122         funpackf(tfile, "Bi", &num_muscles);
1123
1124         for (i = 0; i < num_muscles; i++) {
1125             // skip muscle info
1126             lSize = sizeof(float)
1127                     + sizeof(float)
1128                     + sizeof(float)
1129                     + sizeof(float)
1130                     + sizeof(float)
1131                     + sizeof(int);
1132             fseek(tfile, lSize, SEEK_CUR);
1133
1134             // read numverticesclothes
1135             funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1136
1137             // read verticesclothes
1138             if (muscles[i].numverticesclothes) {
1139                 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1140                 edit = 0;
1141                 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1142                     funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1143                     if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1144                         muscles[i].numverticesclothes--;
1145                         edit--;
1146                     }
1147                 }
1148             }
1149
1150             // skip more stuff
1151             lSize = 1; //sizeof(bool);
1152             fseek ( tfile, lSize, SEEK_CUR);
1153             lSize = sizeof(int);
1154             fseek ( tfile, lSize, SEEK_CUR);
1155             fseek ( tfile, lSize, SEEK_CUR);
1156         }
1157
1158         // ???
1159         lSize = sizeof(int);
1160         for (j = 0; j < num_muscles; j++) {
1161             for (i = 0; i < muscles[j].numverticesclothes; i++) {
1162                 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1163                     modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1164             }
1165         }
1166
1167         // use opengl for its matrix math
1168         for (i = 0; i < modelclothes.vertexNum; i++) {
1169             modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1170             glMatrixMode(GL_MODELVIEW);
1171             glPushMatrix();
1172             glLoadIdentity();
1173             glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1174             glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1175             glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1176             glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1177             glGetFloatv(GL_MODELVIEW_MATRIX, M);
1178             modelclothes.vertex[i].x = M[12];
1179             modelclothes.vertex[i].y = M[13];
1180             modelclothes.vertex[i].z = M[14];
1181             glPopMatrix();
1182         }
1183
1184         modelclothes.CalculateNormals(0);
1185     }
1186     fclose(tfile);
1187
1188     for (i = 0; i < num_joints; i++) {
1189         for (j = 0; j < num_joints; j++) {
1190             if (joints[i].label == j)
1191                 jointlabels[j] = i;
1192         }
1193     }
1194
1195     free = 0;
1196 }
1197
1198 Animation::Animation()
1199 {
1200     numframes = 0;
1201     height = 0;
1202     attack = 0;
1203     joints = 0;
1204     weapontargetnum = 0;
1205
1206     position = 0;
1207     twist = 0;
1208     twist2 = 0;
1209     speed = 0;
1210     onground = 0;
1211     forward = 0;
1212     label = 0;
1213     weapontarget = 0;
1214 }
1215
1216 Animation::~Animation()
1217 {
1218     deallocate();
1219 }
1220
1221 void Animation::deallocate()
1222 {
1223     int i = 0;
1224
1225     if (position) {
1226         for (i = 0; i < joints; i++)
1227             dealloc2(position[i]);
1228
1229         dealloc2(position);
1230     }
1231     position = 0;
1232
1233     if (twist) {
1234         for (i = 0; i < joints; i++)
1235             dealloc2(twist[i]);
1236
1237         dealloc2(twist);
1238     }
1239     twist = 0;
1240
1241     if (twist2) {
1242         for (i = 0; i < joints; i++)
1243             dealloc2(twist2[i]);
1244
1245         dealloc2(twist2);
1246     }
1247     twist2 = 0;
1248
1249     if (onground) {
1250         for (i = 0; i < joints; i++)
1251             dealloc2(onground[i]);
1252
1253         dealloc2(onground);
1254     }
1255     onground = 0;
1256
1257     if (speed)
1258         dealloc2(speed);
1259     speed = 0;
1260
1261     if (forward)
1262         dealloc2(forward);
1263     forward = 0;
1264
1265     if (weapontarget)
1266         dealloc2(weapontarget);
1267     weapontarget = 0;
1268
1269     if (label)
1270         dealloc2(label);
1271     label = 0;
1272
1273     joints = 0;
1274 }
1275
1276 Skeleton::Skeleton()
1277 {
1278     num_joints = 0;
1279
1280     num_muscles = 0;
1281
1282     selected = 0;
1283
1284     memset(forwardjoints, 0, sizeof(forwardjoints));
1285     // XYZ forward;
1286
1287     id = 0;
1288
1289     memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1290     // XYZ lowforward;
1291
1292     // XYZ specialforward[5];
1293     memset(jointlabels, 0, sizeof(jointlabels));
1294
1295     // Model model[7];
1296     // Model modellow;
1297     // Model modelclothes;
1298     num_models = 0;
1299
1300     // Model drawmodel;
1301     // Model drawmodellow;
1302     // Model drawmodelclothes;
1303
1304     clothes = 0;
1305     spinny = 0;
1306
1307     memset(skinText, 0, sizeof(skinText));
1308     skinsize = 0;
1309
1310     checkdelay = 0;
1311
1312     longdead = 0;
1313     broken = 0;
1314
1315     free = 0;
1316     oldfree = 0;
1317     freetime = 0;
1318     freefall = 0;
1319
1320     joints = 0;
1321     muscles = 0;
1322 }
1323
1324 Skeleton::~Skeleton()
1325 {
1326     if (muscles) {
1327         delete [] muscles;
1328     }
1329     muscles = 0;
1330
1331     if (joints) {
1332         delete [] joints;
1333     }
1334     joints = 0;
1335 }
1336
1337 Muscle::Muscle()
1338 {
1339     vertices = 0;
1340     verticeslow = 0;
1341     verticesclothes = 0;
1342
1343     numvertices = 0;
1344     numverticeslow = 0;
1345     numverticesclothes = 0;
1346     length = 0;
1347     targetlength = 0;
1348     parent1 = 0;
1349     parent2 = 0;
1350     maxlength = 0;
1351     minlength = 0;
1352     type = 0;
1353     visible = 0;
1354     rotate1 = 0, rotate2 = 0, rotate3 = 0;
1355     lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1356     oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1357     newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1358
1359     strength = 0;
1360 }
1361
1362 Muscle::~Muscle()
1363 {
1364     dealloc2(vertices);
1365     dealloc2(verticeslow);
1366     dealloc2(verticesclothes);
1367 }
1368
1369 Animation & Animation::operator = (const Animation & ani)
1370 {
1371     int i = 0;
1372
1373     bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1374
1375     if (allocate)
1376         deallocate();
1377
1378     numframes = ani.numframes;
1379     height = ani.height;
1380     attack = ani.attack;
1381     joints = ani.joints;
1382     weapontargetnum = ani.weapontargetnum;
1383     offset = ani.offset;
1384
1385     if (allocate)
1386         position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1387     for (i = 0; i < ani.joints; i++) {
1388         if (allocate)
1389             position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1390         memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1391     }
1392
1393     if (allocate)
1394         twist = (float**)malloc(sizeof(float*)*ani.joints);
1395     for (i = 0; i < ani.joints; i++) {
1396         if (allocate)
1397             twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1398         memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1399     }
1400
1401     if (allocate)
1402         twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1403     for (i = 0; i < ani.joints; i++) {
1404         if (allocate)
1405             twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1406         memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1407     }
1408
1409     if (allocate)
1410         speed = (float*)malloc(sizeof(float) * ani.numframes);
1411     memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1412
1413     if (allocate)
1414         onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1415     for (i = 0; i < ani.joints; i++) {
1416         if (allocate)
1417             onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1418         memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1419     }
1420
1421     if (allocate)
1422         forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1423     memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1424
1425     if (allocate)
1426         weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1427     memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1428
1429     if (allocate)
1430         label = (int*)malloc(sizeof(int) * ani.numframes);
1431     memcpy(label, ani.label, sizeof(int)*ani.numframes);
1432
1433     return (*this);
1434 }
1435
1436
1437
1438
1439 #if 0
1440
1441 // the following functions are not used anywhere
1442
1443 /* EFFECT
1444  * sets forward, lowforward, specialforward[]
1445  *
1446  * USES:
1447  * NONE
1448  */
1449 void Skeleton::FindForwardsfirst()
1450 {
1451     //Find forward vectors
1452     CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1453     Normalise(&forward);
1454
1455     CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1456     Normalise(&lowforward);
1457
1458     //Special forwards
1459     specialforward[0] = forward;
1460     specialforward[1] = forward;
1461     specialforward[2] = forward;
1462     specialforward[3] = forward;
1463     specialforward[4] = forward;
1464
1465 }
1466
1467 /* EFFECT
1468  *
1469  * USES:
1470  * NONE
1471  */
1472 void Skeleton::Draw(int muscleview)
1473 {
1474     static float jointcolor[4];
1475
1476     if (muscleview != 2) {
1477         jointcolor[0] = 0;
1478         jointcolor[1] = 0;
1479         jointcolor[2] = .5;
1480         jointcolor[3] = 1;
1481     }
1482
1483     if (muscleview == 2) {
1484         jointcolor[0] = 0;
1485         jointcolor[1] = 0;
1486         jointcolor[2] = 0;
1487         jointcolor[3] = .5;
1488     }
1489     //Calc motionblur-ness
1490     for (int i = 0; i < num_joints; i++) {
1491         joints[i].oldposition = joints[i].position;
1492         joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1493         if (joints[i].blurred < 1)
1494             joints[i].blurred = 1;
1495     }
1496
1497     //Do Motionblur
1498     glDepthMask(0);
1499     glEnable(GL_BLEND);
1500     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1501     glBegin(GL_QUADS);
1502     for (int i = 0; i < num_joints; i++) {
1503         if (joints[i].hasparent) {
1504             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1505             glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1506             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1507             glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1508             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1509             glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1510             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1511             glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1512         }
1513     }
1514     for (int i = 0; i < num_muscles; i++) {
1515         if (muscles[i].type == boneconnect) {
1516             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1517             glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1518             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1519             glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1520             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1521             glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1522             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1523             glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1524         }
1525     }
1526     glEnd();
1527
1528     glBegin(GL_LINES);
1529     for (int i = 0; i < num_joints; i++) {
1530         if (joints[i].hasparent) {
1531             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1532             glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1533             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1534             glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1535         }
1536     }
1537     for (int i = 0; i < num_muscles; i++) {
1538         if (muscles[i].type == boneconnect) {
1539             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1540             glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1541             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1542             glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1543         }
1544     }
1545     glColor3f(.6, .6, 0);
1546     if (muscleview == 1)
1547         for (int i = 0; i < num_muscles; i++) {
1548             if (muscles[i].type != boneconnect) {
1549                 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1550                 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1551             }
1552         }
1553     glEnd();
1554
1555     if (muscleview != 2) {
1556         glPointSize(3);
1557         glBegin(GL_POINTS);
1558         for (int i = 0; i < num_joints; i++) {
1559             if (i != selected)
1560                 glColor4f(0, 0, .5, 1);
1561             if (i == selected)
1562                 glColor4f(1, 1, 0, 1);
1563             if (joints[i].locked && i != selected)
1564                 glColor4f(1, 0, 0, 1);
1565             glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1566         }
1567         glEnd();
1568     }
1569
1570     //Set old position to current position
1571     if (muscleview == 2)
1572         for (int i = 0; i < num_joints; i++) {
1573             joints[i].oldposition = joints[i].position;
1574         }
1575     glDepthMask(1);
1576 }
1577
1578 /* EFFECT
1579  *
1580  * USES:
1581  * NONE
1582  */
1583 void Skeleton::AddJoint(float x, float y, float z, int which)
1584 {
1585     if (num_joints < max_joints - 1) {
1586         joints[num_joints].velocity = 0;
1587         joints[num_joints].position.x = x;
1588         joints[num_joints].position.y = y;
1589         joints[num_joints].position.z = z;
1590         joints[num_joints].mass = 1;
1591         joints[num_joints].locked = 0;
1592
1593         joints[num_joints].hasparent = 0;
1594         num_joints++;
1595         if (which < num_joints && which >= 0)
1596             AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1597     }
1598 }
1599
1600 /* EFFECT
1601  *
1602  * USES:
1603  * NONE
1604  */
1605 void Skeleton::DeleteJoint(int whichjoint)
1606 {
1607     if (whichjoint < num_joints && whichjoint >= 0) {
1608         joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1609         joints[whichjoint].position = joints[num_joints - 1].position;
1610         joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1611         joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1612         joints[whichjoint].parent = joints[num_joints - 1].parent;
1613         joints[whichjoint].length = joints[num_joints - 1].length;
1614         joints[whichjoint].locked = joints[num_joints - 1].locked;
1615         joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1616         joints[whichjoint].visible = joints[num_joints - 1].visible;
1617
1618         for (int i = 0; i < num_muscles; i++) {
1619             while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1620             while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1621         }
1622         for (int i = 0; i < num_muscles; i++) {
1623             while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1624             while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1625         }
1626         for (int i = 0; i < num_joints; i++) {
1627             if (joints[i].parent == &joints[whichjoint])
1628                 joints[i].hasparent = 0;
1629         }
1630         for (int i = 0; i < num_joints; i++) {
1631             if (joints[i].parent == &joints[num_joints - 1])
1632                 joints[i].parent = &joints[whichjoint];
1633         }
1634
1635         num_joints--;
1636     }
1637 }
1638
1639 /* EFFECT
1640  *
1641  * USES:
1642  * Skeleton::DeleteJoint - UNUSED
1643  */
1644 void Skeleton::DeleteMuscle(int whichmuscle)
1645 {
1646     if (whichmuscle < num_muscles) {
1647         muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1648         muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1649         muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1650         muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1651         muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1652         muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1653         muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1654         muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1655         muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1656
1657         num_muscles--;
1658     }
1659 }
1660
1661 /* EFFECT
1662  *
1663  * USES:
1664  * NONE
1665  */
1666 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1667 {
1668     if (whichjoint < num_joints) {
1669         joints[whichjoint].velocity = 0;
1670         joints[whichjoint].position.x = x;
1671         joints[whichjoint].position.y = y;
1672         joints[whichjoint].position.z = z;
1673
1674         if (which >= num_joints || which < 0)
1675             joints[whichjoint].hasparent = 0;
1676         if (which < num_joints && which >= 0) {
1677             joints[whichjoint].parent = &joints[which];
1678             joints[whichjoint].hasparent = 1;
1679             joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1680         }
1681     }
1682 }
1683
1684 /* EFFECT
1685  *
1686  * USES:
1687  * Skeleton::AddJoint - UNUSED
1688  */
1689 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1690 {
1691     const int max_muscles = 100; // FIXME: Probably can be dropped
1692     if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1693         muscles[num_muscles].parent1 = &joints[attach1];
1694         muscles[num_muscles].parent2 = &joints[attach2];
1695         muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1696         muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1697         muscles[num_muscles].strength = .7;
1698         muscles[num_muscles].type = type;
1699         muscles[num_muscles].minlength = minlength;
1700         muscles[num_muscles].maxlength = maxlength;
1701
1702         num_muscles++;
1703     }
1704 }
1705
1706 /* EFFECT
1707  *
1708  * USES:
1709  * NONE
1710  */
1711 void Skeleton::MusclesSet()
1712 {
1713     for (int i = 0; i < num_muscles; i++) {
1714         muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1715     }
1716 }
1717
1718 /* EFFECT
1719  *
1720  * USES:
1721  * NONE
1722  */
1723 void Skeleton::DoBalance()
1724 {
1725     /*XYZ newpoint;
1726     newpoint=joints[0].position;
1727     newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1728     newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1729     joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1730     //Move child point to within certain distance of parent point
1731     joints[0].position=newpoint;
1732
1733     MusclesSet();*/
1734 }
1735
1736 #endif
1737