2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
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.
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.
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/>.
26 #include "Utils/Folders.h"
30 int Dialog::indialogue;
31 int Dialog::whichdialogue;
32 bool Dialog::directing;
33 float Dialog::dialoguetime;
34 std::vector<Dialog> Dialog::dialogs;
36 void Dialog::loadDialogs(FILE* tfile)
39 funpackf(tfile, "Bi", &numdialogues);
40 for (int k = 0; k < numdialogues; k++) {
41 dialogs.push_back(Dialog(tfile));
45 Dialog::Dialog(FILE* tfile) : gonethrough(0)
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]);
54 for (int l = 0; l < numdialogscenes; l++) {
55 scenes.push_back(DialogScene(tfile));
59 std::string funpackf_string(FILE* tfile, int maxlength)
62 funpackf(tfile, "Bi", &templength);
63 if ((templength > maxlength) || (templength <= 0)) {
64 templength = maxlength;
67 char* text = new char[maxlength];
68 for (m = 0; m < templength; m++) {
69 funpackf(tfile, "Bb", &text[m]);
74 std::string result(text);
79 void fpackf_string(FILE* tfile, std::string text)
81 fpackf(tfile, "Bi", text.size());
82 for (int m = 0; m < text.size(); m++) {
83 fpackf(tfile, "Bb", text[m]);
89 DialogScene::DialogScene(FILE* tfile)
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);
97 text = funpackf_string(tfile, 128);
98 name = funpackf_string(tfile, 64);
100 funpackf(tfile, "Bf Bf Bf", &camera.x, &camera.y, &camera.z);
101 funpackf(tfile, "Bi", &participantfocus);
102 funpackf(tfile, "Bi", &participantaction);
104 for (int m = 0; m < 10; m++)
105 funpackf(tfile, "Bf Bf Bf", &participantfacing[m].x, &participantfacing[m].y, &participantfacing[m].z);
107 funpackf(tfile, "Bf Bf", &camerayaw, &camerapitch);
110 /* Load dialog from txt file, used by console */
111 Dialog::Dialog(int type, std::string filename) : type(type)
113 ifstream ipstream(Folders::getResourcePath(filename));
114 ipstream.ignore(256, ':');
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;
126 DialogScene::DialogScene(ifstream &ipstream)
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++) {
145 ipstream.ignore(256, ':');
149 void Dialog::tick(int id)
151 unsigned playerId = type % 10;
152 bool special = (type > 9);
154 if ((!hostile || (type > 40) && (type < 50)) &&
155 (playerId < Person::players.size()) &&
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)) {
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;
182 Dialog::directing = false;
183 Dialog::indialogue = 0;
185 if (scenes[indialogue].sound != 0) {
186 Game::playdialoguescenesound();
190 void Dialog::saveDialogs(FILE* tfile)
192 fpackf(tfile, "Bi", dialogs.size());
194 for (int k = 0; k < dialogs.size(); k++) {
195 dialogs[k].save(tfile);
199 void Dialog::save(FILE* tfile)
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]);
207 for (int l = 0; l < scenes.size(); l++) {
208 scenes[l].save(tfile);
212 void DialogScene::save(FILE* tfile)
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);
220 fpackf_string(tfile, text);
221 fpackf_string(tfile, name);
223 fpackf(tfile, "Bf Bf Bf", camera.x, camera.y, camera.z);
224 fpackf(tfile, "Bi", participantfocus);
225 fpackf(tfile, "Bi", participantaction);
227 for (int m = 0; m < 10; m++)
228 fpackf(tfile, "Bf Bf Bf", participantfacing[m].x, participantfacing[m].y, participantfacing[m].z);
230 fpackf(tfile, "Bf Bf", camerayaw, camerapitch);