Files @ r26654:c80753c09a6b
Branch filter:

Location: cpp/openttd-patchpack/source/src/newgrf_spritegroup.cpp - annotation

Czcibor Bohusz-Dobosz
Change: Set minimum macOS version to 10.13 (#10253)
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6348:a905c3e6d8fa
r5584:545d748cc681
r19723:ed364738c640
r5584:545d748cc681
r24069:49625df81342
r11967:df0600d2c7e7
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r11967:df0600d2c7e7
r11967:df0600d2c7e7
r5584:545d748cc681
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r23607:36c15679007d
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r23607:36c15679007d
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r21089:ddd56f6ea08d
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r24069:49625df81342
r21089:ddd56f6ea08d
r21088:1f99ef8ae88f
r21088:1f99ef8ae88f
r21008:be2e3d3c935b
r5584:545d748cc681
r8546:225d8773329b
r5584:545d748cc681
r21008:be2e3d3c935b
r21008:be2e3d3c935b
r21008:be2e3d3c935b
r21008:be2e3d3c935b
r5584:545d748cc681
r19723:ed364738c640
r9540:357595a2ed99
r17769:40a7de7cde1c
r6521:f9fb7d189c75
r9750:1e2535e15958
r23607:36c15679007d
r21008:be2e3d3c935b
r9750:1e2535e15958
r22754:42d63bfa0503
r22754:42d63bfa0503
r22754:42d63bfa0503
r22754:42d63bfa0503
r22754:42d63bfa0503
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r25655:1030dcb7eb52
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r23023:7b8669afd1db
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19723:ed364738c640
r19723:ed364738c640
r25642:6743de9f1df8
r25642:6743de9f1df8
r25642:6743de9f1df8
r23607:36c15679007d
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19738:837a139219f0
r19723:ed364738c640
r19723:ed364738c640
r19738:837a139219f0
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19737:d7c69e3b4bd4
r19723:ed364738c640
r5584:545d748cc681
r5600:62b6ba0cfe7f
r11356:ae9277304002
r5600:62b6ba0cfe7f
r25356:d308476ebd68
r5600:62b6ba0cfe7f
r25356:d308476ebd68
r25356:d308476ebd68
r5600:62b6ba0cfe7f
r25356:d308476ebd68
r25356:d308476ebd68
r25356:d308476ebd68
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r25356:d308476ebd68
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r24597:afde5721a3b6
r24597:afde5721a3b6
r24597:afde5721a3b6
r24597:afde5721a3b6
r6617:4cbd48714a2c
r6617:4cbd48714a2c
r6617:4cbd48714a2c
r6617:4cbd48714a2c
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r17769:40a7de7cde1c
r6521:f9fb7d189c75
r19740:cdfba1e51313
r23032:f807fe650c10
r7612:58e7a3f2232d
r7612:58e7a3f2232d
r21376:6c94a5a545a2
r21376:6c94a5a545a2
r21376:6c94a5a545a2
r5600:62b6ba0cfe7f
r5600:62b6ba0cfe7f
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r21008:be2e3d3c935b
r5584:545d748cc681
r5607:024dca12843c
r5606:4b49e34b3fc2
r5584:545d748cc681
r21008:be2e3d3c935b
r5584:545d748cc681
r25356:d308476ebd68
r5584:545d748cc681
r5584:545d748cc681
r25356:d308476ebd68
r25356:d308476ebd68
r23607:36c15679007d
r5617:94e7517dc4e8
r5617:94e7517dc4e8
r11985:d8202dea2203
r5617:94e7517dc4e8
r13157:6b5799323a00
r19740:cdfba1e51313
r25356:d308476ebd68
r25356:d308476ebd68
r5617:94e7517dc4e8
r25356:d308476ebd68
r5617:94e7517dc4e8
r5584:545d748cc681
r5584:545d748cc681
r16464:ae3f342df07b
r5584:545d748cc681
r22757:59be1e5ea50d
r5584:545d748cc681
r5584:545d748cc681
r11991:9a2b087e564c
r19740:cdfba1e51313
r19740:cdfba1e51313
r19740:cdfba1e51313
r12022:f3eef0751aff
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21008:be2e3d3c935b
r5607:024dca12843c
r22761:4149c4b18e24
r5584:545d748cc681
r9088:e6d3f3cef115
r18290:4e805de55757
r11985:d8202dea2203
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r25354:5abb78ea0e29
r25354:5abb78ea0e29
r25354:5abb78ea0e29
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r25354:5abb78ea0e29
r25354:5abb78ea0e29
r25354:5abb78ea0e29
r22757:59be1e5ea50d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21088:1f99ef8ae88f
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21008:be2e3d3c935b
r5584:545d748cc681
r21008:be2e3d3c935b
r22707:80b972b362a5
r5584:545d748cc681
r22707:80b972b362a5
r11991:9a2b087e564c
r5584:545d748cc681
r5584:545d748cc681
r22707:80b972b362a5
r25355:29ba87e2b3c3
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r25355:29ba87e2b3c3
r19723:ed364738c640
r5584:545d748cc681
r21088:1f99ef8ae88f
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21008:be2e3d3c935b
r5584:545d748cc681
r21008:be2e3d3c935b
r5584:545d748cc681
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r18093:bb567a02767f
r23607:36c15679007d
r18093:bb567a02767f
r18093:bb567a02767f
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r18093:bb567a02767f
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r23607:36c15679007d
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
/*
 * 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 newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */

#include "stdafx.h"
#include "debug.h"
#include "newgrf_spritegroup.h"
#include "newgrf_profiling.h"
#include "core/pool_func.hpp"

#include "safeguards.h"

SpriteGroupPool _spritegroup_pool("SpriteGroup");
INSTANTIATE_POOL_METHODS(SpriteGroup)

TemporaryStorageArray<int32, 0x110> _temp_store;


/**
 * ResolverObject (re)entry point.
 * This cannot be made a call to a virtual function because virtual functions
 * do not like nullptr and checking for nullptr *everywhere* is more cumbersome than
 * this little helper function.
 * @param group the group to resolve for
 * @param object information needed to resolve the group
 * @param top_level true if this is a top-level SpriteGroup, false if used nested in another SpriteGroup.
 * @return the resolved group
 */
/* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
{
	if (group == nullptr) return nullptr;

	const GRFFile *grf = object.grffile;
	auto profiler = std::find_if(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](const NewGRFProfiler &pr) { return pr.grffile == grf; });

	if (profiler == _newgrf_profilers.end() || !profiler->active) {
		if (top_level) _temp_store.ClearChanges();
		return group->Resolve(object);
	} else if (top_level) {
		profiler->BeginResolve(object);
		_temp_store.ClearChanges();
		const SpriteGroup *result = group->Resolve(object);
		profiler->EndResolve(result);
		return result;
	} else {
		profiler->RecursiveResolve();
		return group->Resolve(object);
	}
}

static inline uint32 GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
{
	uint32 value;
	switch (variable) {
		case 0x0C: return object.callback;
		case 0x10: return object.callback_param1;
		case 0x18: return object.callback_param2;
		case 0x1C: return object.last_value;

		case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();

		case 0x7D: return _temp_store.GetValue(parameter);

		case 0x7F:
			if (object.grffile == nullptr) return 0;
			return object.grffile->GetParam(parameter);

		default:
			/* First handle variables common with Action7/9/D */
			if (variable < 0x40 && GetGlobalVariable(variable, &value, object.grffile)) return value;
			/* Not a common variable, so evaluate the feature specific variables */
			return scope->GetVariable(variable, parameter, available);
	}
}

