]> git.jsancho.org Git - lugaru.git/blob - Source/Account.cpp
145dea1bdf9cc7ee1dfef1c0f037780637f52dd6
[lugaru.git] / Source / Account.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010 - Côme <MCMic> BERNIGAUD
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
15
16 See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 */
22
23 #include "Account.h"
24 #include "binio.h"
25 #include <fstream>
26 #include "MacCompatibility.h"
27 #include "string.h"
28
29 using namespace std;
30
31 extern bool debugmode;
32
33 vector<Account*> Account::accounts = vector<Account*>();
34
35 Account::Account(string n) {
36         name = string(n);
37         difficulty = 0;
38         progress = 0;
39         points = 0;
40         memset(highscore, 0, sizeof(highscore));
41         memset(fasttime, 0, sizeof(fasttime));
42         memset(unlocked, 0, sizeof(unlocked));
43         campaignhighscore = 0;
44         campaignfasttime = 0;
45         campaignscore = 0;
46         campaigntime = 0;
47 }
48
49 Account* Account::add(string name) {
50         accounts.push_back(new Account(name));
51         return accounts.back();
52 }
53
54 Account* Account::get(int i) {
55         
56         if((i>=0)&&(i<accounts.size())) {
57                 return accounts[i];
58         } else
59                 return NULL;
60 }
61
62 void Account::destroy(int i) {
63         accounts.erase(accounts.begin()+i);
64 }
65 Account* Account::destroy(Account* a) {
66         for(int i=0; i<accounts.size(); i++) {
67                 if(accounts[i]==a) {
68                         accounts.erase(accounts.begin()+i);
69                         return NULL;
70                 }
71         }
72         printf("Unexpected error : User %s not found %d\n",a->getName(),a);
73         return accounts.front();
74 }
75
76 int Account::getDifficulty() {
77         return difficulty;
78 }
79
80 void Account::endGame() {
81         campaignchoices.clear();
82         campaignscore=0;
83         campaigntime=0;
84 }
85
86 void Account::winCampaignLevel(int choice, float score, float time) {
87         campaignchoices.push_back(choice);
88         setCampaignScore(campaignscore+score);
89         campaigntime = time;
90 }
91
92 void Account::winLevel(int level, float score, float time) {
93         if(!debugmode) {
94                 if(score>highscore[level])
95                         highscore[level]=score;
96                 if(time<fasttime[level]||fasttime[level]==0)
97                         fasttime[level]=time;
98         }
99         if(progress<level+1)
100                 progress=level+1;
101 }
102
103 Account* Account::loadFile(string filename) {
104         FILE *tfile;
105         int numaccounts;
106         int accountactive;
107         int j;
108         
109         tfile=fopen(ConvertFileName(filename.c_str()), "rb" );
110         
111         if(tfile)
112         {
113                 funpackf(tfile, "Bi", &numaccounts);
114                 funpackf(tfile, "Bi", &accountactive);
115                 printf("number of accounts %d\n",numaccounts);
116                 for(int i=0;i<numaccounts;i++)
117                 {
118                         printf("loading account %d/%d\n",i,numaccounts);
119                         Account* acc = new Account();
120                         funpackf(tfile, "Bf", &(acc->campaigntime));
121                         funpackf(tfile, "Bf", &(acc->campaignscore));
122                         funpackf(tfile, "Bf", &(acc->campaignfasttime));
123                         funpackf(tfile, "Bf", &(acc->campaignhighscore));
124                         funpackf(tfile, "Bi", &(acc->difficulty));
125                         funpackf(tfile, "Bi", &(acc->progress));
126                         int campaignchoicesmade,campaignchoice;
127                         funpackf(tfile, "Bi", &campaignchoicesmade);
128                         for(j=0;j<campaignchoicesmade;j++)
129                         {
130                                 funpackf(tfile, "Bi", &campaignchoice);
131                                 if (campaignchoice >= 10) // what is that for?
132                                 {
133                                         campaignchoice = 0;
134                                 }
135                                 acc->campaignchoices.push_back(campaignchoice);
136                         }
137                         funpackf(tfile, "Bf", &(acc->points));
138                         for(j=0;j<50;j++)
139                         {
140                                 funpackf(tfile, "Bf", &(acc->highscore[j]));
141                                 funpackf(tfile, "Bf", &(acc->fasttime[j]));
142                         }
143                         for(j=0;j<60;j++)
144                         {
145                                 funpackf(tfile, "Bb",  &(acc->unlocked[j]));
146                         }
147                         int temp;
148                         char ctemp;
149                         funpackf(tfile, "Bi",  &temp);
150                         for(j=0;j<temp;j++)
151                         {
152                                 funpackf(tfile, "Bb",  &ctemp);
153                                 acc->name.append(1,ctemp);
154                         }
155                         if(!strcmp(acc->name.c_str(),""))
156                                 acc->name="Lugaru Player"; // no empty player name security.
157                         accounts.push_back(acc);
158                 }
159
160                 fclose(tfile);
161                 return get(accountactive);
162         } else {
163                 printf("filenotfound\n");
164                 return NULL;
165         }
166 }
167
168 void Account::saveFile(string filename, Account* accountactive) {
169         FILE *tfile;
170         int j;
171         
172         tfile=fopen(ConvertFileName(filename.c_str(), "wb"), "wb" );
173         if(tfile)
174         {
175                 printf("writing %d accounts :\n",getNbAccounts());
176                 fpackf(tfile, "Bi", getNbAccounts());
177                 fpackf(tfile, "Bi", indice(accountactive));
178                 
179                 for(int i=0;i<getNbAccounts();i++)
180                 {
181                         Account* a = Account::get(i);
182                         printf("writing account %d/%d (%s)\n",i+1,getNbAccounts(),a->getName());
183                         fpackf(tfile, "Bf", a->campaigntime);
184                         fpackf(tfile, "Bf", a->campaignscore);
185                         fpackf(tfile, "Bf", a->campaignfasttime);
186                         fpackf(tfile, "Bf", a->campaignhighscore);
187                         fpackf(tfile, "Bi", a->difficulty);
188                         fpackf(tfile, "Bi", a->progress);
189                         fpackf(tfile, "Bi", a->getCampaignChoicesMade());
190                         for(j=0;j<a->getCampaignChoicesMade();j++)
191                         {
192                                 fpackf(tfile, "Bi", a->campaignchoices[j]);
193                         }
194                         fpackf(tfile, "Bf", a->points);
195                         for(j=0;j<50;j++)
196                         {
197                                 fpackf(tfile, "Bf", a->highscore[j]);
198                                 fpackf(tfile, "Bf", a->fasttime[j]);
199                         }
200                         for(j=0;j<60;j++)
201                         {
202                                 fpackf(tfile, "Bb",  a->unlocked[j]);
203                         }
204                         fpackf(tfile, "Bi",  a->name.size());
205                         for(j=0;j<a->name.size();j++)
206                         {
207                                 fpackf(tfile, "Bb",  a->name[j]);
208                         }
209                 }
210
211                 fclose(tfile);
212         }
213 }
214
215 int Account::indice(Account* a) {
216         for(int i=0; i < accounts.size(); i++) {
217                 if(accounts[i]==a)
218                         return i;
219         }
220         return -1;
221 }