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"
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 "map/object.h"
31 #include "map/bridge.h"
32 #include "object_base.h"
33 #include "newgrf_config.h"
34 #include "newgrf_object.h"
35 #include "date_func.h"
36 #include "newgrf_debug.h"
37 #include "vehicle_func.h"
39 #include "table/strings.h"
40 #include "table/object_land.h"
42 template<> Object::Pool
Object::PoolItem::pool ("Object");
43 INSTANTIATE_POOL_METHODS(Object
)
44 uint16
Object::counts
[NUM_OBJECTS
];
47 * Get the object associated with a tile.
48 * @param tile The tile to fetch the object for.
51 /* static */ Object
*Object::GetByTile(TileIndex tile
)
53 return Object::Get(GetObjectIndex(tile
));
57 * Gets the ObjectType of the given object tile
58 * @param t the tile to get the type from.
59 * @pre IsObjectTile(t)
62 ObjectType
GetObjectType(TileIndex t
)
64 assert(IsObjectTile(t
));
65 return Object::GetByTile(t
)->type
;
68 /** Initialize/reset the objects. */
69 void InitializeObjects()
71 Object::ResetTypeCounts();
75 * Actually build the object.
76 * @param type The type of object to build.
77 * @param tile The tile to build the northern tile of the object on.
78 * @param owner The owner of the object.
79 * @param town Town the tile is related with.
80 * @param view The view for the object.
81 * @pre All preconditions for building the object at that location
82 * are met, e.g. slope and clearness of tiles are checked.
84 void BuildObject(ObjectType type
, TileIndex tile
, CompanyID owner
, Town
*town
, uint8 view
)
86 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
88 TileArea
ta(tile
, GB(spec
->size
, HasBit(view
, 0) ? 4 : 0, 4), GB(spec
->size
, HasBit(view
, 0) ? 0 : 4, 4));
89 Object
*o
= new Object();
92 o
->town
= town
== NULL
? CalcClosestTownFromTile(tile
) : town
;
93 o
->build_date
= _date
;
96 /* If nothing owns the object, the colour will be random. Otherwise
97 * get the colour from the company's livery settings. */
98 if (owner
== OWNER_NONE
) {
101 const Livery
*l
= Company::Get(owner
)->livery
;
102 o
->colour
= l
->colour1
+ l
->colour2
* 16;
105 /* If the object wants only one colour, then give it that colour. */
106 if ((spec
->flags
& OBJECT_FLAG_2CC_COLOUR
) == 0) o
->colour
&= 0xF;
108 if (HasBit(spec
->callback_mask
, CBM_OBJ_COLOUR
)) {
109 uint16 res
= GetObjectCallback(CBID_OBJECT_COLOUR
, o
->colour
, 0, spec
, o
, tile
);
110 if (res
!= CALLBACK_FAILED
) {
111 if (res
>= 0x100) ErrorUnknownCallbackResult(spec
->grf_prop
.grffile
->grfid
, CBID_OBJECT_COLOUR
, res
);
112 o
->colour
= GB(res
, 0, 8);
116 assert(o
->town
!= NULL
);
118 TILE_AREA_LOOP(t
, ta
) {
119 WaterClass wc
= (IsPlainWaterTile(t
) ? GetWaterClass(t
) : WATER_CLASS_INVALID
);
120 /* Update company infrastructure counts for objects build on canals owned by nobody. */
121 if (wc
== WATER_CLASS_CANAL
&& owner
!= OWNER_NONE
&& (IsTileOwner(tile
, OWNER_NONE
) || IsTileOwner(tile
, OWNER_WATER
))) {
122 Company::Get(owner
)->infrastructure
.water
++;
123 DirtyCompanyInfrastructureWindows(owner
);
125 MakeObject(t
, owner
, o
->index
, wc
, Random());
126 MarkTileDirtyByTile(t
);
129 Object::IncTypeCount(type
);
130 if (spec
->flags
& OBJECT_FLAG_ANIMATION
) TriggerObjectAnimation(o
, OAT_BUILT
, spec
);
134 * Increase the animation stage of a whole structure.
135 * @param tile The tile of the structure.
137 static void IncreaseAnimationStage(TileIndex tile
)
139 TileArea ta
= Object::GetByTile(tile
)->location
;
140 TILE_AREA_LOOP(t
, ta
) {
141 SetAnimationFrame(t
, GetAnimationFrame(t
) + 1);
142 MarkTileDirtyByTile(t
);
146 /** We encode the company HQ size in the animation stage. */
147 #define GetCompanyHQSize GetAnimationFrame
148 /** We encode the company HQ size in the animation stage. */
149 #define IncreaseCompanyHQSize IncreaseAnimationStage
152 * Update the CompanyHQ to the state associated with the given score
153 * @param tile The (northern) tile of the company HQ, or INVALID_TILE.
154 * @param score The current (performance) score of the company.
156 void UpdateCompanyHQ(TileIndex tile
, uint score
)
158 if (tile
== INVALID_TILE
) return;
161 (val
= 0, score
< 170) ||
162 (val
++, score
< 350) ||
163 (val
++, score
< 520) ||
164 (val
++, score
< 720) ||
167 while (GetCompanyHQSize(tile
) < val
) {
168 IncreaseCompanyHQSize(tile
);
173 * Updates the colour of the object whenever a company changes.
174 * @param c The company the company colour changed of.
176 void UpdateObjectColours(const Company
*c
)
179 FOR_ALL_OBJECTS(obj
) {
180 Owner owner
= GetTileOwner(obj
->location
.tile
);
181 /* Not the current owner, so colour doesn't change. */
182 if (owner
!= c
->index
) continue;
184 const ObjectSpec
*spec
= ObjectSpec::GetByTile(obj
->location
.tile
);
185 /* Using the object colour callback, so not using company colour. */
186 if (HasBit(spec
->callback_mask
, CBM_OBJ_COLOUR
)) continue;
188 const Livery
*l
= c
->livery
;
189 obj
->colour
= ((spec
->flags
& OBJECT_FLAG_2CC_COLOUR
) ? (l
->colour2
* 16) : 0) + l
->colour1
;
193 extern CommandCost
CheckBuildableTile(TileIndex tile
, uint invalid_dirs
, int &allowed_z
, bool allow_steep
, bool check_bridge
);
194 static CommandCost
ClearTile_Object(TileIndex tile
, DoCommandFlag flags
);
197 * Build an object object
198 * @param tile tile where the object will be located
199 * @param flags type of operation
200 * @param p1 the object type to build
201 * @param p2 the view for the object
203 * @return the cost of this operation or an error
205 CommandCost
CmdBuildObject(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
207 CommandCost
cost(EXPENSES_PROPERTY
);
209 ObjectType type
= (ObjectType
)GB(p1
, 0, 16);
210 if (type
>= NUM_OBJECTS
) return CMD_ERROR
;
211 uint8 view
= GB(p2
, 0, 2);
212 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
213 if (_game_mode
== GM_NORMAL
&& !spec
->IsAvailable() && !_generating_world
) return CMD_ERROR
;
214 if ((_game_mode
== GM_EDITOR
|| _generating_world
) && !spec
->WasEverAvailable()) return CMD_ERROR
;
216 if ((spec
->flags
& OBJECT_FLAG_ONLY_IN_SCENEDIT
) != 0 && ((!_generating_world
&& _game_mode
!= GM_EDITOR
) || _current_company
!= OWNER_NONE
)) return CMD_ERROR
;
217 if ((spec
->flags
& OBJECT_FLAG_ONLY_IN_GAME
) != 0 && (_generating_world
|| _game_mode
!= GM_NORMAL
|| _current_company
> MAX_COMPANIES
)) return CMD_ERROR
;
218 if (view
>= spec
->views
) return CMD_ERROR
;
220 if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS
);
221 if (Town::GetNumItems() == 0) return_cmd_error(STR_ERROR_MUST_FOUND_TOWN_FIRST
);
223 int size_x
= GB(spec
->size
, HasBit(view
, 0) ? 4 : 0, 4);
224 int size_y
= GB(spec
->size
, HasBit(view
, 0) ? 0 : 4, 4);
225 TileArea
ta(tile
, size_x
, size_y
);
227 if (type
== OBJECT_OWNED_LAND
) {
228 /* Owned land is special as it can be placed on any slope. */
229 cost
.AddCost(DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
));
231 /* Check the surface to build on. At this time we can't actually execute the
232 * the CLEAR_TILE commands since the newgrf callback later on can check
233 * some information about the tiles. */
234 bool allow_water
= (spec
->flags
& (OBJECT_FLAG_BUILT_ON_WATER
| OBJECT_FLAG_NOT_ON_LAND
)) != 0;
235 bool allow_ground
= (spec
->flags
& OBJECT_FLAG_NOT_ON_LAND
) == 0;
236 TILE_AREA_LOOP(t
, ta
) {
237 if (HasTileWaterGround(t
)) {
238 if (!allow_water
) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER
);
239 if (!IsPlainWaterTile(t
)) {
240 /* Normal water tiles don't have to be cleared. For all other tile types clear
241 * the tile but leave the water. */
242 cost
.AddCost(DoCommand(t
, 0, 0, flags
& ~DC_NO_WATER
& ~DC_EXEC
, CMD_LANDSCAPE_CLEAR
));
244 /* Can't build on water owned by another company. */
245 Owner o
= GetTileOwner(t
);
246 if (o
!= OWNER_NONE
&& o
!= OWNER_WATER
) cost
.AddCost(CheckOwnership(o
, t
));
248 /* However, the tile has to be clear of vehicles. */
249 cost
.AddCost(EnsureNoVehicleOnGround(t
));
252 if (!allow_ground
) return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER
);
253 /* For non-water tiles, we'll have to clear it before building. */
254 cost
.AddCost(DoCommand(t
, 0, 0, flags
& ~DC_EXEC
, CMD_LANDSCAPE_CLEAR
));
258 /* So, now the surface is checked... check the slope of said surface. */
260 if (GetTileSlope(tile
, &allowed_z
) != SLOPE_FLAT
) allowed_z
++;
262 TILE_AREA_LOOP(t
, ta
) {
263 uint16 callback
= CALLBACK_FAILED
;
264 if (HasBit(spec
->callback_mask
, CBM_OBJ_SLOPE_CHECK
)) {
265 TileIndex diff
= t
- tile
;
266 callback
= GetObjectCallback(CBID_OBJECT_LAND_SLOPE_CHECK
, GetTileSlope(t
), TileY(diff
) << 4 | TileX(diff
), spec
, NULL
, t
, view
);
269 if (callback
== CALLBACK_FAILED
) {
270 cost
.AddCost(CheckBuildableTile(t
, 0, allowed_z
, false, false));
272 /* The meaning of bit 10 is inverted for a grf version < 8. */
273 if (spec
->grf_prop
.grffile
->grf_version
< 8) ToggleBit(callback
, 10);
274 CommandCost ret
= GetErrorMessageFromLocationCallbackResult(callback
, spec
->grf_prop
.grffile
, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION
);
275 if (ret
.Failed()) return ret
;
279 if (flags
& DC_EXEC
) {
280 /* This is basically a copy of the loop above with the exception that we now
281 * execute the commands and don't check for errors, since that's already done. */
282 TILE_AREA_LOOP(t
, ta
) {
283 if (HasTileWaterGround(t
)) {
284 if (!IsPlainWaterTile(t
)) {
285 DoCommand(t
, 0, 0, (flags
& ~DC_NO_WATER
) | DC_NO_MODIFY_TOWN_RATING
, CMD_LANDSCAPE_CLEAR
);
288 DoCommand(t
, 0, 0, flags
| DC_NO_MODIFY_TOWN_RATING
, CMD_LANDSCAPE_CLEAR
);
293 if (cost
.Failed()) return cost
;
295 /* Finally do a check for bridges. */
296 TILE_AREA_LOOP(t
, ta
) {
297 if (HasBridgeAbove(t
) && (
298 !(spec
->flags
& OBJECT_FLAG_ALLOW_UNDER_BRIDGE
) ||
299 (GetTileMaxZ(t
) + spec
->height
>= GetBridgeHeight(GetSouthernBridgeEnd(t
))))) {
300 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST
);
306 case OBJECT_TRANSMITTER
:
307 case OBJECT_LIGHTHOUSE
:
308 if (!IsTileFlat(tile
)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED
);
311 case OBJECT_OWNED_LAND
:
312 if (IsObjectTile(tile
) &&
313 IsTileOwner(tile
, _current_company
) &&
314 IsObjectType(tile
, OBJECT_OWNED_LAND
)) {
315 return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT
);
320 Company
*c
= Company::Get(_current_company
);
321 if (c
->location_of_HQ
!= INVALID_TILE
) {
322 /* We need to persuade a bit harder to remove the old HQ. */
323 _current_company
= OWNER_WATER
;
324 cost
.AddCost(ClearTile_Object(c
->location_of_HQ
, flags
));
325 _current_company
= c
->index
;
328 if (flags
& DC_EXEC
) {
329 hq_score
= UpdateCompanyRatingAndValue(c
, false);
330 c
->location_of_HQ
= tile
;
331 SetWindowDirty(WC_COMPANY
, c
->index
);
337 /* This may never be constructed using this method. */
340 default: // i.e. NewGRF provided.
344 if (flags
& DC_EXEC
) {
345 BuildObject(type
, tile
, _current_company
, NULL
, view
);
347 /* Make sure the HQ starts at the right size. */
348 if (type
== OBJECT_HQ
) UpdateCompanyHQ(tile
, hq_score
);
351 cost
.AddCost(ObjectSpec::Get(type
)->GetBuildCost() * size_x
* size_y
);
356 static Foundation
GetFoundation_Object(TileIndex tile
, Slope tileh
);
358 static void DrawTile_Object(TileInfo
*ti
)
360 ObjectType type
= GetObjectType(ti
->tile
);
361 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
363 /* Fall back for when the object doesn't exist anymore. */
364 if (!spec
->enabled
) type
= OBJECT_TRANSMITTER
;
366 if ((spec
->flags
& OBJECT_FLAG_HAS_NO_FOUNDATION
) == 0) DrawFoundation(ti
, GetFoundation_Object(ti
->tile
, ti
->tileh
));
368 if (type
< NEW_OBJECT_OFFSET
) {
369 const DrawTileSprites
*dts
= NULL
;
370 Owner to
= GetTileOwner(ti
->tile
);
371 PaletteID palette
= to
== OWNER_NONE
? PAL_NONE
: COMPANY_SPRITE_COLOUR(to
);
373 if (type
== OBJECT_HQ
) {
374 TileIndex diff
= ti
->tile
- Object::GetByTile(ti
->tile
)->location
.tile
;
375 dts
= &_object_hq
[GetCompanyHQSize(ti
->tile
) << 2 | TileY(diff
) << 1 | TileX(diff
)];
377 dts
= &_objects
[type
];
380 if (spec
->flags
& OBJECT_FLAG_HAS_NO_FOUNDATION
) {
381 /* If an object has no foundation, but tries to draw a (flat) ground
382 * type... we have to be nice and convert that for them. */
383 switch (dts
->ground
.sprite
) {
384 case SPR_FLAT_BARE_LAND
: DrawClearLandTile(ti
, 0); break;
385 case SPR_FLAT_1_THIRD_GRASS_TILE
: DrawClearLandTile(ti
, 1); break;
386 case SPR_FLAT_2_THIRD_GRASS_TILE
: DrawClearLandTile(ti
, 2); break;
387 case SPR_FLAT_GRASS_TILE
: DrawClearLandTile(ti
, 3); break;
388 default: DrawGroundSprite(dts
->ground
.sprite
, palette
); break;
391 DrawGroundSprite(dts
->ground
.sprite
, palette
);
394 if (!IsInvisibilitySet(TO_STRUCTURES
)) {
395 const DrawTileSeqStruct
*dtss
;
396 foreach_draw_tile_seq(dtss
, dts
->seq
) {
397 AddSortableSpriteToDraw(
398 dtss
->image
.sprite
, palette
,
399 ti
->x
+ dtss
->delta_x
, ti
->y
+ dtss
->delta_y
,
400 dtss
->size_x
, dtss
->size_y
,
401 dtss
->size_z
, ti
->z
+ dtss
->delta_z
,
402 IsTransparencySet(TO_STRUCTURES
)
407 DrawNewObjectTile(ti
, spec
);
410 DrawBridgeMiddle(ti
);
413 static int GetSlopePixelZ_Object(TileIndex tile
, uint x
, uint y
)
415 if (IsObjectType(tile
, OBJECT_OWNED_LAND
)) {
417 Slope tileh
= GetTilePixelSlope(tile
, &z
);
419 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
421 return GetTileMaxPixelZ(tile
);
425 static Foundation
GetFoundation_Object(TileIndex tile
, Slope tileh
)
427 return IsObjectType(tile
, OBJECT_OWNED_LAND
) ? FOUNDATION_NONE
: FlatteningFoundation(tileh
);
431 * Perform the actual removal of the object from the map.
432 * @param o The object to really clear.
434 static void ReallyClearObjectTile(Object
*o
)
436 Object::DecTypeCount(o
->type
);
437 TILE_AREA_LOOP(tile_cur
, o
->location
) {
438 DeleteNewGRFInspectWindow(GSF_OBJECTS
, tile_cur
);
440 MakeWaterKeepingClass(tile_cur
, GetTileOwner(tile_cur
));
445 SmallVector
<ClearedObjectArea
, 4> _cleared_object_areas
;
448 * Find the entry in _cleared_object_areas which occupies a certain tile.
449 * @param tile Tile of interest
450 * @return Occupying entry, or NULL if none
452 ClearedObjectArea
*FindClearedObject(TileIndex tile
)
454 TileArea ta
= TileArea(tile
, 1, 1);
456 const ClearedObjectArea
*end
= _cleared_object_areas
.End();
457 for (ClearedObjectArea
*coa
= _cleared_object_areas
.Begin(); coa
!= end
; coa
++) {
458 if (coa
->area
.Intersects(ta
)) return coa
;
464 static CommandCost
ClearTile_Object(TileIndex tile
, DoCommandFlag flags
)
466 /* Get to the northern most tile. */
467 Object
*o
= Object::GetByTile(tile
);
468 TileArea ta
= o
->location
;
470 ObjectType type
= o
->type
;
471 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
473 CommandCost
cost(EXPENSES_CONSTRUCTION
, spec
->GetClearCost() * ta
.w
* ta
.h
/ 5);
474 if (spec
->flags
& OBJECT_FLAG_CLEAR_INCOME
) cost
.MultiplyCost(-1); // They get an income!
476 /* Towns can't remove any objects. */
477 if (_current_company
== OWNER_TOWN
) return CMD_ERROR
;
479 /* Water can remove everything! */
480 if (_current_company
!= OWNER_WATER
) {
481 if ((flags
& DC_NO_WATER
) && IsTileOnWater(tile
)) {
482 /* There is water under the object, treat it as water tile. */
483 return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER
);
484 } else if (!(spec
->flags
& OBJECT_FLAG_AUTOREMOVE
) && (flags
& DC_AUTO
)) {
485 /* No automatic removal by overbuilding stuff. */
486 return_cmd_error(type
== OBJECT_HQ
? STR_ERROR_COMPANY_HEADQUARTERS_IN
: STR_ERROR_OBJECT_IN_THE_WAY
);
487 } else if (_game_mode
== GM_EDITOR
) {
488 /* No further limitations for the editor. */
489 } else if (GetTileOwner(tile
) == OWNER_NONE
) {
490 /* Owned by nobody and unremovable, so we can only remove it with brute force! */
491 if (!_cheats
.magic_bulldozer
.value
&& (spec
->flags
& OBJECT_FLAG_CANNOT_REMOVE
) != 0) return CMD_ERROR
;
492 } else if (CheckTileOwnership(tile
).Failed()) {
493 /* We don't own it!. */
494 return_cmd_error(STR_ERROR_OWNED_BY
);
495 } else if ((spec
->flags
& OBJECT_FLAG_CANNOT_REMOVE
) != 0 && (spec
->flags
& OBJECT_FLAG_AUTOREMOVE
) == 0) {
496 /* In the game editor or with cheats we can remove, otherwise we can't. */
497 if (!_cheats
.magic_bulldozer
.value
) return CMD_ERROR
;
499 /* Removing with the cheat costs more in TTDPatch / the specs. */
500 cost
.MultiplyCost(25);
502 } else if ((spec
->flags
& (OBJECT_FLAG_BUILT_ON_WATER
| OBJECT_FLAG_NOT_ON_LAND
)) != 0) {
503 /* Water can't remove objects that are buildable on water. */
509 Company
*c
= Company::Get(GetTileOwner(tile
));
510 if (flags
& DC_EXEC
) {
511 c
->location_of_HQ
= INVALID_TILE
; // reset HQ position
512 SetWindowDirty(WC_COMPANY
, c
->index
);
513 CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS
, c
->index
);
516 /* cost of relocating company is 1% of company value */
517 cost
= CommandCost(EXPENSES_PROPERTY
, CalculateCompanyValue(c
) / 100);
522 if (flags
& DC_EXEC
) {
523 Town
*town
= o
->town
;
524 ClrBit(town
->statues
, GetTileOwner(tile
));
525 SetWindowDirty(WC_TOWN_AUTHORITY
, town
->index
);
533 ClearedObjectArea
*cleared_area
= _cleared_object_areas
.Append();
534 cleared_area
->first_tile
= tile
;
535 cleared_area
->area
= ta
;
537 if (flags
& DC_EXEC
) ReallyClearObjectTile(o
);
542 static void AddAcceptedCargo_Object(TileIndex tile
, CargoArray
&acceptance
, uint32
*always_accepted
)
544 if (!IsObjectType(tile
, OBJECT_HQ
)) return;
546 /* HQ accepts passenger and mail; but we have to divide the values
547 * between 4 tiles it occupies! */
549 /* HQ level (depends on company performance) in the range 1..5. */
550 uint level
= GetCompanyHQSize(tile
) + 1;
552 /* Top town building generates 10, so to make HQ interesting, the top
554 acceptance
[CT_PASSENGERS
] += max(1U, level
);
555 SetBit(*always_accepted
, CT_PASSENGERS
);
557 /* Top town building generates 4, HQ can make up to 8. The
558 * proportion passengers:mail is different because such a huge
559 * commercial building generates unusually high amount of mail
560 * correspondence per physical visitor. */
561 acceptance
[CT_MAIL
] += max(1U, level
/ 2);
562 SetBit(*always_accepted
, CT_MAIL
);
566 static void GetTileDesc_Object(TileIndex tile
, TileDesc
*td
)
568 const ObjectSpec
*spec
= ObjectSpec::GetByTile(tile
);
569 td
->str
= spec
->name
;
570 td
->owner
[0] = GetTileOwner(tile
);
571 td
->build_date
= Object::GetByTile(tile
)->build_date
;
573 if (spec
->grf_prop
.grffile
!= NULL
) {
574 td
->grf
= GetGRFConfig(spec
->grf_prop
.grffile
->grfid
)->GetName();
578 static void TileLoop_Object(TileIndex tile
)
580 const ObjectSpec
*spec
= ObjectSpec::GetByTile(tile
);
581 if (spec
->flags
& OBJECT_FLAG_ANIMATION
) {
582 Object
*o
= Object::GetByTile(tile
);
583 TriggerObjectTileAnimation(o
, tile
, OAT_TILELOOP
, spec
);
584 if (o
->location
.tile
== tile
) TriggerObjectAnimation(o
, OAT_256_TICKS
, spec
);
587 if (IsTileOnWater(tile
)) TileLoop_Water(tile
);
589 if (!IsObjectType(tile
, OBJECT_HQ
)) return;
591 /* HQ accepts passenger and mail; but we have to divide the values
592 * between 4 tiles it occupies! */
594 /* HQ level (depends on company performance) in the range 1..5. */
595 uint level
= GetCompanyHQSize(tile
) + 1;
598 StationFinder
stations(TileArea(tile
, 2, 2));
601 /* Top town buildings generate 250, so the top HQ type makes 256. */
602 if (GB(r
, 0, 8) < (256 / 4 / (6 - level
))) {
603 uint amt
= GB(r
, 0, 8) / 8 / 4 + 1;
604 if (EconomyIsInRecession()) amt
= (amt
+ 1) >> 1;
605 MoveGoodsToStation(CT_PASSENGERS
, amt
, ST_HEADQUARTERS
, GetTileOwner(tile
), stations
.GetStations());
608 /* Top town building generates 90, HQ can make up to 196. The
609 * proportion passengers:mail is about the same as in the acceptance
611 if (GB(r
, 8, 8) < (196 / 4 / (6 - level
))) {
612 uint amt
= GB(r
, 8, 8) / 8 / 4 + 1;
613 if (EconomyIsInRecession()) amt
= (amt
+ 1) >> 1;
614 MoveGoodsToStation(CT_MAIL
, amt
, ST_HEADQUARTERS
, GetTileOwner(tile
), stations
.GetStations());
619 static bool ClickTile_Object(TileIndex tile
)
621 if (!IsObjectType(tile
, OBJECT_HQ
)) return false;
623 ShowCompany(GetTileOwner(tile
));
627 static void AnimateTile_Object(TileIndex tile
)
629 AnimateNewObjectTile(tile
);
633 * Helper function for \c CircularTileSearch.
634 * @param tile The tile to check.
635 * @param user Ignored.
636 * @return True iff the tile has a radio tower.
638 static bool HasTransmitter(TileIndex tile
, void *user
)
640 return IsObjectTypeTile(tile
, OBJECT_TRANSMITTER
);
644 * Try to build a lighthouse.
645 * @return True iff building a lighthouse succeeded.
647 static bool TryBuildLightHouse()
649 uint maxx
= MapMaxX();
650 uint maxy
= MapMaxY();
653 /* Scatter the lighthouses more evenly around the perimeter */
654 int perimeter
= (GB(r
, 16, 16) % (2 * (maxx
+ maxy
))) - maxy
;
656 for (dir
= DIAGDIR_NE
; perimeter
> 0; dir
++) {
657 perimeter
-= (DiagDirToAxis(dir
) == AXIS_X
) ? maxx
: maxy
;
663 case DIAGDIR_NE
: tile
= TileXY(maxx
- 1, r
% maxy
); break;
664 case DIAGDIR_SE
: tile
= TileXY(r
% maxx
, 1); break;
665 case DIAGDIR_SW
: tile
= TileXY(1, r
% maxy
); break;
666 case DIAGDIR_NW
: tile
= TileXY(r
% maxx
, maxy
- 1); break;
669 /* Only build lighthouses at tiles where the border is sea. */
670 if (!IsWaterTile(tile
)) return false;
672 for (int j
= 0; j
< 19; j
++) {
674 if (IsGroundTile(tile
) && IsTileFlat(tile
, &h
) && h
<= 2 && !IsBridgeAbove(tile
)) {
675 BuildObject(OBJECT_LIGHTHOUSE
, tile
);
676 assert(tile
< MapSize());
679 tile
+= TileOffsByDiagDir(dir
);
680 if (!IsValidTile(tile
)) return false;
686 * Try to build a transmitter.
687 * @return True iff a transmitter was built.
689 static bool TryBuildTransmitter()
691 TileIndex tile
= RandomTile();
693 if (IsGroundTile(tile
) && IsTileFlat(tile
, &h
) && h
>= 4 && !IsBridgeAbove(tile
)) {
695 if (CircularTileSearch(&t
, 9, HasTransmitter
, NULL
)) return false;
697 BuildObject(OBJECT_TRANSMITTER
, tile
);
703 void GenerateObjects()
705 /* Set a guestimate on how much we progress */
706 SetGeneratingWorldProgress(GWP_OBJECT
, NUM_OBJECTS
);
708 /* Determine number of water tiles at map border needed for freeform_edges */
709 uint num_water_tiles
= 0;
710 if (_settings_game
.construction
.freeform_edges
) {
711 for (uint x
= 0; x
< MapMaxX(); x
++) {
712 if (IsWaterTile(TileXY(x
, 1))) num_water_tiles
++;
713 if (IsWaterTile(TileXY(x
, MapMaxY() - 1))) num_water_tiles
++;
715 for (uint y
= 1; y
< MapMaxY() - 1; y
++) {
716 if (IsWaterTile(TileXY(1, y
))) num_water_tiles
++;
717 if (IsWaterTile(TileXY(MapMaxX() - 1, y
))) num_water_tiles
++;
721 /* Iterate over all possible object types */
722 for (uint i
= 0; i
< NUM_OBJECTS
; i
++) {
723 const ObjectSpec
*spec
= ObjectSpec::Get(i
);
725 /* Continue, if the object was never available till now or shall not be placed */
726 if (!spec
->WasEverAvailable() || spec
->generate_amount
== 0) continue;
728 uint16 amount
= spec
->generate_amount
;
730 /* Scale by map size */
731 if ((spec
->flags
& OBJECT_FLAG_SCALE_BY_WATER
) && _settings_game
.construction
.freeform_edges
) {
732 /* Scale the amount of lighthouses with the amount of land at the borders.
733 * The -6 is because the top borders are void (-2) and all corners
734 * are counted twice (-4). */
735 amount
= ScaleByMapPerimeter(amount
* num_water_tiles
) / (2 * MapMaxY() + 2 * MapMaxX() - 6);
736 } else if (spec
->flags
& OBJECT_FLAG_SCALE_BY_WATER
) {
737 amount
= ScaleByMapPerimeter(amount
);
739 amount
= ScaleByMapSize(amount
);
742 /* Now try to place the requested amount of this object */
743 for (uint j
= ScaleByMapSize(1000); j
!= 0 && amount
!= 0 && Object::CanAllocateItem(); j
--) {
745 case OBJECT_TRANSMITTER
:
746 if (TryBuildTransmitter()) amount
--;
749 case OBJECT_LIGHTHOUSE
:
750 if (TryBuildLightHouse()) amount
--;
754 uint8 view
= RandomRange(spec
->views
);
755 if (CmdBuildObject(RandomTile(), DC_EXEC
| DC_AUTO
| DC_NO_TEST_TOWN_RATING
| DC_NO_MODIFY_TOWN_RATING
, i
, view
, NULL
).Succeeded()) amount
--;
759 IncreaseGeneratingWorldProgress(GWP_OBJECT
);
763 static void ChangeTileOwner_Object(TileIndex tile
, Owner old_owner
, Owner new_owner
)
765 if (!IsTileOwner(tile
, old_owner
)) return;
767 if (IsObjectType(tile
, OBJECT_OWNED_LAND
) && new_owner
!= INVALID_OWNER
) {
768 SetTileOwner(tile
, new_owner
);
769 } else if (IsObjectType(tile
, OBJECT_STATUE
)) {
770 Town
*t
= Object::GetByTile(tile
)->town
;
771 ClrBit(t
->statues
, old_owner
);
772 if (new_owner
!= INVALID_OWNER
&& !HasBit(t
->statues
, new_owner
)) {
773 /* Transfer ownership to the new company */
774 SetBit(t
->statues
, new_owner
);
775 SetTileOwner(tile
, new_owner
);
777 ReallyClearObjectTile(Object::GetByTile(tile
));
780 SetWindowDirty(WC_TOWN_AUTHORITY
, t
->index
);
782 ReallyClearObjectTile(Object::GetByTile(tile
));
786 static CommandCost
TerraformTile_Object(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
788 ObjectType type
= GetObjectType(tile
);
790 if (type
== OBJECT_OWNED_LAND
) {
791 /* Owned land remains unsold */
792 CommandCost ret
= CheckTileOwnership(tile
);
793 if (ret
.Succeeded()) return CommandCost();
794 } else if (AutoslopeEnabled() && type
!= OBJECT_TRANSMITTER
&& type
!= OBJECT_LIGHTHOUSE
) {
796 * - Both new and old slope must not be steep.
797 * - TileMaxZ must not be changed.
798 * - Allow autoslope by default.
799 * - Disallow autoslope if callback succeeds and returns non-zero.
801 Slope tileh_old
= GetTileSlope(tile
);
802 /* TileMaxZ must not be changed. Slopes must not be steep. */
803 if (!IsSteepSlope(tileh_old
) && !IsSteepSlope(tileh_new
) && (GetTileMaxZ(tile
) == z_new
+ GetSlopeMaxZ(tileh_new
))) {
804 const ObjectSpec
*spec
= ObjectSpec::Get(type
);
806 /* Call callback 'disable autosloping for objects'. */
807 if (HasBit(spec
->callback_mask
, CBM_OBJ_AUTOSLOPE
)) {
808 /* If the callback fails, allow autoslope. */
809 uint16 res
= GetObjectCallback(CBID_OBJECT_AUTOSLOPE
, 0, 0, spec
, Object::GetByTile(tile
), tile
);
810 if (res
== CALLBACK_FAILED
|| !ConvertBooleanCallback(spec
->grf_prop
.grffile
, CBID_OBJECT_AUTOSLOPE
, res
)) return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
811 } else if (spec
->enabled
) {
812 /* allow autoslope */
813 return CommandCost(EXPENSES_CONSTRUCTION
, _price
[PR_BUILD_FOUNDATION
]);
818 return DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
821 extern const TileTypeProcs _tile_type_object_procs
= {
822 DrawTile_Object
, // draw_tile_proc
823 GetSlopePixelZ_Object
, // get_slope_z_proc
824 ClearTile_Object
, // clear_tile_proc
825 AddAcceptedCargo_Object
, // add_accepted_cargo_proc
826 GetTileDesc_Object
, // get_tile_desc_proc
827 NULL
, // get_tile_railway_status_proc
828 NULL
, // get_tile_road_status_proc
829 NULL
, // get_tile_waterway_status_proc
830 ClickTile_Object
, // click_tile_proc
831 AnimateTile_Object
, // animate_tile_proc
832 TileLoop_Object
, // tile_loop_proc
833 ChangeTileOwner_Object
, // change_tile_owner_proc
834 NULL
, // add_produced_cargo_proc
835 GetFoundation_Object
, // get_foundation_proc
836 TerraformTile_Object
, // terraform_tile_proc