]> git.jsancho.org Git - lugaru.git/blob - Source/Level/Dialog.cpp
Rename all C++ headers with .hpp extension
[lugaru.git] / Source / Level / Dialog.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/Dialog.hpp"
22
23 #include "Game.hpp"
24 #include "Objects/Person.hpp"
25 #include "Utils/binio.h"
26 #include "Utils/Folders.hpp"
27 #include "Utils/Input.hpp"
28
29 extern int hostile;
30
31 int Dialog::indialogue;
32 int Dialog::whichdialogue;
33 bool Dialog::directing;
34 float Dialog::dialoguetime;
35 std::vector<Dialog> Dialog::dialogs;
36
37 void Dialog::loadDialogs(FILE* tfile)
38 {
39     int numdialogues;
40     funpackf(tfile, "Bi", &numdialogues);
41     for (int k = 0; k < numdialogues; k++) {
42         dialogs.push_back(Dialog(tfile));
43     }
44 }
45
46 Dialog::Dialog(FILE* tfile) : gonethrough(0)
47 {
48     int numdialogscenes;
49     funpackf(tfile, "Bi", &numdialogscenes);
50     funpackf(tfile, "Bi", &type);
51     for (int l = 0; l < 10; l++) {
52         funpackf(tfile, "Bf Bf Bf", &participantlocation[l].x, &participantlocation[l].y, &participantlocation[l].z);
53         funpackf(tfile, "Bf", &participantyaw[l]);
54     }
55     for (int l = 0; l < numdialogscenes; l++) {
56         scenes.push_back(DialogScene(tfile));
57     }
58 }
59
60 std::string funpackf_string(FILE* tfile, int maxlength)
61 {
62     int templength;
63     funpackf(tfile, "Bi", &templength);
64     if ((templength > maxlength) || (templength <= 0)) {
65         templength = maxlength;
66     }
67     int m;
68     char* text = new char[maxlength];
69     for (m = 0; m < templength; m++) {
70         funpackf(tfile, "Bb", &text[m]);
71         if (text[m] == '\0')
72             break;
73     }
74     text[m] = 0;
75     std::string result(text);
76     delete[] text;
77     return result;
78 }
79
80 void fpackf_string(FILE* tfile, std::string text)
81 {
82     fpackf(tfile, "Bi", text.size());
83     for (int m = 0; m < text.size(); m++) {
84         fpackf(tfile, "Bb", text[m]);
85         if (text[m] == '\0')
86             break;
87     }
88 }
89
90 DialogScene::DialogScene(FILE* tfile)
91 {
92     funpackf(tfile, "Bi", &location);
93     funpackf(tfile, "Bf", &color[0]);
94     funpackf(tfile, "Bf", &color[1]);
95     funpackf(tfile, "Bf", &color[2]);
96     funpackf(tfile, "Bi", &sound);
97
98     text = funpackf_string(tfile, 128);
99     name = funpackf_string(tfile, 64);
100
101     funpackf(tfile, "Bf Bf Bf", &camera.x, &camera.y, &camera.z);
102     funpackf(tfile, "Bi", &participantfocus);
103     funpackf(tfile, "Bi", &participantaction);
104
105     for (int m = 0; m < 10; m++)
106         funpackf(tfile, "Bf Bf Bf", &participantfacing[m].x, &participantfacing[m].y, &participantfacing[m].z);
107
108     funpackf(tfile, "Bf Bf", &camerayaw, &camerapitch);
109 }
110
111 /* Load dialog from txt file, used by console */
112 Dialog::Dialog(int type, std::string filename) : type(type)
113 {
114     ifstream ipstream(Folders::getResourcePath(filename));
115     ipstream.ignore(256, ':');
116     int numscenes;
117     ipstream >> numscenes;
118     for (int i = 0; i < numscenes; i++) {
119         scenes.push_back(DialogScene(ipstream));
120         for (unsigned j = 0; j < Person::players.size(); j++) {
121             scenes.back().participantfacing[j] = Person::players[j]->facing;
122         }
123     }
124     ipstream.close();
125 }
126
127 DialogScene::DialogScene(ifstream &ipstream)
128 {
129     ipstream.ignore(256, ':');
130     ipstream.ignore(256, ':');
131     ipstream.ignore(256, ' ');
132     ipstream >> location;
133     ipstream.ignore(256, ':');
134     ipstream >> color[0];
135     ipstream >> color[1];
136     ipstream >> color[2];
137     ipstream.ignore(256, ':');
138     getline(ipstream, name);
139     ipstream.ignore(256, ':');
140     ipstream.ignore(256, ' ');
141     getline(ipstream, text);
142     for (int j = 0; j < 128; j++) {
143         if (text[j] == '\\')
144             text[j] = '\n';
145     }
146     ipstream.ignore(256, ':');
147     ipstream >> sound;
148 }
149
150 void Dialog::tick(int id)
151 {
152     unsigned playerId = type % 10;
153     bool special = (type > 9);
154
155     if ((!hostile || (type > 40) && (type < 50)) &&
156             (playerId < Person::players.size()) &&
157             (playerId > 0) &&
158             ((gonethrough == 0) || !special) &&
159             (special || Input::isKeyPressed(Game::attackkey))) {
160         if ((distsq(&Person::players[0]->coords, &Person::players[playerId]->coords) < 6) ||
161                 (Person::players[playerId]->howactive >= typedead1) ||
162                 (type > 40) && (type < 50)) {
163             whichdialogue = id;
164             play();
165             dialoguetime = 0;
166             gonethrough++;
167         }
168     }
169 }
170
171 void Dialog::play()
172 {
173     for (int i = 0; i < scenes.size(); i++) {
174         int playerId = scenes[i].participantfocus;
175         Person::players[playerId]->coords = participantlocation[playerId];
176         Person::players[playerId]->yaw = participantyaw[playerId];
177         Person::players[playerId]->targetyaw = participantyaw[playerId];
178         Person::players[playerId]->velocity = 0;
179         Person::players[playerId]->animTarget = Person::players[playerId]->getIdle();
180         Person::players[playerId]->frameTarget = 0;
181     }
182
183     Dialog::directing = false;
184     Dialog::indialogue = 0;
185
186     if (scenes[indialogue].sound != 0) {
187         Game::playdialoguescenesound();
188     }
189 }
190
191 void Dialog::saveDialogs(FILE* tfile)
192 {
193     fpackf(tfile, "Bi", dialogs.size());
194
195     for (int k = 0; k < dialogs.size(); k++) {
196         dialogs[k].save(tfile);
197     }
198 }
199
200 void Dialog::save(FILE* tfile)
201 {
202     fpackf(tfile, "Bi", scenes.size());
203     fpackf(tfile, "Bi", type);
204     for (int l = 0; l < 10; l++) {
205         fpackf(tfile, "Bf Bf Bf", participantlocation[l].x, participantlocation[l].y, participantlocation[l].z);
206         fpackf(tfile, "Bf", participantyaw[l]);
207     }
208     for (int l = 0; l < scenes.size(); l++) {
209         scenes[l].save(tfile);
210     }
211 }
212
213 void DialogScene::save(FILE* tfile)
214 {
215     fpackf(tfile, "Bi", location);
216     fpackf(tfile, "Bf", color[0]);
217     fpackf(tfile, "Bf", color[1]);
218     fpackf(tfile, "Bf", color[2]);
219     fpackf(tfile, "Bi", sound);
220
221     fpackf_string(tfile, text);
222     fpackf_string(tfile, name);
223
224     fpackf(tfile, "Bf Bf Bf", camera.x, camera.y, camera.z);
225     fpackf(tfile, "Bi", participantfocus);
226     fpackf(tfile, "Bi", participantaction);
227
228     for (int m = 0; m < 10; m++)
229         fpackf(tfile, "Bf Bf Bf", participantfacing[m].x, participantfacing[m].y, participantfacing[m].z);
230
231     fpackf(tfile, "Bf Bf", camerayaw, camerapitch);
232 }