l_bsp_ent.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "l_cmd.h"
  19. #include "l_math.h"
  20. #include "l_mem.h"
  21. #include "l_log.h"
  22. #include "../botlib/l_script.h"
  23. #include "l_bsp_ent.h"
  24. #define MAX_KEY 32
  25. #define MAX_VALUE 1024
  26. int num_entities;
  27. entity_t entities[MAX_MAP_ENTITIES];
  28. void StripTrailing(char *e)
  29. {
  30. char *s;
  31. s = e + strlen(e)-1;
  32. while (s >= e && *s <= 32)
  33. {
  34. *s = 0;
  35. s--;
  36. }
  37. }
  38. /*
  39. =================
  40. ParseEpair
  41. =================
  42. */
  43. epair_t *ParseEpair(script_t *script)
  44. {
  45. epair_t *e;
  46. token_t token;
  47. e = GetMemory(sizeof(epair_t));
  48. memset (e, 0, sizeof(epair_t));
  49. PS_ExpectAnyToken(script, &token);
  50. StripDoubleQuotes(token.string);
  51. if (strlen(token.string) >= MAX_KEY-1)
  52. Error ("ParseEpair: token %s too long", token.string);
  53. e->key = copystring(token.string);
  54. PS_ExpectAnyToken(script, &token);
  55. StripDoubleQuotes(token.string);
  56. if (strlen(token.string) >= MAX_VALUE-1)
  57. Error ("ParseEpair: token %s too long", token.string);
  58. e->value = copystring(token.string);
  59. // strip trailing spaces
  60. StripTrailing(e->key);
  61. StripTrailing(e->value);
  62. return e;
  63. } //end of the function ParseEpair
  64. /*
  65. ================
  66. ParseEntity
  67. ================
  68. */
  69. qboolean ParseEntity(script_t *script)
  70. {
  71. epair_t *e;
  72. entity_t *mapent;
  73. token_t token;
  74. if (!PS_ReadToken(script, &token))
  75. return false;
  76. if (strcmp(token.string, "{"))
  77. Error ("ParseEntity: { not found");
  78. if (num_entities == MAX_MAP_ENTITIES)
  79. Error ("num_entities == MAX_MAP_ENTITIES");
  80. mapent = &entities[num_entities];
  81. num_entities++;
  82. do
  83. {
  84. if (!PS_ReadToken(script, &token))
  85. Error ("ParseEntity: EOF without closing brace");
  86. if (!strcmp(token.string, "}") )
  87. break;
  88. PS_UnreadLastToken(script);
  89. e = ParseEpair(script);
  90. e->next = mapent->epairs;
  91. mapent->epairs = e;
  92. } while (1);
  93. return true;
  94. } //end of the function ParseEntity
  95. void PrintEntity (entity_t *ent)
  96. {
  97. epair_t *ep;
  98. printf ("------- entity %p -------\n", ent);
  99. for (ep=ent->epairs ; ep ; ep=ep->next)
  100. {
  101. printf ("%s = %s\n", ep->key, ep->value);
  102. }
  103. }
  104. void SetKeyValue (entity_t *ent, char *key, char *value)
  105. {
  106. epair_t *ep;
  107. for (ep=ent->epairs ; ep ; ep=ep->next)
  108. if (!strcmp (ep->key, key) )
  109. {
  110. FreeMemory(ep->value);
  111. ep->value = copystring(value);
  112. return;
  113. }
  114. ep = GetMemory(sizeof(*ep));
  115. ep->next = ent->epairs;
  116. ent->epairs = ep;
  117. ep->key = copystring(key);
  118. ep->value = copystring(value);
  119. }
  120. char *ValueForKey (entity_t *ent, char *key)
  121. {
  122. epair_t *ep;
  123. for (ep=ent->epairs ; ep ; ep=ep->next)
  124. if (!strcmp (ep->key, key) )
  125. return ep->value;
  126. return "";
  127. }
  128. vec_t FloatForKey (entity_t *ent, char *key)
  129. {
  130. char *k;
  131. k = ValueForKey (ent, key);
  132. return atof(k);
  133. }
  134. void GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
  135. {
  136. char *k;
  137. double v1, v2, v3;
  138. k = ValueForKey (ent, key);
  139. // scanf into doubles, then assign, so it is vec_t size independent
  140. v1 = v2 = v3 = 0;
  141. sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
  142. vec[0] = v1;
  143. vec[1] = v2;
  144. vec[2] = v3;
  145. }