/* fileio.cpp * * Micropolis, Unix Version. This game was released for the Unix platform * in or about 1990 and has been modified for inclusion in the One Laptop * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If * you need assistance with this program, you may contact: * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. You should have received a * copy of the GNU General Public License along with this program. If * not, see . * * ADDITIONAL TERMS per GNU GPL Section 7 * * No trademark or publicity rights are granted. This license does NOT * give you any right, title or interest in the trademark SimCity or any * other Electronic Arts trademark. You may not distribute any * modification of this program using the trademark SimCity or claim any * affliation or association with Electronic Arts Inc. or its employees. * * Any propagation or conveyance of this program must include this * copyright notice and these terms. * * If you convey this program (or any modifications of it) and assume * contractual liability for the program to recipients of it, you agree * to indemnify Electronic Arts for any liability that those contractual * assumptions impose on Electronic Arts. * * You may not misrepresent the origins of this program; modified * versions of the program must be marked as such and not identified as * the original program. * * This disclaimer supplements the one included in the General Public * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES, * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY, * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING, * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY * NOT APPLY TO YOU. */ /** * @file fileio.cpp * @brief File I/O operations for Micropolis game engine. * * Implements file loading and saving functionalities for the * Micropolis game engine. It includes endianess conversions for data * compatibility between different architectures, loading and saving * city files, handling scenario files, and various utility functions * to support file I/O operations. It also includes functions to * report file operations to the front-end. */ //////////////////////////////////////////////////////////////////////// #include "micropolis.h" //////////////////////////////////////////////////////////////////////// #ifdef IS_INTEL /** * Convert an array of short values between MAC and Intel endian formats. * @param buf Array with shorts. * @param len Number of short values in the array. */ #define SWAP_SHORTS(buf, len) swap_shorts(buf, len) /** * Convert an array of long values between MAC and Intel endian formats. * @param buf Array with longs. * @param len Number of long values in the array. */ #define HALF_SWAP_LONGS(buf, len) half_swap_longs(buf, len) /** * Swap upper and lower byte of all shorts in the array. * @param buf Array with shorts. * @param len Number of short values in the array. */ static void swap_shorts(short *buf, int len) { int i; /* Flip bytes in each short! */ for (i = 0; i < len; i++) { *buf = ((*buf & 0xFF) <<8) | ((*buf &0xFF00) >>8); buf++; } } /** * Swap upper and lower words of all longs in the array. * @param buf Array with longs. * @param len Number of long values in the array. */ static void half_swap_longs(long *buf, int len) { int i; /* Flip bytes in each long! */ for (i = 0; i < len; i++) { long l = *buf; *buf = ((l & 0x0000ffff) << 16) | ((l & 0xffff0000) >> 16); buf++; } } #else /** * Convert an array of short values between MAC and MAC endian formats. * @param buf Array with shorts. * @param len Number of short values in the array. * @note This version does not change anything since the data is already in the * correct format. */ #define SWAP_SHORTS(buf, len) /** * Convert an array of long values between MAC and MAC endian formats. * @param buf Array with longs. * @param len Number of long values in the array. * @note This version does not change anything since the data is already in the * correct format. */ #define HALF_SWAP_LONGS(buf, len) #endif /** * Load an array of short values from file to memory. * * Convert to the correct processor architecture, if necessary. * @param buf Buffer to put the loaded short values in. * @param len Number of short values to load. * @param f File handle of the file to load from. * @return Load was succesfull. */ static bool load_short(short *buf, int len, FILE *f) { size_t result = fread(buf, sizeof(short), len, f); if ((int)result != len) { return false; } SWAP_SHORTS(buf, len); /* to intel */ return true; } /** * Save an array of short values from memory to file. * * Convert to the correct endianness first, if necessary. * @param buf Buffer containing the short values to save. * @param len Number of short values to save. * @param f File handle of the file to save to. * @return Save was succesfull. */ static bool save_short(short *buf, int len, FILE *f) { SWAP_SHORTS(buf, len); /* to MAC */ if ((int)fwrite(buf, sizeof(short), len, f) != len) { return false; } SWAP_SHORTS(buf, len); /* back to intel */ return true; } /** * Load a city file from a given filename and (optionally) directory. * @param filename Name of the file to load. * @param dir If not \c NULL, name of the directory containing the file. * @return Load was succesfull. */ bool Micropolis::loadFileData(const std::string &filename) { FILE *f; Quad size; // Open the file. f = fopen(filename.c_str(), "rb"); // open() failed; report failure. if (f == NULL) { return false; } fseek(f, 0L, SEEK_END); size = ftell(f); fseek(f, 0L, SEEK_SET); bool result = (size == 27120) && load_short(resHist, HISTORY_LENGTH / sizeof(short), f) && load_short(comHist, HISTORY_LENGTH / sizeof(short), f) && load_short(indHist, HISTORY_LENGTH / sizeof(short), f) && load_short(crimeHist, HISTORY_LENGTH / sizeof(short), f) && load_short(pollutionHist, HISTORY_LENGTH / sizeof(short), f) && load_short(moneyHist, HISTORY_LENGTH / sizeof(short), f) && load_short(miscHist, MISC_HISTORY_LENGTH / sizeof(short), f) && load_short(((short *)&map[0][0]), WORLD_W * WORLD_H, f); fclose(f); return result; } /** * Load a file, and initialize the game variables. * @param filename Name of the file to load. * @return Load was succesfull. */ bool Micropolis::loadFile(const std::string &filename) { long n; if (!loadFileData(filename)) { return false; } /* total funds is a long..... miscHist is array of shorts */ /* total funds is being put in the 50th & 51th word of miscHist */ /* find the address, cast the ptr to a longPtr, take contents */ n = *(Quad *)(miscHist + 50); HALF_SWAP_LONGS(&n, 1); setFunds(n); n = *(Quad *)(miscHist + 8); HALF_SWAP_LONGS(&n, 1); cityTime = n; setAutoBulldoze(miscHist[52] != 0); // flag for autoBulldoze setAutoBudget(miscHist[53] != 0); // flag for autoBudget setAutoGoto(miscHist[54] != 0); // flag for auto-goto setEnableSound(miscHist[55] != 0); // flag for the sound on/off setCityTax(miscHist[56]); setSpeed(miscHist[57]); changeCensus(); mustUpdateOptions = true; /* yayaya */ n = *(Quad *)(miscHist + 58); HALF_SWAP_LONGS(&n, 1); policePercent = ((float)n) / ((float)65536); n = *(Quad *)(miscHist + 60); HALF_SWAP_LONGS(&n, 1); firePercent = (float)n / (float)65536.0; n = *(Quad *)(miscHist + 62); HALF_SWAP_LONGS(&n, 1); roadPercent = (float)n / (float)65536.0; policePercent = (float)(*(Quad*)(miscHist + 58)) / (float)65536.0; /* and 59 */ firePercent = (float)(*(Quad*)(miscHist + 60)) / (float)65536.0; /* and 61 */ roadPercent = (float)(*(Quad*)(miscHist + 62)) / (float)65536.0; /* and 63 */ cityTime = max((Quad)0, cityTime); // If the tax is nonsensical, set it to a reasonable value. if (cityTax > 20 || cityTax < 0) { setCityTax(7); } // If the speed is nonsensical, set it to a reasonable value. if (simSpeed < 0 || simSpeed > 3) { setSpeed(3); } setSpeed(simSpeed); setPasses(1); initFundingLevel(); // Set the scenario id to 0. initWillStuff(); scenario = SC_NONE; initSimLoad = 1; doInitialEval = false; doSimInit(); invalidateMaps(); return true; } /** * Save a game to disk. * @param filename Name of the file to use for storing the game. * @return The game was saved successfully. */ bool Micropolis::saveFile(const std::string &filename) { long n; FILE *f; if ((f = fopen(filename.c_str(), "wb")) == NULL) { /// @todo Report error saving file. return false; } /* total funds is a long..... miscHist is array of ints */ /* total funds is bien put in the 50th & 51th word of miscHist */ /* find the address, cast the ptr to a longPtr, take contents */ n = totalFunds; HALF_SWAP_LONGS(&n, 1); (*(Quad *)(miscHist + 50)) = n; n = cityTime; HALF_SWAP_LONGS(&n, 1); (*(Quad *)(miscHist + 8)) = n; miscHist[52] = autoBulldoze; // flag for autoBulldoze miscHist[53] = autoBudget; // flag for autoBudget miscHist[54] = autoGoto; // flag for auto-goto miscHist[55] = enableSound; // flag for the sound on/off miscHist[57] = simSpeed; miscHist[56] = cityTax; /* post release */ /* yayaya */ n = (int)(policePercent * 65536); HALF_SWAP_LONGS(&n, 1); (*(Quad *)(miscHist + 58)) = n; n = (int)(firePercent * 65536); HALF_SWAP_LONGS(&n, 1); (*(Quad *)(miscHist + 60)) = n; n = (int)(roadPercent * 65536); HALF_SWAP_LONGS(&n, 1); (*(Quad *)(miscHist + 62)) = n; bool result = save_short(resHist, HISTORY_LENGTH / 2, f) && save_short(comHist, HISTORY_LENGTH / 2, f) && save_short(indHist, HISTORY_LENGTH / 2, f) && save_short(crimeHist, HISTORY_LENGTH / 2, f) && save_short(pollutionHist, HISTORY_LENGTH / 2, f) && save_short(moneyHist, HISTORY_LENGTH / 2, f) && save_short(miscHist, MISC_HISTORY_LENGTH / 2, f) && save_short(((short *)&map[0][0]), WORLD_W * WORLD_H, f); fclose(f); return result; } /** * Load a scenario. * @param s Scenario to load. * @note \a s cannot be \c SC_NONE. */ void Micropolis::loadScenario(Scenario s) { std::string name = NULL; std::string fname = NULL; cityFileName = ""; setGameLevel(LEVEL_EASY); if (s < SC_DULLSVILLE || s > SC_RIO) { s = SC_DULLSVILLE; } switch (s) { case SC_DULLSVILLE: name = "Dullsville"; fname = "cities/scenario_dullsville.cty"; scenario = SC_DULLSVILLE; cityTime = ((1900 - 1900) * 48) + 2; setFunds(5000); break; case SC_SAN_FRANCISCO: name = "San Francisco"; fname = "cities/scenario_san_francisco.cty"; scenario = SC_SAN_FRANCISCO; cityTime = ((1906 - 1900) * 48) + 2; setFunds(20000); break; case SC_HAMBURG: name = "Hamburg"; fname = "cities/scenario_hamburg.cty"; scenario = SC_HAMBURG; cityTime = ((1944 - 1900) * 48) + 2; setFunds(20000); break; case SC_BERN: name = "Bern"; fname = "cities/scenario_bern.cty"; scenario = SC_BERN; cityTime = ((1965 - 1900) * 48) + 2; setFunds(20000); break; case SC_TOKYO: name = "Tokyo"; fname = "cities/scenario_tokyo.cty"; scenario = SC_TOKYO; cityTime = ((1957 - 1900) * 48) + 2; setFunds(20000); break; case SC_DETROIT: name = "Detroit"; fname = "cities/scenario_detroit.cty"; scenario = SC_DETROIT; cityTime = ((1972 - 1900) * 48) + 2; setFunds(20000); break; case SC_BOSTON: name = "Boston"; fname = "cities/scenario_boston.cty"; scenario = SC_BOSTON; cityTime = ((2010 - 1900) * 48) + 2; setFunds(20000); break; case SC_RIO: name = "Rio de Janeiro"; fname = "cities/scenario_rio_de_janeiro.cty"; scenario = SC_RIO; cityTime = ((2047 - 1900) * 48) + 2; setFunds(20000); break; default: NOT_REACHED(); break; } setCleanCityName(name); setSpeed(3); setCityTax(7); loadFileData(fname); initWillStuff(); initFundingLevel(); updateFunds(); invalidateMaps(); initSimLoad = 1; doInitialEval = false; doSimInit(); didLoadScenario(s, name, fname); } /** Report to the front-end that the scenario was loaded. */ void Micropolis::didLoadScenario(int s, const std::string name, const std::string fname) { callback->didLoadScenario(this, callbackVal, name, fname); } /** * Try to load a new game from disk. * @param filename Name of the file to load. * @return Game was loaded successfully. * @todo In what state is the game left when loading fails? * @todo String normalization code is duplicated in Micropolis::saveCityAs(). * Extract to a sub-function. * @bug Function fails if \c lastDotdidLoadCity(this, callbackVal, filename); } /** * Report to the frontend that the game failed to load. * @param msg File that attempted to load */ void Micropolis::didntLoadCity(const std::string &filename) { callback->didntLoadCity(this, callbackVal, filename); } /** * Try to save the game. * @todo This is a no-op if the Micropolis::cityFileName is empty. * In that case, we should probably warn the user about the failure. */ void Micropolis::saveCity() { if (cityFileName.length() > 0) { doSaveCityAs(cityFileName); } else { if (saveFile(cityFileName)) { didSaveCity(cityFileName); } else { didntSaveCity(cityFileName); } } } /** * Report to the frontend that the city is being saved. * @param filename Name of the file used */ void Micropolis::doSaveCityAs(const std::string &filename) { callback->saveCityAs(this, callbackVal, filename); } /** * Report to the frontend that the city was saved successfully. * @param filename Name of the file used */ void Micropolis::didSaveCity(const std::string &filename) { callback->didSaveCity(this, callbackVal, filename); } /** * Report to the frontend that the city could not be saved. * @param filename Name of the file used */ void Micropolis::didntSaveCity(const std::string &filename) { callback->didntSaveCity(this, callbackVal, filename); } /** * Save the city under a new name (?) * @param filename Name of the file to use for storing the game. * @todo String normalization code is duplicated in Micropolis::loadCity(). * Extract to a sub-function. * @bug Function fails if \c lastDot