src/ai/ai_squirrel.hpp
author truebrain
Fri, 13 Jun 2008 20:19:00 +0000
branchnoai
changeset 10958 65088d587094
parent 10891 5ebb6f9068d0
permissions -rw-r--r--
(svn r13512) [NoAI] -Fix: don't load a library over and over, but keep track of which libraries we have loaded (per AI) and re-use it where possible (reduces memory-footprint)
[NoAI] -Fix: change the fake-library-name-counter to a per AI value, not global
[NoAI] -Fix: Load the script inside the thread, not in the main thread. This avoids unneeded error-handling
/* $Id$ */

/** @file squirrel.hpp declarations of the class for squirrel loader */

#ifndef AI_SQUIRREL_HPP
#define AI_SQUIRREL_HPP

#include <map>
#ifndef AI_CONTROLLER_HPP
struct ltstr { bool operator()(const char *s1, const char *s2) const { return strcmp(s1, s2) < 0; } };
#endif /* AI_CONTROLLER_HPP */

class AISquirrel {
public:
	AISquirrel();
	~AISquirrel();

	/**
	 * Import a library inside the Squirrel VM.
	 */
	bool ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm, AIController *controller);

	/**
	 * Register a library to be put in the available list.
	 */
	void RegisterLibrary(class AILibrary *library);

	/**
	 * Register an AI to be put in the available list.
	 */
	void RegisterAI(class AIInfo *info);

	/**
	 * Remove an AI from the available list.
	 */
	void UnregisterAI(class AIInfo *info);

	/**
	 * Select a Random AI.
	 */
	class AIInfo *SelectRandomAI();

	/**
	 * Select an AI by name.
	 */
	class AIInfo *SelectAI(const char *name);

	/**
	 * Get the list of available AIs for the console.
	 */
	char *GetAIConsoleList(char *p, const char *last);

	/**
	 * Get the engine of the main squirrel handler (it indexes all avialable squirrels).
	 */
	class Squirrel *GetEngine() { return this->engine; }

	/**
	 * Get the current script the ScanDir is looking at.
	 */
	const char *GetCurrentScript() { return this->current_script; }

	/**
	 * Get the directory of the current script the ScanDir is looking at.
	 */
	const char *GetCurrentDirName() { return this->current_dir; }

	/**
	 * Rescan the AI dir for scripts.
	 */
	void RescanAIDir();

private:
	typedef std::map<const char *, class AIInfo *, ltstr> AIInfoList;
	typedef std::map<const char *, class AILibrary *, ltstr> AILibraryList;

	/**
	 * Scan the AI dir for scripts.
	 */
	void ScanAIDir();

	/**
	 * Scan a dir for AIs.
	 *  For non-library-scan, if an AI is found, AIInfo is created, and the AI
	 *    is registered to the central system.
	 *  For library-scan, if a library is found, AILibrary is created, and the
	 *    library is registered to the central system.
	 */
	void ScanDir(const char *dirname, bool library_dir, char *library_depth = NULL);

	AIInfoList info_list;
	AILibraryList library_list;
	class Squirrel *engine;
	char *current_script;
	char *current_dir;
};

#endif /* AI_SQUIRREL_HPP */