# ai_company.cpp

Go to the documentation of this file.

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