src/ai/ai_squirrel.cpp
author truebrain
Tue, 10 Jun 2008 18:59:10 +0000
branchnoai
changeset 10898 cf5ee9ede576
parent 10891 5ebb6f9068d0
child 10900 90b1d2fbb899
permissions -rw-r--r--
(svn r13449) [NoAI] -Fix: .tar support is not yet available inside the library-structure .. disable it for now
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
     1
/* $Id$ */
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
     2
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
     3
/** @file squirrel.cpp allows loading squirrel scripts to control an AI */
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
     4
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
     5
#include "../stdafx.h"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
     6
#include "../debug.h"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
     7
#include "../openttd.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9714
diff changeset
     8
#include "../string_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9714
diff changeset
     9
#include "../fileio.h"
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    10
#include "../fios.h"
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
    11
#include "../network/network.h"
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
    12
#include "../core/random_func.hpp"
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    13
#include <sys/types.h>
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    14
#include <sys/stat.h>
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
    15
9426
b90c0d1a36b7 (svn r9229) [NoAI] -Change: move more header-mess from .hpp to .cpp
truelight
parents: 9425
diff changeset
    16
#include <squirrel.h>
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    17
#include "../squirrel.hpp"
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
    18
#include "ai_info.hpp"
9431
346932a30fc9 (svn r9235) [NoAI] -Codechange: renamed ai/squirrel.* to ai/ai_squirrel.*
truelight
parents: 9429
diff changeset
    19
#include "ai_squirrel.hpp"
9426
b90c0d1a36b7 (svn r9229) [NoAI] -Change: move more header-mess from .hpp to .cpp
truelight
parents: 9425
diff changeset
    20
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    21
void AISquirrel::ScanDir(const char *dirname, bool library_dir, char *library_depth)
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
    22
{
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
    23
	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
    24
	extern bool FiosIsHiddenFile(const struct dirent *ent);
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    25
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    26
	struct stat sb;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    27
	struct dirent *dirent;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    28
	DIR *dir;
9726
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
    29
	char d_name[MAX_PATH];
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
    30
	char script_name[MAX_PATH];
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    31
	dir = ttd_opendir(dirname);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    32
	/* Dir not found, so do nothing */
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    33
	if (dir == NULL) return;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    34
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    35
	/* Walk all dirs trying to find a dir in which 'main.nut' exists */
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    36
	while ((dirent = readdir(dir)) != NULL) {
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    37
		ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    38
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    39
		/* Valid file, not '.' or '..', not hidden */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    40
		if (!FiosIsValidFile(dirname, dirent, &sb)) continue;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    41
		if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    42
		if (FiosIsHiddenFile(dirent) && strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) != 0) continue;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    43
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    44
		if (sb.st_mode & S_IFDIR) {
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    45
			/* Create the full-length script-name */
9726
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
    46
			ttd_strlcpy(script_name, dirname, sizeof(script_name));
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
    47
			ttd_strlcat(script_name, d_name, sizeof(script_name));
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
    48
			ttd_strlcat(script_name, PATHSEP, sizeof(script_name));
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    49
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    50
			if (library_dir && library_depth == NULL) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    51
				ScanDir(script_name, library_dir, d_name);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    52
				continue;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    53
			}
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    54
		}
