]> git.jsancho.org Git - lugaru.git/commitdiff
Moved Person loading from file into a Person constructor
authorCôme Chilliet <come@chilliet.eu>
Tue, 29 Nov 2016 05:11:28 +0000 (12:11 +0700)
committerCôme Chilliet <come@chilliet.eu>
Tue, 29 Nov 2016 05:20:16 +0000 (12:20 +0700)
Source/GameTick.cpp
Source/Person.cpp
Source/Person.h

index a251430198746370c04b231cf3f8efebe1d6b6ee..85514c32be90e4b7bedfa6af0a74dbc85edf77ad 100644 (file)
@@ -788,8 +788,6 @@ void Game::Loadlevel(const char *name)
     static const char *pfx = ":Data:Maps:";
     char *buf;
 
-    float headprop, legprop, armprop, bodyprop;
-
     LOGFUNC;
 
     LOG(std::string("Loading level...") + name);
@@ -912,6 +910,7 @@ void Game::Loadlevel(const char *name)
         }
 
         weapons.clear();
+        Person::players.resize(1);
 
         funpackf(tfile, "Bi", &mapvers);
         if (mapvers >= 15)
@@ -1027,8 +1026,9 @@ void Game::Loadlevel(const char *name)
                     funpackf(tfile, "Bf Bf", &dialoguecamerayaw[k][l], &dialoguecamerapitch[k][l]);
                 }
             }
