newgrf_house.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_house.cpp 23740 2012-01-03 21:32:51Z 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 "landscape.h"
00015 #include "newgrf_house.h"
00016 #include "newgrf_spritegroup.h"
00017 #include "newgrf_town.h"
00018 #include "newgrf_sound.h"
00019 #include "company_func.h"
00020 #include "company_base.h"
00021 #include "town.h"
00022 #include "genworld.h"
00023 #include "newgrf_animation_base.h"
00024 #include "newgrf_cargo.h"
00025 #include "station_base.h"
00026 
00027 static BuildingCounts<uint32> _building_counts;
00028 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
00029 
00030 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
00031 
00032 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
00033 {
00034   /* Start from 1 because 0 means that no class has been assigned. */
00035   for (int i = 1; i != lengthof(_class_mapping); i++) {
00036     HouseClassMapping *map = &_class_mapping[i];
00037 
00038     if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
00039 
00040     if (map->class_id == 0 && map->grfid == 0) {
00041       map->class_id = grf_class_id;
00042       map->grfid    = grfid;
00043       return (HouseClassID)i;
00044     }
00045   }
00046   return HOUSE_NO_CLASS;
00047 }
00048 
00049 void InitializeBuildingCounts()
00050 {
00051   memset(&_building_counts, 0, sizeof(_building_counts));
00052 }
00053 
00060 void IncreaseBuildingCount(Town *t, HouseID house_id)
00061 {
00062   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00063 
00064   if (!_loaded_newgrf_features.has_newhouses) return;
00065 
00066   t->building_counts.id_count[house_id]++;
00067   _building_counts.id_count[house_id]++;
00068 
00069   if (class_id == HOUSE_NO_CLASS) return;
00070 
00071   t->building_counts.class_count[class_id]++;
00072   _building_counts.class_count[class_id]++;
00073 }
00074 
00081 void DecreaseBuildingCount(Town *t, HouseID house_id)
00082 {
00083   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00084 
00085   if (!_loaded_newgrf_features.has_newhouses) return;
00086 
00087   if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--;
00088   if (_building_counts.id_count[house_id] > 0)   _building_counts.id_count[house_id]--;
00089 
00090   if (class_id == HOUSE_NO_CLASS) return;
00091 
00092   if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
00093   if (_building_counts.class_count[class_id] > 0)   _building_counts.class_count[class_id]--;
00094 }
00095 
00096 static uint32 HouseGetRandomBits(const ResolverObject *object)
00097 {
00098   /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
00099   TileIndex tile = object->u.house.tile;
00100   assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00101   return object->u.house.not_yet_constructed ? object->u.house.initial_random_bits : GetHouseRandomBits(tile);
00102 }
00103 
00104 static uint32 HouseGetTriggers(const ResolverObject *object)
00105 {
00106   /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
00107   TileIndex tile = object->u.house.tile;
00108   assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00109   return object->u.house.not_yet_constructed ? 0 : GetHouseTriggers(tile);
00110 }
00111 
00112 static void HouseSetTriggers(const ResolverObject *object, int triggers)
00113 {
00114   TileIndex tile = object->u.house.tile;
00115   assert(!object->u.house.not_yet_constructed && IsValidTile(tile) && IsTileType(tile, MP_HOUSE));
00116   SetHouseTriggers(tile, triggers);
00117 }
00118 
00119 static uint32 GetNumHouses(HouseID house_id, const Town *town)
00120 {
00121   uint8 map_id_count, town_id_count, map_class_count, town_class_count;
00122   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00123 
00124   map_id_count     = ClampU(_building_counts.id_count[house_id], 0, 255);
00125   map_class_count  = ClampU(_building_counts.class_count[class_id], 0, 255);
00126   town_id_count    = ClampU(town->building_counts.id_count[house_id], 0, 255);
00127   town_class_count = ClampU(town->building_counts.class_count[class_id], 0, 255);
00128 
00129   return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
00130 }
00131 
00139 static uint32 GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8)
00140 {
00141   tile = GetNearbyTile(parameter, tile);
00142   return GetNearbyTileInformation(tile, grf_version8);
00143 }
00144 
00146 typedef struct {
00147   const HouseSpec *hs;  
00148   TileIndex north_tile; 
00149 } SearchNearbyHouseData;
00150 
00157 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
00158 {
00159   if (IsTileType(tile, MP_HOUSE)) {
00160     HouseID house = GetHouseType(tile); // tile been examined
00161     const HouseSpec *hs = HouseSpec::Get(house);
00162     if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
00163       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00164 
00165       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00166       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00167 
00168       return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id &&  // same local id as the one requested
00169         hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;  // from the same grf
00170     }
00171   }
00172   return false;
00173 }
00174 
00181 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
00182 {
00183   if (IsTileType(tile, MP_HOUSE)) {
00184     HouseID house = GetHouseType(tile); // tile been examined
00185     const HouseSpec *hs = HouseSpec::Get(house);
00186     if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
00187       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00188 
00189       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00190       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00191 
00192       return hs->class_id == nbhd->hs->class_id &&  // same classid as the one requested
00193         hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;  // from the same grf
00194     }
00195   }
00196   return false;
00197 }
00198 
00205 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
00206 {
00207   if (IsTileType(tile, MP_HOUSE)) {
00208     HouseID house = GetHouseType(tile); // tile been examined
00209     const HouseSpec *hs = HouseSpec::Get(house);
00210     if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
00211       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00212 
00213       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00214       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00215 
00216       return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;  // from the same grf
00217     }
00218   }
00219   return false;
00220 }
00221 
00232 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
00233 {
00234   static TestTileOnSearchProc * const search_procs[3] = {
00235     SearchNearbyHouseID,
00236     SearchNearbyHouseClass,
00237     SearchNearbyHouseGRFID,
00238   };
00239   TileIndex found_tile = tile;
00240   uint8 searchtype = GB(parameter, 6, 2);
00241   uint8 searchradius = GB(parameter, 0, 6);
00242   if (searchtype >= lengthof(search_procs)) return 0;  // do not run on ill-defined code
00243   if (searchradius < 1) return 0; // do not use a too low radius
00244 
00245   SearchNearbyHouseData nbhd;
00246   nbhd.hs = HouseSpec::Get(house);
00247   nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00248 
00249   /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
00250   if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
00251     return DistanceManhattan(found_tile, tile);
00252   }
00253   return 0;
00254 }
00255 
00261 static uint32 HouseGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00262 {
00263   Town *town = object->u.house.town;
00264   TileIndex tile   = object->u.house.tile;
00265   HouseID house_id = object->u.house.house_id;
00266 
00267   if (object->scope == VSG_SCOPE_PARENT) {
00268     return TownGetVariable(variable, parameter, available, town, object->grffile);
00269   }
00270 
00271   switch (variable) {
00272     /* Construction stage. */
00273     case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
00274 
00275     /* Building age. */
00276     case 0x41: return IsTileType(tile, MP_HOUSE) ? GetHouseAge(tile) : 0;
00277 
00278     /* Town zone */
00279     case 0x42: return GetTownRadiusGroup(town, tile);
00280 
00281     /* Terrain type */
00282     case 0x43: return GetTerrainType(tile);
00283 
00284     /* Number of this type of building on the map. */
00285     case 0x44: return GetNumHouses(house_id, town);
00286 
00287     /* Whether the town is being created or just expanded. */
00288     case 0x45: return _generating_world ? 1 : 0;
00289 
00290     /* Current animation frame. */
00291     case 0x46: return IsTileType(tile, MP_HOUSE) ? GetAnimationFrame(tile) : 0;
00292 
00293     /* Position of the house */
00294     case 0x47: return TileY(tile) << 16 | TileX(tile);
00295 
00296     /* Building counts for old houses with id = parameter. */
00297     case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, town) : 0;
00298 
00299     /* Building counts for new houses with id = parameter. */
00300     case 0x61: {
00301       const HouseSpec *hs = HouseSpec::Get(house_id);
00302       if (hs->grf_prop.grffile == NULL) return 0;
00303 
00304       HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
00305       return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
00306     }
00307 
00308     /* Land info for nearby tiles. */
00309     case 0x62: return GetNearbyTileInformation(parameter, tile, object->grffile->grf_version >= 8);
00310 
00311     /* Current animation frame of nearby house tiles */
00312     case 0x63: {
00313       TileIndex testtile = GetNearbyTile(parameter, tile);
00314       return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
00315     }
00316 
00317     /* Cargo acceptance history of nearby stations */
00318     case 0x64: {
00319       CargoID cid = GetCargoTranslation(parameter, object->grffile);
00320       if (cid == CT_INVALID) return 0;
00321 
00322       /* Extract tile offset. */
00323       int8 x_offs = GB(GetRegister(0x100), 0, 8);
00324       int8 y_offs = GB(GetRegister(0x100), 8, 8);
00325       TileIndex testtile = TILE_MASK(tile + TileDiffXY(x_offs, y_offs));
00326 
00327       StationFinder stations(TileArea(testtile, 1, 1));
00328       const StationList *sl = stations.GetStations();
00329 
00330       /* Collect acceptance stats. */
00331       uint32 res = 0;
00332       for (Station * const * st_iter = sl->Begin(); st_iter != sl->End(); st_iter++) {
00333         const Station *st = *st_iter;
00334         if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_EVER_ACCEPTED))    SetBit(res, 0);
00335         if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_LAST_MONTH))       SetBit(res, 1);
00336         if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_CURRENT_MONTH))    SetBit(res, 2);
00337         if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3);
00338       }
00339 
00340       /* Cargo triggered CB 148? */
00341       if (HasBit(object->u.house.watched_cargo_triggers, cid)) SetBit(res, 4);
00342 
00343       return res;
00344     }
00345 
00346     /* Distance test for some house types */
00347     case 0x65: return GetDistanceFromNearbyHouse(parameter, tile, object->u.house.house_id);
00348 
00349     /* Class and ID of nearby house tile */
00350     case 0x66: {
00351       TileIndex testtile = GetNearbyTile(parameter, tile);
00352       if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00353       HouseSpec *hs = HouseSpec::Get(GetHouseType(testtile));
00354       /* Information about the grf local classid if the house has a class */
00355       uint houseclass = 0;
00356       if (hs->class_id != HOUSE_NO_CLASS) {
00357         houseclass = (hs->grf_prop.grffile == object->grffile ? 1 : 2) << 8;
00358         houseclass |= _class_mapping[hs->class_id].class_id;
00359       }
00360       /* old house type or grf-local houseid */
00361       uint local_houseid = 0;
00362       if (house_id < NEW_HOUSE_OFFSET) {
00363         local_houseid = house_id;
00364       } else {
00365         local_houseid = (hs->grf_prop.grffile == object->grffile ? 1 : 2) << 8;
00366         local_houseid |= hs->grf_prop.local_id;
00367       }
00368       return houseclass << 16 | local_houseid;
00369     }
00370 
00371     /* GRFID of nearby house tile */
00372     case 0x67: {
00373       TileIndex testtile = GetNearbyTile(parameter, tile);
00374       if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00375       HouseID house_id = GetHouseType(testtile);
00376       if (house_id < NEW_HOUSE_OFFSET) return 0;
00377       /* Checking the grffile information via HouseSpec doesn't work
00378        * in case the newgrf was removed. */
00379       return _house_mngr.GetGRFID(house_id);
00380     }
00381   }
00382 
00383   DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
00384 
00385   *available = false;
00386   return UINT_MAX;
00387 }
00388 
00389 static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00390 {
00391   /* Houses do not have 'real' groups */
00392   return NULL;
00393 }
00394 
00401 void HouseStorePSA(ResolverObject *object, uint pos, int32 value)
00402 {
00403   /* Houses have no persistent storage. */
00404   if (object->scope != VSG_SCOPE_PARENT || object->u.house.not_yet_constructed) return;
00405 
00406   /* Pass the request on to the town of the house */
00407   TownStorePSA(object->u.house.town, object->grffile, pos, value);
00408 }
00409 
00415 static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town)
00416 {
00417   res->GetRandomBits = HouseGetRandomBits;
00418   res->GetTriggers   = HouseGetTriggers;
00419   res->SetTriggers   = HouseSetTriggers;
00420   res->GetVariable   = HouseGetVariable;
00421   res->ResolveReal   = HouseResolveReal;
00422   res->StorePSA      = HouseStorePSA;
00423 
00424   res->u.house.tile     = tile;
00425   res->u.house.town     = town;
00426   res->u.house.house_id = house_id;
00427   res->u.house.not_yet_constructed = false;
00428 
00429   res->callback        = CBID_NO_CALLBACK;
00430   res->callback_param1 = 0;
00431   res->callback_param2 = 0;
00432   res->ResetState();
00433 
00434   const HouseSpec *hs  = HouseSpec::Get(house_id);
00435   res->grffile         = (hs != NULL ? hs->grf_prop.grffile : NULL);
00436 }
00437 
00438 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile, bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
00439 {
00440   ResolverObject object;
00441   const SpriteGroup *group;
00442 
00443   assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00444 
00445   NewHouseResolver(&object, house_id, tile, town);
00446   object.callback = callback;
00447   object.callback_param1 = param1;
00448   object.callback_param2 = param2;
00449   object.u.house.not_yet_constructed = not_yet_constructed;
00450   object.u.house.initial_random_bits = initial_random_bits;
00451   object.u.house.watched_cargo_triggers = watched_cargo_triggers;
00452 
00453   group = SpriteGroup::Resolve(HouseSpec::Get(house_id)->grf_prop.spritegroup[0], &object);
00454   if (group == NULL) return CALLBACK_FAILED;
00455 
00456   return group->GetCallbackResult();
00457 }
00458 
00459 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
00460 {
00461   const DrawTileSprites *dts = group->ProcessRegisters(&stage);
00462 
00463   const HouseSpec *hs = HouseSpec::Get(house_id);
00464   PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
00465   if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
00466     uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00467     if (callback != CALLBACK_FAILED) {
00468       /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
00469       palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
00470     }
00471   }
00472 
00473   SpriteID image = dts->ground.sprite;
00474   PaletteID pal  = dts->ground.pal;
00475 
00476   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00477   if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
00478 
00479   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00480     DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00481   }
00482 
00483   DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
00484 }
00485 
00486 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
00487 {
00488   const HouseSpec *hs = HouseSpec::Get(house_id);
00489   const SpriteGroup *group;
00490   ResolverObject object;
00491 
00492   if (ti->tileh != SLOPE_FLAT) {
00493     bool draw_old_one = true;
00494     if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
00495       /* Called to determine the type (if any) of foundation to draw for the house tile */
00496       uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00497       if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
00498     }
00499 
00500     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00501   }
00502 
00503   NewHouseResolver(&object, house_id, ti->tile, Town::GetByTile(ti->tile));
00504 
00505   group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
00506   if (group == NULL || group->type != SGT_TILELAYOUT) {
00507     return;
00508   } else {
00509     /* Limit the building stage to the number of stages supplied. */
00510     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00511     byte stage = GetHouseBuildingStage(ti->tile);
00512     DrawTileLayout(ti, tlgroup, stage, house_id);
00513   }
00514 }
00515 
00516 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00517 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, Town *town, TileIndex tile, uint32 extra_data)
00518 {
00519   return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
00520 }
00521 
00523 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, uint32, GetSimpleHouseCallback> {
00524   static const CallbackID cb_animation_speed      = CBID_HOUSE_ANIMATION_SPEED;
00525   static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
00526 
00527   static const HouseCallbackMask cbm_animation_speed      = CBM_HOUSE_ANIMATION_SPEED;
00528   static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
00529 };
00530 
00531 void AnimateNewHouseTile(TileIndex tile)
00532 {
00533   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00534   if (hs == NULL) return;
00535 
00536   HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
00537 }
00538 
00539 void AnimateNewHouseConstruction(TileIndex tile)
00540 {
00541   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00542 
00543   if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
00544     HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
00545   }
00546 }
00547 
00548 bool CanDeleteHouse(TileIndex tile)
00549 {
00550   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00551 
00552   /* Humans are always allowed to remove buildings, as is water and
00553    * anyone using the scenario editor. */
00554   if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
00555     return true;
00556   }
00557 
00558   if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
00559     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00560     return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
00561   } else {
00562     return !(hs->extra_flags & BUILDING_IS_PROTECTED);
00563   }
00564 }
00565 
00566 static void AnimationControl(TileIndex tile, uint16 random_bits)
00567 {
00568   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00569 
00570   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00571     uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
00572     HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
00573   }
00574 }
00575 
00576 bool NewHouseTileLoop(TileIndex tile)
00577 {
00578   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00579 
00580   if (GetHouseProcessingTime(tile) > 0) {
00581     DecHouseProcessingTime(tile);
00582     return true;
00583   }
00584 
00585   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
00586   if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
00587 
00588   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00589     /* If this house is marked as having a synchronised callback, all the
00590      * tiles will have the callback called at once, rather than when the
00591      * tile loop reaches them. This should only be enabled for the northern
00592      * tile, or strange things will happen (here, and in TTDPatch). */
00593     if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
00594       uint16 random = GB(Random(), 0, 16);
00595 
00596       if (hs->building_flags & BUILDING_HAS_1_TILE)  AnimationControl(tile, random);
00597       if (hs->building_flags & BUILDING_2_TILES_Y)   AnimationControl(TILE_ADDXY(tile, 0, 1), random);
00598       if (hs->building_flags & BUILDING_2_TILES_X)   AnimationControl(TILE_ADDXY(tile, 1, 0), random);
00599       if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
00600     } else {
00601       AnimationControl(tile, 0);
00602     }
00603   }
00604 
00605   /* Check callback 21, which determines if a house should be destroyed. */
00606   if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
00607     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00608     if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
00609       ClearTownHouse(Town::GetByTile(tile), tile);
00610       return false;
00611     }
00612   }
00613 
00614   SetHouseProcessingTime(tile, hs->processing_time);
00615   MarkTileDirtyByTile(tile);
00616   return true;
00617 }
00618 
00619 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
00620 {
00621   ResolverObject object;
00622 
00623   /* We can't trigger a non-existent building... */
00624   assert(IsTileType(tile, MP_HOUSE));
00625 
00626   HouseID hid = GetHouseType(tile);
00627   HouseSpec *hs = HouseSpec::Get(hid);
00628 
00629   if (hs->grf_prop.spritegroup == NULL) return;
00630 
00631   NewHouseResolver(&object, hid, tile, Town::GetByTile(tile));
00632 
00633   object.callback = CBID_RANDOM_TRIGGER;
00634   object.trigger = trigger;
00635 
00636   const SpriteGroup *group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
00637   if (group == NULL) return;
00638 
00639   byte new_random_bits = Random();
00640   byte random_bits = GetHouseRandomBits(tile);
00641   uint32 reseed = object.GetReseedSum(); // The scope only affects triggers, not the reseeding
00642   random_bits &= ~reseed;
00643   random_bits |= (first ? new_random_bits : base_random) & reseed;
00644   SetHouseRandomBits(tile, random_bits);
00645 
00646   switch (trigger) {
00647     case HOUSE_TRIGGER_TILE_LOOP:
00648       /* Random value already set. */
00649       break;
00650 
00651     case HOUSE_TRIGGER_TILE_LOOP_TOP:
00652       if (!first) {
00653         /* The top tile is marked dirty by the usual TileLoop */
00654         MarkTileDirtyByTile(tile);
00655         break;
00656       }
00657       /* Random value of first tile already set. */
00658       if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
00659       if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
00660       if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
00661       break;
00662   }
00663 }
00664 
00665 void TriggerHouse(TileIndex t, HouseTrigger trigger)
00666 {
00667   DoTriggerHouse(t, trigger, 0, true);
00668 }
00669 
00677 void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, uint32 trigger_cargoes, uint16 random)
00678 {
00679   TileIndexDiffC diff = TileIndexToTileIndexDiffC(origin, tile);
00680   uint32 cb_info = random << 16 | (uint8)diff.y << 8 | (uint8)diff.x;
00681   HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes);
00682 }
00683 
00690 void WatchedCargoCallback(TileIndex tile, uint32 trigger_cargoes)
00691 {
00692   assert(IsTileType(tile, MP_HOUSE));
00693   HouseID id = GetHouseType(tile);
00694   const HouseSpec *hs = HouseSpec::Get(id);
00695 
00696   trigger_cargoes &= hs->watched_cargoes;
00697   /* None of the trigger cargoes is watched? */
00698   if (trigger_cargoes == 0) return;
00699 
00700   /* Same random value for all tiles of a multi-tile house. */
00701   uint16 r = Random();
00702 
00703   /* Do the callback, start at northern tile. */
00704   TileIndex north = tile + GetHouseNorthPart(id);
00705   hs = HouseSpec::Get(id);
00706 
00707   DoWatchedCargoCallback(north, tile, trigger_cargoes, r);
00708   if (hs->building_flags & BUILDING_2_TILES_Y)   DoWatchedCargoCallback(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
00709   if (hs->building_flags & BUILDING_2_TILES_X)   DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
00710   if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);
00711 }
00712 
00718 void GetHouseResolver(ResolverObject *ro, uint index)
00719 {
00720   NewHouseResolver(ro, GetHouseType(index), index, Town::GetByTile(index));
00721 }