]> git.jsancho.org Git - lugaru.git/blob - Source/Account.cpp
beb5f46a041787aae4e675b1588de9449ab21584
[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) : campaignProgress() {
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         
44         setCurrentCampaign("main");
45 }
46
47 void Account::setCurrentCampaign(string name) {
48         currentCampaign = name;
49         campaignProgress[name].highscore = 0;
50         campaignProgress[name].fasttime = 0;
51         campaignProgress[name].score = 0;
52         campaignProgress[name].time = 0;
53 }
54
55 Account* Account::add(string name) {
56         accounts.push_back(new Account(name));
57         return accounts.back();
58 }
59
60 Account* Account::get(int i) {
61         
62         if((i>=0)&&(i<accounts.size())) {
63                 return accounts[i];
64         } else
65                 return NULL;
66 }
67
68 void Account::destroy(int i) {
69         accounts.erase(accounts.begin()+i);
70 }
71 Account* Account::destroy(Account* a) {
72         for(int i=0; i<accounts.size(); i++) {
73                 if(accounts[i]==a) {
74                         accounts.erase(accounts.begin()+i);
75                         return NULL;
76                 }
77         }
78         printf("Unexpected error : User %s not found %d\n",a->getName(),a);
79         return accounts.front();
80 }
81
82 int Account::getDifficulty() {
83         return difficulty;
84 }
85
86 void Account::endGame() {
87         campaignProgress[currentCampaign].choices.clear();
88         campaignProgress[currentCampaign].score=0;
89         campaignProgress[currentCampaign].time=0;
90 }
91
92 void Account::winCampaignLevel(int choice, float score, float time) {
93         campaignProgress[currentCampaign].choices.push_back(choice);
94         setCampaignScore(campaignProgress[currentCampaign].score+score);
95         campaignProgress[currentCampaign].time = time;
96 }
97
98 void Account::winLevel(int level, float score, float time) {
99         if(!debugmode) {
100                 if(score>highscore[level])
101                         highscore[level]=score;
102                 if(time<fasttime[level]||fasttime[level]==0)
103                         fasttime[level]=time;
104         }
105         if(progress<level+1)
106                 progress=level+1;
107 }
108
109 Account* Account::loadFile(string filename) {
110         FILE *tfile;
111         int numaccounts;
112         int accountactive;
113         
114         tfile=fopen(ConvertFileName(filename.c_str()), "rb" );
115         
116         if(tfile)
117         {
118                 funpackf(tfile, "Bi", &numaccounts);
119                 funpackf(tfile, "Bi", &accountactive);
120                 printf("number of accounts %d\n",numaccounts);
121                 for(int i=0;i<numaccounts;i++)
122                 {
123                         printf("loading account %d/%d\n",i,numaccounts);
124                         Account* acc = new Account();
125                         funpackf(tfile, "Bi", &(acc->difficulty));
126                         funpackf(tfile, "Bi", &(acc->progress));
127                         int nbCampaigns;
128                         funpackf(tfile, "Bi", &nbCampaigns);
129                         printf("loading %d campaign progress info\n",nbCampaigns);
130                         
131                         for(int k=0;k<nbCampaigns;++k) {
132                                 string campaignName = "";
133                                 int t;
134                                 char c;
135                                 funpackf(tfile, "Bi",  &t);
136                                 for(int j=0;j<t;j++)
137                                 {
138                                         funpackf(tfile, "Bb",  &c);
139                                         campaignName.append(1,c);
140                                 }
141                                 printf("loading %s campaign progress info\n",campaignName.c_str());
142                                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].time));
143                                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].score));
144                                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].fasttime));
145                                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].highscore));
146                                 int campaignchoicesmade,campaignchoice;
147                                 funpackf(tfile, "Bi", &campaignchoicesmade);
148                                 for(int j=0;j<campaignchoicesmade;j++)
149                                 {
150                                         funpackf(tfile, "Bi", &campaignchoice);
151                                         if (campaignchoice >= 10) // what is that for?
152                                         {
153                                                 campaignchoice = 0;
154                                         }
155                                         acc->campaignProgress[campaignName].choices.push_back(campaignchoice);
156                                 }
157                         }
158                         
159                         funpackf(tfile, "Bf", &(acc->points));
160                         for(int i=0;i<50;i++)
161                         {
162                                 funpackf(tfile, "Bf", &(acc->highscore[i]));
163                                 funpackf(tfile, "Bf", &(acc->fasttime[i]));
164                         }
165                         for(int i=0;i<60;i++)
166                         {
167                                 funpackf(tfile, "Bb",  &(acc->unlocked[i]));
168                         }
169                         int temp;
170                         char ctemp;
171                         funpackf(tfile, "Bi",  &temp);
172                         for(int i=0;i<temp;i++)
173                         {
174                                 funpackf(tfile, "Bb",  &ctemp);
175                                 acc->name.append(1,ctemp);
176                         }
177                         if(!strcmp(acc->name.c_str(),""))
178                                 acc->name="Lugaru Player"; // no empty player name security.
179                         accounts.push_back(acc);
180                 }
181
182                 fclose(tfile);
183                 return get(accountactive);
184         } else {
185                 printf("filenotfound\n");
186                 return NULL;
187         }
188 }
189
190 void Account::saveFile(string filename, Account* accountactive) {
191         FILE *tfile;
192         int j;
193         
194         tfile=fopen(ConvertFileName(filename.c_str(), "wb"), "wb" );
195         if(tfile)
196         {
197                 printf("writing %d accounts :\n",getNbAccounts());
198                 fpackf(tfile, "Bi", getNbAccounts());
199                 fpackf(tfile, "Bi", indice(accountactive));
200                 
201                 for(int i=0;i<getNbAccounts();i++)
202                 {
203                         Account* a = Account::get(i);
204                         printf("writing account %d/%d (%s)\n",i+1,getNbAccounts(),a->getName());
205                         fpackf(tfile, "Bi", a->difficulty);
206                         fpackf(tfile, "Bi", a->progress);
207                         fpackf(tfile, "Bi", a->campaignProgress.size());
208                         
209                         map<string,campaign_progress_t>::const_iterator it;
210                         for( it=a->campaignProgress.begin(); it!= a->campaignProgress.end(); ++it) {
211                                 fpackf(tfile, "Bi",  it->first.size());
212                                 for(j=0;j<it->first.size();j++)
213                                 {
214                                         fpackf(tfile, "Bb",  it->first[j]);
215                                 }
216                                 fpackf(tfile, "Bf", it->second.time);
217                                 fpackf(tfile, "Bf", it->second.score);
218                                 fpackf(tfile, "Bf", it->second.fasttime);
219                                 fpackf(tfile, "Bf", it->second.highscore);
220                                 fpackf(tfile, "Bi", it->second.choices.size());
221                                 for(j=0;j<it->second.choices.size();j++)
222                                 {
223                                         fpackf(tfile, "Bi", it->second.choices[j]);
224                                 }
225                         }
226                         
227                         fpackf(tfile, "Bf", a->points);
228                         for(j=0;j<50;j++)
229                         {
230                                 fpackf(tfile, "Bf", a->highscore[j]);
231                                 fpackf(tfile, "Bf", a->fasttime[j]);
232                         }
233                         for(j=0;j<60;j++)
234                         {
235                                 fpackf(tfile, "Bb",  a->unlocked[j]);
236                         }
237                         fpackf(tfile, "Bi",  a->name.size());
238                         for(j=0;j<a->name.size();j++)
239                         {
240                                 fpackf(tfile, "Bb",  a->name[j]);
241                         }
242                 }
243
244                 fclose(tfile);
245         }
246 }
247
248 int Account::indice(Account* a) {
249         for(int i=0; i < accounts.size(); i++) {
250                 if(accounts[i]==a)
251                         return i;
252         }
253         return -1;
254 }