Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

9.3 KB
Raw Blame
/*
**	Command & Conquer Generals Zero Hour(tm)
**	Copyright 2025 Electronic Arts Inc.
**
**	This program is free software: you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation, either version 3 of the License, or
**	(at your option) any later version.
**
**	This program is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// RandomMapGenerator.h
// Builds a complete .map image in memory from a seed, so a match can be played
// on a map that was never written to disk.

#pragma once

#ifndef __RANDOMMAPGENERATOR_H_
#define __RANDOMMAPGENERATOR_H_

#include "Common/STLTypedefs.h"
#include "Common/MapReaderWriterInfo.h"

/** Version of the generated bytes. Bump it whenever the generator's output
	changes for unchanged settings: it travels with the seed in multiplayer, so
	a build that generates a different map is caught before the match starts
	rather than desyncing partway through it. The fingerprint test in
	test_gameengine.cpp fails until the version and the recorded CRCs agree, so
	the bump cannot be forgotten. */
#define RANDOM_MAP_GENERATOR_VERSION 18

/** How much ground the map gives everybody, before the per-player share. Players see them as
	Tiny, Small, Medium, Large and Extra Large. */
enum RandomMapSize
{
	RANDOM_MAP_SIZE_TINY = 0,
	RANDOM_MAP_SIZE_SMALL,
	RANDOM_MAP_SIZE_NORMAL,
	RANDOM_MAP_SIZE_LARGE,
	RANDOM_MAP_SIZE_HUGE,

	RANDOM_MAP_SIZE_COUNT
};

/** Everything the generator is allowed to read. Two machines handed the same
	settings must produce byte-identical maps, so nothing else - no clock, no
	options file, no global state - may leak into generate(). */
struct RandomMapSettings
{
	Int m_seed;						///< drives the whole layout
	Int m_playableCells;			///< cells per side of the playable area
	Int m_numPlayers;				///< start positions to place
	Int m_style;					///< 0: the seed picks the kind of map; else a shipped map's style (RandomMapGenerator::styleNamed)
	AsciiString m_template;			///< the shipped map the layout is synthesised from, as MapCache names it ("maps\twilight flame\twilight flame.map", lowercase); empty: the generator above, untouched
	UnsignedInt m_templateCRC;		///< CRC of that map's stored bytes; 0 until clampSettings reads it off the installed file. It is in the name, so a machine holding other bytes under the same path does not build a different map under it

	RandomMapSettings() : m_seed(0), m_playableCells(0), m_numPlayers(2), m_style(0), m_templateCRC(0) { }
};

class RandomMapGenerator
{
public:
	enum { MIN_PLAYERS = 2, MAX_PLAYERS = 8 };
	/** 768 plus the 20-cell border each side is 808 across: past the largest shipped map (Death
		Valley, 550 playable, 710 with its border) and inside the 1024 the shroud texture and the
		terrain assume (DEFAULT_TERRAIN_SIZE in W3DShroud.cpp). */
	enum { MIN_CELLS = 64, MAX_CELLS = 768 };
	enum { PREVIEW_PIXELS = 128 };		///< what getMapPreviewImage expects of the tga

	/// Build an uncompressed CkMp map image. Deterministic in the settings alone.
	static void generate( const RandomMapSettings& settings, std::vector<char>& mapBytes );

	/// A 128x128 24-bit tga of the same layout, for the map list entry.
	static void generatePreview( const RandomMapSettings& settings, std::vector<char>& tgaBytes );

	/** CRC of the generated bytes with the generator version folded in. Two
		builds that agree on this agree on the map; the test suite pins it. */
	static UnsignedInt fingerprint( const RandomMapSettings& settings );

	/** How big a map of this size is for this many players. Every player brings
		ground with them, so an eight-way game is not eight bases crammed onto a
		duel map. */
	static Int cellsFor( RandomMapSize size, Int numPlayers );

	/** The same for a map built from a shipped one: Medium gives each player the ground a seat has
		on the template, the other sizes a fixed share more or less of it. An empty or uninstalled
		template falls back to the plain table. */
	static Int cellsFor( RandomMapSize size, Int numPlayers, const AsciiString& templatePath );

	/** That rule on a template's playable cells and seats, for the tests. */
	static Int cellsForTemplate( RandomMapSize size, Int numPlayers, Int cellsX, Int cellsY, Int seats );

	/** Settings clamped the way generate() clamps them, so callers can report the truth. A template
		drops the style, and one that is not an installed shipped map is dropped itself. */
	static void clampSettings( RandomMapSettings& settings );

