]> git.jsancho.org Git - lugaru.git/blob - Source/Campaign.cpp
Moved everything related to Menu is Menu.h,cpp and Campaign in Campaign.h,cpp
[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 (!accountactive)
59         return;
60     std::ifstream ipstream(Folders::getResourcePath("Campaigns/" + accountactive->getCurrentCampaign() + ".txt"));
61     if (!ipstream.good()) {
62         if (accountactive->getCurrentCampaign() == "main") {
63             cerr << "Could not found main campaign!" << endl;
64             return;
65         }
66         cerr << "Could not found campaign \"" << accountactive->getCurrentCampaign() << "\", falling back to main." << endl;
67         accountactive->setCurrentCampaign("main");
68         return LoadCampaign();
69     }
70     ipstream.ignore(256, ':');
71     int numlevels;
72     ipstream >> numlevels;
73     campaignlevels.clear();
74     for (int i = 0; i < numlevels; i++) {
75         CampaignLevel cl;
76         ipstream >> cl;
77         campaignlevels.push_back(cl);
78     }
79     ipstream.close();
80
81     std::ifstream test(Folders::getResourcePath("Textures/" + accountactive->getCurrentCampaign() + "/World.png"));
82     if (test.good()) {
83         Mainmenuitems[7].load("Textures/" + accountactive->getCurrentCampaign() + "/World.png", 0);
84     } else {
85         Mainmenuitems[7].load("Textures/World.png", 0);
86     }
87
88     if (accountactive->getCampaignChoicesMade() == 0) {
89         accountactive->setCampaignScore(0);
90         accountactive->resetFasttime();
91     }
92 }
93
94 CampaignLevel::CampaignLevel() :
95     width(10),
96     choosenext(1)
97 {
98     location.x = 0;
99     location.y = 0;
100 }
101
102 int CampaignLevel::getStartX() {
103     return 30 + 120 + location.x * 400 / 512;
104 }
105
106 int CampaignLevel::getStartY() {
107     return 30 + 30 + (512 - location.y) * 400 / 512;
108 }
109
110 int CampaignLevel::getEndX() {
111     return getStartX() + width;
112 }
113
114 int CampaignLevel::getEndY() {
115     return getStartY() + width;
116 }
117
118 XYZ CampaignLevel::getCenter() {
119     XYZ center;
120     center.x = getStartX() + width / 2;
121     center.y = getStartY() + width / 2;
122     return center;
123 }
124
125 int CampaignLevel::getWidth() {
126     return width;
127 }
128
129 istream& CampaignLevel::operator<< (istream& is) {
130     is.ignore(256, ':');
131     is.ignore(256, ':');
132     is.ignore(256, ' ');
133     is >> mapname;
134     is.ignore(256, ':');
135     is >> description;
136     for (size_t pos = description.find('_'); pos != string::npos; pos = description.find('_', pos)) {
137         description.replace(pos, 1, 1, ' ');
138     }
139     is.ignore(256, ':');
140     is >> choosenext;
141     is.ignore(256, ':');
142     int numnext, next;
143     is >> numnext;
144     for (int j = 0; j < numnext; j++) {
145         is.ignore(256, ':');
146         is >> next;
147         nextlevel.push_back(next - 1);
148     }
149     is.ignore(256, ':');
150     is >> location.x;
151     is.ignore(256, ':');
152     is >> location.y;
153     return is;
154 }