]> git.jsancho.org Git - lugaru.git/blob - Source/Campaign.cpp
Account active is now stored as an integer to avoid weird loops to find the right id
[lugaru.git] / Source / Campaign.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 "Campaign.h"
22 #include "Game.h"
23 #include "Utils/Folders.h"
24 #include <dirent.h>
25
26 using namespace Game;
27
28 std::vector<CampaignLevel> campaignlevels;
29
30 bool campaign = false;
31
32 int actuallevel = 0;
33
34 std::vector<std::string> ListCampaigns()
35 {
36     errno = 0;
37     DIR *campaigns = opendir(Folders::getResourcePath("Campaigns").c_str());
38     struct dirent *campaign = NULL;
39     if (!campaigns) {
40         perror(("Problem while loading campaigns from " + Folders::getResourcePath("Campaigns")).c_str());
41         exit(EXIT_FAILURE);
42     }
43     std::vector<std::string> campaignNames;
44     while ((campaign = readdir(campaigns)) != NULL) {
45         std::string name(campaign->d_name);
46         if (name.length() < 5)
47             continue;
48         if (!name.compare(name.length() - 4, 4, ".txt")) {
49             campaignNames.push_back(name.substr(0, name.length() - 4));
50         }
51     }
52     closedir(campaigns);
53     return campaignNames;
54 }
55
56 void LoadCampaign()
57 {
58     if (!Account::hasActive()) {
59         return;
60     }
61     std::ifstream ipstream(Folders::getResourcePath("Campaigns/" + Account::active().getCurrentCampaign() + ".txt"));
62     if (!ipstream.good()) {
63         if (Account::active().getCurrentCampaign() == "main") {
64             cerr << "Could not found main campaign!" << endl;
65             return;
66         }
67         cerr << "Could not found campaign \"" << Account::active().getCurrentCampaign() << "\", falling back to main." << endl;
68         Account::active().setCurrentCampaign("main");
69         return LoadCampaign();
70     }
71     ipstream.ignore(256, ':');
72     int numlevels;
73     ipstream >> numlevels;
74     campaignlevels.clear();
75     for (int i = 0; i < numlevels; i++) {
76         CampaignLevel cl;
77         ipstream >> cl;
78         campaignlevels.push_back(cl);
79     }
80     ipstream.close();
81
82     std::ifstream test(Folders::getResourcePath("Textures/" + Account::active().getCurrentCampaign() + "/World.png"));
83     if (test.good()) {
84         Mainmenuitems[7].load("Textures/" + Account::active().getCurrentCampaign() + "/World.png", 0);
85     } else {
86         Mainmenuitems[7].load("Textures/World.png", 0);
87     }
88
89     if (Account::active().getCampaignChoicesMade() == 0) {
90         Account::active().setCampaignScore(0);
91         Account::active().resetFasttime();
92     }
93 }
94
95 CampaignLevel::CampaignLevel() :
96     width(10),
97     choosenext(1)
98 {
99     location.x = 0;
100     location.y = 0;
101 }
102
103 int CampaignLevel::getStartX() {
104     return 30 + 120 + location.x * 400 / 512;
105 }
106
107 int CampaignLevel::getStartY() {
108     return 30 + 30 + (512 - location.y) * 400 / 512;
109 }
110
111 int CampaignLevel::getEndX() {
112     return getStartX() + width;
113 }
114
115 int CampaignLevel::getEndY() {
116     return getStartY() + width;
117 }
118
119 XYZ CampaignLevel::getCenter() {
120     XYZ center;
121     center.x = getStartX() + width / 2;
122     center.y = getStartY() + width / 2;
123     return center;
124 }
125
126 int CampaignLevel::getWidth() {
127     return width;
128 }
129
130 istream& CampaignLevel::operator<< (istream& is) {
131     is.ignore(256, ':');
132     is.ignore(256, ':');
133     is.ignore(256, ' ');
134     is >> mapname;
135     is.ignore(256, ':');
136     is >> description;
137     for (size_t pos = description.find('_'); pos != string::npos; pos = description.find('_', pos)) {
138         description.replace(pos, 1, 1, ' ');
139     }
140     is.ignore(256, ':');
141     is >> choosenext;
142     is.ignore(256, ':');
143     int numnext, next;
144     is >> numnext;
145     for (int j = 0; j < numnext; j++) {
146         is.ignore(256, ':');
147         is >> next;
148         nextlevel.push_back(next - 1);
149     }
150     is.ignore(256, ':');
151     is >> location.x;
152     is.ignore(256, ':');
153     is >> location.y;
154     return is;
155 }