1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14 //
15 // Interaction structs.
16 //
17 //-----------------------------------------------------------------------------
18 //
19 // Most of the interaction types here were used before the script and have
20 // very limited capabilities. They were removed from AGS completely in
21 // generation 3.0. The code is left for backwards compatibility.
22 //
23 //-----------------------------------------------------------------------------
24 //
25 /* THE WAY THIS WORKS:
26 *
27 * Interaction (Hotspot 1)
28 *  |
29 *  +-- eventTypes [NUM_EVENTS]
30 *  +-- InteractionCommandList [NUM_EVENTS]   (Look at hotspot)
31 *        |
32 *        +-- InteractionCommand [NUM_COMMANDS]   (Display message)
33 *             |
34 *             +-- InteractionValue [NUM_ARGUMENTS]   (5)
35 */
36 //
37 //=============================================================================
38 #ifndef __AGS_CN_GAME__INTEREACTIONS_H
39 #define __AGS_CN_GAME__INTEREACTIONS_H
40 
41 #include <memory>
42 #include "util/string_types.h"
43 
44 #define LOCAL_VARIABLE_OFFSET       10000
45 #define MAX_GLOBAL_VARIABLES        100
46 #define MAX_ACTION_ARGS             5
47 #define MAX_NEWINTERACTION_EVENTS   30
48 #define MAX_COMMANDS_PER_LIST       40
49 
50 namespace AGS
51 {
52 namespace Common
53 {
54 
55 enum InterValType
56 {
57     kInterValLiteralInt = 1,
58     kInterValVariable   = 2,
59     kInterValBoolean    = 3,
60     kInterValCharnum    = 4
61 };
62 
63 enum InteractionVersion
64 {
65     kInteractionVersion_Initial = 1
66 };
67 
68 // InteractionValue represents an argument of interaction command
69 struct InteractionValue
70 {
71     InterValType Type;  // value type
72     int          Value; // value definition
73     int          Extra;
74 
75     InteractionValue();
76 
77     void Read(Stream *in);
78     void Write(Stream *out) const;
79 };
80 
81 
82 struct InteractionCommandList;
83 typedef std::auto_ptr<InteractionCommandList> AInterCmdList; // TODO: use unique_ptr when available
84 
85 // InteractionCommand represents a single command (action), an item of Command List
86 struct InteractionCommand
87 {
88     int                     Type;       // type of action
89     InteractionValue        Data[MAX_ACTION_ARGS]; // action arguments
90     AInterCmdList           Children;   // list of sub-actions
91     InteractionCommandList *Parent;     // action parent (optional)
92 
93     InteractionCommand();
94     InteractionCommand(const InteractionCommand &ic);
95 
96     void Assign(const InteractionCommand &ic, InteractionCommandList *parent);
97     void Reset();
98 
99     void Read_v321(Stream *in, bool &has_children);
100     void Write_v321(Stream *out) const;
101 
102     InteractionCommand &operator = (const InteractionCommand &ic);
103 
104 private:
105     void ReadValues_Aligned(Stream *in);
106     void WriteValues_Aligned(Stream *out) const;
107 };
108 
109 
110 typedef std::vector<InteractionCommand> InterCmdVector;
111 // InteractionCommandList represents a list of commands (actions) that need to be
112 // performed on particular game event
113 struct InteractionCommandList
114 {
115     InterCmdVector  Cmds;     // actions to run
116     int             TimesRun; // used by engine to track score changes
117 
118     InteractionCommandList();
119     InteractionCommandList(const InteractionCommandList &icmd_list);
120 
121     void Reset();
122 
123     void Read_v321(Stream *in);
124     void Write_v321(Stream *out) const;
125 
126 protected:
127     void Read_Aligned(Common::Stream *in, std::vector<bool> &cmd_children);
128     void Write_Aligned(Common::Stream *out) const;
129 };
130 
131 
132 // InteractionEvent is a single event with a list of commands to performed
133 struct InteractionEvent
134 {
135     int           Type;     // type of event
136     int           TimesRun; // used by engine to track score changes
137     AInterCmdList Response; // list of commands to run
138 
139     InteractionEvent();
140     InteractionEvent(const InteractionEvent &ie);
141 
142     InteractionEvent &operator = (const InteractionEvent &ic);
143 };
144 
145 typedef std::vector<InteractionEvent> InterEvtVector;
146 // Interaction is the list of events and responses for a game or game object
147 struct Interaction
148 {
149     // The first few event types depend on the item - ID's of 100+ are
150     // custom events (used for subroutines)
151     InterEvtVector Events;
152 
153     Interaction();
154     Interaction(const Interaction &inter);
155 
156     // Copy information on number of times events of this interaction were fired
157     void CopyTimesRun(const Interaction &inter);
158     void Reset();
159 
160     // Game static data (de)serialization
161     static Interaction *CreateFromStream(Stream *in);
162     void                Write(Stream *out) const;
163 
164     // Reading and writing runtime data from/to savedgame;
165     // NOTE: these are backwards-compatible methods, that do not always
166     // have practical sense
167     void ReadFromSavedgame_v321(Stream *in);
168     void WriteToSavedgame_v321(Stream *out) const;
169     void ReadTimesRunFromSavedgame(Stream *in);
170     void WriteTimesRunToSavedgame(Stream *out) const;
171 
172     Interaction &operator =(const Interaction &inter);
173 };
174 
175 
176 // Legacy pre-3.0 kind of global variables
177 struct InteractionVariable
178 {
179     String Name;
180     char   Type;
181     int    Value;
182 
183     InteractionVariable();
184     InteractionVariable(const String &name, char type, int val);
185 
186     void Read(Stream *in);
187     void Write(Stream *out) const;
188 };
189 
190 
191 // A list of script function names for all supported events
192 struct InteractionScripts
193 {
194     StringV ScriptFuncNames;
195 
196     static InteractionScripts *CreateFromStream(Stream *in);
197 };
198 
199 } // namespace Common
200 } // namespace AGS
201 
202 // Legacy global variables
203 extern AGS::Common::InteractionVariable globalvars[MAX_GLOBAL_VARIABLES];
204 extern int numGlobalVars;
205 
206 #endif // __AGS_CN_GAME__INTEREACTIONS_H
207