]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Skeleton.cpp
some findClosest* refactoring
[lugaru.git] / Source / Skeleton.cpp
index 34a5aa5574bfb140c0b7ac845a4d081eb89640bd..cb8e043e4dc57ff26edc1598bfebfb13e1f73a78 100644 (file)
@@ -29,10 +29,8 @@ extern float multiplier;
 extern float gravity;
 extern Skeleton testskeleton;
 extern Terrain terrain;
-extern int channels[100];
 extern Objects objects;
 extern int environment;
-extern float terraindetail;
 extern float camerashake;
 extern bool freeze;
 extern int detail;
@@ -45,15 +43,15 @@ extern int tutoriallevel;
 extern int whichjointstartarray[26];
 extern int whichjointendarray[26];
 
-extern Game * pgame;
 extern bool visibleloading;
-extern "C"     void PlaySoundEx(int channel, OPENAL_SAMPLE *sptr, OPENAL_DSPUNIT *dsp, signed char startpaused);
 
 void dealloc2(void* param){
        free(param);
        param=0;
 }
 
+enum {boneconnect, constraint, muscle};
+
 void Muscle::DoConstraint(bool spinny)
 {
        static XYZ vel;
@@ -194,8 +192,8 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
        if(free){
                freetime+=multiplier;
 
-               whichpatchx=coords->x/(terrain.size/subdivision*terrain.scale*terraindetail);
-               whichpatchz=coords->z/(terrain.size/subdivision*terrain.scale*terraindetail);
+               whichpatchx=coords->x/(terrain.size/subdivision*terrain.scale);
+               whichpatchz=coords->z/(terrain.size/subdivision*terrain.scale);
 
                terrainlight=*coords;
                objects.SphereCheckPossible(&terrainlight, 1);
@@ -249,136 +247,6 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                        temp=joints[jointlabels[leftknee]].position-(joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position)/2;
                                }
                        }
