-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.Handler.nut
249 lines (218 loc) · 8.02 KB
/
Event.Handler.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Event Handler v.1, r.252, [2012-06-30]
* part of WmDOT v.11
* Copyright © 2012 by W. Minchin. For more info,
* please visit https://github.com/MinchinWeb/openttd-wmdot
*
* Permission is granted to you to use, copy, modify, merge, publish,
* distribute, sublincense, and/or sell this software, and provide these
* rights to others, provided:
*
* + The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the software.
* + Attribution is provided in the normal place for recognition of 3rd party
* contributions.
* + You accept that this software is provided to you "as is", without warranty.
*/
/* Event Handler deals with events as OpenTTD feed them to the AI.
*/
class Events {
function GetVersion() { return 1; }
function GetRevision() { return 252; }
function GetDate() { return "2012-06-30"; }
function GetName() { return "Event Handler"; }
_NextRun = null;
_SleepLength = null; // as measured in days
Log = null;
Money = null;
Manager_Ships = null;
constructor()
{
this._NextRun = 0;
this._SleepLength = 3;
this.Settings = this.Settings(this);
this.State = this.State(this);
Log = OpLog();
Money = OpMoney();
Manager_Ships = ManShips();
}
}
class Events.Settings {
_main = null;
function _set(idx, val)
{
switch (idx) {
case "SleepLength": this._main._SleepLength = val; break;
default: throw("The index '" + idx + "' does not exist");
}
return val;
}
function _get(idx)
{
switch (idx) {
case "SleepLength": return this._main._SleepLength; break;
default: throw("The index '" + idx + "' does not exist");
}
}
constructor(main)
{
this._main = main;
}
}
class Events.State {
_main = null;
function _get(idx)
{
switch (idx) {
case "NextRun": return this._main._NextRun; break;
default: throw("The index '" + idx + "' does not exist");
}
}
constructor(main)
{
this._main = main;
}
}
function Events::LinkUp()
{
this.Log = WmDOT.Log;
this.Money = WmDOT.Money;
this.Manager_Ships = WmDOT.Manager_Ships;
Log.Note(this.GetName() + " linked up!",3);
}
function Events::Run() {
Log.Note("Event Handler running at tick " + AIController.GetTick() + ".", 1);
// Reset next run
this._NextRun = AIController.GetTick() + this._SleepLength * 17;
// Handle Events
while(AIEventController.IsEventWaiting()) {
local Event = AIEventController.GetNextEvent();
Log.Note("Event: " + Event.GetEventType(), 3);
switch(Event.GetEventType()) {
case AIEvent.ET_SUBSIDY_OFFER:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_SUBSIDY_OFFER_EXPIRED:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_SUBSIDY_AWARDED:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_SUBSIDY_EXPIRED:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_COMPANY_NEW:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_COMPANY_IN_TROUBLE:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_COMPANY_MERGER:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_COMPANY_BANKRUPT:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_VEHICLE_LOST:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_VEHICLE_UNPROFITABLE:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_INDUSTRY_OPEN:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_INDUSTRY_CLOSE:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_ENGINE_AVAILABLE:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_STATION_FIRST_VEHICLE:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_DISASTER_ZEPPELINER_CRASHED:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_DISASTER_ZEPPELINER_CLEARED:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_TOWN_FOUNDED:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_AIRCRAFT_DEST_TOO_FAR:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_ADMIN_PORT:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_WINDOW_WIDGET_CLICK:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_GOAL_QUESTION_ANSWER:
Log.Note("Nice event and all, but I have no idea what to do about it...", 4);
break;
case AIEvent.ET_COMPANY_ASK_MERGER:
// Accept the merger is the company is a 'DOT' or the value is $2
local Event2 = AIEventCompanyAskMerger.Convert(Event);
local Company = Event2.GetCompanyID();
local Value = Event2.GetValue();
local Name = AICompany.GetName(Company);
// if Name == null, then the company has ceased to exist, and
// so we can't accept the merger.
if (Name != null) {
Name.find("DOT")== null
if ((Name.find("DOT") != null) || (Value < 2)) {
Money.FundsRequest(Value);
local Accepted = Event2.AcceptMerger();
Log.Note("Merger Accepted with " + Name + " (Value: " + Value + "£) : " + Accepted, 4);
} else {
Log.Note("Merger request with " + Name + " (Value: " + Value + "£) : Declined", 4);
}
} else {
Log.Note("Merger offered, but we're too late.", 4);
}
break;
case AIEvent.ET_VEHICLE_CRASHED:
// Clone the crashed vehicle
local Event2 = AIEventVehicleCrashed.Convert(Event);
local Reason = Event2.GetCrashReason();
if (Reason == AIEventVehicleCrashed.CRASH_FLOODED) {
// don't replace if vehicle flooded out
Log.Note("Vehicle flooded out!", 4);
} else if (Reason == AIEventVehicleCrashed.CRASH_AIRCRAFT_NO_AIRPORT) {
// don't replace if there was no place to land
Log.Note("Aircraft crashed because no airport to land at could be found...", 4);
} else {
local OldVehicle = Event2.GetVehicleID();
Money.FundsRequest(AIEngine.GetPrice(AIVehicle.GetEngineType(OldVehicle)) * 1.1);
// Get the depot closest to the first order of the vehicle
local AllDepots = AIDepotList(AIVehicle.GetVehicleType(OldVehicle)); // TO-DO: check this
AllDepot.Valuate(GetDistanceManhattanToTile, AIOrder.GetOrderDestination(OldVehicle, 0));
local Depot = AllDepots.Begin();
local NewVehicle;
NewVehicle = AIVehicle.CloneVehicle(Depot, OldVehicle, true);
AIVehicle.StartStopVehicle(NewVehicle);
Log.Note("Crahsed Vehicle Replaced: " + NewVehicle, 4);
}
break;
case AIEvent.ET_VEHICLE_WAITING_IN_DEPOT:
// Sell the sucker!!
// TO-DO: Check to see if it is on the 'to sell' list
local Event2 = AIEventVehicleWaitingInDepot.Convert(Event);
local Vehicle = Event2.GetVehicleID();
local Result = AIVehicle.SellVehicle(Vehicle);
Log.Note("Vehicle " + Vehicle + " sold! : " + Result, 4);
break;
case AIEvent.ET_ENGINE_PREVIEW:
// Always accept
local Event2 = AIEventEnginePreview.Convert(Event);
local Name = Event2.GetName();
local Result = Event2.AcceptPreview();
Log.Note("Preview of " + Name + " accepted! : " + Result, 4);
break;
default:
Log.Warning(" Unknown event type!!");
break;
} // end switch(Event.GetEventType())
} // end while(AIEventController.IsEventWaiting())
}