src/ai/ai_squirrel.cpp
author truebrain
Thu, 12 Jun 2008 19:47:02 +0000
branchnoai
changeset 10942 cd3f2d07199f
parent 10900 90b1d2fbb899
child 10958 65088d587094
permissions -rw-r--r--
(svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
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, '.');
10900
90b1d2fbb899 (svn r13451) [NoAI] -Fix: no comments ...
truebrain
parents: 10898
diff changeset
    57
			if (ext == NULL || strcasecmp(ext, ".tar") != 0) continue;
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
    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
10942
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   188
	/* Get the current table/class we belong to */
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   189
	HSQOBJECT parent;
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   190
	sq_getstackobj(vm, 1, &parent);
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   191
10889
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 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
   193
	sq_pushroottable(vm);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   194
	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
   195
	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
   196
	/* Load the library */
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   197
	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
   198
		char error[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   199
		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
   200
		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
   201
		return false;
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
	/* 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
   204
	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
   205
	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
   206
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   207
	/* 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
   208
	sq_pushroottable(vm);
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   209
	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
   210
	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
   211
		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
   212
		return false;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   213
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   214
	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
   215
	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
   216
		char error[1024];
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   217
		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
   218
		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
   219
		return false;
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
	HSQOBJECT obj;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   222
	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
   223
	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
   224
10942
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   225
	if (StrEmpty(class_name)) {
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   226
		sq_pushobject(vm, obj);
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   227
		return true;
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   228
	}
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   229
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   230
	/* Now link the name the user wanted to our 'fake' class */
10942
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   231
	sq_pushobject(vm, parent);
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   232
	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
   233
	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
   234
	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
   235
	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
   236
	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
   237
10942
cd3f2d07199f (svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents: 10900
diff changeset
   238
	sq_pushobject(vm, obj);
10889
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   239
	return true;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   240
}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   241
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   242
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
   243
{
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   244
	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
   245
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   246
	/* 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
   247
	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
   248
		/* 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
   249
		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
   250
			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
   251
			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
   252
			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
   253
			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
   254
		}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   255
		/* 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
   256
		delete library;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   257
		return;
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   258
	}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   259
	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
   260
}
ecb77cfc4a10 (svn r13440) [NoAI] -Add: introducing ai/library, a method to load libraries into your AI.
truebrain
parents: 10872
diff changeset
   261
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
   262
void AISquirrel::RegisterAI(AIInfo *info)
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   263
{
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
	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
   265
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
   266
	/* 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
   267
	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
   268
		/* 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
   269
		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
   270
			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
   271
			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
   272
			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
   273
			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
   274
		}
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
   275
		/* 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
   276
		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
   277
		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
   278
	}
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
   279
	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
   280
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   281
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
   282
void AISquirrel::UnregisterAI(AIInfo *info)
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   283
{
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
   284
	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
   285
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   286
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
   287
AIInfo *AISquirrel::SelectRandomAI()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   288
{
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
   289
	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
   290
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
	/* 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
   292
	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
   293
	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
   294
	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
   295
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
	/* 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
   297
	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
   298
	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
   299
	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
   300
	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
   301
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
   302
	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
   303
		/* 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
   304
		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
   305
			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
   306
			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
   307
			/* 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
   308
			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
   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
			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
   311
		} 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
   312
	}
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
   313
	return i;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   314
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   315
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
   316
AIInfo *AISquirrel::SelectAI(const char *name)
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   317
{
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
   318
	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
   319
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
   320
	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
   321
	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
   322
		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
   323
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
		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
   325
			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
   326
		}
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
	}
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
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
	return NULL;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   330
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   331
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
   332
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
   333
{
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
   334
	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
   335
	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
   336
	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
   337
		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
   338
		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
   339
		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
   340
	}
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
   341
	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
   342
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
   343
	return p;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   344
}