Files @ r22926:82779a967ee2
Branch filter:

Location: cpp/openttd-patchpack/source/src/road_func.h - annotation

Niels Martin Hansen
Change: Modernise music control logic implementation (#6839)

Rewrite of almost the entire music control logic to a more modern style, hopefully also easier to understand. The old playlist handling made it look like arcane magic, which it doesn't have to be.

- Playlists are now stored in std::vector of objects instead of arrays of bytes with magic sentinel values, that need to be rotated around all the time. Position in playlist is stored as a simple index.
- The theme song is now reserved for the title screen, it doesn't play on any of the standard playlists, but is still available for use on custom playlists.
- When the player enters/leaves the game from the main menu, the music always restarts.
- Playback state (playing or not) is kept even if music becomes unavailable due to an empty playlist (or an empty music set), so it can restart immediately if music becomes available again.
- The shuffle algorithm was changed to a standard Fisher-Yates.
- Possibly better behavior when editing a custom playlist while it's playing.
- Custom playlists should be compatible.
- Framework for supporting custom playlists with songs from multiple music sets.
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8113:7815c26d7612
r8102:c3f2dd9e9116
r18560:ff3a65f20dab
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r15152:07ec410a4abe
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r21025:36271f2714f4
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r21025:36271f2714f4
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r21025:36271f2714f4
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r21025:36271f2714f4
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r19944:25a78576fb5e
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r21025:36271f2714f4
r12695:e3d3f57fdea8
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r21025:36271f2714f4
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r8734:d0410200053c
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r21025:36271f2714f4
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8102:c3f2dd9e9116
r8236:a2ecfe92ab23
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r21025:36271f2714f4
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r8716:fe14dd1f6c9e
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r21025:36271f2714f4
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r18560:ff3a65f20dab
r10207:a1fc2f2a33db
r8236:a2ecfe92ab23
r10207:a1fc2f2a33db
r8236:a2ecfe92ab23
r8344:df24797cd78b
r8342:c2c539be911f
r8102:c3f2dd9e9116
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD 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, version 2.
 * OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file road_func.h Functions related to roads. */

#ifndef ROAD_FUNC_H
#define ROAD_FUNC_H

#include "core/bitmath_func.hpp"
#include "road_type.h"
#include "economy_func.h"

/**
 * Iterate through each set RoadType in a RoadTypes value.
 * For more informations see FOR_EACH_SET_BIT_EX.
 *
 * @param var Loop index variable that stores fallowing set road type. Must be of type RoadType.
 * @param road_types The value to iterate through (any expression).
 *
 * @see FOR_EACH_SET_BIT_EX
 */
#define FOR_EACH_SET_ROADTYPE(var, road_types) FOR_EACH_SET_BIT_EX(RoadType, var, RoadTypes, road_types)

/**
 * Whether the given roadtype is valid.
 * @param rt the roadtype to check for validness
 * @return true if and only if valid
 */
static inline bool IsValidRoadType(RoadType rt)
{
	return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
}

/**
 * Whether the given roadtype is valid.
 * @param rt the roadtype to check for validness
 * @return true if and only if valid
 */
static inline bool IsValidRoadBits(RoadBits r)
{
	return r < ROAD_END;
}

/**
 * Maps a RoadType to the corresponding RoadTypes value
 *
 * @param rt the roadtype to get the roadtypes from
 * @return the roadtypes with the given roadtype
 */
static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
{
	assert(IsValidRoadType(rt));
	return (RoadTypes)(1 << rt);
}

/**
 * Returns the RoadTypes which are not present in the given RoadTypes
 *
 * This function returns the complement of a given RoadTypes.
 *
 * @param r The given RoadTypes
 * @return The complement of the given RoadTypes
 */
static inline RoadTypes ComplementRoadTypes(RoadTypes r)
{
	return (RoadTypes)(ROADTYPES_ALL ^ r);
}


/**
 * Calculate the complement of a RoadBits value
 *
 * Simply flips all bits in the RoadBits value to get the complement
 * of the RoadBits.
 *
 * @param r The given RoadBits value
 * @return the complement
 */
static inline RoadBits ComplementRoadBits(RoadBits r)
{
	assert(IsValidRoadBits(r));
	return (RoadBits)(ROAD_ALL ^ r);
}

/**
 * Calculate the mirrored RoadBits
 *
 * Simply move the bits to their new position.
 *
 * @param r The given RoadBits value
 * @return the mirrored
 */
static inline RoadBits MirrorRoadBits(RoadBits r)
{
	assert(IsValidRoadBits(r));
	return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
}

/**
 * Calculate rotated RoadBits
 *
 * Move the Roadbits clockwise until they are in their final position.
 *
 * @param r The given RoadBits value
 * @param rot The given Rotation angle
 * @return the rotated
 */
static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
{
	assert(IsValidRoadBits(r));
	for (; rot > (DiagDirDiff)0; rot--) {
		r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
	}
	return r;
}

/**
 * Check if we've got a straight road
 *
 * @param r The given RoadBits
 * @return true if we've got a straight road
 */
static inline bool IsStraightRoad(RoadBits r)
{
	assert(IsValidRoadBits(r));
	return (r == ROAD_X || r == ROAD_Y);
}

/**
 * Create the road-part which belongs to the given DiagDirection
 *
 * This function returns a RoadBits value which belongs to
 * the given DiagDirection.
 *
 * @param d The DiagDirection
 * @return The result RoadBits which the selected road-part set
 */
static inline RoadBits DiagDirToRoadBits(DiagDirection d)
{
	assert(IsValidDiagDirection(d));
	return (RoadBits)(ROAD_NW << (3 ^ d));
}

/**
 * Create the road-part which belongs to the given Axis
 *
 * This function returns a RoadBits value which belongs to
 * the given Axis.
 *
 * @param a The Axis
 * @return The result RoadBits which the selected road-part set
 */
static inline RoadBits AxisToRoadBits(Axis a)
{
	assert(IsValidAxis(a));
	return a == AXIS_X ? ROAD_X : ROAD_Y;
}


/**
 * Calculates the maintenance cost of a number of road bits.
 * @param roadtype Road type to get the cost for.
 * @param num Number of road bits.
 * @return Total cost.
 */
static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
{
	assert(IsValidRoadType(roadtype));
	return (_price[PR_INFRASTRUCTURE_ROAD] * (roadtype == ROADTYPE_TRAM ? 3 : 2) * num * (1 + IntSqrt(num))) >> 9; // 2 bits fraction for the multiplier and 7 bits scaling.
}

bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts);
bool ValParamRoadType(const RoadType rt);
RoadTypes GetCompanyRoadtypes(const CompanyID company);

void UpdateLevelCrossing(TileIndex tile, bool sound = true);

#endif /* ROAD_FUNC_H */