src/ai/api/ai_company.cpp
author rubidium
Wed, 09 Jan 2008 18:11:12 +0000
branchnoai
changeset 9723 eee46cb39750
parent 9701 d1ac22c62f64
child 9724 b39bc69bb2f2
permissions -rw-r--r--
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
/* $Id$ */

/** @file ai_company.cpp handles the functions of the AICompany class */

#include "ai_company.hpp"
#include "../../command_func.h"
#include "../../player.h"
#include "../../economy_func.h"
#include "../../strings_func.h"
#include "../../variables.h"
#include "../../core/alloc_func.hpp"
#include "table/strings.h"

AICompany::CompanyIndex AICompany::ResolveCompanyIndex(AICompany::CompanyIndex company)
{
	if (company == MY_COMPANY) return (CompanyIndex)((byte)_current_player);

	return (IsValidPlayer((PlayerID)company) && GetPlayer((PlayerID)company)->is_active) ? company : INVALID_COMPANY;
}

bool AICompany::SetCompanyName(const char *name)
{
	if (name == NULL) return false;

	_cmd_text = name;
	return this->DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
}

char *AICompany::GetCompanyName(AICompany::CompanyIndex company)
{
	company = this->ResolveCompanyIndex(company);
	if (company == INVALID_COMPANY) return NULL;

	static const int len = 64;
	char *company_name = MallocT<char>(len);

	SetDParam(0, GetPlayer((PlayerID)company)->index);
	GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
	return company_name;
}

bool AICompany::SetPresidentName(const char *name)
{
	if (name == NULL) return false;

	_cmd_text = name;
	return this->DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
}

char *AICompany::GetPresidentName(AICompany::CompanyIndex company)
{
	company = this->ResolveCompanyIndex(company);

	static const int len = 64;
	char *president_name = MallocT<char>(len);
	if (company != INVALID_COMPANY) {
		SetDParam(0, GetPlayer((PlayerID)company)->index);
		GetString(president_name, STR_PLAYER_NAME, &president_name[len - 1]);
	} else {
		*president_name = '\0';
	}

	return president_name;
}

int32 AICompany::GetCompanyValue(AICompany::CompanyIndex company)
{
	company = this->ResolveCompanyIndex(company);
	if (company == INVALID_COMPANY) return 0;

	return GetPlayer((PlayerID)company)->cur_economy.company_value;
}

int32 AICompany::GetBankBalance(AICompany::CompanyIndex company)
{
	company = this->ResolveCompanyIndex(company);
	if (company == INVALID_COMPANY) return 0;

	return GetPlayer((PlayerID)company)->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(MY_COMPANY)) < 0) {
		return false;
	}

	if (loan == this->GetLoanAmount()) return true;

	return this->DoCommand(0,
			abs(loan - this->GetLoanAmount()), 2,
			(loan > this->GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
}

bool AICompany::SetMinimumLoanAmount(int32 loan)
{
	if (loan < 0) return false;

	int32 over_interval = loan % this->GetLoanInterval();
	if (over_interval != 0) loan += this->GetLoanInterval() - over_interval;

	if (loan > this->GetMaxLoanAmount()) return false;

	this->SetLoanAmount(loan);

	return this->GetLoanAmount() == loan;
}