2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010 - Côme <MCMic> BERNIGAUD
5 This file is part of Lugaru.
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.
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.
16 See the GNU General Public License for more details.
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.
26 #include "MacCompatibility.h"
31 extern bool debugmode;
33 vector<Account*> Account::accounts = vector<Account*>();
35 Account::Account(string n) : campaignProgress() {
40 memset(highscore, 0, sizeof(highscore));
41 memset(fasttime, 0, sizeof(fasttime));
42 memset(unlocked, 0, sizeof(unlocked));
44 setCurrentCampaign("main");
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;
57 Account* Account::add(string name) {
58 accounts.push_back(new Account(name));
59 return accounts.back();
62 Account* Account::get(int i) {
64 if((i>=0)&&(i<accounts.size())) {
70 void Account::destroy(int i) {
71 accounts.erase(accounts.begin()+i);
73 Account* Account::destroy(Account* a) {
74 for(int i=0; i<accounts.size(); i++) {
76 accounts.erase(accounts.begin()+i);
80 printf("Unexpected error : User %s not found\n",a->getName());
81 return accounts.front();
84 int Account::getDifficulty() {
88 void Account::endGame() {
89 campaignProgress[currentCampaign].choices.clear();
90 campaignProgress[currentCampaign].score=0;
91 campaignProgress[currentCampaign].time=0;
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;
100 void Account::winLevel(int level, float score, float time) {
102 if(score>highscore[level])
103 highscore[level]=score;
104 if(time<fasttime[level]||fasttime[level]==0)
105 fasttime[level]=time;
111 Account* Account::loadFile(string filename) {
116 tfile=fopen(ConvertFileName(filename.c_str()), "rb" );
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++)
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));
130 funpackf(tfile, "Bi", &nbCampaigns);
131 printf("loading %d campaign progress info\n",nbCampaigns);
133 for(int k=0;k<nbCampaigns;++k) {
134 string campaignName = "";
137 funpackf(tfile, "Bi", &t);
140 funpackf(tfile, "Bb", &c);
141 campaignName.append(1,c);
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++)
152 funpackf(tfile, "Bi", &campaignchoice);
153 if (campaignchoice >= 10) // what is that for?
157 acc->campaignProgress[campaignName].choices.push_back(campaignchoice);
161 acc->currentCampaign = "";
164 funpackf(tfile, "Bi", &t);
167 funpackf(tfile, "Bb", &c);
168 acc->currentCampaign.append(1,c);
171 funpackf(tfile, "Bf", &(acc->points));
172 for(int i=0;i<50;i++)
174 funpackf(tfile, "Bf", &(acc->highscore[i]));
175 funpackf(tfile, "Bf", &(acc->fasttime[i]));
177 for(int i=0;i<60;i++)
179 funpackf(tfile, "Bb", &(acc->unlocked[i]));
183 funpackf(tfile, "Bi", &temp);
184 for(int i=0;i<temp;i++)
186 funpackf(tfile, "Bb", &ctemp);
187 acc->name.append(1,ctemp);
189 if(!strcmp(acc->name.c_str(),""))
190 acc->name="Lugaru Player"; // no empty player name security.
191 accounts.push_back(acc);
195 return get(accountactive);
197 printf("filenotfound\n");
202 void Account::saveFile(string filename, Account* accountactive) {
206 tfile=fopen(ConvertFileName(filename.c_str(), "wb"), "wb" );
209 printf("writing %d accounts :\n",getNbAccounts());
210 fpackf(tfile, "Bi", getNbAccounts());
211 fpackf(tfile, "Bi", indice(accountactive));
213 for(int i=0;i<getNbAccounts();i++)
215 Account* a = Account::get(i);
216 printf("writing account %d/%d (%s)\n",i+1,getNbAccounts(),a->getName());
217 fpackf(tfile, "Bi", a->difficulty);
218 fpackf(tfile, "Bi", a->progress);
219 fpackf(tfile, "Bi", a->campaignProgress.size());
221 map<string,campaign_progress_t>::const_iterator it;
222 for( it=a->campaignProgress.begin(); it!= a->campaignProgress.end(); ++it) {
223 fpackf(tfile, "Bi", it->first.size());
224 for(j=0;j<it->first.size();j++)
226 fpackf(tfile, "Bb", it->first[j]);
228 fpackf(tfile, "Bf", it->second.time);
229 fpackf(tfile, "Bf", it->second.score);
230 fpackf(tfile, "Bf", it->second.fasttime);
231 fpackf(tfile, "Bf", it->second.highscore);
232 fpackf(tfile, "Bi", it->second.choices.size());
233 for(j=0;j<it->second.choices.size();j++)
235 fpackf(tfile, "Bi", it->second.choices[j]);
239 fpackf(tfile, "Bi", a->getCurrentCampaign().size());
240 for(j=0;j<a->getCurrentCampaign().size();j++)
242 fpackf(tfile, "Bb", a->getCurrentCampaign()[j]);
245 fpackf(tfile, "Bf", a->points);
248 fpackf(tfile, "Bf", a->highscore[j]);
249 fpackf(tfile, "Bf", a->fasttime[j]);
253 fpackf(tfile, "Bb", a->unlocked[j]);
255 fpackf(tfile, "Bi", a->name.size());
256 for(j=0;j<a->name.size();j++)
258 fpackf(tfile, "Bb", a->name[j]);
266 int Account::indice(Account* a) {
267 for(int i=0; i < accounts.size(); i++) {