Add replay loading
This commit is contained in:
parent
9485a7cd95
commit
8040d6755f
3 changed files with 85 additions and 13 deletions
73
racing.h
73
racing.h
|
@ -16,11 +16,12 @@
|
|||
- Replay text format: first there is the name of the map terminated by ';',
|
||||
then hexadecimal hash of the map follows (exactly 8 characters), then
|
||||
blank character follows, then achieved time as a series of decimal digits
|
||||
expressing the number of milliseconds, then a non-decimal character follows,
|
||||
then the replay data, i.e. the series of 16 bit words in hexadecimal. The
|
||||
blocks (but nothing else) may be preceeded or followed by blank characters.
|
||||
All hexadecimal letters must be lowercase. The word 00000000 may optinally
|
||||
be used to terminate the replay, the rest of the string will be ignored.
|
||||
expressing the number of milliseconds, then the replay data, i.e. the series
|
||||
of 16 bit words in hexadecimal, each preceded by ':'. The events (but
|
||||
nothing else) may otherwise be preceeded or followed by other characters
|
||||
(possible comments). All hexadecimal letters must be lowercase. The word
|
||||
00000000 may optinally be used to terminate the replay, the rest of the
|
||||
string will be ignored.
|
||||
*/
|
||||
|
||||
typedef int32_t LCR_GameUnit; ///< abstract game unit
|
||||
|
@ -189,7 +190,7 @@ void LCR_replayOutputStr(void (*printChar)(char))
|
|||
for (int i = 0; i < LCR_replay.eventCount; ++i)
|
||||
{
|
||||
uint16_t e = LCR_replay.events[i];
|
||||
printChar(' ');
|
||||
printChar(':');
|
||||
|
||||
for (int j = 0; j < 4; ++j)
|
||||
{
|
||||
|
@ -201,6 +202,66 @@ void LCR_replayOutputStr(void (*printChar)(char))
|
|||
printChar('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
Reads replay from string using provided function that returns next character
|
||||
in the string. Returns 1 on success, else 0.
|
||||
*/
|
||||
int LCR_replayLoadFromStr(char (*nextChar)(void))
|
||||
{
|
||||
char c = ' ';
|
||||
|
||||
LCR_replay.eventCount = 0;
|
||||
LCR_replay.achievedTime = 0;
|
||||
|
||||
do // map name
|
||||
{
|
||||
c = nextChar();
|
||||
|
||||
if (c == 0)
|
||||
return 0;
|
||||
} while (c != ';');
|
||||
|
||||
for (int i = 0; i < 8; ++i) // hash
|
||||
if (_LCR_hexDigitVal(nextChar()) < 0)
|
||||
return 0;
|
||||
|
||||
nextChar();
|
||||
|
||||
while (1) // time
|
||||
{
|
||||
c = nextChar();
|
||||
|
||||
if (c < '0' || c > '9')
|
||||
break;
|
||||
|
||||
LCR_replay.achievedTime = LCR_replay.achievedTime * 10 + c - '0';
|
||||
}
|
||||
|
||||
while (c != 0) // events
|
||||
{
|
||||
if (c == ':')
|
||||
{
|
||||
uint16_t e = 0;
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
e = (e << 4) | _LCR_hexDigitVal(nextChar());
|
||||
|
||||
if (e == 0)
|
||||
break;
|
||||
|
||||
if (LCR_replay.eventCount >= LCR_SETTING_REPLAY_MAX_SIZE)
|
||||
return 0;
|
||||
|
||||
LCR_replay.events[LCR_replay.eventCount] = e;
|
||||
LCR_replay.eventCount++;
|
||||
}
|
||||
|
||||
c = nextChar();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
During playing of a replay returns the next input and shifts to next frame.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue