Change: Move GSGoal creation and removal to the CompanyGoal.

This commit is contained in:
Alberth
2014-12-30 11:01:30 +01:00
parent 78aa73dd2c
commit fd0469ad9b
2 changed files with 30 additions and 35 deletions

View File

@@ -6,11 +6,26 @@ class CompanyGoal {
accept = null; // Accepting resource.
wanted_amount = null; // Amount to deliver for achieving the goal.
delivered_amount = 0; // Amount delivered so far.
goal_id = null;
constructor(cargo_id, accept, wanted_amount) {
constructor(comp_id, cargo_id, accept, wanted_amount) {
this.cargo_id = cargo_id;
this.accept = accept;
this.wanted_amount = wanted_amount;
// Construct goal.
local destination, destination_string, goal_type;
if ("town" in this.accept) {
destination = accept.town;
destination_string = GSText(GSText.STR_TOWN_NAME, destination);
goal_type = GSGoal.GT_TOWN;
} else {
destination = accept.ind;
destination_string = GSText(GSText.STR_INDUSTRY_NAME, destination);
goal_type = GSGoal.GT_INDUSTRY;
}
local goal_text = GSText(GSText.STR_COMPANY_GOAL, this.cargo_id, this.wanted_amount, destination_string);
this.goal_id = GSGoal.New(comp_id, goal_text, goal_type, destination);
}
function AddMonitorElement(mon);
@@ -58,7 +73,7 @@ function CompanyGoal::CheckFinished()
// (to make room for a new goal).
function CompanyGoal::FinalizeGoal()
{
// Nothing to do (yet).
if (this.goal_id != null) GSGoal.Remove(this.goal_id);
}
class CompanyData {
@@ -100,7 +115,7 @@ function CompanyData::AddActiveGoal(cargo_id, accept, amount)
{
foreach (num, goal in this.active_goals) {
if (goal == null) {
this.active_goals[num] = CompanyGoal(cargo_id, accept, amount);
this.active_goals[num] = CompanyGoal(this.comp_id, cargo_id, accept, amount);
break;
}
}

View File

@@ -169,26 +169,15 @@ function FMainClass::CreateChallenge(cid)
if (cdata != null) {
cdata.AddActiveGoal(cargo, accept, amount);
local destination, destination_name, destination_string, goal_type;
local destination_name;
if ("town" in accept) {
destination = accept.town;
destination_name = GSTown.GetName(destination);
destination_string = GSText(GSText.STR_TOWN_NAME, destination);
goal_type = GSGoal.GT_TOWN;
destination_name = GSTown.GetName(accept.town);
} else {
destination = accept.ind;
destination_name = GSIndustry.GetName(destination);
destination_string = GSText(GSText.STR_INDUSTRY_NAME, destination);
goal_type = GSGoal.GT_INDUSTRY;
destination_name = GSIndustry.GetName(accept.ind);
}
GSLog.Info("Company " + cid + ": " + amount + " of " +
GSCargo.GetCargoLabel(this.cargoes[cargo].cid) +
" to " + destination_name);
local goal_text = GSText(GSText.STR_COMPANY_GOAL, cargo, amount, destination_string);
local goal_uid = GSGoal.New(cid, goal_text, goal_type, destination);
// ¿ we're probably going to want to store the GoalID in the company goal data ?
// for now, just log it
GSLog.Info("Goal ID: " + goal_uid)
break;
}
}
@@ -202,15 +191,6 @@ function FMainClass::Start()
this.ExamineCargoes();
// local accept = FindChallenge(0, 50, GSCompany.COMPANY_FIRST); // Mail challenge
// if (accept != null) {
// if ("town" in accept) {
// GSLog.Info("Use town " + GSTown.GetName(accept.town));
// } else {
// GSLog.Info("Use industry " + GSIndustry.GetName(accept.ind));
// }
// }
// Construct empty companies.
this.companies = {};
for (local cid = GSCompany.COMPANY_FIRST; cid <= GSCompany.COMPANY_LAST; cid++) {
@@ -325,7 +305,7 @@ function FMainClass::Start()
// XXX Perhaps check for company events?
GSLog.Info("");
GSLog.Info("Sleeping for " + delay_time + " ticks.");
// GSLog.Info("Sleeping for " + delay_time + " ticks.");
if (delay_time > 0) this.Sleep(delay_time);
companies_timeout -= delay_time;
@@ -346,20 +326,20 @@ function FMainClass::FillMonitors(cmon)
foreach (ind_id, _ in rmon.ind) {
local amount = GSCargoMonitor.GetIndustryDeliveryAmount(comp_id, cargo_id, ind_id, true);
rmon.ind[ind_id] = amount;
local text = "Industry " + GSIndustry.GetName(ind_id) + " received " + amount +
" units for company " + comp_id +
", cargo " + GSCargo.GetCargoLabel(cargo_id);
GSLog.Info(text);
// local text = "Industry " + GSIndustry.GetName(ind_id) + " received " + amount +
// " units for company " + comp_id +
// ", cargo " + GSCargo.GetCargoLabel(cargo_id);
// GSLog.Info(text);
}
}
if ("town" in rmon) {
foreach (town_id, _ in rmon.town) {
local amount = GSCargoMonitor.GetTownDeliveryAmount(comp_id, cargo_id, town_id, true);
rmon.town[town_id] = amount;
local text = "Town " + GSTown.GetName(town_id) + " received " + amount +
" units for company " + comp_id +
", cargo " + GSCargo.GetCargoLabel(cargo_id);
GSLog.Info(text);
// local text = "Town " + GSTown.GetName(town_id) + " received " + amount +
// " units for company " + comp_id +
// ", cargo " + GSCargo.GetCargoLabel(cargo_id);
// GSLog.Info(text);
}
}
}