src/ai/ai_squirrel.cpp
author truebrain
Fri, 25 Apr 2008 15:29:44 +0000
branchnoai
changeset 10358 73d8177db39e
parent 10344 b7e9f5c65e30
child 10370 fa4f88090694
permissions -rw-r--r--
(svn r12899) [NoAI] -Add: added AIEventCompany(New|Merger|InTrouble|Bankrupt) (Yexo)
[NoAI] -Fix: don't export constructor for AIEventTypes, as you shouldn't do it via SQ
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"
9851
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9844
diff changeset
    46
#include "api/ai_log.hpp.sq"
9596
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    47
#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
    48
#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
    49
#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
    50
#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
    51
#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
    52
#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
    53
#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
    54
#include "api/ai_stationlist.hpp.sq"
10344
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
    55
#include "api/ai_subsidy.hpp.sq"
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
    56
#include "api/ai_subsidylist.hpp.sq"
9596
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_testmode.hpp.sq"
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents: 9616
diff changeset
    58
#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
    59
#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
    60
#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
    61
#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
    62
#include "api/ai_transactionmode.hpp.sq"
9794
5d866d7cb991 (svn r12298) [NoAI] -Add: added AITunnel (Morloth)
truebrain
parents: 9792
diff changeset
    63
#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
    64
#include "api/ai_vehicle.hpp.sq"
9614
814c3bbf8ecc (svn r9781) [NoAI] -Add: added AIVehicleListProfit[This|Last]Year as valuator
truelight
parents: 9611
diff changeset
    65
#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
    66
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    67
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
    68
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    69
/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    70
 * 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
    71
 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    72
