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