]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/replicate.qh
On client connection, send client cvars only after client has been initialized on...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / replicate.qh
1 #pragma once
2
3 #ifdef SVQC
4 // copies a string to a tempstring (so one can strunzone it)
5 string strcat1(string s) = #115; // FRIK_FILE
6 #endif
7
8 #if defined(CSQC)
9 const int REPLICATEVARS_SEND_ALL = -1; // sync all cvars with the server (init)
10 const int REPLICATEVARS_CHECK = 0; // check if any cvar has changed and sync it with the server
11 const int REPLICATEVARS_DESTROY = 1; // destroy data associated with cvars (shutdown)
12 #define REPLICATE_INIT(type, name) type name
13 #elif defined(SVQC)
14 #define REPLICATE_INIT(type, name) .type name
15 #endif
16
17 #ifdef GAMEQC
18
19     /**
20      * \def REPLICATE(fld, type, cvar)
21      * Replicates a client cvar into a server field
22      *
23      * @param fld   The field to replicate into
24      * @param type  The field type
25      * @param cvarname
26      * @param fixup_func((entity this, string original_value))  optional parameter
27      */
28         #define REPLICATE(...) EVAL_REPLICATE(OVERLOAD(REPLICATE, __VA_ARGS__))
29         #define EVAL_REPLICATE(...) __VA_ARGS__
30
31         #if defined(SVQC)
32         ACCUMULATE void ReplicateVars(entity this, entity store, string thisname, int i) {}
33         ACCUMULATE void ReplicateVars_ApplyChange(entity this, entity store, string thisname, int i) {}
34     /**
35      * \def REPLICATE_APPLYCHANGE(cvarname, ApplyChange_code)
36      * Allows setting code that will be executed on cvar value changes
37      *
38      * @param cvarname
39      * @param ApplyChange_code  code meant to be run on cvar value changes
40      */
41         #define REPLICATE_APPLYCHANGE(var, ApplyChange_code) \
42                 void ReplicateVars_ApplyChange(entity this, entity store, string thisname, int i) \
43                         { if (thisname == var) { ApplyChange_code } }
44         #elif defined(CSQC)
45         noref float ReplicateVars_time;
46         ACCUMULATE void ReplicateVars(int mode)
47         {
48                 if (!ReplicateVars_time || time < ReplicateVars_time)
49                         return;
50                 ReplicateVars_time = time + 0.8 + random() * 0.4; // check cvars periodically
51         }
52         void ReplicateVars_Start()
53         {
54                 ReplicateVars_time = time;
55                 ReplicateVars(REPLICATEVARS_SEND_ALL);
56         }
57         #endif
58
59         #define REPLICATE_3(fld, type, var) REPLICATE_4(fld, type, var, )
60         #define REPLICATE_4(fld, type, var, func) REPLICATE_##type(fld, var, func)
61         #if defined(SVQC)
62                 #define REPLICATE_string(fld, var, func) \
63                         REPLICATE_7(fld, string, var, , \
64                 { strcpy(field, it); }, \
65                 { strfree(field); }, \
66                 { \
67                         /* also initialize to the default value of func when requesting cvars */ \
68                         string s = func(this, strcat1(field)); \
69                         if (s != field) \
70                         { \
71                                 strcpy(field, s); \
72                         } \
73                 })
74                 #define REPLICATE_float(fld, var, func) REPLICATE_7(fld, float, var, func,  { field = stof(it); },          , )
75                 #define REPLICATE_bool(fld, var, func) REPLICATE_7(fld, bool, var, func,   { field = boolean(stoi(it)); }, , )
76                 #define REPLICATE_int(fld, var, func) REPLICATE_7(fld, int, var, func,    { field = stoi(it); },          , )
77
78                 #define REPLICATE_7(fld, type, var, func, create, destroy, after) \
79                         void ReplicateVars(entity this, entity store, string thisname, int i) \
80                         { \
81                                 type field = store.fld; \
82                                 if (i < 0) { destroy } \
83                                 else \
84                                 { \
85                                         string it = func(argv(i + 1)); \
86                                         bool current = thisname == var; \
87                                         if (i > 0) \
88                                         { \
89                                                 if (current) { create } \
90                                         } \
91                                         else \
92                                         { \
93                                                 stuffcmd(this, strcat("cl_cmd sendcvar ", var, "\n")); \
94                                         } \
95                                         if (current) { after } \
96                                 } \
97                                 store.fld = field; \
98                         }
99         #elif defined(CSQC)
100                 #define REPLICATE_string(fld, var, func) REPLICATE_7(fld, float, var, func, (fld != cvar_string(var)), { strcpy(fld, cvar_string(var)); }, { strfree(fld); })
101                 #define REPLICATE_float(fld, var, func) REPLICATE_7(fld, float, var, func, (fld != cvar(var)), { fld = cvar(var); }, )
102                 #define REPLICATE_bool(fld, var, func) REPLICATE_7(fld, bool, var, func, (fld != cvar(var)), { fld = cvar(var); }, )
103                 #define REPLICATE_int(fld, var, func) REPLICATE_7(fld, int, var, func, (fld != cvar(var)), { fld = cvar(var); }, )
104
105                 void ReplicateVars_Send(string cvarname) { localcmd(strcat("cl_cmd sendcvar ", cvarname, "\n")); }
106
107                 #define REPLICATE_7(fld, type, var, func, check, update, destroy) \
108                         void ReplicateVars(int mode) \
109                         { \
110                                 if (mode == REPLICATEVARS_DESTROY) { destroy } \
111                                 else if (mode == REPLICATEVARS_SEND_ALL || check) \
112                                 { \
113                                         ReplicateVars_Send(var); \
114                                         update \
115                                 } \
116                         }
117
118                 #define REPLICATE_SIMPLE(field, cvarname) MACRO_BEGIN \
119                         float thecvar = cvar(cvarname); \
120                         if(field != thecvar) \
121                         { \
122                                 ReplicateVars_Send(cvarname); \
123                                 field = thecvar; \
124                         } \
125                 MACRO_END
126         #endif
127
128 #endif