Write to data file

This commit is contained in:
Miloslav Ciz 2025-03-10 15:49:29 +01:00
parent 9fb117901d
commit 34704a934a
3 changed files with 91 additions and 35 deletions

View file

@ -104,8 +104,6 @@ static const char *LCR_internalDataFile =
":-k5k1 :f5120" ":-k5k1 :f5120"
" map end " " map end "
"#Rrep1;testmap;482f70f9 00000188:00c1:0089:0111:00b9:0091:0109:0028:0050:00c1:0093:0030:00d1:0069:0041:0020:0071:0013:0012:0023:0022:0050:0032:0020:0022:0060:0024:00bc:0044"
// MAP 1: // MAP 1:
"#MLC1;4321 0 " "#MLC1;4321 0 "
":*B2mL:!x6G:+L2H:+D38" // start, finish, CPs ":*B2mL:!x6G:+L2H:+D38" // start, finish, CPs
@ -359,6 +357,8 @@ static const char *LCR_internalDataFile =
// fans: // fans:
":Vm1xI:Vm1z" ":Vm1xI:Vm1z"
":-w0q1:-y0q1" ":-w0q1:-y0q1"
"#" // for user data file
; ;
#define LCR_IMAGE_SIZE 64 ///< one-dimension resolution of bitmap image #define LCR_IMAGE_SIZE 64 ///< one-dimension resolution of bitmap image

4
data
View file

@ -1,4 +1,4 @@
#Mmap2;4321 1 :=s0s0 :-u0s0 :;w0s0 :,y0s0 :.s0u0 :Is0w0 :'s1y0 Mmap2;4321 1 :=s0s0 :-u0s0 :;w0s0 :,y0s0 :.s0u0 :Is0w0 :'s1y0
#Bnomap #Bnomap
#Rrep2;testmap;482f70f9 00000843:0173:0081:0029:0111:0039:0071:00a3:0061:0169:0051:00b3:0041:0073:0081:0033:0041:0033:0231:0030:0098:0029:0011:0163:00f1:0053:0081:0033:0051:0023:0031:00f0:0032:0023:0131:00b9:0081:0023:00a1:0119:00e1:00c9:0071:0039:00a1:00d3:0021:01f9:0091:0079:0091:0039:0051:0049:0021:0083:0031:0083:0031:0083:0061:0089:0121:00a0:0058:002c:0048:0061:0013:0150:0052:00c0:00a1:0053:0041:0043:0031:0020:0092:0063:0181:0010:00a2:0013:0071:00e0:0028:00e9:0078:00a9:0043:0032:0123:0042:0080:0038:004c:01a8:0050:0032:0033:0101 #Rrep2;testmap;482f70f9 00000843:0173:0081:0029:0111:0039:0071:00a3:0061:0169:0051:00b3:0041:0073:0081:0033:0041:0033:0231:0030:0098:0029:0011:0163:00f1:0053:0081:0033:0051:0023:0031:00f0:0032:0023:0131:00b9:0081:0023:00a1:0119:00e1:00c9:0071:0039:00a1:00d3:0021:01f9:0091:0079:0091:0039:0051:0049:0021:0083:0031:0083:0031:0083:0061:0089:0121:00a0:0058:002c:0048:0061:0013:0150:0052:00c0:00a1:0053:0041:0043:0031:0020:0092:0063:0181:0010:00a2:0013:0071:00e0:0028:00e9:0078:00a9:0043:0032:0123:0042:0080:0038:004c:01a8:0050:0032:0033:0101
#Btestmap; #Btestmap;
@ -31,3 +31,5 @@ ramps
finish finish
:!vdt :!vdA :!vdt :!vdA
#RLCtiny1;00LCtiny1;8ecff5c0 000381:00a1:02b9:0041:0265:0061:029:0141:0263:0041:0043:0051:003:0041:0033:0031:0033:0052:00260042:0096:0092:0026:0022:0086:02e:001c:0072:0043:00f1:0033:012:0039:0061:0069:0041:00b5:0021:263:0031:00c9:0011
#BLCtiny1;

