# ai_company.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013 #include "ai_company.hpp"
    00014 #include "ai_error.hpp"
    00015 #include "../../command_func.h"
    00016 #include "../../company_func.h"
    00017 #include "../../company_base.h"
    00018 #include "../../company_manager_face.h"
    00019 #include "../../economy_func.h"
    00020 #include "../../object_type.h"
    00021 #include "../../strings_func.h"
    00022 #include "../../tile_map.h"
    00023 #include "../../string_func.h"
    00024 #include "../../settings_func.h"
    00025 #include "table/strings.h"
    00026
    00027 /* static */ AICompany::CompanyID AICompany::ResolveCompanyID(AICompany::CompanyID company)
    00028 {
    00029   if (company == COMPANY_SELF) return (CompanyID)((byte)_current_company);
    00030
    00031   return ::Company::IsValidID((::CompanyID)company) ? company : COMPANY_INVALID;
    00032 }
    00033
    00034 /* static */ bool AICompany::IsMine(AICompany::CompanyID company)
    00035 {
    00036   return ResolveCompanyID(company) == ResolveCompanyID(COMPANY_SELF);
    00037 }
    00038
    00039 /* static */ bool AICompany::SetName(const char *name)
    00040 {
    00041   EnforcePrecondition(false, !::StrEmpty(name));
    00042   EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_COMPANY_NAME_CHARS, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
    00043
    00044   return AIObject::DoCommand(0, 0, 0, CMD_RENAME_COMPANY, name);
    00045 }
    00046
    00047 /* static */ char *AICompany::GetName(AICompany::CompanyID company)
    00048 {
    00049   company = ResolveCompanyID(company);
    00050   if (company == COMPANY_INVALID) return NULL;
    00051
    00052   static const int len = 64;
    00053   char *company_name = MallocT<char>(len);
    00054
    00055 	::SetDParam(0, company);
    00056   ::GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
    00057   return company_name;
    00058 }
    00059
    00060 /* static */ bool AICompany::SetPresidentName(const char *name)
    00061 {
    00062   EnforcePrecondition(false, !::StrEmpty(name));
    00063
    00064   return AIObject::DoCommand(0, 0, 0, CMD_RENAME_PRESIDENT, name);
    00065 }
    00066
    00067 /* static */ char *AICompany::GetPresidentName(AICompany::CompanyID company)
    00068 {
    00069   company = ResolveCompanyID(company);
    00070
    00071   static const int len = 64;
    00072   char *president_name = MallocT<char>(len);
    00073   if (company != COMPANY_INVALID) {
    00074 		::SetDParam(0, company);
    00075     ::GetString(president_name, STR_PRESIDENT_NAME, &president_name[len - 1]);
    00076   } else {
    00077     *president_name = '\0';
    00078   }
    00079
    00080   return president_name;
    00081 }
    00082
    00083 /* static */ bool AICompany::SetPresidentGender(Gender gender)
    00084 {
    00085   EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE);
    00086   EnforcePrecondition(false, GetPresidentGender(AICompany::COMPANY_SELF) != gender);
    00087
    00088   CompanyManagerFace cmf;
    00089   GenderEthnicity ge = (GenderEthnicity)((gender == GENDER_FEMALE ? (1 << ::GENDER_FEMALE) : 0) | (::InteractiveRandom() & (1 << ETHNICITY_BLACK)));
    00090   RandomCompanyManagerFaceBits(cmf, ge, false);
    00091
    00092   return AIObject::DoCommand(0, 0, cmf, CMD_SET_COMPANY_MANAGER_FACE);
    00093 }
    00094
    00095 /* static */ AICompany::Gender AICompany::GetPresidentGender(CompanyID company)
    00096 {
    00097   company = ResolveCompanyID(company);
    00098   if (company == COMPANY_INVALID) return GENDER_INVALID;
    00099
    00100   GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(Company::Get(company)->face, CMFV_GEN_ETHN, GE_WM);
    00101   return HasBit(ge, ::GENDER_FEMALE) ? GENDER_FEMALE : GENDER_MALE;
    00102 }
    00103
    00104 /* static */ Money AICompany::GetCompanyValue(AICompany::CompanyID company)
    00105 {
    00106   company = ResolveCompanyID(company);
    00107   if (company == COMPANY_INVALID) return -1;
    00108
    00109   return ::CalculateCompanyValue(::Company::Get((CompanyID)company));
    00110 }
    00111
    00112 /* static */ Money AICompany::GetBankBalance(AICompany::CompanyID company)
    00113 {
    00114   company = ResolveCompanyID(company);
    00115   if (company == COMPANY_INVALID) return -1;
    00116
    00117   return ::Company::Get((CompanyID)company)->money;
    00118 }
    00119
    00120 /* static */ Money AICompany::GetLoanAmount()
    00121 {
    00122   return ::Company::Get(_current_company)->current_loan;
    00123 }
    00124
    00125 /* static */ Money AICompany::GetMaxLoanAmount()
    00126 {
    00127   return _economy.max_loan;
    00128 }
    00129
    00130 /* static */ Money AICompany::GetLoanInterval()
    00131 {
    00132   return LOAN_INTERVAL;
    00133 }
    00134
    00135 /* static */ bool AICompany::SetLoanAmount(int32 loan)
    00136 {
    00137   EnforcePrecondition(false, loan >= 0);
    00138   EnforcePrecondition(false, (loan % GetLoanInterval()) == 0);
    00139   EnforcePrecondition(false, loan <= GetMaxLoanAmount());
    00140   EnforcePrecondition(false, (loan - GetLoanAmount() + GetBankBalance(COMPANY_SELF)) >= 0);
    00141
    00142   if (loan == GetLoanAmount()) return true;
    00143
    00144   return AIObject::DoCommand(0,
    00145       abs(loan - GetLoanAmount()), 2,
    00146       (loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
    00147 }
    00148
    00149 /* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
    00150 {
    00151   EnforcePrecondition(false, loan >= 0);
    00152
    00153   int32 over_interval = loan % GetLoanInterval();
    00154   if (over_interval != 0) loan += GetLoanInterval() - over_interval;
    00155
    00156   EnforcePrecondition(false, loan <= GetMaxLoanAmount());
    00157
    00158   SetLoanAmount(loan);
    00159
    00160   return GetLoanAmount() == loan;
    00161 }
    00162
    00163 /* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
    00164 {
    00165   EnforcePrecondition(false, ::IsValidTile(tile));
    00166
    00167   return AIObject::DoCommand(tile, OBJECT_HQ, 0, CMD_BUILD_OBJECT);
    00168 }
    00169
    00170 /* static */ TileIndex AICompany::GetCompanyHQ(CompanyID company)
    00171 {
    00172   company = ResolveCompanyID(company);
    00173   if (company == COMPANY_INVALID) return INVALID_TILE;
    00174
    00175   TileIndex loc = ::Company::Get((CompanyID)company)->location_of_HQ;
    00176   return (loc == 0) ? INVALID_TILE : loc;
    00177 }
    00178
    00179 /* static */ bool AICompany::SetAutoRenewStatus(bool autorenew)
    00180 {
    00181   return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew"), autorenew ? 1 : 0, CMD_CHANGE_COMPANY_SETTING);
    00182 }
    00183
    00184 /* static */ bool AICompany::GetAutoRenewStatus(CompanyID company)
    00185 {
    00186   company = ResolveCompanyID(company);
    00187   if (company == COMPANY_INVALID) return false;
    00188
    00189   return ::Company::Get((CompanyID)company)->settings.engine_renew;
    00190 }
    00191
    00192 /* static */ bool AICompany::SetAutoRenewMonths(int16 months)
    00193 {
    00194   return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_months"), months, CMD_CHANGE_COMPANY_SETTING);
    00195 }
    00196
    00197 /* static */ int16 AICompany::GetAutoRenewMonths(CompanyID company)
    00198 {
    00199   company = ResolveCompanyID(company);
    00200   if (company == COMPANY_INVALID) return 0;
    00201
    00202   return ::Company::Get((CompanyID)company)->settings.engine_renew_months;
    00203 }
    00204
    00205 /* static */ bool AICompany::SetAutoRenewMoney(uint32 money)
    00206 {
    00207   return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_money"), money, CMD_CHANGE_COMPANY_SETTING);
    00208 }
    00209
    00210 /* static */ uint32 AICompany::GetAutoRenewMoney(CompanyID company)
    00211 {
    00212   company = ResolveCompanyID(company);
    00213   if (company == COMPANY_INVALID) return 0;
    00214
    00215   return ::Company::Get((CompanyID)company)->settings.engine_renew_money;
    00216 }