forked from bnoordhuis/bspc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
l_utils.cpp
259 lines (244 loc) · 7.33 KB
/
l_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
//#ifndef BOTLIB
//#define BOTLIB
//#endif //BOTLIB
#ifdef BOTLIB
#include "q_shared.h"
#include "qfiles.h"
#include "botlib.h"
#include "l_log.h"
#include "l_libvar.h"
#include "l_memory.h"
//#include "l_utils.h"
#include "be_interface.h"
#else //BOTLIB
#include "qbsp.h"
#include "l_mem.h"
#endif //BOTLIB
#ifdef BOTLIB
//========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//========================================================================
void Vector2Angles(vec3_t value1, vec3_t angles)
{
float forward;
float yaw, pitch;
if (value1[1] == 0 && value1[0] == 0)
{
yaw = 0;
if (value1[2] > 0) pitch = 90;
else pitch = 270;
} //end if
else
{
yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
if (yaw < 0) yaw += 360;
forward = sqrt (value1[0]*value1[0] + value1[1]*value1[1]);
pitch = (int) (atan2(value1[2], forward) * 180 / M_PI);
if (pitch < 0) pitch += 360;
} //end else
angles[PITCH] = -pitch;
angles[YAW] = yaw;
angles[ROLL] = 0;
} //end of the function Vector2Angles
#endif //BOTLIB
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void ConvertPath(char *path)
{
while(*path)
{
if (*path == '/' || *path == '\\') *path = PATHSEPERATOR_CHAR;
path++;
} //end while
} //end of the function ConvertPath
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AppendPathSeperator(char *path, int length)
{
int pathlen = strlen(path);
if (strlen(path) && length-pathlen > 1 && path[pathlen-1] != '/' && path[pathlen-1] != '\\')
{
path[pathlen] = PATHSEPERATOR_CHAR;
path[pathlen+1] = '\0';
} //end if
} //end of the function AppenPathSeperator
#if 0
//===========================================================================
// returns pointer to file handle
// sets offset to and length of 'filename' in the pak file
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean FindFileInPak(char *pakfile, char *filename, foundfile_t *file)
{
FILE *fp;
dpackheader_t packheader;
dpackfile_t *packfiles;
int numdirs, i;
char path[MAX_PATH];
//open the pak file
fp = fopen(pakfile, "rb");
if (!fp)
{
return qfalse;
} //end if
//read pak header, check for valid pak id and seek to the dir entries
if ((fread(&packheader, 1, sizeof(dpackheader_t), fp) != sizeof(dpackheader_t))
|| (packheader.ident != IDPAKHEADER)
|| (fseek(fp, LittleLong(packheader.dirofs), SEEK_SET))
)
{
fclose(fp);
return qfalse;
} //end if
//number of dir entries in the pak file
numdirs = LittleLong(packheader.dirlen) / sizeof(dpackfile_t);
packfiles = (dpackfile_t *) GetMemory(numdirs * sizeof(dpackfile_t));
//read the dir entry
if (fread(packfiles, sizeof(dpackfile_t), numdirs, fp) != numdirs)
{
fclose(fp);
FreeMemory(packfiles);
return qfalse;
} //end if
fclose(fp);
//
strcpy(path, filename);
ConvertPath(path);
//find the dir entry in the pak file
for (i = 0; i < numdirs; i++)
{
//convert the dir entry name
ConvertPath(packfiles[i].name);
//compare the dir entry name with the filename
if (Q_strcasecmp(packfiles[i].name, path) == 0)
{
strcpy(file->filename, pakfile);
file->offset = LittleLong(packfiles[i].filepos);
file->length = LittleLong(packfiles[i].filelen);
FreeMemory(packfiles);
return qtrue;
} //end if
} //end for
FreeMemory(packfiles);
return qfalse;
} //end of the function FindFileInPak
//===========================================================================
// find a Quake2 file
// returns full path in 'filename'
// sets offset and length of the file
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean FindQuakeFile2(char *basedir, char *gamedir, char *filename, foundfile_t *file)
{
int dir, i;
//NOTE: 3 is necessary (LCC bug???)
char gamedirs[3][MAX_PATH] = {"","",""};
char filedir[MAX_PATH] = "";
//
if (gamedir) strncpy(gamedirs[0], gamedir, MAX_PATH);
strncpy(gamedirs[1], "baseq2", MAX_PATH);
//
//find the file in the two game directories
for (dir = 0; dir < 2; dir++)
{
//check if the file is in a directory
filedir[0] = 0;
if (basedir && strlen(basedir))
{
strncpy(filedir, basedir, MAX_PATH);
AppendPathSeperator(filedir, MAX_PATH);
} //end if
if (strlen(gamedirs[dir]))
{
strncat(filedir, gamedirs[dir], MAX_PATH - strlen(filedir));
AppendPathSeperator(filedir, MAX_PATH);
} //end if
strncat(filedir, filename, MAX_PATH - strlen(filedir));
ConvertPath(filedir);
Log_Write("accessing %s", filedir);
if (!access(filedir, 0x04))
{
strcpy(file->filename, filedir);
file->length = 0;
file->offset = 0;
return qtrue;
} //end if
//check if the file is in a pak?.pak
for (i = 0; i < 10; i++)
{
filedir[0] = 0;
if (basedir && strlen(basedir))
{
strncpy(filedir, basedir, MAX_PATH);
AppendPathSeperator(filedir, MAX_PATH);
} //end if
if (strlen(gamedirs[dir]))
{
strncat(filedir, gamedirs[dir], MAX_PATH - strlen(filedir));
AppendPathSeperator(filedir, MAX_PATH);
} //end if
sprintf(&filedir[strlen(filedir)], "pak%d.pak\0", i);
if (!access(filedir, 0x04))
{
Log_Write("searching %s in %s", filename, filedir);
if (FindFileInPak(filedir, filename, file)) return qtrue;
} //end if
} //end for
} //end for
file->offset = 0;
file->length = 0;
return qfalse;
} //end of the function FindQuakeFile2
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
#ifdef BOTLIB
qboolean FindQuakeFile(char *filename, foundfile_t *file)
{
return FindQuakeFile2(LibVarGetString("basedir"),
LibVarGetString("gamedir"), filename, file);
} //end of the function FindQuakeFile
#else //BOTLIB
qboolean FindQuakeFile(char *basedir, char *gamedir, char *filename, foundfile_t *file)
{
return FindQuakeFile2(basedir, gamedir, filename, file);
} //end of the function FindQuakeFile
#endif //BOTLIB
#endif