1#include "auto_map.h"
2
3#include <base/io.h>
4#include <base/log.h>
5#include <base/str.h>
6
7#include <engine/shared/linereader.h>
8#include <engine/storage.h>
9
10#include <game/editor/editor_actions.h>
11#include <game/editor/mapitems/layer_tiles.h>
12#include <game/editor/mapitems/map.h>
13#include <game/mapitems.h>
14
15#include <cinttypes>
16#include <cstdio> // sscanf
17#include <memory>
18
19// Based on triple32inc from https://github.com/skeeto/hash-prospector/tree/79a6074062a84907df6e45b756134b74e2956760
20static uint32_t HashUInt32(uint32_t Num)
21{
22 Num++;
23 Num ^= Num >> 17;
24 Num *= 0xed5ad4bbu;
25 Num ^= Num >> 11;
26 Num *= 0xac4c1b51u;
27 Num ^= Num >> 15;
28 Num *= 0x31848babu;
29 Num ^= Num >> 14;
30 return Num;
31}
32
33#define HASH_MAX 65536
34
35static int HashLocation(uint32_t Seed, uint32_t Run, uint32_t Rule, uint32_t X, uint32_t Y)
36{
37 const uint32_t Prime = 31;
38 uint32_t Hash = 1;
39 Hash = Hash * Prime + HashUInt32(Num: Seed);
40 Hash = Hash * Prime + HashUInt32(Num: Run);
41 Hash = Hash * Prime + HashUInt32(Num: Rule);
42 Hash = Hash * Prime + HashUInt32(Num: X);
43 Hash = Hash * Prime + HashUInt32(Num: Y);
44 Hash = HashUInt32(Num: Hash * Prime); // Just to double-check that values are well-distributed
45 return Hash % HASH_MAX;
46}
47
48CAutomapper::CAutomapper(CEditorMap *pMap) :
49 CMapObject(pMap)
50{
51}
52
53void CAutomapper::Load(const char *pTileName)
54{
55 char aPath[IO_MAX_PATH_LENGTH];
56 str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "editor/automap/%s.rules", pTileName);
57 if(!Storage()->FileExists(pFilename: aPath, Type: IStorage::TYPE_ALL))
58 {
59 return; // Avoid error message if no rules exist
60 }
61
62 CLineReader LineReader;
63 if(!LineReader.OpenFile(File: Storage()->OpenFile(pFilename: aPath, Flags: IOFLAG_READ, Type: IStorage::TYPE_ALL)))
64 {
65 log_error("editor/automap", "Failed to load rules from '%s'", aPath);
66 return;
67 }
68
69 CConfiguration *pCurrentConf = nullptr;
70 CRun *pCurrentRun = nullptr;
71 CIndexRule *pCurrentIndex = nullptr;
72
73 // read each line
74 while(const char *pLine = LineReader.Get())
75 {
76 // skip blank/empty lines as well as comments
77 if(str_length(str: pLine) > 0 && pLine[0] != '#' && pLine[0] != '\n' && pLine[0] != '\r' && pLine[0] != '\t' && pLine[0] != '\v' && pLine[0] != ' ')
78 {
79 if(pLine[0] == '[')
80 {
81 // new configuration, get the name
82 pLine++;
83 CConfiguration NewConf;
84 NewConf.m_aName[0] = '\0';
85 NewConf.m_StartX = 0;
86 NewConf.m_StartY = 0;
87 NewConf.m_EndX = 0;
88 NewConf.m_EndY = 0;
89 m_vConfigs.push_back(x: NewConf);
90 int ConfigurationId = m_vConfigs.size() - 1;
91 pCurrentConf = &m_vConfigs[ConfigurationId];
92 str_copy(dst: pCurrentConf->m_aName, src: pLine, dst_size: std::min(a: sizeof(pCurrentConf->m_aName), b: (size_t)str_length(str: pLine)));
93
94 // add start run
95 CRun NewRun;
96 NewRun.m_AutomapCopy = true;
97 pCurrentConf->m_vRuns.push_back(x: NewRun);
98 int RunId = pCurrentConf->m_vRuns.size() - 1;
99 pCurrentRun = &pCurrentConf->m_vRuns[RunId];
100 }
101 else if(str_startswith(str: pLine, prefix: "NewRun") && pCurrentConf)
102 {
103 // add new run
104 CRun NewRun;
105 NewRun.m_AutomapCopy = true;
106 pCurrentConf->m_vRuns.push_back(x: NewRun);
107 int RunId = pCurrentConf->m_vRuns.size() - 1;
108 pCurrentRun = &pCurrentConf->m_vRuns[RunId];
109 }
110 else if(str_startswith(str: pLine, prefix: "Index") && pCurrentRun)
111 {
112 // new index
113 CIndexRule NewIndexRule;
114
115 char aOrientation1[128] = "";
116 char aOrientation2[128] = "";
117 char aOrientation3[128] = "";
118
119 sscanf(s: pLine, format: "Index %d %127s %127s %127s", &NewIndexRule.m_Id, aOrientation1, aOrientation2, aOrientation3);
120
121 NewIndexRule.m_Flag = 0;
122 NewIndexRule.m_RandomProbability = 1.0f;
123 NewIndexRule.m_DefaultRule = true;
124 NewIndexRule.m_SkipEmpty = false;
125 NewIndexRule.m_SkipFull = false;
126
127 if(str_length(str: aOrientation1) > 0)
128 NewIndexRule.m_Flag = CheckIndexFlag(Flag: NewIndexRule.m_Flag, pFlag: aOrientation1, CheckNone: false);
129
130 if(str_length(str: aOrientation2) > 0)
131 NewIndexRule.m_Flag = CheckIndexFlag(Flag: NewIndexRule.m_Flag, pFlag: aOrientation2, CheckNone: false);
132
133 if(str_length(str: aOrientation3) > 0)
134 NewIndexRule.m_Flag = CheckIndexFlag(Flag: NewIndexRule.m_Flag, pFlag: aOrientation3, CheckNone: false);
135
136 // add the index rule object and make it current
137 pCurrentRun->m_vIndexRules.push_back(x: NewIndexRule);
138 int IndexRuleId = pCurrentRun->m_vIndexRules.size() - 1;
139 pCurrentIndex = &pCurrentRun->m_vIndexRules[IndexRuleId];
140 }
141 else if(str_startswith(str: pLine, prefix: "Pos") && pCurrentIndex)
142 {
143 int x = 0, y = 0;
144 char aValue[128];
145 int Value = CPosRule::NORULE;
146 std::vector<CIndexInfo> vNewIndexList;
147
148 sscanf(s: pLine, format: "Pos %d %d %127s", &x, &y, aValue);
149
150 if(!str_comp(a: aValue, b: "EMPTY"))
151 {
152 Value = CPosRule::INDEX;
153 CIndexInfo NewIndexInfo = {.m_Id: 0, .m_Flag: 0, .m_TestFlag: false};
154 vNewIndexList.push_back(x: NewIndexInfo);
155 }
156 else if(!str_comp(a: aValue, b: "FULL"))
157 {
158 Value = CPosRule::NOTINDEX;
159 CIndexInfo NewIndexInfo1 = {.m_Id: 0, .m_Flag: 0, .m_TestFlag: false};
160 // CIndexInfo NewIndexInfo2 = {-1, 0};
161 vNewIndexList.push_back(x: NewIndexInfo1);
162 // vNewIndexList.push_back(NewIndexInfo2);
163 }
164 else if(!str_comp(a: aValue, b: "INDEX") || !str_comp(a: aValue, b: "NOTINDEX"))
165 {
166 if(!str_comp(a: aValue, b: "INDEX"))
167 Value = CPosRule::INDEX;
168 else
169 Value = CPosRule::NOTINDEX;
170
171 int pWord = 4;
172 while(true)
173 {
174 CIndexInfo NewIndexInfo;
175
176 char aOrientation1[128] = "";
177 char aOrientation2[128] = "";
178 char aOrientation3[128] = "";
179 char aOrientation4[128] = "";
180 sscanf(s: str_trim_words(str: pLine, words: pWord), format: "%d %127s %127s %127s %127s", &NewIndexInfo.m_Id, aOrientation1, aOrientation2, aOrientation3, aOrientation4);
181
182 NewIndexInfo.m_Flag = 0;
183 NewIndexInfo.m_TestFlag = false;
184
185 if(!str_comp(a: aOrientation1, b: "OR"))
186 {
187 vNewIndexList.push_back(x: NewIndexInfo);
188 pWord += 2;
189 continue;
190 }
191 else if(str_length(str: aOrientation1) > 0)
192 {
193 NewIndexInfo.m_Flag = CheckIndexFlag(Flag: NewIndexInfo.m_Flag, pFlag: aOrientation1, CheckNone: true);
194 NewIndexInfo.m_TestFlag = !(NewIndexInfo.m_Flag == 0 && str_comp(a: aOrientation1, b: "NONE"));
195 }
196 else
197 {
198 vNewIndexList.push_back(x: NewIndexInfo);
199 break;
200 }
201
202 if(!str_comp(a: aOrientation2, b: "OR"))
203 {
204 vNewIndexList.push_back(x: NewIndexInfo);
205 pWord += 3;
206 continue;
207 }
208 else if(str_length(str: aOrientation2) > 0 && NewIndexInfo.m_Flag != 0)
209 {
210 NewIndexInfo.m_Flag = CheckIndexFlag(Flag: NewIndexInfo.m_Flag, pFlag: aOrientation2, CheckNone: false);
211 }
212 else
213 {
214 vNewIndexList.push_back(x: NewIndexInfo);
215 break;
216 }
217
218 if(!str_comp(a: aOrientation3, b: "OR"))
219 {
220 vNewIndexList.push_back(x: NewIndexInfo);
221 pWord += 4;
222 continue;
223 }
224 else if(str_length(str: aOrientation3) > 0 && NewIndexInfo.m_Flag != 0)
225 {
226 NewIndexInfo.m_Flag = CheckIndexFlag(Flag: NewIndexInfo.m_Flag, pFlag: aOrientation3, CheckNone: false);
227 }
228 else
229 {
230 vNewIndexList.push_back(x: NewIndexInfo);
231 break;
232 }
233
234 if(!str_comp(a: aOrientation4, b: "OR"))
235 {
236 vNewIndexList.push_back(x: NewIndexInfo);
237 pWord += 5;
238 continue;
239 }
240 else
241 {
242 vNewIndexList.push_back(x: NewIndexInfo);
243 break;
244 }
245 }
246 }
247
248 if(Value != CPosRule::NORULE)
249 {
250 CPosRule NewPosRule = {.m_X: x, .m_Y: y, .m_Value: Value, .m_vIndexList: vNewIndexList};
251 pCurrentIndex->m_vRules.push_back(x: NewPosRule);
252
253 pCurrentConf->m_StartX = std::min(a: pCurrentConf->m_StartX, b: NewPosRule.m_X);
254 pCurrentConf->m_StartY = std::min(a: pCurrentConf->m_StartY, b: NewPosRule.m_Y);
255 pCurrentConf->m_EndX = std::max(a: pCurrentConf->m_EndX, b: NewPosRule.m_X);
256 pCurrentConf->m_EndY = std::max(a: pCurrentConf->m_EndY, b: NewPosRule.m_Y);
257
258 if(x == 0 && y == 0)
259 {
260 for(const auto &Index : vNewIndexList)
261 {
262 if(Index.m_Id == 0 && Value == CPosRule::INDEX)
263 {
264 // Skip full tiles if we have a rule "POS 0 0 INDEX 0"
265 // because that forces the tile to be empty
266 pCurrentIndex->m_SkipFull = true;
267 }
268 else if((Index.m_Id > 0 && Value == CPosRule::INDEX) || (Index.m_Id == 0 && Value == CPosRule::NOTINDEX))
269 {
270 // Skip empty tiles if we have a rule "POS 0 0 INDEX i" where i > 0
271 // or if we have a rule "POS 0 0 NOTINDEX 0"
272 pCurrentIndex->m_SkipEmpty = true;
273 }
274 }
275 }
276 }
277 }
278 else if(str_startswith(str: pLine, prefix: "Random") && pCurrentIndex)
279 {
280 float Value;
281 char Specifier = ' ';
282 sscanf(s: pLine, format: "Random %f%c", &Value, &Specifier);
283 if(Specifier == '%')
284 {
285 pCurrentIndex->m_RandomProbability = Value / 100.0f;
286 }
287 else
288 {
289 pCurrentIndex->m_RandomProbability = 1.0f / Value;
290 }
291 }
292 else if(str_startswith(str: pLine, prefix: "Modulo") && pCurrentIndex)
293 {
294 CModuloRule NewModuloRule;
295 sscanf(s: pLine, format: "Modulo %d %d %d %d", &NewModuloRule.m_ModX, &NewModuloRule.m_ModY, &NewModuloRule.m_OffsetX, &NewModuloRule.m_OffsetY);
296 if(NewModuloRule.m_ModX == 0)
297 NewModuloRule.m_ModX = 1;
298 if(NewModuloRule.m_ModY == 0)
299 NewModuloRule.m_ModY = 1;
300 pCurrentIndex->m_vModuloRules.push_back(x: NewModuloRule);
301 }
302 else if(str_startswith(str: pLine, prefix: "NoDefaultRule") && pCurrentIndex)
303 {
304 pCurrentIndex->m_DefaultRule = false;
305 }
306 else if(str_startswith(str: pLine, prefix: "NoLayerCopy") && pCurrentRun)
307 {
308 pCurrentRun->m_AutomapCopy = false;
309 }
310 }
311 }
312
313 // add default rule for Pos 0 0 if there is none
314 for(auto &Config : m_vConfigs)
315 {
316 for(auto &Run : Config.m_vRuns)
317 {
318 for(auto &IndexRule : Run.m_vIndexRules)
319 {
320 bool Found = false;
321
322 // Search for the exact rule "POS 0 0 INDEX 0" which corresponds to the default rule
323 for(const auto &Rule : IndexRule.m_vRules)
324 {
325 if(Rule.m_X == 0 && Rule.m_Y == 0 && Rule.m_Value == CPosRule::INDEX)
326 {
327 for(const auto &Index : Rule.m_vIndexList)
328 {
329 if(Index.m_Id == 0)
330 Found = true;
331 }
332 break;
333 }
334
335 if(Found)
336 break;
337 }
338
339 // If the default rule was not found, and we require it, then add it
340 if(!Found && IndexRule.m_DefaultRule)
341 {
342 std::vector<CIndexInfo> vNewIndexList;
343 CIndexInfo NewIndexInfo = {.m_Id: 0, .m_Flag: 0, .m_TestFlag: false};
344 vNewIndexList.push_back(x: NewIndexInfo);
345 CPosRule NewPosRule = {.m_X: 0, .m_Y: 0, .m_Value: CPosRule::NOTINDEX, .m_vIndexList: vNewIndexList};
346 IndexRule.m_vRules.push_back(x: NewPosRule);
347
348 IndexRule.m_SkipEmpty = true;
349 IndexRule.m_SkipFull = false;
350 }
351
352 if(IndexRule.m_SkipEmpty && IndexRule.m_SkipFull)
353 {
354 IndexRule.m_SkipEmpty = false;
355 IndexRule.m_SkipFull = false;
356 }
357 }
358 }
359 }
360
361 log_trace("editor/automap", "Loaded '%s'", aPath);
362 m_FileLoaded = true;
363}
364
365void CAutomapper::Unload()
366{
367 m_FileLoaded = false;
368 m_vConfigs.clear();
369}
370
371int CAutomapper::CheckIndexFlag(int Flag, const char *pFlag, bool CheckNone) const
372{
373 if(!str_comp(a: pFlag, b: "XFLIP"))
374 Flag |= TILEFLAG_XFLIP;
375 else if(!str_comp(a: pFlag, b: "YFLIP"))
376 Flag |= TILEFLAG_YFLIP;
377 else if(!str_comp(a: pFlag, b: "ROTATE"))
378 Flag |= TILEFLAG_ROTATE;
379 else if(!str_comp(a: pFlag, b: "NONE") && CheckNone)
380 Flag = 0;
381
382 return Flag;
383}
384
385const char *CAutomapper::GetConfigName(int Index) const
386{
387 if(Index < 0 || Index >= (int)m_vConfigs.size())
388 {
389 return "(unknown)";
390 }
391 return m_vConfigs[Index].m_aName;
392}
393
394void CAutomapper::ProceedLocalized(CLayerTiles *pLayer, CLayerTiles *pGameLayer, int ReferenceId, int ConfigId, int Seed, int X, int Y, int Width, int Height)
395{
396 if(!m_FileLoaded || pLayer->m_Readonly || ConfigId < 0 || ConfigId >= (int)m_vConfigs.size())
397 return;
398
399 if(Width < 0)
400 Width = pLayer->m_Width;
401
402 if(Height < 0)
403 Height = pLayer->m_Height;
404
405 CConfiguration *pConf = &m_vConfigs[ConfigId];
406
407 int CommitFromX = std::clamp(val: X + pConf->m_StartX, lo: 0, hi: pLayer->m_Width);
408 int CommitFromY = std::clamp(val: Y + pConf->m_StartY, lo: 0, hi: pLayer->m_Height);
409 int CommitToX = std::clamp(val: X + Width + pConf->m_EndX, lo: 0, hi: pLayer->m_Width);
410 int CommitToY = std::clamp(val: Y + Height + pConf->m_EndY, lo: 0, hi: pLayer->m_Height);
411
412 int UpdateFromX = std::clamp(val: X + 3 * pConf->m_StartX, lo: 0, hi: pLayer->m_Width);
413 int UpdateFromY = std::clamp(val: Y + 3 * pConf->m_StartY, lo: 0, hi: pLayer->m_Height);
414 int UpdateToX = std::clamp(val: X + Width + 3 * pConf->m_EndX, lo: 0, hi: pLayer->m_Width);
415 int UpdateToY = std::clamp(val: Y + Height + 3 * pConf->m_EndY, lo: 0, hi: pLayer->m_Height);
416
417 CLayerTiles *pUpdateLayer = new CLayerTiles(pLayer->Map(), UpdateToX - UpdateFromX, UpdateToY - UpdateFromY);
418 CLayerTiles *pUpdateGame = new CLayerTiles(pLayer->Map(), UpdateToX - UpdateFromX, UpdateToY - UpdateFromY);
419
420 for(int y = UpdateFromY; y < UpdateToY; y++)
421 {
422 for(int x = UpdateFromX; x < UpdateToX; x++)
423 {
424 const CTile *pInLayer = &pLayer->m_pTiles[y * pLayer->m_Width + x];
425 CTile *pOutLayer = &pUpdateLayer->m_pTiles[(y - UpdateFromY) * pUpdateLayer->m_Width + x - UpdateFromX];
426 pOutLayer->m_Index = pInLayer->m_Index;
427 pOutLayer->m_Flags = pInLayer->m_Flags;
428
429 if(x >= pGameLayer->m_Width || y >= pGameLayer->m_Height)
430 continue;
431
432 const CTile *pInGame = &pGameLayer->m_pTiles[y * pGameLayer->m_Width + x];
433 CTile *pOutGame = &pUpdateGame->m_pTiles[(y - UpdateFromY) * pUpdateGame->m_Width + x - UpdateFromX];
434 pOutGame->m_Index = pInGame->m_Index;
435 pOutGame->m_Flags = pInGame->m_Flags;
436 }
437 }
438
439 Proceed(pLayer: pUpdateLayer, pGameLayer: pUpdateGame, ReferenceId, ConfigId, Seed, SeedOffsetX: UpdateFromX, SeedOffsetY: UpdateFromY);
440
441 for(int y = CommitFromY; y < CommitToY; y++)
442 {
443 for(int x = CommitFromX; x < CommitToX; x++)
444 {
445 const CTile *pInLayer = &pUpdateLayer->m_pTiles[(y - UpdateFromY) * pUpdateLayer->m_Width + x - UpdateFromX];
446 CTile *pOutLayer = &pLayer->m_pTiles[y * pLayer->m_Width + x];
447 CTile PreviousLayer = *pOutLayer;
448 pOutLayer->m_Index = pInLayer->m_Index;
449 pOutLayer->m_Flags = pInLayer->m_Flags;
450 pLayer->RecordStateChange(x, y, Previous: PreviousLayer, Tile: *pOutLayer);
451
452 if(x >= pGameLayer->m_Width || y >= pGameLayer->m_Height)
453 continue;
454
455 const CTile *pInGame = &pUpdateGame->m_pTiles[(y - UpdateFromY) * pUpdateGame->m_Width + x - UpdateFromX];
456 CTile *pOutGame = &pGameLayer->m_pTiles[y * pGameLayer->m_Width + x];
457 CTile PreviousGame = *pOutGame;
458 pOutGame->m_Index = pInGame->m_Index;
459 pOutGame->m_Flags = pInGame->m_Flags;
460 pGameLayer->RecordStateChange(x, y, Previous: PreviousGame, Tile: *pOutGame);
461 }
462 }
463
464 delete pUpdateLayer;
465 delete pUpdateGame;
466}
467
468void CAutomapper::AutoMap(CLayerTiles *pLayer, const CLayerTiles *pReadLayer, const CRun *pRun, size_t RunIndex, bool IsFilterable, int Seed, int SeedOffsetX, int SeedOffsetY) const
469{
470 const int LayerWidth = pLayer->m_Width;
471 const int LayerHeight = pLayer->m_Height;
472
473 for(int y = 0; y < LayerHeight; y++)
474 {
475 for(int x = 0; x < LayerWidth; x++)
476 {
477 CTile *pTile = &(pLayer->m_pTiles[y * LayerWidth + x]);
478 const CTile *pReadTile = &(pReadLayer->m_pTiles[y * LayerWidth + x]);
479 pLayer->Map()->OnModify();
480
481 for(size_t i = 0; i < pRun->m_vIndexRules.size(); ++i)
482 {
483 const CIndexRule *pIndexRule = &pRun->m_vIndexRules[i];
484 if(pReadTile->m_Index == 0)
485 {
486 if(pTile->m_Index != 0 && IsFilterable) // TODO: This is a lazy workaround
487 {
488 CTile Previous = *pTile;
489 pTile->m_Index = 0;
490 pTile->m_Flags = pIndexRule->m_Flag;
491 pLayer->RecordStateChange(x, y, Previous, Tile: *pTile);
492 continue;
493 }
494
495 if(pIndexRule->m_SkipEmpty) // skip empty tiles
496 continue;
497 }
498 if(pIndexRule->m_SkipFull && pReadTile->m_Index != 0) // skip full tiles
499 continue;
500
501 bool RespectRules = true;
502 for(size_t j = 0; j < pIndexRule->m_vRules.size() && RespectRules; ++j)
503 {
504 const CPosRule *pRule = &pIndexRule->m_vRules[j];
505
506 int CheckIndex, CheckFlags;
507 int CheckX = x + pRule->m_X;
508 int CheckY = y + pRule->m_Y;
509 if(CheckX >= 0 && CheckX < LayerWidth && CheckY >= 0 && CheckY < LayerHeight)
510 {
511 int CheckTile = CheckY * LayerWidth + CheckX;
512 CheckIndex = pReadLayer->m_pTiles[CheckTile].m_Index;
513 CheckFlags = pReadLayer->m_pTiles[CheckTile].m_Flags & (TILEFLAG_ROTATE | TILEFLAG_XFLIP | TILEFLAG_YFLIP);
514 }
515 else
516 {
517 CheckIndex = -1;
518 CheckFlags = 0;
519 }
520
521 if(pRule->m_Value == CPosRule::INDEX)
522 {
523 RespectRules = false;
524 for(const auto &Index : pRule->m_vIndexList)
525 {
526 if(CheckIndex == Index.m_Id && (!Index.m_TestFlag || CheckFlags == Index.m_Flag))
527 {
528 RespectRules = true;
529 break;
530 }
531 }
532 }
533 else if(pRule->m_Value == CPosRule::NOTINDEX)
534 {
535 for(const auto &Index : pRule->m_vIndexList)
536 {
537 if(CheckIndex == Index.m_Id && (!Index.m_TestFlag || CheckFlags == Index.m_Flag))
538 {
539 RespectRules = false;
540 break;
541 }
542 }
543 }
544 }
545
546 bool PassesModuloCheck;
547 if(pIndexRule->m_vModuloRules.empty())
548 PassesModuloCheck = true;
549 else
550 PassesModuloCheck = std::any_of(first: pIndexRule->m_vModuloRules.cbegin(), last: pIndexRule->m_vModuloRules.cend(), pred: [&](const CModuloRule &ModuloRule) {
551 return (x + SeedOffsetX + ModuloRule.m_OffsetX) % ModuloRule.m_ModX == 0 && (y + SeedOffsetY + ModuloRule.m_OffsetY) % ModuloRule.m_ModY == 0;
552 });
553
554 if(RespectRules && PassesModuloCheck &&
555 (pIndexRule->m_RandomProbability >= 1.0f || HashLocation(Seed, Run: RunIndex, Rule: i, X: x + SeedOffsetX, Y: y + SeedOffsetY) < HASH_MAX * pIndexRule->m_RandomProbability))
556 {
557 CTile Previous = *pTile;
558 pTile->m_Index = pIndexRule->m_Id;
559 pTile->m_Flags = pIndexRule->m_Flag;
560 pLayer->RecordStateChange(x, y, Previous, Tile: *pTile);
561 }
562 }
563 }
564 }
565}
566
567void CAutomapper::Proceed(CLayerTiles *pLayer, CLayerTiles *pGameLayer, int ReferenceId, int ConfigId, int Seed, int SeedOffsetX, int SeedOffsetY)
568{
569 if(!m_FileLoaded || pLayer->m_Readonly || ConfigId < 0 || ConfigId >= (int)m_vConfigs.size())
570 return;
571
572 if(Seed == 0)
573 Seed = rand();
574
575 CConfiguration *pConf = &m_vConfigs[ConfigId];
576 pLayer->ClearHistory();
577
578 const int LayerWidth = pLayer->m_Width;
579 const int LayerHeight = pLayer->m_Height;
580
581 static const int s_aTileIndex[] = {TILE_SOLID, TILE_DEATH, TILE_NOHOOK, TILE_FREEZE, TILE_UNFREEZE, TILE_DFREEZE, TILE_DUNFREEZE, TILE_LFREEZE, TILE_LUNFREEZE};
582
583 static_assert(std::size(AUTOMAP_REFERENCE_NAMES) == std::size(s_aTileIndex) + 1, "AUTOMAP_REFERENCE_NAMES and s_aTileIndex must include the same items");
584
585 // for every run: copy tiles, automap, overwrite tiles
586 for(size_t h = 0; h < pConf->m_vRuns.size(); ++h)
587 {
588 CRun *pRun = &pConf->m_vRuns[h];
589 bool IsFilterable = h == 0 && ReferenceId >= 0;
590
591 // don't make copy if it's requested
592 CLayerTiles *pBuffer = IsFilterable ? pGameLayer : pLayer;
593 if(pRun->m_AutomapCopy)
594 {
595 std::unique_ptr<CLayerTiles> pCopiedLayer = std::make_unique<CLayerTiles>(args: pLayer->Map(), args: LayerWidth, args: LayerHeight);
596
597 int LoopWidth = IsFilterable ? std::min(a: pGameLayer->m_Width, b: LayerWidth) : LayerWidth;
598 int LoopHeight = IsFilterable ? std::min(a: pGameLayer->m_Height, b: LayerHeight) : LayerHeight;
599
600 for(int y = 0; y < LoopHeight; y++)
601 {
602 for(int x = 0; x < LoopWidth; x++)
603 {
604 const CTile *pIn = &pBuffer->m_pTiles[y * pBuffer->m_Width + x];
605 CTile *pOut = &pCopiedLayer->m_pTiles[y * LayerWidth + x];
606 if(h == 0 && ReferenceId >= 1 && pIn->m_Index != s_aTileIndex[ReferenceId - 1])
607 pOut->m_Index = 0;
608 else
609 pOut->m_Index = pIn->m_Index;
610 pOut->m_Flags = pIn->m_Flags;
611 }
612 }
613
614 AutoMap(pLayer, pReadLayer: pCopiedLayer.get(), pRun, RunIndex: h, IsFilterable, Seed, SeedOffsetX, SeedOffsetY);
615 }
616 else
617 {
618 AutoMap(pLayer, pReadLayer: pBuffer, pRun, RunIndex: h, IsFilterable, Seed, SeedOffsetX, SeedOffsetY);
619 }
620 }
621}
622