# ai_marine.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "ai_marine.hpp"
    00013 #include "ai_station.hpp"
    00014 #include "../../station_base.h"
    00015 #include "../../tile_cmd.h"
    00016
    00017
    00018 /* static */ bool AIMarine::IsWaterDepotTile(TileIndex tile)
    00019 {
    00020   if (!::IsValidTile(tile)) return false;
    00021
    00022   return ::IsTileType(tile, MP_WATER) && ::GetWaterTileType(tile) == WATER_TILE_DEPOT;
    00023 }
    00024
    00025 /* static */ bool AIMarine::IsDockTile(TileIndex tile)
    00026 {
    00027   if (!::IsValidTile(tile)) return false;
    00028
    00029   return ::IsTileType(tile, MP_STATION) && ::IsDock(tile);
    00030 }
    00031
    00032 /* static */ bool AIMarine::IsBuoyTile(TileIndex tile)
    00033 {
    00034   if (!::IsValidTile(tile)) return false;
    00035
    00036   return ::IsTileType(tile, MP_STATION) && ::IsBuoy(tile);
    00037 }
    00038
    00039 /* static */ bool AIMarine::IsLockTile(TileIndex tile)
    00040 {
    00041   if (!::IsValidTile(tile)) return false;
    00042
    00043   return ::IsTileType(tile, MP_WATER) && ::GetWaterTileType(tile) == WATER_TILE_LOCK;
    00044 }
    00045
    00046 /* static */ bool AIMarine::IsCanalTile(TileIndex tile)
    00047 {
    00048   if (!::IsValidTile(tile)) return false;
    00049
    00050   return ::IsTileType(tile, MP_WATER) && ::IsCanal(tile);
    00051 }
    00052
    00053 /* static */ bool AIMarine::AreWaterTilesConnected(TileIndex t1, TileIndex t2)
    00054 {
    00055   if (!::IsValidTile(t1)) return false;
    00056   if (!::IsValidTile(t2)) return false;
    00057
    00058   /* Tiles not neighbouring */
    00059   if (::DistanceManhattan(t1, t2) != 1) return false;
    00060
    00061   DiagDirection to_other_tile = ::DiagdirBetweenTiles(t1, t2);
    00062
    00063   /* Determine the reachable tracks from the shared edge */
    00064   TrackBits gtts1 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t1, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
    00065   if (gtts1 == TRACK_BIT_NONE) return false;
    00066
    00067   to_other_tile = ReverseDiagDir(to_other_tile);
    00068   TrackBits gtts2 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t2, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
    00069
    00070   return gtts2 != TRACK_BIT_NONE;
    00071 }
    00072
    00073 /* static */ bool AIMarine::BuildWaterDepot(TileIndex tile, TileIndex front)
    00074 {
    00075   EnforcePrecondition(false, ::IsValidTile(tile));
    00076   EnforcePrecondition(false, ::IsValidTile(front));
    00077   EnforcePrecondition(false, (::TileX(front) == ::TileX(tile)) != (::TileY(front) == ::TileY(tile)));
    00078
    00079   return AIObject::DoCommand(tile, ::TileY(front) == ::TileY(tile), 0, CMD_BUILD_SHIP_DEPOT);
    00080 }
    00081
    00082 /* static */ bool AIMarine::BuildDock(TileIndex tile, StationID station_id)
    00083 {
    00084   EnforcePrecondition(false, ::IsValidTile(tile));
    00085   EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
    00086
    00087   uint p1 = station_id == AIStation::STATION_JOIN_ADJACENT ? 0 : 1;
    00088   uint p2 = (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
    00089   return AIObject::DoCommand(tile, p1, p2, CMD_BUILD_DOCK);
    00090 }
    00091
    00092 /* static */ bool AIMarine::BuildBuoy(TileIndex tile)
    00093 {
    00094   EnforcePrecondition(false, ::IsValidTile(tile));
    00095
    00096   return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_BUOY);
    00097 }
    00098
    00099 /* static */ bool AIMarine::BuildLock(TileIndex tile)
    00100 {
    00101   EnforcePrecondition(false, ::IsValidTile(tile));
    00102
    00103   return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_LOCK);
    00104 }
    00105
    00106 /* static */ bool AIMarine::BuildCanal(TileIndex tile)
    00107 {
    00108   EnforcePrecondition(false, ::IsValidTile(tile));
    00109
    00110   return AIObject::DoCommand(tile, tile, 0, CMD_BUILD_CANAL);
    00111 }
    00112
    00113 /* static */ bool AIMarine::RemoveWaterDepot(TileIndex tile)
    00114 {
    00115   EnforcePrecondition(false, ::IsValidTile(tile));
    00116   EnforcePrecondition(false, IsWaterDepotTile(tile));
    00117
    00118   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00119 }
    00120
    00121 /* static */ bool AIMarine::RemoveDock(TileIndex tile)
    00122 {
    00123   EnforcePrecondition(false, ::IsValidTile(tile));
    00124   EnforcePrecondition(false, IsDockTile(tile));
    00125
    00126   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00127 }
    00128
    00129 /* static */ bool AIMarine::RemoveBuoy(TileIndex tile)
    00130 {
    00131   EnforcePrecondition(false, ::IsValidTile(tile));
    00132   EnforcePrecondition(false, IsBuoyTile(tile));
    00133
    00134   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00135 }
    00136
    00137 /* static */ bool AIMarine::RemoveLock(TileIndex tile)
    00138 {
    00139   EnforcePrecondition(false, ::IsValidTile(tile));
    00140   EnforcePrecondition(false, IsLockTile(tile));
    00141
    00142   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00143 }
    00144
    00145 /* static */ bool AIMarine::RemoveCanal(TileIndex tile)
    00146 {
    00147   EnforcePrecondition(false, ::IsValidTile(tile));
    00148   EnforcePrecondition(false, IsCanalTile(tile));
    00149
    00150   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00151 }