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