Macros | Functions | Variables
savefile.h File Reference

Savefile loading and saving main routines. More...

Go to the source code of this file.


#define FINISHED_CODE   255
#define ITEM_VERSION   5
#define EGO_ART_KNOWN   0xffffffff


bool savefile_save (const char *path)
 Save to the given location. More...
bool savefile_load (const char *path, bool cheat_death)
 Load the savefile given. More...
const char * savefile_get_description (const char *path)
 Try to get a description for this savefile. More...
void note (const char *msg)

Detailed saving and loading functions

void wr_byte (byte v)

Accessor functions

void wr_u16b (u16b v)
void wr_s16b (s16b v)
void wr_u32b (u32b v)
void wr_s32b (s32b v)
void wr_string (const char *str)
void pad_bytes (int n)
void rd_byte (byte *ip)
void rd_u16b (u16b *ip)
void rd_s16b (s16b *ip)
void rd_u32b (u32b *ip)
void rd_s32b (s32b *ip)
void rd_string (char *str, int max)
void strip_bytes (int n)
int rd_randomizer (void)
 Read RNG state. More...
int rd_options (void)
 Read options. More...
int rd_messages (void)
 Read the saved messages. More...
int rd_monster_memory (void)
 Read monster memory. More...
int rd_object_memory (void)
int rd_quests (void)
int rd_artifacts (void)
int rd_player (void)
 Read the player information. More...
int rd_ignore (void)
 Read ignore and autoinscription submenu for all known objects. More...
int rd_misc (void)
int rd_player_hp (void)
int rd_player_spells (void)
 Read the player spells. More...
int rd_gear (void)
 Read the player gear - wrapper functions. More...
int rd_stores (void)
 Read the stores - wrapper functions. More...
int rd_dungeon (void)
int rd_chunks (void)
 Read the chunk list. More...
int rd_objects (void)
 Read the objects - wrapper functions. More...
int rd_monsters (void)
 Read the monster list - wrapper functions. More...
int rd_monster_groups (void)
int rd_history (void)
int rd_traps (void)
 Read the traps - wrapper functions. More...
int rd_null (void)
 For blocks that don't need loading anymore. More...
void wr_description (void)
 Write a description of the character. More...
void wr_randomizer (void)
 Write RNG state. More...
void wr_options (void)
 Write the "options". More...
void wr_messages (void)
void wr_monster_memory (void)
void wr_object_memory (void)
void wr_quests (void)
void wr_artifacts (void)
void wr_player (void)
void wr_ignore (void)
void wr_misc (void)
void wr_player_hp (void)
void wr_player_spells (void)
void wr_randarts (void)
void wr_gear (void)
void wr_stores (void)
void wr_dungeon (void)
void wr_chunks (void)
void wr_objects (void)
void wr_monsters (void)
void wr_monster_groups (void)
void wr_ghost (void)
void wr_history (void)
void wr_traps (void)


bool character_saved

Savefile API


Detailed Description

Savefile loading and saving main routines.

Copyright (c) 2009 Andi Sidwell

This work is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation, version 2, or

b) the "Angband licence": This software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies. Other copyrights may also apply.

Macro Definition Documentation


#define EGO_ART_KNOWN   0xffffffff


#define FINISHED_CODE   255

Referenced by rd_gear_aux(), and wr_gear_aux().


#define ITEM_VERSION   5

Referenced by wr_item().

Function Documentation

◆ note()

void note ( const char *  message)

◆ pad_bytes()

void pad_bytes ( int  n)

References wr_byte().

◆ rd_artifacts()

int rd_artifacts ( void  )

◆ rd_byte()

void rd_byte ( byte ip)

◆ rd_chunks()

int rd_chunks ( void  )

◆ rd_dungeon()

int rd_dungeon ( void  )

◆ rd_gear()

int rd_gear ( void  )

◆ rd_history()

int rd_history ( void  )

◆ rd_ignore()

int rd_ignore ( void  )

◆ rd_messages()

int rd_messages ( void  )

Read the saved messages.

References buf, i, message_add(), num, rd_s16b(), rd_string(), and rd_u16b().

◆ rd_misc()

int rd_misc ( void  )

◆ rd_monster_groups()

int rd_monster_groups ( void  )

◆ rd_monster_memory()

int rd_monster_memory ( void  )

◆ rd_monsters()

int rd_monsters ( void  )

◆ rd_null()

int rd_null ( void  )

For blocks that don't need loading anymore.

◆ rd_object_memory()

int rd_object_memory ( void  )

◆ rd_objects()

int rd_objects ( void  )

Read the objects - wrapper functions.

References cave, player::cave, rd_item(), and rd_objects_aux().

◆ rd_options()

int rd_options ( void  )

◆ rd_player()

int rd_player ( void  )

◆ rd_player_hp()

int rd_player_hp ( void  )

◆ rd_player_spells()

int rd_player_spells ( void  )

◆ rd_quests()

int rd_quests ( void  )

◆ rd_randomizer()

int rd_randomizer ( void  )

Read RNG state.

There were originally 64 bytes of randomizer saved. Now we only need 32 + 5 bytes saved, so we'll read an extra 27 bytes at the end which won't be used.

References i, RAND_DEG, Rand_quick, Rand_value, rd_u32b(), STATE, state_i, z0, z1, and z2.