-        } else
+        } else {
             numdialogues = 0;
+        }
 
         for (int k = 0; k < Person::players[0]->numclothes; k++) {
             funpackf(tfile, "Bi", &templength);
@@ -1091,112 +1091,20 @@ void Game::Loadlevel(const char *name)
 
         int numplayers;
         funpackf(tfile, "Bi", &numplayers);
-        int howmanyremoved = 0;
-        bool removeanother = 0;
         if (numplayers > maxplayers) {
             cout << "Warning: this level contains more players than allowed" << endl;
         }
-        if (numplayers > 1) {
-            for (int i = 1; i < numplayers; i++) {
-                Person::players.push_back(shared_ptr<Person>(new Person()));
-                if (visibleloading)
-                    LoadingScreen();
-                removeanother = 0;
-
-                funpackf(tfile, "Bi Bi Bf Bf Bf Bi", &Person::players[i - howmanyremoved]->whichskin, &Person::players[i - howmanyremoved]->creature, &Person::players[i - howmanyremoved]->coords.x, &Person::players[i - howmanyremoved]->coords.y, &Person::players[i - howmanyremoved]->coords.z, &Person::players[i - howmanyremoved]->num_weapons);
-                if (mapvers >= 5)
-                    funpackf(tfile, "Bi", &Person::players[i - howmanyremoved]->howactive);
-                else
-                    Person::players[i - howmanyremoved]->howactive = typeactive;
-                if (mapvers >= 3)
-                    funpackf(tfile, "Bf", &Person::players[i - howmanyremoved]->scale);
-                else
-                    Person::players[i - howmanyremoved]->scale = -1;
-                if (mapvers >= 11)
-                    funpackf(tfile, "Bb", &Person::players[i - howmanyremoved]->immobile);
-                else
-                    Person::players[i - howmanyremoved]->immobile = 0;
-                if (mapvers >= 12)
-                    funpackf(tfile, "Bf", &Person::players[i - howmanyremoved]->yaw);
-                else
-                    Person::players[i - howmanyremoved]->yaw = 0;
-                Person::players[i - howmanyremoved]->targetyaw = Person::players[i - howmanyremoved]->yaw;
-                if (Person::players[i - howmanyremoved]->num_weapons < 0 || Person::players[i - howmanyremoved]->num_weapons > 5) {
-                    removeanother = 1;
-                    howmanyremoved++;
-                }
-                if (!removeanother) {
-                    if (Person::players[i - howmanyremoved]->num_weapons > 0 && Person::players[i - howmanyremoved]->num_weapons < 5) {
-                        for (int j = 0; j < Person::players[i - howmanyremoved]->num_weapons; j++) {
-                            Person::players[i - howmanyremoved]->weaponids[j] = weapons.size();
-                            int type;
-                            funpackf(tfile, "Bi", &type);
-                            weapons.push_back(Weapon(type, i));
-                        }
-                    }
-                    funpackf(tfile, "Bi", &Person::players[i - howmanyremoved]->numwaypoints);
-                    for (int j = 0; j < Person::players[i - howmanyremoved]->numwaypoints; j++) {
-                        funpackf(tfile, "Bf", &Person::players[i - howmanyremoved]->waypoints[j].x);
-                        funpackf(tfile, "Bf", &Person::players[i - howmanyremoved]->waypoints[j].y);
-                        funpackf(tfile, "Bf", &Person::players[i - howmanyremoved]->waypoints[j].z);
-                        if (mapvers >= 5)
-                            funpackf(tfile, "Bi", &Person::players[i - howmanyremoved]->waypointtype[j]);
-                        else
-                            Person::players[i - howmanyremoved]->waypointtype[j] = wpkeepwalking;
-                    }
-
-                    funpackf(tfile, "Bi", &Person::players[i - howmanyremoved]->waypoint);
-                    if (Person::players[i - howmanyremoved]->waypoint > Person::players[i - howmanyremoved]->numwaypoints - 1)
-                        Person::players[i - howmanyremoved]->waypoint = 0;
-
-                    funpackf(tfile, "Bf Bf Bf", &Person::players[i - howmanyremoved]->armorhead, &Person::players[i - howmanyremoved]->armorhigh, &Person::players[i - howmanyremoved]->armorlow);
-                    funpackf(tfile, "Bf Bf Bf", &Person::players[i - howmanyremoved]->protectionhead, &Person::players[i - howmanyremoved]->protectionhigh, &Person::players[i - howmanyremoved]->protectionlow);
-                    funpackf(tfile, "Bf Bf Bf", &Person::players[i - howmanyremoved]->metalhead, &Person::players[i - howmanyremoved]->metalhigh, &Person::players[i - howmanyremoved]->metallow);
-                    funpackf(tfile, "Bf Bf", &Person::players[i - howmanyremoved]->power, &Person::players[i - howmanyremoved]->speedmult);
-
-                    if (mapvers >= 4)
-                        funpackf(tfile, "Bf Bf Bf Bf", &headprop, &bodyprop, &armprop, &legprop);
-                    else {
-                        headprop = 1;
-                        bodyprop = 1;
-                        armprop = 1;
-                        legprop = 1;
-                    }
-                    if (Person::players[i - howmanyremoved]->creature == wolftype) {
-                        Person::players[i - howmanyremoved]->proportionhead = 1.1 * headprop;
-                        Person::players[i - howmanyremoved]->proportionbody = 1.1 * bodyprop;
-                        Person::players[i - howmanyremoved]->proportionarms = 1.1 * armprop;
-                        Person::players[i - howmanyremoved]->proportionlegs = 1.1 * legprop;
-                    }
-
-                    if (Person::players[i - howmanyremoved]->creature == rabbittype) {
-                        Person::players[i - howmanyremoved]->proportionhead = 1.2 * headprop;
-                        Person::players[i - howmanyremoved]->proportionbody = 1.05 * bodyprop;
-                        Person::players[i - howmanyremoved]->proportionarms = 1.00 * armprop;
-                        Person::players[i - howmanyremoved]->proportionlegs = 1.1 * legprop;
-                        Person::players[i - howmanyremoved]->proportionlegs.y = 1.05 * legprop;
-                    }
-
-                    funpackf(tfile, "Bi", &Person::players[i - howmanyremoved]->numclothes);
-                    if (Person::players[i - howmanyremoved]->numclothes) {
-                        for (int k = 0; k < Person::players[i - howmanyremoved]->numclothes; k++) {
-                            int templength;
-                            funpackf(tfile, "Bi", &templength);
-                            for (int l = 0; l < templength; l++)
-                                funpackf(tfile, "Bb", &Person::players[i - howmanyremoved]->clothes[k][l]);
-                            Person::players[i - howmanyremoved]->clothes[k][templength] = '\0';
-                            funpackf(tfile, "Bf Bf Bf", &Person::players[i - howmanyremoved]->clothestintr[k], &Person::players[i - howmanyremoved]->clothestintg[k], &Person::players[i - howmanyremoved]->clothestintb[k]);
-                        }
-                    }
-                }
+        for (int i = 1; i < numplayers; i++) {
+            unsigned j = 1;
+            try {
+                Person::players.push_back(shared_ptr<Person>(new Person(tfile, mapvers, j)));
+                j++;
+            } catch (InvalidPersonException e) {
             }
         }
         if (visibleloading)
             LoadingScreen();
 
-        numplayers -= howmanyremoved;
-        Person::players.resize(numplayers);
-
         funpackf(tfile, "Bi", &numpathpoints);
         if (numpathpoints > 30 || numpathpoints < 0)
             numpathpoints = 0;
index 0100f8b0ba3210277dbc95bf6f1d80b76fefbc1a..9aff0dcc55e8f8066e52fa3ca97ff4113f7e70b1 100644 (file)
@@ -308,6 +308,100 @@ Person::Person() :
 {
 }
 
+/* Read a person in tfile. Throws an error if it’s not valid */
+Person::Person(FILE *tfile, int mapvers, unsigned i) : Person()
+{
+    id = i;
+    funpackf(tfile, "Bi Bi Bf Bf Bf Bi", &whichskin, &creature, &coords.x, &coords.y, &coords.z, &num_weapons);
+    if (mapvers >= 5) {
+        funpackf(tfile, "Bi", &howactive);
+    } else {
+        howactive = typeactive;
+    }
+    if (mapvers >= 3) {
+        funpackf(tfile, "Bf", &scale);
+    } else {
+        scale = -1;
+    }
+    if (mapvers >= 11) {
+        funpackf(tfile, "Bb", &immobile);
+    } else {
+        immobile = 0;
+    }
+    if (mapvers >= 12) {
+        funpackf(tfile, "Bf", &yaw);
+    } else {
+        yaw = 0;
+    }
+    targetyaw = yaw;
+    if (num_weapons < 0 || num_weapons > 5) {
+        throw InvalidPersonException();
+    }
+    if (num_weapons > 0 && num_weapons < 5) {
+        for (int j = 0; j < num_weapons; j++) {
+            weaponids[j] = weapons.size();
+            int type;
+            funpackf(tfile, "Bi", &type);
+            weapons.push_back(Weapon(type, id));
+        }
+    }
+    funpackf(tfile, "Bi", &numwaypoints);
+    for (int j = 0; j < numwaypoints; j++) {
+        funpackf(tfile, "Bf", &waypoints[j].x);
+        funpackf(tfile, "Bf", &waypoints[j].y);
+        funpackf(tfile, "Bf", &waypoints[j].z);
+        if (mapvers >= 5) {
+            funpackf(tfile, "Bi", &waypointtype[j]);
+        } else {
+            waypointtype[j] = wpkeepwalking;
+        }
+    }
+
+    funpackf(tfile, "Bi", &waypoint);
+    if (waypoint > (numwaypoints - 1)) {
+        waypoint = 0;
+    }
+
+    funpackf(tfile, "Bf Bf Bf", &armorhead, &armorhigh, &armorlow);
+    funpackf(tfile, "Bf Bf Bf", &protectionhead, &protectionhigh, &protectionlow);
+    funpackf(tfile, "Bf Bf Bf", &metalhead, &metalhigh, &metallow);
+    funpackf(tfile, "Bf Bf", &power, &speedmult);
+
+    float headprop, legprop, armprop, bodyprop;
+
+    if (mapvers >= 4) {
+        funpackf(tfile, "Bf Bf Bf Bf", &headprop, &bodyprop, &armprop, &legprop);
+    } else {
+        headprop = 1;
+        bodyprop = 1;
+        armprop = 1;
+        legprop = 1;
+    }
+
+    if (creature == wolftype) {
+        proportionhead = 1.1 * headprop;
+        proportionbody = 1.1 * bodyprop;
+        proportionarms = 1.1 * armprop;
+        proportionlegs = 1.1 * legprop;
+    } else if (creature == rabbittype) {
+        proportionhead = 1.2 * headprop;
+        proportionbody = 1.05 * bodyprop;
+        proportionarms = 1.00 * armprop;
+        proportionlegs = 1.1 * legprop;
+        proportionlegs.y = 1.05 * legprop;
+    }
+
+    funpackf(tfile, "Bi", &numclothes);
+    for (int k = 0; k < numclothes; k++) {
+        int templength;
+        funpackf(tfile, "Bi", &templength);
+        for (int l = 0; l < templength; l++)
+            funpackf(tfile, "Bb", &clothes[k][l]);
+        clothes[k][templength] = '\0';
+        funpackf(tfile, "Bf Bf Bf", &clothestintr[k], &clothestintg[k], &clothestintb[k]);
+    }
+}
+
 /* EFFECT
  *
  * USES:
@@ -6239,7 +6333,6 @@ int Person::DrawSkeleton()
             if (terrainheight > 1.7)
                 terrainheight = 1.7;
 
-            //burnt=0;
             glColor4f((1 - (1 - terrainlight.x) / terrainheight) - burnt, (1 - (1 - terrainlight.y) / terrainheight) - burnt, (1 - (1 - terrainlight.z) / terrainheight) - burnt, distance);
             glDisable(GL_BLEND);
             glAlphaFunc(GL_GREATER, 0.0001);
index 0c030ab3cae27c6962de5511b886be3fb6c2e640..3afbdca6d68e9699fbf976d759c634783a135edd 100644 (file)
@@ -47,6 +47,12 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 #define rabbittype 0
 #define wolftype 1
 
+struct InvalidPersonException : public exception {
+   const char * what () const throw () {
+      return "Invalid weapon number";
+   }
+};
+
 class Person : public enable_shared_from_this<Person>
 {
 public:
@@ -315,6 +321,7 @@ public:
     bool jumpclimb;
 
     Person();
+    Person(FILE*, int, unsigned);
 
     // convenience functions
     inline Joint& joint(int bodypart) { return skeleton.joints[skeleton.jointlabels[bodypart]]; }