Loading

Paste #po0sy3phj

  1. // Global function for checking if SCP is enabled at all
  2. function IsSCPEnabled()
  3. {
  4.     return GSController.GetSetting("scp_support");
  5. }
  6.  
  7.  
  8. // SCP Manager class
  9. class SCPManager {
  10.     _main_ptr = null;
  11.     _scp_enabled = null;
  12.    
  13.    static COMMAND_SET = "Company Value GS";
  14.  
  15.     constructor(main_ptr)
  16.     {
  17.         this._scp_enabled = IsSCPEnabled();
  18.         this._main_ptr = main_ptr;
  19.         this.SetupSCP();
  20.     }
  21.  
  22. }
  23.  
  24. function DataToStr(data) {
  25.     if (data == null) {
  26.         return "null";
  27.     }
  28.  
  29.     local s = "[";
  30.     foreach (d in data) {
  31.         if (s != "[") {
  32.             s += ", ";
  33.         }
  34.        
  35.         if (d == null) {
  36.             s += "null";
  37.         } else if (typeof(d) == "string") {
  38.             s += "\"" + d + "\"";
  39.         } else {
  40.             s += d.tostring();
  41.         }
  42.     }
  43.  
  44.     return s + "]";
  45. }
  46. function SCPManager::SetupSCP()
  47. {
  48.     if (!this._scp_enabled) return;
  49.  
  50.     SCPLib("CVGS", 4, null);
  51.     SCPLib.SCPLogging_Info(this._main_ptr.debug_messages >= 2);
  52.     SCPLib.SCPLogging_Error(this._main_ptr.debug_messages >= 3);
  53.  
  54.     // Register commands
  55.     local self = this;
  56.  
  57.     // AI -> GS commands:
  58.     SCPLib.AddCommand("CurrentGoal", COMMAND_SET, self, SCPManager.ReceivedCurrentGoalCommand);
  59.  
  60.     // GS -> AI commands:
  61. }
  62.  
  63. function SCPManager::Check()
  64. {
  65.     if (!this._scp_enabled) return;
  66.  
  67.     // Update SCP logging in case the log level was changed
  68.     SCPLib.SCPLogging_Info(this._main_ptr.debug_messages >= 2);
  69.     SCPLib.SCPLogging_Error(this._main_ptr.debug_messages >= 3);
  70.  
  71.     // Let SCP check for incoming messages
  72.     if (this._main_ptr.debug_messages >= 3) GSLog.Info("SCP: Check for incoming messages");
  73.     return SCPLib.Check();
  74. }
  75.  
  76. /*****************************************************************
  77.  *                                                               *
  78.  *   Outgoing Commands - commands that we can send to AIs        *
  79.  *                                                               *
  80.  *****************************************************************/
  81.  
  82. // Call one of these methods to send a command to an AI.
  83.  
  84. function SCPManager::SendGoalData()
  85. {
  86.     if (!this._scp_enabled) return false;
  87.  
  88.     for (local to_company = GSCompany.COMPANY_FIRST; to_company < GSCompany.COMPANY_LAST; to_company++) {
  89.         if (GSCompany.ResolveCompanyID(to_company) != GSCompany.COMPANY_INVALID) {
  90.             if (!SCPLib.CanSpeakWith(to_company)) {
  91.                 continue;
  92.             }
  93.             this.SendCurrentGoal(to_company);
  94.         }
  95.     }
  96. }
  97.  
  98. /*
  99.  * @param to_company Which company to send the message to
  100.  * @param answer_to_message if this command is sent in return to a query, pass the query message instance with this parameter
  101.  *
  102.  * See ReceivedCurrentGoalCommand for a detailed explanation of the transmitted data (or just read the code).
  103.  */
  104. function SCPManager::SendCurrentGoal(to_company, answer_to_message = null)
  105. {
  106.     if (!this._scp_enabled) return false;
  107.     if (answer_to_message == null && !SCPLib.CanSpeakWith(to_company)) return false;
  108.  
  109.     local goal_mode = this._main_ptr.goal_mode == null ? true : this._main_ptr.goal_mode;
  110.     local response_value = (this._main_ptr.best_value / 1000).tostring();
  111.  
  112.     local result = [to_company, this._main_ptr.goal_mode, response_value];
  113.  
  114.     if (this._main_ptr.debug_messages >= 2) GSLog.Info("SCP: " + (answer_to_message != null ? "Sending response to message from " : "Telling company ") + GSCompany.GetName(to_company));
  115.     if (this._main_ptr.debug_messages >= 1) GSLog.Info("SCP: Sent CurrentGoal command with data: " + DataToStr(result));
  116.  
  117.     if (answer_to_message != null) {
  118.         SCPLib.Answer(answer_to_message, result);
  119.     } else {
  120.         SCPLib.TellCompany("CurrentGoal", COMMAND_SET, to_company, result);
  121.     }
  122.     return true;
  123. }
  124.  
  125.  
  126. /*****************************************************************
  127.  *                                                               *
  128.  *   Incoming Commands - commands that we can get from AIs       *
  129.  *                                                               *
  130.  *****************************************************************/
  131.  
  132. // These methods are called by the SCP library when we call SCPLib.Check() and there is
  133. // a received incoming message.
  134.  
  135. /*
  136.  * Input:
  137.  *   Data[0] => CompanyID to get goal of
  138.  *
  139.  * Output:
  140.  *   Data[0] => Company ID of the company that these values are valid for
  141.  *   Data[1] => Goal Mode boolean
  142.  *   Data[2] => Goal Value. The value is divided by 1000 and then sent as a string.
  143.  */
  144. function SCPManager::ReceivedCurrentGoalCommand(message, self)
  145. {
  146.     if (!self._scp_enabled) return;
  147.  
  148.     if (self._main_ptr.debug_messages >= 1) GSLog.Info("SCP: Received CurrentGoal command with data: " + DataToStr(message.Data));
  149.     if (self._main_ptr.debug_messages >= 1) GSLog.Info("SCP: " + GSCompany.GetName(message.SenderID) + " asks for the current goal");
  150.  
  151.     if (!self.SendCurrentGoal(message.SenderID, message)) {
  152.         if (self._main_ptr.debug_messages >= 1) GSLog.Info("SCP: Unknown sender asks for its current goal");
  153.  
  154.         // Can't find company - respond to the sender with null to tell
  155.         // that we got the command, but couldn't find any result for the
  156.         // company.
  157.         SCPLib.Answer(message, null);
  158.     }
  159. }

Comments