◆ rd_s16b()

void rd_s16b ( s16b ip)

◆ rd_s32b()

void rd_s32b ( s32b ip)

◆ rd_stores()

int rd_stores ( void  )

Read the stores - wrapper functions.

References rd_item(), and rd_stores_aux().

◆ rd_string()

void rd_string ( char *  str,
int  max 

◆ rd_traps()

int rd_traps ( void  )

Read the traps - wrapper functions.

References cave, player::cave, and rd_traps_aux().

◆ rd_u16b()

void rd_u16b ( u16b ip)

◆ rd_u32b()

void rd_u32b ( u32b ip)

◆ savefile_get_description()

const char* savefile_get_description ( const char *  path)

Try to get a description for this savefile.

Try to get a description for this savefile.

Fail gracefully.

References check_header(), file_close(), file_open(), FTYPE_TEXT, get_desc(), load_block(), MODE_READ, my_strcpy(), blockheader::name, next_blockheader(), savefile_desc, skip_block(), and streq.

Referenced by list_saves().

◆ savefile_load()

bool savefile_load ( const char *  path,
bool  cheat_death 

◆ savefile_save()

bool savefile_save ( const char *  path)

◆ strip_bytes()

void strip_bytes ( int  n)

References rd_byte().

Referenced by rd_ignore(), and rd_player().

◆ wr_artifacts()

void wr_artifacts ( void  )

◆ wr_byte()

void wr_byte ( byte  v)

◆ wr_chunks()

void wr_chunks ( void  )

◆ wr_description()

void wr_description ( void  )

◆ wr_dungeon()

void wr_dungeon ( void  )

◆ wr_gear()

void wr_gear ( void  )

◆ wr_ghost()

void wr_ghost ( void  )

◆ wr_history()

void wr_history ( void  )

◆ wr_ignore()

void wr_ignore ( void  )

◆ wr_messages()

void wr_messages ( void  )

◆ wr_misc()

void wr_misc ( void  )

◆ wr_monster_groups()

void wr_monster_groups ( void  )

◆ wr_monster_memory()

void wr_monster_memory ( void  )

◆ wr_monsters()

void wr_monsters ( void  )

References cave, player::cave, and wr_monsters_aux().

◆ wr_object_memory()

void wr_object_memory ( void  )

◆ wr_objects()

void wr_objects ( void  )

References cave, player::cave, and wr_objects_aux().

◆ wr_options()

void wr_options ( void  )

◆ wr_player()

void wr_player ( void  )

◆ wr_player_hp()

void wr_player_hp ( void  )

◆ wr_player_spells()

void wr_player_spells ( void  )

◆ wr_quests()

void wr_quests ( void  )

◆ wr_randarts()

void wr_randarts ( void  )

◆ wr_randomizer()

void wr_randomizer ( void  )

Write RNG state.

There were originally 64 bytes of randomizer saved. Now we only need 32 + 5 bytes saved, so we'll write an extra 27 bytes at the end which won't be used.

References i, RAND_DEG, Rand_value, STATE, state_i, wr_u32b(), z0, z1, and z2.

◆ wr_s16b()

void wr_s16b ( s16b  v)

◆ wr_s32b()

void wr_s32b ( s32b  v)

References wr_u32b().

Referenced by wr_chunks(), wr_dungeon_aux(), wr_history(), and wr_misc().

◆ wr_stores()

void wr_stores ( void  )

◆ wr_string()

void wr_string ( const char *  str)

◆ wr_traps()

void wr_traps ( void  )

References cave, player::cave, and wr_traps_aux().

◆ wr_u16b()

void wr_u16b ( u16b  v)

◆ wr_u32b()

void wr_u32b ( u32b  v)

Variable Documentation

◆ character_saved

bool character_saved

Savefile API

Global "we've just saved" variable

Savefile API

Savefiles since ~3.1 have used a block-based system. Each savefile consists of an 8-byte header, the first four bytes of which mark this as a savefile, the second four bytes provide a variant ID.

After that, each block has the format:

  • 16-byte string giving the type of block
  • 4-byte block version
  • 4-byte block size
  • 4-byte block checksum ... data ... padding so that block is a multiple of 4 bytes

The savefile deosn't contain the version number of that game that saved it; versioning is left at the individual block level. The current code keeps a list of savefile blocks to save in savers[] below, along with their current versions.

For each block type and version, there is a loading function to load that type/version combination. For example, there may be a loader for v1 and v2 of the RNG block; these must be different functions. It has been done this way since it allows easier maintenance; after each release, you need simply remove old loaders and you will not have to disentangle lots of code with "if (version > 3)" and its like everywhere.

Savefile loading and saving is done by keeping the current block in memory, which is accessed using the wr_* and rd_* functions. This is then written out, whole, to disk, with the appropriate header.

So, if you want to make a savefile compat-breaking change, then there are a few things you should do:

  • increment the version in 'savers' below
  • add a loading function that accepts the new version (in addition to the previous loading function) to 'loaders'
  • and watch the magic happen.


  • wr_ and rd_ should be passed a buffer to work with, rather than using the rd_ and wr_ functions with a universal buffer
  • Global "we've just saved" variable

Referenced by handle_signal_abort(), handle_signal_simple(), inkey_ex(), and savefile_save().