# ai_marine.cpp

Go to the documentation of this file.

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