(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name. noai
authorrubidium
Wed, 14 Mar 2007 02:29:26 +0000
branchnoai
changeset 9374 61379e9b2393
parent 9373 d03563181088
child 9375 8bbd77b18de4
(svn r9161) [NoAI] -Add: functions to get/set company related information: loan, bank balance, company value and company name.
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/ai/NoAI/NoAI.cpp
src/ai/NoAI/NoAI.hpp
src/ai/core/ai_company.hpp
src/ai/core/company/money.cpp
src/ai/core/company/name.cpp
src/misc_cmd.cpp
src/player.h
--- a/projects/openttd.vcproj	Wed Mar 14 02:15:16 2007 +0000
+++ b/projects/openttd.vcproj	Wed Mar 14 02:29:26 2007 +0000
@@ -904,6 +904,9 @@
 				RelativePath=".\..\src\ai\core\ai_base.hpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\core\ai_company.hpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\core\ai_controller.hpp">
 			</File>
 			<File
@@ -913,6 +916,12 @@
 				RelativePath=".\..\src\ai\core\base\random.cpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\core\company\name.cpp">
+			</File>
+			<File
+				RelativePath=".\..\src\ai\core\company\money.cpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\core\object\commands.cpp">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Wed Mar 14 02:15:16 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Wed Mar 14 02:29:26 2007 +0000
@@ -1428,6 +1428,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\core\ai_company.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\core\ai_controller.hpp"
 				>
 			</File>
@@ -1440,6 +1444,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\core\company\name.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\ai\core\company\money.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\core\object\commands.cpp"
 				>
 			</File>
--- a/source.list	Wed Mar 14 02:15:16 2007 +0000
+++ b/source.list	Wed Mar 14 02:29:26 2007 +0000
@@ -271,9 +271,12 @@
 ai/core/ai.cpp
 ai/core/ai.h
 ai/core/ai_base.hpp
+ai/core/ai_company.hpp
 ai/core/ai_controller.hpp
 ai/core/ai_object.hpp
 ai/core/base/random.cpp
+ai/core/company/name.cpp
+ai/core/company/money.cpp
 ai/core/object/commands.cpp
 ai/NoAI/NoAI.cpp
 ai/NoAI/NoAI.hpp
--- a/src/ai/NoAI/NoAI.cpp	Wed Mar 14 02:15:16 2007 +0000
+++ b/src/ai/NoAI/NoAI.cpp	Wed Mar 14 02:29:26 2007 +0000
@@ -6,4 +6,16 @@
 
 /* virtual */ void NoAI::GameLoop()
 {
+	if (this->GetTick() == 1) this->company.SetCompanyName("NoAI");
+
+	if (this->GetTick() % 10 == 0) {
+		char *company_name = this->company.GetCompanyName();
+		printf("%s: %d\n", company_name, this->company.GetLoanAmount());
+		free(company_name);
+	}
+
+	if (this->GetTick() % 14 == 0) {
+		uint level = (this->company.GetMaxLoanAmount() / 10000) + 1;
+		this->company.SetLoanAmount(this->base.RandomRange(level) * 10000);
+	}
 }
--- a/src/ai/NoAI/NoAI.hpp	Wed Mar 14 02:15:16 2007 +0000
+++ b/src/ai/NoAI/NoAI.hpp	Wed Mar 14 02:29:26 2007 +0000
@@ -7,10 +7,12 @@
 
 #include "../core/ai_controller.hpp"
 #include "../core/ai_base.hpp"
+#include "../core/ai_company.hpp"
 
 class NoAI: public AIController {
 private:
 	AIBase base;
+	AICompany company;
 
 public:
 	/* virtual */ void GameLoop();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/ai_company.hpp	Wed Mar 14 02:29:26 2007 +0000
@@ -0,0 +1,67 @@
+/* $Id$ */
+
+/** @file ai_company.hpp Everything to query a company's financials and statistics */
+
+#ifndef AI_COMPANY_HPP
+#define AI_COMPANY_HPP
+
+#include "ai_object.hpp"
+
+class AICompany : public AIObject {
+public:
+	/**
+	 * Set the name of the company
+	 * @param name the new name of the company
+	 * @pre name != NULL
+	 * @return true if the command was send without problem
+	 */
+	bool SetCompanyName(const char *name);
+
+	/**
+	 * Get the name of the company
+	 * @return the name of the company
+	 * @note the returned company name must be freed
+	 */
+	char *GetCompanyName();
+
+	/**
+	 * Gets the value of the company
+	 * @return the value of the company
+	 */
+	int32 GetCompanyValue();
+
+	/**
+	 * Gets the bank balance, i.e. the amount of money that can be spent
+	 * @return the bank balance of the company
+	 */
+	int32 GetBankBalance();
+
+	/**
+	 * Gets the amount the company have loaned
+	 * @return the amount loaned
+	 * @post return >= 0
+	 * @post return % LOAN_INTERVAL == 0
+	 */
+	int32 GetLoanAmount();
+
+	/**
+	 * Gets the maximum amount the company can loan
+	 * @return the maximum amount the company can loan
+	 * @post return >= 0
+	 * @post return % LOAN_INTERVAL == 0
+	 */
+	int32 GetMaxLoanAmount();
+
+	/**
+	 * Sets the amount to loan
+	 * @param loan the amount to load (multitude of LOAN_INTERVAL)
+	 * @pre loan >= 0
+	 * @pre loan % LOAN_INTERVAL == 0
+	 * @pre loan < GetMaxLoan()
+	 * @pre loan - GetLoan() + GetBankBalance() > 0
+	 * @return true if the command was send without a problem
+	 */
+	bool SetLoanAmount(int32 loan);
+};
+
+#endif /* AI_COMPANY_HPP */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/company/money.cpp	Wed Mar 14 02:29:26 2007 +0000
@@ -0,0 +1,50 @@
+/* $Id$ */
+
+/** @file money.cpp handles the money-related functions of the AICompany class */
+
+#include "../ai_company.hpp"
+#include "../../../player.h"
+#include "../../../economy.h"
+
+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;
+}
+
+bool AICompany::SetLoanAmount(int32 loan)
+{
+	if (loan < 0 ||
+			(loan % LOAN_INTERVAL) != 0 ||
+			loan > this->GetMaxLoanAmount() ||
+			(loan - this->GetLoanAmount() + this->GetBankBalance()) < 0) {
+		return false;
+	}
+
+	if (loan == 0) {
+		return !CmdFailed(this->DoCommand(0, 0, true, DC_EXEC, CMD_DECREASE_LOAN));
+	} else if (loan == this->GetMaxLoanAmount()) {
+		return !CmdFailed(this->DoCommand(0, 0, true, DC_EXEC, CMD_INCREASE_LOAN));
+	} else {
+		bool increase = loan > this->GetLoanAmount();
+		for (uint diff_loan = abs(loan - this->GetLoanAmount()) / LOAN_INTERVAL; diff_loan > 0; diff_loan--) {
+			if (CmdFailed(this->DoCommand(0, 0, false, DC_EXEC, increase? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN))) return false;
+		}
+	}
+
+	return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/company/name.cpp	Wed Mar 14 02:29:26 2007 +0000
@@ -0,0 +1,24 @@
+/* $Id$ */
+
+/** @file name.cpp handles the company name-related functions of the AICompany class */
+
+#include "../ai_company.hpp"
+#include "../../../player.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;
+}
--- a/src/misc_cmd.cpp	Wed Mar 14 02:15:16 2007 +0000
+++ b/src/misc_cmd.cpp	Wed Mar 14 02:29:26 2007 +0000
@@ -133,7 +133,7 @@
 
 	if (flags & DC_EXEC) {
 		/* Loan the maximum amount or not? */
-		int32 loan = (p2) ? _economy.max_loan - p->current_loan : 10000;
+		int32 loan = (p2) ? _economy.max_loan - p->current_loan : LOAN_INTERVAL;
 
 		p->money64 += loan;
 		p->current_loan += loan;
@@ -161,13 +161,13 @@
 	loan = p->current_loan;
 
 	/* p2 is true while CTRL is pressed (repay all possible loan, or max money you have)
-	 * Repay any loan in chunks of 10.000 pounds */
+	 * Repay any loan in chunks of 10.000 (LOAN_INTERVAL) pounds */
 	if (p2) {
 		loan = min(loan, p->player_money);
-		loan = max(loan, 10000);
-		loan -= loan % 10000;
+		loan = max(loan, (int32)LOAN_INTERVAL);
+		loan -= loan % LOAN_INTERVAL;
 	} else {
-		loan = min(loan, 10000);
+		loan = min(loan, LOAN_INTERVAL);
 	}
 
 	if (p->player_money < loan) {
--- a/src/player.h	Wed Mar 14 02:15:16 2007 +0000
+++ b/src/player.h	Wed Mar 14 02:29:26 2007 +0000
@@ -19,6 +19,11 @@
 
 typedef uint32 PlayerFace;
 
+enum {
+	/** The stepping in pounds for loans */
+	LOAN_INTERVAL = 10000,
+};
+
 struct Player {
 	uint32 name_2;
 	uint16 name_1;