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