src/ai/ai_squirrel.cpp
author truebrain
Tue, 26 Feb 2008 23:47:37 +0000
branchnoai
changeset 9788 e9dbd4ec1784
parent 9785 edbd185e05ed
child 9792 e1222f4674c2
permissions -rw-r--r--
(svn r12284) [NoAI] -Add: added AICargoList_v(IsFreight|CargoIncomes) (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"
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
    11
#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
    12
#include <sys/stat.h>
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
    13
9426
b90c0d1a36b7 (svn r9229) [NoAI] -Change: move more header-mess from .hpp to .cpp
truelight
parents: 9425
diff changeset
    14
#include <squirrel.h>
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    15
#include "../squirrel.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    16
#include "../squirrel_helper.hpp"
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    17
#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
    18
#include "../squirrel_std.hpp"
9429
25b7d020a3a9 (svn r9232) [NoAI] -Fix r9230: incode update about file/dir moving
truelight
parents: 9427
diff changeset
    19
#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
    20
#include "ai_factory.hpp"
9431
346932a30fc9 (svn r9235) [NoAI] -Codechange: renamed ai/squirrel.* to ai/ai_squirrel.*
truelight
parents: 9429
diff changeset
    21
#include "ai_squirrel.hpp"
9426
b90c0d1a36b7 (svn r9229) [NoAI] -Change: move more header-mess from .hpp to .cpp
truelight
parents: 9425
diff changeset
    22
9597
db3908cf597a (svn r9630) [NoAI] -Codechange: remove now useless define.
rubidium
parents: 9596
diff changeset
    23
/* Convert all AI related classes to Squirrel data.
db3908cf597a (svn r9630) [NoAI] -Codechange: remove now useless define.
rubidium
parents: 9596
diff changeset
    24
 * 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
    25
#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
    26
#include "api/ai_accounting.hpp.sq"
9654
b836eb5c521f (svn r10556) [NoAI] -Add: added AIAirport, which can build an airport
truelight
parents: 9652
diff changeset
    27
#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
    28
#include "api/ai_base.hpp.sq"
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_cargo.hpp.sq"
9785
edbd185e05ed (svn r12280) [NoAI] -Add: added AICargoList (Morloth)
truebrain
parents: 9782
diff changeset
    30
#include "api/ai_cargolist.hpp.sq"
9788
e9dbd4ec1784 (svn r12284) [NoAI] -Add: added AICargoList_v(IsFreight|CargoIncomes) (Morloth)
truebrain
parents: 9785
diff changeset
    31
#include "api/ai_cargolist_valuator.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_company.hpp.sq"
9743
4c44aa6a8f43 (svn r12223) [NoAI] -Fix r12221: look mom, I can break compilation!
truebrain
parents: 9742
diff changeset
    33
#include "api/ai_controller.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
    34
#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
    35
#include "api/ai_enginelist.hpp.sq"
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
    36
#include "api/ai_enginelist_valuator.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
    37
#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
    38
#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
    39
#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
    40
#include "api/ai_industry.hpp.sq"
9649
bc8c06513f5b (svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents: 9648
diff changeset
    41
#include "api/ai_industrylist.hpp.sq"
bc8c06513f5b (svn r10544) [NoAI] -Add: added AIIndustryList to list industries
truelight
parents: 9648
diff changeset
    42
#include "api/ai_industrylist_valuator.hpp.sq"
9596
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_list.hpp.sq"
9660
d0a430e8310b (svn r10569) [NoAI] -Add: added AIListRandomize as Valuator for all lists to attach a random value to all items
truelight
parents: 9657
diff changeset
    44
#include "api/ai_list_valuator.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_map.hpp.sq"
9691
1231d4e5f5aa (svn r10679) [NoAI] -Add: added AIMarine, which takes care of Ships, Docks, ...
truelight
parents: 9684
diff changeset
    46
#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
    47
#include "api/ai_order.hpp.sq"
9652
c15bf5355b95 (svn r10553) [NoAI] -Add: added a framework for AIPathFinder() and implemented a really stupid AIPathFinderStupid()
truelight
parents: 9649
diff changeset
    48
#include "api/ai_pathfinder.hpp.sq"
c15bf5355b95 (svn r10553) [NoAI] -Add: added a framework for AIPathFinder() and implemented a really stupid AIPathFinderStupid()
truelight
parents: 9649
diff changeset
    49
#include "api/ai_pathfinder_stupid.hpp.sq"
9596
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"
1005ffccf641 (svn r10527) [NoAI] -Add: AIStationList(), which lists all your stations; useful to maintain routes
truelight
parents: 9625
diff changeset
    55
#include "api/ai_stationlist_valuator.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_testmode.hpp.sq"
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents: 9616
diff changeset
    57
#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
    58
#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
    59
#include "api/ai_tilelist_valuator.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_townlist_valuator.hpp.sq"
8af5a1399842 (svn r9629) [NoAI] -Codechange: move the squirrel export functions out of the API header files.
rubidium
parents: 9594
diff changeset
    63
#include "api/ai_transactionmode.hpp.sq"
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"
814c3bbf8ecc (svn r9781) [NoAI] -Add: added AIVehicleListProfit[This|Last]Year as valuator
truelight
parents: 9611
diff changeset
    66
#include "api/ai_vehiclelist_valuator.hpp.sq"
9387
4255a0a2d272 (svn r9179) [NoAI] -Add: added templates that makes adding classes to squirrel very easy
truelight
parents: 9386
diff changeset
    67
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    68
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
    69
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    70
/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    71
 * 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
    72
 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    73
class AIFactorySquirrel: public AIFactoryBase {
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    74
public:
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
    75
	Squirrel *engine;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    76
	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
    77
	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
    78
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    79
	~AIFactorySquirrel();
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    80
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   *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
    82
	/* 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
    83
	/* 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
    84
	/* 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
    85
	/* 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
    86
	/* virtual */ AIController *CreateInstance();
9404
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
	/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    89
	 * Register Squirrel script to the Factory.
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    90
	 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    91
	void RegisterSquirrel()        { this->RegisterFactory(this->GetName()); }
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
	/**
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    94
	 * 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
    95
	 */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    96
	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
    97
};
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
    98
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
    99
