src/ai/api/ai_company.cpp
branchnoai
changeset 9865 f241472f09dc
parent 9833 89a64246458f
child 9868 3998f2e73dda
equal deleted inserted replaced
9864:4a9b7b610b13 9865:f241472f09dc
     1 /* $Id$ */
     1 /* $Id$ */
     2 
     2 
     3 /** @file ai_company.cpp Implementation of AICompany. */
     3 /** @file ai_company.cpp Implementation of AICompany. */
     4 
     4 
     5 #include "ai_company.hpp"
     5 #include "ai_company.hpp"
       
     6 #include "ai_error.hpp"
     6 #include "../../command_func.h"
     7 #include "../../command_func.h"
     7 #include "../../player_func.h"
     8 #include "../../player_func.h"
     8 #include "../../player_base.h"
     9 #include "../../player_base.h"
     9 #include "../../economy_func.h"
    10 #include "../../economy_func.h"
    10 #include "../../strings_func.h"
    11 #include "../../strings_func.h"
    11 #include "../../tile_map.h"
    12 #include "../../tile_map.h"
    12 #include "../../variables.h"
    13 #include "../../variables.h"
    13 #include "../../core/alloc_func.hpp"
    14 #include "../../core/alloc_func.hpp"
       
    15 #include "../../string_func.h"
    14 #include "table/strings.h"
    16 #include "table/strings.h"
    15 
    17 
    16 /* static */ AICompany::CompanyIndex AICompany::ResolveCompanyIndex(AICompany::CompanyIndex company)
    18 /* static */ AICompany::CompanyIndex AICompany::ResolveCompanyIndex(AICompany::CompanyIndex company)
    17 {
    19 {
    18 	if (company == MY_COMPANY) return (CompanyIndex)((byte)_current_player);
    20 	if (company == MY_COMPANY) return (CompanyIndex)((byte)_current_player);
    20 	return (::IsValidPlayer((PlayerID)company) && ::GetPlayer((PlayerID)company)->is_active) ? company : INVALID_COMPANY;
    22 	return (::IsValidPlayer((PlayerID)company) && ::GetPlayer((PlayerID)company)->is_active) ? company : INVALID_COMPANY;
    21 }
    23 }
    22 
    24 
    23 /* static */ bool AICompany::SetCompanyName(const char *name)
    25 /* static */ bool AICompany::SetCompanyName(const char *name)
    24 {
    26 {
    25 	if (name == NULL) return false;
    27 	if (::StrEmpty(name)) {
       
    28 		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
       
    29 		return false;
       
    30 	}
    26 
    31 
    27 	_cmd_text = name;
    32 	_cmd_text = name;
    28 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
    33 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
    29 }
    34 }
    30 
    35 
    41 	return company_name;
    46 	return company_name;
    42 }
    47 }
    43 
    48 
    44 /* static */ bool AICompany::SetPresidentName(const char *name)
    49 /* static */ bool AICompany::SetPresidentName(const char *name)
    45 {
    50 {
    46 	if (name == NULL) return false;
    51 	if (::StrEmpty(name)) {
       
    52 		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
       
    53 		return false;
       
    54 	}
    47 
    55 
    48 	_cmd_text = name;
    56 	_cmd_text = name;
    49 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
    57 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
    50 }
    58 }
    51 
    59 
   100 {
   108 {
   101 	if (loan < 0 ||
   109 	if (loan < 0 ||
   102 			(loan % GetLoanInterval()) != 0 ||
   110 			(loan % GetLoanInterval()) != 0 ||
   103 			loan > GetMaxLoanAmount() ||
   111 			loan > GetMaxLoanAmount() ||
   104 			(loan - GetLoanAmount() + GetBankBalance(MY_COMPANY)) < 0) {
   112 			(loan - GetLoanAmount() + GetBankBalance(MY_COMPANY)) < 0) {
       
   113 		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
   105 		return false;
   114 		return false;
   106 	}
   115 	}
   107 
   116 
   108 	if (loan == GetLoanAmount()) return true;
   117 	if (loan == GetLoanAmount()) return true;
   109 
   118 
   112 			(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
   121 			(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
   113 }
   122 }
   114 
   123 
   115 /* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
   124 /* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
   116 {
   125 {
   117 	if (loan < 0) return false;
   126 	if (loan < 0) {
       
   127 		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
       
   128 		return false;
       
   129 	}
   118 
   130 
   119 	int32 over_interval = loan % GetLoanInterval();
   131 	int32 over_interval = loan % GetLoanInterval();
   120 	if (over_interval != 0) loan += GetLoanInterval() - over_interval;
   132 	if (over_interval != 0) loan += GetLoanInterval() - over_interval;
   121 
   133 
   122 	if (loan > GetMaxLoanAmount()) return false;
   134 	if (loan > GetMaxLoanAmount()) {
       
   135 		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
       
   136 		return false;
       
   137 	}
   123 
   138 
   124 	SetLoanAmount(loan);
   139 	SetLoanAmount(loan);
   125 
   140 
   126 	return GetLoanAmount() == loan;
   141 	return GetLoanAmount() == loan;
   127 }
   142 }
   128 
   143 
   129 /* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
   144 /* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
   130 {
   145 {
   131 	if (!::IsValidTile(tile)) return false;
   146 	if (!::IsValidTile(tile)) {
       
   147 		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
       
   148 		return false;
       
   149 	}
   132 
   150 
   133 	return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_COMPANY_HQ);
   151 	return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_COMPANY_HQ);
   134 }
   152 }
   135 
   153 
   136 /* static */ TileIndex AICompany::GetCompanyHQ(CompanyIndex company)
   154 /* static */ TileIndex AICompany::GetCompanyHQ(CompanyIndex company)