2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
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.
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.
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/>.
21 #include "User/Account.hpp"
23 #include "Platform/Platform.hpp"
24 #include "Utils/binio.h"
34 vector<Account> Account::accounts;
35 int Account::i_active = -1;
37 Account::Account(const string& name)
44 memset(highscore, 0, sizeof(highscore));
45 memset(fasttime, 0, sizeof(fasttime));
46 memset(unlocked, 0, sizeof(unlocked));
48 setCurrentCampaign("main");
51 Account::Account(FILE* tfile)
54 funpackf(tfile, "Bi", &difficulty);
55 funpackf(tfile, "Bi", &progress);
57 funpackf(tfile, "Bi", &nbCampaigns);
59 for (int k = 0; k < nbCampaigns; ++k) {
60 string campaignName = "";
63 funpackf(tfile, "Bi", &t);
64 for (int j = 0; j < t; j++) {
65 funpackf(tfile, "Bb", &c);
66 campaignName.append(1, c);
68 float fscore, fhighscore;
69 funpackf(tfile, "Bf", &(campaignProgress[campaignName].time));
70 funpackf(tfile, "Bf", &(fscore));
71 funpackf(tfile, "Bf", &(campaignProgress[campaignName].fasttime));
72 funpackf(tfile, "Bf", &(fhighscore));
73 campaignProgress[campaignName].score = fscore;
74 campaignProgress[campaignName].highscore = fhighscore;
75 int campaignchoicesmade, campaignchoice;
76 funpackf(tfile, "Bi", &campaignchoicesmade);
77 for (int j = 0; j < campaignchoicesmade; j++) {
78 funpackf(tfile, "Bi", &campaignchoice);
79 if (campaignchoice >= 10) { // what is that for?
82 campaignProgress[campaignName].choices.push_back(campaignchoice);
89 funpackf(tfile, "Bi", &t);
90 for (int i = 0; i < t; i++) {
91 funpackf(tfile, "Bb", &c);
92 currentCampaign.append(1, c);
95 funpackf(tfile, "Bf", &points);
96 for (int i = 0; i < 50; i++) {
98 funpackf(tfile, "Bf", &(fscore));
99 funpackf(tfile, "Bf", &(fasttime[i]));
100 highscore[i] = fscore;
102 for (int i = 0; i < 60; i++) {
103 funpackf(tfile, "Bb", &(unlocked[i]));
107 funpackf(tfile, "Bi", &temp);
108 for (int i = 0; i < temp; i++) {
109 funpackf(tfile, "Bb", &ctemp);
110 name.append(1, ctemp);
113 name = "Lugaru Player"; // no empty player name security.
117 void Account::save(FILE* tfile)
119 fpackf(tfile, "Bi", difficulty);
120 fpackf(tfile, "Bi", progress);
121 fpackf(tfile, "Bi", campaignProgress.size());
123 map<string, CampaignProgress>::const_iterator it;
124 for (it = campaignProgress.begin(); it != campaignProgress.end(); ++it) {
125 fpackf(tfile, "Bi", it->first.size());
126 for (unsigned j = 0; j < it->first.size(); j++) {
127 fpackf(tfile, "Bb", it->first[j]);
129 fpackf(tfile, "Bf", it->second.time);
130 fpackf(tfile, "Bf", float(it->second.score));
131 fpackf(tfile, "Bf", it->second.fasttime);
132 fpackf(tfile, "Bf", float(it->second.highscore));
133 fpackf(tfile, "Bi", it->second.choices.size());
134 for (unsigned j = 0; j < it->second.choices.size(); j++) {
135 fpackf(tfile, "Bi", it->second.choices[j]);
139 fpackf(tfile, "Bi", getCurrentCampaign().size());
140 for (unsigned j = 0; j < getCurrentCampaign().size(); j++) {
141 fpackf(tfile, "Bb", getCurrentCampaign()[j]);
144 fpackf(tfile, "Bf", points);
145 for (unsigned j = 0; j < 50; j++) {
146 fpackf(tfile, "Bf", float(highscore[j]));
147 fpackf(tfile, "Bf", fasttime[j]);
149 for (unsigned j = 0; j < 60; j++) {
150 fpackf(tfile, "Bb", unlocked[j]);
152 fpackf(tfile, "Bi", name.size());
153 for (unsigned j = 0; j < name.size(); j++) {
154 fpackf(tfile, "Bb", name[j]);
158 void Account::setCurrentCampaign(const string& name)
160 currentCampaign = name;
163 void Account::add(const string& name)
165 accounts.emplace_back(name);
166 i_active = accounts.size() - 1;
169 Account& Account::get(int i)
171 return accounts.at(i);
174 int Account::getNbAccounts()
176 return accounts.size();
179 bool Account::hasActive()
181 return (i_active >= 0);
184 Account& Account::active()
186 return accounts.at(i_active);
189 void Account::setActive(int i)
191 if ((i >= 0) && (i < int(accounts.size()))) {
194 cerr << "Tried to set active account to " << i << " but there is not such account" << endl;
199 void Account::destroyActive()
201 if ((i_active >= 0) && (i_active < int(accounts.size()))) {
202 accounts.erase(accounts.begin() + i_active);
205 cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl;
210 int Account::getDifficulty()
215 void Account::endGame()
217 campaignProgress[currentCampaign].choices.clear();
218 campaignProgress[currentCampaign].score = 0;
219 campaignProgress[currentCampaign].time = 0;
222 void Account::winCampaignLevel(int choice, int score, float time)
224 campaignProgress[currentCampaign].choices.push_back(choice);
225 setCampaignScore(campaignProgress[currentCampaign].score + score);
226 campaignProgress[currentCampaign].time = time;
229 void Account::winLevel(int level, int score, float time)
232 if (score > highscore[level]) {
233 highscore[level] = score;
235 if (time < fasttime[level] || fasttime[level] == 0) {
236 fasttime[level] = time;
239 if (progress < level + 1) {
240 progress = level + 1;
244 void Account::loadFile(string filename)
251 tfile = fopen(filename.c_str(), "rb");
254 funpackf(tfile, "Bi", &numaccounts);
255 funpackf(tfile, "Bi", &iactive);
256 printf("Loading %d accounts\n", numaccounts);
257 for (int i = 0; i < numaccounts; i++) {
258 printf("Loading account %d/%d\n", i, numaccounts);
259 accounts.emplace_back(tfile);
265 perror(("Couldn't load users from " + filename).c_str());
270 void Account::saveFile(string filename)
275 tfile = fopen(filename.c_str(), "wb");
277 fpackf(tfile, "Bi", getNbAccounts());
278 fpackf(tfile, "Bi", i_active);
280 for (int i = 0; i < getNbAccounts(); i++) {
281 printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), accounts[i].getName().c_str());
282 accounts[i].save(tfile);
287 perror(("Couldn't save users in " + filename).c_str());