10898
cf5ee9ede576 (svn r13449) [NoAI] -Fix: .tar support is not yet available inside the library-structure .. disable it for now
truebrain
parents: 10891
diff changeset
    55
		if (sb.st_mode & S_IFREG && !library_dir) {
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    56
			char *ext = strrchr(d_name, '.');
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    57
			if (ext != NULL && strcasecmp(ext, ".tar") != 0) continue;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    58
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    59
			/* Create the full path to the tarfile */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    60
			char tarname[MAX_PATH];
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    61
			ttd_strlcpy(tarname, dirname, sizeof(tarname));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    62
			ttd_strlcat(tarname, d_name, sizeof(tarname));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    63
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    64
			/* Now the script-name starts with the first dir in the tar */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    65
			assert(FioTarFirstDir(tarname) != NULL);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    66
			ttd_strlcpy(script_name, FioTarFirstDir(tarname), sizeof(script_name));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    67
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    68
			/* The name of the AI is the name of the tar minus the .tar */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    69
			*ext = '\0';
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    70
		}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
    71
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    72
		if (!library_dir) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    73
			/* We look for the file 'info.nut' inside the AI dir.. if it doesn't exists, it isn't an AI */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    74
			ttd_strlcat(script_name, "info.nut", sizeof(script_name));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    75
			if (FioCheckFileExists(script_name, AI_DIR)) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    76
				char load_script[MAX_PATH];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    77
				ttd_strlcpy(load_script, script_name, sizeof(load_script));
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    78
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    79
				/* Remove the 'info.nut' part and replace it with 'main.nut' */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    80
				script_name[strlen(script_name) - 8] = '\0';
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    81
				ttd_strlcat(script_name, "main.nut", sizeof(script_name));
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    82
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    83
				DEBUG(ai, 6, "[squirrel] Loading script '%s' for AI handling", load_script);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    84
				this->current_script = script_name;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    85
				this->current_dir = d_name;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    86
				this->engine->LoadScript(load_script);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    87
			}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    88
		} else {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    89
			/* We look for the file 'library.nut' inside the library dir.. */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    90
			ttd_strlcat(script_name, "library.nut", sizeof(script_name));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    91
			if (FioCheckFileExists(script_name, AI_DIR)) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    92
				char load_script[MAX_PATH];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    93
				char dir_name[MAX_PATH];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    94
				ttd_strlcpy(load_script, script_name, sizeof(load_script));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    95
				ttd_strlcpy(dir_name, library_depth, sizeof(dir_name));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    96
				ttd_strlcat(dir_name, ".", sizeof(dir_name));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    97
				ttd_strlcat(dir_name, d_name, sizeof(dir_name));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    98
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
    99
				/* Remove the 'library.nut' part and replace it with 'main.nut' */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   100
				script_name[strlen(script_name) - 11] = '\0';
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   101
				ttd_strlcat(script_name, "main.nut", sizeof(script_name));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   102
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   103
				DEBUG(ai, 6, "[squirrel] Loading script '%s' for Squirrel library", load_script);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   104
				this->current_script = script_name;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   105
				this->current_dir = dir_name;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   106
				this->engine->LoadScript(load_script);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   107
			}
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   108
		}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   109
	}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   110
	closedir(dir);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   111
}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   112
10891
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   113
void AISquirrel::ScanAIDir()
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   114
{
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   115
	char buf[MAX_PATH];
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   116
	Searchpath sp;
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   117
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   118
	FOR_ALL_SEARCHPATHS(sp) {
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   119
		FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   120
		if (FileExists(buf)) this->ScanDir(buf, false);
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   121
		ttd_strlcat(buf, "library" PATHSEP, MAX_PATH);
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   122
		if (FileExists(buf)) this->ScanDir(buf, true);
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   123
	}
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   124
}
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   125
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   126
void AISquirrel::RescanAIDir()
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   127
{
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   128
	extern void ScanForTarFiles();
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   129
	ScanForTarFiles();
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   130
	this->ScanAIDir();
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   131
}
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   132
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   133
AISquirrel::AISquirrel()
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   134
{
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   135
	this->library_instance_count = 0;
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   136
	this->engine = new Squirrel();
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   137
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   138
	/* Create the AIInfo class, and add the RegisterAI function */
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   139
	this->engine->AddClassBegin("AIInfo");
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   140
	this->engine->AddClassEnd();
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   141
	this->engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   142
	/* Create the AILibrary class, and add the RegisterLibrary function */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   143
	this->engine->AddClassBegin("AILibrary");
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   144
	this->engine->AddClassEnd();
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   145
	this->engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   146
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   147
	/* Mark this class as global pointer */
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   148
	this->engine->SetGlobalPointer(this);
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   149
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   150
	/* Scan the AI dir for scripts */
10891
5ebb6f9068d0 (svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
glx
parents: 10889
diff changeset
   151
	this->ScanAIDir();
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   152
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   153
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   154
AISquirrel::~AISquirrel()
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   155
{
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   156
	AIInfoList::iterator it = this->info_list.begin();
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   157
	for (; it != this->info_list.end(); it++) {
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   158
		AIInfo *i = (*it).second;
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   159
		delete i;
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   160
	}
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   161
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   162
	delete this->engine;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   163
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   164
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   165
bool AISquirrel::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm)
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   166
{
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   167
	AILibraryList::iterator iter = this->library_list.find(library);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   168
	/* Check if the library exists */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   169
	if (iter == this->library_list.end()) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   170
		char error[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   171
		snprintf(error, sizeof(error), "couldn't find library '%s'", library);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   172
		sq_throwerror(vm, OTTD2FS(error));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   173
		return false;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   174
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   175
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   176
	/* Check if the version matches */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   177
	if ((*iter).second->GetVersion() != version) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   178
		char error[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   179
		snprintf(error, sizeof(error), "this AI is expected library '%s' to be version %d, but it is version %d", library, version, (*iter).second->GetVersion());
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   180
		sq_throwerror(vm, OTTD2FS(error));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   181
		return false;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   182
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   183
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   184
	/* Create a fake internal name */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   185
	char fake_class[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   186
	snprintf(fake_class, sizeof(fake_class), "_internalNA%d", ++this->library_instance_count);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   187
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   188
	/* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   189
	sq_pushroottable(vm);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   190
	sq_pushstring(vm, OTTD2FS(fake_class), -1);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   191
	sq_newclass(vm, SQFalse);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   192
	/* Load the library */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   193
	if (!Squirrel::LoadScript(vm, (*iter).second->GetScriptName(), false)) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   194
		char error[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   195
		snprintf(error, sizeof(error), "there was a compile error when importing '%s'", library);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   196
		sq_throwerror(vm, OTTD2FS(error));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   197
		return false;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   198
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   199
	/* Create the fake class */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   200
	sq_newslot(vm, -3, SQFalse);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   201
	sq_pop(vm, 1);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   202
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   203
	/* Find the real class inside the fake class (like 'sets.Vector') */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   204
	sq_pushroottable(vm);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   205
	sq_pushstring(vm, OTTD2FS(fake_class), -1);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   206
	if (SQ_FAILED(sq_get(vm, -2))) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   207
		sq_throwerror(vm, _SC("internal error assigning library class"));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   208
		return false;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   209
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   210
	sq_pushstring(vm, OTTD2FS((*iter).second->GetInstanceName()), -1);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   211
	if (SQ_FAILED(sq_get(vm, -2))) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   212
		char error[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   213
		snprintf(error, sizeof(error), "unable to find class '%s' in the library '%s'", (*iter).second->GetInstanceName(), library);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   214
		sq_throwerror(vm, OTTD2FS(error));
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   215
		return false;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   216
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   217
	HSQOBJECT obj;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   218
	sq_getstackobj(vm, -1, &obj);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   219
	sq_pop(vm, 3);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   220
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   221
	/* Now link the name the user wanted to our 'fake' class */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   222
	sq_pushroottable(vm);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   223
	sq_pushstring(vm, OTTD2FS(class_name), -1);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   224
	sq_pushobject(vm, obj);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   225
	sq_newclass(vm, SQTrue);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   226
	sq_newslot(vm, -3, SQFalse);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   227
	sq_pop(vm, 1);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   228
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   229
	return true;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   230
}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   231
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   232
void AISquirrel::RegisterLibrary(AILibrary *library)
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   233
{
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   234
	const char *ai_name = library->GetDirName();
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   235
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   236
	/* Check if we register twice; than the first always wins */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   237
	if (this->library_list.find(ai_name) != this->library_list.end()) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   238
		/* In case they are not the same dir, give a warning */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   239
		if (strcasecmp(library->GetScriptName(), this->library_list[ai_name]->GetScriptName()) != 0) {
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   240
			DEBUG(ai, 0, "Registering two Libraries with the same name");
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   241
			DEBUG(ai, 0, "  1: %s", this->library_list[ai_name]->GetScriptName());
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   242
			DEBUG(ai, 0, "  2: %s", library->GetScriptName());
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   243
			DEBUG(ai, 0, "The first is taking precedence");
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   244
		}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   245
		/* Delete the new AILibrary, as we will be using the old one */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   246
		delete library;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   247
		return;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   248
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   249
	this->library_list[ai_name] = library;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   250
}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   251
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   252
void AISquirrel::RegisterAI(AIInfo *info)
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   253
{
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   254
	const char *ai_name = info->GetDirName();
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   255
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   256
	/* Check if we register twice; than the first always wins */
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   257
	if (this->info_list.find(ai_name) != this->info_list.end()) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   258
		/* In case they are not the same dir, give a warning */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   259
		if (strcasecmp(info->GetScriptName(), this->info_list[ai_name]->GetScriptName()) != 0) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   260
			DEBUG(ai, 0, "Registering two AIs with the same name");
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   261
			DEBUG(ai, 0, "  1: %s", this->info_list[ai_name]->GetScriptName());
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   262
			DEBUG(ai, 0, "  2: %s", info->GetScriptName());
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   263
			DEBUG(ai, 0, "The first is taking precedence");
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   264
		}
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   265
		/* Delete the new AIInfo, as we will be using the old one */
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
   266
		delete info;
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   267
		return;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   268
	}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10643
diff changeset
   269
	this->info_list[ai_name] = info;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   270
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   271
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   272
void AISquirrel::UnregisterAI(AIInfo *info)
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   273
{
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   274
	this->info_list.erase(info->GetDirName());
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   275
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   276
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   277
AIInfo *AISquirrel::SelectRandomAI()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   278
{
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   279
	if (this->info_list.size() == 0) return NULL;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   280
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   281
	/* Find a random AI */
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   282
	uint pos;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   283
	if (_networking) pos = InteractiveRandomRange(this->info_list.size());
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   284
	else             pos =            RandomRange(this->info_list.size());
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   285
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   286
	/* Find the Nth item from the array */
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   287
	AIInfoList::iterator it = this->info_list.begin();
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   288
	for (; pos > 0; pos--) it++;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   289
	AIInfoList::iterator first_it = it;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   290
	AIInfo *i = (*it).second;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   291
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   292
	if (!i->AllowStartup()) {
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   293
		/* We can't start this AI, try to find the next best */
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   294
		do {
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   295
			it++;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   296
			if (it == this->info_list.end()) it = this->info_list.begin();
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   297
			/* Back at the beginning? We can't start an AI. */
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   298
			if (first_it == it) return NULL;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   299
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   300
			i = (*it).second;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   301
		} while (!i->AllowStartup());
10386
f6ce140b5ffd (svn r12928) [NoAI] -Fix: don't crash when reloading an AI that has a compile-error (bug by Fingon)
truebrain
parents: 10370
diff changeset
   302
	}
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   303
	return i;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   304
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   305
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   306
AIInfo *AISquirrel::SelectAI(const char *name)
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   307
{
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   308
	if (this->info_list.size() == 0) return NULL;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   309
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   310
	AIInfoList::iterator it = this->info_list.begin();
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   311
	for (; it != this->info_list.end(); it++) {
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   312
		AIInfo *i = (*it).second;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   313
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   314
		if (strcasecmp(name, (*it).first) == 0 && i->AllowStartup()) {
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   315
			return i;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   316
		}
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   317
	}
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   318
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   319
	return NULL;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   320
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   321
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   322
char *AISquirrel::GetAIConsoleList(char *p, const char *last)
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   323
{
10643
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   324
	p += snprintf(p, last - p, "List of AIs:\n");
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   325
	AIInfoList::iterator it = this->info_list.begin();
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   326
	for (; it != this->info_list.end(); it++) {
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   327
		AIInfo *i = (*it).second;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   328
		if (!i->AllowStartup()) continue;
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   329
		p += snprintf(p, last - p, "%10s: %s\n", (*it).first, i->GetDescription());
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   330
	}
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   331
	p += snprintf(p, last - p, "\n");
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   332
970417eef395 (svn r13187) [NoAI] -Codechange: rewrote the AI register part; C++ AIs are completely impossible now, and registration happens directly via AISquirrel. This cleans up the registration flow, and makes everything readable again, as sure I made a mess out of it :)
truebrain
parents: 10386
diff changeset
   333
	return p;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   334
}