(svn r27953) -Cleanup: Adjust other languages for r27952
[openttd.git] / src / newgrf_industries.cpp
blob9f227a9f58ff3024acb114fe30eb8fa2720884d5
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file newgrf_industries.cpp Handling of NewGRF industries. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "industry.h"
15 #include "newgrf_industries.h"
16 #include "newgrf_town.h"
17 #include "newgrf_cargo.h"
18 #include "window_func.h"
19 #include "town.h"
20 #include "company_base.h"
21 #include "error.h"
22 #include "strings_func.h"
23 #include "core/random_func.hpp"
25 #include "table/strings.h"
27 #include "safeguards.h"
29 /* Since the industry IDs defined by the GRF file don't necessarily correlate
30 * to those used by the game, the IDs used for overriding old industries must be
31 * translated when the idustry spec is set. */
32 IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
33 IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
35 /**
36 * Map the GRF local type to an industry type.
37 * @param grf_type The GRF local type.
38 * @param grf_id The GRF of the local type.
39 * @return The industry type in the global scope.
41 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
43 if (grf_type == IT_INVALID) return IT_INVALID;
44 if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 7);
46 return _industry_mngr.GetID(GB(grf_type, 0, 7), grf_id);
49 /**
50 * Make an analysis of a tile and check for its belonging to the same
51 * industry, and/or the same grf file
52 * @param tile TileIndex of the tile to query
53 * @param i Industry to which to compare the tile to
54 * @param cur_grfid GRFID of the current callback chain
55 * @return value encoded as per NFO specs
57 uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
59 if (!i->TileBelongsToIndustry(tile)) {
60 /* No industry and/or the tile does not have the same industry as the one we match it with */
61 return 0xFFFF;
64 IndustryGfx gfx = GetCleanIndustryGfx(tile);
65 const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
67 if (gfx < NEW_INDUSTRYTILEOFFSET) { // Does it belongs to an old type?
68 /* It is an old tile. We have to see if it's been overridden */
69 if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
70 return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
72 /* Overridden */
73 const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
75 if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
76 return tile_ovr->grf_prop.local_id; // same grf file
77 } else {
78 return 0xFFFE; // not the same grf file
81 /* Not an 'old type' tile */
82 if (indtsp->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
83 if (indtsp->grf_prop.grffile->grfid == cur_grfid) { // same industry, same grf ?
84 return indtsp->grf_prop.local_id;
85 } else {
86 return 0xFFFE; // Defined in another grf file
89 /* The tile has no spritegroup */
90 return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
93 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
95 uint32 best_dist = UINT32_MAX;
96 const Industry *i;
97 FOR_ALL_INDUSTRIES(i) {
98 if (i->type != type || i == current) continue;
100 best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
103 return best_dist;
107 * Implementation of both var 67 and 68
108 * since the mechanism is almost the same, it is easier to regroup them on the same
109 * function.
110 * @param param_setID parameter given to the callback, which is the set id, or the local id, in our terminology
111 * @param layout_filter on what layout do we filter?
112 * @param town_filter Do we filter on the same town as the current industry?
113 * @param current Industry for which the inquiry is made
114 * @return the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
116 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, bool town_filter, const Industry *current)
118 uint32 GrfID = GetRegister(0x100); ///< Get the GRFID of the definition to look for in register 100h
119 IndustryType ind_index;
120 uint32 closest_dist = UINT32_MAX;
121 byte count = 0;
123 /* Determine what will be the industry type to look for */
124 switch (GrfID) {
125 case 0: // this is a default industry type
126 ind_index = param_setID;
127 break;
129 case 0xFFFFFFFF: // current grf
130 GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
131 FALLTHROUGH;
133 default: // use the grfid specified in register 100h
134 SetBit(param_setID, 7); // bit 7 means it is not an old type
135 ind_index = MapNewGRFIndustryType(param_setID, GrfID);
136 break;
139 /* If the industry type is invalid, there is none and the closest is far away. */
140 if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;
142 if (layout_filter == 0 && !town_filter) {
143 /* If the filter is 0, it could be because none was specified as well as being really a 0.
144 * In either case, just do the regular var67 */
145 closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
146 count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit
147 } else {
148 /* Count only those who match the same industry type and layout filter
149 * Unfortunately, we have to do it manually */
150 const Industry *i;
151 FOR_ALL_INDUSTRIES(i) {
152 if (i->type == ind_index && i != current && (i->selected_layout == layout_filter || layout_filter == 0) && (!town_filter || i->town == current->town)) {
153 closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
154 count++;
159 return count << 16 | GB(closest_dist, 0, 16);
162 /* virtual */ uint32 IndustriesScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
164 if (this->ro.callback == CBID_INDUSTRY_LOCATION) {
165 /* Variables available during construction check. */
167 switch (variable) {
168 case 0x80: return this->tile;
169 case 0x81: return GB(this->tile, 8, 8);
171 /* Pointer to the town the industry is associated with */
172 case 0x82: return this->industry->town->index;
173 case 0x83:
174 case 0x84:
175 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
177 /* Number of the layout */
178 case 0x86: return this->industry->selected_layout;
180 /* Ground type */
181 case 0x87: return GetTerrainType(this->tile);
183 /* Town zone */
184 case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile);
186 /* Manhattan distance of the closest town */
187 case 0x89: return min(DistanceManhattan(this->industry->town->xy, this->tile), 255);
189 /* Lowest height of the tile */
190 case 0x8A: return Clamp(GetTileZ(this->tile) * (this->ro.grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
192 /* Distance to the nearest water/land tile */
193 case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
195 /* Square of Euclidian distance from town */
196 case 0x8D: return min(DistanceSquare(this->industry->town->xy, this->tile), 65535);
198 /* 32 random bits */
199 case 0x8F: return this->random_bits;
203 const IndustrySpec *indspec = GetIndustrySpec(this->type);
205 if (this->industry == NULL) {
206 DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, this->ro.callback);
208 *available = false;
209 return UINT_MAX;
212 switch (variable) {
213 case 0x40:
214 case 0x41:
215 case 0x42: { // waiting cargo, but only if those two callback flags are set
216 uint16 callback = indspec->callback_mask;
217 if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
218 if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
219 if (this->industry->prod_level == 0) return 0;
220 return min(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level, (uint16)0xFFFF);
221 } else {
222 return min(this->industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
224 } else {
225 return 0;
229 /* Manhattan distance of closes dry/water tile */
230 case 0x43:
231 if (this->tile == INVALID_TILE) break;
232 return GetClosestWaterDistance(this->tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
234 /* Layout number */
235 case 0x44: return this->industry->selected_layout;
237 /* Company info */
238 case 0x45: {
239 byte colours = 0;
240 bool is_ai = false;
242 const Company *c = Company::GetIfValid(this->industry->founder);
243 if (c != NULL) {
244 const Livery *l = &c->livery[LS_DEFAULT];
246 is_ai = c->is_ai;
247 colours = l->colour1 + l->colour2 * 16;
250 return this->industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
253 case 0x46: return this->industry->construction_date; // Date when built - long format - (in days)
255 /* Get industry ID at offset param */
256 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro.grffile->grfid);
258 /* Get random tile bits at offset param */
259 case 0x61: {
260 if (this->tile == INVALID_TILE) break;
261 TileIndex tile = GetNearbyTile(parameter, this->tile, false);
262 return this->industry->TileBelongsToIndustry(tile) ? GetIndustryRandomBits(tile) : 0;
265 /* Land info of nearby tiles */
266 case 0x62:
267 if (this->tile == INVALID_TILE) break;
268 return GetNearbyIndustryTileInformation(parameter, this->tile, INVALID_INDUSTRY, false, this->ro.grffile->grf_version >= 8);
270 /* Animation stage of nearby tiles */
271 case 0x63: {
272 if (this->tile == INVALID_TILE) break;
273 TileIndex tile = GetNearbyTile(parameter, this->tile, false);
274 if (this->industry->TileBelongsToIndustry(tile)) {
275 return GetAnimationFrame(tile);
277 return 0xFFFFFFFF;
280 /* Distance of nearest industry of given type */
281 case 0x64:
282 if (this->tile == INVALID_TILE) break;
283 return GetClosestIndustry(this->tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), this->industry);
284 /* Get town zone and Manhattan distance of closest town */
285 case 0x65:
286 if (this->tile == INVALID_TILE) break;
287 return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceManhattan(this->tile, this->industry->town->xy), 0xFFFF);
288 /* Get square of Euclidian distance of closes town */
289 case 0x66:
290 if (this->tile == INVALID_TILE) break;
291 return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceSquare(this->tile, this->industry->town->xy), 0xFFFF);
293 /* Count of industry, distance of closest instance
294 * 68 is the same as 67, but with a filtering on selected layout */
295 case 0x67:
296 case 0x68: {
297 byte layout_filter = 0;
298 bool town_filter = false;
299 if (variable == 0x68) {
300 uint32 reg = GetRegister(0x101);
301 layout_filter = GB(reg, 0, 8);
302 town_filter = HasBit(reg, 8);
304 return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, this->industry);
307 /* Get a variable from the persistent storage */
308 case 0x7C: return (this->industry->psa != NULL) ? this->industry->psa->GetValue(parameter) : 0;
310 /* Industry structure access*/
311 case 0x80: return this->industry->location.tile;
312 case 0x81: return GB(this->industry->location.tile, 8, 8);
313 /* Pointer to the town the industry is associated with */
314 case 0x82: return this->industry->town->index;
315 case 0x83:
316 case 0x84:
317 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
318 case 0x86: return this->industry->location.w;
319 case 0x87: return this->industry->location.h;// xy dimensions
321 case 0x88:
322 case 0x89: return this->industry->produced_cargo[variable - 0x88];
323 case 0x8A: return this->industry->produced_cargo_waiting[0];
324 case 0x8B: return GB(this->industry->produced_cargo_waiting[0], 8, 8);
325 case 0x8C: return this->industry->produced_cargo_waiting[1];
326 case 0x8D: return GB(this->industry->produced_cargo_waiting[1], 8, 8);
327 case 0x8E:
328 case 0x8F: return this->industry->production_rate[variable - 0x8E];
329 case 0x90:
330 case 0x91:
331 case 0x92: return this->industry->accepts_cargo[variable - 0x90];
332 case 0x93: return this->industry->prod_level;
333 /* amount of cargo produced so far THIS month. */
334 case 0x94: return this->industry->this_month_production[0];
335 case 0x95: return GB(this->industry->this_month_production[0], 8, 8);
336 case 0x96: return this->industry->this_month_production[1];
337 case 0x97: return GB(this->industry->this_month_production[1], 8, 8);
338 /* amount of cargo transported so far THIS month. */
339 case 0x98: return this->industry->this_month_transported[0];
340 case 0x99: return GB(this->industry->this_month_transported[0], 8, 8);
341 case 0x9A: return this->industry->this_month_transported[1];
342 case 0x9B: return GB(this->industry->this_month_transported[1], 8, 8);
343 /* fraction of cargo transported LAST month. */
344 case 0x9C:
345 case 0x9D: return this->industry->last_month_pct_transported[variable - 0x9C];
346 /* amount of cargo produced LAST month. */
347 case 0x9E: return this->industry->last_month_production[0];
348 case 0x9F: return GB(this->industry->last_month_production[0], 8, 8);
349 case 0xA0: return this->industry->last_month_production[1];
350 case 0xA1: return GB(this->industry->last_month_production[1], 8, 8);
351 /* amount of cargo transported last month. */
352 case 0xA2: return this->industry->last_month_transported[0];
353 case 0xA3: return GB(this->industry->last_month_transported[0], 8, 8);
354 case 0xA4: return this->industry->last_month_transported[1];
355 case 0xA5: return GB(this->industry->last_month_transported[1], 8, 8);
357 case 0xA6: return indspec->grf_prop.local_id;
358 case 0xA7: return this->industry->founder;
359 case 0xA8: return this->industry->random_colour;
360 case 0xA9: return Clamp(this->industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
361 case 0xAA: return this->industry->counter;
362 case 0xAB: return GB(this->industry->counter, 8, 8);
363 case 0xAC: return this->industry->was_cargo_delivered;
365 case 0xB0: return Clamp(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
366 case 0xB3: return this->industry->construction_type; // Construction type
367 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)
370 DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
372 *available = false;
373 return UINT_MAX;
376 /* virtual */ uint32 IndustriesScopeResolver::GetRandomBits() const
378 return this->industry != NULL ? this->industry->random : 0;
381 /* virtual */ uint32 IndustriesScopeResolver::GetTriggers() const
383 return 0;
386 /* virtual */ void IndustriesScopeResolver::StorePSA(uint pos, int32 value)
388 if (this->industry->index == INVALID_INDUSTRY) return;
390 if (this->industry->psa == NULL) {
391 /* There is no need to create a storage if the value is zero. */
392 if (value == 0) return;
394 /* Create storage on first modification. */
395 const IndustrySpec *indsp = GetIndustrySpec(this->industry->type);
396 uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
397 assert(PersistentStorage::CanAllocateItem());
398 this->industry->psa = new PersistentStorage(grfid, GSF_INDUSTRIES, this->industry->location.tile);
401 this->industry->psa->StoreValue(pos, value);
405 * Get the grf file associated with the given industry type.
406 * @param type Industry type to query.
407 * @return The associated GRF file, if any.
409 static const GRFFile *GetGrffile(IndustryType type)
411 const IndustrySpec *indspec = GetIndustrySpec(type);
412 return (indspec != NULL) ? indspec->grf_prop.grffile : NULL;
416 * Constructor of the industries resolver.
417 * @param tile %Tile owned by the industry.
418 * @param industry %Industry being resolved.
419 * @param type Type of the industry.
420 * @param random_bits Random bits of the new industry.
421 * @param callback Callback ID.
422 * @param callback_param1 First parameter (var 10) of the callback.
423 * @param callback_param2 Second parameter (var 18) of the callback.
425 IndustriesResolverObject::IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32 random_bits,
426 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
427 : ResolverObject(GetGrffile(type), callback, callback_param1, callback_param2),
428 industries_scope(*this, tile, indus, type, random_bits),
429 town_scope(NULL)
431 this->root_spritegroup = GetIndustrySpec(type)->grf_prop.spritegroup[0];
434 IndustriesResolverObject::~IndustriesResolverObject()
436 delete this->town_scope;
440 * Get or create the town scope object associated with the industry.
441 * @return The associated town scope, if it exists.
443 TownScopeResolver *IndustriesResolverObject::GetTown()
445 if (this->town_scope == NULL) {
446 Town *t = NULL;
447 bool readonly = true;
448 if (this->industries_scope.industry != NULL) {
449 t = this->industries_scope.industry->town;
450 readonly = this->industries_scope.industry->index == INVALID_INDUSTRY;
451 } else if (this->industries_scope.tile != INVALID_TILE) {
452 t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX);
454 if (t == NULL) return NULL;
455 this->town_scope = new TownScopeResolver(*this, t, readonly);
457 return this->town_scope;
461 * Scope resolver for industries.
462 * @param ro Surrounding resolver.
463 * @param tile %Tile owned by the industry.
464 * @param industry %Industry being resolved.
465 * @param type Type of the industry.
466 * @param random_bits Random bits of the new industry.
468 IndustriesScopeResolver::IndustriesScopeResolver(ResolverObject &ro, TileIndex tile, Industry *industry, IndustryType type, uint32 random_bits)
469 : ScopeResolver(ro)
471 this->tile = tile;
472 this->industry = industry;
473 this->type = type;
474 this->random_bits = random_bits;
478 * Perform an industry callback.
479 * @param callback The callback to perform.
480 * @param param1 The first parameter.
481 * @param param2 The second parameter.
482 * @param industry The industry to do the callback for.
483 * @param type The type of industry to do the callback for.
484 * @param tile The tile associated with the callback.
485 * @return The callback result.
487 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
489 IndustriesResolverObject object(tile, industry, type, 0, callback, param1, param2);
490 return object.ResolveCallback();
494 * Check that the industry callback allows creation of the industry.
495 * @param tile %Tile to build the industry.
496 * @param type Type of industry to build.
497 * @param layout Layout number.
498 * @param seed Seed for the random generator.
499 * @param initial_random_bits The random bits the industry is going to have after construction.
500 * @param founder Industry founder
501 * @param creation_type The circumstances the industry is created under.
502 * @return Succeeded or failed command.
504 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
506 const IndustrySpec *indspec = GetIndustrySpec(type);
508 Industry ind;
509 ind.index = INVALID_INDUSTRY;
510 ind.location.tile = tile;
511 ind.location.w = 0; // important to mark the industry invalid
512 ind.type = type;
513 ind.selected_layout = layout;
514 ind.town = ClosestTownFromTile(tile, UINT_MAX);
515 ind.random = initial_random_bits;
516 ind.founder = founder;
517 ind.psa = NULL;
519 IndustriesResolverObject object(tile, &ind, type, seed, CBID_INDUSTRY_LOCATION, 0, creation_type);
520 uint16 result = object.ResolveCallback();
522 /* Unlike the "normal" cases, not having a valid result means we allow
523 * the building of the industry, as that's how it's done in TTDP. */
524 if (result == CALLBACK_FAILED) return CommandCost();
526 return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile, STR_ERROR_SITE_UNSUITABLE);
530 * Check with callback #CBID_INDUSTRY_PROBABILITY whether the industry can be built.
531 * @param type Industry type to check.
532 * @param creation_type Reason to construct a new industry.
533 * @return If the industry has no callback or allows building, \c true is returned. Otherwise, \c false is returned.
535 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
537 const IndustrySpec *indspec = GetIndustrySpec(type);
539 if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
540 uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
541 if (res != CALLBACK_FAILED) {
542 if (indspec->grf_prop.grffile->grf_version < 8) {
543 /* Disallow if result != 0 */
544 if (res != 0) default_prob = 0;
545 } else {
546 /* Use returned probability. 0x100 to use default */
547 if (res < 0x100) {
548 default_prob = res;
549 } else if (res > 0x100) {
550 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
555 return default_prob;
558 static int32 DerefIndProd(int field, bool use_register)
560 return use_register ? (int32)GetRegister(field) : field;
564 * Get the industry production callback and apply it to the industry.
565 * @param ind the industry this callback has to be called for
566 * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
568 void IndustryProductionCallback(Industry *ind, int reason)
570 const IndustrySpec *spec = GetIndustrySpec(ind->type);
571 IndustriesResolverObject object(ind->location.tile, ind, ind->type);
572 if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
573 int multiplier = 1;
574 if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
575 object.callback_param2 = reason;
577 for (uint loop = 0;; loop++) {
578 /* limit the number of calls to break infinite loops.
579 * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
580 if (loop >= 0x10000) {
581 /* display error message */
582 SetDParamStr(0, spec->grf_prop.grffile->filename);
583 SetDParam(1, spec->name);
584 ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
586 /* abort the function early, this error isn't critical and will allow the game to continue to run */
587 break;
590 SB(object.callback_param2, 8, 16, loop);
591 const SpriteGroup *tgroup = object.Resolve();
592 if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
593 const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
595 bool deref = (group->version == 1);
597 for (uint i = 0; i < 3; i++) {
598 ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
600 for (uint i = 0; i < 2; i++) {
601 ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
604 int32 again = DerefIndProd(group->again, deref);
605 if (again == 0) break;
607 SB(object.callback_param2, 24, 8, again);
610 SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
614 * Check whether an industry temporarily refuses to accept a certain cargo.
615 * @param ind The industry to query.
616 * @param cargo_type The cargo to get information about.
617 * @pre cargo_type is in ind->accepts_cargo.
618 * @return Whether the given industry refuses to accept this cargo type.
620 bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
622 assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);
624 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
625 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
626 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
627 0, indspec->grf_prop.grffile->cargo_map[cargo_type],
628 ind, ind->type, ind->location.tile);
629 if (res != CALLBACK_FAILED) return !ConvertBooleanCallback(indspec->grf_prop.grffile, CBID_INDUSTRY_REFUSE_CARGO, res);
631 return false;