1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program 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 this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "mutationofjb/commands/randomcommand.h"
24
25 #include "mutationofjb/game.h"
26 #include "mutationofjb/script.h"
27 #include "common/debug.h"
28 #include "common/random.h"
29
30 /** @file
31 * "RANDOM " <numChoices>
32 *
33 * RANDOM command randomly picks one of the command blocks that
34 * follow it and jumps to its start.
35 *
36 * These blocks start with "/" and end with "\". The end of a random
37 * block also ends the current section. The number of blocks must
38 * match numChoices.
39 */
40
41 namespace MutationOfJB {
42
parse(const Common::String & line,ScriptParseContext & parseCtx,Command * & command)43 bool RandomCommandParser::parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) {
44 if (line.size() < 8 || !line.hasPrefix("RANDOM")) {
45 return false;
46 }
47
48 int numChoices = atoi(line.c_str() + 7);
49 if (parseCtx._pendingRandomCommand) {
50 // Nested RANDOM commands are unused and not properly supported by the original game.
51 warning("Ignoring nested RANDOM command.");
52 } else if (numChoices >= 1) {
53 RandomCommand *randomCommand = new RandomCommand(static_cast<uint>(numChoices));
54 parseCtx._pendingRandomCommand = randomCommand;
55 command = randomCommand;
56 } else {
57 warning("Ignoring malformed RANDOM command with %d choices.", numChoices);
58 }
59
60 return true;
61 }
62
63
parse(const Common::String & line,ScriptParseContext & parseCtx,Command * &)64 bool RandomBlockStartParser::parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&) {
65 if (line != "/") {
66 return false;
67 }
68
69 if (!parseCtx._pendingRandomCommand) {
70 warning("Unexpected start of RANDOM block");
71 }
72
73 return true;
74 }
75
transition(ScriptParseContext & parseCtx,Command *,Command * newCommand,CommandParser *)76 void RandomBlockStartParser::transition(ScriptParseContext &parseCtx, Command *, Command *newCommand, CommandParser *) {
77 RandomCommand *randomCommand = parseCtx._pendingRandomCommand;
78 if (newCommand && randomCommand) {
79 randomCommand->_choices.push_back(newCommand);
80
81 if (randomCommand->_choices.size() == randomCommand->_numChoices) {
82 parseCtx._pendingRandomCommand = nullptr;
83 }
84 }
85 }
86
87
RandomCommand(uint numChoices)88 RandomCommand::RandomCommand(uint numChoices)
89 : _numChoices(numChoices),
90 _chosenNext(nullptr) {
91 _choices.reserve(numChoices);
92 }
93
execute(ScriptExecutionContext & scriptExecCtx)94 Command::ExecuteResult RandomCommand::execute(ScriptExecutionContext &scriptExecCtx) {
95 assert(!_choices.empty());
96
97 Common::RandomSource &rng = scriptExecCtx.getGame().getRandomSource();
98 uint choice = rng.getRandomNumber(_choices.size() - 1);
99 _chosenNext = _choices[choice];
100 return Finished;
101 }
102
next() const103 Command *RandomCommand::next() const {
104 return _chosenNext;
105 }
106
debugString() const107 Common::String RandomCommand::debugString() const {
108 return Common::String::format("RANDOM %u", _numChoices);
109 }
110
getChoices() const111 const RandomCommand::Choices &RandomCommand::getChoices() const {
112 return _choices;
113 }
114
115 }
116