diff -r 721cafc243c6 company.nut --- a/company.nut Sun Jul 12 15:12:28 2015 +0100 +++ b/company.nut Sat Sep 26 18:08:30 2015 +0100 @@ -128,9 +128,18 @@ } // Update the delivered amount from the monitored amounts. -function CompanyGoal::UpdateDelivered(mon, comp_id) +function CompanyGoal::UpdateDelivered(mon, cdata) { local delivered; + local comp_id = cdata.comp_id +// foreach (key, value in cdata.won_goals["towns"]) { +// GSLog.Info(key); +// GSLog.Info(value); +// } +// foreach (key, value in cdata.won_goals["industries"]) { +// GSLog.Info(key); +// GSLog.Info(value); +// } if ("ind" in this.accept) { delivered = mon[this.cargo.cid].ind[this.accept.ind]; } else { @@ -148,8 +157,20 @@ local destination_string_news; if ("town" in this.accept) { destination_string_news = GSText(GSText.STR_TOWN_NAME_NEWS, this.accept.town); + if (this.accept.town in cdata.won_goals["towns"]) { + // key already exists, don't need to create it + } else { + cdata.won_goals["towns"][this.accept.town] <- {}; + } + cdata.won_goals["towns"][this.accept.town][this.cargo.cid] <- true; } else { destination_string_news = GSText(GSText.STR_INDUSTRY_NAME_NEWS, this.accept.ind); + if (this.accept.ind in cdata.won_goals["industries"]) { + // key already exists, don't need to create it + } else { + cdata.won_goals["industries"][this.accept.ind] <- {}; + } + cdata.won_goals["industries"][this.accept.ind][this.cargo.cid] <- true; } local goal_won_news = GSText(GSText.STR_COMPANY_GOAL_WON_NEWS, cargo.cid, this.wanted_amount, destination_string_news); @@ -317,10 +338,14 @@ comp_id = null; active_goals = null; + won_goals = null; constructor(comp_id) { this.active_goals = {}; + this.won_goals = {}; + this.won_goals["industries"] <- {} + this.won_goals["towns"] <- {} this.comp_id = comp_id; local num_goals = GSController.GetSetting("num_goals"); @@ -425,6 +450,34 @@ return false; } +// Has this goal been won already for this company for the given cargo and accepting resource? +// @param cargo_id Cargo to check. +// @param accept Accepting resource to check. +// @return Whether this goal has been won for this company for the cargo and resource. +function CompanyData::GoalAlreadyWon(cargo_id, accept) +{ + GSLog.Info("Checking if won already"); + if ("town" in accept) { + foreach (location, cargo_table in this.won_goals["towns"]) { + continue; + } + } else { + foreach (location, cargo_table in this.won_goals["industries"]) { + GSLog.Info(location); + if (location == accept["ind"]) { + GSLog.Info(cargo_table); + GSLog.Info(cargo_id); + foreach (cargo, value in cargo_table) { + if (cargo != cargo_id) continue; + return true; + } + } + } + } + return false; +} + + // Count the number of goals that ask for the given cargo type. // @param cargo_id Cargo to check for. // @return Number of active goals with the given cargo type. @@ -490,7 +543,7 @@ if (this.comp_id in cmon) { foreach (num, goal in this.active_goals) { if (goal == null) continue; - goal.UpdateDelivered(cmon[this.comp_id], this.comp_id); + goal.UpdateDelivered(cmon[this.comp_id], this); if (goal.CheckFinished()) finished = true; } } diff -r 721cafc243c6 main.nut --- a/main.nut Sun Jul 12 15:12:28 2015 +0100 +++ b/main.nut Sat Sep 26 18:08:30 2015 +0100 @@ -243,6 +243,7 @@ local best_accept = null; // Best accepting to target. foreach (_, accept in accepts) { if (cdata != null && cdata.HasGoal(cargo.cid, accept)) continue; // Prevent duplicates. + if (cdata != null && cdata.GoalAlreadyWon(cargo.cid, accept)) continue; // Prevent repeating goals already won. local min_prod_distance = distance * 2; // Smallest found distance to the accepting industry. local prod_score = best_score;