# ai_marine.cpp

Go to the documentation of this file.

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