2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_company.cpp Implementation of ScriptCompany. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_company.hpp"
|
|
|
|
#include "script_error.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../command_func.h"
|
|
|
|
#include "../../company_func.h"
|
|
|
|
#include "../../company_base.h"
|
2009-08-01 00:28:21 +00:00
|
|
|
#include "../../company_manager_face.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../economy_func.h"
|
2010-08-08 10:59:30 +00:00
|
|
|
#include "../../object_type.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../strings_func.h"
|
|
|
|
#include "../../tile_map.h"
|
|
|
|
#include "../../string_func.h"
|
2009-05-26 13:29:01 +00:00
|
|
|
#include "../../settings_func.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptCompany::CompanyID ScriptCompany::ResolveCompanyID(ScriptCompany::CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-12-19 21:05:25 +00:00
|
|
|
if (company == COMPANY_SELF) {
|
|
|
|
if (!::Company::IsValidID((::CompanyID)_current_company)) return COMPANY_INVALID;
|
|
|
|
return (CompanyID)((byte)_current_company);
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
return ::Company::IsValidID((::CompanyID)company) ? company : COMPANY_INVALID;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::IsMine(ScriptCompany::CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-01-16 00:05:26 +00:00
|
|
|
return ResolveCompanyID(company) == ResolveCompanyID(COMPANY_SELF);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetName(const char *name)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, !::StrEmpty(name));
|
2011-11-29 23:15:35 +00:00
|
|
|
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_COMPANY_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_COMPANY, name);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ char *ScriptCompany::GetName(ScriptCompany::CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return NULL;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetPresidentName(const char *name)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, !::StrEmpty(name));
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_PRESIDENT, name);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ char *ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
|
|
|
|
|
|
|
static const int len = 64;
|
|
|
|
char *president_name = MallocT<char>(len);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company != COMPANY_INVALID) {
|
2009-01-12 17:11:45 +00:00
|
|
|
::SetDParam(0, company);
|
|
|
|
::GetString(president_name, STR_PRESIDENT_NAME, &president_name[len - 1]);
|
|
|
|
} else {
|
|
|
|
*president_name = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return president_name;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetPresidentGender(Gender gender)
|
2009-08-01 00:28:21 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE);
|
2011-11-29 23:15:35 +00:00
|
|
|
EnforcePrecondition(false, GetPresidentGender(ScriptCompany::COMPANY_SELF) != gender);
|
2009-08-01 00:28:21 +00:00
|
|
|
|
|
|
|
CompanyManagerFace cmf;
|
|
|
|
GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (::InteractiveRandom() & (1 << ETHNICITY_BLACK)));
|
|
|
|
RandomCompanyManagerFaceBits(cmf, ge, false);
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, 0, cmf, CMD_SET_COMPANY_MANAGER_FACE);
|
2009-08-01 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptCompany::Gender ScriptCompany::GetPresidentGender(CompanyID company)
|
2009-08-01 00:28:21 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetQuarterlyIncome(ScriptCompany::CompanyID company, uint32 quarter)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return -1;
|
2011-06-13 15:23:20 +00:00
|
|
|
if (quarter > EARLIEST_QUARTER) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-06-13 15:23:20 +00:00
|
|
|
if (quarter == CURRENT_QUARTER) {
|
|
|
|
return ::Company::Get((::CompanyID)company)->cur_economy.income;
|
|
|
|
}
|
|
|
|
return ::Company::Get((::CompanyID)company)->old_economy[quarter - 1].income;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetQuarterlyExpenses(ScriptCompany::CompanyID company, uint32 quarter)
|
2011-06-13 15:23:20 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptCompany::GetQuarterlyCargoDelivered(ScriptCompany::CompanyID company, uint32 quarter)
|
2011-06-13 15:23:20 +00:00
|
|
|
{
|
|
|
|
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;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptCompany::GetQuarterlyPerformanceRating(ScriptCompany::CompanyID company, uint32 quarter)
|
2011-06-13 15:23:20 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetQuarterlyCompanyValue(ScriptCompany::CompanyID company, uint32 quarter)
|
2011-06-13 15:23:20 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetBankBalance(ScriptCompany::CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
return ::Company::Get((CompanyID)company)->money;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetLoanAmount()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-12-19 21:05:25 +00:00
|
|
|
ScriptCompany::CompanyID company = ResolveCompanyID(COMPANY_SELF);
|
|
|
|
if (company == COMPANY_INVALID) return -1;
|
|
|
|
|
|
|
|
return ::Company::Get(company)->current_loan;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetMaxLoanAmount()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return _economy.max_loan;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCompany::GetLoanInterval()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return LOAN_INTERVAL;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetLoanAmount(int32 loan)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, loan >= 0);
|
|
|
|
EnforcePrecondition(false, (loan % GetLoanInterval()) == 0);
|
|
|
|
EnforcePrecondition(false, loan <= GetMaxLoanAmount());
|
2009-01-16 00:05:26 +00:00
|
|
|
EnforcePrecondition(false, (loan - GetLoanAmount() + GetBankBalance(COMPANY_SELF)) >= 0);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
if (loan == GetLoanAmount()) return true;
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0,
|
2009-01-12 17:11:45 +00:00
|
|
|
abs(loan - GetLoanAmount()), 2,
|
|
|
|
(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetMinimumLoanAmount(int32 loan)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::BuildCompanyHQ(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, ::IsValidTile(tile));
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(tile, OBJECT_HQ, 0, CMD_BUILD_OBJECT);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TileIndex ScriptCompany::GetCompanyHQ(CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return INVALID_TILE;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
TileIndex loc = ::Company::Get((CompanyID)company)->location_of_HQ;
|
2009-01-12 17:11:45 +00:00
|
|
|
return (loc == 0) ? INVALID_TILE : loc;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetAutoRenewStatus(bool autorenew)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew"), autorenew ? 1 : 0, CMD_CHANGE_COMPANY_SETTING);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::GetAutoRenewStatus(CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return false;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
return ::Company::Get((CompanyID)company)->settings.engine_renew;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetAutoRenewMonths(int16 months)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_months"), months, CMD_CHANGE_COMPANY_SETTING);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int16 ScriptCompany::GetAutoRenewMonths(CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return 0;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
return ::Company::Get((CompanyID)company)->settings.engine_renew_months;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCompany::SetAutoRenewMoney(uint32 money)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_money"), money, CMD_CHANGE_COMPANY_SETTING);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ uint32 ScriptCompany::GetAutoRenewMoney(CompanyID company)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
company = ResolveCompanyID(company);
|
2009-01-16 00:05:26 +00:00
|
|
|
if (company == COMPANY_INVALID) return 0;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
return ::Company::Get((CompanyID)company)->settings.engine_renew_money;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|