ai_industrytype.cpp

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

Generated on Fri Dec 31 17:15:28 2010 for OpenTTD by  doxygen 1.6.1