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