# newgrf_house.cpp

Go to the documentation of this file.

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