src/ai/api/ai_company.cpp
branchnoai
changeset 9668 6fe3d2cb9655
parent 9629 66dde6412125
child 9696 4384ed3de1f0
equal deleted inserted replaced
9667:053afa64f977 9668:6fe3d2cb9655
     8 #include "../../economy.h"
     8 #include "../../economy.h"
     9 #include "../../strings.h"
     9 #include "../../strings.h"
    10 #include "../../variables.h"
    10 #include "../../variables.h"
    11 #include "table/strings.h"
    11 #include "table/strings.h"
    12 
    12 
       
    13 AICompany::CompanyIndex AICompany::ResolveCompanyIndex(AICompany::CompanyIndex company)
       
    14 {
       
    15 	if (company == MY_COMPANY) return (CompanyIndex)_current_player;
       
    16 
       
    17 	return (IsValidPlayer((PlayerID)company) && GetPlayer((PlayerID)company)->is_active) ? company : INVALID_COMPANY;
       
    18 }
       
    19 
    13 bool AICompany::SetCompanyName(const char *name)
    20 bool AICompany::SetCompanyName(const char *name)
    14 {
    21 {
    15 	if (name == NULL) return false;
    22 	if (name == NULL) return false;
    16 
    23 
    17 	_cmd_text = name;
    24 	_cmd_text = name;
    18 	return this->DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
    25 	return this->DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
    19 }
    26 }
    20 
    27 
    21 char *AICompany::GetCompanyName()
    28 char *AICompany::GetCompanyName(AICompany::CompanyIndex company)
    22 {
    29 {
       
    30 	company = this->ResolveCompanyIndex(company);
       
    31 
    23 	static const int len = 64;
    32 	static const int len = 64;
    24 	char *company_name = MallocT<char>(len);
    33 	char *company_name = MallocT<char>(len);
    25 	SetDParam(0, GetPlayer(_current_player)->index);
    34 	if (company != INVALID_COMPANY) {
    26 	GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
    35 		SetDParam(0, GetPlayer((PlayerID)company)->index);
       
    36 		GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
       
    37 	} else {
       
    38 		*company_name = '\0';
       
    39 	}
    27 
    40 
    28 	return company_name;
    41 	return company_name;
    29 }
    42 }
    30 
    43 
    31 bool AICompany::SetPresidentName(const char *name)
    44 bool AICompany::SetPresidentName(const char *name)
    34 
    47 
    35 	_cmd_text = name;
    48 	_cmd_text = name;
    36 	return this->DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
    49 	return this->DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
    37 }
    50 }
    38 
    51 
    39 char *AICompany::GetPresidentName()
    52 char *AICompany::GetPresidentName(AICompany::CompanyIndex company)
    40 {
    53 {
       
    54 	company = this->ResolveCompanyIndex(company);
       
    55 
    41 	static const int len = 64;
    56 	static const int len = 64;
    42 	char *president_name = MallocT<char>(len);
    57 	char *president_name = MallocT<char>(len);
    43 	SetDParam(0, GetPlayer(_current_player)->index);
    58 	if (company != INVALID_COMPANY) {
    44 	GetString(president_name, STR_PLAYER_NAME, &president_name[len - 1]);
    59 		SetDParam(0, GetPlayer((PlayerID)company)->index);
       
    60 		GetString(president_name, STR_PLAYER_NAME, &president_name[len - 1]);
       
    61 	} else {
       
    62 		*president_name = '\0';
       
    63 	}
    45 
    64 
    46 	return president_name;
    65 	return president_name;
    47 }
    66 }
    48 
    67 
    49 int32 AICompany::GetCompanyValue()
    68 int32 AICompany::GetCompanyValue(AICompany::CompanyIndex company)
    50 {
    69 {
    51 	return GetPlayer(_current_player)->cur_economy.company_value;
    70 	company = this->ResolveCompanyIndex(company);
       
    71 	if (company == INVALID_COMPANY) return 0;
       
    72 
       
    73 	return GetPlayer((PlayerID)company)->cur_economy.company_value;
    52 }
    74 }
    53 
    75 
    54 int32 AICompany::GetBankBalance()
    76 int32 AICompany::GetBankBalance(AICompany::CompanyIndex company)
    55 {
    77 {
    56 	return GetPlayer(_current_player)->player_money;
    78 	company = this->ResolveCompanyIndex(company);
       
    79 	if (company == INVALID_COMPANY) return 0;
       
    80 
       
    81 	return GetPlayer((PlayerID)company)->player_money;
    57 }
    82 }
    58 
    83 
    59 int32 AICompany::GetLoanAmount()
    84 int32 AICompany::GetLoanAmount()
    60 {
    85 {
    61 	return GetPlayer(_current_player)->current_loan;
    86 	return GetPlayer(_current_player)->current_loan;
    74 bool AICompany::SetLoanAmount(int32 loan)
    99 bool AICompany::SetLoanAmount(int32 loan)
    75 {
   100 {
    76 	if (loan < 0 ||
   101 	if (loan < 0 ||
    77 			(loan % this->GetLoanInterval()) != 0 ||
   102 			(loan % this->GetLoanInterval()) != 0 ||
    78 			loan > this->GetMaxLoanAmount() ||
   103 			loan > this->GetMaxLoanAmount() ||
    79 			(loan - this->GetLoanAmount() + this->GetBankBalance()) < 0) {
   104 			(loan - this->GetLoanAmount() + this->GetBankBalance(MY_COMPANY)) < 0) {
    80 		return false;
   105 		return false;
    81 	}
   106 	}
    82 
   107 
    83 	if (loan == this->GetLoanAmount()) return true;
   108 	if (loan == this->GetLoanAmount()) return true;
    84 
   109 
    85 	return this->DoCommand(0,
   110 	return this->DoCommand(0,
    86 			abs(loan - this->GetLoanAmount()), 2,
   111 			abs(loan - this->GetLoanAmount()), 2,
    87 			(loan > this->GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
   112 			(loan > this->GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
    88 }
   113 }
       
   114 
       
   115 bool AICompany::SetMinimumLoanAmount(int32 loan)
       
   116 {
       
   117 	if (loan < 0) return false;
       
   118 
       
   119 	int32 over_interval = loan % this->GetLoanInterval();
       
   120 	if (over_interval != 0) loan += this->GetLoanInterval() - over_interval;
       
   121 
       
   122 	if (loan > this->GetMaxLoanAmount()) return false;
       
   123 
       
   124 	this->SetLoanAmount(loan);
       
   125 
       
   126 	return this->GetLoanAmount() == loan;
       
   127 }