Write to data file
This commit is contained in:
parent
9fb117901d
commit
34704a934a
3 changed files with 91 additions and 35 deletions
118
game.h
118
game.h
|
@ -243,8 +243,8 @@ static inline void LCR_gameDrawPixelXYSafe(unsigned int x, unsigned int y,
|
|||
|
||||
#define LCR_MENU_STRING_SIZE 16
|
||||
|
||||
#define LCR_RESOURCE_FILE_SEPARATOR '#'
|
||||
#define LCR_RESOURCE_FILE_SEPARATOR2 ';'
|
||||
#define LCR_DATA_FILE_SEPARATOR '#'
|
||||
#define LCR_DATA_FILE_SEPARATOR2 ';'
|
||||
|
||||
#define LCR_FREE_CAMERA_STEP \
|
||||
((LCR_SETTING_FREE_CAMERA_SPEED * LCR_GAME_UNIT / 8) \
|
||||
|
@ -611,7 +611,7 @@ void LCR_gameRewindDataFile(void)
|
|||
*/
|
||||
char LCR_gameGetNextDataFileChar(void)
|
||||
{
|
||||
#if LCR_SETTING_ENABLE_RESOURCE_FILE
|
||||
#if LCR_SETTING_ENABLE_DATA_FILE
|
||||
char c;
|
||||
|
||||
if (LCR_game.dataFile.state < 0) // external file?
|
||||
|
@ -653,7 +653,7 @@ char LCR_gameGetNextDataFileChar(void)
|
|||
char LCR_gameGetNextDataStrChar(void)
|
||||
{
|
||||
char c = LCR_gameGetNextDataFileChar();
|
||||
return c != LCR_RESOURCE_FILE_SEPARATOR ? c : 0;
|
||||
return c != LCR_DATA_FILE_SEPARATOR ? c : 0;
|
||||
}
|
||||
|
||||
unsigned int LCR_countData(char magicNumber)
|
||||
|
@ -669,7 +669,7 @@ unsigned int LCR_countData(char magicNumber)
|
|||
if (c == magicNumber)
|
||||
result++;
|
||||
|
||||
while (c != 0 && c != LCR_RESOURCE_FILE_SEPARATOR)
|
||||
while (c != 0 && c != LCR_DATA_FILE_SEPARATOR)
|
||||
c = LCR_gameGetNextDataFileChar();
|
||||
|
||||
if (c == 0)
|
||||
|
@ -703,7 +703,7 @@ void LCR_seekDataByIndex(unsigned int index, char magicNumber)
|
|||
return;
|
||||
}
|
||||
|
||||
while (c != 0 && c != LCR_RESOURCE_FILE_SEPARATOR)
|
||||
while (c != 0 && c != LCR_DATA_FILE_SEPARATOR)
|
||||
c = LCR_gameGetNextDataFileChar();
|
||||
|
||||
} while (c);
|
||||
|
@ -727,8 +727,8 @@ uint8_t LCR_mapIsBeaten(const char *name)
|
|||
|
||||
if (name[i] == 0)
|
||||
{
|
||||
if (c < ' ' || c == LCR_RESOURCE_FILE_SEPARATOR ||
|
||||
c == LCR_RESOURCE_FILE_SEPARATOR2 || c == 0)
|
||||
if (c < ' ' || c == LCR_DATA_FILE_SEPARATOR ||
|
||||
c == LCR_DATA_FILE_SEPARATOR2 || c == 0)
|
||||
return 1;
|
||||
else
|
||||
break;
|
||||
|
@ -749,26 +749,25 @@ uint8_t LCR_mapIsBeaten(const char *name)
|
|||
|
||||
void LCR_gameLoadMap(unsigned int mapIndex)
|
||||
{
|
||||
char mapName[LCR_MAP_NAME_MAX_LEN];
|
||||
char name[LCR_MAP_NAME_MAX_LEN + 1];
|
||||
name[0] = 0;
|
||||
|
||||
LCR_seekDataByIndex(mapIndex,'M');
|
||||
|
||||
mapName[0] = 0;
|
||||
|
||||
for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i)
|
||||
{
|
||||
char c = LCR_gameGetNextDataFileChar();
|
||||
|
||||
if (c == LCR_RESOURCE_FILE_SEPARATOR2 ||
|
||||
c == LCR_RESOURCE_FILE_SEPARATOR || c == 0)
|
||||
if (c == LCR_DATA_FILE_SEPARATOR2 ||
|
||||
c == LCR_DATA_FILE_SEPARATOR || c == 0)
|
||||
break;
|
||||
|
||||
mapName[i] = c;
|
||||
mapName[i + 1] = 0;
|
||||
name[i] = c;
|
||||
name[i + 1] = 0;
|
||||
}
|
||||
|
||||
LCR_mapLoadFromStr(LCR_gameGetNextDataStrChar,mapName);
|
||||
LCR_game.mapBeaten = LCR_mapIsBeaten(mapName);
|
||||
LCR_mapLoadFromStr(LCR_gameGetNextDataStrChar,name);
|
||||
LCR_game.mapBeaten = LCR_mapIsBeaten(LCR_currentMap.name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -791,8 +790,8 @@ unsigned int LCR_gameLoadReplay(unsigned int replayIndex)
|
|||
{
|
||||
c = LCR_gameGetNextDataFileChar();
|
||||
}
|
||||
while (c && c != LCR_RESOURCE_FILE_SEPARATOR2 &&
|
||||
c != LCR_RESOURCE_FILE_SEPARATOR);
|
||||
while (c && c != LCR_DATA_FILE_SEPARATOR2 &&
|
||||
c != LCR_DATA_FILE_SEPARATOR);
|
||||
|
||||
if (!LCR_replayLoadFromStr(LCR_gameGetNextDataStrChar,
|
||||
&mapHash,&nameHash))
|
||||
|
@ -835,7 +834,7 @@ unsigned int LCR_gameLoadReplay(unsigned int replayIndex)
|
|||
mapIndex++;
|
||||
}
|
||||
|
||||
while (c != LCR_RESOURCE_FILE_SEPARATOR)
|
||||
while (c != LCR_DATA_FILE_SEPARATOR)
|
||||
{
|
||||
if (c == 0)
|
||||
return -1;
|
||||
|
@ -869,6 +868,42 @@ void LCR_gameLoadMainMenuItems(void)
|
|||
LCR_game.menu.itemCount = 4;
|
||||
}
|
||||
|
||||
#define LCR_GAME_DATA_FILE_BUFFER_SIZE 32
|
||||
|
||||
char _LCR_gameDataFileBuffer[LCR_GAME_DATA_FILE_BUFFER_SIZE];
|
||||
|
||||
/**
|
||||
Appends a single character to the data file WITH buffering, i.e. actual write
|
||||
is only performed when the buffer is filled OR when the character is a
|
||||
newline (which can be used to force a flush).
|
||||
*/
|
||||
void _LCR_gameDataCharWrite(char c)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
|
||||
while (_LCR_gameDataFileBuffer[i])
|
||||
{
|
||||
if (i >= LCR_GAME_DATA_FILE_BUFFER_SIZE - 2)
|
||||
{
|
||||
_LCR_gameDataFileBuffer[i] = c;
|
||||
LCR_appendDataStr(_LCR_gameDataFileBuffer);
|
||||
_LCR_gameDataFileBuffer[0] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
_LCR_gameDataFileBuffer[i] = c;
|
||||
_LCR_gameDataFileBuffer[i + 1] = 0;
|
||||
|
||||
if (c == 0 || c == '\n')
|
||||
{
|
||||
LCR_appendDataStr(_LCR_gameDataFileBuffer);
|
||||
_LCR_gameDataFileBuffer[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Loads up to LCR_RESOURCE_ITEM_CHUNK items of given type, starting at given
|
||||
index (among items of the same type). This will also load the menu item names.
|
||||
|
@ -916,8 +951,8 @@ void LCR_gameLoadDataFileChunk(unsigned int startIndex, char magicNumber)
|
|||
}
|
||||
else if (i == 1 && state != 255)
|
||||
{
|
||||
if (c == LCR_RESOURCE_FILE_SEPARATOR ||
|
||||
c == LCR_RESOURCE_FILE_SEPARATOR2 ||
|
||||
if (c == LCR_DATA_FILE_SEPARATOR ||
|
||||
c == LCR_DATA_FILE_SEPARATOR2 ||
|
||||
state >= 1 + LCR_MENU_STRING_SIZE - 1)
|
||||
{
|
||||
state = 255;
|
||||
|
@ -932,7 +967,7 @@ void LCR_gameLoadDataFileChunk(unsigned int startIndex, char magicNumber)
|
|||
state++;
|
||||
}
|
||||
|
||||
if (c == LCR_RESOURCE_FILE_SEPARATOR)
|
||||
if (c == LCR_DATA_FILE_SEPARATOR)
|
||||
state = 0;
|
||||
}
|
||||
}
|
||||
|
@ -1037,8 +1072,8 @@ void LCR_checkBeatenMaps(void)
|
|||
{
|
||||
c = LCR_gameGetNextDataFileChar();
|
||||
|
||||
if (c < ' ' || c == LCR_RESOURCE_FILE_SEPARATOR ||
|
||||
c == LCR_RESOURCE_FILE_SEPARATOR2)
|
||||
if (c < ' ' || c == LCR_DATA_FILE_SEPARATOR ||
|
||||
c == LCR_DATA_FILE_SEPARATOR2)
|
||||
break;
|
||||
|
||||
name[i] = c;
|
||||
|
@ -1063,7 +1098,7 @@ void LCR_checkBeatenMaps(void)
|
|||
}
|
||||
}
|
||||
else
|
||||
while (c != 0 && c != LCR_RESOURCE_FILE_SEPARATOR)
|
||||
while (c != 0 && c != LCR_DATA_FILE_SEPARATOR)
|
||||
c = LCR_gameGetNextDataFileChar();
|
||||
|
||||
if (c == 0)
|
||||
|
@ -1202,14 +1237,20 @@ void LCR_gameDraw3DView(void)
|
|||
}
|
||||
}
|
||||
|
||||
void _LCR_gameDataCharWrite(char c)
|
||||
{ // TODO
|
||||
printf("%c",c);
|
||||
}
|
||||
|
||||
void LCR_gameSaveReplay(void)
|
||||
{
|
||||
LCR_LOG0("saving replay");
|
||||
_LCR_gameDataCharWrite(LCR_DATA_FILE_SEPARATOR);
|
||||
_LCR_gameDataCharWrite('R');
|
||||
|
||||
for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i)
|
||||
if (LCR_currentMap.name[i])
|
||||
_LCR_gameDataCharWrite(LCR_currentMap.name[i]);
|
||||
else
|
||||
break;
|
||||
|
||||
_LCR_gameDataCharWrite(';');
|
||||
|
||||
LCR_replayOutputStr(_LCR_gameDataCharWrite);
|
||||
LCR_gamePopupMessage(LCR_texts[LCR_TEXTS_SAVED]);
|
||||
}
|
||||
|
@ -1525,7 +1566,20 @@ uint8_t LCR_gameStep(uint32_t time)
|
|||
!LCR_racing.playingReplay)
|
||||
{
|
||||
LCR_LOG1("map beaten");
|
||||
// TODO
|
||||
LCR_game.mapBeaten = 1;
|
||||
|
||||
// record that the map is beaten in the data file:
|
||||
_LCR_gameDataCharWrite(LCR_DATA_FILE_SEPARATOR);
|
||||
_LCR_gameDataCharWrite('B');
|
||||
|
||||
for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i)
|
||||
if (LCR_currentMap.name[i])
|
||||
_LCR_gameDataCharWrite(LCR_currentMap.name[i]);
|
||||
else
|
||||
break;
|
||||
|
||||
_LCR_gameDataCharWrite(';');
|
||||
_LCR_gameDataCharWrite('\n');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue