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