Files
@ r20718:cc912091a170
Branch filter:
Location: cpp/openttd-patchpack/source/src/newgrf_spritegroup.cpp - annotation
r20718:cc912091a170
11.2 KiB
text/x-c
(svn r25756) -Update from WebTranslator v3.0:
indonesian - 2 changes by UseYourIllusion
indonesian - 2 changes by UseYourIllusion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | r5584:545d748cc681 r5584:545d748cc681 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r6348:a905c3e6d8fa r5584:545d748cc681 r19723:ed364738c640 r5584:545d748cc681 r11967:df0600d2c7e7 r5584:545d748cc681 r11967:df0600d2c7e7 r11967:df0600d2c7e7 r5584:545d748cc681 r11985:d8202dea2203 r5584:545d748cc681 r18352:6c15d75d1ab9 r18352:6c15d75d1ab9 r11985:d8202dea2203 r5584:545d748cc681 r11985:d8202dea2203 r11985:d8202dea2203 r11985:d8202dea2203 r11985:d8202dea2203 r11985:d8202dea2203 r5584:545d748cc681 r11985:d8202dea2203 r11985:d8202dea2203 r18352:6c15d75d1ab9 r11985:d8202dea2203 r6332:7393965b5322 r14890:f3cb6e331a95 r6521:f9fb7d189c75 r5584:545d748cc681 r19723:ed364738c640 r5584:545d748cc681 r8546:225d8773329b r8546:225d8773329b r18307:9b4849582975 r8546:225d8773329b r8546:225d8773329b r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r19723:ed364738c640 r9540:357595a2ed99 r17769:40a7de7cde1c r6521:f9fb7d189c75 r9750:1e2535e15958 r14141:092094f6122c r14141:092094f6122c r9750:1e2535e15958 r19944:25a78576fb5e r19723:ed364738c640 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 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 r19738:837a139219f0 r19738:837a139219f0 r19944:25a78576fb5e r19738:837a139219f0 r19738:837a139219f0 r19738:837a139219f0 r19738:837a139219f0 r19724:343db83218d4 r19737:d7c69e3b4bd4 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19723:ed364738c640 r19738:837a139219f0 r19738:837a139219f0 r19738:837a139219f0 r19738:837a139219f0 r19738:837a139219f0 r19723:ed364738c640 r19723:ed364738c640 r19737:d7c69e3b4bd4 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 r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r5600:62b6ba0cfe7f r11356:ae9277304002 r5600:62b6ba0cfe7f r19740:cdfba1e51313 r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5601:e743a91066e5 r5601:e743a91066e5 r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r6617:4cbd48714a2c r6617:4cbd48714a2c r6617:4cbd48714a2c r6617:4cbd48714a2c r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r17769:40a7de7cde1c r6521:f9fb7d189c75 r19740:cdfba1e51313 r7612:58e7a3f2232d r7612:58e7a3f2232d r7612:58e7a3f2232d r15658:82566ed4deb1 r15658:82566ed4deb1 r15658:82566ed4deb1 r5600:62b6ba0cfe7f r5600:62b6ba0cfe7f r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r11991:9a2b087e564c r5584:545d748cc681 r5607:024dca12843c r5606:4b49e34b3fc2 r5584:545d748cc681 r5584:545d748cc681 r19740:cdfba1e51313 r5584:545d748cc681 r11991:9a2b087e564c r11991:9a2b087e564c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5617:94e7517dc4e8 r13157:6b5799323a00 r11985:d8202dea2203 r5617:94e7517dc4e8 r5617:94e7517dc4e8 r11985:d8202dea2203 r5617:94e7517dc4e8 r13157:6b5799323a00 r19740:cdfba1e51313 r16860:0bad3b9958de r19740:cdfba1e51313 r5617:94e7517dc4e8 r19740:cdfba1e51313 r5617:94e7517dc4e8 r5584:545d748cc681 r5584:545d748cc681 r16464:ae3f342df07b r5584:545d748cc681 r11991:9a2b087e564c r5584:545d748cc681 r5584:545d748cc681 r11991:9a2b087e564c r19740:cdfba1e51313 r19740:cdfba1e51313 r19740:cdfba1e51313 r12022:f3eef0751aff r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5607:024dca12843c r5607:024dca12843c r11991:9a2b087e564c r5584:545d748cc681 r9088:e6d3f3cef115 r18290:4e805de55757 r11985:d8202dea2203 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r11991:9a2b087e564c r11991:9a2b087e564c r11991:9a2b087e564c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r11991:9a2b087e564c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r11991:9a2b087e564c r5584:545d748cc681 r19723:ed364738c640 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r19723:ed364738c640 r11991:9a2b087e564c r11991:9a2b087e564c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r17834:447b5cb3ee1a r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r19723:ed364738c640 r5584:545d748cc681 r5584:545d748cc681 r19723:ed364738c640 r19723:ed364738c640 r5584:545d748cc681 r11991:9a2b087e564c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r11991:9a2b087e564c r5584:545d748cc681 r19723:ed364738c640 r5584:545d748cc681 r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r18093:bb567a02767f r18093:bb567a02767f r18093:bb567a02767f r18093:bb567a02767f r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r18093:bb567a02767f r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac r17728:6a6e98f25cac | /* $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 newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */
#include "stdafx.h"
#include "debug.h"
#include "newgrf_spritegroup.h"
#include "core/pool_func.hpp"
SpriteGroupPool _spritegroup_pool("SpriteGroup");
INSTANTIATE_POOL_METHODS(SpriteGroup)
RealSpriteGroup::~RealSpriteGroup()
{
free(this->loaded);
free(this->loading);
}
DeterministicSpriteGroup::~DeterministicSpriteGroup()
{
free(this->adjusts);
free(this->ranges);
}
RandomizedSpriteGroup::~RandomizedSpriteGroup()
{
free(this->groups);
}
TemporaryStorageArray<int32, 0x110> _temp_store;
static inline uint32 GetVariable(const ResolverObject *object, ScopeResolver *scope, byte variable, uint32 parameter, bool *available)
{
/* First handle variables common with Action7/9/D */
uint32 value;
if (GetGlobalVariable(variable, &value, object->grffile)) return value;
/* Non-common variable */
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 == NULL || object->grffile == NULL) return 0;
return object->grffile->GetParam(parameter);
/* Not a common variable, so evaluate the feature specific variables */
default: return scope->GetVariable(variable, parameter, available);
}
}
ScopeResolver::ScopeResolver(ResolverObject *ro)
{
this->ro = ro;
}
ScopeResolver::~ScopeResolver() {}
/**
* 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;
}
/**
* Set the triggers. Base class implementation does nothing.
* @param triggers Triggers to set.
*/
/* virtual */ void ScopeResolver::SetTriggers(int triggers) const {}
/**
* 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 pos Position to store into.
* @param value Value to store.
*/
/* virtual */ void ScopeResolver::StorePSA(uint reg, int32 value) {}
/**
* Resolver constructor.
* @param grffile NewGRF file associated with the object (or \c NULL if none).
* @param callback Callback code being resolved (default value is #CBID_NO_CALLBACK).
* @param callback_param1 First parameter (var 10) of the callback (only used when \a callback is also set).
* @param callback_param2 Second parameter (var 18) of the callback (only used when \a callback is also set).
*/
ResolverObject::ResolverObject(const GRFFile *grffile, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
: default_scope(this)
{
this->callback = callback;
this->callback_param1 = callback_param1;
this->callback_param2 = callback_param2;
this->ResetState();
this->grffile = grffile;
}
ResolverObject::~ResolverObject() {}
/**
* 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
{
return NULL;
}
/**
* 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;
}
/**
* Rotate val rot times to the right
* @param val the value to rotate
* @param rot the amount of times to rotate
* @return the rotated value
*/
static uint32 RotateRight(uint32 val, uint32 rot)
{
/* Do not rotate more than necessary */
rot %= 32;
return (val >> rot) | (val << (32 - rot));
}
/* 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;
if (adjust->type != DSGA_TYPE_NONE) value += (S)adjust->add_val;
switch (adjust->type) {
case DSGA_TYPE_DIV: value /= (S)adjust->divmod_val; break;
case DSGA_TYPE_MOD: value %= (U)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 min((S)last_value, (S)value);
case DSGA_OP_SMAX: return max((S)last_value, (S)value);
case DSGA_OP_UMIN: return min((U)last_value, (U)value);
case DSGA_OP_UMAX: return max((U)last_value, (U)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 RotateRight(last_value, value);
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 (U)last_value << ((U)value & 0x1F); // mask 'value' to 5 bits, which should behave the same on all architectures.
case DSGA_OP_SHR: return (U)last_value >> ((U)value & 0x1F);
case DSGA_OP_SAR: return (S)last_value >> ((U)value & 0x1F);
default: return value;
}
}
const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject *object) const
{
uint32 last_value = 0;
uint32 value = 0;
uint i;
ScopeResolver *scope = object->GetScope(this->var_scope);
for (i = 0; i < this->num_adjusts; i++) {
DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
/* 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);
if (subgroup == NULL) {
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->num_ranges > 0 ? this->ranges[0].group : this->default_group, object);
}
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->num_ranges == 0) {
/* 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;
}
for (i = 0; i < this->num_ranges; i++) {
if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
return SpriteGroup::Resolve(this->ranges[i].group, object);
}
}
return SpriteGroup::Resolve(this->default_group, object);
}
const SpriteGroup *RandomizedSpriteGroup::Resolve(ResolverObject *object) const
{
ScopeResolver *scope = object->GetScope(this->var_scope, this->count);
if (object->trigger != 0) {
/* Handle triggers */
/* Magic code that may or may not do the right things... */
byte waiting_triggers = scope->GetTriggers();
byte match = this->triggers & (waiting_triggers | object->trigger);
bool res = (this->cmp_mode == RSG_CMP_ANY) ? (match != 0) : (match == this->triggers);
if (res) {
waiting_triggers &= ~match;
object->reseed[this->var_scope] |= (this->num_groups - 1) << this->lowest_randbit;
} else {
waiting_triggers |= object->trigger;
}
scope->SetTriggers(waiting_triggers);
}
uint32 mask = (this->num_groups - 1) << this->lowest_randbit;
byte index = (scope->GetRandomBits() & mask) >> this->lowest_randbit;
return SpriteGroup::Resolve(this->groups[index], object);
}
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 NULL if not applicable.
* @return sprite layout to draw.
*/
const DrawTileSprites *TileLayoutSpriteGroup::ProcessRegisters(uint8 *stage) const
{
if (!this->dts.NeedsPreprocessing()) {
if (stage != NULL && this->dts.consistent_max_offset > 0) *stage = GetConstructionStageOffset(*stage, this->dts.consistent_max_offset);
return &this->dts;
}
static DrawTileSprites result;
uint8 actual_stage = stage != NULL ? *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 != NULL) *stage = 0;
return &result;
}
|