* Look for an unreserved EngineID matching the local id, and reserve it if found. * @param type Vehicle type * @param grf_local_id The local id in the newgrf * @param grfid The GrfID that defines the scope of grf_local_id. * If a newgrf overrides the engines of another newgrf, the "scope grfid" is the ID of the overridden newgrf. * If dynamic_engines is disabled, all
| 546 | * @return The engine ID if present and now reserved, or EngineID::Invalid() if not. |
| 547 | */ |
| 548 | EngineID EngineOverrideManager::UseUnreservedID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, bool static_access) |
| 549 | { |
| 550 | auto &map = _engine_mngr.mappings[type]; |
| 551 | const auto key = EngineIDMapping::Key(INVALID_GRFID, grf_local_id); |
| 552 | auto it = std::ranges::lower_bound(map, key, std::less{}, EngineIDMappingKeyProjection{}); |
| 553 | if (it == std::end(map) || it->Key() != key) return EngineID::Invalid(); |
| 554 | |
| 555 | if (!static_access && grfid != INVALID_GRFID) { |
| 556 | /* Reserve the engine slot for the new grfid. */ |
| 557 | it->grfid = grfid; |
| 558 | |
| 559 | /* Relocate entry to its new position in the mapping list to keep it sorted. */ |
| 560 | auto p = std::ranges::lower_bound(map, EngineIDMapping::Key(grfid, grf_local_id), std::less{}, EngineIDMappingKeyProjection{}); |
| 561 | it = Slide(it, std::next(it), p).first; |
| 562 | } |
| 563 | |
| 564 | return it->engine; |
| 565 | } |
| 566 | |
| 567 | void EngineOverrideManager::SetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, uint8_t substitute_id, EngineID engine) |
| 568 | { |
no test coverage detected