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