/* Original code */
(…)
// local price = AIEngine.GetPrice(engine);
// if (HasMoney(price)) GetMoney(price);
// local vehicle = AIVehicle.BuildVehicle(best_hangar, engine);
// RepayLoan();
// if (!AIVehicle.IsValidVehicle(vehicle)) {
// if (!silent_mode) AILog.Error("Couldn't build the aircraft");
// return -1;
// }
(…)
/* Modified code */
class BuildVehicleMoneyTest {
function DoMoneyTest() {
if (WrightAI.HasMoney(GetPrice())) WrightAI.GetMoney(GetPrice());
if (DoBuildVehicle()) {
WrightAI.RepayLoan();
return true;
}
WrightAI.RepayLoan();
return false;
}
}
class TestBuildVehicle extends BuildVehicleMoneyTest {
h = null;
e = null;
v = null;
p = null;
function DoBuildVehicle() {
v = AIVehicle.BuildVehicle(h, e);
if (!AIVehicle.IsValidVehicle(v)) {
// if (!silent_mode) AILog.Error("Couldn't build the aircraft");
return false;
}
return true;
}
function GetPrice() {
return AIEngine.GetPrice(e);
}
function TryBuild(best_hangar, engine) {
h = best_hangar;
e = engine;
p = GetPrice();
if (DoMoneyTest()) {
return v;
}
return null;
}
}
/* Somewhere else */
(…)
local tvb = TestBuildVehicle();
local vehicle = tvb.TryBuild(best_hangar, engine);
if (vehicle == null) {
if (!silent_mode) AILog.Error("Couldn't build the aircraft");
return -1;
}
(…)