4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file newgrf_industries.cpp Handling of NewGRF industries. */
15 #include "newgrf_industries.h"
16 #include "newgrf_town.h"
17 #include "newgrf_cargo.h"
18 #include "window_func.h"
20 #include "company_base.h"
22 #include "strings_func.h"
23 #include "core/random_func.hpp"
24 #include "map/common.h"
25 #include "map/slope.h"
28 #include "table/strings.h"
30 /* Since the industry IDs defined by the GRF file don't necessarily correlate
31 * to those used by the game, the IDs used for overriding old industries must be
32 * translated when the idustry spec is set. */
33 IndustryOverrideManager
_industry_mngr(NEW_INDUSTRYOFFSET
, NUM_INDUSTRYTYPES
, INVALID_INDUSTRYTYPE
);
34 IndustryTileOverrideManager
_industile_mngr(NEW_INDUSTRYTILEOFFSET
, NUM_INDUSTRYTILES
, INVALID_INDUSTRYTILE
);
37 * Map the GRF local type to an industry type.
38 * @param grf_type The GRF local type.
39 * @param grf_id The GRF of the local type.
40 * @return The industry type in the global scope.
42 IndustryType
MapNewGRFIndustryType(IndustryType grf_type
, uint32 grf_id
)
44 if (grf_type
== IT_INVALID
) return IT_INVALID
;
45 if (!HasBit(grf_type
, 7)) return GB(grf_type
, 0, 6);
47 return _industry_mngr
.GetID(GB(grf_type
, 0, 6), grf_id
);
51 * Make an analysis of a tile and check for its belonging to the same
52 * industry, and/or the same grf file
53 * @param tile TileIndex of the tile to query
54 * @param i Industry to which to compare the tile to
55 * @param cur_grfid GRFID of the current callback chain
56 * @return value encoded as per NFO specs
58 uint32
GetIndustryIDAtOffset(TileIndex tile
, const Industry
*i
, uint32 cur_grfid
)
60 if (!i
->TileBelongsToIndustry(tile
)) {
61 /* No industry and/or the tile does not have the same industry as the one we match it with */
65 IndustryGfx gfx
= GetCleanIndustryGfx(tile
);
66 const IndustryTileSpec
*indtsp
= GetIndustryTileSpec(gfx
);
68 if (gfx
< NEW_INDUSTRYTILEOFFSET
) { // Does it belongs to an old type?
69 /* It is an old tile. We have to see if it's been overridden */
70 if (indtsp
->grf_prop
.override
== INVALID_INDUSTRYTILE
) { // has it been overridden?
71 return 0xFF << 8 | gfx
; // no. Tag FF + the gfx id of that tile
74 const IndustryTileSpec
*tile_ovr
= GetIndustryTileSpec(indtsp
->grf_prop
.override
);
76 if (tile_ovr
->grf_prop
.grffile
->grfid
== cur_grfid
) {
77 return tile_ovr
->grf_prop
.local_id
; // same grf file
79 return 0xFFFE; // not the same grf file
82 /* Not an 'old type' tile */
83 if (indtsp
->grf_prop
.spritegroup
[0] != NULL
) { // tile has a spritegroup ?
84 if (indtsp
->grf_prop
.grffile
->grfid
== cur_grfid
) { // same industry, same grf ?
85 return indtsp
->grf_prop
.local_id
;
87 return 0xFFFE; // Defined in another grf file
90 /* The tile has no spritegroup */
91 return 0xFF << 8 | indtsp
->grf_prop
.subst_id
; // so just give him the substitute
94 static uint32
GetClosestIndustry(TileIndex tile
, IndustryType type
, const Industry
*current
)
96 uint32 best_dist
= UINT32_MAX
;
98 FOR_ALL_INDUSTRIES(i
) {
99 if (i
->type
!= type
|| i
== current
) continue;
101 best_dist
= min(best_dist
, DistanceManhattan(tile
, i
->location
.tile
));
108 * Implementation of both var 67 and 68
109 * since the mechanism is almost the same, it is easier to regroup them on the same
111 * @param param_setID parameter given to the callback, which is the set id, or the local id, in our terminology
112 * @param layout_filter on what layout do we filter?
113 * @param town_filter Do we filter on the same town as the current industry?
114 * @param current Industry for which the inquiry is made
115 * @return the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
117 static uint32
GetCountAndDistanceOfClosestInstance(byte param_setID
, byte layout_filter
, bool town_filter
, const Industry
*current
)
119 uint32 GrfID
= GetRegister(0x100); ///< Get the GRFID of the definition to look for in register 100h
120 IndustryType ind_index
;
121 uint32 closest_dist
= UINT32_MAX
;
124 /* Determine what will be the industry type to look for */
126 case 0: // this is a default industry type
127 ind_index
= param_setID
;
130 case 0xFFFFFFFF: // current grf
131 GrfID
= GetIndustrySpec(current
->type
)->grf_prop
.grffile
->grfid
;
134 default: // use the grfid specified in register 100h
135 SetBit(param_setID
, 7); // bit 7 means it is not an old type
136 ind_index
= MapNewGRFIndustryType(param_setID
, GrfID
);
140 /* If the industry type is invalid, there is none and the closest is far away. */
141 if (ind_index
>= NUM_INDUSTRYTYPES
) return 0 | 0xFFFF;
143 if (layout_filter
== 0 && !town_filter
) {
144 /* If the filter is 0, it could be because none was specified as well as being really a 0.
145 * In either case, just do the regular var67 */
146 closest_dist
= GetClosestIndustry(current
->location
.tile
, ind_index
, current
);
147 count
= min(Industry::GetIndustryTypeCount(ind_index
), UINT8_MAX
); // clamp to 8 bit
149 /* Count only those who match the same industry type and layout filter
150 * Unfortunately, we have to do it manually */
152 FOR_ALL_INDUSTRIES(i
) {
153 if (i
->type
== ind_index
&& i
!= current
&& (i
->selected_layout
== layout_filter
|| layout_filter
== 0) && (!town_filter
|| i
->town
== current
->town
)) {
154 closest_dist
= min(closest_dist
, DistanceManhattan(current
->location
.tile
, i
->location
.tile
));
160 return count
<< 16 | GB(closest_dist
, 0, 16);
163 /* virtual */ uint32
IndustriesScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
165 if (this->ro
.callback
== CBID_INDUSTRY_LOCATION
) {
166 /* Variables available during construction check. */
169 case 0x80: return this->tile
;
170 case 0x81: return GB(this->tile
, 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 min(DistanceManhattan(this->industry
->town
->xy
, this->tile
), 255);
190 /* Lowest height of the tile */
191 case 0x8A: return Clamp(GetTileZ(this->tile
) * (this->ro
.grffile
->grf_version
>= 8 ? 1 : TILE_HEIGHT
), 0, 0xFF);
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 min(DistanceSquare(this->industry
->town
->xy
, this->tile
), 65535);
200 case 0x8F: return this->random_bits
;
204 const IndustrySpec
*indspec
= GetIndustrySpec(this->type
);
206 if (this->industry
== NULL
) {
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 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 min(this->industry
->incoming_cargo_waiting
[variable
- 0x40] / this->industry
->prod_level
, (uint16
)0xFFFF);
223 return min(this->industry
->incoming_cargo_waiting
[variable
- 0x40], (uint16
)0xFFFF);
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
; // Date when built - long format - (in days)
256 /* Get industry ID at offset param */
257 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter
, this->industry
->location
.tile
, false), this->industry
, this->ro
.grffile
->grfid
);
259 /* Get random tile bits at offset param */
261 if (this->tile
== INVALID_TILE
) break;
262 TileIndex tile
= GetNearbyTile(parameter
, this->tile
, false);
263 return this->industry
->TileBelongsToIndustry(tile
) ? GetIndustryRandomBits(tile
) : 0;
266 /* Land info of nearby tiles */
268 if (this->tile
== INVALID_TILE
) break;
269 return GetNearbyIndustryTileInformation(parameter
, this->tile
, INVALID_INDUSTRY
, false, this->ro
.grffile
->grf_version
>= 8);
271 /* Animation stage of nearby tiles */
273 if (this->tile
== INVALID_TILE
) break;
274 TileIndex tile
= GetNearbyTile(parameter
, this->tile
, false);
275 if (this->industry
->TileBelongsToIndustry(tile
)) {
276 return GetAnimationFrame(tile
);
281 /* Distance of nearest industry of given type */
283 if (this->tile
== INVALID_TILE
) break;
284 return GetClosestIndustry(this->tile
, MapNewGRFIndustryType(parameter
, indspec
->grf_prop
.grffile
->grfid
), this->industry
);
285 /* Get town zone and Manhattan distance of closest town */
287 if (this->tile
== INVALID_TILE
) break;
288 return GetTownRadiusGroup(this->industry
->town
, this->tile
) << 16 | min(DistanceManhattan(this->tile
, this->industry
->town
->xy
), 0xFFFF);
289 /* Get square of Euclidian distance of closes town */
291 if (this->tile
== INVALID_TILE
) break;
292 return GetTownRadiusGroup(this->industry
->town
, this->tile
) << 16 | min(DistanceSquare(this->tile
, this->industry
->town
->xy
), 0xFFFF);
294 /* Count of industry, distance of closest instance
295 * 68 is the same as 67, but with a filtering on selected layout */
298 byte layout_filter
= 0;
299 bool town_filter
= false;
300 if (variable
== 0x68) {
301 uint32 reg
= GetRegister(0x101);
302 layout_filter
= GB(reg
, 0, 8);
303 town_filter
= HasBit(reg
, 8);
305 return GetCountAndDistanceOfClosestInstance(parameter
, layout_filter
, town_filter
, this->industry
);
308 /* Get a variable from the persistent storage */
309 case 0x7C: return (this->industry
->psa
!= NULL
) ? this->industry
->psa
->GetValue(parameter
) : 0;
311 /* Industry structure access*/
312 case 0x80: return this->industry
->location
.tile
;
313 case 0x81: return GB(this->industry
->location
.tile
, 8, 8);
314 /* Pointer to the town the industry is associated with */
315 case 0x82: return this->industry
->town
->index
;
318 case 0x85: DEBUG(grf
, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
319 case 0x86: return this->industry
->location
.w
;
320 case 0x87: return this->industry
->location
.h
;// xy dimensions
323 case 0x89: return this->industry
->produced_cargo
[variable
- 0x88];
324 case 0x8A: return this->industry
->produced_cargo_waiting
[0];
325 case 0x8B: return GB(this->industry
->produced_cargo_waiting
[0], 8, 8);
326 case 0x8C: return this->industry
->produced_cargo_waiting
[1];
327 case 0x8D: return GB(this->industry
->produced_cargo_waiting
[1], 8, 8);
329 case 0x8F: return this->industry
->production_rate
[variable
- 0x8E];
332 case 0x92: return this->industry
->accepts_cargo
[variable
- 0x90];
333 case 0x93: return this->industry
->prod_level
;
334 /* amount of cargo produced so far THIS month. */
335 case 0x94: return this->industry
->this_month_production
[0];
336 case 0x95: return GB(this->industry
->this_month_production
[0], 8, 8);
337 case 0x96: return this->industry
->this_month_production
[1];
338 case 0x97: return GB(this->industry
->this_month_production
[1], 8, 8);
339 /* amount of cargo transported so far THIS month. */
340 case 0x98: return this->industry
->this_month_transported
[0];
341 case 0x99: return GB(this->industry
->this_month_transported
[0], 8, 8);
342 case 0x9A: return this->industry
->this_month_transported
[1];
343 case 0x9B: return GB(this->industry
->this_month_transported
[1], 8, 8);
344 /* fraction of cargo transported LAST month. */
346 case 0x9D: return this->industry
->last_month_pct_transported
[variable
- 0x9C];
347 /* amount of cargo produced LAST month. */
348 case 0x9E: return this->industry
->last_month_production
[0];
349 case 0x9F: return GB(this->industry
->last_month_production
[0], 8, 8);
350 case 0xA0: return this->industry
->last_month_production
[1];
351 case 0xA1: return GB(this->industry
->last_month_production
[1], 8, 8);
352 /* amount of cargo transported last month. */
353 case 0xA2: return this->industry
->last_month_transported
[0];
354 case 0xA3: return GB(this->industry
->last_month_transported
[0], 8, 8);
355 case 0xA4: return this->industry
->last_month_transported
[1];
356 case 0xA5: return GB(this->industry
->last_month_transported
[1], 8, 8);
358 case 0xA6: return this->industry
->type
;
359 case 0xA7: return this->industry
->founder
;
360 case 0xA8: return this->industry
->random_colour
;
361 case 0xA9: return Clamp(this->industry
->last_prod_year
- ORIGINAL_BASE_YEAR
, 0, 255);
362 case 0xAA: return this->industry
->counter
;
363 case 0xAB: return GB(this->industry
->counter
, 8, 8);
364 case 0xAC: return this->industry
->was_cargo_delivered
;
366 case 0xB0: return Clamp(this->industry
->construction_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535); // Date when built since 1920 (in days)
367 case 0xB3: return this->industry
->construction_type
; // Construction type
368 case 0xB4: return Clamp(this->industry
->last_cargo_accepted_at
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 65535); // Date last cargo accepted since 1920 (in days)
371 DEBUG(grf
, 1, "Unhandled industry variable 0x%X", variable
);
377 /* virtual */ uint32
IndustriesScopeResolver::GetRandomBits() const
379 return this->industry
!= NULL
? this->industry
->random
: 0;
382 /* virtual */ uint32
IndustriesScopeResolver::GetTriggers() const
384 return this->industry
!= NULL
? this->industry
->random_triggers
: 0;
387 /* virtual */ void IndustriesScopeResolver::SetTriggers(int triggers
) const
389 assert(this->industry
!= NULL
&& this->industry
->index
!= INVALID_INDUSTRY
);
390 this->industry
->random_triggers
= triggers
;
393 /* virtual */ void IndustriesScopeResolver::StorePSA(uint pos
, int32 value
)
395 if (this->industry
->index
== INVALID_INDUSTRY
) return;
397 if (this->industry
->psa
== NULL
) {
398 /* There is no need to create a storage if the value is zero. */
399 if (value
== 0) return;
401 /* Create storage on first modification. */
402 const IndustrySpec
*indsp
= GetIndustrySpec(this->industry
->type
);
403 uint32 grfid
= (indsp
->grf_prop
.grffile
!= NULL
) ? indsp
->grf_prop
.grffile
->grfid
: 0;
404 assert(PersistentStorage::CanAllocateItem());
405 this->industry
->psa
= new PersistentStorage(grfid
, GSF_INDUSTRIES
, this->industry
->location
.tile
);
408 this->industry
->psa
->StoreValue(pos
, value
);
412 * Get the grf file associated with the given industry type.
413 * @param type Industry type to query.
414 * @return The associated GRF file, if any.
416 static const GRFFile
*GetGrffile(IndustryType type
)
418 const IndustrySpec
*indspec
= GetIndustrySpec(type
);
419 return (indspec
!= NULL
) ? indspec
->grf_prop
.grffile
: NULL
;
423 * Constructor of the industries resolver.
424 * @param tile %Tile owned by the industry.
425 * @param industry %Industry being resolved.
426 * @param type Type of the industry.
427 * @param random_bits Random bits of the new industry.
428 * @param callback Callback ID.
429 * @param callback_param1 First parameter (var 10) of the callback.
430 * @param callback_param2 Second parameter (var 18) of the callback.
432 IndustriesResolverObject::IndustriesResolverObject(TileIndex tile
, Industry
*indus
, IndustryType type
, uint32 random_bits
,
433 CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
434 : ResolverObject(GetGrffile(type
), callback
, callback_param1
, callback_param2
),
435 industries_scope(*this, tile
, indus
, type
, random_bits
),
440 IndustriesResolverObject::~IndustriesResolverObject()
442 delete this->town_scope
;
446 * Get or create the town scope object associated with the industry.
447 * @return The associated town scope, if it exists.
449 TownScopeResolver
*IndustriesResolverObject::GetTown()
451 if (this->town_scope
== NULL
) {
453 bool readonly
= true;
454 if (this->industries_scope
.industry
!= NULL
) {
455 t
= this->industries_scope
.industry
->town
;
456 readonly
= this->industries_scope
.industry
->index
== INVALID_INDUSTRY
;
457 } else if (this->industries_scope
.tile
!= INVALID_TILE
) {
458 t
= ClosestTownFromTile(this->industries_scope
.tile
, UINT_MAX
);
460 if (t
== NULL
) return NULL
;
461 this->town_scope
= new TownScopeResolver(*this, t
, readonly
);
463 return this->town_scope
;
467 * Scope resolver for industries.
468 * @param ro Surrounding resolver.
469 * @param tile %Tile owned by the industry.
470 * @param industry %Industry being resolved.
471 * @param type Type of the industry.
472 * @param random_bits Random bits of the new industry.
474 IndustriesScopeResolver::IndustriesScopeResolver(ResolverObject
&ro
, TileIndex tile
, Industry
*industry
, IndustryType type
, uint32 random_bits
)
478 this->industry
= industry
;
480 this->random_bits
= random_bits
;
484 * Perform an industry callback.
485 * @param callback The callback to perform.
486 * @param param1 The first parameter.
487 * @param param2 The second parameter.
488 * @param industry The industry to do the callback for.
489 * @param type The type of industry to do the callback for.
490 * @param tile The tile associated with the callback.
491 * @return The callback result.
493 uint16
GetIndustryCallback(CallbackID callback
, uint32 param1
, uint32 param2
, Industry
*industry
, IndustryType type
, TileIndex tile
)
495 IndustriesResolverObject
object(tile
, industry
, type
, 0, callback
, param1
, param2
);
496 const SpriteGroup
*group
= SpriteGroup::Resolve(GetIndustrySpec(type
)->grf_prop
.spritegroup
[0], object
);
497 if (group
== NULL
) return CALLBACK_FAILED
;
499 return group
->GetCallbackResult();
503 * Check that the industry callback allows creation of the industry.
504 * @param tile %Tile to build the industry.
505 * @param type Type of industry to build.
506 * @param layout Layout number.
507 * @param seed Seed for the random generator.
508 * @param initial_random_bits The random bits the industry is going to have after construction.
509 * @param founder Industry founder
510 * @param creation_type The circumstances the industry is created under.
511 * @return Succeeded or failed command.
513 CommandCost
CheckIfCallBackAllowsCreation(TileIndex tile
, IndustryType type
, uint layout
, uint32 seed
, uint16 initial_random_bits
, Owner founder
, IndustryAvailabilityCallType creation_type
)
515 const IndustrySpec
*indspec
= GetIndustrySpec(type
);
518 ind
.index
= INVALID_INDUSTRY
;
519 ind
.location
.tile
= tile
;
520 ind
.location
.w
= 0; // important to mark the industry invalid
522 ind
.selected_layout
= layout
;
523 ind
.town
= ClosestTownFromTile(tile
, UINT_MAX
);
524 ind
.random
= initial_random_bits
;
525 ind
.founder
= founder
;
528 IndustriesResolverObject
object(tile
, &ind
, type
, seed
, CBID_INDUSTRY_LOCATION
, 0, creation_type
);
529 const SpriteGroup
*group
= SpriteGroup::Resolve(GetIndustrySpec(type
)->grf_prop
.spritegroup
[0], object
);
531 /* Unlike the "normal" cases, not having a valid result means we allow
532 * the building of the industry, as that's how it's done in TTDP. */
533 if (group
== NULL
) return CommandCost();
534 uint16 result
= group
->GetCallbackResult();
535 if (result
== CALLBACK_FAILED
) return CommandCost();
537 return GetErrorMessageFromLocationCallbackResult(result
, indspec
->grf_prop
.grffile
->grfid
, STR_ERROR_SITE_UNSUITABLE
);
541 * Check with callback #CBID_INDUSTRY_PROBABILITY whether the industry can be built.
542 * @param type Industry type to check.
543 * @param creation_type Reason to construct a new industry.
544 * @return If the industry has no callback or allows building, \c true is returned. Otherwise, \c false is returned.
546 uint32
GetIndustryProbabilityCallback(IndustryType type
, IndustryAvailabilityCallType creation_type
, uint32 default_prob
)
548 const IndustrySpec
*indspec
= GetIndustrySpec(type
);
550 if (HasBit(indspec
->callback_mask
, CBM_IND_PROBABILITY
)) {
551 uint16 res
= GetIndustryCallback(CBID_INDUSTRY_PROBABILITY
, 0, creation_type
, NULL
, type
, INVALID_TILE
);
552 if (res
!= CALLBACK_FAILED
) {
553 if (indspec
->grf_prop
.grffile
->grf_version
< 8) {
554 /* Disallow if result != 0 */
555 if (res
!= 0) default_prob
= 0;
557 /* Use returned probability. 0x100 to use default */
560 } else if (res
> 0x100) {
561 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_PROBABILITY
, res
);
569 static int32
DerefIndProd(int field
, bool use_register
)
571 return use_register
? (int32
)GetRegister(field
) : field
;
575 * Get the industry production callback and apply it to the industry.
576 * @param ind the industry this callback has to be called for
577 * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
579 void IndustryProductionCallback(Industry
*ind
, int reason
)
581 const IndustrySpec
*spec
= GetIndustrySpec(ind
->type
);
582 IndustriesResolverObject
object(ind
->location
.tile
, ind
, ind
->type
);
583 if ((spec
->behaviour
& INDUSTRYBEH_PRODCALLBACK_RANDOM
) != 0) object
.callback_param1
= Random();
585 if ((spec
->behaviour
& INDUSTRYBEH_PROD_MULTI_HNDLING
) != 0) multiplier
= ind
->prod_level
;
586 object
.callback_param2
= reason
;
588 for (uint loop
= 0;; loop
++) {
589 /* limit the number of calls to break infinite loops.
590 * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
591 if (loop
>= 0x10000) {
592 /* display error message */
593 SetDParamStr(0, spec
->grf_prop
.grffile
->filename
);
594 SetDParam(1, spec
->name
);
595 ShowErrorMessage(STR_NEWGRF_BUGGY
, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK
, WL_WARNING
);
597 /* abort the function early, this error isn't critical and will allow the game to continue to run */
601 SB(object
.callback_param2
, 8, 16, loop
);
602 const SpriteGroup
*tgroup
= SpriteGroup::Resolve(spec
->grf_prop
.spritegroup
[0], object
);
603 if (tgroup
== NULL
|| tgroup
->type
!= SGT_INDUSTRY_PRODUCTION
) break;
604 const IndustryProductionSpriteGroup
*group
= (const IndustryProductionSpriteGroup
*)tgroup
;
606 bool deref
= (group
->version
== 1);
608 for (uint i
= 0; i
< 3; i
++) {
609 ind
->incoming_cargo_waiting
[i
] = Clamp(ind
->incoming_cargo_waiting
[i
] - DerefIndProd(group
->subtract_input
[i
], deref
) * multiplier
, 0, 0xFFFF);
611 for (uint i
= 0; i
< 2; i
++) {
612 ind
->produced_cargo_waiting
[i
] = Clamp(ind
->produced_cargo_waiting
[i
] + max(DerefIndProd(group
->add_output
[i
], deref
), 0) * multiplier
, 0, 0xFFFF);
615 int32 again
= DerefIndProd(group
->again
, deref
);
616 if (again
== 0) break;
618 SB(object
.callback_param2
, 24, 8, again
);
621 SetWindowDirty(WC_INDUSTRY_VIEW
, ind
->index
);
625 * Check whether an industry temporarily refuses to accept a certain cargo.
626 * @param ind The industry to query.
627 * @param cargo_type The cargo to get information about.
628 * @pre cargo_type is in ind->accepts_cargo.
629 * @return Whether the given industry refuses to accept this cargo type.
631 bool IndustryTemporarilyRefusesCargo(Industry
*ind
, CargoID cargo_type
)
633 assert(cargo_type
== ind
->accepts_cargo
[0] || cargo_type
== ind
->accepts_cargo
[1] || cargo_type
== ind
->accepts_cargo
[2]);
635 const IndustrySpec
*indspec
= GetIndustrySpec(ind
->type
);
636 if (HasBit(indspec
->callback_mask
, CBM_IND_REFUSE_CARGO
)) {
637 uint16 res
= GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO
,
638 0, indspec
->grf_prop
.grffile
->cargo_map
[cargo_type
],
639 ind
, ind
->type
, ind
->location
.tile
);
640 if (res
!= CALLBACK_FAILED
) return !ConvertBooleanCallback(indspec
->grf_prop
.grffile
, CBID_INDUSTRY_REFUSE_CARGO
, res
);