1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __SYS_EVENT_H__
30 #define __SYS_EVENT_H__
31 
32 #include "idlib/containers/LinkList.h"
33 #include "cm/CollisionModel.h"
34 
35 // Event are used for scheduling tasks and for linking script commands.
36 
37 #define D_EVENT_MAXARGS				8			// if changed, enable the CREATE_EVENT_CODE define in Event.cpp to generate switch statement for idClass::ProcessEventArgPtr.
38 												// running the game will then generate c:\doom\base\events.txt, the contents of which should be copied into the switch statement.
39 
40 // stack size of idVec3, aligned to native pointer size
41 #define E_EVENT_SIZEOF_VEC			((sizeof(idVec3) + (sizeof(intptr_t) - 1)) & ~(sizeof(intptr_t) - 1))
42 
43 #define D_EVENT_VOID				( ( char )0 )
44 #define D_EVENT_INTEGER				'd'
45 #define D_EVENT_FLOAT				'f'
46 #define D_EVENT_VECTOR				'v'
47 #define D_EVENT_STRING				's'
48 #define D_EVENT_ENTITY				'e'
49 #define	D_EVENT_ENTITY_NULL			'E'			// event can handle NULL entity pointers
50 #define D_EVENT_TRACE				't'
51 
52 #define MAX_EVENTS					4096
53 
54 class idClass;
55 class idTypeInfo;
56 
57 class idEventDef {
58 private:
59 	const char					*name;
60 	const char					*formatspec;
61 	unsigned int				formatspecIndex;
62 	int							returnType;
63 	int							numargs;
64 	size_t						argsize;
65 	int							argOffset[ D_EVENT_MAXARGS ];
66 	int							eventnum;
67 	const idEventDef *			next;
68 
69 	static idEventDef *			eventDefList[MAX_EVENTS];
70 	static int					numEventDefs;
71 
72 public:
73 								idEventDef( const char *command, const char *formatspec = NULL, char returnType = 0 );
74 
75 	const char					*GetName( void ) const;
76 	const char					*GetArgFormat( void ) const;
77 	unsigned int				GetFormatspecIndex( void ) const;
78 	char						GetReturnType( void ) const;
79 	int							GetEventNum( void ) const;
80 	int							GetNumArgs( void ) const;
81 	size_t						GetArgSize( void ) const;
82 	int							GetArgOffset( int arg ) const;
83 
84 	static int					NumEventCommands( void );
85 	static const idEventDef		*GetEventCommand( int eventnum );
86 	static const idEventDef		*FindEvent( const char *name );
87 };
88 
89 class idSaveGame;
90 class idRestoreGame;
91 
92 class idEvent {
93 private:
94 	const idEventDef			*eventdef;
95 	byte						*data;
96 	int							time;
97 	idClass						*object;
98 	const idTypeInfo			*typeinfo;
99 
100 	idLinkList<idEvent>			eventNode;
101 
102 	static idDynamicBlockAlloc<byte, 16 * 1024, 256> eventDataAllocator;
103 
104 
105 public:
106 	static bool					initialized;
107 
108 								~idEvent();
109 
110 	static idEvent				*Alloc( const idEventDef *evdef, int numargs, va_list args );
111 	static void					CopyArgs( const idEventDef *evdef, int numargs, va_list args, intptr_t data[ D_EVENT_MAXARGS ]  );
112 
113 	void						Free( void );
114 	void						Schedule( idClass *object, const idTypeInfo *cls, int time );
115 	byte						*GetData( void );
116 
117 	static void					CancelEvents( const idClass *obj, const idEventDef *evdef = NULL );
118 	static void					ClearEventList( void );
119 	static void					ServiceEvents( void );
120 	static void					Init( void );
121 	static void					Shutdown( void );
122 
123 	// save games
124 	static void					Save( idSaveGame *savefile );					// archives object for save game file
125 	static void					Restore( idRestoreGame *savefile );				// unarchives object from save game file
126 	static void					SaveTrace( idSaveGame *savefile, const trace_t &trace );
127 	static void					RestoreTrace( idRestoreGame *savefile, trace_t &trace );
128 
129 };
130 
131 /*
132 ================
133 idEvent::GetData
134 ================
135 */
GetData(void)136 ID_INLINE byte *idEvent::GetData( void ) {
137 	return data;
138 }
139 
140 /*
141 ================
142 idEventDef::GetName
143 ================
144 */
GetName(void)145 ID_INLINE const char *idEventDef::GetName( void ) const {
146 	return name;
147 }
148 
149 /*
150 ================
151 idEventDef::GetArgFormat
152 ================
153 */
GetArgFormat(void)154 ID_INLINE const char *idEventDef::GetArgFormat( void ) const {
155 	return formatspec;
156 }
157 
158 /*
159 ================
160 idEventDef::GetFormatspecIndex
161 ================
162 */
GetFormatspecIndex(void)163 ID_INLINE unsigned int idEventDef::GetFormatspecIndex( void ) const {
164 	return formatspecIndex;
165 }
166 
167 /*
168 ================
169 idEventDef::GetReturnType
170 ================
171 */
GetReturnType(void)172 ID_INLINE char idEventDef::GetReturnType( void ) const {
173 	return returnType;
174 }
175 
176 /*
177 ================
178 idEventDef::GetNumArgs
179 ================
180 */
GetNumArgs(void)181 ID_INLINE int idEventDef::GetNumArgs( void ) const {
182 	return numargs;
183 }
184 
185 /*
186 ================
187 idEventDef::GetArgSize
188 ================
189 */
GetArgSize(void)190 ID_INLINE size_t idEventDef::GetArgSize( void ) const {
191 	return argsize;
192 }
193 
194 /*
195 ================
196 idEventDef::GetArgOffset
197 ================
198 */
GetArgOffset(int arg)199 ID_INLINE int idEventDef::GetArgOffset( int arg ) const {
200 	assert( ( arg >= 0 ) && ( arg < D_EVENT_MAXARGS ) );
201 	return argOffset[ arg ];
202 }
203 
204 /*
205 ================
206 idEventDef::GetEventNum
207 ================
208 */
GetEventNum(void)209 ID_INLINE int idEventDef::GetEventNum( void ) const {
210 	return eventnum;
211 }
212 
213 #endif /* !__SYS_EVENT_H__ */
214