src/industry_cmd.cpp
branchNewGRF_ports
changeset 6719 4cc327ad39d5
parent 6683 7ec558346172
child 6720 35756db7e577
equal deleted inserted replaced
6718:5a8b295aa345 6719:4cc327ad39d5
    35 void BuildOilRig(TileIndex tile);
    35 void BuildOilRig(TileIndex tile);
    36 
    36 
    37 static byte _industry_sound_ctr;
    37 static byte _industry_sound_ctr;
    38 static TileIndex _industry_sound_tile;
    38 static TileIndex _industry_sound_tile;
    39 
    39 
       
    40 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
       
    41 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
       
    42 
       
    43 /** This function initialize the spec arrays of both
       
    44  * industry and industry tiles.
       
    45  * It adjusts the enabling of the industry too, based on climate availability.
       
    46  * This will allow for clearer testings */
       
    47 void ResetIndustries()
       
    48 {
       
    49 	memset(&_industry_specs, 0, sizeof(_industry_specs));
       
    50 	memcpy(&_industry_specs, &_origin_industry_specs, sizeof(_origin_industry_specs));
       
    51 
       
    52 	/* once performed, enable only the current climate industries */
       
    53 	for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
       
    54 		_industry_specs[i].enabled = HASBIT(_origin_industry_specs[i].climate_availability, _opt.landscape);
       
    55 	}
       
    56 
       
    57 
       
    58 	memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
       
    59 	memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
       
    60 }
       
    61 
    40 /**
    62 /**
    41  * Called if a new block is added to the industry-pool
    63  * Called if a new block is added to the industry-pool
    42  */
    64  */
    43 static void IndustryPoolNewBlock(uint start_item)
    65 static void IndustryPoolNewBlock(uint start_item)
    44 {
    66 {
    53 
    75 
    54 /**
    76 /**
    55  * Retrieve the type for this industry.  Although it is accessed by a tile,
    77  * Retrieve the type for this industry.  Although it is accessed by a tile,
    56  * it will return the general type of industry, and not the sprite index
    78  * it will return the general type of industry, and not the sprite index
    57  * as would do GetIndustryGfx.
    79  * as would do GetIndustryGfx.
    58  * The same information can be accessed by looking at Industry->type
       
    59  * @param tile that is queried
    80  * @param tile that is queried
    60  * @pre IsTileType(tile, MP_INDUSTRY)
    81  * @pre IsTileType(tile, MP_INDUSTRY)
    61  * @return general type for this industry, as defined in industry.h
    82  * @return general type for this industry, as defined in industry.h
    62  **/
    83  **/
    63 IndustryType GetIndustryType(TileIndex tile)
    84 IndustryType GetIndustryType(TileIndex tile)
    64 {
    85 {
    65 	IndustryGfx this_type = GetIndustryGfx(tile);
       
    66 	IndustryType iloop;
       
    67 
       
    68 	assert(IsTileType(tile, MP_INDUSTRY));
    86 	assert(IsTileType(tile, MP_INDUSTRY));
    69 
    87 
    70 	for (iloop = IT_COAL_MINE; iloop < IT_END; iloop += 1) {
    88 	const Industry *ind = GetIndustryByTile(tile);
    71 		if (IS_BYTE_INSIDE(this_type, industry_gfx_Solver[iloop].MinGfx,
    89 	return IsValidIndustry(ind) ? ind->type : (IndustryType)IT_INVALID;
    72 				industry_gfx_Solver[iloop].MaxGfx+1)) {
       
    73 			return iloop;
       
    74 		}
       
    75 	}
       
    76 
       
    77 	return IT_INVALID;  //we have not found equivalent, whatever the reason
       
    78 }
    90 }
    79 
    91 
    80 /**
    92 /**
    81  * Accessor for array _industry_specs.
    93  * Accessor for array _industry_specs.
    82  * This will ensure at once : proper access and
    94  * This will ensure at once : proper access and
    83  * not allowing modifications of it.
    95  * not allowing modifications of it.
    84  * @param thistype of industry (which is the index in _industry_specs)
    96  * @param thistype of industry (which is the index in _industry_specs)
    85  * @pre thistype < IT_END
    97  * @pre thistype < NUM_INDUSTRYTYPES
    86  * @return a pointer to the corresponding industry spec
    98  * @return a pointer to the corresponding industry spec
    87  **/
    99  **/
    88 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
   100 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
    89 {
   101 {
    90 	assert(thistype < IT_END);
   102 	assert(thistype < NUM_INDUSTRYTYPES);
    91 	return &_industry_specs[thistype];
   103 	return &_industry_specs[thistype];
    92 }
   104 }
    93 
   105 
    94 /**
   106 /**
    95  * Accessor for array _industry_tile_specs.
   107  * Accessor for array _industry_tile_specs.
    96  * This will ensure at once : proper access and
   108  * This will ensure at once : proper access and
    97  * not allowing modifications of it.
   109  * not allowing modifications of it.
    98  * @param gfx of industrytile (which is the index in _industry_specs)
   110  * @param gfx of industrytile (which is the index in _industry_specs)
    99  * @pre gfx < NUM_INDUSTRY_GFXES
   111  * @pre gfx < INVALID_INDUSTRYTILE
   100  * @return a pointer to the corresponding industrytile spec
   112  * @return a pointer to the corresponding industrytile spec
   101  **/
   113  **/
   102 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
   114 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
   103 {
   115 {
   104 	assert(gfx < NUM_INDUSTRY_GFXES);
   116 	assert(gfx < INVALID_INDUSTRYTILE);
   105 	return &_industry_tile_specs[gfx];
   117 	return &_industry_tile_specs[gfx];
   106 }
   118 }
   107 
   119 
   108 void DestroyIndustry(Industry *i)
   120 void DestroyIndustry(Industry *i)
   109 {
   121 {
   115 		} else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
   127 		} else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
   116 			DeleteOilRig(tile_cur);
   128 			DeleteOilRig(tile_cur);
   117 		}
   129 		}
   118 	END_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
   130 	END_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
   119 
   131 
   120 	if (i->type == IT_FARM || i->type == IT_FARM_2) {
   132 	if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
   121 		/* Remove the farmland and convert it to regular tiles over time. */
   133 		/* Remove the farmland and convert it to regular tiles over time. */
   122 		BEGIN_TILE_LOOP(tile_cur, 42, 42, i->xy - TileDiffXY(21, 21)) {
   134 		BEGIN_TILE_LOOP(tile_cur, 42, 42, i->xy - TileDiffXY(21, 21)) {
   123 			tile_cur = TILE_MASK(tile_cur);
   135 			tile_cur = TILE_MASK(tile_cur);
   124 			if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
   136 			if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
   125 					GetIndustryIndexOfField(tile_cur) == i->index) {
   137 					GetIndustryIndexOfField(tile_cur) == i->index) {
   135 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
   147 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
   136 }
   148 }
   137 
   149 
   138 static void IndustryDrawSugarMine(const TileInfo *ti)
   150 static void IndustryDrawSugarMine(const TileInfo *ti)
   139 {
   151 {
   140 	const DrawIndustrySpec1Struct *d;
   152 	const DrawIndustryAnimationStruct *d;
   141 	uint32 image;
       
   142 
   153 
   143 	if (!IsIndustryCompleted(ti->tile)) return;
   154 	if (!IsIndustryCompleted(ti->tile)) return;
   144 
   155 
   145 	d = &_draw_industry_spec1[GetIndustryAnimationState(ti->tile)];
   156 	d = &_draw_industry_spec1[GetIndustryAnimationState(ti->tile)];
   146 
   157 
   147 	AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
   158 	AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
   148 
   159 
   149 	image = d->image_2;
   160 	if (d->image_2 != 0) {
   150 	if (image != 0) AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + image - 1, PAL_NONE, 8, 41);
   161 		AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
   151 
   162 	}
   152 	image = d->image_3;
   163 
   153 	if (image != 0) {
   164 	if (d->image_3 != 0) {
   154 		AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + image - 1, PAL_NONE,
   165 		AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
   155 			_drawtile_proc1_x[image - 1], _drawtile_proc1_y[image - 1]);
   166 			_drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
   156 	}
   167 	}
   157 }
   168 }
   158 
   169 
   159 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
   170 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
   160 {
   171 {
   161 	int x = 0;
   172 	uint8 x = 0;
   162 
   173 
   163 	if (IsIndustryCompleted(ti->tile)) {
   174 	if (IsIndustryCompleted(ti->tile)) {
   164 		x = _industry_anim_offs[GetIndustryAnimationState(ti->tile)];
   175 		x = _industry_anim_offs_toffee[GetIndustryAnimationState(ti->tile)];
   165 		if ( (byte)x == 0xFF)
   176 		if (x == 0xFF)
   166 			x = 0;
   177 			x = 0;
   167 	}
   178 	}
   168 
   179 
   169 	AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
   180 	AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
   170 	AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
   181 	AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
   171 }
   182 }
   172 
   183 
   173 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
   184 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
   174 {
   185 {
   175 	if (IsIndustryCompleted(ti->tile)) {
   186 	if (IsIndustryCompleted(ti->tile)) {
   176 		AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_2[GetIndustryAnimationState(ti->tile)]);
   187 		AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetIndustryAnimationState(ti->tile)]);
   177 	} else {
   188 	} else {
   178 		AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
   189 		AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
   179 	}
   190 	}
   180 }
   191 }
   181 
   192 
   182 static void IndustryDrawToyFactory(const TileInfo *ti)
   193 static void IndustryDrawToyFactory(const TileInfo *ti)
   183 {
   194 {
   184 	const DrawIndustrySpec4Struct *d;
   195 	const DrawIndustryAnimationStruct *d;
   185 
   196 
   186 	d = &_industry_anim_offs_3[GetIndustryAnimationState(ti->tile)];
   197 	d = &_industry_anim_offs_toys[GetIndustryAnimationState(ti->tile)];
   187 
   198 
   188 	if (d->image_1 != 0xFF) {
   199 	if (d->image_1 != 0xFF) {
   189 		AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, 50 - d->image_1 * 2, 96 + d->image_1);
   200 		AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
   190 	}
   201 	}
   191 
   202 
   192 	if (d->image_2 != 0xFF) {
   203 	if (d->image_2 != 0xFF) {
   193 		AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
   204 		AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
   194 	}
   205 	}
   198 }
   209 }
   199 
   210 
   200 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
   211 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
   201 {
   212 {
   202 	if (IsIndustryCompleted(ti->tile)) {
   213 	if (IsIndustryCompleted(ti->tile)) {
   203 		uint image = GetIndustryAnimationState(ti->tile);
   214 		uint8 image = GetIndustryAnimationState(ti->tile);
   204 
   215 
   205 		if (image != 0 && image < 7) {
   216 		if (image != 0 && image < 7) {
   206 			AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
   217 			AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
   207 				PAL_NONE,
   218 				PAL_NONE,
   208 				_coal_plant_sparks_x[image - 1],
   219 				_coal_plant_sparks[image - 1].x,
   209 				_coal_plant_sparks_y[image - 1]
   220 				_coal_plant_sparks[image - 1].y
   210 			);
   221 			);
   211 		}
   222 		}
   212 	}
   223 	}
   213 }
   224 }
   214 
   225 
   257 	}
   268 	}
   258 
   269 
   259 	/* Add industry on top of the ground? */
   270 	/* Add industry on top of the ground? */
   260 	image = dits->building.sprite;
   271 	image = dits->building.sprite;
   261 	if (image != 0) {
   272 	if (image != 0) {
   262 		if (_display_opt & DO_TRANS_BUILDINGS) {
   273 		if (HASBIT(_transparent_opt, TO_INDUSTRIES)) {
   263 			SETBIT(image, PALETTE_MODIFIER_TRANSPARENT);
   274 			SETBIT(image, PALETTE_MODIFIER_TRANSPARENT);
   264 			pal = PALETTE_TO_TRANSPARENT;
   275 			pal = PALETTE_TO_TRANSPARENT;
   265 		} else {
   276 		} else {
   266 			pal = dits->building.pal;
   277 			pal = dits->building.pal;
   267 		}
   278 		}
   272 			dits->width  + 1,
   283 			dits->width  + 1,
   273 			dits->height + 1,
   284 			dits->height + 1,
   274 			dits->dz,
   285 			dits->dz,
   275 			z);
   286 			z);
   276 
   287 
   277 		if (_display_opt & DO_TRANS_BUILDINGS) return;
   288 		if (HASBIT(_transparent_opt, TO_INDUSTRIES)) return;
   278 	}
   289 	}
   279 
   290 
   280 	{
   291 	{
   281 		int proc = dits->draw_proc - 1;
   292 		int proc = dits->draw_proc - 1;
   282 		if (proc >= 0) _industry_draw_tile_procs[proc](ti);
   293 		if (proc >= 0) _industry_draw_tile_procs[proc](ti);
   296 static void GetAcceptedCargo_Industry(TileIndex tile, AcceptedCargo ac)
   307 static void GetAcceptedCargo_Industry(TileIndex tile, AcceptedCargo ac)
   297 {
   308 {
   298 	const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
   309 	const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
   299 	CargoID a;
   310 	CargoID a;
   300 
   311 
   301 	a = itspec->accepts_cargo[0];
   312 	for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
   302 	if (a != CT_INVALID) ac[a] = (a == CT_PASSENGERS) ? 1 : 8;
   313 		a = itspec->accepts_cargo[i];
   303 
   314 		if (a != CT_INVALID) ac[a] = itspec->acceptance[i];
   304 	a = itspec->accepts_cargo[1];
   315 	}
   305 	if (a != CT_INVALID) ac[a] = 8;
       
   306 
       
   307 	a = itspec->accepts_cargo[2];
       
   308 	if (a != CT_INVALID) ac[a] = 8;
       
   309 }
   316 }
   310 
   317 
   311 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
   318 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
   312 {
   319 {
   313 	const Industry *i = GetIndustryByTile(tile);
   320 	const Industry *i = GetIndustryByTile(tile);
   321 }
   328 }
   322 
   329 
   323 static int32 ClearTile_Industry(TileIndex tile, byte flags)
   330 static int32 ClearTile_Industry(TileIndex tile, byte flags)
   324 {
   331 {
   325 	Industry *i = GetIndustryByTile(tile);
   332 	Industry *i = GetIndustryByTile(tile);
       
   333 	const IndustrySpec *indspec = GetIndustrySpec(i->type);
   326 
   334 
   327 	/* water can destroy industries
   335 	/* water can destroy industries
   328 	 * in editor you can bulldoze industries
   336 	 * in editor you can bulldoze industries
   329 	 * with magic_bulldozer cheat you can destroy industries
   337 	 * with magic_bulldozer cheat you can destroy industries
   330 	 * (area around OILRIG is water, so water shouldn't flood it
   338 	 * (area around OILRIG is water, so water shouldn't flood it
   331 	 */
   339 	 */
   332 	if ((_current_player != OWNER_WATER && _game_mode != GM_EDITOR &&
   340 	if ((_current_player != OWNER_WATER && _game_mode != GM_EDITOR &&
   333 			!_cheats.magic_bulldozer.value) ||
   341 			!_cheats.magic_bulldozer.value) ||
   334 			(_current_player == OWNER_WATER && i->type == IT_OIL_RIG)) {
   342 			(_current_player == OWNER_WATER && (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER))) {
   335 		SetDParam(0, GetIndustrySpec(i->type)->name);
   343 		SetDParam(0, indspec->name);
   336 		return_cmd_error(STR_4800_IN_THE_WAY);
   344 		return_cmd_error(STR_4800_IN_THE_WAY);
   337 	}
   345 	}
   338 
   346 
   339 	if (flags & DC_EXEC) DeleteIndustry(i);
   347 	if (flags & DC_EXEC) DeleteIndustry(i);
   340 	return 0;
   348 	return 0;
   353 		/* fluctuating economy? */
   361 		/* fluctuating economy? */
   354 		if (_economy.fluct <= 0) cw = (cw + 1) / 2;
   362 		if (_economy.fluct <= 0) cw = (cw + 1) / 2;
   355 
   363 
   356 		i->last_mo_production[0] += cw;
   364 		i->last_mo_production[0] += cw;
   357 
   365 
   358 		am = MoveGoodsToStation(i->xy, i->width, i->height, i->produced_cargo[0], cw);
   366 		am = MoveGoodsToStation(i->xy, i->width, i->height, indspec->produced_cargo[0], cw);
   359 		i->last_mo_transported[0] += am;
   367 		i->last_mo_transported[0] += am;
   360 		if (am != 0) {
   368 		if (am != 0) {
   361 			uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
   369 			uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
   362 
   370 
   363 			if (newgfx != INDUTILE_NOANIM) {
   371 			if (newgfx != INDUSTRYTILE_NOANIM) {
   364 				ResetIndustryConstructionStage(tile);
   372 				ResetIndustryConstructionStage(tile);
   365 				SetIndustryCompleted(tile, true);
   373 				SetIndustryCompleted(tile, true);
   366 				SetIndustryGfx(tile, newgfx);
   374 				SetIndustryGfx(tile, newgfx);
   367 				MarkTileDirtyByTile(tile);
   375 				MarkTileDirtyByTile(tile);
   368 			}
   376 			}
   375 
   383 
   376 		if (_economy.fluct <= 0) cw = (cw + 1) / 2;
   384 		if (_economy.fluct <= 0) cw = (cw + 1) / 2;
   377 
   385 
   378 		i->last_mo_production[1] += cw;
   386 		i->last_mo_production[1] += cw;
   379 
   387 
   380 		am = MoveGoodsToStation(i->xy, i->width, i->height, i->produced_cargo[1], cw);
   388 		am = MoveGoodsToStation(i->xy, i->width, i->height, indspec->produced_cargo[1], cw);
   381 		i->last_mo_transported[1] += am;
   389 		i->last_mo_transported[1] += am;
   382 	}
   390 	}
   383 }
   391 }
   384 
   392 
   385 
   393 
   409 
   417 
   410 	case GFX_TOFFEE_QUARY:
   418 	case GFX_TOFFEE_QUARY:
   411 		if ((_tick_counter & 3) == 0) {
   419 		if ((_tick_counter & 3) == 0) {
   412 			m = GetIndustryAnimationState(tile);
   420 			m = GetIndustryAnimationState(tile);
   413 
   421 
   414 			if (_industry_anim_offs[m] == 0xFF) {
   422 			if (_industry_anim_offs_toffee[m] == 0xFF) {
   415 				SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
   423 				SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
   416 			}
   424 			}
   417 
   425 
   418 			if (++m >= 70) {
   426 			if (++m >= 70) {
   419 				m = 0;
   427 				m = 0;
   455 
   463 
   456 	case GFX_TOY_FACTORY:
   464 	case GFX_TOY_FACTORY:
   457 		if ((_tick_counter & 1) == 0) {
   465 		if ((_tick_counter & 1) == 0) {
   458 			m = GetIndustryAnimationState(tile) + 1;
   466 			m = GetIndustryAnimationState(tile) + 1;
   459 
   467 
   460 			if (m == 1) {
   468 			switch (m) {
   461 				SndPlayTileFx(SND_2C_MACHINERY, tile);
   469 				case  1: SndPlayTileFx(SND_2C_MACHINERY, tile); break;
   462 			} else if (m == 23) {
   470 				case 23: SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
   463 				SndPlayTileFx(SND_2B_COMEDY_HIT, tile);
   471 				case 28: SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
   464 			} else if (m == 28) {
   472 				default:
   465 				SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile);
   473 					if (m >= 50) {
   466 			}
   474 						int n = GetIndustryAnimationLoop(tile) + 1;
   467 
   475 						m = 0;
   468 			if (m >= 50) {
   476 						if (n >= 8) {
   469 				int n = GetIndustryAnimationLoop(tile) + 1;
   477 							n = 0;
   470 				m = 0;
   478 							DeleteAnimatedTile(tile);
   471 				if (n >= 8) {
   479 						}
   472 					n = 0;
   480 						SetIndustryAnimationLoop(tile, n);
   473 					DeleteAnimatedTile(tile);
   481 					}
   474 				}
   482 			}
   475 				SetIndustryAnimationLoop(tile, n);
   483 
   476 			}
       
   477 			SetIndustryAnimationState(tile, m);
   484 			SetIndustryAnimationState(tile, m);
   478 			MarkTileDirtyByTile(tile);
   485 			MarkTileDirtyByTile(tile);
   479 		}
   486 		}
   480 		break;
   487 		break;
   481 
   488 
   494 
   501 
   495 	case GFX_OILWELL_ANIMATED_1:
   502 	case GFX_OILWELL_ANIMATED_1:
   496 	case GFX_OILWELL_ANIMATED_2:
   503 	case GFX_OILWELL_ANIMATED_2:
   497 	case GFX_OILWELL_ANIMATED_3:
   504 	case GFX_OILWELL_ANIMATED_3:
   498 		if ((_tick_counter & 7) == 0) {
   505 		if ((_tick_counter & 7) == 0) {
   499 			bool b = CHANCE16(1,7);
   506 			bool b = CHANCE16(1, 7);
   500 			IndustryGfx gfx = GetIndustryGfx(tile);
   507 			IndustryGfx gfx = GetIndustryGfx(tile);
   501 
   508 
   502 			m = GetIndustryAnimationState(tile) + 1;
   509 			m = GetIndustryAnimationState(tile) + 1;
   503 			if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
   510 			if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
   504 				SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
   511 				SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
   645 	if (_game_mode == GM_EDITOR) return;
   652 	if (_game_mode == GM_EDITOR) return;
   646 
   653 
   647 	TransportIndustryGoods(tile);
   654 	TransportIndustryGoods(tile);
   648 
   655 
   649 	newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
   656 	newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
   650 	if (newgfx != INDUTILE_NOANIM) {
   657 	if (newgfx != INDUSTRYTILE_NOANIM) {
   651 		ResetIndustryConstructionStage(tile);
   658 		ResetIndustryConstructionStage(tile);
   652 		SetIndustryGfx(tile, newgfx);
   659 		SetIndustryGfx(tile, newgfx);
   653 		MarkTileDirtyByTile(tile);
   660 		MarkTileDirtyByTile(tile);
   654 		return;
   661 		return;
   655 	}
   662 	}
   703 			DeleteAnimatedTile(tile);
   710 			DeleteAnimatedTile(tile);
   704 		}
   711 		}
   705 		break;
   712 		break;
   706 
   713 
   707 	case GFX_POWERPLANT_SPARKS:
   714 	case GFX_POWERPLANT_SPARKS:
   708 		if (CHANCE16(1,3)) {
   715 		if (CHANCE16(1, 3)) {
   709 			SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
   716 			SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
   710 			AddAnimatedTile(tile);
   717 			AddAnimatedTile(tile);
   711 		}
   718 		}
   712 		break;
   719 		break;
   713 
   720 
   744 static void ClickTile_Industry(TileIndex tile)
   751 static void ClickTile_Industry(TileIndex tile)
   745 {
   752 {
   746 	ShowIndustryViewWindow(GetIndustryIndex(tile));
   753 	ShowIndustryViewWindow(GetIndustryIndex(tile));
   747 }
   754 }
   748 
   755 
   749 static uint32 GetTileTrackStatus_Industry(TileIndex tile, TransportType mode)
   756 static uint32 GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode)
   750 {
   757 {
   751 	return 0;
   758 	return 0;
   752 }
   759 }
   753 
   760 
   754 static void GetProducedCargo_Industry(TileIndex tile, CargoID *b)
   761 static void GetProducedCargo_Industry(TileIndex tile, CargoID *b)
   755 {
   762 {
   756 	const Industry *i = GetIndustryByTile(tile);
   763 	const IndustrySpec *i = GetIndustrySpec(GetIndustryByTile(tile)->type);
   757 
   764 
   758 	b[0] = i->produced_cargo[0];
   765 	b[0] = i->produced_cargo[0];
   759 	b[1] = i->produced_cargo[1];
   766 	b[1] = i->produced_cargo[1];
   760 }
   767 }
   761 
   768 
   914 
   921 
   915 	if (CircularTileSearch(tile, 40, SearchLumberMillTrees, 0)) ///< 40x40 tiles  to search
   922 	if (CircularTileSearch(tile, 40, SearchLumberMillTrees, 0)) ///< 40x40 tiles  to search
   916 		i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + 45); ///< Found a tree, add according value to waiting cargo
   923 		i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + 45); ///< Found a tree, add according value to waiting cargo
   917 }
   924 }
   918 
   925 
   919 static const byte _industry_sounds[37][2] = {
       
   920 	{0},
       
   921 	{0},
       
   922 	{1, SND_28_SAWMILL},
       
   923 	{0},
       
   924 	{0},
       
   925 	{0},
       
   926 	{1, SND_03_FACTORY_WHISTLE},
       
   927 	{1, SND_03_FACTORY_WHISTLE},
       
   928 	{0},
       
   929 	{3, SND_24_SHEEP},
       
   930 	{0},
       
   931 	{0},
       
   932 	{0},
       
   933 	{0},
       
   934 	{1, SND_28_SAWMILL},
       
   935 	{0},
       
   936 	{0},
       
   937 	{0},
       
   938 	{0},
       
   939 	{0},
       
   940 	{0},
       
   941 	{0},
       
   942 	{0},
       
   943 	{1, SND_03_FACTORY_WHISTLE},
       
   944 	{0},
       
   945 	{0},
       
   946 	{0},
       
   947 	{0},
       
   948 	{0},
       
   949 	{0},
       
   950 	{0},
       
   951 	{0},
       
   952 	{1, SND_33_PLASTIC_MINE},
       
   953 	{0},
       
   954 	{0},
       
   955 	{0},
       
   956 	{0},
       
   957 };
       
   958 
       
   959 
       
   960 static void ProduceIndustryGoods(Industry *i)
   926 static void ProduceIndustryGoods(Industry *i)
   961 {
   927 {
   962 	uint32 r;
   928 	uint32 r;
   963 	uint num;
   929 	uint num;
       
   930 	const IndustrySpec *indsp = GetIndustrySpec(i->type);
   964 
   931 
   965 	/* play a sound? */
   932 	/* play a sound? */
   966 	if ((i->counter & 0x3F) == 0) {
   933 	if ((i->counter & 0x3F) == 0) {
   967 		if (CHANCE16R(1,14,r) && (num=_industry_sounds[i->type][0]) != 0) {
   934 		if (CHANCE16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
   968 			SndPlayTileFx(
   935 			SndPlayTileFx(
   969 				(SoundFx)(_industry_sounds[i->type][1] + (((r >> 16) * num) >> 16)),
   936 				(SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
   970 				i->xy);
   937 				i->xy);
   971 		}
   938 		}
   972 	}
   939 	}
   973 
   940 
   974 	i->counter--;
   941 	i->counter--;
   975 
   942 
   976 	/* produce some cargo */
   943 	/* produce some cargo */
   977 	if ((i->counter & 0xFF) == 0) {
   944 	if ((i->counter & 0xFF) == 0) {
       
   945 		IndustyBehaviour indbehav = indsp->behaviour;
   978 		i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + i->production_rate[0]);
   946 		i->cargo_waiting[0] = min(0xffff, i->cargo_waiting[0] + i->production_rate[0]);
   979 		i->cargo_waiting[1] = min(0xffff, i->cargo_waiting[1] + i->production_rate[1]);
   947 		i->cargo_waiting[1] = min(0xffff, i->cargo_waiting[1] + i->production_rate[1]);
   980 
   948 
   981 		if (i->type == IT_FARM || i->type == IT_FARM_2) {
   949 		if (indbehav & INDUSTRYBEH_PLANT_FIELDS) {
   982 			MaybePlantFarmField(i);
   950 			MaybePlantFarmField(i);
   983 		} else if (i->type == IT_LUMBER_MILL && (i->counter & 0x1FF) == 0) {
   951 		} else if ((indbehav & INDUSTRYBEH_CUT_TREES) && (i->counter & 0x1FF) == 0) {
   984 			ChopLumberMillTrees(i);
   952 			ChopLumberMillTrees(i);
   985 		}
   953 		}
   986 	}
   954 	}
   987 }
   955 }
   988 
   956 
  1154 					GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
  1122 					GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
  1155 				return false;
  1123 				return false;
  1156 			}
  1124 			}
  1157 		} else {
  1125 		} else {
  1158 			if (!EnsureNoVehicle(cur_tile)) return false;
  1126 			if (!EnsureNoVehicle(cur_tile)) return false;
  1159 
  1127 			IndustyBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
  1160 			if (type == IT_OIL_RIG)  {
  1128 
       
  1129 			if (ind_behav & INDUSTRYBEH_BUILT_ONWATER) {
       
  1130 				/* As soon as the tile is not water, bail out.
       
  1131 				 * But that does not mean the search is over.  You have
       
  1132 				 * to make sure every tile of the industry will be only water*/
  1161 				if (!IsClearWaterTile(cur_tile)) return false;
  1133 				if (!IsClearWaterTile(cur_tile)) return false;
  1162 			} else {
  1134 			} else {
  1163 				Slope tileh;
  1135 				Slope tileh;
  1164 
  1136 
  1165 				if (IsClearWaterTile(cur_tile)) return false;
  1137 				if (IsClearWaterTile(cur_tile)) return false;
  1184 						if (bits & 4 && (t & SLOPE_SW)) return false;
  1156 						if (bits & 4 && (t & SLOPE_SW)) return false;
  1185 						if (bits & 8 && (t & SLOPE_SE)) return false;
  1157 						if (bits & 8 && (t & SLOPE_SE)) return false;
  1186 					}
  1158 					}
  1187 				}
  1159 				}
  1188 
  1160 
  1189 				if (type == IT_BANK_TEMP) {
  1161 				if (ind_behav & INDUSTRYBEH_ONLY_INTOWN) {
  1190 					if (!IsTileType(cur_tile, MP_HOUSE)) {
  1162 					if (!IsTileType(cur_tile, MP_HOUSE)) {
  1191 						_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1163 						_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1192 						return false;
  1164 						return false;
  1193 					}
  1165 					}
  1194 				} else if (type == IT_BANK_TROPIC_ARCTIC) {
  1166 				} else {
  1195 					if (!IsTileType(cur_tile, MP_HOUSE)) {
  1167 					if (ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) {
  1196 						_error_message = STR_030D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1168 						if (!IsTileType(cur_tile, MP_HOUSE)) goto do_clear;
  1197 						return false;
  1169 					} else {
       
  1170 do_clear:
       
  1171 						if (CmdFailed(DoCommand(cur_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
       
  1172 							return false;
  1198 					}
  1173 					}
  1199 				} else if (type == IT_TOY_SHOP) {
       
  1200 					if (!IsTileType(cur_tile, MP_HOUSE)) goto do_clear;
       
  1201 				} else if (type == IT_WATER_TOWER) {
       
  1202 					if (!IsTileType(cur_tile, MP_HOUSE)) {
       
  1203 						_error_message = STR_0316_CAN_ONLY_BE_BUILT_IN_TOWNS;
       
  1204 						return false;
       
  1205 					}
       
  1206 				} else {
       
  1207 do_clear:
       
  1208 					if (CmdFailed(DoCommand(cur_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
       
  1209 						return false;
       
  1210 				}
  1174 				}
  1211 			}
  1175 			}
  1212 		}
  1176 		}
  1213 	} while ((++it)->ti.x != -0x80);
  1177 	} while ((++it)->ti.x != -0x80);
  1214 
  1178 
  1215 	return true;
  1179 	return true;
  1216 }
  1180 }
  1217 
  1181 
  1218 static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
  1182 static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
  1219 {
  1183 {
  1220 	if (type == IT_BANK_TEMP && t->population < 1200) {
  1184 	if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
  1221 		_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1185 		_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1222 		return false;
  1186 		return false;
  1223 	}
  1187 	}
  1224 
  1188 
  1225 	if (type == IT_TOY_SHOP && DistanceMax(t->xy, tile) > 9) {
  1189 	if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
  1226 		_error_message = STR_0239_SITE_UNSUITABLE;
  1190 		_error_message = STR_0239_SITE_UNSUITABLE;
  1227 		return false;
  1191 		return false;
  1228 	}
  1192 	}
  1229 
  1193 
  1230 	return true;
  1194 	return true;
  1335 
  1299 
  1336 	FOR_ALL_INDUSTRIES(i) {
  1300 	FOR_ALL_INDUSTRIES(i) {
  1337 		/* check if an industry that accepts the same goods is nearby */
  1301 		/* check if an industry that accepts the same goods is nearby */
  1338 		if (DistanceMax(tile, i->xy) <= 14 &&
  1302 		if (DistanceMax(tile, i->xy) <= 14 &&
  1339 				indspec->accepts_cargo[0] != CT_INVALID &&
  1303 				indspec->accepts_cargo[0] != CT_INVALID &&
  1340 				indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
  1304 				indspec->accepts_cargo[0] == indspec->accepts_cargo[0] && (
  1341 					_game_mode != GM_EDITOR ||
  1305 					_game_mode != GM_EDITOR ||
  1342 					!_patches.same_industry_close ||
  1306 					!_patches.same_industry_close ||
  1343 					!_patches.multiple_industry_per_town
  1307 					!_patches.multiple_industry_per_town
  1344 				)) {
  1308 				)) {
  1345 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1309 			_error_message = STR_INDUSTRY_TOO_CLOSE;
  1386 	_total_industries++;
  1350 	_total_industries++;
  1387 	i->xy = tile;
  1351 	i->xy = tile;
  1388 	i->width = i->height = 0;
  1352 	i->width = i->height = 0;
  1389 	i->type = type;
  1353 	i->type = type;
  1390 
  1354 
  1391 	i->produced_cargo[0] = indspec->produced_cargo[0];
       
  1392 	i->produced_cargo[1] = indspec->produced_cargo[1];
       
  1393 	i->accepts_cargo[0] = indspec->accepts_cargo[0];
       
  1394 	i->accepts_cargo[1] = indspec->accepts_cargo[1];
       
  1395 	i->accepts_cargo[2] = indspec->accepts_cargo[2];
       
  1396 	i->production_rate[0] = indspec->production_rate[0];
  1355 	i->production_rate[0] = indspec->production_rate[0];
  1397 	i->production_rate[1] = indspec->production_rate[1];
  1356 	i->production_rate[1] = indspec->production_rate[1];
  1398 
  1357 
  1399 	if (_patches.smooth_economy) {
  1358 	if (_patches.smooth_economy) {
  1400 		i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8 , 255);
  1359 		i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8 , 255);
  1448 	} while ((++it)->ti.x != -0x80);
  1407 	} while ((++it)->ti.x != -0x80);
  1449 
  1408 
  1450 	i->width++;
  1409 	i->width++;
  1451 	i->height++;
  1410 	i->height++;
  1452 
  1411 
  1453 	if (i->type == IT_FARM || i->type == IT_FARM_2) {
  1412 	if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
  1454 		for (j = 0; j != 50; j++) PlantRandomFarmField(i);
  1413 		for (j = 0; j != 50; j++) PlantRandomFarmField(i);
  1455 	}
  1414 	}
  1456 	_industry_sort_dirty = true;
  1415 	_industry_sort_dirty = true;
  1457 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
  1416 	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
  1458 }
  1417 }
  1509 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1468 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1510 
  1469 
  1511 	indspec = GetIndustrySpec(p1);
  1470 	indspec = GetIndustrySpec(p1);
  1512 
  1471 
  1513 	/* Check if the to-be built/founded industry is available for this climate. */
  1472 	/* Check if the to-be built/founded industry is available for this climate. */
  1514 	if (!HASBIT(indspec->climate_availability, _opt_ptr->landscape)) return CMD_ERROR;
  1473 	if (!indspec->enabled) {
       
  1474 		return CMD_ERROR;
       
  1475 	}
  1515 
  1476 
  1516 	/* If the patch for raw-material industries is not on, you cannot build raw-material industries.
  1477 	/* If the patch for raw-material industries is not on, you cannot build raw-material industries.
  1517 	 * Raw material industries are industries that do not accept cargo (at least for now)
  1478 	 * Raw material industries are industries that do not accept cargo (at least for now)
  1518 	 * Exclude the lumber mill (only "raw" industry that can be built) */
  1479 	 * Exclude the lumber mill (only "raw" industry that can be built) */
  1519 	if (!_patches.build_rawmaterial_ind &&
  1480 	if (!_patches.build_rawmaterial_ind &&
  1520 			indspec->accepts_cargo[0] == CT_INVALID &&
  1481 			indspec->accepts_cargo[0] == CT_INVALID &&
  1521 			indspec->accepts_cargo[1] == CT_INVALID &&
  1482 			indspec->accepts_cargo[1] == CT_INVALID &&
  1522 			indspec->accepts_cargo[2] == CT_INVALID &&
  1483 			indspec->accepts_cargo[2] == CT_INVALID &&
  1523 			p1 != IT_LUMBER_MILL) {
  1484 			!(indspec->behaviour & INDUSTRYBEH_CUT_TREES)) {
  1524 		return CMD_ERROR;
  1485 		return CMD_ERROR;
  1525 	}
  1486 	}
  1526 
  1487 
  1527 	num = indspec->num_table;
  1488 	num = indspec->num_table;
  1528 	itt = indspec->table;
  1489 	itt = indspec->table;
  1543 	const IndustryTileTable *it = indspec->table[RandomRange(indspec->num_table)];
  1504 	const IndustryTileTable *it = indspec->table[RandomRange(indspec->num_table)];
  1544 
  1505 
  1545 	return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, it);
  1506 	return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, it);
  1546 }
  1507 }
  1547 
  1508 
  1548 static const byte _numof_industry_table[4][12] = {
  1509 static const byte _numof_industry_table[5][12] = {
  1549 	/* difficulty settings for number of industries */
  1510 	/* difficulty settings for number of industries */
  1550 	{0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   //none
  1511 	{0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   //none
       
  1512 	{0, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1},   //very low
  1551 	{0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   //low
  1513 	{0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   //low
  1552 	{0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   //normal
  1514 	{0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   //normal
  1553 	{0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   //high
  1515 	{0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   //high
  1554 };
  1516 };
  1555 
  1517 
       
  1518 /** This function is the one who really do the creation work
       
  1519  * of random industries during game creation
       
  1520  * @param type IndustryType of the desired industry
       
  1521  * @param amount of industries that need to be built */
  1556 static void PlaceInitialIndustry(IndustryType type, int amount)
  1522 static void PlaceInitialIndustry(IndustryType type, int amount)
  1557 {
  1523 {
  1558 	int num = _numof_industry_table[_opt.diff.number_industries][amount];
  1524 	int num = _numof_industry_table[_opt.diff.number_industries][amount];
  1559 
  1525 	const IndustrySpec *ind_spc = GetIndustrySpec(type);
  1560 	if (type == IT_OIL_REFINERY || type == IT_OIL_RIG) {
  1526 
  1561 		/* These are always placed next to the coastline, so we scale by the perimeter instead. */
  1527 	/* These are always placed next to the coastline, so we scale by the perimeter instead. */
  1562 		num = ScaleByMapSize1D(num);
  1528 	num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
  1563 	} else {
       
  1564 		num = ScaleByMapSize(num);
       
  1565 	}
       
  1566 
  1529 
  1567 	if (_opt.diff.number_industries != 0) {
  1530 	if (_opt.diff.number_industries != 0) {
  1568 		PlayerID old_player = _current_player;
  1531 		PlayerID old_player = _current_player;
  1569 		_current_player = OWNER_NONE;
  1532 		_current_player = OWNER_NONE;
  1570 		assert(num > 0);
  1533 		assert(num > 0);
  1581 
  1544 
  1582 		_current_player = old_player;
  1545 		_current_player = old_player;
  1583 	}
  1546 	}
  1584 }
  1547 }
  1585 
  1548 
       
  1549 /** This function will create ramdon industries during game creation.
       
  1550  * It will scale the amount of industries by map size as well as difficulty level */
  1586 void GenerateIndustries()
  1551 void GenerateIndustries()
  1587 {
  1552 {
  1588 	const byte *b;
       
  1589 	uint i = 0;
  1553 	uint i = 0;
       
  1554 	uint8 chance;
       
  1555 	IndustryType it;
       
  1556 	const IndustrySpec *ind_spc;
  1590 
  1557 
  1591 	/* Find the total amount of industries */
  1558 	/* Find the total amount of industries */
  1592 	b = _industry_create_table[_opt.landscape];
  1559 	for (it = IT_COAL_MINE; it < NUM_INDUSTRYTYPES; it++) {
  1593 	do {
  1560 
  1594 		int num = _numof_industry_table[_opt.diff.number_industries][b[0]];
  1561 		ind_spc = GetIndustrySpec(it);
  1595 
  1562 		if (ind_spc->enabled) {
  1596 		if (b[1] == IT_OIL_REFINERY || b[1] == IT_OIL_RIG) {
  1563 			chance = ind_spc->appear_creation[_opt.landscape];
  1597 			/* These are always placed next to the coastline, so we scale by the perimeter instead. */
  1564 			if (chance > 0) {
  1598 			num = ScaleByMapSize1D(num);
  1565 				/* once the chance of appearance is determind, it have to be scaled by
  1599 		} else {
  1566 				 * the difficulty level. The "chance" in question is more an index into
  1600 			num = ScaleByMapSize(num);
  1567 				 * the _numof_industry_table,in fact */
  1601 		}
  1568 				int num = _numof_industry_table[_opt.diff.number_industries][chance];
  1602 
  1569 
  1603 		i += num;
  1570 				/* These are always placed next to the coastline, so we scale by the perimeter instead. */
  1604 	} while ( (b+=2)[0] != 0);
  1571 				num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
       
  1572 				i += num;
       
  1573 			}
       
  1574 		}
       
  1575 	}
       
  1576 
  1605 	SetGeneratingWorldProgress(GWP_INDUSTRY, i);
  1577 	SetGeneratingWorldProgress(GWP_INDUSTRY, i);
  1606 
  1578 
  1607 	b = _industry_create_table[_opt.landscape];
  1579 	for (it = IT_COAL_MINE; it < NUM_INDUSTRYTYPES; it++) {
  1608 	do {
  1580 		/* Once the number of industries has been determined, let's really create them.
  1609 		PlaceInitialIndustry(b[1], b[0]);
  1581 		 * The test for chance allows us to try create industries that are available only
  1610 	} while ( (b+=2)[0] != 0);
  1582 		 * for this landscape.
       
  1583 		 * @todo :  Do we really have to pass chance as un-scaled value, since we've already
       
  1584 		 *          processed that scaling above? No, don't think so.  Will find a way. */
       
  1585 		ind_spc = GetIndustrySpec(it);
       
  1586 		if (ind_spc->enabled) {
       
  1587 			chance = ind_spc->appear_creation[_opt.landscape];
       
  1588 			if (chance > 0) PlaceInitialIndustry(it, chance);
       
  1589 		}
       
  1590 	};
  1611 }
  1591 }
  1612 
  1592 
  1613 /* Change industry production or do closure */
  1593 /* Change industry production or do closure */
  1614 static void ExtChangeIndustryProduction(Industry *i)
  1594 static void ExtChangeIndustryProduction(Industry *i)
  1615 {
  1595 {
  1625 			if ((byte)(_cur_year - i->last_prod_year) < 5 || !CHANCE16(1, 180))
  1605 			if ((byte)(_cur_year - i->last_prod_year) < 5 || !CHANCE16(1, 180))
  1626 				closeit = false;
  1606 				closeit = false;
  1627 			break;
  1607 			break;
  1628 
  1608 
  1629 		default: /* INDUSTRY_PRODUCTION */
  1609 		default: /* INDUSTRY_PRODUCTION */
  1630 			for (j = 0; j < 2 && i->produced_cargo[j] != CT_INVALID; j++){
  1610 			for (j = 0; j < 2 && indspec->produced_cargo[j] != CT_INVALID; j++){
  1631 				uint32 r = Random();
  1611 				uint32 r = Random();
  1632 				int old_prod, new_prod, percent;
  1612 				int old_prod, new_prod, percent;
  1633 				int mag;
  1613 				int mag;
  1634 
  1614 
  1635 				new_prod = old_prod = i->production_rate[j];
  1615 				new_prod = old_prod = i->production_rate[j];
  1651 					closeit = false;
  1631 					closeit = false;
  1652 
  1632 
  1653 				mag = abs(percent);
  1633 				mag = abs(percent);
  1654 				if (mag >= 10) {
  1634 				if (mag >= 10) {
  1655 					SetDParam(2, mag);
  1635 					SetDParam(2, mag);
  1656 					SetDParam(0, GetCargo(i->produced_cargo[j])->name);
  1636 					SetDParam(0, GetCargo(indspec->produced_cargo[j])->name);
  1657 					SetDParam(1, i->index);
  1637 					SetDParam(1, i->index);
  1658 					AddNewsItem(
  1638 					AddNewsItem(
  1659 						percent >= 0 ? STR_INDUSTRY_PROD_GOUP : STR_INDUSTRY_PROD_GODOWN,
  1639 						percent >= 0 ? STR_INDUSTRY_PROD_GOUP : STR_INDUSTRY_PROD_GODOWN,
  1660 						NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_ECONOMY, 0),
  1640 						NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_TILE, NT_ECONOMY, 0),
  1661 						i->xy + TileDiffXY(1, 1), 0
  1641 						i->xy + TileDiffXY(1, 1), 0
  1662 					);
  1642 					);
  1663 				}
  1643 				}
  1664 			}
  1644 			}
  1665 			break;
  1645 			break;
  1669 	if (closeit) {
  1649 	if (closeit) {
  1670 		i->prod_level = 0;
  1650 		i->prod_level = 0;
  1671 		SetDParam(0, i->index);
  1651 		SetDParam(0, i->index);
  1672 		AddNewsItem(
  1652 		AddNewsItem(
  1673 			indspec->closure_text,
  1653 			indspec->closure_text,
  1674 			NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_OPENCLOSE, 0),
  1654 			NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_TILE, NT_OPENCLOSE, 0),
  1675 			i->xy + TileDiffXY(1, 1), 0
  1655 			i->xy + TileDiffXY(1, 1), 0
  1676 		);
  1656 		);
  1677 	}
  1657 	}
  1678 }
  1658 }
  1679 
  1659 
  1680 
  1660 
  1681 static void UpdateIndustryStatistics(Industry *i)
  1661 static void UpdateIndustryStatistics(Industry *i)
  1682 {
  1662 {
  1683 	byte pct;
  1663 	byte pct;
  1684 
  1664 	bool refresh = false;
  1685 	if (i->produced_cargo[0] != CT_INVALID) {
  1665 	const IndustrySpec *indsp = GetIndustrySpec(i->type);
  1686 		pct = 0;
  1666 
  1687 		if (i->last_mo_production[0] != 0) {
  1667 	for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
  1688 			i->last_prod_year = _cur_year;
  1668 		if (indsp->produced_cargo[j] != CT_INVALID) {
  1689 			pct = min(i->last_mo_transported[0] * 256 / i->last_mo_production[0],255);
  1669 			pct = 0;
  1690 		}
  1670 			if (i->last_mo_production[j] != 0) {
  1691 		i->pct_transported[0] = pct;
  1671 				i->last_prod_year = _cur_year;
  1692 
  1672 				pct = min(i->last_mo_transported[j] * 256 / i->last_mo_production[j], 255);
  1693 		i->total_production[0] = i->last_mo_production[0];
  1673 			}
  1694 		i->last_mo_production[0] = 0;
  1674 			i->pct_transported[j] = pct;
  1695 
  1675 
  1696 		i->total_transported[0] = i->last_mo_transported[0];
  1676 			i->total_production[j] = i->last_mo_production[j];
  1697 		i->last_mo_transported[0] = 0;
  1677 			i->last_mo_production[j] = 0;
  1698 	}
  1678 
  1699 
  1679 			i->total_transported[j] = i->last_mo_transported[j];
  1700 	if (i->produced_cargo[1] != CT_INVALID) {
  1680 			i->last_mo_transported[j] = 0;
  1701 		pct = 0;
  1681 			refresh = true;
  1702 		if (i->last_mo_production[1] != 0) {
  1682 		}
  1703 			i->last_prod_year = _cur_year;
  1683 	}
  1704 			pct = min(i->last_mo_transported[1] * 256 / i->last_mo_production[1],255);
  1684 
  1705 		}
  1685 	if (refresh)
  1706 		i->pct_transported[1] = pct;
       
  1707 
       
  1708 		i->total_production[1] = i->last_mo_production[1];
       
  1709 		i->last_mo_production[1] = 0;
       
  1710 
       
  1711 		i->total_transported[1] = i->last_mo_transported[1];
       
  1712 		i->last_mo_transported[1] = 0;
       
  1713 	}
       
  1714 
       
  1715 
       
  1716 	if (i->produced_cargo[0] != CT_INVALID || i->produced_cargo[1] != CT_INVALID)
       
  1717 		InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
  1686 		InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
  1718 
  1687 
  1719 	if (i->prod_level == 0) {
  1688 	if (i->prod_level == 0) {
  1720 		DeleteIndustry(i);
  1689 		DeleteIndustry(i);
  1721 	} else if (_patches.smooth_economy) {
  1690 	} else if (_patches.smooth_economy) {
  1722 		ExtChangeIndustryProduction(i);
  1691 		ExtChangeIndustryProduction(i);
  1723 	}
  1692 	}
  1724 }
  1693 }
  1725 
  1694 
  1726 static const byte _new_industry_rand[4][32] = {
  1695 /** Simple helper that will collect data for the generation of industries */
  1727 	{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},
  1696 struct ProbabilityHelper {
  1728 	{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},
  1697 	uint16 prob;      ///< probability
  1729 	{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},
  1698 	IndustryType ind; ///< industry id correcponding
  1730 	{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},
       
  1731 };
  1699 };
  1732 
  1700 
  1733 static void MaybeNewIndustry(uint32 r)
  1701 /**
  1734 {
  1702  * Try to create a random industry, during gameplay
  1735 	int type =_new_industry_rand[_opt.landscape][GB(r, 16, 5)];
  1703  */
  1736 	int j;
  1704 static void MaybeNewIndustry(void)
  1737 	Industry *i;
  1705 {
  1738 	const IndustrySpec *ind_spc = GetIndustrySpec(type);;
  1706 	Industry *ind;               //will receive the industry's creation pointer
  1739 
  1707 	IndustryType rndtype, j;     // Loop controlers
  1740 	if (type == IT_OIL_WELL && _cur_year > 1950) return;
  1708 	const IndustrySpec *ind_spc;
  1741 	if (type == IT_OIL_RIG  && _cur_year < 1960) return;
  1709 	uint num = 0;
  1742 
  1710 	ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES]; // probability collector
  1743 	j = 2000;
  1711 	uint16 probability_max = 0;
       
  1712 
       
  1713 	/* Generate a list of all possible industries that can be built. */
       
  1714 	for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
       
  1715 		byte chance = GetIndustrySpec(j)->appear_ingame[_opt.landscape];
       
  1716 
       
  1717 		/* if appearing chance for this landscape is above 0, this industry can be chosen */
       
  1718 		if (chance != 0) {
       
  1719 			probability_max += chance;
       
  1720 			/* adds the result for this industry */
       
  1721 			cumulative_probs[num].ind = j;
       
  1722 			cumulative_probs[num++].prob = probability_max;
       
  1723 		}
       
  1724 	}
       
  1725 
       
  1726 	/* Find a random type, with maximum being what has been evaluate above*/
       
  1727 	rndtype = RandomRange(probability_max);
       
  1728 	for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
       
  1729 		/* and choose the index of the industry that matches as close as possible this random type */
       
  1730 		if (cumulative_probs[j].prob >= rndtype) break;
       
  1731 	}
       
  1732 
       
  1733 	ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
       
  1734 	/*  Check if it is allowed */
       
  1735 	if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
       
  1736 	if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
       
  1737 
       
  1738 	/* try to create 2000 times this industry */
       
  1739 	num = 2000;
  1744 	for (;;) {
  1740 	for (;;) {
  1745 		i = CreateNewIndustry(RandomTile(), type);
  1741 		ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
  1746 		if (i != NULL) break;
  1742 		if (ind != NULL) break;
  1747 		if (--j == 0) return;
  1743 		if (--num == 0) return;
  1748 	}
  1744 	}
  1749 
  1745 
  1750 	SetDParam(0, ind_spc->name);
  1746 	SetDParam(0, ind_spc->name);
  1751 	SetDParam(1, i->town->index);
  1747 	SetDParam(1, ind->town->index);
  1752 	AddNewsItem(ind_spc->new_industry_text,
  1748 	AddNewsItem(ind_spc->new_industry_text,
  1753 		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, NT_OPENCLOSE, 0), i->xy, 0);
  1749 		NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_TILE, NT_OPENCLOSE, 0), ind->xy, 0);
  1754 }
  1750 }
  1755 
  1751 
  1756 static void ChangeIndustryProduction(Industry *i)
  1752 static void ChangeIndustryProduction(Industry *i)
  1757 {
  1753 {
  1758 	bool only_decrease = false;
  1754 	bool only_decrease = false;
  1764 		case INDUSTRYLIFE_NOT_CLOSABLE:
  1760 		case INDUSTRYLIFE_NOT_CLOSABLE:
  1765 			return;
  1761 			return;
  1766 
  1762 
  1767 		case INDUSTRYLIFE_PRODUCTION:
  1763 		case INDUSTRYLIFE_PRODUCTION:
  1768 			/* decrease or increase */
  1764 			/* decrease or increase */
  1769 			if (type == IT_OIL_WELL && _opt.landscape == LT_TEMPERATE)
  1765 			if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _opt.landscape == LT_TEMPERATE)
  1770 				only_decrease = true;
  1766 				only_decrease = true;
  1771 
  1767 
  1772 			if (only_decrease || CHANCE16(1,3)) {
  1768 			if (only_decrease || CHANCE16(1, 3)) {
  1773 				/* If you transport > 60%, 66% chance we increase, else 33% chance we increase */
  1769 				/* If you transport > 60%, 66% chance we increase, else 33% chance we increase */
  1774 				if (!only_decrease && (i->pct_transported[0] > 153) != CHANCE16(1,3)) {
  1770 				if (!only_decrease && (i->pct_transported[0] > 153) != CHANCE16(1, 3)) {
  1775 					/* Increase production */
  1771 					/* Increase production */
  1776 					if (i->prod_level != 0x80) {
  1772 					if (i->prod_level != 0x80) {
  1777 						byte b;
  1773 						byte b;
  1778 
  1774 
  1779 						i->prod_level <<= 1;
  1775 						i->prod_level <<= 1;
  1806 			}
  1802 			}
  1807 			break;
  1803 			break;
  1808 
  1804 
  1809 		case INDUSTRYLIFE_CLOSABLE:
  1805 		case INDUSTRYLIFE_CLOSABLE:
  1810 			/* maybe close */
  1806 			/* maybe close */
  1811 			if ( (byte)(_cur_year - i->last_prod_year) >= 5 && CHANCE16(1,2)) {
  1807 			if ( (byte)(_cur_year - i->last_prod_year) >= 5 && CHANCE16(1, 2)) {
  1812 				i->prod_level = 0;
  1808 				i->prod_level = 0;
  1813 				str = indspec->closure_text;
  1809 				str = indspec->closure_text;
  1814 			}
  1810 			}
  1815 			break;
  1811 			break;
  1816 	}
  1812 	}
  1817 
  1813 
  1818 	if (str != STR_NULL) {
  1814 	if (str != STR_NULL) {
  1819 		SetDParam(0, i->index);
  1815 		SetDParam(0, i->index);
  1820 		AddNewsItem(str, NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_TILE, str == indspec->closure_text ? NT_OPENCLOSE : NT_ECONOMY, 0), i->xy + TileDiffXY(1, 1), 0);
  1816 		AddNewsItem(str, NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_TILE, str == indspec->closure_text ? NT_OPENCLOSE : NT_ECONOMY, 0), i->xy + TileDiffXY(1, 1), 0);
  1821 	}
  1817 	}
  1822 }
  1818 }
  1823 
  1819 
  1824 void IndustryMonthlyLoop()
  1820 void IndustryMonthlyLoop()
  1825 {
  1821 {
  1831 		UpdateIndustryStatistics(i);
  1827 		UpdateIndustryStatistics(i);
  1832 	}
  1828 	}
  1833 
  1829 
  1834 	/* 3% chance that we start a new industry */
  1830 	/* 3% chance that we start a new industry */
  1835 	if (CHANCE16(3, 100)) {
  1831 	if (CHANCE16(3, 100)) {
  1836 		MaybeNewIndustry(Random());
  1832 		MaybeNewIndustry();
  1837 	} else if (!_patches.smooth_economy) {
  1833 	} else if (!_patches.smooth_economy) {
  1838 		i = GetRandomIndustry();
  1834 		i = GetRandomIndustry();
  1839 		if (i != NULL) ChangeIndustryProduction(i);
  1835 		if (i != NULL) ChangeIndustryProduction(i);
  1840 	}
  1836 	}
  1841 
  1837 
  1877 	SLE_CONDVAR(Industry, xy,                  SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
  1873 	SLE_CONDVAR(Industry, xy,                  SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
  1878 	SLE_CONDVAR(Industry, xy,                  SLE_UINT32,                  6, SL_MAX_VERSION),
  1874 	SLE_CONDVAR(Industry, xy,                  SLE_UINT32,                  6, SL_MAX_VERSION),
  1879 	    SLE_VAR(Industry, width,               SLE_UINT8),
  1875 	    SLE_VAR(Industry, width,               SLE_UINT8),
  1880 	    SLE_VAR(Industry, height,              SLE_UINT8),
  1876 	    SLE_VAR(Industry, height,              SLE_UINT8),
  1881 	    SLE_REF(Industry, town,                REF_TOWN),
  1877 	    SLE_REF(Industry, town,                REF_TOWN),
  1882 	    SLE_ARR(Industry, produced_cargo,      SLE_UINT8,  2),
  1878 	SLE_CONDNULL( 2, 2, 60),       ///< used to be industry's produced_cargo
  1883 	    SLE_ARR(Industry, cargo_waiting,       SLE_UINT16, 2),
  1879 	    SLE_ARR(Industry, cargo_waiting,       SLE_UINT16, 2),
  1884 	    SLE_ARR(Industry, production_rate,     SLE_UINT8,  2),
  1880 	    SLE_ARR(Industry, production_rate,     SLE_UINT8,  2),
  1885 	    SLE_ARR(Industry, accepts_cargo,       SLE_UINT8,  3),
  1881 	SLE_CONDNULL( 3, 2, 60),       ///< used to be industry's accepts_cargo
  1886 	    SLE_VAR(Industry, prod_level,          SLE_UINT8),
  1882 	    SLE_VAR(Industry, prod_level,          SLE_UINT8),
  1887 	    SLE_ARR(Industry, last_mo_production,  SLE_UINT16, 2),
  1883 	    SLE_ARR(Industry, last_mo_production,  SLE_UINT16, 2),
  1888 	    SLE_ARR(Industry, last_mo_transported, SLE_UINT16, 2),
  1884 	    SLE_ARR(Industry, last_mo_transported, SLE_UINT16, 2),
  1889 	    SLE_ARR(Industry, pct_transported,     SLE_UINT8,  2),
  1885 	    SLE_ARR(Industry, pct_transported,     SLE_UINT8,  2),
  1890 	    SLE_ARR(Industry, total_production,    SLE_UINT16, 2),
  1886 	    SLE_ARR(Industry, total_production,    SLE_UINT16, 2),