| 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" |
| 64 | #include "player.h" |
| 65 | #include "pilot.h" |
| 66 | #include "cfile.h" |
| 67 | #include "mono.h" |
| 68 | #include "ddio.h" |
| 69 | #include "manage.h" |
| 70 | #include "PilotPicsAPI.h" |
| 71 | #include "mem.h" |
| 72 | |
| 73 | #include <algorithm> |
| 74 | |
| 75 | #define PILOTPIC_DATABASE_HOG "PPics.Hog" |
| 76 | #define PILOTPIC_DATABASE_INDEX "PPics.idx" |
| 77 | |
| 78 | static CFILE *PilotPic_database_index_handle = NULL; |
| 79 | static int PilotPic_database_hog_handle = 0; |
| 80 | static int PilotPic_find_offset; |
| 81 | static char PilotPic_find_name[PILOT_STRING_SIZE]; |
| 82 | static bool 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_BuildDatabases(void); |
| 91 | // ----------------------------------------------- |
| 92 | // PPic_FreeDatabase |
| 93 | // Purpose: |
| 94 | // Frees up memory associated with the databases |
| 95 | // ----------------------------------------------- |
| 96 | static void PPic_FreeDatabase(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_JumpToPilot(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_GetIndexFromID(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_GetOffsetByID(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_InitDatabase(void) { |
| 128 | if (PilotPic_init) { |
| 129 | mprintf(0, "PPIC: InitDatabase already called\n"); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | // attempt to open the hog database |
| 134 | // -------------------------------- |
| 135 | char fullpath[_MAX_PATH]; |
| 136 | ddio_MakePath(fullpath, LocalD3Dir, PILOTPIC_DATABASE_HOG, NULL); |
| 137 | PilotPic_database_hog_handle = cf_OpenLibrary(fullpath); |
| 138 | |
| 139 | if (PilotPic_database_hog_handle == 0) { |
| 140 | // there was an error opening the hog database |
| 141 | // ----------------------------------------- |
| 142 | mprintf(0, "PPIC: Error opening %s database\n", PILOTPIC_DATABASE_HOG); |
| 143 | PilotPic_database_index_handle = NULL; |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // attempt to open the pilotpicture database index |
| 148 | // ----------------------------------------------- |
| 149 | PilotPic_database_index_handle = cfopen(PILOTPIC_DATABASE_INDEX, "rb"); |
| 150 | |
| 151 | if (PilotPic_database_index_handle == NULL) { |
| 152 | // there was an error opening the database index |
| 153 | // --------------------------------------------- |
| 154 | mprintf(0, "PPIC: Error opening database index '%s'\n", PILOTPIC_DATABASE_INDEX); |
| 155 | cf_CloseLibrary(PilotPic_database_hog_handle); |
| 156 | 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_BuildDatabases()) { |
| 164 | // there was an error building the databases |
| 165 | // ----------------------------------------- |
| 166 | mprintf(0, "PPIC: Error building databases\n"); |
| 167 | cfclose(PilotPic_database_index_handle); |
| 168 | PilotPic_database_index_handle = NULL; |
| 169 | cf_CloseLibrary(PilotPic_database_hog_handle); |
| 170 | PilotPic_database_hog_handle = 0; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | PilotPic_init = true; |
| 175 | |
| 176 | atexit(PPic_CloseDatabase); |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | // --------------------------------------------------------- |
| 182 | // PPic_CloseDatabase |
| 183 | // Purpose: |
| 184 | // Closes down the picture database. |
| 185 | // --------------------------------------------------------- |
| 186 | void PPic_CloseDatabase(void) { |
| 187 | if (!PilotPic_init) |
| 188 | return; |
| 189 | |
| 190 | // close up the database |
| 191 | PPic_FreeDatabase(); |
| 192 | |
| 193 | // close up files |
| 194 | if (PilotPic_database_index_handle) { |
| 195 | cfclose(PilotPic_database_index_handle); |
| 196 | PilotPic_database_index_handle = NULL; |
| 197 | } |
| 198 | |
| 199 | if (PilotPic_database_hog_handle) { |
| 200 | cf_CloseLibrary(PilotPic_database_hog_handle); |
| 201 | PilotPic_database_hog_handle = 0; |
| 202 | } |
| 203 | |
| 204 | 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_t PPic_QueryPilot(char *pilot_name) { |
| 214 | if (!PilotPic_init) |
| 215 | return 0; |
| 216 | |
| 217 | int letter_offset = PPic_JumpToPilot(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_SIZE]; |
| 224 | CFILE *file = 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_t name_size; |
| 230 | name_size = cf_ReadByte(file); |
| 231 | |
| 232 | if (cfeof(file)) { |
| 233 | done = true; |
| 234 | continue; |
| 235 | } |
| 236 | cf_ReadBytes((uint8_t *)name_buffer, name_size, file); |
| 237 | name_buffer[name_size] = '\0'; |
| 238 | |
| 239 | // next read in pilot_id |
| 240 | uint16_t pilot_id; |
| 241 | pilot_id = (uint16_t)cf_ReadShort(file); |
| 242 | |
| 243 | // next read past the bitmap name |
| 244 | uint8_t bmp_size; |
| 245 | bmp_size = cf_ReadByte(file); |
| 246 | cfseek(file, bmp_size, SEEK_CUR); |
| 247 | |
| 248 | if (stricmp(name_buffer, pilot_name)) { |
| 249 | // we're done |
| 250 | done = true; |
| 251 | } else |
| 252 | count++; |
| 253 | |
| 254 | if (cfeof(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_FindFirst(char *pilot_name, uint16_t *pilot_id) { |
| 270 | if (!PilotPic_init) |
| 271 | return false; |
| 272 | |
| 273 | *pilot_id = 65535; |
| 274 | |
| 275 | int letter_offset = PPic_JumpToPilot(pilot_name); |
| 276 | if (letter_offset == -1) |
| 277 | return false; |
| 278 | |
| 279 | strcpy(PilotPic_find_name, pilot_name); |
| 280 | |
| 281 | char name_buffer[PILOT_STRING_SIZE]; |
| 282 | CFILE *file = PilotPic_database_index_handle; |
| 283 | |
| 284 | // first read in the pilot name |
| 285 | uint8_t name_size; |
| 286 | name_size = cf_ReadByte(file); |
| 287 | if (cfeof(file)) { |
| 288 | return false; |
| 289 | } |
| 290 | cf_ReadBytes((uint8_t *)name_buffer, name_size, file); |
| 291 | name_buffer[name_size] = '\0'; |
| 292 | |
| 293 | // next read in pilot_id |
| 294 | uint16_t pid; |
| 295 | pid = (uint16_t)cf_ReadShort(file); |
| 296 | |
| 297 | // next read past the bitmap name |
| 298 | uint8_t bmp_size; |
| 299 | bmp_size = cf_ReadByte(file); |
| 300 | cfseek(file, bmp_size, SEEK_CUR); |
| 301 | |
| 302 | if (stricmp(name_buffer, pilot_name)) { |
| 303 | return false; // weird |
| 304 | } |
| 305 | |
| 306 | *pilot_id = pid; |
| 307 | PilotPic_find_offset = cftell(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_FindNext(uint16_t *pilot_id) { |
| 321 | if (!PilotPic_init) |
| 322 | return false; |
| 323 | |
| 324 | *pilot_id = 65535; |
| 325 | |
| 326 | char name_buffer[PILOT_STRING_SIZE]; |
| 327 | CFILE *file = PilotPic_database_index_handle; |
| 328 | |
| 329 | cfseek(file, PilotPic_find_offset, SEEK_SET); |
| 330 | if (cfeof(file)) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | // first read in the pilot name |
| 335 | uint8_t name_size; |
| 336 | name_size = cf_ReadByte(file); |
| 337 | if (cfeof(file)) { |
| 338 | return false; |
| 339 | } |
| 340 | cf_ReadBytes((uint8_t *)name_buffer, name_size, file); |
| 341 | name_buffer[name_size] = '\0'; |
| 342 | |
| 343 | // next read in pilot_id |
| 344 | uint16_t pid; |
| 345 | pid = (uint16_t)cf_ReadShort(file); |
| 346 | |
| 347 | // next read past the bitmap name |
| 348 | uint8_t bmp_size; |
| 349 | bmp_size = cf_ReadByte(file); |
| 350 | cfseek(file, bmp_size, SEEK_CUR); |
| 351 | |
| 352 | if (stricmp(name_buffer, PilotPic_find_name)) { |
| 353 | return false; // we're done |
| 354 | } |
| 355 | |
| 356 | *pilot_id = pid; |
| 357 | PilotPic_find_offset = cftell(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_FindClose(void) { |
| 368 | PilotPic_find_offset = 0; |
| 369 | 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_GetPilot(uint16_t pilot_id, char *pilot_name, int buffersize) { |
| 379 | if (!PilotPic_init) |
| 380 | return false; |
| 381 | |
| 382 | int oldoffset = cftell(PilotPic_database_index_handle); |
| 383 | |
| 384 | int offset = PPic_GetOffsetByID(pilot_id); |
| 385 | if (offset == -1) |
| 386 | return false; |
| 387 | |
| 388 | // jump to the offset |
| 389 | // ------------------ |
| 390 | cfseek(PilotPic_database_index_handle, offset, SEEK_SET); |
| 391 | if (cfeof(PilotPic_database_index_handle)) { |
| 392 | cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // read in the pilot name |
| 397 | // ---------------------- |
| 398 | uint8_t name_size; |
| 399 | name_size = cf_ReadByte(PilotPic_database_index_handle); |
| 400 | if (cfeof(PilotPic_database_index_handle)) { |
| 401 | cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET); |
| 402 | return false; |
| 403 | } |
| 404 | int toread = std::min<int>(name_size, buffersize - 1); |
| 405 | cf_ReadBytes((uint8_t *)pilot_name, toread, PilotPic_database_index_handle); |
| 406 | pilot_name[toread] = '\0'; |
| 407 | |
| 408 | cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET); |
| 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_GetBitmapHandle(uint16_t pilot_id) { |
| 421 | if (!PilotPic_init) |
| 422 | return -1; |
| 423 | |
| 424 | int oldoffset = cftell(PilotPic_database_index_handle); |
| 425 | |
| 426 | int offset = PPic_GetOffsetByID(pilot_id); |
| 427 | if (offset == -1) |
| 428 | return -1; |
| 429 | |
| 430 | // jump to the offset |
| 431 | // ------------------ |
| 432 | cfseek(PilotPic_database_index_handle, offset, SEEK_SET); |
| 433 | if (cfeof(PilotPic_database_index_handle)) { |
| 434 | cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET); |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | // read in the pilot name |
| 439 | // ---------------------- |
| 440 | uint8_t name_size; |
| 441 | char name_buffer[PILOT_STRING_SIZE]; |
| 442 | name_size = cf_ReadByte(PilotPic_database_index_handle); |
| 443 | if (cfeof(PilotPic_database_index_handle)) { |
| 444 | cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET); |
| 445 | return -1; |
| 446 | } |
| 447 | cf_ReadBytes((uint8_t *)name_buffer, name_size, PilotPic_database_index_handle); |
| 448 | name_buffer[name_size] = '\0'; |
| 449 | |
| 450 | // next read in pilot_id |
| 451 | uint16_t pid; |
| 452 | pid = (uint16_t)cf_ReadShort(PilotPic_database_index_handle); |
| 453 | |
| 454 | // next read past the bitmap name |
| 455 | uint8_t bmp_size; |
| 456 | char bitmap_name[_MAX_PATH]; |
| 457 | bmp_size = cf_ReadByte(PilotPic_database_index_handle); |
| 458 | cf_ReadBytes((uint8_t *)bitmap_name, bmp_size, PilotPic_database_index_handle); |
| 459 | bitmap_name[bmp_size] = '\0'; |
| 460 | |
| 461 | cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET); |
| 462 | |
| 463 | int bm_handle = bm_AllocLoadFileBitmap(IGNORE_TABLE(bitmap_name), 0); |
| 464 | if (bm_handle <= BAD_BITMAP_HANDLE) |
| 465 | return BAD_BITMAP_HANDLE; |
| 466 | |
| 467 | return bm_handle; |
| 468 | } |
| 469 | |
| 470 | // =============================================================================== |
| 471 | |
| 472 | // this maps a pilot_id to an index file offset |
| 473 | struct tPilotPicIdOffset { |
| 474 | int offset; |
| 475 | uint16_t id; |
| 476 | }; |
| 477 | tPilotPicIdOffset *Pilot_id_to_offset = NULL; |
| 478 | uint16_t *Sorted_Pilot_id_to_offset = NULL; |
| 479 | int 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_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_BuildDatabases(void) { |
| 491 | CFILE *file = 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_Rewind(file); |
| 498 | |
| 499 | PilotPic_count = cf_ReadInt(file); |
| 500 | if (PilotPic_count < 0) { |
| 501 | // hmm a negative! |
| 502 | mprintf(0, "PPIC: Invalid number of pilot pictures (%d)\n", PilotPic_count); |
| 503 | Int3(); |
| 504 | PilotPic_count = 0; |
| 505 | cf_Rewind(file); |
| 506 | return false; |
| 507 | } |
| 508 | if (PilotPic_count > 65535) { |
| 509 | // too many!!!! |
| 510 | mprintf(0, "PPIC: Invalid number of pilot pictures (%d)\n", PilotPic_count); |
| 511 | Int3(); |
| 512 | PilotPic_count = 0; |
| 513 | cf_Rewind(file); |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // allocate all the memory we're going to need |
| 518 | // ------------------------------------------- |
| 519 | Pilot_id_to_offset = (tPilotPicIdOffset *)mem_malloc(sizeof(tPilotPicIdOffset) * PilotPic_count); |
| 520 | Sorted_Pilot_id_to_offset = (uint16_t *)mem_malloc(sizeof(uint16_t) * PilotPic_count); |
| 521 | if (!Pilot_id_to_offset) { |
| 522 | // out of memory!!! |
| 523 | mprintf(0, "PPIC: Out of memory allocating index database\n"); |
| 524 | Int3(); |
| 525 | PilotPic_count = 0; |
| 526 | cf_Rewind(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_offset[count] = -1; |
| 537 | |
| 538 | for (count = 0; count < PilotPic_count; count++) { |
| 539 | |
| 540 | // get the current file position, we'll need it |
| 541 | int file_pos; |
| 542 | file_pos = cftell(file); |
| 543 | |
| 544 | // first read in the pilot name |
| 545 | uint8_t name_size; |
| 546 | name_size = cf_ReadByte(file); |
| 547 | cf_ReadBytes((uint8_t *)name_buffer, name_size, file); |
| 548 | name_buffer[name_size] = '\0'; |
| 549 | if (name_size >= PILOT_STRING_SIZE) { |
| 550 | mprintf(0, "PPIC: Too big: (%s)%d %d", name_buffer, count, name_size); |
| 551 | } |
| 552 | |
| 553 | // next read in pilot_id |
| 554 | uint16_t pilot_id; |
| 555 | pilot_id = (uint16_t)cf_ReadShort(file); |
| 556 | |
| 557 | // next read past the bitmap name |
| 558 | uint8_t bmp_size; |
| 559 | bmp_size = cf_ReadByte(file); |
| 560 | cfseek(file, bmp_size, SEEK_CUR); |
| 561 | |
| 562 | // add the info to the database |
| 563 | Pilot_id_to_offset[count].offset = file_pos; |
| 564 | Pilot_id_to_offset[count].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_offset[let_pos] == -1) { |
| 574 | 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_count; i++) { |
| 582 | Sorted_Pilot_id_to_offset[i] = i; |
| 583 | } |
| 584 | |
| 585 | for (i = 1; i <= PilotPic_count - 1; i++) { |
| 586 | t = 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_offset[Sorted_Pilot_id_to_offset[j]].id < Pilot_id_to_offset[t].id); j--) { |
| 590 | Sorted_Pilot_id_to_offset[j + 1] = Sorted_Pilot_id_to_offset[j]; |
| 591 | } |
| 592 | // insert |
| 593 | Sorted_Pilot_id_to_offset[j + 1] = t; |
| 594 | } |
| 595 | |
| 596 | cf_Rewind(file); |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | // ----------------------------------------------- |
| 601 | // PPic_FreeDatabase |
| 602 | // Purpose: |
| 603 | // Frees up memory associated with the databases |
| 604 | // ------------------------------------------------ |
| 605 | void PPic_FreeDatabase(void) { |
| 606 | |
| 607 | for (int i = 0; i < 27; i++) |
| 608 | Alphabet_to_offset[i] = -1; |
| 609 | |
| 610 | if (Pilot_id_to_offset) { |
| 611 | mem_free(Pilot_id_to_offset); |
| 612 | Pilot_id_to_offset = NULL; |
| 613 | } |
| 614 | if (Sorted_Pilot_id_to_offset) { |
| 615 | mem_free(Sorted_Pilot_id_to_offset); |
| 616 | Sorted_Pilot_id_to_offset = NULL; |
| 617 | } |
| 618 | |
| 619 | 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_JumpToPilot(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_offset[let_pos]; |
| 640 | if (lett_offset == -1) { |
| 641 | // no pilot names begin with this letter |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | CFILE *file = PilotPic_database_index_handle; |
| 646 | |
| 647 | // jump to that position of the file |
| 648 | cfseek(file, lett_offset, SEEK_SET); |
| 649 | |
| 650 | // now try to find the pilot |
| 651 | bool done = false; |
| 652 | int file_pos; |
| 653 | char name_buffer[PILOT_STRING_SIZE]; |
| 654 | |
| 655 | while (!done) { |
| 656 | |
| 657 | // save the file position |
| 658 | file_pos = cftell(file); |
| 659 | |
| 660 | // first read in the pilot name |
| 661 | uint8_t name_size; |
| 662 | name_size = cf_ReadByte(file); |
| 663 | if (cfeof(file)) { |
| 664 | done = true; |
| 665 | continue; |
| 666 | } |
| 667 | cf_ReadBytes((uint8_t *)name_buffer, name_size, file); |
| 668 | name_buffer[name_size] = '\0'; |
| 669 | |
| 670 | // next read in pilot_id |
| 671 | uint16_t pilot_id; |
| 672 | pilot_id = (uint16_t)cf_ReadShort(file); |
| 673 | |
| 674 | // next read past the bitmap name |
| 675 | uint8_t bmp_size; |
| 676 | bmp_size = cf_ReadByte(file); |
| 677 | cfseek(file, bmp_size, SEEK_CUR); |
| 678 | |
| 679 | int strcmpret = stricmp(name_buffer, pilot_name); |
| 680 | |
| 681 | switch (strcmpret) { |
| 682 | case 0: |
| 683 | // we found a match |
| 684 | cfseek(file, file_pos, SEEK_SET); |
| 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 (cfeof(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_GetOffsetByID(uint16_t pilot_id) { |
| 711 | int index = PPic_GetIndexFromID(pilot_id); |
| 712 | if (index == -1) |
| 713 | return -1; |
| 714 | return Pilot_id_to_offset[index].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_GetIndexFromID(int id) { |
| 724 | // do a binary search for the id,return -1 if not found |
| 725 | int min = 0, max = 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_offset[index_to_check]; |
| 732 | sorted_val = Pilot_id_to_offset[sort_index].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 | } |