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