Debian Codegraph @

Descent3/PilotPicsAPI.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/PilotPicsAPI.cpp $
21 * $Revision: 9 $
22 * $Date: 10/21/99 9:28p $
23 * $Author: Jeff $
24 *
25 * API implementation for Pilot Pictures
26 *
27 * $Log: /DescentIII/main/PilotPicsAPI.cpp $
28 *
29 * 9 10/21/99 9:28p Jeff
30 * B.A. Macintosh code merge
31 *
32 * 8 4/14/99 3:57a Jeff
33 * fixed case mismatch in #includes
34 *
35 * 7 3/30/99 4:26a Jeff
36 * reset variable when closing down system
37 *
38 * 6 3/29/99 7:19p Jeff
39 * increased buffer for importing pilot pics
40 *
41 * 5 2/05/99 7:04p Jeff
42 * table file parsing macros put in
43 *
44 * 4 1/21/99 11:15p Jeff
45 * pulled out some structs and defines from header files and moved them
46 * into seperate header files so that multiplayer dlls don't require major
47 * game headers, just those new headers. Side effect is a shorter build
48 * time. Also cleaned up some header file #includes that weren't needed.
49 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
50 * manage.h and multi.h
51 *
52 * 3 11/24/98 5:16p Jeff
53 * fixed bugs in the api during initial testing
54 *
55 * 2 11/23/98 6:30p Jeff
56 * initial creation
57 *
58 * $NoKeywords: $
59 */
60
61 #include <stdlib.h>
62 #include <stdio.h>
63 #include "bitmap.h"cxx . . $ `<file>/lib/bitmap.h`/
64 #include "player.h"cxx . . $ `<file>/Descent3/player.h`/
65 #include "pilot.h"cxx . . $ `<file>/Descent3/pilot.h`/
66 #include "cfile.h"cxx . . $ `<file>/cfile/cfile.h`/
67 #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/
68 #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/
69 #include "manage.h"cxx . . $ `<file>/lib/manage.h`/
70 #include "PilotPicsAPI.h"cxx . . $ `<file>/Descent3/PilotPicsAPI.h`/
71 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
72
73 #include <algorithm>
74
75 #define PILOTPIC_DATABASE_HOGcxx . . $ `Descent3/PilotPicsAPI.cpp:75:9`! "PPics.Hog"
76 #define PILOTPIC_DATABASE_INDEXcxx . . $ `Descent3/PilotPicsAPI.cpp:76:9`! "PPics.idx"
77
78 static CFILEcxx . . $ CFILE# *PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
79 static int PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle. = 0;
80 static int PilotPic_find_offsetcxx . . $ PilotPic_find_offset.;
81 static char PilotPic_find_namecxx . . $ PilotPic_find_name.[PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!];
82 static bool PilotPic_initcxx . . $ PilotPic_init. = false;
83
84 // -----------------------------------------------
85 // PPic_BuildDatabase
86 // Purpose:
87 // Builds up some databases for fast access
88 // based off the index file.
89 // -----------------------------------------------
90 static bool PPic_BuildDatabasescxx . . $ PPic_BuildDatabases(c9857a73d68e59ba).(void);
91 // -----------------------------------------------
92 // PPic_FreeDatabase
93 // Purpose:
94 // Frees up memory associated with the databases
95 // -----------------------------------------------
96 static void PPic_FreeDatabasecxx . . $ PPic_FreeDatabase(49f6e7a06ebc5aa8).(void);
97 // -------------------------------------------------
98 // PPic_JumpToPilot
99 // Purpose:
100 // Returns an offset into the file to where the
101 // the first pilot that matches pilot_name is. -1
102 // is returned if no match
103 // --------------------------------------------------
104 static int PPic_JumpToPilotcxx . . $ PPic_JumpToPilot(1ef8e6ec083aabb9).(char *pilot_name);
105 // -------------------------------------
106 // PPic_GetIndexFromID
107 // Purpose:
108 // Returns the file index into the Pilot_id_to_offset
109 // list for the given id, -1 if not found.
110 // -------------------------------------
111 static int PPic_GetIndexFromIDcxx . . $ PPic_GetIndexFromID(7864480464b09eea).(int id);
112 // -------------------------------------
113 // PPic_GetOffsetByID
114 // Purpose:
115 // Returns the file offset of the given pilot id. -1 if not found
116 // -------------------------------------
117 static int PPic_GetOffsetByIDcxx . . $ PPic_GetOffsetByID(377f6a3136964dac).(uint16_tcxx . . $ uint16_t# pilot_id);
118
119 //===========================================================================
120
121 // ---------------------------------------------------------
122 // PPic_InitDatabase
123 // Purpose:
124 // Initializes the database for the pictures. Call before
125 // any other Pilot Picture function
126 // ---------------------------------------------------------
127 bool PPic_InitDatabasecxx . . $ PPic_InitDatabase(c9857a73d68e59ba).(void) {
128 if (PilotPic_initcxx . . $ PilotPic_init.) {
129 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: InitDatabase already called\n");
130 return true;
131 }
132
133 // attempt to open the hog database
134 // --------------------------------
135 char fullpath[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
136 ddio_MakePathcxx . . $ ddio_MakePath(9ad4b0815420a85).(fullpath, LocalD3Dircxx . . $ LocalD3Dir., PILOTPIC_DATABASE_HOGcxx . . $ `Descent3/PilotPicsAPI.cpp:75:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
137 PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle. = cf_OpenLibrarycxx . . $ cf_OpenLibrary(293b0826c8d9cb54).(fullpathcxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d).);
138
139 if (PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle. == 0) {
140 // there was an error opening the hog database
141 // -----------------------------------------
142 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Error opening %s database\n", PILOTPIC_DATABASE_HOG);
143 PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
144 return false;
145 }
146
147 // attempt to open the pilotpicture database index
148 // -----------------------------------------------
149 PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle. = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(PILOTPIC_DATABASE_INDEXcxx . . $ `Descent3/PilotPicsAPI.cpp:76:9`!, "rb");
150
151 if (PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
152 // there was an error opening the database index
153 // ---------------------------------------------
154 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Error opening database index '%s'\n", PILOTPIC_DATABASE_INDEX);
155 cf_CloseLibrarycxx . . $ cf_CloseLibrary(d4f767463ce0a6b3).(PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle.);
156 PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle. = 0;
157 return false;
158 }
159
160 // when we get to this point, all of our files have been opened successfully, so to
161 // make searching, etc fast, we'll build some databases in memory, for quick lookup
162 // ---------------------------------------------------------------------------------
163 if (!PPic_BuildDatabasescxx . . $ PPic_BuildDatabases(c9857a73d68e59ba).()) {
164 // there was an error building the databases
165 // -----------------------------------------
166 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Error building databases\n");
167 cfclosecxx . . $ cfclose(3639e799710acb68).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
168 PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
169 cf_CloseLibrarycxx . . $ cf_CloseLibrary(d4f767463ce0a6b3).(PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle.);
170 PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle. = 0;
171 return false;
172 }
173
174 PilotPic_initcxx . . $ PilotPic_init. = true;
175
176 atexitcxx . . $ atexit(cc8c495369588262).(PPic_CloseDatabasecxx . . $ PPic_CloseDatabase(49f6e7a06ebc5aa8).);
177
178 return true;
179 }
180
181 // ---------------------------------------------------------
182 // PPic_CloseDatabase
183 // Purpose:
184 // Closes down the picture database.
185 // ---------------------------------------------------------
186 void PPic_CloseDatabasecxx . . $ PPic_CloseDatabase(49f6e7a06ebc5aa8).(void) {
187 if (!PilotPic_initcxx . . $ PilotPic_init.)
188 return;
189
190 // close up the database
191 PPic_FreeDatabasecxx . . $ PPic_FreeDatabase(49f6e7a06ebc5aa8).();
192
193 // close up files
194 if (PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.) {
195 cfclosecxx . . $ cfclose(3639e799710acb68).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
196 PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
197 }
198
199 if (PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle.) {
200 cf_CloseLibrarycxx . . $ cf_CloseLibrary(d4f767463ce0a6b3).(PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle.);
201 PilotPic_database_hog_handlecxx . . $ PilotPic_database_hog_handle. = 0;
202 }
203
204 PilotPic_initcxx . . $ PilotPic_init. = false;
205 }
206
207 // ---------------------------------------------------------
208 // PPic_QueryPilot
209 // Purpose:
210 // Given a pilot callsign it will search the database
211 // and return the number of pilots that match the name
212 // ---------------------------------------------------------
213 uint16_tcxx . . $ uint16_t# PPic_QueryPilotcxx . . $ PPic_QueryPilot(1c09e5b692edaacb).(char *pilot_name) {
214 if (!PilotPic_initcxx . . $ PilotPic_init.)
215 return 0;
216
217 int letter_offset = PPic_JumpToPilotcxx . . $ PPic_JumpToPilot(1ef8e6ec083aabb9).(pilot_name);
218 if (letter_offset == -1)
219 return 0;
220
221 int count = 0;
222 bool done = false;
223 char name_buffer[PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!];
224 CFILEcxx . . $ CFILE# *file = PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.;
225
226 // now read through the pilots until we don't get a match anymore
227 while (!done) {
228 // first read in the pilot name
229 uint8_tcxx . . $ uint8_t# name_size;
230 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
231
232 if (cfeofcxx . . $ cfeof(413716a422df0036).(file)) {
233 done = true;
234 continue;
235 }
236 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)name_buffer, name_size, file);
237 name_buffer[name_size] = '\0';
238
239 // next read in pilot_id
240 uint16_tcxx . . $ uint16_t# pilot_id;
241 pilot_id = (uint16_tcxx . . $ uint16_t#)cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(file);
242
243 // next read past the bitmap name
244 uint8_tcxx . . $ uint8_t# bmp_size;
245 bmp_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
246 cfseekcxx . . $ cfseek(f9992d249f032805).(file, bmp_size, SEEK_CURcxx . . $ `<external>/14710646456994434043/fcntl.h:138:10`!);
247
248 if (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(name_buffer, pilot_name)) {
249 // we're done
250 done = true;
251 } else
252 count++;
253
254 if (cfeofcxx . . $ cfeof(413716a422df0036).(file))
255 done = true;
256 }
257
258 return count;
259 }
260
261 // ---------------------------------------------------------
262 // PPic_FindFirst
263 // Purpose:
264 // Given a pilot callsign it will return the first pilot
265 // that matches the search. Returns true on success, false
266 // if it couldn't find any pilots matching. It returns
267 // the pilot id.
268 // ----------------------------------------------------------
269 bool PPic_FindFirstcxx . . $ PPic_FindFirst(b345e33955ddca6f).(char *pilot_name, uint16_tcxx . . $ uint16_t# *pilot_id) {
270 if (!PilotPic_initcxx . . $ PilotPic_init.)
271 return false;
272
273 *pilot_id = 65535;
274
275 int letter_offset = PPic_JumpToPilotcxx . . $ PPic_JumpToPilot(1ef8e6ec083aabb9).(pilot_name);
276 if (letter_offset == -1)
277 return false;
278
279 strcpy(PilotPic_find_namecxx . . $ PilotPic_find_name., pilot_name);
280
281 char name_buffer[PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!];
282 CFILEcxx . . $ CFILE# *file = PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.;
283
284 // first read in the pilot name
285 uint8_tcxx . . $ uint8_t# name_size;
286 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
287 if (cfeofcxx . . $ cfeof(413716a422df0036).(file)) {
288 return false;
289 }
290 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)name_buffer, name_size, file);
291 name_buffer[name_size] = '\0';
292
293 // next read in pilot_id
294 uint16_tcxx . . $ uint16_t# pid;
295 pid = (uint16_tcxx . . $ uint16_t#)cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(file);
296
297 // next read past the bitmap name
298 uint8_tcxx . . $ uint8_t# bmp_size;
299 bmp_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
300 cfseekcxx . . $ cfseek(f9992d249f032805).(file, bmp_size, SEEK_CURcxx . . $ `<external>/14710646456994434043/fcntl.h:138:10`!);
301
302 if (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(name_buffer, pilot_name)) {
303 return false; // weird
304 }
305
306 *pilot_id = pid;
307 PilotPic_find_offsetcxx . . $ PilotPic_find_offset. = cftellcxx . . $ cftell(65b457210a6528a1).(file);
308
309 return true;
310 }
311
312 // ----------------------------------------------------------
313 // PPic_FindNext
314 // Purpose:
315 // Call repeatedly until false is returned to get information
316 // about all the pilots that match pilot_name passed into
317 // an initial call to PPic_FindFirst(). It returns the
318 // pilot id.
319 // ----------------------------------------------------------
320 bool PPic_FindNextcxx . . $ PPic_FindNext(6bd48caf83be88b2).(uint16_tcxx . . $ uint16_t# *pilot_id) {
321 if (!PilotPic_initcxx . . $ PilotPic_init.)
322 return false;
323
324 *pilot_id = 65535;
325
326 char name_buffer[PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!];
327 CFILEcxx . . $ CFILE# *file = PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.;
328
329 cfseekcxx . . $ cfseek(f9992d249f032805).(file, PilotPic_find_offsetcxx . . $ PilotPic_find_offset., SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
330 if (cfeofcxx . . $ cfeof(413716a422df0036).(file)) {
331 return false;
332 }
333
334 // first read in the pilot name
335 uint8_tcxx . . $ uint8_t# name_size;
336 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
337 if (cfeofcxx . . $ cfeof(413716a422df0036).(file)) {
338 return false;
339 }
340 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)name_buffer, name_size, file);
341 name_buffer[name_size] = '\0';
342
343 // next read in pilot_id
344 uint16_tcxx . . $ uint16_t# pid;
345 pid = (uint16_tcxx . . $ uint16_t#)cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(file);
346
347 // next read past the bitmap name
348 uint8_tcxx . . $ uint8_t# bmp_size;
349 bmp_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
350 cfseekcxx . . $ cfseek(f9992d249f032805).(file, bmp_size, SEEK_CURcxx . . $ `<external>/14710646456994434043/fcntl.h:138:10`!);
351
352 if (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(name_buffer, PilotPic_find_name)) {
353 return false; // we're done
354 }
355
356 *pilot_id = pid;
357 PilotPic_find_offsetcxx . . $ PilotPic_find_offset. = cftellcxx . . $ cftell(65b457210a6528a1).(file);
358
359 return true;
360 }
361
362 // ----------------------------------------------------------
363 // PPic_FindClose
364 // Purpose:
365 // Stops a PPic_FindFirst/PPic_FindNext session
366 // ----------------------------------------------------------
367 void PPic_FindClosecxx . . $ PPic_FindClose(49f6e7a06ebc5aa8).(void) {
368 PilotPic_find_offsetcxx . . $ PilotPic_find_offset. = 0;
369 PilotPic_find_namecxx . . $ PilotPic_find_name.[0] = 0;
370 }
371
372 // ----------------------------------------------------------
373 // PPic_GetPilot
374 // Purpose:
375 // Given a pilot id, it will return the pilot name of
376 // the pilot name. Returns false if it's an invalid pilot id.
377 // ----------------------------------------------------------
378 bool PPic_GetPilotcxx . . $ PPic_GetPilot(c30de02759139238).(uint16_tcxx . . $ uint16_t# pilot_id, char *pilot_name, int buffersize) {
379 if (!PilotPic_initcxx . . $ PilotPic_init.)
380 return false;
381
382 int oldoffset = cftellcxx . . $ cftell(65b457210a6528a1).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
383
384 int offset = PPic_GetOffsetByIDcxx . . $ PPic_GetOffsetByID(377f6a3136964dac).(pilot_id);
385 if (offset == -1)
386 return false;
387
388 // jump to the offset
389 // ------------------
390 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., offset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
391 if (cfeofcxx . . $ cfeof(413716a422df0036).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.)) {
392 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., oldoffset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
393 return false;
394 }
395
396 // read in the pilot name
397 // ----------------------
398 uint8_tcxx . . $ uint8_t# name_size;
399 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
400 if (cfeofcxx . . $ cfeof(413716a422df0036).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.)) {
401 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., oldoffset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
402 return false;
403 }
404 int toread = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<int>(name_size, buffersize - 1);
405 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)pilot_name, toread, PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
406 pilot_name[toread] = '\0';
407
408 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., oldoffset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
409 return true;
410 }
411
412 // ----------------------------------------------------------
413 // PPic_GetBitmapHandle
414 // Purpose:
415 // Given a pilot id, it will return a handle to the
416 // bitmap for the pilot. MAKE SURE YOU FREE THE BITMAP WITH
417 // bm_FreeBitmap(). Returns -1 if it was an illegal pilot id.
418 // Returns BAD_BITMAP_HANDLE if it couldn't open the bitmap.
419 // ----------------------------------------------------------
420 int PPic_GetBitmapHandlecxx . . $ PPic_GetBitmapHandle(377f6a3136964dac).(uint16_tcxx . . $ uint16_t# pilot_id) {
421 if (!PilotPic_initcxx . . $ PilotPic_init.)
422 return -1;
423
424 int oldoffset = cftellcxx . . $ cftell(65b457210a6528a1).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
425
426 int offset = PPic_GetOffsetByIDcxx . . $ PPic_GetOffsetByID(377f6a3136964dac).(pilot_id);
427 if (offset == -1)
428 return -1;
429
430 // jump to the offset
431 // ------------------
432 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., offset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
433 if (cfeofcxx . . $ cfeof(413716a422df0036).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.)) {
434 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., oldoffset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
435 return -1;
436 }
437
438 // read in the pilot name
439 // ----------------------
440 uint8_tcxx . . $ uint8_t# name_size;
441 char name_buffer[PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!];
442 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
443 if (cfeofcxx . . $ cfeof(413716a422df0036).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.)) {
444 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., oldoffset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
445 return -1;
446 }
447 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)name_buffer, name_size, PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
448 name_buffer[name_size] = '\0';
449
450 // next read in pilot_id
451 uint16_tcxx . . $ uint16_t# pid;
452 pid = (uint16_tcxx . . $ uint16_t#)cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
453
454 // next read past the bitmap name
455 uint8_tcxx . . $ uint8_t# bmp_size;
456 char bitmap_name[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
457 bmp_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
458 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)bitmap_name, bmp_size, PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.);
459 bitmap_name[bmp_size] = '\0';
460
461 cfseekcxx . . $ cfseek(f9992d249f032805).(PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle., oldoffset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
462
463 int bm_handle = bm_AllocLoadFileBitmapcxx . . $ bm_AllocLoadFileBitmap(1163be050dbfff7).(IGNORE_TABLEcxx . . $ `lib/manage_external.h:54:9`!(bitmap_name), 0);
464 if (bm_handle <= BAD_BITMAP_HANDLEcxx . . $ `lib/bitmap.h:42:9`!)
465 return BAD_BITMAP_HANDLEcxx . . $ `lib/bitmap.h:42:9`!;
466
467 return bm_handle;
468 }
469
470 // ===============================================================================
471
472 // this maps a pilot_id to an index file offset
473 struct tPilotPicIdOffsetcxx . . $ tPilotPicIdOffset# {
474 int offsetcxx . . $ tPilotPicIdOffset#offset.;
475 uint16_tcxx . . $ uint16_t# idcxx . . $ tPilotPicIdOffset#id.;
476 };
477 tPilotPicIdOffsetcxx . . $ tPilotPicIdOffset# *Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
478 uint16_tcxx . . $ uint16_t# *Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
479 int PilotPic_countcxx . . $ PilotPic_count. = 0;
480
481 // this maps the alphabet to an index file offset (first pilot_name that begins with the letter)
482 int Alphabet_to_offsetcxx . . $ Alphabet_to_offset.[27];
483
484 // -----------------------------------------------
485 // PPic_BuildDatabase
486 // Purpose:
487 // Builds up some databases for fast access
488 // based off the index file.
489 // -----------------------------------------------
490 bool PPic_BuildDatabasescxx . . $ PPic_BuildDatabases(c9857a73d68e59ba).(void) {
491 CFILEcxx . . $ CFILE# *file = PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.;
492 if (!file)
493 return false;
494
495 // first rewind to the beginning and find out how many pilots we have
496 // -------------------------------------------------------------------
497 cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(file);
498
499 PilotPic_countcxx . . $ PilotPic_count. = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(file);
500 if (PilotPic_countcxx . . $ PilotPic_count. < 0) {
501 // hmm a negative!
502 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Invalid number of pilot pictures (%d)\n", PilotPic_count);
503 Int3cxx . . $ `misc/pserror.h:247:9`!();
504 PilotPic_countcxx . . $ PilotPic_count. = 0;
505 cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(file);
506 return false;
507 }
508 if (PilotPic_countcxx . . $ PilotPic_count. > 65535) {
509 // too many!!!!
510 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Invalid number of pilot pictures (%d)\n", PilotPic_count);
511 Int3cxx . . $ `misc/pserror.h:247:9`!();
512 PilotPic_countcxx . . $ PilotPic_count. = 0;
513 cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(file);
514 return false;
515 }
516
517 // allocate all the memory we're going to need
518 // -------------------------------------------
519 Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset. = (tPilotPicIdOffsetcxx . . $ tPilotPicIdOffset# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(tPilotPicIdOffset) * PilotPic_count);
520 Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset. = (uint16_tcxx . . $ uint16_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(uint16_t) * PilotPic_count);
521 if (!Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.) {
522 // out of memory!!!
523 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Out of memory allocating index database\n");
524 Int3cxx . . $ `misc/pserror.h:247:9`!();
525 PilotPic_countcxx . . $ PilotPic_count. = 0;
526 cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(file);
527 return false;
528 }
529
530 // now it is time to go through the index file and build the databases
531 // -------------------------------------------------------------------
532 int count;
533 char name_buffer[256];
534
535 for (count = 0; count < 27; count++)
536 Alphabet_to_offsetcxx . . $ Alphabet_to_offset.[count] = -1;
537
538 for (count = 0; count < PilotPic_countcxx . . $ PilotPic_count.; count++) {
539
540 // get the current file position, we'll need it
541 int file_pos;
542 file_pos = cftellcxx . . $ cftell(65b457210a6528a1).(file);
543
544 // first read in the pilot name
545 uint8_tcxx . . $ uint8_t# name_size;
546 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
547 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)name_buffer, name_size, file);
548 name_buffer[name_size] = '\0';
549 if (name_size >= PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!) {
550 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PPIC: Too big: (%s)%d %d", name_buffer, count, name_size);
551 }
552
553 // next read in pilot_id
554 uint16_tcxx . . $ uint16_t# pilot_id;
555 pilot_id = (uint16_tcxx . . $ uint16_t#)cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(file);
556
557 // next read past the bitmap name
558 uint8_tcxx . . $ uint8_t# bmp_size;
559 bmp_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
560 cfseekcxx . . $ cfseek(f9992d249f032805).(file, bmp_size, SEEK_CURcxx . . $ `<external>/14710646456994434043/fcntl.h:138:10`!);
561
562 // add the info to the database
563 Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.[count].offsetcxx . . $ tPilotPicIdOffset#offset. = file_pos;
564 Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.[count].idcxx . . $ tPilotPicIdOffset#id. = pilot_id;
565
566 char let_pos;
567 if (isalpha(name_buffer[0])) {
568 let_pos = toupper(name_buffer[0]) - 'A';
569 } else {
570 let_pos = 26; // not alpha letter
571 }
572
573 if (Alphabet_to_offsetcxx . . $ Alphabet_to_offset.[let_pos] == -1) {
574 Alphabet_to_offsetcxx . . $ Alphabet_to_offset.[let_pos] = file_pos;
575 }
576 }
577
578 // now sort the pilot ids
579 int i, t, j;
580
581 for (i = 0; i < PilotPic_countcxx . . $ PilotPic_count.; i++) {
582 Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[i] = i;
583 }
584
585 for (i = 1; i <= PilotPic_countcxx . . $ PilotPic_count. - 1; i++) {
586 t = Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[i];
587 // Shift elements down until
588 // insertion point found.
589 for (j = i - 1; j >= 0 && (Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.[Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[j]].idcxx . . $ tPilotPicIdOffset#id. < Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.[t].idcxx . . $ tPilotPicIdOffset#id.); j--) {
590 Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[j + 1] = Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[j];
591 }
592 // insert
593 Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[j + 1] = t;
594 }
595
596 cf_Rewindcxx . . $ cf_Rewind(3639e799710acb68).(file);
597 return true;
598 }
599
600 // -----------------------------------------------
601 // PPic_FreeDatabase
602 // Purpose:
603 // Frees up memory associated with the databases
604 // ------------------------------------------------
605 void PPic_FreeDatabasecxx . . $ PPic_FreeDatabase(49f6e7a06ebc5aa8).(void) {
606
607 for (int i = 0; i < 27; i++)
608 Alphabet_to_offsetcxx . . $ Alphabet_to_offset.[i] = -1;
609
610 if (Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.) {
611 mem_freecxx . . $ `mem/mem.h:80:9`!(Pilot_id_to_offset);
612 Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
613 }
614 if (Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.) {
615 mem_freecxx . . $ `mem/mem.h:80:9`!(Sorted_Pilot_id_to_offset);
616 Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
617 }
618
619 PilotPic_countcxx . . $ PilotPic_count. = 0;
620 }
621
622 // -------------------------------------------------
623 // PPic_JumpToPilot
624 // Purpose:
625 // Returns an offset into the file to where the
626 // the first pilot that matches pilot_name is. -1
627 // is returned if no match
628 // --------------------------------------------------
629 int PPic_JumpToPilotcxx . . $ PPic_JumpToPilot(1ef8e6ec083aabb9).(char *pilot_name) {
630 int lett_offset;
631 int let_pos;
632
633 if (isalpha(pilot_name[0])) {
634 let_pos = toupper(pilot_name[0]) - 'A';
635 } else {
636 let_pos = 26;
637 }
638
639 lett_offset = Alphabet_to_offsetcxx . . $ Alphabet_to_offset.[let_pos];
640 if (lett_offset == -1) {
641 // no pilot names begin with this letter
642 return -1;
643 }
644
645 CFILEcxx . . $ CFILE# *file = PilotPic_database_index_handlecxx . . $ PilotPic_database_index_handle.;
646
647 // jump to that position of the file
648 cfseekcxx . . $ cfseek(f9992d249f032805).(file, lett_offset, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
649
650 // now try to find the pilot
651 bool done = false;
652 int file_pos;
653 char name_buffer[PILOT_STRING_SIZEcxx . . $ `Descent3/pilot_class.h:124:9`!];
654
655 while (!done) {
656
657 // save the file position
658 file_pos = cftellcxx . . $ cftell(65b457210a6528a1).(file);
659
660 // first read in the pilot name
661 uint8_tcxx . . $ uint8_t# name_size;
662 name_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
663 if (cfeofcxx . . $ cfeof(413716a422df0036).(file)) {
664 done = true;
665 continue;
666 }
667 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)name_buffer, name_size, file);
668 name_buffer[name_size] = '\0';
669
670 // next read in pilot_id
671 uint16_tcxx . . $ uint16_t# pilot_id;
672 pilot_id = (uint16_tcxx . . $ uint16_t#)cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(file);
673
674 // next read past the bitmap name
675 uint8_tcxx . . $ uint8_t# bmp_size;
676 bmp_size = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(file);
677 cfseekcxx . . $ cfseek(f9992d249f032805).(file, bmp_size, SEEK_CURcxx . . $ `<external>/14710646456994434043/fcntl.h:138:10`!);
678
679 int strcmpret = stricmpcxx . . $ `linux/linux_fix.h:43:9`!(name_buffer, pilot_name);
680
681 switch (strcmpret) {
682 case 0:
683 // we found a match
684 cfseekcxx . . $ cfseek(f9992d249f032805).(file, file_pos, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
685 return file_pos;
686 default:
687 if ((let_pos == 25) && !isalpha(name_buffer[0])) {
688 // we're searching the z's and moved to let_pos 26
689 return -1;
690 }
691
692 if (strcmpret > 0) {
693 // we can stop reading/searching
694 return -1;
695 }
696 }
697
698 if (cfeofcxx . . $ cfeof(413716a422df0036).(file))
699 done = true;
700 }
701
702 return -1;
703 }
704
705 // -------------------------------------
706 // PPic_GetOffsetByID
707 // Purpose:
708 // Returns the file offset of the given pilot id. -1 if not found
709 // -------------------------------------
710 int PPic_GetOffsetByIDcxx . . $ PPic_GetOffsetByID(377f6a3136964dac).(uint16_tcxx . . $ uint16_t# pilot_id) {
711 int index = PPic_GetIndexFromIDcxx . . $ PPic_GetIndexFromID(7864480464b09eea).(pilot_id);
712 if (index == -1)
713 return -1;
714 return Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.[index].offsetcxx . . $ tPilotPicIdOffset#offset.;
715 }
716
717 // -------------------------------------
718 // PPic_GetIndexFromID
719 // Purpose:
720 // Returns the file index into the Pilot_id_to_offset
721 // list for the given id, -1 if not found.
722 // -------------------------------------
723 int PPic_GetIndexFromIDcxx . . $ PPic_GetIndexFromID(7864480464b09eea).(int id) {
724 // do a binary search for the id,return -1 if not found
725 int min = 0, max = PilotPic_countcxx . . $ PilotPic_count. - 1;
726 int index_to_check;
727 int sort_index, sorted_val;
728
729 while (1) {
730 index_to_check = (min + max) / 2;
731 sort_index = Sorted_Pilot_id_to_offsetcxx . . $ Sorted_Pilot_id_to_offset.[index_to_check];
732 sorted_val = Pilot_id_to_offsetcxx . . $ Pilot_id_to_offset.[sort_index].idcxx . . $ tPilotPicIdOffset#id.;
733
734 if (sorted_val == id) {
735 // found it!
736 return sort_index;
737 }
738
739 if (min >= max) // exhausted search
740 break;
741
742 if (sorted_val > id) // search key after check key
743 min = index_to_check + 1;
744 else // search key before check key
745 max = index_to_check - 1;
746 }
747 return -1;
748 }