# newgrf_house.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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->last_value      = 0;
    00385   res->trigger         = 0;
    00386   res->reseed          = 0;
    00387   res->count           = 0;
    00388
    00389   const HouseSpec *hs  = HouseSpec::Get(house_id);
    00390   res->grffile         = (hs != NULL ? hs->grf_prop.grffile : NULL);
    00391 }
    00392
    00393 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, const Town *town, TileIndex tile, bool not_yet_constructed, uint8 initial_random_bits)
    00394 {
    00395   ResolverObject object;
    00396   const SpriteGroup *group;
    00397
    00398   assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
    00399
    00400   NewHouseResolver(&object, house_id, tile, town);
    00401   object.callback = callback;
    00402   object.callback_param1 = param1;
    00403   object.callback_param2 = param2;
    00404   object.u.house.not_yet_constructed = not_yet_constructed;
    00405   object.u.house.initial_random_bits = initial_random_bits;
    00406
    00407   group = SpriteGroup::Resolve(HouseSpec::Get(house_id)->grf_prop.spritegroup[0], &object);
    00408   if (group == NULL) return CALLBACK_FAILED;
    00409
    00410   return group->GetCallbackResult();
    00411 }
    00412
    00413 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
    00414 {
    00415   const DrawTileSprites *dts = group->dts;
    00416
    00417   const HouseSpec *hs = HouseSpec::Get(house_id);
    00418   PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
    00419   if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
    00420     uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
    00421     if (callback != CALLBACK_FAILED) {
    00422       /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
    00423       palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
    00424     }
    00425   }
    00426
    00427   SpriteID image = dts->ground.sprite;
    00428   PaletteID pal  = dts->ground.pal;
    00429
    00430   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
    00431
    00432   if (GB(image, 0, SPRITE_WIDTH) != 0) {
    00433     DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
    00434   }
    00435
    00436   DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
    00437 }
    00438
    00439 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
    00440 {
    00441   const HouseSpec *hs = HouseSpec::Get(house_id);
    00442   const SpriteGroup *group;
    00443   ResolverObject object;
    00444
    00445   if (ti->tileh != SLOPE_FLAT) {
    00446     bool draw_old_one = true;
    00447     if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
    00448       /* Called to determine the type (if any) of foundation to draw for the house tile */
    00449       uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
    00450       draw_old_one = (callback_res != 0);
    00451     }
    00452
    00453     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
    00454   }
    00455
    00456   NewHouseResolver(&object, house_id, ti->tile, Town::GetByTile(ti->tile));
    00457
    00458   group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
    00459   if (group == NULL || group->type != SGT_TILELAYOUT) {
    00460     return;
    00461   } else {
    00462     /* Limit the building stage to the number of stages supplied. */
    00463     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
    00464     byte stage = GetHouseBuildingStage(ti->tile);
    00465     stage = Clamp(stage - 4 + tlgroup->num_building_stages, 0, tlgroup->num_building_stages - 1);
    00466     DrawTileLayout(ti, tlgroup, stage, house_id);
    00467   }
    00468 }
    00469
    00470 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
    00471 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, const Town *town, TileIndex tile)
    00472 {
    00473   return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile);
    00474 }
    00475
    00477 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, GetSimpleHouseCallback> {
    00478   static const CallbackID cb_animation_speed      = CBID_HOUSE_ANIMATION_SPEED;
    00479   static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
    00480
    00481   static const HouseCallbackMask cbm_animation_speed      = CBM_HOUSE_ANIMATION_SPEED;
    00482   static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
    00483 };
    00484
    00485 void AnimateNewHouseTile(TileIndex tile)
    00486 {
    00487   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
    00488   if (hs == NULL) return;
    00489
    00490   HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
    00491 }
    00492
    00493 void AnimateNewHouseConstruction(TileIndex tile)
    00494 {
    00495   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
    00496
    00497   if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
    00498     HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
    00499   }
    00500 }
    00501
    00502 bool CanDeleteHouse(TileIndex tile)
    00503 {
    00504   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
    00505
    00506   /* Humans are always allowed to remove buildings, as is water and
    00507    * anyone using the scenario editor. */
    00508   if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
    00509     return true;
    00510   }
    00511
    00512   if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
    00513     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
    00514     return (callback_res == CALLBACK_FAILED || callback_res == 0);
    00515   } else {
    00516     return !(hs->extra_flags & BUILDING_IS_PROTECTED);
    00517   }
    00518 }
    00519
    00520 static void AnimationControl(TileIndex tile, uint16 random_bits)
    00521 {
    00522   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
    00523
    00524   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
    00525     uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
    00526     HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
    00527   }
    00528 }
    00529
    00530 bool NewHouseTileLoop(TileIndex tile)
    00531 {
    00532   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
    00533
    00534   if (GetHouseProcessingTime(tile) > 0) {
    00535     DecHouseProcessingTime(tile);
    00536     return true;
    00537   }
    00538
    00539   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
    00540   if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
    00541
    00542   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
    00543     /* If this house is marked as having a synchronised callback, all the
    00544      * tiles will have the callback called at once, rather than when the
    00545      * tile loop reaches them. This should only be enabled for the northern
    00546      * tile, or strange things will happen (here, and in TTDPatch). */
    00547     if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
    00548       uint16 random = GB(Random(), 0, 16);
    00549
    00550       if (hs->building_flags & BUILDING_HAS_1_TILE)  AnimationControl(tile, random);
    00551       if (hs->building_flags & BUILDING_2_TILES_Y)   AnimationControl(TILE_ADDXY(tile, 0, 1), random);
    00552       if (hs->building_flags & BUILDING_2_TILES_X)   AnimationControl(TILE_ADDXY(tile, 1, 0), random);
    00553       if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
    00554     } else {
    00555       AnimationControl(tile, 0);
    00556     }
    00557   }
    00558
    00559   /* Check callback 21, which determines if a house should be destroyed. */
    00560   if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
    00561     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
    00562     if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) > 0) {
    00563       ClearTownHouse(Town::GetByTile(tile), tile);
    00564       return false;
    00565     }
    00566   }
    00567
    00568   SetHouseProcessingTime(tile, hs->processing_time);
    00569   MarkTileDirtyByTile(tile);
    00570   return true;
    00571 }
    00572
    00573 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
    00574 {
    00575   ResolverObject object;
    00576
    00577   /* We can't trigger a non-existent building... */
    00578   assert(IsTileType(tile, MP_HOUSE));
    00579
    00580   HouseID hid = GetHouseType(tile);
    00581   HouseSpec *hs = HouseSpec::Get(hid);
    00582
    00583   if (hs->grf_prop.spritegroup == NULL) return;
    00584
    00585   NewHouseResolver(&object, hid, tile, Town::GetByTile(tile));
    00586
    00587   object.callback = CBID_RANDOM_TRIGGER;
    00588   object.trigger = trigger;
    00589
    00590   const SpriteGroup *group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
    00591   if (group == NULL) return;
    00592
    00593   byte new_random_bits = Random();
    00594   byte random_bits = GetHouseRandomBits(tile);
    00595   random_bits &= ~object.reseed;
    00596   random_bits |= (first ? new_random_bits : base_random) & object.reseed;
    00597   SetHouseRandomBits(tile, random_bits);
    00598
    00599   switch (trigger) {
    00600     case HOUSE_TRIGGER_TILE_LOOP:
    00601       /* Random value already set. */
    00602       break;
    00603
    00604     case HOUSE_TRIGGER_TILE_LOOP_TOP:
    00605       if (!first) {
    00606         /* The top tile is marked dirty by the usual TileLoop */
    00607         MarkTileDirtyByTile(tile);
    00608         break;
    00609       }
    00610       /* Random value of first tile already set. */
    00611       if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
    00612       if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
    00613       if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
    00614       break;
    00615   }
    00616 }
    00617
    00618 void TriggerHouse(TileIndex t, HouseTrigger trigger)
    00619 {
    00620   DoTriggerHouse(t, trigger, 0, true);
    00621 }
    00622
    00628 void GetHouseResolver(ResolverObject *ro, uint index)
    00629 {
    00630   NewHouseResolver(ro, GetHouseType(index), index, Town::GetByTile(index));
    00631 }