* Modify an order in the orderlist of a vehicle. * @param flags operation to perform * @param veh ID of the vehicle * @param sel_ord the selected order (if any). If the last order is given, * the order will be inserted before that one * the maximum vehicle order id is 254. * @param mof what data to modify (@see ModifyOrderFlags) * @param data the data to modify
| 1170 | * @return the cost of this operation or an error |
| 1171 | */ |
| 1172 | CommandCost CmdModifyOrder(DoCommandFlags flags, VehicleID veh, VehicleOrderID sel_ord, ModifyOrderFlags mof, uint16_t data) |
| 1173 | { |
| 1174 | if (mof >= MOF_END) return CMD_ERROR; |
| 1175 | |
| 1176 | Vehicle *v = Vehicle::GetIfValid(veh); |
| 1177 | if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR; |
| 1178 | |
| 1179 | CommandCost ret = CheckOwnership(v->owner); |
| 1180 | if (ret.Failed()) return ret; |
| 1181 | |
| 1182 | /* Is it a valid order? */ |
| 1183 | if (sel_ord >= v->GetNumOrders()) return CMD_ERROR; |
| 1184 | |
| 1185 | Order *order = v->GetOrder(sel_ord); |
| 1186 | assert(order != nullptr); |
| 1187 | switch (order->GetType()) { |
| 1188 | case OT_GOTO_STATION: |
| 1189 | if (mof != MOF_NON_STOP && mof != MOF_STOP_LOCATION && mof != MOF_UNLOAD && mof != MOF_LOAD) return CMD_ERROR; |
| 1190 | break; |
| 1191 | |
| 1192 | case OT_GOTO_DEPOT: |
| 1193 | if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR; |
| 1194 | break; |
| 1195 | |
| 1196 | case OT_GOTO_WAYPOINT: |
| 1197 | if (mof != MOF_NON_STOP) return CMD_ERROR; |
| 1198 | break; |
| 1199 | |
| 1200 | case OT_CONDITIONAL: |
| 1201 | if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR; |
| 1202 | break; |
| 1203 | |
| 1204 | default: |
| 1205 | return CMD_ERROR; |
| 1206 | } |
| 1207 | |
| 1208 | switch (mof) { |
| 1209 | default: NOT_REACHED(); |
| 1210 | |
| 1211 | case MOF_NON_STOP: { |
| 1212 | if (!v->IsGroundVehicle()) return CMD_ERROR; |
| 1213 | |
| 1214 | OrderNonStopFlags nonstop_flags = static_cast<OrderNonStopFlags>(data); |
| 1215 | if (nonstop_flags == order->GetNonStopType()) return CMD_ERROR; |
| 1216 | |
| 1217 | /* Test for invalid flags. */ |
| 1218 | nonstop_flags.Reset({OrderNonStopFlag::NoIntermediate, OrderNonStopFlag::NoDestination}); |
| 1219 | if (nonstop_flags.Any()) return CMD_ERROR; |
| 1220 | break; |
| 1221 | } |
| 1222 | |
| 1223 | case MOF_STOP_LOCATION: |
| 1224 | if (v->type != VEH_TRAIN) return CMD_ERROR; |
| 1225 | if (data >= to_underlying(OrderStopLocation::End)) return CMD_ERROR; |
| 1226 | break; |
| 1227 | |
| 1228 | case MOF_UNLOAD: { |
| 1229 | if (order->GetNonStopType().Test(OrderNonStopFlag::NoDestination)) return CMD_ERROR; |
nothing calls this directly
no test coverage detected