Search profile engine: closing rule, "Vorschlag" and matching shared with the market news

New game/search_profile.* holds the 18-byte search profile of a human manager (manager record
+0x1f6): the dialog's closing rule, the suggestion generated from the squad (original sub_18F06),
the position strengths (sub_19A38) and the listing match (sub_190B5). search_profile_news in
month.cpp now uses the same matching rule. Compared with the Diamond 3.7 routines in the
emulator harness on 10 savegames (306 runs, 406 matched listings, no difference; a deliberately
changed profile is detected).

Part of #79 (the dialog in the graphical client and the console commands follow)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Sonnet 5.5 committed 2026-11-07 21:26:11 +01:00
1 parent e9ba514a65
commit ff4ad035a7
6 files changed
+346 -39

No files matched your search

+4
View File
@@ -240,6 +240,7 @@ add_library(bmhcore STATIC
src/game/matchday_setup.cpp
src/game/prediction.cpp
src/game/month.cpp
src/game/search_profile.cpp
src/game/desk.cpp
src/game/cup.cpp
src/game/club_info.cpp
@@ -1192,6 +1193,9 @@ add_test(NAME daily_interest_unit_tests COMMAND daily_interest_unit_tests)
add_executable(transfer_seller_actions_tests tests/transfer_seller_actions_tests.cpp)
target_link_libraries(transfer_seller_actions_tests PRIVATE bmhcore)
add_test(NAME transfer_seller_actions_tests COMMAND transfer_seller_actions_tests)
add_executable(search_profile_tests tests/search_profile_tests.cpp)
target_link_libraries(search_profile_tests PRIVATE bmhcore)
add_test(NAME search_profile_tests COMMAND search_profile_tests)
add_executable(transfer_seller_app_tests tests/transfer_seller_app_tests.cpp ${BMH_APP_SESSION_SDL_SOURCE})
target_link_libraries(transfer_seller_app_tests PRIVATE bmhui openbmh_audio ${BMH_SDL_TARGET})
+1
View File
@@ -1415,6 +1415,7 @@ inline constexpr auto Month_ReturnLoans_TextPointer7 = 0xaee44;
inline constexpr auto Month_ReturnLoans_TextPointer8 = 0xaee48;
inline constexpr auto Month_SearchProfileNews_TextPointer = 0xaedd8;
inline constexpr auto Month_SearchProfileNews_TextPointer2 = 0xaf254;
inline constexpr auto Month_SearchProfileNews_TextPointer3 = 0xaf258; // ending of the message box after "Suchen"
inline constexpr auto NationalFriendlyPayerPrefixText = 0xa000f;
inline constexpr auto NextDeskManager = 0xbd6cc;
inline constexpr auto NoClubText = 0xaec74;
+3 -39
View File
@@ -24,6 +24,7 @@
#include "game/messages.hpp"
#include "game/prediction.hpp"
#include "game/schedule.hpp"
#include "game/search_profile.hpp"
#include "game/season.hpp"
#include "game/weekly.hpp"
#include "game/human_buyer_sale.hpp"
@@ -915,46 +916,9 @@ ContractInsuranceResult contract_insurance(GameState& g, int manager, int row, i
}
void search_profile_news(GameState& g, int manager, uint32_t list, int player) {
const StaticMemory& mem = g.memory;
const size_t rec = size_t(manager) * kRecord;
auto b = [&](size_t off) { return int(g.manager_records.at(rec + off)); };
const int team = g.manager_teams.at(size_t(manager));
// The matching rule is shared with the search dialog (search_profile.cpp, original sub_190B5).
player &= 0xFFF;
if (team == kNoTeam || (mem.u16(list + 8) & 0x7FFF) == team || (b(0x1f6) & 0x40)) return;
const uint32_t w364 = g.players.at(size_t(player)).w364, w35c = g.players.at(size_t(player)).w35c;
// position wanted (or none selected) and age within the range
if (b(0x1f6 + (w364 & 3)) == 0 && b(0x1f9) + b(0x1f8) + b(0x1f7) + b(0x1f6) != 0) return;
const int age = int((w364 >> 21) & 0x1F);
if (age < b(0x1fa) || age > b(0x1fb)) return;
// strength by the selected attributes (condition, technique, form; none = all three)
int8_t selected = int8_t(b(0x200) + b(0x1fe) + b(0x1ff));
if (selected == 0) selected = 3;
int sum = 0, n = 0, form = 0;
if (b(0x1fe) != 0 || selected == 3) sum = int((w35c >> 14) & 0x7F), n = 1;
if (b(0x1ff) != 0 || selected == 3) sum = int16_t(sum) + int((w35c >> 7) & 0x7F), n++;
if (b(0x200) != 0 || selected == 3) form = int(w35c & 0x7F);
int16_t strength;
if (n == 0) {
strength = int16_t((form - 0x23) * 3 + 10);
} else {
strength = int16_t(uint32_t(sum) / uint32_t(n));
if (form != 0) strength = int16_t(uint32_t(strength * int16_t((int16_t(form) >> 3) + 0x5e)) / 100);
}
if (strength < b(0x1fc) || strength > b(0x1fd)) return;
// left / right foot and wing preferences (w35c bits 21-23 and 24-26)
const int lane = int((w35c >> 21) & 7), foot = int((w35c >> 24) & 7);
bool low_foot = b(0x201) || b(0x203), high_foot = b(0x202) || b(0x204);
if (low_foot && high_foot) low_foot = high_foot = false;
bool low_lane = b(0x201) || b(0x202), high_lane = b(0x203) || b(0x204);
if (low_lane && high_lane) low_lane = high_lane = false;
if ((low_lane && lane >= 3) || (high_lane && lane <= 4) || (low_foot && foot >= 3) || (high_foot && foot <= 4)) return;
// the price class (asking price 70..130 %) against the classes wanted
int8_t price_class = int8_t((mem.u8(list + 0x54) - 0x46) / 0x14);
price_class = std::clamp<int8_t>(price_class, 0, 2);
const int8_t classes = int8_t(b(0x207) + b(0x205) + b(0x206));
int highest = 2;
while (highest >= 0 && b(0x205 + size_t(highest)) == 0) highest--;
if (classes == 0 || price_class <= highest)
if (search_profile_matches_listing(g, manager, list, player))
post_composed(g, manager, message_text(g, ::bmh::layout::Month_SearchProfileNews_TextPointer) + g.player_name(player) + message_text(g, ::bmh::layout::Month_SearchProfileNews_TextPointer2));
}
+188
View File
@@ -0,0 +1,188 @@
// SPDX-License-Identifier: GPL-3.0-only
// Search profile of a human manager: storage, closing rule, "Vorschlag" and matching against the transfer list
// (see search_profile.hpp). Original routines: sub_19409 (dialog, closing rule), sub_18F06 (suggestion),
// sub_19A38 (position strengths), sub_190B5 (match one listing) of the Diamond 3.7 executable.
#include "game/search_profile.hpp"
#include <algorithm>
#include <cstdlib>
#include "formats/executable_layout.hpp"
#include "game/messages.hpp"
namespace bmh {
namespace {
constexpr size_t kRecord = 0xB0C; // manager record
constexpr uint32_t kTransferList = ::bmh::layout::EditorList;
constexpr uint32_t kListEntry = 0x58;
constexpr int kPoolFirst = 0xb70; // the transfer list lives in the first 40 pool slots
// Fewest players per position (goalkeeper, defender, midfielder, forward) before the suggestion asks for one.
constexpr int kMinimumPerPosition[4] = {2, 6, 7, 5};
// Strength of a player as shown in the squad bars (original sub_199D0 with the halving flag set):
// (first attribute + second attribute) * (94 + form / 8) / 100 / 2, limited to 0..99.
int bar_strength(uint32_t w35c) {
const int first = int((w35c >> 14) & 0x7F), second = int((w35c >> 7) & 0x7F), form = int(w35c & 0x7F);
int16_t value = int16_t(int16_t(first + second) * ((form >> 3) + 94) / 100);
value = int16_t(value / 2);
if (value < 0) value = 0;
if (value > 99) value = 99;
return value;
}
uint8_t record_byte(const GameState& g, int manager, size_t offset) {
return g.manager_records.at(size_t(manager) * kRecord + offset);
}
} // namespace
SearchProfile search_profile(const GameState& g, int manager) {
SearchProfile profile{};
if (manager < 0 || size_t(manager) >= g.manager_teams.size() ||
(size_t(manager) + 1) * kRecord > g.manager_records.size()) return profile;
for (size_t i = 0; i < profile.size(); ++i) profile[i] = record_byte(g, manager, kSearchProfileOffset + i);
return profile;
}
bool set_search_profile(GameState& g, int manager, SearchProfile profile, bool cancelled) {
if (manager < 0 || size_t(manager) >= g.manager_teams.size() ||
(size_t(manager) + 1) * kRecord > g.manager_records.size()) return false;
profile[0] &= 0x3F; // the dialog starts from the stored profile without the "off" bit
for (size_t i = 0; i < profile.size(); ++i) {
const bool range = i >= 4 && i <= 7;
if (!range && profile[i] > 1) return false; // switches
if (i >= 4 && i <= 5 && profile[i] > 24) return false; // age bar: 25 cells
if (i >= 6 && i <= 7 && profile[i] > 99) return false; // strength bar: 100 cells
}
int sum = 0;
for (uint8_t value : profile) sum += value;
if (sum == 0 || sum == 18 || cancelled) profile[0] |= kSearchProfileOff;
for (size_t i = 0; i < profile.size(); ++i)
g.manager_records.at(size_t(manager) * kRecord + kSearchProfileOffset + i) = profile[i];
return (profile[0] & kSearchProfileOff) == 0;
}
std::array<uint16_t, 5> position_strengths(const GameState& g, int club, bool whole_squad) {
unsigned sum[5] = {}, count[5] = {};
const SquadRange range = squad_range(club);
for (size_t i = 0; i < 24; ++i) {
if (i >= 11 && !whole_squad) continue;
const size_t slot = range.first + i;
if (slot >= g.squad_slots.size()) break;
const int player = g.squad_slots[slot] & 0xFFF;
if (player == 0xFFF) continue;
const Player& p = g.players.at(size_t(player));
const int position = int(p.w364 & 3);
const int value = bar_strength(p.w35c);
++count[position]; ++count[4];
sum[position] += unsigned(value); sum[4] += unsigned(value);
}
std::array<uint16_t, 5> average{};
for (size_t k = 0; k < 5; ++k) average[k] = count[k] ? uint16_t(sum[k] / count[k]) : 0;
return average;
}
SearchProfile suggest_search_profile(const GameState& g, int manager, bool whole_squad) {
SearchProfile profile{};
if (manager < 0 || size_t(manager) >= g.manager_teams.size()) return profile;
const int club = g.manager_teams[size_t(manager)];
// Players per position in the first 24 squad slots (the count stops at the first empty slot).
int present[4] = {};
const SquadRange range = squad_range(club);
for (size_t i = 0; i < 24; ++i) {
const size_t slot = range.first + i;
if (slot >= g.squad_slots.size()) break;
const int player = g.squad_slots[slot] & 0xFFF;
if (player == 0xFFF) break;
++present[g.players.at(size_t(player)).w364 & 3];
}
const auto average = position_strengths(g, club, whole_squad);
int weakest = 0, weakest_value = 127;
for (int position = 0; position < 4; ++position) {
if (present[position] - kMinimumPerPosition[position] < 0) profile[size_t(position)] = 1;
if (int(average[size_t(position)]) < weakest_value) {
weakest_value = int(int8_t(average[size_t(position)]));
weakest = position;
}
}
profile[size_t(weakest)] = 1;
// Whole squad, all four positions: team averages of the first and of the second attribute.
const int first = team_average(g, club, 0xF1).value;
const int second = team_average(g, club, 0xF2).value;
int ratio = 100;
if (uint16_t(second) != 0) ratio = 100 * uint16_t(first) / uint16_t(second);
if (std::abs(100 - ratio) > 9) profile[size_t(8 + (ratio > 100 ? 1 : 0))] = 1;
int16_t upper = int16_t(second + 20);
upper = std::clamp<int16_t>(upper, 0, 99);
profile[4] = 2;
profile[5] = 12;
profile[6] = uint8_t(second);
profile[7] = uint8_t(upper);
return profile;
}
bool search_profile_matches_listing(const GameState& g, int manager, uint32_t list, int player) {
if (manager < 0 || size_t(manager) >= g.manager_teams.size() ||
(size_t(manager) + 1) * kRecord > g.manager_records.size()) return false;
const StaticMemory& mem = g.memory;
const auto b = [&](size_t offset) { return int(record_byte(g, manager, offset)); };
const int team = g.manager_teams[size_t(manager)];
player &= 0xFFF;
if (team == kNoTeam || (mem.u16(list + 8) & 0x7FFF) == team || (b(kSearchProfileOffset) & kSearchProfileOff)) return false;
const uint32_t w364 = g.players.at(size_t(player)).w364, w35c = g.players.at(size_t(player)).w35c;
// position wanted (or none selected) and age within the range
if (b(0x1f6 + (w364 & 3)) == 0 && b(0x1f9) + b(0x1f8) + b(0x1f7) + b(0x1f6) != 0) return false;
const int age = int((w364 >> 21) & 0x1F);
if (age < b(0x1fa) || age > b(0x1fb)) return false;
// strength by the selected attributes (first, second, form; none = all three)
int8_t selected = int8_t(b(0x200) + b(0x1fe) + b(0x1ff));
if (selected == 0) selected = 3;
int sum = 0, n = 0, form = 0;
if (b(0x1fe) != 0 || selected == 3) sum = int((w35c >> 14) & 0x7F), n = 1;
if (b(0x1ff) != 0 || selected == 3) sum = int16_t(sum) + int((w35c >> 7) & 0x7F), n++;
if (b(0x200) != 0 || selected == 3) form = int(w35c & 0x7F);
int16_t strength;
if (n == 0) {
strength = int16_t((form - 0x23) * 3 + 10);
} else {
strength = int16_t(uint32_t(sum) / uint32_t(n));
if (form != 0) strength = int16_t(uint32_t(strength * int16_t((int16_t(form) >> 3) + 0x5e)) / 100);
}
if (strength < b(0x1fc) || strength > b(0x1fd)) return false;
// left / right foot and wing preferences (w35c bits 21-23 and 24-26)
const int lane = int((w35c >> 21) & 7), foot = int((w35c >> 24) & 7);
bool low_foot = b(0x201) || b(0x203), high_foot = b(0x202) || b(0x204);
if (low_foot && high_foot) low_foot = high_foot = false;
bool low_lane = b(0x201) || b(0x202), high_lane = b(0x203) || b(0x204);
if (low_lane && high_lane) low_lane = high_lane = false;
if ((low_lane && lane >= 3) || (high_lane && lane <= 4) || (low_foot && foot >= 3) || (high_foot && foot <= 4)) return false;
// the price class (asking price 70..130 %) against the classes wanted
int8_t price_class = int8_t((mem.u8(list + 0x54) - 0x46) / 0x14);
price_class = std::clamp<int8_t>(price_class, 0, 2);
const int8_t classes = int8_t(b(0x207) + b(0x205) + b(0x206));
int highest = 2;
while (highest >= 0 && b(0x205 + size_t(highest)) == 0) highest--;
return classes == 0 || price_class <= highest;
}
bool search_profile_matches(const GameState& g, int manager, int entry) {
if (entry < 0 || entry >= 40 || size_t(kPoolFirst + entry) >= g.squad_slots.size()) return false;
const int player = g.squad_slots[size_t(kPoolFirst + entry)] & 0xFFF;
if (player == 0xFFF) return false;
return search_profile_matches_listing(g, manager, kTransferList + uint32_t(entry) * kListEntry, player);
}
std::vector<SearchProfileHit> search_profile_hits(const GameState& g, int manager) {
std::vector<SearchProfileHit> hits;
for (int entry = 0; entry < 40; ++entry) {
if (!search_profile_matches(g, manager, entry)) continue;
const int player = g.squad_slots[size_t(kPoolFirst + entry)] & 0xFFF;
hits.push_back({entry, player,
message_text(g, ::bmh::layout::Month_SearchProfileNews_TextPointer) + g.player_name(player) +
message_text(g, ::bmh::layout::Month_SearchProfileNews_TextPointer3)});
}
return hits;
}
} // namespace bmh
+61
View File
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-3.0-only
// The search profile of a human manager (transfer market buttons "Suchen" and "Vorschlag", original
// sub_19409 / sub_18F06 / sub_190B5). The profile is 18 bytes in the manager record at +0x1f6:
// [0..3] positions wanted (goalkeeper, defender, midfielder, forward); none set = every position
// [4..5] age range (stored as 0..24, i.e. age - 16), [6..7] strength range (0..99)
// [8..10] strength is judged by condition / technique / form (none set = all three)
// [11..14] foot and wing preference, [15..17] price classes wanted (asking price 70..130 %)
// Bit 0x40 of byte 0 marks the profile as switched off. Implementation: search_profile.cpp.
#pragma once
#include <array>
#include <cstdint>
#include <string>
#include <vector>
#include "game/state.hpp"
namespace bmh {
using SearchProfile = std::array<uint8_t, 18>;
constexpr size_t kSearchProfileOffset = 0x1f6;
constexpr uint8_t kSearchProfileOff = 0x40;
// The stored profile of a manager slot (all zero for an invalid slot).
SearchProfile search_profile(const GameState& g, int manager);
// Stores the profile the way the original dialog does when it is closed: a profile with every value 0 or
// every value 1 is switched off (bit 0x40), and so is any profile that was left by right click
// (`cancelled`). Returns whether the stored profile is active. Fails (false, nothing stored) for an
// invalid manager slot or a value above 1 where the original has only switches.
bool set_search_profile(GameState& g, int manager, SearchProfile profile, bool cancelled);
// Average strengths per position class of the manager's own squad (original sub_19A38): index 0..3
// goalkeeper..forward, index 4 the whole squad. `whole_squad` also counts the substitutes (24 slots
// instead of the first 11).
std::array<uint16_t, 5> position_strengths(const GameState& g, int club, bool whole_squad);
// The "Vorschlag" profile (original sub_18F06) from the squad of the manager: positions with too few
// players or the weakest line, age range 2..12, strength from the condition average to +20, and the
// attribute (condition or technique) in which the squad is clearly weaker.
SearchProfile suggest_search_profile(const GameState& g, int manager, bool whole_squad);
// The matching rule itself for the listing record at `list` (EditorList entry address) holding `player`.
// Shared with the news of month.cpp (search_profile_news).
bool search_profile_matches_listing(const GameState& g, int manager, uint32_t list, int player);
// Whether the listing in transfer-list entry `entry` (0..39) fits the manager's active profile
// (original sub_190B5 up to the message). Listings of his own club never match.
bool search_profile_matches(const GameState& g, int manager, int entry);
struct SearchProfileHit {
int entry = -1; // transfer-list entry 0..39
int player = -1; // player id
std::string message; // "<player name> ..." as shown in the original message box
};
// Confirming the dialog with "Suchen": every listing that fits the profile right now (original
// sub_190B5 with the message-box variant), in list order.
std::vector<SearchProfileHit> search_profile_hits(const GameState& g, int manager);
} // namespace bmh
+89
View File
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: GPL-3.0-only
// Search profile of a human manager (game/search_profile.*): the closing rule of the dialog and the
// matching of a transfer-list entry. The engine was also compared with the original routines sub_190B5 /
// sub_18F06 of the Diamond 3.7 executable on whole savegames (private emulator harness, not part of this test).
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include "formats/executable_layout.hpp"
#include "game/search_profile.hpp"
using namespace bmh;
namespace {
void require(bool value, const char* message) { if (!value) throw std::runtime_error(message); }
// Manager 0 manages club 3; one listing of a defender of club 5 (age field 6, price byte 0x5a = class 1).
GameState fixture() {
GameState g;
g.manager_teams = {3};
g.manager_records.resize(0xb0c);
g.squad_slots.assign(3072, 0xfff);
g.squad_slots[0xb70] = 0;
g.players.resize(1);
g.players[0].name = "Listed";
g.players[0].w35c = 70u << 14 | 70u << 7 | 70u;
g.players[0].w364 = 6u << 21 | 1u; // age field 6, position 1 (defender)
g.memory.add_block(layout::EditorList, 44 * 0x58);
g.memory.set16(layout::EditorList + 8, 5);
g.memory.set8(layout::EditorList + 0x54, 0x5a);
return g;
}
SearchProfile open_profile() {
SearchProfile p{};
p[4] = 0; p[5] = 24; p[6] = 0; p[7] = 99;
return p;
}
} // namespace
int main() try {
{ // a fresh manager has no active profile
auto g = fixture();
g.manager_records[kSearchProfileOffset] = kSearchProfileOff;
require(!search_profile_matches(g, 0, 0), "an inactive profile matches nothing");
}
{ // an open profile for defenders matches the listing, one for goalkeepers only does not
auto g = fixture();
auto p = open_profile(); p[1] = 1;
require(set_search_profile(g, 0, p, false) && search_profile(g, 0) == p, "profile with a position is stored active");
require(search_profile_matches(g, 0, 0), "defender profile matches the defender");
p[1] = 0; p[0] = 1;
require(set_search_profile(g, 0, p, false) && !search_profile_matches(g, 0, 0), "goalkeeper profile does not match");
require(!search_profile_matches(g, 0, 1), "an empty list slot never matches");
}
{ // the dialog's closing rule
auto g = fixture();
SearchProfile zero{};
require(!set_search_profile(g, 0, zero, false) && (search_profile(g, 0)[0] & kSearchProfileOff), "all zero switches the profile off");
SearchProfile ones; ones.fill(1);
require(!set_search_profile(g, 0, ones, false) && (search_profile(g, 0)[0] & kSearchProfileOff), "all one switches the profile off");
auto p = open_profile(); p[2] = 1;
require(!set_search_profile(g, 0, p, true) && (search_profile(g, 0)[0] & kSearchProfileOff), "leaving by right click switches the profile off");
require(search_profile(g, 0)[2] == 1, "the edited values are kept when the dialog is cancelled");
auto bad = p; bad[9] = 2;
const auto before = search_profile(g, 0);
require(!set_search_profile(g, 0, bad, false) && search_profile(g, 0) == before, "a switch above 1 is refused and nothing is stored");
bad = p; bad[5] = 25;
require(!set_search_profile(g, 0, bad, false), "the age bar has 25 cells");
require(!set_search_profile(g, 5, p, false), "an invalid manager is refused");
}
{ // age and price class
auto g = fixture();
auto p = open_profile(); p[1] = 1; p[4] = 10; p[5] = 20;
require(set_search_profile(g, 0, p, false) && !search_profile_matches(g, 0, 0), "age field 6 is outside 10..20");
p[4] = 2; p[5] = 8; p[15] = 1; // only the cheapest price class wanted, the listing asks 90 % (class 1)
require(set_search_profile(g, 0, p, false) && !search_profile_matches(g, 0, 0), "price class 1 is above the wanted class 0");
p[16] = 1;
require(set_search_profile(g, 0, p, false) && search_profile_matches(g, 0, 0), "price class 1 is wanted");
}
{ // the own club's listings never match
auto g = fixture();
g.memory.set16(layout::EditorList + 8, 3);
auto p = open_profile(); p[1] = 1;
require(set_search_profile(g, 0, p, false) && !search_profile_matches(g, 0, 0), "an own listing never matches");
}
std::cout << "PASS search profile closing rule and matching\n";
return 0;
} catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }