Loading

Paste #pmacpimxt

  1. class SCPClient_CompanyValueGS {
  2.  
  3.     /* Private members variables */
  4.     static COMMAND_SET = "Company Value GS";
  5.  
  6.     _scp = null;                   // SCPLib instance
  7.     _goal_mode = null;
  8.     _goal_value = null;
  9.     _company_value_gs_game = null; // Is this a Company Value GS game? (true/null)
  10.  
  11.     /**
  12.      * Library constructor
  13.      * @param scp_ptr Instance of SCPLib. If null is passed, the library
  14.      * will act as if no Company Value GS has been detected.
  15.      */
  16.     constructor(scp_ptr)
  17.     {
  18.         this._goal_mode = null;
  19.         this._goal_value = null;
  20.         this._company_value_gs_game = null;
  21.  
  22.         this._scp = scp_ptr;
  23.  
  24.         this.RegisterCommands();
  25.         this.AskForGoals();
  26.     }
  27.  
  28.     /* Public methods */
  29.  
  30.     /**
  31.      * Is this a Company Value GS game?
  32.      * @return true, if the game has a GS that respond to Company Value GS
  33.      * commands, otherwise false.
  34.      * @note If you call this method too early, it will return false even if
  35.      * the game has the Company Value GS with SCP activated because a round-trip
  36.      * has not yet been completed.
  37.      */
  38.     function IsCompanyValueGSGame();
  39.  
  40.     /**
  41.      * Is Company Value GS running in Goal mode?
  42.      * In this mode, companies are competing to become the first to reach a
  43.      * company value target. See GetCurrentTargetValue to get the value.
  44.      * @return true, if Company Value GS is running in Goal mode.
  45.      * @note When it returns false, it doesn't necessarily mean that Company Value GS
  46.      * is running in Ranking mode.
  47.      * @note When the target value is reached by one of the companies, Company Value GS
  48.      * pauses the game, and switches from Goal mode to Ranking mode after a user presses
  49.      * the "Continue" button. Note that AIs are unable to respond when the game is paused.
  50.      */
  51.     function IsCompanyValueGSInGoalMode();
  52.  
  53.     /**
  54.      * Is Company Value GS running in Ranking mode?
  55.      * In this mode, companies are competing between each other to be the most
  56.      * valuable company at all times. See GetBestCompanyValue to get the value.
  57.      * @return true, if Company Value GS is running in Ranking mode.
  58.      * @note When it returns false, it doesn't necessarily mean that Company Value GS
  59.      * is running in Goal mode.
  60.      */
  61.     function IsCompanyValueGSInRankingMode();
  62.  
  63.     /**
  64.      * Get the Company ID of the current most valuable company.
  65.      * @return CompanyID of the company with the highest value at the moment.
  66.      * @note Returns AICompany.COMPANY_INVALID when Company Value GS SCP isn't active,
  67.      * or when no companies are found (shouldn't happen, because we're a company too).
  68.      */
  69.     function GetBestCompanyID();
  70.  
  71.     /**
  72.      * Get the value of the current most valuable company.
  73.      * @return integer value of the company with the best value at the moment.
  74.      * @note Returns -1 when Company Value GS SCP isn't active, or when no companies
  75.      * are found (shouldn't happen, because we're a company too).
  76.      */
  77.     function GetBestCompanyValue();
  78.  
  79.     /**
  80.      * Get the current targeted value to be reached.
  81.      * In Goal mode, the value is defined in the Company Value GS settings.
  82.      * In Ranking mode, the value is that of the current most valuable company.
  83.      * @return integer value of the current target to be reached.
  84.      * @note Returns -1 when Company Value GS SCP isn't active, or when no companies
  85.      * are found (shouldn't happen, because we're a company too).
  86.      */
  87.     function GetCurrentTargetValue();
  88.  
  89.     /**
  90.      * Get a list of companies, sorted by their respective values in descending order.
  91.      * @return AIList with Company ID as the item, and Company Value as the value
  92.      * @note Returns an empty AIList when Company Value GS SCP isn't active, or when
  93.      * no companies are found (shouldn't happen, because we're a company too).
  94.      */
  95.     function GetRankingList();
  96.  
  97.     /**
  98.      * Get the rank position of the provided Company ID from a list of running
  99.      * companies with their respective company values.
  100.      * @param company_id The Company ID to get the rank of.
  101.      * @return integer position of provided company in relation to the
  102.      * others regarding their company value.
  103.      * @note The rank at the top has a value of 1. The rank at the bottom has a value
  104.      * dependent on the number of companies running in the game.
  105.      * @note Returns -1 when the provided Company ID is invalid, or when no companies
  106.      * are found (shouldn't happen, because we're a company too).
  107.      */
  108.     function GetCompanyIDRank(company_id);
  109.    
  110.     /**
  111.      * Get the company value of the provided Company ID.
  112.      * @param company_id The Company ID to get the company value of.
  113.      * @return integer value of the specified company.
  114.      * @note Returns -1 when Company Value GS SCP isn't active, or when the provided
  115.      * Company ID is invalid.
  116.      */
  117.     function GetCompanyIDValue(company_id);
  118.  
  119.     /**
  120.      * Get the difference in company value between the current targeted value and the
  121.      * provided Company ID's value.
  122.      * @param company_id The Company ID in which the difference is based of.
  123.      * @return integer value of the difference in company value between the current
  124.      * targeted value and the specified Company ID's value.
  125.      * @note The difference is always equal or higher than zero.
  126.      * @note Returns -1 when Company Value GS SCP isn't active, or when the provided
  127.      * Company ID is invalid.
  128.      */
  129.     function GetCompanyIDDiffToTarget(company_id);
  130.  
  131.     /**
  132.      * Get the difference in company value between the value of the best company and
  133.      * the provided Company ID's value.
  134.      * @param company_id The Company ID in which the difference is based of.
  135.      * @return integer value of the difference in company value between the best
  136.      * company and the specified Company ID.
  137.      * @note The difference is always equal or higher than zero.
  138.      * @note Returns -1 when Company Value GS SCP isn't active, or when the provided
  139.      * Company ID is invalid.
  140.      */
  141.     function GetCompanyIDDiffToBest(company_id);
  142. }
  143.  
  144. /**** Private methods: ****/
  145.  
  146. function SCPClient_CompanyValueGS::RegisterCommands()
  147. {
  148.     if (this._scp == null) return;
  149.  
  150.     local self = this;
  151.  
  152.     // AI -> GS commands:
  153.     this._scp.AddCommand("CurrentGoal", COMMAND_SET, self, SCPClient_CompanyValueGS.ReceivedCurrentGoalCommand);
  154.  
  155.     // GS -> AI commands:
  156. }
  157.  
  158. function SCPClient_CompanyValueGS::AskForGoals()
  159. {
  160.     if (this._scp == null) return;
  161.  
  162.     this._scp.QueryServer("CurrentGoal", COMMAND_SET, [AICompany.ResolveCompanyID(AICompany.COMPANY_SELF)]);
  163. }
  164.  
  165. /**** Public API methods: ****/
  166.  
  167. function SCPClient_CompanyValueGS::IsCompanyValueGSGame()
  168. {
  169.     if (this._scp == null) return false;
  170.  
  171.     return this._company_value_gs_game == true;
  172. }
  173.  
  174. function SCPClient_CompanyValueGS::IsCompanyValueGSInGoalMode()
  175. {
  176.     if (this._scp == null) return false;
  177.  
  178.     return this._goal_mode == true;
  179. }
  180.  
  181. function SCPClient_CompanyValueGS::IsCompanyValueGSInRankingMode()
  182. {
  183.     if (this._scp == null) return false;
  184.  
  185.     return this._goal_mode == false;
  186. }
  187.  
  188. function SCPClient_CompanyValueGS::GetBestCompanyID()
  189. {
  190.     if (this._scp == null) return AICompany.COMPANY_INVALID;
  191.     if (this._company_value_gs_game != true) return AICompany.COMPANY_INVALID;
  192.  
  193.     local best_company_id = AICompany.COMPANY_INVALID;
  194.     local best_company_value = -1;
  195.     for (local c_id = AICompany.COMPANY_FIRST; c_id < AICompany.COMPANY_LAST; c_id++) {
  196.         if (AICompany.ResolveCompanyID(c_id) != AICompany.COMPANY_INVALID) {
  197.             local c_value = AICompany.GetQuarterlyCompanyValue(c_id, AICompany.CURRENT_QUARTER);
  198.             if (c_value >= best_company_value) {
  199.                 best_company_id = c_id;
  200.                 best_company_value = c_value;
  201.             }
  202.         }
  203.     }
  204.     return best_company_id;
  205. }
  206.  
  207. function SCPClient_CompanyValueGS::GetBestCompanyValue()
  208. {
  209.     if (this._scp == null) return -1;
  210.     if (this._company_value_gs_game != true) return -1;
  211.  
  212.     local best_company_value = -1;
  213.     for (local c_id = AICompany.COMPANY_FIRST; c_id < AICompany.COMPANY_LAST; c_id++) {
  214.         if (AICompany.ResolveCompanyID(c_id) != AICompany.COMPANY_INVALID) {
  215.             local c_value = AICompany.GetQuarterlyCompanyValue(c_id, AICompany.CURRENT_QUARTER);
  216.             if (c_value >= best_company_value) {
  217.                 best_company_value = c_value;
  218.             }
  219.         }
  220.     }
  221.     return best_company_value;
  222. }
  223.  
  224. function SCPClient_CompanyValueGS::GetCurrentTargetValue()
  225. {
  226.     if (this._scp == null) return -1;
  227.     if (this._company_value_gs_game != true) return -1;
  228.  
  229.     if (this._goal_mode == true) return this._goal_value;
  230.     return this.GetBestCompanyValue();
  231. }
  232.  
  233. function SCPClient_CompanyValueGS::GetRankingList()
  234. {
  235.     if (this._scp == null) return AIList();
  236.     if (this._company_value_gs_game != true) return AIList();
  237.  
  238.     local global_list = AIList();  
  239.     for (local c_id = AICompany.COMPANY_FIRST; c_id < AICompany.COMPANY_LAST; c_id++) {
  240.         if (AICompany.ResolveCompanyID(c_id) != AICompany.COMPANY_INVALID) {
  241.             local c_value = AICompany.GetQuarterlyCompanyValue(c_id, AICompany.CURRENT_QUARTER);
  242.             global_list.AddItem(c_id, c_value);
  243.         }
  244.     }
  245.     global_list.Sort(AIList.SORT_BY_VALUE, AIList.SORT_DESCENDING);
  246.     return global_list;
  247. }
  248.  
  249. function SCPClient_CompanyValueGS::GetCompanyIDRank(company_id)
  250. {
  251.     if (this._scp == null) return -1;
  252.     if (this._company_value_gs_game != true) return -1;
  253.  
  254.     if (AICompany.ResolveCompanyID(company_id) == AICompany.COMPANY_INVALID) return -1;
  255.  
  256.     local global_list = this.GetRankingList()
  257.     local rank = 0;
  258.     for (local c_id = global_list.Begin(); !global_list.IsEnd(); c_id = global_list.Next()) {
  259.         rank++;
  260.         if (c_id == company_id) {
  261.             return rank;
  262.         }
  263.     }
  264.     return -1;
  265. }
  266.  
  267. function SCPClient_CompanyValueGS::GetCompanyIDValue(company_id)
  268. {
  269.     if (this._scp == null) return -1;
  270.     if (this._company_value_gs_game != true) return -1;
  271.    
  272.     if (AICompany.ResolveCompanyID(company_id) == AICompany.COMPANY_INVALID) return -1;
  273.    
  274.     return AICompany.GetQuarterlyCompanyValue(company_id, AICompany.CURRENT_QUARTER);
  275. }
  276.  
  277. function SCPClient_CompanyValueGS::GetCompanyIDDiffToTarget(company_id)
  278. {
  279.     if (this._scp == null) return -1;
  280.     if (this._company_value_gs_game != true) return -1;
  281.    
  282.     if (AICompany.ResolveCompanyID(company_id) == AICompany.COMPANY_INVALID) return -1;
  283.    
  284.     local current_target = this.GetCurrentTargetValue();
  285.     local company_id_value = this.GetCompanyIDValue(company_id);
  286.    
  287.     local difference = -1;
  288.     if (current_target != -1 && company_id_value != -1) {
  289.         difference = current_target - company_id_value;
  290.     }
  291.    
  292.     return difference;
  293. }
  294.  
  295. function SCPClient_CompanyValueGS::GetCompanyIDDiffToBest(company_id)
  296. {
  297.     if (this._scp == null) return -1;
  298.     if (this._company_value_gs_game != true) return -1;
  299.    
  300.     if (AICompany.ResolveCompanyID(company_id) == AICompany.COMPANY_INVALID) return -1;
  301.    
  302.     local best_company_value = this.GetBestCompanyValue();
  303.     local company_id_value = this.GetCompanyIDValue(company_id);
  304.    
  305.     local difference = -1;
  306.     if (best_company_value != -1 && company_id_value != -1) {
  307.         difference = best_company_value - company_id_value;
  308.     }
  309.    
  310.     return difference;
  311. }
  312.  
  313.  
  314.  
  315. /** SCP in/out methods (private to the library) */
  316.  
  317. /*****************************************************************
  318.  *                                                               *
  319.  *   Outgoing Commands - commands that we can send to GSes       *
  320.  *                                                               *
  321.  *****************************************************************/
  322.  
  323.  
  324. /*****************************************************************
  325.  *                                                               *
  326.  *   Incoming Commands - commands that we can get from AIs       *
  327.  *                                                               *
  328.  *****************************************************************/
  329.  
  330. // These methods are called by the SCP library when we call this._scp.Check() and there is
  331. // a received incoming message.
  332.  
  333. // Use 'self' instead of 'this'.
  334.  
  335. function SCPClient_CompanyValueGS::ReceivedCurrentGoalCommand(message, self)
  336. {
  337.     self._company_value_gs_game = true;
  338.  
  339.     local data0 = message.GetIntData(0);    // Company ID
  340.     local data1 = message.GetBoolData(1);   // Goal Mode
  341.     local data2 = message.GetStringData(2); // Goal Value divided by 1000, received as a string
  342.     local data = [data0, data1, data2];
  343.  
  344.     local s = "[";
  345.     foreach (d in data) {
  346.         if (s != "[") {
  347.             s += ", ";
  348.         }
  349.         if (d == null) {
  350.             s += "null";
  351.         } else if (typeof(d) == "string") {
  352.             s += "\"" + d + "\"";
  353.         } else {
  354.             s += d.tostring();
  355.         }
  356.     }
  357.     s += "]";
  358.  
  359.     if (data0 != null && AICompany.IsMine(data0)) {
  360.         self._goal_mode = data1;
  361.         self._goal_value = data2.tointeger() * 1000;
  362.  
  363.         AILog.Info("SCP: Reveived CurrentGoal command with data: " + s);
  364.     }
  365. }

Comments