M3 - Modern world loader: - modern/content/world: typed view of the world tables (leagues, clubs, players, nations, league dates, training camp catalogue). - modern/content/world_package: opens a Modern world (runtime:modern-v1, or editor:world-v1 until data revision 1.4), reads only the engine entries and decodes the Classic payload (import: load_classic_payload). - modern/content/world_expand: turns the Classic start into the world: club ids in the order of the world's leagues, per-club tables remapped (added clubs copy an original club of their league), added players from their attributes, 24 squad slots per league club, pools kept, layout, league sizes and league dates installed; UTF-8 names and OBW ids in ModernState. - Fixes found by playing the Modern data set: referee pool exhausted on days with German and larger international leagues (Modern deviation), card table per league club grown, slot-to-team lookup via WorldLayout, form history stride from the layout, remaining slot literals. - Classic UI projections used by the session read the layout instead of executable constants (Classic values unchanged). - modern_season_sim opens .obw worlds; MODERN_DEBUG / MODERN_WATCH tracing. M4 - training camps: - Catalogue in ModernState (built-in 18 fictional camps on three maps, or the world's extended catalogue); price, booking and effect use it. - Six-star grades: step index capped, limit +1 from pitch 5 and +1 with pitch and fitness 6, catering 6 one more point of condition, lodging 6 one week less injury. Classic data behaves as before. modern_camp_tests. Verified: 190 tests pass; Classic replay regression identical to the M0 baseline; season_sim and modern_season_sim identical with Classic data; the Modern data set plays full seasons (leagues 34/38/34 rounds). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
103 lines
4.4 KiB
C++
103 lines
4.4 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#pragma once
|
|
// The static data of the original (its data object, LE addresses GermanDataSegmentBegin-layout field) as far
|
|
// as the savegame stores it.
|
|
//
|
|
// Many parts of the original work on small global tables that point at each other, e.g.
|
|
// the cup table at CupTable whose entries hold pointers to team lists and result arrays in
|
|
// other globals. Porting every one of them into a typed field would lose that structure,
|
|
// so they are kept here as raw memory addressed like in the original. The pointers stored
|
|
// in such tables are guest linear addresses: data address + kGuestDataBase.
|
|
//
|
|
// Blocks that GameState also decodes into typed fields (squads, players, results, ...)
|
|
// must be read and written through those fields; their copies here are not kept current.
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <map>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "formats/savestate.hpp"
|
|
|
|
namespace bmh {
|
|
|
|
// Offset between the LE addresses of the data object and the guest linear addresses the
|
|
// original stores in pointers (DOS/4GW loads the data object at relocated data-segment base).
|
|
constexpr uint32_t kGuestDataBase = 0x1a2000;
|
|
|
|
class StaticMemory {
|
|
public:
|
|
StaticMemory() = default;
|
|
// All non-heap blocks of a savegame.
|
|
explicit StaticMemory(const SaveState& save) {
|
|
for (const SaveBlock& b : save.blocks())
|
|
if (!b.heap && b.size != 0 && !blocks_.count(b.address))
|
|
blocks_[b.address].assign(save.stream().begin() + b.offset, save.stream().begin() + b.offset + b.size);
|
|
}
|
|
|
|
// A global the savegame does not store (e.g. a work list the original rebuilds before
|
|
// use): created zeroed unless it is there already.
|
|
void add_block(uint32_t address, size_t size) {
|
|
if (!has(address)) blocks_[address].assign(size, 0);
|
|
}
|
|
|
|
// Enlarges the block starting at `address` to `size` bytes (new bytes zero) when the space
|
|
// up to the next block is free; a Modern world uses it for tables per club that have
|
|
// room behind them. Throws std::out_of_range when the block is missing or too close.
|
|
void grow_block(uint32_t address, size_t size) {
|
|
auto it = blocks_.find(address);
|
|
if (it == blocks_.end()) throw std::out_of_range("static memory: no block at " + hex(address));
|
|
if (size <= it->second.size()) return;
|
|
auto next = std::next(it);
|
|
if (next != blocks_.end() && address + size > next->first)
|
|
throw std::out_of_range("static memory: block " + hex(address) + " cannot grow into " + hex(next->first));
|
|
it->second.resize(size, 0);
|
|
}
|
|
|
|
bool has(uint32_t address) const {
|
|
auto it = blocks_.upper_bound(address);
|
|
if (it == blocks_.begin()) return false;
|
|
--it;
|
|
return address - it->first < it->second.size();
|
|
}
|
|
uint8_t& at(uint32_t address) {
|
|
auto it = blocks_.upper_bound(address);
|
|
if (it != blocks_.begin()) {
|
|
--it;
|
|
if (address - it->first < it->second.size()) return it->second[address - it->first];
|
|
}
|
|
throw std::out_of_range("static memory: address " + hex(address) + " is not in the savegame");
|
|
}
|
|
uint8_t u8(uint32_t a) const { return const_cast<StaticMemory*>(this)->at(a); }
|
|
uint16_t u16(uint32_t a) const { return uint16_t(u8(a) | u8(a + 1) << 8); }
|
|
uint32_t u32(uint32_t a) const { return uint32_t(u16(a) | uint32_t(u16(a + 2)) << 16); }
|
|
void set8(uint32_t a, uint8_t v) { at(a) = v; }
|
|
void set16(uint32_t a, uint16_t v) { at(a) = uint8_t(v), at(a + 1) = uint8_t(v >> 8); }
|
|
void set32(uint32_t a, uint32_t v) { set16(a, uint16_t(v)), set16(a + 2, uint16_t(v >> 16)); }
|
|
// A pointer stored in the original's memory, as an LE data address (0 stays 0).
|
|
uint32_t pointer(uint32_t a) const {
|
|
uint32_t p = u32(a);
|
|
return p == 0 ? 0 : p - kGuestDataBase;
|
|
}
|
|
void set_pointer(uint32_t a, uint32_t address) { set32(a, address == 0 ? 0 : address + kGuestDataBase); }
|
|
|
|
const std::map<uint32_t, Bytes> &blocks() const { return blocks_; }
|
|
static StaticMemory from_blocks(std::map<uint32_t, Bytes> blocks) {
|
|
StaticMemory memory;
|
|
memory.blocks_ = std::move(blocks);
|
|
return memory;
|
|
}
|
|
|
|
private:
|
|
static std::string hex(uint32_t v) {
|
|
char b[16];
|
|
std::snprintf(b, sizeof b, "0x%x", v);
|
|
return b;
|
|
}
|
|
std::map<uint32_t, std::vector<uint8_t>> blocks_;
|
|
};
|
|
|
|
} // namespace bmh
|