newgrf_house.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_house.cpp 19855 2010-05-18 21:30:56Z 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 "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   /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
00103   TileIndex tile = object->u.house.tile;
00104   assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00105   return object->u.house.not_yet_constructed ? 0 : GetHouseRandomBits(tile);
00106 }
00107 
00108 static uint32 HouseGetTriggers(const ResolverObject *object)
00109 {
00110   /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
00111   TileIndex tile = object->u.house.tile;
00112   assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00113   return object->u.house.not_yet_constructed ? 0 : GetHouseTriggers(tile);
00114 }
00115 
00116 static void HouseSetTriggers(const ResolverObject *object, int triggers)
00117 {
00118   TileIndex tile = object->u.house.tile;
00119   assert(!object->u.house.not_yet_constructed && IsValidTile(tile) && IsTileType(tile, MP_HOUSE));
00120   SetHouseTriggers(tile, triggers);
00121 }
00122 
00123 static uint32 GetNumHouses(HouseID house_id, const Town *town)
00124 {
00125   uint8 map_id_count, town_id_count, map_class_count, town_class_count;
00126   HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00127 
00128   map_id_count     = ClampU(_building_counts.id_count[house_id], 0, 255);
00129   map_class_count  = ClampU(_building_counts.class_count[class_id], 0, 255);
00130   town_id_count    = ClampU(town->building_counts.id_count[house_id], 0, 255);
00131   town_class_count = ClampU(town->building_counts.class_count[class_id], 0, 255);
00132 
00133   return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
00134 }
00135 
00136 uint32 GetNearbyTileInformation(byte parameter, TileIndex tile)
00137 {
00138   tile = GetNearbyTile(parameter, tile);
00139   return GetNearbyTileInformation(tile);
00140 }
00141 
00143 typedef struct {
00144   const HouseSpec *hs;  
00145   TileIndex north_tile; 
00146 } SearchNearbyHouseData;
00147 
00153 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
00154 {
00155   if (IsTileType(tile, MP_HOUSE)) {
00156     HouseID house = GetHouseType(tile); // tile been examined
00157     const HouseSpec *hs = HouseSpec::Get(house);
00158     if (hs->grffile != NULL) { // must be one from a grf file
00159       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00160 
00161       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00162       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00163 
00164       return hs->local_id == nbhd->hs->local_id &&  // same local id as the one requested
00165         hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
00166     }
00167   }
00168   return false;
00169 }
00170 
00176 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
00177 {
00178   if (IsTileType(tile, MP_HOUSE)) {
00179     HouseID house = GetHouseType(tile); // tile been examined
00180     const HouseSpec *hs = HouseSpec::Get(house);
00181     if (hs->grffile != NULL) { // must be one from a grf file
00182       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00183 
00184       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00185       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00186 
00187       return hs->class_id == nbhd->hs->class_id &&  // same classid as the one requested
00188         hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
00189     }
00190   }
00191   return false;
00192 }
00193 
00199 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
00200 {
00201   if (IsTileType(tile, MP_HOUSE)) {
00202     HouseID house = GetHouseType(tile); // tile been examined
00203     const HouseSpec *hs = HouseSpec::Get(house);
00204     if (hs->grffile != NULL) { // must be one from a grf file
00205       SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00206 
00207       TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00208       if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
00209 
00210       return hs->grffile->grfid == nbhd->hs->grffile->grfid;  // from the same grf
00211     }
00212   }
00213   return false;
00214 }
00215 
00225 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
00226 {
00227   static TestTileOnSearchProc * const search_procs[3] = {
00228     SearchNearbyHouseID,
00229     SearchNearbyHouseClass,
00230     SearchNearbyHouseGRFID,
00231   };
00232   TileIndex found_tile = tile;
00233   uint8 searchtype = GB(parameter, 6, 2);
00234   uint8 searchradius = GB(parameter, 0, 6);
00235   if (searchtype >= lengthof(search_procs)) return 0;  // do not run on ill-defined code
00236   if (searchradius < 1) return 0; // do not use a too low radius
00237 
00238   SearchNearbyHouseData nbhd;
00239   nbhd.hs = HouseSpec::Get(house);
00240   nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
00241 
00242   /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
00243   if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
00244     return DistanceManhattan(found_tile, tile);
00245   }
00246   return 0;
00247 }
00248 
00254 static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00255 {
00256   const Town *town = object->u.house.town;
00257   TileIndex tile   = object->u.house.tile;
00258   HouseID house_id = object->u.house.house_id;
00259 
00260   if (object->scope == VSG_SCOPE_PARENT) {
00261     return TownGetVariable(variable, parameter, available, town);
00262   }
00263 
00264   switch (variable) {
00265     /* Construction stage. */
00266     case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
00267 
00268     /* Building age. */
00269     case 0x41: return IsTileType(tile, MP_HOUSE) ? GetHouseAge(tile) : 0;
00270 
00271     /* Town zone */
00272     case 0x42: return GetTownRadiusGroup(town, tile);
00273 
00274     /* Terrain type */
00275     case 0x43: return GetTerrainType(tile);
00276 
00277     /* Number of this type of building on the map. */
00278     case 0x44: return GetNumHouses(house_id, town);
00279 
00280     /* Whether the town is being created or just expanded. */
00281     case 0x45: return _generating_world ? 1 : 0;
00282 
00283     /* Current animation frame. */
00284     case 0x46: return IsTileType(tile, MP_HOUSE) ? GetHouseAnimationFrame(tile) : 0;
00285 
00286     /* Position of the house */
00287     case 0x47: return TileY(tile) << 16 | TileX(tile);
00288 
00289     /* Building counts for old houses with id = parameter. */
00290     case 0x60: return GetNumHouses(parameter, town);
00291 
00292     /* Building counts for new houses with id = parameter. */
00293     case 0x61: {
00294       const HouseSpec *hs = HouseSpec::Get(house_id);
00295       if (hs->grffile == NULL) return 0;
00296 
00297       HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid);
00298       return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
00299     }
00300 
00301     /* Land info for nearby tiles. */
00302     case 0x62: return GetNearbyTileInformation(parameter, tile);
00303 
00304     /* Current animation frame of nearby house tiles */
00305     case 0x63: {
00306       TileIndex testtile = GetNearbyTile(parameter, tile);
00307       return IsTileType(testtile, MP_HOUSE) ? GetHouseAnimationFrame(testtile) : 0;
00308     }
00309 
00310     /* Cargo acceptance history of nearby stations */
00311     /*case 0x64: not implemented yet */
00312 
00313     /* Distance test for some house types */
00314     case 0x65: return GetDistanceFromNearbyHouse(parameter, tile, object->u.house.house_id);
00315 
00316     /* Class and ID of nearby house tile */
00317     case 0x66: {
00318       TileIndex testtile = GetNearbyTile(parameter, tile);
00319       if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00320       HouseSpec *hs = HouseSpec::Get(GetHouseType(testtile));
00321       /* Information about the grf local classid if the house has a class */
00322       uint houseclass = 0;
00323       if (hs->class_id != HOUSE_NO_CLASS) {
00324         houseclass = (hs->grffile == object->grffile ? 1 : 2) << 8;
00325         houseclass |= _class_mapping[hs->class_id].class_id;
00326       }
00327       /* old house type or grf-local houseid */
00328       uint local_houseid = 0;
00329       if (house_id < NEW_HOUSE_OFFSET) {
00330         local_houseid = house_id;
00331       } else {
00332         local_houseid = (hs->grffile == object->grffile ? 1 : 2) << 8;
00333         local_houseid |= hs->local_id;
00334       }
00335       return houseclass << 16 | local_houseid;
00336     }
00337 
00338     /* GRFID of nearby house tile */
00339     case 0x67: {
00340       TileIndex testtile = GetNearbyTile(parameter, tile);
00341       if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00342       HouseID house_id = GetHouseType(testtile);
00343       if (house_id < NEW_HOUSE_OFFSET) return 0;
00344       /* Checking the grffile information via HouseSpec doesn't work
00345        * in case the newgrf was removed. */
00346       return _house_mngr.mapping_ID[house_id].grfid;
00347     }
00348   }
00349 
00350   DEBUG(grf, 1, "Unhandled house property 0x%X", variable);
00351 
00352   *available = false;
00353   return UINT_MAX;
00354 }
00355 
00356 static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00357 {
00358   /* Houses do not have 'real' groups */
00359   return NULL;
00360 }
00361 
00367 static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town)
00368 {
00369   res->GetRandomBits = HouseGetRandomBits;
00370   res->GetTriggers   = HouseGetTriggers;
00371   res->SetTriggers   = HouseSetTriggers;
00372   res->GetVariable   = HouseGetVariable;
00373   res->ResolveReal   = HouseResolveReal;
00374 
00375   res->u.house.tile     = tile;
00376   res->u.house.town     = town;
00377   res->u.house.house_id = house_id;
00378   res->u.house.not_yet_constructed = false;
00379 
00380   res->callback        = CBID_NO_CALLBACK;
00381   res->callback_param1 = 0;
00382   res->callback_param2 = 0;
00383   res->last_value      = 0;
00384   res->trigger         = 0;
00385   res->reseed          = 0;
00386   res->count           = 0;
00387 
00388   const HouseSpec *hs = HouseSpec::Get(house_id);
00389   res->grffile         = (hs != NULL ? hs->grffile : NULL);
00390 }
00391 
00392 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile, bool not_yet_constructed)
00393 {
00394   ResolverObject object;
00395   const SpriteGroup *group;
00396 
00397   assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00398 
00399   NewHouseResolver(&object, house_id, tile, town);
00400   object.callback = callback;
00401   object.callback_param1 = param1;
00402   object.callback_param2 = param2;
00403   object.u.house.not_yet_constructed = not_yet_constructed;
00404 
00405   group = SpriteGroup::Resolve(HouseSpec::Get(house_id)->spritegroup, &object);
00406   if (group == NULL) return CALLBACK_FAILED;
00407 
00408   return group->GetCallbackResult();
00409 }
00410 
00411 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
00412 {
00413   const DrawTileSprites *dts = group->dts;
00414 
00415   const HouseSpec *hs = HouseSpec::Get(house_id);
00416   PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
00417   if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
00418     uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00419     if (callback != CALLBACK_FAILED) {
00420       /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
00421       palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
00422     }
00423   }
00424 
00425   SpriteID image = dts->ground.sprite;
00426   PaletteID pal  = dts->ground.pal;
00427 
00428   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00429 
00430   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00431     DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00432   }
00433 
00434   DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
00435 }
00436 
00437 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
00438 {
00439   const HouseSpec *hs = HouseSpec::Get(house_id);
00440   const SpriteGroup *group;
00441   ResolverObject object;
00442 
00443   if (ti->tileh != SLOPE_FLAT) {
00444     bool draw_old_one = true;
00445     if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
00446       /* Called to determine the type (if any) of foundation to draw for the house tile */
00447       uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00448       draw_old_one = (callback_res != 0);
00449     }
00450 
00451     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00452   }
00453 
00454   NewHouseResolver(&object, house_id, ti->tile, Town::GetByTile(ti->tile));
00455 
00456   group = SpriteGroup::Resolve(hs->spritegroup, &object);
00457   if (group == NULL || group->type != SGT_TILELAYOUT) {
00458     return;
00459   } else {
00460     /* Limit the building stage to the number of stages supplied. */
00461     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00462     byte stage = GetHouseBuildingStage(ti->tile);
00463     stage = Clamp(stage - 4 + tlgroup->num_building_stages, 0, tlgroup->num_building_stages - 1);
00464     DrawTileLayout(ti, tlgroup, stage, house_id);
00465   }
00466 }
00467 
00468 void AnimateNewHouseTile(TileIndex tile)
00469 {
00470   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00471   byte animation_speed = hs->animation_speed;
00472   bool frame_set_by_callback = false;
00473 
00474   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) {
00475     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00476     if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16);
00477   }
00478 
00479   /* An animation speed of 2 means the animation frame changes 4 ticks, and
00480    * increasing this value by one doubles the wait. 2 is the minimum value
00481    * allowed for animation_speed, which corresponds to 120ms, and 16 is the
00482    * maximum, corresponding to around 33 minutes. */
00483   if (_tick_counter % (1 << animation_speed) != 0) return;
00484 
00485   byte frame      = GetHouseAnimationFrame(tile);
00486   byte num_frames = GB(hs->animation_frames, 0, 7);
00487 
00488   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_NEXT_FRAME)) {
00489     uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0;
00490     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00491 
00492     if (callback_res != CALLBACK_FAILED) {
00493       frame_set_by_callback = true;
00494 
00495       switch (callback_res & 0xFF) {
00496         case 0xFF:
00497           DeleteAnimatedTile(tile);
00498           break;
00499         case 0xFE:
00500           /* Carry on as normal. */
00501           frame_set_by_callback = false;
00502           break;
00503         default:
00504           frame = callback_res & 0xFF;
00505           break;
00506       }
00507 
00508       /* If the lower 7 bits of the upper byte of the callback
00509        * result are not empty, it is a sound effect. */
00510       if (GB(callback_res, 8, 7) != 0) PlayTileSound(hs->grffile, GB(callback_res, 8, 7), tile);
00511     }
00512   }
00513 
00514   if (!frame_set_by_callback) {
00515     if (frame < num_frames) {
00516       frame++;
00517     } else if (frame == num_frames && HasBit(hs->animation_frames, 7)) {
00518       /* This animation loops, so start again from the beginning */
00519       frame = 0;
00520     } else {
00521       /* This animation doesn't loop, so stay here */
00522       DeleteAnimatedTile(tile);
00523     }
00524   }
00525 
00526   SetHouseAnimationFrame(tile, frame);
00527   MarkTileDirtyByTile(tile);
00528 }
00529 
00530 void ChangeHouseAnimationFrame(const GRFFile *file, TileIndex tile, uint16 callback_result)
00531 {
00532   switch (callback_result & 0xFF) {
00533     case 0xFD: /* Do nothing. */         break;
00534     case 0xFE: AddAnimatedTile(tile);    break;
00535     case 0xFF: DeleteAnimatedTile(tile); break;
00536     default:
00537       SetHouseAnimationFrame(tile, callback_result & 0xFF);
00538       AddAnimatedTile(tile);
00539       break;
00540   }
00541   /* If the lower 7 bits of the upper byte of the callback
00542    * result are not empty, it is a sound effect. */
00543   if (GB(callback_result, 8, 7) != 0) PlayTileSound(file, GB(callback_result, 8, 7), tile);
00544 }
00545 
00546 bool CanDeleteHouse(TileIndex tile)
00547 {
00548   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00549 
00550   /* Humans are always allowed to remove buildings, as is water and
00551    * anyone using the scenario editor. */
00552   if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
00553     return true;
00554   }
00555 
00556   if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
00557     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00558     return (callback_res == CALLBACK_FAILED || callback_res == 0);
00559   } else {
00560     return !(hs->extra_flags & BUILDING_IS_PROTECTED);
00561   }
00562 }
00563 
00564 static void AnimationControl(TileIndex tile, uint16 random_bits)
00565 {
00566   const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00567 
00568   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00569     uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
00570     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00571 
00572     if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
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 && GB(callback_res, 0, 8) > 0) {
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->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->spritegroup, &object);
00637   if (group == NULL) return;
00638 
00639   byte new_random_bits = Random();
00640   byte random_bits = GetHouseRandomBits(tile);
00641   random_bits &= ~object.reseed;
00642   random_bits |= (first ? new_random_bits : base_random) & object.reseed;
00643   SetHouseRandomBits(tile, random_bits);
00644 
00645   switch (trigger) {
00646     case HOUSE_TRIGGER_TILE_LOOP:
00647       /* Random value already set. */
00648       break;
00649 
00650     case HOUSE_TRIGGER_TILE_LOOP_TOP:
00651       if (!first) {
00652         /* The top tile is marked dirty by the usual TileLoop */
00653         MarkTileDirtyByTile(tile);
00654         break;
00655       }
00656       /* Random value of first tile already set. */
00657       if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
00658       if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
00659       if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
00660       break;
00661   }
00662 }
00663 
00664 void TriggerHouse(TileIndex t, HouseTrigger trigger)
00665 {
00666   DoTriggerHouse(t, trigger, 0, true);
00667 }

Generated on Sat Jul 31 21:37:49 2010 for OpenTTD by  doxygen 1.6.1