2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - 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.h"
22 #include "Utils/binio.h"
23 #include "MacCompatibility.h"
32 vector<Account> Account::accounts;
33 int Account::i_active = -1;
35 Account::Account(const string& name) : name(name), campaignProgress()
40 memset(highscore, 0, sizeof(highscore));
41 memset(fasttime, 0, sizeof(fasttime));
42 memset(unlocked, 0, sizeof(unlocked));
44 setCurrentCampaign("main");
47 Account::Account(FILE* tfile) : Account("")
49 funpackf(tfile, "Bi", &difficulty);
50 funpackf(tfile, "Bi", &progress);
52 funpackf(tfile, "Bi", &nbCampaigns);
54 for (int k = 0; k < nbCampaigns; ++k) {
55 string campaignName = "";
58 funpackf(tfile, "Bi", &t);
59 for (int j = 0; j < t; j++) {
60 funpackf(tfile, "Bb", &c);
61 campaignName.append(1, c);
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?
74 campaignProgress[campaignName].choices.push_back(campaignchoice);
81 funpackf(tfile, "Bi", &t);
82 for (int i = 0; i < t; i++) {
83 funpackf(tfile, "Bb", &c);
84 currentCampaign.append(1, c);
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]));
92 for (int i = 0; i < 60; i++) {
93 funpackf(tfile, "Bb", &(unlocked[i]));
97 funpackf(tfile, "Bi", &temp);
98 for (int i = 0; i < temp; i++) {
99 funpackf(tfile, "Bb", &ctemp);
100 name.append(1, ctemp);
103 name = "Lugaru Player"; // no empty player name security.
107 void Account::save(FILE* tfile)
109 fpackf(tfile, "Bi", difficulty);
110 fpackf(tfile, "Bi", progress);
111 fpackf(tfile, "Bi", campaignProgress.size());
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]);
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]);
129 fpackf(tfile, "Bi", getCurrentCampaign().size());
130 for (unsigned j = 0; j < getCurrentCampaign().size(); j++) {
131 fpackf(tfile, "Bb", getCurrentCampaign()[j]);
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]);
139 for (unsigned j = 0; j < 60; j++) {
140 fpackf(tfile, "Bb", unlocked[j]);
142 fpackf(tfile, "Bi", name.size());
143 for (unsigned j = 0; j < name.size(); j++) {
144 fpackf(tfile, "Bb", name[j]);
148 void Account::setCurrentCampaign(const string& name)
150 currentCampaign = name;
153 void Account::add(const string& name)
155 accounts.emplace_back(name);
156 i_active = accounts.size() - 1;
159 Account& Account::get(int i)
161 return accounts.at(i);
164 int Account::getNbAccounts()
166 return accounts.size();
169 bool Account::hasActive()
171 return (i_active >= 0);
174 Account& Account::active()
176 return accounts.at(i_active);
179 void Account::setActive(int i)
181 if ((i >= 0) && (i < int(accounts.size()))) {
184 cerr << "Tried to set active account to " << i << " but there is not such account" << endl;
189 void Account::destroyActive()
191 if ((i_active >= 0) && (i_active < int(accounts.size()))) {
192 accounts.erase(accounts.begin() + i_active);
195 cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl;
200 int Account::getDifficulty()
205 void Account::endGame()
207 campaignProgress[currentCampaign].choices.clear();
208 campaignProgress[currentCampaign].score = 0;
209 campaignProgress[currentCampaign].time = 0;
212 void Account::winCampaignLevel(int choice, float score, float time)
214 campaignProgress[currentCampaign].choices.push_back(choice);
215 setCampaignScore(campaignProgress[currentCampaign].score + score);
216 campaignProgress[currentCampaign].time = time;
219 void Account::winLevel(int level, float score, float time)
222 if (score > highscore[level])
223 highscore[level] = score;
224 if (time < fasttime[level] || fasttime[level] == 0)
225 fasttime[level] = time;
227 if (progress < level + 1)
228 progress = level + 1;
231 void Account::loadFile(string filename)
238 tfile = fopen(filename.c_str(), "rb" );
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);
252 perror(("Couldn't load users from " + filename).c_str());
257 void Account::saveFile(string filename)
262 tfile = fopen(filename.c_str(), "wb" );
264 fpackf(tfile, "Bi", getNbAccounts());
265 fpackf(tfile, "Bi", i_active);
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);
274 perror(("Couldn't save users in " + filename).c_str());