| 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: /descent3/main/megacell.cpp $ |
| 21 | * $Revision: 3 $ |
| 22 | * $Date: 6/06/97 5:25p $ |
| 23 | * $Author: Mark $ |
| 24 | * |
| 25 | * $Log: /descent3/main/megacell.cpp $ |
| 26 | * |
| 27 | * 3 6/06/97 5:25p Mark |
| 28 | * FROM JASON:Fixed megacell bug |
| 29 | * |
| 30 | * 2 6/05/97 2:52p Jason |
| 31 | * added megacell functions |
| 32 | * |
| 33 | * 1 6/05/97 10:56a Jason |
| 34 | * |
| 35 | |
| 36 | * |
| 37 | * $NoKeywords: $ |
| 38 | */ |
| 39 | |
| 40 | #include "pserror.h" |
| 41 | #include "megacell.h" |
| 42 | #include "bitmap.h" |
| 43 | #include <stdarg.h> |
| 44 | #include <string.h> |
| 45 | |
| 46 | megacell Megacells[MAX_MEGACELLS]; |
| 47 | int Num_megacells = 0; |
| 48 | |
| 49 | // Sets all megacells to unused |
| 50 | void InitMegacells() { |
| 51 | for (int i = 0; i < MAX_MEGACELLS; i++) { |
| 52 | Megacells[i].used = 0; |
| 53 | Megacells[i].name[0] = 0; |
| 54 | } |
| 55 | Num_megacells = 0; |
| 56 | } |
| 57 | |
| 58 | // Allocs a megacell for use, returns -1 if error, else index on success |
| 59 | int AllocMegacell() { |
| 60 | for (int i = 0; i < MAX_MEGACELLS; i++) { |
| 61 | if (Megacells[i].used == 0) { |
| 62 | memset(&Megacells[i], 0, sizeof(megacell)); |
| 63 | Megacells[i].used = 1; |
| 64 | Megacells[i].width = DEFAULT_MEGACELL_WIDTH; |
| 65 | Megacells[i].height = DEFAULT_MEGACELL_HEIGHT; |
| 66 | Num_megacells++; |
| 67 | return i; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Int3(); // No megacells free! |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | // Frees megacell index n |
| 76 | void FreeMegacell(int n) { |
| 77 | ASSERT(Megacells[n].used > 0); |
| 78 | |
| 79 | Megacells[n].used = 0; |
| 80 | Megacells[n].name[0] = 0; |
| 81 | Num_megacells--; |
| 82 | } |
| 83 | |
| 84 | // Gets next megacell from n that has actually been alloced |
| 85 | int GetNextMegacell(int n) { |
| 86 | int i; |
| 87 | |
| 88 | ASSERT(n >= 0 && n < MAX_MEGACELLS); |
| 89 | |
| 90 | if (Num_megacells == 0) |
| 91 | return 0; |
| 92 | |
| 93 | for (i = n + 1; i < MAX_MEGACELLS; i++) |
| 94 | if (Megacells[i].used) |
| 95 | return i; |
| 96 | for (i = 0; i < n; i++) |
| 97 | if (Megacells[i].used) |
| 98 | return i; |
| 99 | |
| 100 | // this is the only one |
| 101 | |
| 102 | return n; |
| 103 | } |
| 104 | |
| 105 | // Gets previous megacell from n that has actually been alloced |
| 106 | int GetPrevMegacell(int n) { |
| 107 | int i; |
| 108 | |
| 109 | ASSERT(n >= 0 && n < MAX_MEGACELLS); |
| 110 | |
| 111 | if (Num_megacells == 0) |
| 112 | return 0; |
| 113 | |
| 114 | for (i = n - 1; i >= 0; i--) { |
| 115 | if (Megacells[i].used) |
| 116 | return i; |
| 117 | } |
| 118 | for (i = MAX_MEGACELLS - 1; i > n; i--) { |
| 119 | if (Megacells[i].used) |
| 120 | return i; |
| 121 | } |
| 122 | |
| 123 | // this is the only one |
| 124 | return n; |
| 125 | } |
| 126 | // Searches thru all megacells for a specific name, returns -1 if not found |
| 127 | // or index of megacell with name |
| 128 | int FindMegacellName(char *name) { |
| 129 | int i; |
| 130 | |
| 131 | ASSERT(name != NULL); |
| 132 | |
| 133 | for (i = 0; i < MAX_MEGACELLS; i++) |
| 134 | if (Megacells[i].used && !stricmp(name, Megacells[i].name)) |
| 135 | return i; |
| 136 | |
| 137 | return -1; |
| 138 | } |