Limit sendcvar calls to one per frame
authorterencehill <piuntn@gmail.com>
Mon, 19 Aug 2019 17:20:10 +0000 (19:20 +0200)
committerterencehill <piuntn@gmail.com>
Mon, 19 Aug 2019 17:20:10 +0000 (19:20 +0200)
qcsrc/client/defs.qh
qcsrc/client/view.qc
qcsrc/lib/replicate.qh

index 23760d7..c8ed213 100644 (file)
@@ -175,10 +175,12 @@ REPLICATE(cvar_cl_weaponpriorities[9], string, "cl_weaponpriority9");
 .int cvar_value;
 void ReplicateVars(bool would_destroy)
 {
-       FOREACH(Notifications, it.nent_type == MSG_CHOICE, {
-               string cvarname = sprintf("notification_%s", Get_Notif_CvarName(it));
-               REPLICATE_SIMPLE(it.cvar_value, cvarname);
-       });
+       if (!would_destroy)
+               FOREACH(Notifications, it.nent_type == MSG_CHOICE, {
+                       string cvarname = sprintf("notification_%s", Get_Notif_CvarName(it));
+                       // NOTE: REPLICATE_SIMPLE can return;
+                       REPLICATE_SIMPLE(it.cvar_value, cvarname);
+               });
 }
 
 float bgmtime;
index a744949..ac4d13a 100644 (file)
@@ -1541,7 +1541,6 @@ float oldr_useinfinitefarclip;
 float prev_myteam;
 int lasthud;
 float vh_notice_time;
-float ReplicateVars_time;
 void CSQC_UpdateView(entity this, float w, float h)
 {
        TC(int, w); TC(int, h);
@@ -1568,11 +1567,9 @@ void CSQC_UpdateView(entity this, float w, float h)
        // TODO remove references to sendcvar from cvar descriptions
        // TODO stop server from requesting cvar values to the client on connection as it's no longer necessary
 
-       if (time > ReplicateVars_time) // prevents network spam
-       {
-               ReplicateVars(false);
-               ReplicateVars_time = time + 0.8 + random() * 0.4;
-       }
+       ReplicateVars(false);
+       if (ReplicateVars_NOT_SENDING())
+               ReplicateVars_DELAY(0.8 + random() * 0.4); // no need to check cvars every frame
 
        HUD_Scale_Disable();
 
index 6b1dc20..7faa2ec 100644 (file)
                                store.fld = field; \
                        }
        #elif defined(CSQC)
+               float ReplicateVars_time;
+               #define ReplicateVars_NOT_SENDING() (time > ReplicateVars_time)
+               #define ReplicateVars_DELAY(t) ReplicateVars_time = time + t
+               #define ReplicateVars_DELAY_1FRAME() ReplicateVars_time = time
                #define REPLICATE_string(fld, var, func) REPLICATE_7(fld, float, var, func, (fld != cvar_string(var)), { strcpy(fld, cvar_string(var)); }, { strfree(fld); })
                #define REPLICATE_float(fld, var, func) REPLICATE_7(fld, float, var, func, (fld != cvar(var)), { fld = cvar(var); }, )
                #define REPLICATE_bool(fld, var, func) REPLICATE_7(fld, bool, var, func, (fld != cvar(var)), { fld = cvar(var); }, )
                        void ReplicateVars(bool would_destroy) \
                        { \
                                if (would_destroy > 0) { destroy } \
-                               else if (check) \
+                               else if (ReplicateVars_NOT_SENDING() && check) \
                                { \
                                        localcmd(strcat("cl_cmd sendcvar ", var, "\n")); \
+                                       ReplicateVars_DELAY_1FRAME(); \
                                        update \
+                                       return; \
                                } \
                        }
 
                #define REPLICATE_SIMPLE(field, cvarname) MACRO_BEGIN \
-                       if (field != cvar(cvarname)) \
+                       if (ReplicateVars_NOT_SENDING() && field != cvar(cvarname)) \
                        { \
                                localcmd(strcat("cl_cmd sendcvar ", cvarname, "\n")); \
+                               ReplicateVars_DELAY_1FRAME(); \
                                field = cvar(cvarname); \
+                               return; \
                        } \
                MACRO_END
        #endif