1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 /** @file industry_cmd.cpp Handling of industry tiles. */
9
10 #include "stdafx.h"
11 #include "clear_map.h"
12 #include "industry.h"
13 #include "station_base.h"
14 #include "landscape.h"
15 #include "viewport_func.h"
16 #include "command_func.h"
17 #include "town.h"
18 #include "news_func.h"
19 #include "cheat_type.h"
20 #include "company_base.h"
21 #include "genworld.h"
22 #include "tree_map.h"
23 #include "newgrf_cargo.h"
24 #include "newgrf_debug.h"
25 #include "newgrf_industrytiles.h"
26 #include "autoslope.h"
27 #include "water.h"
28 #include "strings_func.h"
29 #include "window_func.h"
30 #include "date_func.h"
31 #include "vehicle_func.h"
32 #include "sound_func.h"
33 #include "animated_tile_func.h"
34 #include "effectvehicle_func.h"
35 #include "effectvehicle_base.h"
36 #include "ai/ai.hpp"
37 #include "core/pool_func.hpp"
38 #include "subsidy_func.h"
39 #include "core/backup_type.hpp"
40 #include "object_base.h"
41 #include "game/game.hpp"
42 #include "error.h"
43 #include "cmd_helper.h"
44 #include "string_func.h"
45
46 #include "table/strings.h"
47 #include "table/industry_land.h"
48 #include "table/build_industry.h"
49
50 #include "safeguards.h"
51
52 IndustryPool _industry_pool("Industry");
53 INSTANTIATE_POOL_METHODS(Industry)
54
55 void ShowIndustryViewWindow(int industry);
56 void BuildOilRig(TileIndex tile);
57
58 static byte _industry_sound_ctr;
59 static TileIndex _industry_sound_tile;
60
61 uint16 Industry::counts[NUM_INDUSTRYTYPES];
62
63 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
64 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
65 IndustryBuildData _industry_builder; ///< In-game manager of industries.
66
67 /**
68 * This function initialize the spec arrays of both
69 * industry and industry tiles.
70 * It adjusts the enabling of the industry too, based on climate availability.
71 * This will allow for clearer testings
72 */
ResetIndustries()73 void ResetIndustries()
74 {
75 for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
76 /* Reset the spec to default */
77 if (i < lengthof(_origin_industry_specs)) {
78 _industry_specs[i] = _origin_industry_specs[i];
79 } else {
80 _industry_specs[i] = IndustrySpec{};
81 }
82
83 /* Enable only the current climate industries */
84 _industry_specs[i].enabled = i < NEW_INDUSTRYOFFSET &&
85 HasBit(_origin_industry_specs[i].climate_availability, _settings_game.game_creation.landscape);
86 }
87
88 memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
89 memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
90
91 /* Reset any overrides that have been set. */
92 _industile_mngr.ResetOverride();
93 _industry_mngr.ResetOverride();
94 }
95
96 /**
97 * Retrieve the type for this industry. Although it is accessed by a tile,
98 * it will return the general type of industry, and not the sprite index
99 * as would do GetIndustryGfx.
100 * @param tile that is queried
101 * @pre IsTileType(tile, MP_INDUSTRY)
102 * @return general type for this industry, as defined in industry.h
103 */
GetIndustryType(TileIndex tile)104 IndustryType GetIndustryType(TileIndex tile)
105 {
106 assert(IsTileType(tile, MP_INDUSTRY));
107
108 const Industry *ind = Industry::GetByTile(tile);
109 assert(ind != nullptr);
110 return ind->type;
111 }
112
113 /**
114 * Accessor for array _industry_specs.
115 * This will ensure at once : proper access and
116 * not allowing modifications of it.
117 * @param thistype of industry (which is the index in _industry_specs)
118 * @pre thistype < NUM_INDUSTRYTYPES
119 * @return a pointer to the corresponding industry spec
120 */
GetIndustrySpec(IndustryType thistype)121 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
122 {
123 assert(thistype < NUM_INDUSTRYTYPES);
124 return &_industry_specs[thistype];
125 }
126
127 /**
128 * Accessor for array _industry_tile_specs.
129 * This will ensure at once : proper access and
130 * not allowing modifications of it.
131 * @param gfx of industrytile (which is the index in _industry_tile_specs)
132 * @pre gfx < INVALID_INDUSTRYTILE
133 * @return a pointer to the corresponding industrytile spec
134 */
GetIndustryTileSpec(IndustryGfx gfx)135 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
136 {
137 assert(gfx < INVALID_INDUSTRYTILE);
138 return &_industry_tile_specs[gfx];
139 }
140
~Industry()141 Industry::~Industry()
142 {
143 if (CleaningPool()) return;
144
145 /* Industry can also be destroyed when not fully initialized.
146 * This means that we do not have to clear tiles either.
147 * Also we must not decrement industry counts in that case. */
148 if (this->location.w == 0) return;
149
150 const bool has_neutral_station = this->neutral_station != nullptr;
151
152 for (TileIndex tile_cur : this->location) {
153 if (IsTileType(tile_cur, MP_INDUSTRY)) {
154 if (GetIndustryIndex(tile_cur) == this->index) {
155 DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, tile_cur);
156
157 /* MakeWaterKeepingClass() can also handle 'land' */
158 MakeWaterKeepingClass(tile_cur, OWNER_NONE);
159 }
160 } else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
161 DeleteOilRig(tile_cur);
162 }
163 }
164
165 if (has_neutral_station) {
166 /* Remove possible docking tiles */
167 for (TileIndex tile_cur : this->location) {
168 ClearDockingTilesCheckingNeighbours(tile_cur);
169 }
170 }
171
172 if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
173 TileArea ta = TileArea(this->location.tile, 0, 0).Expand(21);
174
175 /* Remove the farmland and convert it to regular tiles over time. */
176 for (TileIndex tile_cur : ta) {
177 if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
178 GetIndustryIndexOfField(tile_cur) == this->index) {
179 SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
180 }
181 }
182 }
183
184 /* don't let any disaster vehicle target invalid industry */
185 ReleaseDisastersTargetingIndustry(this->index);
186
187 /* Clear the persistent storage. */
188 delete this->psa;
189
190 DecIndustryTypeCount(this->type);
191
192 DeleteIndustryNews(this->index);
193 CloseWindowById(WC_INDUSTRY_VIEW, this->index);
194 DeleteNewGRFInspectWindow(GSF_INDUSTRIES, this->index);
195
196 DeleteSubsidyWith(ST_INDUSTRY, this->index);
197 CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
198
199 for (Station *st : this->stations_near) {
200 st->industries_near.erase(this);
201 }
202 }
203
204 /**
205 * Invalidating some stuff after removing item from the pool.
206 * @param index index of deleted item
207 */
PostDestructor(size_t index)208 void Industry::PostDestructor(size_t index)
209 {
210 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_FORCE_REBUILD);
211 }
212
213
214 /**
215 * Return a random valid industry.
216 * @return random industry, nullptr if there are no industries
217 */
GetRandom()218 /* static */ Industry *Industry::GetRandom()
219 {
220 if (Industry::GetNumItems() == 0) return nullptr;
221 int num = RandomRange((uint16)Industry::GetNumItems());
222 size_t index = MAX_UVALUE(size_t);
223
224 while (num >= 0) {
225 num--;
226 index++;
227
228 /* Make sure we have a valid industry */
229 while (!Industry::IsValidID(index)) {
230 index++;
231 assert(index < Industry::GetPoolSize());
232 }
233 }
234
235 return Industry::Get(index);
236 }
237
238
IndustryDrawSugarMine(const TileInfo * ti)239 static void IndustryDrawSugarMine(const TileInfo *ti)
240 {
241 if (!IsIndustryCompleted(ti->tile)) return;
242
243 const DrawIndustryAnimationStruct *d = &_draw_industry_spec1[GetAnimationFrame(ti->tile)];
244
245 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
246
247 if (d->image_2 != 0) {
248 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
249 }
250
251 if (d->image_3 != 0) {
252 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
253 _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
254 }
255 }
256
IndustryDrawToffeeQuarry(const TileInfo * ti)257 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
258 {
259 uint8 x = 0;
260
261 if (IsIndustryCompleted(ti->tile)) {
262 x = _industry_anim_offs_toffee[GetAnimationFrame(ti->tile)];
263 if (x == 0xFF) {
264 x = 0;
265 }
266 }
267
268 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
269 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
270 }
271
IndustryDrawBubbleGenerator(const TileInfo * ti)272 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
273 {
274 if (IsIndustryCompleted(ti->tile)) {
275 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetAnimationFrame(ti->tile)]);
276 }
277 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
278 }
279
IndustryDrawToyFactory(const TileInfo * ti)280 static void IndustryDrawToyFactory(const TileInfo *ti)
281 {
282 const DrawIndustryAnimationStruct *d = &_industry_anim_offs_toys[GetAnimationFrame(ti->tile)];
283
284 if (d->image_1 != 0xFF) {
285 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
286 }
287
288 if (d->image_2 != 0xFF) {
289 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
290 }
291
292 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
293 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
294 }
295
IndustryDrawCoalPlantSparks(const TileInfo * ti)296 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
297 {
298 if (IsIndustryCompleted(ti->tile)) {
299 uint8 image = GetAnimationFrame(ti->tile);
300
301 if (image != 0 && image < 7) {
302 AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
303 PAL_NONE,
304 _coal_plant_sparks[image - 1].x,
305 _coal_plant_sparks[image - 1].y
306 );
307 }
308 }
309 }
310
311 typedef void IndustryDrawTileProc(const TileInfo *ti);
312 static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
313 IndustryDrawSugarMine,
314 IndustryDrawToffeeQuarry,
315 IndustryDrawBubbleGenerator,
316 IndustryDrawToyFactory,
317 IndustryDrawCoalPlantSparks,
318 };
319
DrawTile_Industry(TileInfo * ti)320 static void DrawTile_Industry(TileInfo *ti)
321 {
322 IndustryGfx gfx = GetIndustryGfx(ti->tile);
323 Industry *ind = Industry::GetByTile(ti->tile);
324 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
325
326 /* Retrieve pointer to the draw industry tile struct */
327 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
328 /* Draw the tile using the specialized method of newgrf industrytile.
329 * DrawNewIndustry will return false if ever the resolver could not
330 * find any sprite to display. So in this case, we will jump on the
331 * substitute gfx instead. */
332 if (indts->grf_prop.spritegroup[0] != nullptr && DrawNewIndustryTile(ti, ind, gfx, indts)) {
333 return;
334 } else {
335 /* No sprite group (or no valid one) found, meaning no graphics associated.
336 * Use the substitute one instead */
337 if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
338 gfx = indts->grf_prop.subst_id;
339 /* And point the industrytile spec accordingly */
340 indts = GetIndustryTileSpec(gfx);
341 }
342 }
343 }
344
345 const DrawBuildingsTileStruct *dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
346 GetAnimationFrame(ti->tile) & INDUSTRY_COMPLETED :
347 GetIndustryConstructionStage(ti->tile))];
348
349 SpriteID image = dits->ground.sprite;
350
351 /* DrawFoundation() modifies ti->z and ti->tileh */
352 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
353
354 /* If the ground sprite is the default flat water sprite, draw also canal/river borders.
355 * Do not do this if the tile's WaterClass is 'land'. */
356 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
357 DrawWaterClassGround(ti);
358 } else {
359 DrawGroundSprite(image, GroundSpritePaletteTransform(image, dits->ground.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)));
360 }
361
362 /* If industries are transparent and invisible, do not draw the upper part */
363 if (IsInvisibilitySet(TO_INDUSTRIES)) return;
364
365 /* Add industry on top of the ground? */
366 image = dits->building.sprite;
367 if (image != 0) {
368 AddSortableSpriteToDraw(image, SpriteLayoutPaletteTransform(image, dits->building.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)),
369 ti->x + dits->subtile_x,
370 ti->y + dits->subtile_y,
371 dits->width,
372 dits->height,
373 dits->dz,
374 ti->z,
375 IsTransparencySet(TO_INDUSTRIES));
376
377 if (IsTransparencySet(TO_INDUSTRIES)) return;
378 }
379
380 {
381 int proc = dits->draw_proc - 1;
382 if (proc >= 0) _industry_draw_tile_procs[proc](ti);
383 }
384 }
385
GetSlopePixelZ_Industry(TileIndex tile,uint x,uint y)386 static int GetSlopePixelZ_Industry(TileIndex tile, uint x, uint y)
387 {
388 return GetTileMaxPixelZ(tile);
389 }
390
GetFoundation_Industry(TileIndex tile,Slope tileh)391 static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
392 {
393 IndustryGfx gfx = GetIndustryGfx(tile);
394
395 /* For NewGRF industry tiles we might not be drawing a foundation. We need to
396 * account for this, as other structures should
397 * draw the wall of the foundation in this case.
398 */
399 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
400 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
401 if (indts->grf_prop.spritegroup[0] != nullptr && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
402 uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
403 if (callback_res != CALLBACK_FAILED && !ConvertBooleanCallback(indts->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res)) return FOUNDATION_NONE;
404 }
405 }
406 return FlatteningFoundation(tileh);
407 }
408
AddAcceptedCargo_Industry(TileIndex tile,CargoArray & acceptance,CargoTypes * always_accepted)409 static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted)
410 {
411 IndustryGfx gfx = GetIndustryGfx(tile);
412 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
413 const Industry *ind = Industry::GetByTile(tile);
414
415 /* Starting point for acceptance */
416 CargoID accepts_cargo[lengthof(itspec->accepts_cargo)];
417 int8 cargo_acceptance[lengthof(itspec->acceptance)];
418 MemCpyT(accepts_cargo, itspec->accepts_cargo, lengthof(accepts_cargo));
419 MemCpyT(cargo_acceptance, itspec->acceptance, lengthof(cargo_acceptance));
420
421 if (itspec->special_flags & INDTILE_SPECIAL_ACCEPTS_ALL_CARGO) {
422 /* Copy all accepted cargoes from industry itself */
423 for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
424 CargoID *pos = std::find(accepts_cargo, endof(accepts_cargo), ind->accepts_cargo[i]);
425 if (pos == endof(accepts_cargo)) {
426 /* Not found, insert */
427 pos = std::find(accepts_cargo, endof(accepts_cargo), CT_INVALID);
428 if (pos == endof(accepts_cargo)) continue; // nowhere to place, give up on this one
429 *pos = ind->accepts_cargo[i];
430 }
431 cargo_acceptance[pos - accepts_cargo] += 8;
432 }
433 }
434
435 if (HasBit(itspec->callback_mask, CBM_INDT_ACCEPT_CARGO)) {
436 /* Try callback for accepts list, if success override all existing accepts */
437 uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
438 if (res != CALLBACK_FAILED) {
439 MemSetT(accepts_cargo, CT_INVALID, lengthof(accepts_cargo));
440 for (uint i = 0; i < 3; i++) accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
441 }
442 }
443
444 if (HasBit(itspec->callback_mask, CBM_INDT_CARGO_ACCEPTANCE)) {
445 /* Try callback for acceptance list, if success override all existing acceptance */
446 uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
447 if (res != CALLBACK_FAILED) {
448 MemSetT(cargo_acceptance, 0, lengthof(cargo_acceptance));
449 for (uint i = 0; i < 3; i++) cargo_acceptance[i] = GB(res, i * 4, 4);
450 }
451 }
452
453 for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
454 CargoID a = accepts_cargo[i];
455 if (a == CT_INVALID || cargo_acceptance[i] <= 0) continue; // work only with valid cargoes
456
457 /* Add accepted cargo */
458 acceptance[a] += cargo_acceptance[i];
459
460 /* Maybe set 'always accepted' bit (if it's not set already) */
461 if (HasBit(*always_accepted, a)) continue;
462
463 bool accepts = false;
464 for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
465 /* Test whether the industry itself accepts the cargo type */
466 if (ind->accepts_cargo[cargo_index] == a) {
467 accepts = true;
468 break;
469 }
470 }
471
472 if (accepts) continue;
473
474 /* If the industry itself doesn't accept this cargo, set 'always accepted' bit */
475 SetBit(*always_accepted, a);
476 }
477 }
478
GetTileDesc_Industry(TileIndex tile,TileDesc * td)479 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
480 {
481 const Industry *i = Industry::GetByTile(tile);
482 const IndustrySpec *is = GetIndustrySpec(i->type);
483
484 td->owner[0] = i->owner;
485 td->str = is->name;
486 if (!IsIndustryCompleted(tile)) {
487 SetDParamX(td->dparam, 0, td->str);
488 td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
489 }
490
491 if (is->grf_prop.grffile != nullptr) {
492 td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->GetName();
493 }
494 }
495
ClearTile_Industry(TileIndex tile,DoCommandFlag flags)496 static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
497 {
498 Industry *i = Industry::GetByTile(tile);
499 const IndustrySpec *indspec = GetIndustrySpec(i->type);
500
501 /* water can destroy industries
502 * in editor you can bulldoze industries
503 * with magic_bulldozer cheat you can destroy industries
504 * (area around OILRIG is water, so water shouldn't flood it
505 */
506 if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
507 !_cheats.magic_bulldozer.value) ||
508 ((flags & DC_AUTO) != 0) ||
509 (_current_company == OWNER_WATER &&
510 ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
511 HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
512 SetDParam(1, indspec->name);
513 return_cmd_error(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
514 }
515
516 if (flags & DC_EXEC) {
517 AI::BroadcastNewEvent(new ScriptEventIndustryClose(i->index));
518 Game::NewEvent(new ScriptEventIndustryClose(i->index));
519 delete i;
520 }
521 return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
522 }
523
524 /**
525 * Move produced cargo from industry to nearby stations.
526 * @param tile Industry tile
527 * @return true if any cargo was moved.
528 */
TransportIndustryGoods(TileIndex tile)529 static bool TransportIndustryGoods(TileIndex tile)
530 {
531 Industry *i = Industry::GetByTile(tile);
532 const IndustrySpec *indspec = GetIndustrySpec(i->type);
533 bool moved_cargo = false;
534
535 for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
536 uint cw = std::min<uint>(i->produced_cargo_waiting[j], 255u);
537 if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
538 i->produced_cargo_waiting[j] -= cw;
539
540 /* fluctuating economy? */
541 if (EconomyIsInRecession()) cw = (cw + 1) / 2;
542
543 i->this_month_production[j] += cw;
544
545 uint am = MoveGoodsToStation(i->produced_cargo[j], cw, ST_INDUSTRY, i->index, &i->stations_near, i->exclusive_consumer);
546 i->this_month_transported[j] += am;
547
548 moved_cargo |= (am != 0);
549 }
550 }
551
552 return moved_cargo;
553 }
554
555
AnimateTile_Industry(TileIndex tile)556 static void AnimateTile_Industry(TileIndex tile)
557 {
558 IndustryGfx gfx = GetIndustryGfx(tile);
559
560 if (GetIndustryTileSpec(gfx)->animation.status != ANIM_STATUS_NO_ANIMATION) {
561 AnimateNewIndustryTile(tile);
562 return;
563 }
564
565 switch (gfx) {
566 case GFX_SUGAR_MINE_SIEVE:
567 if ((_tick_counter & 1) == 0) {
568 byte m = GetAnimationFrame(tile) + 1;
569
570 if (_settings_client.sound.ambient) {
571 switch (m & 7) {
572 case 2: SndPlayTileFx(SND_2D_SUGAR_MINE_1, tile); break;
573 case 6: SndPlayTileFx(SND_29_SUGAR_MINE_2, tile); break;
574 }
575 }
576
577 if (m >= 96) {
578 m = 0;
579 DeleteAnimatedTile(tile);
580 }
581 SetAnimationFrame(tile, m);
582
583 MarkTileDirtyByTile(tile);
584 }
585 break;
586
587 case GFX_TOFFEE_QUARY:
588 if ((_tick_counter & 3) == 0) {
589 byte m = GetAnimationFrame(tile);
590
591 if (_industry_anim_offs_toffee[m] == 0xFF && _settings_client.sound.ambient) {
592 SndPlayTileFx(SND_30_TOFFEE_QUARRY, tile);
593 }
594
595 if (++m >= 70) {
596 m = 0;
597 DeleteAnimatedTile(tile);
598 }
599 SetAnimationFrame(tile, m);
600
601 MarkTileDirtyByTile(tile);
602 }
603 break;
604
605 case GFX_BUBBLE_CATCHER:
606 if ((_tick_counter & 1) == 0) {
607 byte m = GetAnimationFrame(tile);
608
609 if (++m >= 40) {
610 m = 0;
611 DeleteAnimatedTile(tile);
612 }
613 SetAnimationFrame(tile, m);
614
615 MarkTileDirtyByTile(tile);
616 }
617 break;
618
619 /* Sparks on a coal plant */
620 case GFX_POWERPLANT_SPARKS:
621 if ((_tick_counter & 3) == 0) {
622 byte m = GetAnimationFrame(tile);
623 if (m == 6) {
624 SetAnimationFrame(tile, 0);
625 DeleteAnimatedTile(tile);
626 } else {
627 SetAnimationFrame(tile, m + 1);
628 MarkTileDirtyByTile(tile);
629 }
630 }
631 break;
632
633 case GFX_TOY_FACTORY:
634 if ((_tick_counter & 1) == 0) {
635 byte m = GetAnimationFrame(tile) + 1;
636
637 switch (m) {
638 case 1: if (_settings_client.sound.ambient) SndPlayTileFx(SND_2C_TOY_FACTORY_1, tile); break;
639 case 23: if (_settings_client.sound.ambient) SndPlayTileFx(SND_2B_TOY_FACTORY_2, tile); break;
640 case 28: if (_settings_client.sound.ambient) SndPlayTileFx(SND_2A_TOY_FACTORY_3, tile); break;
641 default:
642 if (m >= 50) {
643 int n = GetIndustryAnimationLoop(tile) + 1;
644 m = 0;
645 if (n >= 8) {
646 n = 0;
647 DeleteAnimatedTile(tile);
648 }
649 SetIndustryAnimationLoop(tile, n);
650 }
651 }
652
653 SetAnimationFrame(tile, m);
654 MarkTileDirtyByTile(tile);
655 }
656 break;
657
658 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
659 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
660 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
661 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
662 if ((_tick_counter & 3) == 0) {
663 IndustryGfx gfx = GetIndustryGfx(tile);
664
665 gfx = (gfx < 155) ? gfx + 1 : 148;
666 SetIndustryGfx(tile, gfx);
667 MarkTileDirtyByTile(tile);
668 }
669 break;
670
671 case GFX_OILWELL_ANIMATED_1:
672 case GFX_OILWELL_ANIMATED_2:
673 case GFX_OILWELL_ANIMATED_3:
674 if ((_tick_counter & 7) == 0) {
675 bool b = Chance16(1, 7);
676 IndustryGfx gfx = GetIndustryGfx(tile);
677
678 byte m = GetAnimationFrame(tile) + 1;
679 if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
680 SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
681 SetIndustryConstructionStage(tile, 3);
682 DeleteAnimatedTile(tile);
683 } else {
684 SetAnimationFrame(tile, m);
685 SetIndustryGfx(tile, gfx);
686 MarkTileDirtyByTile(tile);
687 }
688 }
689 break;
690
691 case GFX_COAL_MINE_TOWER_ANIMATED:
692 case GFX_COPPER_MINE_TOWER_ANIMATED:
693 case GFX_GOLD_MINE_TOWER_ANIMATED: {
694 int state = _tick_counter & 0x7FF;
695
696 if ((state -= 0x400) < 0) return;
697
698 if (state < 0x1A0) {
699 if (state < 0x20 || state >= 0x180) {
700 byte m = GetAnimationFrame(tile);
701 if (!(m & 0x40)) {
702 SetAnimationFrame(tile, m | 0x40);
703 if (_settings_client.sound.ambient) SndPlayTileFx(SND_0B_MINE, tile);
704 }
705 if (state & 7) return;
706 } else {
707 if (state & 3) return;
708 }
709 byte m = (GetAnimationFrame(tile) + 1) | 0x40;
710 if (m > 0xC2) m = 0xC0;
711 SetAnimationFrame(tile, m);
712 MarkTileDirtyByTile(tile);
713 } else if (state >= 0x200 && state < 0x3A0) {
714 int i = (state < 0x220 || state >= 0x380) ? 7 : 3;
715 if (state & i) return;
716
717 byte m = (GetAnimationFrame(tile) & 0xBF) - 1;
718 if (m < 0x80) m = 0x82;
719 SetAnimationFrame(tile, m);
720 MarkTileDirtyByTile(tile);
721 }
722 break;
723 }
724 }
725 }
726
CreateChimneySmoke(TileIndex tile)727 static void CreateChimneySmoke(TileIndex tile)
728 {
729 uint x = TileX(tile) * TILE_SIZE;
730 uint y = TileY(tile) * TILE_SIZE;
731 int z = GetTileMaxPixelZ(tile);
732
733 CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
734 }
735
MakeIndustryTileBigger(TileIndex tile)736 static void MakeIndustryTileBigger(TileIndex tile)
737 {
738 byte cnt = GetIndustryConstructionCounter(tile) + 1;
739 if (cnt != 4) {
740 SetIndustryConstructionCounter(tile, cnt);
741 return;
742 }
743
744 byte stage = GetIndustryConstructionStage(tile) + 1;
745 SetIndustryConstructionCounter(tile, 0);
746 SetIndustryConstructionStage(tile, stage);
747 StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
748 if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile);
749
750 MarkTileDirtyByTile(tile);
751
752 if (!IsIndustryCompleted(tile)) return;
753
754 IndustryGfx gfx = GetIndustryGfx(tile);
755 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
756 /* New industries are already animated on construction. */
757 return;
758 }
759
760 switch (gfx) {
761 case GFX_POWERPLANT_CHIMNEY:
762 CreateChimneySmoke(tile);
763 break;
764
765 case GFX_OILRIG_1: {
766 /* Do not require an industry tile to be after the first two GFX_OILRIG_1
767 * tiles (like the default oil rig). Do a proper check to ensure the
768 * tiles belong to the same industry and based on that build the oil rig's
769 * station. */
770 TileIndex other = tile + TileDiffXY(0, 1);
771
772 if (IsTileType(other, MP_INDUSTRY) &&
773 GetIndustryGfx(other) == GFX_OILRIG_1 &&
774 GetIndustryIndex(tile) == GetIndustryIndex(other)) {
775 BuildOilRig(tile);
776 }
777 break;
778 }
779
780 case GFX_TOY_FACTORY:
781 case GFX_BUBBLE_CATCHER:
782 case GFX_TOFFEE_QUARY:
783 SetAnimationFrame(tile, 0);
784 SetIndustryAnimationLoop(tile, 0);
785 break;
786
787 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
788 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
789 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
790 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
791 AddAnimatedTile(tile);
792 break;
793 }
794 }
795
TileLoopIndustry_BubbleGenerator(TileIndex tile)796 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
797 {
798 static const int8 _bubble_spawn_location[3][4] = {
799 { 11, 0, -4, -14 },
800 { -4, -10, -4, 1 },
801 { 49, 59, 60, 65 },
802 };
803
804 if (_settings_client.sound.ambient) SndPlayTileFx(SND_2E_BUBBLE_GENERATOR, tile);
805
806 int dir = Random() & 3;
807
808 EffectVehicle *v = CreateEffectVehicleAbove(
809 TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
810 TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
811 _bubble_spawn_location[2][dir],
812 EV_BUBBLE
813 );
814
815 if (v != nullptr) v->animation_substate = dir;
816 }
817
TileLoop_Industry(TileIndex tile)818 static void TileLoop_Industry(TileIndex tile)
819 {
820 if (IsTileOnWater(tile)) TileLoop_Water(tile);
821
822 /* Normally this doesn't happen, but if an industry NewGRF is removed
823 * an industry that was previously build on water can now be flooded.
824 * If this happens the tile is no longer an industry tile after
825 * returning from TileLoop_Water. */
826 if (!IsTileType(tile, MP_INDUSTRY)) return;
827
828 TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
829
830 if (!IsIndustryCompleted(tile)) {
831 MakeIndustryTileBigger(tile);
832 return;
833 }
834
835 if (_game_mode == GM_EDITOR) return;
836
837 if (TransportIndustryGoods(tile) && !StartStopIndustryTileAnimation(Industry::GetByTile(tile), IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
838 uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
839
840 if (newgfx != INDUSTRYTILE_NOANIM) {
841 ResetIndustryConstructionStage(tile);
842 SetIndustryCompleted(tile);
843 SetIndustryGfx(tile, newgfx);
844 MarkTileDirtyByTile(tile);
845 return;
846 }
847 }
848
849 if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
850
851 IndustryGfx newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
852 if (newgfx != INDUSTRYTILE_NOANIM) {
853 ResetIndustryConstructionStage(tile);
854 SetIndustryGfx(tile, newgfx);
855 MarkTileDirtyByTile(tile);
856 return;
857 }
858
859 IndustryGfx gfx = GetIndustryGfx(tile);
860 switch (gfx) {
861 case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
862 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
863 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
864 if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
865 switch (gfx) {
866 case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break;
867 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
868 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_ANIMATED; break;
869 }
870 SetIndustryGfx(tile, gfx);
871 SetAnimationFrame(tile, 0x80);
872 AddAnimatedTile(tile);
873 }
874 break;
875
876 case GFX_OILWELL_NOT_ANIMATED:
877 if (Chance16(1, 6)) {
878 SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
879 SetAnimationFrame(tile, 0);
880 AddAnimatedTile(tile);
881 }
882 break;
883
884 case GFX_COAL_MINE_TOWER_ANIMATED:
885 case GFX_COPPER_MINE_TOWER_ANIMATED:
886 case GFX_GOLD_MINE_TOWER_ANIMATED:
887 if (!(_tick_counter & 0x400)) {
888 switch (gfx) {
889 case GFX_COAL_MINE_TOWER_ANIMATED: gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED; break;
890 case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
891 case GFX_GOLD_MINE_TOWER_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED; break;
892 }
893 SetIndustryGfx(tile, gfx);
894 SetIndustryCompleted(tile);
895 SetIndustryConstructionStage(tile, 3);
896 DeleteAnimatedTile(tile);
897 }
898 break;
899
900 case GFX_POWERPLANT_SPARKS:
901 if (Chance16(1, 3)) {
902 if (_settings_client.sound.ambient) SndPlayTileFx(SND_0C_POWER_STATION, tile);
903 AddAnimatedTile(tile);
904 }
905 break;
906
907 case GFX_COPPER_MINE_CHIMNEY:
908 CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_COPPER_MINE_SMOKE);
909 break;
910
911
912 case GFX_TOY_FACTORY: {
913 Industry *i = Industry::GetByTile(tile);
914 if (i->was_cargo_delivered) {
915 i->was_cargo_delivered = false;
916 SetIndustryAnimationLoop(tile, 0);
917 AddAnimatedTile(tile);
918 }
919 }
920 break;
921
922 case GFX_BUBBLE_GENERATOR:
923 TileLoopIndustry_BubbleGenerator(tile);
924 break;
925
926 case GFX_TOFFEE_QUARY:
927 AddAnimatedTile(tile);
928 break;
929
930 case GFX_SUGAR_MINE_SIEVE:
931 if (Chance16(1, 3)) AddAnimatedTile(tile);
932 break;
933 }
934 }
935
ClickTile_Industry(TileIndex tile)936 static bool ClickTile_Industry(TileIndex tile)
937 {
938 ShowIndustryViewWindow(GetIndustryIndex(tile));
939 return true;
940 }
941
GetTileTrackStatus_Industry(TileIndex tile,TransportType mode,uint sub_mode,DiagDirection side)942 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
943 {
944 return 0;
945 }
946
ChangeTileOwner_Industry(TileIndex tile,Owner old_owner,Owner new_owner)947 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
948 {
949 /* If the founder merges, the industry was created by the merged company */
950 Industry *i = Industry::GetByTile(tile);
951 if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
952
953 if (i->exclusive_supplier == old_owner) i->exclusive_supplier = new_owner;
954 if (i->exclusive_consumer == old_owner) i->exclusive_consumer = new_owner;
955 }
956
957 /**
958 * Check whether the tile is a forest.
959 * @param tile the tile to investigate.
960 * @return true if and only if the tile is a forest
961 */
IsTileForestIndustry(TileIndex tile)962 bool IsTileForestIndustry(TileIndex tile)
963 {
964 /* Check for industry tile */
965 if (!IsTileType(tile, MP_INDUSTRY)) return false;
966
967 const Industry *ind = Industry::GetByTile(tile);
968
969 /* Check for organic industry (i.e. not processing or extractive) */
970 if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_ORGANIC) == 0) return false;
971
972 /* Check for wood production */
973 for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
974 /* The industry produces wood. */
975 if (ind->produced_cargo[i] != CT_INVALID && CargoSpec::Get(ind->produced_cargo[i])->label == 'WOOD') return true;
976 }
977
978 return false;
979 }
980
981 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
982
983 /**
984 * Check whether the tile can be replaced by a farm field.
985 * @param tile the tile to investigate.
986 * @param allow_fields if true, the method will return true even if
987 * the tile is a farm tile, otherwise the tile may not be a farm tile
988 * @return true if the tile can become a farm field
989 */
IsSuitableForFarmField(TileIndex tile,bool allow_fields)990 static bool IsSuitableForFarmField(TileIndex tile, bool allow_fields)
991 {
992 switch (GetTileType(tile)) {
993 case MP_CLEAR: return !IsClearGround(tile, CLEAR_SNOW) && !IsClearGround(tile, CLEAR_DESERT) && (allow_fields || !IsClearGround(tile, CLEAR_FIELDS));
994 case MP_TREES: return GetTreeGround(tile) != TREE_GROUND_SHORE;
995 default: return false;
996 }
997 }
998
999 /**
1000 * Build farm field fence
1001 * @param tile the tile to position the fence on
1002 * @param size the size of the field being planted in tiles
1003 * @param type type of fence to set
1004 * @param side the side of the tile to attempt placement
1005 */
SetupFarmFieldFence(TileIndex tile,int size,byte type,DiagDirection side)1006 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, DiagDirection side)
1007 {
1008 TileIndexDiff diff = (DiagDirToAxis(side) == AXIS_Y ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
1009
1010 do {
1011 tile = TILE_MASK(tile);
1012
1013 if (IsTileType(tile, MP_CLEAR) && IsClearGround(tile, CLEAR_FIELDS)) {
1014 byte or_ = type;
1015
1016 if (or_ == 1 && Chance16(1, 7)) or_ = 2;
1017
1018 SetFence(tile, side, or_);
1019 }
1020
1021 tile += diff;
1022 } while (--size);
1023 }
1024
PlantFarmField(TileIndex tile,IndustryID industry)1025 static void PlantFarmField(TileIndex tile, IndustryID industry)
1026 {
1027 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
1028 if (GetTileZ(tile) + 2 >= GetSnowLine()) return;
1029 }
1030
1031 /* determine field size */
1032 uint32 r = (Random() & 0x303) + 0x404;
1033 if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
1034 uint size_x = GB(r, 0, 8);
1035 uint size_y = GB(r, 8, 8);
1036
1037 TileArea ta(tile - TileDiffXY(std::min(TileX(tile), size_x / 2), std::min(TileY(tile), size_y / 2)), size_x, size_y);
1038 ta.ClampToMap();
1039
1040 if (ta.w == 0 || ta.h == 0) return;
1041
1042 /* check the amount of bad tiles */
1043 int count = 0;
1044 for (TileIndex cur_tile : ta) {
1045 assert(cur_tile < MapSize());
1046 count += IsSuitableForFarmField(cur_tile, false);
1047 }
1048 if (count * 2 < ta.w * ta.h) return;
1049
1050 /* determine type of field */
1051 r = Random();
1052 uint counter = GB(r, 5, 3);
1053 uint field_type = GB(r, 8, 8) * 9 >> 8;
1054
1055 /* make field */
1056 for (TileIndex cur_tile : ta) {
1057 assert(cur_tile < MapSize());
1058 if (IsSuitableForFarmField(cur_tile, true)) {
1059 MakeField(cur_tile, field_type, industry);
1060 SetClearCounter(cur_tile, counter);
1061 MarkTileDirtyByTile(cur_tile);
1062 }
1063 }
1064
1065 int type = 3;
1066 if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
1067 type = _plantfarmfield_type[Random() & 0xF];
1068 }
1069
1070 SetupFarmFieldFence(ta.tile, ta.h, type, DIAGDIR_NE);
1071 SetupFarmFieldFence(ta.tile, ta.w, type, DIAGDIR_NW);
1072 SetupFarmFieldFence(ta.tile + TileDiffXY(ta.w - 1, 0), ta.h, type, DIAGDIR_SW);
1073 SetupFarmFieldFence(ta.tile + TileDiffXY(0, ta.h - 1), ta.w, type, DIAGDIR_SE);
1074 }
1075
PlantRandomFarmField(const Industry * i)1076 void PlantRandomFarmField(const Industry *i)
1077 {
1078 int x = i->location.w / 2 + Random() % 31 - 16;
1079 int y = i->location.h / 2 + Random() % 31 - 16;
1080
1081 TileIndex tile = TileAddWrap(i->location.tile, x, y);
1082
1083 if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
1084 }
1085
1086 /**
1087 * Search callback function for ChopLumberMillTrees
1088 * @param tile to test
1089 * @param user_data that is passed by the caller. In this case, nothing
1090 * @return the result of the test
1091 */
SearchLumberMillTrees(TileIndex tile,void * user_data)1092 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
1093 {
1094 if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { ///< 3 and up means all fully grown trees
1095 /* found a tree */
1096
1097 Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
1098
1099 _industry_sound_ctr = 1;
1100 _industry_sound_tile = tile;
1101 if (_settings_client.sound.ambient) SndPlayTileFx(SND_38_LUMBER_MILL_1, tile);
1102
1103 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
1104
1105 cur_company.Restore();
1106 return true;
1107 }
1108 return false;
1109 }
1110
1111 /**
1112 * Perform a circular search around the Lumber Mill in order to find trees to cut
1113 * @param i industry
1114 */
ChopLumberMillTrees(Industry * i)1115 static void ChopLumberMillTrees(Industry *i)
1116 {
1117 /* We only want to cut trees if all tiles are completed. */
1118 for (TileIndex tile_cur : i->location) {
1119 if (i->TileBelongsToIndustry(tile_cur)) {
1120 if (!IsIndustryCompleted(tile_cur)) return;
1121 }
1122 }
1123
1124 TileIndex tile = i->location.tile;
1125 if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, nullptr)) { // 40x40 tiles to search.
1126 i->produced_cargo_waiting[0] = std::min(0xffff, i->produced_cargo_waiting[0] + 45); // Found a tree, add according value to waiting cargo.
1127 }
1128 }
1129
ProduceIndustryGoods(Industry * i)1130 static void ProduceIndustryGoods(Industry *i)
1131 {
1132 const IndustrySpec *indsp = GetIndustrySpec(i->type);
1133
1134 /* play a sound? */
1135 if ((i->counter & 0x3F) == 0) {
1136 uint32 r;
1137 if (Chance16R(1, 14, r) && indsp->number_of_sounds != 0 && _settings_client.sound.ambient) {
1138 for (size_t j = 0; j < lengthof(i->last_month_production); j++) {
1139 if (i->last_month_production[j] > 0) {
1140 /* Play sound since last month had production */
1141 SndPlayTileFx(
1142 (SoundFx)(indsp->random_sounds[((r >> 16) * indsp->number_of_sounds) >> 16]),
1143 i->location.tile);
1144 break;
1145 }
1146 }
1147 }
1148 }
1149
1150 i->counter--;
1151
1152 /* produce some cargo */
1153 if ((i->counter % INDUSTRY_PRODUCE_TICKS) == 0) {
1154 if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
1155
1156 IndustryBehaviour indbehav = indsp->behaviour;
1157 for (size_t j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
1158 i->produced_cargo_waiting[j] = std::min(0xffff, i->produced_cargo_waiting[j] + i->production_rate[j]);
1159 }
1160
1161 if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
1162 uint16 cb_res = CALLBACK_FAILED;
1163 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
1164 cb_res = GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile);
1165 }
1166
1167 bool plant;
1168 if (cb_res != CALLBACK_FAILED) {
1169 plant = ConvertBooleanCallback(indsp->grf_prop.grffile, CBID_INDUSTRY_SPECIAL_EFFECT, cb_res);
1170 } else {
1171 plant = Chance16(1, 8);
1172 }
1173
1174 if (plant) PlantRandomFarmField(i);
1175 }
1176 if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
1177 uint16 cb_res = CALLBACK_FAILED;
1178 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
1179 cb_res = GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 1, i, i->type, i->location.tile);
1180 }
1181
1182 bool cut;
1183 if (cb_res != CALLBACK_FAILED) {
1184 cut = ConvertBooleanCallback(indsp->grf_prop.grffile, CBID_INDUSTRY_SPECIAL_EFFECT, cb_res);
1185 } else {
1186 cut = ((i->counter % INDUSTRY_CUT_TREE_TICKS) == 0);
1187 }
1188
1189 if (cut) ChopLumberMillTrees(i);
1190 }
1191
1192 TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
1193 StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
1194 }
1195 }
1196
OnTick_Industry()1197 void OnTick_Industry()
1198 {
1199 if (_industry_sound_ctr != 0) {
1200 _industry_sound_ctr++;
1201
1202 if (_industry_sound_ctr == 75) {
1203 if (_settings_client.sound.ambient) SndPlayTileFx(SND_37_LUMBER_MILL_2, _industry_sound_tile);
1204 } else if (_industry_sound_ctr == 160) {
1205 _industry_sound_ctr = 0;
1206 if (_settings_client.sound.ambient) SndPlayTileFx(SND_36_LUMBER_MILL_3, _industry_sound_tile);
1207 }
1208 }
1209
1210 if (_game_mode == GM_EDITOR) return;
1211
1212 for (Industry *i : Industry::Iterate()) {
1213 ProduceIndustryGoods(i);
1214 }
1215 }
1216
1217 /**
1218 * Check the conditions of #CHECK_NOTHING (Always succeeds).
1219 * @param tile %Tile to perform the checking.
1220 * @return Succeeded or failed command.
1221 */
CheckNewIndustry_NULL(TileIndex tile)1222 static CommandCost CheckNewIndustry_NULL(TileIndex tile)
1223 {
1224 return CommandCost();
1225 }
1226
1227 /**
1228 * Check the conditions of #CHECK_FOREST (Industry should be build above snow-line in arctic climate).
1229 * @param tile %Tile to perform the checking.
1230 * @return Succeeded or failed command.
1231 */
CheckNewIndustry_Forest(TileIndex tile)1232 static CommandCost CheckNewIndustry_Forest(TileIndex tile)
1233 {
1234 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
1235 if (GetTileZ(tile) < HighestSnowLine() + 2) {
1236 return_cmd_error(STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED);
1237 }
1238 }
1239 return CommandCost();
1240 }
1241
1242 /**
1243 * Check if a tile is within a distance from map edges, scaled by map dimensions independently.
1244 * Each dimension is checked independently, and dimensions smaller than 256 are not scaled.
1245 * @param tile Which tile to check distance of.
1246 * @param maxdist Normal distance on a 256x256 map.
1247 * @return True if the tile is near the map edge.
1248 */
CheckScaledDistanceFromEdge(TileIndex tile,uint maxdist)1249 static bool CheckScaledDistanceFromEdge(TileIndex tile, uint maxdist)
1250 {
1251 uint maxdist_x = maxdist;
1252 uint maxdist_y = maxdist;
1253
1254 if (MapSizeX() > 256) maxdist_x *= MapSizeX() / 256;
1255 if (MapSizeY() > 256) maxdist_y *= MapSizeY() / 256;
1256
1257 if (DistanceFromEdgeDir(tile, DIAGDIR_NE) < maxdist_x) return true;
1258 if (DistanceFromEdgeDir(tile, DIAGDIR_NW) < maxdist_y) return true;
1259 if (DistanceFromEdgeDir(tile, DIAGDIR_SW) < maxdist_x) return true;
1260 if (DistanceFromEdgeDir(tile, DIAGDIR_SE) < maxdist_y) return true;
1261
1262 return false;
1263 }
1264
1265 /**
1266 * Check the conditions of #CHECK_REFINERY (Industry should be positioned near edge of the map).
1267 * @param tile %Tile to perform the checking.
1268 * @return Succeeded or failed command.
1269 */
CheckNewIndustry_OilRefinery(TileIndex tile)1270 static CommandCost CheckNewIndustry_OilRefinery(TileIndex tile)
1271 {
1272 if (_game_mode == GM_EDITOR) return CommandCost();
1273
1274 if (CheckScaledDistanceFromEdge(TILE_ADDXY(tile, 1, 1), _settings_game.game_creation.oil_refinery_limit)) return CommandCost();
1275
1276 return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
1277 }
1278
1279 extern bool _ignore_restrictions;
1280
1281 /**
1282 * Check the conditions of #CHECK_OIL_RIG (Industries at sea should be positioned near edge of the map).
1283 * @param tile %Tile to perform the checking.
1284 * @return Succeeded or failed command.
1285 */
CheckNewIndustry_OilRig(TileIndex tile)1286 static CommandCost CheckNewIndustry_OilRig(TileIndex tile)
1287 {
1288 if (_game_mode == GM_EDITOR && _ignore_restrictions) return CommandCost();
1289
1290 if (TileHeight(tile) == 0 &&
1291 CheckScaledDistanceFromEdge(TILE_ADDXY(tile, 1, 1), _settings_game.game_creation.oil_refinery_limit)) return CommandCost();
1292
1293 return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
1294 }
1295
1296 /**
1297 * Check the conditions of #CHECK_FARM (Industry should be below snow-line in arctic).
1298 * @param tile %Tile to perform the checking.
1299 * @return Succeeded or failed command.
1300 */
CheckNewIndustry_Farm(TileIndex tile)1301 static CommandCost CheckNewIndustry_Farm(TileIndex tile)
1302 {
1303 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
1304 if (GetTileZ(tile) + 2 >= HighestSnowLine()) {
1305 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1306 }
1307 }
1308 return CommandCost();
1309 }
1310
1311 /**
1312 * Check the conditions of #CHECK_PLANTATION (Industry should NOT be in the desert).
1313 * @param tile %Tile to perform the checking.
1314 * @return Succeeded or failed command.
1315 */
CheckNewIndustry_Plantation(TileIndex tile)1316 static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
1317 {
1318 if (GetTropicZone(tile) == TROPICZONE_DESERT) {
1319 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1320 }
1321 return CommandCost();
1322 }
1323
1324 /**
1325 * Check the conditions of #CHECK_WATER (Industry should be in the desert).
1326 * @param tile %Tile to perform the checking.
1327 * @return Succeeded or failed command.
1328 */
CheckNewIndustry_Water(TileIndex tile)1329 static CommandCost CheckNewIndustry_Water(TileIndex tile)
1330 {
1331 if (GetTropicZone(tile) != TROPICZONE_DESERT) {
1332 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
1333 }
1334 return CommandCost();
1335 }
1336
1337 /**
1338 * Check the conditions of #CHECK_LUMBERMILL (Industry should be in the rain forest).
1339 * @param tile %Tile to perform the checking.
1340 * @return Succeeded or failed command.
1341 */
CheckNewIndustry_Lumbermill(TileIndex tile)1342 static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
1343 {
1344 if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
1345 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
1346 }
1347 return CommandCost();
1348 }
1349
1350 /**
1351 * Check the conditions of #CHECK_BUBBLEGEN (Industry should be in low land).
1352 * @param tile %Tile to perform the checking.
1353 * @return Succeeded or failed command.
1354 */
CheckNewIndustry_BubbleGen(TileIndex tile)1355 static CommandCost CheckNewIndustry_BubbleGen(TileIndex tile)
1356 {
1357 if (GetTileZ(tile) > 4) {
1358 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS);
1359 }
1360 return CommandCost();
1361 }
1362
1363 /**
1364 * Industrytype check function signature.
1365 * @param tile %Tile to check.
1366 * @return Succeeded or failed command.
1367 */
1368 typedef CommandCost CheckNewIndustryProc(TileIndex tile);
1369
1370 /** Check functions for different types of industry. */
1371 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
1372 CheckNewIndustry_NULL, ///< CHECK_NOTHING
1373 CheckNewIndustry_Forest, ///< CHECK_FOREST
1374 CheckNewIndustry_OilRefinery, ///< CHECK_REFINERY
1375 CheckNewIndustry_Farm, ///< CHECK_FARM
1376 CheckNewIndustry_Plantation, ///< CHECK_PLANTATION
1377 CheckNewIndustry_Water, ///< CHECK_WATER
1378 CheckNewIndustry_Lumbermill, ///< CHECK_LUMBERMILL
1379 CheckNewIndustry_BubbleGen, ///< CHECK_BUBBLEGEN
1380 CheckNewIndustry_OilRig, ///< CHECK_OIL_RIG
1381 };
1382
1383 /**
1384 * Find a town for the industry, while checking for multiple industries in the same town.
1385 * @param tile Position of the industry to build.
1386 * @param type Industry type.
1387 * @param[out] t Pointer to return town for the new industry, \c nullptr is written if no good town can be found.
1388 * @return Succeeded or failed command.
1389 *
1390 * @pre \c *t != nullptr
1391 * @post \c *t points to a town on success, and \c nullptr on failure.
1392 */
FindTownForIndustry(TileIndex tile,int type,Town ** t)1393 static CommandCost FindTownForIndustry(TileIndex tile, int type, Town **t)
1394 {
1395 *t = ClosestTownFromTile(tile, UINT_MAX);
1396
1397 if (_settings_game.economy.multiple_industry_per_town) return CommandCost();
1398
1399 for (const Industry *i : Industry::Iterate()) {
1400 if (i->type == (byte)type && i->town == *t) {
1401 *t = nullptr;
1402 return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
1403 }
1404 }
1405
1406 return CommandCost();
1407 }
1408
IsSlopeRefused(Slope current,Slope refused)1409 bool IsSlopeRefused(Slope current, Slope refused)
1410 {
1411 if (IsSteepSlope(current)) return true;
1412 if (current != SLOPE_FLAT) {
1413 if (IsSteepSlope(refused)) return true;
1414
1415 Slope t = ComplementSlope(current);
1416
1417 if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
1418 if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
1419 if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
1420 if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
1421 }
1422
1423 return false;
1424 }
1425
1426 /**
1427 * Are the tiles of the industry free?
1428 * @param tile Position to check.
1429 * @param layout Industry tiles table.
1430 * @param layout_index The index of the layout to build/fund
1431 * @param type Type of the industry.
1432 * @param initial_random_bits The random bits the industry is going to have after construction.
1433 * @param founder Industry founder
1434 * @param creation_type The circumstances the industry is created under.
1435 * @param[out] custom_shape_check Perform custom check for the site.
1436 * @return Failed or succeeded command.
1437 */
CheckIfIndustryTilesAreFree(TileIndex tile,const IndustryTileLayout & layout,size_t layout_index,int type,uint16 initial_random_bits,Owner founder,IndustryAvailabilityCallType creation_type,bool * custom_shape_check=nullptr)1438 static CommandCost CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileLayout &layout, size_t layout_index, int type, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type, bool *custom_shape_check = nullptr)
1439 {
1440 bool refused_slope = false;
1441 bool custom_shape = false;
1442
1443 for (const IndustryTileLayoutTile &it : layout) {
1444 IndustryGfx gfx = GetTranslatedIndustryTileID(it.gfx);
1445 TileIndex cur_tile = TileAddWrap(tile, it.ti.x, it.ti.y);
1446
1447 if (!IsValidTile(cur_tile)) {
1448 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1449 }
1450
1451 if (gfx == GFX_WATERTILE_SPECIALCHECK) {
1452 if (!IsWaterTile(cur_tile) ||
1453 !IsTileFlat(cur_tile)) {
1454 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1455 }
1456 } else {
1457 CommandCost ret = EnsureNoVehicleOnGround(cur_tile);
1458 if (ret.Failed()) return ret;
1459 if (IsBridgeAbove(cur_tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1460
1461 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
1462
1463 IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
1464
1465 /* Perform land/water check if not disabled */
1466 if (!HasBit(its->slopes_refused, 5) && ((HasTileWaterClass(cur_tile) && IsTileOnWater(cur_tile)) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1467
1468 if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
1469 custom_shape = true;
1470 CommandCost ret = PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, layout_index, initial_random_bits, founder, creation_type);
1471 if (ret.Failed()) return ret;
1472 } else {
1473 Slope tileh = GetTileSlope(cur_tile);
1474 refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
1475 }
1476
1477 if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) || // Tile must be a house
1478 ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) { // Tile is allowed to be a house (and it is a house)
1479 if (!IsTileType(cur_tile, MP_HOUSE)) {
1480 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS);
1481 }
1482
1483 /* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */
1484 Backup<CompanyID> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
1485 CommandCost ret = DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
1486 cur_company.Restore();
1487
1488 if (ret.Failed()) return ret;
1489 } else {
1490 /* Clear the tiles, but do not affect town ratings */
1491 CommandCost ret = DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
1492
1493 if (ret.Failed()) return ret;
1494 }
1495 }
1496 }
1497
1498 if (custom_shape_check != nullptr) *custom_shape_check = custom_shape;
1499
1500 /* It is almost impossible to have a fully flat land in TG, so what we
1501 * do is that we check if we can make the land flat later on. See
1502 * CheckIfCanLevelIndustryPlatform(). */
1503 if (!refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions)) {
1504 return CommandCost();
1505 }
1506 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1507 }
1508
1509 /**
1510 * Is the industry allowed to be built at this place for the town?
1511 * @param tile Tile to construct the industry.
1512 * @param type Type of the industry.
1513 * @param t Town authority that the industry belongs to.
1514 * @return Succeeded or failed command.
1515 */
CheckIfIndustryIsAllowed(TileIndex tile,int type,const Town * t)1516 static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
1517 {
1518 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->cache.population < 1200) {
1519 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
1520 }
1521
1522 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
1523 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_NEAR_TOWN_CENTER);
1524 }
1525
1526 return CommandCost();
1527 }
1528
CheckCanTerraformSurroundingTiles(TileIndex tile,uint height,int internal)1529 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
1530 {
1531 /* Check if we don't leave the map */
1532 if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
1533
1534 TileArea ta(tile - TileDiffXY(1, 1), 2, 2);
1535 for (TileIndex tile_walk : ta) {
1536 uint curh = TileHeight(tile_walk);
1537 /* Is the tile clear? */
1538 if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES)) return false;
1539
1540 /* Don't allow too big of a change if this is the sub-tile check */
1541 if (internal != 0 && Delta(curh, height) > 1) return false;
1542
1543 /* Different height, so the surrounding tiles of this tile
1544 * has to be correct too (in level, or almost in level)
1545 * else you get a chain-reaction of terraforming. */
1546 if (internal == 0 && curh != height) {
1547 if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1)) {
1548 return false;
1549 }
1550 }
1551 }
1552
1553 return true;
1554 }
1555
1556 /**
1557 * This function tries to flatten out the land below an industry, without
1558 * damaging the surroundings too much.
1559 */
CheckIfCanLevelIndustryPlatform(TileIndex tile,DoCommandFlag flags,const IndustryTileLayout & layout,int type)1560 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileLayout &layout, int type)
1561 {
1562 int max_x = 0;
1563 int max_y = 0;
1564
1565 /* Finds dimensions of largest variant of this industry */
1566 for (const IndustryTileLayoutTile &it : layout) {
1567 if (it.gfx == GFX_WATERTILE_SPECIALCHECK) continue; // watercheck tiles don't count for footprint size
1568 if (it.ti.x > max_x) max_x = it.ti.x;
1569 if (it.ti.y > max_y) max_y = it.ti.y;
1570 }
1571
1572 /* Remember level height */
1573 uint h = TileHeight(tile);
1574
1575 if (TileX(tile) <= _settings_game.construction.industry_platform + 1U || TileY(tile) <= _settings_game.construction.industry_platform + 1U) return false;
1576 /* Check that all tiles in area and surrounding are clear
1577 * this determines that there are no obstructing items */
1578
1579 /* TileArea::Expand is not used here as we need to abort
1580 * instead of clamping if the bounds cannot expanded. */
1581 TileArea ta(tile + TileDiffXY(-_settings_game.construction.industry_platform, -_settings_game.construction.industry_platform),
1582 max_x + 2 + 2 * _settings_game.construction.industry_platform, max_y + 2 + 2 * _settings_game.construction.industry_platform);
1583
1584 if (TileX(ta.tile) + ta.w >= MapMaxX() || TileY(ta.tile) + ta.h >= MapMaxY()) return false;
1585
1586 /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry.
1587 * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */
1588 Backup<CompanyID> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
1589
1590 for (TileIndex tile_walk : ta) {
1591 uint curh = TileHeight(tile_walk);
1592 if (curh != h) {
1593 /* This tile needs terraforming. Check if we can do that without
1594 * damaging the surroundings too much. */
1595 if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
1596 cur_company.Restore();
1597 return false;
1598 }
1599 /* This is not 100% correct check, but the best we can do without modifying the map.
1600 * What is missing, is if the difference in height is more than 1.. */
1601 if (DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND).Failed()) {
1602 cur_company.Restore();
1603 return false;
1604 }
1605 }
1606 }
1607
1608 if (flags & DC_EXEC) {
1609 /* Terraform the land under the industry */
1610 for (TileIndex tile_walk : ta) {
1611 uint curh = TileHeight(tile_walk);
1612 while (curh != h) {
1613 /* We give the terraforming for free here, because we can't calculate
1614 * exact cost in the test-round, and as we all know, that will cause
1615 * a nice assert if they don't match ;) */
1616 DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
1617 curh += (curh > h) ? -1 : 1;
1618 }
1619 }
1620 }
1621
1622 cur_company.Restore();
1623 return true;
1624 }
1625
1626
1627 /**
1628 * Check that the new industry is far enough from conflicting industries.
1629 * @param tile Tile to construct the industry.
1630 * @param type Type of the new industry.
1631 * @return Succeeded or failed command.
1632 */
CheckIfFarEnoughFromConflictingIndustry(TileIndex tile,int type)1633 static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
1634 {
1635 const IndustrySpec *indspec = GetIndustrySpec(type);
1636
1637 /* On a large map with many industries, it may be faster to check an area. */
1638 static const int dmax = 14;
1639 if (Industry::GetNumItems() > (size_t) (dmax * dmax * 2)) {
1640 const Industry* i = nullptr;
1641 TileArea tile_area = TileArea(tile, 1, 1).Expand(dmax);
1642 for (TileIndex atile : tile_area) {
1643 if (GetTileType(atile) == MP_INDUSTRY) {
1644 const Industry *i2 = Industry::GetByTile(atile);
1645 if (i == i2) continue;
1646 i = i2;
1647 if (DistanceMax(tile, i->location.tile) > (uint)dmax) continue;
1648 if (i->type == indspec->conflicting[0] ||
1649 i->type == indspec->conflicting[1] ||
1650 i->type == indspec->conflicting[2]) {
1651 return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
1652 }
1653 }
1654 }
1655 return CommandCost();
1656 }
1657
1658 for (const Industry *i : Industry::Iterate()) {
1659 /* Within 14 tiles from another industry is considered close */
1660 if (DistanceMax(tile, i->location.tile) > 14) continue;
1661
1662 /* check if there are any conflicting industry types around */
1663 if (i->type == indspec->conflicting[0] ||
1664 i->type == indspec->conflicting[1] ||
1665 i->type == indspec->conflicting[2]) {
1666 return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
1667 }
1668 }
1669 return CommandCost();
1670 }
1671
1672 /**
1673 * Advertise about a new industry opening.
1674 * @param ind Industry being opened.
1675 */
AdvertiseIndustryOpening(const Industry * ind)1676 static void AdvertiseIndustryOpening(const Industry *ind)
1677 {
1678 const IndustrySpec *ind_spc = GetIndustrySpec(ind->type);
1679 SetDParam(0, ind_spc->name);
1680 if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
1681 SetDParam(1, STR_TOWN_NAME);
1682 SetDParam(2, ind->town->index);
1683 } else {
1684 SetDParam(1, ind->town->index);
1685 }
1686 AddIndustryNewsItem(ind_spc->new_industry_text, NT_INDUSTRY_OPEN, ind->index);
1687 AI::BroadcastNewEvent(new ScriptEventIndustryOpen(ind->index));
1688 Game::NewEvent(new ScriptEventIndustryOpen(ind->index));
1689 }
1690
1691 /**
1692 * Populate an industry's list of nearby stations, and if it accepts any cargo, also
1693 * add the industry to each station's nearby industry list.
1694 * @param ind Industry
1695 */
PopulateStationsNearby(Industry * ind)1696 static void PopulateStationsNearby(Industry *ind)
1697 {
1698 if (ind->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) {
1699 /* Industry has a neutral station. Use it and ignore any other nearby stations. */
1700 ind->stations_near.insert(ind->neutral_station);
1701 ind->neutral_station->industries_near.clear();
1702 ind->neutral_station->industries_near.insert(ind);
1703 return;
1704 }
1705
1706 ForAllStationsAroundTiles(ind->location, [ind](Station *st, TileIndex tile) {
1707 if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != ind->index) return false;
1708 ind->stations_near.insert(st);
1709 st->AddIndustryToDeliver(ind);
1710 return true;
1711 });
1712 }
1713
1714 /**
1715 * Put an industry on the map.
1716 * @param i Just allocated poolitem, mostly empty.
1717 * @param tile North tile of the industry.
1718 * @param type Type of the industry.
1719 * @param layout Industrylayout to build.
1720 * @param layout_index Number of the industry layout.
1721 * @param t Nearest town.
1722 * @param founder Founder of the industry; OWNER_NONE in case of random construction.
1723 * @param initial_random_bits Random bits for the industry.
1724 */
DoCreateNewIndustry(Industry * i,TileIndex tile,IndustryType type,const IndustryTileLayout & layout,size_t layout_index,Town * t,Owner founder,uint16 initial_random_bits)1725 static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, const IndustryTileLayout &layout, size_t layout_index, Town *t, Owner founder, uint16 initial_random_bits)
1726 {
1727 const IndustrySpec *indspec = GetIndustrySpec(type);
1728
1729 i->location = TileArea(tile, 1, 1);
1730 i->type = type;
1731 Industry::IncIndustryTypeCount(type);
1732
1733 MemCpyT(i->produced_cargo, indspec->produced_cargo, lengthof(i->produced_cargo));
1734 MemCpyT(i->production_rate, indspec->production_rate, lengthof(i->production_rate));
1735 MemCpyT(i->accepts_cargo, indspec->accepts_cargo, lengthof(i->accepts_cargo));
1736
1737 MemSetT(i->produced_cargo_waiting, 0, lengthof(i->produced_cargo_waiting));
1738 MemSetT(i->this_month_production, 0, lengthof(i->this_month_production));
1739 MemSetT(i->this_month_transported, 0, lengthof(i->this_month_transported));
1740 MemSetT(i->last_month_pct_transported, 0, lengthof(i->last_month_pct_transported));
1741 MemSetT(i->last_month_transported, 0, lengthof(i->last_month_transported));
1742 MemSetT(i->incoming_cargo_waiting, 0, lengthof(i->incoming_cargo_waiting));
1743 MemSetT(i->last_cargo_accepted_at, 0, lengthof(i->last_cargo_accepted_at));
1744
1745 /* Randomize inital production if non-original economy is used and there are no production related callbacks. */
1746 if (!indspec->UsesOriginalEconomy()) {
1747 for (size_t ci = 0; ci < lengthof(i->production_rate); ci++) {
1748 i->production_rate[ci] = std::min((RandomRange(256) + 128) * i->production_rate[ci] >> 8, 255u);
1749 }
1750 }
1751
1752 i->town = t;
1753 i->owner = OWNER_NONE;
1754
1755 uint16 r = Random();
1756 i->random_colour = GB(r, 0, 4);
1757 i->counter = GB(r, 4, 12);
1758 i->random = initial_random_bits;
1759 i->was_cargo_delivered = false;
1760 i->last_prod_year = _cur_year;
1761 i->founder = founder;
1762 i->ctlflags = INDCTL_NONE;
1763
1764 i->construction_date = _date;
1765 i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
1766 (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
1767
1768 /* Adding 1 here makes it conform to specs of var44 of varaction2 for industries
1769 * 0 = created prior of newindustries
1770 * else, chosen layout + 1 */
1771 i->selected_layout = (byte)(layout_index + 1);
1772
1773 i->exclusive_supplier = INVALID_OWNER;
1774 i->exclusive_consumer = INVALID_OWNER;
1775
1776 i->prod_level = PRODLEVEL_DEFAULT;
1777
1778 /* Call callbacks after the regular fields got initialised. */
1779
1780 if (HasBit(indspec->callback_mask, CBM_IND_PROD_CHANGE_BUILD)) {
1781 uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROD_CHANGE_BUILD, 0, Random(), i, type, INVALID_TILE);
1782 if (res != CALLBACK_FAILED) {
1783 if (res < PRODLEVEL_MINIMUM || res > PRODLEVEL_MAXIMUM) {
1784 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROD_CHANGE_BUILD, res);
1785 } else {
1786 i->prod_level = res;
1787 i->RecomputeProductionMultipliers();
1788 }
1789 }
1790 }
1791
1792 if (_generating_world) {
1793 if (HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
1794 IndustryProductionCallback(i, 1);
1795 for (size_t ci = 0; ci < lengthof(i->last_month_production); ci++) {
1796 i->last_month_production[ci] = i->produced_cargo_waiting[ci] * 8;
1797 i->produced_cargo_waiting[ci] = 0;
1798 }
1799 }
1800
1801 for (size_t ci = 0; ci < lengthof(i->last_month_production); ci++) {
1802 i->last_month_production[ci] += i->production_rate[ci] * 8;
1803 }
1804 }
1805
1806 if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
1807 uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
1808 if (res != CALLBACK_FAILED) {
1809 if (GB(res, 4, 11) != 0) ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_DECIDE_COLOUR, res);
1810 i->random_colour = GB(res, 0, 4);
1811 }
1812 }
1813
1814 if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
1815 /* Clear all input cargo types */
1816 for (uint j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
1817 /* Query actual types */
1818 uint maxcargoes = (indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED) ? lengthof(i->accepts_cargo) : 3;
1819 for (uint j = 0; j < maxcargoes; j++) {
1820 uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
1821 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
1822 if (indspec->grf_prop.grffile->grf_version >= 8 && res >= 0x100) {
1823 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_INPUT_CARGO_TYPES, res);
1824 break;
1825 }
1826 CargoID cargo = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
1827 /* Industries without "unlimited" cargo types support depend on the specific order/slots of cargo types.
1828 * They need to be able to blank out specific slots without aborting the callback sequence,
1829 * and solve this by returning undefined cargo indexes. Skip these. */
1830 if (cargo == CT_INVALID && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue;
1831 /* Verify valid cargo */
1832 if (std::find(indspec->accepts_cargo, endof(indspec->accepts_cargo), cargo) == endof(indspec->accepts_cargo)) {
1833 /* Cargo not in spec, error in NewGRF */
1834 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_INPUT_CARGO_TYPES, res);
1835 break;
1836 }
1837 if (std::find(i->accepts_cargo, i->accepts_cargo + j, cargo) != i->accepts_cargo + j) {
1838 /* Duplicate cargo */
1839 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_INPUT_CARGO_TYPES, res);
1840 break;
1841 }
1842 i->accepts_cargo[j] = cargo;
1843 }
1844 }
1845
1846 if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
1847 /* Clear all output cargo types */
1848 for (uint j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
1849 /* Query actual types */
1850 uint maxcargoes = (indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED) ? lengthof(i->produced_cargo) : 2;
1851 for (uint j = 0; j < maxcargoes; j++) {
1852 uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
1853 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
1854 if (indspec->grf_prop.grffile->grf_version >= 8 && res >= 0x100) {
1855 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
1856 break;
1857 }
1858 CargoID cargo = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
1859 /* Allow older GRFs to skip slots. */
1860 if (cargo == CT_INVALID && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue;
1861 /* Verify valid cargo */
1862 if (std::find(indspec->produced_cargo, endof(indspec->produced_cargo), cargo) == endof(indspec->produced_cargo)) {
1863 /* Cargo not in spec, error in NewGRF */
1864 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
1865 break;
1866 }
1867 if (std::find(i->produced_cargo, i->produced_cargo + j, cargo) != i->produced_cargo + j) {
1868 /* Duplicate cargo */
1869 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
1870 break;
1871 }
1872 i->produced_cargo[j] = cargo;
1873 }
1874 }
1875
1876 /* Plant the tiles */
1877
1878 for (const IndustryTileLayoutTile &it : layout) {
1879 TileIndex cur_tile = tile + ToTileIndexDiff(it.ti);
1880
1881 if (it.gfx != GFX_WATERTILE_SPECIALCHECK) {
1882 i->location.Add(cur_tile);
1883
1884 WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
1885
1886 DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
1887
1888 MakeIndustry(cur_tile, i->index, it.gfx, Random(), wc);
1889
1890 if (_generating_world) {
1891 SetIndustryConstructionCounter(cur_tile, 3);
1892 SetIndustryConstructionStage(cur_tile, 2);
1893 }
1894
1895 /* it->gfx is stored in the map. But the translated ID cur_gfx is the interesting one */
1896 IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it.gfx);
1897 const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
1898 if (its->animation.status != ANIM_STATUS_NO_ANIMATION) AddAnimatedTile(cur_tile);
1899 }
1900 }
1901
1902 if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
1903 for (uint j = 0; j != 50; j++) PlantRandomFarmField(i);
1904 }
1905 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_FORCE_REBUILD);
1906
1907 if (!_generating_world) PopulateStationsNearby(i);
1908 }
1909
1910 /**
1911 * Helper function for Build/Fund an industry
1912 * @param tile tile where industry is built
1913 * @param type of industry to build
1914 * @param flags of operations to conduct
1915 * @param indspec pointer to industry specifications
1916 * @param layout_index the index of the itsepc to build/fund
1917 * @param random_var8f random seed (possibly) used by industries
1918 * @param random_initial_bits The random bits the industry is going to have after construction.
1919 * @param founder Founder of the industry
1920 * @param creation_type The circumstances the industry is created under.
1921 * @param[out] ip Pointer to store newly created industry.
1922 * @return Succeeded or failed command.
1923 *
1924 * @post \c *ip contains the newly created industry if all checks are successful and the \a flags request actual creation, else it contains \c nullptr afterwards.
1925 */
CreateNewIndustryHelper(TileIndex tile,IndustryType type,DoCommandFlag flags,const IndustrySpec * indspec,size_t layout_index,uint32 random_var8f,uint16 random_initial_bits,Owner founder,IndustryAvailabilityCallType creation_type,Industry ** ip)1926 static CommandCost CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, size_t layout_index, uint32 random_var8f, uint16 random_initial_bits, Owner founder, IndustryAvailabilityCallType creation_type, Industry **ip)
1927 {
1928 assert(layout_index < indspec->layouts.size());
1929 const IndustryTileLayout &layout = indspec->layouts[layout_index];
1930 bool custom_shape_check = false;
1931
1932 *ip = nullptr;
1933
1934 std::vector<ClearedObjectArea> object_areas(_cleared_object_areas);
1935 CommandCost ret = CheckIfIndustryTilesAreFree(tile, layout, layout_index, type, random_initial_bits, founder, creation_type, &custom_shape_check);
1936 _cleared_object_areas = object_areas;
1937 if (ret.Failed()) return ret;
1938
1939 if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
1940 ret = CheckIfCallBackAllowsCreation(tile, type, layout_index, random_var8f, random_initial_bits, founder, creation_type);
1941 } else {
1942 ret = _check_new_industry_procs[indspec->check_proc](tile);
1943 }
1944 if (ret.Failed()) return ret;
1945
1946 if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world &&
1947 !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, layout, type)) {
1948 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
1949 }
1950
1951 ret = CheckIfFarEnoughFromConflictingIndustry(tile, type);
1952 if (ret.Failed()) return ret;
1953
1954 Town *t = nullptr;
1955 ret = FindTownForIndustry(tile, type, &t);
1956 if (ret.Failed()) return ret;
1957 assert(t != nullptr);
1958
1959 ret = CheckIfIndustryIsAllowed(tile, type, t);
1960 if (ret.Failed()) return ret;
1961
1962 if (!Industry::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_INDUSTRIES);
1963
1964 if (flags & DC_EXEC) {
1965 *ip = new Industry(tile);
1966 if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, layout, type);
1967 DoCreateNewIndustry(*ip, tile, type, layout, layout_index, t, founder, random_initial_bits);
1968 }
1969
1970 return CommandCost();
1971 }
1972
1973 /**
1974 * Build/Fund an industry
1975 * @param tile tile where industry is built
1976 * @param flags of operations to conduct
1977 * @param p1 various bitstuffed elements
1978 * - p1 = (bit 0 - 7) - industry type see build_industry.h and see industry.h
1979 * - p1 = (bit 8 - 15) - first layout to try
1980 * - p1 = (bit 16 ) - 0 = prospect, 1 = fund (only valid if current company is DEITY)
1981 * @param p2 seed to use for desyncfree randomisations
1982 * @param text unused
1983 * @return the cost of this operation or an error
1984 */
CmdBuildIndustry(TileIndex tile,DoCommandFlag flags,uint32 p1,uint32 p2,const std::string & text)1985 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
1986 {
1987 IndustryType it = GB(p1, 0, 8);
1988 if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
1989
1990 const IndustrySpec *indspec = GetIndustrySpec(it);
1991
1992 /* Check if the to-be built/founded industry is available for this climate. */
1993 if (!indspec->enabled || indspec->layouts.empty()) return CMD_ERROR;
1994
1995 /* If the setting for raw-material industries is not on, you cannot build raw-material industries.
1996 * Raw material industries are industries that do not accept cargo (at least for now) */
1997 if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
1998 return CMD_ERROR;
1999 }
2000
2001 if (_game_mode != GM_EDITOR && GetIndustryProbabilityCallback(it, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) {
2002 return CMD_ERROR;
2003 }
2004
2005 Randomizer randomizer;
2006 randomizer.SetSeed(p2);
2007 uint16 random_initial_bits = GB(p2, 0, 16);
2008 uint32 random_var8f = randomizer.Next();
2009 size_t num_layouts = indspec->layouts.size();
2010 CommandCost ret = CommandCost(STR_ERROR_SITE_UNSUITABLE);
2011 const bool deity_prospect = _current_company == OWNER_DEITY && !HasBit(p1, 16);
2012
2013 Industry *ind = nullptr;
2014 if (deity_prospect || (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry())) {
2015 if (flags & DC_EXEC) {
2016 /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
2017 Backup<CompanyID> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
2018 /* Prospecting has a chance to fail, however we cannot guarantee that something can
2019 * be built on the map, so the chance gets lower when the map is fuller, but there
2020 * is nothing we can really do about that. */
2021 if (deity_prospect || Random() <= indspec->prospecting_chance) {
2022 for (int i = 0; i < 5000; i++) {
2023 /* We should not have more than one Random() in a function call
2024 * because parameter evaluation order is not guaranteed in the c++ standard
2025 */
2026 tile = RandomTile();
2027 /* Start with a random layout */
2028 size_t layout = RandomRange((uint32)num_layouts);
2029 /* Check now each layout, starting with the random one */
2030 for (size_t j = 0; j < num_layouts; j++) {
2031 layout = (layout + 1) % num_layouts;
2032 ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, cur_company.GetOriginalValue(), _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_PROSPECTCREATION, &ind);
2033 if (ret.Succeeded()) break;
2034 }
2035 if (ret.Succeeded()) break;
2036 }
2037 }
2038 cur_company.Restore();
2039 }
2040 } else {
2041 size_t layout = GB(p1, 8, 8);
2042 if (layout >= num_layouts) return CMD_ERROR;
2043
2044 /* Check subsequently each layout, starting with the given layout in p1 */
2045 for (size_t i = 0; i < num_layouts; i++) {
2046 layout = (layout + 1) % num_layouts;
2047 ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, _current_company, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, &ind);
2048 if (ret.Succeeded()) break;
2049 }
2050
2051 /* If it still failed, there's no suitable layout to build here, return the error */
2052 if (ret.Failed()) return ret;
2053 }
2054
2055 if ((flags & DC_EXEC) && ind != nullptr && _game_mode != GM_EDITOR) {
2056 AdvertiseIndustryOpening(ind);
2057 }
2058
2059 return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
2060 }
2061
2062 /**
2063 * Change industry properties
2064 * @param tile Unused.
2065 * @param flags Type of operation.
2066 * @param p1 IndustryID
2067 * @param p2 various bitstuffed elements
2068 * - p2 = (bit 0 - 7) - IndustryAction to perform
2069 * - p2 = (bit 8 - 15) - IndustryControlFlags
2070 * (only used with set control flags)
2071 * - p2 = (bit 16 - 23) - CompanyID to set or INVALID_OWNER (available to everyone) or
2072 * OWNER_NONE (neutral stations only) or OWNER_DEITY (no one)
2073 * (only used with set exclusive supplier / consumer)
2074 * @param text - Additional industry text (only used with set text action)
2075 * @return Empty cost or an error.
2076 */
CmdIndustryCtrl(TileIndex tile,DoCommandFlag flags,uint32 p1,uint32 p2,const std::string & text)2077 CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
2078 {
2079 if (_current_company != OWNER_DEITY) return CMD_ERROR;
2080
2081 Industry *ind = Industry::GetIfValid(p1);
2082 if (ind == nullptr) return CMD_ERROR;
2083
2084 auto action = static_cast<IndustryAction>(GB(p2, 0, 8));
2085
2086 switch (action) {
2087 case IndustryAction::SetControlFlags: {
2088 IndustryControlFlags ctlflags = (IndustryControlFlags)GB(p2, 8, 8) & INDCTL_MASK;
2089
2090 if (flags & DC_EXEC) ind->ctlflags = ctlflags;
2091
2092 break;
2093 }
2094
2095 case IndustryAction::SetExclusiveSupplier:
2096 case IndustryAction::SetExclusiveConsumer: {
2097 Owner company_id = (Owner)GB(p2, 16, 8);
2098
2099 if (company_id != OWNER_NONE && company_id != INVALID_OWNER && company_id != OWNER_DEITY
2100 && !Company::IsValidID(company_id)) return CMD_ERROR;
2101
2102 if (flags & DC_EXEC) {
2103 if (action == IndustryAction::SetExclusiveSupplier) {
2104 ind->exclusive_supplier = company_id;
2105 } else {
2106 ind->exclusive_consumer = company_id;
2107 }
2108 }
2109
2110 break;
2111 }
2112
2113 case IndustryAction::SetText: {
2114 ind->text.clear();
2115 if (!text.empty()) ind->text = text;
2116 InvalidateWindowData(WC_INDUSTRY_VIEW, ind->index);
2117 break;
2118 }
2119
2120 default:
2121 NOT_REACHED();
2122 }
2123
2124 return CommandCost();
2125 }
2126
2127 /**
2128 * Create a new industry of random layout.
2129 * @param tile The location to build the industry.
2130 * @param type The industry type to build.
2131 * @param creation_type The circumstances the industry is created under.
2132 * @return the created industry or nullptr if it failed.
2133 */
CreateNewIndustry(TileIndex tile,IndustryType type,IndustryAvailabilityCallType creation_type)2134 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAvailabilityCallType creation_type)
2135 {
2136 const IndustrySpec *indspec = GetIndustrySpec(type);
2137
2138 uint32 seed = Random();
2139 uint32 seed2 = Random();
2140 Industry *i = nullptr;
2141 size_t layout_index = RandomRange((uint32)indspec->layouts.size());
2142 [[maybe_unused]] CommandCost ret = CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, layout_index, seed, GB(seed2, 0, 16), OWNER_NONE, creation_type, &i);
2143 assert(i != nullptr || ret.Failed());
2144 return i;
2145 }
2146
2147 /**
2148 * Compute the appearance probability for an industry during map creation.
2149 * @param it Industry type to compute.
2150 * @param[out] force_at_least_one Returns whether at least one instance should be forced on map creation.
2151 * @return Relative probability for the industry to appear.
2152 */
GetScaledIndustryGenerationProbability(IndustryType it,bool * force_at_least_one)2153 static uint32 GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
2154 {
2155 const IndustrySpec *ind_spc = GetIndustrySpec(it);
2156 uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
2157 if (!ind_spc->enabled || ind_spc->layouts.empty() ||
2158 (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) ||
2159 (chance = GetIndustryProbabilityCallback(it, IACT_MAPGENERATION, chance)) == 0) {
2160 *force_at_least_one = false;
2161 return 0;
2162 } else {
2163 chance *= 16; // to increase precision
2164 /* We want industries appearing at coast to appear less often on bigger maps, as length of coast increases slower than map area.
2165 * For simplicity we scale in both cases, though scaling the probabilities of all industries has no effect. */
2166 chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
2167
2168 *force_at_least_one = (chance > 0) && !(ind_spc->behaviour & INDUSTRYBEH_NOBUILT_MAPCREATION) && (_game_mode != GM_EDITOR);
2169 return chance;
2170 }
2171 }
2172
2173 /**
2174 * Compute the probability for constructing a new industry during game play.
2175 * @param it Industry type to compute.
2176 * @param[out] min_number Minimal number of industries that should exist at the map.
2177 * @return Relative probability for the industry to appear.
2178 */
GetIndustryGamePlayProbability(IndustryType it,byte * min_number)2179 static uint16 GetIndustryGamePlayProbability(IndustryType it, byte *min_number)
2180 {
2181 if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) {
2182 *min_number = 0;
2183 return 0;
2184 }
2185
2186 const IndustrySpec *ind_spc = GetIndustrySpec(it);
2187 byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
2188 if (!ind_spc->enabled || ind_spc->layouts.empty() ||
2189 ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) ||
2190 ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) ||
2191 (chance = GetIndustryProbabilityCallback(it, IACT_RANDOMCREATION, chance)) == 0) {
2192 *min_number = 0;
2193 return 0;
2194 }
2195 *min_number = (ind_spc->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) ? 1 : 0;
2196 return chance;
2197 }
2198
2199 /**
2200 * Get wanted number of industries on the map.
2201 * @return Wanted number of industries at the map.
2202 */
GetNumberOfIndustries()2203 static uint GetNumberOfIndustries()
2204 {
2205 /* Number of industries on a 256x256 map. */
2206 static const uint16 numof_industry_table[] = {
2207 0, // none
2208 0, // minimal
2209 10, // very low
2210 25, // low
2211 55, // normal
2212 80, // high
2213 };
2214
2215 assert(lengthof(numof_industry_table) == ID_END);
2216 uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.industry_density : (uint)ID_VERY_LOW;
2217 return std::min<uint>(IndustryPool::MAX_SIZE, ScaleByMapSize(numof_industry_table[difficulty]));
2218 }
2219
2220 /**
2221 * Try to place the industry in the game.
2222 * Since there is no feedback why placement fails, there is no other option
2223 * than to try a few times before concluding it does not work.
2224 * @param type Industry type of the desired industry.
2225 * @param try_hard Try very hard to find a place. (Used to place at least one industry per type.)
2226 * @return Pointer to created industry, or \c nullptr if creation failed.
2227 */
PlaceIndustry(IndustryType type,IndustryAvailabilityCallType creation_type,bool try_hard)2228 static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
2229 {
2230 uint tries = try_hard ? 10000u : 2000u;
2231 for (; tries > 0; tries--) {
2232 Industry *ind = CreateNewIndustry(RandomTile(), type, creation_type);
2233 if (ind != nullptr) return ind;
2234 }
2235 return nullptr;
2236 }
2237
2238 /**
2239 * Try to build a industry on the map.
2240 * @param type IndustryType of the desired industry
2241 * @param try_hard Try very hard to find a place. (Used to place at least one industry per type)
2242 */
PlaceInitialIndustry(IndustryType type,bool try_hard)2243 static void PlaceInitialIndustry(IndustryType type, bool try_hard)
2244 {
2245 Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
2246
2247 IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
2248 PlaceIndustry(type, IACT_MAPGENERATION, try_hard);
2249
2250 cur_company.Restore();
2251 }
2252
2253 /**
2254 * Get total number of industries existing in the game.
2255 * @return Number of industries currently in the game.
2256 */
GetCurrentTotalNumberOfIndustries()2257 static uint GetCurrentTotalNumberOfIndustries()
2258 {
2259 int total = 0;
2260 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) total += Industry::GetIndustryTypeCount(it);
2261 return total;
2262 }
2263
2264
2265 /** Reset the entry. */
Reset()2266 void IndustryTypeBuildData::Reset()
2267 {
2268 this->probability = 0;
2269 this->min_number = 0;
2270 this->target_count = 0;
2271 this->max_wait = 1;
2272 this->wait_count = 0;
2273 }
2274
2275 /** Completely reset the industry build data. */
Reset()2276 void IndustryBuildData::Reset()
2277 {
2278 this->wanted_inds = GetCurrentTotalNumberOfIndustries() << 16;
2279
2280 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2281 this->builddata[it].Reset();
2282 }
2283 }
2284
2285 /** Monthly update of industry build data. */
MonthlyLoop()2286 void IndustryBuildData::MonthlyLoop()
2287 {
2288 static const int NEWINDS_PER_MONTH = 0x38000 / (10 * 12); // lower 16 bits is a float fraction, 3.5 industries per decade, divided by 10 * 12 months.
2289 if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // 'no industries' setting.
2290
2291 /* To prevent running out of unused industries for the player to connect,
2292 * add a fraction of new industries each month, but only if the manager can keep up. */
2293 uint max_behind = 1 + std::min(99u, ScaleByMapSize(3)); // At most 2 industries for small maps, and 100 at the biggest map (about 6 months industry build attempts).
2294 if (GetCurrentTotalNumberOfIndustries() + max_behind >= (this->wanted_inds >> 16)) {
2295 this->wanted_inds += ScaleByMapSize(NEWINDS_PER_MONTH);
2296 }
2297 }
2298
2299 /**
2300 * This function will create random industries during game creation.
2301 * It will scale the amount of industries by mapsize and difficulty level.
2302 */
GenerateIndustries()2303 void GenerateIndustries()
2304 {
2305 if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // No industries in the game.
2306
2307 uint32 industry_probs[NUM_INDUSTRYTYPES];
2308 bool force_at_least_one[NUM_INDUSTRYTYPES];
2309 uint32 total_prob = 0;
2310 uint num_forced = 0;
2311
2312 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2313 industry_probs[it] = GetScaledIndustryGenerationProbability(it, force_at_least_one + it);
2314 total_prob += industry_probs[it];
2315 if (force_at_least_one[it]) num_forced++;
2316 }
2317
2318 uint total_amount = GetNumberOfIndustries();
2319 if (total_prob == 0 || total_amount < num_forced) {
2320 /* Only place the forced ones */
2321 total_amount = num_forced;
2322 }
2323
2324 SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
2325
2326 /* Try to build one industry per type independent of any probabilities */
2327 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2328 if (force_at_least_one[it]) {
2329 assert(total_amount > 0);
2330 total_amount--;
2331 PlaceInitialIndustry(it, true);
2332 }
2333 }
2334
2335 /* Add the remaining industries according to their probabilities */
2336 for (uint i = 0; i < total_amount; i++) {
2337 uint32 r = RandomRange(total_prob);
2338 IndustryType it = 0;
2339 while (r >= industry_probs[it]) {
2340 r -= industry_probs[it];
2341 it++;
2342 assert(it < NUM_INDUSTRYTYPES);
2343 }
2344 assert(industry_probs[it] > 0);
2345 PlaceInitialIndustry(it, false);
2346 }
2347 _industry_builder.Reset();
2348 }
2349
2350 /**
2351 * Monthly update of industry statistics.
2352 * @param i Industry to update.
2353 */
UpdateIndustryStatistics(Industry * i)2354 static void UpdateIndustryStatistics(Industry *i)
2355 {
2356 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
2357 if (i->produced_cargo[j] != CT_INVALID) {
2358 byte pct = 0;
2359 if (i->this_month_production[j] != 0) {
2360 i->last_prod_year = _cur_year;
2361 pct = std::min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
2362 }
2363 i->last_month_pct_transported[j] = pct;
2364
2365 i->last_month_production[j] = i->this_month_production[j];
2366 i->this_month_production[j] = 0;
2367
2368 i->last_month_transported[j] = i->this_month_transported[j];
2369 i->this_month_transported[j] = 0;
2370 }
2371 }
2372 }
2373
2374 /**
2375 * Recompute #production_rate for current #prod_level.
2376 * This function is only valid when not using smooth economy.
2377 */
RecomputeProductionMultipliers()2378 void Industry::RecomputeProductionMultipliers()
2379 {
2380 const IndustrySpec *indspec = GetIndustrySpec(this->type);
2381 assert(indspec->UsesOriginalEconomy());
2382
2383 /* Rates are rounded up, so e.g. oilrig always produces some passengers */
2384 for (size_t i = 0; i < lengthof(this->production_rate); i++) {
2385 this->production_rate[i] = std::min(CeilDiv(indspec->production_rate[i] * this->prod_level, PRODLEVEL_DEFAULT), 0xFFu);
2386 }
2387 }
2388
FillCachedName() const2389 void Industry::FillCachedName() const
2390 {
2391 char buf[256];
2392 int64 args_array[] = { this->index };
2393 StringParameters tmp_params(args_array);
2394 char *end = GetStringWithArgs(buf, STR_INDUSTRY_NAME, &tmp_params, lastof(buf));
2395 this->cached_name.assign(buf, end);
2396 }
2397
ClearAllIndustryCachedNames()2398 void ClearAllIndustryCachedNames()
2399 {
2400 for (Industry *ind : Industry::Iterate()) {
2401 ind->cached_name.clear();
2402 }
2403 }
2404
2405 /**
2406 * Set the #probability and #min_number fields for the industry type \a it for a running game.
2407 * @param it Industry type.
2408 * @return At least one of the fields has changed value.
2409 */
GetIndustryTypeData(IndustryType it)2410 bool IndustryTypeBuildData::GetIndustryTypeData(IndustryType it)
2411 {
2412 byte min_number;
2413 uint32 probability = GetIndustryGamePlayProbability(it, &min_number);
2414 bool changed = min_number != this->min_number || probability != this->probability;
2415 this->min_number = min_number;
2416 this->probability = probability;
2417 return changed;
2418 }
2419
2420 /** Decide how many industries of each type are needed. */
SetupTargetCount()2421 void IndustryBuildData::SetupTargetCount()
2422 {
2423 bool changed = false;
2424 uint num_planned = 0; // Number of industries planned in the industry build data.
2425 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2426 changed |= this->builddata[it].GetIndustryTypeData(it);
2427 num_planned += this->builddata[it].target_count;
2428 }
2429 uint total_amount = this->wanted_inds >> 16; // Desired total number of industries.
2430 changed |= num_planned != total_amount;
2431 if (!changed) return; // All industries are still the same, no need to re-randomize.
2432
2433 /* Initialize the target counts. */
2434 uint force_build = 0; // Number of industries that should always be available.
2435 uint32 total_prob = 0; // Sum of probabilities.
2436 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2437 IndustryTypeBuildData *ibd = this->builddata + it;
2438 force_build += ibd->min_number;
2439 ibd->target_count = ibd->min_number;
2440 total_prob += ibd->probability;
2441 }
2442
2443 if (total_prob == 0) return; // No buildable industries.
2444
2445 /* Subtract forced industries from the number of industries available for construction. */
2446 total_amount = (total_amount <= force_build) ? 0 : total_amount - force_build;
2447
2448 /* Assign number of industries that should be aimed for, by using the probability as a weight. */
2449 while (total_amount > 0) {
2450 uint32 r = RandomRange(total_prob);
2451 IndustryType it = 0;
2452 while (r >= this->builddata[it].probability) {
2453 r -= this->builddata[it].probability;
2454 it++;
2455 assert(it < NUM_INDUSTRYTYPES);
2456 }
2457 assert(this->builddata[it].probability > 0);
2458 this->builddata[it].target_count++;
2459 total_amount--;
2460 }
2461 }
2462
2463 /**
2464 * Try to create a random industry, during gameplay
2465 */
TryBuildNewIndustry()2466 void IndustryBuildData::TryBuildNewIndustry()
2467 {
2468 this->SetupTargetCount();
2469
2470 int missing = 0; // Number of industries that need to be build.
2471 uint count = 0; // Number of industry types eligible for build.
2472 uint32 total_prob = 0; // Sum of probabilities.
2473 IndustryType forced_build = NUM_INDUSTRYTYPES; // Industry type that should be forcibly build.
2474 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2475 int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
2476 missing += difference;
2477 if (this->builddata[it].wait_count > 0) continue; // This type may not be built now.
2478 if (difference > 0) {
2479 if (Industry::GetIndustryTypeCount(it) == 0 && this->builddata[it].min_number > 0) {
2480 /* An industry that should exist at least once, is not available. Force it, trying the most needed one first. */
2481 if (forced_build == NUM_INDUSTRYTYPES ||
2482 difference > this->builddata[forced_build].target_count - Industry::GetIndustryTypeCount(forced_build)) {
2483 forced_build = it;
2484 }
2485 }
2486 total_prob += difference;
2487 count++;
2488 }
2489 }
2490
2491 if (EconomyIsInRecession() || (forced_build == NUM_INDUSTRYTYPES && (missing <= 0 || total_prob == 0))) count = 0; // Skip creation of an industry.
2492
2493 if (count >= 1) {
2494 /* If not forced, pick a weighted random industry to build.
2495 * For the case that count == 1, there is no need to draw a random number. */
2496 IndustryType it;
2497 if (forced_build != NUM_INDUSTRYTYPES) {
2498 it = forced_build;
2499 } else {
2500 /* Non-forced, select an industry type to build (weighted random). */
2501 uint32 r = 0; // Initialized to silence the compiler.
2502 if (count > 1) r = RandomRange(total_prob);
2503 for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
2504 if (this->builddata[it].wait_count > 0) continue; // Type may not be built now.
2505 int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
2506 if (difference <= 0) continue; // Too many of this kind.
2507 if (count == 1) break;
2508 if (r < (uint)difference) break;
2509 r -= difference;
2510 }
2511 assert(it < NUM_INDUSTRYTYPES && this->builddata[it].target_count > Industry::GetIndustryTypeCount(it));
2512 }
2513
2514 /* Try to create the industry. */
2515 const Industry *ind = PlaceIndustry(it, IACT_RANDOMCREATION, false);
2516 if (ind == nullptr) {
2517 this->builddata[it].wait_count = this->builddata[it].max_wait + 1; // Compensate for decrementing below.
2518 this->builddata[it].max_wait = std::min(1000, this->builddata[it].max_wait + 2);
2519 } else {
2520 AdvertiseIndustryOpening(ind);
2521 this->builddata[it].max_wait = std::max(this->builddata[it].max_wait / 2, 1); // Reduce waiting time of the industry type.
2522 }
2523 }
2524
2525 /* Decrement wait counters. */
2526 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2527 if (this->builddata[it].wait_count > 0) this->builddata[it].wait_count--;
2528 }
2529 }
2530
2531 /**
2532 * Protects an industry from closure if the appropriate flags and conditions are met
2533 * INDUSTRYBEH_CANCLOSE_LASTINSTANCE must be set (which, by default, it is not) and the
2534 * count of industries of this type must one (or lower) in order to be protected
2535 * against closure.
2536 * @param type IndustryType been queried
2537 * @result true if protection is on, false otherwise (except for oil wells)
2538 */
CheckIndustryCloseDownProtection(IndustryType type)2539 static bool CheckIndustryCloseDownProtection(IndustryType type)
2540 {
2541 const IndustrySpec *indspec = GetIndustrySpec(type);
2542
2543 /* oil wells (or the industries with that flag set) are always allowed to closedown */
2544 if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
2545 return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && Industry::GetIndustryTypeCount(type) <= 1;
2546 }
2547
2548 /**
2549 * Can given cargo type be accepted or produced by the industry?
2550 * @param cargo: Cargo type
2551 * @param ind: Industry
2552 * @param *c_accepts: Pointer to boolean for acceptance of cargo
2553 * @param *c_produces: Pointer to boolean for production of cargo
2554 * @return: \c *c_accepts is set when industry accepts the cargo type,
2555 * \c *c_produces is set when the industry produces the cargo type
2556 */
CanCargoServiceIndustry(CargoID cargo,Industry * ind,bool * c_accepts,bool * c_produces)2557 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
2558 {
2559 if (cargo == CT_INVALID) return;
2560
2561 /* Check for acceptance of cargo */
2562 for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
2563 if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) {
2564 *c_accepts = true;
2565 break;
2566 }
2567 }
2568
2569 /* Check for produced cargo */
2570 for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
2571 if (cargo == ind->produced_cargo[j]) {
2572 *c_produces = true;
2573 break;
2574 }
2575 }
2576 }
2577
2578 /**
2579 * Compute who can service the industry.
2580 *
2581 * Here, 'can service' means that they have trains and stations close enough
2582 * to the industry with the right cargo type and the right orders (ie has the
2583 * technical means).
2584 *
2585 * @param ind: Industry being investigated.
2586 *
2587 * @return: 0 if nobody can service the industry, 2 if the local company can
2588 * service the industry, and 1 otherwise (only competitors can service the
2589 * industry)
2590 */
WhoCanServiceIndustry(Industry * ind)2591 static int WhoCanServiceIndustry(Industry *ind)
2592 {
2593 if (ind->stations_near.size() == 0) return 0; // No stations found at all => nobody services
2594
2595 int result = 0;
2596 for (const Vehicle *v : Vehicle::Iterate()) {
2597 /* Is it worthwhile to try this vehicle? */
2598 if (v->owner != _local_company && result != 0) continue;
2599
2600 /* Check whether it accepts the right kind of cargo */
2601 bool c_accepts = false;
2602 bool c_produces = false;
2603 if (v->type == VEH_TRAIN && v->IsFrontEngine()) {
2604 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
2605 CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
2606 }
2607 } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
2608 CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
2609 } else {
2610 continue;
2611 }
2612 if (!c_accepts && !c_produces) continue; // Wrong cargo
2613
2614 /* Check orders of the vehicle.
2615 * We cannot check the first of shared orders only, since the first vehicle in such a chain
2616 * may have a different cargo type.
2617 */
2618 for (const Order *o : v->Orders()) {
2619 if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
2620 /* Vehicle visits a station to load or unload */
2621 Station *st = Station::Get(o->GetDestination());
2622 assert(st != nullptr);
2623
2624 /* Same cargo produced by industry is dropped here => not serviced by vehicle v */
2625 if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
2626
2627 if (ind->stations_near.find(st) != ind->stations_near.end()) {
2628 if (v->owner == _local_company) return 2; // Company services industry
2629 result = 1; // Competitor services industry
2630 }
2631 }
2632 }
2633 }
2634 return result;
2635 }
2636
2637 /**
2638 * Report news that industry production has changed significantly
2639 *
2640 * @param ind: Industry with changed production
2641 * @param type: Cargo type that has changed
2642 * @param percent: Percentage of change (>0 means increase, <0 means decrease)
2643 */
ReportNewsProductionChangeIndustry(Industry * ind,CargoID type,int percent)2644 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
2645 {
2646 NewsType nt;
2647
2648 switch (WhoCanServiceIndustry(ind)) {
2649 case 0: nt = NT_INDUSTRY_NOBODY; break;
2650 case 1: nt = NT_INDUSTRY_OTHER; break;
2651 case 2: nt = NT_INDUSTRY_COMPANY; break;
2652 default: NOT_REACHED();
2653 }
2654 SetDParam(2, abs(percent));
2655 SetDParam(0, CargoSpec::Get(type)->name);
2656 SetDParam(1, ind->index);
2657 AddIndustryNewsItem(
2658 percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
2659 nt,
2660 ind->index
2661 );
2662 }
2663
2664 static const uint PERCENT_TRANSPORTED_60 = 153;
2665 static const uint PERCENT_TRANSPORTED_80 = 204;
2666
2667 /**
2668 * Change industry production or do closure
2669 * @param i Industry for which changes are performed
2670 * @param monthly true if it's the monthly call, false if it's the random call
2671 */
ChangeIndustryProduction(Industry * i,bool monthly)2672 static void ChangeIndustryProduction(Industry *i, bool monthly)
2673 {
2674 StringID str = STR_NULL;
2675 bool closeit = false;
2676 const IndustrySpec *indspec = GetIndustrySpec(i->type);
2677 bool standard = false;
2678 bool suppress_message = false;
2679 bool recalculate_multipliers = false; ///< reinitialize production_rate to match prod_level
2680 /* use original economy for industries using production related callbacks */
2681 bool original_economy = indspec->UsesOriginalEconomy();
2682 byte div = 0;
2683 byte mul = 0;
2684 int8 increment = 0;
2685
2686 bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
2687 if (callback_enabled) {
2688 uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
2689 if (res != CALLBACK_FAILED) { // failed callback means "do nothing"
2690 suppress_message = HasBit(res, 7);
2691 /* Get the custom message if any */
2692 if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
2693 res = GB(res, 0, 4);
2694 switch (res) {
2695 default: NOT_REACHED();
2696 case 0x0: break; // Do nothing, but show the custom message if any
2697 case 0x1: div = 1; break; // Halve industry production. If production reaches the quarter of the default, the industry is closed instead.
2698 case 0x2: mul = 1; break; // Double industry production if it hasn't reached eight times of the original yet.
2699 case 0x3: closeit = true; break; // The industry announces imminent closure, and is physically removed from the map next month.
2700 case 0x4: standard = true; break; // Do the standard random production change as if this industry was a primary one.
2701 case 0x5: case 0x6: case 0x7: // Divide production by 4, 8, 16
2702 case 0x8: div = res - 0x3; break; // Divide production by 32
2703 case 0x9: case 0xA: case 0xB: // Multiply production by 4, 8, 16
2704 case 0xC: mul = res - 0x7; break; // Multiply production by 32
2705 case 0xD: // decrement production
2706 case 0xE: // increment production
2707 increment = res == 0x0D ? -1 : 1;
2708 break;
2709 case 0xF: // Set production to third byte of register 0x100
2710 i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
2711 recalculate_multipliers = true;
2712 break;
2713 }
2714 }
2715 } else {
2716 if (monthly == original_economy) return;
2717 if (!original_economy && _settings_game.economy.type == ET_FROZEN) return;
2718 if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
2719 }
2720
2721 if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
2722 /* decrease or increase */
2723 bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
2724
2725 if (original_economy) {
2726 if (only_decrease || Chance16(1, 3)) {
2727 /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */
2728 if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
2729 mul = 1; // Increase production
2730 } else {
2731 div = 1; // Decrease production
2732 }
2733 }
2734 } else if (_settings_game.economy.type == ET_SMOOTH) {
2735 closeit = !(i->ctlflags & (INDCTL_NO_CLOSURE | INDCTL_NO_PRODUCTION_DECREASE));
2736 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
2737 if (i->produced_cargo[j] == CT_INVALID) continue;
2738 uint32 r = Random();
2739 int old_prod, new_prod, percent;
2740 /* If over 60% is transported, mult is 1, else mult is -1. */
2741 int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
2742
2743 new_prod = old_prod = i->production_rate[j];
2744
2745 /* For industries with only_decrease flags (temperate terrain Oil Wells),
2746 * the multiplier will always be -1 so they will only decrease. */
2747 if (only_decrease) {
2748 mult = -1;
2749 /* For normal industries, if over 60% is transported, 33% chance for decrease.
2750 * Bonus for very high station ratings (over 80%): 16% chance for decrease. */
2751 } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
2752 mult *= -1;
2753 }
2754
2755 /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change,
2756 * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */
2757 if (Chance16I(1, 22, r >> 16)) {
2758 new_prod += mult * (std::max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
2759 }
2760
2761 /* Prevent production to overflow or Oil Rig passengers to be over-"produced" */
2762 new_prod = Clamp(new_prod, 1, 255);
2763 if (i->produced_cargo[j] == CT_PASSENGERS && !(indspec->behaviour & INDUSTRYBEH_NO_PAX_PROD_CLAMP)) {
2764 new_prod = Clamp(new_prod, 0, 16);
2765 }
2766
2767 /* If override flags are set, prevent actually changing production if any was decided on */
2768 if ((i->ctlflags & INDCTL_NO_PRODUCTION_DECREASE) && new_prod < old_prod) continue;
2769 if ((i->ctlflags & INDCTL_NO_PRODUCTION_INCREASE) && new_prod > old_prod) continue;
2770
2771 /* Do not stop closing the industry when it has the lowest possible production rate */
2772 if (new_prod == old_prod && old_prod > 1) {
2773 closeit = false;
2774 continue;
2775 }
2776
2777 percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
2778 i->production_rate[j] = new_prod;
2779
2780 /* Close the industry when it has the lowest possible production rate */
2781 if (new_prod > 1) closeit = false;
2782
2783 if (abs(percent) >= 10) {
2784 ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
2785 }
2786 }
2787 }
2788 }
2789
2790 /* If override flags are set, prevent actually changing production if any was decided on */
2791 if ((i->ctlflags & INDCTL_NO_PRODUCTION_DECREASE) && (div > 0 || increment < 0)) return;
2792 if ((i->ctlflags & INDCTL_NO_PRODUCTION_INCREASE) && (mul > 0 || increment > 0)) return;
2793
2794 if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
2795 if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, original_economy ? 2 : 180)) {
2796 closeit = true;
2797 }
2798 }
2799
2800 /* Increase if needed */
2801 while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
2802 i->prod_level = std::min<int>(i->prod_level * 2, PRODLEVEL_MAXIMUM);
2803 recalculate_multipliers = true;
2804 if (str == STR_NULL) str = indspec->production_up_text;
2805 }
2806
2807 /* Decrease if needed */
2808 while (div-- != 0 && !closeit) {
2809 if (i->prod_level == PRODLEVEL_MINIMUM) {
2810 closeit = true;
2811 break;
2812 } else {
2813 i->prod_level = std::max<int>(i->prod_level / 2, PRODLEVEL_MINIMUM);
2814 recalculate_multipliers = true;
2815 if (str == STR_NULL) str = indspec->production_down_text;
2816 }
2817 }
2818
2819 /* Increase or Decreasing the production level if needed */
2820 if (increment != 0) {
2821 if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
2822 closeit = true;
2823 } else {
2824 i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
2825 recalculate_multipliers = true;
2826 }
2827 }
2828
2829 /* Recalculate production_rate
2830 * For non-smooth economy these should always be synchronized with prod_level */
2831 if (recalculate_multipliers) i->RecomputeProductionMultipliers();
2832
2833 /* Close if needed and allowed */
2834 if (closeit && !CheckIndustryCloseDownProtection(i->type) && !(i->ctlflags & INDCTL_NO_CLOSURE)) {
2835 i->prod_level = PRODLEVEL_CLOSURE;
2836 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
2837 str = indspec->closure_text;
2838 }
2839
2840 if (!suppress_message && str != STR_NULL) {
2841 NewsType nt;
2842 /* Compute news category */
2843 if (closeit) {
2844 nt = NT_INDUSTRY_CLOSE;
2845 AI::BroadcastNewEvent(new ScriptEventIndustryClose(i->index));
2846 Game::NewEvent(new ScriptEventIndustryClose(i->index));
2847 } else {
2848 switch (WhoCanServiceIndustry(i)) {
2849 case 0: nt = NT_INDUSTRY_NOBODY; break;
2850 case 1: nt = NT_INDUSTRY_OTHER; break;
2851 case 2: nt = NT_INDUSTRY_COMPANY; break;
2852 default: NOT_REACHED();
2853 }
2854 }
2855 /* Set parameters of news string */
2856 if (str > STR_LAST_STRINGID) {
2857 SetDParam(0, STR_TOWN_NAME);
2858 SetDParam(1, i->town->index);
2859 SetDParam(2, indspec->name);
2860 } else if (closeit) {
2861 SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
2862 SetDParam(1, i->town->index);
2863 SetDParam(2, indspec->name);
2864 } else {
2865 SetDParam(0, i->index);
2866 }
2867 /* and report the news to the user */
2868 if (closeit) {
2869 AddTileNewsItem(str, nt, i->location.tile + TileDiffXY(1, 1));
2870 } else {
2871 AddIndustryNewsItem(str, nt, i->index);
2872 }
2873 }
2874 }
2875
2876 /**
2877 * Daily handler for the industry changes
2878 * Taking the original map size of 256*256, the number of random changes was always of just one unit.
2879 * But it cannot be the same on smaller or bigger maps. That number has to be scaled up or down.
2880 * For small maps, it implies that less than one change per month is required, while on bigger maps,
2881 * it would be way more. The daily loop handles those changes.
2882 */
IndustryDailyLoop()2883 void IndustryDailyLoop()
2884 {
2885 _economy.industry_daily_change_counter += _economy.industry_daily_increment;
2886
2887 /* Bits 16-31 of industry_construction_counter contain the number of industries to change/create today,
2888 * the lower 16 bit are a fractional part that might accumulate over several days until it
2889 * is sufficient for an industry. */
2890 uint16 change_loop = _economy.industry_daily_change_counter >> 16;
2891
2892 /* Reset the active part of the counter, just keeping the "fractional part" */
2893 _economy.industry_daily_change_counter &= 0xFFFF;
2894
2895 if (change_loop == 0) {
2896 return; // Nothing to do? get out
2897 }
2898
2899 Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
2900
2901 /* perform the required industry changes for the day */
2902
2903 uint perc = 3; // Between 3% and 9% chance of creating a new industry.
2904 if ((_industry_builder.wanted_inds >> 16) > GetCurrentTotalNumberOfIndustries()) {
2905 perc = std::min(9u, perc + (_industry_builder.wanted_inds >> 16) - GetCurrentTotalNumberOfIndustries());
2906 }
2907 for (uint16 j = 0; j < change_loop; j++) {
2908 if (Chance16(perc, 100)) {
2909 _industry_builder.TryBuildNewIndustry();
2910 } else {
2911 Industry *i = Industry::GetRandom();
2912 if (i != nullptr) {
2913 ChangeIndustryProduction(i, false);
2914 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
2915 }
2916 }
2917 }
2918
2919 cur_company.Restore();
2920
2921 /* production-change */
2922 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_PRODUCTION_CHANGE);
2923 }
2924
IndustryMonthlyLoop()2925 void IndustryMonthlyLoop()
2926 {
2927 Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
2928
2929 _industry_builder.MonthlyLoop();
2930
2931 for (Industry *i : Industry::Iterate()) {
2932 UpdateIndustryStatistics(i);
2933 if (i->prod_level == PRODLEVEL_CLOSURE) {
2934 delete i;
2935 } else {
2936 ChangeIndustryProduction(i, true);
2937 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
2938 }
2939 }
2940
2941 cur_company.Restore();
2942
2943 /* production-change */
2944 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_PRODUCTION_CHANGE);
2945 }
2946
2947
InitializeIndustries()2948 void InitializeIndustries()
2949 {
2950 Industry::ResetIndustryCounts();
2951 _industry_sound_tile = 0;
2952
2953 _industry_builder.Reset();
2954 }
2955
2956 /** Verify whether the generated industries are complete, and warn the user if not. */
CheckIndustries()2957 void CheckIndustries()
2958 {
2959 int count = 0;
2960 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2961 if (Industry::GetIndustryTypeCount(it) > 0) continue; // Types of existing industries can be skipped.
2962
2963 bool force_at_least_one;
2964 uint32 chance = GetScaledIndustryGenerationProbability(it, &force_at_least_one);
2965 if (chance == 0 || !force_at_least_one) continue; // Types that are not available can be skipped.
2966
2967 const IndustrySpec *is = GetIndustrySpec(it);
2968 SetDParam(0, is->name);
2969 ShowErrorMessage(STR_ERROR_NO_SUITABLE_PLACES_FOR_INDUSTRIES, STR_ERROR_NO_SUITABLE_PLACES_FOR_INDUSTRIES_EXPLANATION, WL_WARNING);
2970
2971 count++;
2972 if (count >= 3) break; // Don't swamp the user with errors.
2973 }
2974 }
2975
2976 /**
2977 * Is an industry with the spec a raw industry?
2978 * @return true if it should be handled as a raw industry
2979 */
IsRawIndustry() const2980 bool IndustrySpec::IsRawIndustry() const
2981 {
2982 return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0;
2983 }
2984
2985 /**
2986 * Is an industry with the spec a processing industry?
2987 * @return true if it should be handled as a processing industry
2988 */
IsProcessingIndustry() const2989 bool IndustrySpec::IsProcessingIndustry() const
2990 {
2991 /* Lumber mills are neither raw nor processing */
2992 return (this->life_type & INDUSTRYLIFE_PROCESSING) != 0 &&
2993 (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
2994 }
2995
2996 /**
2997 * Get the cost for constructing this industry
2998 * @return the cost (inflation corrected etc)
2999 */
GetConstructionCost() const3000 Money IndustrySpec::GetConstructionCost() const
3001 {
3002 /* Building raw industries like secondary uses different price base */
3003 return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
3004 PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
3005 }
3006
3007 /**
3008 * Get the cost for removing this industry
3009 * Take note that the cost will always be zero for non-grf industries.
3010 * Only if the grf author did specified a cost will it be applicable.
3011 * @return the cost (inflation corrected etc)
3012 */
GetRemovalCost() const3013 Money IndustrySpec::GetRemovalCost() const
3014 {
3015 return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
3016 }
3017
3018 /**
3019 * Determines whether this industrytype uses standard/newgrf production changes.
3020 * @return true if original economy is used.
3021 */
UsesOriginalEconomy() const3022 bool IndustrySpec::UsesOriginalEconomy() const
3023 {
3024 return _settings_game.economy.type == ET_ORIGINAL ||
3025 HasBit(this->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || // production callbacks
3026 HasBit(this->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CHANGE) || HasBit(this->callback_mask, CBM_IND_PROD_CHANGE_BUILD); // production change callbacks
3027 }
3028
~IndustrySpec()3029 IndustrySpec::~IndustrySpec()
3030 {
3031 if (HasBit(this->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
3032 free(this->random_sounds);
3033 }
3034 }
3035
TerraformTile_Industry(TileIndex tile,DoCommandFlag flags,int z_new,Slope tileh_new)3036 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
3037 {
3038 if (AutoslopeEnabled()) {
3039 /* We imitate here TTDP's behaviour:
3040 * - Both new and old slope must not be steep.
3041 * - TileMaxZ must not be changed.
3042 * - Allow autoslope by default.
3043 * - Disallow autoslope if callback succeeds and returns non-zero.
3044 */
3045 Slope tileh_old = GetTileSlope(tile);
3046 /* TileMaxZ must not be changed. Slopes must not be steep. */
3047 if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
3048 const IndustryGfx gfx = GetIndustryGfx(tile);
3049 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
3050
3051 /* Call callback 3C 'disable autosloping for industry tiles'. */
3052 if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
3053 /* If the callback fails, allow autoslope. */
3054 uint16 res = GetIndustryTileCallback(CBID_INDTILE_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
3055 if (res == CALLBACK_FAILED || !ConvertBooleanCallback(itspec->grf_prop.grffile, CBID_INDTILE_AUTOSLOPE, res)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
3056 } else {
3057 /* allow autoslope */
3058 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
3059 }
3060 }
3061 }
3062 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
3063 }
3064
3065 extern const TileTypeProcs _tile_type_industry_procs = {
3066 DrawTile_Industry, // draw_tile_proc
3067 GetSlopePixelZ_Industry, // get_slope_z_proc
3068 ClearTile_Industry, // clear_tile_proc
3069 AddAcceptedCargo_Industry, // add_accepted_cargo_proc
3070 GetTileDesc_Industry, // get_tile_desc_proc
3071 GetTileTrackStatus_Industry, // get_tile_track_status_proc
3072 ClickTile_Industry, // click_tile_proc
3073 AnimateTile_Industry, // animate_tile_proc
3074 TileLoop_Industry, // tile_loop_proc
3075 ChangeTileOwner_Industry, // change_tile_owner_proc
3076 nullptr, // add_produced_cargo_proc
3077 nullptr, // vehicle_enter_tile_proc
3078 GetFoundation_Industry, // get_foundation_proc
3079 TerraformTile_Industry, // terraform_tile_proc
3080 };
3081
operator ()(const Industry * lhs,const Industry * rhs) const3082 bool IndustryCompare::operator() (const Industry *lhs, const Industry *rhs) const
3083 {
3084 return lhs->index < rhs->index;
3085 }
3086