Loading

Paste #p5qgn92bu

  1. diff -r 721cafc243c6 company.nut
  2. --- a/company.nut   Sun Jul 12 15:12:28 2015 +0100
  3. +++ b/company.nut   Sat Sep 26 18:17:56 2015 +0100
  4. @@ -128,9 +128,18 @@
  5.  }
  6.  
  7.  // Update the delivered amount from the monitored amounts.
  8. -function CompanyGoal::UpdateDelivered(mon, comp_id)
  9. +function CompanyGoal::UpdateDelivered(mon, cdata)
  10.  {
  11.      local delivered;
  12. +    local comp_id = cdata.comp_id
  13. +//    foreach (key, value in cdata.won_goals["towns"]) {
  14. +//        GSLog.Info(key);
  15. +//        GSLog.Info(value);
  16. +//    }
  17. +//    foreach (key, value in cdata.won_goals["industries"]) {
  18. +//        GSLog.Info(key);
  19. +//        GSLog.Info(value);
  20. +//    }
  21.      if ("ind" in this.accept) {
  22.          delivered = mon[this.cargo.cid].ind[this.accept.ind];
  23.      } else {
  24. @@ -148,8 +157,20 @@
  25.                  local destination_string_news;
  26.                  if ("town" in this.accept) {
  27.                      destination_string_news = GSText(GSText.STR_TOWN_NAME_NEWS, this.accept.town);
  28. +                    if (this.accept.town in cdata.won_goals["towns"]) {
  29. +                        // key already exists, don't need to create it
  30. +                    } else {
  31. +                        cdata.won_goals["towns"][this.accept.town] <- {};
  32. +                    }
  33. +                    cdata.won_goals["towns"][this.accept.town][this.cargo.cid] <- true;
  34.                  } else {
  35.                      destination_string_news = GSText(GSText.STR_INDUSTRY_NAME_NEWS, this.accept.ind);
  36. +                    if (this.accept.ind in cdata.won_goals["industries"]) {
  37. +                        // key already exists, don't need to create it
  38. +                    } else {
  39. +                        cdata.won_goals["industries"][this.accept.ind] <- {};
  40. +                    }
  41. +                    cdata.won_goals["industries"][this.accept.ind][this.cargo.cid] <- true;
  42.                  }
  43.                  local goal_won_news = GSText(GSText.STR_COMPANY_GOAL_WON_NEWS, cargo.cid,
  44.                                           this.wanted_amount, destination_string_news);
  45. @@ -317,10 +338,14 @@
  46.      comp_id = null;
  47.  
  48.      active_goals = null;
  49. +    won_goals = null;
  50.  
  51.      constructor(comp_id)
  52.      {
  53.          this.active_goals = {};
  54. +        this.won_goals = {};
  55. +        this.won_goals["industries"] <- {}
  56. +        this.won_goals["towns"] <- {}
  57.          this.comp_id = comp_id;
  58.  
  59.          local num_goals = GSController.GetSetting("num_goals");
  60. @@ -425,6 +450,28 @@
  61.      return false;
  62.  }
  63.  
  64. +// Has this goal been won already for this company for the given cargo and accepting resource?
  65. +// @param cargo_id Cargo to check.
  66. +// @param accept Accepting resource to check.
  67. +// @return Whether this goal has been won for this company for the cargo and resource.
  68. +function CompanyData::GoalAlreadyWon(cargo_id, accept)
  69. +{
  70. +    GSLog.Info("Checking if won already");
  71. +    if ("town" in accept) {
  72. +        foreach (location, cargo_table in this.won_goals["towns"]) {
  73. +            continue;
  74. +        }
  75. +    } else {
  76. +        if (accept["ind"] in this.won_goals["industries"]) {
  77. +            if (cargo_id in this.won_goals["industries"][accept["ind"]]) {
  78. +                return true;
  79. +            }
  80. +        }
  81. +    }
  82. +    return false;
  83. +}
  84. +
  85. +
  86.  // Count the number of goals that ask for the given cargo type.
  87.  // @param cargo_id Cargo to check for.
  88.  // @return Number of active goals with the given cargo type.
  89. @@ -490,7 +537,7 @@
  90.      if (this.comp_id in cmon) {
  91.          foreach (num, goal in this.active_goals) {
  92.              if (goal == null) continue;
  93. -            goal.UpdateDelivered(cmon[this.comp_id], this.comp_id);
  94. +            goal.UpdateDelivered(cmon[this.comp_id], this);
  95.              if (goal.CheckFinished()) finished = true;
  96.          }
  97.      }
  98. diff -r 721cafc243c6 main.nut
  99. --- a/main.nut  Sun Jul 12 15:12:28 2015 +0100
  100. +++ b/main.nut  Sat Sep 26 18:17:56 2015 +0100
  101. @@ -243,6 +243,7 @@
  102.      local best_accept = null; // Best accepting to target.
  103.      foreach (_, accept in accepts) {
  104.          if (cdata != null && cdata.HasGoal(cargo.cid, accept)) continue; // Prevent duplicates.
  105. +        if (cdata != null && cdata.GoalAlreadyWon(cargo.cid, accept)) continue; // Prevent repeating goals already won.
  106.  
  107.          local min_prod_distance = distance * 2; // Smallest found distance to the accepting industry.
  108.          local prod_score = best_score;

Comments