File: gameeventmanager.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (371 lines) | stat: -rw-r--r-- 12,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This code is based on the CRAB engine
 *
 * Copyright (c) Arvind Raja Yadav
 *
 * Licensed under MIT
 *
 */

#include "crab/event/gameeventmanager.h"

namespace Crab {

using namespace pyrodactyl::people;
using namespace pyrodactyl::event;
using namespace pyrodactyl::level;
using namespace pyrodactyl::image;
using namespace pyrodactyl::ui;

void Manager::init() {
	_eventMap.clear();
	_activeSeq = UINT_MAX;
	_curSp = nullptr;
	_player = false;
	_curEvent = nullptr;
	_drawGame = true;
}

//------------------------------------------------------------------------
// Purpose: Load this
//------------------------------------------------------------------------
void Manager::load(rapidxml::xml_node<char> *node, ParagraphData &popup) {
	if (nodeValid(node)) {
		XMLDoc conf(node->first_attribute("list")->value());
		if (conf.ready()) {
			rapidxml::xml_node<char> *lnode = conf.doc()->first_node("event_list");
			for (rapidxml::xml_node<char> *loc = lnode->first_node("loc"); loc != nullptr; loc = loc->next_sibling("loc")) {
				Common::String locName;
				loadStr(locName, "name", loc);

				for (auto n = loc->first_node("file"); n != nullptr; n = n->next_sibling("file")) {
					uint id;
					Common::Path path;
					loadNum(id, "name", n);
					loadPath(path, "path", n);
					_eventMap[locName].addSeq(id, path);
				}
			}
		}

		_activeSeq = UINT_MAX;

		conf.load(node->first_attribute("layout")->value());
		if (conf.ready()) {
			rapidxml::xml_node<char> *layout = conf.doc()->first_node("layout");
			if (nodeValid(layout)) {
				if (nodeValid("character", layout))
					_oh.load(layout->first_node("character"));

				if (nodeValid("popup", layout))
					popup.load(layout->first_node("popup"));

				if (nodeValid("intro", layout))
					_intro.load(layout->first_node("intro"));
			}
		}

		_reply.load(node->first_attribute("conversation")->value());

		_per.load(node->first_attribute("char")->value());
	}
}

//------------------------------------------------------------------------
// Purpose: Handle events
//------------------------------------------------------------------------
void Manager::handleEvents(Info &info, const Common::String &playerId, Common::Event &event, HUD &hud, Level &level, Common::Array<EventResult> &result) {
	// If an event is already being performed
	if (_eventMap.contains(info.curLocID()) && _eventMap[info.curLocID()].eventInProgress(_activeSeq)) {
		switch (_curEvent->_type) {
		case EVENT_DIALOG:
			if (_oh._showJournal) {
				info._journal.handleEvents(playerId, event);

				if (hud._back.handleEvents(event) == BUAC_LCLICK || hud._pausekey.handleEvents(event))
					_oh._showJournal = false;
			} else {
				// If journal button is select from within an event, go to the entry corresponding to that person's name
				if (_oh.handleCommonEvents(event)) {
					if (info.personValid(_curEvent->_title)) {
						Person &p = info.personGet(_curEvent->_title);
						if (p._altJournalName)
							info._journal.open(playerId, JE_PEOPLE, p._journalName);
						else
							info._journal.open(playerId, JE_PEOPLE, p._name);
					}
				}

				if (_oh.handleDlboxEvents(event)) {
					_oh.onExit();
					_eventMap[info.curLocID()].nextEvent(_activeSeq, info, playerId, result, _endSeq);
					_oh._showJournal = false;
				}
			}
			break;
		case EVENT_ANIM:
			// Skip animation if key pressed or mouse pressed
			if (event.type == Common::EVENT_LBUTTONUP || event.type == Common::EVENT_RBUTTONUP)
				_eventMap[info.curLocID()].nextEvent(_activeSeq, info, playerId, result, _endSeq);
			break;
		case EVENT_REPLY:
			if (_oh._showJournal) {
				info._journal.handleEvents(playerId, event);

				if (hud._back.handleEvents(event) == BUAC_LCLICK || hud._pausekey.handleEvents(event))
					_oh._showJournal = false;
			} else {
				// If journal button is select from within an event, go to the entry corresponding to that person's name
				if (_oh.handleCommonEvents(event))
					if (info.personValid(_curEvent->_title))
						info._journal.open(playerId, JE_PEOPLE, info.personGet(_curEvent->_title)._name);

				unsigned int option = static_cast<unsigned int>(_reply.hoverIndex());
				const auto &replyChoice = g_engine->_eventStore->_con[_curEvent->_special]._reply;
				if (option < replyChoice.size()) {
					_intro.onEntry(replyChoice[option]._text);
				}

				int choice = _reply.handleEvents(info, g_engine->_eventStore->_con[_curEvent->_special], _curEvent->_title, _oh, event);
				if (choice >= 0) {
					_reply.onExit();
					_eventMap[info.curLocID()].nextEvent(_activeSeq, info, playerId, result, _endSeq, choice);
					_oh._showJournal = false;
				}
			}
			break;
		case EVENT_TEXT:
			// If journal button is select from within an event, go to the entry corresponding to that person's name
			if (_oh.handleCommonEvents(event))
				if (info.personValid(_curEvent->_title))
					info._journal.open(playerId, JE_PEOPLE, info.personGet(_curEvent->_title)._name);

			if (_textin.handleEvents(event))
				_eventMap[info.curLocID()].nextEvent(_activeSeq, info, playerId, result, _endSeq);
			break;
		case EVENT_SPLASH:
			if (_intro._showTraits) {
				_per.handleEvents(info, _curEvent->_title, event);

				if (hud._back.handleEvents(event) == BUAC_LCLICK || hud._pausekey.handleEvents(event))
					_intro._showTraits = false;
			} else {
				if (_intro.handleEvents(event)) {
					_intro.onExit();
					_eventMap[info.curLocID()].nextEvent(_activeSeq, info, playerId, result, _endSeq);
				}
				if (_intro._showTraits)
					_per.Cache(info, level.playerId(), level);
			}
			break;
		default:
			break;
		}

		endSequence(info.curLocID());
	}
}

//------------------------------------------------------------------------
// Purpose: Internal Events
//------------------------------------------------------------------------
void Manager::internalEvents(Info &info, Level &level, Common::Array<EventResult> &result) {
	if (_eventMap.contains(info.curLocID())) {
		if (_eventMap[info.curLocID()].eventInProgress(_activeSeq)) {
			switch (_curEvent->_type) {
			case EVENT_DIALOG:
			// fallthrough intended
			case EVENT_REPLY:
			// fallthrough intended
			case EVENT_SPLASH:
				updateDialogBox(info, level);
				break;
			case EVENT_ANIM: {
				using namespace pyrodactyl::anim;

				DrawType drawVal = DRAW_SAME;
				if (g_engine->_eventStore->_anim[_curEvent->_special].internalEvents(drawVal))
					_eventMap[info.curLocID()].nextEvent(_activeSeq, info, level.playerId(), result, _endSeq);

				if (drawVal == DRAW_STOP)
					_drawGame = false;
				else if (drawVal == DRAW_START)
					_drawGame = true;
			} break;
			case EVENT_SILENT:
				_eventMap[info.curLocID()].nextEvent(_activeSeq, info, level.playerId(), result, _endSeq);
				break;
			default:
				break;
			}

			endSequence(info.curLocID());
		} else {
			_eventMap[info.curLocID()].internalEvents(info);
			calcActiveSeq(info, level, level.camera());
		}
	}
}

void Manager::updateDialogBox(Info &info, pyrodactyl::level::Level &level) {
	_oh.internalEvents(_curEvent->_state, _curSp);
}
//------------------------------------------------------------------------
// Purpose: Draw
//------------------------------------------------------------------------
void Manager::draw(Info &info, HUD &hud, Level &level) {
	if (_eventMap.contains(info.curLocID()) && _eventMap[info.curLocID()].eventInProgress(_activeSeq)) {
		switch (_curEvent->_type) {
		case EVENT_ANIM:
			g_engine->_eventStore->_anim[_curEvent->_special].draw();
			break;
		case EVENT_DIALOG:
			g_engine->_imageManager->dimScreen();
			if (_oh._showJournal) {
				info._journal.draw(level.playerId());
				hud._back.draw();
			} else
				_oh.draw(info, _curEvent, _curEvent->_title, _player, _curSp);
			break;
		case EVENT_REPLY:
			g_engine->_imageManager->dimScreen();
			if (_oh._showJournal) {
				info._journal.draw(level.playerId());
				hud._back.draw();
			} else {
				_oh.draw(info, _curEvent, _curEvent->_title, _player, _curSp);
				_reply.draw();
			}
			break;
		case EVENT_TEXT:
			_oh.draw(info, _curEvent, _curEvent->_title, _player, _curSp);
			_textin.draw();
			break;
		case EVENT_SPLASH:
			g_engine->_imageManager->dimScreen();
			if (_intro._showTraits) {
				_per.draw(info, _curEvent->_title);
				hud._back.draw();
			} else
				_intro.draw(info, _curEvent->_dialog, _curSp, _curEvent->_state);

			break;
		default:
			break;
		}
	}
}

//------------------------------------------------------------------------
// Purpose: Calculate the current sequence in progress
//------------------------------------------------------------------------
void Manager::calcActiveSeq(Info &info, pyrodactyl::level::Level &level, const Rect &camera) {
	if (_eventMap[info.curLocID()].activeSeq(_activeSeq)) {
		// Set all the pointers to the new values
		_curEvent = _eventMap[info.curLocID()].curEvent(_activeSeq);
		_oh.reset(_curEvent->_title);
		_curSp = level.getSprite(_curEvent->_title);

		// The player character's dialog is drawn a bit differently compared to others
		_player = (_curEvent->_title == level.playerId());

		switch (_curEvent->_type) {
		case EVENT_DIALOG:
			_oh.onEntry(_curEvent->_dialog);
			break;
		case EVENT_ANIM:
			g_engine->_eventStore->_anim[_curEvent->_special].start();
			break;
		case EVENT_REPLY:
			_reply.onEntry(_curEvent->_dialog);
			_reply.cache(info, g_engine->_eventStore->_con[_curEvent->_special]);
			break;
		case EVENT_SPLASH:
			_intro.onEntry(_curEvent->_dialog);
			break;
		default:
			break;
		}
	}
}

//------------------------------------------------------------------------
// Purpose: Get/set info
//------------------------------------------------------------------------
void Manager::endSequence(const Common::String &curloc) {
	if (_endSeq.empty())
		return;

	for (const auto &i : _endSeq)
		if (i._cur)
			_eventMap[curloc].endSeq(_activeSeq);
		else if (_eventMap.contains(i._loc))
			_eventMap[i._loc].endSeq(stringToNumber<uint>(i._val));

	_activeSeq = UINT_MAX;
	_endSeq.clear();
}

bool Manager::eventInProgress() {
	if (_activeSeq == UINT_MAX)
		return false;
	return true;
}

//------------------------------------------------------------------------
// Purpose: Save the state of the object
//------------------------------------------------------------------------
void Manager::saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root) {
	for (auto &i : _eventMap) {
		rapidxml::xml_node<char> *child = doc.allocate_node(rapidxml::node_element, "loc");
		child->append_attribute(doc.allocate_attribute("name", i._key.c_str()));
		i._value.saveState(doc, child);
		root->append_node(child);
	}
}

//------------------------------------------------------------------------
// Purpose: Load the state of the object
//------------------------------------------------------------------------
void Manager::loadState(rapidxml::xml_node<char> *node) {
	for (auto n = node->first_node("loc"); n != nullptr; n = n->next_sibling("loc")) {
		if (n->first_attribute("name") != nullptr) {
			Common::String name = n->first_attribute("name")->value();
			if (_eventMap.contains(name))
				_eventMap[name].loadState(n);
		}
	}
}

//------------------------------------------------------------------------
// Purpose: Function called when window size is changed to adjust UI
//------------------------------------------------------------------------
void Manager::setUI() {
	_oh.setUI();
	_reply.setUI();
	_textin.setUI();
	_per.setUI();
}

} // End of namespace Crab