7 #include "ai_cargo.hpp" |
7 #include "ai_cargo.hpp" |
8 #include "ai_order.hpp" |
8 #include "ai_order.hpp" |
9 #include "../../depot.h" |
9 #include "../../depot.h" |
10 #include "../../player_func.h" |
10 #include "../../player_func.h" |
11 #include "../../aircraft.h" |
11 #include "../../aircraft.h" |
|
12 #include "../../string_func.h" |
12 #include "../../strings_func.h" |
13 #include "../../strings_func.h" |
13 #include "../../core/alloc_func.hpp" |
14 #include "../../core/alloc_func.hpp" |
14 #include "../../command_type.h" |
15 #include "../../command_type.h" |
15 #include "../../command_func.h" |
16 #include "../../command_func.h" |
16 #include "table/strings.h" |
17 #include "table/strings.h" |
20 return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player && ::GetVehicle(vehicle_id)->IsPrimaryVehicle(); |
21 return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player && ::GetVehicle(vehicle_id)->IsPrimaryVehicle(); |
21 } |
22 } |
22 |
23 |
23 /* static */ VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) |
24 /* static */ VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) |
24 { |
25 { |
25 if (!AIEngine::IsValidEngine(engine_id)) return false; |
26 EnforcePrecondition(false, AIEngine::IsValidEngine(engine_id)); |
26 |
27 |
27 /* Reset the internal NewVehicleID in case we are in TestMode */ |
28 /* Reset the internal NewVehicleID in case we are in TestMode */ |
28 AIObject::SetNewVehicleID(0); |
29 AIObject::SetNewVehicleID(0); |
29 |
30 |
30 bool ret; |
31 bool ret; |
39 return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE; |
40 return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE; |
40 } |
41 } |
41 |
42 |
42 /* static */ VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders) |
43 /* static */ VehicleID AIVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders) |
43 { |
44 { |
44 if (!IsValidVehicle(vehicle_id)) return false; |
45 EnforcePrecondition(false, IsValidVehicle(vehicle_id)); |
45 |
46 |
46 /* Reset the internal NewVehicleID in case we are in TestMode */ |
47 /* Reset the internal NewVehicleID in case we are in TestMode */ |
47 AIObject::SetNewVehicleID(0); |
48 AIObject::SetNewVehicleID(0); |
48 |
49 |
49 bool ret = AIObject::DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE); |
50 bool ret = AIObject::DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE); |
50 return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE; |
51 return ret ? AIObject::GetNewVehicleID() : INVALID_VEHICLE; |
51 } |
52 } |
52 |
53 |
53 /* static */ bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo) |
54 /* static */ bool AIVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo) |
54 { |
55 { |
55 if (!IsValidVehicle(vehicle_id) || !AICargo::IsValidCargo(cargo)) return false; |
56 EnforcePrecondition(false, IsValidVehicle(vehicle_id) && AICargo::IsValidCargo(cargo)); |
56 |
57 |
57 switch (::GetVehicle(vehicle_id)->type) { |
58 switch (::GetVehicle(vehicle_id)->type) { |
58 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH); |
59 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_ROAD_VEH); |
59 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_RAIL_VEHICLE); |
60 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_RAIL_VEHICLE); |
60 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_SHIP); |
61 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, cargo, CMD_REFIT_SHIP); |
64 } |
65 } |
65 |
66 |
66 |
67 |
67 /* static */ bool AIVehicle::SellVehicle(VehicleID vehicle_id) |
68 /* static */ bool AIVehicle::SellVehicle(VehicleID vehicle_id) |
68 { |
69 { |
69 if (!IsValidVehicle(vehicle_id)) return false; |
70 EnforcePrecondition(false, IsValidVehicle(vehicle_id)); |
70 |
71 |
71 switch (::GetVehicle(vehicle_id)->type) { |
72 switch (::GetVehicle(vehicle_id)->type) { |
72 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH); |
73 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_ROAD_VEH); |
73 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_RAIL_WAGON); |
74 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_RAIL_WAGON); |
74 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_SHIP); |
75 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SELL_SHIP); |
77 } |
78 } |
78 } |
79 } |
79 |
80 |
80 /* static */ bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id) |
81 /* static */ bool AIVehicle::SendVehicleToDepot(VehicleID vehicle_id) |
81 { |
82 { |
82 if (!IsValidVehicle(vehicle_id)) return false; |
83 EnforcePrecondition(false, IsValidVehicle(vehicle_id)); |
83 |
84 |
84 switch (::GetVehicle(vehicle_id)->type) { |
85 switch (::GetVehicle(vehicle_id)->type) { |
85 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT); |
86 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_ROADVEH_TO_DEPOT); |
86 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_TRAIN_TO_DEPOT); |
87 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_TRAIN_TO_DEPOT); |
87 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_SHIP_TO_DEPOT); |
88 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_SEND_SHIP_TO_DEPOT); |
102 return ::GetVehicle(vehicle_id)->IsStoppedInDepot(); |
103 return ::GetVehicle(vehicle_id)->IsStoppedInDepot(); |
103 } |
104 } |
104 |
105 |
105 /* static */ bool AIVehicle::StartStopVehicle(VehicleID vehicle_id) |
106 /* static */ bool AIVehicle::StartStopVehicle(VehicleID vehicle_id) |
106 { |
107 { |
107 if (!IsValidVehicle(vehicle_id)) return false; |
108 EnforcePrecondition(false, IsValidVehicle(vehicle_id)); |
108 |
109 |
109 switch (::GetVehicle(vehicle_id)->type) { |
110 switch (::GetVehicle(vehicle_id)->type) { |
110 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH); |
111 case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_ROADVEH); |
111 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_TRAIN); |
112 case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_TRAIN); |
112 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_SHIP); |
113 case VEH_SHIP: return AIObject::DoCommand(0, vehicle_id, 0, CMD_START_STOP_SHIP); |
115 } |
116 } |
116 } |
117 } |
117 |
118 |
118 /* static */ bool AIVehicle::SkipToVehicleOrder(VehicleID vehicle_id, uint32 order_id) |
119 /* static */ bool AIVehicle::SkipToVehicleOrder(VehicleID vehicle_id, uint32 order_id) |
119 { |
120 { |
120 if (!AIOrder::IsValidVehicleOrder(vehicle_id, order_id)) return false; |
121 EnforcePrecondition(false, AIOrder::IsValidVehicleOrder(vehicle_id, order_id)); |
121 |
122 |
122 return AIObject::DoCommand(0, vehicle_id, order_id, CMD_SKIP_TO_ORDER); |
123 return AIObject::DoCommand(0, vehicle_id, order_id, CMD_SKIP_TO_ORDER); |
123 } |
124 } |
124 |
125 |
125 /* static */ bool AIVehicle::SetName(VehicleID vehicle_id, const char *name) |
126 /* static */ bool AIVehicle::SetName(VehicleID vehicle_id, const char *name) |
126 { |
127 { |
127 if (!IsValidVehicle(vehicle_id)) return false; |
128 EnforcePrecondition(false, IsValidVehicle(vehicle_id)); |
128 if (name == NULL) return false; |
129 EnforcePrecondition(false, !StrEmpty(name)); |
129 |
130 |
130 _cmd_text = name; |
131 _cmd_text = name; |
131 return AIObject::DoCommand(0, vehicle_id, 0, CMD_NAME_VEHICLE); |
132 return AIObject::DoCommand(0, vehicle_id, 0, CMD_NAME_VEHICLE); |
132 } |
133 } |
133 |
134 |