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/>.
8 /** @file newgrf_industries.cpp Handling of NewGRF industries. */
13 #include "newgrf_industries.h"
14 #include "newgrf_town.h"
15 #include "newgrf_cargo.h"
16 #include "window_func.h"
18 #include "company_base.h"
20 #include "strings_func.h"
21 #include "core/random_func.hpp"
22 #include "timer/timer_game_calendar.h"
24 #include "table/strings.h"
26 #include "safeguards.h"
28 /* Since the industry IDs defined by the GRF file don't necessarily correlate
29 * to those used by the game, the IDs used for overriding old industries must be
30 * translated when the idustry spec is set. */
31 IndustryOverrideManager
_industry_mngr(NEW_INDUSTRYOFFSET
, NUM_INDUSTRYTYPES
, INVALID_INDUSTRYTYPE
);
32 IndustryTileOverrideManager
_industile_mngr(NEW_INDUSTRYTILEOFFSET
, NUM_INDUSTRYTILES
, INVALID_INDUSTRYTILE
);
35 * Map the GRF local type to an industry type.
36 * @param grf_type The GRF local type.
37 * @param grf_id The GRF of the local type.
38 * @return The industry type in the global scope.
40 IndustryType
MapNewGRFIndustryType(IndustryType grf_type
, uint32_t grf_id
)
42 if (grf_type
== IT_INVALID
) return IT_INVALID
;
43 if (!HasBit(grf_type
, 7)) return GB(grf_type
, 0, 7);
45 return _industry_mngr
.GetID(GB(grf_type
, 0, 7), grf_id
);
49 * Make an analysis of a tile and check for its belonging to the same
50 * industry, and/or the same grf file
51 * @param tile TileIndex of the tile to query
52 * @param i Industry to which to compare the tile to
53 * @param cur_grfid GRFID of the current callback chain
54 * @return value encoded as per NFO specs
56 uint32_t GetIndustryIDAtOffset(TileIndex tile
, const Industry
*i
, uint32_t cur_grfid
)
58 if (!i
->TileBelongsToIndustry(tile
)) {
59 /* No industry and/or the tile does not have the same industry as the one we match it with */
63 IndustryGfx gfx
= GetCleanIndustryGfx(tile
);
64 const IndustryTileSpec
*indtsp
= GetIndustryTileSpec(gfx
);
66 if (gfx
< NEW_INDUSTRYTILEOFFSET
) { // Does it belongs to an old type?
67 /* It is an old tile. We have to see if it's been overridden */
68 if (indtsp
->grf_prop
.override
== INVALID_INDUSTRYTILE
) { // has it been overridden?
69 return 0xFF << 8 | gfx
; // no. Tag FF + the gfx id of that tile
72 const IndustryTileSpec
*tile_ovr
= GetIndustryTileSpec(indtsp
->grf_prop
.override
);
74 if (tile_ovr
->grf_prop
.grffile
->grfid
== cur_grfid
) {
75 return tile_ovr
->grf_prop
.local_id
; // same grf file
77 return 0xFFFE; // not the same grf file
80 /* Not an 'old type' tile */
81 if (indtsp
->grf_prop
.spritegroup
[0] != nullptr) { // tile has a spritegroup ?
82 if (indtsp
->grf_prop
.grffile
->grfid
== cur_grfid
) { // same industry, same grf ?
83 return indtsp
->grf_prop
.local_id
;
85 return 0xFFFE; // Defined in another grf file
88 /* The tile has no spritegroup */
89 return 0xFF << 8 | indtsp
->grf_prop
.subst_id
; // so just give it the substitute
92 static uint32_t GetClosestIndustry(TileIndex tile
, IndustryType type
, const Industry
*current
)
94 uint32_t best_dist
= UINT32_MAX
;
96 for (const IndustryID
&industry
: Industry::industries
[type
]) {
97 if (industry
== current
->index
) continue;
99 best_dist
= std::min(best_dist
, DistanceManhattan(tile
, Industry::Get(industry
)->location
.tile
));
106 * Implementation of both var 67 and 68
107 * since the mechanism is almost the same, it is easier to regroup them on the same
109 * @param param_setID parameter given to the callback, which is the set id, or the local id, in our terminology
110 * @param layout_filter on what layout do we filter?
111 * @param town_filter Do we filter on the same town as the current industry?
112 * @param current Industry for which the inquiry is made
113 * @return the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
115 static uint32_t GetCountAndDistanceOfClosestInstance(uint8_t param_setID
, uint8_t layout_filter
, bool town_filter
, const Industry
*current
)
117 uint32_t GrfID
= GetRegister(0x100); ///< Get the GRFID of the definition to look for in register 100h
118 IndustryType ind_index
;
119 uint32_t closest_dist
= UINT32_MAX
;
122 /* Determine what will be the industry type to look for */
124 case 0: // this is a default industry type
125 ind_index
= param_setID
;
128 case 0xFFFFFFFF: // current grf
129 GrfID
= GetIndustrySpec(current
->type
)->grf_prop
.grffile
->grfid
;
132 default: // use the grfid specified in register 100h
133 SetBit(param_setID
, 7); // bit 7 means it is not an old type
134 ind_index
= MapNewGRFIndustryType(param_setID
, GrfID
);
138 /* If the industry type is invalid, there is none and the closest is far away. */
139 if (ind_index
>= NUM_INDUSTRYTYPES
) return 0 | 0xFFFF;
141 if (layout_filter
== 0 && !town_filter
) {
142 /* If the filter is 0, it could be because none was specified as well as being really a 0.
143 * In either case, just do the regular var67 */
144 closest_dist
= GetClosestIndustry(current
->location
.tile
, ind_index
, current
);
145 count
= ClampTo
<uint8_t>(Industry::GetIndustryTypeCount(ind_index
));
147 /* Count only those who match the same industry type and layout filter
148 * Unfortunately, we have to do it manually */
149 for (const IndustryID
&industry
: Industry::industries
[ind_index
]) {
150 if (industry
== current
->index
) continue;
152 const Industry
*i
= Industry::Get(industry
);
153 if ((layout_filter
== 0 || i
->selected_layout
== layout_filter
) && (!town_filter
|| i
->town
== current
->town
)) {
154 closest_dist
= std::min(closest_dist
, DistanceManhattan(current
->location
.tile
, i
->location
.tile
));
160 return count
<< 16 | GB(closest_dist
, 0, 16);
163 /* virtual */ uint32_t IndustriesScopeResolver::GetVariable(uint8_t variable
, [[maybe_unused
]] uint32_t parameter
, bool &available
) const
165 if (this->ro
.callback
== CBID_INDUSTRY_LOCATION
) {
166 /* Variables available during construction check. */
169 case 0x80: return this->tile
.base();
170 case 0x81: return GB(this->tile
.base(), 8, 8);
172 /* Pointer to the town the industry is associated with */
173 case 0x82: return this->industry
->town
->index
;
176 case 0x85: Debug(grf
, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
178 /* Number of the layout */
179 case 0x86: return this->industry
->selected_layout
;
182 case 0x87: return GetTerrainType(this->tile
);
185 case 0x88: return GetTownRadiusGroup(this->industry
->town
, this->tile
);
187 /* Manhattan distance of the closest town */
188 case 0x89: return ClampTo
<uint8_t>(DistanceManhattan(this->industry
->town
->xy
, this->tile
));
190 /* Lowest height of the tile */
191 case 0x8A: return ClampTo
<uint8_t>(GetTileZ(this->tile
) * (this->ro
.grffile
->grf_version
>= 8 ? 1 : TILE_HEIGHT
));
193 /* Distance to the nearest water/land tile */
194 case 0x8B: return GetClosestWaterDistance(this->tile
, (GetIndustrySpec(this->industry
->type
)->behaviour
& INDUSTRYBEH_BUILT_ONWATER
) == 0);
196 /* Square of Euclidian distance from town */
197 case 0x8D: return ClampTo
<uint16_t>(DistanceSquare(this->industry
->town
->xy
, this->tile
));
200 case 0x8F: return this->random_bits
;
204 const IndustrySpec
*indspec
= GetIndustrySpec(this->type
);
206 if (this->industry
== nullptr) {
207 Debug(grf
, 1, "Unhandled variable 0x{:X} (no available industry) in callback 0x{:x}", variable
, this->ro
.callback
);
216 case 0x42: { // waiting cargo, but only if those two callback flags are set
217 uint16_t callback
= indspec
->callback_mask
;
218 if (HasBit(callback
, CBM_IND_PRODUCTION_CARGO_ARRIVAL
) || HasBit(callback
, CBM_IND_PRODUCTION_256_TICKS
)) {
219 if ((indspec
->behaviour
& INDUSTRYBEH_PROD_MULTI_HNDLING
) != 0) {
220 if (this->industry
->prod_level
== 0) return 0;
221 return ClampTo
<uint16_t>(this->industry
->GetAccepted(variable
- 0x40).waiting
/ this->industry
->prod_level
);
223 return ClampTo
<uint16_t>(this->industry
->GetAccepted(variable
- 0x40).waiting
);
230 /* Manhattan distance of closes dry/water tile */
232 if (this->tile
== INVALID_TILE
) break;
233 return GetClosestWaterDistance(this->tile
, (indspec
->behaviour
& INDUSTRYBEH_BUILT_ONWATER
) == 0);
236 case 0x44: return this->industry
->selected_layout
;
243 const Company
*c
= Company::GetIfValid(this->industry
->founder
);
245 const Livery
*l
= &c
->livery
[LS_DEFAULT
];
248 colours
= l
->colour1
+ l
->colour2
* 16;
251 return this->industry
->founder
| (is_ai
? 0x10000 : 0) | (colours
<< 24);
254 case 0x46: return this->industry
->construction_date
.base(); // Date when built - long format - (in days)
256 /* Override flags from GS */
257 case 0x47: return this->industry
->ctlflags
;
259 /* Get industry ID at offset param */
260 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter
, this->industry
->location
.tile
, false), this->industry
, this->ro
.grffile
->grfid
);
262 /* Get random tile bits at offset param */
264 if (this->tile
== INVALID_TILE
) break;
265 TileIndex tile
= GetNearbyTile(parameter
, this->tile
, false);
266 return this->industry
->TileBelongsToIndustry(tile
) ? GetIndustryRandomBits(tile
) : 0;
269 /* Land info of nearby tiles */
271 if (this->tile
== INVALID_TILE
) break;
272 return GetNearbyIndustryTileInformation(parameter
, this->tile
, INVALID_INDUSTRY
, false, this->ro
.grffile
->grf_version
>= 8);
274 /* Animation stage of nearby tiles */
276 if (this->tile
== INVALID_TILE
) break;
277 TileIndex tile
= GetNearbyTile(parameter
, this->tile
, false);
278 if (this->industry
->TileBelongsToIndustry(tile
)) {
279 return GetAnimationFrame(tile
);
284 /* Distance of nearest industry of given type */
286 if (this->tile
== INVALID_TILE
) break;
287 IndustryType type
= MapNewGRFIndustryType(parameter
, indspec
->grf_prop
.grffile
->grfid
);
288 if (type
>= NUM_INDUSTRYTYPES
) return UINT32_MAX
;
289 return GetClosestIndustry(this->tile
, type
, this->industry
);
291 /* Get town zone and Manhattan distance of closest town */
293 if (this->tile
== INVALID_TILE
) break;
294 TileIndex tile
= GetNearbyTile(parameter
, this->tile
, true);
295 return GetTownRadiusGroup(this->industry
->town
, tile
) << 16 | ClampTo
<uint16_t>(DistanceManhattan(tile
, this->industry
->town
->xy
));
297 /* Get square of Euclidian distance of closest town */
299 if (this->tile
== INVALID_TILE
) break;
300 TileIndex tile
= GetNearbyTile(parameter
, this->tile
, true);
301 return DistanceSquare(tile
, this->industry
->town
->xy
);
304 /* Count of industry, distance of closest instance
305 * 68 is the same as 67, but with a filtering on selected layout */
308 uint8_t layout_filter
= 0;
309 bool town_filter
= false;
310 if (variable
== 0x68) {
311 uint32_t reg
= GetRegister(0x101);
312 layout_filter
= GB(reg
, 0, 8);
313 town_filter
= HasBit(reg
, 8);
315 return GetCountAndDistanceOfClosestInstance(parameter
, layout_filter
, town_filter
, this->industry
);
325 CargoID cargo
= GetCargoTranslation(parameter
, this->ro
.grffile
);
326 if (!IsValidCargoID(cargo
)) return 0;
327 auto it
= this->industry
->GetCargoProduced(cargo
);
328 if (it
== std::end(this->industry
->produced
)) return 0; // invalid cargo
330 case 0x69: return it
->waiting
;
331 case 0x6A: return it
->history
[THIS_MONTH
].production
;
332 case 0x6B: return it
->history
[THIS_MONTH
].transported
;
333 case 0x6C: return it
->history
[LAST_MONTH
].production
;
334 case 0x6D: return it
->history
[LAST_MONTH
].transported
;
335 case 0x70: return it
->rate
;
336 case 0x71: return it
->history
[LAST_MONTH
].PctTransported();
337 default: NOT_REACHED();
344 CargoID cargo
= GetCargoTranslation(parameter
, this->ro
.grffile
);
345 if (!IsValidCargoID(cargo
)) return 0;
346 auto it
= this->industry
->GetCargoAccepted(cargo
);
347 if (it
== std::end(this->industry
->accepted
)) return 0; // invalid cargo
348 if (variable
== 0x6E) return it
->last_accepted
.base();
349 if (variable
== 0x6F) return it
->waiting
;
353 /* Get a variable from the persistent storage */
354 case 0x7C: return (this->industry
->psa
!= nullptr) ? this->industry
->psa
->GetValue(parameter
) : 0;
356 /* Industry structure access*/
357 case 0x80: return this->industry
->location
.tile
.base();
358 case 0x81: return GB(this->industry
->location
.tile
.base(), 8, 8);
359 /* Pointer to the town the industry is associated with */
360 case 0x82: return this->industry
->town
->index
;
363 case 0x85: Debug(grf
, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
364 case 0x86: return this->industry
->location
.w
;
365 case 0x87: return this->industry
->location
.h
;// xy dimensions
368 case 0x89: return this->industry
->GetProduced(variable
- 0x88).cargo
;
369 case 0x8A: return this->industry
->GetProduced(0).waiting
;
370 case 0x8B: return GB(this->industry
->GetProduced(0).waiting
, 8, 8);
371 case 0x8C: return this->industry
->GetProduced(1).waiting
;
372 case 0x8D: return GB(this->industry
->GetProduced(1).waiting
, 8, 8);
374 case 0x8F: return this->industry
->GetProduced(variable
- 0x8E).rate
;
377 case 0x92: return this->industry
->GetAccepted(variable
- 0x90).cargo
;
378 case 0x93: return this->industry
->prod_level
;
379 /* amount of cargo produced so far THIS month. */
380 case 0x94: return this->industry
->GetProduced(0).history
[THIS_MONTH
].production
;
381 case 0x95: return GB(this->industry
->GetProduced(0).history
[THIS_MONTH
].production
, 8, 8);
382 case 0x96: return this->industry
->GetProduced(1).history
[THIS_MONTH
].production
;
383 case 0x97: return GB(this->industry
->GetProduced(1).history
[THIS_MONTH
].production
, 8, 8);
384 /* amount of cargo transported so far THIS month. */
385 case 0x98: return this->industry
->GetProduced(0).history
[THIS_MONTH
].transported
;
386 case 0x99: return GB(this->industry
->GetProduced(0).history
[THIS_MONTH
].transported
, 8, 8);
387 case 0x9A: return this->industry
->GetProduced(1).history
[THIS_MONTH
].transported
;
388 case 0x9B: return GB(this->industry
->GetProduced(1).history
[THIS_MONTH
].transported
, 8, 8);
389 /* fraction of cargo transported LAST month. */
391 case 0x9D: return this->industry
->GetProduced(variable
- 0x9C).history
[LAST_MONTH
].PctTransported();
392 /* amount of cargo produced LAST month. */
393 case 0x9E: return this->industry
->GetProduced(0).history
[LAST_MONTH
].production
;
394 case 0x9F: return GB(this->industry
->GetProduced(0).history
[LAST_MONTH
].production
, 8, 8);
395 case 0xA0: return this->industry
->GetProduced(1).history
[LAST_MONTH
].production
;
396 case 0xA1: return GB(this->industry
->GetProduced(1).history
[LAST_MONTH
].production
, 8, 8);
397 /* amount of cargo transported last month. */
398 case 0xA2: return this->industry
->GetProduced(0).history
[LAST_MONTH
].transported
;
399 case 0xA3: return GB(this->industry
->GetProduced(0).history
[LAST_MONTH
].transported
, 8, 8);
400 case 0xA4: return this->industry
->GetProduced(1).history
[LAST_MONTH
].transported
;
401 case 0xA5: return GB(this->industry
->GetProduced(1).history
[LAST_MONTH
].transported
, 8, 8);
403 case 0xA6: return indspec
->grf_prop
.local_id
;
404 case 0xA7: return this->industry
->founder
;
405 case 0xA8: return this->industry
->random_colour
;
406 case 0xA9: return ClampTo
<uint8_t>(this->industry
->last_prod_year
- EconomyTime::ORIGINAL_BASE_YEAR
);
407 case 0xAA: return this->industry
->counter
;
408 case 0xAB: return GB(this->industry
->counter
, 8, 8);
409 case 0xAC: return this->industry
->was_cargo_delivered
;
411 case 0xB0: return ClampTo
<uint16_t>(this->industry
->construction_date
- CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR
); // Date when built since 1920 (in days)
412 case 0xB3: return this->industry
->construction_type
; // Construction type
414 if (this->industry
->accepted
.empty()) return 0;
415 auto it
= std::max_element(std::begin(this->industry
->accepted
), std::end(this->industry
->accepted
), [](const auto &a
, const auto &b
) { return a
.last_accepted
< b
.last_accepted
; });
416 return ClampTo
<uint16_t>(it
->last_accepted
- EconomyTime::DAYS_TILL_ORIGINAL_BASE_YEAR
); // Date last cargo accepted since 1920 (in days)
420 Debug(grf
, 1, "Unhandled industry variable 0x{:X}", variable
);
426 /* virtual */ uint32_t IndustriesScopeResolver::GetRandomBits() const
428 return this->industry
!= nullptr ? this->industry
->random
: 0;
431 /* virtual */ uint32_t IndustriesScopeResolver::GetTriggers() const
436 /* virtual */ void IndustriesScopeResolver::StorePSA(uint pos
, int32_t value
)
438 if (this->industry
->index
== INVALID_INDUSTRY
) return;
440 if (this->industry
->psa
== nullptr) {
441 /* There is no need to create a storage if the value is zero. */
442 if (value
== 0) return;
444 /* Create storage on first modification. */
445 const IndustrySpec
*indsp
= GetIndustrySpec(this->industry
->type
);
446 uint32_t grfid
= (indsp
->grf_prop
.grffile
!= nullptr) ? indsp
->grf_prop
.grffile
->grfid
: 0;
447 assert(PersistentStorage::CanAllocateItem());
448 this->industry
->psa
= new PersistentStorage(grfid
, GSF_INDUSTRIES
, this->industry
->location
.tile
);
451 this->industry
->psa
->StoreValue(pos
, value
);
455 * Get the grf file associated with the given industry type.
456 * @param type Industry type to query.
457 * @return The associated GRF file, if any.
459 static const GRFFile
*GetGrffile(IndustryType type
)
461 const IndustrySpec
*indspec
= GetIndustrySpec(type
);
462 return (indspec
!= nullptr) ? indspec
->grf_prop
.grffile
: nullptr;
466 * Constructor of the industries resolver.
467 * @param tile %Tile owned by the industry.
468 * @param indus %Industry being resolved.
469 * @param type Type of the industry.
470 * @param random_bits Random bits of the new industry.
471 * @param callback Callback ID.
472 * @param callback_param1 First parameter (var 10) of the callback.
473 * @param callback_param2 Second parameter (var 18) of the callback.
475 IndustriesResolverObject::IndustriesResolverObject(TileIndex tile
, Industry
*indus
, IndustryType type
, uint32_t random_bits
,
476 CallbackID callback
, uint32_t callback_param1
, uint32_t callback_param2
)
477 : ResolverObject(GetGrffile(type
), callback
, callback_param1
, callback_param2
),
478 industries_scope(*this, tile
, indus
, type
, random_bits
)
480 this->root_spritegroup
= GetIndustrySpec(type
)->grf_prop
.spritegroup
[0];
484 * Get or create the town scope object associated with the industry.
485 * @return The associated town scope, if it exists.
487 TownScopeResolver
*IndustriesResolverObject::GetTown()
489 if (!this->town_scope
.has_value()) {
491 bool readonly
= true;
492 if (this->industries_scope
.industry
!= nullptr) {
493 t
= this->industries_scope
.industry
->town
;
494 readonly
= this->industries_scope
.industry
->index
== INVALID_INDUSTRY
;
495 } else if (this->industries_scope
.tile
!= INVALID_TILE
) {
496 t
= ClosestTownFromTile(this->industries_scope
.tile
, UINT_MAX
);
498 if (t
== nullptr) return nullptr;
499 this->town_scope
.emplace(*this, t
, readonly
);
501 return &*this->town_scope
;
504 GrfSpecFeature
IndustriesResolverObject::GetFeature() const
506 return GSF_INDUSTRIES
;
509 uint32_t IndustriesResolverObject::GetDebugID() const
511 return GetIndustrySpec(this->industries_scope
.type
)->grf_prop
.local_id
;
515 * Perform an industry callback.
516 * @param callback The callback to perform.
517 * @param param1 The first parameter.
518 * @param param2 The second parameter.
519 * @param industry The industry to do the callback for.
520 * @param type The type of industry to do the callback for.
521 * @param tile The tile associated with the callback.
522 * @return The callback result.
524 uint16_t GetIndustryCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, Industry
*industry
, IndustryType type
, TileIndex tile
)
526 IndustriesResolverObject
object(tile
, industry
, type
, 0, callback
, param1
, param2
);
527 return object
.ResolveCallback();
531 * Check that the industry callback allows creation of the industry.
532 * @param tile %Tile to build the industry.
533 * @param type Type of industry to build.
534 * @param layout Layout number.
535 * @param seed Seed for the random generator.
536 * @param initial_random_bits The random bits the industry is going to have after construction.
537 * @param founder Industry founder
538 * @param creation_type The circumstances the industry is created under.
539 * @return Succeeded or failed command.
541 CommandCost
CheckIfCallBackAllowsCreation(TileIndex tile
, IndustryType type
, size_t layout
, uint32_t seed
, uint16_t initial_random_bits
, Owner founder
, IndustryAvailabilityCallType creation_type
)
543 const IndustrySpec
*indspec
= GetIndustrySpec(type
);
546 ind
.index
= INVALID_INDUSTRY
;
547 ind
.location
.tile
= tile
;
548 ind
.location
.w
= 0; // important to mark the industry invalid
550 ind
.selected_layout
= (uint8_t)layout
;
551 ind
.town
= ClosestTownFromTile(tile
, UINT_MAX
);
552 ind
.random
= initial_random_bits
;
553 ind
.founder
= founder
;
556 IndustriesResolverObject
object(tile
, &ind
, type
, seed
, CBID_INDUSTRY_LOCATION
, 0, creation_type
);
557 uint16_t result
= object
.ResolveCallback();
559 /* Unlike the "normal" cases, not having a valid result means we allow
560 * the building of the industry, as that's how it's done in TTDP. */
561 if (result
== CALLBACK_FAILED
) return CommandCost();
563 return GetErrorMessageFromLocationCallbackResult(result
, indspec
->grf_prop
.grffile
, STR_ERROR_SITE_UNSUITABLE
);
567 * Check with callback #CBID_INDUSTRY_PROBABILITY whether the industry can be built.
568 * @param type Industry type to check.
569 * @param creation_type Reason to construct a new industry.
570 * @return If the industry has no callback or allows building, \c true is returned. Otherwise, \c false is returned.
572 uint32_t GetIndustryProbabilityCallback(IndustryType type
, IndustryAvailabilityCallType creation_type
, uint32_t default_prob
)
574 const IndustrySpec
*indspec
= GetIndustrySpec(type
);
576 if (HasBit(indspec
->callback_mask
, CBM_IND_PROBABILITY
)) {
577 uint16_t res
= GetIndustryCallback(CBID_INDUSTRY_PROBABILITY
, 0, creation_type
, nullptr, type
, INVALID_TILE
);
578 if (res
!= CALLBACK_FAILED
) {
579 if (indspec
->grf_prop
.grffile
->grf_version
< 8) {
580 /* Disallow if result != 0 */
581 if (res
!= 0) default_prob
= 0;
583 /* Use returned probability. 0x100 to use default */
586 } else if (res
> 0x100) {
587 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_PROBABILITY
, res
);
595 static int32_t DerefIndProd(int field
, bool use_register
)
597 return use_register
? (int32_t)GetRegister(field
) : field
;
601 * Get the industry production callback and apply it to the industry.
602 * @param ind the industry this callback has to be called for
603 * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
605 void IndustryProductionCallback(Industry
*ind
, int reason
)
607 const IndustrySpec
*spec
= GetIndustrySpec(ind
->type
);
608 IndustriesResolverObject
object(ind
->location
.tile
, ind
, ind
->type
);
609 if ((spec
->behaviour
& INDUSTRYBEH_PRODCALLBACK_RANDOM
) != 0) object
.callback_param1
= Random();
611 if ((spec
->behaviour
& INDUSTRYBEH_PROD_MULTI_HNDLING
) != 0) multiplier
= ind
->prod_level
;
612 object
.callback_param2
= reason
;
614 for (uint loop
= 0;; loop
++) {
615 /* limit the number of calls to break infinite loops.
616 * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
617 if (loop
>= 0x10000) {
618 /* display error message */
619 SetDParamStr(0, spec
->grf_prop
.grffile
->filename
);
620 SetDParam(1, spec
->name
);
621 ShowErrorMessage(STR_NEWGRF_BUGGY
, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK
, WL_WARNING
);
623 /* abort the function early, this error isn't critical and will allow the game to continue to run */
627 SB(object
.callback_param2
, 8, 16, loop
);
628 const SpriteGroup
*tgroup
= object
.Resolve();
629 if (tgroup
== nullptr || tgroup
->type
!= SGT_INDUSTRY_PRODUCTION
) break;
630 const IndustryProductionSpriteGroup
*group
= (const IndustryProductionSpriteGroup
*)tgroup
;
632 if (group
->version
== 0xFF) {
633 /* Result was marked invalid on load, display error message */
634 SetDParamStr(0, spec
->grf_prop
.grffile
->filename
);
635 SetDParam(1, spec
->name
);
636 SetDParam(2, ind
->location
.tile
);
637 ShowErrorMessage(STR_NEWGRF_BUGGY
, STR_NEWGRF_BUGGY_INVALID_CARGO_PRODUCTION_CALLBACK
, WL_WARNING
);
639 /* abort the function early, this error isn't critical and will allow the game to continue to run */
643 bool deref
= (group
->version
>= 1);
645 if (group
->version
< 2) {
646 /* Callback parameters map directly to industry cargo slot indices */
647 for (uint i
= 0; i
< group
->num_input
&& i
< ind
->accepted
.size(); i
++) {
648 if (!IsValidCargoID(ind
->accepted
[i
].cargo
)) continue;
649 ind
->accepted
[i
].waiting
= ClampTo
<uint16_t>(ind
->accepted
[i
].waiting
- DerefIndProd(group
->subtract_input
[i
], deref
) * multiplier
);
651 for (uint i
= 0; i
< group
->num_output
&& i
< ind
->produced
.size(); i
++) {
652 if (!IsValidCargoID(ind
->produced
[i
].cargo
)) continue;
653 ind
->produced
[i
].waiting
= ClampTo
<uint16_t>(ind
->produced
[i
].waiting
+ std::max(DerefIndProd(group
->add_output
[i
], deref
), 0) * multiplier
);
656 /* Callback receives list of cargos to apply for, which need to have their cargo slots in industry looked up */
657 for (uint i
= 0; i
< group
->num_input
; i
++) {
658 auto it
= ind
->GetCargoAccepted(group
->cargo_input
[i
]);
659 if (it
== std::end(ind
->accepted
)) continue;
660 it
->waiting
= ClampTo
<uint16_t>(it
->waiting
- DerefIndProd(group
->subtract_input
[i
], deref
) * multiplier
);
662 for (uint i
= 0; i
< group
->num_output
; i
++) {
663 auto it
= ind
->GetCargoProduced(group
->cargo_output
[i
]);
664 if (it
== std::end(ind
->produced
)) continue;
665 it
->waiting
= ClampTo
<uint16_t>(it
->waiting
+ std::max(DerefIndProd(group
->add_output
[i
], deref
), 0) * multiplier
);
669 int32_t again
= DerefIndProd(group
->again
, deref
);
670 if (again
== 0) break;
672 SB(object
.callback_param2
, 24, 8, again
);
675 SetWindowDirty(WC_INDUSTRY_VIEW
, ind
->index
);
679 * Check whether an industry temporarily refuses to accept a certain cargo.
680 * @param ind The industry to query.
681 * @param cargo_type The cargo to get information about.
682 * @pre cargo_type is in ind->accepts_cargo.
683 * @return Whether the given industry refuses to accept this cargo type.
685 bool IndustryTemporarilyRefusesCargo(Industry
*ind
, CargoID cargo_type
)
687 assert(ind
->IsCargoAccepted(cargo_type
));
689 const IndustrySpec
*indspec
= GetIndustrySpec(ind
->type
);
690 if (HasBit(indspec
->callback_mask
, CBM_IND_REFUSE_CARGO
)) {
691 uint16_t res
= GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO
,
692 0, indspec
->grf_prop
.grffile
->cargo_map
[cargo_type
],
693 ind
, ind
->type
, ind
->location
.tile
);
694 if (res
!= CALLBACK_FAILED
) return !ConvertBooleanCallback(indspec
->grf_prop
.grffile
, CBID_INDUSTRY_REFUSE_CARGO
, res
);