Changeset - r11119:63ffa1a1ddd6
[Not reviewed]
master
0 2 0
smatz - 15 years ago 2009-02-13 02:11:54
smatz@openttd.org
(svn r15465) -Codechange: constify most of AIInfo/AIFileInfo methods, move definition of very simple getters to header file
2 files changed with 23 insertions and 68 deletions:
0 comments (0 inline, 0 general)
src/ai/ai_info.cpp
Show inline comments
 
@@ -43,58 +43,18 @@ AIFileInfo::~AIFileInfo()
 

	
 
AILibrary::~AILibrary()
 
{
 
	free((void *)this->category);
 
}
 

	
 
const char *AIFileInfo::GetAuthor()
 
{
 
	return this->author;
 
}
 

	
 
const char *AIFileInfo::GetName()
 
{
 
	return this->name;
 
}
 

	
 
const char *AIFileInfo::GetShortName()
 
{
 
	return this->short_name;
 
}
 

	
 
const char *AIFileInfo::GetDescription()
 
{
 
	return this->description;
 
}
 

	
 
int AIFileInfo::GetVersion()
 
{
 
	return this->version;
 
}
 

	
 
void AIFileInfo::GetSettings()
 
void AIFileInfo::GetSettings() const
 
{
 
	this->engine->CallMethod(*this->SQ_instance, "GetSettings", NULL, -1);
 
}
 

	
 
const char *AIFileInfo::GetDate()
 
{
 
	return this->date;
 
}
 

	
 
const char *AIFileInfo::GetInstanceName()
 
{
 
	return this->instance_name;
 
}
 

	
 
const char *AIFileInfo::GetMainScript()
 
{
 
	return this->main_script;
 
}
 

	
 
bool AIFileInfo::CheckMethod(const char *name)
 
bool AIFileInfo::CheckMethod(const char *name) const
 
{
 
	if (!this->engine->MethodExists(*this->SQ_instance, name)) {
 
		char error[1024];
 
		snprintf(error, sizeof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
 
		this->engine->ThrowError(error);
 
		return false;
 
@@ -204,13 +164,13 @@ AIInfo::~AIInfo()
 
			delete it->labels;
 
		}
 
	}
 
	this->config_list.clear();
 
}
 

	
 
bool AIInfo::CanLoadFromVersion(int version)
 
bool AIInfo::CanLoadFromVersion(int version) const
 
{
 
	if (version == -1) return true;
 
	return version >= this->min_loadable_version && version <= this->GetVersion();
 
}
 

	
 
SQInteger AIInfo::AddSetting(HSQUIRRELVM vm)
 
@@ -362,28 +322,28 @@ SQInteger AIInfo::AddLabels(HSQUIRRELVM 
 
	}
 
	sq_pop(vm, 1);
 

	
 
	return 0;
 
}
 

	
 
const AIConfigItemList *AIInfo::GetConfigList()
 
const AIConfigItemList *AIInfo::GetConfigList() const
 
{
 
	return &this->config_list;
 
}
 

	
 
const AIConfigItem *AIInfo::GetConfigItem(const char *name)
 
const AIConfigItem *AIInfo::GetConfigItem(const char *name) const
 
{
 
	for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
 
	for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
 
		if (strcmp((*it).name, name) == 0) return &(*it);
 
	}
 
	return NULL;
 
}
 

	
 
int AIInfo::GetSettingDefaultValue(const char *name)
 
int AIInfo::GetSettingDefaultValue(const char *name) const
 