-                       /*
-                       if(terrain.patchobjectnum[whichpatchx][whichpatchz])
-                       for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
-                       k=terrain.patchobjects[whichpatchx][whichpatchz][m];
-                       if(k<objects.numobjects&&k>=0)
-                       if(objects.possible[k]){
-                       temp=joints[jointlabels[head]].position*(*scale)+*coords;
-                       if(objects.model[k].SphereCheck(&temp, 0.06, &start, &objects.position[k], &objects.rotation[k])!=-1){
-                       //temp=(joints[jointlabels[head]].position*(*scale)+*coords)-start;
-                       //Normalise(&temp);
-                       //joints[jointlabels[head]].position=((temp*.2+start)-*coords)/(*scale);
-                       joints[jointlabels[head]].position=(temp-*coords)/(*scale);
-                       }
-                       }
-                       }       */
-
-
-                       //Ears check
-                       /*XYZ startheadpos;
-                       startheadpos=joints[jointlabels[head]].position;
-                       XYZ headpos;
-                       headpos=joints[jointlabels[head]].position+(joints[jointlabels[head]].position-joints[jointlabels[neck]].position);
-                       if(terrain.patchobjectnum[whichpatchx][whichpatchz])
-                       for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
-                       k=terrain.patchobjects[whichpatchx][whichpatchz][m];
-                       if(k<objects.numobjects&&k>=0)
-                       if(objects.possible[k]){
-                       friction=objects.friction[k];
-                       start=joints[jointlabels[head]].position*(*scale)+*coords;
-                       end=(headpos)*(*scale)+*coords;
-                       whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
-                       if(whichhit!=-1){
-                       if(joints[jointlabels[head]].label==groin&&!joints[jointlabels[head]].locked&&joints[jointlabels[head]].delay<=0){
-                       joints[jointlabels[head]].locked=1;
-                       joints[jointlabels[head]].delay=1;
-                       static float gLoc[3];
-                       static float vel[3];
-                       gLoc[0]=headpos.x*(*scale)+coords->x;
-                       gLoc[1]=headpos.y*(*scale)+coords->y;
-                       gLoc[2]=headpos.z*(*scale)+coords->z;
-                       vel[0]=joints[jointlabels[head]].velocity.x;
-                       vel[1]=joints[jointlabels[head]].velocity.y;
-                       vel[2]=joints[jointlabels[head]].velocity.z;
-                       PlaySoundEx( landsound1, samp[landsound1], NULL, true);
-                       OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
-                       OPENAL_SetVolume(channels[landsound1], 128);
-                       OPENAL_SetPaused(channels[landsound1], false);
-
-                       breaking=1;
-                       }
-
-                       if(joints[jointlabels[head]].label==head&&!joints[jointlabels[head]].locked&&joints[jointlabels[head]].delay<=0){
-                       joints[jointlabels[head]].locked=1;
-                       joints[jointlabels[head]].delay=1;
-                       static float gLoc[3];
-                       static float vel[3];
-                       gLoc[0]=headpos.x*(*scale)+coords->x;
-                       gLoc[1]=headpos.y*(*scale)+coords->y;
-                       gLoc[2]=headpos.z*(*scale)+coords->z;
-                       vel[0]=joints[jointlabels[head]].velocity.x;
-                       vel[1]=joints[jointlabels[head]].velocity.y;
-                       vel[2]=joints[jointlabels[head]].velocity.z;
-                       PlaySoundEx( landsound2, samp[landsound2], NULL, true);
-                       OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
-                       OPENAL_SetVolume(channels[landsound2], 128);
-                       OPENAL_SetPaused(channels[landsound2], false);
-                       }
-
-                       terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
-                       if(terrainnormal.y>.8)freefall=0;
-                       bounceness=terrainnormal*findLength(&joints[jointlabels[head]].velocity)*(abs(normaldotproduct(joints[jointlabels[head]].velocity,terrainnormal)));
-                       if(findLengthfast(&joints[jointlabels[head]].velocity)>findLengthfast(&joints[jointlabels[head]].oldvelocity)){
-                       bounceness=0;
-                       joints[jointlabels[head]].velocity=joints[jointlabels[head]].oldvelocity;
-                       }
-                       if(findLengthfast(&bounceness)>4000&&breaking){
-                       objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
-                       Sprite::MakeSprite(cloudsprite, headpos*(*scale)+*coords,joints[jointlabels[head]].velocity*.06, 1,1,1, 4, .2);
-                       breaking=0;
-                       camerashake+=.6;
-
-                       static float gLoc[3];
-                       static float vel[3];
-                       gLoc[0]=headpos.x*(*scale)+coords->x;
-                       gLoc[1]=headpos.y*(*scale)+coords->y;
-                       gLoc[2]=headpos.z*(*scale)+coords->z;
-                       vel[0]=joints[jointlabels[head]].velocity.x;
-                       vel[1]=joints[jointlabels[head]].velocity.y;
-                       vel[2]=joints[jointlabels[head]].velocity.z;
-                       PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
-                       OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
-                       OPENAL_SetVolume(channels[breaksound2], 300);
-                       OPENAL_SetPaused(channels[breaksound2], false);
-
-                       envsound[numenvsounds]=*coords;
-                       envsoundvol[numenvsounds]=64;
-                       envsoundlife[numenvsounds]=.4;
-                       numenvsounds++;
-                       }
-                       if(objects.type[k]==treetrunktype){
-                       objects.rotx[k]+=joints[jointlabels[head]].velocity.x*multiplier*.4;
-                       objects.roty[k]+=joints[jointlabels[head]].velocity.z*multiplier*.4;
-                       objects.rotx[k+1]+=joints[jointlabels[head]].velocity.x*multiplier*.4;
-                       objects.roty[k+1]+=joints[jointlabels[head]].velocity.z*multiplier*.4;
-                       }
-                       if(!joints[jointlabels[head]].locked)damage+=findLengthfast(&bounceness)/2500;
-                       ReflectVector(&joints[jointlabels[head]].velocity,&terrainnormal);
-                       frictionness=abs(normaldotproduct(joints[jointlabels[head]].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[jointlabels[head]].velocity);
-                       joints[jointlabels[head]].velocity-=bounceness;
-                       if(1-friction*frictionness>0)joints[jointlabels[head]].velocity*=1-friction*frictionness;
-                       else joints[jointlabels[head]].velocity=0;
-                       if(findLengthfast(&bounceness)>2500){
-                       Normalise(&bounceness);
-                       bounceness=bounceness*50;
-                       }
-                       joints[jointlabels[head]].velocity+=bounceness*elasticity;
-
-
-                       if(!joints[jointlabels[head]].locked)
-                       if(findLengthfast(&joints[jointlabels[head]].velocity)<1){
-                       joints[jointlabels[head]].locked=1;
-                       //joints[jointlabels[head]].velocity*=3;
-                       }
-                       if(findLengthfast(&bounceness)>500)Sprite::MakeSprite(cloudsprite, headpos*(*scale)+*coords,joints[jointlabels[head]].velocity*.06, 1,1,1, .5, .2);
-                       joints[jointlabels[head]].position=(temp-*coords)/(*scale)+(startheadpos-headpos)+terrainnormal*.005;
-                       if(longdead>100)broken=1;
-                       }
-                       }
-                       }
-                       */
 
                        for(i=0; i<num_joints; i++){
                                //joints[i].delay-=multiplier/1.5;
@@ -414,19 +282,8 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                                if(joints[i].label==groin&&!joints[i].locked&&joints[i].delay<=0){
                                                        joints[i].locked=1;
                                                        joints[i].delay=1;
-                                                       static float gLoc[3];
-                                                       static float vel[3];
-                                                       gLoc[0]=joints[i].position.x*(*scale)+coords->x;
-                                                       gLoc[1]=joints[i].position.y*(*scale)+coords->y;
-                                                       gLoc[2]=joints[i].position.z*(*scale)+coords->z;
-                                                       vel[0]=joints[i].velocity.x;
-                                                       vel[1]=joints[i].velocity.y;
-                                                       vel[2]=joints[i].velocity.z;
                                                        if(tutoriallevel!=1||id==0){
-                                                               PlaySoundEx( landsound1, samp[landsound1], NULL, true);
-                                                               OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
-                                                               OPENAL_SetVolume(channels[landsound1], 128);
-                                                               OPENAL_SetPaused(channels[landsound1], false);
+                                                               emit_sound_at(landsound1, joints[i].position*(*scale)+*coords, 128.);
                                                        }
                                                        breaking=1;
                                                }
@@ -434,19 +291,8 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                                if(joints[i].label==head&&!joints[i].locked&&joints[i].delay<=0){
                                                        joints[i].locked=1;
                                                        joints[i].delay=1;
-                                                       static float gLoc[3];
-                                                       static float vel[3];
-                                                       gLoc[0]=joints[i].position.x*(*scale)+coords->x;
-                                                       gLoc[1]=joints[i].position.y*(*scale)+coords->y;
-                                                       gLoc[2]=joints[i].position.z*(*scale)+coords->z;
-                                                       vel[0]=joints[i].velocity.x;
-                                                       vel[1]=joints[i].velocity.y;
-                                                       vel[2]=joints[i].velocity.z;
                                                        if(tutoriallevel!=1||id==0){
-                                                               PlaySoundEx( landsound2, samp[landsound2], NULL, true);
-                                                               OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
-                                                               OPENAL_SetVolume(channels[landsound2], 128);
-                                                               OPENAL_SetPaused(channels[landsound2], false);
+                                                               emit_sound_at(landsound2, joints[i].position*(*scale)+*coords, 128.);
                                                        }
                                                }
 
@@ -462,24 +308,13 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
 
                                                if(tutoriallevel!=1||id==0)
                                                        if(findLengthfast(&bounceness)>8000&&breaking){
-                                                               objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
+                                                               objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.yaw[k],0),.4,.5,Random()%360);
                                                                Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 4, .2);
                                                                //Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 1, .2);
                                                                breaking=0;
                                                                camerashake+=.6;
 
