OpenTTD
newgrf_industries.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
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  */
9 
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"
24 
25 #include "table/strings.h"
26 
27 #include "safeguards.h"
28 
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. */
34 
41 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
42 {
43  if (grf_type == IT_INVALID) return IT_INVALID;
44  if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 7);
45 
46  return _industry_mngr.GetID(GB(grf_type, 0, 7), grf_id);
47 }
48 
57 uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
58 {
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;
62  }
63 
64  IndustryGfx gfx = GetCleanIndustryGfx(tile);
65  const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
66 
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
71  }
72  /* Overridden */
73  const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
74 
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
79  }
80  }
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
87  }
88  }
89  /* The tile has no spritegroup */
90  return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
91 }
92 
93 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
94 {
95  uint32 best_dist = UINT32_MAX;
96  const Industry *i;
97  FOR_ALL_INDUSTRIES(i) {
98  if (i->type != type || i == current) continue;
99 
100  best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
101  }
102 
103  return best_dist;
104 }
105 
116 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, bool town_filter, const Industry *current)
117 {
118  uint32 GrfID = GetRegister(0x100);
119  IndustryType ind_index;
120  uint32 closest_dist = UINT32_MAX;
121  byte count = 0;
122 
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;
128 
129  case 0xFFFFFFFF: // current grf
130  GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
131  FALLTHROUGH;
132 
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;
137  }
138 
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;
141 
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++;
155  }
156  }
157  }
158 
159  return count << 16 | GB(closest_dist, 0, 16);
160 }
161 
162 /* virtual */ uint32 IndustriesScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
163 {
164  if (this->ro.callback == CBID_INDUSTRY_LOCATION) {
165  /* Variables available during construction check. */
166 
167  switch (variable) {
168  case 0x80: return this->tile;
169  case 0x81: return GB(this->tile, 8, 8);
170 
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
176 
177  /* Number of the layout */
178  case 0x86: return this->industry->selected_layout;
179 
180  /* Ground type */
181  case 0x87: return GetTerrainType(this->tile);
182 
183  /* Town zone */
184  case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile);
185 
186  /* Manhattan distance of the closest town */
187  case 0x89: return min(DistanceManhattan(this->industry->town->xy, this->tile), 255);
188 
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);
191 
192  /* Distance to the nearest water/land tile */
194 
195  /* Square of Euclidian distance from town */
196  case 0x8D: return min(DistanceSquare(this->industry->town->xy, this->tile), 65535);
197 
198  /* 32 random bits */
199  case 0x8F: return this->random_bits;
200  }
201  }
202 
203  const IndustrySpec *indspec = GetIndustrySpec(this->type);
204 
205  if (this->industry == NULL) {
206  DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, this->ro.callback);
207 
208  *available = false;
209  return UINT_MAX;
210  }
211 
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;
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);
223  }
224  } else {
225  return 0;
226  }
227  }
228 
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);
233 
234  /* Layout number */
235  case 0x44: return this->industry->selected_layout;
236 
237  /* Company info */
238  case 0x45: {
239  byte colours = 0;
240  bool is_ai = false;
241 
242  const Company *c = Company::GetIfValid(this->industry->founder);
243  if (c != NULL) {
244  const Livery *l = &c->livery[LS_DEFAULT];
245 
246  is_ai = c->is_ai;
247  colours = l->colour1 + l->colour2 * 16;
248  }
249 
250  return this->industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
251  }
252 
253  case 0x46: return this->industry->construction_date; // Date when built - long format - (in days)
254 
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);
257 
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;
263  }
264 
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);
269 
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);
276  }
277  return 0xFFFFFFFF;
278  }
279 
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);
292 
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);
303  }
304  return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, this->industry);
305  }
306 
307  case 0x69:
308  case 0x6A:
309  case 0x6B:
310  case 0x6C:
311  case 0x6D: {
312  CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile);
313  int index = this->industry->GetCargoProducedIndex(cargo);
314  if (index < 0) return 0; // invalid cargo
315  if (variable == 0x69) return this->industry->produced_cargo_waiting[index];
316  if (variable == 0x6A) return this->industry->this_month_production[index];
317  if (variable == 0x6B) return this->industry->this_month_transported[index];
318  if (variable == 0x6C) return this->industry->last_month_production[index];
319  if (variable == 0x6D) return this->industry->last_month_transported[index];
320  NOT_REACHED();
321  }
322 
323 
324  case 0x6E:
325  case 0x6F: {
326  CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile);
327  int index = this->industry->GetCargoAcceptedIndex(cargo);
328  if (index < 0) return 0; // invalid cargo
329  if (variable == 0x6E) return this->industry->last_cargo_accepted_at[index];
330  if (variable == 0x6F) return this->industry->incoming_cargo_waiting[index];
331  NOT_REACHED();
332  }
333 
334  /* Get a variable from the persistent storage */
335  case 0x7C: return (this->industry->psa != NULL) ? this->industry->psa->GetValue(parameter) : 0;
336 
337  /* Industry structure access*/
338  case 0x80: return this->industry->location.tile;
339  case 0x81: return GB(this->industry->location.tile, 8, 8);
340  /* Pointer to the town the industry is associated with */
341  case 0x82: return this->industry->town->index;
342  case 0x83:
343  case 0x84:
344  case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
345  case 0x86: return this->industry->location.w;
346  case 0x87: return this->industry->location.h;// xy dimensions
347 
348  case 0x88:
349  case 0x89: return this->industry->produced_cargo[variable - 0x88];
350  case 0x8A: return this->industry->produced_cargo_waiting[0];
351  case 0x8B: return GB(this->industry->produced_cargo_waiting[0], 8, 8);
352  case 0x8C: return this->industry->produced_cargo_waiting[1];
353  case 0x8D: return GB(this->industry->produced_cargo_waiting[1], 8, 8);
354  case 0x8E:
355  case 0x8F: return this->industry->production_rate[variable - 0x8E];
356  case 0x90:
357  case 0x91:
358  case 0x92: return this->industry->accepts_cargo[variable - 0x90];
359  case 0x93: return this->industry->prod_level;
360  /* amount of cargo produced so far THIS month. */
361  case 0x94: return this->industry->this_month_production[0];
362  case 0x95: return GB(this->industry->this_month_production[0], 8, 8);
363  case 0x96: return this->industry->this_month_production[1];
364  case 0x97: return GB(this->industry->this_month_production[1], 8, 8);
365  /* amount of cargo transported so far THIS month. */
366  case 0x98: return this->industry->this_month_transported[0];
367  case 0x99: return GB(this->industry->this_month_transported[0], 8, 8);
368  case 0x9A: return this->industry->this_month_transported[1];
369  case 0x9B: return GB(this->industry->this_month_transported[1], 8, 8);
370  /* fraction of cargo transported LAST month. */
371  case 0x9C:
372  case 0x9D: return this->industry->last_month_pct_transported[variable - 0x9C];
373  /* amount of cargo produced LAST month. */
374  case 0x9E: return this->industry->last_month_production[0];
375  case 0x9F: return GB(this->industry->last_month_production[0], 8, 8);
376  case 0xA0: return this->industry->last_month_production[1];
377  case 0xA1: return GB(this->industry->last_month_production[1], 8, 8);
378  /* amount of cargo transported last month. */
379  case 0xA2: return this->industry->last_month_transported[0];
380  case 0xA3: return GB(this->industry->last_month_transported[0], 8, 8);
381  case 0xA4: return this->industry->last_month_transported[1];
382  case 0xA5: return GB(this->industry->last_month_transported[1], 8, 8);
383 
384  case 0xA6: return indspec->grf_prop.local_id;
385  case 0xA7: return this->industry->founder;
386  case 0xA8: return this->industry->random_colour;
387  case 0xA9: return Clamp(this->industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
388  case 0xAA: return this->industry->counter;
389  case 0xAB: return GB(this->industry->counter, 8, 8);
390  case 0xAC: return this->industry->was_cargo_delivered;
391 
392  case 0xB0: return Clamp(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
393  case 0xB3: return this->industry->construction_type; // Construction type
394  case 0xB4: {
395  Date *latest = std::max_element(this->industry->last_cargo_accepted_at, endof(this->industry->last_cargo_accepted_at));
396  return Clamp((*latest) - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days)
397  }
398  }
399 
400  DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
401 
402  *available = false;
403  return UINT_MAX;
404 }
405 
406 /* virtual */ uint32 IndustriesScopeResolver::GetRandomBits() const
407 {
408  return this->industry != NULL ? this->industry->random : 0;
409 }
410 
411 /* virtual */ uint32 IndustriesScopeResolver::GetTriggers() const
412 {
413  return 0;
414 }
415 
416 /* virtual */ void IndustriesScopeResolver::StorePSA(uint pos, int32 value)
417 {
418  if (this->industry->index == INVALID_INDUSTRY) return;
419 
420  if (this->industry->psa == NULL) {
421  /* There is no need to create a storage if the value is zero. */
422  if (value == 0) return;
423 
424  /* Create storage on first modification. */
425  const IndustrySpec *indsp = GetIndustrySpec(this->industry->type);
426  uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
428  this->industry->psa = new PersistentStorage(grfid, GSF_INDUSTRIES, this->industry->location.tile);
429  }
430 
431  this->industry->psa->StoreValue(pos, value);
432 }
433 
439 static const GRFFile *GetGrffile(IndustryType type)
440 {
441  const IndustrySpec *indspec = GetIndustrySpec(type);
442  return (indspec != NULL) ? indspec->grf_prop.grffile : NULL;
443 }
444 
456  CallbackID callback, uint32 callback_param1, uint32 callback_param2)
457  : ResolverObject(GetGrffile(type), callback, callback_param1, callback_param2),
458  industries_scope(*this, tile, indus, type, random_bits),
459  town_scope(NULL)
460 {
462 }
463 
464 IndustriesResolverObject::~IndustriesResolverObject()
465 {
466  delete this->town_scope;
467 }
468 
474 {
475  if (this->town_scope == NULL) {
476  Town *t = NULL;
477  bool readonly = true;
478  if (this->industries_scope.industry != NULL) {
479  t = this->industries_scope.industry->town;
480  readonly = this->industries_scope.industry->index == INVALID_INDUSTRY;
481  } else if (this->industries_scope.tile != INVALID_TILE) {
482  t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX);
483  }
484  if (t == NULL) return NULL;
485  this->town_scope = new TownScopeResolver(*this, t, readonly);
486  }
487  return this->town_scope;
488 }
489 
500 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
501 {
502  IndustriesResolverObject object(tile, industry, type, 0, callback, param1, param2);
503  return object.ResolveCallback();
504 }
505 
517 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
518 {
519  const IndustrySpec *indspec = GetIndustrySpec(type);
520 
521  Industry ind;
522  ind.index = INVALID_INDUSTRY;
523  ind.location.tile = tile;
524  ind.location.w = 0; // important to mark the industry invalid
525  ind.type = type;
526  ind.selected_layout = layout;
527  ind.town = ClosestTownFromTile(tile, UINT_MAX);
528  ind.random = initial_random_bits;
529  ind.founder = founder;
530  ind.psa = NULL;
531 
532  IndustriesResolverObject object(tile, &ind, type, seed, CBID_INDUSTRY_LOCATION, 0, creation_type);
533  uint16 result = object.ResolveCallback();
534 
535  /* Unlike the "normal" cases, not having a valid result means we allow
536  * the building of the industry, as that's how it's done in TTDP. */
537  if (result == CALLBACK_FAILED) return CommandCost();
538 
539  return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile, STR_ERROR_SITE_UNSUITABLE);
540 }
541 
548 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
549 {
550  const IndustrySpec *indspec = GetIndustrySpec(type);
551 
552  if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
553  uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
554  if (res != CALLBACK_FAILED) {
555  if (indspec->grf_prop.grffile->grf_version < 8) {
556  /* Disallow if result != 0 */
557  if (res != 0) default_prob = 0;
558  } else {
559  /* Use returned probability. 0x100 to use default */
560  if (res < 0x100) {
561  default_prob = res;
562  } else if (res > 0x100) {
564  }
565  }
566  }
567  }
568  return default_prob;
569 }
570 
571 static int32 DerefIndProd(int field, bool use_register)
572 {
573  return use_register ? (int32)GetRegister(field) : field;
574 }
575 
581 void IndustryProductionCallback(Industry *ind, int reason)
582 {
583  const IndustrySpec *spec = GetIndustrySpec(ind->type);
584  IndustriesResolverObject object(ind->location.tile, ind, ind->type);
585  if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
586  int multiplier = 1;
587  if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
588  object.callback_param2 = reason;
589 
590  for (uint loop = 0;; loop++) {
591  /* limit the number of calls to break infinite loops.
592  * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
593  if (loop >= 0x10000) {
594  /* display error message */
595  SetDParamStr(0, spec->grf_prop.grffile->filename);
596  SetDParam(1, spec->name);
597  ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
598 
599  /* abort the function early, this error isn't critical and will allow the game to continue to run */
600  break;
601  }
602 
603  SB(object.callback_param2, 8, 16, loop);
604  const SpriteGroup *tgroup = object.Resolve();
605  if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
606  const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
607 
608  if (group->version == 0xFF) {
609  /* Result was marked invalid on load, display error message */
610  SetDParamStr(0, spec->grf_prop.grffile->filename);
611  SetDParam(1, spec->name);
612  SetDParam(2, ind->location.tile);
613  ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_INVALID_CARGO_PRODUCTION_CALLBACK, WL_WARNING);
614 
615  /* abort the function early, this error isn't critical and will allow the game to continue to run */
616  break;
617  }
618 
619  bool deref = (group->version >= 1);
620 
621  if (group->version < 2) {
622  /* Callback parameters map directly to industry cargo slot indices */
623  for (uint i = 0; i < group->num_input; i++) {
624  ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
625  }
626  for (uint i = 0; i < group->num_output; i++) {
627  ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
628  }
629  } else {
630  /* Callback receives list of cargos to apply for, which need to have their cargo slots in industry looked up */
631  for (uint i = 0; i < group->num_input; i++) {
632  int cargo_index = ind->GetCargoAcceptedIndex(group->cargo_input[i]);
633  if (cargo_index < 0) continue;
634  ind->incoming_cargo_waiting[cargo_index] = Clamp(ind->incoming_cargo_waiting[cargo_index] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
635  }
636  for (uint i = 0; i < group->num_output; i++) {
637  int cargo_index = ind->GetCargoProducedIndex(group->cargo_output[i]);
638  if (cargo_index < 0) continue;
639  ind->produced_cargo_waiting[cargo_index] = Clamp(ind->produced_cargo_waiting[cargo_index] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
640  }
641  }
642 
643  int32 again = DerefIndProd(group->again, deref);
644  if (again == 0) break;
645 
646  SB(object.callback_param2, 24, 8, again);
647  }
648 
650 }
651 
660 {
661  assert(std::find(ind->accepts_cargo, endof(ind->accepts_cargo), cargo_type) != endof(ind->accepts_cargo));
662 
663  const IndustrySpec *indspec = GetIndustrySpec(ind->type);
664  if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
666  0, indspec->grf_prop.grffile->cargo_map[cargo_type],
667  ind, ind->type, ind->location.tile);
669  }
670  return false;
671 }
Functions related to OTTD&#39;s strings.
static IndustryGfx GetCleanIndustryGfx(TileIndex t)
Get the industry graphics ID for the given industry tile as stored in the without translation...
Definition: industry_map.h:127
Called to determine if the given industry can be built on specific area.
Definition of stuff that is very close to a company, like the company struct itself.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
uint32 GetTriggers() const
Get the triggers.
CargoID cargo_output[INDUSTRY_NUM_OUTPUTS]
Which output cargoes to add to (only cb version 2)
byte production_rate[INDUSTRY_NUM_OUTPUTS]
production rate for each cargo
Definition: industry.h:47
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
int16 subtract_input[INDUSTRY_NUM_INPUTS]
Take this much of the input cargo (can be negative, is indirect in cb version 1+) ...
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3201
ResolverObject & ro
Surrounding resolver object.
uint16 counter
used for animation and/or production (if available cargo)
Definition: industry.h:55
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the &#39;_date == 0&#39; till &#39;ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)&#39;.
Definition: date_type.h:82
OwnerByte founder
Founder of the industry.
Definition: industry.h:65
Functions for NewGRF industries.
Town * town
Nearest town.
Definition: industry.h:43
static byte GetAnimationFrame(TileIndex t)
Get the current animation frame.
Definition: tile_map.h:252
Functions related to debugging.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
static const IndustryGfx INVALID_INDUSTRYTILE
one above amount is considered invalid
Definition: industry_type.h:36
CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Interface for SpriteGroup-s to access the gamestate.
uint32 GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
Check with callback CBID_INDUSTRY_PROBABILITY whether the industry can be built.
byte selected_layout
Which tile layout was used when creating the industry.
Definition: industry.h:69
CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
Check that the industry callback allows creation of the industry.
uint8 construction_type
Way the industry was constructed (.
Definition: industry.h:67
TownScopeResolver * town_scope
Scope resolver for the associated town (if needed and available, else NULL).
uint16 callback_mask
Bitmask of industry callbacks that have to be called.
Definition: industrytype.h:135
Defines the internal data of a functional industry.
Definition: industry.h:41
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets, bool grf_version8)
Based on newhouses equivalent, but adapted for newindustries.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
void IndustryProductionCallback(Industry *ind, int reason)
Get the industry production callback and apply it to the industry.
Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS]
Last day each cargo type was accepted by this industry.
Definition: industry.h:68
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
byte was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station...
Definition: industry.h:61
uint16 random
Random value used for randomisation of all kinds of things.
Definition: industry.h:71
Common return value for all commands.
Definition: command_type.h:25
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
uint16 w
The width of the area.
Definition: tilearea_type.h:20
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error)
Get the error message from a shape/location/slope check callback result.
byte random_colour
randomized colour of the industry, for display purpose
Definition: industry.h:59
StringID name
Displayed name of the industry.
Definition: industrytype.h:124
Automatic production multiplier handling.
Definition: industrytype.h:79
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
Check whether an industry temporarily refuses to accept a certain cargo.
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:123
Date construction_date
Date of the construction of the industry.
Definition: industry.h:66
Pseudo random number generator.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=NULL, uint textref_stack_size=0, const uint32 *textref_stack=NULL)
Display an error message in a window.
Definition: error_gui.cpp:378
uint16 add_output[INDUSTRY_NUM_OUTPUTS]
Add this much output cargo when successful (unsigned, is indirect in cb version 1+) ...
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:3329
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:282
uint16 this_month_production[INDUSTRY_NUM_OUTPUTS]
stats of this month&#39;s production per cargo
Definition: industry.h:50
uint8 num_output
How many add_output values are valid.
uint32 callback_param2
Second parameter (var 18) of the callback.
static uint32 GetRegister(uint i)
Gets the value of a so-called newgrf "register".
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
TileIndex xy
town center tile
Definition: town.h:56
Other information.
Definition: error.h:24
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets, Axis axis)
Get the tile at the given offset.
TYPE GetValue(uint pos) const
Gets the value from a given position.
Functions related to errors.
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:73
Called to determine if the given industry type is available.
uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS]
stats of this month&#39;s transport per cargo
Definition: industry.h:51
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
Year last_prod_year
last year of production
Definition: industry.h:60
bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Scope resolver for a town.
Definition: newgrf_town.h:24
IndustryType type
type of industry.
Definition: industry.h:57
IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
Map the GRF local type to an industry type.
static const IndustryGfx NEW_INDUSTRYTILEOFFSET
original number of tiles
Definition: industry_type.h:34
TileArea location
Location of the industry.
Definition: industry.h:42
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]
total units produced per cargo in the last full month
Definition: industry.h:53
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
Information about a particular livery.
Definition: livery.h:80
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]
16 production cargo slots
Definition: industry.h:44
Class for pooled persistent storage of data.
static const IndustryGfx NUM_INDUSTRYTILES
total number of industry tiles, new and old
Definition: industry_type.h:35
uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
Make an analysis of a tile and check for its belonging to the same industry, and/or the same grf file...
Defines the data structure for constructing industry.
Definition: industrytype.h:103
static uint16 GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition: industry.h:147
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition: company_base.h:92
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:167
IndustryType type
Type of the industry.
const IndustryTileSpec * GetIndustryTileSpec(IndustryGfx gfx)
Accessor for array _industry_tile_specs.
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 input cargo slots
Definition: industry.h:49
IndustryAvailabilityCallType
From where has callback CBID_INDUSTRY_PROBABILITY been called.
Industry view; Window numbers:
Definition: window_type.h:358
uint8 cargo_map[NUM_CARGO]
Inverse cargo translation table (CargoID -> local ID)
Definition: newgrf.h:126
uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS]
incoming cargo waiting to be processed
Definition: industry.h:46
IndustryBehaviour behaviour
How this industry will behave, and how others entities can use it.
Definition: industrytype.h:122
byte prod_level
general production level
Definition: industry.h:48
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:138
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:2039
Called to determine if the industry can still accept or refuse more cargo arrival.
static const Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
Definition: date_type.h:51
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:83
const struct SpriteGroup * spritegroup[Tcnt]
pointer to the different sprites of the entity
industry availability/probability callback
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
static const GRFFile * GetGrffile(IndustryType type)
Get the grf file associated with the given industry type.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
uint16 override
id of the entity been replaced by
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition: map.cpp:342
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const
Return the ID (if ever available) of a previously inserted entity.
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
static const IndustryType INVALID_INDUSTRYTYPE
one above amount is considered invalid
Definition: industry_type.h:29
uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS]
amount of cargo produced per cargo
Definition: industry.h:45
is built on water (oil rig)
Definition: industrytype.h:66
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
Cargo support for NewGRFs.
option out of accepting cargo
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:28
TownScopeResolver * GetTown()
Get or create the town scope object associated with the industry.
CallbackID callback
Callback being resolved.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
uint8 version
Production callback version used, or 0xFF if marked invalid.
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:412
Town data structure.
Definition: town.h:55
call production callback when cargo arrives at the industry
Resolver for industries.
static const IndustryType NEW_INDUSTRYOFFSET
original number of industry types
Definition: industry_type.h:27
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
static byte GetIndustryRandomBits(TileIndex tile)
Get the random bits for this tile.
Definition: industry_map.h:226
uint16 local_id
id defined by the grf file for this entity
uint32 GetRandomBits() const
Get a few random bits.
uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS]
total units transported per cargo in the last full month
Definition: industry.h:54
Base of all industries.
int32 Date
The type to store our dates in.
Definition: date_type.h:16
const struct GRFFile * grffile
grf file that introduced this entity
CallbackID
List of implemented NewGRF callbacks.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Base of the town class.
byte colour1
First colour, for all vehicles.
Definition: livery.h:82
Functions to handle the town part of NewGRF towns.
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition: industry.h:85
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Defines the data structure of each individual tile of an industry.
Definition: industrytype.h:151
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the &#39;Square&#39; distance between the two given tiles.
Definition: map.cpp:176
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Window functions not directly related to making/drawing windows.
uint32 random_bits
Random bits of the new industry.
static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, bool town_filter, const Industry *current)
Implementation of both var 67 and 68 since the mechanism is almost the same, it is easier to regroup ...
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]
percentage transported per cargo in the last full month
Definition: industry.h:52
uint8 num_input
How many subtract_input values are valid.
void StorePSA(uint pos, int32 value)
Store a value into the persistent storage area (PSA).
call production callback every 256 ticks
Industry * industry
Industry being resolved.
Production callback needs random bits in var 10.
Definition: industrytype.h:80
uint16 h
The height of the area.
Definition: tilearea_type.h:21
IndustriesScopeResolver industries_scope
Scope resolver for the industry.
CargoID cargo_input[INDUSTRY_NUM_INPUTS]
Which input cargoes to take from (only cb version 2)
TileIndex tile
Tile owned by the industry.
IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32 random_bits=0, CallbackID callback=CBID_NO_CALLBACK, uint32 callback_param1=0, uint32 callback_param2=0)
Constructor of the industries resolver.
uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
Perform an industry callback.
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104
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:201