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