Loading

Paste #pom6cmewi

  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:08:30 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,34 @@
  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. +        foreach (location, cargo_table in this.won_goals["industries"]) {
  77. +            GSLog.Info(location);
  78. +            if (location == accept["ind"]) {
  79. +                GSLog.Info(cargo_table);
  80. +                GSLog.Info(cargo_id);
  81. +                foreach (cargo, value in cargo_table) {
  82. +                    if (cargo != cargo_id) continue;
  83. +                    return true;
  84. +                }
  85. +            }
  86. +        }
  87. +    }
  88. +    return false;
  89. +}
  90. +
  91. +
  92.  // Count the number of goals that ask for the given cargo type.
  93.  // @param cargo_id Cargo to check for.
  94.  // @return Number of active goals with the given cargo type.
  95. @@ -490,7 +543,7 @@
  96.      if (this.comp_id in cmon) {
  97.          foreach (num, goal in this.active_goals) {
  98.              if (goal == null) continue;
  99. -            goal.UpdateDelivered(cmon[this.comp_id], this.comp_id);
  100. +            goal.UpdateDelivered(cmon[this.comp_id], this);
  101.              if (goal.CheckFinished()) finished = true;
  102.          }
  103.      }
  104. diff -r 721cafc243c6 main.nut
  105. --- a/main.nut  Sun Jul 12 15:12:28 2015 +0100
  106. +++ b/main.nut  Sat Sep 26 18:08:30 2015 +0100
  107. @@ -243,6 +243,7 @@
  108.      local best_accept = null; // Best accepting to target.
  109.      foreach (_, accept in accepts) {
  110.          if (cdata != null && cdata.HasGoal(cargo.cid, accept)) continue; // Prevent duplicates.
  111. +        if (cdata != null && cdata.GoalAlreadyWon(cargo.cid, accept)) continue; // Prevent repeating goals already won.
  112.  
  113.          local min_prod_distance = distance * 2; // Smallest found distance to the accepting industry.
  114.          local prod_score = best_score;

Comments