4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file object_cmd.cpp Handling of object tiles. */
13 #include "landscape.h"
14 #include "command_func.h"
15 #include "viewport_func.h"
16 #include "company_base.h"
18 #include "bridge_map.h"
20 #include "autoslope.h"
21 #include "clear_func.h"
23 #include "window_func.h"
24 #include "company_gui.h"
25 #include "cheat_type.h"
27 #include "cargopacket.h"
28 #include "core/random_func.hpp"
29 #include "core/pool_func.hpp"
30 #include "object_map.h"
31 #include "object_base.h"
32 #include "newgrf_config.h"
33 #include "newgrf_object.h"
34 #include "date_func.h"
35 #include "newgrf_debug.h"
36 #include "vehicle_func.h"
38 #include "table/strings.h"
39 #include "table/object_land.h"
41 #include "safeguards.h"
43 ObjectPool
_object_pool("Object");
44 INSTANTIATE_POOL_METHODS(Object
)
45 uint16
Object::counts
[NUM_OBJECTS
];
48 * Get the object associated with a tile.
49 * @param tile The tile to fetch the object for.
52 /* static */ Object
*Object::GetByTile(TileIndex tile
)
54 return Object::Get(GetObjectIndex(tile
));
58 * Gets the ObjectType of the given object tile
59 * @param t the tile to get the type from.
60 * @pre IsTileType(t, MP_OBJECT)
63 ObjectType
GetObjectType(TileIndex t
)
65 assert(IsTileType(t
, MP_OBJECT
));
66 return Object::GetByTile(t
)->type
;
69 /** Initialize/reset the objects. */
70 void InitializeObjects()
72 Object::ResetTypeCounts();
76 * Actually build the object.
77 * @param type The type of object to build.
78 * @param tile The tile to build the northern tile of the object on.
79 * @param owner The owner of the object.
80 * @param town Town the tile is related with.
81 * @param view The view for the object.
82 * @pre All preconditions for building the object at that location
83 * are met, e.g. slope and clearness of tiles are checked.
85 void BuildObject(ObjectType type
, TileIndex tile
, CompanyID owner
, Town
*town
, uint8 view
)
87 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
89 TileArea
ta(tile
, GB(spec
->size
, HasBit(view
, 0) ? 4 : 0, 4), GB(spec
->size
, HasBit(view
, 0) ? 0 : 4, 4));
90 Object
*o
= new Object();
93 o
->town
= town
== NULL
? CalcClosestTownFromTile(tile
) : town
;
94 o
->build_date
= _date
;
97 /* If nothing owns the object, the colour will be random. Otherwise
98 * get the colour from the company's livery settings. */
99 if (owner
== OWNER_NONE
) {
100 o
->colour
= Random();
102 const Livery
*l
= Company::Get(owner
)->livery
;
103 o
->colour
= l
->colour1
+ l
->colour2
* 16;
106 /* If the object wants only one colour, then give it that colour. */
107 if ((spec
->flags
& OBJECT_FLAG_2CC_COLOUR
) == 0) o
->colour
&= 0xF;
109 if (HasBit(spec
->callback_mask
, CBM_OBJ_COLOUR
)) {
110 uint16 res
= GetObjectCallback(CBID_OBJECT_COLOUR
, o
->colour
, 0, spec
, o
, tile
);
111 if (res
!= CALLBACK_FAILED
) {
112 if (res
>= 0x100) ErrorUnknownCallbackResult(spec
->grf_prop
.grffile
->grfid
, CBID_OBJECT_COLOUR
, res
);
113 o
->colour
= GB(res
, 0, 8);
117 assert(o
->town
!= NULL
);
119 TILE_AREA_LOOP(t
, ta
) {
120 WaterClass wc
= (IsWaterTile(t
) ? GetWaterClass(t
) : WATER_CLASS_INVALID
);
121 /* Update company infrastructure counts for objects build on canals owned by nobody. */
122 if (wc
== WATER_CLASS_CANAL
&& owner
!= OWNER_NONE
&& (IsTileOwner(tile
, OWNER_NONE
) || IsTileOwner(tile
, OWNER_WATER
))) {
123 Company::Get(owner
)->infrastructure
.water
++;
124 DirtyCompanyInfrastructureWindows(owner
);
126 MakeObject(t
, owner
, o
->index
, wc
, Random());
127 MarkTileDirtyByTile(t
);
130 Object::IncTypeCount(type
);
131 if (spec
->flags
& OBJECT_FLAG_ANIMATION
) TriggerObjectAnimation(o
, OAT_BUILT
, spec
);
135 * Increase the animation stage of a whole structure.
136 * @param tile The tile of the structure.
138 static void IncreaseAnimationStage(TileIndex tile
)
140 TileArea ta
= Object::GetByTile(tile
)->location
;
141 TILE_AREA_LOOP(t
, ta
) {
142 SetAnimationFrame(t
, GetAnimationFrame(t
) + 1);
143 MarkTileDirtyByTile(t
);
147 /** We encode the company HQ size in the animation stage. */
148 #define GetCompanyHQSize GetAnimationFrame
149 /** We encode the company HQ size in the animation stage. */
150 #define IncreaseCompanyHQSize IncreaseAnimationStage
153 * Update the CompanyHQ to the state associated with the given score
154 * @param tile The (northern) tile of the company HQ, or INVALID_TILE.
155 * @param score The current (performance) score of the company.
157 void UpdateCompanyHQ(TileIndex tile
, uint score
)
159 if (tile
== INVALID_TILE
) return;
162 (val
= 0, score
< 170) ||
163 (val
++, score
< 350) ||
164 (val
++, score
< 520) ||
165 (val
++, score
< 720) ||
168 while (GetCompanyHQSize(tile
) < val
) {
169 IncreaseCompanyHQSize(tile
);
174 * Updates the colour of the object whenever a company changes.
175 * @param c The company the company colour changed of.
177 void UpdateObjectColours(const Company
*c
)
180 FOR_ALL_OBJECTS(obj
) {
181 Owner owner
= GetTileOwner(obj
->location
.tile
);
182 /* Not the current owner, so colour doesn't change. */
183 if (owner
!= c
->index
) continue;
185 const ObjectSpec
*spec
= ObjectSpec::GetByTile(obj
->location
.tile
);
186 /* Using the object colour callback, so not using company colour. */
187 if (HasBit(spec
->callback_mask
, CBM_OBJ_COLOUR
)) continue;
189 const Livery
*l
= c
->livery
;
190 obj
->colour
= ((spec
->flags
& OBJECT_FLAG_2CC_COLOUR
) ? (l
->colour2
* 16) : 0) + l
->colour1
;
194 extern CommandCost
CheckBuildableTile(TileIndex tile
, uint invalid_dirs
, int &allowed_z
, bool allow_steep
, bool check_bridge
);
195 static CommandCost
ClearTile_Object(TileIndex tile
, DoCommandFlag flags
);
198 * Build an object object
199 * @param tile tile where the object will be located
200 * @param flags type of operation
201 * @param p1 the object type to build
202 * @param p2 the view for the object
204 * @return the cost of this operation or an error
206 CommandCost
CmdBuildObject(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
208 CommandCost
cost(EXPENSES_PROPERTY
);
210 ObjectType type
= (ObjectType
)GB(p1
, 0, 16);
211 if (type
>= NUM_OBJECTS
) return CMD_ERROR
;
212 uint8 view
= GB(p2
, 0, 2);
213 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
214 if (_game_mode
== GM_NORMAL
&& !spec
->IsAvailable() && !_generating_world
) return CMD_ERROR
;
215 if ((_game_mode
== GM_EDITOR
|| _generating_world
) && !spec
->WasEverAvailable()) return CMD_ERROR
;
217 if ((spec
->flags
& OBJECT_FLAG_ONLY_IN_SCENEDIT
) != 0 && ((!_generating_world
&& _game_mode
!= GM_EDITOR
) || _current_company
!= OWNER_NONE
)) return CMD_ERROR
;
218 if ((spec
->flags
& OBJECT_FLAG_ONLY_IN_GAME
) != 0 && (_generating_world
|| _game_mode
!= GM_NORMAL
|| _current_company
> MAX_COMPANIES
)) return CMD_ERROR
;
219 if (view
>= spec
->views
) return CMD_ERROR
;
221 if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS
);
222 if (Town::GetNumItems() == 0) return_cmd_error(STR_ERROR_MUST_FOUND_TOWN_FIRST
);
224 int size_x
= GB(spec
->size
, HasBit(view
, 0) ? 4 : 0, 4);
225 int size_y
= GB(spec
->size
, HasBit(view
, 0) ? 0 : 4, 4);
226 TileArea
ta(tile
, size_x
, size_y
);
228 if (type
== OBJECT_OWNED_LAND
) {
229 /* Owned land is special as it can be placed on any slope. */
230 cost
.AddCost(DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
));
232 /* Check the surface to build on. At this time we can't actually execute the
233 * the CLEAR_TILE commands since the newgrf callback later on can check
234 * some information about the tiles. */
235 bool allow_water
= (spec
->flags
& (OBJECT_FLAG_BUILT_ON_WATER
| OBJECT_FLAG_NOT_ON_LAND
)) != 0;
236 bool allow_ground
= (spec
->flags
& OBJECT_FLAG_NOT_ON_LAND
) == 0;
237 TILE_AREA_LOOP(t
, ta
) {
238 if (HasTileWaterGround(t
)) {
239 if (!allow_water
) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER
);
240 if (!IsWaterTile(t
)) {
241 /* Normal water tiles don't have to be cleared. For all other tile types clear
242 * the tile but leave the water. */
243 cost
.AddCost(DoCommand(t
, 0, 0, flags
& ~DC_NO_WATER
& ~DC_EXEC
, CMD_LANDSCAPE_CLEAR
));
245 /* Can't build on water owned by another company. */
246 Owner o
= GetTileOwner(t
);
247 if (o
!= OWNER_NONE
&& o
!= OWNER_WATER
) cost
.AddCost(CheckOwnership(o
, t
));
249 /* However, the tile has to be clear of vehicles. */
250 cost
.AddCost(EnsureNoVehicleOnGround(t
));
253 if (!allow_ground
) return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER
);
254 /* For non-water tiles, we'll have to clear it before building. */
255 cost
.AddCost(DoCommand(t
, 0, 0, flags
& ~DC_EXEC
, CMD_LANDSCAPE_CLEAR
));
259 /* So, now the surface is checked... check the slope of said surface. */
261 if (GetTileSlope(tile
, &allowed_z
) != SLOPE_FLAT
) allowed_z
++;
263 TILE_AREA_LOOP(t
, ta
) {
264 uint16 callback
= CALLBACK_FAILED
;
265 if (HasBit(spec
->callback_mask
, CBM_OBJ_SLOPE_CHECK
)) {
266 TileIndex diff
= t
- tile
;
267 callback
= GetObjectCallback(CBID_OBJECT_LAND_SLOPE_CHECK
, GetTileSlope(t
), TileY(diff
) << 4 | TileX(diff
), spec
, NULL
, t
, view
);
270 if (callback
== CALLBACK_FAILED
) {
271 cost
.AddCost(CheckBuildableTile(t
, 0, allowed_z
, false, false));
273 /* The meaning of bit 10 is inverted for a grf version < 8. */
274 if (spec
->grf_prop
.grffile
->grf_version
< 8) ToggleBit(callback
, 10);
275 CommandCost ret
= GetErrorMessageFromLocationCallbackResult(callback
, spec
->grf_prop
.grffile
, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
276 if (ret
.Failed()) return ret
;
280 if (flags
& DC_EXEC
) {
281 /* This is basically a copy of the loop above with the exception that we now
282 * execute the commands and don't check for errors, since that's already done. */
283 TILE_AREA_LOOP(t
, ta
) {
284 if (HasTileWaterGround(t
)) {
285 if (!IsWaterTile(t
)) {
286 DoCommand(t
, 0, 0, (flags
& ~DC_NO_WATER
) | DC_NO_MODIFY_TOWN_RATING
, CMD_LANDSCAPE_CLEAR
);
289 DoCommand(t
, 0, 0, flags
| DC_NO_MODIFY_TOWN_RATING
, CMD_LANDSCAPE_CLEAR
);
294 if (cost
.Failed()) return cost
;
296 /* Finally do a check for bridges. */
297 TILE_AREA_LOOP(t
, ta
) {
298 if (IsBridgeAbove(t
) && (
299 !(spec
->flags
& OBJECT_FLAG_ALLOW_UNDER_BRIDGE
) ||
300 (GetTileMaxZ(t
) + spec
->height
>= GetBridgeHeight(GetSouthernBridgeEnd(t
))))) {
301 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
307 case OBJECT_TRANSMITTER
:
308 case OBJECT_LIGHTHOUSE
:
309 if (!IsTileFlat(tile
)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED
);
312 case OBJECT_OWNED_LAND
:
313 if (IsTileType(tile
, MP_OBJECT
) &&
314 IsTileOwner(tile
, _current_company
) &&
315 IsObjectType(tile
, OBJECT_OWNED_LAND
)) {
316 return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT
);
321 Company
*c
= Company::Get(_current_company
);
322 if (c
->location_of_HQ
!= INVALID_TILE
) {
323 /* We need to persuade a bit harder to remove the old HQ. */
324 _current_company
= OWNER_WATER
;
325 cost
.AddCost(ClearTile_Object(c
->location_of_HQ
, flags
));
326 _current_company
= c
->index
;
329 if (flags
& DC_EXEC
) {
330 hq_score
= UpdateCompanyRatingAndValue(c
, false);
331 c
->location_of_HQ
= tile
;
332 SetWindowDirty(WC_COMPANY
, c
->index
);
338 /* This may never be constructed using this method. */
341 default: // i.e. NewGRF provided.
345 if (flags
& DC_EXEC
) {
346 BuildObject(type
, tile
, _current_company
, NULL
, view
);
348 /* Make sure the HQ starts at the right size. */
349 if (type
== OBJECT_HQ
) UpdateCompanyHQ(tile
, hq_score
);
352 cost
.AddCost(ObjectSpec::Get(type
)->GetBuildCost() * size_x
* size_y
);
357 static Foundation
GetFoundation_Object(TileIndex tile
, Slope tileh
);
359 static void DrawTile_Object(TileInfo
*ti
)
361 ObjectType type
= GetObjectType(ti
->tile
);
362 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
364 /* Fall back for when the object doesn't exist anymore. */
365 if (!spec
->enabled
) type
= OBJECT_TRANSMITTER
;
367 if ((spec
->flags
& OBJECT_FLAG_HAS_NO_FOUNDATION
) == 0) DrawFoundation(ti
, GetFoundation_Object(ti
->tile
, ti
->tileh
));
369 if (type
< NEW_OBJECT_OFFSET
) {
370 const DrawTileSprites
*dts
= NULL
;
371 Owner to
= GetTileOwner(ti
->tile
);
372 PaletteID palette
= to
== OWNER_NONE
? PAL_NONE
: COMPANY_SPRITE_COLOUR(to
);
374 if (type
== OBJECT_HQ
) {
375 TileIndex diff
= ti
->tile
- Object::GetByTile(ti
->tile
)->location
.tile
;
376 dts
= &_object_hq
[GetCompanyHQSize(ti
->tile
) << 2 | TileY(diff
) << 1 | TileX(diff
)];
378 dts
= &_objects
[type
];
381 if (spec
->flags
& OBJECT_FLAG_HAS_NO_FOUNDATION
) {
382 /* If an object has no foundation, but tries to draw a (flat) ground
383 * type... we have to be nice and convert that for them. */
384 switch (dts
->ground
.sprite
) {
385 case SPR_FLAT_BARE_LAND
: DrawClearLandTile(ti
, 0); break;
386 case SPR_FLAT_1_THIRD_GRASS_TILE
: DrawClearLandTile(ti
, 1); break;
387 case SPR_FLAT_2_THIRD_GRASS_TILE
: DrawClearLandTile(ti
, 2); break;
388 case SPR_FLAT_GRASS_TILE
: DrawClearLandTile(ti
, 3); break;
389 default: DrawGroundSprite(dts
->ground
.sprite
, palette
); break;
392 DrawGroundSprite(dts
->ground
.sprite
, palette
);
395 if (!IsInvisibilitySet(TO_STRUCTURES
)) {
396 const DrawTileSeqStruct
*dtss
;
397 foreach_draw_tile_seq(dtss
, dts
->seq
) {
398 AddSortableSpriteToDraw(
399 dtss
->image
.sprite
, palette
,
400 ti
->x
+ dtss
->delta_x
, ti
->y
+ dtss
->delta_y
,
401 dtss
->size_x
, dtss
->size_y
,
402 dtss
->size_z
, ti
->z
+ dtss
->delta_z
,
403 IsTransparencySet(TO_STRUCTURES
)
408 DrawNewObjectTile(ti
, spec
);
411 DrawBridgeMiddle(ti
);
414 static int GetSlopePixelZ_Object(TileIndex tile
, uint x
, uint y
)
416 if (IsObjectType(tile
, OBJECT_OWNED_LAND
)) {
418 Slope tileh
= GetTilePixelSlope(tile
, &z
);
420 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
422 return GetTileMaxPixelZ(tile
);
426 static Foundation
GetFoundation_Object(TileIndex tile
, Slope tileh
)
428 return IsObjectType(tile
, OBJECT_OWNED_LAND
) ? FOUNDATION_NONE
: FlatteningFoundation(tileh
);
432 * Perform the actual removal of the object from the map.
433 * @param o The object to really clear.
435 static void ReallyClearObjectTile(Object
*o
)
437 Object::DecTypeCount(o
->type
);
438 TILE_AREA_LOOP(tile_cur
, o
->location
) {
439 DeleteNewGRFInspectWindow(GSF_OBJECTS
, tile_cur
);
441 MakeWaterKeepingClass(tile_cur
, GetTileOwner(tile_cur
));
446 SmallVector
<ClearedObjectArea
, 4> _cleared_object_areas
;
449 * Find the entry in _cleared_object_areas which occupies a certain tile.
450 * @param tile Tile of interest
451 * @return Occupying entry, or NULL if none
453 ClearedObjectArea
*FindClearedObject(TileIndex tile
)
455 TileArea ta
= TileArea(tile
, 1, 1);
457 const ClearedObjectArea
*end
= _cleared_object_areas
.End();
458 for (ClearedObjectArea
*coa
= _cleared_object_areas
.Begin(); coa
!= end
; coa
++) {
459 if (coa
->area
.Intersects(ta
)) return coa
;
465 static CommandCost
ClearTile_Object(TileIndex tile
, DoCommandFlag flags
)
467 /* Get to the northern most tile. */
468 Object
*o
= Object::GetByTile(tile
);
469 TileArea ta
= o
->location
;
471 ObjectType type
= o
->type
;
472 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
474 CommandCost
cost(EXPENSES_CONSTRUCTION
, spec
->GetClearCost() * ta
.w
* ta
.h
/ 5);
475 if (spec
->flags
& OBJECT_FLAG_CLEAR_INCOME
) cost
.MultiplyCost(-1); // They get an income!
477 /* Towns can't remove any objects. */
478 if (_current_company
== OWNER_TOWN
) return CMD_ERROR
;
480 /* Water can remove everything! */
481 if (_current_company
!= OWNER_WATER
) {
482 if ((flags
& DC_NO_WATER
) && IsTileOnWater(tile
)) {
483 /* There is water under the object, treat it as water tile. */
484 return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER
);
485 } else if (!(spec
->flags
& OBJECT_FLAG_AUTOREMOVE
) && (flags
& DC_AUTO
)) {
486 /* No automatic removal by overbuilding stuff. */
487 return_cmd_error(type
== OBJECT_HQ
? STR_ERROR_COMPANY_HEADQUARTERS_IN
: STR_ERROR_OBJECT_IN_THE_WAY
);
488 } else if (_game_mode
== GM_EDITOR
) {
489 /* No further limitations for the editor. */
490 } else if (GetTileOwner(tile
) == OWNER_NONE
) {
491 /* Owned by nobody and unremovable, so we can only remove it with brute force! */
492 if (!_cheats
.magic_bulldozer
.value
&& (spec
->flags
& OBJECT_FLAG_CANNOT_REMOVE
) != 0) return CMD_ERROR
;
493 } else if (CheckTileOwnership(tile
).Failed()) {
494 /* We don't own it!. */
495 return_cmd_error(STR_ERROR_OWNED_BY
);
496 } else if ((spec
->flags
& OBJECT_FLAG_CANNOT_REMOVE
) != 0 && (spec
->flags
& OBJECT_FLAG_AUTOREMOVE
) == 0) {
497 /* In the game editor or with cheats we can remove, otherwise we can't. */
498 if (!_cheats
.magic_bulldozer
.value
) return CMD_ERROR
;
500 /* Removing with the cheat costs more in TTDPatch / the specs. */
501 cost
.MultiplyCost(25);
503 } else if ((spec
->flags
& (OBJECT_FLAG_BUILT_ON_WATER
| OBJECT_FLAG_NOT_ON_LAND
)) != 0) {
504 /* Water can't remove objects that are buildable on water. */
510 Company
*c
= Company::Get(GetTileOwner(tile
));
511 if (flags
& DC_EXEC
) {
512 c
->location_of_HQ
= INVALID_TILE
; // reset HQ position
513 SetWindowDirty(WC_COMPANY
, c
->index
);
514 CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS
, c
->index
);
517 /* cost of relocating company is 1% of company value */
518 cost
= CommandCost(EXPENSES_PROPERTY
, CalculateCompanyValue(c
) / 100);
523 if (flags
& DC_EXEC
) {
524 Town
*town
= o
->town
;
525 ClrBit(town
->statues
, GetTileOwner(tile
));
526 SetWindowDirty(WC_TOWN_AUTHORITY
, town
->index
);
534 ClearedObjectArea
*cleared_area
= _cleared_object_areas
.Append();
535 cleared_area
->first_tile
= tile
;
536 cleared_area
->area
= ta
;
538 if (flags
& DC_EXEC
) ReallyClearObjectTile(o
);
543 static void AddAcceptedCargo_Object(TileIndex tile
, CargoArray
&acceptance
, uint32
*always_accepted
)
545 if (!IsObjectType(tile
, OBJECT_HQ
)) return;
547 /* HQ accepts passenger and mail; but we have to divide the values
548 * between 4 tiles it occupies! */
550 /* HQ level (depends on company performance) in the range 1..5. */
551 uint level
= GetCompanyHQSize(tile
) + 1;
553 /* Top town building generates 10, so to make HQ interesting, the top
555 acceptance
[CT_PASSENGERS
] += max(1U, level
);
556 SetBit(*always_accepted
, CT_PASSENGERS
);
558 /* Top town building generates 4, HQ can make up to 8. The
559 * proportion passengers:mail is different because such a huge
560 * commercial building generates unusually high amount of mail
561 * correspondence per physical visitor. */
562 acceptance
[CT_MAIL
] += max(1U, level
/ 2);
563 SetBit(*always_accepted
, CT_MAIL
);
567 static void GetTileDesc_Object(TileIndex tile
, TileDesc
*td
)
569 const ObjectSpec
*spec
= ObjectSpec::GetByTile(tile
);
570 td
->str
= spec
->name
;
571 td
->owner
[0] = GetTileOwner(tile
);
572 td
->build_date
= Object::GetByTile(tile
)->build_date
;
574 if (spec
->grf_prop
.grffile
!= NULL
) {
575 td
->grf
= GetGRFConfig(spec
->grf_prop
.grffile
->grfid
)->GetName();
579 static void TileLoop_Object(TileIndex tile
)
581 const ObjectSpec
*spec
= ObjectSpec::GetByTile(tile
);
582 if (spec
->flags
& OBJECT_FLAG_ANIMATION
) {
583 Object
*o
= Object::GetByTile(tile
);
584 TriggerObjectTileAnimation(o
, tile
, OAT_TILELOOP
, spec
);
585 if (o
->location
.tile
== tile
) TriggerObjectAnimation(o
, OAT_256_TICKS
, spec
);
588 if (IsTileOnWater(tile
)) TileLoop_Water(tile
);
590 if (!IsObjectType(tile
, OBJECT_HQ
)) return;
592 /* HQ accepts passenger and mail; but we have to divide the values
593 * between 4 tiles it occupies! */
595 /* HQ level (depends on company performance) in the range 1..5. */
596 uint level
= GetCompanyHQSize(tile
) + 1;
599 StationFinder
stations(TileArea(tile
, 2, 2));
602 /* Top town buildings generate 250, so the top HQ type makes 256. */
603 if (GB(r
, 0, 8) < (256 / 4 / (6 - level
))) {
604 uint amt
= GB(r
, 0, 8) / 8 / 4 + 1;
605 if (EconomyIsInRecession()) amt
= (amt
+ 1) >> 1;
606 MoveGoodsToStation(CT_PASSENGERS
, amt
, ST_HEADQUARTERS
, GetTileOwner(tile
), stations
.GetStations());
609 /* Top town building generates 90, HQ can make up to 196. The
610 * proportion passengers:mail is about the same as in the acceptance
612 if (GB(r
, 8, 8) < (196 / 4 / (6 - level
))) {
613 uint amt
= GB(r
, 8, 8) / 8 / 4 + 1;
614 if (EconomyIsInRecession()) amt
= (amt
+ 1) >> 1;
615 MoveGoodsToStation(CT_MAIL
, amt
, ST_HEADQUARTERS
, GetTileOwner(tile
), stations
.GetStations());
620 static TrackStatus
GetTileTrackStatus_Object(TileIndex tile
, TransportType mode
, uint sub_mode
, DiagDirection side
)
625 static bool ClickTile_Object(TileIndex tile
)
627 if (!IsObjectType(tile
, OBJECT_HQ
)) return false;
629 ShowCompany(GetTileOwner(tile
));
633 static void AnimateTile_Object(TileIndex tile
)
635 AnimateNewObjectTile(tile
);
639 * Helper function for \c CircularTileSearch.
640 * @param tile The tile to check.
641 * @param user Ignored.
642 * @return True iff the tile has a radio tower.
644 static bool HasTransmitter(TileIndex tile
, void *user
)
646 return IsObjectTypeTile(tile
, OBJECT_TRANSMITTER
);
650 * Try to build a lighthouse.
651 * @return True iff building a lighthouse succeeded.
653 static bool TryBuildLightHouse()
655 uint maxx
= MapMaxX();
656 uint maxy
= MapMaxY();
659 /* Scatter the lighthouses more evenly around the perimeter */
660 int perimeter
= (GB(r
, 16, 16) % (2 * (maxx
+ maxy
))) - maxy
;
662 for (dir
= DIAGDIR_NE
; perimeter
> 0; dir
++) {
663 perimeter
-= (DiagDirToAxis(dir
) == AXIS_X
) ? maxx
: maxy
;
669 case DIAGDIR_NE
: tile
= TileXY(maxx
- 1, r
% maxy
); break;
670 case DIAGDIR_SE
: tile
= TileXY(r
% maxx
, 1); break;
671 case DIAGDIR_SW
: tile
= TileXY(1, r
% maxy
); break;
672 case DIAGDIR_NW
: tile
= TileXY(r
% maxx
, maxy
- 1); break;
675 /* Only build lighthouses at tiles where the border is sea. */
676 if (!IsTileType(tile
, MP_WATER
)) return false;
678 for (int j
= 0; j
< 19; j
++) {
680 if (IsTileType(tile
, MP_CLEAR
) && IsTileFlat(tile
, &h
) && h
<= 2 && !IsBridgeAbove(tile
)) {
681 BuildObject(OBJECT_LIGHTHOUSE
, tile
);
682 assert(tile
< MapSize());
685 tile
+= TileOffsByDiagDir(dir
);
686 if (!IsValidTile(tile
)) return false;
692 * Try to build a transmitter.
693 * @return True iff a transmitter was built.
695 static bool TryBuildTransmitter()
697 TileIndex tile
= RandomTile();
699 if (IsTileType(tile
, MP_CLEAR
) && IsTileFlat(tile
, &h
) && h
>= 4 && !IsBridgeAbove(tile
)) {
701 if (CircularTileSearch(&t
, 9, HasTransmitter
, NULL
)) return false;
703 BuildObject(OBJECT_TRANSMITTER
, tile
);
709 void GenerateObjects()
711 /* Set a guestimate on how much we progress */
712 SetGeneratingWorldProgress(GWP_OBJECT
, NUM_OBJECTS
);
714 /* Determine number of water tiles at map border needed for freeform_edges */
715 uint num_water_tiles
= 0;
716 if (_settings_game
.construction
.freeform_edges
) {
717 for (uint x
= 0; x
< MapMaxX(); x
++) {
718 if (IsTileType(TileXY(x
, 1), MP_WATER
)) num_water_tiles
++;
719 if (IsTileType(TileXY(x
, MapMaxY() - 1), MP_WATER
)) num_water_tiles
++;
721 for (uint y
= 1; y
< MapMaxY() - 1; y
++) {
722 if (IsTileType(TileXY(1, y
), MP_WATER
)) num_water_tiles
++;
723 if (IsTileType(TileXY(MapMaxX() - 1, y
), MP_WATER
)) num_water_tiles
++;
727 /* Iterate over all possible object types */
728 for (uint i
= 0; i
< NUM_OBJECTS
; i
++) {
729 const ObjectSpec
*spec
= ObjectSpec::Get(i
);
731 /* Continue, if the object was never available till now or shall not be placed */
732 if (!spec
->WasEverAvailable() || spec
->generate_amount
== 0) continue;
734 uint16 amount
= spec
->generate_amount
;
736 /* Scale by map size */
737 if ((spec
->flags
& OBJECT_FLAG_SCALE_BY_WATER
) && _settings_game
.construction
.freeform_edges
) {
738 /* Scale the amount of lighthouses with the amount of land at the borders.
739 * The -6 is because the top borders are MP_VOID (-2) and all corners
740 * are counted twice (-4). */
741 amount
= ScaleByMapSize1D(amount
* num_water_tiles
) / (2 * MapMaxY() + 2 * MapMaxX() - 6);
742 } else if (spec
->flags
& OBJECT_FLAG_SCALE_BY_WATER
) {
743 amount
= ScaleByMapSize1D(amount
);
745 amount
= ScaleByMapSize(amount
);
748 /* Now try to place the requested amount of this object */
749 for (uint j
= ScaleByMapSize(1000); j
!= 0 && amount
!= 0 && Object::CanAllocateItem(); j
--) {
751 case OBJECT_TRANSMITTER
:
752 if (TryBuildTransmitter()) amount
--;
755 case OBJECT_LIGHTHOUSE
:
756 if (TryBuildLightHouse()) amount
--;
760 uint8 view
= RandomRange(spec
->views
);
761 if (CmdBuildObject(RandomTile(), DC_EXEC
| DC_AUTO
| DC_NO_TEST_TOWN_RATING
| DC_NO_MODIFY_TOWN_RATING
, i
, view
, NULL
).Succeeded()) amount
--;
765 IncreaseGeneratingWorldProgress(GWP_OBJECT
);
769 static void ChangeTileOwner_Object(TileIndex tile
, Owner old_owner
, Owner new_owner
)
771 if (!IsTileOwner(tile
, old_owner
)) return;
773 bool do_clear
= false;
775 if (IsObjectType(tile
, OBJECT_OWNED_LAND
) && new_owner
!= INVALID_OWNER
) {
776 SetTileOwner(tile
, new_owner
);
777 } else if (IsObjectType(tile
, OBJECT_STATUE
)) {
778 Town
*t
= Object::GetByTile(tile
)->town
;
779 ClrBit(t
->statues
, old_owner
);
780 if (new_owner
!= INVALID_OWNER
&& !HasBit(t
->statues
, new_owner
)) {
781 /* Transfer ownership to the new company */
782 SetBit(t
->statues
, new_owner
);
783 SetTileOwner(tile
, new_owner
);
788 SetWindowDirty(WC_TOWN_AUTHORITY
, t
->index
);
794 ReallyClearObjectTile(Object::GetByTile(tile
));
795 /* When clearing objects, they may turn into canal, which may require transfering ownership. */
796 ChangeTileOwner(tile
, old_owner
, new_owner
);
800 static CommandCost
TerraformTile_Object(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
802 ObjectType type
= GetObjectType(tile
);
804 if (type
== OBJECT_OWNED_LAND
) {
805 /* Owned land remains unsold */
806 CommandCost ret
= CheckTileOwnership(tile
);
807 if (ret
.Succeeded()) return CommandCost();
808 } else if (AutoslopeEnabled() && type
!= OBJECT_TRANSMITTER
&& type
!= OBJECT_LIGHTHOUSE
) {
810 * - Both new and old slope must not be steep.
811 * - TileMaxZ must not be changed.
812 * - Allow autoslope by default.
813 * - Disallow autoslope if callback succeeds and returns non-zero.
815 Slope tileh_old
= GetTileSlope(tile
);
816 /* TileMaxZ must not be changed. Slopes must not be steep. */
817 if (!IsSteepSlope(tileh_old
) && !IsSteepSlope(tileh_new
) && (GetTileMaxZ(tile
) == z_new
+ GetSlopeMaxZ(tileh_new
))) {
818 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
820 /* Call callback 'disable autosloping for objects'. */
821 if (HasBit(spec
->callback_mask
, CBM_OBJ_AUTOSLOPE
)) {
822 /* If the callback fails, allow autoslope. */
823 uint16 res
= GetObjectCallback(CBID_OBJECT_AUTOSLOPE
, 0, 0, spec
, Object::GetByTile(tile
), tile
);
824 if (res
== CALLBACK_FAILED
|| !ConvertBooleanCallback(spec
->grf_prop
.grffile
, CBID_OBJECT_AUTOSLOPE
, res
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
825 } else if (spec
->enabled
) {
826 /* allow autoslope */
827 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
832 return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
835 extern const TileTypeProcs _tile_type_object_procs
= {
836 DrawTile_Object
, // draw_tile_proc
837 GetSlopePixelZ_Object
, // get_slope_z_proc
838 ClearTile_Object
, // clear_tile_proc
839 AddAcceptedCargo_Object
, // add_accepted_cargo_proc
840 GetTileDesc_Object
, // get_tile_desc_proc
841 GetTileTrackStatus_Object
, // get_tile_track_status_proc
842 ClickTile_Object
, // click_tile_proc
843 AnimateTile_Object
, // animate_tile_proc
844 TileLoop_Object
, // tile_loop_proc
845 ChangeTileOwner_Object
, // change_tile_owner_proc
846 NULL
, // add_produced_cargo_proc
847 NULL
, // vehicle_enter_tile_proc
848 GetFoundation_Object
, // get_foundation_proc
849 TerraformTile_Object
, // terraform_tile_proc