You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
8.5 KiB
C++
268 lines
8.5 KiB
C++
16 years ago
|
/* $Id$ */
|
||
|
|
||
15 years ago
|
/*
|
||
|
* This file is part of OpenTTD.
|
||
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
13 years ago
|
/** @file script_company.cpp Implementation of AICompany. */
|
||
16 years ago
|
|
||
14 years ago
|
#include "../../stdafx.h"
|
||
13 years ago
|
#include "script_company.hpp"
|
||
|
#include "script_error.hpp"
|
||
16 years ago
|
#include "../../command_func.h"
|
||
|
#include "../../company_func.h"
|
||
|
#include "../../company_base.h"
|
||
15 years ago
|
#include "../../company_manager_face.h"
|
||
16 years ago
|
#include "../../economy_func.h"
|
||
14 years ago
|
#include "../../object_type.h"
|
||
16 years ago
|
#include "../../strings_func.h"
|
||
|
#include "../../tile_map.h"
|
||
|
#include "../../string_func.h"
|
||
16 years ago
|
#include "../../settings_func.h"
|
||
16 years ago
|
#include "table/strings.h"
|
||
|
|
||
|
/* static */ AICompany::CompanyID AICompany::ResolveCompanyID(AICompany::CompanyID company)
|
||
|
{
|
||
16 years ago
|
if (company == COMPANY_SELF) return (CompanyID)((byte)_current_company);
|
||
16 years ago
|
|
||
16 years ago
|
return ::Company::IsValidID((::CompanyID)company) ? company : COMPANY_INVALID;
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ bool AICompany::IsMine(AICompany::CompanyID company)
|
||
|
{
|
||
16 years ago
|
return ResolveCompanyID(company) == ResolveCompanyID(COMPANY_SELF);
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetName(const char *name)
|
||
|
{
|
||
|
EnforcePrecondition(false, !::StrEmpty(name));
|
||
14 years ago
|
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_COMPANY_NAME_CHARS, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
|
||
16 years ago
|
|
||
|
return AIObject::DoCommand(0, 0, 0, CMD_RENAME_COMPANY, name);
|
||
|
}
|
||
|
|
||
16 years ago
|
/* static */ char *AICompany::GetName(AICompany::CompanyID company)
|
||
16 years ago
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return NULL;
|
||
16 years ago
|
|
||
|
static const int len = 64;
|
||
|
char *company_name = MallocT<char>(len);
|
||
|
|
||
|
::SetDParam(0, company);
|
||
|
::GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
|
||
|
return company_name;
|
||
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetPresidentName(const char *name)
|
||
|
{
|
||
|
EnforcePrecondition(false, !::StrEmpty(name));
|
||
|
|
||
|
return AIObject::DoCommand(0, 0, 0, CMD_RENAME_PRESIDENT, name);
|
||
|
}
|
||
|
|
||
16 years ago
|
/* static */ char *AICompany::GetPresidentName(AICompany::CompanyID company)
|
||
16 years ago
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
|
|
||
|
static const int len = 64;
|
||
|
char *president_name = MallocT<char>(len);
|
||
16 years ago
|
if (company != COMPANY_INVALID) {
|
||
16 years ago
|
::SetDParam(0, company);
|
||
|
::GetString(president_name, STR_PRESIDENT_NAME, &president_name[len - 1]);
|
||
|
} else {
|
||
|
*president_name = '\0';
|
||
|
}
|
||
|
|
||
|
return president_name;
|
||
|
}
|
||
|
|
||
15 years ago
|
/* static */ bool AICompany::SetPresidentGender(Gender gender)
|
||
|
{
|
||
|
EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE);
|
||
|
EnforcePrecondition(false, GetPresidentGender(AICompany::COMPANY_SELF) != gender);
|
||
|
|
||
|
CompanyManagerFace cmf;
|
||
|
GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (::InteractiveRandom() & (1 << ETHNICITY_BLACK)));
|
||
|
RandomCompanyManagerFaceBits(cmf, ge, false);
|
||
|
|
||
|
return AIObject::DoCommand(0, 0, cmf, CMD_SET_COMPANY_MANAGER_FACE);
|
||
|
}
|
||
|
|
||
|
/* static */ AICompany::Gender AICompany::GetPresidentGender(CompanyID company)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
|
if (company == COMPANY_INVALID) return GENDER_INVALID;
|
||
|
|
||
|
GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(Company::Get(company)->face, CMFV_GEN_ETHN, GE_WM);
|
||
|
return HasBit(ge, ::GENDER_FEMALE) ? GENDER_FEMALE : GENDER_MALE;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* static */ Money AICompany::GetQuarterlyIncome(AICompany::CompanyID company, uint32 quarter)
|
||
16 years ago
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return -1;
|
||
13 years ago
|
if (quarter > EARLIEST_QUARTER) return -1;
|
||
16 years ago
|
|
||
13 years ago
|
if (quarter == CURRENT_QUARTER) {
|
||
|
return ::Company::Get((::CompanyID)company)->cur_economy.income;
|
||
|
}
|
||
|
return ::Company::Get((::CompanyID)company)->old_economy[quarter - 1].income;
|
||
|
}
|
||
|
|
||
|
/* static */ Money AICompany::GetQuarterlyExpenses(AICompany::CompanyID company, uint32 quarter)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
|
if (company == COMPANY_INVALID) return -1;
|
||
|
if (quarter > EARLIEST_QUARTER) return -1;
|
||
|
|
||
|
if (quarter == CURRENT_QUARTER) {
|
||
|
return ::Company::Get((::CompanyID)company)->cur_economy.expenses;
|
||
|
}
|
||
|
return ::Company::Get((::CompanyID)company)->old_economy[quarter - 1].expenses;
|
||
|
}
|
||
|
|
||
|
/* static */ int32 AICompany::GetQuarterlyCargoDelivered(AICompany::CompanyID company, uint32 quarter)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
|
if (company == COMPANY_INVALID) return -1;
|
||
|
if (quarter > EARLIEST_QUARTER) return -1;
|
||
|
|
||
|
if (quarter == CURRENT_QUARTER) {
|
||
|
return ::Company::Get((::CompanyID)company)->cur_economy.delivered_cargo;
|
||
|
}
|
||
|
return ::Company::Get((::CompanyID)company)->old_economy[quarter - 1].delivered_cargo;
|
||
16 years ago
|
}
|
||
|
|
||
13 years ago
|
/* static */ int32 AICompany::GetQuarterlyPerformanceRating(AICompany::CompanyID company, uint32 quarter)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
|
if (company == COMPANY_INVALID) return -1;
|
||
|
if (quarter > EARLIEST_QUARTER) return -1;
|
||
|
if (quarter == CURRENT_QUARTER) return -1;
|
||
|
|
||
|
return ::Company::Get((::CompanyID)company)->old_economy[quarter - 1].performance_history;
|
||
|
}
|
||
|
|
||
|
/* static */ Money AICompany::GetQuarterlyCompanyValue(AICompany::CompanyID company, uint32 quarter)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
|
if (company == COMPANY_INVALID) return -1;
|
||
|
if (quarter > EARLIEST_QUARTER) return -1;
|
||
|
|
||
|
if (quarter == CURRENT_QUARTER) {
|
||
|
return ::CalculateCompanyValue(::Company::Get((::CompanyID)company));
|
||
|
}
|
||
|
return ::Company::Get((::CompanyID)company)->old_economy[quarter - 1].company_value;
|
||
|
}
|
||
|
|
||
|
|
||
16 years ago
|
/* static */ Money AICompany::GetBankBalance(AICompany::CompanyID company)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return -1;
|
||
16 years ago
|
|
||
16 years ago
|
return ::Company::Get((CompanyID)company)->money;
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ Money AICompany::GetLoanAmount()
|
||
|
{
|
||
16 years ago
|
return ::Company::Get(_current_company)->current_loan;
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ Money AICompany::GetMaxLoanAmount()
|
||
|
{
|
||
|
return _economy.max_loan;
|
||
|
}
|
||
|
|
||
|
/* static */ Money AICompany::GetLoanInterval()
|
||
|
{
|
||
|
return LOAN_INTERVAL;
|
||
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetLoanAmount(int32 loan)
|
||
|
{
|
||
|
EnforcePrecondition(false, loan >= 0);
|
||
|
EnforcePrecondition(false, (loan % GetLoanInterval()) == 0);
|
||
|
EnforcePrecondition(false, loan <= GetMaxLoanAmount());
|
||
16 years ago
|
EnforcePrecondition(false, (loan - GetLoanAmount() + GetBankBalance(COMPANY_SELF)) >= 0);
|
||
16 years ago
|
|
||
|
if (loan == GetLoanAmount()) return true;
|
||
|
|
||
|
return AIObject::DoCommand(0,
|
||
|
abs(loan - GetLoanAmount()), 2,
|
||
|
(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
|
||
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
|
||
|
{
|
||
|
EnforcePrecondition(false, loan >= 0);
|
||
|
|
||
|
int32 over_interval = loan % GetLoanInterval();
|
||
|
if (over_interval != 0) loan += GetLoanInterval() - over_interval;
|
||
|
|
||
|
EnforcePrecondition(false, loan <= GetMaxLoanAmount());
|
||
|
|
||
|
SetLoanAmount(loan);
|
||
|
|
||
|
return GetLoanAmount() == loan;
|
||
|
}
|
||
|
|
||
|
/* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
|
||
|
{
|
||
|
EnforcePrecondition(false, ::IsValidTile(tile));
|
||
|
|
||
14 years ago
|
return AIObject::DoCommand(tile, OBJECT_HQ, 0, CMD_BUILD_OBJECT);
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ TileIndex AICompany::GetCompanyHQ(CompanyID company)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return INVALID_TILE;
|
||
16 years ago
|
|
||
16 years ago
|
TileIndex loc = ::Company::Get((CompanyID)company)->location_of_HQ;
|
||
16 years ago
|
return (loc == 0) ? INVALID_TILE : loc;
|
||
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetAutoRenewStatus(bool autorenew)
|
||
|
{
|
||
16 years ago
|
return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew"), autorenew ? 1 : 0, CMD_CHANGE_COMPANY_SETTING);
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ bool AICompany::GetAutoRenewStatus(CompanyID company)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return false;
|
||
16 years ago
|
|
||
16 years ago
|
return ::Company::Get((CompanyID)company)->settings.engine_renew;
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetAutoRenewMonths(int16 months)
|
||
|
{
|
||
16 years ago
|
return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_months"), months, CMD_CHANGE_COMPANY_SETTING);
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ int16 AICompany::GetAutoRenewMonths(CompanyID company)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return 0;
|
||
16 years ago
|
|
||
16 years ago
|
return ::Company::Get((CompanyID)company)->settings.engine_renew_months;
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ bool AICompany::SetAutoRenewMoney(uint32 money)
|
||
|
{
|
||
16 years ago
|
return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_money"), money, CMD_CHANGE_COMPANY_SETTING);
|
||
16 years ago
|
}
|
||
|
|
||
|
/* static */ uint32 AICompany::GetAutoRenewMoney(CompanyID company)
|
||
|
{
|
||
|
company = ResolveCompanyID(company);
|
||
16 years ago
|
if (company == COMPANY_INVALID) return 0;
|
||
16 years ago
|
|
||
16 years ago
|
return ::Company::Get((CompanyID)company)->settings.engine_renew_money;
|
||
16 years ago
|
}
|