From 2c16800454ddc76e32a28411f1c2013798043f7e Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 28 Jan 2018 17:26:35 +0000 Subject: [PATCH] Add debug console command to show cargo packet deferred payment stats --- src/cargopacket.cpp | 25 +++++++++++++++++++++++++ src/console_cmds.cpp | 15 +++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index 3b23fd4f58..6e42993ddb 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -18,6 +18,8 @@ #include "order_type.h" #include "company_func.h" #include "core/backup_type.hpp" +#include "string_func.h" +#include "strings_func.h" #include "3rdparty/cpp-btree/btree_map.h" #include "safeguards.h" @@ -71,6 +73,29 @@ inline void IterateCargoPacketDeferredPayments(CargoPacketID index, bool erase_r } } +void DumpCargoPacketDeferredPaymentStats(char *buffer, const char *last) +{ + Money payments[256][4] = {}; + for (auto &it : _cargo_packet_deferred_payments) { + payments[GB(it.first, 24, 8)][GB(it.first, 22, 2)] += it.second; + } + for (uint i = 0; i < 256; i++) { + for (uint j = 0; j < 4; j++) { + if (payments[i][j] != 0) { + SetDParam(0, i); + buffer = GetString(buffer, STR_COMPANY_NAME, last); + buffer += seprintf(buffer, last, " ("); + buffer = GetString(buffer, STR_REPLACE_VEHICLE_TRAIN + j, last); + buffer += seprintf(buffer, last, "): "); + SetDParam(0, payments[i][j]); + buffer = GetString(buffer, STR_JUST_CURRENCY_LONG, last); + buffer += seprintf(buffer, last, "\n"); + } + } + } + buffer += seprintf(buffer, last, "Deferred payment count: %u\n", (uint) _cargo_packet_deferred_payments.size()); +} + /** * Create a new packet for savegame loading. */ diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 3b65224de8..772cafd056 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1882,6 +1882,20 @@ DEF_CONSOLE_CMD(ConNewGRFReload) return true; } +DEF_CONSOLE_CMD(ConDumpCpdpStats) +{ + if (argc == 0) { + IConsoleHelp("Dump cargo packet deferred payment stats."); + return true; + } + + extern void DumpCargoPacketDeferredPaymentStats(char *buffer, const char *last); + char buffer[32768]; + DumpCargoPacketDeferredPaymentStats(buffer, lastof(buffer)); + PrintLineByLine(buffer); + return true; +} + #ifdef _DEBUG /****************** * debug commands @@ -2025,6 +2039,7 @@ void IConsoleStdLibRegister() #ifdef _DEBUG IConsoleDebugLibRegister(); #endif + IConsoleCmdRegister("dump_cpdp_stats", ConDumpCpdpStats); /* NewGRF development stuff */ IConsoleCmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool);