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 #ifndef MUTATIONOFJB_SCRIPT_H
24 #define MUTATIONOFJB_SCRIPT_H
25 
26 #include "mutationofjb/commands/command.h"
27 #include "common/array.h"
28 #include "common/hashmap.h"
29 #include "common/hash-str.h"
30 #include "common/stack.h"
31 
32 namespace Common {
33 class SeekableReadStream;
34 class String;
35 }
36 
37 namespace MutationOfJB {
38 
39 class Command;
40 class LabelCommand;
41 class Game;
42 class GotoCommand;
43 class ConditionalCommand;
44 class Script;
45 class RandomCommand;
46 struct GameData;
47 
48 typedef Common::Array<Command *> Commands;
49 
50 struct ActionInfo {
51 	enum Action {
52 		Look,
53 		Walk,
54 		Talk,
55 		Use,
56 		PickUp
57 	};
58 
59 	Action _action;
60 	Common::String _entity1Name;
61 	Common::String _entity2Name;
62 	bool _walkTo;
63 	Command *_command;
64 };
65 
66 typedef Common::Array<ActionInfo> ActionInfos;
67 typedef Common::Array<GotoCommand *> GotoCommands;
68 typedef Common::HashMap<Common::String, Command *> Macros;
69 typedef Common::HashMap<uint8, Command *> Startups;
70 typedef Common::HashMap<Common::String, Command *> Extras;
71 
72 class ScriptParseContext {
73 public:
74 	ScriptParseContext(Common::SeekableReadStream &stream);
75 	bool readLine(Common::String &line);
76 	void addConditionalCommand(ConditionalCommand *command, char tag, bool firstHash);
77 	void addLookSection(const Common::String &item, bool walkTo);
78 
79 	Common::SeekableReadStream &_stream;
80 	Command *_currentCommand;
81 	Command *_lastCommand;
82 
83 	struct ConditionalCommandInfo {
84 		ConditionalCommand *_command;
85 		char _tag;
86 		bool _firstHash;
87 	};
88 	typedef Common::Array<ConditionalCommandInfo> ConditionalCommandInfos;
89 	ConditionalCommandInfos _pendingCondCommands;
90 
91 	typedef Common::HashMap<Common::String, LabelCommand *> LabelMap;
92 	LabelMap _labels;
93 
94 	typedef Common::HashMap<Common::String, GotoCommands> PendingGotoMap;
95 	PendingGotoMap _pendingGotos;
96 
97 	RandomCommand *_pendingRandomCommand;
98 
99 	ActionInfos _actionInfos;
100 	Macros _macros;
101 	Startups _startups;
102 	Extras _extras;
103 
104 private:
105 };
106 
107 class ScriptExecutionContext {
108 public:
_game(game)109 	ScriptExecutionContext(Game &game, Script *localScriptOverride = nullptr) : _game(game), _activeCommand(nullptr), _localScriptOverride(localScriptOverride) {}
110 	void clear();
111 
112 	Command::ExecuteResult runActiveCommand();
113 	Command::ExecuteResult startCommand(Command *cmd);
114 	Command::ExecuteResult startStartupSection();
115 
116 	void pushReturnCommand(Command *);
117 	Command *popReturnCommand();
118 	Game &getGame();
119 	GameData &getGameData();
120 	Command *getMacro(const Common::String &name) const;
121 	Command *getExtra(const Common::String &name) const;
122 	bool isCommandRunning() const;
123 
124 private:
125 	Game &_game;
126 	Command *_activeCommand;
127 	Common::Stack<Command *> _callStack;
128 	Script *_localScriptOverride;
129 };
130 
131 class Script {
132 public:
133 	bool loadFromStream(Common::SeekableReadStream &stream);
134 	~Script();
135 
136 	const ActionInfos &getActionInfos(ActionInfo::Action action);
137 	const Commands &getAllCommands() const;
138 	const Macros &getMacros() const;
139 	const Startups &getStartups() const;
140 	Command *getMacro(const Common::String &name) const;
141 	Command *getStartup(uint8 startupId) const;
142 	Command *getExtra(const Common::String &name) const;
143 
144 private:
145 	void destroy();
146 	Commands _allCommands;
147 	ActionInfos _actionInfos[5];
148 	Macros _macros;
149 	Startups _startups;
150 	Extras _extras;
151 };
152 
153 }
154 
155 #endif
156