l_libvar.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. /*****************************************************************************
  19. * name: l_libvar.c
  20. *
  21. * desc: bot library variables
  22. *
  23. * $Archive: /MissionPack/code/botlib/l_libvar.c $
  24. *
  25. *****************************************************************************/
  26. #include "../game/q_shared.h"
  27. #include "l_memory.h"
  28. #include "l_libvar.h"
  29. //list with library variables
  30. libvar_t *libvarlist;
  31. //===========================================================================
  32. //
  33. // Parameter: -
  34. // Returns: -
  35. // Changes Globals: -
  36. //===========================================================================
  37. float LibVarStringValue(char *string)
  38. {
  39. int dotfound = 0;
  40. float value = 0;
  41. while(*string)
  42. {
  43. if (*string < '0' || *string > '9')
  44. {
  45. if (dotfound || *string != '.')
  46. {
  47. return 0;
  48. } //end if
  49. else
  50. {
  51. dotfound = 10;
  52. string++;
  53. } //end if
  54. } //end if
  55. if (dotfound)
  56. {
  57. value = value + (float) (*string - '0') / (float) dotfound;
  58. dotfound *= 10;
  59. } //end if
  60. else
  61. {
  62. value = value * 10.0 + (float) (*string - '0');
  63. } //end else
  64. string++;
  65. } //end while
  66. return value;
  67. } //end of the function LibVarStringValue
  68. //===========================================================================
  69. //
  70. // Parameter: -
  71. // Returns: -
  72. // Changes Globals: -
  73. //===========================================================================
  74. libvar_t *LibVarAlloc(char *var_name)
  75. {
  76. libvar_t *v;
  77. v = (libvar_t *) GetMemory(sizeof(libvar_t) + strlen(var_name) + 1);
  78. Com_Memset(v, 0, sizeof(libvar_t));
  79. v->name = (char *) v + sizeof(libvar_t);
  80. strcpy(v->name, var_name);
  81. //add the variable in the list
  82. v->next = libvarlist;
  83. libvarlist = v;
  84. return v;
  85. } //end of the function LibVarAlloc
  86. //===========================================================================
  87. //
  88. // Parameter: -
  89. // Returns: -
  90. // Changes Globals: -
  91. //===========================================================================
  92. void LibVarDeAlloc(libvar_t *v)
  93. {
  94. if (v->string) FreeMemory(v->string);
  95. FreeMemory(v);
  96. } //end of the function LibVarDeAlloc
  97. //===========================================================================
  98. //
  99. // Parameter: -
  100. // Returns: -
  101. // Changes Globals: -
  102. //===========================================================================
  103. void LibVarDeAllocAll(void)
  104. {
  105. libvar_t *v;
  106. for (v = libvarlist; v; v = libvarlist)
  107. {
  108. libvarlist = libvarlist->next;
  109. LibVarDeAlloc(v);
  110. } //end for
  111. libvarlist = NULL;
  112. } //end of the function LibVarDeAllocAll
  113. //===========================================================================
  114. //
  115. // Parameter: -
  116. // Returns: -
  117. // Changes Globals: -
  118. //===========================================================================
  119. libvar_t *LibVarGet(char *var_name)
  120. {
  121. libvar_t *v;
  122. for (v = libvarlist; v; v = v->next)
  123. {
  124. if (!Q_stricmp(v->name, var_name))
  125. {
  126. return v;
  127. } //end if
  128. } //end for
  129. return NULL;
  130. } //end of the function LibVarGet
  131. //===========================================================================
  132. //
  133. // Parameter: -
  134. // Returns: -
  135. // Changes Globals: -
  136. //===========================================================================
  137. char *LibVarGetString(char *var_name)
  138. {
  139. libvar_t *v;
  140. v = LibVarGet(var_name);
  141. if (v)
  142. {
  143. return v->string;
  144. } //end if
  145. else
  146. {
  147. return "";
  148. } //end else
  149. } //end of the function LibVarGetString
  150. //===========================================================================
  151. //
  152. // Parameter: -
  153. // Returns: -
  154. // Changes Globals: -
  155. //===========================================================================
  156. float LibVarGetValue(char *var_name)
  157. {
  158. libvar_t *v;
  159. v = LibVarGet(var_name);
  160. if (v)
  161. {
  162. return v->value;
  163. } //end if
  164. else
  165. {
  166. return 0;
  167. } //end else
  168. } //end of the function LibVarGetValue
  169. //===========================================================================
  170. //
  171. // Parameter: -
  172. // Returns: -
  173. // Changes Globals: -
  174. //===========================================================================
  175. libvar_t *LibVar(char *var_name, char *value)
  176. {
  177. libvar_t *v;
  178. v = LibVarGet(var_name);
  179. if (v) return v;
  180. //create new variable
  181. v = LibVarAlloc(var_name);
  182. //variable string
  183. v->string = (char *) GetMemory(strlen(value) + 1);
  184. strcpy(v->string, value);
  185. //the value
  186. v->value = LibVarStringValue(v->string);
  187. //variable is modified
  188. v->modified = qtrue;
  189. //
  190. return v;
  191. } //end of the function LibVar
  192. //===========================================================================
  193. //
  194. // Parameter: -
  195. // Returns: -
  196. // Changes Globals: -
  197. //===========================================================================
  198. char *LibVarString(char *var_name, char *value)
  199. {
  200. libvar_t *v;
  201. v = LibVar(var_name, value);
  202. return v->string;
  203. } //end of the function LibVarString
  204. //===========================================================================
  205. //
  206. // Parameter: -
  207. // Returns: -
  208. // Changes Globals: -
  209. //===========================================================================
  210. float LibVarValue(char *var_name, char *value)
  211. {
  212. libvar_t *v;
  213. v = LibVar(var_name, value);
  214. return v->value;
  215. } //end of the function LibVarValue
  216. //===========================================================================
  217. //
  218. // Parameter: -
  219. // Returns: -
  220. // Changes Globals: -
  221. //===========================================================================
  222. void LibVarSet(char *var_name, char *value)
  223. {
  224. libvar_t *v;
  225. v = LibVarGet(var_name);
  226. if (v)
  227. {
  228. FreeMemory(v->string);
  229. } //end if
  230. else
  231. {
  232. v = LibVarAlloc(var_name);
  233. } //end else
  234. //variable string
  235. v->string = (char *) GetMemory(strlen(value) + 1);
  236. strcpy(v->string, value);
  237. //the value
  238. v->value = LibVarStringValue(v->string);
  239. //variable is modified
  240. v->modified = qtrue;
  241. } //end of the function LibVarSet
  242. //===========================================================================
  243. //
  244. // Parameter: -
  245. // Returns: -
  246. // Changes Globals: -
  247. //===========================================================================
  248. qboolean LibVarChanged(char *var_name)
  249. {
  250. libvar_t *v;
  251. v = LibVarGet(var_name);
  252. if (v)
  253. {
  254. return v->modified;
  255. } //end if
  256. else
  257. {
  258. return qfalse;
  259. } //end else
  260. } //end of the function LibVarChanged
  261. //===========================================================================
  262. //
  263. // Parameter: -
  264. // Returns: -
  265. // Changes Globals: -
  266. //===========================================================================
  267. void LibVarSetNotModified(char *var_name)
  268. {
  269. libvar_t *v;
  270. v = LibVarGet(var_name);
  271. if (v)
  272. {
  273. v->modified = qfalse;
  274. } //end if
  275. } //end of the function LibVarSetNotModified