src/ai/ai_squirrel.cpp
author truebrain
Mon, 31 Mar 2008 10:55:13 +0000
branchnoai
changeset 9844 738b8f69675f
parent 9831 634100485aca
child 9851 a5f5a7cf2b61
permissions -rw-r--r--
(svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
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"
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9831
diff changeset
    11
#include "table/strings.h"
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    12
#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
    13
#include <sys/stat.h>
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
    14
9426
b90c0d1a36b7 (svn r9229) [NoAI] -Change: move more header-mess from .hpp to .cpp
truelight
parents: 9425
diff changeset
    15
#include <squirrel.h>
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    16
#include "../squirrel.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    17
#include "../squirrel_helper.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    18
#include "../squirrel_class.hpp"
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents: 9480
diff changeset
    19
#include "../squirrel_std.hpp"
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    20
#include "api/ai_controller.hpp"
9433
d439c1c469f4 (svn r9238) [NoAI] -Codechange: removed ai/api/ai_factory.h, as it isn't an api header
truelight
parents: 9431
diff changeset
    21
#include "ai_factory.hpp"
9431
346932a30fc9 (svn r9235) [NoAI] -Codechange: renamed ai/squirrel.* to ai/ai_squirrel.*
truelight
parents: 9429
diff changeset
    22
#include "ai_squirrel.hpp"
9426
b90c0d1a36b7 (svn r9229) [NoAI] -Change: move more header-mess from .hpp to .cpp
truelight
parents: 9425
diff changeset
    23
9597
db3908cf597a (svn r9630) [NoAI] -Codechange: remove now useless define.
rubidium
parents: 9596
diff changeset
    24
/* Convert all AI related classes to Squirrel data.
db3908cf597a (svn r9630) [NoAI] -Codechange: remove now useless define.
rubidium
parents: 9596
diff changeset
    25
 * Note: this line a marker in squirrel_export.sh. Do not change! */
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    26
#include "api/ai_abstractlist.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    27
#include "api/ai_accounting.hpp.sq"
9654
b836eb5c521f (svn r10556) [NoAI] -Add: added AIAirport, which can build an airport
truelight
parents: 9652
diff changeset
    28
#include "api/ai_airport.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    29
#include "api/ai_base.hpp.sq"
9792
e1222f4674c2 (svn r12294) [NoAI] -Add: added AIBridge(List), which lists all available bridges (no build yet)
truebrain
parents: 9788
diff changeset
    30
#include "api/ai_bridge.hpp.sq"
e1222f4674c2 (svn r12294) [NoAI] -Add: added AIBridge(List), which lists all available bridges (no build yet)
truebrain
parents: 9788
diff changeset
    31
#include "api/ai_bridgelist.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    32
#include "api/ai_cargo.hpp.sq"
9785
edbd185e05ed (svn r12280) [NoAI] -Add: added AICargoList (Morloth)
truebrain
parents: 9782
diff changeset
    33
#include "api/ai_cargolist.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    34
#include "api/ai_company.hpp.sq"
9743
4c44aa6a8f43 (svn r12223) [NoAI] -Fix r12221: look mom, I can break compilation!
truebrain
parents: 9742
diff changeset
    35
#include "api/ai_controller.hpp.sq"
9825
cc77111ebd85 (svn r12437) [NoAI] -Add: functions to get the current date and to determine the year/month/day from that date.
rubidium
parents: 9823
diff changeset
    36
#include "api/ai_date.hpp.sq"
9711
c8b427215c9d (svn r11295) [NoAI] -Change [API CHANGE]: put Engine things in AIEngine, and rename functions as such. Most noticable: FindBestXXXVehicle -> FineBestXXXEngine
truelight
parents: 9710
diff changeset
    37
#include "api/ai_engine.hpp.sq"
9714
fdbdae7ea647 (svn r11298) [NoAI] -Add: added EngineList + Valuators for all common functions. This should replace all FindXXXEngine, which will be removed soon
truelight
parents: 9711
diff changeset
    38
#include "api/ai_enginelist.hpp.sq"
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9831
diff changeset
    39
#include "api/ai_error.hpp.sq"
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9676
diff changeset
    40
#include "api/ai_event.hpp.sq"
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9676
diff changeset
    41
#include "api/ai_event_types.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    42
#include "api/ai_execmode.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    43
#include "api/ai_industry.hpp.sq"
9649
bc8c06513f5b (svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents: 9648
diff changeset
    44
#include "api/ai_industrylist.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    45
#include "api/ai_list.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    46
#include "api/ai_map.hpp.sq"
9691
1231d4e5f5aa (svn r10679) [NoAI] -Add: added AIMarine, which takes care of Ships, Docks, ...
truelight
parents: 9684
diff changeset
    47
#include "api/ai_marine.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    48
#include "api/ai_order.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    49
#include "api/ai_road.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    50
#include "api/ai_settings.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    51
#include "api/ai_sign.hpp.sq"
9638
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents: 9636
diff changeset
    52
#include "api/ai_station.hpp.sq"
9636
1005ffccf641 (svn r10527) [NoAI] -Add: AIStationList(), which lists all your stations; useful to maintain routes
truelight
parents: 9625
diff changeset
    53
#include "api/ai_stationlist.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    54
#include "api/ai_testmode.hpp.sq"
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents: 9616
diff changeset
    55
#include "api/ai_tile.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    56
#include "api/ai_tilelist.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    57
#include "api/ai_town.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    58
#include "api/ai_townlist.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    59
#include "api/ai_transactionmode.hpp.sq"
9794
5d866d7cb991 (svn r12298) [NoAI] -Add: added AITunnel (Morloth)
truebrain
parents: 9792
diff changeset
    60
#include "api/ai_tunnel.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    61
#include "api/ai_vehicle.hpp.sq"
9614
814c3bbf8ecc (svn r9781) [NoAI] -Add: added AIVehicleListProfit[This|Last]Year as valuator
truelight
parents: 9611
diff changeset
    62
#include "api/ai_vehiclelist.hpp.sq"
9387
4255a0a2d272 (svn r9179) [NoAI] -Add: added templates that makes adding classes to squirrel very easy
truelight
parents: 9386
diff changeset
    63
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    64
static FSquirrel iFSquirrel; ///< Tell the AI-core that we have an AI with which we like to play.
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
    65
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    66
/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    67
 * Our tiny wrapper between C++ and Squirrel.
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    68
 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    69
class AIFactorySquirrel: public AIFactoryBase {
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    70
public:
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
    71
	Squirrel *engine;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    72
	HSQOBJECT SQ_instance;
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    73
	char *script_name;
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
    74
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    75
	~AIFactorySquirrel();
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    76
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    77
	/* virtual */ const char   *GetAuthor()      { return this->engine->CallStringMethod (this->SQ_instance, "GetAuthor"); }
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    78
	/* virtual */ const char   *GetName()        { return this->engine->CallStringMethod (this->SQ_instance, "GetName"); }
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    79
	/* virtual */ const char   *GetDescription() { return this->engine->CallStringMethod (this->SQ_instance, "GetDescription"); }
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    80
	/* virtual */ int           GetVersion()     { return this->engine->CallIntegerMethod(this->SQ_instance, "GetVersion"); }
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    81
	/* virtual */ const char   *GetDate()        { return this->engine->CallStringMethod (this->SQ_instance, "GetDate"); }
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    82
	/* virtual */ AIController *CreateInstance();
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    83
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    84
	/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    85
	 * Register Squirrel script to the Factory.
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    86
	 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    87
	void RegisterSquirrel()        { this->RegisterFactory(this->GetName()); }
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    88
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    89
	/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    90
	 * Check if a method exists in the script.
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    91
	 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    92
	void CheckMethods(AIFactorySquirrel *fbase, SQInteger *res, const char *name);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    93
};
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    94
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    95
AIFactorySquirrel::~AIFactorySquirrel()
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    96
{
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    97
	free(this->script_name);
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    98
}
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    99
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   100
void AIFactorySquirrel::CheckMethods(AIFactorySquirrel *fbase, SQInteger *res, const char *name)
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   101
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   102
	if (!fbase->engine->MethodExists(fbase->SQ_instance, name)) {
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   103
		char error[1024];
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   104
		snprintf(error, sizeof(error), "Missing method '%s' for FactoryClass", name);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   105
		fbase->engine->ThrowError(error);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   106
		*res = SQ_ERROR;
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   107
	}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   108
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   109
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   110
SQInteger FSquirrel::FactoryConstructor(HSQUIRRELVM vm)
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   111
{
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   112
	AIFactorySquirrel *fbase = new AIFactorySquirrel();
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   113
	SQInteger res = 0;
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   114
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   115
	Squirrel::GetInstance(vm, &fbase->SQ_instance);
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   116
	fbase->engine = ((FSquirrel *)Squirrel::GetGlobalPointer(vm))->engine;
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   117
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   118
	/* Check if all needed fields are there */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   119
	fbase->CheckMethods(fbase, &res, "GetAuthor");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   120
	fbase->CheckMethods(fbase, &res, "GetName");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   121
	fbase->CheckMethods(fbase, &res, "GetDescription");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   122
	fbase->CheckMethods(fbase, &res, "GetVersion");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   123
	fbase->CheckMethods(fbase, &res, "GetDate");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   124
	fbase->CheckMethods(fbase, &res, "CreateInstance");
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   125
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   126
	/* Abort if one method was missing */
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   127
	if (res != 0) return res;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   128
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   129
	fbase->RegisterSquirrel();
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   130
	fbase->script_name = strdup(((FSquirrel *)Squirrel::GetGlobalPointer(vm))->current_script);
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   131
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   132
	return 0;
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   133
}
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   134
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   135
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   136
extern bool FiosIsHiddenFile(const struct dirent *ent);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   137
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   138
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   139
void FSquirrel::ScanDir(const char *dirname)
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   140
{
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   141
	struct stat sb;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   142
	struct dirent *dirent;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   143
	DIR *dir;
9726
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   144
	char d_name[MAX_PATH];
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   145
	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
   146
	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
   147
	/* 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
   148
	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
   149
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   150
	/* 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
   151
	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
   152
		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
   153
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   154
		/* Found file must be directory, but not '.' or '..' */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9617
diff changeset
   155
		if (FiosIsValidFile("ai/", dirent, &sb) && (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
   156
				(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   157
				strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   158
			/* 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
   159
			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
   160
			ttd_strlcat(script_name, PATHSEP, sizeof(script_name));
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   161
			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
   162
			ttd_strlcat(script_name, PATHSEP, sizeof(script_name));
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   163
			ttd_strlcat(script_name, "main.nut", sizeof(script_name));
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   164
			/* If it exists, load it up */
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   165
			if (FileExists(script_name)) {
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   166
				DEBUG(ai, 6, "[squirrel] Loading script '%s' for AI handling", script_name);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   167
				this->current_script = script_name;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   168
				this->engine->LoadScript(this->current_script);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   169
			}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   170
		}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   171
	}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   172
	closedir(dir);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   173
}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   174
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   175
void FSquirrel::Initializer()
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   176
{
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   177
	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
   178
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   179
	/* Create the AIFactory class, and bind the constructor */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   180
	this->engine->AddClassBegin("AIFactory");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   181
	this->engine->AddMethod("constructor", &FSquirrel::FactoryConstructor, 1, "x");
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   182
	this->engine->AddClassEnd();
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   183
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   184
	/* Set a dummy AIController, so script can load */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   185
	this->engine->AddClassBegin("AIController");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   186
	this->engine->AddClassEnd();
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   187
	/* 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
   188
	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
   189
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   190
	/* Scan the AI dir for scripts */
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   191
	this->ScanDir("ai");
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   192
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   193
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   194
FSquirrel::~FSquirrel()
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   195
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   196
	delete this->engine;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   197
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   198
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   199
AIController *AIFactorySquirrel::CreateInstance()
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   200
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   201
	const char *class_name = this->engine->CallStringMethod(this->SQ_instance, "CreateInstance");
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   202
	AIControllerSquirrel *controller = new AIControllerSquirrel(this->script_name, class_name);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   203
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   204
	return controller;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   205
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   206
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   207
void AIControllerSquirrel::RegisterClasses()
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   208
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   209
	/* Ignore AIFactory if we are really starting an AI */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   210
	this->engine->AddClassBegin("AIFactory");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   211
	this->engine->AddClassEnd();
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   212
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   213
	/* Register all classes */
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents: 9480
diff changeset
   214
	squirrel_register_std(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   215
	SQAIAbstractList_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   216
	SQAIAccounting_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   217
	SQAIAirport_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   218
	SQAIBase_Register(this->engine);
9792
e1222f4674c2 (svn r12294) [NoAI] -Add: added AIBridge(List), which lists all available bridges (no build yet)
truebrain
parents: 9788
diff changeset
   219
	SQAIBridge_Register(this->engine);
e1222f4674c2 (svn r12294) [NoAI] -Add: added AIBridge(List), which lists all available bridges (no build yet)
truebrain
parents: 9788
diff changeset
   220
	SQAIBridgeList_Register(this->engine);
e1222f4674c2 (svn r12294) [NoAI] -Add: added AIBridge(List), which lists all available bridges (no build yet)
truebrain
parents: 9788
diff changeset
   221
	SQAIBridgeList_Length_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   222
	SQAICargo_Register(this->engine);
9785
edbd185e05ed (svn r12280) [NoAI] -Add: added AICargoList (Morloth)
truebrain
parents: 9782
diff changeset
   223
	SQAICargoList_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   224
	SQAICompany_Register(this->engine);
9743
4c44aa6a8f43 (svn r12223) [NoAI] -Fix r12221: look mom, I can break compilation!
truebrain
parents: 9742
diff changeset
   225
	SQAIController_Register(this->engine);
9825
cc77111ebd85 (svn r12437) [NoAI] -Add: functions to get the current date and to determine the year/month/day from that date.
rubidium
parents: 9823
diff changeset
   226
	SQAIDate_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   227
	SQAIEngine_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   228
	SQAIEngineList_Register(this->engine);
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents: 9831
diff changeset
   229
	SQAIError_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   230
	SQAIEvent_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   231
	SQAIEventController_Register(this->engine);
9823
0b7f816cf46f (svn r12431) [NoAI] -Add: added AIEventSubsidiaryOffer, which keeps you informed about new Subsidiaries
truebrain
parents: 9814
diff changeset
   232
	SQAIEventSubsidiaryOffer_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   233
	SQAIEventTest_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   234
	SQAIEventVehicleCrash_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   235
	SQAIExecMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   236
	SQAIIndustry_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   237
	SQAIIndustryList_Register(this->engine);
9776
ee9923d46fba (svn r12270) [NoAI] -Add: added AIIndustryList_CargoAccepting and AIIndustryList_CargoProducing, which gives you Industry Lists with from where to where you can move cargo
truebrain
parents: 9771
diff changeset
   238
	SQAIIndustryList_CargoAccepting_Register(this->engine);
ee9923d46fba (svn r12270) [NoAI] -Add: added AIIndustryList_CargoAccepting and AIIndustryList_CargoProducing, which gives you Industry Lists with from where to where you can move cargo
truebrain
parents: 9771
diff changeset
   239
	SQAIIndustryList_CargoProducing_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   240
	SQAIList_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   241
	SQAIMap_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   242
	SQAIMarine_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   243
	SQAIOrder_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   244
	SQAIRoad_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   245
	SQAISettings_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   246
	SQAISign_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   247
	SQAIStation_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   248
	SQAIStationList_Register(this->engine);
9745
fb2454d70f37 (svn r12225) [NoAI] -Change [API CHANGE]: AIStationVehicleList -> AIVehicleList_Station (WATCH THE NAMES CAREFULLY!)
truebrain
parents: 9743
diff changeset
   249
	SQAIStationList_Vehicle_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   250
	SQAITestMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   251
	SQAITile_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   252
	SQAITileList_Register(this->engine);
9757
5cdc14959fb6 (svn r12242) [NoAI] -Add: added AITileList_Industry(Accepting|Producing), giving tiles on which you want to build a station for an industry
truebrain
parents: 9754
diff changeset
   253
	SQAITileList_IndustryAccepting_Register(this->engine);
5cdc14959fb6 (svn r12242) [NoAI] -Add: added AITileList_Industry(Accepting|Producing), giving tiles on which you want to build a station for an industry
truebrain
parents: 9754
diff changeset
   254
	SQAITileList_IndustryProducing_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   255
	SQAITown_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   256
	SQAITownList_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   257
	SQAITransactionMode_Register(this->engine);
9794
5d866d7cb991 (svn r12298) [NoAI] -Add: added AITunnel (Morloth)
truebrain
parents: 9792
diff changeset
   258
	SQAITunnel_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   259
	SQAIVehicle_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   260
	SQAIVehicleList_Register(this->engine);
9745
fb2454d70f37 (svn r12225) [NoAI] -Change [API CHANGE]: AIStationVehicleList -> AIVehicleList_Station (WATCH THE NAMES CAREFULLY!)
truebrain
parents: 9743
diff changeset
   261
	SQAIVehicleList_Station_Register(this->engine);
9387
4255a0a2d272 (svn r9179) [NoAI] -Add: added templates that makes adding classes to squirrel very easy
truelight
parents: 9386
diff changeset
   262
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   263
	this->engine->SetGlobalPointer(this->engine);
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   264
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   265
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   266
AIControllerSquirrel::AIControllerSquirrel(const char *script, const char *class_name)
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   267
{
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   268
	this->engine = new Squirrel();
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9779
diff changeset
   269
	this->engine->SetPrintFunction(&AIControllerSquirrel::PrintFunc);
9373
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   270
	this->RegisterClasses();
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   271
	this->engine->LoadScript(script);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   272
	/* Create the main-class */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   273
	this->engine->CreateClassInstance(class_name, this, &this->SQ_instance);
9754
ec106fe84b72 (svn r12237) [NoAI] -Fix: don't force the existance of a 'constructor' (tnx Progman)
truebrain
parents: 9753
diff changeset
   274
	if (this->engine->MethodExists(this->SQ_instance, "constructor"))
ec106fe84b72 (svn r12237) [NoAI] -Fix: don't force the existance of a 'constructor' (tnx Progman)
truebrain
parents: 9753
diff changeset
   275
		this->engine->CallMethod(this->SQ_instance, "constructor");
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   276
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   277
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   278
AIControllerSquirrel::~AIControllerSquirrel()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   279
{
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   280
	delete this->engine;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   281
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   282
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9438
diff changeset
   283
/* virtual */ void AIControllerSquirrel::Start()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   284
{
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents: 9438
diff changeset
   285
	this->engine->CallMethod(this->SQ_instance, "Start");
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   286
}
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   287
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   288
/* virtual */ void AIControllerSquirrel::Stop()
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   289
{
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   290
	this->engine->CallMethod(this->SQ_instance, "Stop");
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   291
}