class AIFactorySquirrel: public AIFactoryBase {
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    73
public:
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
    74
	Squirrel *engine;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    75
	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
    76
	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
    77
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    78
	~AIFactorySquirrel();
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    79
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    80
	/* 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
    81
	/* 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
    82
	/* 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
    83
	/* 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
    84
	/* 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
    85
	/* virtual */ AIController *CreateInstance();
9404
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
	/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    88
	 * Register Squirrel script to the Factory.
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
	void RegisterSquirrel()        { this->RegisterFactory(this->GetName()); }
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
	/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    93
	 * 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
    94
	 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    95
	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
    96
};
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    97
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    98
AIFactorySquirrel::~AIFactorySquirrel()
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    99
{
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   100
	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
   101
}
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   102
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   103
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
   104
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   105
	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
   106
		char error[1024];
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   107
		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
   108
		fbase->engine->ThrowError(error);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   109
		*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
   110
	}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   111
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   112
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   113
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
   114
{
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   115
	AIFactorySquirrel *fbase = new AIFactorySquirrel();
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   116
	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
   117
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   118
	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
   119
	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
   120
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   121
	/* 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
   122
	fbase->CheckMethods(fbase, &res, "GetAuthor");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   123
	fbase->CheckMethods(fbase, &res, "GetName");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   124
	fbase->CheckMethods(fbase, &res, "GetDescription");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   125
	fbase->CheckMethods(fbase, &res, "GetVersion");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   126
	fbase->CheckMethods(fbase, &res, "GetDate");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   127
	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
   128
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   129
	/* 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
   130
	if (res != 0) return res;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   131
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   132
	fbase->RegisterSquirrel();
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   133
	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
   134
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   135
	return 0;
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   136
}
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   137
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   138
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
   139
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
   140
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   141
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   142
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
   143
{
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   144
	struct stat sb;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   145
	struct dirent *dirent;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   146
	DIR *dir;
9726
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   147
	char d_name[MAX_PATH];
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   148
	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
   149
	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
   150
	/* 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
   151
	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
   152
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   153
	/* 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
   154
	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
   155
		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
   156
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   157
		/* Found file must be directory, but not '.' or '..' */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9617
diff changeset
   158
		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
   159
				(!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
   160
				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
   161
			/* 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
   162
			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
   163
			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
   164
			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
   165
			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
   166
			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
   167
			/* 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
   168
			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
   169
				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
   170
				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
   171
				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
   172
			}
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
	}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   175
	closedir(dir);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   176
}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   177
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   178
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
   179
{
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   180
	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
   181
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   182
	/* 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
   183
	this->engine->AddClassBegin("AIFactory");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   184
	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
   185
	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
   186
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   187
	/* 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
   188
	this->engine->AddClassBegin("AIController");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   189
	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
   190
	/* 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
   191
	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
   192
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   193
	/* 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
   194
	this->ScanDir("ai");
9404
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
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   197
FSquirrel::~FSquirrel()
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
	delete this->engine;
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
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   202
AIController *AIFactorySquirrel::CreateInstance()
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
	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
   205
	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
   206
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   207
	return controller;
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
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   210
void AIControllerSquirrel::RegisterClasses()
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   211
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   212
	/* 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
   213
	this->engine->AddClassBegin("AIFactory");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   214
	this->engine->AddClassEnd();
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   215
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   216
	/* 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
   217
	squirrel_register_std(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   218
	SQAIAbstractList_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   219
	SQAIAccounting_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   220
	SQAIAirport_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   221
	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
   222
	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
   223
	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
   224
	SQAIBridgeList_Length_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   225
	SQAICargo_Register(this->engine);
9785
edbd185e05ed (svn r12280) [NoAI] -Add: added AICargoList (Morloth)
truebrain
parents: 9782
diff changeset
   226
	SQAICargoList_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   227
	SQAICompany_Register(this->engine);
9743
4c44aa6a8f43 (svn r12223) [NoAI] -Fix r12221: look mom, I can break compilation!
truebrain
parents: 9742
diff changeset
   228
	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
   229
	SQAIDate_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   230
	SQAIEngine_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   231
	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
   232
	SQAIError_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   233
	SQAIEvent_Register(this->engine);
10358
73d8177db39e (svn r12899) [NoAI] -Add: added AIEventCompany(New|Merger|InTrouble|Bankrupt) (Yexo)
truebrain
parents: 10344
diff changeset
   234
	SQAIEventCompanyBankrupt_Register(this->engine);
73d8177db39e (svn r12899) [NoAI] -Add: added AIEventCompany(New|Merger|InTrouble|Bankrupt) (Yexo)
truebrain
parents: 10344
diff changeset
   235
	SQAIEventCompanyInTrouble_Register(this->engine);
73d8177db39e (svn r12899) [NoAI] -Add: added AIEventCompany(New|Merger|InTrouble|Bankrupt) (Yexo)
truebrain
parents: 10344
diff changeset
   236
	SQAIEventCompanyMerger_Register(this->engine);
73d8177db39e (svn r12899) [NoAI] -Add: added AIEventCompany(New|Merger|InTrouble|Bankrupt) (Yexo)
truebrain
parents: 10344
diff changeset
   237
	SQAIEventCompanyNew_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   238
	SQAIEventController_Register(this->engine);
10297
433b21fd002c (svn r12838) [NoAI] -Add: added AIEventEnginePreview, which tells you when you have been chosen for testing an engine, and allows you to accept it
glx
parents: 10086
diff changeset
   239
	SQAIEventEnginePreview_Register(this->engine);
10344
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
   240
	SQAIEventSubsidyAwarded_Register(this->engine);
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
   241
	SQAIEventSubsidyExpired_Register(this->engine);
10086
6497ef78f031 (svn r12610) [NoAI] -Fix: a subsidiary is a 'subcompany' and not the financial bonus for routes which is called a subsidy.
rubidium
parents: 9851
diff changeset
   242
	SQAIEventSubsidyOffer_Register(this->engine);
10344
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
   243
	SQAIEventSubsidyOfferExpired_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   244
	SQAIEventTest_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   245
	SQAIEventVehicleCrash_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   246
	SQAIExecMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   247
	SQAIIndustry_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   248
	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
   249
	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
   250
	SQAIIndustryList_CargoProducing_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   251
	SQAIList_Register(this->engine);
9851
a5f5a7cf2b61 (svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
truebrain
parents: 9844
diff changeset
   252
	SQAILog_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   253
	SQAIMap_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   254
	SQAIMarine_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   255
	SQAIOrder_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   256
	SQAIRoad_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   257
	SQAISettings_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   258
	SQAISign_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   259
	SQAIStation_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   260
	SQAIStationList_Register(this->engine);
9745
fb2454d70f37 (svn r12225) [NoAI] -Change [API CHANGE]: AIStationVehicleList -> AIVehicleList_Station (WATCH THE NAMES CAREFULLY!)
truebrain
parents: 9743
diff changeset
   261
	SQAIStationList_Vehicle_Register(this->engine);
10344
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
   262
	SQAISubsidy_Register(this->engine);
b7e9f5c65e30 (svn r12885) [NoAI] -Add: added AISubsidy and AIEventSubsidyNNN (Yexo)
truebrain
parents: 10297
diff changeset
   263
	SQAISubsidyList_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   264
	SQAITestMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   265
	SQAITile_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   266
	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
   267
	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
   268
	SQAITileList_IndustryProducing_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   269
	SQAITown_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   270
	SQAITownList_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   271
	SQAITransactionMode_Register(this->engine);
9794
5d866d7cb991 (svn r12298) [NoAI] -Add: added AITunnel (Morloth)
truebrain
parents: 9792
diff changeset
   272
	SQAITunnel_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   273
	SQAIVehicle_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   274
	SQAIVehicleList_Register(this->engine);
9745
fb2454d70f37 (svn r12225) [NoAI] -Change [API CHANGE]: AIStationVehicleList -> AIVehicleList_Station (WATCH THE NAMES CAREFULLY!)
truebrain
parents: 9743
diff changeset
   275
	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
   276
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   277
	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
   278
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   279
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   280
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
   281
{
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   282
	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
   283
	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
   284
	this->RegisterClasses();
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   285
	this->engine->LoadScript(script);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   286
	/* Create the main-class */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   287
	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
   288
	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
   289
		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
   290
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   291
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   292
AIControllerSquirrel::~AIControllerSquirrel()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   293
{
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   294
	delete this->engine;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   295
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   296
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
   297
/* virtual */ void AIControllerSquirrel::Start()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   298
{
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
   299
	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
   300
}
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   301
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   302
/* 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
   303
{
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   304
	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
   305
}