newgrf_house.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_house.cpp 22838 2011-08-25 13:24:32Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
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_house.h"
00018 #include "newgrf_spritegroup.h"
00019 #include "newgrf_town.h"
00020 #include "newgrf_sound.h"
00021 #include "company_func.h"
00022 #include "company_base.h"
00023 #include "town.h"
00024 #include "sprite.h"
00025 #include "genworld.h"
00026 #include "newgrf_animation_base.h"
00027 
00028 static BuildingCounts<uint32> _building_counts;
00029 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
00030 
00031 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
00032 
00033 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
00034 {
00035   /* Start from 1 because 0 means that no class has been assigned. */
00036   for (int i = 1; i != lengthof(_class_mapping); i++) {
00037     HouseClassMapping *map = &_class_mapping[i];
00038 
00039     if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
00040 
00041     if (map->class_id == 0 && map->grfid == 0) {
00042       map->class_id = grf_class_id;
00043       map->grfid    = grfid;
00044       return (HouseClassID)i;
00045     }
00046   }
00047   return HOUSE_NO_CLASS;
00048 }
00049 
00050 void InitializeBuildingCounts()
00051 {
00052   memset(&_building_counts, 0, sizeof(_building_counts));
00053 }
00054 
00061 void IncreaseBuildingCount(Town *t, HouseID house_id)
00062 {
00063   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00064 
00065   if (!_loaded_newgrf_features.has_newhouses) return;
00066 
00067   t->building_counts.id_count[house_id]++;
00068   _building_counts.id_count[house_id]++;
00069 
00070   if (class_id == HOUSE_NO_CLASS) return;
00071 
00072   t->building_counts.class_count[class_id]++;
00073   _building_counts.class_count[class_id]++;
00074 }
00075 
00082 void DecreaseBuildingCount(Town *t, HouseID house_id)
00083 {
00084   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00085 
00086   if (!_loaded_newgrf_features.has_newhouses) return;
00087 
00088   if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--;
00089   if (_building_counts.id_count[house_id] > 0)   _building_counts.id_count[house_id]--;
00090 
00091   if (class_id == HOUSE_NO_CLASS) return;
00092 
00093   if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
00094   if (_building_counts.class_count[class_id] > 0)   _building_counts.class_count[class_id]--;
00095 }
00096 
00097 static uint32 HouseGetRandomBits(const ResolverObject *object)
00098 {
00099   /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
00100   TileIndex tile = object->u.house.tile;
00101   assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00102   return object->u.house.not_yet_constructed ? object->u.house.initial_random_bits : GetHouseRandomBits(tile);
00103 }
00104 
00105 static uint32 HouseGetTriggers(const ResolverObject *object)
00106 {
00107   /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
00108   TileIndex tile = object->u.house.tile;
00109   assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00110   return object->u.house.not_yet_constructed ? 0 : GetHouseTriggers(tile);
00111 }
00112 
00113 static void HouseSetTriggers(const ResolverObject *object, int triggers)
00114 {
00115   TileIndex tile = object->u.house.tile;
00116   assert(!object->u.house.not_yet_constructed && IsValidTile(tile) && IsTileType(tile, MP_HOUSE));
00117   SetHouseTriggers(tile, triggers);
00118 }
00119 
00120 static uint32 GetNumHouses(HouseID house_id, const Town *town)
00121 {
00122   uint8 map_id_count, town_id_count, map_class_count, town_class_count;
00123   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00124 
00125   map_id_count     = ClampU(_building_counts.id_count[house_id], 0, 255);
00126   map_class_count  = ClampU(_building_counts.class_count[class_id], 0, 255);
00127   town_id_count    = ClampU(town->building_counts.id_count[house_id], 0, 255);
00128   town_class_count = ClampU(town->building_counts.class_count[class_id], 0, 255);
00129 
00130   return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
00131 }
00132 
00133 uint32 GetNearbyTileInformation(byte parameter, TileIndex tile)
00134 {
00135   tile = GetNearbyTile(parameter, tile);
00136   return GetNearbyTileInformation(tile);
00137 }
00138 
00140 typedef struct {
00141   const HouseSpec *hs;  
00142   TileIndex north_tile; 
00143 } SearchNearbyHouseData;
00144 
00151 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
00152 {
00153   if (IsTileType(tile, MP_HOUSE)) {
00154     HouseID house = GetHouseType(tile); // tile been examined
00155     const HouseSpec *hs = HouseSpec::Get(house);
00156     if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
00157       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00158 
00159       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00160       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00161 
00162       return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id &&  // same local id as the one requested
00163         hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;  // from the same grf
00164     }
00165   }
00166   return false;
00167 }
00168 
00175 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
00176 {
00177   if (IsTileType(tile, MP_HOUSE)) {
00178     HouseID house = GetHouseType(tile); // tile been examined
00179     const HouseSpec *hs = HouseSpec::Get(house);
00180     if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
00181       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00182 
00183       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00184       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00185 
00186       return hs->class_id == nbhd->hs->class_id &&  // same classid as the one requested
00187         hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;  // from the same grf
00188     }
00189   }
00190   return false;
00191 }
00192 
00199 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
00200 {
00201   if (IsTileType(tile, MP_HOUSE)) {
00202     HouseID house = GetHouseType(tile); // tile been examined
00203     const HouseSpec *hs = HouseSpec::Get(house);
00204     if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
00205       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00206 
00207       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00208       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00209 
00210       return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;  // from the same grf
00211     }
00212   }
00213   return false;
00214 }
00215 
00226 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
00227 {
00228   static TestTileOnSearchProc * const search_procs[3] = {
00229     SearchNearbyHouseID,
00230     SearchNearbyHouseClass,
00231     SearchNearbyHouseGRFID,
00232   };
00233   TileIndex found_tile = tile;
00234   uint8 searchtype = GB(parameter, 6, 2);
00235   uint8 searchradius = GB(parameter, 0, 6);
00236   if (searchtype >= lengthof(search_procs)) return 0;  // do not run on ill-defined code
00237   if (searchradius < 1) return 0; // do not use a too low radius
00238 
00239   SearchNearbyHouseData nbhd;
00240   nbhd.hs = HouseSpec::Get(house);
00241   nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00242 
00243   /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
00244   if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
00245     return DistanceManhattan(found_tile, tile);
00246   }
00247   return 0;
00248 }
00249 
00255 static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00256 {
00257   const Town *town = object->u.house.town;
00258   TileIndex tile   = object->u.house.tile;
00259   HouseID house_id = object->u.house.house_id;
00260 
00261   if (object->scope == VSG_SCOPE_PARENT) {
00262     return TownGetVariable(variable, parameter, available, town);
00263   }
00264 
00265   switch (variable) {
00266     /* Construction stage. */
00267     case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
00268 
00269     /* Building age. */
00270     case 0x41: return IsTileType(tile, MP_HOUSE) ? GetHouseAge(tile) : 0;
00271 
00272     /* Town zone */
00273     case 0x42: return GetTownRadiusGroup(town, tile);
00274 
00275     /* Terrain type */
00276     case 0x43: return GetTerrainType(tile);
00277 
00278     /* Number of this type of building on the map. */
00279     case 0x44: return GetNumHouses(house_id, town);
00280 
00281     /* Whether the town is being created or just expanded. */
00282     case 0x45: return _generating_world ? 1 : 0;
00283 
00284     /* Current animation frame. */
00285     case 0x46: return IsTileType(tile, MP_HOUSE) ? GetAnimationFrame(tile) : 0;
00286 
00287     /* Position of the house */
00288     case 0x47: return TileY(tile) << 16 | TileX(tile);
00289 
00290     /* Building counts for old houses with id = parameter. */
00291     case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, town) : 0;
00292 
00293     /* Building counts for new houses with id = parameter. */
00294     case 0x61: {
00295       const HouseSpec *hs = HouseSpec::Get(house_id);
00296       if (hs->grf_prop.grffile == NULL) return 0;
00297 
00298       HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
00299       return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
00300     }
00301 
00302     /* Land info for nearby tiles. */
00303     case 0x62: return GetNearbyTileInformation(parameter, tile);
00304 
00305     /* Current animation frame of nearby house tiles */
00306     case 0x63: {
00307       TileIndex testtile = GetNearbyTile(parameter, tile);
00308       return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
00309     }
00310 
00311     /* Cargo acceptance history of nearby stations */
00312     /*case 0x64: not implemented yet */
00313 
00314     /* Distance test for some house types */
00315     case 0x65: return GetDistanceFromNearbyHouse(parameter, tile, object->u.house.house_id);
00316 
00317     /* Class and ID of nearby house tile */
00318     case 0x66: {
00319       TileIndex testtile = GetNearbyTile(parameter, tile);
00320       if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00321       HouseSpec *hs = HouseSpec::Get(GetHouseType(testtile));
00322       /* Information about the grf local classid if the house has a class */
00323       uint houseclass = 0;
00324       if (hs->class_id != HOUSE_NO_CLASS) {
00325         houseclass = (hs->grf_prop.grffile == object->grffile ? 1 : 2) << 8;
00326         houseclass |= _class_mapping[hs->class_id].class_id;
00327       }
00328       /* old house type or grf-local houseid */
00329       uint local_houseid = 0;
00330       if (house_id < NEW_HOUSE_OFFSET) {
00331         local_houseid = house_id;
00332       } else {
00333         local_houseid = (hs->grf_prop.grffile == object->grffile ? 1 : 2) << 8;
00334         local_houseid |= hs->grf_prop.local_id;
00335       }
00336       return houseclass << 16 | local_houseid;
00337     }
00338 
00339     /* GRFID of nearby house tile */
00340     case 0x67: {
00341       TileIndex testtile = GetNearbyTile(parameter, tile);
00342       if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00343       HouseID house_id = GetHouseType(testtile);
00344       if (house_id < NEW_HOUSE_OFFSET) return 0;
00345       /* Checking the grffile information via HouseSpec doesn't work
00346        * in case the newgrf was removed. */
00347       return _house_mngr.mapping_ID[house_id].grfid;
00348     }
00349   }
00350 
00351   DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
00352 
00353   *available = false;
00354   return UINT_MAX;
00355 }
00356 
00357 static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00358 {
00359   /* Houses do not have 'real' groups */
00360   return NULL;
00361 }
00362 
00368 static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, const Town *town)
00369 {
00370   res->GetRandomBits = HouseGetRandomBits;
00371   res->GetTriggers   = HouseGetTriggers;
00372   res->SetTriggers   = HouseSetTriggers;
00373   res->GetVariable   = HouseGetVariable;
00374   res->ResolveReal   = HouseResolveReal;
00375 
00376   res->u.house.tile     = tile;
00377   res->u.house.town     = town;
00378   res->u.house.house_id = house_id;
00379   res->u.house.not_yet_constructed = false;
00380 
00381   res->callback        = CBID_NO_CALLBACK;
00382   res->callback_param1 = 0;
00383   res->callback_param2 = 0;
00384   res->ResetState();
00385 
00386   const HouseSpec *hs  = HouseSpec::Get(house_id);
00387   res->grffile         = (hs != NULL ? hs->grf_prop.grffile : NULL);
00388 }
00389 
00390 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, const Town *town, TileIndex tile, bool not_yet_constructed, uint8 initial_random_bits)
00391 {
00392   ResolverObject object;
00393   const SpriteGroup *group;
00394 
00395   assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00396 
00397   NewHouseResolver(&object, house_id, tile, town);
00398   object.callback = callback;
00399   object.callback_param1 = param1;
00400   object.callback_param2 = param2;
00401   object.u.house.not_yet_constructed = not_yet_constructed;
00402   object.u.house.initial_random_bits = initial_random_bits;
00403 
00404   group = SpriteGroup::Resolve(HouseSpec::Get(house_id)->grf_prop.spritegroup[0], &object);
00405   if (group == NULL) return CALLBACK_FAILED;
00406 
00407   return group->GetCallbackResult();
00408 }
00409 
00410 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
00411 {
00412   const DrawTileSprites *dts = group->dts;
00413 
00414   const HouseSpec *hs = HouseSpec::Get(house_id);
00415   PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
00416   if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
00417     uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00418     if (callback != CALLBACK_FAILED) {
00419       /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
00420       palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
00421     }
00422   }
00423 
00424   SpriteID image = dts->ground.sprite;
00425   PaletteID pal  = dts->ground.pal;
00426 
00427   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00428 
00429   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00430     DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00431   }
00432 
00433   DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
00434 }
00435 
00436 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
00437 {
00438   const HouseSpec *hs = HouseSpec::Get(house_id);
00439   const SpriteGroup *group;
00440   ResolverObject object;
00441 
00442   if (ti->tileh != SLOPE_FLAT) {
00443     bool draw_old_one = true;
00444     if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
00445       /* Called to determine the type (if any) of foundation to draw for the house tile */
00446       uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00447       draw_old_one = (callback_res != 0);
00448     }
00449 
00450     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00451   }
00452 
00453   NewHouseResolver(&object, house_id, ti->tile, Town::GetByTile(ti->tile));
00454 
00455   group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
00456   if (group == NULL || group->type != SGT_TILELAYOUT) {
00457     return;
00458   } else {
00459     /* Limit the building stage to the number of stages supplied. */
00460     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00461     byte stage = GetHouseBuildingStage(ti->tile);
00462     stage = tlgroup->GetConstructionStageOffset(stage);
00463     DrawTileLayout(ti, tlgroup, stage, house_id);
00464   }
00465 }
00466 
00467 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00468 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, const Town *town, TileIndex tile)
00469 {
00470   return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile);
00471 }
00472 
00474 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, GetSimpleHouseCallback> {
00475   static const CallbackID cb_animation_speed      = CBID_HOUSE_ANIMATION_SPEED;
00476   static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
00477 
00478   static const HouseCallbackMask cbm_animation_speed      = CBM_HOUSE_ANIMATION_SPEED;
00479   static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
00480 };
00481 
00482 void AnimateNewHouseTile(TileIndex tile)
00483 {
00484   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00485   if (hs == NULL) return;
00486 
00487   HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
00488 }
00489 
00490 void AnimateNewHouseConstruction(TileIndex tile)
00491 {
00492   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00493 
00494   if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
00495     HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
00496   }
00497 }
00498 
00499 bool CanDeleteHouse(TileIndex tile)
00500 {
00501   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00502 
00503   /* Humans are always allowed to remove buildings, as is water and
00504    * anyone using the scenario editor. */
00505   if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
00506     return true;
00507   }
00508 
00509   if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
00510     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00511     return (callback_res == CALLBACK_FAILED || callback_res == 0);
00512   } else {
00513     return !(hs->extra_flags & BUILDING_IS_PROTECTED);
00514   }
00515 }
00516 
00517 static void AnimationControl(TileIndex tile, uint16 random_bits)
00518 {
00519   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00520 
00521   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00522     uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
00523     HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
00524   }
00525 }
00526 
00527 bool NewHouseTileLoop(TileIndex tile)
00528 {
00529   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00530 
00531   if (GetHouseProcessingTime(tile) > 0) {
00532     DecHouseProcessingTime(tile);
00533     return true;
00534   }
00535 
00536   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
00537   if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
00538 
00539   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00540     /* If this house is marked as having a synchronised callback, all the
00541      * tiles will have the callback called at once, rather than when the
00542      * tile loop reaches them. This should only be enabled for the northern
00543      * tile, or strange things will happen (here, and in TTDPatch). */
00544     if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
00545       uint16 random = GB(Random(), 0, 16);
00546 
00547       if (hs->building_flags & BUILDING_HAS_1_TILE)  AnimationControl(tile, random);
00548       if (hs->building_flags & BUILDING_2_TILES_Y)   AnimationControl(TILE_ADDXY(tile, 0, 1), random);
00549       if (hs->building_flags & BUILDING_2_TILES_X)   AnimationControl(TILE_ADDXY(tile, 1, 0), random);
00550       if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
00551     } else {
00552       AnimationControl(tile, 0);
00553     }
00554   }
00555 
00556   /* Check callback 21, which determines if a house should be destroyed. */
00557   if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
00558     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00559     if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) > 0) {
00560       ClearTownHouse(Town::GetByTile(tile), tile);
00561       return false;
00562     }
00563   }
00564 
00565   SetHouseProcessingTime(tile, hs->processing_time);
00566   MarkTileDirtyByTile(tile);
00567   return true;
00568 }
00569 
00570 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
00571 {
00572   ResolverObject object;
00573 
00574   /* We can't trigger a non-existent building... */
00575   assert(IsTileType(tile, MP_HOUSE));
00576 
00577   HouseID hid = GetHouseType(tile);
00578   HouseSpec *hs = HouseSpec::Get(hid);
00579 
00580   if (hs->grf_prop.spritegroup == NULL) return;
00581 
00582   NewHouseResolver(&object, hid, tile, Town::GetByTile(tile));
00583 
00584   object.callback = CBID_RANDOM_TRIGGER;
00585   object.trigger = trigger;
00586 
00587   const SpriteGroup *group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
00588   if (group == NULL) return;
00589 
00590   byte new_random_bits = Random();
00591   byte random_bits = GetHouseRandomBits(tile);
00592   uint32 reseed = object.GetReseedSum(); // The scope only affects triggers, not the reseeding
00593   random_bits &= ~reseed;
00594   random_bits |= (first ? new_random_bits : base_random) & reseed;
00595   SetHouseRandomBits(tile, random_bits);
00596 
00597   switch (trigger) {
00598     case HOUSE_TRIGGER_TILE_LOOP:
00599       /* Random value already set. */
00600       break;
00601 
00602     case HOUSE_TRIGGER_TILE_LOOP_TOP:
00603       if (!first) {
00604         /* The top tile is marked dirty by the usual TileLoop */
00605         MarkTileDirtyByTile(tile);
00606         break;
00607       }
00608       /* Random value of first tile already set. */
00609       if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
00610       if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
00611       if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
00612       break;
00613   }
00614 }
00615 
00616 void TriggerHouse(TileIndex t, HouseTrigger trigger)
00617 {
00618   DoTriggerHouse(t, trigger, 0, true);
00619 }
00620 
00626 void GetHouseResolver(ResolverObject *ro, uint index)
00627 {
00628   NewHouseResolver(ro, GetHouseType(index), index, Town::GetByTile(index));
00629 }