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