# ai_industrytype.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_industrytype.cpp 19855 2010-05-18 21:30:56Z 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_industrytype.hpp"
    00013 #include "ai_map.hpp"
    00014 #include "../../command_type.h"
    00015 #include "../../strings_func.h"
    00016 #include "../../industry.h"
    00017 #include "../../newgrf_industries.h"
    00018 #include "../../core/random_func.hpp"
    00019
    00020 /* static */ bool AIIndustryType::IsValidIndustryType(IndustryType industry_type)
    00021 {
    00022   if (industry_type >= NUM_INDUSTRYTYPES) return false;
    00023
    00024   return ::GetIndustrySpec(industry_type)->enabled;
    00025 }
    00026
    00027 /* static */ bool AIIndustryType::IsRawIndustry(IndustryType industry_type)
    00028 {
    00029   if (!IsValidIndustryType(industry_type)) return false;
    00030
    00031   return ::GetIndustrySpec(industry_type)->IsRawIndustry();
    00032 }
    00033
    00034 /* static */ bool AIIndustryType::ProductionCanIncrease(IndustryType industry_type)
    00035 {
    00036   if (!IsValidIndustryType(industry_type)) return false;
    00037
    00038   if (_settings_game.game_creation.landscape != LT_TEMPERATE) return true;
    00039   return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_DONT_INCR_PROD) == 0;
    00040 }
    00041
    00042 /* static */ Money AIIndustryType::GetConstructionCost(IndustryType industry_type)
    00043 {
    00044   if (!IsValidIndustryType(industry_type)) return -1;
    00045   if (::GetIndustrySpec(industry_type)->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) return -1;
    00046
    00047   return ::GetIndustrySpec(industry_type)->GetConstructionCost();
    00048 }
    00049
    00050 /* static */ char *AIIndustryType::GetName(IndustryType industry_type)
    00051 {
    00052   if (!IsValidIndustryType(industry_type)) return NULL;
    00053   static const int len = 64;
    00054   char *industrytype_name = MallocT<char>(len);
    00055
    00056   ::GetString(industrytype_name, ::GetIndustrySpec(industry_type)->name, &industrytype_name[len - 1]);
    00057
    00058   return industrytype_name;
    00059 }
    00060
    00061 /* static */ AIList *AIIndustryType::GetProducedCargo(IndustryType industry_type)
    00062 {
    00063   if (!IsValidIndustryType(industry_type)) return NULL;
    00064
    00065   const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
    00066
    00067   AIList *list = new AIList();
    00068   for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) {
    00069     if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i], 0);
    00070   }
    00071
    00072   return list;
    00073 }
    00074
    00075 /* static */ AIList *AIIndustryType::GetAcceptedCargo(IndustryType industry_type)
    00076 {
    00077   if (!IsValidIndustryType(industry_type)) return NULL;
    00078
    00079   const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
    00080
    00081   AIList *list = new AIList();
    00082   for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) {
    00083     if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i], 0);
    00084   }
    00085
    00086   return list;
    00087 }
    00088
    00089 /* static */ bool AIIndustryType::CanBuildIndustry(IndustryType industry_type)
    00090 {
    00091   if (!IsValidIndustryType(industry_type)) return false;
    00092
    00093   if (!::CheckIfCallBackAllowsAvailability(industry_type, IACT_USERCREATION)) return false;
    00094   if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return true;
    00095
    00096   /* raw_industry_construction == 1 means "Build as other industries" */
    00097   return _settings_game.construction.raw_industry_construction == 1;
    00098 }
    00099
    00100 /* static */ bool AIIndustryType::CanProspectIndustry(IndustryType industry_type)
    00101 {
    00102   if (!IsValidIndustryType(industry_type)) return false;
    00103
    00104   if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return false;
    00105   if (!::CheckIfCallBackAllowsAvailability(industry_type, IACT_USERCREATION)) return false;
    00106
    00107   /* raw_industry_construction == 2 means "prospect" */
    00108   return _settings_game.construction.raw_industry_construction == 2;
    00109 }
    00110
    00111 /* static */ bool AIIndustryType::BuildIndustry(IndustryType industry_type, TileIndex tile)
    00112 {
    00113   EnforcePrecondition(false, CanBuildIndustry(industry_type));
    00114   EnforcePrecondition(false, AIMap::IsValidTile(tile));
    00115
    00116   uint32 seed = ::InteractiveRandom();
    00117   return AIObject::DoCommand(tile, (::InteractiveRandomRange(::GetIndustrySpec(industry_type)->num_table) << 8) | industry_type, seed, CMD_BUILD_INDUSTRY);
    00118 }
    00119
    00120 /* static */ bool AIIndustryType::ProspectIndustry(IndustryType industry_type)
    00121 {
    00122   EnforcePrecondition(false, CanProspectIndustry(industry_type));
    00123
    00124   uint32 seed = ::InteractiveRandom();
    00125   return AIObject::DoCommand(0, industry_type, seed, CMD_BUILD_INDUSTRY);
    00126 }
    00127
    00128 /* static */ bool AIIndustryType::IsBuiltOnWater(IndustryType industry_type)
    00129 {
    00130   if (!IsValidIndustryType(industry_type)) return false;
    00131
    00132   return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
    00133 }
    00134
    00135 /* static */ bool AIIndustryType::HasHeliport(IndustryType industry_type)
    00136 {
    00137   if (!IsValidIndustryType(industry_type)) return false;
    00138
    00139   return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
    00140 }
    00141
    00142 /* static */ bool AIIndustryType::HasDock(IndustryType industry_type)
    00143 {
    00144   if (!IsValidIndustryType(industry_type)) return false;
    00145
    00146   return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
    00147 }