]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Skeleton.cpp
Switched all data access to the new methods
[lugaru.git] / Source / Skeleton.cpp
index 11d7fa9d339b8cff6fca078c36e45be412ccd482..a9f63e258ee438861871746d1baf78a47e7faddf 100644 (file)
@@ -1,22 +1,21 @@
 /*
 Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
 
 This file is part of Lugaru.
 
-Lugaru is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
+Lugaru is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
+Lugaru is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-
-See the GNU General Public License for more details.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /**> HEADER FILES <**/
@@ -24,20 +23,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "Skeleton.h"
 #include "openal_wrapper.h"
 #include "Animation.h"
+#include "Utils/Folders.h"
 
 extern float multiplier;
 extern float gravity;
-extern Skeleton testskeleton;
 extern Terrain terrain;
 extern Objects objects;
 extern int environment;
 extern float camerashake;
 extern bool freeze;
 extern int detail;
-extern XYZ envsound[30];
-extern float envsoundvol[30];
-extern int numenvsounds;
-extern float envsoundlife[30];
 extern int tutoriallevel;
 
 extern int whichjointstartarray[26];
@@ -45,19 +40,11 @@ extern int whichjointendarray[26];
 
 extern bool visibleloading;
 
-/* convenience functions
- */
-Joint& Skeleton::joint(int bodypart) { return joints[jointlabels[bodypart]]; }
-XYZ& Skeleton::jointPos(int bodypart) { return joint(bodypart).position; }
-XYZ& Skeleton::jointVel(int bodypart) { return joint(bodypart).velocity; }
-
-
 /* EFFECT
  */
 void dealloc2(void* param)
 {
     free(param);
-    param = 0; // FIXME: does this *do* anything???
 }
 
 enum {boneconnect, constraint, muscle};
@@ -192,7 +179,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale)
     XYZ bounceness;
     const int numrepeats = 3;
     float groundlevel = .15;
-    int i, j, k, l, m;
+    int i, j, k, m;
     XYZ temp;
     XYZ terrainnormal;
     int whichhit;
@@ -217,17 +204,20 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale)
             joints[i].position = joints[i].position + joints[i].velocity * multiplier;
 
             switch (joints[i].label) {
-                case head:
-                    groundlevel = .8; break;
-                case righthand:
-                case rightwrist:
-                case rightelbow:
-                case lefthand:
-                case leftwrist:
-                case leftelbow:
-                    groundlevel = .2; break;
-                default:
-                    groundlevel = .15; break;
+            case head:
+                groundlevel = .8;
+                break;
+            case righthand:
+            case rightwrist:
+            case rightelbow:
+            case lefthand:
+            case leftwrist:
+            case leftelbow:
+                groundlevel = .2;
+                break;
+            default:
+                groundlevel = .15;
+                break;
             }
 
             joints[i].position.y -= groundlevel;
@@ -347,17 +337,17 @@ 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.yaw[k], 0), .4, .5, Random() % 360);
+                            // FIXME: this crashes because k is not initialized!
+                            // to reproduce, type 'wolfie' in console and play a while
+                            // I'll just comment it out for now
+                            //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 = false;
                             camerashake += .6;
 
                             emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
 
-                            envsound[numenvsounds] = *coords;
-                            envsoundvol[numenvsounds] = 64;
-                            envsoundlife[numenvsounds] = .4;
-                            numenvsounds++;
+                            addEnvSound(*coords, 64);
                         }
 
                     if (findLengthfast(&bounceness) > 2500) {
@@ -442,10 +432,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale)
 
                                             emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
 
-                                            envsound[numenvsounds] = *coords;
-                                            envsoundvol[numenvsounds] = 64;
-                                            envsoundlife[numenvsounds] = .4;
-                                            numenvsounds++;
+                                            addEnvSound(*coords, 64);
                                         }
                                     if (objects.type[k] == treetrunktype) {
                                         objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
@@ -509,17 +496,20 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale)
 
         for (i = 0; i < num_joints; i++) {
             switch (joints[i].label) {
-                case head:
-                    groundlevel = .8; break;
-                case righthand:
-                case rightwrist:
-                case rightelbow:
-                case lefthand:
-                case leftwrist:
-                case leftelbow:
-                    groundlevel = .2; break;
-                default:
-                    groundlevel = .15; break;
+            case head:
+                groundlevel = .8;
+                break;
+            case righthand:
+            case rightwrist:
+            case rightelbow:
+            case lefthand:
+            case leftwrist:
+            case leftelbow:
+                groundlevel = .2;
+                break;
+            default:
+                groundlevel = .15;
+                break;
             }
             joints[i].position.y += groundlevel;
             joints[i].mass = 1;
@@ -553,7 +543,16 @@ void Skeleton::DoGravity(float *scale)
 {
     static int i;
     for (i = 0; i < num_joints; i++) {
-        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))
+        if (
+                (
+                    ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
+                    (lowforward.y > -.1) ||
+                    (joints[i].mass < 5)
+                ) && (
+                    ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
+                    (forward.y < .3)
+                )
+            )
             joints[i].velocity.y += gravity * multiplier / (*scale);
     }
 }
