MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / reverseVehicleOrderTable

Function reverseVehicleOrderTable

src/OpenLoco/src/Vehicles/OrderManager.cpp:428–472  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

426 }
427
428 uint16_t reverseVehicleOrderTable(uint16_t tableOffset, uint16_t orderOfInterest)
429 {
430 // Currently the use of std:: algorithms is not feasible due to variable order lengths
431 // TODO: simplify this after the changing the data structure for the order table
432
433 // Retrieve list of raw orders
434 std::vector<uint64_t> rawOrders{};
435 Vehicles::OrderRingView orderTable(tableOffset);
436 for (auto& order : orderTable)
437 {
438 rawOrders.push_back(order.getRaw());
439 }
440
441 // Nothing to do? Bail early
442 if (rawOrders.size() == 0)
443 {
444 return orderOfInterest;
445 }
446
447 // Keep track of the type of the order of interest
448 auto ooiType = orders()[tableOffset + orderOfInterest].getType();
449
450 // Figure out where the order table starts in memory
451 auto firstOrder = reinterpret_cast<uint8_t*>(orderTable.atIndex(0));
452 auto dest = firstOrder;
453
454 // Write reversed list over existing list
455 for (auto it = rawOrders.rbegin(); it != rawOrders.rend(); ++it)
456 {
457 auto rawOrder = *it;
458 auto orderType = rawOrder & 0x7;
459 if (orderType >= std::size(kOrderSizes))
460 {
461 throw Exception::OutOfRange("Order type is greater than the order size table");
462 }
463 auto orderLength = kOrderSizes[orderType];
464 std::memcpy(dest, &rawOrder, orderLength);
465 dest += orderLength;
466 }
467
468 // Figure out the new position of the order of interest
469 auto newOOIOffset = dest - orderOfInterest - kOrderSizes[enumValue(ooiType)];
470
471 return newOOIOffset - firstOrder;
472 }
473
474 uint8_t swapAdjacentOrders(Order& a, Order& b)
475 {

Callers 1

vehicleOrderReverseFunction · 0.85

Calls 9

ordersFunction · 0.85
enumValueFunction · 0.85
push_backMethod · 0.80
atIndexMethod · 0.80
rbeginMethod · 0.80
rendMethod · 0.80
getRawMethod · 0.45
sizeMethod · 0.45
getTypeMethod · 0.45

Tested by

no test coverage detected