ai_company.cpp

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

Generated on Sun Sep 13 08:19:14 2009 for OpenTTD by  doxygen 1.5.6