118
game.h
View file

@ -243,8 +243,8 @@ static inline void LCR_gameDrawPixelXYSafe(unsigned int x, unsigned int y,
#define LCR_MENU_STRING_SIZE 16 #define LCR_MENU_STRING_SIZE 16
#define LCR_RESOURCE_FILE_SEPARATOR '#' #define LCR_DATA_FILE_SEPARATOR '#'
#define LCR_RESOURCE_FILE_SEPARATOR2 ';' #define LCR_DATA_FILE_SEPARATOR2 ';'
#define LCR_FREE_CAMERA_STEP \ #define LCR_FREE_CAMERA_STEP \
((LCR_SETTING_FREE_CAMERA_SPEED * LCR_GAME_UNIT / 8) \ ((LCR_SETTING_FREE_CAMERA_SPEED * LCR_GAME_UNIT / 8) \
@ -611,7 +611,7 @@ void LCR_gameRewindDataFile(void)
*/ */
char LCR_gameGetNextDataFileChar(void) char LCR_gameGetNextDataFileChar(void)
{ {
#if LCR_SETTING_ENABLE_RESOURCE_FILE #if LCR_SETTING_ENABLE_DATA_FILE
char c; char c;
if (LCR_game.dataFile.state < 0) // external file? if (LCR_game.dataFile.state < 0) // external file?
@ -653,7 +653,7 @@ char LCR_gameGetNextDataFileChar(void)
char LCR_gameGetNextDataStrChar(void) char LCR_gameGetNextDataStrChar(void)
{ {
char c = LCR_gameGetNextDataFileChar(); 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) unsigned int LCR_countData(char magicNumber)
@ -669,7 +669,7 @@ unsigned int LCR_countData(char magicNumber)
if (c == magicNumber) if (c == magicNumber)
result++; result++;
while (c != 0 && c != LCR_RESOURCE_FILE_SEPARATOR) while (c != 0 && c != LCR_DATA_FILE_SEPARATOR)
c = LCR_gameGetNextDataFileChar(); c = LCR_gameGetNextDataFileChar();
if (c == 0) if (c == 0)
@ -703,7 +703,7 @@ void LCR_seekDataByIndex(unsigned int index, char magicNumber)
return; return;
} }
while (c != 0 && c != LCR_RESOURCE_FILE_SEPARATOR) while (c != 0 && c != LCR_DATA_FILE_SEPARATOR)
c = LCR_gameGetNextDataFileChar(); c = LCR_gameGetNextDataFileChar();
} while (c); } while (c);
@ -727,8 +727,8 @@ uint8_t LCR_mapIsBeaten(const char *name)
if (name[i] == 0) if (name[i] == 0)
{ {
if (c < ' ' || c == LCR_RESOURCE_FILE_SEPARATOR || if (c < ' ' || c == LCR_DATA_FILE_SEPARATOR ||
c == LCR_RESOURCE_FILE_SEPARATOR2 || c == 0) c == LCR_DATA_FILE_SEPARATOR2 || c == 0)
return 1; return 1;
else else
break; break;
@ -749,26 +749,25 @@ uint8_t LCR_mapIsBeaten(const char *name)
void LCR_gameLoadMap(unsigned int mapIndex) 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'); LCR_seekDataByIndex(mapIndex,'M');
mapName[0] = 0;
for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i) for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i)
{ {
char c = LCR_gameGetNextDataFileChar(); char c = LCR_gameGetNextDataFileChar();
if (c == LCR_RESOURCE_FILE_SEPARATOR2 || if (c == LCR_DATA_FILE_SEPARATOR2 ||
c == LCR_RESOURCE_FILE_SEPARATOR || c == 0) c == LCR_DATA_FILE_SEPARATOR || c == 0)
break; break;
mapName[i] = c; name[i] = c;
mapName[i + 1] = 0; name[i + 1] = 0;
} }
LCR_mapLoadFromStr(LCR_gameGetNextDataStrChar,mapName); LCR_mapLoadFromStr(LCR_gameGetNextDataStrChar,name);
LCR_game.mapBeaten = LCR_mapIsBeaten(mapName); LCR_game.mapBeaten = LCR_mapIsBeaten(LCR_currentMap.name);
} }
/** /**
@ -791,8 +790,8 @@ unsigned int LCR_gameLoadReplay(unsigned int replayIndex)
{ {
c = LCR_gameGetNextDataFileChar(); c = LCR_gameGetNextDataFileChar();
} }
while (c && c != LCR_RESOURCE_FILE_SEPARATOR2 && while (c && c != LCR_DATA_FILE_SEPARATOR2 &&
c != LCR_RESOURCE_FILE_SEPARATOR); c != LCR_DATA_FILE_SEPARATOR);
if (!LCR_replayLoadFromStr(LCR_gameGetNextDataStrChar, if (!LCR_replayLoadFromStr(LCR_gameGetNextDataStrChar,
&mapHash,&nameHash)) &mapHash,&nameHash))
@ -835,7 +834,7 @@ unsigned int LCR_gameLoadReplay(unsigned int replayIndex)
mapIndex++; mapIndex++;
} }
while (c != LCR_RESOURCE_FILE_SEPARATOR) while (c != LCR_DATA_FILE_SEPARATOR)
{ {
if (c == 0) if (c == 0)
return -1; return -1;
@ -869,6 +868,42 @@ void LCR_gameLoadMainMenuItems(void)
LCR_game.menu.itemCount = 4; 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 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. 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) else if (i == 1 && state != 255)
{ {
if (c == LCR_RESOURCE_FILE_SEPARATOR || if (c == LCR_DATA_FILE_SEPARATOR ||
c == LCR_RESOURCE_FILE_SEPARATOR2 || c == LCR_DATA_FILE_SEPARATOR2 ||
state >= 1 + LCR_MENU_STRING_SIZE - 1) state >= 1 + LCR_MENU_STRING_SIZE - 1)
{ {
state = 255; state = 255;
@ -932,7 +967,7 @@ void LCR_gameLoadDataFileChunk(unsigned int startIndex, char magicNumber)
state++; state++;
} }
if (c == LCR_RESOURCE_FILE_SEPARATOR) if (c == LCR_DATA_FILE_SEPARATOR)
state = 0; state = 0;
} }
} }
@ -1037,8 +1072,8 @@ void LCR_checkBeatenMaps(void)
{ {
c = LCR_gameGetNextDataFileChar(); c = LCR_gameGetNextDataFileChar();
if (c < ' ' || c == LCR_RESOURCE_FILE_SEPARATOR || if (c < ' ' || c == LCR_DATA_FILE_SEPARATOR ||
c == LCR_RESOURCE_FILE_SEPARATOR2) c == LCR_DATA_FILE_SEPARATOR2)
break; break;
name[i] = c; name[i] = c;
@ -1063,7 +1098,7 @@ void LCR_checkBeatenMaps(void)
} }
} }
else else
while (c != 0 && c != LCR_RESOURCE_FILE_SEPARATOR) while (c != 0 && c != LCR_DATA_FILE_SEPARATOR)
c = LCR_gameGetNextDataFileChar(); c = LCR_gameGetNextDataFileChar();
if (c == 0) if (c == 0)
@ -1202,14 +1237,20 @@ void LCR_gameDraw3DView(void)
} }
} }
void _LCR_gameDataCharWrite(char c)
{ // TODO
printf("%c",c);
}
void LCR_gameSaveReplay(void) void LCR_gameSaveReplay(void)
{ {
LCR_LOG0("saving replay"); 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_replayOutputStr(_LCR_gameDataCharWrite);
LCR_gamePopupMessage(LCR_texts[LCR_TEXTS_SAVED]); LCR_gamePopupMessage(LCR_texts[LCR_TEXTS_SAVED]);
} }
@ -1525,7 +1566,20 @@ uint8_t LCR_gameStep(uint32_t time)
!LCR_racing.playingReplay) !LCR_racing.playingReplay)
{ {
LCR_LOG1("map beaten"); 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');
} }
} }