1 // SONIC ROBO BLAST 2
2 //-----------------------------------------------------------------------------
3 // Copyright (C) 1998-2000 by DooM Legacy Team.
4 // Copyright (C) 1999-2020 by Sonic Team Junior.
5 //
6 // This program is free software distributed under the
7 // terms of the GNU General Public License, version 2.
8 // See the 'LICENSE' file for more details.
9 //-----------------------------------------------------------------------------
10 /// \file  command.h
11 /// \brief Deals with commands from console input, scripts, and remote server
12 
13 #ifndef __COMMAND_H__
14 #define __COMMAND_H__
15 
16 #include <stdio.h>
17 #include "doomdef.h"
18 
19 //===================================
20 // Command buffer & command execution
21 //===================================
22 
23 /* Lua command registration flags. */
24 enum
25 {
26 	COM_ADMIN       = 1,
27 	COM_SPLITSCREEN = 2,
28 	COM_LOCAL       = 4,
29 };
30 
31 /* Command buffer flags. */
32 enum
33 {
34 	COM_SAFE = 1,
35 };
36 
37 typedef void (*com_func_t)(void);
38 
39 void COM_AddCommand(const char *name, com_func_t func);
40 int COM_AddLuaCommand(const char *name);
41 
42 size_t COM_Argc(void);
43 const char *COM_Argv(size_t arg); // if argv > argc, returns empty string
44 char *COM_Args(void);
45 size_t COM_CheckParm(const char *check); // like M_CheckParm :)
46 size_t COM_CheckPartialParm(const char *check);
47 size_t COM_FirstOption(void);
48 
49 // match existing command or NULL
50 const char *COM_CompleteCommand(const char *partial, INT32 skips);
51 
52 const char *COM_CompleteAlias(const char *partial, INT32 skips);
53 
54 // insert at queu (at end of other command)
55 #define COM_BufAddText(s) COM_BufAddTextEx(s, 0)
56 void COM_BufAddTextEx(const char *btext, int flags);
57 
58 // insert in head (before other command)
59 #define COM_BufInsertText(s) COM_BufInsertTextEx(s, 0)
60 void COM_BufInsertTextEx(const char *btext, int flags);
61 
62 // don't bother inserting, just do immediately
63 void COM_ImmedExecute(const char *ptext);
64 
65 // Execute commands in buffer, flush them
66 void COM_BufExecute(void);
67 
68 // As above; and progress the wait timer.
69 void COM_BufTicker(void);
70 
71 // setup command buffer, at game tartup
72 void COM_Init(void);
73 
74 // ======================
75 // Variable sized buffers
76 // ======================
77 
78 typedef struct vsbuf_s
79 {
80 	boolean allowoverflow; // if false, do a I_Error
81 	boolean overflowed; // set to true if the buffer size failed
82 	UINT8 *data;
83 	size_t maxsize;
84 	size_t cursize;
85 } vsbuf_t;
86 
87 void VS_Alloc(vsbuf_t *buf, size_t initsize);
88 void VS_Free(vsbuf_t *buf);
89 void VS_Clear(vsbuf_t *buf);
90 void *VS_GetSpace(vsbuf_t *buf, size_t length);
91 void VS_Write(vsbuf_t *buf, const void *data, size_t length);
92 void VS_WriteEx(vsbuf_t *buf, const void *data, size_t length, int flags);
93 void VS_Print(vsbuf_t *buf, const char *data); // strcats onto the sizebuf
94 
95 //==================
96 // Console variables
97 //==================
98 // console vars are variables that can be changed through code or console,
99 // at RUN TIME. They can also act as simplified commands, because a func-
100 // tion can be attached to a console var, which is called whenever the
101 // variable is modified (using flag CV_CALL).
102 
103 // flags for console vars
104 
105 typedef enum
106 {
107 	CV_SAVE = 1,   // save to config when quit game
108 	CV_CALL = 2,   // call function on change
109 	CV_NETVAR = 4, // send it when change (see logboris.txt at 12-4-2000)
110 	CV_NOINIT = 8, // dont call function when var is registered (1st set)
111 	CV_FLOAT = 16, // the value is fixed 16 : 16, where unit is FRACUNIT
112 	               // (allow user to enter 0.45 for ex)
113 	               // WARNING: currently only supports set with CV_Set()
114 	CV_NOTINNET = 32,    // some varaiable can't be changed in network but is not netvar (ex: splitscreen)
115 	CV_MODIFIED = 64,    // this bit is set when cvar is modified
116 	CV_SHOWMODIF = 128,  // say something when modified
117 	CV_SHOWMODIFONETIME = 256, // same but will be reset to 0 when modified, set in toggle
118 	CV_NOSHOWHELP = 512, // Don't show variable in the HELP list Tails 08-13-2002
119 	CV_HIDEN = 1024, // variable is not part of the cvar list so cannot be accessed by the console
120 	                 // can only be set when we have the pointer to it
121                    // used on menus
122 	CV_CHEAT = 2048, // Don't let this be used in multiplayer unless cheats are on.
123 	CV_NOLUA = 4096,/* don't let this be called from Lua */
124 } cvflags_t;
125 
126 typedef struct CV_PossibleValue_s
127 {
128 	INT32 value;
129 	const char *strvalue;
130 } CV_PossibleValue_t;
131 
132 typedef struct consvar_s //NULL, NULL, 0, NULL, NULL |, 0, NULL, NULL, 0, 0, NULL
133 {
134 	const char *name;
135 	const char *defaultvalue;
136 	INT32 flags;            // flags see cvflags_t above
137 	CV_PossibleValue_t *PossibleValue; // table of possible values
138 	void (*func)(void);   // called on change, if CV_CALL set
139 	INT32 value;            // for INT32 and fixed_t
140 	const char *string;   // value in string
141 	char *zstring;        // Either NULL or same as string.
142 	                      // If non-NULL, must be Z_Free'd later.
143 	struct
144 	{
145 		char allocated; // whether to Z_Free
146 		union
147 		{
148 			char       * string;
149 			const char * const_munge;
150 		} v;
151 	} revert;             // value of netvar before joining netgame
152 
153 	UINT16 netid; // used internaly : netid for send end receive
154 	                      // used only with CV_NETVAR
155 	char changed;         // has variable been changed by the user? 0 = no, 1 = yes
156 	struct consvar_s *next;
157 } consvar_t;
158 
159 /* name, defaultvalue, flags, PossibleValue, func */
160 #define CVAR_INIT( ... ) \
161 { __VA_ARGS__, 0, NULL, NULL, {0, {NULL}}, 0U, (char)0, NULL }
162 
163 #ifdef OLD22DEMOCOMPAT
164 typedef struct old_demo_var old_demo_var_t;
165 
166 struct old_demo_var
167 {
168 	UINT16  checksum;
169 	boolean collides;/* this var is a collision of multiple hashes */
170 
171 	consvar_t      *cvar;
172 	old_demo_var_t *next;
173 };
174 #endif/*OLD22DEMOCOMPAT*/
175 
176 extern CV_PossibleValue_t CV_OnOff[];
177 extern CV_PossibleValue_t CV_YesNo[];
178 extern CV_PossibleValue_t CV_Unsigned[];
179 extern CV_PossibleValue_t CV_Natural[];
180 
181 // Filter consvars by version
182 extern consvar_t cv_execversion;
183 
184 void CV_InitFilterVar(void);
185 void CV_ToggleExecVersion(boolean enable);
186 
187 // register a variable for use at the console
188 void CV_RegisterVar(consvar_t *variable);
189 
190 // returns a console variable by name
191 consvar_t *CV_FindVar(const char *name);
192 
193 // sets changed to 0 for every console variable
194 void CV_ClearChangedFlags(void);
195 
196 // returns the name of the nearest console variable name found
197 const char *CV_CompleteVar(char *partial, INT32 skips);
198 
199 // equivalent to "<varname> <value>" typed at the console
200 void CV_Set(consvar_t *var, const char *value);
201 
202 // expands value to a string and calls CV_Set
203 void CV_SetValue(consvar_t *var, INT32 value);
204 
205 // avoids calling the function if it is CV_CALL
206 void CV_StealthSetValue(consvar_t *var, INT32 value);
207 void CV_StealthSet(consvar_t *var, const char *value);
208 
209 // it a setvalue but with a modulo at the maximum
210 void CV_AddValue(consvar_t *var, INT32 increment);
211 
212 // write all CV_SAVE variables to config file
213 void CV_SaveVariables(FILE *f);
214 
215 // load/save gamesate (load and save option and for network join in game)
216 void CV_SaveVars(UINT8 **p, boolean in_demo);
217 
218 #define CV_SaveNetVars(p) CV_SaveVars(p, false)
219 void CV_LoadNetVars(UINT8 **p);
220 
221 // then revert after leaving a netgame
222 void CV_RevertNetVars(void);
223 
224 #define CV_SaveDemoVars(p) CV_SaveVars(p, true)
225 void CV_LoadDemoVars(UINT8 **p);
226 
227 #ifdef OLD22DEMOCOMPAT
228 void CV_LoadOldDemoVars(UINT8 **p);
229 #endif
230 
231 // reset cheat netvars after cheats is deactivated
232 void CV_ResetCheatNetVars(void);
233 
234 boolean CV_IsSetToDefault(consvar_t *v);
235 UINT8 CV_CheatsEnabled(void);
236 
237 #endif // __COMMAND_H__
238