# newgrf_house.cpp

Go to the documentation of this file.

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