From e2f81aaa54eebde47faa4d62866b1cad34aa299d Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Mon, 31 Mar 2008 14:05:57 +0000 Subject: [PATCH] * Add a new resource type (for use with .wrf files): DBPROP which is similar to SPROP, with as difference that instead of a CSV file it specifies an SQLite database file to load from * Add the code to load propulsions from the propulsion table of the given database file (function `loadPropulsionStatsFromDB`) * This code will make sure to load the propulsion stats-data in a similar manner (i.e. the resulting data in-memory should be the same) to the propulsion.txt loading code * Use this stats loading implementation on single player git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@4444 4a71c877-e1ca-e34f-864e-861f7616d084 --- data/wrf/stats.wrf | 2 +- src/data.c | 14 +++++ src/stats-db.c | 134 ++++++++++++++++++++++++++++++++++++++++++++- src/stats-db.h | 1 + 4 files changed, 149 insertions(+), 2 deletions(-) diff --git a/data/wrf/stats.wrf b/data/wrf/stats.wrf index 0835bed6e..8b0259126 100644 --- a/data/wrf/stats.wrf +++ b/data/wrf/stats.wrf @@ -9,8 +9,8 @@ directory "stats-sql" file DBWEAPON "stats.db" file DBBODY "stats.db" file DBBRAIN "stats.db" +file DBPROP "stats.db" directory "stats" -file SPROP "propulsion.txt" file SSENSOR "sensor.txt" file SECM "ecm.txt" file SREPAIR "repair.txt" diff --git a/src/data.c b/src/data.c index 9725acca6..27882e885 100644 --- a/src/data.c +++ b/src/data.c @@ -195,6 +195,19 @@ static BOOL bufferSPROPLoad(const char *pBuffer, UDWORD size, void **ppData) return true; } +static BOOL dataDBPROPLoad(const char* filename, void **ppData) +{ + if (!loadPropulsionStatsFromDB(filename) + || !allocComponentList(COMP_PROPULSION, numPropulsionStats)) + { + return false; + } + + // not interested in this value + *ppData = NULL; + return true; +} + /* Load the Sensor stats */ static BOOL bufferSSENSORLoad(const char *pBuffer, UDWORD size, void **ppData) { @@ -990,6 +1003,7 @@ static const RES_TYPE_MIN_FILE FileResourceTypes[] = {"DBWEAPON", dataDBWEAPONLoad, NULL}, {"DBBODY", dataDBBODYLoad, dataReleaseStats}, {"DBBRAIN", dataDBBRAINLoad, NULL}, + {"DBPROP", dataDBPROPLoad, NULL}, {"WAV", dataAudioLoad, (RES_FREE)sound_ReleaseTrack}, {"AUDIOCFG", dataAudioCfgLoad, NULL}, {"ANI", dataAnimLoad, dataAnimRelease}, diff --git a/src/stats-db.c b/src/stats-db.c index 8f23bbb3a..4b8108f85 100644 --- a/src/stats-db.c +++ b/src/stats-db.c @@ -782,7 +782,6 @@ bool loadBrainStatsFromDB(const char* filename) "FROM `brain`;", &stmt)) goto in_db_err; - while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { BRAIN_STATS sStats, * const stats = &sStats; @@ -858,3 +857,136 @@ in_db_err: return retval; } + +/** Load the propulsion stats from the given SQLite database file + * \param filename name of the database file to load the propulsion stats from. + */ +bool loadPropulsionStatsFromDB(const char* filename) +{ + bool retval = false; + sqlite3* db; + sqlite3_stmt* stmt; + int rc; + + if (!openDB(filename, &db)) + goto in_db_err; + + // Prepare this SQL statement for execution + if (!prepareStatement(db, "SELECT MAX(id) FROM `propulsion`;", &stmt)) + goto in_db_err; + + /* Execute and process the results of the above SQL statement to + * determine the amount of propulsions we're about to fetch. Then make + * sure to allocate memory for that amount of propulsions. */ + rc = sqlite3_step(stmt); + if (rc != SQLITE_ROW + || sqlite3_data_count(stmt) != 1 + || !statsAllocPropulsion(sqlite3_column_int(stmt, 0))) + { + goto in_statement_err; + } + + sqlite3_finalize(stmt); + if (!prepareStatement(db, "SELECT `id`," + "`name`," + "`techlevel`," + "`buildPower`," + "`buildPoints`," + "`weight`," + "`hitpoints`," + "`systempoints`," + "`body`," + "`GfxFile`," + "`type`," + "`maxSpeed`," + "`designable`" + "FROM `propulsion`;", &stmt)) + goto in_db_err; + + while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) + { + PROPULSION_STATS sStats, * const stats = &sStats; + unsigned int colnum = 0; + const unsigned int propulsion_id = sqlite3_column_int(stmt, colnum++); + + memset(stats, 0, sizeof(*stats)); + + stats->ref = REF_PROPULSION_START + propulsion_id - 1; + + // name TEXT NOT NULL, -- Text id name (short language independant name) + if (!allocateStatName((BASE_STATS *)stats, (const char*)sqlite3_column_text(stmt, colnum++))) + { + goto in_statement_err; + } + + // techlevel TEXT NOT NULL, -- Technology level of this component + if (!setTechLevel((BASE_STATS *)stats, (const char*)sqlite3_column_text(stmt, colnum++))) + { + goto in_statement_err; + } + + // buildPower NUMERIC NOT NULL, -- Power required to build this component + stats->buildPower = sqlite3_column_double(stmt, colnum++); + // buildPoints NUMERIC NOT NULL, -- Time required to build this component + stats->buildPoints = sqlite3_column_double(stmt, colnum++); + // weight NUMERIC NOT NULL, -- Component's weight (mass?) + stats->weight = sqlite3_column_double(stmt, colnum++); + // hitpoints NUMERIC NOT NULL, -- Component's hitpoints - SEEMS TO BE UNUSED + stats->hitPoints = sqlite3_column_double(stmt, colnum++); + // systempoints NUMERIC NOT NULL, -- Space the component takes in the droid - SEEMS TO BE UNUSED + stats->systemPoints = sqlite3_column_double(stmt, colnum++); + // body NUMERIC NOT NULL, -- Component's body points + stats->body = sqlite3_column_double(stmt, colnum++); + + // Get the IMD for the component + // GfxFile TEXT, -- The IMD to draw for this component + if (sqlite3_column_type(stmt, colnum) != SQLITE_NULL) + { + stats->pIMD = (iIMDShape *) resGetData("IMD", (const char*)sqlite3_column_text(stmt, colnum++)); + if (stats->pIMD == NULL) + { + debug(LOG_ERROR, "Cannot find the propulsion PIE for record %s", getStatName(stats)); + abort(); + goto in_statement_err; + } + } + else + { + stats->pIMD = NULL; + ++colnum; + } + + // type TEXT NOT NULL, -- Type of propulsion + stats->propulsionType = getPropulsionType((const char*)sqlite3_column_text(stmt, colnum++)); + if (stats->propulsionType == INVALID_PROP_TYPE) + { + debug(LOG_ERROR, "loadPropulsionStatsFromDB: Invalid Propulsion type for %s", getStatName(stats)); + abort(); + goto in_statement_err; + } + + // maxSpeed NUMERIC NOT NULL, -- Max speed for droids with this propulsion + stats->maxSpeed = sqlite3_column_double(stmt, colnum++); + // designable NUMERIC NOT NULL -- flag to indicate whether this component can be used in the design screen + stats->design = sqlite3_column_int(stmt, colnum++) ? true : false; + + // set the max stats values for the design screen + if (stats->design) + { + setMaxPropulsionSpeed(stats->maxSpeed); + //setMaxComponentWeight(stats->weight); + } + + //save the stats + statsSetPropulsion(stats, propulsion_id - 1); + } + + retval = true; + +in_statement_err: + sqlite3_finalize(stmt); +in_db_err: + sqlite3_close(db); + + return retval; +} diff --git a/src/stats-db.h b/src/stats-db.h index 953d81c98..f1779859d 100644 --- a/src/stats-db.h +++ b/src/stats-db.h @@ -30,5 +30,6 @@ extern bool loadWeaponStatsFromDB(const char* filename); extern bool loadBodyStatsFromDB(const char* filename); extern bool loadBrainStatsFromDB(const char* filename); +extern bool loadPropulsionStatsFromDB(const char* filename); #endif // __INCLUDED_STATS_DB_H__