]> git.jsancho.org Git - lugaru.git/blob - Source/Account.cpp
560592f62ebf7a0809b579d8b0b1a9757dcbd0e7
[lugaru.git] / Source / Account.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru 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.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Account.h"
22 #include "binio.h"
23 #include <fstream>
24 #include "MacCompatibility.h"
25 #include "string.h"
26 #include <iostream>
27
28 using namespace std;
29
30 extern bool devtools;
31
32 vector<Account*> Account::accounts = vector<Account*>();
33 int Account::i_active = -1;
34
35 Account::Account(const string& name) : name(name), campaignProgress()
36 {
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(const string& name)
48 {
49     currentCampaign = name;
50 }
51
52 void Account::add(const string& name)
53 {
54     accounts.push_back(new Account(name));
55     i_active = accounts.size() - 1;
56 }
57
58 Account* Account::get(int i)
59 {
60     return accounts.at(i);
61 }
62
63 int Account::getNbAccounts()
64 {
65     return accounts.size();
66 }
67
68 bool Account::hasActive()
69 {
70     return (i_active >= 0);
71 }
72
73 Account& Account::active()
74 {
75     return *(accounts.at(i_active));
76 }
77
78 void Account::setActive(int i)
79 {
80     if ((i >= 0) && (i < int(accounts.size()))) {
81         i_active = i;
82     } else {
83         cerr << "Tried to set active account to " << i << " but there is not such account" << endl;
84         i_active = -1;
85     }
86 }
87
88 void Account::destroyActive()
89 {
90     if ((i_active >= 0) && (i_active < int(accounts.size()))) {
91         accounts.erase(accounts.begin() + i_active);
92         i_active = -1;
93     } else {
94         cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl;
95         i_active = -1;
96     }
97 }
98
99 int Account::getDifficulty()
100 {
101     return difficulty;
102 }
103
104 void Account::endGame()
105 {
106     campaignProgress[currentCampaign].choices.clear();
107     campaignProgress[currentCampaign].score = 0;
108     campaignProgress[currentCampaign].time = 0;
109 }
110
111 void Account::winCampaignLevel(int choice, float score, float time)
112 {
113     campaignProgress[currentCampaign].choices.push_back(choice);
114     setCampaignScore(campaignProgress[currentCampaign].score + score);
115     campaignProgress[currentCampaign].time = time;
116 }
117
118 void Account::winLevel(int level, float score, float time)
119 {
120     if (!devtools) {
121         if (score > highscore[level])
122             highscore[level] = score;
123         if (time < fasttime[level] || fasttime[level] == 0)
124             fasttime[level] = time;
125     }
126     if (progress < level + 1)
127         progress = level + 1;
128 }
129
130 void Account::loadFile(string filename)
131 {
132     FILE *tfile;
133     int numaccounts;
134     int iactive;
135     errno = 0;
136
137     tfile = fopen(filename.c_str(), "rb" );
138
139     if (tfile) {
140         funpackf(tfile, "Bi", &numaccounts);
141         funpackf(tfile, "Bi", &iactive);
142         printf("number of accounts %d\n", numaccounts);
143         for (int i = 0; i < numaccounts; i++) {
144             printf("loading account %d/%d\n", i, numaccounts);
145             Account* acc = new Account();
146             funpackf(tfile, "Bi", &(acc->difficulty));
147             funpackf(tfile, "Bi", &(acc->progress));
148             int nbCampaigns;
149             funpackf(tfile, "Bi", &nbCampaigns);
150
151             for (int k = 0; k < nbCampaigns; ++k) {
152                 string campaignName = "";
153                 int t;
154                 char c;
155                 funpackf(tfile, "Bi",  &t);
156                 for (int j = 0; j < t; j++) {
157                     funpackf(tfile, "Bb",  &c);
158                     campaignName.append(1, c);
159                 }
160                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].time));
161                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].score));
162                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].fasttime));
163                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].highscore));
164                 int campaignchoicesmade, campaignchoice;
165                 funpackf(tfile, "Bi", &campaignchoicesmade);
166                 for (int j = 0; j < campaignchoicesmade; j++) {
167                     funpackf(tfile, "Bi", &campaignchoice);
168                     if (campaignchoice >= 10) { // what is that for?
169                         campaignchoice = 0;
170                     }
171                     acc->campaignProgress[campaignName].choices.push_back(campaignchoice);
172                 }
173             }
174
175             acc->currentCampaign = "";
176             int t;
177             char c;
178             funpackf(tfile, "Bi",  &t);
179             for (int i = 0; i < t; i++) {
180                 funpackf(tfile, "Bb",  &c);
181                 acc->currentCampaign.append(1, c);
182             }
183
184             funpackf(tfile, "Bf", &(acc->points));
185             for (int i = 0; i < 50; i++) {
186                 funpackf(tfile, "Bf", &(acc->highscore[i]));
187                 funpackf(tfile, "Bf", &(acc->fasttime[i]));
188             }
189             for (int i = 0; i < 60; i++) {
190                 funpackf(tfile, "Bb",  &(acc->unlocked[i]));
191             }
192             int temp;
193             char ctemp;
194             funpackf(tfile, "Bi",  &temp);
195             for (int i = 0; i < temp; i++) {
196                 funpackf(tfile, "Bb",  &ctemp);
197                 acc->name.append(1, ctemp);
198             }
199             if (!strcmp(acc->name.c_str(), ""))
200                 acc->name = "Lugaru Player"; // no empty player name security.
201             accounts.push_back(acc);
202         }
203
204         fclose(tfile);
205         setActive(iactive);
206     } else {
207         perror(("Couldn't load users from " + filename).c_str());
208         i_active = -1;
209     }
210 }
211
212 void Account::saveFile(string filename)
213 {
214     FILE *tfile;
215     errno = 0;
216
217     tfile = fopen(filename.c_str(), "wb" );
218     if (tfile) {
219         fpackf(tfile, "Bi", getNbAccounts());
220         fpackf(tfile, "Bi", i_active);
221
222         for (int i = 0; i < getNbAccounts(); i++) {
223             Account* a = Account::get(i);
224             printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), a->getName().c_str());
225             fpackf(tfile, "Bi", a->difficulty);
226             fpackf(tfile, "Bi", a->progress);
227             fpackf(tfile, "Bi", a->campaignProgress.size());
228
229             map<string, CampaignProgress>::const_iterator it;
230             for (it = a->campaignProgress.begin(); it != a->campaignProgress.end(); ++it) {
231                 fpackf(tfile, "Bi",  it->first.size());
232                 for (unsigned j = 0; j < it->first.size(); j++) {
233                     fpackf(tfile, "Bb",  it->first[j]);
234                 }
235                 fpackf(tfile, "Bf", it->second.time);
236                 fpackf(tfile, "Bf", it->second.score);
237                 fpackf(tfile, "Bf", it->second.fasttime);
238                 fpackf(tfile, "Bf", it->second.highscore);
239                 fpackf(tfile, "Bi", it->second.choices.size());
240                 for (unsigned j = 0; j < it->second.choices.size(); j++) {
241                     fpackf(tfile, "Bi", it->second.choices[j]);
242                 }
243             }
244
245             fpackf(tfile, "Bi", a->getCurrentCampaign().size());
246             for (unsigned j = 0; j < a->getCurrentCampaign().size(); j++) {
247                 fpackf(tfile, "Bb", a->getCurrentCampaign()[j]);
248             }
249
250             fpackf(tfile, "Bf", a->points);
251             for (unsigned j = 0; j < 50; j++) {
252                 fpackf(tfile, "Bf", a->highscore[j]);
253                 fpackf(tfile, "Bf", a->fasttime[j]);
254             }
255             for (unsigned j = 0; j < 60; j++) {
256                 fpackf(tfile, "Bb",  a->unlocked[j]);
257             }
258             fpackf(tfile, "Bi",  a->name.size());
259             for (unsigned j = 0; j < a->name.size(); j++) {
260                 fpackf(tfile, "Bb",  a->name[j]);
261             }
262         }
263
264         fclose(tfile);
265     } else {
266         perror(("Couldn't save users in " + filename).c_str());
267     }
268 }