Codechange: Rename variables to make them more consistent in meaning.

This commit is contained in:
Alberth
2015-01-01 14:24:53 +01:00
parent b64b314103
commit 5234a754ed
2 changed files with 52 additions and 49 deletions

View File

@@ -2,15 +2,15 @@
// A goal for a company.
class CompanyGoal {
cargo_id = null; // Cargo id
cargo = null; // Cargo data (#Cargo)
accept = null; // Accepting resource.
wanted_amount = null; // Amount to deliver for achieving the goal.
delivered_amount = 0; // Amount delivered so far.
goal_id = null; // Number of the goal in OpenTTD goal window.
timeout = null; // Timeout in ticks before the goal becomes obsolete.
constructor(comp_id, cargo_id, accept, wanted_amount, cargoes) {
this.cargo_id = cargo_id;
constructor(comp_id, cargo, accept, wanted_amount) {
this.cargo = cargo;
this.accept = accept;
this.wanted_amount = wanted_amount;
this.timeout = 60 * 30 * 74; // 60 months timeout (30 days, 74 ticks).
@@ -26,7 +26,7 @@ class CompanyGoal {
destination_string = GSText(GSText.STR_INDUSTRY_NAME, destination);
goal_type = GSGoal.GT_INDUSTRY;
}
local goal_text = GSText(GSText.STR_COMPANY_GOAL, cargoes[this.cargo_id].cid, this.wanted_amount, destination_string);
local goal_text = GSText(GSText.STR_COMPANY_GOAL, cargo.cid, this.wanted_amount, destination_string);
this.goal_id = GSGoal.New(comp_id, goal_text, goal_type, destination);
}
@@ -41,8 +41,8 @@ class CompanyGoal {
// @param [inout] mon Table with 'cargo_id' to 'town' and 'ind' tables, holding ids to 'null'.
function CompanyGoal::AddMonitorElement(mon)
{
if (!(this.cargo_id in mon)) mon[this.cargo_id] <- {};
mon = mon[this.cargo_id];
if (!(this.cargo.cid in mon)) mon[this.cargo.cid] <- {};
mon = mon[this.cargo.cid];
if ("ind" in this.accept) {
if (!("ind" in mon)) mon.ind <- {};
mon.ind[this.accept.ind] <- null;
@@ -57,9 +57,9 @@ function CompanyGoal::UpdateDelivered(mon)
{
local delivered;
if ("ind" in this.accept) {
delivered = mon[this.cargo_id].ind[this.accept.ind];
delivered = mon[this.cargo.cid].ind[this.accept.ind];
} else {
delivered = mon[this.cargo_id].town[this.accept.town];
delivered = mon[this.cargo.cid].town[this.accept.town];
}
if (delivered > 0) {
@@ -112,25 +112,26 @@ class CompanyData {
active_goals = null;
constructor(comp_id) {
constructor(comp_id)
{
this.active_goals = {};
this.comp_id = comp_id;
for (local num = 0; num < 5; num += 1) this.active_goals[num] <- null;
}
function FinalizeCompany();
function GetMissingGoalCount();
function AddActiveGoal(cargo_id, accept, amount, cargoes);
function AddActiveGoal(cargo, accept, amount);
function HasGoal(cargo_id, accept);
function GetNumberOfGoalsForCargo(cargo_id);
function IndustryClosed(ind_id);
function AddMonitorElement(mon);
function UpdateDelivered(mon);
function AddMonitorElements(cmon);
function UpdateDelivereds(cmon);
function UpdateTimeout(step);
function CheckAndFinishGoals();
function FinalizeCompany();
};
// Company is about to be deleted, last chance to clean up.
@@ -157,28 +158,28 @@ function CompanyData::GetMissingGoalCount()
}
// Add a goal to the list of the company.
// @param cargo_id Cargo of the goal.
// @param cargo Cargo of the goal (#Cargo).
// @param accept Accepting resource of the goal.
// @param amount Amount of cargo to deliver.
function CompanyData::AddActiveGoal(cargo_id, accept, amount, cargoes)
// @param wanted_amount Amount of cargo to deliver.
function CompanyData::AddActiveGoal(cargo, accept, wanted_amount)
{
foreach (num, goal in this.active_goals) {
if (goal == null) {
this.active_goals[num] = CompanyGoal(this.comp_id, cargo_id, accept, amount, cargoes);
this.active_goals[num] = CompanyGoal(this.comp_id, cargo, accept, wanted_amount);
break;
}
}
}
// Does the company have an active goal for the given cargo and accepting resource?
// @param cargo_id Cargo to check for.
// @param cargo_id Cargo to check.
// @param accept Accepting resource to check.
// @return Whether the company has a goal for the cargo and resource.
function CompanyData::HasGoal(cargo_id, accept)
{
foreach (num, goal in this.active_goals) {
if (goal == null) continue;
if (goal.cargo_id != cargo_id) continue;
if (goal.cargo.cid != cargo_id) continue;
if ("town" in accept) {
if ("ind" in goal.accept || accept.town != goal.accept.town) continue;
} else {
@@ -197,7 +198,7 @@ function CompanyData::GetNumberOfGoalsForCargo(cargo_id)
local count = 0;
foreach (num, goal in this.active_goals) {
if (goal == null) continue;
if (goal.cargo_id == cargo_id) count += 1;
if (goal.cargo.cid == cargo_id) count += 1;
}
return count;
}

View File

@@ -45,12 +45,12 @@ function BusyBeeClass::ExamineCargoes()
}
// Find cargo sources.
// @param cargo_id Cargo index (index in this.cargoes).
// @param cargo_index Cargo index (index in this.cargoes).
// @return List of resources that produce the requested cargo, list of
// 'ind' or 'town' number, 'prod' produced amount, 'transp' transported amount, and 'loc' location.
function BusyBeeClass::FindSources(cargo_id)
function BusyBeeClass::FindSources(cargo_index)
{
local cargo = this.cargoes[cargo_id];
local cargo = this.cargoes[cargo_index];
local num_sources = 0;
local sources = {};
@@ -93,12 +93,12 @@ function BusyBeeClass::FindSources(cargo_id)
}
// Find destinations for the cargo.
// @param cargo_id Cargo index (index in this.cargoes).
// @param cargo_index Cargo index (index in this.cargoes).
// @param company Company to inspect.
// @return A list of destinations, tables 'ind' or 'town' id, and a 'loc' location.
function BusyBeeClass::FindDestinations(cargo_id, company)
function BusyBeeClass::FindDestinations(cargo_index, company)
{
local cargo = this.cargoes[cargo_id];
local cargo = this.cargoes[cargo_index];
local num_dests = 0;
local dests = {};
@@ -141,23 +141,24 @@ function BusyBeeClass::GetDistanceScore(desired, actual)
}
// Try to find a challenge for a given cargo and a desired distance.
// @param cargo Cargo entry from BusyBeeClass.cargoes (#Cargo instance).
// @param cargo_index Index in BusyBeeClass.cargoes.
// @param distance Desired distance between source and target.
// @param cid Company to find a challenge for.
// @param comp_id Company to find a challenge for.
// @return Best accepting industry to use, or 'null' if no industry-pair found.
function BusyBeeClass::FindChallenge(cargo_id, distance, cid)
function BusyBeeClass::FindChallenge(cargo_index, distance, comp_id)
{
local prods = this.FindSources(cargo_id);
local prods = this.FindSources(cargo_index);
if (prods.len() == 0) return null;
local accepts = this.FindDestinations(cargo_id, cid);
local accepts = this.FindDestinations(cargo_index, comp_id);
if (accepts.len() == 0) return null;
local cdata = this.companies[cid];
local cdata = this.companies[comp_id];
local cargo = this.cargoes[cargo_index];
local best_score = 0; // Best overall distance.
local best_accept = null; // Best accepting to target.
foreach (_, accept in accepts) {
if (cdata != null && cdata.HasGoal(cargo_id, accept)) continue; // Prevent duplicates.
if (cdata != null && cdata.HasGoal(cargo.cid, accept)) continue; // Prevent duplicates.
local min_prod_distance = distance * 2; // Smallest found distance to the accepting industry.
local prod_score = best_score;
@@ -180,14 +181,16 @@ function BusyBeeClass::FindChallenge(cargo_id, distance, cid)
}
// Try to add a goal for a company.
function BusyBeeClass::CreateChallenge(cid)
// @param comp_id Company to find a challenge for.
function BusyBeeClass::CreateChallenge(comp_id)
{
local cdata = this.companies[cid];
local cdata = this.companies[comp_id];
for (local attempt = 0;attempt < 20; attempt += 1) {
local cargo = GSBase.RandRange(this.num_cargoes);
if (cdata.GetNumberOfGoalsForCargo(cargo) > 1) continue; // Already 2 goals for this cargo.
local cargo_index = GSBase.RandRange(this.num_cargoes);
local cargo = this.cargoes[cargo_index];
if (cdata.GetNumberOfGoalsForCargo(cargo.cid) > 1) continue; // Already 2 goals for this cargo.
local distance = GSBase.RandRange(200) + 50; // Distance 50 .. 250 tiles.
local accept = FindChallenge(cargo, distance, cid);
local accept = FindChallenge(cargo_index, distance, comp_id);
if (accept != null) {
local amount = GSBase.RandRange(100) + 1;
if (amount < 10) {
@@ -198,17 +201,16 @@ function BusyBeeClass::CreateChallenge(cid)
amount = 10 * 25 + 35 * 50 + (amount - 10 - 35) * 100; // 2000..7500
}
if (cdata != null) {
cdata.AddActiveGoal(cargo, accept, amount, this.cargoes);
cdata.AddActiveGoal(cargo, accept, amount);
local destination_name;
local dest_name;
if ("town" in accept) {
destination_name = GSTown.GetName(accept.town);
dest_name = GSTown.GetName(accept.town);
} else {
destination_name = GSIndustry.GetName(accept.ind);
dest_name = GSIndustry.GetName(accept.ind);
}
GSLog.Info("Company " + cid + ": " + amount + " of " +
GSCargo.GetCargoLabel(this.cargoes[cargo].cid) +
" to " + destination_name);
GSLog.Info("Company " + comp_id + ": " + amount + " of " +
GSCargo.GetCargoLabel(cargo.cid) + " to " + dest_name);
break;
}
}
@@ -325,8 +327,8 @@ function BusyBeeClass::Start()
// Construct empty companies.
this.companies = {};
for (local cid = GSCompany.COMPANY_FIRST; cid <= GSCompany.COMPANY_LAST; cid++) {
this.companies[cid] <- null;
for (local comp_id = GSCompany.COMPANY_FIRST; comp_id <= GSCompany.COMPANY_LAST; comp_id++) {
this.companies[comp_id] <- null;
}
// Main event loop.
@@ -361,7 +363,7 @@ function BusyBeeClass::Start()
// Check for finished goals, and remove them if they exist.
if (finished_timeout <= 0) {
foreach (cid, cdata in companies) {
foreach (comp_id, cdata in companies) {
if (cdata == null) continue;
cdata.CheckAndFinishGoals();
}