ai_company.cpp

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

Generated on Sun Mar 15 22:49:44 2009 for openttd by  doxygen 1.5.6