src/ai/api/ai_vehicle.cpp
branchnoai
changeset 9737 ee408edf3851
parent 9736 183b38e0a480
child 9806 d65cc2bcb0de
equal deleted inserted replaced
9736:183b38e0a480 9737:ee408edf3851
    11 #include "../../engine.h"
    11 #include "../../engine.h"
    12 #include "../../player_func.h"
    12 #include "../../player_func.h"
    13 #include "../../aircraft.h"
    13 #include "../../aircraft.h"
    14 #include "../../strings_func.h"
    14 #include "../../strings_func.h"
    15 #include "../../core/alloc_func.hpp"
    15 #include "../../core/alloc_func.hpp"
       
    16 #include "../../command_type.h"
       
    17 #include "../../command_func.h"
    16 #include "table/strings.h"
    18 #include "table/strings.h"
    17 
    19 
    18 /* static */ bool AIVehicle::IsValidVehicle(VehicleID vehicle_id)
    20 /* static */ bool AIVehicle::IsValidVehicle(VehicleID vehicle_id)
    19 {
    21 {
    20 	return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player && ::GetVehicle(vehicle_id)->IsPrimaryVehicle();
    22 	return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player && ::GetVehicle(vehicle_id)->IsPrimaryVehicle();
    21 }
    23 }
    22 
    24 
    23 VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
    25 /* static */ VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
    24 {
    26 {
    25 	if (!AIEngine::IsValidEngine(engine_id)) return false;
    27 	if (!AIEngine::IsValidEngine(engine_id)) return false;
    26 
    28 
    27 	/* Reset the internal NewVehicleID in case we are in TestMode */
    29 	/* Reset the internal NewVehicleID in case we are in TestMode */
    28 	AIObject::SetNewVehicleID(0);
    30 	AIObject::SetNewVehicleID(0);
    29 
    31 
    30 	bool ret;
    32 	bool ret;
    31 	switch (::GetEngine(engine_id)->type) {
    33 	switch (::GetEngine(engine_id)->type) {
    32 		case VEH_ROAD: ret = this->DoCommand(depot, engine_id, 0, CMD_BUILD_ROAD_VEH); break;
    34 		case VEH_ROAD: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_ROAD_VEH); break;
    33 		case VEH_TRAIN: ret = this->DoCommand(depot, engine_id, 0, CMD_BUILD_RAIL_VEHICLE); break;
    35 		case VEH_TRAIN: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_RAIL_VEHICLE); break;
    34 		case VEH_SHIP: ret = this->DoCommand(depot, engine_id, 0, CMD_BUILD_SHIP); break;
    36 		case VEH_SHIP: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_SHIP); break;
    35 		case VEH_AIRCRAFT: ret = this->DoCommand(depot, engine_id, 0, CMD_BUILD_AIRCRAFT); break;
    37 		case VEH_AIRCRAFT: ret = AIObject::DoCommand(depot, engine_id, 0, CMD_BUILD_AIRCRAFT); break;
    36 		default: NOT_REACHED(); return INVALID_VEHICLE;
    38 		default: NOT_REACHED(); return INVALID_VEHICLE;
    37 	}
    39 	}
    38 
    40 
    39 	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
    41 	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
    40 }
    42 }
    41 
    43 
    42 VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders)
    44 /* static */ VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders)
    43 {
    45 {
    44 	if (!IsValidVehicle(vehicle_id)) return false;
    46 	if (!IsValidVehicle(vehicle_id)) return false;
    45 
    47 
    46 	/* Reset the internal NewVehicleID in case we are in TestMode */
    48 	/* Reset the internal NewVehicleID in case we are in TestMode */
    47 	AIObject::SetNewVehicleID(0);
    49 	AIObject::SetNewVehicleID(0);
    48 
    50 
    49 	bool ret = this->DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE);
    51 	bool ret = AIObject::DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE);
    50 	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
    52 	return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE;
    51 }
    53 }
    52 
    54 
    53 bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo)
    55 /* static */ bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo)
    54 {
    56 {
    55 	if (!IsValidVehicle(vehicle_id) || !AICargo::IsValidCargo(cargo)) return false;
    57 	if (!IsValidVehicle(vehicle_id) || !AICargo::IsValidCargo(cargo)) return false;
    56 
    58 
    57 	switch (::GetVehicle(vehicle_id)->type) {
    59 	switch (::GetVehicle(vehicle_id)->type) {
    58 		case VEH_ROAD: return this->DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH);
    60 		case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH);
    59 		case VEH_TRAIN: return this->DoCommand(0, vehicle_id, cargo, CMD_REFIT_RAIL_VEHICLE);
    61 		case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_RAIL_VEHICLE);
    60 		case VEH_SHIP: return this->DoCommand(0, vehicle_id, cargo, CMD_REFIT_SHIP);
    62 		case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_SHIP);
    61 		case VEH_AIRCRAFT: return this->DoCommand(0, vehicle_id, cargo, CMD_REFIT_AIRCRAFT);
    63 		case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_AIRCRAFT);
    62 		default: return false;
    64 		default: return false;
    63 	}
    65 	}
    64 }
    66 }
    65 
    67 
    66 
    68 
    67 bool AIVehicle::SellVehicle(VehicleID vehicle_id)
    69 /* static */ bool AIVehicle::SellVehicle(VehicleID vehicle_id)
    68 {
    70 {
    69 	if (!IsValidVehicle(vehicle_id)) return false;
    71 	if (!IsValidVehicle(vehicle_id)) return false;
    70 
    72 
    71 	switch (::GetVehicle(vehicle_id)->type) {
    73 	switch (::GetVehicle(vehicle_id)->type) {
    72 		case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH);
    74 		case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH);
    73 		case VEH_TRAIN: return this->DoCommand(0, vehicle_id, 0, CMD_SELL_RAIL_WAGON);
    75 		case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_RAIL_WAGON);
    74 		case VEH_SHIP: return this->DoCommand(0, vehicle_id, 0, CMD_SELL_SHIP);
    76 		case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_SHIP);
    75 		case VEH_AIRCRAFT: return this->DoCommand(0, vehicle_id, 0, CMD_SELL_AIRCRAFT);
    77 		case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_AIRCRAFT);
    76 		default: return false;
    78 		default: return false;
    77 	}
    79 	}
    78 }
    80 }
    79 
    81 
    80 bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id)
    82 /* static */ bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id)
    81 {
    83 {
    82 	if (!IsValidVehicle(vehicle_id)) return false;
    84 	if (!IsValidVehicle(vehicle_id)) return false;
    83 
    85 
    84 	switch (::GetVehicle(vehicle_id)->type) {
    86 	switch (::GetVehicle(vehicle_id)->type) {
    85 		case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT);
    87 		case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT);
    86 		case VEH_TRAIN: return this->DoCommand(0, vehicle_id, 0, CMD_SEND_TRAIN_TO_DEPOT);
    88 		case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_TRAIN_TO_DEPOT);
    87 		case VEH_SHIP: return this->DoCommand(0, vehicle_id, 0, CMD_SEND_SHIP_TO_DEPOT);
    89 		case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_SHIP_TO_DEPOT);
    88 		case VEH_AIRCRAFT: return this->DoCommand(0, vehicle_id, 0, CMD_SEND_AIRCRAFT_TO_HANGAR);
    90 		case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_AIRCRAFT_TO_HANGAR);
    89 		default: return false;
    91 		default: return false;
    90 	}
    92 	}
    91 }
    93 }
    92 
    94 
    93 bool AIVehicle::IsInDepot(VehicleID vehicle_id)
    95 /* static */ bool AIVehicle::IsInDepot(VehicleID vehicle_id)
    94 {
    96 {
    95 	if (!IsValidVehicle(vehicle_id)) return false;
    97 	if (!IsValidVehicle(vehicle_id)) return false;
    96 	return ::GetVehicle(vehicle_id)->IsInDepot();
    98 	return ::GetVehicle(vehicle_id)->IsInDepot();
    97 }
    99 }
    98 
   100 
    99 bool AIVehicle::IsStoppedInDepot(VehicleID vehicle_id)
   101 /* static */ bool AIVehicle::IsStoppedInDepot(VehicleID vehicle_id)
   100 {
   102 {
   101 	if (!IsValidVehicle(vehicle_id)) return false;
   103 	if (!IsValidVehicle(vehicle_id)) return false;
   102 	return ::GetVehicle(vehicle_id)->IsStoppedInDepot();
   104 	return ::GetVehicle(vehicle_id)->IsStoppedInDepot();
   103 }
   105 }
   104 
   106 
   105 bool AIVehicle::StartStopVehicle(VehicleID vehicle_id)
   107 /* static */ bool AIVehicle::StartStopVehicle(VehicleID vehicle_id)
   106 {
   108 {
   107 	if (!IsValidVehicle(vehicle_id)) return false;
   109 	if (!IsValidVehicle(vehicle_id)) return false;
   108 
   110 
   109 	switch (::GetVehicle(vehicle_id)->type) {
   111 	switch (::GetVehicle(vehicle_id)->type) {
   110 		case VEH_ROAD: return this->DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH);
   112 		case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH);
   111 		case VEH_TRAIN: return this->DoCommand(0, vehicle_id, 0, CMD_START_STOP_TRAIN);
   113 		case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_TRAIN);
   112 		case VEH_SHIP: return this->DoCommand(0, vehicle_id, 0, CMD_START_STOP_SHIP);
   114 		case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_SHIP);
   113 		case VEH_AIRCRAFT: return this->DoCommand(0, vehicle_id, 0, CMD_START_STOP_AIRCRAFT);
   115 		case VEH_AIRCRAFT: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_AIRCRAFT);
   114 		default: return false;
   116 		default: return false;
   115 	}
   117 	}
   116 }
   118 }
   117 
   119 
   118 bool AIVehicle::SkipToVehicleOrder(VehicleID vehicle_id, uint32 order_id)
   120 /* static */ bool AIVehicle::SkipToVehicleOrder(VehicleID vehicle_id, uint32 order_id)
   119 {
   121 {
   120 	if (!AIOrder::IsValidVehicleOrder(vehicle_id, order_id)) return false;
   122 	if (!AIOrder::IsValidVehicleOrder(vehicle_id, order_id)) return false;
   121 
   123 
   122 	return this->DoCommand(0, vehicle_id, order_id, CMD_SKIP_TO_ORDER);
   124 	return AIObject::DoCommand(0, vehicle_id, order_id, CMD_SKIP_TO_ORDER);
   123 }
   125 }
   124 
   126 
   125 bool AIVehicle::SetName(VehicleID vehicle_id, const char *name)
   127 /* static */ bool AIVehicle::SetName(VehicleID vehicle_id, const char *name)
   126 {
   128 {
   127 	if (!IsValidVehicle(vehicle_id)) return false;
   129 	if (!IsValidVehicle(vehicle_id)) return false;
   128 	if (name == NULL) return false;
   130 	if (name == NULL) return false;
   129 
   131 
   130 	_cmd_text = name;
   132 	_cmd_text = name;
   131 	return this->DoCommand(0, vehicle_id, 0, CMD_NAME_VEHICLE);
   133 	return AIObject::DoCommand(0, vehicle_id, 0, CMD_NAME_VEHICLE);
   132 }
   134 }
   133 
   135 
   134 /* static */ TileIndex AIVehicle::GetLocation(VehicleID vehicle_id)
   136 /* static */ TileIndex AIVehicle::GetLocation(VehicleID vehicle_id)
   135 {
   137 {
   136 	if (!IsValidVehicle(vehicle_id)) return INVALID_TILE;
   138 	if (!IsValidVehicle(vehicle_id)) return INVALID_TILE;
   216 		case VEH_SHIP:     return VEHICLE_WATER;
   218 		case VEH_SHIP:     return VEHICLE_WATER;
   217 		case VEH_AIRCRAFT: return VEHICLE_AIR;
   219 		case VEH_AIRCRAFT: return VEHICLE_AIR;
   218 		default:           return VEHICLE_INVALID;
   220 		default:           return VEHICLE_INVALID;
   219 	}
   221 	}
   220 }
   222 }
       
   223