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