newgrf_house.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_house.cpp 15428 2009-02-09 02:57:15Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "variables.h"
00008 #include "debug.h"
00009 #include "viewport_func.h"
00010 #include "landscape.h"
00011 #include "town_map.h"
00012 #include "sprite.h"
00013 #include "newgrf.h"
00014 #include "newgrf_house.h"
00015 #include "newgrf_spritegroup.h"
00016 #include "newgrf_town.h"
00017 #include "newgrf_sound.h"
00018 #include "newgrf_commons.h"
00019 #include "transparency.h"
00020 #include "functions.h"
00021 #include "company_func.h"
00022 #include "animated_tile_func.h"
00023 #include "company_base.h"
00024 
00025 static BuildingCounts    _building_counts;
00026 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
00027 
00028 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
00029 
00030 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
00031 {
00032   /* Start from 1 because 0 means that no class has been assigned. */
00033   for (int i = 1; i != lengthof(_class_mapping); i++) {
00034     HouseClassMapping *map = &_class_mapping[i];
00035 
00036     if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
00037 
00038     if (map->class_id == 0 && map->grfid == 0) {
00039       map->class_id = grf_class_id;
00040       map->grfid    = grfid;
00041       return (HouseClassID)i;
00042     }
00043   }
00044   return HOUSE_NO_CLASS;
00045 }
00046 
00047 void InitializeBuildingCounts()
00048 {
00049   memset(&_building_counts, 0, sizeof(_building_counts));
00050 }
00051 
00058 void IncreaseBuildingCount(Town *t, HouseID house_id)
00059 {
00060   HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
00061 
00062   if (!_loaded_newgrf_features.has_newhouses) return;
00063 
00064   /* If there are 255 buildings of this type in this town, there are also
00065    * at least that many houses of the same class in the town, and
00066    * therefore on the map as well. */
00067   if (t->building_counts.id_count[house_id] == 255) return;
00068 
00069   t->building_counts.id_count[house_id]++;
00070   if (_building_counts.id_count[house_id] < 255) _building_counts.id_count[house_id]++;
00071 
00072   /* Similarly, if there are 255 houses of this class in this town, there
00073    * must be at least that number on the map too. */
00074   if (class_id == HOUSE_NO_CLASS || t->building_counts.class_count[class_id] == 255) return;
00075 
00076   t->building_counts.class_count[class_id]++;
00077   if (_building_counts.class_count[class_id] < 255) _building_counts.class_count[class_id]++;
00078 }
00079 
00086 void DecreaseBuildingCount(Town *t, HouseID house_id)
00087 {
00088   HouseClassID class_id = GetHouseSpecs(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 = GetHouseSpecs(house_id)->class_id;
00123 
00124   map_id_count     = _building_counts.id_count[house_id];
00125   map_class_count  = _building_counts.class_count[class_id];
00126   town_id_count    = town->building_counts.id_count[house_id];
00127   town_class_count = town->building_counts.class_count[class_id];
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 = GetHouseSpecs(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 = GetHouseSpecs(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 = GetHouseSpecs(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 = GetHouseSpecs(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 GetHouseAge(tile);
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 = GetHouseSpecs(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 SpriteGroup *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 = GetHouseSpecs(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 = Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
00365   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00366 
00367   return group->g.callback.result;
00368 }
00369 
00370 static void DrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte stage, HouseID house_id)
00371 {
00372   const DrawTileSprites *dts = group->g.layout.dts;
00373   const DrawTileSeqStruct *dtss;
00374 
00375   const HouseSpec *hs = GetHouseSpecs(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, GetTownByTile(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 = GetHouseSpecs(house_id);
00425   const SpriteGroup *group;
00426   ResolverObject object;
00427 
00428   if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00429 
00430   NewHouseResolver(&object, house_id, ti->tile, GetTownByTile(ti->tile));
00431 
00432   group = Resolve(hs->spritegroup, &object);
00433   if (group == NULL || group->type != SGT_TILELAYOUT) {
00434     /* XXX: This is for debugging purposes really, and shouldn't stay. */
00435     DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE);
00436   } else {
00437     /* Limit the building stage to the number of stages supplied. */
00438     byte stage = GetHouseBuildingStage(ti->tile);
00439     stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
00440     DrawTileLayout(ti, group, stage, house_id);
00441   }
00442 }
00443 
00444 void AnimateNewHouseTile(TileIndex tile)
00445 {
00446   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00447   byte animation_speed = hs->animation_speed;
00448   bool frame_set_by_callback = false;
00449 
00450   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) {
00451     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00452     if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16);
00453   }
00454 
00455   /* An animation speed of 2 means the animation frame changes 4 ticks, and
00456    * increasing this value by one doubles the wait. 2 is the minimum value
00457    * allowed for animation_speed, which corresponds to 120ms, and 16 is the
00458    * maximum, corresponding to around 33 minutes. */
00459   if (_tick_counter % (1 << animation_speed) != 0) return;
00460 
00461   byte frame      = GetHouseAnimationFrame(tile);
00462   byte num_frames = GB(hs->animation_frames, 0, 7);
00463 
00464   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_NEXT_FRAME)) {
00465     uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0;
00466     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00467 
00468     if (callback_res != CALLBACK_FAILED) {
00469       frame_set_by_callback = true;
00470 
00471       switch (callback_res & 0xFF) {
00472         case 0xFF:
00473           DeleteAnimatedTile(tile);
00474           break;
00475         case 0xFE:
00476           /* Carry on as normal. */
00477           frame_set_by_callback = false;
00478           break;
00479         default:
00480           frame = callback_res & 0xFF;
00481           break;
00482       }
00483 
00484       /* If the lower 7 bits of the upper byte of the callback
00485        * result are not empty, it is a sound effect. */
00486       if (GB(callback_res, 8, 7) != 0) PlayTileSound(hs->grffile, GB(callback_res, 8, 7), tile);
00487     }
00488   }
00489 
00490   if (!frame_set_by_callback) {
00491     if (frame < num_frames) {
00492       frame++;
00493     } else if (frame == num_frames && HasBit(hs->animation_frames, 7)) {
00494       /* This animation loops, so start again from the beginning */
00495       frame = 0;
00496     } else {
00497       /* This animation doesn't loop, so stay here */
00498       DeleteAnimatedTile(tile);
00499     }
00500   }
00501 
00502   SetHouseAnimationFrame(tile, frame);
00503   MarkTileDirtyByTile(tile);
00504 }
00505 
00506 void ChangeHouseAnimationFrame(const GRFFile *file, TileIndex tile, uint16 callback_result)
00507 {
00508   switch (callback_result & 0xFF) {
00509     case 0xFD: /* Do nothing. */         break;
00510     case 0xFE: AddAnimatedTile(tile);    break;
00511     case 0xFF: DeleteAnimatedTile(tile); break;
00512     default:
00513       SetHouseAnimationFrame(tile, callback_result & 0xFF);
00514       AddAnimatedTile(tile);
00515       break;
00516   }
00517   /* If the lower 7 bits of the upper byte of the callback
00518    * result are not empty, it is a sound effect. */
00519   if (GB(callback_result, 8, 7) != 0) PlayTileSound(file, GB(callback_result, 8, 7), tile);
00520 }
00521 
00522 bool CanDeleteHouse(TileIndex tile)
00523 {
00524   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00525 
00526   /* Humans are always allowed to remove buildings, as is water and
00527    * anyone using the scenario editor. */
00528   if ((IsValidCompanyID(_current_company) && IsHumanCompany(_current_company))
00529       || _current_company == OWNER_WATER || _current_company == OWNER_NONE) return true;
00530 
00531   if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
00532     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00533     return (callback_res == CALLBACK_FAILED || callback_res == 0);
00534   } else {
00535     return !(hs->extra_flags & BUILDING_IS_PROTECTED);
00536   }
00537 }
00538 
00539 static void AnimationControl(TileIndex tile, uint16 random_bits)
00540 {
00541   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00542 
00543   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00544     uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
00545     uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00546 
00547     if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
00548   }
00549 }
00550 
00551 bool NewHouseTileLoop(TileIndex tile)
00552 {
00553   const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00554 
00555   if (GetHouseProcessingTime(tile) > 0) {
00556     DecHouseProcessingTime(tile);
00557     return true;
00558   }
00559 
00560   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
00561   TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
00562 
00563   if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00564     /* If this house is marked as having a synchronised callback, all the
00565      * tiles will have the callback called at once, rather than when the
00566      * tile loop reaches them. This should only be enabled for the northern
00567      * tile, or strange things will happen (here, and in TTDPatch). */
00568     if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
00569       uint16 random = GB(Random(), 0, 16);
00570 
00571       if (hs->building_flags & BUILDING_HAS_1_TILE)  AnimationControl(tile, random);
00572       if (hs->building_flags & BUILDING_2_TILES_Y)   AnimationControl(TILE_ADDXY(tile, 0, 1), random);
00573       if (hs->building_flags & BUILDING_2_TILES_X)   AnimationControl(TILE_ADDXY(tile, 1, 0), random);
00574       if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
00575     } else {
00576       AnimationControl(tile, 0);
00577     }
00578   }
00579 
00580   /* Check callback 21, which determines if a house should be destroyed. */
00581   if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
00582     uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00583     if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) > 0) {
00584       ClearTownHouse(GetTownByTile(tile), tile);
00585       return false;
00586     }
00587   }
00588 
00589   SetHouseProcessingTime(tile, hs->processing_time);
00590   return true;
00591 }
00592 
00593 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
00594 {
00595   ResolverObject object;
00596 
00597   /* We can't trigger a non-existent building... */
00598   assert(IsTileType(tile, MP_HOUSE));
00599 
00600   HouseID hid = GetHouseType(tile);
00601   HouseSpec *hs = GetHouseSpecs(hid);
00602 
00603   if (hs->spritegroup == NULL) return;
00604 
00605   NewHouseResolver(&object, hid, tile, GetTownByTile(tile));
00606 
00607   object.callback = CBID_RANDOM_TRIGGER;
00608   object.trigger = trigger;
00609 
00610   const SpriteGroup *group = Resolve(hs->spritegroup, &object);
00611   if (group == NULL) return;
00612 
00613   byte new_random_bits = Random();
00614   byte random_bits = GetHouseRandomBits(tile);
00615   random_bits &= ~object.reseed;
00616   random_bits |= (first ? new_random_bits : base_random) & object.reseed;
00617   SetHouseRandomBits(tile, random_bits);
00618 
00619   switch (trigger) {
00620     case HOUSE_TRIGGER_TILE_LOOP:
00621       /* Random value already set. */
00622       break;
00623 
00624     case HOUSE_TRIGGER_TILE_LOOP_TOP:
00625       if (!first) break;
00626       /* Random value of first tile already set. */
00627       if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
00628       if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
00629       if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
00630       break;
00631   }
00632 }
00633 
00634 void TriggerHouse(TileIndex t, HouseTrigger trigger)
00635 {
00636   DoTriggerHouse(t, trigger, 0, true);
00637 }

Generated on Mon Feb 16 23:12:08 2009 for openttd by  doxygen 1.5.6