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