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