Loading

Paste #ppdvctj9y

  1.  
  2. // Global function for checking if SCP is enabled at all
  3. function IsSCPEnabled()
  4. {
  5.     return 1;
  6. }
  7.  
  8.  
  9. // SCP Manager class
  10. class SCPManager {
  11.     _main_ptr = null;
  12.     _scp_enabled = null;
  13.    
  14.    static COMMAND_SET = "Company Value GS";
  15.  
  16.     constructor(main_ptr)
  17.     {
  18.         this._scp_enabled = IsSCPEnabled();
  19.         this._main_ptr = main_ptr;
  20.         this.SetupSCP();
  21.     }
  22.  
  23. }
  24.  
  25. function DataToStr(data)
  26. {
  27.     if (data == null)
  28.         return "NULL";
  29.  
  30.     local s = "[";
  31.     foreach(d in data)
  32.     {
  33.         if (s != "[") s += ", ";
  34.         if (d == null)
  35.             s += "NULL";
  36.         else if(typeof(d) == "string")
  37.             s += "\"" + d + "\"";
  38.         else
  39.             s += d.tostring();
  40.     }
  41.  
  42.     return s + "]";
  43. }
  44.  
  45. function SCPManager::SetupSCP()
  46. {
  47.     if (!this._scp_enabled) return;
  48.  
  49.     // Initialize the library itself
  50.     local dummy = SCPLib("CVGS", 4, null); // yes, call constructor and then throw away instance.
  51.     local show_debug = true;
  52.     SCPLib.SCPLogging_Info(show_debug); // <-- static call to library
  53.     SCPLib.SCPLogging_Error(true);
  54.  
  55.     // Register commands
  56.     local self = this;
  57.  
  58.     // AI -> GS commands:
  59. //  SCPLib.AddCommand("CurrentGoal", COMMAND_SET, self, SCPManager.ReceivedCurrentGoalCommand);
  60.     SCPLib.AddCommand("Setting", COMMAND_SET, self, SCPManager.ReceivedSettingCommand);
  61.  
  62.     // GS -> AI commands:
  63.     SCPLib.AddCommand("GoalReached", COMMAND_SET, self);
  64. }
  65.  
  66. function SCPManager::Check()
  67. {
  68.     if (!this._scp_enabled) return;
  69.  
  70.     // Update SCP logging in case the log level was changed
  71.     local show_debug = true;
  72.     SCPLib.SCPLogging_Info(show_debug);
  73.  
  74.     // Let SCP check for incoming messages
  75.     return SCPLib.Check();
  76. }
  77.  
  78. /*****************************************************************
  79.  *                                                               *
  80.  *   Outgoing Commands - commands that we can send to AIs        *
  81.  *                                                               *
  82.  *****************************************************************/
  83.  
  84. // Call one of these methods to send a command to an AI.
  85.  
  86.  
  87. /*
  88.  * Tells a company that it has reached the transport goal for the specified cargo
  89.  * @param to_company Company ID of the company to inform
  90.  * @param cargo_id Cargo ID of the cargo that was completed
  91.  * @param date Date when the goal was detected to be completed. (date is in the from and data type as returned form GSDate.GetDate())
  92.  */
  93. function SCPManager::SendGoalReached(to_company, c_id, goal_value, days_taken)
  94. {
  95.     if (!this._scp_enabled) return false;
  96.     if (!SCPLib.CanSpeakWith(to_company)) return false;
  97.  
  98.     //GSController.Break("Tell about goal completed: " + GSCargo.GetCargoLabel(cargo_id));
  99.     SCPLib.TellCompany("GoalReached", COMMAND_SET, to_company, [c_id, goal_value, days_taken]);
  100. }
  101.  
  102. /*
  103.  * @param to_company Which company to send the message to
  104.  * @param for_company Which company to lookup the goals for
  105.  * @param answer_to_message if this command is sent in return to a query, pass the query message instance with this parameter
  106.  *
  107.  * See ReceivedCurrentGoalCommand for a detailed explanation of the transmitted data (or just read the code).
  108.  */
  109. function SCPManager::SendCurrentGoal(to_company, for_company, answer_to_message = null)
  110. {
  111.     if (!this._scp_enabled) return false;
  112.     if (answer_to_message == null && !SCPLib.CanSpeakWith(to_company)) return false;
  113.  
  114.     // Find the data for company
  115.     foreach(company_data in this._main_ptr._company_list)
  116.     {
  117.         if(company_data._company_id == for_company)
  118.         {
  119.             local date = GSDate.GetCurrentDate();
  120.             local days_left = this._main_ptr._goal_data.GetDaysLeft();
  121.  
  122.             //              company id   date  days left  goal 0    goal 1    goal 2
  123.             local result = [for_company, date, days_left, 0, 0, 0,  0, 0, 0,  0, 0, 0];
  124.  
  125.             for(local i = 0; i < 3; i++)
  126.             {
  127.                 local cargo = this._main_ptr._goal_data._cargo_list[i];
  128.                 local target = GSController.GetSetting("transport_target")
  129.                 local transported = company_data._transported_list[i];
  130.  
  131.                 result[3 + i * 3    ] = cargo;
  132.                 result[3 + i * 3 + 1] = target;
  133.                 result[3 + i * 3 + 2] = transported;
  134.             }
  135.  
  136.             local s = "";
  137.             for(local i = 0; i < result.len(); i++)
  138.             {
  139.                 if (i == 3) s+= "  |  ";
  140.                 else if (i == 3 + 3) s+= "  |  ";
  141.                 else if (i == 3 + 6) s+= "  |  ";
  142.                 else if (s != "") s+=", ";
  143.                 s += "" + result[i];
  144.             }
  145.             Log.Info(answer_to_message != null? "send response to message" : "tell company", Log.LVL_DEBUG);
  146.             Log.Info("sent CurrentGoal: " + s, Log.LVL_SUB_DECISIONS);
  147.  
  148.            
  149.             if(answer_to_message != null)
  150.                 SCPLib.Answer(answer_to_message, result);
  151.             else
  152.                 SCPLib.TellCompany("CurrentGoal", COMMAND_SET, to_company, result);
  153.             return true;
  154.         }
  155.     }
  156.  
  157.     return false;
  158. }
  159.  
  160.  
  161. /*****************************************************************
  162.  *                                                               *
  163.  *   Incoming Commands - commands that we can get from AIs       *
  164.  *                                                               *
  165.  *****************************************************************/
  166.  
  167. // These methods are called by the SCP library when we call SCPLib.Check() and there is
  168. // a received incoming message.
  169.  
  170. /*
  171.  * Input:
  172.  *   Data[0] => CompanyID to get goal of
  173.  *
  174.  * Output:
  175.  *   Data[0] => Company ID of the company that these values are valid for
  176.  *   Data[1] => Date when the goals was sent
  177.  *   Data[2] => Days left of the game. (lower bound, a few extra days may happen due to frequency of checks)
  178.  *
  179.  *   Data[3] => Cargo of goal 1
  180.  *   Data[4] => Amount of transported cargo for goal 1
  181.  *   Data[5] => Target transport amount for goal 1
  182.  *   Data[6] => Cargo of goal 2
  183.  *   ...
  184.  *   Data[11] => Target transport amount for goal 3
  185.  *
  186.  * Note that also accomplished goals are included in the output.
  187.  * If [transported amount] > [target amount], then the goal has already been accomplished.
  188.  */
  189. function SCPManager::ReceivedCurrentGoalCommand(message, self)
  190. {
  191.     if (!self._scp_enabled) return;
  192.  
  193.     if(Log.IsLevelAccepted(Log.LVL_SUB_DECISIONS))
  194.     {
  195.         Log.Info("Received CurrentGoal Command with data: " + DataToStr(message.Data), Log.LVL_INFO);
  196.     }
  197.  
  198.     // If first param is available, use it as company ID, else
  199.     // use SenderID.
  200.     local query_company = message.GetIntData(0);
  201.     if(query_company == null) query_company = message.SenderID;
  202.  
  203.     Log.Info(GSCompany.GetName(message.SenderID) + " asks for the current goal of " + GSCompany.GetName(query_company), Log.LVL_DEBUG);
  204.  
  205.     if(!self.SendCurrentGoal(message.SenderID, query_company, message))
  206.     {
  207.         Log.Info("Unknown sender asks for its current goal", Log.LVL_INFO);
  208.  
  209.         // Can't find company - respond to the sender with null to tell
  210.         // that we got the command, but couldn't find any result for the
  211.         // company.
  212.         SCPLib.Answer(message, null);
  213.     }
  214. }
  215.  
  216. /*
  217.  * Input:
  218.  *   Data[0] => string to pass to GSController.GetSetting(setting)
  219.  *   Data[1] => new value of setting (only accepted by some settings)
  220.  *
  221.  * Output:
  222.  *   Data[0] => setting name string
  223.  *   Data[1] => setting value
  224.  *
  225.  * If the setting doesn't exist, Data[0] will contain the setting name and
  226.  * Data[1] will contain null.
  227.  */
  228. function SCPManager::ReceivedSettingCommand(message, self)
  229. {
  230.     if (!self._scp_enabled) return;
  231.  
  232.     if(true)
  233.     {
  234.         GSLog.Info("Received Setting Command with data: " + DataToStr(message.Data));
  235.     }
  236.  
  237.     // Get setting string
  238.     local setting = message.GetStringData(0);
  239.     local response_value = null;
  240.     if(setting != null)
  241.     {
  242.         // fake setting?
  243.         if (setting == "version")
  244.         {
  245.             response_value = 4;
  246.         }
  247.         else if(setting == "goal_mode")
  248.         {
  249.             response_value = CompanyValue.goal_mode;
  250.         }
  251.         else if(setting == "goal_value")
  252.         {
  253.             response_value = CompanyValue.best_value;
  254.         }
  255. //      else if(setting == "ai_monthly_report")
  256. //      {
  257. //          local from = message.SenderID;
  258. //          local from_company = self._main_ptr.GetCompanyData(from);
  259. //          if (from_company != null)
  260. //          {
  261. //              local new_value = message.GetBoolData(1);
  262. //              if (new_value != null)
  263. //              {
  264. //                  from_company.SetAIMonthlyReport(new_value);
  265. //              }
  266. //
  267. //              response_value = from_company.GetAIMonthlyReport();
  268. //              Log.Info("Received request to update monthly AI report for " + GSCompany.GetName(from_company._company_id) + " to " + new_value + " | setting value after update: " + response_value, Log.LVL_SUB_DECISIONS);
  269. //          }
  270. //      }  
  271. //      else if(setting == "play_years") // only white listed GSSetting settings are allowed
  272. //      {
  273. //          response_value = GSController.GetSetting(setting);
  274. //      }
  275.         else
  276.         {
  277.             Log.Info("AI company " + GSCompany.GetName(message.SenderID) + " asked for unknown setting \"" + setting + "\"", Log.LVL_INFO);
  278.         }
  279.  
  280. //      if (response_value != null)
  281.             GSLog.Info(GSCompany.GetName(message.SenderID) + " asked for setting value of \"" + setting + "\" which has the value: " + response_value);
  282.     }
  283.  
  284.     // Answer with the setting value
  285.     SCPLib.Answer(message, setting, response_value);
  286. }

Comments