| 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 | |
| 19 | #include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/ |
| 20 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <string.h> |
| 26 | #include "game.h"cxx . . $ `<file>/Descent3/game.h`/ |
| 27 | #include "gamefile.h"cxx . . $ `<file>/Descent3/gamefile.h`/ |
| 28 | |
| 29 | gamefilecxx . . $ gamefile# Gamefilescxx . . $ Gamefiles.[MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!]; |
| 30 | int Num_gamefilescxx . . $ Num_gamefiles. = 0; |
| 31 | |
| 32 | // Sets all gamefiles to unused |
| 33 | void InitGamefilescxx . . $ InitGamefiles(49f6e7a06ebc5aa8).() { |
| 34 | for (int i = 0; i < MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!; i++) { |
| 35 | Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used. = 0; |
| 36 | Gamefilescxx . . $ Gamefiles.[i].namecxx . . $ gamefile#name.[0] = 0; |
| 37 | } |
| 38 | Num_gamefilescxx . . $ Num_gamefiles. = 0; |
| 39 | } |
| 40 | |
| 41 | // Allocs a gamefile for use, returns -1 if error, else index on success |
| 42 | int AllocGamefilecxx . . $ AllocGamefile(b126dc7c1de90089).() { |
| 43 | for (int i = 0; i < MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!; i++) { |
| 44 | if (Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used. == 0) { |
| 45 | memset(&Gamefilescxx . . $ Gamefiles.[i], 0, sizeof(gamefilecxx . . $ gamefile#)); |
| 46 | Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used. = 1; |
| 47 | Num_gamefilescxx . . $ Num_gamefiles.++; |
| 48 | return i; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Int3cxx . . $ `misc/pserror.h:247:9`!(); // No gamefiles free! |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | // Frees gamefile index n and all associated images |
| 57 | void FreeGamefilecxx . . $ FreeGamefile(d4f767463ce0a6b3).(int n) { |
| 58 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(Gamefiles[n].used > 0); |
| 59 | |
| 60 | Gamefilescxx . . $ Gamefiles.[n].usedcxx . . $ gamefile#used. = 0; |
| 61 | Gamefilescxx . . $ Gamefiles.[n].namecxx . . $ gamefile#name.[0] = 0; |
| 62 | Num_gamefilescxx . . $ Num_gamefiles.--; |
| 63 | } |
| 64 | |
| 65 | // Gets next gamefile from n that has actually been alloced |
| 66 | int GetNextGamefilecxx . . $ GetNextGamefile(7864480464b09eea).(int n) { |
| 67 | int i; |
| 68 | |
| 69 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(n >= 0 && n < MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!); |
| 70 | |
| 71 | if (Num_gamefilescxx . . $ Num_gamefiles. == 0) |
| 72 | return -1; |
| 73 | |
| 74 | for (i = n + 1; i < MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!; i++) |
| 75 | if (Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used.) |
| 76 | return i; |
| 77 | for (i = 0; i < n; i++) |
| 78 | if (Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used.) |
| 79 | return i; |
| 80 | |
| 81 | // this is the only one |
| 82 | |
| 83 | return n; |
| 84 | } |
| 85 | |
| 86 | // Gets previous gamefile from n that has actually been alloced |
| 87 | int GetPrevGamefilecxx . . $ GetPrevGamefile(7864480464b09eea).(int n) { |
| 88 | int i; |
| 89 | |
| 90 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(n >= 0 && n < MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!); |
| 91 | |
| 92 | if (Num_gamefilescxx . . $ Num_gamefiles. == 0) |
| 93 | return -1; |
| 94 | |
| 95 | for (i = n - 1; i >= 0; i--) { |
| 96 | if (Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used.) |
| 97 | return i; |
| 98 | } |
| 99 | for (i = MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`! - 1; i > n; i--) { |
| 100 | if (Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used.) |
| 101 | return i; |
| 102 | } |
| 103 | |
| 104 | // this is the only one |
| 105 | return n; |
| 106 | } |
| 107 | // Searches thru all gamefiles for a specific name, returns -1 if not found |
| 108 | // or index of gamefile with name |
| 109 | int FindGamefileNamecxx . . $ FindGamefileName(1ef8e6ec083aabb9).(char *name) { |
| 110 | int i; |
| 111 | |
| 112 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(name != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 113 | |
| 114 | for (i = 0; i < MAX_GAMEFILEScxx . . $ `Descent3/gamefile.h:25:9`!; i++) |
| 115 | if (Gamefilescxx . . $ Gamefiles.[i].usedcxx . . $ gamefile#used. && !stricmpcxx . . $ `linux/linux_fix.h:43:9`!(name, Gamefiles[i].name)) |
| 116 | return i; |
| 117 | |
| 118 | return -1; |
| 119 | } |