--- a/bin/ai/wrightai/main.nut Fri Feb 22 12:30:17 2008 +0000
+++ b/bin/ai/wrightai/main.nut Fri Feb 22 12:38:28 2008 +0000
@@ -1,12 +1,5 @@
class WrightAI extends AIController {
- airport = null;
- company = null;
- map = null;
- order = null;
- station = null;
- vehicle = null;
-
name = null;
towns_used = null;
route_1 = null;
@@ -19,13 +12,6 @@
function Stop();
constructor() {
- this.airport = AIAirport();
- this.company = AICompany();
- this.map = AIMap();
- this.order = AIOrder();
- this.station = AIStation();
- this.vehicle = AIVehicle();
-
this.towns_used = AIList();
this.route_1 = AIList();
this.route_2 = AIList();
@@ -37,7 +23,7 @@
*/
function WrightAI::HasMoney(money)
{
- if (this.company.GetBankBalance(AICompany.MY_COMPANY) + (this.company.GetMaxLoanAmount() - this.company.GetLoanAmount()) > money) return true;
+ if (AICompany.GetBankBalance(AICompany.MY_COMPANY) + (AICompany.GetMaxLoanAmount() - AICompany.GetLoanAmount()) > money) return true;
return false;
}
@@ -47,12 +33,12 @@
function WrightAI::GetMoney(money)
{
if (!this.HasMoney(money)) return;
- if (this.company.GetBankBalance(AICompany.MY_COMPANY) > money) return;
+ if (AICompany.GetBankBalance(AICompany.MY_COMPANY) > money) return;
- local loan = money - this.company.GetBankBalance(AICompany.MY_COMPANY) + this.company.GetLoanInterval() + this.company.GetLoanAmount();
- loan = loan - loan % this.company.GetLoanInterval();
+ local loan = money - AICompany.GetBankBalance(AICompany.MY_COMPANY) + AICompany.GetLoanInterval() + AICompany.GetLoanAmount();
+ loan = loan - loan % AICompany.GetLoanInterval();
print(this.name + ": [INFO] Need a loan to get " + money + ": " + loan);
- this.company.SetLoanAmount(loan);
+ AICompany.SetLoanAmount(loan);
}
/**
@@ -75,22 +61,22 @@
if (tile_2 < 0) return -2;
/* Build the airports for real */
- if (!this.airport.BuildAirport(tile_1, airport_type)) {
+ 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 */
return -3;
}
- if (!this.airport.BuildAirport(tile_2, airport_type)) {
+ 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 + ".");
- this.airport.RemoveAirport(tile_1);
+ AIAirport.RemoveAirport(tile_1);
/* XXX -- We should free the towns again for an other day */
return -4;
}
local ret = this.BuildAircraft(tile_1, tile_2);
if (ret < 0) {
- this.airport.RemoveAirport(tile_1);
- this.airport.RemoveAirport(tile_2);
+ AIAirport.RemoveAirport(tile_1);
+ AIAirport.RemoveAirport(tile_2);
/* XXX -- We should free the towns again for an other day */
return ret;
}
@@ -107,13 +93,13 @@
function WrightAI::BuildAircraft(tile_1, tile_2)
{
/* Build an aircraft */
- local hangar = this.airport.GetHangarOfAirport(tile_1);
+ local hangar = AIAirport.GetHangarOfAirport(tile_1);
local engine = null;
local engine_list = AIEngineList(AIVehicle.VEHICLE_AIR);
/* When bank balance < 300000, buy cheaper planes */
- local balance = this.company.GetBankBalance(AICompany.MY_COMPANY);
+ local balance = AICompany.GetBankBalance(AICompany.MY_COMPANY);
engine_list.Valuate(AIEngineListPrice());
engine_list.KeepBelowValue(balance < 300000 ? 50000 : (balance < 1000000 ? 300000 : 1000000));
@@ -129,15 +115,15 @@
print(this.name + ": [ERROR] Couldn't find a suitable engine");
return -5;
}
- local vehicle = this.vehicle.BuildVehicle(hangar, engine);
- if (!this.vehicle.IsValidVehicle(vehicle)) {
+ local vehicle = AIVehicle.BuildVehicle(hangar, engine);
+ if (!AIVehicle.IsValidVehicle(vehicle)) {
print(this.name + ": [ERROR] Couldn't build the aircraft");
return -6;
}
/* Send him on his way */
- this.order.AppendOrder(vehicle, tile_1, AIOrder.AIOF_NONE);
- this.order.AppendOrder(vehicle, tile_2, AIOrder.AIOF_NONE);
- this.vehicle.StartStopVehicle(vehicle);
+ AIOrder.AppendOrder(vehicle, tile_1, AIOrder.AIOF_NONE);
+ AIOrder.AppendOrder(vehicle, tile_2, AIOrder.AIOF_NONE);
+ AIVehicle.StartStopVehicle(vehicle);
this.distance_of_route.rawset(vehicle, AIMap.DistanceManhattan(tile_1, tile_2));
this.route_1.AddItem(vehicle, tile_1);
this.route_2.AddItem(vehicle, tile_2);
@@ -179,7 +165,7 @@
/* Create a 30x30 grid around the core of the town and see if we can find a spot for a small airport */
local list = AITileList();
/* XXX -- We assume we are more than 15 tiles away from the border! */
- list.AddRectangle(tile - this.map.GetTileIndex(15, 15), tile + this.map.GetTileIndex(15, 15));
+ list.AddRectangle(tile - AIMap.GetTileIndex(15, 15), tile + AIMap.GetTileIndex(15, 15));
list.Valuate(AITileListBuildableRectangle(airport_x, airport_y));
list.KeepValue(1);
if (center_tile != 0) {
@@ -200,7 +186,7 @@
for (tile = list.Begin(); list.HasNext(); tile = list.Next()) {
Sleep(1);
- if (!this.airport.BuildAirport(tile, airport_type)) continue;
+ if (!AIAirport.BuildAirport(tile, airport_type)) continue;
good_tile = tile;
break;
}
@@ -231,30 +217,30 @@
for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
local profit = list.GetValue(i);
/* Profit last year and this year bad? Let's sell the vehicle */
- if (profit < 10000 && this.vehicle.GetProfitThisYear(i) < 10000) {
+ if (profit < 10000 && AIVehicle.GetProfitThisYear(i) < 10000) {
/* Send the vehicle to depot if we didn't do so yet */
- if (!this.vehicle_to_depot.rawin(i) || this.vehicle_to_depot.rawget(i) != true) {
- print(this.name + ": [INFO] Sending " + i + " to depot as profit is: " + profit + " / " + this.vehicle.GetProfitThisYear(i));
- this.vehicle.SendVehicleToDepot(i);
- this.vehicle_to_depot.rawset(i, true);
+ if (!vehicle_to_depot.rawin(i) || vehicle_to_depot.rawget(i) != true) {
+ print(this.name + ": [INFO] Sending " + i + " to depot as profit is: " + profit + " / " + AIVehicle.GetProfitThisYear(i));
+ AIVehicle.SendVehicleToDepot(i);
+ vehicle_to_depot.rawset(i, true);
}
}
/* Try to sell it over and over till it really is in the depot */
- if (this.vehicle_to_depot.rawin(i) && this.vehicle_to_depot.rawget(i) == true) {
- if (this.vehicle.SellVehicle(i)) {
+ if (vehicle_to_depot.rawin(i) && vehicle_to_depot.rawget(i) == true) {
+ if (AIVehicle.SellVehicle(i)) {
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 = AIStationVehicleList(this.station.GetStationID(this.route_1.GetValue(i)));
+ local list2 = AIStationVehicleList(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.");
- this.airport.RemoveAirport(this.route_1.GetValue(i));
- this.airport.RemoveAirport(this.route_2.GetValue(i));
+ 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 */
}
- this.vehicle_to_depot.rawdelete(i);
+ vehicle_to_depot.rawdelete(i);
}
}
}
@@ -281,7 +267,7 @@
/* Do not build a new vehicle if we bought a new one in the last DISTANCE days */
if (list2.Count() != 0) continue;
- print(this.name + ": [INFO] Station " + i + " (" + this.station.GetLocation(i) + ") has too many cargo, adding a new vehicle for the route.");
+ print(this.name + ": [INFO] Station " + i + " (" + AIStation.GetLocation(i) + ") has too many cargo, adding a new vehicle for the route.");
/* Make sure we have enough money */
this.GetMoney(50000);
@@ -316,17 +302,17 @@
Sleep(1);
/* Give the boy a name */
- if (!this.company.SetCompanyName("WrightAI")) {
+ if (!AICompany.SetCompanyName("WrightAI")) {
local i = 2;
- while (!this.company.SetCompanyName("WrightAI #" + i)) {
+ while (!AICompany.SetCompanyName("WrightAI #" + i)) {
i++;
}
}
- this.name = this.company.GetCompanyName(AICompany.MY_COMPANY);
+ this.name = AICompany.GetCompanyName(AICompany.MY_COMPANY);
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 */
- this.company.SetLoanAmount(this.company.GetLoanInterval());
+ AICompany.SetLoanAmount(AICompany.GetLoanInterval());
/* We need our local ticker, as GetTick() will skip ticks */
local ticker = 0;
@@ -347,7 +333,7 @@
}
/* Try to get ride of our loan once in a while */
if (ticker % 5000 == 0) {
- this.company.SetLoanAmount(0);
+ AICompany.SetLoanAmount(0);
}
/* Check for events once in a while */
if (ticker % 100 == 0) {