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 "layers.h"
4
5#include "mapitems.h"
6
7#include <engine/map.h>
8
9CLayers::CLayers()
10{
11 Unload();
12}
13
14void CLayers::Init(class IKernel *pKernel)
15{
16 Unload();
17
18 m_pMap = pKernel->RequestInterface<IMap>();
19 m_pMap->GetType(Type: MAPITEMTYPE_GROUP, pStart: &m_GroupsStart, pNum: &m_GroupsNum);
20 m_pMap->GetType(Type: MAPITEMTYPE_LAYER, pStart: &m_LayersStart, pNum: &m_LayersNum);
21
22 for(int g = 0; g < NumGroups(); g++)
23 {
24 CMapItemGroup *pGroup = GetGroup(Index: g);
25 for(int l = 0; l < pGroup->m_NumLayers; l++)
26 {
27 CMapItemLayer *pLayer = GetLayer(Index: pGroup->m_StartLayer + l);
28
29 if(pLayer->m_Type == LAYERTYPE_TILES)
30 {
31 CMapItemLayerTilemap *pTilemap = reinterpret_cast<CMapItemLayerTilemap *>(pLayer);
32 bool IsEntities = false;
33
34 if(pTilemap->m_Flags & TILESLAYERFLAG_GAME)
35 {
36 m_pGameLayer = pTilemap;
37 m_pGameGroup = pGroup;
38
39 // make sure the game group has standard settings
40 m_pGameGroup->m_OffsetX = 0;
41 m_pGameGroup->m_OffsetY = 0;
42 m_pGameGroup->m_ParallaxX = 100;
43 m_pGameGroup->m_ParallaxY = 100;
44
45 if(m_pGameGroup->m_Version >= 2)
46 {
47 m_pGameGroup->m_UseClipping = 0;
48 m_pGameGroup->m_ClipX = 0;
49 m_pGameGroup->m_ClipY = 0;
50 m_pGameGroup->m_ClipW = 0;
51 m_pGameGroup->m_ClipH = 0;
52 }
53
54 IsEntities = true;
55 //break;
56 }
57 if(pTilemap->m_Flags & TILESLAYERFLAG_TELE)
58 {
59 if(pTilemap->m_Version <= 2)
60 {
61 pTilemap->m_Tele = *((int *)(pTilemap) + 15);
62 }
63 m_pTeleLayer = pTilemap;
64 IsEntities = true;
65 }
66 if(pTilemap->m_Flags & TILESLAYERFLAG_SPEEDUP)
67 {
68 if(pTilemap->m_Version <= 2)
69 {
70 pTilemap->m_Speedup = *((int *)(pTilemap) + 16);
71 }
72 m_pSpeedupLayer = pTilemap;
73 IsEntities = true;
74 }
75 if(pTilemap->m_Flags & TILESLAYERFLAG_FRONT)
76 {
77 if(pTilemap->m_Version <= 2)
78 {
79 pTilemap->m_Front = *((int *)(pTilemap) + 17);
80 }
81 m_pFrontLayer = pTilemap;
82 IsEntities = true;
83 }
84 if(pTilemap->m_Flags & TILESLAYERFLAG_SWITCH)
85 {
86 if(pTilemap->m_Version <= 2)
87 {
88 pTilemap->m_Switch = *((int *)(pTilemap) + 18);
89 }
90 m_pSwitchLayer = pTilemap;
91 IsEntities = true;
92 }
93 if(pTilemap->m_Flags & TILESLAYERFLAG_TUNE)
94 {
95 if(pTilemap->m_Version <= 2)
96 {
97 pTilemap->m_Tune = *((int *)(pTilemap) + 19);
98 }
99 m_pTuneLayer = pTilemap;
100 IsEntities = true;
101 }
102
103 if(IsEntities)
104 { // Ensure default color for entities layers
105 pTilemap->m_Color = CColor(255, 255, 255, 255);
106 }
107 }
108 }
109 }
110
111 InitTilemapSkip();
112}
113
114void CLayers::InitBackground(class IMap *pMap)
115{
116 Unload();
117
118 m_pMap = pMap;
119 m_pMap->GetType(Type: MAPITEMTYPE_GROUP, pStart: &m_GroupsStart, pNum: &m_GroupsNum);
120 m_pMap->GetType(Type: MAPITEMTYPE_LAYER, pStart: &m_LayersStart, pNum: &m_LayersNum);
121
122 for(int g = 0; g < NumGroups(); g++)
123 {
124 CMapItemGroup *pGroup = GetGroup(Index: g);
125 for(int l = 0; l < pGroup->m_NumLayers; l++)
126 {
127 CMapItemLayer *pLayer = GetLayer(Index: pGroup->m_StartLayer + l);
128
129 if(pLayer->m_Type == LAYERTYPE_TILES)
130 {
131 CMapItemLayerTilemap *pTilemap = reinterpret_cast<CMapItemLayerTilemap *>(pLayer);
132
133 if(pTilemap->m_Flags & TILESLAYERFLAG_GAME)
134 {
135 m_pGameLayer = pTilemap;
136 m_pGameGroup = pGroup;
137
138 // make sure the game group has standard settings
139 m_pGameGroup->m_OffsetX = 0;
140 m_pGameGroup->m_OffsetY = 0;
141 m_pGameGroup->m_ParallaxX = 100;
142 m_pGameGroup->m_ParallaxY = 100;
143
144 if(m_pGameGroup->m_Version >= 2)
145 {
146 m_pGameGroup->m_UseClipping = 0;
147 m_pGameGroup->m_ClipX = 0;
148 m_pGameGroup->m_ClipY = 0;
149 m_pGameGroup->m_ClipW = 0;
150 m_pGameGroup->m_ClipH = 0;
151 }
152 //We don't care about tile layers.
153 }
154 }
155 }
156 }
157
158 InitTilemapSkip();
159}
160
161void CLayers::Unload()
162{
163 m_GroupsNum = 0;
164 m_GroupsStart = 0;
165 m_LayersNum = 0;
166 m_LayersStart = 0;
167
168 m_pGameGroup = nullptr;
169 m_pGameLayer = nullptr;
170 m_pMap = nullptr;
171
172 m_pTeleLayer = nullptr;
173 m_pSpeedupLayer = nullptr;
174 m_pFrontLayer = nullptr;
175 m_pSwitchLayer = nullptr;
176 m_pTuneLayer = nullptr;
177}
178
179void CLayers::InitTilemapSkip()
180{
181 for(int g = 0; g < NumGroups(); g++)
182 {
183 const CMapItemGroup *pGroup = GetGroup(Index: g);
184
185 for(int l = 0; l < pGroup->m_NumLayers; l++)
186 {
187 const CMapItemLayer *pLayer = GetLayer(Index: pGroup->m_StartLayer + l);
188
189 if(pLayer->m_Type == LAYERTYPE_TILES)
190 {
191 const CMapItemLayerTilemap *pTilemap = (CMapItemLayerTilemap *)pLayer;
192 CTile *pTiles = (CTile *)m_pMap->GetData(Index: pTilemap->m_Data);
193 for(int y = 0; y < pTilemap->m_Height; y++)
194 {
195 for(int x = 1; x < pTilemap->m_Width;)
196 {
197 int SkippedX;
198 for(SkippedX = 1; x + SkippedX < pTilemap->m_Width && SkippedX < 255; SkippedX++)
199 {
200 if(pTiles[y * pTilemap->m_Width + x + SkippedX].m_Index)
201 break;
202 }
203
204 pTiles[y * pTilemap->m_Width + x].m_Skip = SkippedX - 1;
205 x += SkippedX;
206 }
207 }
208 }
209 }
210 }
211}
212
213CMapItemGroup *CLayers::GetGroup(int Index) const
214{
215 return static_cast<CMapItemGroup *>(m_pMap->GetItem(Index: m_GroupsStart + Index));
216}
217
218CMapItemLayer *CLayers::GetLayer(int Index) const
219{
220 return static_cast<CMapItemLayer *>(m_pMap->GetItem(Index: m_LayersStart + Index));
221}
222