src/industry_cmd.cpp
changeset 6915 99e67df845fd
parent 6886 742e1d75f442
child 6923 6913c8a82cc0
equal deleted inserted replaced
6914:3ba37b6fa39b 6915:99e67df845fd
  1716 	} else if (_patches.smooth_economy) {
  1716 	} else if (_patches.smooth_economy) {
  1717 		ExtChangeIndustryProduction(i);
  1717 		ExtChangeIndustryProduction(i);
  1718 	}
  1718 	}
  1719 }
  1719 }
  1720 
  1720 
  1721 static const byte _new_industry_rand[4][32] = {
  1721 /**
  1722 	{12, 12, 12, 12, 12, 12, 12,  0,  0,  6,  6,  9,  9,  3,  3,  3, 18, 18,  4,  4,  2,  2,  5,  5,  5,  5,  5,  5,  1,  1,  8,  8},
  1722  * Try to create a random industry, during gameplay
  1723 	{16, 16, 16,  0,  0,  0,  9,  9,  9,  9, 13, 13,  3,  3,  3,  3, 15, 15, 15,  4,  4, 11, 11, 11, 11, 11, 14, 14,  1,  1,  7,  7},
  1723  */
  1724 	{21, 21, 21, 24, 22, 22, 22, 22, 23, 23, 16, 16, 16,  4,  4, 19, 19, 19, 13, 13, 20, 20, 20, 11, 11, 11, 17, 17, 17, 10, 10, 10},
  1724 static void MaybeNewIndustry(void)
  1725 	{30, 30, 30, 36, 36, 31, 31, 31, 27, 27, 27, 28, 28, 28, 26, 26, 26, 34, 34, 34, 35, 35, 35, 29, 29, 29, 32, 32, 32, 33, 33, 33},
  1725 {
  1726 };
  1726 	Industry *ind;               //will receive the industry's creation pointer
  1727 
  1727 	IndustryType rndtype, j;     // Loop controlers
  1728 static void MaybeNewIndustry(uint32 r)
  1728 	const IndustrySpec *ind_spc;
  1729 {
  1729 	uint num = 0;
  1730 	int type =_new_industry_rand[_opt.landscape][GB(r, 16, 5)];
  1730 	uint16 cumulative_probs[IT_END];
  1731 	int j;
  1731 	uint16 probability_max = 0;
  1732 	Industry *i;
  1732 
  1733 	const IndustrySpec *ind_spc = GetIndustrySpec(type);;
  1733 	/* Generate a list of all possible industries that can be built. */
  1734 
  1734 	for (j = 0; j < IT_END; j++) {
       
  1735 		byte chance = GetIndustrySpec(j)->appear_ingame[_opt.landscape];
       
  1736 
       
  1737 		/* if appearing chance for this landscape is above 0, this industry can be chosen */
       
  1738 		if (chance != 0) {
       
  1739 			probability_max += chance;
       
  1740 			cumulative_probs[num++] = probability_max;
       
  1741 		}
       
  1742 	}
       
  1743 
       
  1744 	/* Find a random type, with maximum been what has been evaluate above*/
       
  1745 	rndtype = RandomRange(probability_max);
       
  1746 	for (j = 0; j < num; j++) {
       
  1747 		/* and choose the industry thta matches as close as possible this random type */
       
  1748 		if (cumulative_probs[j] >= rndtype) break;
       
  1749 	}
       
  1750 
       
  1751 	ind_spc = GetIndustrySpec(j);
       
  1752 	/*  Check if it is allowed */
  1735 	if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
  1753 	if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
  1736 	if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
  1754 	if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
  1737 
  1755 
  1738 	j = 2000;
  1756 	num = 2000;
  1739 	for (;;) {
  1757 	for (;;) {
  1740 		i = CreateNewIndustry(RandomTile(), type);
  1758 		ind = CreateNewIndustry(RandomTile(), j);
  1741 		if (i != NULL) break;
  1759 		if (ind != NULL) break;
  1742 		if (--j == 0) return;
  1760 		if (--num == 0) return;
  1743 	}
  1761 	}
  1744 
  1762 
  1745 	SetDParam(0, ind_spc->name);
  1763 	SetDParam(0, ind_spc->name);
  1746 	SetDParam(1, i->town->index);
  1764 	SetDParam(1, ind->town->index);
  1747 	AddNewsItem(ind_spc->new_industry_text,
  1765 	AddNewsItem(ind_spc->new_industry_text,
  1748 		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_OPENCLOSE, 0), i->xy, 0);
  1766 		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_OPENCLOSE, 0), ind->xy, 0);
  1749 }
  1767 }
  1750 
  1768 
  1751 static void ChangeIndustryProduction(Industry *i)
  1769 static void ChangeIndustryProduction(Industry *i)
  1752 {
  1770 {
  1753 	bool only_decrease = false;
  1771 	bool only_decrease = false;
  1826 		UpdateIndustryStatistics(i);
  1844 		UpdateIndustryStatistics(i);
  1827 	}
  1845 	}
  1828 
  1846 
  1829 	/* 3% chance that we start a new industry */
  1847 	/* 3% chance that we start a new industry */
  1830 	if (CHANCE16(3, 100)) {
  1848 	if (CHANCE16(3, 100)) {
  1831 		MaybeNewIndustry(Random());
  1849 		MaybeNewIndustry();
  1832 	} else if (!_patches.smooth_economy) {
  1850 	} else if (!_patches.smooth_economy) {
  1833 		i = GetRandomIndustry();
  1851 		i = GetRandomIndustry();
  1834 		if (i != NULL) ChangeIndustryProduction(i);
  1852 		if (i != NULL) ChangeIndustryProduction(i);
  1835 	}
  1853 	}
  1836 
  1854