// Global function for checking if SCP is enabled at all
function IsSCPEnabled()
{
return GSController.GetSetting("scp_support");
}
// SCP Manager class
class SCPManager {
_main_ptr = null;
_scp_enabled = null;
static COMMAND_SET = "Company Value GS";
constructor(main_ptr)
{
this._scp_enabled = IsSCPEnabled();
this._main_ptr = main_ptr;
this.SetupSCP();
}
}
function DataToStr(data) {
if (data == null) {
return "null";
}
local s = "[";
foreach (d in data) {
if (s != "[") {
s += ", ";
}
if (d == null) {
s += "null";
} else if (typeof(d) == "string") {
s += "\"" + d + "\"";
} else {
s += d.tostring();
}
}
return s + "]";
}
function SCPManager::SetupSCP()
{
if (!this._scp_enabled) return;
SCPLib("CVGS", 4, null);
SCPLib.SCPLogging_Info(this._main_ptr.debug_messages >= 2);
SCPLib.SCPLogging_Error(this._main_ptr.debug_messages >= 3);
// Register commands
local self = this;
// AI -> GS commands:
SCPLib.AddCommand("CurrentGoal", COMMAND_SET, self, SCPManager.ReceivedCurrentGoalCommand);
// GS -> AI commands:
}
function SCPManager::Check()
{
if (!this._scp_enabled) return;
// Update SCP logging in case the log level was changed
SCPLib.SCPLogging_Info(this._main_ptr.debug_messages >= 2);
SCPLib.SCPLogging_Error(this._main_ptr.debug_messages >= 3);
// Let SCP check for incoming messages
if (this._main_ptr.debug_messages >= 3) GSLog.Info("SCP: Check for incoming messages");
return SCPLib.Check();
}
/*****************************************************************
* *
* Outgoing Commands - commands that we can send to AIs *
* *
*****************************************************************/
// Call one of these methods to send a command to an AI.
function SCPManager::SendGoalData()
{
if (!this._scp_enabled) return false;
for (local to_company = GSCompany.COMPANY_FIRST; to_company < GSCompany.COMPANY_LAST; to_company++) {
if (GSCompany.ResolveCompanyID(to_company) != GSCompany.COMPANY_INVALID) {
if (!SCPLib.CanSpeakWith(to_company)) {
continue;
}
this.SendCurrentGoal(to_company);
}
}
}
/*
* @param to_company Which company to send the message to
* @param answer_to_message if this command is sent in return to a query, pass the query message instance with this parameter
*
* See ReceivedCurrentGoalCommand for a detailed explanation of the transmitted data (or just read the code).
*/
function SCPManager::SendCurrentGoal(to_company, answer_to_message = null)
{
if (!this._scp_enabled) return false;
if (answer_to_message == null && !SCPLib.CanSpeakWith(to_company)) return false;
local goal_mode = this._main_ptr.goal_mode == null ? true : this._main_ptr.goal_mode;
local response_value = (this._main_ptr.best_value / 1000).tostring();
local result = [to_company, this._main_ptr.goal_mode, response_value];
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));
if (this._main_ptr.debug_messages >= 1) GSLog.Info("SCP: Sent CurrentGoal command with data: " + DataToStr(result));
if (answer_to_message != null) {
SCPLib.Answer(answer_to_message, result);
} else {
SCPLib.TellCompany("CurrentGoal", COMMAND_SET, to_company, result);
}
return true;
}
/*****************************************************************
* *
* Incoming Commands - commands that we can get from AIs *
* *
*****************************************************************/
// These methods are called by the SCP library when we call SCPLib.Check() and there is
// a received incoming message.
/*
* Input:
* Data[0] => CompanyID to get goal of
*
* Output:
* Data[0] => Company ID of the company that these values are valid for
* Data[1] => Goal Mode boolean
* Data[2] => Goal Value. The value is divided by 1000 and then sent as a string.
*/
function SCPManager::ReceivedCurrentGoalCommand(message, self)
{
if (!self._scp_enabled) return;
if (self._main_ptr.debug_messages >= 1) GSLog.Info("SCP: Received CurrentGoal command with data: " + DataToStr(message.Data));
if (self._main_ptr.debug_messages >= 1) GSLog.Info("SCP: " + GSCompany.GetName(message.SenderID) + " asks for the current goal");
if (!self.SendCurrentGoal(message.SenderID, message)) {
if (self._main_ptr.debug_messages >= 1) GSLog.Info("SCP: Unknown sender asks for its current goal");
// Can't find company - respond to the sender with null to tell
// that we got the command, but couldn't find any result for the
// company.
SCPLib.Answer(message, null);
}
}