OpenTTD Source: src/industry_cmd.cpp Source File

OpenTTD Source 1.11.0-RC1

industry_cmd.cpp
Go to the documentation of this file.
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
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;
66
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
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
121 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
122 {
123 assert(thistype < NUM_INDUSTRYTYPES);
124 return &_industry_specs[thistype];
125 }
126
135 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
136 {
137 assert(gfx < INVALID_INDUSTRYTILE);
138 return &_industry_tile_specs[gfx];
139 }
140
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 TILE_AREA_LOOP(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 TILE_AREA_LOOP(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 TILE_AREA_LOOP(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 DeleteWindowById(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
208 void Industry::PostDestructor(size_t index)
209 {
210 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_FORCE_REBUILD);
211 }
212
213
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
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
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
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
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
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
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
386 static int GetSlopePixelZ_Industry(TileIndex tile, uint x, uint y)
387 {
388 return GetTileMaxPixelZ(tile);
389 }
390
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
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
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
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
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
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
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
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
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
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
936 static bool ClickTile_Industry(TileIndex tile)
937 {
938 ShowIndustryViewWindow(GetIndustryIndex(tile));
939 return true;
940 }
941
942 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
943 {
944 return 0;
945 }
946
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
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
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
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
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 TILE_AREA_LOOP(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 TILE_AREA_LOOP(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
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
1092 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
1093 {
1094 if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) {
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
1115 static void ChopLumberMillTrees(Industry *i)
1116 {
1117 /* We only want to cut trees if all tiles are completed. */
1118 TILE_AREA_LOOP(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
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
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
1222 static CommandCost CheckNewIndustry_NULL(TileIndex tile)
1223 {
1224 return CommandCost();
1225 }
1226
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
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
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
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
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
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
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
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
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
1368 typedef CommandCost CheckNewIndustryProc(TileIndex tile);
1369
1371 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
1372 CheckNewIndustry_NULL,
1373 CheckNewIndustry_Forest,
1374 CheckNewIndustry_OilRefinery,
1375 CheckNewIndustry_Farm,
1376 CheckNewIndustry_Plantation,
1377 CheckNewIndustry_Water,
1378 CheckNewIndustry_Lumbermill,
1379 CheckNewIndustry_BubbleGen,
1380 CheckNewIndustry_OilRig,
1381 };
1382
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
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
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
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
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 TILE_AREA_LOOP(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
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 TILE_AREA_LOOP(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 TILE_AREA_LOOP(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
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 TILE_AREA_LOOP(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
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
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
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
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
1985 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *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
2077 CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *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 (!StrEmpty(text)) 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
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 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
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
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
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
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
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
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
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
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
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
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
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
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
2389 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
2398 void ClearAllIndustryCachedNames()
2399 {
2400 for (Industry *ind : Industry::Iterate()) {
2401 ind->cached_name.clear();
2402 }
2403 }
2404
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
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
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
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
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
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
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
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;
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
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
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
2948 void InitializeIndustries()
2949 {
2950 Industry::ResetIndustryCounts();
2951 _industry_sound_tile = 0;
2952
2953 _industry_builder.Reset();
2954 }
2955
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
2980 bool IndustrySpec::IsRawIndustry() const
2981 {
2982 return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0;
2983 }
2984
2989 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
3000 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
3013 Money IndustrySpec::GetRemovalCost() const
3014 {
3015 return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
3016 }
3017
3022 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
3029 IndustrySpec::~IndustrySpec()
3030 {
3031 if (HasBit(this->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
3032 free(this->random_sounds);
3033 }
3034 }
3035
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
3082 bool IndustryCompare::operator() (const Industry *lhs, const Industry *rhs) const
3083 {
3084 return lhs->index < rhs->index;
3085 }
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
game.hpp
INDUSTRYBEH_ONLY_INTOWN
@ INDUSTRYBEH_ONLY_INTOWN
can only be built in towns (arctic/tropic banks, water tower)
Definition: industrytype.h:67
CheckScaledDistanceFromEdge
static bool CheckScaledDistanceFromEdge(TileIndex tile, uint maxdist)
Check if a tile is within a distance from map edges, scaled by map dimensions independently.
Definition: industry_cmd.cpp:1249
NT_INDUSTRY_NOBODY
@ NT_INDUSTRY_NOBODY
Other industry production changes.
Definition: news_type.h:31
TileInfo::z
int z
Height.
Definition: tile_cmd.h:47
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:41
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:44
CmdBuildIndustry
CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Build/Fund an industry.
Definition: industry_cmd.cpp:1985
DeleteNewGRFInspectWindow
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Delete inspect window for a given feature and index.
Definition: newgrf_debug_gui.cpp:734
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition: tile_map.cpp:100
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition: direction_type.h:80
TileDesc::grf
const char * grf
newGRF used for the tile contents
Definition: tile_cmd.h:61
PRODLEVEL_MINIMUM
@ PRODLEVEL_MINIMUM
below this level, the industry is set to be closing
Definition: industry.h:31
CLEAR_SNOW
@ CLEAR_SNOW
0-3
Definition: clear_map.h:24
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
TROPICZONE_DESERT
@ TROPICZONE_DESERT
Tile is desert.
Definition: tile_type.h:71
NT_INDUSTRY_OPEN
@ NT_INDUSTRY_OPEN
Opening of industries.
Definition: news_type.h:26
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3321
Industry::owner
Owner owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition: industry.h:84
sound_func.h
ID_FUND_ONLY
@ ID_FUND_ONLY
The game does not build industries.
Definition: settings_type.h:52
CBM_IND_PRODUCTION_CARGO_ARRIVAL
@ CBM_IND_PRODUCTION_CARGO_ARRIVAL
call production callback when cargo arrives at the industry
Definition: newgrf_callbacks.h:349
NUM_INDUSTRYTYPES
static const IndustryType NUM_INDUSTRYTYPES
total number of industry types, new and old; limited to 240 because we need some special ids like INV...
Definition: industry_type.h:26
SND_30_TOFFEE_QUARRY
@ SND_30_TOFFEE_QUARRY
48 == 0x30 Industry animation: toffee quarry: drill
Definition: sound_type.h:87
Industry::this_month_production
uint16 this_month_production[INDUSTRY_NUM_OUTPUTS]
stats of this month's production per cargo
Definition: industry.h:76
Cheats::magic_bulldozer
Cheat magic_bulldozer
dynamite industries, objects
Definition: cheat_type.h:27
OUFB_UNLOAD
@ OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
Definition: order_type.h:54
Chance16
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
Definition: random_func.hpp:131
DoCommand
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
Definition: command.cpp:450
CheckNewIndustryProc
CommandCost CheckNewIndustryProc(TileIndex tile)
Industrytype check function signature.
Definition: industry_cmd.cpp:1368
SND_0B_MINE
@ SND_0B_MINE
9 == 0x09 Industry animation: coal/copper/gold mine: headgear
Definition: sound_type.h:48
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
ReportNewsProductionChangeIndustry
static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
Report news that industry production has changed significantly.
Definition: industry_cmd.cpp:2644
INDUSTRYBEH_CARGOTYPES_UNLIMITED
@ INDUSTRYBEH_CARGOTYPES_UNLIMITED
Allow produced/accepted cargoes callbacks to supply more than 2 and 3 types.
Definition: industrytype.h:82
IndustrySpec::UsesOriginalEconomy
bool UsesOriginalEconomy() const
Determines whether this industrytype uses standard/newgrf production changes.
Definition: industry_cmd.cpp:3022
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:57
SND_2B_TOY_FACTORY_2
@ SND_2B_TOY_FACTORY_2
43 == 0x2B Industry animation: toy factory (2): stamp product
Definition: sound_type.h:82
DeleteIndustryNews
void DeleteIndustryNews(IndustryID iid)
Remove news regarding given industry.
Definition: news_gui.cpp:938
NT_INDUSTRY_CLOSE
@ NT_INDUSTRY_CLOSE
Closing of industries.
Definition: news_type.h:27
GameSettings::station
StationSettings station
settings related to station management
Definition: settings_type.h:571
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3220
AdvertiseIndustryOpening
static void AdvertiseIndustryOpening(const Industry *ind)
Advertise about a new industry opening.
Definition: industry_cmd.cpp:1676
water.h
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:141
SetIndustryGfx
static void SetIndustryGfx(TileIndex t, IndustryGfx gfx)
Set the industry graphics ID for the given industry tile.
Definition: industry_map.h:149
ID_VERY_LOW
@ ID_VERY_LOW
Very few industries at game start.
Definition: settings_type.h:54
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
EV_BUBBLE
@ EV_BUBBLE
Bubble of bubble generator (industry).
Definition: effectvehicle_func.h:26
INDCTL_NONE
@ INDCTL_NONE
No flags in effect.
Definition: industry.h:49
GetIndustryGfx
static IndustryGfx GetIndustryGfx(TileIndex t)
Get the industry graphics ID for the given industry tile.
Definition: industry_map.h:137
HasTileWaterClass
static bool HasTileWaterClass(TileIndex t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:95
command_func.h
INDUSTRYBEH_AFTER_1960
@ INDUSTRYBEH_AFTER_1960
can only be built after 1960 (oil rigs)
Definition: industrytype.h:72
TileInfo::x
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:43
EV_CHIMNEY_SMOKE
@ EV_CHIMNEY_SMOKE
Smoke of power plant (industry).
Definition: effectvehicle_func.h:17
Pool::PoolItem<&_industry_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:340
IAT_TILELOOP
@ IAT_TILELOOP
Trigger in the periodic tile loop.
Definition: newgrf_animation_type.h:39
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
INDCTL_NO_CLOSURE
@ INDCTL_NO_CLOSURE
Industry can not close regardless of production level or time since last delivery.
Definition: industry.h:57
INDUSTRYBEH_NO_PAX_PROD_CLAMP
@ INDUSTRYBEH_NO_PAX_PROD_CLAMP
Do not clamp production of passengers. (smooth economy only)
Definition: industrytype.h:83
IndustryBuildData::builddata
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:229
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Definition: town_cmd.cpp:3595
CheckIndustryCloseDownProtection
static bool CheckIndustryCloseDownProtection(IndustryType type)
Protects an industry from closure if the appropriate flags and conditions are met INDUSTRYBEH_CANCLOS...
Definition: industry_cmd.cpp:2539
WL_WARNING
@ WL_WARNING
Other information.
Definition: error.h:23
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:42
Industry::this_month_transported
uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS]
stats of this month's transport per cargo
Definition: industry.h:77
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:305
Backup
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
SpriteLayoutPaletteTransform
static PaletteID SpriteLayoutPaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOUR to a palette entry of a sprite layou...
Definition: sprite.h:149
GetTreeGrowth
static uint GetTreeGrowth(TileIndex t)
Returns the tree growth status.
Definition: tree_map.h:181
company_base.h
IsTransparencySet
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:48
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition: vehicle_base.h:1077
EXPENSES_OTHER
@ EXPENSES_OTHER
Other expenses.
Definition: economy_type.h:170
OUFB_TRANSFER
@ OUFB_TRANSFER
Transfer all cargo onto the platform.
Definition: order_type.h:55
TileDesc::owner
Owner owner[4]
Name of the owner(s)
Definition: tile_cmd.h:53
IndustrySpec::GetRemovalCost
Money GetRemovalCost() const
Get the cost for removing this industry Take note that the cost will always be zero for non-grf indus...
Definition: industry_cmd.cpp:3013
CheckIndustries
void CheckIndustries()
Verify whether the generated industries are complete, and warn the user if not.
Definition: industry_cmd.cpp:2957
SND_2A_TOY_FACTORY_3
@ SND_2A_TOY_FACTORY_3
42 == 0x2A Industry animation: toy factory (3): eject product
Definition: sound_type.h:81
CBID_INDUSTRY_PRODUCTION_CHANGE
@ CBID_INDUSTRY_PRODUCTION_CHANGE
Called on production changes, so it can be adjusted.
Definition: newgrf_callbacks.h:111
Station
Station data structure.
Definition: station_base.h:450
Randomizer::Next
uint32 Next()
Generate the next pseudo random number.
Definition: random_func.cpp:31
Economy::industry_daily_change_counter
uint32 industry_daily_change_counter
Bits 31-16 are number of industry to be performed, 15-0 are fractional collected daily.
Definition: economy_type.h:34
PRODLEVEL_CLOSURE
@ PRODLEVEL_CLOSURE
signal set to actually close the industry
Definition: industry.h:30
IndustrySpec::GetConstructionCost
Money GetConstructionCost() const
Get the cost for constructing this industry.
Definition: industry_cmd.cpp:3000
CargoPacket::InvalidateAllFrom
static void InvalidateAllFrom(SourceType src_type, SourceID src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
Definition: cargopacket.cpp:127
CBID_INDTILE_CARGO_ACCEPTANCE
@ CBID_INDTILE_CARGO_ACCEPTANCE
Called to query the cargo acceptance of the industry tile.
Definition: newgrf_callbacks.h:117
ChopLumberMillTrees
static void ChopLumberMillTrees(Industry *i)
Perform a circular search around the Lumber Mill in order to find trees to cut.
Definition: industry_cmd.cpp:1115
IndustryAction::SetText
@ SetText
Set additional text.
Industry::last_month_transported
uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS]
total units transported per cargo in the last full month
Definition: industry.h:80
SetIndustryConstructionStage
static void SetIndustryConstructionStage(TileIndex tile, byte value)
Sets the industry construction stage of the specified tile.
Definition: industry_map.h:112
DiagDirToAxis
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
IAT_INDUSTRY_TICK
@ IAT_INDUSTRY_TICK
Trigger every tick.
Definition: newgrf_animation_type.h:40
IndustryTileSpec::anim_state
bool anim_state
When true, the tile has to be drawn using the animation state instead of the construction state.
Definition: industrytype.h:166
GetIndustryType
IndustryType GetIndustryType(TileIndex tile)
Retrieve the type for this industry.
Definition: industry_cmd.cpp:104
SND_0C_POWER_STATION
@ SND_0C_POWER_STATION
10 == 0x0A Industry animation: power station: spark
Definition: sound_type.h:49
CBM_INDT_SHAPE_CHECK
@ CBM_INDT_SHAPE_CHECK
decides slope suitability
Definition: newgrf_callbacks.h:373
_industry_builder
IndustryBuildData _industry_builder
In-game manager of industries.
Definition: industry_cmd.cpp:65
WC_INDUSTRY_VIEW
@ WC_INDUSTRY_VIEW
Industry view; Window numbers:
Definition: window_type.h:356
IndustrySpec::removal_cost_multiplier
uint32 removal_cost_multiplier
Base removal cost multiplier.
Definition: industrytype.h:110
IsIndustryCompleted
static bool IsIndustryCompleted(TileIndex t)
Is this industry tile fully built?
Definition: industry_map.h:75
TREE_GROUND_SHORE
@ TREE_GROUND_SHORE
shore
Definition: tree_map.h:56
Pool::PoolItem<&_industry_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:117
CmdIndustryCtrl
CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Change industry properties.
Definition: industry_cmd.cpp:2077
IACT_RANDOMCREATION
@ IACT_RANDOMCREATION
during creation of random ingame industry
Definition: newgrf_industries.h:84
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:81
Industry::was_cargo_delivered
byte was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station....
Definition: industry.h:87
TROPICZONE_RAINFOREST
@ TROPICZONE_RAINFOREST
Rainforest tile.
Definition: tile_type.h:72
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition: gfx_type.h:23
ClampU
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:122
Industry::produced_cargo_waiting
uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS]
amount of cargo produced per cargo
Definition: industry.h:71
GameSettings::difficulty
DifficultySettings difficulty
settings related to the difficulty
Definition: settings_type.h:559
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121
Industry::last_month_production
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]
total units produced per cargo in the last full month
Definition: industry.h:79
CheckNewIndustry_Farm
static CommandCost CheckNewIndustry_Farm(TileIndex tile)
Check the conditions of CHECK_FARM (Industry should be below snow-line in arctic).
Definition: industry_cmd.cpp:1301
IsClearGround
static bool IsClearGround(TileIndex t, ClearGround ct)
Set the type of clear tile.
Definition: clear_map.h:71
INDUSTRYBEH_DONT_INCR_PROD
@ INDUSTRYBEH_DONT_INCR_PROD
do not increase production (oil wells) in the temperate climate
Definition: industrytype.h:70
DrawIndustryAnimationStruct::x
int x
coordinate x of the first image offset
Definition: industry_land.h:19
WaterClass
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
SetDParamX
static void SetDParamX(uint64 *s, uint n, uint64 v)
Set a string parameter v at index n in a given array s.
Definition: strings_func.h:189
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:13
IndustryAction::SetExclusiveConsumer
@ SetExclusiveConsumer
Set exclusive consumer.
CBM_INDT_ACCEPT_CARGO
@ CBM_INDT_ACCEPT_CARGO
decides accepted types
Definition: newgrf_callbacks.h:372
build_industry.h
Industry::RecomputeProductionMultipliers
void RecomputeProductionMultipliers()
Recompute production_rate for current prod_level.
Definition: industry_cmd.cpp:2378
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
Industry::construction_type
uint8 construction_type
Way the industry was constructed (.
Definition: industry.h:96
TileInfo::y
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:44
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
GetRegister
static uint32 GetRegister(uint i)
Gets the value of a so-called newgrf "register".
Definition: newgrf_spritegroup.h:29
Town::xy
TileIndex xy
town center tile
Definition: town.h:51
GWP_INDUSTRY
@ GWP_INDUSTRY
Generate industries.
Definition: genworld.h:70
DC_NO_WATER
@ DC_NO_WATER
don't allow building on water
Definition: command_type.h:351
CBID_INDTILE_DRAW_FOUNDATIONS
@ CBID_INDTILE_DRAW_FOUNDATIONS
Called to determine the type (if any) of foundation to draw for industry tile.
Definition: newgrf_callbacks.h:135
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:49
newgrf_debug.h
town.h
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
CBM_IND_PRODUCTION_256_TICKS
@ CBM_IND_PRODUCTION_256_TICKS
call production callback every 256 ticks
Definition: newgrf_callbacks.h:350
IsTileForestIndustry
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
Definition: industry_cmd.cpp:962
OrthogonalTileArea::Add
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition: tilearea.cpp:43
effectvehicle_base.h
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:82
RandomRange
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
Chance16I
static bool Chance16I(const uint a, const uint b, const uint32 r)
Checks if a given randomize-number is below a given probability.
Definition: random_func.hpp:112
Industry::construction_date
Date construction_date
Date of the construction of the industry.
Definition: industry.h:95
Industry::exclusive_consumer
Owner exclusive_consumer
Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
Definition: industry.h:100
TransportType
TransportType
Available types of transport.
Definition: transport_type.h:19
SND_2C_TOY_FACTORY_1
@ SND_2C_TOY_FACTORY_1
44 == 0x2C Industry animation: toy factory (1): conveyor belt
Definition: sound_type.h:83
clear_map.h
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
PRODLEVEL_DEFAULT
@ PRODLEVEL_DEFAULT
default level set when the industry is created
Definition: industry.h:32
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
Industry
Defines the internal data of a functional industry.
Definition: industry.h:66
INDUSTRY_TRIGGER_INDUSTRY_TICK
@ INDUSTRY_TRIGGER_INDUSTRY_TICK
The industry has been triggered via its tick.
Definition: newgrf_industrytiles.h:72
TILE_MASK
#define TILE_MASK(x)
'Wraps' the given tile to it is within the map.
Definition: map_func.h:26
ConvertBooleanCallback
bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
Definition: newgrf_commons.cpp:550
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:283
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Industry::ctlflags
IndustryControlFlags ctlflags
flags overriding standard behaviours
Definition: industry.h:88
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
CBID_INDUSTRY_OUTPUT_CARGO_TYPES
@ CBID_INDUSTRY_OUTPUT_CARGO_TYPES
Customize the output cargo types of a newly build industry.
Definition: newgrf_callbacks.h:221
MemCpyT
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
GFX_WATERTILE_SPECIALCHECK
@ GFX_WATERTILE_SPECIALCHECK
not really a tile, but rather a very special check
Definition: industry_map.h:54
CBID_INDTILE_ACCEPT_CARGO
@ CBID_INDTILE_ACCEPT_CARGO
Called to determine which cargoes an industry should accept.
Definition: newgrf_callbacks.h:120
TriggerIndustry
void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
Trigger a random trigger for all industry tiles.
Definition: newgrf_industrytiles.cpp:372
TileDesc
Tile description for the 'land area information' tool.
Definition: tile_cmd.h:51
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
CBID_INDUSTRY_SPECIAL_EFFECT
@ CBID_INDUSTRY_SPECIAL_EFFECT
Called to determine industry special effects.
Definition: newgrf_callbacks.h:174
CBM_INDT_AUTOSLOPE
@ CBM_INDT_AUTOSLOPE
decides allowance of autosloping
Definition: newgrf_callbacks.h:375
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
SetFence
static void SetFence(TileIndex t, DiagDirection side, uint h)
Sets the type of fence (and whether there is one) for the given border.
Definition: clear_map.h:240
genworld.h
Foundation
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
EnsureNoVehicleOnGround
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition: vehicle.cpp:539
INDUSTRYBEH_NOBUILT_MAPCREATION
@ INDUSTRYBEH_NOBUILT_MAPCREATION
Do not force one instance of this type to appear on map generation.
Definition: industrytype.h:80
IncreaseGeneratingWorldProgress
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
Definition: genworld_gui.cpp:1380
Industry::neutral_station
Station * neutral_station
Associated neutral station.
Definition: industry.h:69
FlatteningFoundation
static Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
CommandCost::Succeeded
bool Succeeded() const
Did this command succeed?
Definition: command_type.h:150
ResetIndustryConstructionStage
static void ResetIndustryConstructionStage(TileIndex tile)
Reset the construction stage counter of the industry, as well as the completion bit.
Definition: industry_map.h:187
object_base.h
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:372
IndustryDailyLoop
void IndustryDailyLoop()
Daily handler for the industry changes Taking the original map size of 256*256, the number of random ...
Definition: industry_cmd.cpp:2883
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:560
_coal_plant_sparks
static const DrawIndustryCoordinates _coal_plant_sparks[]
Movement of the sparks , only used for Power Station.
Definition: industry_land.h:948
effectvehicle_func.h
IndustryTileLayout
std::vector< IndustryTileLayoutTile > IndustryTileLayout
A complete tile layout for an industry is a list of tiles.
Definition: industrytype.h:102
ai.hpp
SND_2D_SUGAR_MINE_1
@ SND_2D_SUGAR_MINE_1
45 == 0x2D Industry animation: sugar mine (1): shaking sieve
Definition: sound_type.h:84
SLOPE_NW
@ SLOPE_NW
north and west corner are raised
Definition: slope_type.h:55
IndustryAction
IndustryAction
Definition: industry.h:36
TileInfo::tileh
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:45
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
GetIndustryProbabilityCallback
uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
Check with callback CBID_INDUSTRY_PROBABILITY whether the industry can be built.
Definition: newgrf_industries.cpp:565
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
IsTileOnWater
static bool IsTileOnWater(TileIndex t)
Tests if the tile was built on water.
Definition: water_map.h:130
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
INDUSTRYBEH_PLANT_ON_BUILT
@ INDUSTRYBEH_PLANT_ON_BUILT
Fields are planted around when built (all farms)
Definition: industrytype.h:69
ClearDockingTilesCheckingNeighbours
void ClearDockingTilesCheckingNeighbours(TileIndex tile)
Clear docking tile status from tiles around a removed dock, if the tile has no neighbours which would...
Definition: station_cmd.cpp:2620
GetStringWithArgs
char * GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition: strings.cpp:223
SoundSettings::ambient
bool ambient
Play ambient, industry and town sounds.
Definition: settings_type.h:201
IndustrySpec::closure_text
StringID closure_text
Message appearing when the industry closes.
Definition: industrytype.h:129
Industry::GetRandom
static Industry * GetRandom()
Return a random valid industry.
Definition: industry_cmd.cpp:218
IndustryAction::SetExclusiveSupplier
@ SetExclusiveSupplier
Set exclusive supplier.
IndustryTileSpec::special_flags
IndustryTileSpecialFlags special_flags
Bitmask of extra flags used by the tile.
Definition: industrytype.h:170
INDUSTRY_COMPLETED
static const int INDUSTRY_COMPLETED
final stage of industry construction.
Definition: industry_type.h:36
ToTileIndexDiff
static TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between to tiles from a TileIndexDiffC struct.
Definition: map_func.h:230
Pool::PoolItem<&_industry_pool >::GetPoolSize
static size_t GetPoolSize()
Returns first unused index.
Definition: pool_type.hpp:350
Vehicle::Orders
IterateWrapper Orders() const
Returns an iterable ensemble of orders of a vehicle.
Definition: vehicle_base.h:1034
Economy::industry_daily_increment
uint32 industry_daily_increment
The value which will increment industry_daily_change_counter. Computed value. NOSAVE.
Definition: economy_type.h:35
WATER_CLASS_INVALID
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:51
GetSlopeMaxZ
static int GetSlopeMaxZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition: slope_func.h:160
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition: direction_type.h:81
UpdateIndustryStatistics
static void UpdateIndustryStatistics(Industry *i)
Monthly update of industry statistics.
Definition: industry_cmd.cpp:2354
TownCache::population
uint32 population
Current population of people.
Definition: town.h:42
Industry::PostDestructor
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Definition: industry_cmd.cpp:208
CHECK_END
@ CHECK_END
End marker of the industry check procedures.
Definition: industrytype.h:49
GetAnimationFrame
static byte GetAnimationFrame(TileIndex t)
Get the current animation frame.
Definition: tile_map.h:250
SND_29_SUGAR_MINE_2
@ SND_29_SUGAR_MINE_2
41 == 0x29 Industry animation: sugar mine (2): shaking sieve
Definition: sound_type.h:80
IndustryTileSpec::acceptance
int8 acceptance[INDUSTRY_NUM_INPUTS]
Level of acceptance per cargo type (signed, may be negative!)
Definition: industrytype.h:158
CheckIfCanLevelIndustryPlatform
static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileLayout &layout, int type)
This function tries to flatten out the land below an industry, without damaging the surroundings too ...
Definition: industry_cmd.cpp:1560
AXIS_Y
@ AXIS_Y
The y axis.
Definition: direction_type.h:125
StationSettings::serve_neutral_industries
bool serve_neutral_industries
company stations can serve industries with attached neutral stations
Definition: settings_type.h:532
IndustryTypeBuildData::wait_count
uint16 wait_count
Number of turns to wait before trying to build again.
Definition: industry.h:218
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
IndustrySpec::IsRawIndustry
bool IsRawIndustry() const
Is an industry with the spec a raw industry?
Definition: industry_cmd.cpp:2980
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
WhoCanServiceIndustry
static int WhoCanServiceIndustry(Industry *ind)
Compute who can service the industry.
Definition: industry_cmd.cpp:2591
SetIndustryConstructionCounter
static void SetIndustryConstructionCounter(TileIndex tile, byte value)
Sets this industry tile's construction counter value.
Definition: industry_map.h:174
PlaceIndustry
static Industry * PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
Try to place the industry in the game.
Definition: industry_cmd.cpp:2228
EXPENSES_CONSTRUCTION
@ EXPENSES_CONSTRUCTION
Construction costs.
Definition: economy_type.h:158
Industry::stations_near
StationList stations_near
NOSAVE: List of nearby stations.
Definition: industry.h:91
TileAddWrap
TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
This function checks if we add addx/addy to tile, if we do wrap around the edges.
Definition: map.cpp:114
GetIndustryAnimationLoop
static byte GetIndustryAnimationLoop(TileIndex tile)
Get the animation loop number.
Definition: industry_map.h:199
IsSteepSlope
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
CommandCost
Common return value for all commands.
Definition: command_type.h:23
Industry::random
uint16 random
Random value used for randomisation of all kinds of things.
Definition: industry.h:103
GetSnowLine
byte GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:644
Industry::location
TileArea location
Location of the industry.
Definition: industry.h:67
IndustryAction::SetControlFlags
@ SetControlFlags
Set IndustryControlFlags.
IndustryTypeBuildData::Reset
void Reset()
Reset the entry.
Definition: industry_cmd.cpp:2266
SetClearCounter
static void SetClearCounter(TileIndex t, uint c)
Sets the counter used to advance to the next clear density/field type.
Definition: clear_map.h:144
cmd_helper.h
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
CheckNewIndustry_OilRefinery
static CommandCost CheckNewIndustry_OilRefinery(TileIndex tile)
Check the conditions of CHECK_REFINERY (Industry should be positioned near edge of the map).
Definition: industry_cmd.cpp:1270
NEW_INDUSTRYOFFSET
static const IndustryType NEW_INDUSTRYOFFSET
original number of industry types
Definition: industry_type.h:25
TILE_AREA_LOOP
#define TILE_AREA_LOOP(var, ta)
A loop which iterates over the tiles of a TileArea.
Definition: tilearea_type.h:232
TileHeight
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:29
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition: settings_type.h:580
CircularTileSearch
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
Function performing a search around a center tile and going outward, thus in circle.
Definition: map.cpp:258
CMD_TERRAFORM_LAND
@ CMD_TERRAFORM_LAND
terraform a tile
Definition: command_type.h:186
Industry::cached_name
std::string cached_name
NOSAVE: Cache of the resolved name of the industry.
Definition: industry.h:92
Industry::GetByTile
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition: industry.h:144
Pool::MAX_SIZE
static const size_t MAX_SIZE
Make template parameter accessible from outside.
Definition: pool_type.hpp:85
IndustrySpec::conflicting
IndustryType conflicting[3]
Industries this industry cannot be close to.
Definition: industrytype.h:112
Industry::exclusive_supplier
Owner exclusive_supplier
Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
Definition: industry.h:99
INDUSTRYBEH_PLANT_FIELDS
@ INDUSTRYBEH_PLANT_FIELDS
periodically plants fields around itself (temp and arctic farms)
Definition: industrytype.h:63
GetScaledIndustryGenerationProbability
static uint32 GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
Compute the appearance probability for an industry during map creation.
Definition: industry_cmd.cpp:2153
Industry::type
IndustryType type
type of industry.
Definition: industry.h:83
SND_36_LUMBER_MILL_3
@ SND_36_LUMBER_MILL_3
54 == 0x36 Industry animation: lumber mill (3): crashing tree
Definition: sound_type.h:93
IndustryTypeBuildData::max_wait
uint16 max_wait
Starting number of turns to wait (copied to wait_count).
Definition: industry.h:217
TileIndexDiff
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:154
autoslope.h
CBM_IND_PRODUCTION_CHANGE
@ CBM_IND_PRODUCTION_CHANGE
controls random production change
Definition: newgrf_callbacks.h:352
SND_2E_BUBBLE_GENERATOR
@ SND_2E_BUBBLE_GENERATOR
46 == 0x2E Industry animation: bubble generator (1): generate bubble
Definition: sound_type.h:85
WC_INDUSTRY_DIRECTORY
@ WC_INDUSTRY_DIRECTORY
Industry directory; Window numbers:
Definition: window_type.h:259
_cheats
Cheats _cheats
All the cheats.
Definition: cheat.cpp:16
IndustryBuildData
Data for managing the number and type of industries in the game.
Definition: industry.h:228
CBID_INDUSTRY_MONTHLYPROD_CHANGE
@ CBID_INDUSTRY_MONTHLYPROD_CHANGE
Called monthly on production changes, so it can be adjusted more frequently.
Definition: newgrf_callbacks.h:153
IndustrySpec::layouts
std::vector< IndustryTileLayout > layouts
List of possible tile layouts for the industry.
Definition: industrytype.h:108
IndustrySpec::accepts_cargo
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 accepted cargoes.
Definition: industrytype.h:121
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
OrthogonalTileArea::w
uint16 w
The width of the area.
Definition: tilearea_type.h:18
INDUSTRYTILE_NOANIM
static const IndustryGfx INDUSTRYTILE_NOANIM
flag to mark industry tiles as having no animation
Definition: industry_type.h:31
CBM_INDT_DRAW_FOUNDATIONS
@ CBM_INDT_DRAW_FOUNDATIONS
decides if default foundations need to be drawn
Definition: newgrf_callbacks.h:374
INVALID_INDUSTRYTILE
static const IndustryGfx INVALID_INDUSTRYTILE
one above amount is considered invalid
Definition: industry_type.h:34
Industry::produced_cargo
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]
16 production cargo slots
Definition: industry.h:70
IndustrySpec::number_of_sounds
uint8 number_of_sounds
Number of sounds available in the sounds array.
Definition: industrytype.h:135
CBM_IND_LOCATION
@ CBM_IND_LOCATION
check industry construction on given area
Definition: newgrf_callbacks.h:351
CBM_IND_INPUT_CARGO_TYPES
@ CBM_IND_INPUT_CARGO_TYPES
customize the cargoes the industry requires
Definition: newgrf_callbacks.h:360
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition: command_type.h:159
Industry::incoming_cargo_waiting
uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS]
incoming cargo waiting to be processed
Definition: industry.h:72
EV_COPPER_MINE_SMOKE
@ EV_COPPER_MINE_SMOKE
Smoke at copper mine.
Definition: effectvehicle_func.h:28
INDUSTRYBEH_BUILT_ONWATER
@ INDUSTRYBEH_BUILT_ONWATER
is built on water (oil rig)
Definition: industrytype.h:65
IndustrySpec::production_up_text
StringID production_up_text
Message appearing when the industry's production is increasing.
Definition: industrytype.h:130
IsInvisibilitySet
static bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:59
NewsType
NewsType
Type of news.
Definition: news_type.h:21
ST_INDUSTRY
@ ST_INDUSTRY
Source/destination is an industry.
Definition: cargo_type.h:147
CBID_INDUSTRY_INPUT_CARGO_TYPES
@ CBID_INDUSTRY_INPUT_CARGO_TYPES
Customize the input cargo types of a newly build industry.
Definition: newgrf_callbacks.h:218
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:16
PerformIndustryTileSlopeCheck
CommandCost PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, size_t layout_index, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
Check the slope of a tile of a new industry.
Definition: newgrf_industrytiles.cpp:230
ConstructionSettings::raw_industry_construction
uint8 raw_industry_construction
type of (raw) industry construction (none, "normal", prospecting)
Definition: settings_type.h:327
CLEAR_DESERT
@ CLEAR_DESERT
1,3
Definition: clear_map.h:25
IndustrySpec::minimal_cargo
byte minimal_cargo
minimum amount of cargo transported to the stations.
Definition: industrytype.h:120
Vehicle::IsFrontEngine
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:895
Industry::DecIndustryTypeCount
static void DecIndustryTypeCount(IndustryType type)
Decrement the count of industries for this type.
Definition: industry.h:168
DrawIndustryAnimationStruct::image_2
byte image_2
image offset 2
Definition: industry_land.h:21
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
ANIM_STATUS_NO_ANIMATION
static const uint8 ANIM_STATUS_NO_ANIMATION
There is no animation.
Definition: newgrf_animation_type.h:15
GetTropicZone
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:238
DeleteSubsidyWith
void DeleteSubsidyWith(SourceType type, SourceID index)
Delete the subsidies associated with a given cargo source type and id.
Definition: subsidy.cpp:149
SetupFarmFieldFence
static void SetupFarmFieldFence(TileIndex tile, int size, byte type, DiagDirection side)
Build farm field fence.
Definition: industry_cmd.cpp:1006
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:141
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition: settings_type.h:569
IndustryBuildData::MonthlyLoop
void MonthlyLoop()
Monthly update of industry build data.
Definition: industry_cmd.cpp:2286
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
PRODLEVEL_MAXIMUM
@ PRODLEVEL_MAXIMUM
the industry is running at full speed
Definition: industry.h:33
AI::BroadcastNewEvent
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
Definition: ai_core.cpp:259
_tick_counter
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
Definition: date.cpp:30
industry.h
industry_land.h
safeguards.h
GetNumberOfIndustries
static uint GetNumberOfIndustries()
Get wanted number of industries on the map.
Definition: industry_cmd.cpp:2203
HighestSnowLine
byte HighestSnowLine()
Get the highest possible snow line height, either variable or static.
Definition: landscape.cpp:658
SetIndustryIndexOfField
static void SetIndustryIndexOfField(TileIndex t, IndustryID i)
Set the industry (farm) that made the field.
Definition: clear_map.h:207
DistanceFromEdgeDir
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition: map.cpp:234
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
IndustryTileSpec::slopes_refused
Slope slopes_refused
slope pattern on which this tile cannot be built
Definition: industrytype.h:159
Industry::ResetIndustryCounts
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:186
GetTileSlope
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
DifficultySettings::industry_density
byte industry_density
The industry density.
Definition: settings_type.h:66
RandomTile
#define RandomTile()
Get a valid random tile.
Definition: map_func.h:435
DC_NO_TEST_TOWN_RATING
@ DC_NO_TEST_TOWN_RATING
town rating does not disallow you from building
Definition: command_type.h:353
INDUSTRYBEH_BEFORE_1950
@ INDUSTRYBEH_BEFORE_1950
can only be built before 1950 (oil wells)
Definition: industrytype.h:71
CheckNewIndustry_BubbleGen
static CommandCost CheckNewIndustry_BubbleGen(TileIndex tile)
Check the conditions of CHECK_BUBBLEGEN (Industry should be in low land).
Definition: industry_cmd.cpp:1355
AutoslopeEnabled
static bool AutoslopeEnabled()
Tests if autoslope is enabled for _current_company.
Definition: autoslope.h:44
IndustryBuildData::SetupTargetCount
void SetupTargetCount()
Decide how many industries of each type are needed.
Definition: industry_cmd.cpp:2421
CBM_IND_SPECIAL_EFFECT
@ CBM_IND_SPECIAL_EFFECT
control special effects
Definition: newgrf_callbacks.h:357
IsSuitableForFarmField
static bool IsSuitableForFarmField(TileIndex tile, bool allow_fields)
Check whether the tile can be replaced by a farm field.
Definition: industry_cmd.cpp:990
NUM_INDUSTRYTILES
static const IndustryGfx NUM_INDUSTRYTILES
total number of industry tiles, new and old
Definition: industry_type.h:33
IndustryAvailabilityCallType
IndustryAvailabilityCallType
From where has callback CBID_INDUSTRY_PROBABILITY been called.
Definition: newgrf_industries.h:82
FOUNDATION_NONE
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
error.h
SetIndustryAnimationLoop
static void SetIndustryAnimationLoop(TileIndex tile, byte count)
Set the animation loop number.
Definition: industry_map.h:211
SLOPE_SW
@ SLOPE_SW
south and west corner are raised
Definition: slope_type.h:56
GameCreationSettings::oil_refinery_limit
byte oil_refinery_limit
distance oil refineries allowed from map edge
Definition: settings_type.h:298
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
IndustryBuildData::TryBuildNewIndustry
void TryBuildNewIndustry()
Try to create a random industry, during gameplay.
Definition: industry_cmd.cpp:2466
INDUSTRYLIFE_BLACK_HOLE
@ INDUSTRYLIFE_BLACK_HOLE
Like power plants and banks.
Definition: industrytype.h:29
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
IndustrySpec::appear_creation
byte appear_creation[NUM_LANDSCAPE]
Probability of appearance during map creation.
Definition: industrytype.h:134
IACT_PROSPECTCREATION
@ IACT_PROSPECTCREATION
from the Fund/build using prospecting
Definition: newgrf_industries.h:86
IndustryTileSpec::anim_production
byte anim_production
Animation frame to start when goods are produced.
Definition: industrytype.h:160
DrawIndustryAnimationStruct::image_3
byte image_3
image offset 3
Definition: industry_land.h:22
date_func.h
TileDesc::dparam
uint64 dparam[2]
Parameters of the str string.
Definition: tile_cmd.h:62
stdafx.h
GetIndustryIndexOfField
static IndustryID GetIndustryIndexOfField(TileIndex t)
Get the industry (farm) that made the field.
Definition: clear_map.h:195
landscape.h
TileTypeProcs
Set of callback functions for performing tile operations of a given tile type.
Definition: tile_cmd.h:145
SoundFx
SoundFx
Sound effects from baseset.
Definition: sound_type.h:37
SND_38_LUMBER_MILL_1
@ SND_38_LUMBER_MILL_1
56 == 0x38 Industry animation: lumber mill (1): chainsaw
Definition: sound_type.h:95
ReleaseDisastersTargetingIndustry
void ReleaseDisastersTargetingIndustry(IndustryID i)
Marks all disasters targeting this industry in such a way they won't call Industry::Get(v->dest_tile)...
Definition: disaster_vehicle.cpp:938
IndustrySpec
Defines the data structure for constructing industry.
Definition: industrytype.h:107
GetIndustryConstructionStage
static byte GetIndustryConstructionStage(TileIndex tile)
Returns the industry construction stage of the specified tile.
Definition: industry_map.h:100
IndustryTileSpec::grf_prop
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:172
Cheat::value
bool value
tells if the bool cheat is active or not
Definition: cheat_type.h:18
CreateNewIndustry
static Industry * CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAvailabilityCallType creation_type)
Create a new industry of random layout.
Definition: industry_cmd.cpp:2134
viewport_func.h
Industry::text
std::string text
General text with additional information.
Definition: industry.h:101
TileLoop_Water
void TileLoop_Water(TileIndex tile)
Let a water tile floods its diagonal adjoining tiles called from tunnelbridge_cmd,...
Definition: water_cmd.cpp:1210
IACT_USERCREATION
@ IACT_USERCREATION
from the Fund/build window
Definition: newgrf_industries.h:85
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
DrawIndustryAnimationStruct
This is used to gather some data about animation drawing in the industry code Image_1-2-3 are in fact...
Definition: industry_land.h:18
TO_INDUSTRIES
@ TO_INDUSTRIES
industries
Definition: transparency.h:26
animated_tile_func.h
ICT_MAP_GENERATION
@ ICT_MAP_GENERATION
during random map creation
Definition: industrytype.h:56
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:660
GroundSpritePaletteTransform
static PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_COLOUR to a palette entry of a ground sprite.
Definition: sprite.h:168
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:19
Industry::last_month_pct_transported
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]
percentage transported per cargo in the last full month
Definition: industry.h:78
_industry_draw_tile_data
static const DrawBuildingsTileStruct _industry_draw_tile_data[NEW_INDUSTRYTILEOFFSET *4]
Structure for industry tiles drawing.
Definition: industry_land.h:51
DistanceMax
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition: map.cpp:189
MP_TREES
@ MP_TREES
Tile got trees.
Definition: tile_type.h:45
DrawFoundation
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Definition: landscape.cpp:470
_generating_world
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:60
CreateEffectVehicle
EffectVehicle * CreateEffectVehicle(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle at a particular location.
Definition: effectvehicle.cpp:594
IndustryTemporarilyRefusesCargo
bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
Check whether an industry temporarily refuses to accept a certain cargo.
Definition: newgrf_industries.cpp:676
Industry::town
Town * town
Nearest town.
Definition: industry.h:68
INDUSTRYLIFE_PROCESSING
@ INDUSTRYLIFE_PROCESSING
Like factories.
Definition: industrytype.h:32
CBM_IND_MONTHLYPROD_CHANGE
@ CBM_IND_MONTHLYPROD_CHANGE
controls monthly random production change
Definition: newgrf_callbacks.h:353
CBID_INDUSTRY_PROD_CHANGE_BUILD
@ CBID_INDUSTRY_PROD_CHANGE_BUILD
Called when industry is built to set initial production level.
Definition: newgrf_callbacks.h:278
CheckNewIndustry_Lumbermill
static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
Check the conditions of CHECK_LUMBERMILL (Industry should be in the rain forest).
Definition: industry_cmd.cpp:1342
IndustryTileSpec::accepts_cargo
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
Cargo accepted by this tile.
Definition: industrytype.h:157
string_func.h
IndustrySpec::enabled
bool enabled
entity still available (by default true).newgrf can disable it, though
Definition: industrytype.h:140
IndustryBuildData::Reset
void Reset()
Completely reset the industry build data.
Definition: industry_cmd.cpp:2276
EconomyIsInRecession
static bool EconomyIsInRecession()
Is the economy in recession?
Definition: economy_func.h:47
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:404
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Station::industries_near
IndustryList industries_near
Cached list of industries near the station that can accept cargo,.
Definition: station_base.h:482
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
vehicle_func.h
IndustryBuildData::wanted_inds
uint32 wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:230
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
IndustrySpec::IsProcessingIndustry
bool IsProcessingIndustry() const
Is an industry with the spec a processing industry?
Definition: industry_cmd.cpp:2989
station_base.h
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
DrawIndustryAnimationStruct::image_1
byte image_1
image offset 1
Definition: industry_land.h:20
Pool::PoolItem<&_industry_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
CHECK_OIL_RIG
@ CHECK_OIL_RIG
Industries at sea should be positioned near edge of the map.
Definition: industrytype.h:48
GRFFilePropsBase::spritegroup
const struct SpriteGroup * spritegroup[Tcnt]
pointer to the different sprites of the entity
Definition: newgrf_commons.h:321
strings_func.h
CLEAN_RANDOMSOUNDS
@ CLEAN_RANDOMSOUNDS
Free the dynamically allocated sounds table.
Definition: industrytype.h:24
Industry::selected_layout
byte selected_layout
Which tile layout was used when creating the industry.
Definition: industry.h:98
NT_INDUSTRY_COMPANY
@ NT_INDUSTRY_COMPANY
Production changes of industry serviced by local company.
Definition: news_type.h:29
Pool
Base class for all pools.
Definition: pool_type.hpp:81
Industry::GetIndustryTypeCount
static uint16 GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition: industry.h:179
DeleteWindowById
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1165
GetWaterClass
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:106
ScaleByMapSize
static uint ScaleByMapSize(uint n)
Scales the given value by the map size, where the given value is for a 256 by 256 map.
Definition: map_func.h:122
newgrf_industrytiles.h
EconomySettings::type
EconomyType type
economy type (original/smooth/frozen)
Definition: settings_type.h:485
MapMaxY
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:111
StringParameters
Definition: strings_func.h:60
PopulateStationsNearby
static void PopulateStationsNearby(Industry *ind)
Populate an industry's list of nearby stations, and if it accepts any cargo, also add the industry to...
Definition: industry_cmd.cpp:1696
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition: tile_type.h:48
subsidy_func.h
CheckNewIndustry_Plantation
static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
Check the conditions of CHECK_PLANTATION (Industry should NOT be in the desert).
Definition: industry_cmd.cpp:1316
INDTILE_TRIGGER_TILE_LOOP
@ INDTILE_TRIGGER_TILE_LOOP
The tile of the industry has been triggered during the tileloop.
Definition: newgrf_industrytiles.h:71
FindTownForIndustry
static CommandCost FindTownForIndustry(TileIndex tile, int type, Town **t)
Find a town for the industry, while checking for multiple industries in the same town.
Definition: industry_cmd.cpp:1393
Pool::PoolItem<&_industry_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:359
DeleteAnimatedTile
void DeleteAnimatedTile(TileIndex tile)
Removes the given tile from the animated tile table.
Definition: animated_tile.cpp:26
Backup::Restore
void Restore()
Restore the variable.
Definition: backup_type.hpp:112
CheckIfIndustryTilesAreFree
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)
Are the tiles of the industry free?
Definition: industry_cmd.cpp:1438
INDUSTRYBEH_ONLY_NEARTOWN
@ INDUSTRYBEH_ONLY_NEARTOWN
is always built near towns (toy shop)
Definition: industrytype.h:68
Randomizer
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:21
IndustryTileSpec::animation
AnimationInfo animation
Information about the animation (is it looping, how many loops etc)
Definition: industrytype.h:169
GameCreationSettings::land_generator
byte land_generator
the landscape generator
Definition: settings_type.h:297
IsWaterTile
static bool IsWaterTile(TileIndex t)
Is it a water tile with plain water?
Definition: water_map.h:184
Industry::founder
Owner founder
Founder of the industry.
Definition: industry.h:94
CheckIfFarEnoughFromConflictingIndustry
static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
Check that the new industry is far enough from conflicting industries.
Definition: industry_cmd.cpp:1633
ICT_SCENARIO_EDITOR
@ ICT_SCENARIO_EDITOR
while editing a scenario
Definition: industrytype.h:57
SetGeneratingWorldProgress
void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
Set the total of a stage of the world generation.
Definition: genworld_gui.cpp:1366
CreateNewIndustryHelper
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)
Helper function for Build/Fund an industry.
Definition: industry_cmd.cpp:1926
GetIndustryTileSpec
const IndustryTileSpec * GetIndustryTileSpec(IndustryGfx gfx)
Accessor for array _industry_tile_specs.
Definition: industry_cmd.cpp:135
Industry::IncIndustryTypeCount
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:157
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:17
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:375
CBM_INDT_CARGO_ACCEPTANCE
@ CBM_INDT_CARGO_ACCEPTANCE
decides amount of cargo acceptance
Definition: newgrf_callbacks.h:371
GetIndustryGamePlayProbability
static uint16 GetIndustryGamePlayProbability(IndustryType it, byte *min_number)
Compute the probability for constructing a new industry during game play.
Definition: industry_cmd.cpp:2179
INDUSTRYLIFE_ORGANIC
@ INDUSTRYLIFE_ORGANIC
Like forests.
Definition: industrytype.h:31
GetIndustryIndex
static IndustryID GetIndustryIndex(TileIndex t)
Get the industry ID of the given tile.
Definition: industry_map.h:63
DrawBuildingsTileStruct
This structure is the same for both Industries and Houses.
Definition: sprite.h:67
cheat_type.h
CheckNewIndustry_OilRig
static CommandCost CheckNewIndustry_OilRig(TileIndex tile)
Check the conditions of CHECK_OIL_RIG (Industries at sea should be positioned near edge of the map).
Definition: industry_cmd.cpp:1286
Pool::PoolItem<&_industry_pool >::CleaningPool
static bool CleaningPool()
Returns current state of pool cleaning - yes or no.
Definition: pool_type.hpp:308
tree_map.h
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1985
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
GenerateIndustries
void GenerateIndustries()
This function will create random industries during game creation.
Definition: industry_cmd.cpp:2303
FOUNDATION_LEVELED
@ FOUNDATION_LEVELED
The tile is leveled up to a flat slope.
Definition: slope_type.h:95
IndustrySpec::production_down_text
StringID production_down_text
Message appearing when the industry's production is decreasing.
Definition: industrytype.h:131
TileDiffXY
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
Industry::last_cargo_accepted_at
Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS]
Last day each cargo type was accepted by this industry.
Definition: industry.h:97
LG_TERRAGENESIS
@ LG_TERRAGENESIS
TerraGenesis Perlin landscape generator.
Definition: genworld.h:21
IndustrySpec::cleanup_flag
uint8 cleanup_flag
flags indicating which data should be freed upon cleaning up
Definition: industrytype.h:139
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:46
IndustrySpec::appear_ingame
byte appear_ingame[NUM_LANDSCAPE]
Probability of appearance in game.
Definition: industrytype.h:133
IndustrySpec::grf_prop
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:141
Town::cache
TownCache cache
Container for all cacheable data.
Definition: town.h:53
OverrideManagerBase::ResetOverride
void ResetOverride()
Resets the override, which is used while initializing game.
Definition: newgrf_commons.cpp:88
IACT_MAPGENERATION
@ IACT_MAPGENERATION
during random map generation
Definition: newgrf_industries.h:83
SearchLumberMillTrees
static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
Search callback function for ChopLumberMillTrees.
Definition: industry_cmd.cpp:1092
GetCargoTranslation
CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Definition: newgrf_cargo.cpp:91
IndustrySpec::prospecting_chance
uint32 prospecting_chance
Chance prospecting succeeds.
Definition: industrytype.h:111
ForAllStationsAroundTiles
void ForAllStationsAroundTiles(const TileArea &ta, Func func)
Call a function on all stations that have any part of the requested area within their catchment.
Definition: station_base.h:569
Pool::PoolItem<&_industry_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:299
IndustryTileLayoutTile
Definition of one tile in an industry tile layout.
Definition: industrytype.h:96
ChangeIndustryProduction
static void ChangeIndustryProduction(Industry *i, bool monthly)
Change industry production or do closure.
Definition: industry_cmd.cpp:2672
Industry::last_prod_year
Year last_prod_year
last year of production
Definition: industry.h:86
IndustryTypeBuildData::min_number
byte min_number
Smallest number of industries that should exist (either 0 or 1).
Definition: industry.h:215
TriggerIndustryTile
void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
Trigger a random trigger for a single industry tile.
Definition: newgrf_industrytiles.cpp:359
MAX_UVALUE
#define MAX_UVALUE(type)
The largest value that can be entered in a variable.
Definition: stdafx.h:463
CBID_INDTILE_AUTOSLOPE
@ CBID_INDTILE_AUTOSLOPE
Called to determine if industry can alter the ground below industry tile.
Definition: newgrf_callbacks.h:177
IndustryTypeBuildData
Data for managing the number of industries of a single industry type.
Definition: industry.h:213
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
TileDesc::str
StringID str
Description of the tile.
Definition: tile_cmd.h:52
DC_AUTO
@ DC_AUTO
don't allow building on structures
Definition: command_type.h:349
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
GetTileMaxPixelZ
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:304
ScaleByMapSize1D
static uint ScaleByMapSize1D(uint n)
Scales the given value by the maps circumference, where the given value is for a 256 by 256 map.
Definition: map_func.h:136
EffectVehicle
A special vehicle is one of the following:
Definition: effectvehicle_base.h:24
DC_NO_MODIFY_TOWN_RATING
@ DC_NO_MODIFY_TOWN_RATING
do not change town rating
Definition: command_type.h:358
CreateEffectVehicleAbove
EffectVehicle * CreateEffectVehicleAbove(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular location.
Definition: effectvehicle.cpp:622
Backup::GetOriginalValue
const T & GetOriginalValue() const
Returns the backupped value.
Definition: backup_type.hpp:72
CheckNewIndustry_Forest
static CommandCost CheckNewIndustry_Forest(TileIndex tile)
Check the conditions of CHECK_FOREST (Industry should be build above snow-line in arctic climate).
Definition: industry_cmd.cpp:1232
IndustrySpec::callback_mask
uint16 callback_mask
Bitmask of industry callbacks that have to be called.
Definition: industrytype.h:138
IndustrySpec::random_sounds
const uint8 * random_sounds
array of random sounds.
Definition: industrytype.h:136
MapMaxX
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:102
CanCargoServiceIndustry
static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
Can given cargo type be accepted or produced by the industry?
Definition: industry_cmd.cpp:2557
EconomySettings::multiple_industry_per_town
bool multiple_industry_per_town
allow many industries of the same type per town
Definition: settings_type.h:495
IndustrySpec::life_type
IndustryLifeType life_type
This is also known as Industry production flag, in newgrf specs.
Definition: industrytype.h:123
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
CheckIfCallBackAllowsCreation
CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, size_t layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
Check that the industry callback allows creation of the industry.
Definition: newgrf_industries.cpp:534
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
TILE_ADDXY
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:258
GetTreeGround
static TreeGround GetTreeGround(TileIndex t)
Returns the groundtype for tree tiles.
Definition: tree_map.h:88
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:96
IndustryTileSpec::callback_mask
uint8 callback_mask
Bitmask of industry tile callbacks that have to be called.
Definition: industrytype.h:168
_check_new_industry_procs
static CheckNewIndustryProc *const _check_new_industry_procs[CHECK_END]
Check functions for different types of industry.
Definition: industry_cmd.cpp:1371
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
window_func.h
Industry::random_colour
byte random_colour
randomized colour of the industry, for display purpose
Definition: industry.h:85
IndustrySpec::behaviour
IndustryBehaviour behaviour
How this industry will behave, and how others entities can use it.
Definition: industrytype.h:125
AnimationInfo::status
uint8 status
Status; 0: no looping, 1: looping, 0xFF: no animation.
Definition: newgrf_animation_type.h:20
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
Town
Town data structure.
Definition: town.h:50
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
SetIndustryCompleted
static void SetIndustryCompleted(TileIndex tile)
Set if the industry that owns the tile as under construction or not.
Definition: industry_map.h:88
INDUSTRYBEH_CANCLOSE_LASTINSTANCE
@ INDUSTRYBEH_CANCLOSE_LASTINSTANCE
Allow closing down the last instance of this type.
Definition: industrytype.h:81
CBID_INDUSTRY_DECIDE_COLOUR
@ CBID_INDUSTRY_DECIDE_COLOUR
Called to determine the colour of an industry.
Definition: newgrf_callbacks.h:215
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
TransportIndustryGoods
static bool TransportIndustryGoods(TileIndex tile)
Move produced cargo from industry to nearby stations.
Definition: industry_cmd.cpp:529
IsOilRig
static bool IsOilRig(TileIndex t)
Is tile t part of an oilrig?
Definition: station_map.h:274
DoCreateNewIndustry
static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, const IndustryTileLayout &layout, size_t layout_index, Town *t, Owner founder, uint16 initial_random_bits)
Put an industry on the map.
Definition: industry_cmd.cpp:1725
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
INDUSTRY_CUT_TREE_TICKS
static const int INDUSTRY_CUT_TREE_TICKS
cycle duration for lumber mill's extra action
Definition: date_type.h:38
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Chance16R
static bool Chance16R(const uint a, const uint b, uint32 &r)
Flips a coin with a given probability and saves the randomize-number in a variable.
Definition: random_func.hpp:155
MakeField
static void MakeField(TileIndex t, uint field_type, IndustryID industry)
Make a (farm) field tile.
Definition: clear_map.h:280
INDCTL_NO_PRODUCTION_DECREASE
@ INDCTL_NO_PRODUCTION_DECREASE
When industry production change is evaluated, rolls to decrease are ignored.
Definition: industry.h:51
CBM_IND_DECIDE_COLOUR
@ CBM_IND_DECIDE_COLOUR
give a custom colour to newly build industries
Definition: newgrf_callbacks.h:359
GetIndustryConstructionCounter
static byte GetIndustryConstructionCounter(TileIndex tile)
Returns this industry tile's construction counter value.
Definition: industry_map.h:162
Vehicle::cargo_type
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:314
OrthogonalTileArea::Expand
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
Definition: tilearea.cpp:123
GetCurrentTotalNumberOfIndustries
static uint GetCurrentTotalNumberOfIndustries()
Get total number of industries existing in the game.
Definition: industry_cmd.cpp:2257
CheckNewIndustry_NULL
static CommandCost CheckNewIndustry_NULL(TileIndex tile)
Check the conditions of CHECK_NOTHING (Always succeeds).
Definition: industry_cmd.cpp:1222
INDUSTRYBEH_TOWN1200_MORE
@ INDUSTRYBEH_TOWN1200_MORE
can only be built in towns larger than 1200 inhabitants (temperate bank)
Definition: industrytype.h:66
ID_END
@ ID_END
Number of industry density settings.
Definition: settings_type.h:59
IndustryTypeBuildData::probability
uint32 probability
Relative probability of building this industry.
Definition: industry.h:214
IndustrySpec::check_proc
byte check_proc
Index to a procedure to check for conflicting circumstances.
Definition: industrytype.h:113
NEW_INDUSTRYTILEOFFSET
static const IndustryGfx NEW_INDUSTRYTILEOFFSET
original number of tiles
Definition: industry_type.h:32
CeilDiv
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:254
PalSpriteID::pal
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:24
NT_INDUSTRY_OTHER
@ NT_INDUSTRY_OTHER
Production changes of industry serviced by competitor(s)
Definition: news_type.h:30
TileInfo::tile
TileIndex tile
Tile index.
Definition: tile_cmd.h:46
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:561
ErrorUnknownCallbackResult
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Definition: newgrf_commons.cpp:516
GetIndustrySpec
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
Definition: industry_cmd.cpp:121
DC_NONE
@ DC_NONE
no flag is set
Definition: command_type.h:347
CBM_IND_PROD_CHANGE_BUILD
@ CBM_IND_PROD_CHANGE_BUILD
initialise production level on construction
Definition: newgrf_callbacks.h:362
IAT_CONSTRUCTION_STATE_CHANGE
@ IAT_CONSTRUCTION_STATE_CHANGE
Trigger whenever the construction state changes.
Definition: newgrf_animation_type.h:38
GetTileType
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
CheckNewIndustry_Water
static CommandCost CheckNewIndustry_Water(TileIndex tile)
Check the conditions of CHECK_WATER (Industry should be in the desert).
Definition: industry_cmd.cpp:1329
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Industry::accepts_cargo
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 input cargo slots
Definition: industry.h:75
IndustrySpec::name
StringID name
Displayed name of the industry.
Definition: industrytype.h:127
Pool::PoolItem<&_industry_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
IndustryBehaviour
IndustryBehaviour
Various industry behaviours mostly to represent original TTD specialities.
Definition: industrytype.h:61
IndustrySpec::new_industry_text
StringID new_industry_text
Message appearing when the industry is built.
Definition: industrytype.h:128
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
INDCTL_MASK
@ INDCTL_MASK
Mask of all flags set.
Definition: industry.h:59
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:320
Industry::prod_level
byte prod_level
general production level
Definition: industry.h:74
Industry::counts
static uint16 counts[NUM_INDUSTRYTYPES]
Number of industries per type ingame.
Definition: industry.h:201
Randomizer::SetSeed
void SetSeed(uint32 seed)
(Re)set the state of the random number generator.
Definition: random_func.cpp:55
MakeIndustry
static void MakeIndustry(TileIndex t, IndustryID index, IndustryGfx gfx, uint8 random, WaterClass wc)
Make the given tile an industry tile.
Definition: industry_map.h:278
Industry::psa
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:105
INDCTL_NO_PRODUCTION_INCREASE
@ INDCTL_NO_PRODUCTION_INCREASE
When industry production change is evaluated, rolls to increase are ignored.
Definition: industry.h:53
IAT_INDUSTRY_DISTRIBUTES_CARGO
@ IAT_INDUSTRY_DISTRIBUTES_CARGO
Trigger when cargo is distributed.
Definition: newgrf_animation_type.h:42
pool_func.hpp
IndustryControlFlags
IndustryControlFlags
Flags to control/override the behaviour of an industry.
Definition: industry.h:47
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
CheckIfIndustryIsAllowed
static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
Is the industry allowed to be built at this place for the town?
Definition: industry_cmd.cpp:1516
OrthogonalTileArea::ClampToMap
void ClampToMap()
Clamp the tile area to map borders.
Definition: tilearea.cpp:142
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:68
MapGRFStringID
StringID MapGRFStringID(uint32 grfid, StringID str)
Used when setting an object's property to map to the GRF's strings while taking in consideration the ...
Definition: newgrf.cpp:551
SetAnimationFrame
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:262
INDUSTRYBEH_CUT_TREES
@ INDUSTRYBEH_CUT_TREES
cuts trees and produce first output cargo from them (lumber mill)
Definition: industrytype.h:64
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:79
ResetIndustries
void ResetIndustries()
This function initialize the spec arrays of both industry and industry tiles.
Definition: industry_cmd.cpp:73
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
IndustryTileSpec::anim_next
byte anim_next
Next frame in an animation.
Definition: industrytype.h:161
CLEAR_FIELDS
@ CLEAR_FIELDS
3
Definition: clear_map.h:23
IndustryTileSpec
Defines the data structure of each individual tile of an industry.
Definition: industrytype.h:156
SLOPE_SE
@ SLOPE_SE
south and east corner are raised
Definition: slope_type.h:57
Industry::production_rate
byte production_rate[INDUSTRY_NUM_OUTPUTS]
production rate for each cargo
Definition: industry.h:73
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
IndustryTypeBuildData::target_count
uint16 target_count
Desired number of industries of this type.
Definition: industry.h:216
Order
Definition: order_base.h:32
IndustryTypeBuildData::GetIndustryTypeData
bool GetIndustryTypeData(IndustryType it)
Set the probability and min_number fields for the industry type it for a running game.
Definition: industry_cmd.cpp:2410
newgrf_cargo.h
IndustryProductionCallback
void IndustryProductionCallback(Industry *ind, int reason)
Get the industry production callback and apply it to the industry.
Definition: newgrf_industries.cpp:598
CHECK_REFINERY
@ CHECK_REFINERY
Industry should be positioned near edge of the map.
Definition: industrytype.h:42
SLOPE_NE
@ SLOPE_NE
north and east corner are raised
Definition: slope_type.h:58
EffectVehicle::animation_substate
byte animation_substate
Sub state to time the change of the graphics/behaviour.
Definition: effectvehicle_base.h:26
ComplementSlope
static Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:76
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52
CMD_LANDSCAPE_CLEAR
@ CMD_LANDSCAPE_CLEAR
demolish a tile
Definition: command_type.h:180
GetGRFConfig
GRFConfig * GetGRFConfig(uint32 grfid, uint32 mask)
Retrieve a NewGRF from the current config by its grfid.
Definition: newgrf_config.cpp:810
CBM_IND_OUTPUT_CARGO_TYPES
@ CBM_IND_OUTPUT_CARGO_TYPES
customize the cargoes the industry produces
Definition: newgrf_callbacks.h:361
OWNER_TOWN
@ OWNER_TOWN
A town owns the tile, or a town is expanding.
Definition: company_type.h:24
PlaceInitialIndustry
static void PlaceInitialIndustry(IndustryType type, bool try_hard)
Try to build a industry on the map.
Definition: industry_cmd.cpp:2243
DrawGroundSprite
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition: viewport.cpp:576
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
Delta
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:170
Industry::counter
uint16 counter
used for animation and/or production (if available cargo)
Definition: industry.h:81
ConstructionSettings::industry_platform
uint8 industry_platform
the amount of flat land around an industry
Definition: settings_type.h:328
GRFConfig::GetName
const char * GetName() const
Get the name of this grf.
Definition: newgrf_config.cpp:104
INDTILE_SPECIAL_ACCEPTS_ALL_CARGO
@ INDTILE_SPECIAL_ACCEPTS_ALL_CARGO
Tile always accepts all cargoes the associated industry accepts.
Definition: industrytype.h:91
GetTranslatedIndustryTileID
static IndustryGfx GetTranslatedIndustryTileID(IndustryGfx gfx)
Do industry gfx ID translation for NewGRFs.
Definition: industrytype.h:194
ICT_NORMAL_GAMEPLAY
@ ICT_NORMAL_GAMEPLAY
either by user or random creation process
Definition: industrytype.h:55
news_func.h
IsBridgeAbove
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
AddAnimatedTile
void AddAnimatedTile(TileIndex tile)
Add the given tile to the animated tile table (if it does not exist on that table yet).
Definition: animated_tile.cpp:41
GetIndustryCallback
uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
Perform an industry callback.
Definition: newgrf_industries.cpp:517
INDUSTRY_PRODUCE_TICKS
static const int INDUSTRY_PRODUCE_TICKS
cycle duration for industry production
Definition: date_type.h:36
Industry::TileBelongsToIndustry
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition: industry.h:117
AddChildSpriteScreen
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
Add a child sprite to a parent sprite.
Definition: viewport.cpp:814
backup_type.hpp
SND_37_LUMBER_MILL_2
@ SND_37_LUMBER_MILL_2
55 == 0x37 Industry animation: lumber mill (2): falling tree
Definition: sound_type.h:94
INDUSTRYLIFE_EXTRACTIVE
@ INDUSTRYLIFE_EXTRACTIVE
Like mines.
Definition: industrytype.h:30