/**
 * Get a few random bits. Default implementation has no random bits.
 * @return Random bits.
 */
/* virtual */ uint32 ScopeResolver::GetRandomBits() const
{
	return 0;
}

/**
 * Get the triggers. Base class returns \c 0 to prevent trouble.
 * @return The triggers.
 */
/* virtual */ uint32 ScopeResolver::GetTriggers() const
{
	return 0;
}

/**
 * Get a variable value. Default implementation has no available variables.
 * @param variable Variable to read
 * @param parameter Parameter for 60+x variables
 * @param[out] available Set to false, in case the variable does not exist.
 * @return Value
 */
/* virtual */ uint32 ScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
{
	Debug(grf, 1, "Unhandled scope variable 0x{:X}", variable);
	*available = false;
	return UINT_MAX;
}

/**
 * Store a value into the persistent storage area (PSA). Default implementation does nothing (for newgrf classes without storage).
 * @param reg Position to store into.
 * @param value Value to store.
 */
/* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}

/**
 * Get the real sprites of the grf.
 * @param group Group to get.
 * @return The available sprite group.
 */
/* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
{
	if (!group->loaded.empty())  return group->loaded[0];
	if (!group->loading.empty()) return group->loading[0];

	return nullptr;
}

/**
 * Get a resolver for the \a scope.
 * @param scope Scope to return.
 * @param relative Additional parameter for #VSG_SCOPE_RELATIVE.
 * @return The resolver for the requested scope.
 */
/* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope scope, byte relative)
{
	return &this->default_scope;
}

/* Evaluate an adjustment for a variable of the given size.
 * U is the unsigned type and S is the signed type to use. */
