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) {
40 memset(highscore, 0, sizeof(highscore));
41 memset(fasttime, 0, sizeof(fasttime));
42 memset(unlocked, 0, sizeof(unlocked));
43 campaignhighscore = 0;
47 campaignchoicesmade = 0;
48 memset(campaignchoices, 0, sizeof(campaignchoices));
51 Account* Account::add(string name) {
52 accounts.push_back(new Account(name));
53 return accounts.back();
56 Account* Account::get(int i) {
58 if((i>=0)&&(i<accounts.size())) {
64 void Account::destroy(int i) {
65 accounts.erase(accounts.begin()+i);
67 Account* Account::destroy(Account* a) {
68 for(int i=0; i<accounts.size(); i++) {
70 accounts.erase(accounts.begin()+i);
74 printf("Unexpected error : User %s not found %d\n",a->getName(),a);
75 return accounts.front();
78 int Account::getDifficulty() {
82 void Account::endGame() {
83 campaignchoicesmade=0;
88 void Account::winCampaignLevel(int choice, float score, float time) {
89 campaignchoices[campaignchoicesmade++] = choice;
90 setCampaignScore(campaignscore+score);
94 void Account::winLevel(int level, float score, float time) {
96 if(score>highscore[level])
97 highscore[level]=score;
98 if(time<fasttime[level]||fasttime[level]==0)
105 Account* Account::loadFile(string filename) {
111 tfile=fopen(ConvertFileName(filename.c_str()), "rb" );
115 funpackf(tfile, "Bi", &numaccounts);
116 funpackf(tfile, "Bi", &accountactive);
117 printf("number of accounts %d\n",numaccounts);
118 for(int i=0;i<numaccounts;i++)
120 printf("loading account %d/%d\n",i,numaccounts);
121 Account* acc = new Account();
122 funpackf(tfile, "Bf", &(acc->campaigntime));
123 funpackf(tfile, "Bf", &(acc->campaignscore));
124 funpackf(tfile, "Bf", &(acc->campaignfasttime));
125 funpackf(tfile, "Bf", &(acc->campaignhighscore));
126 funpackf(tfile, "Bi", &(acc->difficulty));
127 funpackf(tfile, "Bi", &(acc->progress));
128 funpackf(tfile, "Bi", &(acc->campaignchoicesmade));
129 for(j=0;j<acc->campaignchoicesmade;j++)
131 funpackf(tfile, "Bi", &(acc->campaignchoices[j]));
132 if (acc->campaignchoices[j] >= 10)
134 acc->campaignchoices[j] = 0;
137 funpackf(tfile, "Bf", &(acc->points));
140 funpackf(tfile, "Bf", &(acc->highscore[j]));
141 funpackf(tfile, "Bf", &(acc->fasttime[j]));
145 funpackf(tfile, "Bb", &(acc->unlocked[j]));
149 funpackf(tfile, "Bi", &temp);
152 funpackf(tfile, "Bb", &ctemp);
153 acc->name.append(1,ctemp);
155 if(!strcmp(acc->name.c_str(),""))
156 acc->name="Lugaru Player"; // no empty player name security.
157 accounts.push_back(acc);
161 return get(accountactive);
163 printf("filenotfound\n");
168 void Account::saveFile(string filename, Account* accountactive) {
172 tfile=fopen(ConvertFileName(filename.c_str(), "wb"), "wb" );
175 printf("writing %d accounts :\n",getNbAccounts());
176 fpackf(tfile, "Bi", getNbAccounts());
177 fpackf(tfile, "Bi", indice(accountactive));
179 for(int i=0;i<getNbAccounts();i++)
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->campaignchoicesmade);
190 for(j=0;j<a->campaignchoicesmade;j++)
192 fpackf(tfile, "Bi", a->campaignchoices[j]);
194 fpackf(tfile, "Bf", a->points);
197 fpackf(tfile, "Bf", a->highscore[j]);
198 fpackf(tfile, "Bf", a->fasttime[j]);
202 fpackf(tfile, "Bb", a->unlocked[j]);
204 fpackf(tfile, "Bi", a->name.size());
205 for(j=0;j<a->name.size();j++)
207 fpackf(tfile, "Bb", a->name[j]);
215 int Account::indice(Account* a) {
216 for(int i=0; i < accounts.size(); i++) {