1 /* $Id$ */ |
1 /* $Id$ */ |
2 |
2 |
3 /** @file ai_controller.cpp Implementation of AIControler. */ |
3 /** @file ai_controller.cpp Implementation of AIControler. */ |
4 |
4 |
5 #include "ai_controller.hpp" |
|
6 #include "ai_object.hpp" |
|
7 #include "ai_log.hpp" |
|
8 #include "../../stdafx.h" |
5 #include "../../stdafx.h" |
9 #include "../../openttd.h" |
6 #include "../../openttd.h" |
10 #include "../../player_func.h" |
7 #include "../../player_func.h" |
|
8 #include "../../core/alloc_func.hpp" |
|
9 #include "table/strings.h" |
|
10 |
|
11 #include <squirrel.h> |
|
12 #include "../../squirrel.hpp" |
|
13 #include "../../squirrel_helper.hpp" |
|
14 #include "../../squirrel_class.hpp" |
|
15 #include "../../squirrel_std.hpp" |
|
16 #include "ai_controller.hpp" |
11 #include "../ai_threads.h" |
17 #include "../ai_threads.h" |
|
18 |
|
19 /* Convert all AI related classes to Squirrel data. |
|
20 * Note: this line a marker in squirrel_export.sh. Do not change! */ |
|
21 #include "ai_abstractlist.hpp.sq" |
|
22 #include "ai_accounting.hpp.sq" |
|
23 #include "ai_airport.hpp.sq" |
|
24 #include "ai_base.hpp.sq" |
|
25 #include "ai_bridge.hpp.sq" |
|
26 #include "ai_bridgelist.hpp.sq" |
|
27 #include "ai_cargo.hpp.sq" |
|
28 #include "ai_cargolist.hpp.sq" |
|
29 #include "ai_company.hpp.sq" |
|
30 #include "ai_controller.hpp.sq" |
|
31 #include "ai_date.hpp.sq" |
|
32 #include "ai_engine.hpp.sq" |
|
33 #include "ai_enginelist.hpp.sq" |
|
34 #include "ai_error.hpp.sq" |
|
35 #include "ai_event.hpp.sq" |
|
36 #include "ai_event_types.hpp.sq" |
|
37 #include "ai_execmode.hpp.sq" |
|
38 #include "ai_industry.hpp.sq" |
|
39 #include "ai_industrylist.hpp.sq" |
|
40 #include "ai_list.hpp.sq" |
|
41 #include "ai_log.hpp.sq" |
|
42 #include "ai_map.hpp.sq" |
|
43 #include "ai_marine.hpp.sq" |
|
44 #include "ai_order.hpp.sq" |
|
45 #include "ai_road.hpp.sq" |
|
46 #include "ai_settings.hpp.sq" |
|
47 #include "ai_sign.hpp.sq" |
|
48 #include "ai_station.hpp.sq" |
|
49 #include "ai_stationlist.hpp.sq" |
|
50 #include "ai_subsidy.hpp.sq" |
|
51 #include "ai_subsidylist.hpp.sq" |
|
52 #include "ai_testmode.hpp.sq" |
|
53 #include "ai_tile.hpp.sq" |
|
54 #include "ai_tilelist.hpp.sq" |
|
55 #include "ai_town.hpp.sq" |
|
56 #include "ai_townlist.hpp.sq" |
|
57 #include "ai_transactionmode.hpp.sq" |
|
58 #include "ai_tunnel.hpp.sq" |
|
59 #include "ai_vehicle.hpp.sq" |
|
60 #include "ai_vehiclelist.hpp.sq" |
12 |
61 |
13 /* static */ void AIController::Sleep(uint ticks) |
62 /* static */ void AIController::Sleep(uint ticks) |
14 { |
63 { |
15 AI_SuspendPlayer(_current_player, ticks); |
64 AI_SuspendPlayer(_current_player, ticks); |
16 } |
65 } |
17 |
66 |
18 /* static */ void AIController::Print(bool error_msg, const char *message) |
67 /* static */ void AIController::Print(bool error_msg, const char *message) |
19 { |
68 { |
20 AILog::Log(error_msg ? AILog::LOG_SQ_ERROR : AILog::LOG_SQ_INFO, message); |
69 AILog::Log(error_msg ? AILog::LOG_SQ_ERROR : AILog::LOG_SQ_INFO, message); |
21 } |
70 } |
|
71 |
|
72 void AIController::RegisterClasses() |
|
73 { |
|
74 /* Ignore AIFactory if we are really starting an AI */ |
|
75 this->engine->AddClassBegin("AIFactory"); |
|
76 this->engine->AddClassEnd(); |
|
77 |
|
78 /* Register all classes */ |
|
79 squirrel_register_std(this->engine); |
|
80 SQAIAbstractList_Register(this->engine); |
|
81 SQAIAccounting_Register(this->engine); |
|
82 SQAIAirport_Register(this->engine); |
|
83 SQAIBase_Register(this->engine); |
|
84 SQAIBridge_Register(this->engine); |
|
85 SQAIBridgeList_Register(this->engine); |
|
86 SQAIBridgeList_Length_Register(this->engine); |
|
87 SQAICargo_Register(this->engine); |
|
88 SQAICargoList_Register(this->engine); |
|
89 SQAICompany_Register(this->engine); |
|
90 SQAIController_Register(this->engine); |
|
91 SQAIDate_Register(this->engine); |
|
92 SQAIEngine_Register(this->engine); |
|
93 SQAIEngineList_Register(this->engine); |
|
94 SQAIError_Register(this->engine); |
|
95 SQAIEvent_Register(this->engine); |
|
96 SQAIEventCompanyBankrupt_Register(this->engine); |
|
97 SQAIEventCompanyInTrouble_Register(this->engine); |
|
98 SQAIEventCompanyMerger_Register(this->engine); |
|
99 SQAIEventCompanyNew_Register(this->engine); |
|
100 SQAIEventController_Register(this->engine); |
|
101 SQAIEventEnginePreview_Register(this->engine); |
|
102 SQAIEventSubsidyAwarded_Register(this->engine); |
|
103 SQAIEventSubsidyExpired_Register(this->engine); |
|
104 SQAIEventSubsidyOffer_Register(this->engine); |
|
105 SQAIEventSubsidyOfferExpired_Register(this->engine); |
|
106 SQAIEventTest_Register(this->engine); |
|
107 SQAIEventVehicleCrash_Register(this->engine); |
|
108 SQAIEventVehicleLost_Register(this->engine); |
|
109 SQAIEventVehicleUnprofitable_Register(this->engine); |
|
110 SQAIEventVehicleWaitingInDepot_Register(this->engine); |
|
111 SQAIExecMode_Register(this->engine); |
|
112 SQAIIndustry_Register(this->engine); |
|
113 SQAIIndustryList_Register(this->engine); |
|
114 SQAIIndustryList_CargoAccepting_Register(this->engine); |
|
115 SQAIIndustryList_CargoProducing_Register(this->engine); |
|
116 SQAIList_Register(this->engine); |
|
117 SQAILog_Register(this->engine); |
|
118 SQAIMap_Register(this->engine); |
|
119 SQAIMarine_Register(this->engine); |
|
120 SQAIOrder_Register(this->engine); |
|
121 SQAIRoad_Register(this->engine); |
|
122 SQAISettings_Register(this->engine); |
|
123 SQAISign_Register(this->engine); |
|
124 SQAIStation_Register(this->engine); |
|
125 SQAIStationList_Register(this->engine); |
|
126 SQAIStationList_Vehicle_Register(this->engine); |
|
127 SQAISubsidy_Register(this->engine); |
|
128 SQAISubsidyList_Register(this->engine); |
|
129 SQAITestMode_Register(this->engine); |
|
130 SQAITile_Register(this->engine); |
|
131 SQAITileList_Register(this->engine); |
|
132 SQAITileList_IndustryAccepting_Register(this->engine); |
|
133 SQAITileList_IndustryProducing_Register(this->engine); |
|
134 SQAITown_Register(this->engine); |
|
135 SQAITownList_Register(this->engine); |
|
136 SQAITransactionMode_Register(this->engine); |
|
137 SQAITunnel_Register(this->engine); |
|
138 SQAIVehicle_Register(this->engine); |
|
139 SQAIVehicleList_Register(this->engine); |
|
140 SQAIVehicleList_Station_Register(this->engine); |
|
141 |
|
142 this->engine->SetGlobalPointer(this->engine); |
|
143 } |
|
144 |
|
145 static void PrintFunc(bool error_msg, const SQChar *message) |
|
146 { |
|
147 /* Convert to OpenTTD internal capable string */ |
|
148 AIController::Print(error_msg, FS2OTTD(message)); |
|
149 } |
|
150 |
|
151 AIController::AIController(const char *script, const char *class_name) |
|
152 { |
|
153 this->tick = 0; |
|
154 |
|
155 /* Create the Squirrel Engine and assign all required classes and settings */ |
|
156 this->engine = new Squirrel(); |
|
157 this->engine->SetPrintFunction(&PrintFunc); |
|
158 this->RegisterClasses(); |
|
159 if (!this->engine->LoadScript(script)) { |
|
160 delete this->engine; |
|
161 this->engine = NULL; |
|
162 return; |
|
163 } |
|
164 /* Create the main-class */ |
|
165 this->SQ_instance = MallocT<SQObject>(sizeof(SQObject)); |
|
166 this->engine->CreateClassInstance(class_name, this, this->SQ_instance); |
|
167 if (this->engine->MethodExists(*this->SQ_instance, "constructor")) |
|
168 this->engine->CallMethod(*this->SQ_instance, "constructor"); |
|
169 } |
|
170 |
|
171 void AIController::Start() |
|
172 { |
|
173 if (this->engine != NULL) this->engine->CallMethod(*this->SQ_instance, "Start"); |
|
174 } |
|
175 |
|
176 void AIController::Stop() |
|
177 { |
|
178 if (this->engine != NULL) this->engine->CallMethod(*this->SQ_instance, "Stop"); |
|
179 } |
|
180 |
|
181 AIController::~AIController() |
|
182 { |
|
183 delete this->engine; |
|
184 free(this->SQ_instance); |
|
185 } |
|
186 |
|
187 void AIController::IncreaseTick() |
|
188 { |
|
189 this->tick++; |
|
190 } |
|
191 |
|
192 uint AIController::GetTick() |
|
193 { |
|
194 return this->tick; |
|
195 } |