Skip to content

Commit

Permalink
AAI: code dedup / fix crash:
Browse files Browse the repository at this point in the history
- add getUniqueName():
- fix crash when no metal extractor unit is found
  • Loading branch information
abma committed Aug 13, 2014
1 parent e93a728 commit f1d12a0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 115 deletions.
30 changes: 29 additions & 1 deletion AI/Skirmish/AAI/AAIConfig.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
10 changes: 7 additions & 3 deletions AI/Skirmish/AAI/AAIConfig.h
Expand Up @@ -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);
Expand Down
136 changes: 28 additions & 108 deletions AI/Skirmish/AAI/AAIMap.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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+");
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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+");

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions AI/Skirmish/AAI/AAIMap.h
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion AI/Skirmish/AAI/aidef.h
Expand Up @@ -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/"

Expand Down

0 comments on commit f1d12a0

Please sign in to comment.