# ai_industrytype.cpp

Go to the documentation of this file.

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