-                                                               static float gLoc[3];
-                                                               static float vel[3];
-                                                               gLoc[0]=joints[i].position.x*(*scale)+coords->x;
-                                                               gLoc[1]=joints[i].position.y*(*scale)+coords->y;
-                                                               gLoc[2]=joints[i].position.z*(*scale)+coords->z;
-                                                               vel[0]=joints[i].velocity.x;
-                                                               vel[1]=joints[i].velocity.y;
-                                                               vel[2]=joints[i].velocity.z;
-                                                               PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
-                                                               OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
-                                                               OPENAL_SetVolume(channels[breaksound2], 300);
-                                                               OPENAL_SetPaused(channels[breaksound2], false);
+                                                               emit_sound_at(breaksound2, joints[i].position*(*scale)+*coords);
 
                                                                envsound[numenvsounds]=*coords;
                                                                envsoundvol[numenvsounds]=64;
@@ -531,24 +366,13 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                                                        friction=objects.friction[k];
                                                                        start=joints[i].realoldposition;
                                                                        end=joints[i].position*(*scale)+*coords;
-                                                                       whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
+                                                                       whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.yaw[k]);
                                                                        if(whichhit!=-1){
                                                                                if(joints[i].label==groin&&!joints[i].locked&&joints[i].delay<=0){
                                                                                        joints[i].locked=1;
                                                                                        joints[i].delay=1;
-                                                                                       static float gLoc[3];
-                                                                                       static float vel[3];
-                                                                                       gLoc[0]=joints[i].position.x*(*scale)+coords->x;
-                                                                                       gLoc[1]=joints[i].position.y*(*scale)+coords->y;
-                                                                                       gLoc[2]=joints[i].position.z*(*scale)+coords->z;
-                                                                                       vel[0]=joints[i].velocity.x;
-                                                                                       vel[1]=joints[i].velocity.y;
-                                                                                       vel[2]=joints[i].velocity.z;
                                                                                        if(tutoriallevel!=1||id==0){
-                                                                                               PlaySoundEx( landsound1, samp[landsound1], NULL, true);
-                                                                                               OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
-                                                                                               OPENAL_SetVolume(channels[landsound1], 128);
-                                                                                               OPENAL_SetPaused(channels[landsound1], false);
+                                                                                               emit_sound_at(landsound1, joints[i].position*(*scale)+*coords, 128.);
                                                                                        }
                                                                                        breaking=1;
                                                                                }
@@ -556,23 +380,12 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                                                                if(joints[i].label==head&&!joints[i].locked&&joints[i].delay<=0){
                                                                                        joints[i].locked=1;
                                                                                        joints[i].delay=1;
-                                                                                       static float gLoc[3];
-                                                                                       static float vel[3];
-                                                                                       gLoc[0]=joints[i].position.x*(*scale)+coords->x;
-                                                                                       gLoc[1]=joints[i].position.y*(*scale)+coords->y;
-                                                                                       gLoc[2]=joints[i].position.z*(*scale)+coords->z;
-                                                                                       vel[0]=joints[i].velocity.x;
-                                                                                       vel[1]=joints[i].velocity.y;
-                                                                                       vel[2]=joints[i].velocity.z;
                                                                                        if(tutoriallevel!=1||id==0){
-                                                                                               PlaySoundEx( landsound2, samp[landsound2], NULL, true);
-                                                                                               OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
-                                                                                               OPENAL_SetVolume(channels[landsound2], 128);
-                                                                                               OPENAL_SetPaused(channels[landsound2], false);
+                                                                                               emit_sound_at(landsound2, joints[i].position*(*scale)+*coords, 128.);
                                                                                        }
                                                                                }
 
-                                                                               terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
+                                                                               terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.yaw[k],0)*-1;
                                                                                if(terrainnormal.y>.8)freefall=0;
                                                                                bounceness=terrainnormal*findLength(&joints[i].velocity)*(abs(normaldotproduct(joints[i].velocity,terrainnormal)));
                                                                                if(findLengthfast(&joints[i].velocity)>findLengthfast(&joints[i].oldvelocity)){
@@ -581,23 +394,12 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                                                                }
                                                                                if(tutoriallevel!=1||id==0)
                                                                                        if(findLengthfast(&bounceness)>4000&&breaking){
-                                                                                               objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
+                                                                                               objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.yaw[k],0),.4,.5,Random()%360);
                                                                                                Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 4, .2);
                                                                                                breaking=0;
                                                                                                camerashake+=.6;
 
