MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ClearTile_Object

Function ClearTile_Object

src/object_cmd.cpp:542–619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

540}
541
542static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlags flags)
543{
544 /* Get to the northern most tile. */
545 Object *o = Object::GetByTile(tile);
546 TileArea ta = o->location;
547
548 ObjectType type = o->type;
549 const ObjectSpec *spec = ObjectSpec::Get(type);
550
551 CommandCost cost(EXPENSES_CONSTRUCTION, spec->GetClearCost() * ta.w * ta.h / 5);
552 if (spec->flags.Test(ObjectFlag::ClearIncome)) cost.MultiplyCost(-1); // They get an income!
553
554 /* Towns can't remove any objects. */
555 if (_current_company == OWNER_TOWN) return CMD_ERROR;
556
557 /* Water can remove everything! */
558 if (_current_company != OWNER_WATER) {
559 if (flags.Test(DoCommandFlag::NoWater) && IsTileOnWater(tile)) {
560 /* There is water under the object, treat it as water tile. */
561 return CommandCost(STR_ERROR_CAN_T_BUILD_ON_WATER);
562 } else if (!spec->flags.Test(ObjectFlag::Autoremove) && flags.Test(DoCommandFlag::Auto)) {
563 /* No automatic removal by overbuilding stuff. */
564 return CommandCost(type == OBJECT_HQ ? STR_ERROR_COMPANY_HEADQUARTERS_IN : STR_ERROR_OBJECT_IN_THE_WAY);
565 } else if (_game_mode == GM_EDITOR) {
566 /* No further limitations for the editor. */
567 } else if (GetTileOwner(tile) == OWNER_NONE) {
568 /* Owned by nobody and unremovable, so we can only remove it with brute force! */
569 if (!_cheats.magic_bulldozer.value && spec->flags.Test(ObjectFlag::CannotRemove)) return CMD_ERROR;
570 } else if (CommandCost ret = CheckTileOwnership(tile); ret.Failed()) {
571 /* We don't own it!. */
572 return ret;
573 } else if (spec->flags.Test(ObjectFlag::CannotRemove) && !spec->flags.Test(ObjectFlag::Autoremove)) {
574 /* In the game editor or with cheats we can remove, otherwise we can't. */
575 if (!_cheats.magic_bulldozer.value) {
576 if (type == OBJECT_HQ) return CommandCost(STR_ERROR_COMPANY_HEADQUARTERS_IN);
577 return CMD_ERROR;
578 }
579
580 /* Removing with the cheat costs more in TTDPatch / the specs. */
581 cost.MultiplyCost(25);
582 }
583 } else if (spec->flags.Any({ObjectFlag::BuiltOnWater, ObjectFlag::NotOnLand})) {
584 /* Water can't remove objects that are buildable on water. */
585 return CMD_ERROR;
586 }
587
588 switch (type) {
589 case OBJECT_HQ: {
590 Company *c = Company::Get(GetTileOwner(tile));
591 if (flags.Test(DoCommandFlag::Execute)) {
592 c->location_of_HQ = INVALID_TILE; // reset HQ position
593 SetWindowDirty(WC_COMPANY, c->index);
594 CargoPacket::InvalidateAllFrom({c->index, SourceType::Headquarters});
595 }
596
597 /* cost of relocating company is 1% of company value */
598 cost = CommandCost(EXPENSES_CONSTRUCTION, CalculateCompanyValue(c) / 100);
599 break;

Callers 1

CmdBuildObjectFunction · 0.85

Calls 13

IsTileOnWaterFunction · 0.85
CommandCostClass · 0.85
GetTileOwnerFunction · 0.85
CheckTileOwnershipFunction · 0.85
SetWindowDirtyFunction · 0.85
CalculateCompanyValueFunction · 0.85
ReallyClearObjectTileFunction · 0.85
TestMethod · 0.80
MultiplyCostMethod · 0.80
FailedMethod · 0.80
AnyMethod · 0.80
GetClearCostMethod · 0.45

Tested by

no test coverage detected