src/industry_cmd.cpp
changeset 6201 bee01dc45e39
parent 6092 53c6b9b18d0b
child 6247 7d81e3a5d803
equal deleted inserted replaced
6200:10efd7fb90d4 6201:bee01dc45e39
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file industry_cmd.cpp */
     2 
     4 
     3 #include "stdafx.h"
     5 #include "stdafx.h"
     4 #include "openttd.h"
     6 #include "openttd.h"
     5 #include "clear_map.h"
     7 #include "clear_map.h"
     6 #include "functions.h"
     8 #include "functions.h"
    78  * Accessor for array _industry_specs.
    80  * Accessor for array _industry_specs.
    79  * This will ensure at once : proper access and
    81  * This will ensure at once : proper access and
    80  * not allowing modifications of it.
    82  * not allowing modifications of it.
    81  * @param thistype of industry (which is the index in _industry_specs)
    83  * @param thistype of industry (which is the index in _industry_specs)
    82  * @pre thistype < IT_END
    84  * @pre thistype < IT_END
       
    85  * @return a pointer to the corresponding industry spec
    83  **/
    86  **/
    84 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
    87 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
    85 {
    88 {
    86 	assert(thistype < IT_END);
    89 	assert(thistype < IT_END);
    87 	return &_industry_specs[thistype];
    90 	return &_industry_specs[thistype];
    88 }
    91 }
    89 
    92 
       
    93 /**
       
    94  * Accessor for array _industry_tile_specs.
       
    95  * This will ensure at once : proper access and
       
    96  * not allowing modifications of it.
       
    97  * @param gfx of industrytile (which is the index in _industry_specs)
       
    98  * @pre gfx < NUM_INDUSTRY_GFXES
       
    99  * @return a pointer to the corresponding industrytile spec
       
   100  **/
    90 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
   101 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
    91 {
   102 {
    92 	assert(gfx < NUM_INDUSTRY_GFXES);
   103 	assert(gfx < NUM_INDUSTRY_GFXES);
    93 	return &_industry_tile_specs[gfx];
   104 	return &_industry_tile_specs[gfx];
    94 }
   105 }
   425 
   436 
   426 			MarkTileDirtyByTile(tile);
   437 			MarkTileDirtyByTile(tile);
   427 		}
   438 		}
   428 		break;
   439 		break;
   429 
   440 
   430 	// Sparks on a coal plant
   441 	/* Sparks on a coal plant */
   431 	case GFX_POWERPLANT_SPARKS:
   442 	case GFX_POWERPLANT_SPARKS:
   432 		if ((_tick_counter & 3) == 0) {
   443 		if ((_tick_counter & 3) == 0) {
   433 			m = GetIndustryAnimationState(tile);
   444 			m = GetIndustryAnimationState(tile);
   434 			if (m == 6) {
   445 			if (m == 6) {
   435 				SetIndustryAnimationState(tile, 0);
   446 				SetIndustryAnimationState(tile, 0);
   867 
   878 
   868 /**
   879 /**
   869  * Search callback function for ChopLumberMillTrees
   880  * Search callback function for ChopLumberMillTrees
   870  * @param tile to test
   881  * @param tile to test
   871  * @param data that is passed by the caller.  In this case, nothing
   882  * @param data that is passed by the caller.  In this case, nothing
   872  * @result of the test
   883  * @return the result of the test
   873  */
   884  */
   874 static bool SearchLumberMillTrees(TileIndex tile, uint32 data)
   885 static bool SearchLumberMillTrees(TileIndex tile, uint32 data)
   875 {
   886 {
   876 	if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { ///< 3 and up means all fully grown trees
   887 	if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { ///< 3 and up means all fully grown trees
   877 		PlayerID old_player = _current_player;
   888 		PlayerID old_player = _current_player;
  1315 static bool CheckIfTooCloseToIndustry(TileIndex tile, int type)
  1326 static bool CheckIfTooCloseToIndustry(TileIndex tile, int type)
  1316 {
  1327 {
  1317 	const IndustrySpec *indspec = GetIndustrySpec(type);
  1328 	const IndustrySpec *indspec = GetIndustrySpec(type);
  1318 	const Industry *i;
  1329 	const Industry *i;
  1319 
  1330 
  1320 	// accepting industries won't be close, not even with patch
  1331 	/* accepting industries won't be close, not even with patch */
  1321 	if (_patches.same_industry_close && indspec->accepts_cargo[0] == CT_INVALID)
  1332 	if (_patches.same_industry_close && indspec->accepts_cargo[0] == CT_INVALID)
  1322 		return true;
  1333 		return true;
  1323 
  1334 
  1324 	FOR_ALL_INDUSTRIES(i) {
  1335 	FOR_ALL_INDUSTRIES(i) {
  1325 		// check if an industry that accepts the same goods is nearby
  1336 		/* check if an industry that accepts the same goods is nearby */
  1326 		if (DistanceMax(tile, i->xy) <= 14 &&
  1337 		if (DistanceMax(tile, i->xy) <= 14 &&
  1327 				indspec->accepts_cargo[0] != CT_INVALID &&
  1338 				indspec->accepts_cargo[0] != CT_INVALID &&
  1328 				indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
  1339 				indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
  1329 					_game_mode != GM_EDITOR ||
  1340 					_game_mode != GM_EDITOR ||
  1330 					!_patches.same_industry_close ||
  1341 					!_patches.same_industry_close ||
  1332 				)) {
  1343 				)) {
  1333 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1344 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1334 			return false;
  1345 			return false;
  1335 		}
  1346 		}
  1336 
  1347 
  1337 		// check "not close to" field.
  1348 		/* check "not close to" field. */
  1338 		if ((i->type == indspec->conflicting[0] || i->type == indspec->conflicting[1] || i->type == indspec->conflicting[2]) &&
  1349 		if ((i->type == indspec->conflicting[0] || i->type == indspec->conflicting[1] || i->type == indspec->conflicting[2]) &&
  1339 				DistanceMax(tile, i->xy) <= 14) {
  1350 				DistanceMax(tile, i->xy) <= 14) {
  1340 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1351 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1341 			return false;
  1352 			return false;
  1342 		}
  1353 		}
  1443 	}
  1454 	}
  1444 	_industry_sort_dirty = true;
  1455 	_industry_sort_dirty = true;
  1445 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
  1456 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
  1446 }
  1457 }
  1447 
  1458 
       
  1459 /** Helper function for Build/Fund an industry
       
  1460  * @param tile tile where industry is built
       
  1461  * @param type of industry to build
       
  1462  * @param flags of operations to conduct
       
  1463  * @param indspec pointer to industry specifications
       
  1464  * @param it pointer to list of tile type to build
       
  1465  * @return the pointer of the newly created industry, or NULL if it failed
       
  1466  */
  1448 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, uint32 flags, const IndustrySpec *indspec, const IndustryTileTable *it)
  1467 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, uint32 flags, const IndustrySpec *indspec, const IndustryTileTable *it)
  1449 {
  1468 {
  1450 	const Town *t;
  1469 	const Town *t;
  1451 	Industry *i;
  1470 	Industry *i;
  1452 
  1471 
  1472 	return i;
  1491 	return i;
  1473 }
  1492 }
  1474 
  1493 
  1475 /** Build/Fund an industry
  1494 /** Build/Fund an industry
  1476  * @param tile tile where industry is built
  1495  * @param tile tile where industry is built
  1477  * @param p1 industry type @see build_industry.h and @see industry.h
  1496  * @param flags of operations to conduct
       
  1497  * @param p1 industry type see build_industry.h and see industry.h
  1478  * @param p2 unused
  1498  * @param p2 unused
       
  1499  * @return index of the newly create industry, or CMD_ERROR if it failed
  1479  */
  1500  */
  1480 int32 CmdBuildIndustry(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1501 int32 CmdBuildIndustry(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1481 {
  1502 {
  1482 	int num;
  1503 	int num;
  1483 	const IndustryTileTable * const *itt;
  1504 	const IndustryTileTable * const *itt;
  1522 
  1543 
  1523 	return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, it);
  1544 	return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, it);
  1524 }
  1545 }
  1525 
  1546 
  1526 static const byte _numof_industry_table[4][12] = {
  1547 static const byte _numof_industry_table[4][12] = {
  1527 	// difficulty settings for number of industries
  1548 	/* difficulty settings for number of industries */
  1528 	{0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   //none
  1549 	{0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   //none
  1529 	{0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   //low
  1550 	{0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   //low
  1530 	{0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   //normal
  1551 	{0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   //normal
  1531 	{0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   //high
  1552 	{0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   //high
  1532 };
  1553 };
  1534 static void PlaceInitialIndustry(IndustryType type, int amount)
  1555 static void PlaceInitialIndustry(IndustryType type, int amount)
  1535 {
  1556 {
  1536 	int num = _numof_industry_table[_opt.diff.number_industries][amount];
  1557 	int num = _numof_industry_table[_opt.diff.number_industries][amount];
  1537 
  1558 
  1538 	if (type == IT_OIL_REFINERY || type == IT_OIL_RIG) {
  1559 	if (type == IT_OIL_REFINERY || type == IT_OIL_RIG) {
  1539 		// These are always placed next to the coastline, so we scale by the perimeter instead.
  1560 		/* These are always placed next to the coastline, so we scale by the perimeter instead. */
  1540 		num = ScaleByMapSize1D(num);
  1561 		num = ScaleByMapSize1D(num);
  1541 	} else {
  1562 	} else {
  1542 		num = ScaleByMapSize(num);
  1563 		num = ScaleByMapSize(num);
  1543 	}
  1564 	}
  1544 
  1565 
  1821 		if (i != NULL) ChangeIndustryProduction(i);
  1842 		if (i != NULL) ChangeIndustryProduction(i);
  1822 	}
  1843 	}
  1823 
  1844 
  1824 	_current_player = old_player;
  1845 	_current_player = old_player;
  1825 
  1846 
  1826 	// production-change
  1847 	/* production-change */
  1827 	_industry_sort_dirty = true;
  1848 	_industry_sort_dirty = true;
  1828 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
  1849 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
  1829 }
  1850 }
  1830 
  1851 
  1831 
  1852 
  1879 	    SLE_VAR(Industry, random_color,        SLE_UINT8),
  1900 	    SLE_VAR(Industry, random_color,        SLE_UINT8),
  1880 	SLE_CONDVAR(Industry, last_prod_year,      SLE_FILE_U8 | SLE_VAR_I32,  0, 30),
  1901 	SLE_CONDVAR(Industry, last_prod_year,      SLE_FILE_U8 | SLE_VAR_I32,  0, 30),
  1881 	SLE_CONDVAR(Industry, last_prod_year,      SLE_INT32,                 31, SL_MAX_VERSION),
  1902 	SLE_CONDVAR(Industry, last_prod_year,      SLE_INT32,                 31, SL_MAX_VERSION),
  1882 	    SLE_VAR(Industry, was_cargo_delivered, SLE_UINT8),
  1903 	    SLE_VAR(Industry, was_cargo_delivered, SLE_UINT8),
  1883 
  1904 
  1884 	// reserve extra space in savegame here. (currently 32 bytes)
  1905 	/* reserve extra space in savegame here. (currently 32 bytes) */
  1885 	SLE_CONDNULL(32, 2, SL_MAX_VERSION),
  1906 	SLE_CONDNULL(32, 2, SL_MAX_VERSION),
  1886 
  1907 
  1887 	SLE_END()
  1908 	SLE_END()
  1888 };
  1909 };
  1889 
  1910 
  1890 static void Save_INDY(void)
  1911 static void Save_INDY(void)
  1891 {
  1912 {
  1892 	Industry *ind;
  1913 	Industry *ind;
  1893 
  1914 
  1894 	// Write the vehicles
  1915 	/* Write the vehicles */
  1895 	FOR_ALL_INDUSTRIES(ind) {
  1916 	FOR_ALL_INDUSTRIES(ind) {
  1896 		SlSetArrayIndex(ind->index);
  1917 		SlSetArrayIndex(ind->index);
  1897 		SlObject(ind, _industry_desc);
  1918 		SlObject(ind, _industry_desc);
  1898 	}
  1919 	}
  1899 }
  1920 }