| 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/manage/megapage.cpp $ |
| 21 | * $Revision: 9 $ |
| 22 | * $Date: 8/11/99 5:32p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * |
| 26 | * $Log: /DescentIII/Main/manage/megapage.cpp $ |
| 27 | * |
| 28 | * 9 8/11/99 5:32p Jeff |
| 29 | * changes to fix addon tablefile support so it works correctly |
| 30 | * |
| 31 | * 8 7/28/99 2:28p Kevin |
| 32 | * Macintosh Changes! |
| 33 | * |
| 34 | * 7 4/14/99 1:33a Jeff |
| 35 | * fixed case mismatched #includes |
| 36 | * |
| 37 | * 6 3/04/99 4:47p Jason |
| 38 | * temp fix (ie BAD HACK) for OEM table file woes |
| 39 | * |
| 40 | * 5 1/13/99 7:05a Jeff |
| 41 | * put #ifdef around #include <windows.h> |
| 42 | * |
| 43 | * 4 12/29/98 4:30p Jason |
| 44 | * added add-on data functionality |
| 45 | * |
| 46 | * 3 11/06/98 12:35p Jason |
| 47 | * more speedups for manage system |
| 48 | * |
| 49 | * 2 11/05/98 7:55p Jason |
| 50 | * changes for new manage system |
| 51 | * |
| 52 | * 3 6/05/97 6:58p Jason |
| 53 | * added more megacell functionality |
| 54 | * |
| 55 | * 2 6/05/97 2:52p Jason |
| 56 | * added megacell functions |
| 57 | * |
| 58 | * 1 6/05/97 11:13a Jason |
| 59 | * page for megacells |
| 60 | * |
| 61 | * $NoKeywords: $ |
| 62 | */ |
| 63 | |
| 64 | #include <cstring> |
| 65 | #include "cfile.h" |
| 66 | #include "manage.h" |
| 67 | #include "megacell.h" |
| 68 | #include "megapage.h" |
| 69 | #include "texpage.h" |
| 70 | #include "mono.h" |
| 71 | #include "pserror.h" |
| 72 | #include "polymodel.h" |
| 73 | #include "ddio.h" |
| 74 | // megacellpage commands that are read/written |
| 75 | // A command is followed by a byte count describing how many bytes |
| 76 | // are in the data for the command |
| 77 | #define MEGACELLPAGE_COMMAND_NAME 1 |
| 78 | #define MEGACELLPAGE_COMMAND_END 2 |
| 79 | #define MEGACELLPAGE_COMMAND_CELL_NAME 3 |
| 80 | #define MEGACELLPAGE_COMMAND_VERSION 4 |
| 81 | #define MEGACELLPAGE_COMMAND_WIDTH 5 |
| 82 | #define MEGACELLPAGE_COMMAND_HEIGHT 6 |
| 83 | #define MEGACELL_VERSION 2 |
| 84 | // Given an open file pointer and a megacell_page struct, writes that megacell page out |
| 85 | void mng_WriteMegacellPage(CFILE *outfile, mngs_megacell_page *megacellpage) { |
| 86 | int i; |
| 87 | ASSERT(outfile != NULL); |
| 88 | ASSERT(megacellpage != NULL); |
| 89 | |
| 90 | cf_WriteByte(outfile, PAGETYPE_MEGACELL); |
| 91 | |
| 92 | cf_WriteByte(outfile, MEGACELLPAGE_COMMAND_NAME); // write out megacell name |
| 93 | cf_WriteByte(outfile, strlen(megacellpage->megacell_struct.name) + 1); |
| 94 | cf_WriteString(outfile, megacellpage->megacell_struct.name); |
| 95 | |
| 96 | // Write out its cell names |
| 97 | for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) { |
| 98 | cf_WriteByte(outfile, MEGACELLPAGE_COMMAND_CELL_NAME); // get ready to write out name |
| 99 | cf_WriteByte(outfile, strlen(megacellpage->cellname[i]) + 2); |
| 100 | cf_WriteByte(outfile, i); |
| 101 | cf_WriteString(outfile, megacellpage->cellname[i]); |
| 102 | } |
| 103 | |
| 104 | cf_WriteByte(outfile, MEGACELLPAGE_COMMAND_WIDTH); |
| 105 | cf_WriteByte(outfile, 1); |
| 106 | cf_WriteByte(outfile, megacellpage->megacell_struct.width); |
| 107 | cf_WriteByte(outfile, MEGACELLPAGE_COMMAND_HEIGHT); |
| 108 | cf_WriteByte(outfile, 1); |
| 109 | cf_WriteByte(outfile, megacellpage->megacell_struct.height); |
| 110 | cf_WriteByte(outfile, MEGACELLPAGE_COMMAND_END); // we're all done |
| 111 | cf_WriteByte(outfile, 0); |
| 112 | } |
| 113 | // Given an open file pointer and a megacell_page struct, writes that megacell page out |
| 114 | void mng_WriteNewMegacellPage(CFILE *outfile, mngs_megacell_page *megacellpage) { |
| 115 | int i; |
| 116 | ASSERT(outfile != NULL); |
| 117 | ASSERT(megacellpage != NULL); |
| 118 | int offset = StartManagePage(outfile, PAGETYPE_MEGACELL); |
| 119 | cf_WriteShort(outfile, MEGACELL_VERSION); |
| 120 | |
| 121 | cf_WriteString(outfile, megacellpage->megacell_struct.name); |
| 122 | |
| 123 | // Write out its cell names |
| 124 | for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) |
| 125 | cf_WriteString(outfile, megacellpage->cellname[i]); |
| 126 | |
| 127 | cf_WriteByte(outfile, megacellpage->megacell_struct.width); |
| 128 | cf_WriteByte(outfile, megacellpage->megacell_struct.height); |
| 129 | EndManagePage(outfile, offset); |
| 130 | } |
| 131 | // Reads a megacell page from an open file. Returns 0 on error. |
| 132 | int mng_ReadMegacellPage(CFILE *infile, mngs_megacell_page *megacellpage) { |
| 133 | int done = 0; |
| 134 | char command; |
| 135 | uint16_t len; |
| 136 | int i, temp; |
| 137 | if (!Old_table_method) |
| 138 | return mng_ReadNewMegacellPage(infile, megacellpage); |
| 139 | ASSERT(infile != NULL); |
| 140 | memset(megacellpage, 0, sizeof(mngs_megacell_page)); |
| 141 | while (!done) { |
| 142 | // Read in command byte then read in the length of that commands data |
| 143 | |
| 144 | command = cf_ReadByte(infile); |
| 145 | len = cf_ReadByte(infile); |
| 146 | switch (command) { |
| 147 | case MEGACELLPAGE_COMMAND_END: |
| 148 | done = 1; |
| 149 | break; |
| 150 | case MEGACELLPAGE_COMMAND_CELL_NAME: // the name of the megacell model |
| 151 | temp = cf_ReadByte(infile); |
| 152 | cf_ReadString(megacellpage->cellname[temp], PAGENAME_LEN, infile); |
| 153 | break; |
| 154 | case MEGACELLPAGE_COMMAND_NAME: |
| 155 | cf_ReadString(megacellpage->megacell_struct.name, PAGENAME_LEN, infile); |
| 156 | break; |
| 157 | case MEGACELLPAGE_COMMAND_WIDTH: |
| 158 | megacellpage->megacell_struct.width = cf_ReadByte(infile); |
| 159 | break; |
| 160 | case MEGACELLPAGE_COMMAND_HEIGHT: |
| 161 | megacellpage->megacell_struct.height = cf_ReadByte(infile); |
| 162 | break; |
| 163 | default: |
| 164 | // Ignore the ones we don't know |
| 165 | for (i = 0; i < len; i++) |
| 166 | cf_ReadByte(infile); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | // This is a valid new page |
| 171 | megacellpage->megacell_struct.used = 1; |
| 172 | return 1; // successfully read |
| 173 | } |
| 174 | // Reads a megacell page from an open file. Returns 0 on error. |
| 175 | int mng_ReadNewMegacellPage(CFILE *infile, mngs_megacell_page *megacellpage) { |
| 176 | int i; |
| 177 | ASSERT(infile != NULL); |
| 178 | memset(megacellpage, 0, sizeof(mngs_megacell_page)); |
| 179 | int version = cf_ReadShort(infile); |
| 180 | |
| 181 | cf_ReadString(megacellpage->megacell_struct.name, PAGENAME_LEN, infile); |
| 182 | |
| 183 | // Write out its cell names |
| 184 | for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) |
| 185 | cf_ReadString(megacellpage->cellname[i], PAGENAME_LEN, infile); |
| 186 | |
| 187 | megacellpage->megacell_struct.width = cf_ReadByte(infile); |
| 188 | megacellpage->megacell_struct.height = cf_ReadByte(infile); |
| 189 | // This is a valid new page |
| 190 | megacellpage->megacell_struct.used = 1; |
| 191 | return 1; // successfully read |
| 192 | } |
| 193 | // Reads in the megacell named "name" into megacellpage struct |
| 194 | // Returns 0 on error or couldn't find, else 1 if all is good |
| 195 | int mng_FindSpecificMegacellPage(char *name, mngs_megacell_page *megacellpage, int local) { |
| 196 | CFILE *infile; |
| 197 | uint8_t pagetype; |
| 198 | int done = 0, found = 0; |
| 199 | |
| 200 | if (local) |
| 201 | infile = cfopen(LocalTableFilename, "rb"); |
| 202 | else |
| 203 | infile = cfopen(TableFilename, "rb"); |
| 204 | if (!infile) { |
| 205 | mprintf(0, "Couldn't open table file to find megacell!\n"); |
| 206 | Int3(); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | // Read in the entire page file until we find the page we want |
| 211 | while (!done) { |
| 212 | if (cfeof(infile)) { |
| 213 | done = 1; |
| 214 | continue; |
| 215 | } |
| 216 | pagetype = cf_ReadByte(infile); |
| 217 | int len = cf_ReadInt(infile); |
| 218 | // If not a megacell page, just read it in and ignore it |
| 219 | if (pagetype != PAGETYPE_MEGACELL) { |
| 220 | cfseek(infile, len - 4, SEEK_CUR); |
| 221 | continue; |
| 222 | } |
| 223 | mng_ReadNewMegacellPage(infile, megacellpage); |
| 224 | |
| 225 | if (!stricmp(name, megacellpage->megacell_struct.name)) { |
| 226 | // This is the page we want |
| 227 | found = 1; |
| 228 | done = 1; |
| 229 | } |
| 230 | } |
| 231 | cfclose(infile); |
| 232 | |
| 233 | return found; // successful! |
| 234 | } |
| 235 | // Given a megacell page, allocs a megacell and calls AssignMegacellPageToMegacell to actually |
| 236 | // load models and values. Rturns megacell handle on success, -1 if fail |
| 237 | int mng_SetAndLoadMegacell(mngs_megacell_page *megacellpage) { |
| 238 | int n; |
| 239 | |
| 240 | n = AllocMegacell(); |
| 241 | if (n < 0) |
| 242 | return -1; |
| 243 | if (!mng_AssignMegacellPageToMegacell(megacellpage, n)) |
| 244 | return -1; |
| 245 | |
| 246 | return n; |
| 247 | } |
| 248 | // Given a megacellpage and a megacell handle, attempts to make megacell n correspond to |
| 249 | // to the megacellpage. |
| 250 | // Returns 1 on success, 0 otherwise |
| 251 | int mng_AssignMegacellPageToMegacell(mngs_megacell_page *megacellpage, int n) { |
| 252 | megacell *megacellpointer = &Megacells[n]; |
| 253 | int img_handle, i; |
| 254 | // copy our values |
| 255 | memcpy(megacellpointer, &megacellpage->megacell_struct, sizeof(megacell)); |
| 256 | strcpy(megacellpointer->name, megacellpage->megacell_struct.name); |
| 257 | // Try and load our megacell images from the disk |
| 258 | for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) { |
| 259 | if (!stricmp(megacellpage->cellname[i], "INVALID IMAGE NAME")) { |
| 260 | megacellpointer->texture_handles[i] = 0; |
| 261 | } else { |
| 262 | img_handle = mng_GetGuaranteedTexturePage(megacellpage->cellname[i]); |
| 263 | if (img_handle < 0) |
| 264 | megacellpointer->texture_handles[i] = 0; |
| 265 | else |
| 266 | megacellpointer->texture_handles[i] = img_handle; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return 1; |
| 271 | } |
| 272 | // Copies values from a megacell into a megacell_page |
| 273 | void mng_AssignMegacellToMegacellPage(int n, mngs_megacell_page *megacellpage) { |
| 274 | megacell *megacellpointer = &Megacells[n]; |
| 275 | // Assign the values |
| 276 | memcpy(&megacellpage->megacell_struct, megacellpointer, sizeof(megacell)); |
| 277 | |
| 278 | strcpy(megacellpage->megacell_struct.name, megacellpointer->name); |
| 279 | for (int i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) { |
| 280 | if (megacellpointer->texture_handles[i] != 0) |
| 281 | strcpy(megacellpage->cellname[i], GameTextures[megacellpointer->texture_handles[i]].name); |
| 282 | else |
| 283 | strcpy(megacellpage->cellname[i], "INVALID IMAGE NAME"); |
| 284 | } |
| 285 | } |
| 286 | // Loads a megacell found in the net table file. It then allocs a megacell and |
| 287 | // then calls SetAndLoadMegacell to actually load in any images/models associated |
| 288 | // with it |
| 289 | void mng_LoadNetMegacellPage(CFILE *infile) { |
| 290 | mngs_megacell_page megacellpage; |
| 291 | memset(&megacellpage, 0, sizeof(mngs_megacell_page)); |
| 292 | |
| 293 | if (mng_ReadNewMegacellPage(infile, &megacellpage)) { |
| 294 | int ret = mng_SetAndLoadMegacell(&megacellpage); |
| 295 | ASSERT(ret >= 0); |
| 296 | } else |
| 297 | mprintf(0, "Could not load megacellpage named %s!\n", megacellpage.megacell_struct.name); |
| 298 | } |
| 299 | // Reads a megacell page from a local table file. It then allocs a megacell and |
| 300 | // loads any images/models associated with that megacell |
| 301 | void mng_LoadLocalMegacellPage(CFILE *infile) { |
| 302 | mngs_megacell_page megacellpage; |
| 303 | int ok = 0; |
| 304 | memset(&megacellpage, 0, sizeof(mngs_megacell_page)); |
| 305 | if (mng_ReadNewMegacellPage(infile, &megacellpage)) { |
| 306 | // Check to see if this is a local copy that is supposed |
| 307 | // to go over a network copy (supersede the net copy) |
| 308 | int i = FindMegacellName(megacellpage.megacell_struct.name); |
| 309 | if (i != -1) { |
| 310 | // Make sure we really have this page checked out |
| 311 | mngs_Pagelock pl; |
| 312 | |
| 313 | strcpy(pl.name, megacellpage.megacell_struct.name); |
| 314 | pl.pagetype = PAGETYPE_MEGACELL; |
| 315 | |
| 316 | /*if (Network_up && Stand_alone==0) |
| 317 | { |
| 318 | int locked=mng_CheckIfPageOwned(&pl,TableUser); |
| 319 | if (locked!=1) |
| 320 | Int3(); // Your local vs net copies of the lock file do not match |
| 321 | }*/ |
| 322 | |
| 323 | mng_AssignMegacellPageToMegacell(&megacellpage, i); |
| 324 | ok = 1; |
| 325 | } else { |
| 326 | // This is a local megacell that has never been checked in |
| 327 | if ((i = mng_SetAndLoadMegacell(&megacellpage)) < 0) |
| 328 | ok = 0; |
| 329 | else |
| 330 | ok = 1; |
| 331 | } |
| 332 | |
| 333 | ASSERT(ok == 1); |
| 334 | if (Loading_addon_table == -1) |
| 335 | mng_AllocTrackLock(megacellpage.megacell_struct.name, PAGETYPE_MEGACELL); |
| 336 | } else |
| 337 | mprintf(0, "Could not load megacellpage named %s!\n", megacellpage.megacell_struct.name); |
| 338 | } |