@@ -597,34 +596,39 @@ void Skeleton::FindRotationMuscle(int which, int animation)
     const int label1 = muscles[which].parent1->label;
     const int label2 = muscles[which].parent2->label;
     switch (label1) {
-        case head:
-            fwd = specialforward[0]; break;
-        case rightshoulder:
-        case rightelbow:
-        case rightwrist:
-        case righthand:
-            fwd = specialforward[1]; break;
-        case leftshoulder:
-        case leftelbow:
-        case leftwrist:
-        case lefthand:
-            fwd = specialforward[2]; break;
-        case righthip:
-        case rightknee:
-        case rightankle:
-        case rightfoot:
-            fwd = specialforward[3]; break;
-        case lefthip:
-        case leftknee:
-        case leftankle:
-        case leftfoot:
-            fwd = specialforward[4]; break;
-        default:
-            if (muscles[which].parent1->lower)
-                fwd = lowforward;
-            else
-                fwd = forward;
-            break;
+    case head:
+        fwd = specialforward[0];
+        break;
+    case rightshoulder:
+    case rightelbow:
+    case rightwrist:
+    case righthand:
+        fwd = specialforward[1];
+        break;
+    case leftshoulder:
+    case leftelbow:
+    case leftwrist:
+    case lefthand:
+        fwd = specialforward[2];
+        break;
+    case righthip:
+    case rightknee:
+    case rightankle:
+    case rightfoot:
+        fwd = specialforward[3];
+        break;
+    case lefthip:
+    case leftknee:
+    case leftankle:
+    case leftfoot:
+        fwd = specialforward[4];
+        break;
+    default:
+        if (muscles[which].parent1->lower)
+            fwd = lowforward;
+        else
+            fwd = forward;
+        break;
     }
 
     if (animation == hanganim) {
@@ -665,27 +669,18 @@ void Skeleton::FindRotationMuscle(int which, int animation)
 /* EFFECT
  * load an animation from file
  */
-void Animation::Load(const char *filename, int aheight, int aattack)
+void Animation::Load(const std::string& filename, int aheight, int aattack)
 {
     FILE *tfile;
     int i, j;
-    XYZ startoffset, endoffset;
-
-    // path to dir
-    const char *anim_prefix = ":Data:Animations:";
-
+    XYZ endoffset;
 
     LOGFUNC;
 
-    // concatenate anim_prefix + filename
-    int len = strlen(anim_prefix) + strlen(filename);
-    char *buf = new char[len + 1];
-    snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
     // Changing the filename into something the OS can understand
-    char *fixedFN = ConvertFileName(buf);
-    delete[] buf;
+    std::string filepath = Folders::getResourcePath("Animations/"+filename);
 
-    LOG(std::string("Loading animation...") + fixedFN);
+    LOG(std::string("Loading animation...") + filepath);
 
     // clear existing data
     deallocate();
@@ -697,27 +692,10 @@ void Animation::Load(const char *filename, int aheight, int aattack)
         Game::LoadingScreen();
 
     // read file in binary mode
-    tfile = fopen( fixedFN, "rb" );
+    tfile = fopen( filepath.c_str(), "rb" );
     if (tfile) {
         // read numframes, joints to know how much memory to allocate
         funpackf(tfile, "Bi Bi", &numframes, &joints);
-        /*
-        for(i = 0; i < joints; i++){
-        if(position[i])dealloc2(position[i]);
-        if(twist[i])dealloc2(twist[i]);
-        if(twist2[i])dealloc2(twist2[i]);
-        if(onground[i])dealloc2(onground[i]);
-        }*/
-        /*
-        if(position)dealloc2(position);
-        if(twist)dealloc2(twist);
-        if(twist2)dealloc2(twist2);
-        if(speed)dealloc2(speed);
-        if(onground)dealloc2(onground);
-        if(forward)dealloc2(forward);
-        if(weapontarget)dealloc2(weapontarget);
-        if(label)dealloc2(label);*/
-
 
         // allocate memory for everything
 
@@ -743,15 +721,6 @@ void Animation::Load(const char *filename, int aheight, int aattack)
         weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
         label = (int*)malloc(sizeof(int) * numframes);
 
-        /*position = new XYZ[joints][numframes];
-        twist = new float[joints][numframes];
-        twist2 = new float[joints][numframes];
-        speed = new float[numframes];
-        onground = new bool[joints][numframes];
-        forward = new XYZ[numframes];
-        label = new int[numframes];*/
-
-
         // read binary data as animation
 
         // for each frame...
@@ -794,18 +763,14 @@ void Animation::Load(const char *filename, int aheight, int aattack)
         fclose(tfile);
     }
 
-    startoffset = 0;
     endoffset = 0;
-    // find average position of certain joints on first and last frames
-    // and save in startoffset, endoffset
+    // find average position of certain joints on last frames
+    // and save in endoffset
     // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
     for (j = 0; j < joints; j++) {
-        if (position[j][0].y < 1)
-            startoffset += position[j][0];
         if (position[j][numframes - 1].y < 1)
             endoffset += position[j][numframes - 1];
     }
-    startoffset /= joints;
     endoffset /= joints;
     offset = endoffset;
     offset.y = 0;
@@ -827,16 +792,11 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
     int parentID;
     FILE *tfile;
     float lSize;
-    int i, j, tempmuscle;
-
-    int newload;
+    int i, j;
     int edit;
 
     LOGFUNC;
 
-
-    newload = 0;
-
     num_models = 7;
 
     // load various models
@@ -899,17 +859,16 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
 
     // load skeleton
 
-    tfile = fopen( ConvertFileName(filename), "rb" );
+    tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" );
 
     if (1) { // FIXME: should this be if(tfile) ?
         // read num_joints
         funpackf(tfile, "Bi", &num_joints);
 
         // allocate memory
-        //joints.resize(num_joints);
         if (joints)
             delete [] joints; //dealloc2(joints);
-        joints = (Joint*)new Joint[num_joints]; //malloc(sizeof(Joint)*num_joints);
+        joints = (Joint*)new Joint[num_joints];
 
         // read info for each joint
         for (i = 0; i < num_joints; i++) {
@@ -927,27 +886,19 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
         }
 
         // read num_muscles
-        tempmuscle = num_muscles;
         funpackf(tfile, "Bi", &num_muscles);
 
         // allocate memory
-        //muscles.clear();
         if (muscles)
             delete [] muscles; //dealloc2(muscles);
         muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
 
-        newload = 1;
-
-        // for each muscle... 
+        // for each muscle...
         for (i = 0; i < num_muscles; i++) {
             // read info
-            tempmuscle = muscles[i].numvertices;
             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);
 
             // allocate memory for vertices
-            //muscles[i].vertices.clear();
-            //muscles[i].vertices.resize(muscles[i].numvertices);
-            //if(muscles[i].vertices)dealloc2(muscles[i].vertices);
             muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
 
             // read vertices
@@ -1018,15 +969,13 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
 
     // load ???
 
-    tfile = fopen( ConvertFileName(lowfilename), "rb" );
+    tfile = fopen( Folders::getResourcePath(lowfilename).c_str(), "rb" );
 
     if (1) { // FIXME: should this be if(tfile) ?
         // skip joints section
 
         lSize = sizeof(num_joints);
         fseek(tfile, lSize, SEEK_CUR);
-        //joints = new Joint[num_joints];
-        //jointlabels = new int[num_joints];
         for (i = 0; i < num_joints; i++) {
             // skip joint info
             lSize = sizeof(XYZ)
@@ -1051,7 +1000,6 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
 
         // read num_muscles
         funpackf(tfile, "Bi", &num_muscles);
-        //muscles = new Muscle[num_muscles];
 
         for (i = 0; i < num_muscles; i++) {
             // skip muscle info
@@ -1064,14 +1012,10 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
             fseek(tfile, lSize, SEEK_CUR);
 
             // read numverticeslow
-            tempmuscle = muscles[i].numverticeslow;
             funpackf(tfile, "Bi", &muscles[i].numverticeslow);
 
             if (muscles[i].numverticeslow) {
                 // allocate memory
-                //muscles[i].verticeslow.clear();
-                //muscles[i].verticeslow.resize(muscles[i].numverticeslow);
-                //if(muscles[i].verticeslow)dealloc2(muscles[i].verticeslow);
                 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
 
                 // read verticeslow
@@ -1093,23 +1037,13 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
             fseek ( tfile, lSize, SEEK_CUR);
         }
 
-        // ???
-        lSize = sizeof(int);
         for (j = 0; j < num_muscles; j++) {
             for (i = 0; i < muscles[j].numverticeslow; i++) {
-                if (muscles[j].numverticeslow && muscles[j].verticeslow[i] < modellow.vertexNum)
+                if (muscles[j].verticeslow[i] < modellow.vertexNum)
                     modellow.owner[muscles[j].verticeslow[i]] = j;
             }
         }
 
-        /*FindForwards();
-        for(i=0;i<num_joints;i++){
-        joints[i].startpos=joints[i].position;
-        }
-        for(i=0;i<num_muscles;i++){
-        FindRotationMuscle(i,-1);
-        }*/
-
         // use opengl for its matrix math
         for (i = 0; i < modellow.vertexNum; i++) {
             modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
@@ -1133,13 +1067,11 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
     // load clothes
 
     if (clothes) {
-        tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
+        tfile = fopen( Folders::getResourcePath(clothesfilename).c_str(), "rb" ); // FIXME: where's the check for valid load
 
         // skip num_joints
         lSize = sizeof(num_joints);
         fseek ( tfile, lSize, SEEK_CUR);
-        //joints = new Joint[num_joints];
-        //jointlabels = new int[num_joints];
 
         for (i = 0; i < num_joints; i++) {
             // skip joint info
@@ -1165,7 +1097,6 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
 
         // read num_muscles
         funpackf(tfile, "Bi", &num_muscles);
-        //muscles = new Muscle[num_muscles];
 
         for (i = 0; i < num_muscles; i++) {
             // skip muscle info
@@ -1178,14 +1109,10 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
             fseek(tfile, lSize, SEEK_CUR);
 
             // read numverticesclothes
-            tempmuscle = muscles[i].numverticesclothes;
             funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
 
             // read verticesclothes
             if (muscles[i].numverticesclothes) {
-                //muscles[i].verticesclothes.clear();
-                //muscles[i].verticesclothes.resize(muscles[i].numverticesclothes);
-                //if(muscles[i].verticesclothes)dealloc2(muscles[i].verticesclothes);
                 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
                 edit = 0;
                 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
@@ -1214,14 +1141,6 @@ void Skeleton::Load(const char *filename,       const char *lowfilename, const c
             }
         }
 
-        /*FindForwards();
-        for(i=0;i<num_joints;i++){
-        joints[i].startpos=joints[i].position;
-        }
-        for(i=0;i<num_muscles;i++){
-        FindRotationMuscle(i,-1);
-        }*/
-
         // use opengl for its matrix math
         for (i = 0; i < modelclothes.vertexNum; i++) {
             modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
@@ -1340,24 +1259,24 @@ Skeleton::Skeleton()
     selected = 0;
 
     memset(forwardjoints, 0, sizeof(forwardjoints));
-    //         XYZ forward;
+    // XYZ forward;
 
     id = 0;
 
     memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
-    //         XYZ lowforward;
+    // XYZ lowforward;
 
-    //         XYZ specialforward[5];
+    // XYZ specialforward[5];
     memset(jointlabels, 0, sizeof(jointlabels));
 
-    //         Model model[7];
-    //         Model modellow;
-    //         Model modelclothes;
+    // Model model[7];
+    // Model modellow;
+    // Model modelclothes;
     num_models = 0;
 
-    //         Model drawmodel;
-    //         Model drawmodellow;
-    //         Model drawmodelclothes;
+    // Model drawmodel;
+    // Model drawmodellow;
+    // Model drawmodelclothes;
 
     clothes = 0;
     spinny = 0;
@@ -1428,9 +1347,7 @@ Animation & Animation::operator = (const Animation & ani)
 {
     int i = 0;
 
-    bool allocate = true;
-
-    allocate = ((ani.numframes != numframes) || (ani.joints != joints));
+    bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
 
     if (allocate)
         deallocate();
@@ -1440,6 +1357,7 @@ Animation & Animation::operator = (const Animation & ani)
     attack = ani.attack;
     joints = ani.joints;
     weapontargetnum = ani.weapontargetnum;
+    offset = ani.offset;
 
     if (allocate)
         position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
@@ -1593,14 +1511,6 @@ void Skeleton::Draw(int muscleview)
             glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
         }
     }
-    /*for(int i=0; i<num_joints; i++){
-    if(joints[i].hasparent){
-    glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
-    glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
-    glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
-    glVertex3f(joints[i].position.x+forward.x,joints[i].position.y+forward.y,joints[i].position.z+forward.z);
-    }
-    }*/
     for (int i = 0; i < num_muscles; i++) {
         if (muscles[i].type == boneconnect) {
             glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
@@ -1657,13 +1567,7 @@ void Skeleton::AddJoint(float x, float y, float z, int which)
         joints[num_joints].mass = 1;
         joints[num_joints].locked = 0;
 
-        /*if(which>=num_joints||which<0)*/
         joints[num_joints].hasparent = 0;
-        /*if(which<num_joints&&which>=0){
-        joints[num_joints].parent=&joints[which];
-        joints[num_joints].hasparent=1;
-        joints[num_joints].length=findDistance(joints[num_joints].position,joints[num_joints].parent->position);
-        }*/
         num_joints++;
         if (which < num_joints && which >= 0)
             AddMuscle(num_joints - 1, which, 0, 10, boneconnect);