# newgrf_house.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_house.cpp 16324 2009-05-16 20:29:53Z rubidium $ */
    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 "town_map.h"
    00012 #include "sprite.h"
    00013 #include "newgrf.h"
    00014 #include "newgrf_house.h"
    00015 #include "newgrf_spritegroup.h"
    00016 #include "newgrf_town.h"
    00017 #include "newgrf_sound.h"
    00018 #include "newgrf_commons.h"
    00019 #include "transparency.h"
    00020 #include "functions.h"
    00021 #include "company_func.h"
    00022 #include "animated_tile_func.h"
    00023 #include "company_base.h"
    00024
    00025 static BuildingCounts<uint32> _building_counts;
    00026 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
    00027
    00028 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
    00029
    00030 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
    00031 {
    00032   /* Start from 1 because 0 means that no class has been assigned. */
    00033   for (int i = 1; i != lengthof(_class_mapping); i++) {
    00034     HouseClassMapping *map = &_class_mapping[i];
    00035
    00036     if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
    00037
    00038     if (map->class_id == 0 && map->grfid == 0) {
    00039       map->class_id = grf_class_id;
    00040       map->grfid    = grfid;
    00041       return (HouseClassID)i;
    00042     }
    00043   }
    00044   return HOUSE_NO_CLASS;
    00045 }
    00046
    00047 void InitializeBuildingCounts()
    00048 {
    00049   memset(&_building_counts, 0, sizeof(_building_counts));
    00050 }
    00051
    00058 void IncreaseBuildingCount(Town *t, HouseID house_id)
    00059 {
    00060   HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
    00061
    00062   if (!_loaded_newgrf_features.has_newhouses) return;
    00063
    00064   t->building_counts.id_count[house_id]++;
    00065   _building_counts.id_count[house_id]++;
    00066
    00067   if (class_id == HOUSE_NO_CLASS) return;
    00068
    00069   t->building_counts.class_count[class_id]++;
    00070   _building_counts.class_count[class_id]++;
    00071 }
    00072
    00079 void DecreaseBuildingCount(Town *t, HouseID house_id)
    00080 {
    00081   HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
    00082
    00083   if (!_loaded_newgrf_features.has_newhouses) return;
    00084
    00085   if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--;
    00086   if (_building_counts.id_count[house_id] > 0)   _building_counts.id_count[house_id]--;
    00087
    00088   if (class_id == HOUSE_NO_CLASS) return;
    00089
    00090   if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
    00091   if (_building_counts.class_count[class_id] > 0)   _building_counts.class_count[class_id]--;
    00092 }
    00093
    00094 static uint32 HouseGetRandomBits(const ResolverObject *object)
    00095 {
    00096   const TileIndex tile = object->u.house.tile;
    00097   return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseRandomBits(tile);
    00098 }
    00099
    00100 static uint32 HouseGetTriggers(const ResolverObject *object)
    00101 {
    00102   const TileIndex tile = object->u.house.tile;
    00103   return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseTriggers(tile);
    00104 }
    00105
    00106 static void HouseSetTriggers(const ResolverObject *object, int triggers)
    00107 {
    00108   const TileIndex tile = object->u.house.tile;
    00109   if (IsTileType(tile, MP_HOUSE)) SetHouseTriggers(tile, triggers);
    00110 }
    00111
    00112 static uint32 GetNumHouses(HouseID house_id, const Town *town)
    00113 {
    00114   uint8 map_id_count, town_id_count, map_class_count, town_class_count;
    00115   HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
    00116
    00117   map_id_count     = ClampU(_building_counts.id_count[house_id], 0, 255);
    00118   map_class_count  = ClampU(_building_counts.class_count[class_id], 0, 255);
    00119   town_id_count    = ClampU(town->building_counts.id_count[house_id], 0, 255);
    00120   town_class_count = ClampU(town->building_counts.class_count[class_id], 0, 255);
    00121
    00122   return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
    00123 }
    00124
    00125 uint32 GetNearbyTileInformation(byte parameter, TileIndex tile)
    00126 {
    00127   tile = GetNearbyTile(parameter, tile);
    00128   return GetNearbyTileInformation(tile);
    00129 }
    00130
    00132 typedef struct {
    00133   const HouseSpec *hs;
    00134   TileIndex north_tile;
    00135 } SearchNearbyHouseData;
    00136
    00142 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
    00143 {
    00144   if (IsTileType(tile, MP_HOUSE)) {
    00145     HouseID house = GetHouseType(tile); // tile been examined
    00146     const HouseSpec *hs = GetHouseSpecs(house);
    00147     if (hs->grffile != NULL) { // must be one from a grf file
    00148       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
    00149
    00150       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
    00151       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
    00152
    00153       return hs->local_id == nbhd->hs->local_id &&  // same local id as the one requested
    00154         hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
    00155     }
    00156   }
    00157   return false;
    00158 }
    00159
    00165 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
    00166 {
    00167   if (IsTileType(tile, MP_HOUSE)) {
    00168     HouseID house = GetHouseType(tile); // tile been examined
    00169     const HouseSpec *hs = GetHouseSpecs(house);
    00170     if (hs->grffile != NULL) { // must be one from a grf file
    00171       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
    00172
    00173       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
    00174       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
    00175
    00176       return hs->class_id == nbhd->hs->class_id &&  // same classid as the one requested
    00177         hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
    00178     }
    00179   }
    00180   return false;
    00181 }
    00182
    00188 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
    00189 {
    00190   if (IsTileType(tile, MP_HOUSE)) {
    00191     HouseID house = GetHouseType(tile); // tile been examined
    00192     const HouseSpec *hs = GetHouseSpecs(house);
    00193     if (hs->grffile != NULL) { // must be one from a grf file
    00194       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
    00195
    00196       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
    00197       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
    00198
    00199       return hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
    00200     }
    00201   }
    00202   return false;
    00203 }
    00204
    00214 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
    00215 {
    00216   static TestTileOnSearchProc * const search_procs[3] = {
    00217     SearchNearbyHouseID,
    00218     SearchNearbyHouseClass,
    00219     SearchNearbyHouseGRFID,
    00220   };
    00221   TileIndex found_tile = tile;
    00222   uint8 searchtype = GB(parameter, 6, 2);
    00223   uint8 searchradius = GB(parameter, 0, 6);
    00224   if (searchtype >= lengthof(search_procs)) return 0;  // do not run on ill-defined code
    00225   if (searchradius < 1) return 0; // do not use a too low radius
    00226
    00227   SearchNearbyHouseData nbhd;
    00228   nbhd.hs = GetHouseSpecs(house);
    00229   nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
    00230
    00231   /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
    00232   if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
    00233     return DistanceManhattan(found_tile, tile);
    00234   }
    00235   return 0;
    00236 }
    00237
    00243 static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00244 {
    00245   const Town *town = object->u.house.town;
    00246   TileIndex tile   = object->u.house.tile;
    00247   HouseID house_id = object->u.house.house_id;
    00248
    00249   if (object->scope == VSG_SCOPE_PARENT) {
    00250     return TownGetVariable(variable, parameter, available, town);
    00251   }
    00252
    00253   switch (variable) {
    00254     /* Construction stage. */
    00255     case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
    00256
    00257     /* Building age. */
    00258     case 0x41: return IsTileType(tile, MP_HOUSE) ? GetHouseAge(tile) : 0;
    00259
    00260     /* Town zone */
    00261     case 0x42: return GetTownRadiusGroup(town, tile);
    00262
    00263     /* Terrain type */
    00264     case 0x43: return GetTerrainType(tile);
    00265
    00266     /* Number of this type of building on the map. */
    00267     case 0x44: return GetNumHouses(house_id, town);
    00268
    00269     /* Whether the town is being created or just expanded. */
    00270     case 0x45: return _generating_world ? 1 : 0;
    00271
    00272     /* Current animation frame. */
    00273     case 0x46: return IsTileType(tile, MP_HOUSE) ? GetHouseAnimationFrame(tile) : 0;
    00274
    00275     /* Position of the house */
    00276     case 0x47: return TileY(tile) << 16 | TileX(tile);
    00277
    00278     /* Building counts for old houses with id = parameter. */
    00279     case 0x60: return GetNumHouses(parameter, town);
    00280
    00281     /* Building counts for new houses with id = parameter. */
    00282     case 0x61: {
    00283       const HouseSpec *hs = GetHouseSpecs(house_id);
    00284       if (hs->grffile == NULL) return 0;
    00285
    00286       HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid);
    00287       return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
    00288     }
    00289
    00290     /* Land info for nearby tiles. */
    00291     case 0x62: return GetNearbyTileInformation(parameter, tile);
    00292
    00293     /* Current animation frame of nearby house tiles */
    00294     case 0x63: {
    00295       TileIndex testtile = GetNearbyTile(parameter, tile);
    00296       return IsTileType(testtile, MP_HOUSE) ? GetHouseAnimationFrame(testtile) : 0;
    00297     }
    00298
    00299     /* Cargo acceptance history of nearby stations */
    00300     /*case 0x64: not implemented yet */
    00301
    00302     /* Distance test for some house types */
    00303     case 0x65: return GetDistanceFromNearbyHouse(parameter, tile, object->u.house.house_id);
    00304   }
    00305
    00306   DEBUG(grf, 1, "Unhandled house property 0x%X", variable);
    00307
    00308   *available = false;
    00309   return UINT_MAX;
    00310 }
    00311
    00312 static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const SpriteGroup *group)
    00313 {
    00314   /* Houses do not have 'real' groups */
    00315   return NULL;
    00316 }
    00317
    00323 static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town)
    00324 {
    00325   res->GetRandomBits = HouseGetRandomBits;
    00326   res->GetTriggers   = HouseGetTriggers;
    00327   res->SetTriggers   = HouseSetTriggers;
    00328   res->GetVariable   = HouseGetVariable;
    00329   res->ResolveReal   = HouseResolveReal;
    00330
    00331   res->u.house.tile     = tile;
    00332   res->u.house.town     = town;
    00333   res->u.house.house_id = house_id;
    00334
    00335   res->callback        = CBID_NO_CALLBACK;
    00336   res->callback_param1 = 0;
    00337   res->callback_param2 = 0;
    00338   res->last_value      = 0;
    00339   res->trigger         = 0;
    00340   res->reseed          = 0;
    00341   res->count           = 0;
    00342
    00343   const HouseSpec *hs = GetHouseSpecs(house_id);
    00344   res->grffile         = (hs != NULL ? hs->grffile : NULL);
    00345 }
    00346
    00347 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile)
    00348 {
    00349   ResolverObject object;
    00350   const SpriteGroup *group;
    00351
    00352   NewHouseResolver(&object, house_id, tile, town);
    00353   object.callback = callback;
    00354   object.callback_param1 = param1;
    00355   object.callback_param2 = param2;
    00356
    00357   group = Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
    00358   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
    00359
    00360   return group->g.callback.result;
    00361 }
    00362
    00363 static void DrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte stage, HouseID house_id)
    00364 {
    00365   const DrawTileSprites *dts = group->g.layout.dts;
    00366   const DrawTileSeqStruct *dtss;
    00367
    00368   const HouseSpec *hs = GetHouseSpecs(house_id);
    00369   SpriteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
    00370   if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
    00371     uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile);
    00372     if (callback != CALLBACK_FAILED) {
    00373       /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
    00374       palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
    00375     }
    00376   }
    00377
    00378   SpriteID image = dts->ground.sprite;
    00379   SpriteID pal   = dts->ground.pal;
    00380
    00381   if (IS_CUSTOM_SPRITE(image)) image += stage;
    00382
    00383   if (GB(image, 0, SPRITE_WIDTH) != 0) {
    00384     DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
    00385   }
    00386
    00387   foreach_draw_tile_seq(dtss, dts->seq) {
    00388     if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;
    00389
    00390     image = dtss->image.sprite;
    00391     pal   = dtss->image.pal;
    00392
    00393     /* Stop drawing sprite sequence once we meet a sprite that doesn't have to be opaque */
    00394     if (IsInvisibilitySet(TO_HOUSES) && !HasBit(image, SPRITE_MODIFIER_OPAQUE)) return;
    00395
    00396     if (IS_CUSTOM_SPRITE(image)) image += stage;
    00397
    00398     pal = SpriteLayoutPaletteTransform(image, pal, palette);
    00399
    00400     if ((byte)dtss->delta_z != 0x80) {
    00401       AddSortableSpriteToDraw(
    00402         image, pal,
    00403         ti->x + dtss->delta_x, ti->y + dtss->delta_y,
    00404         dtss->size_x, dtss->size_y,
    00405         dtss->size_z, ti->z + dtss->delta_z,
    00406         !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_HOUSES)
    00407       );
    00408     } else {
    00409       /* For industries and houses delta_x and delta_y are unsigned */
    00410       AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_HOUSES));
    00411     }
    00412   }
    00413 }
    00414
    00415 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
    00416 {
    00417   const HouseSpec *hs = GetHouseSpecs(house_id);
    00418   const SpriteGroup *group;
    00419   ResolverObject object;
    00420
    00421   if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    00422
    00423   NewHouseResolver(&object, house_id, ti->tile, GetTownByTile(ti->tile));
    00424
    00425   group = Resolve(hs->spritegroup, &object);
    00426   if (group == NULL || group->type != SGT_TILELAYOUT) {
    00427     /* XXX: This is for debugging purposes really, and shouldn't stay. */
    00428     DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE);
    00429   } else {
    00430     /* Limit the building stage to the number of stages supplied. */
    00431     byte stage = GetHouseBuildingStage(ti->tile);
    00432     stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
    00433     DrawTileLayout(ti, group, stage, house_id);
    00434   }
    00435 }
    00436
    00437 void AnimateNewHouseTile(TileIndex tile)
    00438 {
    00439   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
    00440   byte animation_speed = hs->animation_speed;
    00441   bool frame_set_by_callback = false;
    00442
    00443   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) {
    00444     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
    00445     if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16);
    00446   }
    00447
    00448   /* An animation speed of 2 means the animation frame changes 4 ticks, and
    00449    * increasing this value by one doubles the wait. 2 is the minimum value
    00450    * allowed for animation_speed, which corresponds to 120ms, and 16 is the
    00451    * maximum, corresponding to around 33 minutes. */
    00452   if (_tick_counter % (1 << animation_speed) != 0) return;
    00453
    00454   byte frame      = GetHouseAnimationFrame(tile);
    00455   byte num_frames = GB(hs->animation_frames, 0, 7);
    00456
    00457   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_NEXT_FRAME)) {
    00458     uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0;
    00459     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
    00460
    00461     if (callback_res != CALLBACK_FAILED) {
    00462       frame_set_by_callback = true;
    00463
    00464       switch (callback_res & 0xFF) {
    00465         case 0xFF:
    00466           DeleteAnimatedTile(tile);
    00467           break;
    00468         case 0xFE:
    00469           /* Carry on as normal. */
    00470           frame_set_by_callback = false;
    00471           break;
    00472         default:
    00473           frame = callback_res & 0xFF;
    00474           break;
    00475       }
    00476
    00477       /* If the lower 7 bits of the upper byte of the callback
    00478        * result are not empty, it is a sound effect. */
    00479       if (GB(callback_res, 8, 7) != 0) PlayTileSound(hs->grffile, GB(callback_res, 8, 7), tile);
    00480     }
    00481   }
    00482
    00483   if (!frame_set_by_callback) {
    00484     if (frame < num_frames) {
    00485       frame++;
    00486     } else if (frame == num_frames && HasBit(hs->animation_frames, 7)) {
    00487       /* This animation loops, so start again from the beginning */
    00488       frame = 0;
    00489     } else {
    00490       /* This animation doesn't loop, so stay here */
    00491       DeleteAnimatedTile(tile);
    00492     }
    00493   }
    00494
    00495   SetHouseAnimationFrame(tile, frame);
    00496   MarkTileDirtyByTile(tile);
    00497 }
    00498
    00499 void ChangeHouseAnimationFrame(const GRFFile *file, TileIndex tile, uint16 callback_result)
    00500 {
    00501   switch (callback_result & 0xFF) {
    00502     case 0xFD: /* Do nothing. */         break;
    00503     case 0xFE: AddAnimatedTile(tile);    break;
    00504     case 0xFF: DeleteAnimatedTile(tile); break;
    00505     default:
    00506       SetHouseAnimationFrame(tile, callback_result & 0xFF);
    00507       AddAnimatedTile(tile);
    00508       break;
    00509   }
    00510   /* If the lower 7 bits of the upper byte of the callback
    00511    * result are not empty, it is a sound effect. */
    00512   if (GB(callback_result, 8, 7) != 0) PlayTileSound(file, GB(callback_result, 8, 7), tile);
    00513 }
    00514
    00515 bool CanDeleteHouse(TileIndex tile)
    00516 {
    00517   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
    00518
    00519   /* Humans are always allowed to remove buildings, as is water and
    00520    * anyone using the scenario editor. */
    00521   if ((IsValidCompanyID(_current_company) && IsHumanCompany(_current_company))
    00522       || _current_company == OWNER_WATER || _current_company == OWNER_NONE) return true;
    00523
    00524   if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
    00525     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
    00526     return (callback_res == CALLBACK_FAILED || callback_res == 0);
    00527   } else {
    00528     return !(hs->extra_flags & BUILDING_IS_PROTECTED);
    00529   }
    00530 }
    00531
    00532 static void AnimationControl(TileIndex tile, uint16 random_bits)
    00533 {
    00534   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
    00535
    00536   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
    00537     uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
    00538     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
    00539
    00540     if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
    00541   }
    00542 }
    00543
    00544 bool NewHouseTileLoop(TileIndex tile)
    00545 {
    00546   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
    00547
    00548   if (GetHouseProcessingTime(tile) > 0) {
    00549     DecHouseProcessingTime(tile);
    00550     return true;
    00551   }
    00552
    00553   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
    00554   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
    00555
    00556   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
    00557     /* If this house is marked as having a synchronised callback, all the
    00558      * tiles will have the callback called at once, rather than when the
    00559      * tile loop reaches them. This should only be enabled for the northern
    00560      * tile, or strange things will happen (here, and in TTDPatch). */
    00561     if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
    00562       uint16 random = GB(Random(), 0, 16);
    00563
    00564       if (hs->building_flags & BUILDING_HAS_1_TILE)  AnimationControl(tile, random);
    00565       if (hs->building_flags & BUILDING_2_TILES_Y)   AnimationControl(TILE_ADDXY(tile, 0, 1), random);
    00566       if (hs->building_flags & BUILDING_2_TILES_X)   AnimationControl(TILE_ADDXY(tile, 1, 0), random);
    00567       if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
    00568     } else {
    00569       AnimationControl(tile, 0);
    00570     }
    00571   }
    00572
    00573   /* Check callback 21, which determines if a house should be destroyed. */
    00574   if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
    00575     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
    00576     if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) > 0) {
    00577       ClearTownHouse(GetTownByTile(tile), tile);
    00578       return false;
    00579     }
    00580   }
    00581
    00582   SetHouseProcessingTime(tile, hs->processing_time);
    00583   return true;
    00584 }
    00585
    00586 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
    00587 {
    00588   ResolverObject object;
    00589
    00590   /* We can't trigger a non-existent building... */
    00591   assert(IsTileType(tile, MP_HOUSE));
    00592
    00593   HouseID hid = GetHouseType(tile);
    00594   HouseSpec *hs = GetHouseSpecs(hid);
    00595
    00596   if (hs->spritegroup == NULL) return;
    00597
    00598   NewHouseResolver(&object, hid, tile, GetTownByTile(tile));
    00599
    00600   object.callback = CBID_RANDOM_TRIGGER;
    00601   object.trigger = trigger;
    00602
    00603   const SpriteGroup *group = Resolve(hs->spritegroup, &object);
    00604   if (group == NULL) return;
    00605
    00606   byte new_random_bits = Random();
    00607   byte random_bits = GetHouseRandomBits(tile);
    00608   random_bits &= ~object.reseed;
    00609   random_bits |= (first ? new_random_bits : base_random) & object.reseed;
    00610   SetHouseRandomBits(tile, random_bits);
    00611
    00612   switch (trigger) {
    00613     case HOUSE_TRIGGER_TILE_LOOP:
    00614       /* Random value already set. */
    00615       break;
    00616
    00617     case HOUSE_TRIGGER_TILE_LOOP_TOP:
    00618       if (!first) break;
    00619       /* Random value of first tile already set. */
    00620       if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
    00621       if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
    00622       if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
    00623       break;
    00624   }
    00625 }
    00626
    00627 void TriggerHouse(TileIndex t, HouseTrigger trigger)
    00628 {
    00629   DoTriggerHouse(t, trigger, 0, true);
    00630 }