]> git.jsancho.org Git - lugaru.git/blob - Source/User/Account.cpp
Sorted all source files in folders
[lugaru.git] / Source / User / 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 "User/Account.h"
22 #include "Utils/binio.h"
23 #include "MacCompatibility.h"
24 #include <fstream>
25 #include "string.h"
26 #include <iostream>
27
28 using namespace std;
29
30 extern bool devtools;
31
32 vector<Account> Account::accounts;
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 Account::Account(FILE* tfile) : Account("")
48 {
49     funpackf(tfile, "Bi", &difficulty);
50     funpackf(tfile, "Bi", &progress);
51     int nbCampaigns;
52     funpackf(tfile, "Bi", &nbCampaigns);
53
54     for (int k = 0; k < nbCampaigns; ++k) {
55         string campaignName = "";
56         int t;
57         char c;
58         funpackf(tfile, "Bi",  &t);
59         for (int j = 0; j < t; j++) {
60             funpackf(tfile, "Bb",  &c);
61             campaignName.append(1, c);
62         }
63         funpackf(tfile, "Bf", &(campaignProgress[campaignName].time));
64         funpackf(tfile, "Bf", &(campaignProgress[campaignName].score));
65         funpackf(tfile, "Bf", &(campaignProgress[campaignName].fasttime));
66         funpackf(tfile, "Bf", &(campaignProgress[campaignName].highscore));
67         int campaignchoicesmade, campaignchoice;
68         funpackf(tfile, "Bi", &campaignchoicesmade);
69         for (int j = 0; j < campaignchoicesmade; j++) {
70             funpackf(tfile, "Bi", &campaignchoice);
71             if (campaignchoice >= 10) { // what is that for?
72                 campaignchoice = 0;
73             }
74             campaignProgress[campaignName].choices.push_back(campaignchoice);
75         }
76     }
77
78     currentCampaign = "";
79     int t;
80     char c;
81     funpackf(tfile, "Bi",  &t);
82     for (int i = 0; i < t; i++) {
83         funpackf(tfile, "Bb",  &c);
84         currentCampaign.append(1, c);
85     }
86
87     funpackf(tfile, "Bf", &points);
88     for (int i = 0; i < 50; i++) {
89         funpackf(tfile, "Bf", &(highscore[i]));
90         funpackf(tfile, "Bf", &(fasttime[i]));
91     }
92     for (int i = 0; i < 60; i++) {
93         funpackf(tfile, "Bb",  &(unlocked[i]));
94     }
95     int temp;
96     char ctemp;
97     funpackf(tfile, "Bi",  &temp);
98     for (int i = 0; i < temp; i++) {
99         funpackf(tfile, "Bb",  &ctemp);
100         name.append(1, ctemp);
101     }
102     if (name.empty()) {
103         name = "Lugaru Player"; // no empty player name security.
104     }
105 }
106
107 void Account::save(FILE* tfile)
108 {
109     fpackf(tfile, "Bi", difficulty);
110     fpackf(tfile, "Bi", progress);
111     fpackf(tfile, "Bi", campaignProgress.size());
112
113     map<string, CampaignProgress>::const_iterator it;
114     for (it = campaignProgress.begin(); it != campaignProgress.end(); ++it) {
115         fpackf(tfile, "Bi",  it->first.size());
116         for (unsigned j = 0; j < it->first.size(); j++) {
117             fpackf(tfile, "Bb",  it->first[j]);
118         }
119         fpackf(tfile, "Bf", it->second.time);
120         fpackf(tfile, "Bf", it->second.score);
121         fpackf(tfile, "Bf", it->second.fasttime);
122         fpackf(tfile, "Bf", it->second.highscore);
123         fpackf(tfile, "Bi", it->second.choices.size());
124         for (unsigned j = 0; j < it->second.choices.size(); j++) {
125             fpackf(tfile, "Bi", it->second.choices[j]);
126         }
127     }
128
129     fpackf(tfile, "Bi", getCurrentCampaign().size());
130     for (unsigned j = 0; j < getCurrentCampaign().size(); j++) {
131         fpackf(tfile, "Bb", getCurrentCampaign()[j]);
132     }
133
134     fpackf(tfile, "Bf", points);
135     for (unsigned j = 0; j < 50; j++) {
136         fpackf(tfile, "Bf", highscore[j]);
137         fpackf(tfile, "Bf", fasttime[j]);
138     }
139     for (unsigned j = 0; j < 60; j++) {
140         fpackf(tfile, "Bb",  unlocked[j]);
141     }
142     fpackf(tfile, "Bi",  name.size());
143     for (unsigned j = 0; j < name.size(); j++) {
144         fpackf(tfile, "Bb",  name[j]);
145     }
146 }
147
148 void Account::setCurrentCampaign(const string& name)
149 {
150     currentCampaign = name;
151 }
152
153 void Account::add(const string& name)
154 {
155     accounts.emplace_back(name);
156     i_active = accounts.size() - 1;
157 }
158
159 Account& Account::get(int i)
160 {
161     return accounts.at(i);
162 }
163
164 int Account::getNbAccounts()
165 {
166     return accounts.size();
167 }
168
169 bool Account::hasActive()
170 {
171     return (i_active >= 0);
172 }
173
174 Account& Account::active()
175 {
176     return accounts.at(i_active);
177 }
178
179 void Account::setActive(int i)
180 {
181     if ((i >= 0) && (i < int(accounts.size()))) {
182         i_active = i;
183     } else {
184         cerr << "Tried to set active account to " << i << " but there is not such account" << endl;
185         i_active = -1;
186     }
187 }
188
189 void Account::destroyActive()
190 {
191     if ((i_active >= 0) && (i_active < int(accounts.size()))) {
192         accounts.erase(accounts.begin() + i_active);
193         i_active = -1;
194     } else {
195         cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl;
196         i_active = -1;
197     }
198 }
199
200 int Account::getDifficulty()
201 {
202     return difficulty;
203 }
204
205 void Account::endGame()
206 {
207     campaignProgress[currentCampaign].choices.clear();
208     campaignProgress[currentCampaign].score = 0;
209     campaignProgress[currentCampaign].time = 0;
210 }
211
212 void Account::winCampaignLevel(int choice, float score, float time)
213 {
214     campaignProgress[currentCampaign].choices.push_back(choice);
215     setCampaignScore(campaignProgress[currentCampaign].score + score);
216     campaignProgress[currentCampaign].time = time;
217 }
218
219 void Account::winLevel(int level, float score, float time)
220 {
221     if (!devtools) {
222         if (score > highscore[level])
223             highscore[level] = score;
224         if (time < fasttime[level] || fasttime[level] == 0)
225             fasttime[level] = time;
226     }
227     if (progress < level + 1)
228         progress = level + 1;
229 }
230
231 void Account::loadFile(string filename)
232 {
233     FILE *tfile;
234     int numaccounts;
235     int iactive;
236     errno = 0;
237
238     tfile = fopen(filename.c_str(), "rb" );
239
240     if (tfile) {
241         funpackf(tfile, "Bi", &numaccounts);
242         funpackf(tfile, "Bi", &iactive);
243         printf("Loading %d accounts\n", numaccounts);
244         for (int i = 0; i < numaccounts; i++) {
245             printf("Loading account %d/%d\n", i, numaccounts);
246             accounts.emplace_back(tfile);
247         }
248
249         fclose(tfile);
250         setActive(iactive);
251     } else {
252         perror(("Couldn't load users from " + filename).c_str());
253         i_active = -1;
254     }
255 }
256
257 void Account::saveFile(string filename)
258 {
259     FILE *tfile;
260     errno = 0;
261
262     tfile = fopen(filename.c_str(), "wb" );
263     if (tfile) {
264         fpackf(tfile, "Bi", getNbAccounts());
265         fpackf(tfile, "Bi", i_active);
266
267         for (int i = 0; i < getNbAccounts(); i++) {
268             printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), accounts[i].getName().c_str());
269             accounts[i].save(tfile);
270         }
271
272         fclose(tfile);
273     } else {
274         perror(("Couldn't save users in " + filename).c_str());
275     }
276 }