Add getblockat function

This commit is contained in:
Miloslav Ciz 2023-08-08 16:17:51 +02:00
parent e2d12804d2
commit e678c68b66
3 changed files with 45 additions and 0 deletions

33
map.h
View file

@ -16,6 +16,7 @@
The STORAGE map format is binary and consists of the following values:
- 76, 77 (for "LM", magic number)
- one byte recording the map environment
- ASCII map name
- 10 (separator)
- ASCII comment
@ -95,6 +96,8 @@ struct
uint8_t blocks[LCR_SETTING_MAP_MAX_SIZE * 4];
uint32_t startPos;
uint8_t environment;
// TODO: name, desc? possibly as a single '\n' separated string?
} LCR_currentMap;
@ -132,6 +135,32 @@ uint32_t LCR_mapBlockGetCoordNumber(const uint8_t block[4])
((((uint32_t) block[3]) & 0x3) << 16);
}
uint8_t *LCR_getMapBlockAtCoordNumber(uint32_t coord)
{
// binary search the block:
uint16_t a = 0, b = LCR_currentMap.blockCount - 1;
while (b >= a)
{
uint16_t mid = (a + b) / 2;
uint8_t *block = LCR_currentMap.blocks + mid * 4;
uint32_t coord2 =
LCR_mapBlockGetCoordNumber(block);
if (coord2 == coord)
return block;
else if (coord2 > coord)
b = mid - 1;
else
a = mid + 1;
}
return 0;
}
/**
Adds given block to current map, including possibly deleting a block by
adding LCR_BLOCK_NONE. The function handles sorting the block to the right
@ -213,6 +242,10 @@ uint8_t LCR_mapLoad(const uint8_t *map)
map++;
LCR_currentMap.environment = *map;
map++;
while (*map != LCR_MAP_TERMINATOR)
{
if (!_LCR_mapAddBlock(map))