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