home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / source / c / q1source_amy / qw / client / cvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-21  |  4.7 KB  |  250 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // cvar.c -- dynamic variable tracking
  21.  
  22. #ifdef SERVERONLY 
  23. #include "qwsvdef.h"
  24. #else
  25. #include "quakedef.h"
  26. #endif
  27.  
  28. cvar_t    *cvar_vars;
  29. char    *cvar_null_string = "";
  30.  
  31. /*
  32. ============
  33. Cvar_FindVar
  34. ============
  35. */
  36. cvar_t *Cvar_FindVar (char *var_name)
  37. {
  38.     cvar_t    *var;
  39.     
  40.     for (var=cvar_vars ; var ; var=var->next)
  41.         if (!Q_strcmp (var_name, var->name))
  42.             return var;
  43.  
  44.     return NULL;
  45. }
  46.  
  47. /*
  48. ============
  49. Cvar_VariableValue
  50. ============
  51. */
  52. float    Cvar_VariableValue (char *var_name)
  53. {
  54.     cvar_t    *var;
  55.     
  56.     var = Cvar_FindVar (var_name);
  57.     if (!var)
  58.         return 0;
  59.     return Q_atof (var->string);
  60. }
  61.  
  62.  
  63. /*
  64. ============
  65. Cvar_VariableString
  66. ============
  67. */
  68. char *Cvar_VariableString (char *var_name)
  69. {
  70.     cvar_t *var;
  71.     
  72.     var = Cvar_FindVar (var_name);
  73.     if (!var)
  74.         return cvar_null_string;
  75.     return var->string;
  76. }
  77.  
  78.  
  79. /*
  80. ============
  81. Cvar_CompleteVariable
  82. ============
  83. */
  84. char *Cvar_CompleteVariable (char *partial)
  85. {
  86.     cvar_t        *cvar;
  87.     int            len;
  88.     
  89.     len = Q_strlen(partial);
  90.     
  91.     if (!len)
  92.         return NULL;
  93.         
  94.     // check exact match
  95.     for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
  96.         if (!strcmp (partial,cvar->name))
  97.             return cvar->name;
  98.  
  99.     // check partial match
  100.     for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
  101.         if (!Q_strncmp (partial,cvar->name, len))
  102.             return cvar->name;
  103.  
  104.     return NULL;
  105. }
  106.  
  107.  
  108. #ifdef SERVERONLY
  109. void SV_SendServerInfoChange(char *key, char *value);
  110. #endif
  111.  
  112. /*
  113. ============
  114. Cvar_Set
  115. ============
  116. */
  117. void Cvar_Set (char *var_name, char *value)
  118. {
  119.     cvar_t    *var;
  120.     
  121.     var = Cvar_FindVar (var_name);
  122.     if (!var)
  123.     {    // there is an error in C code if this happens
  124.         Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
  125.         return;
  126.     }
  127.  
  128. #ifdef SERVERONLY
  129.     if (var->info)
  130.     {
  131.         Info_SetValueForKey (svs.info, var_name, value, MAX_SERVERINFO_STRING);
  132.         SV_SendServerInfoChange(var_name, value);
  133. //        SV_BroadcastCommand ("fullserverinfo \"%s\"\n", svs.info);
  134.     }
  135. #else
  136.     if (var->info)
  137.     {
  138.         Info_SetValueForKey (cls.userinfo, var_name, value, MAX_INFO_STRING);
  139.         if (cls.state >= ca_connected)
  140.         {
  141.             MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
  142.             SZ_Print (&cls.netchan.message, va("setinfo \"%s\" \"%s\"\n", var_name, value));
  143.         }
  144.     }
  145. #endif
  146.     
  147.     Z_Free (var->string);    // free the old value string
  148.     
  149.     var->string = Z_Malloc (Q_strlen(value)+1);
  150.     Q_strcpy (var->string, value);
  151.     var->value = Q_atof (var->string);
  152. }
  153.  
  154. /*
  155. ============
  156. Cvar_SetValue
  157. ============
  158. */
  159. void Cvar_SetValue (char *var_name, float value)
  160. {
  161.     char    val[32];
  162.     
  163.     sprintf (val, "%f",value);
  164.     Cvar_Set (var_name, val);
  165. }
  166.  
  167.  
  168. /*
  169. ============
  170. Cvar_RegisterVariable
  171.  
  172. Adds a freestanding variable to the variable list.
  173. ============
  174. */
  175. void Cvar_RegisterVariable (cvar_t *variable)
  176. {
  177.     char    value[512];
  178.  
  179. // first check to see if it has allready been defined
  180.     if (Cvar_FindVar (variable->name))
  181.     {
  182.         Con_Printf ("Can't register variable %s, allready defined\n", variable->name);
  183.         return;
  184.     }
  185.     
  186. // check for overlap with a command
  187.     if (Cmd_Exists (variable->name))
  188.     {
  189.         Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
  190.         return;
  191.     }
  192.         
  193. // link the variable in
  194.     variable->next = cvar_vars;
  195.     cvar_vars = variable;
  196.  
  197. // copy the value off, because future sets will Z_Free it
  198.     strcpy (value, variable->string);
  199.     variable->string = Z_Malloc (1);    
  200.     
  201. // set it through the function to be consistant
  202.     Cvar_Set (variable->name, value);
  203. }
  204.  
  205. /*
  206. ============
  207. Cvar_Command
  208.  
  209. Handles variable inspection and changing from the console
  210. ============
  211. */
  212. qboolean    Cvar_Command (void)
  213. {
  214.     cvar_t            *v;
  215.  
  216. // check variables
  217.     v = Cvar_FindVar (Cmd_Argv(0));
  218.     if (!v)
  219.         return false;
  220.         
  221. // perform a variable print or set
  222.     if (Cmd_Argc() == 1)
  223.     {
  224.         Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
  225.         return true;
  226.     }
  227.  
  228.     Cvar_Set (v->name, Cmd_Argv(1));
  229.     return true;
  230. }
  231.  
  232.  
  233. /*
  234. ============
  235. Cvar_WriteVariables
  236.  
  237. Writes lines containing "set variable value" for all variables
  238. with the archive flag set to true.
  239. ============
  240. */
  241. void Cvar_WriteVariables (FILE *f)
  242. {
  243.     cvar_t    *var;
  244.     
  245.     for (var = cvar_vars ; var ; var = var->next)
  246.         if (var->archive)
  247.             fprintf (f, "%s \"%s\"\n", var->name, var->string);
  248. }
  249.  
  250.