-                                                                                               static float gLoc[3];
-                                                                                               static float vel[3];
-                                                                                               gLoc[0]=joints[i].position.x*(*scale)+coords->x;
-                                                                                               gLoc[1]=joints[i].position.y*(*scale)+coords->y;
-                                                                                               gLoc[2]=joints[i].position.z*(*scale)+coords->z;
-                                                                                               vel[0]=joints[i].velocity.x;
-                                                                                               vel[1]=joints[i].velocity.y;
-                                                                                               vel[2]=joints[i].velocity.z;
-                                                                                               PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
-                                                                                               OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
-                                                                                               OPENAL_SetVolume(channels[breaksound2], 300);
-                                                                                               OPENAL_SetPaused(channels[breaksound2], false);
+                                                                                               emit_sound_at(breaksound2, joints[i].position*(*scale)+*coords);
 
                                                                                                envsound[numenvsounds]=*coords;
                                                                                                envsoundvol[numenvsounds]=64;
@@ -667,7 +469,7 @@ float Skeleton::DoConstraints(XYZ *coords,float *scale)
                                                //Make this less stupid
                                                start=joints[jointlabels[whichjointstartarray[i]]].position*(*scale)+*coords;
                                                end=joints[jointlabels[whichjointendarray[i]]].position*(*scale)+*coords;
-                                               whichhit=objects.model[k].LineCheckSlidePossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
+                                               whichhit=objects.model[k].LineCheckSlidePossible(&start,&end,&temp,&objects.position[k],&objects.yaw[k]);
                                                if(whichhit!=-1){
                                                        joints[jointlabels[whichjointendarray[i]]].position=(end-*coords)/(*scale);
                                                        for(j=0; j<num_muscles; j++){
@@ -910,6 +712,7 @@ void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
 
 void Skeleton::AddMuscle(int attach1,int attach2,float minlength,float maxlength,int type)
 {
+       const int max_muscles = 100; // FIXME: Probably can be dropped
        if(num_muscles<max_muscles-1&&attach1<num_joints&&attach1>=0&&attach2<num_joints&&attach2>=0&&attach1!=attach2){
                muscles[num_muscles].parent1=&joints[attach1];
                muscles[num_muscles].parent2=&joints[attach2];
@@ -1031,7 +834,7 @@ void Animation::Load(const char *filename, int aheight, int aattack)
        height=aheight;
        attack=aattack;
 
-       if(visibleloading)pgame->LoadingScreen();
+       if(visibleloading) Game::LoadingScreen();
 
        tfile=fopen( fixedFN, "rb" );
        if(tfile){