| 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" |
| 20 | #include "pserror.h" |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <string.h> |
| 26 | #include "game.h" |
| 27 | #include "gamefile.h" |
| 28 | |
| 29 | gamefile Gamefiles[MAX_GAMEFILES]; |
| 30 | int Num_gamefiles = 0; |
| 31 | |
| 32 | // Sets all gamefiles to unused |
| 33 | void InitGamefiles() { |
| 34 | for (int i = 0; i < MAX_GAMEFILES; i++) { |
| 35 | Gamefiles[i].used = 0; |
| 36 | Gamefiles[i].name[0] = 0; |
| 37 | } |
| 38 | Num_gamefiles = 0; |
| 39 | } |
| 40 | |
| 41 | // Allocs a gamefile for use, returns -1 if error, else index on success |
| 42 | int AllocGamefile() { |
| 43 | for (int i = 0; i < MAX_GAMEFILES; i++) { |
| 44 | if (Gamefiles[i].used == 0) { |
| 45 | memset(&Gamefiles[i], 0, sizeof(gamefile)); |
| 46 | Gamefiles[i].used = 1; |
| 47 | Num_gamefiles++; |
| 48 | return i; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Int3(); // No gamefiles free! |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | // Frees gamefile index n and all associated images |
| 57 | void FreeGamefile(int n) { |
| 58 | ASSERT(Gamefiles[n].used > 0); |
| 59 | |
| 60 | Gamefiles[n].used = 0; |
| 61 | Gamefiles[n].name[0] = 0; |
| 62 | Num_gamefiles--; |
| 63 | } |
| 64 | |
| 65 | // Gets next gamefile from n that has actually been alloced |
| 66 | int GetNextGamefile(int n) { |
| 67 | int i; |
| 68 | |
| 69 | ASSERT(n >= 0 && n < MAX_GAMEFILES); |
| 70 | |
| 71 | if (Num_gamefiles == 0) |
| 72 | return -1; |
| 73 | |
| 74 | for (i = n + 1; i < MAX_GAMEFILES; i++) |
| 75 | if (Gamefiles[i].used) |
| 76 | return i; |
| 77 | for (i = 0; i < n; i++) |
| 78 | if (Gamefiles[i].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 GetPrevGamefile(int n) { |
| 88 | int i; |
| 89 | |
| 90 | ASSERT(n >= 0 && n < MAX_GAMEFILES); |
| 91 | |
| 92 | if (Num_gamefiles == 0) |
| 93 | return -1; |
| 94 | |
| 95 | for (i = n - 1; i >= 0; i--) { |
| 96 | if (Gamefiles[i].used) |
| 97 | return i; |
| 98 | } |
| 99 | for (i = MAX_GAMEFILES - 1; i > n; i--) { |
| 100 | if (Gamefiles[i].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 FindGamefileName(char *name) { |
| 110 | int i; |
| 111 | |
| 112 | ASSERT(name != NULL); |
| 113 | |
| 114 | for (i = 0; i < MAX_GAMEFILES; i++) |
| 115 | if (Gamefiles[i].used && !stricmp(name, Gamefiles[i].name)) |
| 116 | return i; |
| 117 | |
| 118 | return -1; |
| 119 | } |