author | rubidium |
Thu, 15 Mar 2007 22:46:22 +0000 | |
branch | noai |
changeset 9430 | 9e0a193b2bec |
parent 9427 | src/ai/api/company/money.cpp@ef0c109c5661 |
child 9436 | e28503f827a5 |
permissions | -rw-r--r-- |
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 |
|
9430
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
3 |
/** @file ai_company.cpp handles the money-related functions of the AICompany class */ |
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" |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
6 |
#include "../../player.h" |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
7 |
#include "../../economy.h" |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
8 |
#include "../../strings.h" |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
9 |
|
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
10 |
bool AICompany::SetCompanyName(const char *name) |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
11 |
{ |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
12 |
if (name == NULL) return false; |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
13 |
|
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
14 |
_cmd_text = name; |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
15 |
return !CmdFailed(this->DoCommand(0, 0, 0, DC_EXEC, CMD_CHANGE_PRESIDENT_NAME)); |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
16 |
} |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
17 |
|
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
18 |
char *AICompany::GetCompanyName() |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
19 |
{ |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
20 |
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
|
21 |
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
|
22 |
GetString(company_name, GetPlayer(_current_player)->name_1, &company_name[len - 1]); |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
23 |
|
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
24 |
return company_name; |
9e0a193b2bec
(svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents:
9427
diff
changeset
|
25 |
} |
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
|
26 |
|
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
27 |
int32 AICompany::GetCompanyValue() |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
28 |
{ |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
29 |
return GetPlayer(_current_player)->cur_economy.company_value; |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
30 |
} |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
31 |
|
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
32 |
int32 AICompany::GetBankBalance() |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
33 |
{ |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
34 |
return GetPlayer(_current_player)->player_money; |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
35 |
} |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
36 |
|
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
37 |
int32 AICompany::GetLoanAmount() |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
38 |
{ |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
39 |
return GetPlayer(_current_player)->current_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
|
40 |
} |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
41 |
|
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
42 |
int32 AICompany::GetMaxLoanAmount() |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
43 |
{ |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
44 |
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
|
45 |
} |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
46 |
|
9376
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
47 |
int32 AICompany::GetLoanInterval() |
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
48 |
{ |
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
49 |
return LOAN_INTERVAL; |
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
50 |
} |
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
51 |
|
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
|
52 |
bool AICompany::SetLoanAmount(int32 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
|
53 |
{ |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
54 |
if (loan < 0 || |
9376
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
55 |
(loan % this->GetLoanInterval()) != 0 || |
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
|
56 |
loan > this->GetMaxLoanAmount() || |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
57 |
(loan - this->GetLoanAmount() + this->GetBankBalance()) < 0) { |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
58 |
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
|
59 |
} |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
60 |
|
9376
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
61 |
/* When we get/repay everything at once (or the maximum we can repay), |
7c12a15c945a
(svn r9164) [NoAI] -Add: function to get the value of LOAN_INTERVAL in the AIs.
rubidium
parents:
9374
diff
changeset
|
62 |
* use the shortcut for that. Otherwise send several commands at once */ |
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
|
63 |
if (loan == 0) { |
9414
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
64 |
return this->CmdSucceeded(this->DoCommand(0, 0, true, DC_EXEC, CMD_DECREASE_LOAN)); |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
65 |
} |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
66 |
if (loan == this->GetMaxLoanAmount()) { |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
67 |
return this->CmdSucceeded(this->DoCommand(0, 0, true, DC_EXEC, CMD_INCREASE_LOAN)); |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
68 |
} |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
69 |
|
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
70 |
bool increase = loan > this->GetLoanAmount(); |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
71 |
for (uint diff_loan = abs(loan - this->GetLoanAmount()) / this->GetLoanInterval(); diff_loan > 0; diff_loan--) { |
a7f7bbec08da
(svn r9212) [NoAI] -Add: added CmdFailed and CmdSucceeded in AIObject() and used it
truelight
parents:
9376
diff
changeset
|
72 |
if (this->CmdFailed(this->DoCommand(0, 0, false, DC_EXEC, increase ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN))) return false; |
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
|
73 |
} |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
74 |
|
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
75 |
return true; |
61379e9b2393
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
rubidium
parents:
diff
changeset
|
76 |
} |