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 (212 lines) | stat: -rw-r--r-- 5,458 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
/* 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_enh/spells/cast_spell.h"
#include "mm/mm1/game/spells_party.h"
#include "mm/mm1/globals.h"

namespace MM {
namespace MM1 {
namespace ViewsEnh {
namespace Spells {

CastSpell::CastSpell() : PartyView("CastSpell") {
	_bounds = Common::Rect(225, 0, 320, 146);

	_icons.load("cast.icn");
	addButton(&_icons, Common::Point(0, 100), 0,
		Common::KeyState(Common::KEYCODE_c, 'c'));
	addButton(&_icons, Common::Point(28, 100), 2,
		Common::KeyState(Common::KEYCODE_n, 'n'));
	addButton(&_icons, Common::Point(56, 100), 4, KEYBIND_ESCAPE);
}

bool CastSpell::msgFocus(const FocusMessage &msg) {
	if (!isInCombat())
		(void)PartyView::msgFocus(msg);

	updateSelectedSpell();
	return true;
}

bool CastSpell::msgUnfocus(const UnfocusMessage &msg) {
	if (!isInCombat())
		(void)PartyView::msgUnfocus(msg);

	return true;
}

void CastSpell::draw() {
	if (!isInCombat()) {
		PartyView::draw();
	} else {
		ScrollView::draw();
	}
	_fontReduced = false;

	const Character &c = *g_globals->_currCharacter;
	writeString(0, 0, STRING["enhdialogs.cast_spell.title"], ALIGN_MIDDLE);
	writeString(0, 20, c._name, ALIGN_MIDDLE);
	writeString(0, 40, STRING["enhdialogs.cast_spell.spell_ready"]);

	setTextColor(37);

	int spellNum = c.spellNumber();
	Common::String spellName = STRING["enhdialogs.cast_spell.none"];
	if (spellNum >= 0 && spellNum < 47) {
		spellName = STRING[Common::String::format("spells.cleric.%d", spellNum)];
	} else if (spellNum >= 47) {
		spellName = STRING[Common::String::format("spells.wizard.%d", spellNum - 47)];
	}
	writeString(0, 60, spellName, ALIGN_MIDDLE);

	_fontReduced = true;
	setTextColor(0);
	writeString(0, 80, STRING["enhdialogs.cast_spell.cost"]);
	writeString(0, 90, STRING["enhdialogs.cast_spell.cur_sp"]);
	writeString(0, 80, Common::String::format("%d/%d",
		_requiredSp, _requiredGems), ALIGN_RIGHT);
	writeString(0, 90, Common::String::format("%d", c._sp._current), ALIGN_RIGHT);

	writeString(0, 122, STRING["enhdialogs.cast_spell.cast"]);
	writeString(30, 122, STRING["enhdialogs.cast_spell.new"]);
	writeString(60, 122, STRING["enhdialogs.cast_spell.esc"]);

	_fontReduced = false;
}

bool CastSpell::msgKeypress(const KeypressMessage &msg) {
	if (msg.keycode == Common::KEYCODE_c) {
		// Cast a spell
		if (_spellIndex != -1) {
			if (!canCast()) {
				close();
				spellError();
			} else if (hasCharTarget()) {
				addView("CharacterSelect");
			} else {
				close();
				castSpell();
			}
		}

		return true;

	} else if (msg.keycode == Common::KEYCODE_n) {
		// Select a new spell
		addView("Spellbook");
		return true;
	} else if (!isInCombat()) {
		return PartyView::msgKeypress(msg);
	} else {
		return false;
	}
}

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

	} else if (!isInCombat()) {
		return PartyView::msgAction(msg);
	} else {
		return false;
	}
}

bool CastSpell::msgGame(const GameMessage &msg) {
	if (msg._name == "UPDATE") {
		updateSelectedSpell();
		draw();
		return true;
	} else if (msg._name == "CHAR_SELECTED" && msg._value != -1) {
		close();
		castSpell(&g_globals->_party[msg._value]);
	}

	return true;
}

void CastSpell::updateSelectedSpell() {
	const Character &c = *g_globals->_currCharacter;

	int spellNum = c.spellNumber();
	if (spellNum == -1) {
		_requiredSp = _requiredGems = 0;
		_spellIndex = -1;

	} else {
		int lvl, num;
		getSpellLevelNum(spellNum, lvl, num);
		assert(getSpellIndex(&c, lvl, num) == spellNum);

		setSpell(&c, lvl, num);
	}
}

void CastSpell::charSwitched(Character *priorChar) {
	PartyView::charSwitched(priorChar);
	updateSelectedSpell();
}

void CastSpell::castSpell(Character *target) {
	if (_spellIndex == -1)
		return;

	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()) {
		g_events->send(InfoMessage(STRING["spells.magic_doesnt_work"]));

	} else {
		// Cast the spell
		Game::SpellResult result = Game::SpellsParty::cast(_spellIndex, target);

		switch (result) {
		case Game::SR_FAILED:
			g_events->send(InfoMessage(STRING["spells.failed"]));
			break;

		case Game::SR_SUCCESS_DONE:
			g_events->send(InfoMessage(STRING["spells.done"]));
			break;

		default:
			// Spell done, but don't display done message
			break;
		}
	}
}

void CastSpell::spellError() {
	g_events->drawElements();

	Common::String msg = getSpellError();
	send(SoundMessage(msg, ALIGN_MIDDLE));
}

} // namespace Spells
} // namespace ViewsEnh
} // namespace MM1
} // namespace MM