script_industrytype.cpp

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