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->ProcessRegisters(&stage);
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 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
00186
00187 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00188
00189
00190 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00191 DrawWaterClassGround(ti);
00192 } else {
00193 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00194 }
00195 }
00196
00197 DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
00198 }
00199
00200 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00201 {
00202 ResolverObject object;
00203 const SpriteGroup *group;
00204
00205 assert(industry != NULL && IsValidTile(tile));
00206 assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00207
00208 NewIndustryTileResolver(&object, gfx_id, tile, industry);
00209 object.callback = callback;
00210 object.callback_param1 = param1;
00211 object.callback_param2 = param2;
00212
00213 group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], &object);
00214 if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00215
00216 return group->GetCallbackResult();
00217 }
00218
00219 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00220 {
00221 const SpriteGroup *group;
00222 ResolverObject object;
00223
00224 if (ti->tileh != SLOPE_FLAT) {
00225 bool draw_old_one = true;
00226 if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00227
00228 uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00229 draw_old_one = (callback_res != 0);
00230 }
00231
00232 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00233 }
00234
00235 NewIndustryTileResolver(&object, gfx, ti->tile, i);
00236
00237 group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], &object);
00238 if (group == NULL || group->type != SGT_TILELAYOUT) {
00239 return false;
00240 } else {
00241
00242 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00243 byte stage = GetIndustryConstructionStage(ti->tile);
00244 stage = Clamp(stage - 4 + tlgroup->num_building_stages, 0, tlgroup->num_building_stages - 1);
00245 IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00246 return true;
00247 }
00248 }
00249
00250 extern bool IsSlopeRefused(Slope current, Slope refused);
00251
00265 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)
00266 {
00267 Industry ind;
00268 ind.index = INVALID_INDUSTRY;
00269 ind.location.tile = ind_base_tile;
00270 ind.location.w = 0;
00271 ind.type = type;
00272 ind.random = initial_random_bits;
00273 ind.founder = founder;
00274
00275 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
00276 if (callback_res == CALLBACK_FAILED) {
00277 if (!IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused)) return CommandCost();
00278 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00279 }
00280 if (its->grf_prop.grffile->grf_version < 7) {
00281 if (callback_res != 0) return CommandCost();
00282 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00283 }
00284 if (callback_res == 0x400) return CommandCost();
00285
00286
00287 SwitchToErrorRefStack();
00288 PrepareTextRefStackUsage(4);
00289 SwitchToNormalRefStack();
00290
00291 switch (callback_res) {
00292 case 0x401: return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00293 case 0x402: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
00294 case 0x403: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
00295 default: return_cmd_error(GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res));
00296 }
00297 }
00298
00299
00300 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, const Industry *ind, TileIndex tile)
00301 {
00302 return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), const_cast<Industry *>(ind), tile);
00303 }
00304
00306 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, GetSimpleIndustryCallback> {
00307 static const CallbackID cb_animation_speed = CBID_INDTILE_ANIMATION_SPEED;
00308 static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
00309
00310 static const IndustryTileCallbackMask cbm_animation_speed = CBM_INDT_ANIM_SPEED;
00311 static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
00312 };
00313
00314 void AnimateNewIndustryTile(TileIndex tile)
00315 {
00316 const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00317 if (itspec == NULL) return;
00318
00319 IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
00320 }
00321
00322 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00323 {
00324 const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00325
00326 if (!HasBit(itspec->animation.triggers, iat)) return false;
00327
00328 IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
00329 return true;
00330 }
00331
00332 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00333 {
00334 bool ret = true;
00335 uint32 random = Random();
00336 TILE_AREA_LOOP(tile, ind->location) {
00337 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00338 if (StartStopIndustryTileAnimation(tile, iat, random)) {
00339 SB(random, 0, 16, Random());
00340 } else {
00341 ret = false;
00342 }
00343 }
00344 }
00345
00346 return ret;
00347 }
00348
00349 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
00350 {
00351 ResolverObject object;
00352
00353 assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00354
00355 IndustryGfx gfx = GetIndustryGfx(tile);
00356 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00357
00358 if (itspec->grf_prop.spritegroup[0] == NULL) return;
00359
00360 NewIndustryTileResolver(&object, gfx, tile, ind);
00361
00362 object.callback = CBID_RANDOM_TRIGGER;
00363 object.trigger = trigger;
00364
00365 const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], &object);
00366 if (group == NULL) return;
00367
00368 byte new_random_bits = Random();
00369 byte random_bits = GetIndustryRandomBits(tile);
00370 random_bits &= ~object.reseed;
00371 random_bits |= new_random_bits & object.reseed;
00372 SetIndustryRandomBits(tile, random_bits);
00373 MarkTileDirtyByTile(tile);
00374 }
00375
00376 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00377 {
00378 DoTriggerIndustryTile(tile, trigger, Industry::GetByTile(tile));
00379 }
00380
00381 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00382 {
00383 TILE_AREA_LOOP(tile, ind->location) {
00384 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00385 DoTriggerIndustryTile(tile, trigger, ind);
00386 }
00387 }
00388 }
00389
00395 void GetIndustryTileResolver(ResolverObject *ro, uint index)
00396 {
00397 NewIndustryTileResolver(ro, GetIndustryGfx(index), index, Industry::GetByTile(index));
00398 }