Naming rights, marketing, the further properties and the stages now fill items of the balance sheet (other income, public relations, rents, periphery, other expenses). The manager gets a note in his mailbox when he sells the stadium's name, when that contract ends, when a stage is finished, after the annual general meeting and after a change of club. The live demo QA driver is marked as not yet ported to the notebook. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
112 lines
5.4 KiB
C++
112 lines
5.4 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/game/naming_rights.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "game/random.hpp"
|
|
#include "game/schedule.hpp"
|
|
#include "formats/executable_layout.hpp"
|
|
#include "modern/game/matchday.hpp"
|
|
#include "modern/game/modern_notes.hpp"
|
|
|
|
namespace bmh::modern {
|
|
namespace {
|
|
|
|
constexpr size_t kRecord = 0xb0c;
|
|
|
|
int league_club(const ModernState& g, int manager) {
|
|
if (manager < 0 || size_t(manager) >= g.manager_teams.size() || g.manager_records.size() < size_t(manager + 1) * kRecord) return -1;
|
|
const int team = g.manager_teams[size_t(manager)];
|
|
return team >= 0 && team < league_clubs() ? team : -1;
|
|
}
|
|
|
|
// The sponsor's name as the advertising list has it ("" where the texts are not loaded).
|
|
std::string sponsor_name(const ModernState& g, int sponsor) {
|
|
try {
|
|
return message_text(g, ::bmh::layout::Advertising_FormatError_ExecutableField3 + uint32_t(sponsor) * 4);
|
|
} catch (const std::exception&) { return {}; }
|
|
}
|
|
|
|
} // namespace
|
|
|
|
const ModernState::StadiumName* stadium_entry(const ModernState& g, int manager) {
|
|
return manager >= 0 && size_t(manager) < g.stadiums.size() ? &g.stadiums[size_t(manager)] : nullptr;
|
|
}
|
|
|
|
bool naming_rights_sold(const ModernState& g, int manager) {
|
|
const auto* entry = stadium_entry(g, manager);
|
|
return entry && entry->months > 0;
|
|
}
|
|
|
|
bool rename_stadium(ModernState& g, int manager, const std::string& name) {
|
|
if (league_club(g, manager) < 0 || naming_rights_sold(g, manager)) return false;
|
|
const size_t first = name.find_first_not_of(' '), last = name.find_last_not_of(' ');
|
|
if (first == std::string::npos) return false;
|
|
const std::string trimmed = name.substr(first, last - first + 1);
|
|
if (trimmed.size() > 40 || std::any_of(trimmed.begin(), trimmed.end(), [](unsigned char c) { return c < 0x20 || c == 0x7f; })) return false;
|
|
if (g.stadiums.size() <= size_t(manager)) g.stadiums.resize(size_t(manager) + 1);
|
|
g.stadiums[size_t(manager)].name = trimmed;
|
|
return true;
|
|
}
|
|
|
|
// OpenBMH deviates from the original: in Modern a manager names his stadium and can sell the name to a sponsor for one to five years; the sponsor pays monthly and the name is fixed until the contract ends.
|
|
std::vector<NamingOffer> naming_offers(const ModernState& g, int manager) {
|
|
const int team = league_club(g, manager);
|
|
if (team < 0 || naming_rights_sold(g, manager)) return {};
|
|
const size_t rec = size_t(manager) * kRecord;
|
|
int seats = 0;
|
|
for (size_t block = 0; block < 8; ++block) seats += int(g.manager_records[rec + 0x4c + block]) * 1000;
|
|
if (seats <= 0) return {};
|
|
// euros a seat and year, by league
|
|
static constexpr int kRate[5] = {36, 22, 12, 7, 7};
|
|
const int league = std::clamp(team_league(team), 0, 4);
|
|
const int prestige = g.manager_records[rec + 0x211];
|
|
const int64_t base = int64_t(seats) * kRate[league] / 12 * (80 + prestige * 2 / 5) / 100;
|
|
|
|
uint32_t salt = 2166136261u;
|
|
for (const unsigned char c : g.career_id) salt = (salt ^ c) * 16777619u;
|
|
GameRandom draw(int32_t((uint64_t(salt) + uint64_t(g.date / 28) * 6151u + uint64_t(manager) * 98317u + 17u) % 4000000u));
|
|
std::vector<NamingOffer> offers;
|
|
while (offers.size() < 3) {
|
|
NamingOffer offer;
|
|
offer.sponsor = uint8_t(draw.range(0, 9));
|
|
const int years = draw.range(1, 5);
|
|
// a longer contract pays a little less each month
|
|
const int percent = draw.range(80, 125) - (years - 1) * 3;
|
|
if (std::any_of(offers.begin(), offers.end(), [&](const NamingOffer& o) { return o.sponsor == offer.sponsor; })) continue;
|
|
offer.months = uint16_t(years * 12);
|
|
offer.monthly = uint32_t(std::max<int64_t>(base * percent / 100 / 1000 * 1000, 1000));
|
|
offers.push_back(offer);
|
|
}
|
|
return offers;
|
|
}
|
|
|
|
bool sell_naming_rights(ModernState& g, int manager, int index) {
|
|
const auto offers = naming_offers(g, manager);
|
|
if (index < 0 || size_t(index) >= offers.size()) return false;
|
|
if (g.stadiums.size() <= size_t(manager)) g.stadiums.resize(size_t(manager) + 1);
|
|
auto& entry = g.stadiums[size_t(manager)];
|
|
entry.sponsor = offers[size_t(index)].sponsor, entry.monthly = offers[size_t(index)].monthly, entry.months = offers[size_t(index)].months;
|
|
if (const std::string sponsor = sponsor_name(g, entry.sponsor); !sponsor.empty()) try {
|
|
post_note(g, manager, original_german("Namensrecht: Das Stadion heißt jetzt ") + sponsor + original_german("-Arena. ") + sponsor + original_german(" zahlt ") +
|
|
format_amount(g, int32_t(entry.monthly)) + original_german(" im Monat, ") + std::to_string(entry.months) + " Monate lang.");
|
|
} catch (const std::exception&) {} // (a state without the texts: no note)
|
|
return true;
|
|
}
|
|
|
|
void naming_rights_month(ModernState& g) {
|
|
for (size_t m = 0; m < g.stadiums.size() && m < g.manager_teams.size(); ++m) {
|
|
auto& entry = g.stadiums[m];
|
|
if (entry.months == 0) continue;
|
|
// the payment counts as other income on the balance sheet
|
|
if (league_club(g, int(m)) >= 0) modern::book_money(g, int(m), -int32_t(entry.monthly), true, true);
|
|
if (--entry.months == 0) {
|
|
if (const std::string sponsor = sponsor_name(g, entry.sponsor); !sponsor.empty())
|
|
post_note(g, int(m), original_german("Der Namensvertrag mit ") + sponsor + original_german(" ist ausgelaufen. Das Stadion trägt wieder seinen eigenen Namen."));
|
|
entry.sponsor = 0xff, entry.monthly = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace bmh::modern
|