00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "variables.h"
00008 #include "debug.h"
00009 #include "viewport_func.h"
00010 #include "landscape.h"
00011 #include "newgrf.h"
00012 #include "core/random_func.hpp"
00013 #include "newgrf_commons.h"
00014 #include "newgrf_industries.h"
00015 #include "newgrf_industrytiles.h"
00016 #include "newgrf_sound.h"
00017 #include "newgrf_text.h"
00018 #include "industry_map.h"
00019 #include "sprite.h"
00020 #include "transparency.h"
00021 #include "functions.h"
00022 #include "town.h"
00023 #include "command_func.h"
00024 #include "animated_tile_func.h"
00025 #include "water.h"
00026
00027 #include "table/strings.h"
00028
00036 uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index)
00037 {
00038 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00039 bool is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
00040
00041 return GetNearbyTileInformation(tile) | (is_same_industry ? 1 : 0) << 8;
00042 }
00043
00054 static uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
00055 {
00056 byte x = TileX(tile) - TileX(ind_tile);
00057 byte y = TileY(tile) - TileY(ind_tile);
00058
00059 return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
00060 }
00061
00062 static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00063 {
00064 const Industry *inds = object->u.industry.ind;
00065 TileIndex tile = object->u.industry.tile;
00066
00067 if (object->scope == VSG_SCOPE_PARENT) {
00068 return IndustryGetVariable(object, variable, parameter, available);
00069 }
00070
00071 switch (variable) {
00072
00073 case 0x40 : return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
00074
00075 case 0x41 : return GetTerrainType(tile);
00076
00077
00078 case 0x42 : return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
00079
00080
00081 case 0x43 : return GetRelativePosition(tile, inds->xy);
00082
00083
00084 case 0x44 : return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryAnimationState(tile) : 0;
00085
00086
00087 case 0x60 : return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index);
00088
00089
00090 case 0x61 : {
00091 tile = GetNearbyTile(parameter, tile);
00092 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == inds) {
00093 return GetIndustryAnimationState(tile);
00094 }
00095 return 0xFFFFFFFF;
00096 }
00097
00098
00099 case 0x62 : return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds);
00100 }
00101
00102 DEBUG(grf, 1, "Unhandled industry tile property 0x%X", variable);
00103
00104 *available = false;
00105 return (uint32)-1;
00106 }
00107
00108 static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const SpriteGroup *group)
00109 {
00110
00111 return NULL;
00112 }
00113
00114 static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
00115 {
00116 const TileIndex tile = object->u.industry.tile;
00117 if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
00118 return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : GetIndustryByTile(tile)->random;
00119 }
00120
00121 static uint32 IndustryTileGetTriggers(const ResolverObject *object)
00122 {
00123 const TileIndex tile = object->u.industry.tile;
00124 if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
00125 return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : GetIndustryByTile(tile)->random_triggers;
00126 }
00127
00128 static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
00129 {
00130 const TileIndex tile = object->u.industry.tile;
00131 if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return;
00132
00133 if (object->scope == VSG_SCOPE_SELF) {
00134 SetIndustryTriggers(tile, triggers);
00135 } else {
00136 GetIndustryByTile(tile)->random_triggers = triggers;
00137 }
00138 }
00139
00140 static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
00141 {
00142 res->GetRandomBits = IndustryTileGetRandomBits;
00143 res->GetTriggers = IndustryTileGetTriggers;
00144 res->SetTriggers = IndustryTileSetTriggers;
00145 res->GetVariable = IndustryTileGetVariable;
00146 res->ResolveReal = IndustryTileResolveReal;
00147
00148 res->psa = &indus->psa;
00149 res->u.industry.tile = tile;
00150 res->u.industry.ind = indus;
00151 res->u.industry.gfx = gfx;
00152 res->u.industry.type = indus->type;
00153
00154 res->callback = CBID_NO_CALLBACK;
00155 res->callback_param1 = 0;
00156 res->callback_param2 = 0;
00157 res->last_value = 0;
00158 res->trigger = 0;
00159 res->reseed = 0;
00160 res->count = 0;
00161
00162 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
00163 res->grffile = (its != NULL ? its->grf_prop.grffile : NULL);
00164 }
00165
00166 static void IndustryDrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
00167 {
00168 const DrawTileSprites *dts = group->g.layout.dts;
00169 const DrawTileSeqStruct *dtss;
00170
00171 SpriteID image = dts->ground.sprite;
00172 SpriteID pal = dts->ground.pal;
00173
00174 if (IS_CUSTOM_SPRITE(image)) image += stage;
00175
00176 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00177
00178
00179 if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
00180 DrawWaterClassGround(ti);
00181 } else {
00182 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00183 }
00184 }
00185
00186 foreach_draw_tile_seq(dtss, dts->seq) {
00187 if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;
00188
00189 image = dtss->image.sprite;
00190 pal = dtss->image.pal;
00191
00192
00193 if (IsInvisibilitySet(TO_INDUSTRIES) && !HasBit(image, SPRITE_MODIFIER_OPAQUE)) return;
00194
00195 if (IS_CUSTOM_SPRITE(image)) image += stage;
00196
00197 pal = SpriteLayoutPaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour));
00198
00199 if ((byte)dtss->delta_z != 0x80) {
00200 AddSortableSpriteToDraw(
00201 image, pal,
00202 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
00203 dtss->size_x, dtss->size_y,
00204 dtss->size_z, ti->z + dtss->delta_z,
00205 !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_INDUSTRIES)
00206 );
00207 } else {
00208
00209 AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_INDUSTRIES));
00210 }
00211 }
00212 }
00213
00214 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00215 {
00216 ResolverObject object;
00217 const SpriteGroup *group;
00218
00219 NewIndustryTileResolver(&object, gfx_id, tile, industry);
00220 object.callback = callback;
00221 object.callback_param1 = param1;
00222 object.callback_param2 = param2;
00223
00224 group = Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
00225 if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00226
00227 return group->g.callback.result;
00228 }
00229
00230 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00231 {
00232 const SpriteGroup *group;
00233 ResolverObject object;
00234
00235 if (ti->tileh != SLOPE_FLAT) {
00236 bool draw_old_one = true;
00237 if (HasBit(inds->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
00238
00239 uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00240 draw_old_one = callback_res != 0;
00241 }
00242
00243 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00244 }
00245
00246 NewIndustryTileResolver(&object, gfx, ti->tile, i);
00247
00248 group = Resolve(inds->grf_prop.spritegroup, &object);
00249 if (group == NULL || group->type != SGT_TILELAYOUT) {
00250 return false;
00251 } else {
00252
00253 byte stage = GetIndustryConstructionStage(ti->tile);
00254 stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
00255 IndustryDrawTileLayout(ti, group, i->random_colour, stage, gfx);
00256 return true;
00257 }
00258 }
00259
00260 extern bool IsSlopeRefused(Slope current, Slope refused);
00261
00262 bool PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index)
00263 {
00264 Industry ind;
00265 ind.index = INVALID_INDUSTRY;
00266 ind.xy = ind_base_tile;
00267 ind.width = 0;
00268 ind.type = type;
00269
00270 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, itspec_index, gfx, &ind, ind_tile);
00271 if (callback_res == CALLBACK_FAILED) {
00272 return !IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused);
00273 }
00274 if (its->grf_prop.grffile->grf_version < 7) {
00275 return callback_res != 0;
00276 }
00277 if (callback_res == 0x400) return true;
00278
00279
00280 SwitchToErrorRefStack();
00281 PrepareTextRefStackUsage(4);
00282 SwitchToNormalRefStack();
00283
00284 switch (callback_res) {
00285 case 0x401: _error_message = STR_0239_SITE_UNSUITABLE; return false;
00286 case 0x402: _error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST; return false;
00287 case 0x403: _error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT; return false;
00288 default: _error_message = GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res); return false;
00289 }
00290 }
00291
00292 void AnimateNewIndustryTile(TileIndex tile)
00293 {
00294 Industry *ind = GetIndustryByTile(tile);
00295 IndustryGfx gfx = GetIndustryGfx(tile);
00296 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00297 byte animation_speed = itspec->animation_speed;
00298
00299 if (HasBit(itspec->callback_flags, CBM_INDT_ANIM_SPEED)) {
00300 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
00301 if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
00302 }
00303
00304
00305
00306
00307
00308 if ((_tick_counter % (1 << animation_speed)) != 0) return;
00309
00310 bool frame_set_by_callback = false;
00311 byte frame = GetIndustryAnimationState(tile);
00312 uint16 num_frames = GB(itspec->animation_info, 0, 8);
00313
00314 if (HasBit(itspec->callback_flags, CBM_INDT_ANIM_NEXT_FRAME)) {
00315 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME, HasBit(itspec->animation_special_flags, 0) ? Random() : 0, 0, gfx, ind, tile);
00316
00317 if (callback_res != CALLBACK_FAILED) {
00318 frame_set_by_callback = true;
00319
00320 switch (callback_res & 0xFF) {
00321 case 0xFF:
00322 DeleteAnimatedTile(tile);
00323 break;
00324 case 0xFE:
00325
00326 frame_set_by_callback = false;
00327 break;
00328 default:
00329 frame = callback_res & 0xFF;
00330 break;
00331 }
00332
00333
00334
00335 if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
00336 }
00337 }
00338
00339 if (!frame_set_by_callback) {
00340 if (frame < num_frames) {
00341 frame++;
00342 } else if (frame == num_frames && GB(itspec->animation_info, 8, 8) == 1) {
00343
00344 frame = 0;
00345 } else {
00346
00347 DeleteAnimatedTile(tile);
00348 }
00349 }
00350
00351 SetIndustryAnimationState(tile, frame);
00352 MarkTileDirtyByTile(tile);
00353 }
00354
00355 static void ChangeIndustryTileAnimationFrame(const IndustryTileSpec *itspec, TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
00356 {
00357 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_START_STOP, random_bits, iat, gfx, ind, tile);
00358 if (callback_res == CALLBACK_FAILED) return;
00359
00360 switch (callback_res & 0xFF) {
00361 case 0xFD: break;
00362 case 0xFE: AddAnimatedTile(tile); break;
00363 case 0xFF: DeleteAnimatedTile(tile); break;
00364 default:
00365 SetIndustryAnimationState(tile, callback_res & 0xFF);
00366 AddAnimatedTile(tile);
00367 break;
00368 }
00369
00370
00371
00372 if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
00373 }
00374
00375 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00376 {
00377 IndustryGfx gfx = GetIndustryGfx(tile);
00378 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00379
00380 if (!HasBit(itspec->animation_triggers, iat)) return false;
00381
00382 Industry *ind = GetIndustryByTile(tile);
00383 ChangeIndustryTileAnimationFrame(itspec, tile, iat, random, gfx, ind);
00384 return true;
00385 }
00386
00387 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00388 {
00389 bool ret = true;
00390 uint32 random = Random();
00391 BEGIN_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
00392 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00393 if (StartStopIndustryTileAnimation(tile, iat, random)) {
00394 SB(random, 0, 16, Random());
00395 } else {
00396 ret = false;
00397 }
00398 }
00399 END_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
00400
00401 return ret;
00402 }
00403
00404 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
00405 {
00406 ResolverObject object;
00407
00408 IndustryGfx gfx = GetIndustryGfx(tile);
00409 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00410
00411 if (itspec->grf_prop.spritegroup == NULL) return;
00412
00413 NewIndustryTileResolver(&object, gfx, tile, ind);
00414
00415 object.callback = CBID_RANDOM_TRIGGER;
00416 object.trigger = trigger;
00417
00418 const SpriteGroup *group = Resolve(itspec->grf_prop.spritegroup, &object);
00419 if (group == NULL) return;
00420
00421 byte new_random_bits = Random();
00422 byte random_bits = GetIndustryRandomBits(tile);
00423 random_bits &= ~object.reseed;
00424 random_bits |= new_random_bits & object.reseed;
00425 SetIndustryRandomBits(tile, random_bits);
00426 }
00427
00428 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00429 {
00430 DoTriggerIndustryTile(tile, trigger, GetIndustryByTile(tile));
00431 }
00432
00433 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00434 {
00435 BEGIN_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
00436 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00437 DoTriggerIndustryTile(tile, trigger, ind);
00438 }
00439 END_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
00440 }