l_bsp_ent.c (3887B)
1 /* 2 =========================================================================== 3 Copyright (C) 1999-2005 Id Software, Inc. 4 5 This file is part of Quake III Arena source code. 6 7 Quake III Arena source code is free software; you can redistribute it 8 and/or modify it under the terms of the GNU General Public License as 9 published by the Free Software Foundation; either version 2 of the License, 10 or (at your option) any later version. 11 12 Quake III Arena source code is distributed in the hope that it will be 13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with Foobar; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 =========================================================================== 21 */ 22 23 #include "l_cmd.h" 24 #include "l_math.h" 25 #include "l_mem.h" 26 #include "l_log.h" 27 #include "../botlib/l_script.h" 28 #include "l_bsp_ent.h" 29 30 #define MAX_KEY 32 31 #define MAX_VALUE 1024 32 33 int num_entities; 34 entity_t entities[MAX_MAP_ENTITIES]; 35 36 void StripTrailing(char *e) 37 { 38 char *s; 39 40 s = e + strlen(e)-1; 41 while (s >= e && *s <= 32) 42 { 43 *s = 0; 44 s--; 45 } 46 } 47 48 /* 49 ================= 50 ParseEpair 51 ================= 52 */ 53 epair_t *ParseEpair(script_t *script) 54 { 55 epair_t *e; 56 token_t token; 57 58 e = GetMemory(sizeof(epair_t)); 59 memset (e, 0, sizeof(epair_t)); 60 61 PS_ExpectAnyToken(script, &token); 62 StripDoubleQuotes(token.string); 63 if (strlen(token.string) >= MAX_KEY-1) 64 Error ("ParseEpair: token %s too long", token.string); 65 e->key = copystring(token.string); 66 PS_ExpectAnyToken(script, &token); 67 StripDoubleQuotes(token.string); 68 if (strlen(token.string) >= MAX_VALUE-1) 69 Error ("ParseEpair: token %s too long", token.string); 70 e->value = copystring(token.string); 71 72 // strip trailing spaces 73 StripTrailing(e->key); 74 StripTrailing(e->value); 75 76 return e; 77 } //end of the function ParseEpair 78 79 80 /* 81 ================ 82 ParseEntity 83 ================ 84 */ 85 qboolean ParseEntity(script_t *script) 86 { 87 epair_t *e; 88 entity_t *mapent; 89 token_t token; 90 91 if (!PS_ReadToken(script, &token)) 92 return false; 93 94 if (strcmp(token.string, "{")) 95 Error ("ParseEntity: { not found"); 96 97 if (num_entities == MAX_MAP_ENTITIES) 98 Error ("num_entities == MAX_MAP_ENTITIES"); 99 100 mapent = &entities[num_entities]; 101 num_entities++; 102 103 do 104 { 105 if (!PS_ReadToken(script, &token)) 106 Error ("ParseEntity: EOF without closing brace"); 107 if (!strcmp(token.string, "}") ) 108 break; 109 PS_UnreadLastToken(script); 110 e = ParseEpair(script); 111 e->next = mapent->epairs; 112 mapent->epairs = e; 113 } while (1); 114 115 return true; 116 } //end of the function ParseEntity 117 118 void PrintEntity (entity_t *ent) 119 { 120 epair_t *ep; 121 122 printf ("------- entity %p -------\n", ent); 123 for (ep=ent->epairs ; ep ; ep=ep->next) 124 { 125 printf ("%s = %s\n", ep->key, ep->value); 126 } 127 128 } 129 130 void SetKeyValue (entity_t *ent, char *key, char *value) 131 { 132 epair_t *ep; 133 134 for (ep=ent->epairs ; ep ; ep=ep->next) 135 if (!strcmp (ep->key, key) ) 136 { 137 FreeMemory(ep->value); 138 ep->value = copystring(value); 139 return; 140 } 141 ep = GetMemory(sizeof(*ep)); 142 ep->next = ent->epairs; 143 ent->epairs = ep; 144 ep->key = copystring(key); 145 ep->value = copystring(value); 146 } 147 148 char *ValueForKey (entity_t *ent, char *key) 149 { 150 epair_t *ep; 151 152 for (ep=ent->epairs ; ep ; ep=ep->next) 153 if (!strcmp (ep->key, key) ) 154 return ep->value; 155 return ""; 156 } 157 158 vec_t FloatForKey (entity_t *ent, char *key) 159 { 160 char *k; 161 162 k = ValueForKey (ent, key); 163 return atof(k); 164 } 165 166 void GetVectorForKey (entity_t *ent, char *key, vec3_t vec) 167 { 168 char *k; 169 double v1, v2, v3; 170 171 k = ValueForKey (ent, key); 172 // scanf into doubles, then assign, so it is vec_t size independent 173 v1 = v2 = v3 = 0; 174 sscanf (k, "%lf %lf %lf", &v1, &v2, &v3); 175 vec[0] = v1; 176 vec[1] = v2; 177 vec[2] = v3; 178 } 179 180