1#include "layer_switch.h"
2
3#include <game/editor/editor.h>
4
5CLayerSwitch::CLayerSwitch(CEditorMap *pMap, int w, int h) :
6 CLayerTiles(pMap, w, h)
7{
8 str_copy(dst&: m_aName, src: "Switch");
9 m_HasSwitch = true;
10
11 m_pSwitchTile = new CSwitchTile[w * h];
12 mem_zero(block: m_pSwitchTile, size: (size_t)w * h * sizeof(CSwitchTile));
13 m_GotoSwitchLastPos = ivec2(-1, -1);
14 m_GotoSwitchOffset = 0;
15}
16
17CLayerSwitch::CLayerSwitch(const CLayerSwitch &Other) :
18 CLayerTiles(Other)
19{
20 str_copy(dst&: m_aName, src: "Switch copy");
21 m_HasSwitch = true;
22
23 m_pSwitchTile = new CSwitchTile[m_Width * m_Height];
24 mem_copy(dest: m_pSwitchTile, source: Other.m_pSwitchTile, size: (size_t)m_Width * m_Height * sizeof(CSwitchTile));
25}
26
27CLayerSwitch::~CLayerSwitch()
28{
29 delete[] m_pSwitchTile;
30}
31
32void CLayerSwitch::Resize(int NewW, int NewH)
33{
34 // resize switch data
35 CSwitchTile *pNewSwitchData = new CSwitchTile[NewW * NewH];
36 mem_zero(block: pNewSwitchData, size: (size_t)NewW * NewH * sizeof(CSwitchTile));
37
38 // copy old data
39 for(int y = 0; y < std::min(a: NewH, b: m_Height); y++)
40 mem_copy(dest: &pNewSwitchData[y * NewW], source: &m_pSwitchTile[y * m_Width], size: std::min(a: m_Width, b: NewW) * sizeof(CSwitchTile));
41
42 // replace old
43 delete[] m_pSwitchTile;
44 m_pSwitchTile = pNewSwitchData;
45
46 // resize tile data
47 CLayerTiles::Resize(NewW, NewH);
48
49 // resize gamelayer too
50 if(Map()->m_pGameLayer->m_Width != NewW || Map()->m_pGameLayer->m_Height != NewH)
51 Map()->m_pGameLayer->Resize(NewW, NewH);
52}
53
54void CLayerSwitch::Shift(EShiftDirection Direction)
55{
56 CLayerTiles::Shift(Direction);
57 ShiftImpl(pTiles: m_pSwitchTile, Direction, ShiftBy: Map()->m_ShiftBy);
58}
59
60bool CLayerSwitch::IsEmpty() const
61{
62 for(int y = 0; y < m_Height; y++)
63 {
64 for(int x = 0; x < m_Width; x++)
65 {
66 const int Index = GetTile(x, y).m_Index;
67 if(Index == 0)
68 {
69 continue;
70 }
71 if(Editor()->IsAllowPlaceUnusedTiles() || IsValidSwitchTile(Index))
72 {
73 return false;
74 }
75 }
76 }
77 return true;
78}
79
80void CLayerSwitch::BrushDraw(CLayer *pBrush, vec2 WorldPos)
81{
82 if(m_Readonly)
83 return;
84
85 CLayerSwitch *pSwitchLayer = static_cast<CLayerSwitch *>(pBrush);
86 int sx = ConvertX(x: WorldPos.x);
87 int sy = ConvertY(y: WorldPos.y);
88 if(str_comp(a: pSwitchLayer->m_aFilename, b: pSwitchLayer->Map()->m_aFilename))
89 {
90 Editor()->m_SwitchNumber = pSwitchLayer->m_SwitchNumber;
91 Editor()->m_SwitchDelay = pSwitchLayer->m_SwitchDelay;
92 }
93
94 bool Destructive = Editor()->m_BrushDrawDestructive || pSwitchLayer->IsEmpty();
95
96 for(int y = 0; y < pSwitchLayer->m_Height; y++)
97 for(int x = 0; x < pSwitchLayer->m_Width; x++)
98 {
99 int fx = x + sx;
100 int fy = y + sy;
101
102 if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height)
103 continue;
104
105 if(!Destructive && GetTile(x: fx, y: fy).m_Index)
106 continue;
107
108 const int SrcIndex = y * pSwitchLayer->m_Width + x;
109 const int TgtIndex = fy * m_Width + fx;
110
111 SSwitchTileStateChange::SData Previous{
112 .m_Number: m_pSwitchTile[TgtIndex].m_Number,
113 .m_Type: m_pSwitchTile[TgtIndex].m_Type,
114 .m_Flags: m_pSwitchTile[TgtIndex].m_Flags,
115 .m_Delay: m_pSwitchTile[TgtIndex].m_Delay,
116 .m_Index: m_pTiles[TgtIndex].m_Index};
117
118 if((Editor()->IsAllowPlaceUnusedTiles() || IsValidSwitchTile(Index: pSwitchLayer->m_pTiles[SrcIndex].m_Index)) && pSwitchLayer->m_pTiles[SrcIndex].m_Index != TILE_AIR)
119 {
120 if(Editor()->m_SwitchNumber != pSwitchLayer->m_SwitchNumber || Editor()->m_SwitchDelay != pSwitchLayer->m_SwitchDelay)
121 {
122 m_pSwitchTile[TgtIndex].m_Number = Editor()->m_SwitchNumber;
123 m_pSwitchTile[TgtIndex].m_Delay = Editor()->m_SwitchDelay;
124 }
125 else if(pSwitchLayer->m_pSwitchTile[SrcIndex].m_Number)
126 {
127 m_pSwitchTile[TgtIndex].m_Number = pSwitchLayer->m_pSwitchTile[SrcIndex].m_Number;
128 m_pSwitchTile[TgtIndex].m_Delay = pSwitchLayer->m_pSwitchTile[SrcIndex].m_Delay;
129 }
130 else
131 {
132 m_pSwitchTile[TgtIndex].m_Number = Editor()->m_SwitchNumber;
133 m_pSwitchTile[TgtIndex].m_Delay = Editor()->m_SwitchDelay;
134 }
135
136 m_pSwitchTile[TgtIndex].m_Type = pSwitchLayer->m_pTiles[SrcIndex].m_Index;
137 m_pSwitchTile[TgtIndex].m_Flags = pSwitchLayer->m_pTiles[SrcIndex].m_Flags;
138 m_pTiles[TgtIndex].m_Index = pSwitchLayer->m_pTiles[SrcIndex].m_Index;
139 m_pTiles[TgtIndex].m_Flags = pSwitchLayer->m_pTiles[SrcIndex].m_Flags;
140
141 if(!IsSwitchTileFlagsUsed(Index: pSwitchLayer->m_pTiles[SrcIndex].m_Index))
142 {
143 m_pSwitchTile[TgtIndex].m_Flags = 0;
144 }
145 if(!IsSwitchTileNumberUsed(Index: pSwitchLayer->m_pTiles[SrcIndex].m_Index))
146 {
147 m_pSwitchTile[TgtIndex].m_Number = 0;
148 }
149 if(!IsSwitchTileDelayUsed(Index: pSwitchLayer->m_pTiles[SrcIndex].m_Index))
150 {
151 m_pSwitchTile[TgtIndex].m_Delay = 0;
152 }
153 }
154 else
155 {
156 m_pSwitchTile[TgtIndex].m_Number = 0;
157 m_pSwitchTile[TgtIndex].m_Type = 0;
158 m_pSwitchTile[TgtIndex].m_Flags = 0;
159 m_pSwitchTile[TgtIndex].m_Delay = 0;
160 m_pTiles[TgtIndex].m_Index = 0;
161
162 if(pSwitchLayer->m_pTiles[SrcIndex].m_Index != TILE_AIR)
163 ShowPreventUnusedTilesWarning();
164 }
165
166 SSwitchTileStateChange::SData Current{
167 .m_Number: m_pSwitchTile[TgtIndex].m_Number,
168 .m_Type: m_pSwitchTile[TgtIndex].m_Type,
169 .m_Flags: m_pSwitchTile[TgtIndex].m_Flags,
170 .m_Delay: m_pSwitchTile[TgtIndex].m_Delay,
171 .m_Index: m_pTiles[TgtIndex].m_Index};
172
173 RecordStateChange(x: fx, y: fy, Previous, Current);
174 }
175 FlagModified(x: sx, y: sy, w: pSwitchLayer->m_Width, h: pSwitchLayer->m_Height);
176}
177
178void CLayerSwitch::RecordStateChange(int x, int y, SSwitchTileStateChange::SData Previous, SSwitchTileStateChange::SData Current)
179{
180 SSwitchTileStateChange &Change = m_History[y][x];
181 if(Change.m_Changed)
182 {
183 Change.m_Current = Current;
184 }
185 else
186 {
187 Change = SSwitchTileStateChange{.m_Changed: true, .m_Previous: Previous, .m_Current: Current};
188 }
189}
190
191void CLayerSwitch::BrushFlipX()
192{
193 CLayerTiles::BrushFlipX();
194 BrushFlipXImpl(pTiles: m_pSwitchTile);
195}
196
197void CLayerSwitch::BrushFlipY()
198{
199 CLayerTiles::BrushFlipY();
200 BrushFlipYImpl(pTiles: m_pSwitchTile);
201}
202
203void CLayerSwitch::BrushRotate(float Amount)
204{
205 int Rotation = (round_to_int(f: 360.0f * Amount / (pi * 2)) / 90) % 4; // 0=0°, 1=90°, 2=180°, 3=270°
206 if(Rotation < 0)
207 Rotation += 4;
208
209 if(Rotation == 1 || Rotation == 3)
210 {
211 // 90° rotation
212 CSwitchTile *pTempData1 = new CSwitchTile[m_Width * m_Height];
213 CTile *pTempData2 = new CTile[m_Width * m_Height];
214 mem_copy(dest: pTempData1, source: m_pSwitchTile, size: (size_t)m_Width * m_Height * sizeof(CSwitchTile));
215 mem_copy(dest: pTempData2, source: m_pTiles, size: (size_t)m_Width * m_Height * sizeof(CTile));
216 CSwitchTile *pDst1 = m_pSwitchTile;
217 CTile *pDst2 = m_pTiles;
218 for(int x = 0; x < m_Width; ++x)
219 for(int y = m_Height - 1; y >= 0; --y, ++pDst1, ++pDst2)
220 {
221 *pDst1 = pTempData1[y * m_Width + x];
222 *pDst2 = pTempData2[y * m_Width + x];
223 if(IsRotatableTile(Index: pDst2->m_Index))
224 {
225 if(pDst2->m_Flags & TILEFLAG_ROTATE)
226 pDst2->m_Flags ^= (TILEFLAG_YFLIP | TILEFLAG_XFLIP);
227 pDst2->m_Flags ^= TILEFLAG_ROTATE;
228 }
229 }
230
231 std::swap(a&: m_Width, b&: m_Height);
232 delete[] pTempData1;
233 delete[] pTempData2;
234 }
235
236 if(Rotation == 2 || Rotation == 3)
237 {
238 BrushFlipX();
239 BrushFlipY();
240 }
241}
242
243void CLayerSwitch::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect)
244{
245 if(m_Readonly || (!Empty && pBrush->m_Type != LAYERTYPE_TILES))
246 return;
247
248 Snap(pRect: &Rect);
249
250 int sx = ConvertX(x: Rect.x);
251 int sy = ConvertY(y: Rect.y);
252 int w = ConvertX(x: Rect.w);
253 int h = ConvertY(y: Rect.h);
254
255 CLayerSwitch *pLt = static_cast<CLayerSwitch *>(pBrush);
256
257 bool Destructive = Editor()->m_BrushDrawDestructive || Empty || pLt->IsEmpty();
258
259 for(int y = 0; y < h; y++)
260 {
261 for(int x = 0; x < w; x++)
262 {
263 int fx = x + sx;
264 int fy = y + sy;
265
266 if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height)
267 continue;
268
269 if(!Destructive && GetTile(x: fx, y: fy).m_Index)
270 continue;
271
272 const int SrcIndex = Empty ? 0 : (y * pLt->m_Width + x % pLt->m_Width) % (pLt->m_Width * pLt->m_Height);
273 const int TgtIndex = fy * m_Width + fx;
274
275 SSwitchTileStateChange::SData Previous{
276 .m_Number: m_pSwitchTile[TgtIndex].m_Number,
277 .m_Type: m_pSwitchTile[TgtIndex].m_Type,
278 .m_Flags: m_pSwitchTile[TgtIndex].m_Flags,
279 .m_Delay: m_pSwitchTile[TgtIndex].m_Delay,
280 .m_Index: m_pTiles[TgtIndex].m_Index};
281
282 if(Empty || (!Editor()->IsAllowPlaceUnusedTiles() && !IsValidSwitchTile(Index: (pLt->m_pTiles[SrcIndex]).m_Index)))
283 {
284 m_pTiles[TgtIndex].m_Index = 0;
285 m_pSwitchTile[TgtIndex].m_Type = 0;
286 m_pSwitchTile[TgtIndex].m_Number = 0;
287 m_pSwitchTile[TgtIndex].m_Delay = 0;
288
289 if(!Empty)
290 ShowPreventUnusedTilesWarning();
291 }
292 else
293 {
294 m_pTiles[TgtIndex] = pLt->m_pTiles[SrcIndex];
295 m_pSwitchTile[TgtIndex].m_Type = m_pTiles[TgtIndex].m_Index;
296 if(pLt->m_HasSwitch && m_pTiles[TgtIndex].m_Index > 0)
297 {
298 if(!IsSwitchTileNumberUsed(Index: m_pSwitchTile[TgtIndex].m_Type))
299 m_pSwitchTile[TgtIndex].m_Number = 0;
300 else if(pLt->m_pSwitchTile[SrcIndex].m_Number == 0 || Editor()->m_SwitchNumber != pLt->m_SwitchNumber)
301 m_pSwitchTile[TgtIndex].m_Number = Editor()->m_SwitchNumber;
302 else
303 m_pSwitchTile[TgtIndex].m_Number = pLt->m_pSwitchTile[SrcIndex].m_Number;
304
305 if(!IsSwitchTileDelayUsed(Index: m_pSwitchTile[TgtIndex].m_Type))
306 m_pSwitchTile[TgtIndex].m_Delay = 0;
307 else if(pLt->m_pSwitchTile[SrcIndex].m_Delay == 0 || Editor()->m_SwitchDelay != pLt->m_SwitchDelay)
308 m_pSwitchTile[TgtIndex].m_Delay = Editor()->m_SwitchDelay;
309 else
310 m_pSwitchTile[TgtIndex].m_Delay = pLt->m_pSwitchTile[SrcIndex].m_Delay;
311
312 if(!IsSwitchTileFlagsUsed(Index: m_pSwitchTile[TgtIndex].m_Type))
313 m_pSwitchTile[TgtIndex].m_Flags = 0;
314 else
315 m_pSwitchTile[TgtIndex].m_Flags = pLt->m_pSwitchTile[SrcIndex].m_Flags;
316 }
317 else
318 {
319 m_pTiles[TgtIndex].m_Index = 0;
320 m_pSwitchTile[TgtIndex].m_Type = 0;
321 m_pSwitchTile[TgtIndex].m_Number = 0;
322 m_pSwitchTile[TgtIndex].m_Delay = 0;
323 }
324 }
325
326 SSwitchTileStateChange::SData Current{
327 .m_Number: m_pSwitchTile[TgtIndex].m_Number,
328 .m_Type: m_pSwitchTile[TgtIndex].m_Type,
329 .m_Flags: m_pSwitchTile[TgtIndex].m_Flags,
330 .m_Delay: m_pSwitchTile[TgtIndex].m_Delay,
331 .m_Index: m_pTiles[TgtIndex].m_Index};
332
333 RecordStateChange(x: fx, y: fy, Previous, Current);
334 }
335 }
336 FlagModified(x: sx, y: sy, w, h);
337}
338
339int CLayerSwitch::FindNextFreeNumber() const
340{
341 for(int i = 1; i <= 255; i++)
342 {
343 if(!ContainsElementWithId(Id: i))
344 {
345 return i;
346 }
347 }
348 return -1;
349}
350
351bool CLayerSwitch::ContainsElementWithId(int Id) const
352{
353 for(int y = 0; y < m_Height; ++y)
354 {
355 for(int x = 0; x < m_Width; ++x)
356 {
357 if(IsSwitchTileNumberUsed(Index: m_pSwitchTile[y * m_Width + x].m_Type) && m_pSwitchTile[y * m_Width + x].m_Number == Id)
358 {
359 return true;
360 }
361 }
362 }
363
364 return false;
365}
366
367void CLayerSwitch::GetPos(int Number, int Offset, ivec2 &SwitchPos)
368{
369 int Match = -1;
370 ivec2 MatchPos = ivec2(-1, -1);
371 SwitchPos = ivec2(-1, -1);
372
373 auto FindTile = [this, &Match, &MatchPos, &Number, &Offset]() {
374 for(int x = 0; x < m_Width; x++)
375 {
376 for(int y = 0; y < m_Height; y++)
377 {
378 int i = y * m_Width + x;
379 int Switch = m_pSwitchTile[i].m_Number;
380 if(Number == Switch)
381 {
382 Match++;
383 if(Offset != -1)
384 {
385 if(Match == Offset)
386 {
387 MatchPos = ivec2(x, y);
388 m_GotoSwitchOffset = Match;
389 return;
390 }
391 continue;
392 }
393 MatchPos = ivec2(x, y);
394 if(m_GotoSwitchLastPos != ivec2(-1, -1))
395 {
396 if(distance(a: m_GotoSwitchLastPos, b: MatchPos) < 10.0f)
397 {
398 m_GotoSwitchOffset++;
399 continue;
400 }
401 }
402 m_GotoSwitchLastPos = MatchPos;
403 if(Match == m_GotoSwitchOffset)
404 return;
405 }
406 }
407 }
408 };
409 FindTile();
410
411 if(MatchPos == ivec2(-1, -1))
412 return;
413 if(Match < m_GotoSwitchOffset)
414 m_GotoSwitchOffset = -1;
415 SwitchPos = MatchPos;
416 m_GotoSwitchOffset++;
417}
418
419std::shared_ptr<CLayer> CLayerSwitch::Duplicate() const
420{
421 return std::make_shared<CLayerSwitch>(args: *this);
422}
423
424const char *CLayerSwitch::TypeName() const
425{
426 return "switch";
427}
428