1 /* 2 * Copyright (c) 1997 - 2002 Hansj�rg Malthaner 3 * 4 * This file is part of the Simutrans project under the artistic licence. 5 * (see licence.txt) 6 */ 7 8 #ifndef factory_builder_t_h 9 #define factory_builder_t_h 10 11 #include "../tpl/stringhashtable_tpl.h" 12 #include "../tpl/weighted_vector_tpl.h" 13 #include "../dataobj/koord3d.h" 14 #include "../descriptor/factory_desc.h" 15 16 class stadt_t; 17 class karte_ptr_t; 18 class player_t; 19 class fabrik_t; 20 21 22 /** 23 * This class builds factories. Never construct factories directly 24 * but always by calling factory_builder_t::build() (for a single factory) 25 * or factory_builder_t::baue_hierachie() (for a full chain of suppliers). 26 */ 27 class factory_builder_t 28 { 29 private: 30 static karte_ptr_t welt; 31 32 /** 33 * @class factories_to_crossconnect_t 34 * Used for cross-connection checks between factories. 35 * This is necessary for finding producers for factory supply. 36 */ 37 class factories_to_crossconnect_t { 38 public: 39 fabrik_t *fab; ///< The factory 40 sint32 demand; ///< To how many factories this factory needs to connect to 41 factories_to_crossconnect_t()42 factories_to_crossconnect_t() { fab = NULL; demand = 0; } factories_to_crossconnect_t(fabrik_t * f,sint32 d)43 factories_to_crossconnect_t(fabrik_t *f, sint32 d) { fab = f; demand = d; } 44 45 bool operator == (const factories_to_crossconnect_t& x) const { return fab == x.fab; } 46 bool operator != (const factories_to_crossconnect_t& x) const { return fab != x.fab; } 47 }; 48 49 /// Table of all factories that can be built 50 static stringhashtable_tpl<const factory_desc_t *> desc_table; 51 52 /// @returns the number of producers producing @p ware 53 static int count_producers(const goods_desc_t *ware, uint16 timeline); 54 55 /** 56 * Finds a random producer producing @p ware. 57 * @param timeline the current time (months) 58 */ 59 static void find_producer(weighted_vector_tpl<const factory_desc_t *> &producer, const goods_desc_t *ware, uint16 timeline ); 60 61 public: 62 /// Registers the factory description so the factory can be built in-game. 63 static void register_desc(factory_desc_t *desc); 64 65 /** 66 * Initializes weighted vector for farm field class indices. 67 * @returns true 68 */ 69 static bool successfully_loaded(); 70 71 /** 72 * Tells the factory builder a new map is being loaded or generated. 73 * In this case the list of all factory positions must be reinitialized. 74 */ 75 static void new_world(); 76 77 /// Creates a certain number of tourist attractions. 78 static void distribute_attractions(int max_number); 79 80 /// @returns a factory description for a factory name 81 static const factory_desc_t *get_desc(const char *factory_name); 82 83 /// @returns the table containing all factory descriptions get_factory_table()84 static const stringhashtable_tpl<const factory_desc_t*>& get_factory_table() { return desc_table; } 85 86 /** 87 * @param electric true to limit search to electricity producers only 88 * @param cl allowed climates 89 * @returns a random consumer 90 */ 91 static const factory_desc_t *get_random_consumer(bool electric, climate_bits cl, uint16 timeline ); 92 93 /** 94 * Builds a single new factory. 95 * 96 * @param parent location of the parent factory 97 * @param info Description for the new factory 98 * @param initial_prod_base Initial base production (-1 to disable) 99 * @param rotate building rotation (0..3) 100 * @returns The newly constructed factory. 101 */ 102 static fabrik_t* build_factory(koord3d* parent, const factory_desc_t* info, sint32 initial_prod_base, int rotate, koord3d pos, player_t* owner); 103 104 /** 105 * Builds a new full chain of factories. Precondition before calling this function: 106 * @p pos is suitable for factory construction and number of chains 107 * is the maximum number of good types for which suppliers chains are built 108 * (meaning there are no unfinished factory chains). 109 * @returns number of factories built 110 */ 111 static int build_link(koord3d* parent, const factory_desc_t* info, sint32 initial_prod_base, int rotate, koord3d* pos, player_t* player, int number_of_chains, bool ignore_climates ); 112 113 /** 114 * Helper function for baue_hierachie(): builds the connections (chain) for one single product) 115 * @returns number of factories built 116 */ 117 static int build_chain_link(const fabrik_t* our_fab, const factory_desc_t* info, int supplier_nr, player_t* player); 118 119 /** 120 * This function is called whenever it is time for industry growth. 121 * If there is still a pending consumer, this method will first complete another chain for it. 122 * If not, it will decide to either build a power station (if power is needed) 123 * or build a new consumer near the indicated position. 124 * @returns number of factories built 125 */ 126 static int increase_industry_density( bool tell_me ); 127 128 private: 129 /** 130 * Checks if the site at @p pos is suitable for construction. 131 * @param size Size of the building site 132 * @param water true to search on water 133 * @param cl allowed climates 134 */ 135 static bool check_construction_site(koord pos, koord size, factory_desc_t::site_t site, bool is_factory, climate_bits cl); 136 137 /** 138 * Find a random site to place a factory. 139 * @param radius Radius of the search circle around @p pos 140 * @param size size of the building site 141 */ 142 static koord3d find_random_construction_site(koord pos, int radius, koord size, factory_desc_t::site_t site, const building_desc_t *desc, bool ignore_climates, uint32 max_iterations); 143 144 /** 145 * Checks if all factories in this factory tree can be rotated. 146 * This method is called recursively on all potential suppliers. 147 * @returns true if all factories in this tree can be rotated. 148 */ 149 static bool can_factory_tree_rotate( const factory_desc_t *desc ); 150 }; 151 152 #endif 153