ScummVM API documentation
scenecontext.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef NANCY_SCRIPT_SCENECONTEXT_H
23 #define NANCY_SCRIPT_SCENECONTEXT_H
24 
25 #include "common/array.h"
26 #include "common/hash-str.h"
27 #include "common/keyboard.h"
28 #include "common/str.h"
29 
30 #include "engines/nancy/script/types.h"
31 
32 struct lua_State;
33 
34 namespace Nancy {
35 
36 struct NancyInput;
37 
38 namespace Script {
39 
40 class LuaState;
41 class Record;
42 struct RefImage;
43 class ConvoRecord;
44 struct ConvoResponse;
45 class SummaryRecord;
46 
47 // One scene of a stream in Nancy19 and up, where it takes the place of the action
48 // records read from a scene's IFF. A scene is a Lua script, which is run once to
49 // make its records, and then kept around for the functions the records call.
50 // Every scene has an interpreter of its own, so the scripts of two scenes share
51 // nothing but what the engine keeps for them.
52 class SceneContext {
53 public:
54  SceneContext(const Common::String &streamName, const Common::String &sceneName, uint frame, int32 zOffset);
55  ~SceneContext();
56 
57  // Runs the script of the scene. Returns false when it is missing, or when it
58  // doesn't begin with a summary.
59  bool load();
60 
61  // One frame, which lasted the given number of seconds
62  void run(double elapsedTime);
63  void handleInput(NancyInput &input);
64 
65  // Tells the records that the game is about to be saved
66  void onSave();
67 
68  // Tells the records that the scene is being left, while the game goes on.
69  // They use it to undo what they did to the rest of the game, the UI for one.
70  void end();
71 
72  // The scene is being deleted
73  bool isEnding() const { return _isEnding; }
74 
75  // The Ref image of that name, which the records of the scene share. Places
76  // outside of it take the nearest pixel.
77  const RefImage *loadRef(const Common::String &name);
78 
79  // Hands a message to the scene when it is the one it names, and to the
80  // records that go by that name otherwise, until one of them takes it.
81  // Returns false when the message is for neither.
82  bool receiveMessage(const Message &message, bool &wasTaken);
83 
84  lua_State *getLua() const;
85 
86  // A new record only joins the scene at the start of the next frame. Until then
87  // it doesn't run, isn't drawn, and scripts that go over the records of the scene
88  // don't come across it.
89  void addRecord(Record *record) { _newRecords.push_back(record); }
90  bool hasRecords() const { return !_records.empty() || !_newRecords.empty(); }
91  const Common::Array<Record *> &getRecords() const { return _records; }
92 
93  // The record that describes the scene, which is always the first one
94  SummaryRecord *getSummary() const { return _summary; }
95 
96  const Common::String &getStreamName() const { return _streamName; }
97  const Common::String &getSceneName() const { return _sceneName; }
98  uint getFrame() const { return _frame; }
99  void setFrame(uint frame) { _frame = frame; }
100  int32 getZOffset() const { return _zOffset; }
101 
102  // The seconds the scene has been running for
103  double getTime() const { return _time; }
104  double getElapsedTime() const { return _elapsedTime; } // of the last frame
105 
106  // Enhancement: skips the movies that are playing. They are played through
107  // a frame at a time, without their sounds, so that everything the scripts
108  // tie to them still happens. Returns false when there is nothing to skip.
109  bool skipMovies();
110  bool isSkipping() const { return _isSkipping; }
111 
112  // The conversation of the scene, once one has begun
113  ConvoRecord *getConvo() const { return _convo; }
114  void setConvo(ConvoRecord *convo) { _convo = convo; }
115 
116  // Keeps a response the scripts made for as long as the scene lasts
117  void addConvoResponse(ConvoResponse *response) { _convoResponses.push_back(response); }
118 
119  // The panels a scene adds to the UI call into its scripts, so they go when it does
120  void setMakesUI() { _makesUI = true; }
121 
122  // Runs another script as a part of this scene
123  bool include(const Common::String &name);
124 
125  // Changes the scene of a stream; no name stands for the one this scene is in
126  void changeScene(const Common::String &streamName, const Common::String &sceneName, uint16 frame);
127 
128  // Scripts can take over parts of what a scene does by setting functions
129  // on the "Scene" object. Takes the function at the top of the stack.
130  void setSceneFunction(const char *name);
131 
132  // Where the mouse is in the viewport. Returns whether it is inside of it.
133  static bool getViewportMouse(const NancyInput &input, ScriptPoint &point);
134 
135 private:
136  bool callSceneFunction(const char *name, int numArgs);
137  void pushMessage(const Message &message);
138  void runFrame(double elapsedTime);
139  void deleteDroppedRecords();
140  void renderAttachment();
141  bool hasSkippableRecord() const;
142  void pushSceneFunctions();
143  void sendMouseEvent(int event, const ScriptPoint &point, bool inViewport);
144  void sendKeyEvent(int event, Common::KeyCode key);
145 
146  Common::String _streamName;
147  Common::String _sceneName;
148  bool _isEnding = false;
149  uint _frame;
150  int32 _zOffset;
151 
152  LuaState *_lua;
153  Common::Array<Record *> _records;
154  Common::Array<Record *> _newRecords;
155  Common::Array<Record *> _inputOrder; // the records, in the order the player's input goes to them
156  SummaryRecord *_summary = nullptr;
157  ConvoRecord *_convo = nullptr;
158  Common::Array<ConvoResponse *> _convoResponses;
160 
161  double _time = 0.0;
162  double _elapsedTime = 0.0;
163  bool _hasRunOnce = false;
164  bool _makesUI = false;
165  bool _isSkipping = false;
166  bool _hasChangedScene = false;
167 
168  // The keys records were told are down
169  uint16 _heldMoveKeys = 0;
170  Common::Array<Common::KeyCode> _pressedKeys;
171 };
172 
173 } // End of namespace Script
174 } // End of namespace Nancy
175 
176 #endif // NANCY_SCRIPT_SCENECONTEXT_H
Definition: types.h:91
Definition: str.h:59
Definition: array.h:52
Definition: lstate.h:100
Definition: input.h:41
Definition: scenecontext.h:52
Definition: luastate.h:38
Definition: convo.h:59
Definition: hashmap.h:85
Definition: convo.h:41
Definition: record.h:71
Definition: types.h:62
Definition: ref.h:41
Definition: records.h:93
Definition: actionmanager.h:32