1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "mapimages.h"
4
5#include <base/log.h>
6
7#include <engine/graphics.h>
8#include <engine/map.h>
9#include <engine/storage.h>
10#include <engine/textrender.h>
11
12#include <generated/client_data.h>
13
14#include <game/client/gameclient.h>
15#include <game/layers.h>
16#include <game/localization.h>
17#include <game/mapitems.h>
18
19CMapImages::CMapImages()
20{
21 m_Count = 0;
22 std::fill(first: std::begin(arr&: m_aEntitiesIsLoaded), last: std::end(arr&: m_aEntitiesIsLoaded), value: false);
23 m_SpeedupArrowIsLoaded = false;
24
25 str_copy(dst&: m_aEntitiesPath, src: "editor/entities_clear");
26
27 static_assert(std::size(gs_apModEntitiesNames) == MAP_IMAGE_MOD_TYPE_COUNT, "Mod name string count is not equal to mod type count");
28}
29
30void CMapImages::OnInit()
31{
32 m_TextureScale = g_Config.m_ClTextEntitiesSize;
33 InitOverlayTextures();
34
35 if(str_comp(a: g_Config.m_ClAssetsEntities, b: "default") == 0)
36 str_copy(dst&: m_aEntitiesPath, src: "editor/entities_clear");
37 else
38 {
39 str_format(buffer: m_aEntitiesPath, buffer_size: sizeof(m_aEntitiesPath), format: "assets/entities/%s", g_Config.m_ClAssetsEntities);
40 }
41
42 Console()->Chain(pName: "cl_text_entities_size", pfnChainFunc: ConchainClTextEntitiesSize, pUser: this);
43}
44
45void CMapImages::Unload()
46{
47 // unload all textures
48 for(int i = 0; i < m_Count; i++)
49 {
50 Graphics()->UnloadTexture(pIndex: &m_aTextures[i]);
51 }
52}
53
54void CMapImages::OnMapLoadImpl(class CLayers *pLayers, IMap *pMap)
55{
56 Unload();
57
58 int Start;
59 pMap->GetType(Type: MAPITEMTYPE_IMAGE, pStart: &Start, pNum: &m_Count);
60 m_Count = std::clamp<int>(val: m_Count, lo: 0, hi: MAX_MAPIMAGES);
61
62 unsigned char aTextureUsedByTileOrQuadLayerFlag[MAX_MAPIMAGES] = {0}; // 0: nothing, 1(as flag): tile layer, 2(as flag): quad layer
63 for(int GroupIndex = 0; GroupIndex < pLayers->NumGroups(); GroupIndex++)
64 {
65 const CMapItemGroup *pGroup = pLayers->GetGroup(Index: GroupIndex);
66 if(!pGroup)
67 {
68 continue;
69 }
70
71 for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++)
72 {
73 const CMapItemLayer *pLayer = pLayers->GetLayer(Index: pGroup->m_StartLayer + LayerIndex);
74 if(!pLayer)
75 {
76 continue;
77 }
78
79 if(pLayer->m_Type == LAYERTYPE_TILES)
80 {
81 const CMapItemLayerTilemap *pLayerTilemap = reinterpret_cast<const CMapItemLayerTilemap *>(pLayer);
82 if(pLayerTilemap->m_Image >= 0 && pLayerTilemap->m_Image < m_Count)
83 {
84 aTextureUsedByTileOrQuadLayerFlag[pLayerTilemap->m_Image] |= 1;
85 }
86 }
87 else if(pLayer->m_Type == LAYERTYPE_QUADS)
88 {
89 const CMapItemLayerQuads *pLayerQuads = reinterpret_cast<const CMapItemLayerQuads *>(pLayer);
90 if(pLayerQuads->m_Image >= 0 && pLayerQuads->m_Image < m_Count)
91 {
92 aTextureUsedByTileOrQuadLayerFlag[pLayerQuads->m_Image] |= 2;
93 }
94 }
95 }
96 }
97
98 // load new textures
99 bool ShowWarning = false;
100 for(int i = 0; i < m_Count; i++)
101 {
102 if(aTextureUsedByTileOrQuadLayerFlag[i] == 0)
103 {
104 // skip loading unused images
105 continue;
106 }
107
108 const int LoadFlag = (((aTextureUsedByTileOrQuadLayerFlag[i] & 1) != 0) ? Graphics()->TextureLoadFlags() : 0) | (((aTextureUsedByTileOrQuadLayerFlag[i] & 2) != 0) ? 0 : (Graphics()->HasTextureArraysSupport() ? IGraphics::TEXLOAD_NO_2D_TEXTURE : 0));
109 const CMapItemImage_v2 *pImg = static_cast<const CMapItemImage_v2 *>(pMap->GetItem(Index: Start + i));
110
111 const char *pName = pMap->GetDataString(Index: pImg->m_ImageName);
112 if(pName == nullptr || pName[0] == '\0')
113 {
114 if(pImg->m_External)
115 {
116 log_error("mapimages", "Failed to load map image %d: failed to load name.", i);
117 ShowWarning = true;
118 continue;
119 }
120 pName = "(error)";
121 }
122
123 if(pImg->m_Version > 1 && pImg->m_MustBe1 != 1)
124 {
125 log_error("mapimages", "Failed to load map image %d '%s': invalid map image type.", i, pName);
126 ShowWarning = true;
127 continue;
128 }
129
130 if(pImg->m_External)
131 {
132 char aPath[IO_MAX_PATH_LENGTH];
133 bool Translated = false;
134 if(Client()->IsSixup())
135 {
136 Translated =
137 !str_comp(a: pName, b: "grass_doodads") ||
138 !str_comp(a: pName, b: "grass_main") ||
139 !str_comp(a: pName, b: "winter_main") ||
140 !str_comp(a: pName, b: "generic_shadows") ||
141 !str_comp(a: pName, b: "generic_unhookable") ||
142 !str_comp(a: pName, b: "easter");
143 }
144 str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "mapres/%s%s.png", pName, Translated ? "_0.7" : "");
145 m_aTextures[i] = Graphics()->LoadTexture(pFilename: aPath, StorageType: IStorage::TYPE_ALL, Flags: LoadFlag);
146 }
147 else
148 {
149 CImageInfo ImageInfo;
150 ImageInfo.m_Width = pImg->m_Width;
151 ImageInfo.m_Height = pImg->m_Height;
152 ImageInfo.m_Format = CImageInfo::FORMAT_RGBA;
153 ImageInfo.m_pData = static_cast<uint8_t *>(pMap->GetData(Index: pImg->m_ImageData));
154 if(ImageInfo.m_pData && (size_t)pMap->GetDataSize(Index: pImg->m_ImageData) >= ImageInfo.DataSize())
155 {
156 char aTexName[IO_MAX_PATH_LENGTH];
157 str_format(buffer: aTexName, buffer_size: sizeof(aTexName), format: "embedded: %s", pName);
158 m_aTextures[i] = Graphics()->LoadTextureRaw(Image: ImageInfo, Flags: LoadFlag, pTexName: aTexName);
159 pMap->UnloadData(Index: pImg->m_ImageData);
160 }
161 else
162 {
163 pMap->UnloadData(Index: pImg->m_ImageData);
164 log_error("mapimages", "Failed to load map image %d: failed to load data.", i);
165 ShowWarning = true;
166 continue;
167 }
168 }
169 pMap->UnloadData(Index: pImg->m_ImageName);
170 ShowWarning = ShowWarning || m_aTextures[i].IsNullTexture();
171 }
172 if(ShowWarning)
173 {
174 Client()->AddWarning(Warning: SWarning(Localize(pStr: "Some map images could not be loaded. Check the local console for details.")));
175 }
176}
177
178void CMapImages::OnMapLoad()
179{
180 OnMapLoadImpl(pLayers: GameClient()->Layers(), pMap: GameClient()->Map());
181}
182
183void CMapImages::LoadBackground(class CLayers *pLayers, class IMap *pMap)
184{
185 OnMapLoadImpl(pLayers, pMap);
186}
187
188static EMapImageModType GetEntitiesModType(const CGameInfo &GameInfo)
189{
190 if(GameInfo.m_EntitiesFDDrace)
191 return MAP_IMAGE_MOD_TYPE_FDDRACE;
192 else if(GameInfo.m_EntitiesDDNet)
193 return MAP_IMAGE_MOD_TYPE_DDNET;
194 else if(GameInfo.m_EntitiesDDRace)
195 return MAP_IMAGE_MOD_TYPE_DDRACE;
196 else if(GameInfo.m_EntitiesRace)
197 return MAP_IMAGE_MOD_TYPE_RACE;
198 else if(GameInfo.m_EntitiesBW)
199 return MAP_IMAGE_MOD_TYPE_BLOCKWORLDS;
200 else if(GameInfo.m_EntitiesFNG)
201 return MAP_IMAGE_MOD_TYPE_FNG;
202 else if(GameInfo.m_EntitiesVanilla)
203 return MAP_IMAGE_MOD_TYPE_VANILLA;
204 else
205 return MAP_IMAGE_MOD_TYPE_DDNET;
206}
207
208static bool IsValidTile(int LayerType, bool EntitiesAreMasked, EMapImageModType EntitiesModType, int TileIndex)
209{
210 if(TileIndex == TILE_AIR)
211 return false;
212 if(!EntitiesAreMasked)
213 return true;
214
215 if(EntitiesModType == MAP_IMAGE_MOD_TYPE_DDNET || EntitiesModType == MAP_IMAGE_MOD_TYPE_DDRACE)
216 {
217 if(EntitiesModType == MAP_IMAGE_MOD_TYPE_DDNET || TileIndex != TILE_SPEED_BOOST_OLD)
218 {
219 if(LayerType == MAP_IMAGE_ENTITY_LAYER_TYPE_ALL_EXCEPT_SWITCH &&
220 !IsValidGameTile(Index: TileIndex) &&
221 !IsValidFrontTile(Index: TileIndex) &&
222 !IsValidSpeedupTile(Index: TileIndex) &&
223 !IsValidTeleTile(Index: TileIndex) &&
224 !IsValidTuneTile(Index: TileIndex))
225 {
226 return false;
227 }
228 else if(LayerType == MAP_IMAGE_ENTITY_LAYER_TYPE_SWITCH &&
229 !IsValidSwitchTile(Index: TileIndex))
230 {
231 return false;
232 }
233 }
234 }
235 else if(EntitiesModType == MAP_IMAGE_MOD_TYPE_RACE && IsCreditsTile(TileIndex))
236 {
237 return false;
238 }
239 else if(EntitiesModType == MAP_IMAGE_MOD_TYPE_FNG && IsCreditsTile(TileIndex))
240 {
241 return false;
242 }
243 else if(EntitiesModType == MAP_IMAGE_MOD_TYPE_VANILLA && IsCreditsTile(TileIndex))
244 {
245 return false;
246 }
247 return true;
248}
249
250IGraphics::CTextureHandle CMapImages::GetEntities(EMapImageEntityLayerType EntityLayerType)
251{
252 const bool EntitiesAreMasked = !GameClient()->m_GameInfo.m_DontMaskEntities;
253 const EMapImageModType EntitiesModType = GetEntitiesModType(GameInfo: GameClient()->m_GameInfo);
254
255 if(!m_aEntitiesIsLoaded[(EntitiesModType * 2) + (int)EntitiesAreMasked])
256 {
257 m_aEntitiesIsLoaded[(EntitiesModType * 2) + (int)EntitiesAreMasked] = true;
258
259 int TextureLoadFlag = 0;
260 if(Graphics()->HasTextureArraysSupport())
261 TextureLoadFlag = Graphics()->TextureLoadFlags() | IGraphics::TEXLOAD_NO_2D_TEXTURE;
262
263 CImageInfo ImgInfo;
264 char aPath[IO_MAX_PATH_LENGTH];
265 str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "%s/%s.png", m_aEntitiesPath, gs_apModEntitiesNames[EntitiesModType]);
266 Graphics()->LoadPng(Image&: ImgInfo, pFilename: aPath, StorageType: IStorage::TYPE_ALL);
267
268 // try as single ddnet replacement
269 if(ImgInfo.m_pData == nullptr && EntitiesModType == MAP_IMAGE_MOD_TYPE_DDNET)
270 {
271 str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "%s.png", m_aEntitiesPath);
272 Graphics()->LoadPng(Image&: ImgInfo, pFilename: aPath, StorageType: IStorage::TYPE_ALL);
273 }
274
275 // try default
276 if(ImgInfo.m_pData == nullptr)
277 {
278 str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "editor/entities_clear/%s.png", gs_apModEntitiesNames[EntitiesModType]);
279 Graphics()->LoadPng(Image&: ImgInfo, pFilename: aPath, StorageType: IStorage::TYPE_ALL);
280 }
281
282 if(ImgInfo.m_pData != nullptr)
283 {
284 CImageInfo BuildImageInfo;
285 BuildImageInfo.m_Width = ImgInfo.m_Width;
286 BuildImageInfo.m_Height = ImgInfo.m_Height;
287 BuildImageInfo.m_Format = ImgInfo.m_Format;
288 BuildImageInfo.Allocate();
289
290 // build game layer
291 for(int LayerType = 0; LayerType < MAP_IMAGE_ENTITY_LAYER_TYPE_COUNT; ++LayerType)
292 {
293 dbg_assert(!m_aaEntitiesTextures[(EntitiesModType * 2) + (int)EntitiesAreMasked][LayerType].IsValid(), "entities texture already loaded when it should not be");
294
295 // set everything transparent
296 mem_zero(block: BuildImageInfo.m_pData, size: BuildImageInfo.DataSize());
297
298 for(int i = 0; i < 256; ++i)
299 {
300 int TileIndex = i;
301 if(IsValidTile(LayerType, EntitiesAreMasked, EntitiesModType, TileIndex))
302 {
303 if(LayerType == MAP_IMAGE_ENTITY_LAYER_TYPE_SWITCH && TileIndex == TILE_SWITCHTIMEDOPEN)
304 {
305 TileIndex = 8;
306 }
307
308 const size_t CopyWidth = ImgInfo.m_Width / 16;
309 const size_t CopyHeight = ImgInfo.m_Height / 16;
310 const size_t OffsetX = (size_t)(TileIndex % 16) * CopyWidth;
311 const size_t OffsetY = (size_t)(TileIndex / 16) * CopyHeight;
312 BuildImageInfo.CopyRectFrom(SrcImage: ImgInfo, SrcX: OffsetX, SrcY: OffsetY, Width: CopyWidth, Height: CopyHeight, DestX: OffsetX, DestY: OffsetY);
313 }
314 }
315
316 m_aaEntitiesTextures[(EntitiesModType * 2) + (int)EntitiesAreMasked][LayerType] = Graphics()->LoadTextureRaw(Image: BuildImageInfo, Flags: TextureLoadFlag, pTexName: aPath);
317 }
318
319 BuildImageInfo.Free();
320 ImgInfo.Free();
321 }
322 }
323
324 return m_aaEntitiesTextures[(EntitiesModType * 2) + (int)EntitiesAreMasked][EntityLayerType];
325}
326
327IGraphics::CTextureHandle CMapImages::GetSpeedupArrow()
328{
329 if(!m_SpeedupArrowIsLoaded)
330 {
331 int TextureLoadFlag = Graphics()->TextureLoadFlags() | IGraphics::TEXLOAD_NO_2D_TEXTURE;
332 m_SpeedupArrowTexture = Graphics()->LoadTexture(pFilename: "editor/speed_arrow_array.png", StorageType: IStorage::TYPE_ALL, Flags: TextureLoadFlag);
333 m_SpeedupArrowIsLoaded = true;
334 }
335 return m_SpeedupArrowTexture;
336}
337
338IGraphics::CTextureHandle CMapImages::GetOverlayBottom()
339{
340 return m_OverlayBottomTexture;
341}
342
343IGraphics::CTextureHandle CMapImages::GetOverlayTop()
344{
345 return m_OverlayTopTexture;
346}
347
348IGraphics::CTextureHandle CMapImages::GetOverlayCenter()
349{
350 return m_OverlayCenterTexture;
351}
352
353void CMapImages::ChangeEntitiesPath(const char *pPath)
354{
355 if(str_comp(a: pPath, b: "default") == 0)
356 str_copy(dst&: m_aEntitiesPath, src: "editor/entities_clear");
357 else
358 {
359 str_format(buffer: m_aEntitiesPath, buffer_size: sizeof(m_aEntitiesPath), format: "assets/entities/%s", pPath);
360 }
361
362 for(int ModType = 0; ModType < MAP_IMAGE_MOD_TYPE_COUNT * 2; ++ModType)
363 {
364 if(m_aEntitiesIsLoaded[ModType])
365 {
366 for(int LayerType = 0; LayerType < MAP_IMAGE_ENTITY_LAYER_TYPE_COUNT; ++LayerType)
367 {
368 Graphics()->UnloadTexture(pIndex: &m_aaEntitiesTextures[ModType][LayerType]);
369 }
370 m_aEntitiesIsLoaded[ModType] = false;
371 }
372 }
373}
374
375void CMapImages::ConchainClTextEntitiesSize(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
376{
377 pfnCallback(pResult, pCallbackUserData);
378 if(pResult->NumArguments())
379 {
380 CMapImages *pThis = static_cast<CMapImages *>(pUserData);
381 pThis->SetTextureScale(g_Config.m_ClTextEntitiesSize);
382 }
383}
384
385void CMapImages::SetTextureScale(int Scale)
386{
387 if(m_TextureScale == Scale)
388 return;
389
390 m_TextureScale = Scale;
391
392 if(Graphics() && m_OverlayCenterTexture.IsValid()) // check if component was initialized
393 {
394 // reinitialize component
395 Graphics()->UnloadTexture(pIndex: &m_OverlayBottomTexture);
396 Graphics()->UnloadTexture(pIndex: &m_OverlayTopTexture);
397 Graphics()->UnloadTexture(pIndex: &m_OverlayCenterTexture);
398
399 InitOverlayTextures();
400 }
401}
402
403int CMapImages::GetTextureScale() const
404{
405 return m_TextureScale;
406}
407
408IGraphics::CTextureHandle CMapImages::UploadEntityLayerText(int TextureSize, int MaxWidth, int YOffset)
409{
410 CImageInfo TextImage;
411 TextImage.m_Width = 1024;
412 TextImage.m_Height = 1024;
413 TextImage.m_Format = CImageInfo::FORMAT_RGBA;
414 TextImage.AllocateFillZero();
415
416 UpdateEntityLayerText(TextImage, TextureSize, MaxWidth, YOffset, NumbersPower: 0);
417 UpdateEntityLayerText(TextImage, TextureSize, MaxWidth, YOffset, NumbersPower: 1);
418 UpdateEntityLayerText(TextImage, TextureSize, MaxWidth, YOffset, NumbersPower: 2, MaxNumber: 255);
419
420 const int TextureLoadFlag = Graphics()->TextureLoadFlags() | IGraphics::TEXLOAD_NO_2D_TEXTURE;
421 return Graphics()->LoadTextureRawMove(Image&: TextImage, Flags: TextureLoadFlag);
422}
423
424void CMapImages::UpdateEntityLayerText(CImageInfo &TextImage, int TextureSize, int MaxWidth, int YOffset, int NumbersPower, int MaxNumber)
425{
426 char aBuf[4];
427 int DigitsCount = NumbersPower + 1;
428
429 int CurrentNumber = std::pow(x: 10, y: NumbersPower);
430
431 if(MaxNumber == -1)
432 MaxNumber = CurrentNumber * 10 - 1;
433
434 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", CurrentNumber);
435
436 int CurrentNumberSuitableFontSize = TextRender()->AdjustFontSize(pText: aBuf, TextLength: DigitsCount, MaxSize: TextureSize, MaxWidth);
437 int UniversalSuitableFontSize = CurrentNumberSuitableFontSize * 0.92f; // should be smoothed enough to fit any digits combination
438
439 YOffset += ((TextureSize - UniversalSuitableFontSize) / 2);
440
441 for(; CurrentNumber <= MaxNumber; ++CurrentNumber)
442 {
443 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", CurrentNumber);
444
445 float x = (CurrentNumber % 16) * 64;
446 float y = (CurrentNumber / 16) * 64;
447
448 int ApproximateTextWidth = TextRender()->CalculateTextWidth(pText: aBuf, TextLength: DigitsCount, FontWidth: 0, FontSize: UniversalSuitableFontSize);
449 int XOffSet = (MaxWidth - std::clamp(val: ApproximateTextWidth, lo: 0, hi: MaxWidth)) / 2;
450
451 TextRender()->UploadEntityLayerText(TextImage, TexSubWidth: (TextImage.m_Width / 16) - XOffSet, TexSubHeight: (TextImage.m_Height / 16) - YOffset, pText: aBuf, Length: DigitsCount, x: x + XOffSet, y: y + YOffset, FontSize: UniversalSuitableFontSize);
452 }
453}
454
455void CMapImages::InitOverlayTextures()
456{
457 int TextureSize = 64 * m_TextureScale / 100;
458 TextureSize = std::clamp(val: TextureSize, lo: 2, hi: 64);
459 int TextureToVerticalCenterOffset = (64 - TextureSize) / 2 + TextureSize * 0.1f; // should be used to move texture to the center of 64 pixels area
460
461 if(!m_OverlayBottomTexture.IsValid())
462 {
463 m_OverlayBottomTexture = UploadEntityLayerText(TextureSize: TextureSize / 2, MaxWidth: 64, YOffset: 32 + TextureToVerticalCenterOffset / 2);
464 }
465
466 if(!m_OverlayTopTexture.IsValid())
467 {
468 m_OverlayTopTexture = UploadEntityLayerText(TextureSize: TextureSize / 2, MaxWidth: 64, YOffset: TextureToVerticalCenterOffset / 2);
469 }
470
471 if(!m_OverlayCenterTexture.IsValid())
472 {
473 m_OverlayCenterTexture = UploadEntityLayerText(TextureSize, MaxWidth: 64, YOffset: TextureToVerticalCenterOffset);
474 }
475}
476