l_utils.c (7497B)
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 //#ifndef BOTLIB 24 //#define BOTLIB 25 //#endif //BOTLIB 26 27 #ifdef BOTLIB 28 #include "q_shared.h" 29 #include "qfiles.h" 30 #include "botlib.h" 31 #include "l_log.h" 32 #include "l_libvar.h" 33 #include "l_memory.h" 34 //#include "l_utils.h" 35 #include "be_interface.h" 36 #else //BOTLIB 37 #include "qbsp.h" 38 #include "l_mem.h" 39 #endif //BOTLIB 40 41 #ifdef BOTLIB 42 //======================================================================== 43 // 44 // Parameter: - 45 // Returns: - 46 // Changes Globals: - 47 //======================================================================== 48 void Vector2Angles(vec3_t value1, vec3_t angles) 49 { 50 float forward; 51 float yaw, pitch; 52 53 if (value1[1] == 0 && value1[0] == 0) 54 { 55 yaw = 0; 56 if (value1[2] > 0) pitch = 90; 57 else pitch = 270; 58 } //end if 59 else 60 { 61 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI); 62 if (yaw < 0) yaw += 360; 63 64 forward = sqrt (value1[0]*value1[0] + value1[1]*value1[1]); 65 pitch = (int) (atan2(value1[2], forward) * 180 / M_PI); 66 if (pitch < 0) pitch += 360; 67 } //end else 68 69 angles[PITCH] = -pitch; 70 angles[YAW] = yaw; 71 angles[ROLL] = 0; 72 } //end of the function Vector2Angles 73 #endif //BOTLIB 74 //=========================================================================== 75 // 76 // Parameter: - 77 // Returns: - 78 // Changes Globals: - 79 //=========================================================================== 80 void ConvertPath(char *path) 81 { 82 while(*path) 83 { 84 if (*path == '/' || *path == '\\') *path = PATHSEPERATOR_CHAR; 85 path++; 86 } //end while 87 } //end of the function ConvertPath 88 //=========================================================================== 89 // 90 // Parameter: - 91 // Returns: - 92 // Changes Globals: - 93 //=========================================================================== 94 void AppendPathSeperator(char *path, int length) 95 { 96 int pathlen = strlen(path); 97 98 if (strlen(path) && length-pathlen > 1 && path[pathlen-1] != '/' && path[pathlen-1] != '\\') 99 { 100 path[pathlen] = PATHSEPERATOR_CHAR; 101 path[pathlen+1] = '\0'; 102 } //end if 103 } //end of the function AppenPathSeperator 104 105 #if 0 106 //=========================================================================== 107 // returns pointer to file handle 108 // sets offset to and length of 'filename' in the pak file 109 // 110 // Parameter: - 111 // Returns: - 112 // Changes Globals: - 113 //=========================================================================== 114 qboolean FindFileInPak(char *pakfile, char *filename, foundfile_t *file) 115 { 116 FILE *fp; 117 dpackheader_t packheader; 118 dpackfile_t *packfiles; 119 int numdirs, i; 120 char path[MAX_PATH]; 121 122 //open the pak file 123 fp = fopen(pakfile, "rb"); 124 if (!fp) 125 { 126 return false; 127 } //end if 128 //read pak header, check for valid pak id and seek to the dir entries 129 if ((fread(&packheader, 1, sizeof(dpackheader_t), fp) != sizeof(dpackheader_t)) 130 || (packheader.ident != IDPAKHEADER) 131 || (fseek(fp, LittleLong(packheader.dirofs), SEEK_SET)) 132 ) 133 { 134 fclose(fp); 135 return false; 136 } //end if 137 //number of dir entries in the pak file 138 numdirs = LittleLong(packheader.dirlen) / sizeof(dpackfile_t); 139 packfiles = (dpackfile_t *) GetMemory(numdirs * sizeof(dpackfile_t)); 140 //read the dir entry 141 if (fread(packfiles, sizeof(dpackfile_t), numdirs, fp) != numdirs) 142 { 143 fclose(fp); 144 FreeMemory(packfiles); 145 return false; 146 } //end if 147 fclose(fp); 148 // 149 strcpy(path, filename); 150 ConvertPath(path); 151 //find the dir entry in the pak file 152 for (i = 0; i < numdirs; i++) 153 { 154 //convert the dir entry name 155 ConvertPath(packfiles[i].name); 156 //compare the dir entry name with the filename 157 if (Q_strcasecmp(packfiles[i].name, path) == 0) 158 { 159 strcpy(file->filename, pakfile); 160 file->offset = LittleLong(packfiles[i].filepos); 161 file->length = LittleLong(packfiles[i].filelen); 162 FreeMemory(packfiles); 163 return true; 164 } //end if 165 } //end for 166 FreeMemory(packfiles); 167 return false; 168 } //end of the function FindFileInPak 169 //=========================================================================== 170 // find a Quake2 file 171 // returns full path in 'filename' 172 // sets offset and length of the file 173 // 174 // Parameter: - 175 // Returns: - 176 // Changes Globals: - 177 //=========================================================================== 178 qboolean FindQuakeFile2(char *basedir, char *gamedir, char *filename, foundfile_t *file) 179 { 180 int dir, i; 181 //NOTE: 3 is necessary (LCC bug???) 182 char gamedirs[3][MAX_PATH] = {"","",""}; 183 char filedir[MAX_PATH] = ""; 184 185 // 186 if (gamedir) strncpy(gamedirs[0], gamedir, MAX_PATH); 187 strncpy(gamedirs[1], "baseq2", MAX_PATH); 188 // 189 //find the file in the two game directories 190 for (dir = 0; dir < 2; dir++) 191 { 192 //check if the file is in a directory 193 filedir[0] = 0; 194 if (basedir && strlen(basedir)) 195 { 196 strncpy(filedir, basedir, MAX_PATH); 197 AppendPathSeperator(filedir, MAX_PATH); 198 } //end if 199 if (strlen(gamedirs[dir])) 200 { 201 strncat(filedir, gamedirs[dir], MAX_PATH - strlen(filedir)); 202 AppendPathSeperator(filedir, MAX_PATH); 203 } //end if 204 strncat(filedir, filename, MAX_PATH - strlen(filedir)); 205 ConvertPath(filedir); 206 Log_Write("accessing %s", filedir); 207 if (!access(filedir, 0x04)) 208 { 209 strcpy(file->filename, filedir); 210 file->length = 0; 211 file->offset = 0; 212 return true; 213 } //end if 214 //check if the file is in a pak?.pak 215 for (i = 0; i < 10; i++) 216 { 217 filedir[0] = 0; 218 if (basedir && strlen(basedir)) 219 { 220 strncpy(filedir, basedir, MAX_PATH); 221 AppendPathSeperator(filedir, MAX_PATH); 222 } //end if 223 if (strlen(gamedirs[dir])) 224 { 225 strncat(filedir, gamedirs[dir], MAX_PATH - strlen(filedir)); 226 AppendPathSeperator(filedir, MAX_PATH); 227 } //end if 228 sprintf(&filedir[strlen(filedir)], "pak%d.pak\0", i); 229 if (!access(filedir, 0x04)) 230 { 231 Log_Write("searching %s in %s", filename, filedir); 232 if (FindFileInPak(filedir, filename, file)) return true; 233 } //end if 234 } //end for 235 } //end for 236 file->offset = 0; 237 file->length = 0; 238 return false; 239 } //end of the function FindQuakeFile2 240 //=========================================================================== 241 // 242 // Parameter: - 243 // Returns: - 244 // Changes Globals: - 245 //=========================================================================== 246 #ifdef BOTLIB 247 qboolean FindQuakeFile(char *filename, foundfile_t *file) 248 { 249 return FindQuakeFile2(LibVarGetString("basedir"), 250 LibVarGetString("gamedir"), filename, file); 251 } //end of the function FindQuakeFile 252 #else //BOTLIB 253 qboolean FindQuakeFile(char *basedir, char *gamedir, char *filename, foundfile_t *file) 254 { 255 return FindQuakeFile2(basedir, gamedir, filename, file); 256 } //end of the function FindQuakeFile 257 #endif //BOTLIB 258 259 #endif