ai_company.cpp

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

Generated on Thu Jan 20 22:57:31 2011 for OpenTTD by  doxygen 1.6.1