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