1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file newgrf_house.h Functions related to NewGRF houses. */
9 
10 #ifndef NEWGRF_HOUSE_H
11 #define NEWGRF_HOUSE_H
12 
13 #include "newgrf_callbacks.h"
14 #include "tile_cmd.h"
15 #include "house_type.h"
16 #include "newgrf_spritegroup.h"
17 #include "newgrf_town.h"
18 
19 /** Scope resolver for houses. */
20 struct HouseScopeResolver : public ScopeResolver {
21 	HouseID house_id;              ///< Type of house being queried.
22 	TileIndex tile;                ///< Tile of this house.
23 	Town *town;                    ///< Town of this house.
24 	bool not_yet_constructed;      ///< True for construction check.
25 	uint16 initial_random_bits;    ///< Random bits during construction checks.
26 	CargoTypes watched_cargo_triggers; ///< Cargo types that triggered the watched cargo callback.
27 
28 	/**
29 	 * Constructor of a house scope resolver.
30 	 * @param ro Surrounding resolver.
31 	 * @param house_id House type being queried.
32 	 * @param tile %Tile containing the house.
33 	 * @param town %Town containing the house.
34 	 * @param not_yet_constructed House is still under construction.
35 	 * @param initial_random_bits Random bits during construction checks.
36 	 * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
37 	 */
HouseScopeResolverHouseScopeResolver38 	HouseScopeResolver(ResolverObject &ro, HouseID house_id, TileIndex tile, Town *town,
39 			bool not_yet_constructed, uint8 initial_random_bits, CargoTypes watched_cargo_triggers)
40 		: ScopeResolver(ro), house_id(house_id), tile(tile), town(town), not_yet_constructed(not_yet_constructed),
41 		initial_random_bits(initial_random_bits), watched_cargo_triggers(watched_cargo_triggers)
42 	{
43 	}
44 
45 	uint32 GetRandomBits() const override;
46 	uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
47 	uint32 GetTriggers() const override;
48 };
49 
50 /** Resolver object to be used for houses (feature 07 spritegroups). */
51 struct HouseResolverObject : public ResolverObject {
52 	HouseScopeResolver house_scope;
53 	TownScopeResolver  town_scope;
54 
55 	HouseResolverObject(HouseID house_id, TileIndex tile, Town *town,
56 			CallbackID callback = CBID_NO_CALLBACK, uint32 param1 = 0, uint32 param2 = 0,
57 			bool not_yet_constructed = false, uint8 initial_random_bits = 0, CargoTypes watched_cargo_triggers = 0);
58 
59 	ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
60 	{
61 		switch (scope) {
62 			case VSG_SCOPE_SELF:   return &this->house_scope;
63 			case VSG_SCOPE_PARENT: return &this->town_scope;
64 			default: return ResolverObject::GetScope(scope, relative);
65 		}
66 	}
67 
68 	GrfSpecFeature GetFeature() const override;
69 	uint32 GetDebugID() const override;
70 };
71 
72 /**
73  * Makes class IDs unique to each GRF file.
74  * Houses can be assigned class IDs which are only comparable within the GRF
75  * file they were defined in. This mapping ensures that if two houses have the
76  * same class as defined by the GRF file, the classes are different within the
77  * game. An array of HouseClassMapping structs is created, and the array index
78  * of the struct that matches both the GRF ID and the class ID is the class ID
79  * used in the game.
80  *
81  * Although similar to the HouseIDMapping struct above, this serves a different
82  * purpose. Since the class ID is not saved anywhere, this mapping does not
83  * need to be persistent; it just needs to keep class ids unique.
84  */
85 struct HouseClassMapping {
86 	uint32 grfid;     ///< The GRF ID of the file this class belongs to
87 	uint8  class_id;  ///< The class id within the grf file
88 };
89 
90 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid);
91 
92 void InitializeBuildingCounts();
93 void IncreaseBuildingCount(Town *t, HouseID house_id);
94 void DecreaseBuildingCount(Town *t, HouseID house_id);
95 
96 void DrawNewHouseTile(TileInfo *ti, HouseID house_id);
97 void AnimateNewHouseTile(TileIndex tile);
98 void AnimateNewHouseConstruction(TileIndex tile);
99 
100 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile,
101 		bool not_yet_constructed = false, uint8 initial_random_bits = 0, CargoTypes watched_cargo_triggers = 0);
102 void WatchedCargoCallback(TileIndex tile, CargoTypes trigger_cargoes);
103 
104 bool CanDeleteHouse(TileIndex tile);
105 
106 bool NewHouseTileLoop(TileIndex tile);
107 
108 enum HouseTrigger {
109 	/* The tile of the house has been triggered during the tileloop. */
110 	HOUSE_TRIGGER_TILE_LOOP     = 0x01,
111 	/*
112 	 * The top tile of a (multitile) building has been triggered during and all
113 	 * the tileloop other tiles of the same building get the same random value.
114 	 */
115 	HOUSE_TRIGGER_TILE_LOOP_TOP = 0x02,
116 };
117 void TriggerHouse(TileIndex t, HouseTrigger trigger);
118 
119 #endif /* NEWGRF_HOUSE_H */
120