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