1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Handles landscape and sky */
19 
20 #ifndef INC_C4Landscape
21 #define INC_C4Landscape
22 
23 #include "config/C4Constants.h"
24 
25 const int32_t C4MaxMaterial = 125;
26 
27 const int32_t C4LS_MaxRelights = 50;
28 
29 enum class LandscapeMode
30 {
31 	Undefined = 0,
32 	Dynamic = 1,
33 	Static = 2,
34 	Exact = 3
35 };
36 
37 class C4Landscape
38 {
39 	struct P;
40 	std::unique_ptr<P> p;
41 
42 public:
43 	C4Landscape();
44 	~C4Landscape();
45 
46 	// Use this with the various drawing functions to keep current material for
47 	// either foreground or background map. We can use C4M_MaxTexIndex as a value
48 	// here because this value is reserved anyway for the differential landscape
49 	// encoding.
50 	static const uint8_t Transparent = C4M_MaxTexIndex;
51 
52 	void Default();
53 	void Clear(bool fClearMapCreator=true, bool fClearSky=true, bool fClearRenderer=true);
54 	void Execute();
55 	void Synchronize();
56 	void Draw(C4TargetFacet &cgo, class C4FoWRegion *pLight = nullptr);
57 	void ScenarioInit();
58 
59 	void DrawMaterialRect(int32_t mat, int32_t tx, int32_t ty, int32_t wdt, int32_t hgt);
60 
61 	void RaiseTerrain(int32_t tx, int32_t ty, int32_t wdt);
62 	void FindMatTop(int32_t mat, int32_t &x, int32_t &y, bool distant_first) const;
63 	BYTE GetMapIndex(int32_t iX, int32_t iY) const;
64 	BYTE GetBackMapIndex(int32_t iX, int32_t iY) const;
65 	bool Load(C4Group &hGroup, bool fLoadSky, bool fSavegame);
66 	bool Save(C4Group &hGroup) const;
67 	bool SaveDiff(C4Group &hGroup, bool fSyncSave) const;
68 	bool SaveMap(C4Group &hGroup) const;
69 	bool SaveInitial();
70 	bool SaveTextures(C4Group &hGroup) const;
71 	bool Init(C4Group &hGroup, bool fOverloadCurrent, bool fLoadSky, bool &rfLoaded, bool fSavegame);
72 	bool HasMap() const;
73 	bool MapToLandscape();
74 	bool ApplyDiff(C4Group &hGroup);
75 
76 	void SetMode(LandscapeMode iMode);
77 	LandscapeMode GetMode() const;
78 
79 	bool SetPix2(int32_t x, int32_t y, BYTE fgPix, BYTE bgPix); // set landscape pixel (bounds checked)
80 	bool _SetPix2(int32_t x, int32_t y, BYTE fgPix, BYTE bgPix); // set landsape pixel (bounds not checked)
81 	void _SetPix2Tmp(int32_t x, int32_t y, BYTE fgPix, BYTE bgPix); // set landsape pixel (bounds not checked, no material count updates, no landscape relighting). Material must be reset to original value with this function before modifying landscape in any other way. Only used for temporary pixel changes by SolidMask (C4SolidMask::RemoveTemporary, C4SolidMask::PutTemporary).
82 	bool InsertMaterialOutsideLandscape(int32_t tx, int32_t ty, int32_t mdens); // return whether material insertion would be successful on an out-of-landscape position. Does not actually insert material.
83 	bool InsertMaterial(int32_t mat, int32_t *tx, int32_t *ty, int32_t vx = 0, int32_t vy = 0, bool query_only=false); // modifies tx/ty to actual insertion position
84 	bool InsertDeadMaterial(int32_t mat, int32_t tx, int32_t ty);
85 	bool FindMatPath(int32_t &fx, int32_t &fy, int32_t ydir, int32_t mdens, int32_t mslide) const;
86 	bool FindMatSlide(int32_t &fx, int32_t &fy, int32_t ydir, int32_t mdens, int32_t mslide) const;
87 	bool FindMatPathPush(int32_t &fx, int32_t &fy, int32_t mdens, int32_t mslide, bool liquid) const;
88 	bool Incinerate(int32_t x, int32_t y, int32_t cause_player);
89 	bool DrawBrush(int32_t iX, int32_t iY, int32_t iGrade, const char *szMaterial, const char *szTexture, const char *szBackMaterial, const char *szBackTexture);
90 	bool DrawLine(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iGrade, const char *szMaterial, const char *szTexture, const char *szBackMaterial, const char *szBackTexture);
91 	bool DrawBox(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iGrade, const char *szMaterial, const char *szTexture, const char *szBackMaterial, const char *szBackTexture);
92 	bool DrawChunks(int32_t tx, int32_t ty, int32_t wdt, int32_t hgt, int32_t icntx, int32_t icnty, const char *szMaterial, const char *szTexture, bool bIFT);
93 	bool DrawQuad(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iX3, int32_t iY3, int32_t iX4, int32_t iY4, const char *szMaterial, const char *szBackMaterial, bool fDrawBridge);
94 	bool DrawPolygon(int *vtcs, int length, const char *szMaterial, const char *szBackMaterial, bool fDrawBridge);
95 	CStdPalette *GetPal() const;
96 
97 	int32_t GetWidth() const;
98 	int32_t GetHeight() const;
99 	int32_t GetMapZoom() const;
100 	C4Real GetGravity() const;
101 	void SetGravity(C4Real g);
102 
103 	BYTE _GetPix(int32_t x, int32_t y) const; // get landscape pixel (bounds not checked)
104 	BYTE GetPix(int32_t x, int32_t y) const;
105 
106 	int32_t _GetMat(int32_t x, int32_t y) const;
107 	int32_t _GetDensity(int32_t x, int32_t y) const;
108 	int32_t _GetPlacement(int32_t x, int32_t y) const;
109 	int32_t GetMat(int32_t x, int32_t y) const;
110 	int32_t GetDensity(int32_t x, int32_t y) const;
111 	int32_t GetPlacement(int32_t x, int32_t y) const;
112 
113 	BYTE _GetBackPix(int32_t x, int32_t y) const;
114 	BYTE GetBackPix(int32_t x, int32_t y) const;
115 	int32_t _GetBackMat(int32_t x, int32_t y) const;
116 	int32_t _GetBackDensity(int32_t x, int32_t y) const;
117 	int32_t _GetBackPlacement(int32_t x, int32_t y) const;
118 	int32_t GetBackMat(int32_t x, int32_t y) const;
119 	int32_t GetBackDensity(int32_t x, int32_t y) const;
120 	int32_t GetBackPlacement(int32_t x, int32_t y) const;
121 
122 	bool GetLight(int32_t x, int32_t y);
123 	bool _GetLight(int32_t x, int32_t y);
124 
125 	bool _FastSolidCheck(int32_t x, int32_t y) const;
126 	static int32_t FastSolidCheckNextX(int32_t x);
127 	int32_t GetPixMat(BYTE byPix) const;
128 	int32_t GetPixDensity(BYTE byPix) const;
129 	bool _PathFree(int32_t x, int32_t y, int32_t x2, int32_t y2) const; // quickly checks wether there *might* be pixel in the path.
130 	int32_t GetMatHeight(int32_t x, int32_t y, int32_t iYDir, int32_t iMat, int32_t iMax) const;
131 
132 	int32_t AreaSolidCount(int32_t x, int32_t y, int32_t wdt, int32_t hgt) const;
133 	int32_t ExtractMaterial(int32_t fx, int32_t fy, bool distant_first);
134 	bool DrawMap(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, const char *szMapDef, bool ignoreSky = false); // creates and draws a map section using MapCreatorS2
135 	bool ClipRect(int32_t &rX, int32_t &rY, int32_t &rWdt, int32_t &rHgt) const; // clip given rect by landscape size; return whether anything is left unclipped
136 	bool DrawDefMap(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, const char *szMapDef, bool ignoreSky = false); // creates and draws a map section using MapCreatorS2 and a map from the loaded Landscape.txt
137 	bool SetModulation(DWORD dwWithClr);
138 	DWORD GetModulation() const;
139 	bool PostInitMap();   // do script callbacks of MapCreatorS2 in finished landscape
140 	bool ReplaceMapColor(BYTE iOldIndex, BYTE iNewIndex); // find every occurance of iOldIndex in map; replace it by new index
141 	bool SetTextureIndex(const char *szMatTex, BYTE iNewIndex, bool fInsert); // change color index of map texture, or insert a new one
142 	void SetMapChanged();
143 	void HandleTexMapUpdate();
144 	void UpdatePixMaps();
145 	bool DoRelights();
146 	void RemoveUnusedTexMapEntries();
147 
148 	class C4Sky &GetSky();
149 
150 	bool HasFoW() const;
151 	class C4FoW *GetFoW();
152 
153 	int32_t GetMatCount(int material) const;
154 	int32_t GetEffectiveMatCount(int material) const;
155 
156 	int32_t DigFreeShape(int *vtcs, int length, C4Object *by_object = nullptr, bool no_dig2objects = false, bool no_instability_check = false);
157 	void BlastFreeShape(int *vtcs, int length, C4Object *by_object = nullptr, int32_t by_player = NO_OWNER, int32_t iMaxDensity = C4M_Vehicle);
158 
159 	void ClearFreeRect(int32_t tx, int32_t ty, int32_t wdt, int32_t hgt);
160 	int32_t DigFreeRect(int32_t tx, int32_t ty, int32_t wdt, int32_t hgt, C4Object *by_object = nullptr, bool no_dig2objects = false, bool no_instability_check = false);
161 	int32_t DigFree(int32_t tx, int32_t ty, int32_t rad, C4Object *by_object = nullptr, bool no_dig2objects = false, bool no_instability_check = false);
162 	void ShakeFree(int32_t tx, int32_t ty, int32_t rad);
163 	void BlastFree(int32_t tx, int32_t ty, int32_t rad, int32_t caused_by = NO_OWNER, C4Object *by_object = nullptr, int32_t iMaxDensity = C4M_Vehicle);
164 
165 	void CheckInstabilityRange(int32_t tx, int32_t ty);
166 	bool CheckInstability(int32_t tx, int32_t ty, int32_t recursion_count=0);
167 
168 	bool ClearPix(int32_t tx, int32_t ty);	// also used by mass mover (corrode)
169 
170 	void ClearPointers(C4Object *pObj);
171 
172 	void CompileFunc(StdCompiler *pComp); // without landscape bitmaps and sky
173 };
174 
175 extern C4Landscape Landscape;
176 
177 /* Some global landscape functions */
178 
179 bool AboveSolid(int32_t &rx, int32_t &ry);
180 bool AboveSemiSolid(int32_t &rx, int32_t &ry);
181 bool SemiAboveSolid(int32_t &rx, int32_t &ry);
182 bool FindSolidGround(int32_t &rx, int32_t &ry, int32_t width);
183 bool FindLiquid(int32_t &rx, int32_t &ry, int32_t width, int32_t height);
184 bool FindTunnel(int32_t &rx, int32_t &ry, int32_t width, int32_t height);
185 bool FindSurfaceLiquid(int32_t &rx, int32_t &ry, int32_t width, int32_t height);
186 bool FindLevelGround(int32_t &rx, int32_t &ry, int32_t width, int32_t hrange);
187 bool FindConSiteSpot(int32_t &rx, int32_t &ry, int32_t wdt, int32_t hgt, int32_t hrange=-1);
188 bool FindThrowingPosition(int32_t iTx, int32_t iTy, C4Real fXDir, C4Real fYDir, int32_t iHeight, int32_t &rX, int32_t &rY);
189 bool PathFree(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
190 bool PathFree(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t *ix, int32_t *iy);
191 bool PathFreeIgnoreVehicle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t *ix=nullptr, int32_t *iy=nullptr);
192 bool FindClosestFree(int32_t &rX, int32_t &rY, int32_t iAngle1, int32_t iAngle2, int32_t iExcludeAngle1, int32_t iExcludeAngle2);
193 bool ConstructionCheck(C4PropList *, int32_t iX, int32_t iY, C4Object *pByObj=nullptr);
194 int32_t PixCol2Mat(BYTE pixc);
195 
DensitySolid(int32_t dens)196 inline bool DensitySolid(int32_t dens)
197 {
198 	return (dens>=C4M_Solid);
199 }
200 
DensitySemiSolid(int32_t dens)201 inline bool DensitySemiSolid(int32_t dens)
202 {
203 	return (dens>=C4M_SemiSolid);
204 }
205 
DensityLiquid(int32_t dens)206 inline bool DensityLiquid(int32_t dens)
207 {
208 	return ((dens>=C4M_Liquid) && (dens<C4M_Solid));
209 }
210 
PixCol2Tex(BYTE pixc)211 inline int32_t PixCol2Tex(BYTE pixc)
212 {
213 	// Validate
214 	if (pixc >= C4M_MaxTexIndex) return 0;
215 	// Done
216 	return pixc;
217 }
218 
GBackMat(int32_t x,int32_t y)219 inline int32_t GBackMat(int32_t x, int32_t y)
220 {
221 	return ::Landscape.GetMat(x, y);
222 }
223 
GBackDensity(int32_t x,int32_t y)224 inline int32_t GBackDensity(int32_t x, int32_t y)
225 {
226 	return ::Landscape.GetDensity(x, y);
227 }
228 
GBackSolid(int32_t x,int32_t y)229 inline bool GBackSolid(int32_t x, int32_t y)
230 {
231 	return DensitySolid(GBackDensity(x,y));
232 }
233 
GBackSemiSolid(int32_t x,int32_t y)234 inline bool GBackSemiSolid(int32_t x, int32_t y)
235 {
236 	return DensitySemiSolid(GBackDensity(x,y));
237 }
238 
GBackLiquid(int32_t x,int32_t y)239 inline bool GBackLiquid(int32_t x, int32_t y)
240 {
241 	return DensityLiquid(GBackDensity(x,y));
242 }
243 #endif
244