OpenTTD
newgrf_commons.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
13 #include "stdafx.h"
14 #include "debug.h"
15 #include "landscape.h"
16 #include "house.h"
17 #include "industrytype.h"
18 #include "newgrf_config.h"
19 #include "clear_map.h"
20 #include "station_map.h"
21 #include "tree_map.h"
22 #include "tunnelbridge_map.h"
23 #include "newgrf_object.h"
24 #include "genworld.h"
25 #include "newgrf_spritegroup.h"
26 #include "newgrf_text.h"
27 #include "company_base.h"
28 #include "error.h"
29 #include "strings_func.h"
30 
31 #include "table/strings.h"
32 
33 #include "safeguards.h"
34 
41 OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
42 {
43  max_offset = offset;
44  max_new_entities = maximum;
45  invalid_ID = invalid;
46 
47  mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
48  entity_overrides = MallocT<uint16>(max_offset);
49  for (size_t i = 0; i < max_offset; i++) entity_overrides[i] = invalid;
50  grfid_overrides = CallocT<uint32>(max_offset);
51 }
52 
58 {
60  free(entity_overrides);
61  free(grfid_overrides);
62 }
63 
72 void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
73 {
74  assert(entity_type < max_offset);
75  /* An override can be set only once */
76  if (entity_overrides[entity_type] != invalid_ID) return;
77  entity_overrides[entity_type] = local_id;
78  grfid_overrides[entity_type] = grfid;
79 }
80 
83 {
84  memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
85 }
86 
89 {
90  for (uint16 i = 0; i < max_offset; i++) {
91  entity_overrides[i] = invalid_ID;
92  grfid_overrides[i] = 0;
93  }
94 }
95 
102 uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid) const
103 {
104  const EntityIDMapping *map;
105 
106  for (uint16 id = 0; id < max_new_entities; id++) {
107  map = &mapping_ID[id];
108  if (map->entity_id == grf_local_id && map->grfid == grfid) {
109  return id;
110  }
111  }
112 
113  return invalid_ID;
114 }
115 
123 uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
124 {
125  uint16 id = this->GetID(grf_local_id, grfid);
126  EntityIDMapping *map;
127 
128  /* Look to see if this entity has already been added. This is done
129  * separately from the loop below in case a GRF has been deleted, and there
130  * are any gaps in the array.
131  */
132  if (id != invalid_ID) {
133  return id;
134  }
135 
136  /* This entity hasn't been defined before, so give it an ID now. */
137  for (id = max_offset; id < max_new_entities; id++) {
138  map = &mapping_ID[id];
139 
140  if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
141  map->entity_id = grf_local_id;
142  map->grfid = grfid;
143  map->substitute_id = substitute_id;
144  return id;
145  }
146  }
147 
148  return invalid_ID;
149 }
150 
156 uint32 OverrideManagerBase::GetGRFID(uint16 entity_id) const
157 {
158  return mapping_ID[entity_id].grfid;
159 }
160 
166 uint16 OverrideManagerBase::GetSubstituteID(uint16 entity_id) const
167 {
168  return mapping_ID[entity_id].substitute_id;
169 }
170 
177 {
178  HouseID house_id = this->AddEntityID(hs->grf_prop.local_id, hs->grf_prop.grffile->grfid, hs->grf_prop.subst_id);
179 
180  if (house_id == invalid_ID) {
181  grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
182  return;
183  }
184 
185  MemCpyT(HouseSpec::Get(house_id), hs);
186 
187  /* Now add the overrides. */
188  for (int i = 0; i != max_offset; i++) {
189  HouseSpec *overridden_hs = HouseSpec::Get(i);
190 
191  if (entity_overrides[i] != hs->grf_prop.local_id || grfid_overrides[i] != hs->grf_prop.grffile->grfid) continue;
192 
193  overridden_hs->grf_prop.override = house_id;
194  entity_overrides[i] = invalid_ID;
195  grfid_overrides[i] = 0;
196  }
197 }
198 
205 uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid) const
206 {
207  uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
208  if (id != invalid_ID) return id;
209 
210  /* No mapping found, try the overrides */
211  for (id = 0; id < max_offset; id++) {
212  if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
213  }
214 
215  return invalid_ID;
216 }
217 
225 uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
226 {
227  /* This entity hasn't been defined before, so give it an ID now. */
228  for (uint16 id = 0; id < max_new_entities; id++) {
229  /* Skip overridden industries */
230  if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
231 
232  /* Get the real live industry */
233  const IndustrySpec *inds = GetIndustrySpec(id);
234 
235  /* This industry must be one that is not available(enabled), mostly because of climate.
236  * And it must not already be used by a grf (grffile == nullptr).
237  * So reserve this slot here, as it is the chosen one */
238  if (!inds->enabled && inds->grf_prop.grffile == nullptr) {
239  EntityIDMapping *map = &mapping_ID[id];
240 
241  if (map->entity_id == 0 && map->grfid == 0) {
242  /* winning slot, mark it as been used */
243  map->entity_id = grf_local_id;
244  map->grfid = grfid;
245  map->substitute_id = substitute_id;
246  return id;
247  }
248  }
249  }
250 
251  return invalid_ID;
252 }
253 
261 {
262  /* First step : We need to find if this industry is already specified in the savegame data. */
263  IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
264 
265  if (ind_id == invalid_ID) {
266  /* Not found.
267  * Or it has already been overridden, so you've lost your place old boy.
268  * Or it is a simple substitute.
269  * We need to find a free available slot */
270  ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
271  inds->grf_prop.override = invalid_ID; // make sure it will not be detected as overridden
272  }
273 
274  if (ind_id == invalid_ID) {
275  grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
276  return;
277  }
278 
279  /* Now that we know we can use the given id, copy the spec to its final destination... */
280  _industry_specs[ind_id] = *inds;
281  /* ... and mark it as usable*/
282  _industry_specs[ind_id].enabled = true;
283 }
284 
285 void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
286 {
287  IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
288 
289  if (indt_id == invalid_ID) {
290  grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
291  return;
292  }
293 
294  memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
295 
296  /* Now add the overrides. */
297  for (int i = 0; i < max_offset; i++) {
298  IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
299 
300  if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
301 
302  overridden_its->grf_prop.override = indt_id;
303  overridden_its->enabled = false;
304  entity_overrides[i] = invalid_ID;
305  grfid_overrides[i] = 0;
306  }
307 }
308 
316 {
317  /* First step : We need to find if this object is already specified in the savegame data. */
318  ObjectType type = this->GetID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid);
319 
320  if (type == invalid_ID) {
321  /* Not found.
322  * Or it has already been overridden, so you've lost your place old boy.
323  * Or it is a simple substitute.
324  * We need to find a free available slot */
325  type = this->AddEntityID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid, OBJECT_TRANSMITTER);
326  }
327 
328  if (type == invalid_ID) {
329  grfmsg(1, "Object.SetEntitySpec: Too many objects allocated. Ignoring.");
330  return;
331  }
332 
334 
335  /* Now that we know we can use the given id, copy the spec to its final destination. */
336  memcpy(&_object_specs[type], spec, sizeof(*spec));
337  ObjectClass::Assign(&_object_specs[type]);
338 }
339 
348 uint32 GetTerrainType(TileIndex tile, TileContext context)
349 {
351  case LT_TROPIC: return GetTropicZone(tile);
352  case LT_ARCTIC: {
353  bool has_snow;
354  switch (GetTileType(tile)) {
355  case MP_CLEAR:
356  /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
357  if (_generating_world) goto genworld;
358  has_snow = IsSnowTile(tile) && GetClearDensity(tile) >= 2;
359  break;
360 
361  case MP_RAILWAY: {
362  /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
363  if (_generating_world) goto genworld; // we do not care about foundations here
364  RailGroundType ground = GetRailGroundType(tile);
365  has_snow = (ground == RAIL_GROUND_ICE_DESERT || (context == TCX_UPPER_HALFTILE && ground == RAIL_GROUND_HALF_SNOW));
366  break;
367  }
368 
369  case MP_ROAD:
370  /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
371  if (_generating_world) goto genworld; // we do not care about foundations here
372  has_snow = IsOnSnow(tile);
373  break;
374 
375  case MP_TREES: {
376  /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
377  if (_generating_world) goto genworld;
378  TreeGround ground = GetTreeGround(tile);
379  has_snow = (ground == TREE_GROUND_SNOW_DESERT || ground == TREE_GROUND_ROUGH_SNOW) && GetTreeDensity(tile) >= 2;
380  break;
381  }
382 
383  case MP_TUNNELBRIDGE:
384  if (context == TCX_ON_BRIDGE) {
385  has_snow = (GetBridgeHeight(tile) > GetSnowLine());
386  } else {
387  /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
388  if (_generating_world) goto genworld; // we do not care about foundations here
389  has_snow = HasTunnelBridgeSnowOrDesert(tile);
390  }
391  break;
392 
393  case MP_STATION:
394  case MP_HOUSE:
395  case MP_INDUSTRY:
396  case MP_OBJECT:
397  /* These tiles usually have a levelling foundation. So use max Z */
398  has_snow = (GetTileMaxZ(tile) > GetSnowLine());
399  break;
400 
401  case MP_VOID:
402  case MP_WATER:
403  genworld:
404  has_snow = (GetTileZ(tile) > GetSnowLine());
405  break;
406 
407  default: NOT_REACHED();
408  }
409  return has_snow ? 4 : 0;
410  }
411  default: return 0;
412  }
413 }
414 
423 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets, Axis axis)
424 {
425  int8 x = GB(parameter, 0, 4);
426  int8 y = GB(parameter, 4, 4);
427 
428  if (signed_offsets && x >= 8) x -= 16;
429  if (signed_offsets && y >= 8) y -= 16;
430 
431  /* Swap width and height depending on axis for railway stations */
432  if (axis == INVALID_AXIS && HasStationTileRail(tile)) axis = GetRailStationAxis(tile);
433  if (axis == AXIS_Y) Swap(x, y);
434 
435  /* Make sure we never roam outside of the map, better wrap in that case */
436  return TILE_MASK(tile + TileDiffXY(x, y));
437 }
438 
446 uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8)
447 {
448  TileType tile_type = GetTileType(tile);
449 
450  /* Fake tile type for trees on shore */
451  if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
452 
453  int z;
454  Slope tileh = GetTilePixelSlope(tile, &z);
455  /* Return 0 if the tile is a land tile */
456  byte terrain_type = (HasTileWaterClass(tile) ? (GetWaterClass(tile) + 1) & 3 : 0) << 5 | GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
457  if (grf_version8) z /= TILE_HEIGHT;
458  return tile_type << 24 | Clamp(z, 0, 0xFF) << 16 | terrain_type << 8 | tileh;
459 }
460 
467 uint32 GetCompanyInfo(CompanyID owner, const Livery *l)
468 {
469  if (l == nullptr && Company::IsValidID(owner)) l = &Company::Get(owner)->livery[LS_DEFAULT];
470  return owner | (Company::IsValidAiID(owner) ? 0x10000 : 0) | (l != nullptr ? (l->colour1 << 24) | (l->colour2 << 28) : 0);
471 }
472 
480 CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error)
481 {
482  CommandCost res;
483 
484  if (cb_res < 0x400) {
485  res = CommandCost(GetGRFStringID(grffile->grfid, 0xD000 + cb_res));
486  } else {
487  switch (cb_res) {
488  case 0x400: return res; // No error.
489 
490  default: // unknown reason -> default error
491  case 0x401: res = CommandCost(default_error); break;
492 
493  case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
494  case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
495  case 0x404: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_ABOVE_SNOW_LINE); break;
496  case 0x405: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_BELOW_SNOW_LINE); break;
497  case 0x406: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_SEA); break;
498  case 0x407: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_CANAL); break;
499  case 0x408: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_RIVER); break;
500  }
501  }
502 
503  /* Copy some parameters from the registers to the error message text ref. stack */
504  res.UseTextRefStack(grffile, 4);
505 
506  return res;
507 }
508 
516 void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
517 {
518  GRFConfig *grfconfig = GetGRFConfig(grfid);
519 
520  if (!HasBit(grfconfig->grf_bugs, GBUG_UNKNOWN_CB_RESULT)) {
522  SetDParamStr(0, grfconfig->GetName());
523  SetDParam(1, cbid);
524  SetDParam(2, cb_res);
525  ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT, WL_CRITICAL);
526  }
527 
528  /* debug output */
529  char buffer[512];
530 
531  SetDParamStr(0, grfconfig->GetName());
532  GetString(buffer, STR_NEWGRF_BUGGY, lastof(buffer));
533  DEBUG(grf, 0, "%s", buffer + 3);
534 
535  SetDParam(1, cbid);
536  SetDParam(2, cb_res);
537  GetString(buffer, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT, lastof(buffer));
538  DEBUG(grf, 0, "%s", buffer + 3);
539 }
540 
550 bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
551 {
552  assert(cb_res != CALLBACK_FAILED); // We do not know what to return
553 
554  if (grffile->grf_version < 8) return cb_res != 0;
555 
556  if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
557  return cb_res != 0;
558 }
559 
569 bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
570 {
571  assert(cb_res != CALLBACK_FAILED); // We do not know what to return
572 
573  if (grffile->grf_version < 8) return GB(cb_res, 0, 8) != 0;
574 
575  if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
576  return cb_res != 0;
577 }
578 
579 
580 /* static */ std::vector<DrawTileSeqStruct> NewGRFSpriteLayout::result_seq;
581 
587 {
588  assert(this->seq == nullptr);
589  assert(source != nullptr);
590 
591  size_t count = 1; // 1 for the terminator
592  const DrawTileSeqStruct *element;
593  foreach_draw_tile_seq(element, source) count++;
594 
595  DrawTileSeqStruct *sprites = MallocT<DrawTileSeqStruct>(count);
596  MemCpyT(sprites, source, count);
597  this->seq = sprites;
598 }
599 
605 {
606  this->Clone((const DrawTileSprites*)source);
607 
608  if (source->registers != nullptr) {
609  size_t count = 1; // 1 for the ground sprite
610  const DrawTileSeqStruct *element;
611  foreach_draw_tile_seq(element, source->seq) count++;
612 
613  TileLayoutRegisters *regs = MallocT<TileLayoutRegisters>(count);
614  MemCpyT(regs, source->registers, count);
615  this->registers = regs;
616  }
617 }
618 
619 
624 void NewGRFSpriteLayout::Allocate(uint num_sprites)
625 {
626  assert(this->seq == nullptr);
627 
628  DrawTileSeqStruct *sprites = CallocT<DrawTileSeqStruct>(num_sprites + 1);
629  sprites[num_sprites].MakeTerminator();
630  this->seq = sprites;
631 }
632 
637 {
638  assert(this->seq != nullptr);
639  assert(this->registers == nullptr);
640 
641  size_t count = 1; // 1 for the ground sprite
642  const DrawTileSeqStruct *element;
643  foreach_draw_tile_seq(element, this->seq) count++;
644 
645  this->registers = CallocT<TileLayoutRegisters>(count);
646 }
647 
660 uint32 NewGRFSpriteLayout::PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const
661 {
662  result_seq.clear();
663  uint32 var10_values = 0;
664 
665  /* Create a copy of the spritelayout, so we can modify some values.
666  * Also include the groundsprite into the sequence for easier processing. */
667  /*C++17: DrawTileSeqStruct *result = &*/ result_seq.emplace_back();
668  DrawTileSeqStruct *result = &result_seq.back();
669  result->image = ground;
670  result->delta_x = 0;
671  result->delta_y = 0;
672  result->delta_z = (int8)0x80;
673 
674  const DrawTileSeqStruct *dtss;
675  foreach_draw_tile_seq(dtss, this->seq) {
676  result_seq.push_back(*dtss);
677  }
678  result_seq.emplace_back() /*C++17: .MakeTerminator()*/;
679  result_seq.back().MakeTerminator();
680  /* Determine the var10 values the action-1-2-3 chains needs to be resolved for,
681  * and apply the default sprite offsets (unless disabled). */
682  const TileLayoutRegisters *regs = this->registers;
683  bool ground = true;
684  foreach_draw_tile_seq(result, result_seq.data()) {
685  TileLayoutFlags flags = TLF_NOTHING;
686  if (regs != nullptr) flags = regs->flags;
687 
688  /* Record var10 value for the sprite */
689  if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_SPRITE_REG_FLAGS)) {
690  uint8 var10 = (flags & TLF_SPRITE_VAR10) ? regs->sprite_var10 : (ground && separate_ground ? 1 : 0);
691  SetBit(var10_values, var10);
692  }
693 
694  /* Add default sprite offset, unless there is a custom one */
695  if (!(flags & TLF_SPRITE)) {
696  if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
697  result->image.sprite += ground ? newgrf_ground_offset : newgrf_offset;
698  if (constr_stage > 0 && regs != nullptr) result->image.sprite += GetConstructionStageOffset(constr_stage, regs->max_sprite_offset);
699  } else {
700  result->image.sprite += orig_offset;
701  }
702  }
703 
704  /* Record var10 value for the palette */
705  if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_PALETTE_REG_FLAGS)) {
706  uint8 var10 = (flags & TLF_PALETTE_VAR10) ? regs->palette_var10 : (ground && separate_ground ? 1 : 0);
707  SetBit(var10_values, var10);
708  }
709 
710  /* Add default palette offset, unless there is a custom one */
711  if (!(flags & TLF_PALETTE)) {
712  if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
713  result->image.sprite += ground ? newgrf_ground_offset : newgrf_offset;
714  if (constr_stage > 0 && regs != nullptr) result->image.sprite += GetConstructionStageOffset(constr_stage, regs->max_palette_offset);
715  }
716  }
717 
718  ground = false;
719  if (regs != nullptr) regs++;
720  }
721 
722  return var10_values;
723 }
724 
733 void NewGRFSpriteLayout::ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const
734 {
735  DrawTileSeqStruct *result;
736  const TileLayoutRegisters *regs = this->registers;
737  bool ground = true;
738  foreach_draw_tile_seq(result, result_seq.data()) {
739  TileLayoutFlags flags = TLF_NOTHING;
740  if (regs != nullptr) flags = regs->flags;
741 
742  /* Is the sprite or bounding box affected by an action-1-2-3 chain? */
743  if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_SPRITE_REG_FLAGS)) {
744  /* Does the var10 value apply to this sprite? */
745  uint8 var10 = (flags & TLF_SPRITE_VAR10) ? regs->sprite_var10 : (ground && separate_ground ? 1 : 0);
746  if (var10 == resolved_var10) {
747  /* Apply registers */
748  if ((flags & TLF_DODRAW) && GetRegister(regs->dodraw) == 0) {
749  result->image.sprite = 0;
750  } else {
751  if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) result->image.sprite += resolved_sprite;
752  if (flags & TLF_SPRITE) {
753  int16 offset = (int16)GetRegister(regs->sprite); // mask to 16 bits to avoid trouble
754  if (!HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (offset >= 0 && offset < regs->max_sprite_offset)) {
755  result->image.sprite += offset;
756  } else {
757  result->image.sprite = SPR_IMG_QUERY;
758  }
759  }
760 
761  if (result->IsParentSprite()) {
762  if (flags & TLF_BB_XY_OFFSET) {
763  result->delta_x += (int32)GetRegister(regs->delta.parent[0]);
764  result->delta_y += (int32)GetRegister(regs->delta.parent[1]);
765  }
766  if (flags & TLF_BB_Z_OFFSET) result->delta_z += (int32)GetRegister(regs->delta.parent[2]);
767  } else {
768  if (flags & TLF_CHILD_X_OFFSET) result->delta_x += (int32)GetRegister(regs->delta.child[0]);
769  if (flags & TLF_CHILD_Y_OFFSET) result->delta_y += (int32)GetRegister(regs->delta.child[1]);
770  }
771  }
772  }
773  }
774 
775  /* Is the palette affected by an action-1-2-3 chain? */
776  if (result->image.sprite != 0 && (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_PALETTE_REG_FLAGS))) {
777  /* Does the var10 value apply to this sprite? */
778  uint8 var10 = (flags & TLF_PALETTE_VAR10) ? regs->palette_var10 : (ground && separate_ground ? 1 : 0);
779  if (var10 == resolved_var10) {
780  /* Apply registers */
781  if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) result->image.pal += resolved_sprite;
782  if (flags & TLF_PALETTE) {
783  int16 offset = (int16)GetRegister(regs->palette); // mask to 16 bits to avoid trouble
784  if (!HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (offset >= 0 && offset < regs->max_palette_offset)) {
785  result->image.pal += offset;
786  } else {
787  result->image.sprite = SPR_IMG_QUERY;
788  result->image.pal = PAL_NONE;
789  }
790  }
791  }
792  }
793 
794  ground = false;
795  if (regs != nullptr) regs++;
796  }
797 }
Functions related to OTTD&#39;s strings.
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
Owner
Enum for all companies/owners.
Definition: company_type.h:18
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:275
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
Definition of stuff that is very close to a company, like the company struct itself.
void SetEntitySpec(ObjectSpec *spec)
Method to install the new object data in its proper slot The slot assignment is internal of this meth...
bool enabled
entity still available (by default true).newgrf can disable it, though
Definition: industrytype.h:139
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:238
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Add signed offset to child sprite Y positions from register TileLayoutRegisters::delta.child[1].
static const ObjectType NUM_OBJECTS
Number of supported objects overall.
Definition: object_type.h:25
static const ObjectType OBJECT_TRANSMITTER
The large antenna.
Definition: object_type.h:16
byte landscape
the landscape we&#39;re currently in
Maps accessors for stations.
uint16 invalid_ID
ID used to detected invalid entities;.
static bool IsSnowTile(TileIndex t)
Test if a tile is covered with snow.
Definition: clear_map.h:35
Part of an industry.
Definition: tile_type.h:49
void Allocate(uint num_sprites)
Allocate a spritelayout for num_sprites building sprites.
TileLayoutFlags
Flags to enable register usage in sprite layouts.
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:106
TileType
The different types of tiles.
Definition: tile_type.h:40
Functions related to debugging.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
Reserves a place in the mapping array for an entity to be installed.
Add signed offset to bounding box X and Y positions from register TileLayoutRegisters::delta.parent[0..1].
A tile with road (or tram tracks)
Definition: tile_type.h:43
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 GetGRFID(uint16 entity_id) const
Gives the GRFID of the file the entity belongs to.
void Add(uint8 local_id, uint32 grfid, uint entity_type)
Since the entity IDs defined by the GRF file does not necessarily correlate to those used by the game...
GRFFilePropsBase< 2 > grf_prop
Properties related the the grf file.
Definition: newgrf_object.h:60
A snow tile that is rough underneath.
Definition: tree_map.h:57
uint16 max_palette_offset
Maximum offset to add to the palette. (limited by size of the spriteset)
Resolve sprite with a specific value in variable 10.
Flags which require resolving the action-1-2-3 chain for the sprite, even if it is no action-1 sprite...
Add signed offset to palette from register TileLayoutRegisters::palette.
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:380
void MakeTerminator()
Make this struct a sequence terminator.
Definition: sprite.h:35
Icy or sandy.
Definition: rail_map.h:498
void UseTextRefStack(const GRFFile *grffile, uint num_registers)
Activate usage of the NewGRF TextRefStack for the error message.
Definition: command.cpp:793
Add signed offset to sprite from register TileLayoutRegisters::sprite.
Allow incrementing of ObjectClassID variables.
Definition: newgrf_object.h:58
Industry type specs.
int GetBridgeHeight(TileIndex t)
Get the height (&#39;z&#39;) of a bridge.
Definition: bridge_map.cpp:70
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
StringID GetGRFStringID(uint32 grfid, StringID stringid)
Returns the index for this stringid associated with its grfID.
A railway.
Definition: tile_type.h:42
Map accessors for tree tiles.
Functions related to world/map generation.
Contains objects such as transmitters and owned land.
Definition: tile_type.h:51
void AllocateRegisters()
Allocate memory for register modifiers.
Common return value for all commands.
Definition: command_type.h:23
OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
Constructor of generic class.
uint16 HouseID
OpenTTD ID of house types.
Definition: house_type.h:13
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error)
Get the error message from a shape/location/slope check callback result.
const DrawTileSeqStruct * seq
Array of child sprites. Terminated with a terminator entry.
Definition: sprite.h:60
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121
ObjectSpec _object_specs[NUM_OBJECTS]
All the object specifications.
Functions related to NewGRF objects.
virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const
Return the ID (if ever available) of a previously inserted entity.
uint32 grfid
The GRF ID of the file the entity belongs to.
Snow only on higher part of slope (steep or one corner raised)
Definition: rail_map.h:500
Only draw sprite if value of register TileLayoutRegisters::dodraw is non-zero.
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:24
uint16 GetID(uint8 grf_local_id, uint32 grfid) const override
Return the ID (if ever available) of a previously inserted entity.
Querying information about stuff on the bridge (via some bridgehead).
static bool IsOnSnow(TileIndex t)
Check if a road tile has snow/desert.
Definition: road_map.h:459
Header of Action 04 "universal holder" structure and functions.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
uint8 palette_var10
Value for variable 10 when resolving the palette.
static uint32 GetRegister(uint i)
Gets the value of a so-called newgrf "register".
void Clone(const DrawTileSeqStruct *source)
Clone the building sprites of a spritelayout.
uint16 max_new_entities
what is the amount of entities, old and new summed
Querying information about the upper part of a tile with halftile foundation.
uint32 grf_bugs
NOSAVE: bugs in this GRF in this run,.
Action 2 handling.
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets, Axis axis)
Get the tile at the given offset.
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
Functions related to errors.
The y axis.
uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const
Prepares a sprite layout before resolving action-1-2-3 chains.
uint16 max_sprite_offset
Maximum offset to add to the sprite. (limited by size of the spriteset)
Information about GRF, used in the game and (part of it) in savegames.
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:58
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition: company_base.h:138
uint8 substitute_id
The (original) entity ID to use if this GRF is not available.
definition of HouseSpec and accessors
Maps an entity id stored on the map to a GRF file.
virtual ~OverrideManagerBase()
Destructor of the generic class.
bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
#define foreach_draw_tile_seq(idx, list)
Iterate through all DrawTileSeqStructs in DrawTileSprites.
Definition: sprite.h:79
static bool HasStationTileRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:146
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
Definition of base types and functions in a cross-platform compatible way.
void SetEntitySpec(const HouseSpec *hs)
Install the specs into the HouseSpecs array It will find itself the proper slot on which it will go...
A number of safeguards to prevent using unsafe methods.
Water tile.
Definition: tile_type.h:47
static Axis GetRailStationAxis(TileIndex t)
Get the rail direction of a rail station.
Definition: station_map.h:337
void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const
Evaluates the register modifiers and integrates them into the preprocessed sprite layout...
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:280
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:16
Information about a particular livery.
Definition: livery.h:78
uint8 sprite_var10
Value for variable 10 when resolving the sprite.
static uint GetTreeDensity(TileIndex t)
Returns the &#39;density&#39; of a tile with trees.
Definition: tree_map.h:113
Set when a sprite originates from an Action 1.
Definition: sprites.h:1524
Defines the data structure for constructing industry.
Definition: industrytype.h:106
uint8 sprite
Register specifying a signed offset for the sprite.
Add signed offset to bounding box Z positions from register TileLayoutRegisters::delta.parent[2].
bool enabled
entity still available (by default true).newgrf can disable it, though
Definition: industrytype.h:170
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:171
bool IsParentSprite() const
Check whether this is a parent sprite with a boundingbox.
Definition: sprite.h:47
A callback returned an unknown/invalid result.
Definition: newgrf_config.h:46
GRFFileProps grf_prop
properties related to the grf file
Definition: industrytype.h:140
uint16 ObjectType
Types of objects.
Definition: object_type.h:14
NewGRF supplied spritelayout.
bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
a desert or snow tile, depend on landscape
Definition: tree_map.h:55
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:81
Flags which require resolving the action-1-2-3 chain for the palette, even if it is no action-1 palet...
Functions to find and configure NewGRFs.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:137
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
uint16 override
id of the entity been replaced by
uint8 entity_id
The entity ID within the GRF file.
static uint GetClearDensity(TileIndex t)
Get the density of a non-field clear tile.
Definition: clear_map.h:83
uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) override
Method to find an entity ID and to mark it as reserved for the Industry to be included.
Resolve palette with a specific value in variable 10.
Additional modifiers for items in sprite layouts.
EntityIDMapping * mapping_ID
mapping of ids from grf files. Public out of convenience
static bool HasTunnelBridgeSnowOrDesert(TileIndex t)
Tunnel: Is this tunnel entrance in a snowy or desert area? Bridge: Does the bridge ramp lie in a snow...
Tile got trees.
Definition: tile_type.h:45
GRFConfig * GetGRFConfig(uint32 grfid, uint32 mask)
Retrieve a NewGRF from the current config by its grfid.
uint8 child[2]
Registers for signed offsets for the position of child sprites.
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:60
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:50
Invisible tiles at the SW and SE border.
Definition: tile_type.h:48
void ResetMapping()
Resets the mapping, which is used while initializing game.
uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:141
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Map accessors for &#39;clear&#39; tiles.
static TreeGround GetTreeGround(TileIndex t)
Returns the groundtype for tree tiles.
Definition: tree_map.h:88
Add signed offset to child sprite X positions from register TileLayoutRegisters::delta.child[0].
int8 delta_z
0x80 identifies child sprites
Definition: sprite.h:28
void CDECL grfmsg(int severity, const char *str,...)
DEBUG() function dedicated to newGRF debugging messages Function is essentially the same as DEBUG(grf...
Definition: newgrf.cpp:375
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
uint32 GetCompanyInfo(CompanyID owner, const Livery *l)
Returns company information like in vehicle var 43 or station var 43.
A tile of a station.
Definition: tile_type.h:46
Functions related to OTTD&#39;s landscape.
byte GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:644
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
uint16 local_id
id defined by the grf file for this entity
RailGroundType
The ground &#39;under&#39; the rail.
Definition: rail_map.h:485
Functions that have tunnels and bridges in common.
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
const char * GetName() const
Get the name of this grf.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
GRFFileProps grf_prop
Properties related the the grf file.
Definition: house.h:114
void ResetOverride()
Resets the override, which is used while initializing game.
const struct GRFFile * grffile
grf file that introduced this entity
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static bool HasTileWaterClass(TileIndex t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:95
byte colour1
First colour, for all vehicles.
Definition: livery.h:80
static uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
Determines which sprite to use from a spriteset for a specific construction stage.
GameCreationSettings game_creation
settings used during the creation of a game (map)
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:41
A house by a town.
Definition: tile_type.h:44
Defines the data structure of each individual tile of an industry.
Definition: industrytype.h:155
TreeGround
Enumeration for ground types of tiles with trees.
Definition: tree_map.h:52
int8 delta_x
0x80 is sequence terminator
Definition: sprite.h:26
uint16 max_offset
what is the length of the original entity&#39;s array of specs
Flag for an invalid Axis.
SpriteID sprite
The &#39;real&#39; sprite.
Definition: gfx_type.h:23
uint8 parent[3]
Registers for signed offsets for the bounding box position of parent sprites.
uint8 dodraw
Register deciding whether the sprite shall be drawn at all. Non-zero means drawing.
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition: sprite.h:25
void SetEntitySpec(IndustrySpec *inds)
Method to install the new industry data in its proper slot The slot assignment is internal of this me...
Axis
Allow incrementing of DiagDirDiff variables.
#define TILE_MASK(x)
&#39;Wraps&#39; the given tile to it is within the map.
Definition: map_func.h:26
static std::vector< DrawTileSeqStruct > result_seq
Temporary storage when preprocessing spritelayouts.
uint8 palette
Register specifying a signed offset for the palette.
TileLayoutFlags flags
Flags defining which members are valid and to be used.
uint16 GetSubstituteID(uint16 entity_id) const
Gives the substitute of the entity, as specified by the grf file.
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:24
TileContext
Context for tile accesses.