OpenTTD Source  13.2.1
object_cmd.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 
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "command_func.h"
13 #include "viewport_func.h"
14 #include "company_base.h"
15 #include "town.h"
16 #include "bridge_map.h"
17 #include "genworld.h"
18 #include "autoslope.h"
19 #include "clear_func.h"
20 #include "water.h"
21 #include "window_func.h"
22 #include "company_gui.h"
23 #include "cheat_type.h"
24 #include "object.h"
25 #include "cargopacket.h"
26 #include "core/random_func.hpp"
27 #include "core/pool_func.hpp"
28 #include "object_map.h"
29 #include "object_base.h"
30 #include "newgrf_config.h"
31 #include "newgrf_object.h"
32 #include "date_func.h"
33 #include "newgrf_debug.h"
34 #include "vehicle_func.h"
35 #include "station_func.h"
36 #include "object_cmd.h"
37 #include "landscape_cmd.h"
38 
39 #include "table/strings.h"
40 #include "table/object_land.h"
41 
42 #include "safeguards.h"
43 
44 ObjectPool _object_pool("Object");
47 
53 /* static */ Object *Object::GetByTile(TileIndex tile)
54 {
55  return Object::Get(GetObjectIndex(tile));
56 }
57 
65 {
66  assert(IsTileType(t, MP_OBJECT));
67  return Object::GetByTile(t)->type;
68 }
69 
72 {
74 }
75 
86 void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town, uint8 view)
87 {
88  const ObjectSpec *spec = ObjectSpec::Get(type);
89 
90  TileArea ta(tile, GB(spec->size, HasBit(view, 0) ? 4 : 0, 4), GB(spec->size, HasBit(view, 0) ? 0 : 4, 4));
91  Object *o = new Object();
92  o->type = type;
93  o->location = ta;
94  o->town = town == nullptr ? CalcClosestTownFromTile(tile) : town;
95  o->build_date = _date;
96  o->view = view;
97 
98  /* If nothing owns the object, the colour will be random. Otherwise
99  * get the colour from the company's livery settings. */
100  if (owner == OWNER_NONE) {
101  o->colour = Random();
102  } else {
103  const Livery *l = Company::Get(owner)->livery;
104  o->colour = l->colour1 + l->colour2 * 16;
105  }
106 
107  /* If the object wants only one colour, then give it that colour. */
108  if ((spec->flags & OBJECT_FLAG_2CC_COLOUR) == 0) o->colour &= 0xF;
109 
110  if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) {
111  uint16 res = GetObjectCallback(CBID_OBJECT_COLOUR, o->colour, 0, spec, o, tile);
112  if (res != CALLBACK_FAILED) {
113  if (res >= 0x100) ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_COLOUR, res);
114  o->colour = GB(res, 0, 8);
115  }
116  }
117 
118  assert(o->town != nullptr);
119 
120  for (TileIndex t : ta) {
122  /* Update company infrastructure counts for objects build on canals owned by nobody. */
123  if (wc == WATER_CLASS_CANAL && owner != OWNER_NONE && (IsTileOwner(t, OWNER_NONE) || IsTileOwner(t, OWNER_WATER))) {
124  Company::Get(owner)->infrastructure.water++;
126  }
127  bool remove = IsDockingTile(t);
128  MakeObject(t, owner, o->index, wc, Random());
129  if (remove) RemoveDockingTile(t);
131  }
132 
133  Object::IncTypeCount(type);
135 }
136 
142 {
143  TileArea ta = Object::GetByTile(tile)->location;
144  for (TileIndex t : ta) {
147  }
148 }
149 
151 #define GetCompanyHQSize GetAnimationFrame
152 
153 #define IncreaseCompanyHQSize IncreaseAnimationStage
154 
160 void UpdateCompanyHQ(TileIndex tile, uint score)
161 {
162  if (tile == INVALID_TILE) return;
163 
164  byte val = 0;
165  if (score >= 170) val++;
166  if (score >= 350) val++;
167  if (score >= 520) val++;
168  if (score >= 720) val++;
169 
170  while (GetCompanyHQSize(tile) < val) {
171  IncreaseCompanyHQSize(tile);
172  }
173 }
174 
180 {
181  for (Object *obj : Object::Iterate()) {
182  Owner owner = GetTileOwner(obj->location.tile);
183  /* Not the current owner, so colour doesn't change. */
184  if (owner != c->index) continue;
185 
186  const ObjectSpec *spec = ObjectSpec::GetByTile(obj->location.tile);
187  /* Using the object colour callback, so not using company colour. */
188  if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) continue;
189 
190  const Livery *l = c->livery;
191  obj->colour = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? (l->colour2 * 16) : 0) + l->colour1;
192  }
193 }
194 
195 extern CommandCost CheckBuildableTile(TileIndex tile, uint invalid_dirs, int &allowed_z, bool allow_steep, bool check_bridge);
196 static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags);
197 
207 {
209 
210  if (type >= NUM_OBJECTS) return CMD_ERROR;
211  const ObjectSpec *spec = ObjectSpec::Get(type);
212  if (_game_mode == GM_NORMAL && !spec->IsAvailable() && !_generating_world) return CMD_ERROR;
213  if ((_game_mode == GM_EDITOR || _generating_world) && !spec->WasEverAvailable()) return CMD_ERROR;
214 
215  if ((spec->flags & OBJECT_FLAG_ONLY_IN_SCENEDIT) != 0 && ((!_generating_world && _game_mode != GM_EDITOR) || _current_company != OWNER_NONE)) return CMD_ERROR;
216  if ((spec->flags & OBJECT_FLAG_ONLY_IN_GAME) != 0 && (_generating_world || _game_mode != GM_NORMAL || _current_company > MAX_COMPANIES)) return CMD_ERROR;
217  if (view >= spec->views) return CMD_ERROR;
218 
219  if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS);
220  if (Town::GetNumItems() == 0) return_cmd_error(STR_ERROR_MUST_FOUND_TOWN_FIRST);
221 
222  int size_x = GB(spec->size, HasBit(view, 0) ? 4 : 0, 4);
223  int size_y = GB(spec->size, HasBit(view, 0) ? 0 : 4, 4);
224  TileArea ta(tile, size_x, size_y);
225  for (TileIndex t : ta) {
226  if (!IsValidTile(t)) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP_SUB); // Might be off the map
227  }
228 
229  if (type == OBJECT_OWNED_LAND) {
230  /* Owned land is special as it can be placed on any slope. */
231  cost.AddCost(Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile));
232  } else {
233  /* Check the surface to build on. At this time we can't actually execute the
234  * the CLEAR_TILE commands since the newgrf callback later on can check
235  * some information about the tiles. */
236  bool allow_water = (spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0;
237  bool allow_ground = (spec->flags & OBJECT_FLAG_NOT_ON_LAND) == 0;
238  for (TileIndex t : ta) {
239  if (HasTileWaterGround(t)) {
240  if (!allow_water) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
241  if (!IsWaterTile(t)) {
242  /* Normal water tiles don't have to be cleared. For all other tile types clear
243  * the tile but leave the water. */
245  } else {
246  /* Can't build on water owned by another company. */
247  Owner o = GetTileOwner(t);
248  if (o != OWNER_NONE && o != OWNER_WATER) cost.AddCost(CheckOwnership(o, t));
249 
250  /* However, the tile has to be clear of vehicles. */
252  }
253  } else {
254  if (!allow_ground) return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER);
255  /* For non-water tiles, we'll have to clear it before building. */
256 
257  /* When relocating HQ, allow it to be relocated (partial) on itself. */
258  if (!(type == OBJECT_HQ &&
259  IsTileType(t, MP_OBJECT) &&
261  IsObjectType(t, OBJECT_HQ))) {
263  }
264  }
265  }
266 
267  /* So, now the surface is checked... check the slope of said surface. */
268  int allowed_z;
269  if (GetTileSlope(tile, &allowed_z) != SLOPE_FLAT) allowed_z++;
270 
271  for (TileIndex t : ta) {
272  uint16 callback = CALLBACK_FAILED;
274  TileIndex diff = t - tile;
275  callback = GetObjectCallback(CBID_OBJECT_LAND_SLOPE_CHECK, GetTileSlope(t), TileY(diff) << 4 | TileX(diff), spec, nullptr, t, view);
276  }
277 
278  if (callback == CALLBACK_FAILED) {
279  cost.AddCost(CheckBuildableTile(t, 0, allowed_z, false, false));
280  } else {
281  /* The meaning of bit 10 is inverted for a grf version < 8. */
282  if (spec->grf_prop.grffile->grf_version < 8) ToggleBit(callback, 10);
283  CommandCost ret = GetErrorMessageFromLocationCallbackResult(callback, spec->grf_prop.grffile, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
284  if (ret.Failed()) return ret;
285  }
286  }
287 
288  if (flags & DC_EXEC) {
289  /* This is basically a copy of the loop above with the exception that we now
290  * execute the commands and don't check for errors, since that's already done. */
291  for (TileIndex t : ta) {
292  if (HasTileWaterGround(t)) {
293  if (!IsWaterTile(t)) {
295  }
296  } else {
298  }
299  }
300  }
301  }
302  if (cost.Failed()) return cost;
303 
304  /* Finally do a check for bridges. */
305  for (TileIndex t : ta) {
306  if (IsBridgeAbove(t) && (
309  return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
310  }
311  }
312 
313  int hq_score = 0;
314  uint build_object_size = 1;
315  switch (type) {
316  case OBJECT_TRANSMITTER:
317  case OBJECT_LIGHTHOUSE:
318  if (!IsTileFlat(tile)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
319  break;
320 
321  case OBJECT_OWNED_LAND:
322  if (IsTileType(tile, MP_OBJECT) &&
323  IsTileOwner(tile, _current_company) &&
325  return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
326  }
327  break;
328 
329  case OBJECT_HQ: {
331  if (c->location_of_HQ != INVALID_TILE) {
332  /* Don't relocate HQ on the same location. */
333  if (c->location_of_HQ == tile) return_cmd_error(STR_ERROR_ALREADY_BUILT);
334  /* We need to persuade a bit harder to remove the old HQ. */
336  cost.AddCost(ClearTile_Object(c->location_of_HQ, flags));
337  _current_company = c->index;
338  }
339 
340  if (flags & DC_EXEC) {
341  hq_score = UpdateCompanyRatingAndValue(c, false);
342  c->location_of_HQ = tile;
344  }
345  break;
346  }
347 
348  case OBJECT_STATUE:
349  /* This may never be constructed using this method. */
350  return CMD_ERROR;
351 
352  default: // i.e. NewGRF provided.
353  build_object_size = size_x * size_y;
354  break;
355  }
356 
357  /* Don't allow building more objects if the company has reached its limit. */
359  if (c != nullptr && GB(c->build_object_limit, 16, 16) < build_object_size) {
360  return_cmd_error(STR_ERROR_BUILD_OBJECT_LIMIT_REACHED);
361  }
362 
363  if (flags & DC_EXEC) {
364  BuildObject(type, tile, _current_company == OWNER_DEITY ? OWNER_NONE : _current_company, nullptr, view);
365 
366  /* Make sure the HQ starts at the right size. */
367  if (type == OBJECT_HQ) UpdateCompanyHQ(tile, hq_score);
368 
369  /* Subtract the tile from the build limit. */
370  if (c != nullptr) c->build_object_limit -= build_object_size << 16;
371  }
372 
373  cost.AddCost(spec->GetBuildCost() * build_object_size);
374  return cost;
375 }
376 
387 CommandCost CmdBuildObjectArea(DoCommandFlag flags, TileIndex tile, TileIndex start_tile, ObjectType type, uint8 view, bool diagonal)
388 {
389  if (start_tile >= MapSize()) return CMD_ERROR;
390 
391  if (type >= NUM_OBJECTS) return CMD_ERROR;
392  const ObjectSpec *spec = ObjectSpec::Get(type);
393  if (view >= spec->views) return CMD_ERROR;
394 
395  if (spec->size != OBJECT_SIZE_1X1) return CMD_ERROR;
396 
399  CommandCost last_error = CMD_ERROR;
400  bool had_success = false;
401 
403  int limit = (c == nullptr ? INT32_MAX : GB(c->build_object_limit, 16, 16));
404 
405  TileIterator *iter = diagonal ? (TileIterator *)new DiagonalTileIterator(tile, start_tile) : new OrthogonalTileIterator(tile, start_tile);
406  for (; *iter != INVALID_TILE; ++(*iter)) {
407  TileIndex t = *iter;
408  CommandCost ret = Command<CMD_BUILD_OBJECT>::Do(flags & ~DC_EXEC, t, type, view);
409 
410  /* If we've reached the limit, stop building (or testing). */
411  if (c != nullptr && limit-- <= 0) break;
412 
413  if (ret.Failed()) {
414  last_error = ret;
415  continue;
416  }
417 
418  had_success = true;
419  if (flags & DC_EXEC) {
420  money -= ret.GetCost();
421 
422  /* If we run out of money, stop building. */
423  if (ret.GetCost() > 0 && money < 0) break;
424  Command<CMD_BUILD_OBJECT>::Do(flags, t, type, view);
425  }
426  cost.AddCost(ret);
427  }
428 
429  delete iter;
430  return had_success ? cost : last_error;
431 }
432 
433 static Foundation GetFoundation_Object(TileIndex tile, Slope tileh);
434 
435 static void DrawTile_Object(TileInfo *ti)
436 {
437  ObjectType type = GetObjectType(ti->tile);
438  const ObjectSpec *spec = ObjectSpec::Get(type);
439 
440  /* Fall back for when the object doesn't exist anymore. */
441  if (!spec->enabled) type = OBJECT_TRANSMITTER;
442 
443  if ((spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) == 0) DrawFoundation(ti, GetFoundation_Object(ti->tile, ti->tileh));
444 
445  if (type < NEW_OBJECT_OFFSET) {
446  const DrawTileSprites *dts = nullptr;
447  Owner to = GetTileOwner(ti->tile);
448  PaletteID palette = to == OWNER_NONE ? PAL_NONE : COMPANY_SPRITE_COLOUR(to);
449 
450  if (type == OBJECT_HQ) {
451  TileIndex diff = ti->tile - Object::GetByTile(ti->tile)->location.tile;
452  dts = &_object_hq[GetCompanyHQSize(ti->tile) << 2 | TileY(diff) << 1 | TileX(diff)];
453  } else {
454  dts = &_objects[type];
455  }
456 
457  if (spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) {
458  /* If an object has no foundation, but tries to draw a (flat) ground
459  * type... we have to be nice and convert that for them. */
460  switch (dts->ground.sprite) {
461  case SPR_FLAT_BARE_LAND: DrawClearLandTile(ti, 0); break;
462  case SPR_FLAT_1_THIRD_GRASS_TILE: DrawClearLandTile(ti, 1); break;
463  case SPR_FLAT_2_THIRD_GRASS_TILE: DrawClearLandTile(ti, 2); break;
464  case SPR_FLAT_GRASS_TILE: DrawClearLandTile(ti, 3); break;
465  default: DrawGroundSprite(dts->ground.sprite, palette); break;
466  }
467  } else {
468  DrawGroundSprite(dts->ground.sprite, palette);
469  }
470 
472  const DrawTileSeqStruct *dtss;
473  foreach_draw_tile_seq(dtss, dts->seq) {
475  dtss->image.sprite, palette,
476  ti->x + dtss->delta_x, ti->y + dtss->delta_y,
477  dtss->size_x, dtss->size_y,
478  dtss->size_z, ti->z + dtss->delta_z,
480  );
481  }
482  }
483  } else {
484  DrawNewObjectTile(ti, spec);
485  }
486 
487  DrawBridgeMiddle(ti);
488 }
489 
490 static int GetSlopePixelZ_Object(TileIndex tile, uint x, uint y)
491 {
492  if (IsObjectType(tile, OBJECT_OWNED_LAND)) {
493  int z;
494  Slope tileh = GetTilePixelSlope(tile, &z);
495 
496  return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
497  } else {
498  return GetTileMaxPixelZ(tile);
499  }
500 }
501 
502 static Foundation GetFoundation_Object(TileIndex tile, Slope tileh)
503 {
505 }
506 
512 {
514  for (TileIndex tile_cur : o->location) {
515  DeleteNewGRFInspectWindow(GSF_OBJECTS, tile_cur);
516 
517  MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur));
518  }
519  delete o;
520 }
521 
522 std::vector<ClearedObjectArea> _cleared_object_areas;
523 
530 {
531  TileArea ta = TileArea(tile, 1, 1);
532 
533  for (ClearedObjectArea &coa : _cleared_object_areas) {
534  if (coa.area.Intersects(ta)) return &coa;
535  }
536 
537  return nullptr;
538 }
539 
540 static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags)
541 {
542  /* Get to the northern most tile. */
543  Object *o = Object::GetByTile(tile);
544  TileArea ta = o->location;
545 
546  ObjectType type = o->type;
547  const ObjectSpec *spec = ObjectSpec::Get(type);
548 
549  CommandCost cost(EXPENSES_CONSTRUCTION, spec->GetClearCost() * ta.w * ta.h / 5);
550  if (spec->flags & OBJECT_FLAG_CLEAR_INCOME) cost.MultiplyCost(-1); // They get an income!
551 
552  /* Towns can't remove any objects. */
553  if (_current_company == OWNER_TOWN) return CMD_ERROR;
554 
555  /* Water can remove everything! */
556  if (_current_company != OWNER_WATER) {
557  if ((flags & DC_NO_WATER) && IsTileOnWater(tile)) {
558  /* There is water under the object, treat it as water tile. */
559  return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
560  } else if (!(spec->flags & OBJECT_FLAG_AUTOREMOVE) && (flags & DC_AUTO)) {
561  /* No automatic removal by overbuilding stuff. */
562  return_cmd_error(type == OBJECT_HQ ? STR_ERROR_COMPANY_HEADQUARTERS_IN : STR_ERROR_OBJECT_IN_THE_WAY);
563  } else if (_game_mode == GM_EDITOR) {
564  /* No further limitations for the editor. */
565  } else if (GetTileOwner(tile) == OWNER_NONE) {
566  /* Owned by nobody and unremovable, so we can only remove it with brute force! */
567  if (!_cheats.magic_bulldozer.value && (spec->flags & OBJECT_FLAG_CANNOT_REMOVE) != 0) return CMD_ERROR;
568  } else if (CheckTileOwnership(tile).Failed()) {
569  /* We don't own it!. */
570  return_cmd_error(STR_ERROR_OWNED_BY);
571  } else if ((spec->flags & OBJECT_FLAG_CANNOT_REMOVE) != 0 && (spec->flags & OBJECT_FLAG_AUTOREMOVE) == 0) {
572  /* In the game editor or with cheats we can remove, otherwise we can't. */
574  if (type == OBJECT_HQ) return_cmd_error(STR_ERROR_COMPANY_HEADQUARTERS_IN);
575  return CMD_ERROR;
576  }
577 
578  /* Removing with the cheat costs more in TTDPatch / the specs. */
579  cost.MultiplyCost(25);
580  }
581  } else if ((spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0) {
582  /* Water can't remove objects that are buildable on water. */
583  return CMD_ERROR;
584  }
585 
586  switch (type) {
587  case OBJECT_HQ: {
588  Company *c = Company::Get(GetTileOwner(tile));
589  if (flags & DC_EXEC) {
590  c->location_of_HQ = INVALID_TILE; // reset HQ position
593  }
594 
595  /* cost of relocating company is 1% of company value */
597  break;
598  }
599 
600  case OBJECT_STATUE:
601  if (flags & DC_EXEC) {
602  Town *town = o->town;
603  ClrBit(town->statues, GetTileOwner(tile));
605  }
606  break;
607 
608  default:
609  break;
610  }
611 
612  _cleared_object_areas.push_back({tile, ta});
613 
614  if (flags & DC_EXEC) ReallyClearObjectTile(o);
615 
616  return cost;
617 }
618 
619 static void AddAcceptedCargo_Object(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted)
620 {
621  if (!IsObjectType(tile, OBJECT_HQ)) return;
622 
623  /* HQ accepts passenger and mail; but we have to divide the values
624  * between 4 tiles it occupies! */
625 
626  /* HQ level (depends on company performance) in the range 1..5. */
627  uint level = GetCompanyHQSize(tile) + 1;
628 
629  /* Top town building generates 10, so to make HQ interesting, the top
630  * type makes 20. */
631  acceptance[CT_PASSENGERS] += std::max(1U, level);
632  SetBit(*always_accepted, CT_PASSENGERS);
633 
634  /* Top town building generates 4, HQ can make up to 8. The
635  * proportion passengers:mail is different because such a huge
636  * commercial building generates unusually high amount of mail
637  * correspondence per physical visitor. */
638  acceptance[CT_MAIL] += std::max(1U, level / 2);
639  SetBit(*always_accepted, CT_MAIL);
640 }
641 
642 static void AddProducedCargo_Object(TileIndex tile, CargoArray &produced)
643 {
644  if (!IsObjectType(tile, OBJECT_HQ)) return;
645 
646  produced[CT_PASSENGERS]++;
647  produced[CT_MAIL]++;
648 }
649 
650 
651 static void GetTileDesc_Object(TileIndex tile, TileDesc *td)
652 {
653  const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
654  td->str = spec->name;
655  td->owner[0] = GetTileOwner(tile);
657 
658  if (spec->grf_prop.grffile != nullptr) {
659  td->grf = GetGRFConfig(spec->grf_prop.grffile->grfid)->GetName();
660  }
661 }
662 
663 static void TileLoop_Object(TileIndex tile)
664 {
665  const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
666  if (spec->flags & OBJECT_FLAG_ANIMATION) {
667  Object *o = Object::GetByTile(tile);
669  if (o->location.tile == tile) TriggerObjectAnimation(o, OAT_256_TICKS, spec);
670  }
671 
672  if (IsTileOnWater(tile)) TileLoop_Water(tile);
673 
674  if (!IsObjectType(tile, OBJECT_HQ)) return;
675 
676  /* HQ accepts passenger and mail; but we have to divide the values
677  * between 4 tiles it occupies! */
678 
679  /* HQ level (depends on company performance) in the range 1..5. */
680  uint level = GetCompanyHQSize(tile) + 1;
681  assert(level < 6);
682 
683  StationFinder stations(TileArea(tile, 2, 2));
684 
685  uint r = Random();
686  /* Top town buildings generate 250, so the top HQ type makes 256. */
687  if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
688  uint amt = GB(r, 0, 8) / 8 / 4 + 1;
689  if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
690  MoveGoodsToStation(CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
691  }
692 
693  /* Top town building generates 90, HQ can make up to 196. The
694  * proportion passengers:mail is about the same as in the acceptance
695  * equations. */
696  if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
697  uint amt = GB(r, 8, 8) / 8 / 4 + 1;
698  if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
699  MoveGoodsToStation(CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
700  }
701 }
702 
703 
704 static TrackStatus GetTileTrackStatus_Object(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
705 {
706  return 0;
707 }
708 
709 static bool ClickTile_Object(TileIndex tile)
710 {
711  if (!IsObjectType(tile, OBJECT_HQ)) return false;
712 
713  ShowCompany(GetTileOwner(tile));
714  return true;
715 }
716 
717 static void AnimateTile_Object(TileIndex tile)
718 {
719  AnimateNewObjectTile(tile);
720 }
721 
728 static bool HasTransmitter(TileIndex tile, void *user)
729 {
730  return IsObjectTypeTile(tile, OBJECT_TRANSMITTER);
731 }
732 
737 static bool TryBuildLightHouse()
738 {
739  uint maxx = MapMaxX();
740  uint maxy = MapMaxY();
741  uint r = Random();
742 
743  /* Scatter the lighthouses more evenly around the perimeter */
744  int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
745  DiagDirection dir;
746  for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
747  perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
748  }
749 
750  TileIndex tile;
751  switch (dir) {
752  default:
753  case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
754  case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
755  case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
756  case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
757  }
758 
759  /* Only build lighthouses at tiles where the border is sea. */
760  if (!IsTileType(tile, MP_WATER)) return false;
761 
762  for (int j = 0; j < 19; j++) {
763  int h;
764  if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h <= 2 && !IsBridgeAbove(tile)) {
766  assert(tile < MapSize());
767  return true;
768  }
769  tile += TileOffsByDiagDir(dir);
770  if (!IsValidTile(tile)) return false;
771  }
772  return false;
773 }
774 
779 static bool TryBuildTransmitter()
780 {
781  TileIndex tile = RandomTile();
782  int h;
783  if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h >= 4 && !IsBridgeAbove(tile)) {
784  TileIndex t = tile;
785  if (CircularTileSearch(&t, 9, HasTransmitter, nullptr)) return false;
786 
788  return true;
789  }
790  return false;
791 }
792 
793 void GenerateObjects()
794 {
795  /* Set a guestimate on how much we progress */
797 
798  /* Determine number of water tiles at map border needed for freeform_edges */
799  uint num_water_tiles = 0;
801  for (uint x = 0; x < MapMaxX(); x++) {
802  if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
803  if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
804  }
805  for (uint y = 1; y < MapMaxY() - 1; y++) {
806  if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
807  if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
808  }
809  }
810 
811  /* Iterate over all possible object types */
812  for (uint i = 0; i < NUM_OBJECTS; i++) {
813  const ObjectSpec *spec = ObjectSpec::Get(i);
814 
815  /* Continue, if the object was never available till now or shall not be placed */
816  if (!spec->WasEverAvailable() || spec->generate_amount == 0) continue;
817 
818  uint16 amount = spec->generate_amount;
819 
820  /* Scale by map size */
822  /* Scale the amount of lighthouses with the amount of land at the borders.
823  * The -6 is because the top borders are MP_VOID (-2) and all corners
824  * are counted twice (-4). */
825  amount = ScaleByMapSize1D(amount * num_water_tiles) / (2 * MapMaxY() + 2 * MapMaxX() - 6);
826  } else if (spec->flags & OBJECT_FLAG_SCALE_BY_WATER) {
827  amount = ScaleByMapSize1D(amount);
828  } else {
829  amount = ScaleByMapSize(amount);
830  }
831 
832  /* Now try to place the requested amount of this object */
833  for (uint j = ScaleByMapSize(1000); j != 0 && amount != 0 && Object::CanAllocateItem(); j--) {
834  switch (i) {
835  case OBJECT_TRANSMITTER:
836  if (TryBuildTransmitter()) amount--;
837  break;
838 
839  case OBJECT_LIGHTHOUSE:
840  if (TryBuildLightHouse()) amount--;
841  break;
842 
843  default:
844  uint8 view = RandomRange(spec->views);
845  if (CmdBuildObject(DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, RandomTile(), i, view).Succeeded()) amount--;
846  break;
847  }
848  }
850  }
851 }
852 
853 static void ChangeTileOwner_Object(TileIndex tile, Owner old_owner, Owner new_owner)
854 {
855  if (!IsTileOwner(tile, old_owner)) return;
856 
857  bool do_clear = false;
858 
859  ObjectType type = GetObjectType(tile);
860  if ((type == OBJECT_OWNED_LAND || type >= NEW_OBJECT_OFFSET) && new_owner != INVALID_OWNER) {
861  SetTileOwner(tile, new_owner);
862  } else if (type == OBJECT_STATUE) {
863  Town *t = Object::GetByTile(tile)->town;
864  ClrBit(t->statues, old_owner);
865  if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
866  /* Transfer ownership to the new company */
867  SetBit(t->statues, new_owner);
868  SetTileOwner(tile, new_owner);
869  } else {
870  do_clear = true;
871  }
872 
874  } else {
875  do_clear = true;
876  }
877 
878  if (do_clear) {
880  /* When clearing objects, they may turn into canal, which may require transferring ownership. */
881  ChangeTileOwner(tile, old_owner, new_owner);
882  }
883 }
884 
885 static CommandCost TerraformTile_Object(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
886 {
887  ObjectType type = GetObjectType(tile);
888 
889  if (type == OBJECT_OWNED_LAND) {
890  /* Owned land remains unsold */
891  CommandCost ret = CheckTileOwnership(tile);
892  if (ret.Succeeded()) return CommandCost();
893  } else if (AutoslopeEnabled() && type != OBJECT_TRANSMITTER && type != OBJECT_LIGHTHOUSE) {
894  /* Behaviour:
895  * - Both new and old slope must not be steep.
896  * - TileMaxZ must not be changed.
897  * - Allow autoslope by default.
898  * - Disallow autoslope if callback succeeds and returns non-zero.
899  */
900  Slope tileh_old = GetTileSlope(tile);
901  /* TileMaxZ must not be changed. Slopes must not be steep. */
902  if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
903  const ObjectSpec *spec = ObjectSpec::Get(type);
904 
905  /* Call callback 'disable autosloping for objects'. */
906  if (HasBit(spec->callback_mask, CBM_OBJ_AUTOSLOPE)) {
907  /* If the callback fails, allow autoslope. */
908  uint16 res = GetObjectCallback(CBID_OBJECT_AUTOSLOPE, 0, 0, spec, Object::GetByTile(tile), tile);
909  if (res == CALLBACK_FAILED || !ConvertBooleanCallback(spec->grf_prop.grffile, CBID_OBJECT_AUTOSLOPE, res)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
910  } else if (spec->enabled) {
911  /* allow autoslope */
912  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
913  }
914  }
915  }
916 
917  return Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
918 }
919 
920 extern const TileTypeProcs _tile_type_object_procs = {
921  DrawTile_Object, // draw_tile_proc
922  GetSlopePixelZ_Object, // get_slope_z_proc
923  ClearTile_Object, // clear_tile_proc
924  AddAcceptedCargo_Object, // add_accepted_cargo_proc
925  GetTileDesc_Object, // get_tile_desc_proc
926  GetTileTrackStatus_Object, // get_tile_track_status_proc
927  ClickTile_Object, // click_tile_proc
928  AnimateTile_Object, // animate_tile_proc
929  TileLoop_Object, // tile_loop_proc
930  ChangeTileOwner_Object, // change_tile_owner_proc
931  AddProducedCargo_Object, // add_produced_cargo_proc
932  nullptr, // vehicle_enter_tile_proc
933  GetFoundation_Object, // get_foundation_proc
934  TerraformTile_Object, // terraform_tile_proc
935 };
OBJECT_FLAG_BUILT_ON_WATER
@ OBJECT_FLAG_BUILT_ON_WATER
Object can be built on water (not required).
Definition: newgrf_object.h:29
TileInfo::z
int z
Height.
Definition: tile_cmd.h:47
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:48
DeleteNewGRFInspectWindow
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Delete inspect window for a given feature and index.
Definition: newgrf_debug_gui.cpp:730
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition: tile_map.cpp:100
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition: direction_type.h:80
TileDesc::grf
const char * grf
newGRF used for the tile contents
Definition: tile_cmd.h:61
OBJECT_FLAG_ANIMATION
@ OBJECT_FLAG_ANIMATION
Object has animated tiles.
Definition: newgrf_object.h:32
Object::colour
byte colour
Colour of the object, for display purpose.
Definition: object_base.h:28
CBID_OBJECT_COLOUR
@ CBID_OBJECT_COLOUR
Called to determine the colour of a town building.
Definition: newgrf_callbacks.h:266
DiagonalTileIterator
Iterator to iterate over a diagonal area of the map.
Definition: tilearea_type.h:233
Cheats::magic_bulldozer
Cheat magic_bulldozer
dynamite industries, objects
Definition: cheat_type.h:27
GetObjectType
ObjectType GetObjectType(TileIndex t)
Gets the ObjectType of the given object tile.
Definition: object_cmd.cpp:64
StationFinder
Structure contains cached list of stations nearby.
Definition: station_type.h:101
OBJECT_FLAG_ONLY_IN_SCENEDIT
@ OBJECT_FLAG_ONLY_IN_SCENEDIT
Object can only be constructed in the scenario editor.
Definition: newgrf_object.h:26
OAT_256_TICKS
@ OAT_256_TICKS
Triggered every 256 ticks (for all tiles at the same time).
Definition: newgrf_animation_type.h:59
Pool::PoolItem<&_object_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
TileOffsByDiagDir
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
CBID_OBJECT_LAND_SLOPE_CHECK
@ CBID_OBJECT_LAND_SLOPE_CHECK
Callback done for each tile of an object to check the slope.
Definition: newgrf_callbacks.h:254
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3156
water.h
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:141
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
FindClearedObject
ClearedObjectArea * FindClearedObject(TileIndex tile)
Find the entry in _cleared_object_areas which occupies a certain tile.
Definition: object_cmd.cpp:529
OBJECT_FLAG_2CC_COLOUR
@ OBJECT_FLAG_2CC_COLOUR
Object wants 2CC colour mapping.
Definition: newgrf_object.h:34
command_func.h
ObjectSpec
Allow incrementing of ObjectClassID variables.
Definition: newgrf_object.h:60
ObjectSpec::grf_prop
GRFFilePropsBase< 2 > grf_prop
Properties related the the grf file.
Definition: newgrf_object.h:62
TileInfo::x
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:43
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:42
Town::statues
CompanyMask statues
which companies have a statue?
Definition: town.h:66
company_base.h
IsTransparencySet
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:48
CommandCost::MultiplyCost
void MultiplyCost(int factor)
Multiplies the cost of the command by the given factor.
Definition: command_type.h:74
TileDesc::owner
Owner owner[4]
Name of the owner(s)
Definition: tile_cmd.h:53
OBJECT_FLAG_CLEAR_INCOME
@ OBJECT_FLAG_CLEAR_INCOME
When object is cleared a positive income is generated instead of a cost.
Definition: newgrf_object.h:30
company_gui.h
CargoPacket::InvalidateAllFrom
static void InvalidateAllFrom(SourceType src_type, SourceID src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
Definition: cargopacket.cpp:127
Object::location
TileArea location
Location of the object.
Definition: object_base.h:26
DiagDirToAxis
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
CmdBuildObject
CommandCost CmdBuildObject(DoCommandFlag flags, TileIndex tile, ObjectType type, uint8 view)
Build an object object.
Definition: object_cmd.cpp:206
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
Object::GetByTile
static Object * GetByTile(TileIndex tile)
Get the object associated with a tile.
Definition: object_cmd.cpp:53
ObjectSpec::IsAvailable
bool IsAvailable() const
Check whether the object is available at this time.
Definition: newgrf_object.cpp:78
GetCompanyHQSize
#define GetCompanyHQSize
We encode the company HQ size in the animation stage.
Definition: object_cmd.cpp:151
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:82
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition: gfx_type.h:23
GetBridgeHeight
int GetBridgeHeight(TileIndex t)
Get the height ('z') of a bridge.
Definition: bridge_map.cpp:70
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
INVALID_TILE
static constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:108
TryBuildLightHouse
static bool TryBuildLightHouse()
Try to build a lighthouse.
Definition: object_cmd.cpp:737
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
TriggerObjectTileAnimation
void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
Trigger the update of animation on a single tile.
Definition: newgrf_object.cpp:540
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
OBJECT_STATUE
static const ObjectType OBJECT_STATUE
Statue in towns.
Definition: object_type.h:18
NUM_OBJECTS
static const ObjectType NUM_OBJECTS
Number of supported objects overall.
Definition: object_type.h:25
GetPartialPixelZ
uint GetPartialPixelZ(int x, int y, Slope corners)
Determines height at given coordinate of a slope.
Definition: landscape.cpp:219
DrawBridgeMiddle
void DrawBridgeMiddle(const TileInfo *ti)
Draw the middle bits of a bridge.
Definition: tunnelbridge_cmd.cpp:1543
TileInfo::y
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:44
DC_NO_WATER
@ DC_NO_WATER
don't allow building on water
Definition: command_type.h:360
newgrf_debug.h
town.h
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:82
RandomRange
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
DrawNewObjectTile
void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
Draw an object on the map.
Definition: newgrf_object.cpp:446
WC_COMPANY
@ WC_COMPANY
Company view; Window numbers:
Definition: window_type.h:362
newgrf_config.h
ConvertBooleanCallback
bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
Definition: newgrf_commons.cpp:550
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
CalculateCompanyValue
Money CalculateCompanyValue(const Company *c, bool including_loan=true)
Calculate the value of the company.
Definition: economy.cpp:115
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:357
ObjectSpec::Get
static const ObjectSpec * Get(ObjectType index)
Get the specification associated with a specific ObjectType.
Definition: newgrf_object.cpp:39
ObjectSpec::size
uint8 size
The size of this objects; low nibble for X, high nibble for Y.
Definition: newgrf_object.h:67
WATER_CLASS_INVALID
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:51
TileDesc
Tile description for the 'land area information' tool.
Definition: tile_cmd.h:51
ObjectSpec::callback_mask
uint16 callback_mask
Bitmask of requested/allowed callbacks.
Definition: newgrf_object.h:74
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:355
genworld.h
Foundation
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
TriggerObjectAnimation
void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
Trigger the update of animation on a whole object.
Definition: newgrf_object.cpp:553
EnsureNoVehicleOnGround
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition: vehicle.cpp:539
Object::IncTypeCount
static void IncTypeCount(ObjectType type)
Increment the count of objects for this type.
Definition: object_base.h:43
IncreaseGeneratingWorldProgress
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
Definition: genworld_gui.cpp:1572
IncreaseAnimationStage
static void IncreaseAnimationStage(TileIndex tile)
Increase the animation stage of a whole structure.
Definition: object_cmd.cpp:141
ObjectSpec::GetClearCost
Money GetClearCost() const
Get the cost for clearing a structure of this type.
Definition: newgrf_object.h:90
FlatteningFoundation
static Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
CommandCost::Succeeded
bool Succeeded() const
Did this command succeed?
Definition: command_type.h:151
CompanyProperties::build_object_limit
uint32 build_object_limit
Amount of tiles we can (still) build objects on (times 65536). Also applies to buying land.
Definition: company_base.h:90
IncreaseCompanyHQSize
#define IncreaseCompanyHQSize
We encode the company HQ size in the animation stage.
Definition: object_cmd.cpp:153
object_base.h
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
TileInfo::tileh
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:45
IsTileOnWater
static bool IsTileOnWater(TileIndex t)
Tests if the tile was built on water.
Definition: water_map.h:141
ReallyClearObjectTile
static void ReallyClearObjectTile(Object *o)
Perform the actual removal of the object from the map.
Definition: object_cmd.cpp:511
ChangeTileOwner
void ChangeTileOwner(TileIndex tile, Owner old_owner, Owner new_owner)
Change the owner of a tile.
Definition: landscape.cpp:612
NEW_OBJECT_OFFSET
static const ObjectType NEW_OBJECT_OFFSET
Offset for new objects.
Definition: object_type.h:24
TileDesc::build_date
Date build_date
Date of construction of tile contents.
Definition: tile_cmd.h:55
DrawTileSprites::ground
PalSpriteID ground
Palette and sprite for the ground.
Definition: sprite.h:59
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
OrthogonalTileIterator
Iterator to iterate over a tile area (rectangle) of the map.
Definition: tilearea_type.h:183
OBJECT_FLAG_CANNOT_REMOVE
@ OBJECT_FLAG_CANNOT_REMOVE
Object can not be removed.
Definition: newgrf_object.h:27
DrawTileSeqStruct::delta_x
int8 delta_x
0x80 is sequence terminator
Definition: sprite.h:26
GetSlopeMaxZ
static int GetSlopeMaxZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition: slope_func.h:160
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition: direction_type.h:81
landscape_cmd.h
GetAnimationFrame
static byte GetAnimationFrame(TileIndex t)
Get the current animation frame.
Definition: tile_map.h:250
CheckOwnership
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
Definition: company_cmd.cpp:316
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:38
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
EXPENSES_CONSTRUCTION
@ EXPENSES_CONSTRUCTION
Construction costs.
Definition: economy_type.h:158
IsSteepSlope
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
CommandCost
Common return value for all commands.
Definition: command_type.h:24
HasTileWaterGround
static bool HasTileWaterGround(TileIndex t)
Checks whether the tile has water at the ground.
Definition: water_map.h:355
WaterClass
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
CircularTileSearch
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
Function performing a search around a center tile and going outward, thus in circle.
Definition: map.cpp:258
clear_func.h
WC_TOWN_AUTHORITY
@ WC_TOWN_AUTHORITY
Town authority; Window numbers:
Definition: window_type.h:187
DrawTileSeqStruct::delta_z
int8 delta_z
0x80 identifies child sprites
Definition: sprite.h:28
DirtyCompanyInfrastructureWindows
void DirtyCompanyInfrastructureWindows(CompanyID company)
Redraw all windows with company infrastructure counts.
Definition: company_gui.cpp:2774
CalcClosestTownFromTile
Town * CalcClosestTownFromTile(TileIndex tile, uint threshold=UINT_MAX)
Return the town closest to the given tile within threshold.
Definition: town_cmd.cpp:3576
ObjectSpec::WasEverAvailable
bool WasEverAvailable() const
Check whether the object was available at some point in the past or present in this game with the cur...
Definition: newgrf_object.cpp:69
autoslope.h
TileIterator
Base class for tile iterators.
Definition: tilearea_type.h:105
ObjectType
uint16 ObjectType
Types of objects.
Definition: object_type.h:14
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
TransportType
TransportType
Available types of transport.
Definition: transport_type.h:19
_cheats
Cheats _cheats
All the cheats.
Definition: cheat.cpp:16
Object::view
byte view
The view setting for this object.
Definition: object_base.h:29
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
OrthogonalTileArea::w
uint16 w
The width of the area.
Definition: tilearea_type.h:20
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition: command_type.h:160
BuildObject
void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town, uint8 view)
Actually build the object.
Definition: object_cmd.cpp:86
station_func.h
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:49
IsInvisibilitySet
static bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:59
Object
An object, such as transmitter, on the map.
Definition: object_base.h:23
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
GetErrorMessageFromLocationCallbackResult
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error)
Get the error message from a shape/location/slope check callback result.
Definition: newgrf_commons.cpp:480
foreach_draw_tile_seq
#define foreach_draw_tile_seq(idx, list)
Iterate through all DrawTileSeqStructs in DrawTileSprites.
Definition: sprite.h:79
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:54
CBM_OBJ_COLOUR
@ CBM_OBJ_COLOUR
decide the colour of the building
Definition: newgrf_callbacks.h:389
Object::type
ObjectType type
Type of the object.
Definition: object_base.h:24
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
safeguards.h
cargopacket.h
GWP_OBJECT
@ GWP_OBJECT
Generate objects (radio tower, light houses)
Definition: genworld.h:76
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition: settings_type.h:354
CommandCost::GetCost
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:83
GetTileSlope
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
UpdateCompanyRatingAndValue
int UpdateCompanyRatingAndValue(Company *c, bool update)
if update is set to true, the economy is updated with this score (also the house is updated,...
Definition: economy.cpp:166
IsTileOwner
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:214
ST_HEADQUARTERS
@ ST_HEADQUARTERS
Source/destination are company headquarters.
Definition: cargo_type.h:150
CompanyProperties::location_of_HQ
TileIndex location_of_HQ
Northern tile of HQ; INVALID_TILE when there is none.
Definition: company_base.h:75
RandomTile
#define RandomTile()
Get a valid random tile.
Definition: map_func.h:435
ObjectSpec::height
uint8 height
The height of this structure, in heightlevels; max MAX_TILE_HEIGHT.
Definition: newgrf_object.h:75
DrawTileSprites
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:58
DC_NO_TEST_TOWN_RATING
@ DC_NO_TEST_TOWN_RATING
town rating does not disallow you from building
Definition: command_type.h:362
AutoslopeEnabled
static bool AutoslopeEnabled()
Tests if autoslope is enabled for _current_company.
Definition: autoslope.h:44
GetAvailableMoneyForCommand
Money GetAvailableMoneyForCommand()
Definition: command.cpp:173
FOUNDATION_NONE
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
GetSouthernBridgeEnd
TileIndex GetSouthernBridgeEnd(TileIndex t)
Finds the southern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:49
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
date_func.h
CommandCost::AddCost
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Definition: command_type.h:63
stdafx.h
landscape.h
ObjectSpec::enabled
bool enabled
Is this spec enabled?
Definition: newgrf_object.h:78
TileTypeProcs
Set of callback functions for performing tile operations of a given tile type.
Definition: tile_cmd.h:145
Cheat::value
bool value
tells if the bool cheat is active or not
Definition: cheat_type.h:18
viewport_func.h
TileLoop_Water
void TileLoop_Water(TileIndex tile)
Let a water tile floods its diagonal adjoining tiles called from tunnelbridge_cmd,...
Definition: water_cmd.cpp:1216
bridge_map.h
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
object_cmd.h
CBID_OBJECT_AUTOSLOPE
@ CBID_OBJECT_AUTOSLOPE
Called to determine if one can alter the ground below an object tile.
Definition: newgrf_callbacks.h:272
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:667
OBJECT_FLAG_NOT_ON_LAND
@ OBJECT_FLAG_NOT_ON_LAND
Object can not be on land, implicitly sets OBJECT_FLAG_BUILT_ON_WATER.
Definition: newgrf_object.h:35
newgrf_object.h
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:21
ObjectSpec::name
StringID name
The name for this object.
Definition: newgrf_object.h:64
object_map.h
DrawFoundation
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Definition: landscape.cpp:474
OBJECT_OWNED_LAND
static const ObjectType OBJECT_OWNED_LAND
Owned land 'flag'.
Definition: object_type.h:19
_generating_world
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:61
OBJECT_FLAG_AUTOREMOVE
@ OBJECT_FLAG_AUTOREMOVE
Object get automatically removed (like "owned land").
Definition: newgrf_object.h:28
ObjectSpec::generate_amount
uint8 generate_amount
Number of objects which are attempted to be generated per 256^2 map during world generation.
Definition: newgrf_object.h:77
EconomyIsInRecession
static bool EconomyIsInRecession()
Is the economy in recession?
Definition: economy_func.h:47
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:408
object_land.h
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
vehicle_func.h
OAT_BUILT
@ OAT_BUILT
Triggered when the object is built (for all tiles at the same time).
Definition: newgrf_animation_type.h:57
CBM_OBJ_SLOPE_CHECK
@ CBM_OBJ_SLOPE_CHECK
decides slope suitability
Definition: newgrf_callbacks.h:386
Pool::PoolItem<&_object_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
IsObjectTypeTile
static bool IsObjectTypeTile(TileIndex t, ObjectType type)
Check whether a tile is a object tile of a specific type.
Definition: object_map.h:36
Pool
Base class for all pools.
Definition: pool_type.hpp:81
TO_STRUCTURES
@ TO_STRUCTURES
other objects such as transmitters and lighthouses
Definition: transparency.h:29
GetWaterClass
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:117
ScaleByMapSize
static uint ScaleByMapSize(uint n)
Scales the given value by the map size, where the given value is for a 256 by 256 map.
Definition: map_func.h:122
OAT_TILELOOP
@ OAT_TILELOOP
Triggered in the periodic tile loop.
Definition: newgrf_animation_type.h:58
MapMaxY
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:111
Pool::PoolItem<&_town_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:367
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
ObjectSpec::views
uint8 views
The number of views.
Definition: newgrf_object.h:76
TryBuildTransmitter
static bool TryBuildTransmitter()
Try to build a transmitter.
Definition: object_cmd.cpp:779
IsWaterTile
static bool IsWaterTile(TileIndex t)
Is it a water tile with plain water?
Definition: water_map.h:195
CheckBuildableTile
CommandCost CheckBuildableTile(TileIndex tile, uint invalid_dirs, int &allowed_z, bool allow_steep, bool check_bridge)
Checks if the given tile is buildable, flat and has a certain height.
Definition: station_cmd.cpp:794
SetGeneratingWorldProgress
void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
Set the total of a stage of the world generation.
Definition: genworld_gui.cpp:1558
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
CBM_OBJ_AUTOSLOPE
@ CBM_OBJ_AUTOSLOPE
decides allowance of autosloping
Definition: newgrf_callbacks.h:391
cheat_type.h
OBJECT_FLAG_ALLOW_UNDER_BRIDGE
@ OBJECT_FLAG_ALLOW_UNDER_BRIDGE
Object can built under a bridge.
Definition: newgrf_object.h:37
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1998
ObjectSpec::GetByTile
static const ObjectSpec * GetByTile(TileIndex tile)
Get the specification associated with a tile.
Definition: newgrf_object.cpp:50
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
GetObjectCallback
uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
Perform a callback for an object.
Definition: newgrf_object.cpp:408
Pool::PoolItem<&_object_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
MakeObject
static void MakeObject(TileIndex t, Owner o, ObjectID index, WaterClass wc, byte random)
Make an Object tile.
Definition: object_map.h:74
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
TileDesc::str
StringID str
Description of the tile.
Definition: tile_cmd.h:52
DC_AUTO
@ DC_AUTO
don't allow building on structures
Definition: command_type.h:358
GetTileMaxPixelZ
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:304
ScaleByMapSize1D
static uint ScaleByMapSize1D(uint n)
Scales the given value by the maps circumference, where the given value is for a 256 by 256 map.
Definition: map_func.h:136
DC_NO_MODIFY_TOWN_RATING
@ DC_NO_MODIFY_TOWN_RATING
do not change town rating
Definition: command_type.h:367
InitializeObjects
void InitializeObjects()
Initialize/reset the objects.
Definition: object_cmd.cpp:71
MapMaxX
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:102
Object::DecTypeCount
static void DecTypeCount(ObjectType type)
Decrement the count of objects for this type.
Definition: object_base.h:54
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:126
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:102
Object::ResetTypeCounts
static void ResetTypeCounts()
Resets object counts.
Definition: object_base.h:72
IsDockingTile
static bool IsDockingTile(TileIndex t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:376
CheckTileOwnership
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
Definition: company_cmd.cpp:334
CommandHelper
Definition: command_func.h:94
DrawTileSprites::seq
const DrawTileSeqStruct * seq
Array of child sprites. Terminated with a terminator entry.
Definition: sprite.h:60
window_func.h
ToggleBit
static T ToggleBit(T &x, const uint8 y)
Toggles a bit in a variable.
Definition: bitmath_func.hpp:181
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
Town
Town data structure.
Definition: town.h:50
UpdateCompanyHQ
void UpdateCompanyHQ(TileIndex tile, uint score)
Update the CompanyHQ to the state associated with the given score.
Definition: object_cmd.cpp:160
random_func.hpp
OverflowSafeInt< int64 >
OBJECT_FLAG_HAS_NO_FOUNDATION
@ OBJECT_FLAG_HAS_NO_FOUNDATION
Do not display foundations when on a slope.
Definition: newgrf_object.h:31
GetObjectIndex
static ObjectID GetObjectIndex(TileIndex t)
Get the index of which object this tile is attached to.
Definition: object_map.h:47
AnimateNewObjectTile
void AnimateNewObjectTile(TileIndex tile)
Handle the animation of the object tile.
Definition: newgrf_object.cpp:525
OBJECT_FLAG_SCALE_BY_WATER
@ OBJECT_FLAG_SCALE_BY_WATER
Object count is roughly scaled by water amount at edges.
Definition: newgrf_object.h:39
HasTransmitter
static bool HasTransmitter(TileIndex tile, void *user)
Helper function for CircularTileSearch.
Definition: object_cmd.cpp:728
SetTileOwner
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
TileInfo::tile
TileIndex tile
Tile index.
Definition: tile_cmd.h:46
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:588
ErrorUnknownCallbackResult
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Definition: newgrf_commons.cpp:516
ObjectSpec::GetBuildCost
Money GetBuildCost() const
Get the cost for building a structure of this type.
Definition: newgrf_object.h:84
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:320
OBJECT_SIZE_1X1
static const uint8 OBJECT_SIZE_1X1
The value of a NewGRF's size property when the object is 1x1 tiles: low nibble for X,...
Definition: newgrf_object.h:43
GetTilePixelSlope
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:280
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
pool_func.hpp
OBJECT_HQ
static const ObjectType OBJECT_HQ
HeadQuarter of a player.
Definition: object_type.h:20
SetAnimationFrame
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:262
OBJECT_TRANSMITTER
static const ObjectType OBJECT_TRANSMITTER
The large antenna.
Definition: object_type.h:16
OBJECT_LIGHTHOUSE
static const ObjectType OBJECT_LIGHTHOUSE
The nice lighthouse.
Definition: object_type.h:17
ShowCompany
void ShowCompany(CompanyID company)
Show the window with the overview of the company.
Definition: company_gui.cpp:2763
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:79
ClearedObjectArea
Keeps track of removed objects during execution/testruns of commands.
Definition: object_base.h:84
Company
Definition: company_base.h:117
Object::counts
static uint16 counts[NUM_OBJECTS]
Number of objects per type ingame.
Definition: object_base.h:78
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
Object::build_date
Date build_date
Date of construction.
Definition: object_base.h:27
Livery
Information about a particular livery.
Definition: livery.h:78
Object::town
Town * town
Town the object is built in.
Definition: object_base.h:25
GetGRFConfig
GRFConfig * GetGRFConfig(uint32 grfid, uint32 mask)
Retrieve a NewGRF from the current config by its grfid.
Definition: newgrf_config.cpp:771
OWNER_TOWN
@ OWNER_TOWN
A town owns the tile, or a town is expanding.
Definition: company_type.h:24
IsObjectType
static bool IsObjectType(TileIndex t, ObjectType type)
Check whether the object on a tile is of a specific type.
Definition: object_map.h:25
UpdateObjectColours
void UpdateObjectColours(const Company *c)
Updates the colour of the object whenever a company changes.
Definition: object_cmd.cpp:179
DrawGroundSprite
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition: viewport.cpp:583
CmdBuildObjectArea
CommandCost CmdBuildObjectArea(DoCommandFlag flags, TileIndex tile, TileIndex start_tile, ObjectType type, uint8 view, bool diagonal)
Construct multiple objects in an area.
Definition: object_cmd.cpp:387
object.h
GRFConfig::GetName
const char * GetName() const
Get the name of this grf.
Definition: newgrf_config.cpp:105
IsBridgeAbove
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
OBJECT_FLAG_ONLY_IN_GAME
@ OBJECT_FLAG_ONLY_IN_GAME
Object can only be built in game.
Definition: newgrf_object.h:33
DrawTileSeqStruct
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition: sprite.h:25
Livery::colour2
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:81
ObjectSpec::flags
ObjectFlags flags
Flags/settings related to the object.
Definition: newgrf_object.h:72
Livery::colour1
byte colour1
First colour, for all vehicles.
Definition: livery.h:80