AIFactorySquirrel::~AIFactorySquirrel()
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   100
{
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   101
	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
   102
}
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   103
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   104
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
   105
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   106
	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
   107
		char error[1024];
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   108
		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
   109
		fbase->engine->ThrowError(error);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   110
		*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
   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
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   113
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   114
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
   115
{
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   116
	AIFactorySquirrel *fbase = new AIFactorySquirrel();
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   117
	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
   118
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   119
	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
   120
	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
   121
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   122
	/* 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
   123
	fbase->CheckMethods(fbase, &res, "GetAuthor");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   124
	fbase->CheckMethods(fbase, &res, "GetName");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   125
	fbase->CheckMethods(fbase, &res, "GetDescription");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   126
	fbase->CheckMethods(fbase, &res, "GetVersion");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   127
	fbase->CheckMethods(fbase, &res, "GetDate");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   128
	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
   129
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   130
	/* 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
   131
	if (res != 0) return res;
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   132
9410
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   133
	fbase->RegisterSquirrel();
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   134
	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
   135
286fcd8edc64 (svn r9208) [NoAI] -Add: keep track of which script added which class, so we know
truelight
parents: 9407
diff changeset
   136
	return 0;
9385
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   137
}
56803dfb2ff6 (svn r9176) [NoAI] -Add: added a RegisterAI() proc, that makes the AI known to the
truelight
parents: 9381
diff changeset
   138
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   139
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
   140
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
   141
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   142
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   143
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
   144
{
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   145
	struct stat sb;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   146
	struct dirent *dirent;
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   147
	DIR *dir;
9726
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   148
	char d_name[MAX_PATH];
f80f1d0bae90 (svn r12112) [NoAI] -Fix: prevent buffer overruns when creating full-length script-name
glx
parents: 9723
diff changeset
   149
	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
   150
	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
   151
	/* 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
   152
	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
   153
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   154
	/* 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
   155
	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
   156
		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
   157
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   158
		/* Found file must be directory, but not '.' or '..' */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9617
diff changeset
   159
		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
   160
				(!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
   161
				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
   162
			/* 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
   163
			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
   164
			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
   165
			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
   166
			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
   167
			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
   168
			/* 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
   169
			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
   170
				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
   171
				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
   172
				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
   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
	}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   176
	closedir(dir);
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   177
}
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   178
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   179
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
   180
{
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   181
	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
   182
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   183
	/* 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
   184
	this->engine->AddClassBegin("AIFactory");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   185
	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
   186
	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
   187
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   188
	/* 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
   189
	this->engine->AddClassBegin("AIController");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   190
	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
   191
	/* 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
   192
	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
   193
9411
29c9dfcdb8e9 (svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
truelight
parents: 9410
diff changeset
   194
	/* 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
   195
	this->ScanDir("ai");
9404
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
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   198
FSquirrel::~FSquirrel()
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   199
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   200
	delete this->engine;
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
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   203
AIController *AIFactorySquirrel::CreateInstance()
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   204
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   205
	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
   206
	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
   207
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   208
	return controller;
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
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   211
void AIControllerSquirrel::RegisterClasses()
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
	/* 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
   214
	this->engine->AddClassBegin("AIFactory");
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   215
	this->engine->AddClassEnd();
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   216
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   217
	/* 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
   218
	squirrel_register_std(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   219
	SQAIAbstractList_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   220
	SQAIAccounting_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   221
	SQAIAirport_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   222
	SQAIBase_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   223
	SQAICargo_Register(this->engine);
9785
edbd185e05ed (svn r12280) [NoAI] -Add: added AICargoList (Morloth)
truebrain
parents: 9782
diff changeset
   224
	SQAICargoList_Register(this->engine);
9788
e9dbd4ec1784 (svn r12284) [NoAI] -Add: added AICargoList_v(IsFreight|CargoIncomes) (Morloth)
truebrain
parents: 9785
diff changeset
   225
	SQAICargoList_vCargoIncomes_Register(this->engine);
e9dbd4ec1784 (svn r12284) [NoAI] -Add: added AICargoList_v(IsFreight|CargoIncomes) (Morloth)
truebrain
parents: 9785
diff changeset
   226
	SQAICargoList_vIsFreight_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);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   229
	SQAIEngine_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   230
	SQAIEngineList_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   231
	SQAIEngineList_vCapacity_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   232
	SQAIEngineList_vCargoType_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   233
	SQAIEngineList_vMaxSpeed_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   234
	SQAIEngineList_vPrice_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   235
	SQAIEngineList_vReliability_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   236
	SQAIEvent_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   237
	SQAIEventController_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   238
	SQAIEventTest_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   239
	SQAIEventVehicleCrash_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   240
	SQAIExecMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   241
	SQAIIndustry_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   242
	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
   243
	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
   244
	SQAIIndustryList_CargoProducing_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   245
	SQAIIndustryList_vCargoAccepted_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   246
	SQAIIndustryList_vDistanceManhattanToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   247
	SQAIIndustryList_vDistanceSquareToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   248
	SQAIIndustryList_vLocation_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   249
	SQAIIndustryList_vProduction_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   250
	SQAIList_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   251
	SQAIList_vRandomize_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   252
	SQAIMap_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   253
	SQAIMarine_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   254
	SQAIOrder_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   255
	SQAIPathFinder_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   256
	SQAIPathFinderStupid_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   257
	SQAIRoad_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   258
	SQAISettings_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   259
	SQAISign_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   260
	SQAIStation_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   261
	SQAIStationList_Register(this->engine);
9745
fb2454d70f37 (svn r12225) [NoAI] -Change [API CHANGE]: AIStationVehicleList -> AIVehicleList_Station (WATCH THE NAMES CAREFULLY!)
truebrain
parents: 9743
diff changeset
   262
	SQAIStationList_Vehicle_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   263
	SQAIStationList_vCargoRating_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   264
	SQAIStationList_vCargoWaiting_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   265
	SQAIStationList_vDistanceManhattanToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   266
	SQAIStationList_vDistanceSquareToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   267
	SQAIStationList_vLocation_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   268
	SQAITestMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   269
	SQAITile_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   270
	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
   271
	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
   272
	SQAITileList_IndustryProducing_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   273
	SQAITileList_vBuildable_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   274
	SQAITileList_vBuildableRectangle_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   275
	SQAITileList_vCargoAcceptance_Register(this->engine);
9771
769496a7b02e (svn r12264) [NoAI] -Add: added AITileList_vCargoProduction (yorick)
truebrain
parents: 9757
diff changeset
   276
	SQAITileList_vCargoProduction_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   277
	SQAITileList_vDistanceManhattanToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   278
	SQAITileList_vDistanceSquareToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   279
	SQAITileList_vHeight_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   280
	SQAITileList_vNeighbourRoadCount_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   281
	SQAITileList_vRoadTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   282
	SQAITileList_vSlope_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   283
	SQAITileList_vWater_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   284
	SQAITown_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   285
	SQAITownList_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   286
	SQAITownList_vDistanceManhattanToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   287
	SQAITownList_vDistanceSquareToTile_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   288
	SQAITownList_vLocation_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   289
	SQAITownList_vPopulation_Register(this->engine);
9742
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   290
	SQAITransactionMode_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   291
	SQAIVehicle_Register(this->engine);
75f4fded70ae (svn r12222) [NoAI] -Fix r12221: forgot one file
truebrain
parents: 9726
diff changeset
   292
	SQAIVehicleList_Register(this->engine);
9745
fb2454d70f37 (svn r12225) [NoAI] -Change [API CHANGE]: AIStationVehicleList -> AIVehicleList_Station (WATCH THE NAMES CAREFULLY!)
truebrain
parents: 9743
diff changeset
   293
	SQAIVehicleList_Station_Register(this->engine);
9753
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   294
	SQAIVehicleList_vAge_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   295
	SQAIVehicleList_vAgeLeft_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   296
	SQAIVehicleList_vEngineType_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   297
	SQAIVehicleList_vLocation_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   298
	SQAIVehicleList_vMaxAge_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   299
	SQAIVehicleList_vProfitLastYear_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   300
	SQAIVehicleList_vProfitThisYear_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   301
	SQAIVehicleList_vUnitNumber_Register(this->engine);
7209db94ad12 (svn r12236) [NoAI] -Change: [API CHANGE] All valuators are now in the notation: AI<type>List_v<Valuator>. Example: AIVehicleList_vAge
truebrain
parents: 9745
diff changeset
   302
	SQAIVehicleList_vVehicleType_Register(this->engine);
9387
4255a0a2d272 (svn r9179) [NoAI] -Add: added templates that makes adding classes to squirrel very easy
truelight
parents: 9386
diff changeset
   303
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   304
	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
   305
}
d03563181088 (svn r9160) [NoAI] -Add: added AIController for Squirrel; all AIs, C++ or Squirrel, should extend this class
truelight
parents: 9368
diff changeset
   306
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   307
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
   308
{
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9413
diff changeset
   309
	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
   310
	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
   311
	this->RegisterClasses();
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   312
	this->engine->LoadScript(script);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   313
	/* Create the main-class */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   314
	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
   315
	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
   316
		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
   317
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   318
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   319
AIControllerSquirrel::~AIControllerSquirrel()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   320
{
9395
4337e022594c (svn r9187) [NoAI] -Change: renamed SquirrelCore to SquirrelEngine, as it is more
truelight
parents: 9393
diff changeset
   321
	delete this->engine;
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   322
}
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   323
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
   324
/* virtual */ void AIControllerSquirrel::Start()
9365
c3d08e0b1083 (svn r9151) [NoAI] -Add: added squirrel module that loads squirrel scripts and
truelight
parents:
diff changeset
   325
{
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
   326
	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
   327
}
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   328
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   329
/* 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
   330
{
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   331
	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
   332
}