template <typename U, typename S>
static U EvalAdjustT(const DeterministicSpriteGroupAdjust &adjust, ScopeResolver *scope, U last_value, uint32 value)
{
	value >>= adjust.shift_num;
	value  &= adjust.and_mask;

	switch (adjust.type) {
		case DSGA_TYPE_DIV:  value = ((S)value + (S)adjust.add_val) / (S)adjust.divmod_val; break;
		case DSGA_TYPE_MOD:  value = ((S)value + (S)adjust.add_val) % (S)adjust.divmod_val; break;
		case DSGA_TYPE_NONE: break;
	}

	switch (adjust.operation) {
		case DSGA_OP_ADD:  return last_value + value;
		case DSGA_OP_SUB:  return last_value - value;
		case DSGA_OP_SMIN: return std::min<S>(last_value, value);
		case DSGA_OP_SMAX: return std::max<S>(last_value, value);
		case DSGA_OP_UMIN: return std::min<U>(last_value, value);
		case DSGA_OP_UMAX: return std::max<U>(last_value, value);
		case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
		case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
		case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
		case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
		case DSGA_OP_MUL:  return last_value * value;
		case DSGA_OP_AND:  return last_value & value;
		case DSGA_OP_OR:   return last_value | value;
		case DSGA_OP_XOR:  return last_value ^ value;
		case DSGA_OP_STO:  _temp_store.StoreValue((U)value, (S)last_value); return last_value;
		case DSGA_OP_RST:  return value;
		case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
		case DSGA_OP_ROR:  return ROR<uint32>((U)last_value, (U)value & 0x1F); // mask 'value' to 5 bits, which should behave the same on all architectures.
		case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
		case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
		case DSGA_OP_SHL:  return (uint32)(U)last_value << ((U)value & 0x1F); // Same behaviour as in ParamSet, mask 'value' to 5 bits, which should behave the same on all architectures.
		case DSGA_OP_SHR:  return (uint32)(U)last_value >> ((U)value & 0x1F);
		case DSGA_OP_SAR:  return (int32)(S)last_value >> ((U)value & 0x1F);
		default:           return value;
	}
}


static bool RangeHighComparator(const DeterministicSpriteGroupRange& range, uint32 value)
{
	return range.high < value;
}

const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
{
	uint32 last_value = 0;
	uint32 value = 0;

	ScopeResolver *scope = object.GetScope(this->var_scope);

	for (const auto &adjust : this->adjusts) {
		/* Try to get the variable. We shall assume it is available, unless told otherwise. */
		bool available = true;
		if (adjust.variable == 0x7E) {
			const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust.subroutine, object, false);
			if (subgroup == nullptr) {
				value = CALLBACK_FAILED;
			} else {
				value = subgroup->GetCallbackResult();
			}

			/* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
		} else if (adjust.variable == 0x7B) {
			value = GetVariable(object, scope, adjust.parameter, last_value, &available);
		} else {
			value = GetVariable(object, scope, adjust.variable, adjust.parameter, &available);
		}

		if (!available) {
			/* Unsupported variable: skip further processing and return either
			 * the group from the first range or the default group. */
			return SpriteGroup::Resolve(this->error_group, object, false);
		}

		switch (this->size) {
			case DSG_SIZE_BYTE:  value = EvalAdjustT<uint8,  int8> (adjust, scope, last_value, value); break;
			case DSG_SIZE_WORD:  value = EvalAdjustT<uint16, int16>(adjust, scope, last_value, value); break;
			case DSG_SIZE_DWORD: value = EvalAdjustT<uint32, int32>(adjust, scope, last_value, value); break;
			default: NOT_REACHED();
		}
		last_value = value;
	}

	object.last_value = last_value;

	if (this->calculated_result) {
		/* nvar == 0 is a special case -- we turn our value into a callback result */
		if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
		static CallbackResultSpriteGroup nvarzero(0, true);
		nvarzero.result = value;
		return &nvarzero;
	}

	if (this->ranges.size() > 4) {
		const auto &lower = std::lower_bound(this->ranges.begin(), this->ranges.end(), value, RangeHighComparator);
		if (lower != this->ranges.end() && lower->low <= value) {
			assert(lower->low <= value && value <= lower->high);
			return SpriteGroup::Resolve(lower->group, object, false);
		}
	} else {
		for (const auto &range : this->ranges) {
			if (range.low <= value && value <= range.high) {
				return SpriteGroup::Resolve(range.group, object, false);
			}
		}
	}

	return SpriteGroup::Resolve(this->default_group, object, false);
}


const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
{
	ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
	if (object.callback == CBID_RANDOM_TRIGGER) {
		/* Handle triggers */
		byte match = this->triggers & object.waiting_triggers;
		bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);

		if (res) {
			object.used_triggers |= match;
			object.reseed[this->var_scope] |= (this->groups.size() - 1) << this->lowest_randbit;
		}
	}

	uint32 mask = ((uint)this->groups.size() - 1) << this->lowest_randbit;
	byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;

	return SpriteGroup::Resolve(this->groups[index], object, false);
}


const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
{
	return object.ResolveReal(this);
}

/**
 * Process registers and the construction stage into the sprite layout.
 * The passed construction stage might get reset to zero, if it gets incorporated into the layout
 * during the preprocessing.
 * @param[in,out] stage Construction stage (0-3), or nullptr if not applicable.
 * @return sprite layout to draw.
 */
const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
{
	if (!this->dts.NeedsPreprocessing()) {
		if (stage != nullptr && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
		return &this->dts;
	}

	static DrawTileSprites result;
	uint8 actual_stage = stage != nullptr ? *stage : 0;
	this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
	this->dts.ProcessRegisters(0, 0, false);
	result.seq = this->dts.GetLayout(&result.ground);

	/* Stage has been processed by PrepareLayout(), set it to zero. */
	if (stage != nullptr) *stage = 0;

	return &result;
}