rubidium@9491: /* $Id$ */ rubidium@9491: truebrain@9833: /** @file ai_vehicle.cpp Implementation of AIVehicle. */ rubidium@9491: rubidium@9491: #include "ai_vehicle.hpp" truelight@9711: #include "ai_engine.hpp" rubidium@9491: #include "ai_cargo.hpp" truelight@9706: #include "ai_order.hpp" rubidium@9491: #include "../../depot.h" rubidium@9724: #include "../../player_func.h" truelight@9662: #include "../../aircraft.h" rubidium@10087: #include "../../string_func.h" rubidium@9723: #include "../../strings_func.h" rubidium@9723: #include "../../core/alloc_func.hpp" truebrain@9737: #include "../../command_type.h" truebrain@9737: #include "../../command_func.h" truelight@9699: #include "table/strings.h" rubidium@9491: rubidium@9497: /* static */ bool AIVehicle::IsValidVehicle(VehicleID vehicle_id) rubidium@9491: { truebrain@9736: return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player && ::GetVehicle(vehicle_id)->IsPrimaryVehicle(); rubidium@9491: } rubidium@9491: truebrain@9737: /* static */ VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, AIEngine::IsValidEngine(engine_id)); rubidium@9491: truelight@9560: /* Reset the internal NewVehicleID in case we are in TestMode */ truelight@9560: AIObject::SetNewVehicleID(0); truelight@9560: rubidium@9491: bool ret; rubidium@9491: switch (::GetEngine(engine_id)->type) { truebrain@9737: case VEH_ROAD: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_ROAD_VEH); break; truebrain@9737: case VEH_TRAIN: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_RAIL_VEHICLE); break; truebrain@9737: case VEH_SHIP: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_SHIP); break; truebrain@9737: case VEH_AIRCRAFT: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_AIRCRAFT); break; truelight@9654: default: NOT_REACHED(); return INVALID_VEHICLE; rubidium@9491: } rubidium@9491: truelight@9496: return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE; rubidium@9491: } rubidium@9491: truebrain@9737: /* static */ VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, IsValidVehicle(vehicle_id)); rubidium@9491: truelight@9560: /* Reset the internal NewVehicleID in case we are in TestMode */ truelight@9560: AIObject::SetNewVehicleID(0); truelight@9560: truebrain@9737: bool ret = AIObject::DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE); truelight@9496: return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE; rubidium@9491: } rubidium@9491: truebrain@9737: /* static */ bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, IsValidVehicle(vehicle_id) && AICargo::IsValidCargo(cargo)); rubidium@9491: rubidium@9491: switch (::GetVehicle(vehicle_id)->type) { truebrain@9737: case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH); truebrain@9737: case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_RAIL_VEHICLE); truebrain@9737: case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_SHIP); truebrain@9737: case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_AIRCRAFT); truelight@9654: default: return false; rubidium@9491: } rubidium@9491: } rubidium@9491: rubidium@9491: truebrain@9737: /* static */ bool AIVehicle::SellVehicle(VehicleID vehicle_id) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, IsValidVehicle(vehicle_id)); rubidium@9491: rubidium@9491: switch (::GetVehicle(vehicle_id)->type) { truebrain@9737: case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH); truebrain@9737: case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_RAIL_WAGON); truebrain@9737: case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_SHIP); truebrain@9737: case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_AIRCRAFT); truelight@9654: default: return false; rubidium@9491: } rubidium@9491: } rubidium@9491: truebrain@9737: /* static */ bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, IsValidVehicle(vehicle_id)); rubidium@9491: rubidium@9491: switch (::GetVehicle(vehicle_id)->type) { truebrain@9737: case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT); truebrain@9737: case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_TRAIN_TO_DEPOT); truebrain@9737: case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_SHIP_TO_DEPOT); truebrain@9737: case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_AIRCRAFT_TO_HANGAR); truelight@9654: default: return false; rubidium@9491: } rubidium@9491: } rubidium@9491: truebrain@9737: /* static */ bool AIVehicle::IsInDepot(VehicleID vehicle_id) truelight@9709: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return false; truelight@9709: return ::GetVehicle(vehicle_id)->IsInDepot(); truelight@9709: } truelight@9709: truebrain@9737: /* static */ bool AIVehicle::IsStoppedInDepot(VehicleID vehicle_id) truelight@9709: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return false; truelight@9709: return ::GetVehicle(vehicle_id)->IsStoppedInDepot(); truelight@9709: } truelight@9709: truebrain@9737: /* static */ bool AIVehicle::StartStopVehicle(VehicleID vehicle_id) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, IsValidVehicle(vehicle_id)); rubidium@9491: rubidium@9491: switch (::GetVehicle(vehicle_id)->type) { truebrain@9737: case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH); truebrain@9737: case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_TRAIN); truebrain@9737: case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_SHIP); truebrain@9737: case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_AIRCRAFT); truelight@9654: default: return false; rubidium@9491: } rubidium@9491: } rubidium@9491: truebrain@9737: /* static */ bool AIVehicle::SkipToVehicleOrder(VehicleID vehicle_id, uint32 order_id) rubidium@9491: { rubidium@10087: EnforcePrecondition(false, AIOrder::IsValidVehicleOrder(vehicle_id, order_id)); rubidium@9491: truebrain@9737: return AIObject::DoCommand(0, vehicle_id, order_id, CMD_SKIP_TO_ORDER); rubidium@9491: } truelight@9615: truebrain@9737: /* static */ bool AIVehicle::SetName(VehicleID vehicle_id, const char *name) truelight@9699: { rubidium@10087: EnforcePrecondition(false, IsValidVehicle(vehicle_id)); rubidium@10087: EnforcePrecondition(false, !StrEmpty(name)); truelight@9699: truelight@9699: _cmd_text = name; truebrain@9737: return AIObject::DoCommand(0, vehicle_id, 0, CMD_NAME_VEHICLE); truelight@9699: } truelight@9699: truelight@9637: /* static */ TileIndex AIVehicle::GetLocation(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return INVALID_TILE; truelight@9615: truelight@9615: return ::GetVehicle(vehicle_id)->tile; truelight@9615: } truelight@9615: truelight@9637: /* static */ EngineID AIVehicle::GetEngineType(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return INVALID_ENGINE; truelight@9615: truelight@9615: return ::GetVehicle(vehicle_id)->engine_type; truelight@9615: } truelight@9615: truelight@9637: /* static */ int32 AIVehicle::GetUnitNumber(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truelight@9615: truelight@9615: return ::GetVehicle(vehicle_id)->unitnumber; truelight@9615: } truelight@9615: truelight@9699: /* static */ char *AIVehicle::GetName(VehicleID vehicle_id) truelight@9699: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return NULL; truelight@9699: truelight@9699: static const int len = 64; truelight@9699: char *vehicle_name = MallocT(len); truelight@9699: rubidium@9724: ::SetDParam(0, vehicle_id); rubidium@9724: ::GetString(vehicle_name, STR_VEHICLE_NAME, &vehicle_name[len - 1]); truelight@9699: return vehicle_name; truelight@9699: } truelight@9699: truelight@9637: /* static */ int32 AIVehicle::GetAge(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truelight@9615: truelight@9615: return ::GetVehicle(vehicle_id)->age; truelight@9615: } truelight@9615: truelight@9637: /* static */ int32 AIVehicle::GetMaxAge(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truelight@9615: truelight@9615: return ::GetVehicle(vehicle_id)->max_age; truelight@9615: } truelight@9615: truebrain@9733: /* static */ Money AIVehicle::GetRunningCost(VehicleID vehicle_id) truebrain@9733: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truebrain@9733: truebrain@9733: return ::GetVehicle(vehicle_id)->GetRunningCost() >> 8; truebrain@9733: } truebrain@9733: truelight@9637: /* static */ int32 AIVehicle::GetAgeLeft(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truelight@9615: truelight@9615: return ::GetVehicle(vehicle_id)->max_age - ::GetVehicle(vehicle_id)->age; truelight@9615: } truelight@9615: truelight@9637: /* static */ int32 AIVehicle::GetProfitThisYear(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truelight@9615: glx@9732: return ::GetVehicle(vehicle_id)->GetDisplayProfitThisYear(); truelight@9615: } truelight@9615: truelight@9637: /* static */ int32 AIVehicle::GetProfitLastYear(VehicleID vehicle_id) truelight@9615: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return -1; truelight@9615: glx@9732: return ::GetVehicle(vehicle_id)->GetDisplayProfitLastYear(); truelight@9615: } truelight@9684: truelight@9684: /* static */ AIVehicle::VehicleType AIVehicle::GetVehicleType(VehicleID vehicle_id) truelight@9684: { truebrain@9736: if (!IsValidVehicle(vehicle_id)) return VEHICLE_INVALID; truelight@9684: truebrain@9736: switch (::GetVehicle(vehicle_id)->type) { truelight@9684: case VEH_ROAD: return VEHICLE_ROAD; truelight@9684: case VEH_TRAIN: return VEHICLE_RAIL; truelight@9684: case VEH_SHIP: return VEHICLE_WATER; truelight@9684: case VEH_AIRCRAFT: return VEHICLE_AIR; truelight@9684: default: return VEHICLE_INVALID; truelight@9684: } truelight@9684: } truebrain@9737: