diff --git a/AI/Skirmish/AAI/AAIConfig.cpp b/AI/Skirmish/AAI/AAIConfig.cpp index c410f9ed4bb..c268dd39056 100644 --- a/AI/Skirmish/AAI/AAIConfig.cpp +++ b/AI/Skirmish/AAI/AAIConfig.cpp @@ -182,7 +182,7 @@ AAIConfig::~AAIConfig(void) } -std::string AAIConfig::GetFileName(const std::string& filename, const std::string& prefix, const std::string& suffix, bool write) +std::string AAIConfig::GetFileName(const std::string& filename, const std::string& prefix, const std::string& suffix, bool write) const { std::string name = prefix + MakeFileSystemCompatible(filename) + suffix; @@ -512,4 +512,32 @@ const UnitDef* AAIConfig::GetUnitDef(const std::string& name) return res; } +std::string AAIConfig::getUniqueName(bool game, bool gamehash, bool map, bool maphash) const +{ + std::string res; + if (map) { + if (!res.empty()) + res += "-"; + std::string mapName = MakeFileSystemCompatible(ai->Getcb()->GetMapName()); + mapName.resize(mapName.size() - 4); // cut off extension + res += mapName; + } + if (maphash) { + if (!res.empty()) + res += "-"; + res += IntToString(ai->Getcb()->GetMapHash(), "%x"); + } + if (game) { + if (!res.empty()) + res += "_"; + res += MakeFileSystemCompatible(ai->Getcb()->GetModHumanName()); + } + if (gamehash) { + if (!res.empty()) + res += "-"; + res += IntToString(ai->Getcb()->GetModHash(), "%x"); + } + return res; +} + AAIConfig *cfg = new AAIConfig(); diff --git a/AI/Skirmish/AAI/AAIConfig.h b/AI/Skirmish/AAI/AAIConfig.h index 17c0f2a8d00..8f259cf343c 100644 --- a/AI/Skirmish/AAI/AAIConfig.h +++ b/AI/Skirmish/AAI/AAIConfig.h @@ -132,14 +132,18 @@ class AAIConfig float LEARN_SPEED; int LEARN_RATE; int GAME_PERIODS; -private: - ~AAIConfig(void); + /** * open a file in springs data directory * @param filename relative path of the file in the spring data dir * @param mode mode file to open, see manpage of fopen */ - std::string GetFileName(const std::string& filename, const std::string& prefix = "", const std::string& suffix = "", bool write = false); + std::string GetFileName(const std::string& filename, const std::string& prefix = "", const std::string& suffix = "", bool write = false) const; + std::string getUniqueName(bool game, bool gamehash, bool map, bool maphash) const; + +private: + ~AAIConfig(void); + const UnitDef* GetUnitDef(const std::string& name); int GetInt(FILE* file); float GetFloat(FILE* file); diff --git a/AI/Skirmish/AAI/AAIMap.cpp b/AI/Skirmish/AAI/AAIMap.cpp index 3fb58601fa5..4a4f84fd29c 100644 --- a/AI/Skirmish/AAI/AAIMap.cpp +++ b/AI/Skirmish/AAI/AAIMap.cpp @@ -13,12 +13,12 @@ #include "AAIBrain.h" #include "AAIConfig.h" #include "AAISector.h" -#include "aidef.h" #include "System/Util.h" #include "LegacyCpp/UnitDef.h" using namespace springLegacyAI; +#define MAP_CACHE_PATH "cache/" int AAIMap::xSize; int AAIMap::ySize; @@ -81,7 +81,7 @@ AAIMap::~AAIMap(void) { Learn(); - const std::string mapLearn_filename = LocateMapLearnFile(true); + const std::string mapLearn_filename = LocateMapLearnFile(); // save map data FILE *save_file = fopen(mapLearn_filename.c_str(), "w+"); @@ -256,7 +256,7 @@ void AAIMap::ReadMapCacheFile() const size_t buffer_sizeMax = 512; char buffer[buffer_sizeMax]; - const std::string mapCache_filename = LocateMapCacheFile(false); + const std::string mapCache_filename = LocateMapCacheFile(); FILE *file; @@ -345,7 +345,7 @@ void AAIMap::ReadMapCacheFile() ////////////////////////////////////////////////////////////////////////////////////////////////////// // save mod independent map data - const std::string mapCache_filename = LocateMapCacheFile(true); + const std::string mapCache_filename = LocateMapCacheFile(); file = fopen(mapCache_filename.c_str(), "w+"); @@ -456,37 +456,16 @@ void AAIMap::ReadMapCacheFile() } } + + void AAIMap::ReadContinentFile() { - // this size equals the one used in "AIAICallback::GetValue(AIVAL_LOCATE_FILE_..." - static const size_t buffer_sizeMax = 2048; - char buffer[buffer_sizeMax]; - STRCPY(buffer, " "); - STRCAT(buffer, MAP_CACHE_PATH); - std::string mapName = MakeFileSystemCompatible(ai->Getcb()->GetMapName()); - mapName.resize(mapName.size() - 4); // cut off extension - STRCAT(buffer, mapName.c_str()); - STRCAT(buffer, "-"); - const std::string mapHash = IntToString(ai->Getcb()->GetMapHash(), "%x"); - STRCAT(buffer, mapHash.c_str()); - STRCAT(buffer, "_"); - const std::string modHumanName = MakeFileSystemCompatible(ai->Getcb()->GetModHumanName()); - STRCAT(buffer, modHumanName.c_str()); - STRCAT(buffer, "-"); - const std::string modHash = IntToString(ai->Getcb()->GetModHash(), "%x"); - STRCAT(buffer, modHash.c_str()); - STRCAT(buffer, ".dat"); - char filename[buffer_sizeMax]; - STRCPY(filename, buffer); - - // as we will have to write to the file later on anyway, - // we want it writable - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_W, filename); - - FILE* file = fopen(filename, "r"); + const std::string filename = cfg->GetFileName(cfg->getUniqueName(true, true, true, true), MAP_CACHE_PATH "continent-", ".dat", true); + FILE* file = fopen(filename.c_str(), "r"); if(file != NULL) { + char buffer[4096]; // check if correct version fscanf(file, "%s ", buffer); @@ -543,17 +522,8 @@ void AAIMap::ReadContinentFile() ////////////////////////////////////////////////////////////////////////////////////////////////////// // save movement maps - STRCPY(buffer, " "); - STRCAT(buffer, MAP_CACHE_PATH); - STRCAT(buffer, mapName.c_str()); - STRCAT(buffer, "_"); - STRCAT(buffer, modHumanName.c_str()); - STRCAT(buffer, ".dat"); - STRCPY(filename, buffer); - - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_W, filename); - - file = fopen(filename, "w+"); + const std::string movementfile = cfg->GetFileName(cfg->getUniqueName(true, false, true, false), MAP_CACHE_PATH "movement-", ".dat", true); + file = fopen(movementfile.c_str(), "w+"); fprintf(file, "%s\n", CONTINENT_DATA_VERSION); @@ -580,67 +550,20 @@ void AAIMap::ReadContinentFile() fclose(file); } -std::string AAIMap::LocateMapLearnFile(const bool forWriting) const { - - // this size equals the one used in "AIAICallback::GetValue(AIVAL_LOCATE_FILE_..." - const size_t buffer_sizeMax = 2048; - char buffer[buffer_sizeMax]; - - STRCPY(buffer, " "); - STRCAT(buffer, MAP_LEARN_PATH); - std::string mapName = MakeFileSystemCompatible(ai->Getcb()->GetMapName()); - mapName.resize(mapName.size() - 4); // cut off extension - STRCAT(buffer, mapName.c_str()); - STRCAT(buffer, "-"); - const std::string mapHash = IntToString(ai->Getcb()->GetMapHash(), "%x"); - STRCAT(buffer, mapHash.c_str()); - STRCAT(buffer, "_"); - const std::string modHumanName = MakeFileSystemCompatible(ai->Getcb()->GetModHumanName()); - STRCAT(buffer, modHumanName.c_str()); - STRCAT(buffer, "-"); - const std::string modHash = IntToString(ai->Getcb()->GetModHash(), "%x"); - STRCAT(buffer, modHash.c_str()); - STRCAT(buffer, ".dat"); - - if (forWriting) { - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_W, buffer); - } else { - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_R, buffer); - } - - return std::string(buffer); +std::string AAIMap::LocateMapLearnFile() const +{ + return cfg->GetFileName(cfg->getUniqueName(true, true, true, true), MAP_LEARN_PATH "maplearn-", ".dat", true); } -std::string AAIMap::LocateMapCacheFile(const bool forWriting) const { - - // this size equals the one used in "AIAICallback::GetValue(AIVAL_LOCATE_FILE_..." - const size_t buffer_sizeMax = 2048; - char buffer[buffer_sizeMax]; - - STRCPY(buffer, " "); - STRCAT(buffer, MAP_CACHE_PATH); - std::string mapName = MakeFileSystemCompatible(ai->Getcb()->GetMapName()); - mapName.resize(mapName.size() - 4); // cut off extension - STRCAT(buffer, mapName.c_str()); - STRCAT(buffer, "-"); - const std::string mapHash = IntToString(ai->Getcb()->GetMapHash(), "%x"); - STRCAT(buffer, mapHash.c_str()); - STRCAT(buffer, ".dat"); - - if (forWriting) { - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_W, buffer); - } else { - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_R, buffer); - } - - return std::string(buffer); +std::string AAIMap::LocateMapCacheFile() const +{ + return cfg->GetFileName(cfg->getUniqueName(false, false, true, true), MAP_LEARN_PATH "maplearn-", ".dat", true); } void AAIMap::ReadMapLearnFile(bool auto_set) { - const std::string mapLearn_filename = LocateMapLearnFile(false); + const std::string mapLearn_filename = LocateMapLearnFile(); - // this size equals the one used in "AIAICallback::GetValue(AIVAL_LOCATE_FILE_..." const size_t buffer_sizeMax = 2048; char buffer[buffer_sizeMax]; @@ -1992,7 +1915,12 @@ void AAIMap::CalculateContinentMaps() // algorithm more or less by krogothe - thx very much void AAIMap::SearchMetalSpots() { - const UnitDef* def = &ai->Getbt()->GetUnitDef(ai->Getbt()->GetBiggestMex()-1); + const int unitid = ai->Getbt()->GetBiggestMex()-1; //WTF, why -1? + if (unitid <= 0) { + ai->Log("No metal extractor unit known!"); + return; + } + const UnitDef* def = &ai->Getbt()->GetUnitDef(unitid); metalMap = false; bool Stopme = false; @@ -2536,12 +2464,8 @@ void AAIMap::AddDefence(float3 *pos, int defence) } } - // this size equals the one used in "AIAICallback::GetValue(AIVAL_LOCATE_FILE_..." - static const size_t filename_sizeMax = 2048; - char filename[filename_sizeMax]; - STRCPY(filename, "AAIDefMap.txt"); - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_W, filename); - FILE* file = fopen(filename, "w+"); + const std::string filename = cfg->GetFileName("AAIDefMap.txt", "", "", true); + FILE* file = fopen(filename.c_str(), "w+"); for(int y = 0; y < yDefMapSize; ++y) { for(int x = 0; x < xDefMapSize; ++x) @@ -2681,12 +2605,8 @@ float AAIMap::GetDefenceBuildsite(float3 *best_pos, const UnitDef *def, int xSta float range = ai->Getbt()->units_static[def->id].range / 8.0; - // this size equals the one used in "AIAICallback::GetValue(AIVAL_LOCATE_FILE_..." - static const size_t filename_sizeMax = 2048; - char filename[filename_sizeMax]; - STRCPY(filename, "AAIDebug.txt"); - ai->Getcb()->GetValue(AIVAL_LOCATE_FILE_W, filename); - FILE* file = fopen(filename, "w+"); + const std::string filename = cfg->GetFileName("AAIDebug.txt", "", "", true); + FILE* file = fopen(filename.c_str(), "w+"); fprintf(file, "Search area: (%i, %i) x (%i, %i)\n", xStart, yStart, xEnd, yEnd); fprintf(file, "Range: %g\n", range); diff --git a/AI/Skirmish/AAI/AAIMap.h b/AI/Skirmish/AAI/AAIMap.h index 045ae136ad1..dd068e3ea02 100644 --- a/AI/Skirmish/AAI/AAIMap.h +++ b/AI/Skirmish/AAI/AAIMap.h @@ -207,8 +207,8 @@ class AAIMap void BuildMapPos2Pos(float3 *pos, const UnitDef* def); - std::string LocateMapLearnFile(const bool forWriting) const; - std::string LocateMapCacheFile(const bool forWriting) const; + std::string LocateMapLearnFile() const; + std::string LocateMapCacheFile() const; AAI *ai; diff --git a/AI/Skirmish/AAI/aidef.h b/AI/Skirmish/AAI/aidef.h index 2d9093baa1e..24110138145 100644 --- a/AI/Skirmish/AAI/aidef.h +++ b/AI/Skirmish/AAI/aidef.h @@ -29,7 +29,6 @@ void ReplaceExtension(const char *n, char *dst, int s, const char *ext); #define CONTINENT_DATA_VERSION "MOVEMENT_MAPS_0_87" #define AILOG_PATH "log/" -#define MAP_CACHE_PATH "cache/" #define MAP_LEARN_PATH "learn/mod/" #define MOD_LEARN_PATH "learn/mod/"