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.hpp"
23 #include "MacCompatibility.hpp"
24 #include "Utils/binio.h"
34 vector<Account> Account::accounts;
35 int Account::i_active = -1;
37 Account::Account(const string& name) : name(name), campaignProgress()
42 memset(highscore, 0, sizeof(highscore));
43 memset(fasttime, 0, sizeof(fasttime));
44 memset(unlocked, 0, sizeof(unlocked));
46 setCurrentCampaign("main");
49 Account::Account(FILE* tfile) : Account("")
51 funpackf(tfile, "Bi", &difficulty);
52 funpackf(tfile, "Bi", &progress);
54 funpackf(tfile, "Bi", &nbCampaigns);
56 for (int k = 0; k < nbCampaigns; ++k) {
57 string campaignName = "";
60 funpackf(tfile, "Bi", &t);
61 for (int j = 0; j < t; j++) {
62 funpackf(tfile, "Bb", &c);
63 campaignName.append(1, c);
65 funpackf(tfile, "Bf", &(campaignProgress[campaignName].time));
66 funpackf(tfile, "Bf", &(campaignProgress[campaignName].score));
67 funpackf(tfile, "Bf", &(campaignProgress[campaignName].fasttime));
68 funpackf(tfile, "Bf", &(campaignProgress[campaignName].highscore));
69 int campaignchoicesmade, campaignchoice;
70 funpackf(tfile, "Bi", &campaignchoicesmade);
71 for (int j = 0; j < campaignchoicesmade; j++) {
72 funpackf(tfile, "Bi", &campaignchoice);
73 if (campaignchoice >= 10) { // what is that for?
76 campaignProgress[campaignName].choices.push_back(campaignchoice);
83 funpackf(tfile, "Bi", &t);
84 for (int i = 0; i < t; i++) {
85 funpackf(tfile, "Bb", &c);
86 currentCampaign.append(1, c);
89 funpackf(tfile, "Bf", &points);
90 for (int i = 0; i < 50; i++) {
91 funpackf(tfile, "Bf", &(highscore[i]));
92 funpackf(tfile, "Bf", &(fasttime[i]));
94 for (int i = 0; i < 60; i++) {
95 funpackf(tfile, "Bb", &(unlocked[i]));
99 funpackf(tfile, "Bi", &temp);
100 for (int i = 0; i < temp; i++) {
101 funpackf(tfile, "Bb", &ctemp);
102 name.append(1, ctemp);
105 name = "Lugaru Player"; // no empty player name security.
109 void Account::save(FILE* tfile)
111 fpackf(tfile, "Bi", difficulty);
112 fpackf(tfile, "Bi", progress);
113 fpackf(tfile, "Bi", campaignProgress.size());
115 map<string, CampaignProgress>::const_iterator it;
116 for (it = campaignProgress.begin(); it != campaignProgress.end(); ++it) {
117 fpackf(tfile, "Bi", it->first.size());
118 for (unsigned j = 0; j < it->first.size(); j++) {
119 fpackf(tfile, "Bb", it->first[j]);
121 fpackf(tfile, "Bf", it->second.time);
122 fpackf(tfile, "Bf", it->second.score);
123 fpackf(tfile, "Bf", it->second.fasttime);
124 fpackf(tfile, "Bf", it->second.highscore);
125 fpackf(tfile, "Bi", it->second.choices.size());
126 for (unsigned j = 0; j < it->second.choices.size(); j++) {
127 fpackf(tfile, "Bi", it->second.choices[j]);
131 fpackf(tfile, "Bi", getCurrentCampaign().size());
132 for (unsigned j = 0; j < getCurrentCampaign().size(); j++) {
133 fpackf(tfile, "Bb", getCurrentCampaign()[j]);
136 fpackf(tfile, "Bf", points);
137 for (unsigned j = 0; j < 50; j++) {
138 fpackf(tfile, "Bf", highscore[j]);
139 fpackf(tfile, "Bf", fasttime[j]);
141 for (unsigned j = 0; j < 60; j++) {
142 fpackf(tfile, "Bb", unlocked[j]);
144 fpackf(tfile, "Bi", name.size());
145 for (unsigned j = 0; j < name.size(); j++) {
146 fpackf(tfile, "Bb", name[j]);
150 void Account::setCurrentCampaign(const string& name)
152 currentCampaign = name;
155 void Account::add(const string& name)
157 accounts.emplace_back(name);
158 i_active = accounts.size() - 1;
161 Account& Account::get(int i)
163 return accounts.at(i);
166 int Account::getNbAccounts()
168 return accounts.size();
171 bool Account::hasActive()
173 return (i_active >= 0);
176 Account& Account::active()
178 return accounts.at(i_active);
181 void Account::setActive(int i)
183 if ((i >= 0) && (i < int(accounts.size()))) {
186 cerr << "Tried to set active account to " << i << " but there is not such account" << endl;
191 void Account::destroyActive()
193 if ((i_active >= 0) && (i_active < int(accounts.size()))) {
194 accounts.erase(accounts.begin() + i_active);
197 cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl;
202 int Account::getDifficulty()
207 void Account::endGame()
209 campaignProgress[currentCampaign].choices.clear();
210 campaignProgress[currentCampaign].score = 0;
211 campaignProgress[currentCampaign].time = 0;
214 void Account::winCampaignLevel(int choice, float score, float time)
216 campaignProgress[currentCampaign].choices.push_back(choice);
217 setCampaignScore(campaignProgress[currentCampaign].score + score);
218 campaignProgress[currentCampaign].time = time;
221 void Account::winLevel(int level, float score, float time)
224 if (score > highscore[level])
225 highscore[level] = score;
226 if (time < fasttime[level] || fasttime[level] == 0)
227 fasttime[level] = time;
229 if (progress < level + 1)
230 progress = level + 1;
233 void Account::loadFile(string filename)
240 tfile = fopen(filename.c_str(), "rb" );
243 funpackf(tfile, "Bi", &numaccounts);
244 funpackf(tfile, "Bi", &iactive);
245 printf("Loading %d accounts\n", numaccounts);
246 for (int i = 0; i < numaccounts; i++) {
247 printf("Loading account %d/%d\n", i, numaccounts);
248 accounts.emplace_back(tfile);
254 perror(("Couldn't load users from " + filename).c_str());
259 void Account::saveFile(string filename)
264 tfile = fopen(filename.c_str(), "wb" );
266 fpackf(tfile, "Bi", getNbAccounts());
267 fpackf(tfile, "Bi", i_active);
269 for (int i = 0; i < getNbAccounts(); i++) {
270 printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), accounts[i].getName().c_str());
271 accounts[i].save(tfile);
276 perror(("Couldn't save users in " + filename).c_str());