--- a/bin/ai/wrightai/main.nut Sat Feb 23 18:01:05 2008 +0000
+++ b/bin/ai/wrightai/main.nut Sun Feb 24 21:06:38 2008 +0000
@@ -57,19 +57,23 @@
local tile_1 = this.FindSuitableAirportSpot(airport_type, 0);
if (tile_1 < 0) return -1;
local tile_2 = this.FindSuitableAirportSpot(airport_type, tile_1);
- /* XXX -- If tile_2 isn't found, tile_1 town is added to used list, but never really used */
- if (tile_2 < 0) return -2;
+ if (tile_2 < 0) {
+ this.towns_used.RemoveValue(tile_1);
+ return -2;
+ }
/* Build the airports for real */
if (!AIAirport.BuildAirport(tile_1, airport_type)) {
print(this.name + ": [ERROR] Although the testing told us we could build 2 airports, it still failed on the first airport at tile " + tile_1 + ".");
- /* XXX -- We should free the towns again for an other day */
+ this.towns_used.RemoveValue(tile_1);
+ this.towns_used.RemoveValue(tile_2);
return -3;
}
if (!AIAirport.BuildAirport(tile_2, airport_type)) {
print(this.name + ": [ERROR] Although the testing told us we could build 2 airports, it still failed on the second airport at tile " + tile_2 + ".");
AIAirport.RemoveAirport(tile_1);
- /* XXX -- We should free the towns again for an other day */
+ this.towns_used.RemoveValue(tile_1);
+ this.towns_used.RemoveValue(tile_2);
return -4;
}
@@ -77,7 +81,8 @@
if (ret < 0) {
AIAirport.RemoveAirport(tile_1);
AIAirport.RemoveAirport(tile_2);
- /* XXX -- We should free the towns again for an other day */
+ this.towns_used.RemoveValue(tile_1);
+ this.towns_used.RemoveValue(tile_2);
return ret;
}
@@ -85,7 +90,6 @@
return ret;
}
-
/**
* Build an aircraft with orders from tile_1 to tile_2.
* The best available aircraft of that time will be bought.
@@ -199,6 +203,7 @@
/* Make the town as used, so we don't use it again */
this.towns_used.AddItem(town, tile);
+
return tile;
}
@@ -231,15 +236,7 @@
print(this.name + ": [INFO] Selling " + i + " as it finally is in a depot.");
/* Check if we are the last one serving those airports; else sell the airports */
local list2 = AIVehicleList_Station(AIStation.GetStationID(this.route_1.GetValue(i)));
- if (list2.Count() == 0) {
- /* Remove the airports */
- print(this.name + ": [INFO] Removing airports as nobody serves them anymore.");
- AIAirport.RemoveAirport(this.route_1.GetValue(i));
- AIAirport.RemoveAirport(this.route_2.GetValue(i));
- this.route_1.RemoveItem(i);
- this.route_2.RemoveItem(i);
- /* XXX -- We should free the towns_used entries too */
- }
+ if (list2.Count() == 0) this.SellAirports(i);
vehicle_to_depot.rawdelete(i);
}
}
@@ -254,9 +251,11 @@
for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
local list2 = AIVehicleList_Station(i);
- /* No vehicles going to this station, abort */
- /* TODO -- Sell station */
- if (list2.Count() == 0) continue;
+ /* No vehicles going to this station, abort and sell */
+ if (list2.Count() == 0) {
+ this.SellAirports(i);
+ continue;
+ };
/* Find the first vehicle that is going to this station */
local v = list2.Begin();
@@ -276,6 +275,23 @@
}
}
+/**
+ * Sells the airports from route index i
+ * Removes towns from towns_used list too
+ */
+function WrightAI::SellAirports(i) {
+ /* Remove the airports */
+ print(this.name + ": [INFO] Removing airports as nobody serves them anymore.");
+ AIAirport.RemoveAirport(this.route_1.GetValue(i));
+ AIAirport.RemoveAirport(this.route_2.GetValue(i));
+ /* Free the towns_used entries */
+ this.towns_used.RemoveValue(this.route_1.GetValue(i));
+ this.towns_used.RemoveValue(this.route_2.GetValue(i));
+ /* Remove the route */
+ this.route_1.RemoveItem(i);
+ this.route_2.RemoveItem(i);
+}
+
function WrightAI::HandleEvents()
{
while (AIEventController.IsEventWaiting()) {
@@ -309,6 +325,7 @@
}
}
this.name = AICompany.GetCompanyName(AICompany.MY_COMPANY);
+ /* Say hello to the user */
print(this.name + ": Welcome to WrightAI. I will be building airports all day long.");
/* We start with almost no loan, and we take a loan when we want to build something */
@@ -324,12 +341,19 @@
/* Let's go on for ever */
while (true) {
/* Once in a while, with enough money, try to build something */
- if (ticker % this.delay_build_airport_route == 0 && this.HasMoney(100000)) {
+ if ((ticker % this.delay_build_airport_route == 0 || ticker == 0) && this.HasMoney(100000)) {
local ret = this.BuildAirportRoute();
- if (ret == -1) {
+ if (ret == -1 && ticker != 0) {
/* No more route found, delay even more before trying to find an other */
this.delay_build_airport_route = 10000;
}
+ else if (ret < 0 && ticker == 0) {
+ /* The AI failed to build a first airport and is deemed */
+ this.ChangeName("FailedWrightAI");
+ print(this.name + ": Failed to build first airport route, now giving up building. Repaying loan. Have a nice day!");
+ AICompany.SetLoanAmount(0);
+ return;
+ }
}
/* Manage the routes once in a while */
if (ticker % 2000 == 0) {
@@ -357,8 +381,8 @@
function GetAuthor() { return "OpenTTD Dev Team"; }
function GetName() { return "WrightAI"; }
function GetDescription() { return "A simple AI that tries to beat you with only aircrafts"; }
- function GetVersion() { return 1; }
- function GetDate() { return "2007-11-18"; }
+ function GetVersion() { return 1.1; }
+ function GetDate() { return "2008-02-24"; }
function CreateInstance() { return "WrightAI"; }
}