	/** The style a name such as "twilight" stands for, 0 when none does. A style is baked into the
		exe rather than read from a file, because every machine rebuilds the map from its name. */
	static Int styleNamed( const char *name );
};

/** Generate the map and its preview and keep both in memory under a path in the
	user map directory, named for the generator version and the settings. Nothing
	is written to disk: TheFileSystem serves that path out of the store below, so
	the map cache, the loader and the CRC all read it the way they read any other
	map. On success mapPathOut is what -map wants. */
extern Bool stageRandomMap( const RandomMapSettings& settings, AsciiString& mapPathOut );

/** The generated map at this path made again for another number of players and staged: the same
	seed, template or style and size, the size read back from the cells the name carries. A room
	calls it when its seat count changes. FALSE when the path is not a generated map. */
extern Bool restageRandomMap( const AsciiString& path, Int numPlayers, AsciiString& mapPathOut );

/** Is this the path of a generated map or its preview, or the map.ini beside a
	map that snows? The settings are in the name, which is what lets a replay, a
	save or a joining machine rebuild the same bytes from the name alone. A name
	carrying another generator version is not one this build can rebuild, so it
	is not one of ours. */
extern Bool isGeneratedMapPath( const AsciiString& path );

/** The bytes of a generated map, preview or map.ini, generated on the spot if this path
	has not been staged in this run - which is how a replay of a generated map
	plays back without the map having been kept anywhere. FALSE when the path is
	not a generated one. The bytes belong to the store and stay valid until it
	drops that map, so a caller that keeps them across a load is wrong. */
extern Bool generatedMapBytes( const AsciiString& path, const char **bytesOut, Int *sizeOut );

/** Every generated map the store is holding, so the map cache can carry an
	entry for each without a directory to scan. */
extern void generatedMapPaths( std::vector<AsciiString>& pathsOut );

/** The shipped map a generated map's path was built from ("maps\twilight flame\twilight flame.map"),
	for the name the map list shows. FALSE for a plain generated map or any other path. */
extern Bool generatedMapTemplate( const AsciiString& path, AsciiString& templatePathOut );

/** A generated map's folder name as the lobby's options string carries it, and back. The string
	has 400 bytes for eight seats, so the numbers go in base 36 and "_t<CRC>_<template folder>" as
	"~" and four base-36 letters of the folder's name; the template's CRC stays home, since the map's
	own CRC beside it already tells a machine whose template differs. FALSE for any other folder, or
	a wire name no shipped map here answers. */
extern Bool generatedMapWireName( const AsciiString& folder, AsciiString& wireOut );
extern Bool generatedMapFromWireName( const AsciiString& wire, AsciiString& folderOut );

/** One cliff mapping of a BlendTileData chunk, in the order WorldHeightMap::ParseBlendTileData reads
	it (version 5 on): the texture coordinates of a cell's four corners, 0 (x, y), 1 (x + 1, y),
	2 (x + 1, y + 1), 3 (x, y + 1), in TEXTURE_WIDTH units from the class's corner in the terrain
	texture, and a tile of the class they are for. */
struct RMGCliffInfo
{
	Int m_tileIndex;
	Real m_u[4], m_v[4];
	Bool m_flip;

	RMGCliffInfo() : m_tileIndex(0), m_flip(FALSE) { for( Int k = 0; k < 4; k++ ) m_u[k] = m_v[k] = 0.0f; }
};

/** The cliff mapping both writers bake from the final heights: a cell of a cliff class whose first
	tile and sheet width are in sheetFirst and sheetWidth (-1 and 0 for any other cell) and that rises
	steeply takes its texture off the face rather than off the plane. indexOut is the per-cell array
	of the chunk, infoOut the entries with the unwritten default at 0. */
extern void rmgCliffMapping( const std::vector<Int>& heights, Int width, Int height, const std::vector<Int>& sheetFirst,
	const std::vector<Int>& sheetWidth, std::vector<Short>& indexOut, std::vector<RMGCliffInfo>& infoOut );

/** A ChunkInputStream over a block of memory, so a generated map can be handed
	to the same readers that parse maps off disk. */
class MemoryChunkInputStream : public ChunkInputStream
{
public:
	MemoryChunkInputStream( const char *data, Int size );

	virtual Int read( void *pData, Int numBytes );
	virtual UnsignedInt tell( void );
	virtual Bool absoluteSeek( UnsignedInt pos );
	virtual Bool eof( void );

private:
	const char *m_data;
	Int m_size;
	Int m_pos;
};

#endif  // __RANDOMMAPGENERATOR_H_