/* Utils.nut */
function Utils::HasMoney(money)
{
local loan_amount = AICompany.GetLoanAmount();
local max_loan_amount = AICompany.GetMaxLoanAmount();
local bank_balance = AICompany.GetBankBalance(AICompany.COMPANY_SELF);
if (bank_balance + max_loan_amount - loan_amount >= money) return true;
return false;
}
function Utils::GetMoney(money)
{
local bank_balance = AICompany.GetBankBalance(AICompany.COMPANY_SELF);
if (bank_balance >= money) return;
local request_loan = money - bank_balance;
local loan_interval = AICompany.GetLoanInterval();
local over_interval = request_loan % loan_interval;
request_loan += loan_interval - over_interval;
local loan_amount = AICompany.GetLoanAmount();
local max_loan_amount = AICompany.GetMaxLoanAmount();
if (loan_amount + request_loan > max_loan_amount) {
AICompany.SetLoanAmount(max_loan_amount);
} else {
AICompany.SetLoanAmount(loan_amount + request_loan);
}
}
function Utils::RepayLoan()
{
local bank_balance = AICompany.GetBankBalance(AICompany.COMPANY_SELF);
local loan_amount = AICompany.GetLoanAmount();
local repay_loan = loan_amount - bank_balance > 0 ? loan_amount - bank_balance : 0;
AICompany.SetMinimumLoanAmount(repay_loan);
}
class MoneyTest {
function DoMoneyTest() {
local price = GetPrice();
if (Utils.HasMoney(price)) {
Utils.GetMoney(price);
}
if (DoAction()) {
Utils.RepayLoan();
return true;
}
Utils.RepayLoan();
return false;
}
}
/* WrightAI.nut */
class TestBuildAirport extends MoneyTest {
l = null;
t = null;
i = null;
function DoAction() {
return AIExecMode() && AIAirport.BuildAirport(l, t, i);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIAirport.BuildAirport(l, t, i);
return cost.GetCosts();
}
function TryBuild(airport_location, airport_type, airport_stationId) {
l = airport_location;
t = airport_type;
i = airport_stationId;
if (DoMoneyTest()) {
return true;
}
assert(AIError.GetLastError() != AIError.ERR_STATION_TOO_SPREAD_OUT);
return false;
}
}
class TestBuildAircraft extends MoneyTest {
h = null;
e = null;
v = null;
function DoAction() {
v = AIVehicle.BuildVehicle(h, e);
if (!AIVehicle.IsValidVehicle(v)) {
return false;
}
return true;
}
function GetPrice() {
return AIEngine.GetPrice(e);
}
function TryBuild(best_hangar, engine) {
h = best_hangar;
e = engine;
if (DoMoneyTest()) {
return v;
}
return null;
}
}
class TestRefitAircraft extends MoneyTest {
v = null;
c = null;
function DoAction() {
if (AIExecMode() && AIVehicle.RefitVehicle(v, c)) {
return true;
}
AIVehicle.SellVehicle(v);
return false;
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIVehicle.RefitVehicle(v, c);
return cost.GetCosts();
}
function TryRefit(vehicle, cargoId) {
v = vehicle;
c = cargoId;
return DoMoneyTest();
}
}
class TestRemoveAirport extends MoneyTest {
l = null;
function DoAction() {
if (AIExecMode() && AIAirport.RemoveAirport(l)) {
return true;
}
return false;
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIAirport.RemoveAirport(l);
return cost.GetCosts();
}
function TryRemove(airport_location) {
l = airport_location;
if (DoMoneyTest()) {
return true;
}
return false;
}
}
/* BuildManager.nut */
class TestDemolishTile extends MoneyTest {
l = null;
function DoAction() {
return AIExecMode() && AITile.DemolishTile(l);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AITile.DemolishTile(l);
return cost.GetCosts();
}
function TryDemolish(location) {
l = location;
return DoMoneyTest();
}
}
class TestRemoveRoadStation extends MoneyTest {
l = null;
function DoAction() {
return AIExecMode() && AIRoad.RemoveRoadStation(l);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIRoad.RemoveRoadStation(l);
return cost.GetCosts();
}
function TryRemove(location) {
l = location;
return DoMoneyTest();
}
}
class TestBuildRoad extends MoneyTest {
s = null;
e = null;
function DoAction() {
return AIExecMode() && AIRoad.BuildRoad(s, e);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIRoad.BuildRoad(s, e);
return cost.GetCosts();
}
function TryBuild(start, end) {
s = start;
e = end;
return DoMoneyTest();
}
}
class TestBuildTunnel extends MoneyTest {
t = null;
l = null;
function DoAction() {
return AIExecMode() && AITunnel.BuildTunnel(t, l);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AITunnel.BuildTunnel(t, l);
return cost.GetCosts();
}
function TryBuild(vehicleType, location) {
t = vehicleType;
l = location;
return DoMoneyTest();
}
}
class TestBuildBridge extends MoneyTest {
t = null;
i = null;
s = null;
e = null;
function DoAction() {
return AIExecMode() && AIBridge.BuildBridge(t, i, s, e);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIBridge.BuildBridge(t, i, s, e);
return cost.GetCosts();
}
function TryBuild(vehicleType, bridgeId, start, end) {
t = vehicleType;
i = bridgeId;
s = start;
e = end;
return DoMoneyTest();
}
}
class TestBuildRoadStation extends MoneyTest {
l = null;
e = null;
t = null;
i = null;
function DoAction() {
return AIExecMode() && AIRoad.BuildRoadStation(l, e, t, i);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIRoad.BuildRoadStation(l, e, t, i);
return cost.GetCosts();
}
function TryBuild(location, exit, vehicleType, stationId) {
l = location;
e = exit;
t = vehicleType;
i = stationId;
return DoMoneyTest();
}
}
class TestBuildDriveThroughRoadStation extends MoneyTest {
l = null;
e = null;
t = null;
i = null;
function DoAction() {
return AIExecMode() && AIRoad.BuildDriveThroughRoadStation(l, e, t, i);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIRoad.BuildDriveThroughRoadStation(l, e, t, i);
return cost.GetCosts();
}
function TryBuild(location, exit, vehicleType, stationId) {
l = location;
e = exit;
t = vehicleType;
i = stationId;
return DoMoneyTest();
}
}
class TestBuildRoadDepot extends MoneyTest {
l = null;
e = null;
function DoAction() {
return AIExecMode() && AIRoad.BuildRoadDepot(l, e);
}
function GetPrice() {
local cost = AIAccounting();
AITestMode() && AIRoad.BuildRoadDepot(l, e);
return cost.GetCosts();
}
function TryBuild(location, exit) {
l = location;
e = exit;
return DoMoneyTest();
}
}
/* Route.nut */
class TestBuildRoadVehicle extends MoneyTest {
d = null;
e = null;
v = null;
function DoAction() {
v = AIVehicle.BuildVehicle(d, e);
if (!AIVehicle.IsValidVehicle(v)) {
return false;
}
return true;
}
function GetPrice() {
return AIEngine.GetPrice(e);
}
function TryBuild(depot, engine) {
d = depot;
e = engine;
if (DoMoneyTest()) {
return v;
}
return null;
}
}