2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
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.
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.
15 See the GNU General Public License for more details.
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.
22 /**> HEADER FILES <**/
25 #include "openal_wrapper.h"
26 #include "Animation.h"
28 extern float multiplier;
30 extern Skeleton testskeleton;
31 extern Terrain terrain;
32 extern int channels[100];
33 extern Objects objects;
34 extern int environment;
35 extern float terraindetail;
36 extern float camerashake;
39 extern XYZ envsound[30];
40 extern float envsoundvol[30];
41 extern int numenvsounds;
42 extern float envsoundlife[30];
44 extern float bonustime;
45 extern int tutoriallevel;
47 extern int whichjointstartarray[26];
48 extern int whichjointendarray[26];
51 extern bool visibleloading;
52 extern "C" void PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused);
54 void dealloc2(void* param){
59 void Muscle::DoConstraint(bool spinny)
63 static XYZ newpoint1,newpoint2;
65 static float oldlength;
66 static float relaxlength;
70 if(type!=boneconnect)relaxlength=findDistance(&parent1->position,&parent2->position);
72 if(type==boneconnect)strength=1;
73 if(type==constraint)strength=0;
75 if(strength<0)strength=0;
76 if(strength>1)strength=1;
78 length-=(length-relaxlength)*(1-strength)*multiplier*10000;
79 length-=(length-targetlength)*(strength)*multiplier*10000;
80 if(strength==0)length=relaxlength;
82 if((relaxlength-length>0&&relaxlength-oldlength<0)||(relaxlength-length<0&&relaxlength-oldlength>0))length=relaxlength;
85 if(length<minlength)length=minlength;
86 if(length>maxlength)length=maxlength;
90 if(length<minlength*.6)length=minlength*.6;
91 if(length>maxlength*1.4)length=maxlength*1.4;
94 if(length==relaxlength)return;
97 midp=(parent1->position*parent1->mass+parent2->position*parent2->mass)/(parent1->mass+parent2->mass);
98 //Find vector from midpoint to second vector
99 vel=parent2->position-midp;
100 //Change to unit vector
102 //Apply velocity change
103 newpoint1=midp-vel*length*(parent2->mass/(parent1->mass+parent2->mass));
104 newpoint2=midp+vel*length*(parent1->mass/(parent1->mass+parent2->mass));
106 parent1->velocity=parent1->velocity+(newpoint1-parent1->position)/multiplier/4;
107 parent2->velocity=parent2->velocity+(newpoint2-parent2->position)/multiplier/4;
111 parent1->velocity=parent1->velocity+(newpoint1-parent1->position);
112 parent2->velocity=parent2->velocity+(newpoint2-parent2->position);
114 //Move child point to within certain distance of parent point
115 parent1->position=newpoint1;
116 parent2->position=newpoint2;
119 void Skeleton::FindForwardsfirst()
121 //Find forward vectors
122 CrossProduct(joints[forwardjoints[1]].position-joints[forwardjoints[0]].position,joints[forwardjoints[2]].position-joints[forwardjoints[0]].position,&forward);
125 CrossProduct(joints[lowforwardjoints[1]].position-joints[lowforwardjoints[0]].position,joints[lowforwardjoints[2]].position-joints[lowforwardjoints[0]].position,&lowforward);
126 Normalise(&lowforward);
129 specialforward[0]=forward;
130 specialforward[1]=forward;
131 specialforward[2]=forward;
132 specialforward[3]=forward;
133 specialforward[4]=forward;
136 void Skeleton::FindForwards()
138 //Find forward vectors
139 CrossProduct(joints[forwardjoints[1]].position-joints[forwardjoints[0]].position,joints[forwardjoints[2]].position-joints[forwardjoints[0]].position,&forward);
142 CrossProduct(joints[lowforwardjoints[1]].position-joints[lowforwardjoints[0]].position,joints[lowforwardjoints[2]].position-joints[lowforwardjoints[0]].position,&lowforward);
143 Normalise(&lowforward);
146 specialforward[0]=forward;
148 specialforward[1]=joints[jointlabels[rightshoulder]].position+joints[jointlabels[rightwrist]].position;
149 specialforward[1]=joints[jointlabels[rightelbow]].position-specialforward[1]/2;
150 specialforward[1]+=forward*.4;
151 Normalise(&specialforward[1]);
152 specialforward[2]=joints[jointlabels[leftshoulder]].position+joints[jointlabels[leftwrist]].position;
153 specialforward[2]=joints[jointlabels[leftelbow]].position-specialforward[2]/2;
154 specialforward[2]+=forward*.4;
155 Normalise(&specialforward[2]);
157 specialforward[3]=joints[jointlabels[righthip]].position+joints[jointlabels[rightankle]].position;
158 specialforward[3]=specialforward[3]/2-joints[jointlabels[rightknee]].position;
159 specialforward[3]+=lowforward*.4;
160 Normalise(&specialforward[3]);
161 specialforward[4]=joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position;
162 specialforward[4]=specialforward[4]/2-joints[jointlabels[leftknee]].position;
163 specialforward[4]+=lowforward*.4;
164 Normalise(&specialforward[4]);
167 float Skeleton::DoConstraints(XYZ *coords,float *scale)
169 static float friction=1.5;
170 static float elasticity=.3;
171 static XYZ bounceness;
172 static XYZ oldpos[100];
173 static int numrepeats=3;
174 static float groundlevel=.15;
175 static float soundvolume;
176 static int i,j,k,l,m;
177 static XYZ temp,start,end;
178 static XYZ terrainnormal;
182 //static int whichjointstart,whichjointend;
183 static float distance;
184 static float frictionness;
185 static XYZ terrainlight;
186 static int whichpatchx;
187 static int whichpatchz;
190 static float tempmult;
191 static bool breaking;
197 freetime+=multiplier;
199 whichpatchx=coords->x/(terrain.size/subdivision*terrain.scale*terraindetail);
200 whichpatchz=coords->z/(terrain.size/subdivision*terrain.scale*terraindetail);
202 terrainlight=*coords;
203 objects.SphereCheckPossible(&terrainlight, 1);
205 for(i=0; i<num_joints; i++){
206 oldpos[i]=joints[i].position;
210 for(i=0; i<num_joints; i++){
211 //if(!isnormal(joints[i].velocity.x)||!isnormal(joints[i].velocity.y)||!isnormal(joints[i].velocity.z))joints[i].velocity=0;
212 joints[i].position=joints[i].position+joints[i].velocity*multiplier;
214 if(joints[i].label==head)groundlevel=.8;
215 if(joints[i].label==righthand||joints[i].label==rightwrist||joints[i].label==rightelbow)groundlevel=.2;
216 if(joints[i].label==lefthand||joints[i].label==leftwrist||joints[i].label==leftelbow)groundlevel=.2;
217 joints[i].position.y-=groundlevel;
218 //if(!joints[i].locked&&!broken)joints[i].velocity+=joints[i].velchange*multiplier*10*(500-longdead)/500;
219 joints[i].oldvelocity=joints[i].velocity;
222 //multiplier/=numrepeats;
223 for(j=0; j<numrepeats; j++){
224 if(!joints[jointlabels[rightknee]].locked&&!joints[jointlabels[righthip]].locked){
225 temp=joints[jointlabels[rightknee]].position-(joints[jointlabels[righthip]].position+joints[jointlabels[rightankle]].position)/2;
226 while(normaldotproduct(temp,lowforward)>-.1&&!sphere_line_intersection(&joints[jointlabels[righthip]].position,&joints[jointlabels[rightankle]].position,&joints[jointlabels[rightknee]].position,&r)){
227 joints[jointlabels[rightknee]].position-=lowforward*.05;
228 if(spinny)joints[jointlabels[rightknee]].velocity-=lowforward*.05/multiplier/4;
229 else joints[jointlabels[rightknee]].velocity-=lowforward*.05;
230 joints[jointlabels[rightankle]].position+=lowforward*.025;
231 if(spinny)joints[jointlabels[rightankle]].velocity+=lowforward*.025/multiplier/4;
232 else joints[jointlabels[rightankle]].velocity+=lowforward*.25;
233 joints[jointlabels[righthip]].position+=lowforward*.025;
234 if(spinny)joints[jointlabels[righthip]].velocity+=lowforward*.025/multiplier/4;
235 else joints[jointlabels[righthip]].velocity+=lowforward*.025;
236 temp=joints[jointlabels[rightknee]].position-(joints[jointlabels[righthip]].position+joints[jointlabels[rightankle]].position)/2;
239 if(!joints[jointlabels[leftknee]].locked&&!joints[jointlabels[righthip]].locked){
240 temp=joints[jointlabels[leftknee]].position-(joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position)/2;
241 while(normaldotproduct(temp,lowforward)>-.1&&!sphere_line_intersection(&joints[jointlabels[lefthip]].position,&joints[jointlabels[leftankle]].position,&joints[jointlabels[leftknee]].position,&r)){
242 joints[jointlabels[leftknee]].position-=lowforward*.05;
243 if(spinny)joints[jointlabels[leftknee]].velocity-=lowforward*.05/multiplier/4;
244 else joints[jointlabels[leftknee]].velocity-=lowforward*.05;
245 joints[jointlabels[leftankle]].position+=lowforward*.025;
246 if(spinny)joints[jointlabels[leftankle]].velocity+=lowforward*.025/multiplier/4;
247 else joints[jointlabels[leftankle]].velocity+=lowforward*.25;
248 joints[jointlabels[lefthip]].position+=lowforward*.025;
249 if(spinny)joints[jointlabels[lefthip]].velocity+=lowforward*.025/multiplier/4;
250 else joints[jointlabels[lefthip]].velocity+=lowforward*.025;
251 temp=joints[jointlabels[leftknee]].position-(joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position)/2;
255 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
256 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
257 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
258 if(k<objects.numobjects&&k>=0)
259 if(objects.possible[k]){
260 temp=joints[jointlabels[head]].position*(*scale)+*coords;
261 if(objects.model[k].SphereCheck(&temp, 0.06, &start, &objects.position[k], &objects.rotation[k])!=-1){
262 //temp=(joints[jointlabels[head]].position*(*scale)+*coords)-start;
264 //joints[jointlabels[head]].position=((temp*.2+start)-*coords)/(*scale);
265 joints[jointlabels[head]].position=(temp-*coords)/(*scale);
273 startheadpos=joints[jointlabels[head]].position;
275 headpos=joints[jointlabels[head]].position+(joints[jointlabels[head]].position-joints[jointlabels[neck]].position);
276 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
277 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
278 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
279 if(k<objects.numobjects&&k>=0)
280 if(objects.possible[k]){
281 friction=objects.friction[k];
282 start=joints[jointlabels[head]].position*(*scale)+*coords;
283 end=(headpos)*(*scale)+*coords;
284 whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
286 if(joints[jointlabels[head]].label==groin&&!joints[jointlabels[head]].locked&&joints[jointlabels[head]].delay<=0){
287 joints[jointlabels[head]].locked=1;
288 joints[jointlabels[head]].delay=1;
289 static float gLoc[3];
291 gLoc[0]=headpos.x*(*scale)+coords->x;
292 gLoc[1]=headpos.y*(*scale)+coords->y;
293 gLoc[2]=headpos.z*(*scale)+coords->z;
294 vel[0]=joints[jointlabels[head]].velocity.x;
295 vel[1]=joints[jointlabels[head]].velocity.y;
296 vel[2]=joints[jointlabels[head]].velocity.z;
297 PlaySoundEx( landsound1, samp[landsound1], NULL, true);
298 OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
299 OPENAL_SetVolume(channels[landsound1], 128);
300 OPENAL_SetPaused(channels[landsound1], false);
305 if(joints[jointlabels[head]].label==head&&!joints[jointlabels[head]].locked&&joints[jointlabels[head]].delay<=0){
306 joints[jointlabels[head]].locked=1;
307 joints[jointlabels[head]].delay=1;
308 static float gLoc[3];
310 gLoc[0]=headpos.x*(*scale)+coords->x;
311 gLoc[1]=headpos.y*(*scale)+coords->y;
312 gLoc[2]=headpos.z*(*scale)+coords->z;
313 vel[0]=joints[jointlabels[head]].velocity.x;
314 vel[1]=joints[jointlabels[head]].velocity.y;
315 vel[2]=joints[jointlabels[head]].velocity.z;
316 PlaySoundEx( landsound2, samp[landsound2], NULL, true);
317 OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
318 OPENAL_SetVolume(channels[landsound2], 128);
319 OPENAL_SetPaused(channels[landsound2], false);
322 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
323 if(terrainnormal.y>.8)freefall=0;
324 bounceness=terrainnormal*findLength(&joints[jointlabels[head]].velocity)*(abs(normaldotproduct(joints[jointlabels[head]].velocity,terrainnormal)));
325 if(findLengthfast(&joints[jointlabels[head]].velocity)>findLengthfast(&joints[jointlabels[head]].oldvelocity)){
327 joints[jointlabels[head]].velocity=joints[jointlabels[head]].oldvelocity;
329 if(findLengthfast(&bounceness)>4000&&breaking){
330 objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
331 Sprite::MakeSprite(cloudsprite, headpos*(*scale)+*coords,joints[jointlabels[head]].velocity*.06, 1,1,1, 4, .2);
335 static float gLoc[3];
337 gLoc[0]=headpos.x*(*scale)+coords->x;
338 gLoc[1]=headpos.y*(*scale)+coords->y;
339 gLoc[2]=headpos.z*(*scale)+coords->z;
340 vel[0]=joints[jointlabels[head]].velocity.x;
341 vel[1]=joints[jointlabels[head]].velocity.y;
342 vel[2]=joints[jointlabels[head]].velocity.z;
343 PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
344 OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
345 OPENAL_SetVolume(channels[breaksound2], 300);
346 OPENAL_SetPaused(channels[breaksound2], false);
348 envsound[numenvsounds]=*coords;
349 envsoundvol[numenvsounds]=64;
350 envsoundlife[numenvsounds]=.4;
353 if(objects.type[k]==treetrunktype){
354 objects.rotx[k]+=joints[jointlabels[head]].velocity.x*multiplier*.4;
355 objects.roty[k]+=joints[jointlabels[head]].velocity.z*multiplier*.4;
356 objects.rotx[k+1]+=joints[jointlabels[head]].velocity.x*multiplier*.4;
357 objects.roty[k+1]+=joints[jointlabels[head]].velocity.z*multiplier*.4;
359 if(!joints[jointlabels[head]].locked)damage+=findLengthfast(&bounceness)/2500;
360 ReflectVector(&joints[jointlabels[head]].velocity,&terrainnormal);
361 frictionness=abs(normaldotproduct(joints[jointlabels[head]].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[jointlabels[head]].velocity);
362 joints[jointlabels[head]].velocity-=bounceness;
363 if(1-friction*frictionness>0)joints[jointlabels[head]].velocity*=1-friction*frictionness;
364 else joints[jointlabels[head]].velocity=0;
365 if(findLengthfast(&bounceness)>2500){
366 Normalise(&bounceness);
367 bounceness=bounceness*50;
369 joints[jointlabels[head]].velocity+=bounceness*elasticity;
372 if(!joints[jointlabels[head]].locked)
373 if(findLengthfast(&joints[jointlabels[head]].velocity)<1){
374 joints[jointlabels[head]].locked=1;
375 //joints[jointlabels[head]].velocity*=3;
377 if(findLengthfast(&bounceness)>500)Sprite::MakeSprite(cloudsprite, headpos*(*scale)+*coords,joints[jointlabels[head]].velocity*.06, 1,1,1, .5, .2);
378 joints[jointlabels[head]].position=(temp-*coords)/(*scale)+(startheadpos-headpos)+terrainnormal*.005;
379 if(longdead>100)broken=1;
385 for(i=0; i<num_joints; i++){
386 //joints[i].delay-=multiplier/1.5;
388 if(!spinny)if(findLengthfast(&joints[i].velocity)>320)joints[i].locked=0;
389 if(spinny)if(findLengthfast(&joints[i].velocity)>600)joints[i].locked=0;
390 if(joints[i].delay>0){
392 for(j=0;j<num_joints;j++){
393 if(joints[j].locked)freely=0;
395 if(freely)joints[i].delay-=multiplier*3;
397 //if(joints[i].delay>0)
398 //if(findLengthfast(&joints[i].velocity)>700&&joints[i].label!=head)joints[i].delay-=multiplier;
402 for(i=0; i<num_muscles; i++){
404 //muscles[i].DoConstraint(broken);
405 muscles[i].DoConstraint(spinny);
408 for(i=0; i<num_joints; i++){
409 //joints[i].delay-=multiplier/1.5;
413 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){
416 if(joints[i].label==groin&&!joints[i].locked&&joints[i].delay<=0){
419 static float gLoc[3];
421 gLoc[0]=joints[i].position.x*(*scale)+coords->x;
422 gLoc[1]=joints[i].position.y*(*scale)+coords->y;
423 gLoc[2]=joints[i].position.z*(*scale)+coords->z;
424 vel[0]=joints[i].velocity.x;
425 vel[1]=joints[i].velocity.y;
426 vel[2]=joints[i].velocity.z;
427 if(tutoriallevel!=1||id==0){
428 PlaySoundEx( landsound1, samp[landsound1], NULL, true);
429 OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
430 OPENAL_SetVolume(channels[landsound1], 128);
431 OPENAL_SetPaused(channels[landsound1], false);
436 if(joints[i].label==head&&!joints[i].locked&&joints[i].delay<=0){
439 static float gLoc[3];
441 gLoc[0]=joints[i].position.x*(*scale)+coords->x;
442 gLoc[1]=joints[i].position.y*(*scale)+coords->y;
443 gLoc[2]=joints[i].position.z*(*scale)+coords->z;
444 vel[0]=joints[i].velocity.x;
445 vel[1]=joints[i].velocity.y;
446 vel[2]=joints[i].velocity.z;
447 if(tutoriallevel!=1||id==0){
448 PlaySoundEx( landsound2, samp[landsound2], NULL, true);
449 OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
450 OPENAL_SetVolume(channels[landsound2], 128);
451 OPENAL_SetPaused(channels[landsound2], false);
455 terrainnormal=terrain.getNormal(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
456 ReflectVector(&joints[i].velocity,&terrainnormal);
457 bounceness=terrainnormal*findLength(&joints[i].velocity)*(abs(normaldotproduct(joints[i].velocity,terrainnormal)));
458 if(!joints[i].locked)damage+=findLengthfast(&bounceness)/4000;
459 if(findLengthfast(&joints[i].velocity)<findLengthfast(&bounceness))bounceness=0;
460 frictionness=abs(normaldotproduct(joints[i].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[i].velocity);
461 joints[i].velocity-=bounceness;
462 if(1-friction*frictionness>0)joints[i].velocity*=1-friction*frictionness;
463 else joints[i].velocity=0;
465 if(tutoriallevel!=1||id==0)
466 if(findLengthfast(&bounceness)>8000&&breaking){
467 objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
468 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 4, .2);
469 //Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 1, .2);
473 static float gLoc[3];
475 gLoc[0]=joints[i].position.x*(*scale)+coords->x;
476 gLoc[1]=joints[i].position.y*(*scale)+coords->y;
477 gLoc[2]=joints[i].position.z*(*scale)+coords->z;
478 vel[0]=joints[i].velocity.x;
479 vel[1]=joints[i].velocity.y;
480 vel[2]=joints[i].velocity.z;
481 PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
482 OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
483 OPENAL_SetVolume(channels[breaksound2], 300);
484 OPENAL_SetPaused(channels[breaksound2], false);
486 envsound[numenvsounds]=*coords;
487 envsoundvol[numenvsounds]=64;
488 envsoundlife[numenvsounds]=.4;
492 if(findLengthfast(&bounceness)>2500){
493 Normalise(&bounceness);
494 bounceness=bounceness*50;
497 joints[i].velocity+=bounceness*elasticity;
499 if(findLengthfast(&joints[i].velocity)>findLengthfast(&joints[i].oldvelocity)){
501 joints[i].velocity=joints[i].oldvelocity;
505 if(joints[i].locked==0)
506 if(findLengthfast(&joints[i].velocity)<1)joints[i].locked=1;
508 if(environment==snowyenvironment&&findLengthfast(&bounceness)>500&&terrain.getOpacity(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)<.2){
509 terrainlight=terrain.getLighting(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
510 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
511 if(detail==2)terrain.MakeDecal(bodyprintdecal, joints[i].position*(*scale)+*coords,.4,.4,0);
513 else if(environment==desertenvironment&&findLengthfast(&bounceness)>500&&terrain.getOpacity(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)<.2){
514 terrainlight=terrain.getLighting(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
515 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);
518 else if(environment==grassyenvironment&&findLengthfast(&bounceness)>500&&terrain.getOpacity(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)<.2){
519 terrainlight=terrain.getLighting(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
520 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);
522 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);
525 joints[i].position.y=(terrain.getHeight(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)+groundlevel-coords->y)/(*scale);
526 if(longdead>100)broken=1;
528 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
529 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
530 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
531 if(k<objects.numobjects&&k>=0)
532 if(objects.possible[k]){
533 friction=objects.friction[k];
534 start=joints[i].realoldposition;
535 end=joints[i].position*(*scale)+*coords;
536 whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
538 if(joints[i].label==groin&&!joints[i].locked&&joints[i].delay<=0){
541 static float gLoc[3];
543 gLoc[0]=joints[i].position.x*(*scale)+coords->x;
544 gLoc[1]=joints[i].position.y*(*scale)+coords->y;
545 gLoc[2]=joints[i].position.z*(*scale)+coords->z;
546 vel[0]=joints[i].velocity.x;
547 vel[1]=joints[i].velocity.y;
548 vel[2]=joints[i].velocity.z;
549 if(tutoriallevel!=1||id==0){
550 PlaySoundEx( landsound1, samp[landsound1], NULL, true);
551 OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
552 OPENAL_SetVolume(channels[landsound1], 128);
553 OPENAL_SetPaused(channels[landsound1], false);
558 if(joints[i].label==head&&!joints[i].locked&&joints[i].delay<=0){
561 static float gLoc[3];
563 gLoc[0]=joints[i].position.x*(*scale)+coords->x;
564 gLoc[1]=joints[i].position.y*(*scale)+coords->y;
565 gLoc[2]=joints[i].position.z*(*scale)+coords->z;
566 vel[0]=joints[i].velocity.x;
567 vel[1]=joints[i].velocity.y;
568 vel[2]=joints[i].velocity.z;
569 if(tutoriallevel!=1||id==0){
570 PlaySoundEx( landsound2, samp[landsound2], NULL, true);
571 OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
572 OPENAL_SetVolume(channels[landsound2], 128);
573 OPENAL_SetPaused(channels[landsound2], false);
577 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
578 if(terrainnormal.y>.8)freefall=0;
579 bounceness=terrainnormal*findLength(&joints[i].velocity)*(abs(normaldotproduct(joints[i].velocity,terrainnormal)));
580 if(findLengthfast(&joints[i].velocity)>findLengthfast(&joints[i].oldvelocity)){
582 joints[i].velocity=joints[i].oldvelocity;
584 if(tutoriallevel!=1||id==0)
585 if(findLengthfast(&bounceness)>4000&&breaking){
586 objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
587 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 4, .2);
591 static float gLoc[3];
593 gLoc[0]=joints[i].position.x*(*scale)+coords->x;
594 gLoc[1]=joints[i].position.y*(*scale)+coords->y;
595 gLoc[2]=joints[i].position.z*(*scale)+coords->z;
596 vel[0]=joints[i].velocity.x;
597 vel[1]=joints[i].velocity.y;
598 vel[2]=joints[i].velocity.z;
599 PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
600 OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
601 OPENAL_SetVolume(channels[breaksound2], 300);
602 OPENAL_SetPaused(channels[breaksound2], false);
604 envsound[numenvsounds]=*coords;
605 envsoundvol[numenvsounds]=64;
606 envsoundlife[numenvsounds]=.4;
609 if(objects.type[k]==treetrunktype){
610 //if(objects.rotx[k]==0||objects.roty[k]==0){
614 if(environment==grassyenvironment)howmany=findLength(&joints[i].velocity)*4/10;
615 if(environment==snowyenvironment)howmany=findLength(&joints[i].velocity)*1/10;
616 if(environment!=desertenvironment)
617 for(j=0;j<howmany;j++){
618 tempvel.x=float(abs(Random()%100)-50)/20;
619 tempvel.y=float(abs(Random()%100)-50)/20;
620 tempvel.z=float(abs(Random()%100)-50)/20;
621 pos=objects.position[k];
622 pos.y+=objects.scale[k]*15;
623 pos.x+=float(abs(Random()%100)-50)/100*objects.scale[k]*5;
624 pos.y+=float(abs(Random()%100)-50)/100*objects.scale[k]*15;
625 pos.z+=float(abs(Random()%100)-50)/100*objects.scale[k]*5;
626 Sprite::MakeSprite(splintersprite, pos,tempvel*.5, 165/255+float(abs(Random()%100)-50)/400,0,0, .2+float(abs(Random()%100)-50)/1300, 1);
627 Sprite::special[Sprite::numsprites-1]=1;
629 objects.rotx[k]+=joints[i].velocity.x*multiplier*.4;
630 objects.roty[k]+=joints[i].velocity.z*multiplier*.4;
631 objects.rotx[k+1]+=joints[i].velocity.x*multiplier*.4;
632 objects.roty[k+1]+=joints[i].velocity.z*multiplier*.4;
634 if(!joints[i].locked)damage+=findLengthfast(&bounceness)/2500;
635 ReflectVector(&joints[i].velocity,&terrainnormal);
636 frictionness=abs(normaldotproduct(joints[i].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[i].velocity);
637 joints[i].velocity-=bounceness;
638 if(1-friction*frictionness>0)joints[i].velocity*=1-friction*frictionness;
639 else joints[i].velocity=0;
640 if(findLengthfast(&bounceness)>2500){
641 Normalise(&bounceness);
642 bounceness=bounceness*50;
644 joints[i].velocity+=bounceness*elasticity;
647 if(!joints[i].locked)
648 if(findLengthfast(&joints[i].velocity)<1){
650 //joints[i].velocity*=3;
652 if(findLengthfast(&bounceness)>500)Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, .5, .2);
653 joints[i].position=(temp-*coords)/(*scale)+terrainnormal*.005;
654 if(longdead>100)broken=1;
658 joints[i].realoldposition=joints[i].position*(*scale)+*coords;
664 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
665 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
666 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
667 if(objects.possible[k]){
669 //Make this less stupid
670 start=joints[jointlabels[whichjointstartarray[i]]].position*(*scale)+*coords;
671 end=joints[jointlabels[whichjointendarray[i]]].position*(*scale)+*coords;
672 whichhit=objects.model[k].LineCheckSlidePossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
674 joints[jointlabels[whichjointendarray[i]]].position=(end-*coords)/(*scale);
675 for(j=0; j<num_muscles; j++){
676 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]))
677 muscles[j].DoConstraint(spinny);
684 for(i=0; i<num_joints; i++){
686 if(joints[i].label==head)groundlevel=.8;
687 if(joints[i].label==righthand||joints[i].label==rightwrist||joints[i].label==rightelbow)groundlevel=.2;
688 if(joints[i].label==lefthand||joints[i].label==leftwrist||joints[i].label==leftelbow)groundlevel=.2;
689 joints[i].position.y+=groundlevel;
691 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;
692 if(joints[i].locked){
700 for(i=0; i<num_muscles; i++){
701 if(muscles[i].type==boneconnect)
702 muscles[i].DoConstraint(0);
708 void Skeleton::DoGravity(float *scale)
711 for(i=0; i<num_joints; i++){
712 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);
716 void Skeleton::Draw(int muscleview)
718 static float jointcolor[4];
733 //Calc motionblur-ness
734 for(int i=0; i<num_joints; i++){
735 joints[i].oldposition=joints[i].position;
736 joints[i].blurred=findDistance(&joints[i].position,&joints[i].oldposition)*100;
737 if(joints[i].blurred<1)joints[i].blurred=1;
743 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
745 for(int i=0; i<num_joints; i++){
746 if(joints[i].hasparent){
747 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].blurred);
748 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
749 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].parent->blurred);
750 glVertex3f(joints[i].parent->position.x,joints[i].parent->position.y,joints[i].parent->position.z);
751 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].parent->blurred);
752 glVertex3f(joints[i].parent->oldposition.x,joints[i].parent->oldposition.y,joints[i].parent->oldposition.z);
753 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].blurred);
754 glVertex3f(joints[i].oldposition.x,joints[i].oldposition.y,joints[i].oldposition.z);
757 for(int i=0; i<num_muscles; i++){
758 if(muscles[i].type==boneconnect){
759 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
760 glVertex3f(muscles[i].parent1->position.x,muscles[i].parent1->position.y,muscles[i].parent1->position.z);
761 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
762 glVertex3f(muscles[i].parent2->position.x,muscles[i].parent2->position.y,muscles[i].parent2->position.z);
763 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
764 glVertex3f(muscles[i].parent2->oldposition.x,muscles[i].parent2->oldposition.y,muscles[i].parent2->oldposition.z);
765 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent1->blurred);
766 glVertex3f(muscles[i].parent1->oldposition.x,muscles[i].parent1->oldposition.y,muscles[i].parent1->oldposition.z);
772 for(int i=0; i<num_joints; i++){
773 if(joints[i].hasparent){
774 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].blurred);
775 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
776 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].parent->blurred);
777 glVertex3f(joints[i].parent->position.x,joints[i].parent->position.y,joints[i].parent->position.z);
780 /*for(int i=0; i<num_joints; i++){
781 if(joints[i].hasparent){
782 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
783 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
784 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
785 glVertex3f(joints[i].position.x+forward.x,joints[i].position.y+forward.y,joints[i].position.z+forward.z);
788 for(int i=0; i<num_muscles; i++){
789 if(muscles[i].type==boneconnect){
790 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent1->blurred);
791 glVertex3f(muscles[i].parent1->position.x,muscles[i].parent1->position.y,muscles[i].parent1->position.z);
792 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
793 glVertex3f(muscles[i].parent2->position.x,muscles[i].parent2->position.y,muscles[i].parent2->position.z);
798 for(int i=0; i<num_muscles; i++){
799 if(muscles[i].type!=boneconnect){
800 glVertex3f(muscles[i].parent1->position.x,muscles[i].parent1->position.y,muscles[i].parent1->position.z);
801 glVertex3f(muscles[i].parent2->position.x,muscles[i].parent2->position.y,muscles[i].parent2->position.z);
809 for(int i=0; i<num_joints; i++){
810 if(i!=selected)glColor4f(0,0,.5,1);
811 if(i==selected)glColor4f(1,1,0,1);
812 if(joints[i].locked&&i!=selected)glColor4f(1,0,0,1);
813 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
818 //Set old position to current position
820 for(int i=0; i<num_joints; i++){
821 joints[i].oldposition=joints[i].position;
826 void Skeleton::AddJoint(float x, float y, float z, int which)
828 if(num_joints<max_joints-1){
829 joints[num_joints].velocity=0;
830 joints[num_joints].position.x=x;
831 joints[num_joints].position.y=y;
832 joints[num_joints].position.z=z;
833 joints[num_joints].mass=1;
834 joints[num_joints].locked=0;
836 /*if(which>=num_joints||which<0)*/joints[num_joints].hasparent=0;
837 /*if(which<num_joints&&which>=0){
838 joints[num_joints].parent=&joints[which];
839 joints[num_joints].hasparent=1;
840 joints[num_joints].length=findDistance(joints[num_joints].position,joints[num_joints].parent->position);
843 if(which<num_joints&&which>=0)AddMuscle(num_joints-1,which,0,10,boneconnect);
847 void Skeleton::DeleteJoint(int whichjoint)
849 if(whichjoint<num_joints&&whichjoint>=0){
850 joints[whichjoint].velocity=joints[num_joints-1].velocity;
851 joints[whichjoint].position=joints[num_joints-1].position;
852 joints[whichjoint].oldposition=joints[num_joints-1].oldposition;
853 joints[whichjoint].hasparent=joints[num_joints-1].hasparent;
854 joints[whichjoint].parent=joints[num_joints-1].parent;
855 joints[whichjoint].length=joints[num_joints-1].length;
856 joints[whichjoint].locked=joints[num_joints-1].locked;
857 joints[whichjoint].modelnum=joints[num_joints-1].modelnum;
858 joints[whichjoint].visible=joints[num_joints-1].visible;
860 for(int i=0;i<num_muscles;i++){
861 while(muscles[i].parent1==&joints[whichjoint]&&i<num_muscles)DeleteMuscle(i);
862 while(muscles[i].parent2==&joints[whichjoint]&&i<num_muscles)DeleteMuscle(i);
864 for(int i=0;i<num_muscles;i++){
865 while(muscles[i].parent1==&joints[num_joints-1]&&i<num_muscles)muscles[i].parent1=&joints[whichjoint];
866 while(muscles[i].parent2==&joints[num_joints-1]&&i<num_muscles)muscles[i].parent2=&joints[whichjoint];
868 for(int i=0;i<num_joints;i++){
869 if(joints[i].parent==&joints[whichjoint])joints[i].hasparent=0;
871 for(int i=0;i<num_joints;i++){
872 if(joints[i].parent==&joints[num_joints-1])joints[i].parent=&joints[whichjoint];
879 void Skeleton::DeleteMuscle(int whichmuscle)
881 if(whichmuscle<num_muscles){
882 muscles[whichmuscle].minlength=muscles[num_muscles-1].minlength;
883 muscles[whichmuscle].maxlength=muscles[num_muscles-1].maxlength;
884 muscles[whichmuscle].strength=muscles[num_muscles-1].strength;
885 muscles[whichmuscle].parent1=muscles[num_muscles-1].parent1;
886 muscles[whichmuscle].parent2=muscles[num_muscles-1].parent2;
887 muscles[whichmuscle].length=muscles[num_muscles-1].length;
888 muscles[whichmuscle].visible=muscles[num_muscles-1].visible;
889 muscles[whichmuscle].type=muscles[num_muscles-1].type;
890 muscles[whichmuscle].targetlength=muscles[num_muscles-1].targetlength;
896 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
898 if(whichjoint<num_joints){
899 joints[whichjoint].velocity=0;
900 joints[whichjoint].position.x=x;
901 joints[whichjoint].position.y=y;
902 joints[whichjoint].position.z=z;
904 if(which>=num_joints||which<0)joints[whichjoint].hasparent=0;
905 if(which<num_joints&&which>=0){
906 joints[whichjoint].parent=&joints[which];
907 joints[whichjoint].hasparent=1;
908 joints[whichjoint].length=findDistance(&joints[whichjoint].position,&joints[whichjoint].parent->position);
913 void Skeleton::AddMuscle(int attach1,int attach2,float minlength,float maxlength,int type)
915 if(num_muscles<max_muscles-1&&attach1<num_joints&&attach1>=0&&attach2<num_joints&&attach2>=0&&attach1!=attach2){
916 muscles[num_muscles].parent1=&joints[attach1];
917 muscles[num_muscles].parent2=&joints[attach2];
918 muscles[num_muscles].length=findDistance(&muscles[num_muscles].parent1->position,&muscles[num_muscles].parent2->position);
919 muscles[num_muscles].targetlength=findDistance(&muscles[num_muscles].parent1->position,&muscles[num_muscles].parent2->position);
920 muscles[num_muscles].strength=.7;
921 muscles[num_muscles].type=type;
922 muscles[num_muscles].minlength=minlength;
923 muscles[num_muscles].maxlength=maxlength;
929 void Skeleton::MusclesSet()
931 for(int i=0;i<num_muscles;i++){
932 muscles[i].length=findDistance(&muscles[i].parent1->position,&muscles[i].parent2->position);
936 void Skeleton::DoBalance()
939 newpoint=joints[0].position;
940 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
941 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
942 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
943 //Move child point to within certain distance of parent point
944 joints[0].position=newpoint;
949 void Skeleton::FindRotationMuscle(int which, int animation)
951 static XYZ temppoint1,temppoint2,tempforward;
952 static float distance;
954 temppoint1=muscles[which].parent1->position;
955 temppoint2=muscles[which].parent2->position;
956 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));
957 if((temppoint1.y-temppoint2.y)<=distance)muscles[which].rotate2=asin((temppoint1.y-temppoint2.y)/distance);
958 if((temppoint1.y-temppoint2.y)>distance)muscles[which].rotate2=asin(1.f);
959 muscles[which].rotate2*=360/6.28;
962 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));
963 if((temppoint1.z-temppoint2.z)<=distance)muscles[which].rotate1=acos((temppoint1.z-temppoint2.z)/distance);
964 if((temppoint1.z-temppoint2.z)>distance)muscles[which].rotate1=acos(1.f);
965 muscles[which].rotate1*=360/6.28;
966 if(temppoint1.x>temppoint2.x)muscles[which].rotate1=360-muscles[which].rotate1;
967 if(!isnormal(muscles[which].rotate1))muscles[which].rotate1=0;
968 if(!isnormal(muscles[which].rotate2))muscles[which].rotate2=0;
970 if(muscles[which].parent1->label==head)tempforward=specialforward[0];
971 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];
972 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];
973 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];
974 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];
975 else if(!muscles[which].parent1->lower)tempforward=forward;
976 else if(muscles[which].parent1->lower)tempforward=lowforward;
978 if(animation==hanganim){
979 if(muscles[which].parent1->label==righthand||muscles[which].parent2->label==righthand){
983 if(muscles[which].parent1->label==lefthand||muscles[which].parent2->label==lefthand){
990 if(muscles[which].parent1->label==rightfoot||muscles[which].parent2->label==rightfoot){
993 if(muscles[which].parent1->label==leftfoot||muscles[which].parent2->label==leftfoot){
999 tempforward=DoRotation(tempforward,0,muscles[which].rotate1-90,0);
1000 tempforward=DoRotation(tempforward,0,0,muscles[which].rotate2-90);
1002 tempforward/=sqrt(tempforward.x*tempforward.x+tempforward.y*tempforward.y+tempforward.z*tempforward.z);
1003 if(tempforward.z<=1&&tempforward.z>=-1)muscles[which].rotate3=acos(0-tempforward.z);
1004 else muscles[which].rotate3=acos(-1.f);
1005 muscles[which].rotate3*=360/6.28;
1006 if(0>tempforward.x)muscles[which].rotate3=360-muscles[which].rotate3;
1007 if(!isnormal(muscles[which].rotate3))muscles[which].rotate3=0;
1010 void Animation::Load(const char *filename, int aheight, int aattack)
1014 static XYZ startoffset,endoffset;
1017 static const char *anim_prefix = ":Data:Animations:";
1022 int len = strlen(anim_prefix) + strlen(filename);
1023 char *buf = new char[len + 1];
1024 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
1025 // Changing the filename into something the OS can understand
1026 char *fixedFN = ConvertFileName(buf);
1029 LOG(std::string("Loading animation...") + fixedFN);
1036 if(visibleloading)pgame->LoadingScreen();
1038 tfile=fopen( fixedFN, "rb" );
1040 funpackf(tfile, "Bi Bi", &numframes, &joints);
1042 for(i = 0; i < joints; i++){
1043 if(position[i])dealloc2(position[i]);
1044 if(twist[i])dealloc2(twist[i]);
1045 if(twist2[i])dealloc2(twist2[i]);
1046 if(onground[i])dealloc2(onground[i]);
1049 if(position)dealloc2(position);
1050 if(twist)dealloc2(twist);
1051 if(twist2)dealloc2(twist2);
1052 if(speed)dealloc2(speed);
1053 if(onground)dealloc2(onground);
1054 if(forward)dealloc2(forward);
1055 if(weapontarget)dealloc2(weapontarget);
1056 if(label)dealloc2(label);*/
1058 position=(XYZ**)malloc(sizeof(XYZ*)*joints);
1059 for(i = 0; i < joints; i++)
1060 position[i] = (XYZ*)malloc(sizeof(XYZ)*numframes);
1062 twist=(float**)malloc(sizeof(float*)*joints);
1063 for(i = 0; i < joints; i++)
1064 twist[i] = (float*)malloc(sizeof(float)*numframes);
1066 twist2=(float**)malloc(sizeof(float*)*joints);
1067 for(i = 0; i < joints; i++)
1068 twist2[i] = (float*)malloc(sizeof(float)*numframes);
1070 speed = (float*)malloc(sizeof(float)*numframes);
1072 onground=(bool**)malloc(sizeof(bool*)*joints);
1073 for(i = 0; i < joints; i++)
1074 onground[i] =(bool*)malloc(sizeof(bool)*numframes);
1076 forward = (XYZ*)malloc(sizeof(XYZ)*numframes);
1077 weapontarget = (XYZ*)malloc(sizeof(XYZ)*numframes);
1078 label = (int*)malloc(sizeof(int)*numframes);
1080 /*position = new XYZ[joints][numframes];
1081 twist = new float[joints][numframes];
1082 twist2 = new float[joints][numframes];
1083 speed = new float[numframes];
1084 onground = new bool[joints][numframes];
1085 forward = new XYZ[numframes];
1086 label = new int[numframes];*/
1088 for(i=0;i<numframes;i++){
1089 for(j=0;j<joints;j++){
1090 funpackf(tfile, "Bf Bf Bf", &position[j][i].x,&position[j][i].y,&position[j][i].z);
1092 for(j=0;j<joints;j++){
1093 funpackf(tfile, "Bf", &twist[j][i]);
1095 for(j=0;j<joints;j++){
1097 funpackf(tfile, "Bb", &uch);
1098 onground[j][i] = (uch != 0);
1100 funpackf(tfile, "Bf", &speed[i]);
1102 for(i=0;i<numframes;i++){
1103 for(j=0;j<joints;j++){
1104 funpackf(tfile, "Bf", &twist2[j][i]);
1107 for(i=0;i<numframes;i++){
1108 funpackf(tfile, "Bf", &label[i]);
1110 funpackf(tfile, "Bi", &weapontargetnum);
1111 for(i=0;i<numframes;i++){
1112 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x,&weapontarget[i].y,&weapontarget[i].z);
1121 for(j=0;j<joints;j++){
1122 if(position[j][0].y<1)
1123 startoffset+=position[j][0];
1124 if(position[j][numframes-1].y<1)
1125 endoffset+=position[j][numframes-1];
1128 startoffset/=howmany;
1135 void Animation::Move(XYZ how)
1137 static int i,j,joints;
1138 for(i=0;i<numframes;i++){
1139 for(j=0;j<joints;j++){
1145 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
1146 const char *modelfilename, const char *model2filename,
1147 const char *model3filename, const char *model4filename,
1148 const char *model5filename, const char *model6filename,
1149 const char *model7filename, const char *modellowfilename,
1150 const char *modelclothesfilename, bool aclothes)
1152 static GLfloat M[16];
1153 static int parentID;
1156 static int i,j,tempmuscle;
1169 for(i=0;i<num_models;i++){
1170 if(i==0)model[i].loadnotex(modelfilename);
1171 if(i==1)model[i].loadnotex(model2filename);
1172 if(i==2)model[i].loadnotex(model3filename);
1173 if(i==3)model[i].loadnotex(model4filename);
1174 if(i==4)model[i].loadnotex(model5filename);
1175 if(i==5)model[i].loadnotex(model6filename);
1176 if(i==6)model[i].loadnotex(model7filename);
1177 model[i].Rotate(180,0,0);
1178 model[i].Scale(.04,.04,.04);
1179 model[i].CalculateNormals(0);
1182 drawmodel.load(modelfilename,0);
1183 drawmodel.Rotate(180,0,0);
1184 drawmodel.Scale(.04,.04,.04);
1185 drawmodel.FlipTexCoords();
1186 if(tutoriallevel==1&&id!=0)drawmodel.UniformTexCoords();
1187 if(tutoriallevel==1&&id!=0)drawmodel.ScaleTexCoords(0.1);
1188 drawmodel.CalculateNormals(0);
1190 modellow.loadnotex(modellowfilename);
1191 modellow.Rotate(180,0,0);
1192 modellow.Scale(.04,.04,.04);
1193 modellow.CalculateNormals(0);
1195 drawmodellow.load(modellowfilename,0);
1196 drawmodellow.Rotate(180,0,0);
1197 drawmodellow.Scale(.04,.04,.04);
1198 drawmodellow.FlipTexCoords();
1199 if(tutoriallevel==1&&id!=0)drawmodellow.UniformTexCoords();
1200 if(tutoriallevel==1&&id!=0)drawmodellow.ScaleTexCoords(0.1);
1201 drawmodellow.CalculateNormals(0);
1204 modelclothes.loadnotex(modelclothesfilename);
1205 modelclothes.Rotate(180,0,0);
1206 modelclothes.Scale(.041,.04,.041);
1207 modelclothes.CalculateNormals(0);
1209 drawmodelclothes.load(modelclothesfilename,0);
1210 drawmodelclothes.Rotate(180,0,0);
1211 drawmodelclothes.Scale(.04,.04,.04);
1212 drawmodelclothes.FlipTexCoords();
1213 drawmodelclothes.CalculateNormals(0);
1216 tfile=fopen( ConvertFileName(filename), "rb" );
1218 funpackf(tfile, "Bi", &num_joints);
1219 //joints.resize(num_joints);
1220 if(joints) delete [] joints; //dealloc2(joints);
1221 joints=(Joint*)new Joint[num_joints]; //malloc(sizeof(Joint)*num_joints);
1223 for(i=0;i<num_joints;i++){
1224 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);
1225 funpackf(tfile, "Bb Bb", &joints[i].hasparent,&joints[i].locked);
1226 funpackf(tfile, "Bi", &joints[i].modelnum);
1227 funpackf(tfile, "Bb Bb", &joints[i].visible,&joints[i].sametwist);
1228 funpackf(tfile, "Bi Bi", &joints[i].label,&joints[i].hasgun);
1229 funpackf(tfile, "Bb", &joints[i].lower);
1230 funpackf(tfile, "Bi", &parentID);
1231 if(joints[i].hasparent)joints[i].parent=&joints[parentID];
1232 joints[i].velocity=0;
1233 joints[i].oldposition=joints[i].position;
1235 tempmuscle=num_muscles;
1236 funpackf(tfile, "Bi", &num_muscles);
1238 if(muscles) delete [] muscles; //dealloc2(muscles);
1239 muscles=(Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
1241 for(i=0;i<num_muscles;i++){
1242 tempmuscle=muscles[i].numvertices;
1243 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);
1244 //muscles[i].vertices.clear();
1245 //muscles[i].vertices.resize(muscles[i].numvertices);
1246 //if(muscles[i].vertices)dealloc2(muscles[i].vertices);
1247 muscles[i].vertices=(int*)malloc(sizeof(int)*muscles[i].numvertices);
1250 for(j=0;j<muscles[i].numvertices-edit;j++){
1251 funpackf(tfile, "Bi", &muscles[i].vertices[j+edit]);
1252 if(muscles[i].vertices[j+edit]>=model[0].vertexNum){
1253 muscles[i].numvertices--;
1257 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
1258 muscles[i].parent1=&joints[parentID];
1259 funpackf(tfile, "Bi", &parentID);
1260 muscles[i].parent2=&joints[parentID];
1263 funpackf(tfile, "Bi", &forwardjoints[j]);
1266 funpackf(tfile, "Bi", &lowforwardjoints[j]);
1268 for(j=0;j<num_muscles;j++){
1269 for(i=0;i<muscles[j].numvertices;i++){
1270 for(int k=0;k<num_models;k++){
1271 if(muscles[j].numvertices&&muscles[j].vertices[i]<model[k].vertexNum)model[k].owner[muscles[j].vertices[i]]=j;
1276 for(i=0;i<num_joints;i++){
1277 joints[i].startpos=joints[i].position;
1279 for(i=0;i<num_muscles;i++){
1280 FindRotationMuscle(i,-1);
1282 for(int k=0;k<num_models;k++){
1283 for(i=0;i<model[k].vertexNum;i++){
1284 model[k].vertex[i]=model[k].vertex[i]-(muscles[model[k].owner[i]].parent1->position+muscles[model[k].owner[i]].parent2->position)/2;
1285 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1288 glRotatef(muscles[model[k].owner[i]].rotate3,0,1,0);
1289 glRotatef(muscles[model[k].owner[i]].rotate2-90,0,0,1);
1290 glRotatef(muscles[model[k].owner[i]].rotate1-90,0,1,0);
1291 glTranslatef(model[k].vertex[i].x,model[k].vertex[i].y,model[k].vertex[i].z);
1292 glGetFloatv(GL_MODELVIEW_MATRIX,M);
1293 model[k].vertex[i].x=M[12]*1;
1294 model[k].vertex[i].y=M[13]*1;
1295 model[k].vertex[i].z=M[14]*1;
1298 model[k].CalculateNormals(0);
1303 tfile=fopen( ConvertFileName(lowfilename), "rb" );
1305 lSize=sizeof(num_joints);
1306 fseek ( tfile, lSize, SEEK_CUR);
1307 //joints = new Joint[num_joints];
1308 //jointlabels = new int[num_joints];
1309 for(i=0;i<num_joints;i++){
1311 fseek ( tfile, lSize, SEEK_CUR);
1312 lSize=sizeof(float);
1313 fseek ( tfile, lSize, SEEK_CUR);
1314 lSize=sizeof(float);
1315 fseek ( tfile, lSize, SEEK_CUR);
1316 lSize=1;//sizeof(bool);
1317 fseek ( tfile, lSize, SEEK_CUR);
1318 lSize=1;//sizeof(bool);
1319 fseek ( tfile, lSize, SEEK_CUR);
1321 fseek ( tfile, lSize, SEEK_CUR);
1322 lSize=1;//sizeof(bool);
1323 fseek ( tfile, lSize, SEEK_CUR);
1324 lSize=1;//sizeof(bool);
1325 fseek ( tfile, lSize, SEEK_CUR);
1327 fseek ( tfile, lSize, SEEK_CUR);
1329 fseek ( tfile, lSize, SEEK_CUR);
1330 lSize=1;//sizeof(bool);
1331 fseek ( tfile, lSize, SEEK_CUR);
1333 fseek ( tfile, lSize, SEEK_CUR);
1334 if(joints[i].hasparent)joints[i].parent=&joints[parentID];
1335 joints[i].velocity=0;
1336 joints[i].oldposition=joints[i].position;
1338 funpackf(tfile, "Bi", &num_muscles);
1339 //muscles = new Muscle[num_muscles];
1340 for(i=0;i<num_muscles;i++){
1341 lSize=sizeof(float);
1342 fseek ( tfile, lSize, SEEK_CUR);
1343 lSize=sizeof(float);
1344 fseek ( tfile, lSize, SEEK_CUR);
1345 lSize=sizeof(float);
1346 fseek ( tfile, lSize, SEEK_CUR);
1347 lSize=sizeof(float);
1348 fseek ( tfile, lSize, SEEK_CUR);
1349 lSize=sizeof(float);
1350 fseek ( tfile, lSize, SEEK_CUR);
1352 fseek ( tfile, lSize, SEEK_CUR);
1353 tempmuscle=muscles[i].numverticeslow;
1354 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1355 if(muscles[i].numverticeslow){
1356 //muscles[i].verticeslow.clear();
1357 //muscles[i].verticeslow.resize(muscles[i].numverticeslow);
1358 //if(muscles[i].verticeslow)dealloc2(muscles[i].verticeslow);
1359 muscles[i].verticeslow=(int*)malloc(sizeof(int)*muscles[i].numverticeslow);
1361 for(j=0;j<muscles[i].numverticeslow-edit;j++){
1362 funpackf(tfile, "Bi", &muscles[i].verticeslow[j+edit]);
1363 if(muscles[i].verticeslow[j+edit]>=modellow.vertexNum){
1364 muscles[i].numverticeslow--;
1371 lSize=1;//sizeof(bool);
1372 fseek ( tfile, lSize, SEEK_CUR);
1374 fseek ( tfile, lSize, SEEK_CUR);
1375 fseek ( tfile, lSize, SEEK_CUR);
1378 for(j=0;j<num_muscles;j++){
1379 for(i=0;i<muscles[j].numverticeslow;i++){
1380 if(muscles[j].numverticeslow&&muscles[j].verticeslow[i]<modellow.vertexNum)modellow.owner[muscles[j].verticeslow[i]]=j;
1384 for(i=0;i<num_joints;i++){
1385 joints[i].startpos=joints[i].position;
1387 for(i=0;i<num_muscles;i++){
1388 FindRotationMuscle(i,-1);
1390 for(i=0;i<modellow.vertexNum;i++){
1391 modellow.vertex[i]=modellow.vertex[i]-(muscles[modellow.owner[i]].parent1->position+muscles[modellow.owner[i]].parent2->position)/2;
1392 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1395 glRotatef(muscles[modellow.owner[i]].rotate3,0,1,0);
1396 glRotatef(muscles[modellow.owner[i]].rotate2-90,0,0,1);
1397 glRotatef(muscles[modellow.owner[i]].rotate1-90,0,1,0);
1398 glTranslatef(modellow.vertex[i].x,modellow.vertex[i].y,modellow.vertex[i].z);
1399 glGetFloatv(GL_MODELVIEW_MATRIX,M);
1400 modellow.vertex[i].x=M[12];
1401 modellow.vertex[i].y=M[13];
1402 modellow.vertex[i].z=M[14];
1405 modellow.CalculateNormals(0);
1409 tfile=fopen( ConvertFileName(clothesfilename), "rb" );
1410 lSize=sizeof(num_joints);
1411 fseek ( tfile, lSize, SEEK_CUR);
1412 //joints = new Joint[num_joints];
1413 //jointlabels = new int[num_joints];
1414 for(i=0;i<num_joints;i++){
1416 fseek ( tfile, lSize, SEEK_CUR);
1417 lSize=sizeof(float);
1418 fseek ( tfile, lSize, SEEK_CUR);
1419 lSize=sizeof(float);
1420 fseek ( tfile, lSize, SEEK_CUR);
1421 lSize=1;//sizeof(bool);
1422 fseek ( tfile, lSize, SEEK_CUR);
1423 lSize=1;//sizeof(bool);
1424 fseek ( tfile, lSize, SEEK_CUR);
1426 fseek ( tfile, lSize, SEEK_CUR);
1427 lSize=1;//sizeof(bool);
1428 fseek ( tfile, lSize, SEEK_CUR);
1429 lSize=1;//sizeof(bool);
1430 fseek ( tfile, lSize, SEEK_CUR);
1432 fseek ( tfile, lSize, SEEK_CUR);
1434 fseek ( tfile, lSize, SEEK_CUR);
1435 lSize=1;//sizeof(bool);
1436 fseek ( tfile, lSize, SEEK_CUR);
1438 fseek ( tfile, lSize, SEEK_CUR);
1439 if(joints[i].hasparent)joints[i].parent=&joints[parentID];
1440 joints[i].velocity=0;
1441 joints[i].oldposition=joints[i].position;
1443 funpackf(tfile, "Bi", &num_muscles);
1444 //muscles = new Muscle[num_muscles];
1445 for(i=0;i<num_muscles;i++){
1446 lSize=sizeof(float);
1447 fseek ( tfile, lSize, SEEK_CUR);
1448 lSize=sizeof(float);
1449 fseek ( tfile, lSize, SEEK_CUR);
1450 lSize=sizeof(float);
1451 fseek ( tfile, lSize, SEEK_CUR);
1452 lSize=sizeof(float);
1453 fseek ( tfile, lSize, SEEK_CUR);
1454 lSize=sizeof(float);
1455 fseek ( tfile, lSize, SEEK_CUR);
1457 fseek ( tfile, lSize, SEEK_CUR);
1458 tempmuscle=muscles[i].numverticesclothes;
1459 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1460 if(muscles[i].numverticesclothes){
1461 //muscles[i].verticesclothes.clear();
1462 //muscles[i].verticesclothes.resize(muscles[i].numverticesclothes);
1463 //if(muscles[i].verticesclothes)dealloc2(muscles[i].verticesclothes);
1464 muscles[i].verticesclothes=(int*)malloc(sizeof(int)*muscles[i].numverticesclothes);
1466 for(j=0;j<muscles[i].numverticesclothes-edit;j++){
1467 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j+edit]);
1468 if(muscles[i].verticesclothes[j+edit]>=modelclothes.vertexNum){
1469 muscles[i].numverticesclothes--;
1474 lSize=1;//sizeof(bool);
1475 fseek ( tfile, lSize, SEEK_CUR);
1477 fseek ( tfile, lSize, SEEK_CUR);
1478 fseek ( tfile, lSize, SEEK_CUR);
1481 for(j=0;j<num_muscles;j++){
1482 for(i=0;i<muscles[j].numverticesclothes;i++){
1483 if(muscles[j].numverticesclothes&&muscles[j].verticesclothes[i]<modelclothes.vertexNum)modelclothes.owner[muscles[j].verticesclothes[i]]=j;
1487 for(i=0;i<num_joints;i++){
1488 joints[i].startpos=joints[i].position;
1490 for(i=0;i<num_muscles;i++){
1491 FindRotationMuscle(i,-1);
1493 for(i=0;i<modelclothes.vertexNum;i++){
1494 modelclothes.vertex[i]=modelclothes.vertex[i]-(muscles[modelclothes.owner[i]].parent1->position+muscles[modelclothes.owner[i]].parent2->position)/2;
1495 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1498 glRotatef(muscles[modelclothes.owner[i]].rotate3,0,1,0);
1499 glRotatef(muscles[modelclothes.owner[i]].rotate2-90,0,0,1);
1500 glRotatef(muscles[modelclothes.owner[i]].rotate1-90,0,1,0);
1501 glTranslatef(modelclothes.vertex[i].x,modelclothes.vertex[i].y,modelclothes.vertex[i].z);
1502 glGetFloatv(GL_MODELVIEW_MATRIX,M);
1503 modelclothes.vertex[i].x=M[12];
1504 modelclothes.vertex[i].y=M[13];
1505 modelclothes.vertex[i].z=M[14];
1508 modelclothes.CalculateNormals(0);
1512 for(i=0;i<num_joints;i++){
1513 for(j=0;j<num_joints;j++){
1514 if(joints[i].label==j)jointlabels[j]=i;
1521 Animation::Animation()
1527 weapontargetnum = 0;
1539 Animation::~Animation()
1544 void Animation::deallocate()
1550 for(i = 0; i < joints; i++)
1551 dealloc2(position[i]);
1559 for(i = 0; i < joints; i++)
1568 for(i = 0; i < joints; i++)
1569 dealloc2(twist2[i]);
1577 for(i = 0; i < joints; i++)
1578 dealloc2(onground[i]);
1584 if(speed)dealloc2(speed);
1587 if(forward)dealloc2(forward);
1590 if(weapontarget)dealloc2(weapontarget);
1593 if(label)dealloc2(label);
1599 Skeleton::Skeleton()
1607 memset(forwardjoints, 0, sizeof(forwardjoints));
1612 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1615 // XYZ specialforward[5];
1616 memset(jointlabels, 0, sizeof(jointlabels));
1620 // Model modelclothes;
1624 // Model drawmodellow;
1625 // Model drawmodelclothes;
1630 memset(skinText, 0, sizeof(skinText));
1647 Skeleton::~Skeleton()
1670 numverticesclothes = 0;
1679 rotate1 = 0,rotate2 = 0,rotate3 = 0;
1680 lastrotate1 = 0,lastrotate2 = 0,lastrotate3 = 0;
1681 oldrotate1 = 0,oldrotate2 = 0,oldrotate3 = 0;
1682 newrotate1 = 0,newrotate2 = 0,newrotate3 = 0;
1690 dealloc2(verticeslow);
1691 dealloc2(verticesclothes);
1694 Animation & Animation::operator = (const Animation & ani)
1698 bool allocate = true;
1700 allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1702 if (allocate) deallocate();
1704 numframes = ani.numframes;
1705 height = ani.height;
1706 attack = ani.attack;
1707 joints = ani.joints;
1708 weapontargetnum = ani.weapontargetnum;
1710 if (allocate) position=(XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1711 for(i = 0; i < ani.joints; i++)
1713 if (allocate) position[i] = (XYZ*)malloc(sizeof(XYZ)*ani.numframes);
1714 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1717 if (allocate) twist=(float**)malloc(sizeof(float*)*ani.joints);
1718 for(i = 0; i < ani.joints; i++)
1720 if (allocate) twist[i] = (float*)malloc(sizeof(float)*ani.numframes);
1721 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1724 if (allocate) twist2=(float**)malloc(sizeof(float*)*ani.joints);
1725 for(i = 0; i < ani.joints; i++)
1727 if (allocate) twist2[i] = (float*)malloc(sizeof(float)*ani.numframes);
1728 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1731 if (allocate) speed = (float*)malloc(sizeof(float)*ani.numframes);
1732 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1734 if (allocate) onground=(bool**)malloc(sizeof(bool*)*ani.joints);
1735 for(i = 0; i < ani.joints; i++)
1737 if (allocate) onground[i] =(bool*)malloc(sizeof(bool)*ani.numframes);
1738 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1741 if (allocate) forward = (XYZ*)malloc(sizeof(XYZ)*ani.numframes);
1742 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1744 if (allocate) weapontarget = (XYZ*)malloc(sizeof(XYZ)*ani.numframes);
1745 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1747 if (allocate) label = (int*)malloc(sizeof(int)*ani.numframes);
1748 memcpy(label, ani.label, sizeof(int)*ani.numframes);