00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "viewport_func.h"
00015 #include "landscape.h"
00016 #include "newgrf.h"
00017 #include "newgrf_house.h"
00018 #include "newgrf_spritegroup.h"
00019 #include "newgrf_town.h"
00020 #include "newgrf_sound.h"
00021 #include "company_func.h"
00022 #include "company_base.h"
00023 #include "town.h"
00024 #include "sprite.h"
00025 #include "genworld.h"
00026 #include "newgrf_animation_base.h"
00027 #include "newgrf_cargo.h"
00028 #include "station_base.h"
00029
00030 static BuildingCounts<uint32> _building_counts;
00031 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
00032
00033 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
00034
00035 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
00036 {
00037
00038 for (int i = 1; i != lengthof(_class_mapping); i++) {
00039 HouseClassMapping *map = &_class_mapping[i];
00040
00041 if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
00042
00043 if (map->class_id == 0 && map->grfid == 0) {
00044 map->class_id = grf_class_id;
00045 map->grfid = grfid;
00046 return (HouseClassID)i;
00047 }
00048 }
00049 return HOUSE_NO_CLASS;
00050 }
00051
00052 void InitializeBuildingCounts()
00053 {
00054 memset(&_building_counts, 0, sizeof(_building_counts));
00055 }
00056
00063 void IncreaseBuildingCount(Town *t, HouseID house_id)
00064 {
00065 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00066
00067 if (!_loaded_newgrf_features.has_newhouses) return;
00068
00069 t->building_counts.id_count[house_id]++;
00070 _building_counts.id_count[house_id]++;
00071
00072 if (class_id == HOUSE_NO_CLASS) return;
00073
00074 t->building_counts.class_count[class_id]++;
00075 _building_counts.class_count[class_id]++;
00076 }
00077
00084 void DecreaseBuildingCount(Town *t, HouseID house_id)
00085 {
00086 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00087
00088 if (!_loaded_newgrf_features.has_newhouses) return;
00089
00090 if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--;
00091 if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
00092
00093 if (class_id == HOUSE_NO_CLASS) return;
00094
00095 if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
00096 if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
00097 }
00098
00099 static uint32 HouseGetRandomBits(const ResolverObject *object)
00100 {
00101
00102 TileIndex tile = object->u.house.tile;
00103 assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00104 return object->u.house.not_yet_constructed ? object->u.house.initial_random_bits : GetHouseRandomBits(tile);
00105 }
00106
00107 static uint32 HouseGetTriggers(const ResolverObject *object)
00108 {
00109
00110 TileIndex tile = object->u.house.tile;
00111 assert(IsValidTile(tile) && (object->u.house.not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00112 return object->u.house.not_yet_constructed ? 0 : GetHouseTriggers(tile);
00113 }
00114
00115 static void HouseSetTriggers(const ResolverObject *object, int triggers)
00116 {
00117 TileIndex tile = object->u.house.tile;
00118 assert(!object->u.house.not_yet_constructed && IsValidTile(tile) && IsTileType(tile, MP_HOUSE));
00119 SetHouseTriggers(tile, triggers);
00120 }
00121
00122 static uint32 GetNumHouses(HouseID house_id, const Town *town)
00123 {
00124 uint8 map_id_count, town_id_count, map_class_count, town_class_count;
00125 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
00126
00127 map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255);
00128 map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255);
00129 town_id_count = ClampU(town->building_counts.id_count[house_id], 0, 255);
00130 town_class_count = ClampU(town->building_counts.class_count[class_id], 0, 255);
00131
00132 return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
00133 }
00134
00142 static uint32 GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8)
00143 {
00144 tile = GetNearbyTile(parameter, tile);
00145 return GetNearbyTileInformation(tile, grf_version8);
00146 }
00147
00149 typedef struct {
00150 const HouseSpec *hs;
00151 TileIndex north_tile;
00152 } SearchNearbyHouseData;
00153
00160 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
00161 {
00162 if (IsTileType(tile, MP_HOUSE)) {
00163 HouseID house = GetHouseType(tile);
00164 const HouseSpec *hs = HouseSpec::Get(house);
00165 if (hs->grf_prop.grffile != NULL) {
00166 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00167
00168 TileIndex north_tile = tile + GetHouseNorthPart(house);
00169 if (north_tile == nbhd->north_tile) return false;
00170
00171 return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id &&
00172 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;
00173 }
00174 }
00175 return false;
00176 }
00177
00184 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
00185 {
00186 if (IsTileType(tile, MP_HOUSE)) {
00187 HouseID house = GetHouseType(tile);
00188 const HouseSpec *hs = HouseSpec::Get(house);
00189 if (hs->grf_prop.grffile != NULL) {
00190 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00191
00192 TileIndex north_tile = tile + GetHouseNorthPart(house);
00193 if (north_tile == nbhd->north_tile) return false;
00194
00195 return hs->class_id == nbhd->hs->class_id &&
00196 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;
00197 }
00198 }
00199 return false;
00200 }
00201
00208 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
00209 {
00210 if (IsTileType(tile, MP_HOUSE)) {
00211 HouseID house = GetHouseType(tile);
00212 const HouseSpec *hs = HouseSpec::Get(house);
00213 if (hs->grf_prop.grffile != NULL) {
00214 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
00215
00216 TileIndex north_tile = tile + GetHouseNorthPart(house);
00217 if (north_tile == nbhd->north_tile) return false;
00218
00219 return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid;
00220 }
00221 }
00222 return false;
00223 }
00224
00235 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
00236 {
00237 static TestTileOnSearchProc * const search_procs[3] = {
00238 SearchNearbyHouseID,
00239 SearchNearbyHouseClass,
00240 SearchNearbyHouseGRFID,
00241 };
00242 TileIndex found_tile = tile;
00243 uint8 searchtype = GB(parameter, 6, 2);
00244 uint8 searchradius = GB(parameter, 0, 6);
00245 if (searchtype >= lengthof(search_procs)) return 0;
00246 if (searchradius < 1) return 0;
00247
00248 SearchNearbyHouseData nbhd;
00249 nbhd.hs = HouseSpec::Get(house);
00250 nbhd.north_tile = tile + GetHouseNorthPart(house);
00251
00252
00253 if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
00254 return DistanceManhattan(found_tile, tile);
00255 }
00256 return 0;
00257 }
00258
00264 static uint32 HouseGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00265 {
00266 Town *town = object->u.house.town;
00267 TileIndex tile = object->u.house.tile;
00268 HouseID house_id = object->u.house.house_id;
00269
00270 if (object->scope == VSG_SCOPE_PARENT) {
00271 return TownGetVariable(variable, parameter, available, town, object->grffile);
00272 }
00273
00274 switch (variable) {
00275
00276 case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
00277
00278
00279 case 0x41: return IsTileType(tile, MP_HOUSE) ? GetHouseAge(tile) : 0;
00280
00281
00282 case 0x42: return GetTownRadiusGroup(town, tile);
00283
00284
00285 case 0x43: return GetTerrainType(tile);
00286
00287
00288 case 0x44: return GetNumHouses(house_id, town);
00289
00290
00291 case 0x45: return _generating_world ? 1 : 0;
00292
00293
00294 case 0x46: return IsTileType(tile, MP_HOUSE) ? GetAnimationFrame(tile) : 0;
00295
00296
00297 case 0x47: return TileY(tile) << 16 | TileX(tile);
00298
00299
00300 case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, town) : 0;
00301
00302
00303 case 0x61: {
00304 const HouseSpec *hs = HouseSpec::Get(house_id);
00305 if (hs->grf_prop.grffile == NULL) return 0;
00306
00307 HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
00308 return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
00309 }
00310
00311
00312 case 0x62: return GetNearbyTileInformation(parameter, tile, object->grffile->grf_version >= 8);
00313
00314
00315 case 0x63: {
00316 TileIndex testtile = GetNearbyTile(parameter, tile);
00317 return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
00318 }
00319
00320
00321 case 0x64: {
00322 CargoID cid = GetCargoTranslation(parameter, object->grffile);
00323 if (cid == CT_INVALID) return 0;
00324
00325
00326 int8 x_offs = GB(GetRegister(0x100), 0, 8);
00327 int8 y_offs = GB(GetRegister(0x100), 8, 8);
00328 TileIndex testtile = TILE_MASK(tile + TileDiffXY(x_offs, y_offs));
00329
00330 StationFinder stations(TileArea(testtile, 1, 1));
00331 const StationList *sl = stations.GetStations();
00332
00333
00334 uint32 res = 0;
00335 for (Station * const * st_iter = sl->Begin(); st_iter != sl->End(); st_iter++) {
00336 const Station *st = *st_iter;
00337 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_EVER_ACCEPTED)) SetBit(res, 0);
00338 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_LAST_MONTH)) SetBit(res, 1);
00339 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_CURRENT_MONTH)) SetBit(res, 2);
00340 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3);
00341 }
00342
00343
00344 if (HasBit(object->u.house.watched_cargo_triggers, cid)) SetBit(res, 4);
00345
00346 return res;
00347 }
00348
00349
00350 case 0x65: return GetDistanceFromNearbyHouse(parameter, tile, object->u.house.house_id);
00351
00352
00353 case 0x66: {
00354 TileIndex testtile = GetNearbyTile(parameter, tile);
00355 if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00356 HouseSpec *hs = HouseSpec::Get(GetHouseType(testtile));
00357
00358 uint houseclass = 0;
00359 if (hs->class_id != HOUSE_NO_CLASS) {
00360 houseclass = (hs->grf_prop.grffile == object->grffile ? 1 : 2) << 8;
00361 houseclass |= _class_mapping[hs->class_id].class_id;
00362 }
00363
00364 uint local_houseid = 0;
00365 if (house_id < NEW_HOUSE_OFFSET) {
00366 local_houseid = house_id;
00367 } else {
00368 local_houseid = (hs->grf_prop.grffile == object->grffile ? 1 : 2) << 8;
00369 local_houseid |= hs->grf_prop.local_id;
00370 }
00371 return houseclass << 16 | local_houseid;
00372 }
00373
00374
00375 case 0x67: {
00376 TileIndex testtile = GetNearbyTile(parameter, tile);
00377 if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
00378 HouseID house_id = GetHouseType(testtile);
00379 if (house_id < NEW_HOUSE_OFFSET) return 0;
00380
00381
00382 return _house_mngr.GetGRFID(house_id);
00383 }
00384 }
00385
00386 DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
00387
00388 *available = false;
00389 return UINT_MAX;
00390 }
00391
00392 static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00393 {
00394
00395 return NULL;
00396 }
00397
00404 void HouseStorePSA(ResolverObject *object, uint pos, int32 value)
00405 {
00406
00407 if (object->scope != VSG_SCOPE_PARENT || object->u.house.not_yet_constructed) return;
00408
00409
00410 TownStorePSA(object->u.house.town, object->grffile, pos, value);
00411 }
00412
00418 static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town)
00419 {
00420 res->GetRandomBits = HouseGetRandomBits;
00421 res->GetTriggers = HouseGetTriggers;
00422 res->SetTriggers = HouseSetTriggers;
00423 res->GetVariable = HouseGetVariable;
00424 res->ResolveReal = HouseResolveReal;
00425 res->StorePSA = HouseStorePSA;
00426
00427 res->u.house.tile = tile;
00428 res->u.house.town = town;
00429 res->u.house.house_id = house_id;
00430 res->u.house.not_yet_constructed = false;
00431
00432 res->callback = CBID_NO_CALLBACK;
00433 res->callback_param1 = 0;
00434 res->callback_param2 = 0;
00435 res->ResetState();
00436
00437 const HouseSpec *hs = HouseSpec::Get(house_id);
00438 res->grffile = (hs != NULL ? hs->grf_prop.grffile : NULL);
00439 }
00440
00441 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)
00442 {
00443 ResolverObject object;
00444 const SpriteGroup *group;
00445
00446 assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
00447
00448 NewHouseResolver(&object, house_id, tile, town);
00449 object.callback = callback;
00450 object.callback_param1 = param1;
00451 object.callback_param2 = param2;
00452 object.u.house.not_yet_constructed = not_yet_constructed;
00453 object.u.house.initial_random_bits = initial_random_bits;
00454 object.u.house.watched_cargo_triggers = watched_cargo_triggers;
00455
00456 group = SpriteGroup::Resolve(HouseSpec::Get(house_id)->grf_prop.spritegroup[0], &object);
00457 if (group == NULL) return CALLBACK_FAILED;
00458
00459 return group->GetCallbackResult();
00460 }
00461
00462 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
00463 {
00464 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
00465
00466 const HouseSpec *hs = HouseSpec::Get(house_id);
00467 PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
00468 if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
00469 uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00470 if (callback != CALLBACK_FAILED) {
00471
00472 palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
00473 }
00474 }
00475
00476 SpriteID image = dts->ground.sprite;
00477 PaletteID pal = dts->ground.pal;
00478
00479 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00480 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
00481
00482 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00483 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00484 }
00485
00486 DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
00487 }
00488
00489 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
00490 {
00491 const HouseSpec *hs = HouseSpec::Get(house_id);
00492 const SpriteGroup *group;
00493 ResolverObject object;
00494
00495 if (ti->tileh != SLOPE_FLAT) {
00496 bool draw_old_one = true;
00497 if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
00498
00499 uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
00500 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
00501 }
00502
00503 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00504 }
00505
00506 NewHouseResolver(&object, house_id, ti->tile, Town::GetByTile(ti->tile));
00507
00508 group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
00509 if (group == NULL || group->type != SGT_TILELAYOUT) {
00510 return;
00511 } else {
00512
00513 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00514 byte stage = GetHouseBuildingStage(ti->tile);
00515 DrawTileLayout(ti, tlgroup, stage, house_id);
00516 }
00517 }
00518
00519
00520 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, Town *town, TileIndex tile, uint32 extra_data)
00521 {
00522 return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
00523 }
00524
00526 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, uint32, GetSimpleHouseCallback> {
00527 static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED;
00528 static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
00529
00530 static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED;
00531 static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
00532 };
00533
00534 void AnimateNewHouseTile(TileIndex tile)
00535 {
00536 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00537 if (hs == NULL) return;
00538
00539 HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
00540 }
00541
00542 void AnimateNewHouseConstruction(TileIndex tile)
00543 {
00544 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00545
00546 if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
00547 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
00548 }
00549 }
00550
00551 bool CanDeleteHouse(TileIndex tile)
00552 {
00553 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00554
00555
00556
00557 if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
00558 return true;
00559 }
00560
00561 if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
00562 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00563 return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
00564 } else {
00565 return !(hs->extra_flags & BUILDING_IS_PROTECTED);
00566 }
00567 }
00568
00569 static void AnimationControl(TileIndex tile, uint16 random_bits)
00570 {
00571 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00572
00573 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00574 uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
00575 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
00576 }
00577 }
00578
00579 bool NewHouseTileLoop(TileIndex tile)
00580 {
00581 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
00582
00583 if (GetHouseProcessingTime(tile) > 0) {
00584 DecHouseProcessingTime(tile);
00585 return true;
00586 }
00587
00588 TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
00589 if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
00590
00591 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
00592
00593
00594
00595
00596 if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
00597 uint16 random = GB(Random(), 0, 16);
00598
00599 if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random);
00600 if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random);
00601 if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random);
00602 if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
00603 } else {
00604 AnimationControl(tile, 0);
00605 }
00606 }
00607
00608
00609 if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
00610 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
00611 if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
00612 ClearTownHouse(Town::GetByTile(tile), tile);
00613 return false;
00614 }
00615 }
00616
00617 SetHouseProcessingTime(tile, hs->processing_time);
00618 MarkTileDirtyByTile(tile);
00619 return true;
00620 }
00621
00622 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
00623 {
00624 ResolverObject object;
00625
00626
00627 assert(IsTileType(tile, MP_HOUSE));
00628
00629 HouseID hid = GetHouseType(tile);
00630 HouseSpec *hs = HouseSpec::Get(hid);
00631
00632 if (hs->grf_prop.spritegroup == NULL) return;
00633
00634 NewHouseResolver(&object, hid, tile, Town::GetByTile(tile));
00635
00636 object.callback = CBID_RANDOM_TRIGGER;
00637 object.trigger = trigger;
00638
00639 const SpriteGroup *group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], &object);
00640 if (group == NULL) return;
00641
00642 byte new_random_bits = Random();
00643 byte random_bits = GetHouseRandomBits(tile);
00644 uint32 reseed = object.GetReseedSum();
00645 random_bits &= ~reseed;
00646 random_bits |= (first ? new_random_bits : base_random) & reseed;
00647 SetHouseRandomBits(tile, random_bits);
00648
00649 switch (trigger) {
00650 case HOUSE_TRIGGER_TILE_LOOP:
00651
00652 break;
00653
00654 case HOUSE_TRIGGER_TILE_LOOP_TOP:
00655 if (!first) {
00656
00657 MarkTileDirtyByTile(tile);
00658 break;
00659 }
00660
00661 if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
00662 if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
00663 if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
00664 break;
00665 }
00666 }
00667
00668 void TriggerHouse(TileIndex t, HouseTrigger trigger)
00669 {
00670 DoTriggerHouse(t, trigger, 0, true);
00671 }
00672
00680 void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, uint32 trigger_cargoes, uint16 random)
00681 {
00682 TileIndexDiffC diff = TileIndexToTileIndexDiffC(origin, tile);
00683 uint32 cb_info = random << 16 | (uint8)diff.y << 8 | (uint8)diff.x;
00684 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes);
00685 }
00686
00693 void WatchedCargoCallback(TileIndex tile, uint32 trigger_cargoes)
00694 {
00695 assert(IsTileType(tile, MP_HOUSE));
00696 HouseID id = GetHouseType(tile);
00697 const HouseSpec *hs = HouseSpec::Get(id);
00698
00699 trigger_cargoes &= hs->watched_cargoes;
00700
00701 if (trigger_cargoes == 0) return;
00702
00703
00704 uint16 r = Random();
00705
00706
00707 TileIndex north = tile + GetHouseNorthPart(id);
00708 hs = HouseSpec::Get(id);
00709
00710 DoWatchedCargoCallback(north, tile, trigger_cargoes, r);
00711 if (hs->building_flags & BUILDING_2_TILES_Y) DoWatchedCargoCallback(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
00712 if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
00713 if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);
00714 }
00715
00721 void GetHouseResolver(ResolverObject *ro, uint index)
00722 {
00723 NewHouseResolver(ro, GetHouseType(index), index, Town::GetByTile(index));
00724 }