]> git.jsancho.org Git - lugaru.git/blobdiff - Source/User/Account.cpp
Fixes #40 Scores are now integers
[lugaru.git] / Source / User / Account.cpp
index ff4b04f52c0020341385109e300d66c77893a3cb..50195f02f9731614842532190234ab6c636faaa1 100644 (file)
@@ -18,12 +18,14 @@ You should have received a copy of the GNU General Public License
 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "User/Account.h"
+#include "User/Account.hpp"
+
+#include "MacCompatibility.hpp"
 #include "Utils/binio.h"
-#include "MacCompatibility.h"
+
 #include <fstream>
-#include "string.h"
 #include <iostream>
+#include <string.h>
 
 using namespace std;
 
@@ -32,7 +34,9 @@ extern bool devtools;
 vector<Account> Account::accounts;
 int Account::i_active = -1;
 
-Account::Account(const string& name) : name(name), campaignProgress()
+Account::Account(const string& name)
+    : name(name)
+    , campaignProgress()
 {
     difficulty = 0;
     progress = 0;
@@ -44,7 +48,8 @@ Account::Account(const string& name) : name(name), campaignProgress()
     setCurrentCampaign("main");
 }
 
-Account::Account(FILE* tfile) : Account("")
+Account::Account(FILE* tfile)
+    : Account("")
 {
     funpackf(tfile, "Bi", &difficulty);
     funpackf(tfile, "Bi", &progress);
@@ -55,15 +60,18 @@ Account::Account(FILE* tfile) : Account("")
         string campaignName = "";
         int t;
         char c;
-        funpackf(tfile, "Bi",  &t);
+        funpackf(tfile, "Bi", &t);
         for (int j = 0; j < t; j++) {
-            funpackf(tfile, "Bb",  &c);
+            funpackf(tfile, "Bb", &c);
             campaignName.append(1, c);
         }
+        float fscore, fhighscore;
         funpackf(tfile, "Bf", &(campaignProgress[campaignName].time));
-        funpackf(tfile, "Bf", &(campaignProgress[campaignName].score));
+        funpackf(tfile, "Bf", &(fscore));
         funpackf(tfile, "Bf", &(campaignProgress[campaignName].fasttime));
-        funpackf(tfile, "Bf", &(campaignProgress[campaignName].highscore));
+        funpackf(tfile, "Bf", &(fhighscore));
+        campaignProgress[campaignName].score = fscore;
+        campaignProgress[campaignName].highscore = fhighscore;
         int campaignchoicesmade, campaignchoice;
         funpackf(tfile, "Bi", &campaignchoicesmade);
         for (int j = 0; j < campaignchoicesmade; j++) {
@@ -78,25 +86,27 @@ Account::Account(FILE* tfile) : Account("")
     currentCampaign = "";
     int t;
     char c;
-    funpackf(tfile, "Bi",  &t);
+    funpackf(tfile, "Bi", &t);
     for (int i = 0; i < t; i++) {
-        funpackf(tfile, "Bb",  &c);
+        funpackf(tfile, "Bb", &c);
         currentCampaign.append(1, c);
     }
 
     funpackf(tfile, "Bf", &points);
     for (int i = 0; i < 50; i++) {
-        funpackf(tfile, "Bf", &(highscore[i]));
+        float fscore;
+        funpackf(tfile, "Bf", &(fscore));
         funpackf(tfile, "Bf", &(fasttime[i]));
+        highscore[i] = fscore;
     }
     for (int i = 0; i < 60; i++) {
-        funpackf(tfile, "Bb",  &(unlocked[i]));
+        funpackf(tfile, "Bb", &(unlocked[i]));
     }
     int temp;
     char ctemp;
-    funpackf(tfile, "Bi",  &temp);
+    funpackf(tfile, "Bi", &temp);
     for (int i = 0; i < temp; i++) {
-        funpackf(tfile, "Bb",  &ctemp);
+        funpackf(tfile, "Bb", &ctemp);
         name.append(1, ctemp);
     }
     if (name.empty()) {
@@ -112,14 +122,14 @@ void Account::save(FILE* tfile)
 
     map<string, CampaignProgress>::const_iterator it;
     for (it = campaignProgress.begin(); it != campaignProgress.end(); ++it) {
-        fpackf(tfile, "Bi",  it->first.size());
+        fpackf(tfile, "Bi", it->first.size());
         for (unsigned j = 0; j < it->first.size(); j++) {
-            fpackf(tfile, "Bb",  it->first[j]);
+            fpackf(tfile, "Bb", it->first[j]);
         }
         fpackf(tfile, "Bf", it->second.time);
-        fpackf(tfile, "Bf", it->second.score);
+        fpackf(tfile, "Bf", float(it->second.score));
         fpackf(tfile, "Bf", it->second.fasttime);
-        fpackf(tfile, "Bf", it->second.highscore);
+        fpackf(tfile, "Bf", float(it->second.highscore));
         fpackf(tfile, "Bi", it->second.choices.size());
         for (unsigned j = 0; j < it->second.choices.size(); j++) {
             fpackf(tfile, "Bi", it->second.choices[j]);
@@ -133,15 +143,15 @@ void Account::save(FILE* tfile)
 
     fpackf(tfile, "Bf", points);
     for (unsigned j = 0; j < 50; j++) {
-        fpackf(tfile, "Bf", highscore[j]);
+        fpackf(tfile, "Bf", float(highscore[j]));
         fpackf(tfile, "Bf", fasttime[j]);
     }
     for (unsigned j = 0; j < 60; j++) {
-        fpackf(tfile, "Bb",  unlocked[j]);
+        fpackf(tfile, "Bb", unlocked[j]);
     }
-    fpackf(tfile, "Bi",  name.size());
+    fpackf(tfile, "Bi", name.size());
     for (unsigned j = 0; j < name.size(); j++) {
-        fpackf(tfile, "Bb",  name[j]);
+        fpackf(tfile, "Bb", name[j]);
     }
 }
 
@@ -209,33 +219,36 @@ void Account::endGame()
     campaignProgress[currentCampaign].time = 0;
 }
 
-void Account::winCampaignLevel(int choice, float score, float time)
+void Account::winCampaignLevel(int choice, int score, float time)
 {
     campaignProgress[currentCampaign].choices.push_back(choice);
     setCampaignScore(campaignProgress[currentCampaign].score + score);
     campaignProgress[currentCampaign].time = time;
 }
 
-void Account::winLevel(int level, float score, float time)
+void Account::winLevel(int level, int score, float time)
 {
     if (!devtools) {
-        if (score > highscore[level])
+        if (score > highscore[level]) {
             highscore[level] = score;
-        if (time < fasttime[level] || fasttime[level] == 0)
+        }
+        if (time < fasttime[level] || fasttime[level] == 0) {
             fasttime[level] = time;
+        }
     }
-    if (progress < level + 1)
+    if (progress < level + 1) {
         progress = level + 1;
+    }
 }
 
 void Account::loadFile(string filename)
 {
-    FILE *tfile;
+    FILEtfile;
     int numaccounts;
     int iactive;
     errno = 0;
 
-    tfile = fopen(filename.c_str(), "rb" );
+    tfile = fopen(filename.c_str(), "rb");
 
     if (tfile) {
         funpackf(tfile, "Bi", &numaccounts);
@@ -256,10 +269,10 @@ void Account::loadFile(string filename)
 
 void Account::saveFile(string filename)
 {
-    FILE *tfile;
+    FILEtfile;
     errno = 0;
 
-    tfile = fopen(filename.c_str(), "wb" );
+    tfile = fopen(filename.c_str(), "wb");
     if (tfile) {
         fpackf(tfile, "Bi", getNbAccounts());
         fpackf(tfile, "Bi", i_active);