src/ai/api/ai_company.cpp
author rubidium
Thu, 03 Apr 2008 23:01:54 +0000
branchnoai
changeset 9865 f241472f09dc
parent 9833 89a64246458f
child 9868 3998f2e73dda
permissions -rw-r--r--
(svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
     1
/* $Id$ */
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
     2
9833
89a64246458f (svn r12496) [NoAI] -Documentation: give the .cpp files a nice uniform format too
truebrain
parents: 9808
diff changeset
     3
/** @file ai_company.cpp Implementation of AICompany. */
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
     4
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
     5
#include "ai_company.hpp"
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
     6
#include "ai_error.hpp"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
     7
#include "../../command_func.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
     8
#include "../../player_func.h"
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
     9
#include "../../player_base.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    10
#include "../../economy_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    11
#include "../../strings_func.h"
9808
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
    12
#include "../../tile_map.h"
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9575
diff changeset
    13
#include "../../variables.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9701
diff changeset
    14
#include "../../core/alloc_func.hpp"
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    15
#include "../../string_func.h"
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9575
diff changeset
    16
#include "table/strings.h"
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    17
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    18
/* static */ AICompany::CompanyIndex AICompany::ResolveCompanyIndex(AICompany::CompanyIndex company)
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    19
{
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9696
diff changeset
    20
	if (company == MY_COMPANY) return (CompanyIndex)((byte)_current_player);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    21
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    22
	return (::IsValidPlayer((PlayerID)company) && ::GetPlayer((PlayerID)company)->is_active) ? company : INVALID_COMPANY;
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    23
}
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    24
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    25
/* static */ bool AICompany::SetCompanyName(const char *name)
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    26
{
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    27
	if (::StrEmpty(name)) {
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    28
		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    29
		return false;
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    30
	}
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    31
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    32
	_cmd_text = name;
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    33
	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    34
}
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    35
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    36
/* static */ char *AICompany::GetCompanyName(AICompany::CompanyIndex company)
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    37
{
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    38
	company = ResolveCompanyIndex(company);
9696
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9668
diff changeset
    39
	if (company == INVALID_COMPANY) return NULL;
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    40
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    41
	static const int len = 64;
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    42
	char *company_name = MallocT<char>(len);
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    43
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    44
	::SetDParam(0, GetPlayer((PlayerID)company)->index);
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    45
	::GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    46
	return company_name;
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
    47
}
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    48
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    49
/* static */ bool AICompany::SetPresidentName(const char *name)
9575
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    50
{
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    51
	if (::StrEmpty(name)) {
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    52
		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    53
		return false;
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
    54
	}
9575
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    55
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    56
	_cmd_text = name;
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    57
	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
9575
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    58
}
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    59
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    60
/* static */ char *AICompany::GetPresidentName(AICompany::CompanyIndex company)
9575
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    61
{
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    62
	company = ResolveCompanyIndex(company);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    63
9575
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    64
	static const int len = 64;
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    65
	char *president_name = MallocT<char>(len);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    66
	if (company != INVALID_COMPANY) {
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    67
		::SetDParam(0, GetPlayer((PlayerID)company)->index);
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    68
		::GetString(president_name, STR_PLAYER_NAME, &president_name[len - 1]);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    69
	} else {
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    70
		*president_name = '\0';
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    71
	}
9575
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    72
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    73
	return president_name;
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    74
}
a4b6bbfa6c96 (svn r9591) [NoAI] -Fix: SetCompanyName() in fact set the president name (Zuu)
truelight
parents: 9486
diff changeset
    75
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    76
/* static */ int32 AICompany::GetCompanyValue(AICompany::CompanyIndex company)
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    77
{
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    78
	company = ResolveCompanyIndex(company);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    79
	if (company == INVALID_COMPANY) return 0;
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    80
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    81
	return ::GetPlayer((PlayerID)company)->cur_economy.company_value;
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    82
}
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    83
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    84
/* static */ int32 AICompany::GetBankBalance(AICompany::CompanyIndex company)
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    85
{
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    86
	company = ResolveCompanyIndex(company);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    87
	if (company == INVALID_COMPANY) return 0;
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
    88
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    89
	return ::ClampToI32(GetPlayer((PlayerID)company)->player_money);
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    90
}
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    91
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    92
/* static */ int32 AICompany::GetLoanAmount()
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    93
{
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    94
	return ::GetPlayer(_current_player)->current_loan;
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    95
}
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    96
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    97
/* static */ int32 AICompany::GetMaxLoanAmount()
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    98
{
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
    99
	return _economy.max_loan;
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   100
}
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   101
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   102
/* static */ int32 AICompany::GetLoanInterval()
9376
7c12a15c945a (svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents: 9374
diff changeset
   103
{
7c12a15c945a (svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents: 9374
diff changeset
   104
	return LOAN_INTERVAL;
7c12a15c945a (svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents: 9374
diff changeset
   105
}
7c12a15c945a (svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents: 9374
diff changeset
   106
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   107
/* static */ bool AICompany::SetLoanAmount(int32 loan)
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   108
{
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   109
	if (loan < 0 ||
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   110
			(loan % GetLoanInterval()) != 0 ||
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   111
			loan > GetMaxLoanAmount() ||
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   112
			(loan - GetLoanAmount() + GetBankBalance(MY_COMPANY)) < 0) {
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   113
		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   114
		return false;
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   115
	}
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   116
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   117
	if (loan == GetLoanAmount()) return true;
9414
a7f7bbec08da (svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents: 9376
diff changeset
   118
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   119
	return AIObject::DoCommand(0,
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   120
			abs(loan - GetLoanAmount()), 2,
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   121
			(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
9374
61379e9b2393 (svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff changeset
   122
}
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   123
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   124
/* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   125
{
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   126
	if (loan < 0) {
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   127
		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   128
		return false;
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   129
	}
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   130
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   131
	int32 over_interval = loan % GetLoanInterval();
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   132
	if (over_interval != 0) loan += GetLoanInterval() - over_interval;
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   133
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   134
	if (loan > GetMaxLoanAmount()) {
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   135
		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   136
		return false;
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   137
	}
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   138
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   139
	SetLoanAmount(loan);
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   140
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
   141
	return GetLoanAmount() == loan;
9668
6fe3d2cb9655 (svn r10582) [NoAI] -Codechange: allow getting the president and company names of other companies as well as their company value and bank balance.
rubidium
parents: 9629
diff changeset
   142
}
9808
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   143
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   144
/* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   145
{
9865
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   146
	if (!::IsValidTile(tile)) {
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   147
		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   148
		return false;
f241472f09dc (svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
rubidium
parents: 9833
diff changeset
   149
	}
9808
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   150
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   151
	return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_COMPANY_HQ);
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   152
}
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   153
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   154
/* static */ TileIndex AICompany::GetCompanyHQ(CompanyIndex company)
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   155
{
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   156
	company = ResolveCompanyIndex(company);
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   157
	if (company == INVALID_COMPANY) return INVALID_TILE;
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   158
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   159
	TileIndex loc = ::GetPlayer((PlayerID)company)->location_of_house;
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   160
	return (loc == 0) ? INVALID_TILE : loc;
0b40f556e051 (svn r12318) [NoAI] -Add: added AICompany::(Build|Get)CompanyHQ (college of Morloth)
truebrain
parents: 9737
diff changeset
   161
}