industry_cmd.c
changeset 4346 3f00094f2670
parent 4344 5d0e40cd67b9
child 4354 684ab9249dae
equal deleted inserted replaced
4345:dd12549ad473 4346:3f00094f2670
    36  */
    36  */
    37 static void IndustryPoolNewBlock(uint start_item)
    37 static void IndustryPoolNewBlock(uint start_item)
    38 {
    38 {
    39 	Industry *i;
    39 	Industry *i;
    40 
    40 
    41 	FOR_ALL_INDUSTRIES_FROM(i, start_item) i->index = start_item++;
    41 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    42 	 * TODO - This is just a temporary stage, this will be removed. */
       
    43 	for (i = GetIndustry(start_item); i != NULL; i = (i->index + 1 < GetIndustryPoolSize()) ? GetIndustry(i->index + 1) : NULL) i->index = start_item++;
    42 }
    44 }
    43 
    45 
    44 /* Initialize the industry-pool */
    46 /* Initialize the industry-pool */
    45 MemoryPool _industry_pool = { "Industry", INDUSTRY_POOL_MAX_BLOCKS, INDUSTRY_POOL_BLOCK_SIZE_BITS, sizeof(Industry), &IndustryPoolNewBlock, NULL, 0, 0, NULL };
    47 MemoryPool _industry_pool = { "Industry", INDUSTRY_POOL_MAX_BLOCKS, INDUSTRY_POOL_BLOCK_SIZE_BITS, sizeof(Industry), &IndustryPoolNewBlock, NULL, 0, 0, NULL };
    46 
    48 
  1016 	}
  1018 	}
  1017 
  1019 
  1018 	if (_game_mode == GM_EDITOR) return;
  1020 	if (_game_mode == GM_EDITOR) return;
  1019 
  1021 
  1020 	FOR_ALL_INDUSTRIES(i) {
  1022 	FOR_ALL_INDUSTRIES(i) {
  1021 		if (i->xy != 0) ProduceIndustryGoods(i);
  1023 		ProduceIndustryGoods(i);
  1022 	}
  1024 	}
  1023 }
  1025 }
  1024 
  1026 
  1025 
  1027 
  1026 static bool CheckNewIndustry_NULL(TileIndex tile)
  1028 static bool CheckNewIndustry_NULL(TileIndex tile)
  1139 	t = ClosestTownFromTile(tile, (uint)-1);
  1141 	t = ClosestTownFromTile(tile, (uint)-1);
  1140 
  1142 
  1141 	if (_patches.multiple_industry_per_town) return t;
  1143 	if (_patches.multiple_industry_per_town) return t;
  1142 
  1144 
  1143 	FOR_ALL_INDUSTRIES(i) {
  1145 	FOR_ALL_INDUSTRIES(i) {
  1144 		if (i->xy != 0 &&
  1146 		if (i->type == (byte)type &&
  1145 				i->type == (byte)type &&
       
  1146 				i->town == t) {
  1147 				i->town == t) {
  1147 			_error_message = STR_0287_ONLY_ONE_ALLOWED_PER_TOWN;
  1148 			_error_message = STR_0287_ONLY_ONE_ALLOWED_PER_TOWN;
  1148 			return NULL;
  1149 			return NULL;
  1149 		}
  1150 		}
  1150 	}
  1151 	}
  1373 	if (_patches.same_industry_close && indspec->accepts_cargo[0] == CT_INVALID)
  1374 	if (_patches.same_industry_close && indspec->accepts_cargo[0] == CT_INVALID)
  1374 		return true;
  1375 		return true;
  1375 
  1376 
  1376 	FOR_ALL_INDUSTRIES(i) {
  1377 	FOR_ALL_INDUSTRIES(i) {
  1377 		// check if an industry that accepts the same goods is nearby
  1378 		// check if an industry that accepts the same goods is nearby
  1378 		if (i->xy != 0 &&
  1379 		if (DistanceMax(tile, i->xy) <= 14 &&
  1379 				DistanceMax(tile, i->xy) <= 14 &&
       
  1380 				indspec->accepts_cargo[0] != CT_INVALID &&
  1380 				indspec->accepts_cargo[0] != CT_INVALID &&
  1381 				indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
  1381 				indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
  1382 					_game_mode != GM_EDITOR ||
  1382 					_game_mode != GM_EDITOR ||
  1383 					!_patches.same_industry_close ||
  1383 					!_patches.same_industry_close ||
  1384 					!_patches.multiple_industry_per_town
  1384 					!_patches.multiple_industry_per_town
  1386 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1386 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1387 			return false;
  1387 			return false;
  1388 		}
  1388 		}
  1389 
  1389 
  1390 		// check "not close to" field.
  1390 		// check "not close to" field.
  1391 		if (i->xy != 0 &&
  1391 		if ((i->type == indspec->conflicting[0] || i->type == indspec->conflicting[1] || i->type == indspec->conflicting[2]) &&
  1392 				(i->type == indspec->conflicting[0] || i->type == indspec->conflicting[1] || i->type == indspec->conflicting[2]) &&
       
  1393 				DistanceMax(tile, i->xy) <= 14) {
  1392 				DistanceMax(tile, i->xy) <= 14) {
  1394 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1393 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1395 			return false;
  1394 			return false;
  1396 		}
  1395 		}
  1397 	}
  1396 	}
  1400 
  1399 
  1401 static Industry *AllocateIndustry(void)
  1400 static Industry *AllocateIndustry(void)
  1402 {
  1401 {
  1403 	Industry *i;
  1402 	Industry *i;
  1404 
  1403 
  1405 	FOR_ALL_INDUSTRIES(i) {
  1404 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
  1406 		if (i->xy == 0) {
  1405 	 * TODO - This is just a temporary stage, this will be removed. */
  1407 			IndustryID index = i->index;
  1406 	for (i = GetIndustry(0); i != NULL; i = (i->index + 1 < GetIndustryPoolSize()) ? GetIndustry(i->index + 1) : NULL) {
  1408 
  1407 		IndustryID index = i->index;
  1409 			if (i->index > _total_industries) _total_industries = i->index;
  1408 
  1410 
  1409 		if (IsValidIndustry(i)) continue;
  1411 			memset(i, 0, sizeof(*i));
  1410 
  1412 			i->index = index;
  1411 		if (i->index > _total_industries) _total_industries = i->index;
  1413 
  1412 
  1414 			return i;
  1413 		memset(i, 0, sizeof(*i));
  1415 		}
  1414 		i->index = index;
       
  1415 
       
  1416 		return i;
  1416 	}
  1417 	}
  1417 
  1418 
  1418 	/* Check if we can add a block to the pool */
  1419 	/* Check if we can add a block to the pool */
  1419 	return AddBlockToPool(&_industry_pool) ? AllocateIndustry() : NULL;
  1420 	return AddBlockToPool(&_industry_pool) ? AllocateIndustry() : NULL;
  1420 }
  1421 }
  1869 	Industry *i;
  1870 	Industry *i;
  1870 	PlayerID old_player = _current_player;
  1871 	PlayerID old_player = _current_player;
  1871 	_current_player = OWNER_NONE;
  1872 	_current_player = OWNER_NONE;
  1872 
  1873 
  1873 	FOR_ALL_INDUSTRIES(i) {
  1874 	FOR_ALL_INDUSTRIES(i) {
  1874 		if (i->xy != 0) UpdateIndustryStatistics(i);
  1875 		UpdateIndustryStatistics(i);
  1875 	}
  1876 	}
  1876 
  1877 
  1877 	/* 3% chance that we start a new industry */
  1878 	/* 3% chance that we start a new industry */
  1878 	if (CHANCE16(3, 100)) {
  1879 	if (CHANCE16(3, 100)) {
  1879 		MaybeNewIndustry(Random());
  1880 		MaybeNewIndustry(Random());
  1880 	} else if (!_patches.smooth_economy && _total_industries > 0) {
  1881 	} else if (!_patches.smooth_economy && _total_industries > 0) {
  1881 		i = GetIndustry(RandomRange(_total_industries));
  1882 		i = GetIndustry(RandomRange(_total_industries));
  1882 		if (i->xy != 0) ChangeIndustryProduction(i);
  1883 		if (IsValidIndustry(i)) ChangeIndustryProduction(i);
  1883 	}
  1884 	}
  1884 
  1885 
  1885 	_current_player = old_player;
  1886 	_current_player = old_player;
  1886 
  1887 
  1887 	// production-change
  1888 	// production-change
  1951 {
  1952 {
  1952 	Industry *ind;
  1953 	Industry *ind;
  1953 
  1954 
  1954 	// Write the vehicles
  1955 	// Write the vehicles
  1955 	FOR_ALL_INDUSTRIES(ind) {
  1956 	FOR_ALL_INDUSTRIES(ind) {
  1956 		if (ind->xy != 0) {
  1957 		SlSetArrayIndex(ind->index);
  1957 			SlSetArrayIndex(ind->index);
  1958 		SlObject(ind, _industry_desc);
  1958 			SlObject(ind, _industry_desc);
       
  1959 		}
       
  1960 	}
  1959 	}
  1961 }
  1960 }
  1962 
  1961 
  1963 static void Load_INDY(void)
  1962 static void Load_INDY(void)
  1964 {
  1963 {