Fix: Prevent duplicate cargo/resource combinations for a company.

This commit is contained in:
Alberth
2014-12-28 17:22:54 +01:00
parent 744419dd92
commit 2e13920e38
2 changed files with 33 additions and 3 deletions

View File

@@ -13,8 +13,11 @@ class CompanyData {
function GetMissingGoalCount();
function AddActiveGoal(cargo_id, accept, amount);
function HasGoal(cargo_id, accept);
};
// Find the number of active goals that are missing for this company.
// @return Number of additional goals that the company needs.
function CompanyData::GetMissingGoalCount()
{
local missing = 0;
@@ -24,6 +27,10 @@ function CompanyData::GetMissingGoalCount()
return missing;
}
// Add a goal to the list of the company.
// @param cargo_id Cargo of the goal.
// @param accept Accepting resource of the goal.
// @param amount Amount of cargo to deliver.
function CompanyData::AddActiveGoal(cargo_id, accept, amount)
{
foreach (num, goal in this.active_goals) {
@@ -33,3 +40,22 @@ function CompanyData::AddActiveGoal(cargo_id, accept, amount)
}
}
}
// Does the company have an active goal for the given cargo and accepting resource?
// @param cargo_id Cargo to check for.
// @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.cid != cargo_id) continue;
if ("town" in accept) {
if ("ind" in goal.accept || accept.town != goal.accept.town) continue;
} else {
if ("town" in goal.accept || accept.ind != goal.accept.ind) continue;
}
return true;
}
return false;
}