{
 
	for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
 
	for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
 
		if (strcmp((*it).name, name) != 0) continue;
 
		/* The default value depends on the difficulty level */
 
		switch ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) {
 
			case 0: return (*it).easy_value;
 
			case 1: return (*it).medium_value;
 
			case 2: return (*it).hard_value;
 
@@ -413,17 +373,12 @@ int AIInfo::GetSettingDefaultValue(const
 
	/* Register the Library to the base system */
 
	library->base->RegisterLibrary(library);
 

	
 
	return 0;
 
}
 

	
 
const char *AILibrary::GetCategory()
 
{
 
	return this->category;
 
}
 

	
 
/* static */ SQInteger AILibrary::Import(HSQUIRRELVM vm)
 
{
 
	SQConvert::SQAutoFreePointers ptr;
 
	const char *library = GetParam(SQConvert::ForceType<const char *>(), vm, 2, &ptr);
 
	const char *class_name = GetParam(SQConvert::ForceType<const char *>(), vm, 3, &ptr);
 
	int version = GetParam(SQConvert::ForceType<int>(), vm, 4, &ptr);
src/ai/ai_info.hpp
Show inline comments
 
@@ -44,58 +44,58 @@ public:
 
	AIFileInfo() : SQ_instance(NULL), main_script(NULL), author(NULL), name(NULL), short_name(NULL), description(NULL), date(NULL), instance_name(NULL) {};
 
	~AIFileInfo();
 

	
 
	/**
 
	 * Get the Author of the AI.
 
	 */
 
	const char *GetAuthor();
 
	const char *GetAuthor() const { return this->author; }
 

	
 
	/**
 
	 * Get the Name of the AI.
 
	 */
 
	const char *GetName();
 
	const char *GetName() const { return this->name; }
 

	
 
	/**
 
	 * Get the 4 character long short name of the AI.
 
	 */
 
	const char *GetShortName();
 
	const char *GetShortName() const { return this->short_name; }
 

	
 
	/**
 
	 * Get the description of the AI.
 
	 */
 
	const char *GetDescription();
 
	const char *GetDescription() const { return this->description; }
 

	
 
	/**
 
	 * Get the version of the AI.
 
	 */
 
	int GetVersion();
 
	int GetVersion() const { return this->version; }
 

	
 
	/**
 
	 * Get the settings of the AI.
 
	 */
 
	void GetSettings();
 
	void GetSettings() const;
 

	
 
	/**
 
	 * Get the date of the AI.
 
	 */
 
	const char *GetDate();
 
	const char *GetDate() const { return this->date; }
 

	
 
	/**
 
	 * Get the name of the instance of the AI to create.
 
	 */
 
	const char *GetInstanceName();
 
	const char *GetInstanceName() const { return this->instance_name; }
 

	
 
	/**
 
	 * Get the filename of the main.nut script.
 
	 */
 
	const char *GetMainScript();
 
	const char *GetMainScript() const { return this->main_script; }
 

	
 
	/**
 
	 * Check if a given method exists.
 
	 */
 
	bool CheckMethod(const char *name);
 
	bool CheckMethod(const char *name) const;
 

	
 
	/**
 
	 * Process the creation of a FileInfo object.
 
	 */
 
	static SQInteger Constructor(HSQUIRRELVM vm, AIFileInfo *info, bool library);
 

	
 
@@ -125,23 +125,23 @@ public:
 
	static SQInteger Constructor(HSQUIRRELVM vm);
 
	static SQInteger DummyConstructor(HSQUIRRELVM vm);
 

	
 
	/**
 
	 * Get the config list for this AI.
 
	 */
 
	const AIConfigItemList *GetConfigList();
 
	const AIConfigItemList *GetConfigList() const;
 

	
 
	/**
 
	 * Get the description of a certain ai config option.
 
	 */
 
	const AIConfigItem *GetConfigItem(const char *name);
 
	const AIConfigItem *GetConfigItem(const char *name) const;
 

	
 
	/**
 
	 * Check if we can start this AI.
 
	 */
 
	bool CanLoadFromVersion(int version);
 
	bool CanLoadFromVersion(int version) const;
 

	
 
	/**
 
	 * Set a setting.
 
	 */
 
	SQInteger AddSetting(HSQUIRRELVM vm);
 

	
 
@@ -150,13 +150,13 @@ public:
 
	 */
 
	SQInteger AddLabels(HSQUIRRELVM vm);
 

	
 
	/**
 
	 * Get the default value for a setting.
 
	 */
 
	int GetSettingDefaultValue(const char *name);
 
	int GetSettingDefaultValue(const char *name) const;
 

	
 
private:
 
	AIConfigItemList config_list;
 
	int min_loadable_version;
 
};
 

	
 
@@ -172,13 +172,13 @@ public:
 

	
 
	static SQInteger Import(HSQUIRRELVM vm);
 

	
 
	/**
 
	 * Get the category this library is in.
 
	 */
 
	const char *GetCategory();
 
	const char *GetCategory() const { return this->category; }
 

	
 
private:
 
	const char *category;
 
};
 

	
 
#endif /* AI_INFO */
0 comments (0 inline, 0 general)