src/ai/api/ai_company.cpp
branchnoai
changeset 9430 9e0a193b2bec
parent 9427 ef0c109c5661
child 9436 e28503f827a5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_company.cpp	Thu Mar 15 22:46:22 2007 +0000
@@ -0,0 +1,76 @@
+/* $Id$ */
+
+/** @file ai_company.cpp handles the money-related functions of the AICompany class */
+
+#include "ai_company.hpp"
+#include "../../player.h"
+#include "../../economy.h"
+#include "../../strings.h"
+
+bool AICompany::SetCompanyName(const char *name)
+{
+	if (name == NULL) return false;
+
+	_cmd_text = name;
+	return !CmdFailed(this->DoCommand(0, 0, 0, DC_EXEC, CMD_CHANGE_PRESIDENT_NAME));
+}
+
+char *AICompany::GetCompanyName()
+{
+	static const int len = 64;
+	char *company_name = MallocT<char>(len);
+	GetString(company_name, GetPlayer(_current_player)->name_1, &company_name[len - 1]);
+
+	return company_name;
+}
+
+int32 AICompany::GetCompanyValue()
+{
+	return GetPlayer(_current_player)->cur_economy.company_value;
+}
+
+int32 AICompany::GetBankBalance()
+{
+	return GetPlayer(_current_player)->player_money;
+}
+
+int32 AICompany::GetLoanAmount()
+{
+	return GetPlayer(_current_player)->current_loan;
+}
+
+int32 AICompany::GetMaxLoanAmount()
+{
+	return _economy.max_loan;
+}
+
+int32 AICompany::GetLoanInterval()
+{
+	return LOAN_INTERVAL;
+}
+
+bool AICompany::SetLoanAmount(int32 loan)
+{
+	if (loan < 0 ||
+			(loan % this->GetLoanInterval()) != 0 ||
+			loan > this->GetMaxLoanAmount() ||
+			(loan - this->GetLoanAmount() + this->GetBankBalance()) < 0) {
+		return false;
+	}
+
+	/* When we get/repay everything at once (or the maximum we can repay),
+	 * use the shortcut for that. Otherwise send several commands at once */
+	if (loan == 0) {
+		return this->CmdSucceeded(this->DoCommand(0, 0, true, DC_EXEC, CMD_DECREASE_LOAN));
+	}
+	if (loan == this->GetMaxLoanAmount()) {
+		return this->CmdSucceeded(this->DoCommand(0, 0, true, DC_EXEC, CMD_INCREASE_LOAN));
+	}
+
+	bool increase = loan > this->GetLoanAmount();
+	for (uint diff_loan = abs(loan - this->GetLoanAmount()) / this->GetLoanInterval(); diff_loan > 0; diff_loan--) {
+		if (this->CmdFailed(this->DoCommand(0, 0, false, DC_EXEC, increase ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN))) return false;
+	}
+
+	return true;
+}