diff options
author | terkhen <terkhen@openttd.org> | 2010-08-02 21:07:23 +0000 |
---|---|---|
committer | terkhen <terkhen@openttd.org> | 2010-08-02 21:07:23 +0000 |
commit | 7a6df202d0816db1e46c33949f99aed236e531b1 (patch) | |
tree | 3395506d65b1f54db511e97dd710e47200be4741 /src/town_cmd.cpp | |
parent | ca0751adb82094a3f7d0042f386f72f43b756575 (diff) | |
download | openttd-7a6df202d0816db1e46c33949f99aed236e531b1.tar.xz |
(svn r20323) -Codechange: Move Delete town code to a command.
Diffstat (limited to 'src/town_cmd.cpp')
-rw-r--r-- | src/town_cmd.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 702de3fc6..db93a7a00 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -45,6 +45,7 @@ #include "townname_type.h" #include "core/random_func.hpp" #include "core/backup_type.hpp" +#include "depot_base.h" #include "table/strings.h" #include "table/town_land.h" @@ -2349,6 +2350,70 @@ CommandCost CmdExpandTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } /** + * Delete a town (scenario editor only). + * @param tile Unused. + * @param flags Type of operation. + * @param p1 Town ID to delete. + * @param p2 Unused. + * @param text Unused. + * @return Empty cost or an error. + */ +CommandCost CmdDeleteTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) +{ + if (_game_mode != GM_EDITOR) return CMD_ERROR; + Town *t = Town::GetIfValid(p1); + if (t == NULL) return CMD_ERROR; + + /* Stations refer to towns. */ + const Station *st; + FOR_ALL_STATIONS(st) { + if (st->town == t) { + /* Non-oil rig stations are always a problem. */ + if (!(st->facilities & FACIL_AIRPORT) || st->airport.type != AT_OILRIG) return CMD_ERROR; + /* We can only automatically delete oil rigs *if* there's no vehicle on them. */ + CommandCost ret = DoCommand(st->airport.tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR); + if (ret.Failed()) return ret; + } + } + + /* Depots refer to towns. */ + const Depot *d; + FOR_ALL_DEPOTS(d) { + if (d->town == t) return CMD_ERROR; + } + + /* Check all tiles for town ownership. */ + for (TileIndex tile = 0; tile < MapSize(); ++tile) { + switch (GetTileType(tile)) { + case MP_ROAD: + if (HasTownOwnedRoad(tile) && GetTownIndex(tile) == t->index) { + /* Can we clear this tile? */ + CommandCost ret = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR); + if (ret.Failed()) return ret; + } + break; + + case MP_TUNNELBRIDGE: + if (IsTileOwner(tile, OWNER_TOWN) && + ClosestTownFromTile(tile, UINT_MAX) == t) { + /* Can we clear this bridge? */ + CommandCost ret = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR); + if (ret.Failed()) return ret; + } + break; + + default: + break; + } + } + + /* The town destructor will delete everything related to the town. */ + if (flags & DC_EXEC) delete t; + + return CommandCost(); +} + +/** * Factor in the cost of each town action. * @see TownActions */ |