File: cast_spell.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (262 lines) | stat: -rw-r--r-- 5,919 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
/* 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/>.
 *
 */

#include "mm/mm1/views/spells/cast_spell.h"
#include "mm/mm1/game/spells_party.h"
#include "mm/mm1/globals.h"
#include "mm/mm1/sound.h"

namespace MM {
namespace MM1 {
namespace Views {
namespace Spells {

CastSpell::CastSpell() : SpellView("CastSpell") {
	_bounds = getLineBounds(20, 24);
}

bool CastSpell::msgGame(const GameMessage &msg) {
	if (msg._name != "SPELL")
		return false;

	if (msg._value == 0) {
		// Ensure current character can cast spells
		if (g_globals->_currCharacter->_spellLevel != 0 &&
			g_globals->_currCharacter->_sp._current != 0) {
			addView();
			setState(SELECT_SPELL);
		}
	} else {
		// Spell bound to an item
		addView();
		setSpell(msg._value, 0, 0);

		if (!canCast()) {
			spellDone();
		} else if (hasCharTarget()) {
			setState(SELECT_CHAR);
		} else {
			setState(PRESS_ENTER);
		}
	}

	return true;
}

bool CastSpell::msgFocus(const FocusMessage &msg) {
	if (dynamic_cast<TextEntry *>(msg._priorView) == nullptr)
		_state = SELECT_SPELL;

	return true;
}

void CastSpell::setState(State state) {
	_state = state;

	MetaEngine::setKeybindingMode(
		_state == SELECT_CHAR ?
		KeybindingMode::KBMODE_PARTY_MENUS :
		KeybindingMode::KBMODE_MENUS
	);
	draw();
}

void CastSpell::abortFunc() {
	CastSpell *view = (CastSpell *)g_events->focusedView();
	view->close();
}

void CastSpell::enterSpellLevelFunc(const Common::String &text) {
	CastSpell *view = (CastSpell *)g_events->focusedView();
	view->spellLevelEntered(atoi(text.c_str()));
}

void CastSpell::enterSpellNumberFunc(const Common::String &text) {
	CastSpell *view = (CastSpell *)g_events->focusedView();
	view->spellNumberEntered(atoi(text.c_str()));
}

void CastSpell::draw() {
	clearSurface();
	if (_state == NONE)
		return;

	escToGoBack(0);
	writeString(7, 0, STRING["dialogs.character.cast_spell"]);
	if (_state >= SELECT_NUMBER) {
		writeChar(' ');
		writeNumber(_spellLevel);
		writeString(19, 1, STRING["dialogs.character.number"]);
	}

	if (_state > SELECT_NUMBER) {
		writeChar(' ');
		writeNumber(_spellNumber);
	}

	switch (_state) {
	case SELECT_SPELL:
		_state = NONE;
		_textEntry.display(27, 20, 1, true, abortFunc, enterSpellLevelFunc);
		break;

	case SELECT_NUMBER:
		_state = NONE;
		_textEntry.display(27, 21, 1, true, abortFunc, enterSpellNumberFunc);
		break;

	case SELECT_CHAR:
		writeString(22, 3, Common::String::format(
			STRING["spells.cast_on_char"].c_str(),
			(int)g_globals->_party.size()
		));
		break;

	case PRESS_ENTER:
		writeString(24, 4, STRING["spells.enter_to_cast"]);
		break;

	case ENDING:
		clearSurface();
		writeString(_spellResultX, 1, _spellResult);
		delaySeconds(3);
		break;

	default:
		break;
	}
}

void CastSpell::spellLevelEntered(uint level) {
	// Ensure the spell level is valid
	if (level < 1 || level > 7 ||
		(!g_globals->_allSpells && level > g_globals->_currCharacter->_spellLevel)) {
		close();
		return;
	}

	_spellLevel = level;
	setState(SELECT_NUMBER);
}

void CastSpell::spellNumberEntered(uint num) {
	if (num < 1 || num > 8 || (_spellLevel >= 5 && num >= 6)) {
		close();
		return;
	}

	_spellNumber = num;
	setSpell(g_globals->_currCharacter, _spellLevel, num);
	if (!canCast()) {
		spellDone();
	} else {
		if (hasCharTarget())
			setState(SELECT_CHAR);
		else
			setState(PRESS_ENTER);

		draw();
	}
}

bool CastSpell::msgAction(const ActionMessage &msg) {
	if (msg._action == KEYBIND_ESCAPE) {
		close();

	} else if (msg._action == KEYBIND_SELECT) {
		// Time to execute the spell
		performSpell();

	} else if (_state == SELECT_CHAR &&
		msg._action >= KEYBIND_VIEW_PARTY1 &&
		msg._action <= KEYBIND_VIEW_PARTY6) {
		uint charIndex = (int)(msg._action - KEYBIND_VIEW_PARTY1);
		if (charIndex < g_globals->_party.size()) {
			Character *c = isInCombat() ? g_globals->_combatParty[charIndex] :
				&g_globals->_party[charIndex];
			performSpell(c);
		}
	}

	return true;
}


void CastSpell::timeout() {
	close();
}

void CastSpell::performSpell(Character *chr) {
	Character &c = *g_globals->_currCharacter;
	c._sp._current = MAX((int)c._sp._current - _requiredSp, 0);
	c._gems = MAX((int)c._gems - _requiredGems, 0);

	if (!isMagicAllowed()) {
		spellDone(STRING["spells.magic_doesnt_work"], 5);
	} else {
		// Cast the spell
		switch (Game::SpellsParty::cast(_spellIndex, chr)) {
		case Game::SR_FAILED:
			// Spell failed
			spellFailed();
			break;

		case Game::SR_SUCCESS_DONE:
			// Display spell done
			spellDone();
			break;

		default:
			// Spell done, but don't display done message
			if (isFocused())
				close();
			break;
		}
	}
}

void CastSpell::spellDone() {
	Common::String msg = getSpellError();
	int xp = 20 - (msg.size() / 2);

	spellDone(msg, xp);
}

void CastSpell::spellDone(const Common::String &msg, int xp) {
	if (isInCombat()) {
		close();

		GameMessage gameMsg("SPELL_RESULT", msg);
		gameMsg._value = xp;
		g_events->focusedView()->send(gameMsg);

	} else {
		Sound::sound(SOUND_2);
		_spellResult = msg;
		_spellResultX = xp;
		setState(ENDING);
	}
}

} // namespace Spells
} // namespace Views
} // namespace MM1
} // namespace MM