Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / newgrf_spritegroup.cpp
blobdaa65ca0d7dfd547d3fce40345ab5814e2d464a4
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_spritegroup.h"
13 #include "newgrf_profiling.h"
14 #include "core/pool_func.hpp"
16 #include "safeguards.h"
18 SpriteGroupPool _spritegroup_pool("SpriteGroup");
19 INSTANTIATE_POOL_METHODS(SpriteGroup)
21 TemporaryStorageArray<int32_t, 0x110> _temp_store;
24 /**
25 * ResolverObject (re)entry point.
26 * This cannot be made a call to a virtual function because virtual functions
27 * do not like nullptr and checking for nullptr *everywhere* is more cumbersome than
28 * this little helper function.
29 * @param group the group to resolve for
30 * @param object information needed to resolve the group
31 * @param top_level true if this is a top-level SpriteGroup, false if used nested in another SpriteGroup.
32 * @return the resolved group
34 /* static */ const SpriteGroup *SpriteGroup::Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level)
36 if (group == nullptr) return nullptr;
38 const GRFFile *grf = object.grffile;
39 auto profiler = std::find_if(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](const NewGRFProfiler &pr) { return pr.grffile == grf; });
41 if (profiler == _newgrf_profilers.end() || !profiler->active) {
42 if (top_level) _temp_store.ClearChanges();
43 return group->Resolve(object);
44 } else if (top_level) {
45 profiler->BeginResolve(object);
46 _temp_store.ClearChanges();
47 const SpriteGroup *result = group->Resolve(object);
48 profiler->EndResolve(result);
49 return result;
50 } else {
51 profiler->RecursiveResolve();
52 return group->Resolve(object);
56 static inline uint32_t GetVariable(const ResolverObject &object, ScopeResolver *scope, byte variable, uint32_t parameter, bool *available)
58 uint32_t value;
59 switch (variable) {
60 case 0x0C: return object.callback;
61 case 0x10: return object.callback_param1;
62 case 0x18: return object.callback_param2;
63 case 0x1C: return object.last_value;
65 case 0x5F: return (scope->GetRandomBits() << 8) | scope->GetTriggers();
67 case 0x7D: return _temp_store.GetValue(parameter);
69 case 0x7F:
70 if (object.grffile == nullptr) return 0;
71 return object.grffile->GetParam(parameter);
73 default:
74 /* First handle variables common with Action7/9/D */
75 if (variable < 0x40 && GetGlobalVariable(variable, &value, object.grffile)) return value;
76 /* Not a common variable, so evaluate the feature specific variables */
77 return scope->GetVariable(variable, parameter, available);
81 /**
82 * Get a few random bits. Default implementation has no random bits.
83 * @return Random bits.
85 /* virtual */ uint32_t ScopeResolver::GetRandomBits() const
87 return 0;
90 /**
91 * Get the triggers. Base class returns \c 0 to prevent trouble.
92 * @return The triggers.
94 /* virtual */ uint32_t ScopeResolver::GetTriggers() const
96 return 0;
99 /**
100 * Get a variable value. Default implementation has no available variables.
101 * @param variable Variable to read
102 * @param parameter Parameter for 60+x variables
103 * @param[out] available Set to false, in case the variable does not exist.
104 * @return Value
106 /* virtual */ uint32_t ScopeResolver::GetVariable(byte variable, [[maybe_unused]] uint32_t parameter, bool *available) const
108 Debug(grf, 1, "Unhandled scope variable 0x{:X}", variable);
109 *available = false;
110 return UINT_MAX;
114 * Store a value into the persistent storage area (PSA). Default implementation does nothing (for newgrf classes without storage).
116 /* virtual */ void ScopeResolver::StorePSA(uint, int32_t) {}
119 * Get the real sprites of the grf.
120 * @param group Group to get.
121 * @return The available sprite group.
123 /* virtual */ const SpriteGroup *ResolverObject::ResolveReal(const RealSpriteGroup *group) const
125 if (!group->loaded.empty()) return group->loaded[0];
126 if (!group->loading.empty()) return group->loading[0];
128 return nullptr;
132 * Get a resolver for the \a scope.
133 * @return The resolver for the requested scope.
135 /* virtual */ ScopeResolver *ResolverObject::GetScope(VarSpriteGroupScope, byte)
137 return &this->default_scope;
140 /* Evaluate an adjustment for a variable of the given size.
141 * U is the unsigned type and S is the signed type to use. */
142 template <typename U, typename S>
143 static U EvalAdjustT(const DeterministicSpriteGroupAdjust &adjust, ScopeResolver *scope, U last_value, uint32_t value)
145 value >>= adjust.shift_num;
146 value &= adjust.and_mask;
148 switch (adjust.type) {
149 case DSGA_TYPE_DIV: value = ((S)value + (S)adjust.add_val) / (S)adjust.divmod_val; break;
150 case DSGA_TYPE_MOD: value = ((S)value + (S)adjust.add_val) % (S)adjust.divmod_val; break;
151 case DSGA_TYPE_NONE: break;
154 switch (adjust.operation) {
155 case DSGA_OP_ADD: return last_value + value;
156 case DSGA_OP_SUB: return last_value - value;
157 case DSGA_OP_SMIN: return std::min<S>(last_value, value);
158 case DSGA_OP_SMAX: return std::max<S>(last_value, value);
159 case DSGA_OP_UMIN: return std::min<U>(last_value, value);
160 case DSGA_OP_UMAX: return std::max<U>(last_value, value);
161 case DSGA_OP_SDIV: return value == 0 ? (S)last_value : (S)last_value / (S)value;
162 case DSGA_OP_SMOD: return value == 0 ? (S)last_value : (S)last_value % (S)value;
163 case DSGA_OP_UDIV: return value == 0 ? (U)last_value : (U)last_value / (U)value;
164 case DSGA_OP_UMOD: return value == 0 ? (U)last_value : (U)last_value % (U)value;
165 case DSGA_OP_MUL: return last_value * value;
166 case DSGA_OP_AND: return last_value & value;
167 case DSGA_OP_OR: return last_value | value;
168 case DSGA_OP_XOR: return last_value ^ value;
169 case DSGA_OP_STO: _temp_store.StoreValue((U)value, (S)last_value); return last_value;
170 case DSGA_OP_RST: return value;
171 case DSGA_OP_STOP: scope->StorePSA((U)value, (S)last_value); return last_value;
172 case DSGA_OP_ROR: return std::rotr<uint32_t>((U)last_value, (U)value & 0x1F); // mask 'value' to 5 bits, which should behave the same on all architectures.
173 case DSGA_OP_SCMP: return ((S)last_value == (S)value) ? 1 : ((S)last_value < (S)value ? 0 : 2);
174 case DSGA_OP_UCMP: return ((U)last_value == (U)value) ? 1 : ((U)last_value < (U)value ? 0 : 2);
175 case DSGA_OP_SHL: return (uint32_t)(U)last_value << ((U)value & 0x1F); // Same behaviour as in ParamSet, mask 'value' to 5 bits, which should behave the same on all architectures.
176 case DSGA_OP_SHR: return (uint32_t)(U)last_value >> ((U)value & 0x1F);
177 case DSGA_OP_SAR: return (int32_t)(S)last_value >> ((U)value & 0x1F);
178 default: return value;
183 static bool RangeHighComparator(const DeterministicSpriteGroupRange &range, uint32_t value)
185 return range.high < value;
188 const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) const
190 uint32_t last_value = 0;
191 uint32_t value = 0;
193 ScopeResolver *scope = object.GetScope(this->var_scope);
195 for (const auto &adjust : this->adjusts) {
196 /* Try to get the variable. We shall assume it is available, unless told otherwise. */
197 bool available = true;
198 if (adjust.variable == 0x7E) {
199 const SpriteGroup *subgroup = SpriteGroup::Resolve(adjust.subroutine, object, false);
200 if (subgroup == nullptr) {
201 value = CALLBACK_FAILED;
202 } else {
203 value = subgroup->GetCallbackResult();
206 /* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
207 } else if (adjust.variable == 0x7B) {
208 value = GetVariable(object, scope, adjust.parameter, last_value, &available);
209 } else {
210 value = GetVariable(object, scope, adjust.variable, adjust.parameter, &available);
213 if (!available) {
214 /* Unsupported variable: skip further processing and return either
215 * the group from the first range or the default group. */
216 return SpriteGroup::Resolve(this->error_group, object, false);
219 switch (this->size) {
220 case DSG_SIZE_BYTE: value = EvalAdjustT<uint8_t, int8_t> (adjust, scope, last_value, value); break;
221 case DSG_SIZE_WORD: value = EvalAdjustT<uint16_t, int16_t>(adjust, scope, last_value, value); break;
222 case DSG_SIZE_DWORD: value = EvalAdjustT<uint32_t, int32_t>(adjust, scope, last_value, value); break;
223 default: NOT_REACHED();
225 last_value = value;
228 object.last_value = last_value;
230 if (this->calculated_result) {
231 /* nvar == 0 is a special case -- we turn our value into a callback result */
232 if (value != CALLBACK_FAILED) value = GB(value, 0, 15);
233 static CallbackResultSpriteGroup nvarzero(0, true);
234 nvarzero.result = value;
235 return &nvarzero;
238 if (this->ranges.size() > 4) {
239 const auto &lower = std::lower_bound(this->ranges.begin(), this->ranges.end(), value, RangeHighComparator);
240 if (lower != this->ranges.end() && lower->low <= value) {
241 assert(lower->low <= value && value <= lower->high);
242 return SpriteGroup::Resolve(lower->group, object, false);
244 } else {
245 for (const auto &range : this->ranges) {
246 if (range.low <= value && value <= range.high) {
247 return SpriteGroup::Resolve(range.group, object, false);
252 return SpriteGroup::Resolve(this->default_group, object, false);
256 const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject &object) const
258 ScopeResolver *scope = object.GetScope(this->var_scope, this->count);
259 if (object.callback == CBID_RANDOM_TRIGGER) {
260 /* Handle triggers */
261 byte match = this->triggers & object.waiting_triggers;
262 bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
264 if (res) {
265 object.used_triggers |= match;
266 object.reseed[this->var_scope] |= (this->groups.size() - 1) << this->lowest_randbit;
270 uint32_t mask = ((uint)this->groups.size() - 1) << this->lowest_randbit;
271 byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
273 return SpriteGroup::Resolve(this->groups[index], object, false);
277 const SpriteGroup *RealSpriteGroup::Resolve(ResolverObject &object) const
279 return object.ResolveReal(this);
283 * Process registers and the construction stage into the sprite layout.
284 * The passed construction stage might get reset to zero, if it gets incorporated into the layout
285 * during the preprocessing.
286 * @param[in,out] stage Construction stage (0-3), or nullptr if not applicable.
287 * @return sprite layout to draw.
289 const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8_t *stage) const
291 if (!this->dts.NeedsPreprocessing()) {
292 if (stage != nullptr && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
293 return &this->dts;
296 static DrawTileSprites result;
297 uint8_t actual_stage = stage != nullptr ? *stage : 0;
298 this->dts.PrepareLayout(0, 0, 0, actual_stage, false);
299 this->dts.ProcessRegisters(0, 0, false);
300 result.seq = this->dts.GetLayout(&result.ground);
302 /* Stage has been processed by PrepareLayout(), set it to zero. */
303 if (stage != nullptr) *stage = 0;
305 return &result;