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

Generated on Wed Dec 30 20:40:04 2009 for OpenTTD by  doxygen 1.5.6