ai_industrytype.cpp

Go to the documentation of this file.
00001 /* $Id: ai_industrytype.cpp 15441 2009-02-11 00:48:15Z yexo $ */
00002 
00005 #include "ai_industrytype.hpp"
00006 #include "ai_map.hpp"
00007 #include "../../command_type.h"
00008 #include "../../settings_type.h"
00009 #include "../../strings_func.h"
00010 #include "../../industry.h"
00011 
00012 /* static */ bool AIIndustryType::IsValidIndustryType(IndustryType industry_type)
00013 {
00014   if (industry_type >= NUM_INDUSTRYTYPES) return false;
00015 
00016   return ::GetIndustrySpec(industry_type)->enabled;
00017 }
00018 
00019 /* static */ bool AIIndustryType::IsRawIndustry(IndustryType industry_type)
00020 {
00021   if (!IsValidIndustryType(industry_type)) return false;
00022 
00023   return ::GetIndustrySpec(industry_type)->IsRawIndustry();
00024 }
00025 
00026 /* static */ bool AIIndustryType::ProductionCanIncrease(IndustryType industry_type)
00027 {
00028   if (!IsValidIndustryType(industry_type)) return false;
00029 
00030   if (_settings_game.game_creation.landscape != LT_TEMPERATE) return true;
00031   return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_DONT_INCR_PROD) == 0;
00032 }
00033 
00034 /* static */ Money AIIndustryType::GetConstructionCost(IndustryType industry_type)
00035 {
00036   if (!IsValidIndustryType(industry_type)) return false;
00037 
00038   return ::GetIndustrySpec(industry_type)->GetConstructionCost();
00039 }
00040 
00041 /* static */ char *AIIndustryType::GetName(IndustryType industry_type)
00042 {
00043   if (!IsValidIndustryType(industry_type)) return NULL;
00044   static const int len = 64;
00045   char *industrytype_name = MallocT<char>(len);
00046 
00047   ::GetString(industrytype_name, ::GetIndustrySpec(industry_type)->name, &industrytype_name[len - 1]);
00048 
00049   return industrytype_name;
00050 }
00051 
00052 /* static */ AIList *AIIndustryType::GetProducedCargo(IndustryType industry_type)
00053 {
00054   if (!IsValidIndustryType(industry_type)) return NULL;
00055 
00056   const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
00057 
00058   AIList *list = new AIList();
00059   for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) {
00060     if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i], 0);
00061   }
00062 
00063   return list;
00064 }
00065 
00066 /* static */ AIList *AIIndustryType::GetAcceptedCargo(IndustryType industry_type)
00067 {
00068   if (!IsValidIndustryType(industry_type)) return NULL;
00069 
00070   const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
00071 
00072   AIList *list = new AIList();
00073   for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) {
00074     if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i], 0);
00075   }
00076 
00077   return list;
00078 }
00079 
00080 /* static */ bool AIIndustryType::CanBuildIndustry(IndustryType industry_type)
00081 {
00082   if (!IsValidIndustryType(industry_type)) return false;
00083   if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return true;
00084 
00085   /* raw_industry_construction == 1 means "Build as other industries" */
00086   return _settings_game.construction.raw_industry_construction == 1;
00087 }
00088 
00089 /* static */ bool AIIndustryType::CanProspectIndustry(IndustryType industry_type)
00090 {
00091   if (!IsValidIndustryType(industry_type)) return false;
00092   if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return false;
00093 
00094   /* raw_industry_construction == 2 means "prospect" */
00095   return _settings_game.construction.raw_industry_construction == 2;
00096 }
00097 
00098 /* static */ bool AIIndustryType::BuildIndustry(IndustryType industry_type, TileIndex tile)
00099 {
00100   EnforcePrecondition(false, CanBuildIndustry(industry_type));
00101   EnforcePrecondition(false, AIMap::IsValidTile(tile));
00102 
00103   uint32 seed = ::InteractiveRandom();
00104   return AIObject::DoCommand(tile, (::InteractiveRandomRange(::GetIndustrySpec(industry_type)->num_table) << 16) | industry_type, seed, CMD_BUILD_INDUSTRY);
00105 }
00106 
00107 /* static */ bool AIIndustryType::ProspectIndustry(IndustryType industry_type)
00108 {
00109   EnforcePrecondition(false, CanProspectIndustry(industry_type));
00110 
00111   uint32 seed = ::InteractiveRandom();
00112   return AIObject::DoCommand(0, industry_type, seed, CMD_BUILD_INDUSTRY);
00113 }

Generated on Mon Mar 9 23:33:45 2009 for openttd by  doxygen 1.5.6