+ break;
+ }
+ }
+
+ checked_free(buffer);
+
+ micro_chunk_size += 2 + num_bytes;
+ }
+ else // constant size configuration data (1, 2 or 4 bytes)
+ {
+ int value = (byte_mask == CONF_MASK_1_BYTE ? getFile8Bit (file) :
+ byte_mask == CONF_MASK_2_BYTE ? getFile16BitBE(file) :
+ byte_mask == CONF_MASK_4_BYTE ? getFile32BitBE(file) : 0);
+
+ for (i = 0; conf[i].data_type != -1; i++)
+ {
+ if (conf[i].element == element &&
+ conf[i].conf_type == conf_type)
+ {
+ int data_type = conf[i].data_type;
+
+ if (data_type == TYPE_ELEMENT)
+ value = getMappedElement(value);
+
+ if (data_type == TYPE_BOOLEAN)
+ *(boolean *)(conf[i].value) = (value ? TRUE : FALSE);
+ else
+ *(int *) (conf[i].value) = value;
+
+ element_found = TRUE;
+
+ break;
+ }
+ }
+
+ micro_chunk_size += CONF_VALUE_NUM_BYTES(byte_mask);
+ }
+
+ if (!element_found)
+ {
+ char *error_conf_chunk_bytes =
+ (byte_mask == CONF_MASK_1_BYTE ? "CONF_VALUE_8_BIT" :
+ byte_mask == CONF_MASK_2_BYTE ? "CONF_VALUE_16_BIT" :
+ byte_mask == CONF_MASK_4_BYTE ? "CONF_VALUE_32_BIT" :"CONF_VALUE_BYTES");
+ int error_conf_chunk_token = conf_type & CONF_MASK_TOKEN;
+ int error_element = real_element;
+
+ Warn("cannot load micro chunk '%s(%d)' value for element %d ['%s']",
+ error_conf_chunk_bytes, error_conf_chunk_token,
+ error_element, EL_NAME(error_element));
+ }
+
+ return micro_chunk_size;
+}
+
+static int LoadLevel_INFO(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int real_chunk_size = 0;
+
+ li = *level; // copy level data into temporary buffer
+
+ while (!checkEndOfFile(file))
+ {
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1);
+
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ *level = li; // copy temporary buffer back to level data
+
+ return real_chunk_size;
+}
+
+static int LoadLevel_CONF(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int real_chunk_size = 0;
+
+ li = *level; // copy level data into temporary buffer
+
+ while (!checkEndOfFile(file))
+ {
+ int element = getMappedElement(getFile16BitBE(file));
+
+ real_chunk_size += 2;
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CONF,
+ element, element);
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ *level = li; // copy temporary buffer back to level data
+
+ return real_chunk_size;
+}
+
+static int LoadLevel_ELEM(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int real_chunk_size = 0;
+
+ li = *level; // copy level data into temporary buffer
+
+ while (!checkEndOfFile(file))
+ {
+ int element = getMappedElement(getFile16BitBE(file));
+
+ real_chunk_size += 2;
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_ELEM,
+ element, element);
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ *level = li; // copy temporary buffer back to level data
+
+ return real_chunk_size;
+}
+
+static int LoadLevel_NOTE(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int element = getMappedElement(getFile16BitBE(file));
+ int envelope_nr = element - EL_ENVELOPE_1;
+ int real_chunk_size = 2;
+
+ xx_envelope = level->envelope[envelope_nr]; // copy into temporary buffer
+
+ while (!checkEndOfFile(file))
+ {
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE,
+ -1, element);
+
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ level->envelope[envelope_nr] = xx_envelope; // copy from temporary buffer
+
+ return real_chunk_size;
+}
+
+static int LoadLevel_CUSX(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int element = getMappedElement(getFile16BitBE(file));
+ int real_chunk_size = 2;
+ struct ElementInfo *ei = &element_info[element];
+ int i;
+
+ xx_ei = *ei; // copy element data into temporary buffer
+
+ xx_ei.num_change_pages = -1;
+
+ while (!checkEndOfFile(file))
+ {
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base,
+ -1, element);
+ if (xx_ei.num_change_pages != -1)
+ break;
+
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ *ei = xx_ei;
+
+ if (ei->num_change_pages == -1)
+ {
+ Warn("LoadLevel_CUSX(): missing 'num_change_pages' for '%s'",
+ EL_NAME(element));
+
+ ei->num_change_pages = 1;
+
+ setElementChangePages(ei, 1);
+ setElementChangeInfoToDefaults(ei->change);
+
+ return real_chunk_size;
+ }
+
+ // initialize number of change pages stored for this custom element
+ setElementChangePages(ei, ei->num_change_pages);
+ for (i = 0; i < ei->num_change_pages; i++)
+ setElementChangeInfoToDefaults(&ei->change_page[i]);
+
+ // start with reading properties for the first change page
+ xx_current_change_page = 0;
+
+ while (!checkEndOfFile(file))
+ {
+ // level file might contain invalid change page number
+ if (xx_current_change_page >= ei->num_change_pages)
+ break;
+
+ struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page];
+
+ xx_change = *change; // copy change data into temporary buffer
+
+ resetEventBits(); // reset bits; change page might have changed
+
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_change,
+ -1, element);
+
+ *change = xx_change;
+
+ setEventFlagsFromEventBits(change);
+
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ level->file_has_custom_elements = TRUE;
+
+ return real_chunk_size;
+}
+
+static int LoadLevel_GRPX(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int element = getMappedElement(getFile16BitBE(file));
+ int real_chunk_size = 2;
+ struct ElementInfo *ei = &element_info[element];
+ struct ElementGroupInfo *group = ei->group;
+
+ if (group == NULL)
+ return -1;
+
+ xx_ei = *ei; // copy element data into temporary buffer
+ xx_group = *group; // copy group data into temporary buffer
+
+ while (!checkEndOfFile(file))
+ {
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX,
+ -1, element);
+
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ *ei = xx_ei;
+ *group = xx_group;
+
+ level->file_has_custom_elements = TRUE;
+
+ return real_chunk_size;
+}
+
+static int LoadLevel_EMPX(File *file, int chunk_size, struct LevelInfo *level)
+{
+ int element = getMappedElement(getFile16BitBE(file));
+ int real_chunk_size = 2;
+ struct ElementInfo *ei = &element_info[element];
+
+ xx_ei = *ei; // copy element data into temporary buffer
+
+ while (!checkEndOfFile(file))
+ {
+ real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_EMPX,
+ -1, element);
+
+ if (real_chunk_size >= chunk_size)
+ break;
+ }
+
+ *ei = xx_ei;
+
+ level->file_has_custom_elements = TRUE;
+
+ return real_chunk_size;
+}
+
+static void LoadLevelFromFileInfo_RND(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ char *filename = level_file_info->filename;
+ char cookie[MAX_LINE_LEN];
+ char chunk_name[CHUNK_ID_LEN + 1];
+ int chunk_size;
+ File *file;
+
+ if (!(file = openFile(filename, MODE_READ)))
+ {
+ level->no_valid_file = TRUE;
+ level->no_level_file = TRUE;
+
+ if (level_info_only)
+ return;
+
+ Warn("cannot read level '%s' -- using empty level", filename);
+
+ if (!setup.editor.use_template_for_new_levels)
+ return;
+
+ // if level file not found, try to initialize level data from template
+ filename = getGlobalLevelTemplateFilename();
+
+ if (!(file = openFile(filename, MODE_READ)))
+ return;
+
+ // default: for empty levels, use level template for custom elements
+ level->use_custom_template = TRUE;
+
+ level->no_valid_file = FALSE;
+ }
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (strEqual(chunk_name, "RND1"))
+ {
+ getFile32BitBE(file); // not used
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (!strEqual(chunk_name, "CAVE"))
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("unknown format of level file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+ }
+ else // check for pre-2.0 file format with cookie string
+ {
+ strcpy(cookie, chunk_name);
+ if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
+ cookie[4] = '\0';
+ if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
+ cookie[strlen(cookie) - 1] = '\0';
+
+ if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("unknown format of level file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+
+ if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1)
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("unsupported version of level file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+
+ // pre-2.0 level files have no game version, so use file version here
+ level->game_version = level->file_version;
+ }
+
+ if (level->file_version < FILE_VERSION_1_2)
+ {
+ // level files from versions before 1.2.0 without chunk structure
+ LoadLevel_HEAD(file, LEVEL_CHUNK_HEAD_SIZE, level);
+ LoadLevel_BODY(file, level->fieldx * level->fieldy, level);
+ }
+ else
+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(File *, int, struct LevelInfo *);
+ }
+ chunk_info[] =
+ {
+ { "VERS", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS },
+ { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE },
+ { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD },
+ { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME },
+ { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH },
+ { "INFO", -1, LoadLevel_INFO },
+ { "BODY", -1, LoadLevel_BODY },
+ { "CONT", -1, LoadLevel_CONT },
+ { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 },
+ { "CNT3", -1, LoadLevel_CNT3 },
+ { "CUS1", -1, LoadLevel_CUS1 },
+ { "CUS2", -1, LoadLevel_CUS2 },
+ { "CUS3", -1, LoadLevel_CUS3 },
+ { "CUS4", -1, LoadLevel_CUS4 },
+ { "GRP1", -1, LoadLevel_GRP1 },
+ { "CONF", -1, LoadLevel_CONF },
+ { "ELEM", -1, LoadLevel_ELEM },
+ { "NOTE", -1, LoadLevel_NOTE },
+ { "CUSX", -1, LoadLevel_CUSX },
+ { "GRPX", -1, LoadLevel_GRPX },
+ { "EMPX", -1, LoadLevel_EMPX },
+
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunkBE(file, chunk_name, &chunk_size))
+ {
+ int i = 0;
+
+ while (chunk_info[i].name != NULL &&
+ !strEqual(chunk_name, chunk_info[i].name))
+ i++;
+
+ if (chunk_info[i].name == NULL)
+ {
+ Warn("unknown chunk '%s' in level file '%s'",
+ chunk_name, filename);
+
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else if (chunk_info[i].size != -1 &&
+ chunk_info[i].size != chunk_size)
+ {
+ Warn("wrong size (%d) of chunk '%s' in level file '%s'",
+ chunk_size, chunk_name, filename);
+
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else
+ {
+ // call function to load this level chunk
+ int chunk_size_expected =
+ (chunk_info[i].loader)(file, chunk_size, level);
+
+ if (chunk_size_expected < 0)
+ {
+ Warn("error reading chunk '%s' in level file '%s'",
+ chunk_name, filename);
+
+ break;
+ }
+
+ // the size of some chunks cannot be checked before reading other
+ // chunks first (like "HEAD" and "BODY") that contain some header
+ // information, so check them here
+ if (chunk_size_expected != chunk_size)
+ {
+ Warn("wrong size (%d) of chunk '%s' in level file '%s'",
+ chunk_size, chunk_name, filename);
+
+ break;
+ }
+ }
+ }
+ }
+
+ closeFile(file);
+}
+
+
+// ----------------------------------------------------------------------------
+// functions for loading EM level
+// ----------------------------------------------------------------------------
+
+static void CopyNativeLevel_RND_to_EM(struct LevelInfo *level)
+{
+ static int ball_xy[8][2] =
+ {
+ { 0, 0 },
+ { 1, 0 },
+ { 2, 0 },
+ { 0, 1 },
+ { 2, 1 },
+ { 0, 2 },
+ { 1, 2 },
+ { 2, 2 },
+ };
+ struct LevelInfo_EM *level_em = level->native_em_level;
+ struct CAVE *cav = level_em->cav;
+ int i, j, x, y;
+
+ cav->width = MIN(level->fieldx, MAX_PLAYFIELD_WIDTH);
+ cav->height = MIN(level->fieldy, MAX_PLAYFIELD_HEIGHT);
+
+ cav->time_seconds = level->time;
+ cav->gems_needed = level->gems_needed;
+
+ cav->emerald_score = level->score[SC_EMERALD];
+ cav->diamond_score = level->score[SC_DIAMOND];
+ cav->alien_score = level->score[SC_ROBOT];
+ cav->tank_score = level->score[SC_SPACESHIP];
+ cav->bug_score = level->score[SC_BUG];
+ cav->eater_score = level->score[SC_YAMYAM];
+ cav->nut_score = level->score[SC_NUT];
+ cav->dynamite_score = level->score[SC_DYNAMITE];
+ cav->key_score = level->score[SC_KEY];
+ cav->exit_score = level->score[SC_TIME_BONUS];
+
+ cav->num_eater_arrays = level->num_yamyam_contents;
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ cav->eater_array[i][y * 3 + x] =
+ map_element_RND_to_EM_cave(level->yamyam_content[i].e[x][y]);
+
+ cav->amoeba_time = level->amoeba_speed;
+ cav->wonderwall_time = level->time_magic_wall;
+ cav->wheel_time = level->time_wheel;
+
+ cav->android_move_time = level->android_move_time;
+ cav->android_clone_time = level->android_clone_time;
+ cav->ball_random = level->ball_random;
+ cav->ball_active = level->ball_active_initial;
+ cav->ball_time = level->ball_time;
+ cav->num_ball_arrays = level->num_ball_contents;
+
+ cav->lenses_score = level->lenses_score;
+ cav->magnify_score = level->magnify_score;
+ cav->slurp_score = level->slurp_score;
+
+ cav->lenses_time = level->lenses_time;
+ cav->magnify_time = level->magnify_time;
+
+ cav->wind_time = 9999;
+ cav->wind_direction =
+ map_direction_RND_to_EM(level->wind_direction_initial);
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (j = 0; j < 8; j++)
+ cav->ball_array[i][j] =
+ map_element_RND_to_EM_cave(level->ball_content[i].
+ e[ball_xy[j][0]][ball_xy[j][1]]);
+
+ map_android_clone_elements_RND_to_EM(level);
+
+ // first fill the complete playfield with the empty space element
+ for (y = 0; y < EM_MAX_CAVE_HEIGHT; y++)
+ for (x = 0; x < EM_MAX_CAVE_WIDTH; x++)
+ cav->cave[x][y] = Cblank;
+
+ // then copy the real level contents from level file into the playfield
+ for (y = 0; y < cav->height; y++) for (x = 0; x < cav->width; x++)
+ {
+ int new_element = map_element_RND_to_EM_cave(level->field[x][y]);
+
+ if (level->field[x][y] == EL_AMOEBA_DEAD)
+ new_element = map_element_RND_to_EM_cave(EL_AMOEBA_WET);
+
+ cav->cave[x][y] = new_element;
+ }
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+ cav->player_x[i] = -1;
+ cav->player_y[i] = -1;
+ }
+
+ // initialize player positions and delete players from the playfield
+ for (y = 0; y < cav->height; y++) for (x = 0; x < cav->width; x++)
+ {
+ if (IS_PLAYER_ELEMENT(level->field[x][y]))
+ {
+ int player_nr = GET_PLAYER_NR(level->field[x][y]);
+
+ cav->player_x[player_nr] = x;
+ cav->player_y[player_nr] = y;
+
+ cav->cave[x][y] = map_element_RND_to_EM_cave(EL_EMPTY);
+ }
+ }
+}
+
+static void CopyNativeLevel_EM_to_RND(struct LevelInfo *level)
+{
+ static int ball_xy[8][2] =
+ {
+ { 0, 0 },
+ { 1, 0 },
+ { 2, 0 },
+ { 0, 1 },
+ { 2, 1 },
+ { 0, 2 },
+ { 1, 2 },
+ { 2, 2 },
+ };
+ struct LevelInfo_EM *level_em = level->native_em_level;
+ struct CAVE *cav = level_em->cav;
+ int i, j, x, y;
+
+ level->fieldx = MIN(cav->width, MAX_LEV_FIELDX);
+ level->fieldy = MIN(cav->height, MAX_LEV_FIELDY);
+
+ level->time = cav->time_seconds;
+ level->gems_needed = cav->gems_needed;
+
+ sprintf(level->name, "Level %d", level->file_info.nr);
+
+ level->score[SC_EMERALD] = cav->emerald_score;
+ level->score[SC_DIAMOND] = cav->diamond_score;
+ level->score[SC_ROBOT] = cav->alien_score;
+ level->score[SC_SPACESHIP] = cav->tank_score;
+ level->score[SC_BUG] = cav->bug_score;
+ level->score[SC_YAMYAM] = cav->eater_score;
+ level->score[SC_NUT] = cav->nut_score;
+ level->score[SC_DYNAMITE] = cav->dynamite_score;
+ level->score[SC_KEY] = cav->key_score;
+ level->score[SC_TIME_BONUS] = cav->exit_score;
+
+ level->num_yamyam_contents = cav->num_eater_arrays;
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ level->yamyam_content[i].e[x][y] =
+ map_element_EM_to_RND_cave(cav->eater_array[i][y * 3 + x]);
+
+ level->amoeba_speed = cav->amoeba_time;
+ level->time_magic_wall = cav->wonderwall_time;
+ level->time_wheel = cav->wheel_time;
+
+ level->android_move_time = cav->android_move_time;
+ level->android_clone_time = cav->android_clone_time;
+ level->ball_random = cav->ball_random;
+ level->ball_active_initial = cav->ball_active;
+ level->ball_time = cav->ball_time;
+ level->num_ball_contents = cav->num_ball_arrays;
+
+ level->lenses_score = cav->lenses_score;
+ level->magnify_score = cav->magnify_score;
+ level->slurp_score = cav->slurp_score;
+
+ level->lenses_time = cav->lenses_time;
+ level->magnify_time = cav->magnify_time;
+
+ level->wind_direction_initial =
+ map_direction_EM_to_RND(cav->wind_direction);
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (j = 0; j < 8; j++)
+ level->ball_content[i].e[ball_xy[j][0]][ball_xy[j][1]] =
+ map_element_EM_to_RND_cave(cav->ball_array[i][j]);
+
+ map_android_clone_elements_EM_to_RND(level);
+
+ // convert the playfield (some elements need special treatment)
+ for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
+ {
+ int new_element = map_element_EM_to_RND_cave(cav->cave[x][y]);
+
+ if (new_element == EL_AMOEBA_WET && level->amoeba_speed == 0)
+ new_element = EL_AMOEBA_DEAD;
+
+ level->field[x][y] = new_element;
+ }
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+ // in case of all players set to the same field, use the first player
+ int nr = MAX_PLAYERS - i - 1;
+ int jx = cav->player_x[nr];
+ int jy = cav->player_y[nr];
+
+ if (jx != -1 && jy != -1)
+ level->field[jx][jy] = EL_PLAYER_1 + nr;
+ }
+
+ // time score is counted for each 10 seconds left in Emerald Mine levels
+ level->time_score_base = 10;
+}
+
+
+// ----------------------------------------------------------------------------
+// functions for loading SP level
+// ----------------------------------------------------------------------------
+
+static void CopyNativeLevel_RND_to_SP(struct LevelInfo *level)
+{
+ struct LevelInfo_SP *level_sp = level->native_sp_level;
+ LevelInfoType *header = &level_sp->header;
+ int i, x, y;
+
+ level_sp->width = level->fieldx;
+ level_sp->height = level->fieldy;
+
+ for (x = 0; x < level->fieldx; x++)
+ for (y = 0; y < level->fieldy; y++)
+ level_sp->playfield[x][y] = map_element_RND_to_SP(level->field[x][y]);
+
+ header->InitialGravity = (level->initial_player_gravity[0] ? 1 : 0);
+
+ for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
+ header->LevelTitle[i] = level->name[i];
+ // !!! NO STRING TERMINATION IN SUPAPLEX VB CODE YET -- FIX THIS !!!
+
+ header->InfotronsNeeded = level->gems_needed;
+
+ header->SpecialPortCount = 0;
+
+ for (x = 0; x < level->fieldx; x++) for (y = 0; y < level->fieldy; y++)
+ {
+ boolean gravity_port_found = FALSE;
+ boolean gravity_port_valid = FALSE;
+ int gravity_port_flag;
+ int gravity_port_base_element;
+ int element = level->field[x][y];
+
+ if (element >= EL_SP_GRAVITY_ON_PORT_RIGHT &&
+ element <= EL_SP_GRAVITY_ON_PORT_UP)
+ {
+ gravity_port_found = TRUE;
+ gravity_port_valid = TRUE;
+ gravity_port_flag = 1;
+ gravity_port_base_element = EL_SP_GRAVITY_ON_PORT_RIGHT;
+ }
+ else if (element >= EL_SP_GRAVITY_OFF_PORT_RIGHT &&
+ element <= EL_SP_GRAVITY_OFF_PORT_UP)
+ {
+ gravity_port_found = TRUE;
+ gravity_port_valid = TRUE;
+ gravity_port_flag = 0;
+ gravity_port_base_element = EL_SP_GRAVITY_OFF_PORT_RIGHT;
+ }
+ else if (element >= EL_SP_GRAVITY_PORT_RIGHT &&
+ element <= EL_SP_GRAVITY_PORT_UP)
+ {
+ // change R'n'D style gravity inverting special port to normal port
+ // (there are no gravity inverting ports in native Supaplex engine)
+
+ gravity_port_found = TRUE;
+ gravity_port_valid = FALSE;
+ gravity_port_base_element = EL_SP_GRAVITY_PORT_RIGHT;
+ }
+
+ if (gravity_port_found)
+ {
+ if (gravity_port_valid &&
+ header->SpecialPortCount < SP_MAX_SPECIAL_PORTS)
+ {
+ SpecialPortType *port = &header->SpecialPort[header->SpecialPortCount];
+
+ port->PortLocation = (y * level->fieldx + x) * 2;
+ port->Gravity = gravity_port_flag;
+
+ element += EL_SP_GRAVITY_PORT_RIGHT - gravity_port_base_element;
+
+ header->SpecialPortCount++;
+ }
+ else
+ {
+ // change special gravity port to normal port
+
+ element += EL_SP_PORT_RIGHT - gravity_port_base_element;
+ }
+
+ level_sp->playfield[x][y] = element - EL_SP_START;
+ }
+ }
+}
+
+static void CopyNativeLevel_SP_to_RND(struct LevelInfo *level)
+{
+ struct LevelInfo_SP *level_sp = level->native_sp_level;
+ LevelInfoType *header = &level_sp->header;
+ boolean num_invalid_elements = 0;
+ int i, j, x, y;
+
+ level->fieldx = level_sp->width;
+ level->fieldy = level_sp->height;
+
+ for (x = 0; x < level->fieldx; x++)
+ {
+ for (y = 0; y < level->fieldy; y++)
+ {
+ int element_old = level_sp->playfield[x][y];
+ int element_new = getMappedElement(map_element_SP_to_RND(element_old));
+
+ if (element_new == EL_UNKNOWN)
+ {
+ num_invalid_elements++;
+
+ Debug("level:native:SP", "invalid element %d at position %d, %d",
+ element_old, x, y);
+ }
+
+ level->field[x][y] = element_new;
+ }
+ }
+
+ if (num_invalid_elements > 0)
+ Warn("found %d invalid elements%s", num_invalid_elements,
+ (!options.debug ? " (use '--debug' for more details)" : ""));
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ level->initial_player_gravity[i] =
+ (header->InitialGravity == 1 ? TRUE : FALSE);
+
+ // skip leading spaces
+ for (i = 0; i < SP_LEVEL_NAME_LEN; i++)
+ if (header->LevelTitle[i] != ' ')
+ break;
+
+ // copy level title
+ for (j = 0; i < SP_LEVEL_NAME_LEN; i++, j++)
+ level->name[j] = header->LevelTitle[i];
+ level->name[j] = '\0';
+
+ // cut trailing spaces
+ for (; j > 0; j--)
+ if (level->name[j - 1] == ' ' && level->name[j] == '\0')
+ level->name[j - 1] = '\0';
+
+ level->gems_needed = header->InfotronsNeeded;
+
+ for (i = 0; i < header->SpecialPortCount; i++)
+ {
+ SpecialPortType *port = &header->SpecialPort[i];
+ int port_location = port->PortLocation;
+ int gravity = port->Gravity;
+ int port_x, port_y, port_element;
+
+ port_x = (port_location / 2) % level->fieldx;
+ port_y = (port_location / 2) / level->fieldx;
+
+ if (port_x < 0 || port_x >= level->fieldx ||
+ port_y < 0 || port_y >= level->fieldy)
+ {
+ Warn("special port position (%d, %d) out of bounds", port_x, port_y);
+
+ continue;
+ }
+
+ port_element = level->field[port_x][port_y];
+
+ if (port_element < EL_SP_GRAVITY_PORT_RIGHT ||
+ port_element > EL_SP_GRAVITY_PORT_UP)
+ {
+ Warn("no special port at position (%d, %d)", port_x, port_y);
+
+ continue;
+ }
+
+ // change previous (wrong) gravity inverting special port to either
+ // gravity enabling special port or gravity disabling special port
+ level->field[port_x][port_y] +=
+ (gravity == 1 ? EL_SP_GRAVITY_ON_PORT_RIGHT :
+ EL_SP_GRAVITY_OFF_PORT_RIGHT) - EL_SP_GRAVITY_PORT_RIGHT;
+ }
+
+ // change special gravity ports without database entries to normal ports
+ for (x = 0; x < level->fieldx; x++)
+ for (y = 0; y < level->fieldy; y++)
+ if (level->field[x][y] >= EL_SP_GRAVITY_PORT_RIGHT &&
+ level->field[x][y] <= EL_SP_GRAVITY_PORT_UP)
+ level->field[x][y] += EL_SP_PORT_RIGHT - EL_SP_GRAVITY_PORT_RIGHT;
+
+ level->time = 0; // no time limit
+ level->amoeba_speed = 0;
+ level->time_magic_wall = 0;
+ level->time_wheel = 0;
+ level->amoeba_content = EL_EMPTY;
+
+ // original Supaplex does not use score values -- rate by playing time
+ for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
+ level->score[i] = 0;
+
+ level->rate_time_over_score = TRUE;
+
+ // there are no yamyams in supaplex levels
+ for (i = 0; i < level->num_yamyam_contents; i++)
+ for (x = 0; x < 3; x++)
+ for (y = 0; y < 3; y++)
+ level->yamyam_content[i].e[x][y] = EL_EMPTY;
+}
+
+static void CopyNativeTape_RND_to_SP(struct LevelInfo *level)
+{
+ struct LevelInfo_SP *level_sp = level->native_sp_level;
+ struct DemoInfo_SP *demo = &level_sp->demo;
+ int i, j;
+
+ // always start with reliable default values
+ demo->is_available = FALSE;
+ demo->length = 0;
+
+ if (TAPE_IS_EMPTY(tape))
+ return;
+
+ demo->level_nr = tape.level_nr; // (currently not used)
+
+ level_sp->header.DemoRandomSeed = tape.random_seed;
+
+ demo->length = 0;
+
+ for (i = 0; i < tape.length; i++)
+ {
+ int demo_action = map_key_RND_to_SP(tape.pos[i].action[0]);
+ int demo_repeat = tape.pos[i].delay;
+ int demo_entries = (demo_repeat + 15) / 16;
+
+ if (demo->length + demo_entries >= SP_MAX_TAPE_LEN)
+ {
+ Warn("tape truncated: size exceeds maximum SP demo size %d",
+ SP_MAX_TAPE_LEN);
+
+ break;
+ }
+
+ for (j = 0; j < demo_repeat / 16; j++)
+ demo->data[demo->length++] = 0xf0 | demo_action;
+
+ if (demo_repeat % 16)
+ demo->data[demo->length++] = ((demo_repeat % 16 - 1) << 4) | demo_action;
+ }
+
+ demo->is_available = TRUE;
+}
+
+static void setTapeInfoToDefaults(void);
+
+static void CopyNativeTape_SP_to_RND(struct LevelInfo *level)
+{
+ struct LevelInfo_SP *level_sp = level->native_sp_level;
+ struct DemoInfo_SP *demo = &level_sp->demo;
+ char *filename = level->file_info.filename;
+ int i;
+
+ // always start with reliable default values
+ setTapeInfoToDefaults();
+
+ if (!demo->is_available)
+ return;
+
+ tape.level_nr = demo->level_nr; // (currently not used)
+ tape.random_seed = level_sp->header.DemoRandomSeed;
+
+ TapeSetDateFromEpochSeconds(getFileTimestampEpochSeconds(filename));
+
+ tape.counter = 0;
+ tape.pos[tape.counter].delay = 0;
+
+ for (i = 0; i < demo->length; i++)
+ {
+ int demo_action = demo->data[i] & 0x0f;
+ int demo_repeat = (demo->data[i] & 0xf0) >> 4;
+ int tape_action = map_key_SP_to_RND(demo_action);
+ int tape_repeat = demo_repeat + 1;
+ byte action[MAX_TAPE_ACTIONS] = { tape_action };
+ boolean success = 0;
+ int j;
+
+ for (j = 0; j < tape_repeat; j++)
+ success = TapeAddAction(action);
+
+ if (!success)
+ {
+ Warn("SP demo truncated: size exceeds maximum tape size %d",
+ MAX_TAPE_LEN);
+
+ break;
+ }
+ }
+
+ TapeHaltRecording();
+}
+
+
+// ----------------------------------------------------------------------------
+// functions for loading MM level
+// ----------------------------------------------------------------------------
+
+static void CopyNativeLevel_RND_to_MM(struct LevelInfo *level)
+{
+ struct LevelInfo_MM *level_mm = level->native_mm_level;
+ int i, x, y;
+
+ level_mm->fieldx = MIN(level->fieldx, MM_MAX_PLAYFIELD_WIDTH);
+ level_mm->fieldy = MIN(level->fieldy, MM_MAX_PLAYFIELD_HEIGHT);
+
+ level_mm->time = level->time;
+ level_mm->kettles_needed = level->gems_needed;
+ level_mm->auto_count_kettles = level->auto_count_gems;
+
+ level_mm->mm_laser_red = level->mm_laser_red;
+ level_mm->mm_laser_green = level->mm_laser_green;
+ level_mm->mm_laser_blue = level->mm_laser_blue;
+
+ level_mm->df_laser_red = level->df_laser_red;
+ level_mm->df_laser_green = level->df_laser_green;
+ level_mm->df_laser_blue = level->df_laser_blue;
+
+ strcpy(level_mm->name, level->name);
+ strcpy(level_mm->author, level->author);
+
+ level_mm->score[SC_EMERALD] = level->score[SC_EMERALD];
+ level_mm->score[SC_PACMAN] = level->score[SC_PACMAN];
+ level_mm->score[SC_KEY] = level->score[SC_KEY];
+ level_mm->score[SC_TIME_BONUS] = level->score[SC_TIME_BONUS];
+ level_mm->score[SC_ELEM_BONUS] = level->score[SC_ELEM_BONUS];
+
+ level_mm->amoeba_speed = level->amoeba_speed;
+ level_mm->time_fuse = level->mm_time_fuse;
+ level_mm->time_bomb = level->mm_time_bomb;
+ level_mm->time_ball = level->mm_time_ball;
+ level_mm->time_block = level->mm_time_block;
+
+ level_mm->num_ball_contents = level->num_mm_ball_contents;
+ level_mm->ball_choice_mode = level->mm_ball_choice_mode;
+ level_mm->rotate_ball_content = level->rotate_mm_ball_content;
+ level_mm->explode_ball = level->explode_mm_ball;
+
+ for (i = 0; i < level->num_mm_ball_contents; i++)
+ level_mm->ball_content[i] =
+ map_element_RND_to_MM(level->mm_ball_content[i]);
+
+ for (x = 0; x < level->fieldx; x++)
+ for (y = 0; y < level->fieldy; y++)
+ Ur[x][y] =
+ level_mm->field[x][y] = map_element_RND_to_MM(level->field[x][y]);
+}
+
+static void CopyNativeLevel_MM_to_RND(struct LevelInfo *level)
+{
+ struct LevelInfo_MM *level_mm = level->native_mm_level;
+ int i, x, y;
+
+ level->fieldx = MIN(level_mm->fieldx, MAX_LEV_FIELDX);
+ level->fieldy = MIN(level_mm->fieldy, MAX_LEV_FIELDY);
+
+ level->time = level_mm->time;
+ level->gems_needed = level_mm->kettles_needed;
+ level->auto_count_gems = level_mm->auto_count_kettles;
+
+ level->mm_laser_red = level_mm->mm_laser_red;
+ level->mm_laser_green = level_mm->mm_laser_green;
+ level->mm_laser_blue = level_mm->mm_laser_blue;
+
+ level->df_laser_red = level_mm->df_laser_red;
+ level->df_laser_green = level_mm->df_laser_green;
+ level->df_laser_blue = level_mm->df_laser_blue;
+
+ strcpy(level->name, level_mm->name);
+
+ // only overwrite author from 'levelinfo.conf' if author defined in level
+ if (!strEqual(level_mm->author, ANONYMOUS_NAME))
+ strcpy(level->author, level_mm->author);
+
+ level->score[SC_EMERALD] = level_mm->score[SC_EMERALD];
+ level->score[SC_PACMAN] = level_mm->score[SC_PACMAN];
+ level->score[SC_KEY] = level_mm->score[SC_KEY];
+ level->score[SC_TIME_BONUS] = level_mm->score[SC_TIME_BONUS];
+ level->score[SC_ELEM_BONUS] = level_mm->score[SC_ELEM_BONUS];
+
+ level->amoeba_speed = level_mm->amoeba_speed;
+ level->mm_time_fuse = level_mm->time_fuse;
+ level->mm_time_bomb = level_mm->time_bomb;
+ level->mm_time_ball = level_mm->time_ball;
+ level->mm_time_block = level_mm->time_block;
+
+ level->num_mm_ball_contents = level_mm->num_ball_contents;
+ level->mm_ball_choice_mode = level_mm->ball_choice_mode;
+ level->rotate_mm_ball_content = level_mm->rotate_ball_content;
+ level->explode_mm_ball = level_mm->explode_ball;
+
+ for (i = 0; i < level->num_mm_ball_contents; i++)
+ level->mm_ball_content[i] =
+ map_element_MM_to_RND(level_mm->ball_content[i]);
+
+ for (x = 0; x < level->fieldx; x++)
+ for (y = 0; y < level->fieldy; y++)
+ level->field[x][y] = map_element_MM_to_RND(level_mm->field[x][y]);
+}
+
+
+// ----------------------------------------------------------------------------
+// functions for loading DC level
+// ----------------------------------------------------------------------------
+
+#define DC_LEVEL_HEADER_SIZE 344
+
+static unsigned short getDecodedWord_DC(unsigned short data_encoded,
+ boolean init)
+{
+ static int last_data_encoded;
+ static int offset1;
+ static int offset2;
+ int diff;
+ int diff_hi, diff_lo;
+ int data_hi, data_lo;
+ unsigned short data_decoded;
+
+ if (init)
+ {
+ last_data_encoded = 0;
+ offset1 = -1;
+ offset2 = 0;
+
+ return 0;
+ }
+
+ diff = data_encoded - last_data_encoded;
+ diff_hi = diff & ~0xff;
+ diff_lo = diff & 0xff;
+
+ offset2 += diff_lo;
+
+ data_hi = diff_hi - (offset1 << 8) + (offset2 & 0xff00);
+ data_lo = (diff_lo + (data_hi >> 16)) & 0x00ff;
+ data_hi = data_hi & 0xff00;
+
+ data_decoded = data_hi | data_lo;
+
+ last_data_encoded = data_encoded;
+
+ offset1 = (offset1 + 1) % 31;
+ offset2 = offset2 & 0xff;
+
+ return data_decoded;
+}
+
+static int getMappedElement_DC(int element)
+{
+ switch (element)
+ {
+ case 0x0000:
+ element = EL_ROCK;
+ break;
+
+ // 0x0117 - 0x036e: (?)
+ // EL_DIAMOND
+
+ // 0x042d - 0x0684: (?)
+ // EL_EMERALD
+
+ case 0x06f1:
+ element = EL_NUT;
+ break;
+
+ case 0x074c:
+ element = EL_BOMB;
+ break;
+
+ case 0x07a4:
+ element = EL_PEARL;
+ break;
+
+ case 0x0823:
+ element = EL_CRYSTAL;
+ break;
+
+ case 0x0e77: // quicksand (boulder)
+ element = EL_QUICKSAND_FAST_FULL;
+ break;
+
+ case 0x0e99: // slow quicksand (boulder)
+ element = EL_QUICKSAND_FULL;
+ break;
+
+ case 0x0ed2:
+ element = EL_EM_EXIT_OPEN;
+ break;
+
+ case 0x0ee3:
+ element = EL_EM_EXIT_CLOSED;
+ break;
+
+ case 0x0eeb:
+ element = EL_EM_STEEL_EXIT_OPEN;
+ break;
+
+ case 0x0efc:
+ element = EL_EM_STEEL_EXIT_CLOSED;
+ break;
+
+ case 0x0f4f: // dynamite (lit 1)
+ element = EL_EM_DYNAMITE_ACTIVE;
+ break;
+
+ case 0x0f57: // dynamite (lit 2)
+ element = EL_EM_DYNAMITE_ACTIVE;
+ break;
+
+ case 0x0f5f: // dynamite (lit 3)
+ element = EL_EM_DYNAMITE_ACTIVE;
+ break;
+
+ case 0x0f67: // dynamite (lit 4)
+ element = EL_EM_DYNAMITE_ACTIVE;
+ break;
+
+ case 0x0f81:
+ case 0x0f82:
+ case 0x0f83:
+ case 0x0f84:
+ element = EL_AMOEBA_WET;
+ break;
+
+ case 0x0f85:
+ element = EL_AMOEBA_DROP;
+ break;
+
+ case 0x0fb9:
+ element = EL_DC_MAGIC_WALL;
+ break;
+
+ case 0x0fd0:
+ element = EL_SPACESHIP_UP;
+ break;
+
+ case 0x0fd9:
+ element = EL_SPACESHIP_DOWN;
+ break;
+
+ case 0x0ff1:
+ element = EL_SPACESHIP_LEFT;
+ break;
+
+ case 0x0ff9:
+ element = EL_SPACESHIP_RIGHT;
+ break;
+
+ case 0x1057:
+ element = EL_BUG_UP;
+ break;
+
+ case 0x1060:
+ element = EL_BUG_DOWN;
+ break;
+
+ case 0x1078:
+ element = EL_BUG_LEFT;
+ break;
+
+ case 0x1080:
+ element = EL_BUG_RIGHT;
+ break;
+
+ case 0x10de:
+ element = EL_MOLE_UP;
+ break;
+
+ case 0x10e7:
+ element = EL_MOLE_DOWN;
+ break;
+
+ case 0x10ff:
+ element = EL_MOLE_LEFT;
+ break;
+
+ case 0x1107:
+ element = EL_MOLE_RIGHT;
+ break;
+
+ case 0x11c0:
+ element = EL_ROBOT;
+ break;
+
+ case 0x13f5:
+ element = EL_YAMYAM_UP;
+ break;
+
+ case 0x1425:
+ element = EL_SWITCHGATE_OPEN;
+ break;
+
+ case 0x1426:
+ element = EL_SWITCHGATE_CLOSED;
+ break;
+
+ case 0x1437:
+ element = EL_DC_SWITCHGATE_SWITCH_UP;
+ break;
+
+ case 0x143a:
+ element = EL_TIMEGATE_CLOSED;
+ break;
+
+ case 0x144c: // conveyor belt switch (green)
+ element = EL_CONVEYOR_BELT_3_SWITCH_MIDDLE;
+ break;
+
+ case 0x144f: // conveyor belt switch (red)
+ element = EL_CONVEYOR_BELT_1_SWITCH_MIDDLE;
+ break;
+
+ case 0x1452: // conveyor belt switch (blue)
+ element = EL_CONVEYOR_BELT_4_SWITCH_MIDDLE;
+ break;
+
+ case 0x145b:
+ element = EL_CONVEYOR_BELT_3_MIDDLE;
+ break;
+
+ case 0x1463:
+ element = EL_CONVEYOR_BELT_3_LEFT;
+ break;
+
+ case 0x146b:
+ element = EL_CONVEYOR_BELT_3_RIGHT;
+ break;
+
+ case 0x1473:
+ element = EL_CONVEYOR_BELT_1_MIDDLE;
+ break;
+
+ case 0x147b:
+ element = EL_CONVEYOR_BELT_1_LEFT;
+ break;
+
+ case 0x1483:
+ element = EL_CONVEYOR_BELT_1_RIGHT;
+ break;
+
+ case 0x148b:
+ element = EL_CONVEYOR_BELT_4_MIDDLE;
+ break;
+
+ case 0x1493:
+ element = EL_CONVEYOR_BELT_4_LEFT;
+ break;
+
+ case 0x149b:
+ element = EL_CONVEYOR_BELT_4_RIGHT;
+ break;
+
+ case 0x14ac:
+ element = EL_EXPANDABLE_WALL_HORIZONTAL;
+ break;
+
+ case 0x14bd:
+ element = EL_EXPANDABLE_WALL_VERTICAL;
+ break;
+
+ case 0x14c6:
+ element = EL_EXPANDABLE_WALL_ANY;
+ break;
+
+ case 0x14ce: // growing steel wall (left/right)
+ element = EL_EXPANDABLE_STEELWALL_HORIZONTAL;
+ break;
+
+ case 0x14df: // growing steel wall (up/down)
+ element = EL_EXPANDABLE_STEELWALL_VERTICAL;
+ break;
+
+ case 0x14e8: // growing steel wall (up/down/left/right)
+ element = EL_EXPANDABLE_STEELWALL_ANY;
+ break;
+
+ case 0x14e9:
+ element = EL_SHIELD_DEADLY;
+ break;
+
+ case 0x1501:
+ element = EL_EXTRA_TIME;
+ break;
+
+ case 0x154f:
+ element = EL_ACID;
+ break;
+
+ case 0x1577:
+ element = EL_EMPTY_SPACE;
+ break;
+
+ case 0x1578: // quicksand (empty)
+ element = EL_QUICKSAND_FAST_EMPTY;
+ break;
+
+ case 0x1579: // slow quicksand (empty)
+ element = EL_QUICKSAND_EMPTY;
+ break;
+
+ // 0x157c - 0x158b:
+ // EL_SAND
+
+ // 0x1590 - 0x159f:
+ // EL_DC_LANDMINE
+
+ case 0x15a0:
+ element = EL_EM_DYNAMITE;
+ break;
+
+ case 0x15a1: // key (red)
+ element = EL_EM_KEY_1;
+ break;
+
+ case 0x15a2: // key (yellow)
+ element = EL_EM_KEY_2;
+ break;
+
+ case 0x15a3: // key (blue)
+ element = EL_EM_KEY_4;
+ break;
+
+ case 0x15a4: // key (green)
+ element = EL_EM_KEY_3;
+ break;
+
+ case 0x15a5: // key (white)
+ element = EL_DC_KEY_WHITE;
+ break;
+
+ case 0x15a6:
+ element = EL_WALL_SLIPPERY;
+ break;
+
+ case 0x15a7:
+ element = EL_WALL;
+ break;
+
+ case 0x15a8: // wall (not round)
+ element = EL_WALL;
+ break;
+
+ case 0x15a9: // (blue)
+ element = EL_CHAR_A;
+ break;
+
+ case 0x15aa: // (blue)
+ element = EL_CHAR_B;
+ break;
+
+ case 0x15ab: // (blue)
+ element = EL_CHAR_C;
+ break;
+
+ case 0x15ac: // (blue)
+ element = EL_CHAR_D;
+ break;
+
+ case 0x15ad: // (blue)
+ element = EL_CHAR_E;
+ break;
+
+ case 0x15ae: // (blue)
+ element = EL_CHAR_F;
+ break;
+
+ case 0x15af: // (blue)
+ element = EL_CHAR_G;
+ break;
+
+ case 0x15b0: // (blue)
+ element = EL_CHAR_H;
+ break;
+
+ case 0x15b1: // (blue)
+ element = EL_CHAR_I;
+ break;
+
+ case 0x15b2: // (blue)
+ element = EL_CHAR_J;
+ break;
+
+ case 0x15b3: // (blue)
+ element = EL_CHAR_K;
+ break;
+
+ case 0x15b4: // (blue)
+ element = EL_CHAR_L;
+ break;
+
+ case 0x15b5: // (blue)
+ element = EL_CHAR_M;
+ break;
+
+ case 0x15b6: // (blue)
+ element = EL_CHAR_N;
+ break;
+
+ case 0x15b7: // (blue)
+ element = EL_CHAR_O;
+ break;
+
+ case 0x15b8: // (blue)
+ element = EL_CHAR_P;
+ break;
+
+ case 0x15b9: // (blue)
+ element = EL_CHAR_Q;
+ break;
+
+ case 0x15ba: // (blue)
+ element = EL_CHAR_R;
+ break;
+
+ case 0x15bb: // (blue)
+ element = EL_CHAR_S;
+ break;
+
+ case 0x15bc: // (blue)
+ element = EL_CHAR_T;
+ break;
+
+ case 0x15bd: // (blue)
+ element = EL_CHAR_U;
+ break;
+
+ case 0x15be: // (blue)
+ element = EL_CHAR_V;
+ break;
+
+ case 0x15bf: // (blue)
+ element = EL_CHAR_W;
+ break;
+
+ case 0x15c0: // (blue)
+ element = EL_CHAR_X;
+ break;
+
+ case 0x15c1: // (blue)
+ element = EL_CHAR_Y;
+ break;
+
+ case 0x15c2: // (blue)
+ element = EL_CHAR_Z;
+ break;
+
+ case 0x15c3: // (blue)
+ element = EL_CHAR_AUMLAUT;
+ break;
+
+ case 0x15c4: // (blue)
+ element = EL_CHAR_OUMLAUT;
+ break;
+
+ case 0x15c5: // (blue)
+ element = EL_CHAR_UUMLAUT;
+ break;
+
+ case 0x15c6: // (blue)
+ element = EL_CHAR_0;
+ break;
+
+ case 0x15c7: // (blue)
+ element = EL_CHAR_1;
+ break;
+
+ case 0x15c8: // (blue)
+ element = EL_CHAR_2;
+ break;
+
+ case 0x15c9: // (blue)
+ element = EL_CHAR_3;
+ break;
+
+ case 0x15ca: // (blue)
+ element = EL_CHAR_4;
+ break;
+
+ case 0x15cb: // (blue)
+ element = EL_CHAR_5;
+ break;
+
+ case 0x15cc: // (blue)
+ element = EL_CHAR_6;
+ break;
+
+ case 0x15cd: // (blue)
+ element = EL_CHAR_7;
+ break;
+
+ case 0x15ce: // (blue)
+ element = EL_CHAR_8;
+ break;
+
+ case 0x15cf: // (blue)
+ element = EL_CHAR_9;
+ break;
+
+ case 0x15d0: // (blue)
+ element = EL_CHAR_PERIOD;
+ break;
+
+ case 0x15d1: // (blue)
+ element = EL_CHAR_EXCLAM;
+ break;
+
+ case 0x15d2: // (blue)
+ element = EL_CHAR_COLON;
+ break;
+
+ case 0x15d3: // (blue)
+ element = EL_CHAR_LESS;
+ break;
+
+ case 0x15d4: // (blue)
+ element = EL_CHAR_GREATER;
+ break;
+
+ case 0x15d5: // (blue)
+ element = EL_CHAR_QUESTION;
+ break;
+
+ case 0x15d6: // (blue)
+ element = EL_CHAR_COPYRIGHT;
+ break;
+
+ case 0x15d7: // (blue)
+ element = EL_CHAR_UP;
+ break;
+
+ case 0x15d8: // (blue)
+ element = EL_CHAR_DOWN;
+ break;
+
+ case 0x15d9: // (blue)
+ element = EL_CHAR_BUTTON;
+ break;
+
+ case 0x15da: // (blue)
+ element = EL_CHAR_PLUS;
+ break;
+
+ case 0x15db: // (blue)
+ element = EL_CHAR_MINUS;
+ break;
+
+ case 0x15dc: // (blue)
+ element = EL_CHAR_APOSTROPHE;
+ break;
+
+ case 0x15dd: // (blue)
+ element = EL_CHAR_PARENLEFT;
+ break;
+
+ case 0x15de: // (blue)
+ element = EL_CHAR_PARENRIGHT;
+ break;
+
+ case 0x15df: // (green)
+ element = EL_CHAR_A;
+ break;
+
+ case 0x15e0: // (green)
+ element = EL_CHAR_B;
+ break;
+
+ case 0x15e1: // (green)
+ element = EL_CHAR_C;
+ break;
+
+ case 0x15e2: // (green)
+ element = EL_CHAR_D;
+ break;
+
+ case 0x15e3: // (green)
+ element = EL_CHAR_E;
+ break;
+
+ case 0x15e4: // (green)
+ element = EL_CHAR_F;
+ break;
+
+ case 0x15e5: // (green)
+ element = EL_CHAR_G;
+ break;
+
+ case 0x15e6: // (green)
+ element = EL_CHAR_H;
+ break;
+
+ case 0x15e7: // (green)
+ element = EL_CHAR_I;
+ break;
+
+ case 0x15e8: // (green)
+ element = EL_CHAR_J;
+ break;
+
+ case 0x15e9: // (green)
+ element = EL_CHAR_K;
+ break;
+
+ case 0x15ea: // (green)
+ element = EL_CHAR_L;
+ break;
+
+ case 0x15eb: // (green)
+ element = EL_CHAR_M;
+ break;
+
+ case 0x15ec: // (green)
+ element = EL_CHAR_N;
+ break;
+
+ case 0x15ed: // (green)
+ element = EL_CHAR_O;
+ break;
+
+ case 0x15ee: // (green)
+ element = EL_CHAR_P;
+ break;
+
+ case 0x15ef: // (green)
+ element = EL_CHAR_Q;
+ break;
+
+ case 0x15f0: // (green)
+ element = EL_CHAR_R;
+ break;
+
+ case 0x15f1: // (green)
+ element = EL_CHAR_S;
+ break;
+
+ case 0x15f2: // (green)
+ element = EL_CHAR_T;
+ break;
+
+ case 0x15f3: // (green)
+ element = EL_CHAR_U;
+ break;
+
+ case 0x15f4: // (green)
+ element = EL_CHAR_V;
+ break;
+
+ case 0x15f5: // (green)
+ element = EL_CHAR_W;
+ break;
+
+ case 0x15f6: // (green)
+ element = EL_CHAR_X;
+ break;
+
+ case 0x15f7: // (green)
+ element = EL_CHAR_Y;
+ break;
+
+ case 0x15f8: // (green)
+ element = EL_CHAR_Z;
+ break;
+
+ case 0x15f9: // (green)
+ element = EL_CHAR_AUMLAUT;
+ break;
+
+ case 0x15fa: // (green)
+ element = EL_CHAR_OUMLAUT;
+ break;
+
+ case 0x15fb: // (green)
+ element = EL_CHAR_UUMLAUT;
+ break;
+
+ case 0x15fc: // (green)
+ element = EL_CHAR_0;
+ break;
+
+ case 0x15fd: // (green)
+ element = EL_CHAR_1;
+ break;
+
+ case 0x15fe: // (green)
+ element = EL_CHAR_2;
+ break;
+
+ case 0x15ff: // (green)
+ element = EL_CHAR_3;
+ break;
+
+ case 0x1600: // (green)
+ element = EL_CHAR_4;
+ break;
+
+ case 0x1601: // (green)
+ element = EL_CHAR_5;
+ break;
+
+ case 0x1602: // (green)
+ element = EL_CHAR_6;
+ break;
+
+ case 0x1603: // (green)
+ element = EL_CHAR_7;
+ break;
+
+ case 0x1604: // (green)
+ element = EL_CHAR_8;
+ break;
+
+ case 0x1605: // (green)
+ element = EL_CHAR_9;
+ break;
+
+ case 0x1606: // (green)
+ element = EL_CHAR_PERIOD;
+ break;
+
+ case 0x1607: // (green)
+ element = EL_CHAR_EXCLAM;
+ break;
+
+ case 0x1608: // (green)
+ element = EL_CHAR_COLON;
+ break;
+
+ case 0x1609: // (green)
+ element = EL_CHAR_LESS;
+ break;
+
+ case 0x160a: // (green)
+ element = EL_CHAR_GREATER;
+ break;
+
+ case 0x160b: // (green)
+ element = EL_CHAR_QUESTION;
+ break;
+
+ case 0x160c: // (green)
+ element = EL_CHAR_COPYRIGHT;
+ break;
+
+ case 0x160d: // (green)
+ element = EL_CHAR_UP;
+ break;
+
+ case 0x160e: // (green)
+ element = EL_CHAR_DOWN;
+ break;
+
+ case 0x160f: // (green)
+ element = EL_CHAR_BUTTON;
+ break;
+
+ case 0x1610: // (green)
+ element = EL_CHAR_PLUS;
+ break;
+
+ case 0x1611: // (green)
+ element = EL_CHAR_MINUS;
+ break;
+
+ case 0x1612: // (green)
+ element = EL_CHAR_APOSTROPHE;
+ break;
+
+ case 0x1613: // (green)
+ element = EL_CHAR_PARENLEFT;
+ break;
+
+ case 0x1614: // (green)
+ element = EL_CHAR_PARENRIGHT;
+ break;
+
+ case 0x1615: // (blue steel)
+ element = EL_STEEL_CHAR_A;
+ break;
+
+ case 0x1616: // (blue steel)
+ element = EL_STEEL_CHAR_B;
+ break;
+
+ case 0x1617: // (blue steel)
+ element = EL_STEEL_CHAR_C;
+ break;
+
+ case 0x1618: // (blue steel)
+ element = EL_STEEL_CHAR_D;
+ break;
+
+ case 0x1619: // (blue steel)
+ element = EL_STEEL_CHAR_E;
+ break;
+
+ case 0x161a: // (blue steel)
+ element = EL_STEEL_CHAR_F;
+ break;
+
+ case 0x161b: // (blue steel)
+ element = EL_STEEL_CHAR_G;
+ break;
+
+ case 0x161c: // (blue steel)
+ element = EL_STEEL_CHAR_H;
+ break;
+
+ case 0x161d: // (blue steel)
+ element = EL_STEEL_CHAR_I;
+ break;
+
+ case 0x161e: // (blue steel)
+ element = EL_STEEL_CHAR_J;
+ break;
+
+ case 0x161f: // (blue steel)
+ element = EL_STEEL_CHAR_K;
+ break;
+
+ case 0x1620: // (blue steel)
+ element = EL_STEEL_CHAR_L;
+ break;
+
+ case 0x1621: // (blue steel)
+ element = EL_STEEL_CHAR_M;
+ break;
+
+ case 0x1622: // (blue steel)
+ element = EL_STEEL_CHAR_N;
+ break;
+
+ case 0x1623: // (blue steel)
+ element = EL_STEEL_CHAR_O;
+ break;
+
+ case 0x1624: // (blue steel)
+ element = EL_STEEL_CHAR_P;
+ break;
+
+ case 0x1625: // (blue steel)
+ element = EL_STEEL_CHAR_Q;
+ break;
+
+ case 0x1626: // (blue steel)
+ element = EL_STEEL_CHAR_R;
+ break;
+
+ case 0x1627: // (blue steel)
+ element = EL_STEEL_CHAR_S;
+ break;
+
+ case 0x1628: // (blue steel)
+ element = EL_STEEL_CHAR_T;
+ break;
+
+ case 0x1629: // (blue steel)
+ element = EL_STEEL_CHAR_U;
+ break;
+
+ case 0x162a: // (blue steel)
+ element = EL_STEEL_CHAR_V;
+ break;
+
+ case 0x162b: // (blue steel)
+ element = EL_STEEL_CHAR_W;
+ break;
+
+ case 0x162c: // (blue steel)
+ element = EL_STEEL_CHAR_X;
+ break;
+
+ case 0x162d: // (blue steel)
+ element = EL_STEEL_CHAR_Y;
+ break;
+
+ case 0x162e: // (blue steel)
+ element = EL_STEEL_CHAR_Z;
+ break;
+
+ case 0x162f: // (blue steel)
+ element = EL_STEEL_CHAR_AUMLAUT;
+ break;
+
+ case 0x1630: // (blue steel)
+ element = EL_STEEL_CHAR_OUMLAUT;
+ break;
+
+ case 0x1631: // (blue steel)
+ element = EL_STEEL_CHAR_UUMLAUT;
+ break;
+
+ case 0x1632: // (blue steel)
+ element = EL_STEEL_CHAR_0;
+ break;
+
+ case 0x1633: // (blue steel)
+ element = EL_STEEL_CHAR_1;
+ break;
+
+ case 0x1634: // (blue steel)
+ element = EL_STEEL_CHAR_2;
+ break;
+
+ case 0x1635: // (blue steel)
+ element = EL_STEEL_CHAR_3;
+ break;
+
+ case 0x1636: // (blue steel)
+ element = EL_STEEL_CHAR_4;
+ break;
+
+ case 0x1637: // (blue steel)
+ element = EL_STEEL_CHAR_5;
+ break;
+
+ case 0x1638: // (blue steel)
+ element = EL_STEEL_CHAR_6;
+ break;
+
+ case 0x1639: // (blue steel)
+ element = EL_STEEL_CHAR_7;
+ break;
+
+ case 0x163a: // (blue steel)
+ element = EL_STEEL_CHAR_8;
+ break;
+
+ case 0x163b: // (blue steel)
+ element = EL_STEEL_CHAR_9;
+ break;
+
+ case 0x163c: // (blue steel)
+ element = EL_STEEL_CHAR_PERIOD;
+ break;
+
+ case 0x163d: // (blue steel)
+ element = EL_STEEL_CHAR_EXCLAM;
+ break;
+
+ case 0x163e: // (blue steel)
+ element = EL_STEEL_CHAR_COLON;
+ break;
+
+ case 0x163f: // (blue steel)
+ element = EL_STEEL_CHAR_LESS;
+ break;
+
+ case 0x1640: // (blue steel)
+ element = EL_STEEL_CHAR_GREATER;
+ break;
+
+ case 0x1641: // (blue steel)
+ element = EL_STEEL_CHAR_QUESTION;
+ break;
+
+ case 0x1642: // (blue steel)
+ element = EL_STEEL_CHAR_COPYRIGHT;
+ break;
+
+ case 0x1643: // (blue steel)
+ element = EL_STEEL_CHAR_UP;
+ break;
+
+ case 0x1644: // (blue steel)
+ element = EL_STEEL_CHAR_DOWN;
+ break;
+
+ case 0x1645: // (blue steel)
+ element = EL_STEEL_CHAR_BUTTON;
+ break;
+
+ case 0x1646: // (blue steel)
+ element = EL_STEEL_CHAR_PLUS;
+ break;
+
+ case 0x1647: // (blue steel)
+ element = EL_STEEL_CHAR_MINUS;
+ break;
+
+ case 0x1648: // (blue steel)
+ element = EL_STEEL_CHAR_APOSTROPHE;
+ break;
+
+ case 0x1649: // (blue steel)
+ element = EL_STEEL_CHAR_PARENLEFT;
+ break;
+
+ case 0x164a: // (blue steel)
+ element = EL_STEEL_CHAR_PARENRIGHT;
+ break;
+
+ case 0x164b: // (green steel)
+ element = EL_STEEL_CHAR_A;
+ break;
+
+ case 0x164c: // (green steel)
+ element = EL_STEEL_CHAR_B;
+ break;
+
+ case 0x164d: // (green steel)
+ element = EL_STEEL_CHAR_C;
+ break;
+
+ case 0x164e: // (green steel)
+ element = EL_STEEL_CHAR_D;
+ break;
+
+ case 0x164f: // (green steel)
+ element = EL_STEEL_CHAR_E;
+ break;
+
+ case 0x1650: // (green steel)
+ element = EL_STEEL_CHAR_F;
+ break;
+
+ case 0x1651: // (green steel)
+ element = EL_STEEL_CHAR_G;
+ break;
+
+ case 0x1652: // (green steel)
+ element = EL_STEEL_CHAR_H;
+ break;
+
+ case 0x1653: // (green steel)
+ element = EL_STEEL_CHAR_I;
+ break;
+
+ case 0x1654: // (green steel)
+ element = EL_STEEL_CHAR_J;
+ break;
+
+ case 0x1655: // (green steel)
+ element = EL_STEEL_CHAR_K;
+ break;
+
+ case 0x1656: // (green steel)
+ element = EL_STEEL_CHAR_L;
+ break;
+
+ case 0x1657: // (green steel)
+ element = EL_STEEL_CHAR_M;
+ break;
+
+ case 0x1658: // (green steel)
+ element = EL_STEEL_CHAR_N;
+ break;
+
+ case 0x1659: // (green steel)
+ element = EL_STEEL_CHAR_O;
+ break;
+
+ case 0x165a: // (green steel)
+ element = EL_STEEL_CHAR_P;
+ break;
+
+ case 0x165b: // (green steel)
+ element = EL_STEEL_CHAR_Q;
+ break;
+
+ case 0x165c: // (green steel)
+ element = EL_STEEL_CHAR_R;
+ break;
+
+ case 0x165d: // (green steel)
+ element = EL_STEEL_CHAR_S;
+ break;
+
+ case 0x165e: // (green steel)
+ element = EL_STEEL_CHAR_T;
+ break;
+
+ case 0x165f: // (green steel)
+ element = EL_STEEL_CHAR_U;
+ break;
+
+ case 0x1660: // (green steel)
+ element = EL_STEEL_CHAR_V;
+ break;
+
+ case 0x1661: // (green steel)
+ element = EL_STEEL_CHAR_W;
+ break;
+
+ case 0x1662: // (green steel)
+ element = EL_STEEL_CHAR_X;
+ break;
+
+ case 0x1663: // (green steel)
+ element = EL_STEEL_CHAR_Y;
+ break;
+
+ case 0x1664: // (green steel)
+ element = EL_STEEL_CHAR_Z;
+ break;
+
+ case 0x1665: // (green steel)
+ element = EL_STEEL_CHAR_AUMLAUT;
+ break;
+
+ case 0x1666: // (green steel)
+ element = EL_STEEL_CHAR_OUMLAUT;
+ break;
+
+ case 0x1667: // (green steel)
+ element = EL_STEEL_CHAR_UUMLAUT;
+ break;
+
+ case 0x1668: // (green steel)
+ element = EL_STEEL_CHAR_0;
+ break;
+
+ case 0x1669: // (green steel)
+ element = EL_STEEL_CHAR_1;
+ break;
+
+ case 0x166a: // (green steel)
+ element = EL_STEEL_CHAR_2;
+ break;
+
+ case 0x166b: // (green steel)
+ element = EL_STEEL_CHAR_3;
+ break;
+
+ case 0x166c: // (green steel)
+ element = EL_STEEL_CHAR_4;
+ break;
+
+ case 0x166d: // (green steel)
+ element = EL_STEEL_CHAR_5;
+ break;
+
+ case 0x166e: // (green steel)
+ element = EL_STEEL_CHAR_6;
+ break;
+
+ case 0x166f: // (green steel)
+ element = EL_STEEL_CHAR_7;
+ break;
+
+ case 0x1670: // (green steel)
+ element = EL_STEEL_CHAR_8;
+ break;
+
+ case 0x1671: // (green steel)
+ element = EL_STEEL_CHAR_9;
+ break;
+
+ case 0x1672: // (green steel)
+ element = EL_STEEL_CHAR_PERIOD;
+ break;
+
+ case 0x1673: // (green steel)
+ element = EL_STEEL_CHAR_EXCLAM;
+ break;
+
+ case 0x1674: // (green steel)
+ element = EL_STEEL_CHAR_COLON;
+ break;
+
+ case 0x1675: // (green steel)
+ element = EL_STEEL_CHAR_LESS;
+ break;
+
+ case 0x1676: // (green steel)
+ element = EL_STEEL_CHAR_GREATER;
+ break;
+
+ case 0x1677: // (green steel)
+ element = EL_STEEL_CHAR_QUESTION;
+ break;
+
+ case 0x1678: // (green steel)
+ element = EL_STEEL_CHAR_COPYRIGHT;
+ break;
+
+ case 0x1679: // (green steel)
+ element = EL_STEEL_CHAR_UP;
+ break;
+
+ case 0x167a: // (green steel)
+ element = EL_STEEL_CHAR_DOWN;
+ break;
+
+ case 0x167b: // (green steel)
+ element = EL_STEEL_CHAR_BUTTON;
+ break;
+
+ case 0x167c: // (green steel)
+ element = EL_STEEL_CHAR_PLUS;
+ break;
+
+ case 0x167d: // (green steel)
+ element = EL_STEEL_CHAR_MINUS;
+ break;
+
+ case 0x167e: // (green steel)
+ element = EL_STEEL_CHAR_APOSTROPHE;
+ break;
+
+ case 0x167f: // (green steel)
+ element = EL_STEEL_CHAR_PARENLEFT;
+ break;
+
+ case 0x1680: // (green steel)
+ element = EL_STEEL_CHAR_PARENRIGHT;
+ break;
+
+ case 0x1681: // gate (red)
+ element = EL_EM_GATE_1;
+ break;
+
+ case 0x1682: // secret gate (red)
+ element = EL_EM_GATE_1_GRAY;
+ break;
+
+ case 0x1683: // gate (yellow)
+ element = EL_EM_GATE_2;
+ break;
+
+ case 0x1684: // secret gate (yellow)
+ element = EL_EM_GATE_2_GRAY;
+ break;
+
+ case 0x1685: // gate (blue)
+ element = EL_EM_GATE_4;
+ break;
+
+ case 0x1686: // secret gate (blue)
+ element = EL_EM_GATE_4_GRAY;
+ break;
+
+ case 0x1687: // gate (green)
+ element = EL_EM_GATE_3;
+ break;
+
+ case 0x1688: // secret gate (green)
+ element = EL_EM_GATE_3_GRAY;
+ break;
+
+ case 0x1689: // gate (white)
+ element = EL_DC_GATE_WHITE;
+ break;
+
+ case 0x168a: // secret gate (white)
+ element = EL_DC_GATE_WHITE_GRAY;
+ break;
+
+ case 0x168b: // secret gate (no key)
+ element = EL_DC_GATE_FAKE_GRAY;
+ break;
+
+ case 0x168c:
+ element = EL_ROBOT_WHEEL;
+ break;
+
+ case 0x168d:
+ element = EL_DC_TIMEGATE_SWITCH;
+ break;
+
+ case 0x168e:
+ element = EL_ACID_POOL_BOTTOM;
+ break;
+
+ case 0x168f:
+ element = EL_ACID_POOL_TOPLEFT;
+ break;
+
+ case 0x1690:
+ element = EL_ACID_POOL_TOPRIGHT;
+ break;
+
+ case 0x1691:
+ element = EL_ACID_POOL_BOTTOMLEFT;
+ break;
+
+ case 0x1692:
+ element = EL_ACID_POOL_BOTTOMRIGHT;
+ break;
+
+ case 0x1693:
+ element = EL_STEELWALL;
+ break;
+
+ case 0x1694:
+ element = EL_STEELWALL_SLIPPERY;
+ break;
+
+ case 0x1695: // steel wall (not round)
+ element = EL_STEELWALL;
+ break;
+
+ case 0x1696: // steel wall (left)
+ element = EL_DC_STEELWALL_1_LEFT;
+ break;
+
+ case 0x1697: // steel wall (bottom)
+ element = EL_DC_STEELWALL_1_BOTTOM;
+ break;
+
+ case 0x1698: // steel wall (right)
+ element = EL_DC_STEELWALL_1_RIGHT;
+ break;
+
+ case 0x1699: // steel wall (top)
+ element = EL_DC_STEELWALL_1_TOP;
+ break;
+
+ case 0x169a: // steel wall (left/bottom)
+ element = EL_DC_STEELWALL_1_BOTTOMLEFT;
+ break;
+
+ case 0x169b: // steel wall (right/bottom)
+ element = EL_DC_STEELWALL_1_BOTTOMRIGHT;
+ break;
+
+ case 0x169c: // steel wall (right/top)
+ element = EL_DC_STEELWALL_1_TOPRIGHT;
+ break;
+
+ case 0x169d: // steel wall (left/top)
+ element = EL_DC_STEELWALL_1_TOPLEFT;
+ break;
+
+ case 0x169e: // steel wall (right/bottom small)
+ element = EL_DC_STEELWALL_1_BOTTOMRIGHT_2;
+ break;
+
+ case 0x169f: // steel wall (left/bottom small)
+ element = EL_DC_STEELWALL_1_BOTTOMLEFT_2;
+ break;
+
+ case 0x16a0: // steel wall (right/top small)
+ element = EL_DC_STEELWALL_1_TOPRIGHT_2;
+ break;
+
+ case 0x16a1: // steel wall (left/top small)
+ element = EL_DC_STEELWALL_1_TOPLEFT_2;
+ break;
+
+ case 0x16a2: // steel wall (left/right)
+ element = EL_DC_STEELWALL_1_VERTICAL;
+ break;
+
+ case 0x16a3: // steel wall (top/bottom)
+ element = EL_DC_STEELWALL_1_HORIZONTAL;
+ break;
+
+ case 0x16a4: // steel wall 2 (left end)
+ element = EL_DC_STEELWALL_2_LEFT;
+ break;
+
+ case 0x16a5: // steel wall 2 (right end)
+ element = EL_DC_STEELWALL_2_RIGHT;
+ break;
+
+ case 0x16a6: // steel wall 2 (top end)
+ element = EL_DC_STEELWALL_2_TOP;
+ break;
+
+ case 0x16a7: // steel wall 2 (bottom end)
+ element = EL_DC_STEELWALL_2_BOTTOM;
+ break;
+
+ case 0x16a8: // steel wall 2 (left/right)
+ element = EL_DC_STEELWALL_2_HORIZONTAL;
+ break;
+
+ case 0x16a9: // steel wall 2 (up/down)
+ element = EL_DC_STEELWALL_2_VERTICAL;
+ break;
+
+ case 0x16aa: // steel wall 2 (mid)
+ element = EL_DC_STEELWALL_2_MIDDLE;
+ break;
+
+ case 0x16ab:
+ element = EL_SIGN_EXCLAMATION;
+ break;
+
+ case 0x16ac:
+ element = EL_SIGN_RADIOACTIVITY;
+ break;
+
+ case 0x16ad:
+ element = EL_SIGN_STOP;
+ break;
+
+ case 0x16ae:
+ element = EL_SIGN_WHEELCHAIR;
+ break;
+
+ case 0x16af:
+ element = EL_SIGN_PARKING;
+ break;
+
+ case 0x16b0:
+ element = EL_SIGN_NO_ENTRY;
+ break;
+
+ case 0x16b1:
+ element = EL_SIGN_HEART;
+ break;
+
+ case 0x16b2:
+ element = EL_SIGN_GIVE_WAY;
+ break;
+
+ case 0x16b3:
+ element = EL_SIGN_ENTRY_FORBIDDEN;
+ break;
+
+ case 0x16b4:
+ element = EL_SIGN_EMERGENCY_EXIT;
+ break;
+
+ case 0x16b5:
+ element = EL_SIGN_YIN_YANG;
+ break;
+
+ case 0x16b6:
+ element = EL_WALL_EMERALD;
+ break;
+
+ case 0x16b7:
+ element = EL_WALL_DIAMOND;
+ break;
+
+ case 0x16b8:
+ element = EL_WALL_PEARL;
+ break;
+
+ case 0x16b9:
+ element = EL_WALL_CRYSTAL;
+ break;
+
+ case 0x16ba:
+ element = EL_INVISIBLE_WALL;
+ break;
+
+ case 0x16bb:
+ element = EL_INVISIBLE_STEELWALL;
+ break;
+
+ // 0x16bc - 0x16cb:
+ // EL_INVISIBLE_SAND
+
+ case 0x16cc:
+ element = EL_LIGHT_SWITCH;
+ break;
+
+ case 0x16cd:
+ element = EL_ENVELOPE_1;
+ break;
+
+ default:
+ if (element >= 0x0117 && element <= 0x036e) // (?)
+ element = EL_DIAMOND;
+ else if (element >= 0x042d && element <= 0x0684) // (?)
+ element = EL_EMERALD;
+ else if (element >= 0x157c && element <= 0x158b)
+ element = EL_SAND;
+ else if (element >= 0x1590 && element <= 0x159f)
+ element = EL_DC_LANDMINE;
+ else if (element >= 0x16bc && element <= 0x16cb)
+ element = EL_INVISIBLE_SAND;
+ else
+ {
+ Warn("unknown Diamond Caves element 0x%04x", element);
+
+ element = EL_UNKNOWN;
+ }
+ break;
+ }
+
+ return getMappedElement(element);
+}
+
+static void LoadLevelFromFileStream_DC(File *file, struct LevelInfo *level)
+{
+ byte header[DC_LEVEL_HEADER_SIZE];
+ int envelope_size;
+ int envelope_header_pos = 62;
+ int envelope_content_pos = 94;
+ int level_name_pos = 251;
+ int level_author_pos = 292;
+ int envelope_header_len;
+ int envelope_content_len;
+ int level_name_len;
+ int level_author_len;
+ int fieldx, fieldy;
+ int num_yamyam_contents;
+ int i, x, y;
+
+ getDecodedWord_DC(0, TRUE); // initialize DC2 decoding engine
+
+ for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++)
+ {
+ unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
+
+ header[i * 2 + 0] = header_word >> 8;
+ header[i * 2 + 1] = header_word & 0xff;
+ }
+
+ // read some values from level header to check level decoding integrity
+ fieldx = header[6] | (header[7] << 8);
+ fieldy = header[8] | (header[9] << 8);
+ num_yamyam_contents = header[60] | (header[61] << 8);
+
+ // do some simple sanity checks to ensure that level was correctly decoded
+ if (fieldx < 1 || fieldx > 256 ||
+ fieldy < 1 || fieldy > 256 ||
+ num_yamyam_contents < 1 || num_yamyam_contents > 8)
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("cannot decode level from stream -- using empty level");
+
+ return;
+ }
+
+ // maximum envelope header size is 31 bytes
+ envelope_header_len = header[envelope_header_pos];
+ // maximum envelope content size is 110 (156?) bytes
+ envelope_content_len = header[envelope_content_pos];
+
+ // maximum level title size is 40 bytes
+ level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN);
+ // maximum level author size is 30 (51?) bytes
+ level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN);
+
+ envelope_size = 0;
+
+ for (i = 0; i < envelope_header_len; i++)
+ if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
+ level->envelope[0].text[envelope_size++] =
+ header[envelope_header_pos + 1 + i];
+
+ if (envelope_header_len > 0 && envelope_content_len > 0)
+ {
+ if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
+ level->envelope[0].text[envelope_size++] = '\n';
+ if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
+ level->envelope[0].text[envelope_size++] = '\n';
+ }
+
+ for (i = 0; i < envelope_content_len; i++)
+ if (envelope_size < MAX_ENVELOPE_TEXT_LEN)
+ level->envelope[0].text[envelope_size++] =
+ header[envelope_content_pos + 1 + i];
+
+ level->envelope[0].text[envelope_size] = '\0';
+
+ level->envelope[0].xsize = MAX_ENVELOPE_XSIZE;
+ level->envelope[0].ysize = 10;
+ level->envelope[0].autowrap = TRUE;
+ level->envelope[0].centered = TRUE;
+
+ for (i = 0; i < level_name_len; i++)
+ level->name[i] = header[level_name_pos + 1 + i];
+ level->name[level_name_len] = '\0';
+
+ for (i = 0; i < level_author_len; i++)
+ level->author[i] = header[level_author_pos + 1 + i];
+ level->author[level_author_len] = '\0';
+
+ num_yamyam_contents = header[60] | (header[61] << 8);
+ level->num_yamyam_contents =
+ MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS);
+
+ for (i = 0; i < num_yamyam_contents; i++)
+ {
+ for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
+ {
+ unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
+ int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
+
+ if (i < MAX_ELEMENT_CONTENTS)
+ level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc);
+ }
+ }
+
+ fieldx = header[6] | (header[7] << 8);
+ fieldy = header[8] | (header[9] << 8);
+ level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX);
+ level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY);
+
+ for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++)
+ {
+ unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE);
+ int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff);
+
+ if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY)
+ level->field[x][y] = getMappedElement_DC(element_dc);
+ }
+
+ x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1);
+ y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1);
+ level->field[x][y] = EL_PLAYER_1;
+
+ x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1);
+ y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1);
+ level->field[x][y] = EL_PLAYER_2;
+
+ level->gems_needed = header[18] | (header[19] << 8);
+
+ level->score[SC_EMERALD] = header[20] | (header[21] << 8);
+ level->score[SC_DIAMOND] = header[22] | (header[23] << 8);
+ level->score[SC_PEARL] = header[24] | (header[25] << 8);
+ level->score[SC_CRYSTAL] = header[26] | (header[27] << 8);
+ level->score[SC_NUT] = header[28] | (header[29] << 8);
+ level->score[SC_ROBOT] = header[30] | (header[31] << 8);
+ level->score[SC_SPACESHIP] = header[32] | (header[33] << 8);
+ level->score[SC_BUG] = header[34] | (header[35] << 8);
+ level->score[SC_YAMYAM] = header[36] | (header[37] << 8);
+ level->score[SC_DYNAMITE] = header[38] | (header[39] << 8);
+ level->score[SC_KEY] = header[40] | (header[41] << 8);
+ level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8);
+
+ level->time = header[44] | (header[45] << 8);
+
+ level->amoeba_speed = header[46] | (header[47] << 8);
+ level->time_light = header[48] | (header[49] << 8);
+ level->time_timegate = header[50] | (header[51] << 8);
+ level->time_wheel = header[52] | (header[53] << 8);
+ level->time_magic_wall = header[54] | (header[55] << 8);
+ level->extra_time = header[56] | (header[57] << 8);
+ level->shield_normal_time = header[58] | (header[59] << 8);
+
+ // shield and extra time elements do not have a score
+ level->score[SC_SHIELD] = 0;
+ level->extra_time_score = 0;
+
+ // set time for normal and deadly shields to the same value
+ level->shield_deadly_time = level->shield_normal_time;
+
+ // Diamond Caves has the same (strange) behaviour as Emerald Mine that gems
+ // can slip down from flat walls, like normal walls and steel walls
+ level->em_slippery_gems = TRUE;
+
+ // time score is counted for each 10 seconds left in Diamond Caves levels
+ level->time_score_base = 10;
+}
+
+static void LoadLevelFromFileInfo_DC(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ char *filename = level_file_info->filename;
+ File *file;
+ int num_magic_bytes = 8;
+ char magic_bytes[num_magic_bytes + 1];
+ int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
+
+ if (!(file = openFile(filename, MODE_READ)))
+ {
+ level->no_valid_file = TRUE;
+
+ if (!level_info_only)
+ Warn("cannot read level '%s' -- using empty level", filename);
+
+ return;
+ }
+
+ // fseek(file, 0x0000, SEEK_SET);
+
+ if (level_file_info->packed)
+ {
+ // read "magic bytes" from start of file
+ if (getStringFromFile(file, magic_bytes, num_magic_bytes + 1) == NULL)
+ magic_bytes[0] = '\0';
+
+ // check "magic bytes" for correct file format
+ if (!strPrefix(magic_bytes, "DC2"))
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("unknown DC level file '%s' -- using empty level", filename);
+
+ return;
+ }
+
+ if (strPrefix(magic_bytes, "DC2Win95") ||
+ strPrefix(magic_bytes, "DC2Win98"))
+ {
+ int position_first_level = 0x00fa;
+ int extra_bytes = 4;
+ int skip_bytes;
+
+ // advance file stream to first level inside the level package
+ skip_bytes = position_first_level - num_magic_bytes - extra_bytes;
+
+ // each block of level data is followed by block of non-level data
+ num_levels_to_skip *= 2;
+
+ // at least skip header bytes, therefore use ">= 0" instead of "> 0"
+ while (num_levels_to_skip >= 0)
+ {
+ // advance file stream to next level inside the level package
+ if (seekFile(file, skip_bytes, SEEK_CUR) != 0)
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("cannot fseek in file '%s' -- using empty level", filename);
+
+ return;
+ }
+
+ // skip apparently unused extra bytes following each level
+ ReadUnusedBytesFromFile(file, extra_bytes);
+
+ // read size of next level in level package
+ skip_bytes = getFile32BitLE(file);
+
+ num_levels_to_skip--;
+ }
+ }
+ else
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("unknown DC2 level file '%s' -- using empty level", filename);
+
+ return;
+ }
+ }
+
+ LoadLevelFromFileStream_DC(file, level);
+
+ closeFile(file);
+}
+
+
+// ----------------------------------------------------------------------------
+// functions for loading SB level
+// ----------------------------------------------------------------------------
+
+int getMappedElement_SB(int element_ascii, boolean use_ces)
+{
+ static struct
+ {
+ int ascii;
+ int sb;
+ int ce;
+ }
+ sb_element_mapping[] =
+ {
+ { ' ', EL_EMPTY, EL_CUSTOM_1 }, // floor (space)
+ { '#', EL_STEELWALL, EL_CUSTOM_2 }, // wall
+ { '@', EL_PLAYER_1, EL_CUSTOM_3 }, // player
+ { '$', EL_SOKOBAN_OBJECT, EL_CUSTOM_4 }, // box
+ { '.', EL_SOKOBAN_FIELD_EMPTY, EL_CUSTOM_5 }, // goal square
+ { '*', EL_SOKOBAN_FIELD_FULL, EL_CUSTOM_6 }, // box on goal square
+ { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 }, // player on goal square
+ { '_', EL_INVISIBLE_STEELWALL, EL_FROM_LEVEL_TEMPLATE }, // floor beyond border
+
+ { 0, -1, -1 },
+ };
+
+ int i;
+
+ for (i = 0; sb_element_mapping[i].ascii != 0; i++)
+ if (element_ascii == sb_element_mapping[i].ascii)
+ return (use_ces ? sb_element_mapping[i].ce : sb_element_mapping[i].sb);
+
+ return EL_UNDEFINED;
+}
+
+static void SetLevelSettings_SB(struct LevelInfo *level)
+{
+ // time settings
+ level->time = 0;
+ level->use_step_counter = TRUE;
+
+ // score settings
+ level->score[SC_TIME_BONUS] = 0;
+ level->time_score_base = 1;
+ level->rate_time_over_score = TRUE;
+
+ // game settings
+ level->auto_exit_sokoban = TRUE;
+}
+
+static void LoadLevelFromFileInfo_SB(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ char *filename = level_file_info->filename;
+ char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
+ char last_comment[MAX_LINE_LEN];
+ char level_name[MAX_LINE_LEN];
+ char *line_ptr;
+ File *file;
+ int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level;
+ boolean read_continued_line = FALSE;
+ boolean reading_playfield = FALSE;
+ boolean got_valid_playfield_line = FALSE;
+ boolean invalid_playfield_char = FALSE;
+ boolean load_xsb_to_ces = check_special_flags("load_xsb_to_ces");
+ int file_level_nr = 0;
+ int line_nr = 0;
+ int x = 0, y = 0; // initialized to make compilers happy
+
+ last_comment[0] = '\0';
+ level_name[0] = '\0';
+
+ if (!(file = openFile(filename, MODE_READ)))
+ {
+ level->no_valid_file = TRUE;
+
+ if (!level_info_only)
+ Warn("cannot read level '%s' -- using empty level", filename);
+
+ return;
+ }
+
+ while (!checkEndOfFile(file))
+ {
+ // level successfully read, but next level may follow here
+ if (!got_valid_playfield_line && reading_playfield)
+ {
+ // read playfield from single level file -- skip remaining file
+ if (!level_file_info->packed)
+ break;
+
+ if (file_level_nr >= num_levels_to_skip)
+ break;
+
+ file_level_nr++;
+
+ last_comment[0] = '\0';
+ level_name[0] = '\0';
+
+ reading_playfield = FALSE;
+ }
+
+ got_valid_playfield_line = FALSE;
+
+ // read next line of input file
+ if (!getStringFromFile(file, line, MAX_LINE_LEN))
+ break;
+
+ // check if line was completely read and is terminated by line break
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
+ line_nr++;
+
+ // cut trailing line break (this can be newline and/or carriage return)
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ // copy raw input line for later use (mainly debugging output)
+ strcpy(line_raw, line);
+
+ if (read_continued_line)
+ {
+ // append new line to existing line, if there is enough space
+ if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
+ strcat(previous_line, line_ptr);
+
+ strcpy(line, previous_line); // copy storage buffer to line
+
+ read_continued_line = FALSE;
+ }
+
+ // if the last character is '\', continue at next line
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
+ {
+ line[strlen(line) - 1] = '\0'; // cut off trailing backslash
+ strcpy(previous_line, line); // copy line to storage buffer
+
+ read_continued_line = TRUE;
+
+ continue;
+ }
+
+ // skip empty lines
+ if (line[0] == '\0')
+ continue;
+
+ // extract comment text from comment line
+ if (line[0] == ';')
+ {
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != ';')
+ break;
+
+ strcpy(last_comment, line_ptr);
+
+ continue;
+ }
+
+ // extract level title text from line containing level title
+ if (line[0] == '\'')
+ {
+ strcpy(level_name, &line[1]);
+
+ if (strlen(level_name) > 0 && level_name[strlen(level_name) - 1] == '\'')
+ level_name[strlen(level_name) - 1] = '\0';
+
+ continue;
+ }
+
+ // skip lines containing only spaces (or empty lines)
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ if (*line_ptr != ' ')
+ break;
+ if (*line_ptr == '\0')
+ continue;
+
+ // at this point, we have found a line containing part of a playfield
+
+ got_valid_playfield_line = TRUE;
+
+ if (!reading_playfield)
+ {
+ reading_playfield = TRUE;
+ invalid_playfield_char = FALSE;
+
+ for (x = 0; x < MAX_LEV_FIELDX; x++)
+ for (y = 0; y < MAX_LEV_FIELDY; y++)
+ level->field[x][y] = getMappedElement_SB(' ', load_xsb_to_ces);
+
+ level->fieldx = 0;
+ level->fieldy = 0;
+
+ // start with topmost tile row
+ y = 0;
+ }
+
+ // skip playfield line if larger row than allowed
+ if (y >= MAX_LEV_FIELDY)
+ continue;
+
+ // start with leftmost tile column
+ x = 0;
+
+ // read playfield elements from line
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ int mapped_sb_element = getMappedElement_SB(*line_ptr, load_xsb_to_ces);
+
+ // stop parsing playfield line if larger column than allowed
+ if (x >= MAX_LEV_FIELDX)
+ break;
+
+ if (mapped_sb_element == EL_UNDEFINED)
+ {
+ invalid_playfield_char = TRUE;
+
+ break;
+ }
+
+ level->field[x][y] = mapped_sb_element;
+
+ // continue with next tile column
+ x++;
+
+ level->fieldx = MAX(x, level->fieldx);
+ }
+
+ if (invalid_playfield_char)
+ {
+ // if first playfield line, treat invalid lines as comment lines
+ if (y == 0)
+ reading_playfield = FALSE;
+
+ continue;
+ }
+
+ // continue with next tile row
+ y++;
+ }
+
+ closeFile(file);
+
+ level->fieldy = y;
+
+ level->fieldx = MIN(MAX(MIN_LEV_FIELDX, level->fieldx), MAX_LEV_FIELDX);
+ level->fieldy = MIN(MAX(MIN_LEV_FIELDY, level->fieldy), MAX_LEV_FIELDY);
+
+ if (!reading_playfield)
+ {
+ level->no_valid_file = TRUE;
+
+ Warn("cannot read level '%s' -- using empty level", filename);
+
+ return;
+ }
+
+ if (*level_name != '\0')
+ {
+ strncpy(level->name, level_name, MAX_LEVEL_NAME_LEN);
+ level->name[MAX_LEVEL_NAME_LEN] = '\0';
+ }
+ else if (*last_comment != '\0')
+ {
+ strncpy(level->name, last_comment, MAX_LEVEL_NAME_LEN);
+ level->name[MAX_LEVEL_NAME_LEN] = '\0';
+ }
+ else
+ {
+ sprintf(level->name, "--> Level %d <--", level_file_info->nr);
+ }
+
+ // set all empty fields beyond the border walls to invisible steel wall
+ for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++)
+ {
+ if ((x == 0 || x == level->fieldx - 1 ||
+ y == 0 || y == level->fieldy - 1) &&
+ level->field[x][y] == getMappedElement_SB(' ', load_xsb_to_ces))
+ FloodFillLevel(x, y, getMappedElement_SB('_', load_xsb_to_ces),
+ level->field, level->fieldx, level->fieldy);
+ }
+
+ // set special level settings for Sokoban levels
+ SetLevelSettings_SB(level);
+
+ if (load_xsb_to_ces)
+ {
+ // special global settings can now be set in level template
+ level->use_custom_template = TRUE;
+ }
+}
+
+
+// -------------------------------------------------------------------------
+// functions for handling native levels
+// -------------------------------------------------------------------------
+
+static void LoadLevelFromFileInfo_EM(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ if (!LoadNativeLevel_EM(level_file_info->filename, level_info_only))
+ level->no_valid_file = TRUE;
+}
+
+static void LoadLevelFromFileInfo_SP(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ int pos = 0;
+
+ // determine position of requested level inside level package
+ if (level_file_info->packed)
+ pos = level_file_info->nr - leveldir_current->first_level;
+
+ if (!LoadNativeLevel_SP(level_file_info->filename, pos, level_info_only))
+ level->no_valid_file = TRUE;
+}
+
+static void LoadLevelFromFileInfo_MM(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ if (!LoadNativeLevel_MM(level_file_info->filename, level_info_only))
+ level->no_valid_file = TRUE;
+}
+
+void CopyNativeLevel_RND_to_Native(struct LevelInfo *level)
+{
+ if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
+ CopyNativeLevel_RND_to_EM(level);
+ else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
+ CopyNativeLevel_RND_to_SP(level);
+ else if (level->game_engine_type == GAME_ENGINE_TYPE_MM)
+ CopyNativeLevel_RND_to_MM(level);
+}
+
+void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
+{
+ if (level->game_engine_type == GAME_ENGINE_TYPE_EM)
+ CopyNativeLevel_EM_to_RND(level);
+ else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
+ CopyNativeLevel_SP_to_RND(level);
+ else if (level->game_engine_type == GAME_ENGINE_TYPE_MM)
+ CopyNativeLevel_MM_to_RND(level);
+}
+
+void SaveNativeLevel(struct LevelInfo *level)
+{
+ if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
+ {
+ char *basename = getSingleLevelBasenameExt(level->file_info.nr, "sp");
+ char *filename = getLevelFilenameFromBasename(basename);
+
+ CopyNativeLevel_RND_to_SP(level);
+ CopyNativeTape_RND_to_SP(level);
+
+ SaveNativeLevel_SP(filename);
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// functions for loading generic level
+// ----------------------------------------------------------------------------
+
+static void LoadLevelFromFileInfo(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info,
+ boolean level_info_only)
+{
+ // always start with reliable default values
+ setLevelInfoToDefaults(level, level_info_only, TRUE);
+
+ switch (level_file_info->type)
+ {
+ case LEVEL_FILE_TYPE_RND:
+ LoadLevelFromFileInfo_RND(level, level_file_info, level_info_only);
+ break;
+
+ case LEVEL_FILE_TYPE_EM:
+ LoadLevelFromFileInfo_EM(level, level_file_info, level_info_only);
+ level->game_engine_type = GAME_ENGINE_TYPE_EM;
+ break;
+
+ case LEVEL_FILE_TYPE_SP:
+ LoadLevelFromFileInfo_SP(level, level_file_info, level_info_only);
+ level->game_engine_type = GAME_ENGINE_TYPE_SP;
+ break;
+
+ case LEVEL_FILE_TYPE_MM:
+ LoadLevelFromFileInfo_MM(level, level_file_info, level_info_only);
+ level->game_engine_type = GAME_ENGINE_TYPE_MM;
+ break;
+
+ case LEVEL_FILE_TYPE_DC:
+ LoadLevelFromFileInfo_DC(level, level_file_info, level_info_only);
+ break;
+
+ case LEVEL_FILE_TYPE_SB:
+ LoadLevelFromFileInfo_SB(level, level_file_info, level_info_only);
+ break;
+
+ default:
+ LoadLevelFromFileInfo_RND(level, level_file_info, level_info_only);
+ break;
+ }
+
+ // if level file is invalid, restore level structure to default values
+ if (level->no_valid_file)
+ setLevelInfoToDefaults(level, level_info_only, FALSE);
+
+ if (level->game_engine_type == GAME_ENGINE_TYPE_UNKNOWN)
+ level->game_engine_type = GAME_ENGINE_TYPE_RND;
+
+ if (level_file_info->type != LEVEL_FILE_TYPE_RND)
+ CopyNativeLevel_Native_to_RND(level);
+}
+
+void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
+{
+ static struct LevelFileInfo level_file_info;
+
+ // always start with reliable default values
+ setFileInfoToDefaults(&level_file_info);
+
+ level_file_info.nr = 0; // unknown level number
+ level_file_info.type = LEVEL_FILE_TYPE_RND; // no others supported yet
+
+ setString(&level_file_info.filename, filename);
+
+ LoadLevelFromFileInfo(level, &level_file_info, FALSE);
+}
+
+static void LoadLevel_InitVersion(struct LevelInfo *level)
+{
+ int i, j;
+
+ if (leveldir_current == NULL) // only when dumping level
+ return;
+
+ // all engine modifications also valid for levels which use latest engine
+ if (level->game_version < VERSION_IDENT(3,2,0,5))
+ {
+ // time bonus score was given for 10 s instead of 1 s before 3.2.0-5
+ level->time_score_base = 10;
+ }
+
+ if (leveldir_current->latest_engine)
+ {
+ // ---------- use latest game engine --------------------------------------
+
+ /* For all levels which are forced to use the latest game engine version
+ (normally all but user contributed, private and undefined levels), set
+ the game engine version to the actual version; this allows for actual
+ corrections in the game engine to take effect for existing, converted
+ levels (from "classic" or other existing games) to make the emulation
+ of the corresponding game more accurate, while (hopefully) not breaking
+ existing levels created from other players. */
+
+ level->game_version = GAME_VERSION_ACTUAL;
+
+ /* Set special EM style gems behaviour: EM style gems slip down from
+ normal, steel and growing wall. As this is a more fundamental change,
+ it seems better to set the default behaviour to "off" (as it is more
+ natural) and make it configurable in the level editor (as a property
+ of gem style elements). Already existing converted levels (neither
+ private nor contributed levels) are changed to the new behaviour. */
+
+ if (level->file_version < FILE_VERSION_2_0)
+ level->em_slippery_gems = TRUE;
+
+ return;
+ }
+
+ // ---------- use game engine the level was created with --------------------
+
+ /* For all levels which are not forced to use the latest game engine
+ version (normally user contributed, private and undefined levels),
+ use the version of the game engine the levels were created for.
+
+ Since 2.0.1, the game engine version is now directly stored
+ in the level file (chunk "VERS"), so there is no need anymore
+ to set the game version from the file version (except for old,
+ pre-2.0 levels, where the game version is still taken from the
+ file format version used to store the level -- see above). */
+
+ // player was faster than enemies in 1.0.0 and before
+ if (level->file_version == FILE_VERSION_1_0)
+ for (i = 0; i < MAX_PLAYERS; i++)
+ level->initial_player_stepsize[i] = STEPSIZE_FAST;
+
+ // default behaviour for EM style gems was "slippery" only in 2.0.1
+ if (level->game_version == VERSION_IDENT(2,0,1,0))
+ level->em_slippery_gems = TRUE;
+
+ // springs could be pushed over pits before (pre-release version) 2.2.0
+ if (level->game_version < VERSION_IDENT(2,2,0,0))
+ level->use_spring_bug = TRUE;
+
+ if (level->game_version < VERSION_IDENT(3,2,0,5))
+ {
+ // time orb caused limited time in endless time levels before 3.2.0-5
+ level->use_time_orb_bug = TRUE;
+
+ // default behaviour for snapping was "no snap delay" before 3.2.0-5
+ level->block_snap_field = FALSE;
+
+ // extra time score was same value as time left score before 3.2.0-5
+ level->extra_time_score = level->score[SC_TIME_BONUS];
+ }
+
+ if (level->game_version < VERSION_IDENT(3,2,0,7))
+ {
+ // default behaviour for snapping was "not continuous" before 3.2.0-7
+ level->continuous_snapping = FALSE;
+ }
+
+ // only few elements were able to actively move into acid before 3.1.0
+ // trigger settings did not exist before 3.1.0; set to default "any"
+ if (level->game_version < VERSION_IDENT(3,1,0,0))
+ {
+ // correct "can move into acid" settings (all zero in old levels)
+
+ level->can_move_into_acid_bits = 0; // nothing can move into acid
+ level->dont_collide_with_bits = 0; // nothing is deadly when colliding
+
+ setMoveIntoAcidProperty(level, EL_ROBOT, TRUE);
+ setMoveIntoAcidProperty(level, EL_SATELLITE, TRUE);
+ setMoveIntoAcidProperty(level, EL_PENGUIN, TRUE);
+ setMoveIntoAcidProperty(level, EL_BALLOON, TRUE);
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ SET_PROPERTY(EL_CUSTOM_START + i, EP_CAN_MOVE_INTO_ACID, TRUE);
+
+ // correct trigger settings (stored as zero == "none" in old levels)
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ for (j = 0; j < ei->num_change_pages; j++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[j];
+
+ change->trigger_player = CH_PLAYER_ANY;
+ change->trigger_page = CH_PAGE_ANY;
+ }
+ }
+ }
+
+ // try to detect and fix "Snake Bite" levels, which are broken with 3.2.0
+ {
+ int element = EL_CUSTOM_256;
+ struct ElementInfo *ei = &element_info[element];
+ struct ElementChangeInfo *change = &ei->change_page[0];
+
+ /* This is needed to fix a problem that was caused by a bugfix in function
+ game.c/CreateFieldExt() introduced with 3.2.0 that corrects the behaviour
+ when a custom element changes to EL_SOKOBAN_FIELD_PLAYER (before, it did
+ not replace walkable elements, but instead just placed the player on it,
+ without placing the Sokoban field under the player). Unfortunately, this
+ breaks "Snake Bite" style levels when the snake is halfway through a door
+ that just closes (the snake head is still alive and can be moved in this
+ case). This can be fixed by replacing the EL_SOKOBAN_FIELD_PLAYER by the
+ player (without Sokoban element) which then gets killed as designed). */
+
+ if ((strncmp(leveldir_current->identifier, "snake_bite", 10) == 0 ||
+ strncmp(ei->description, "pause b4 death", 14) == 0) &&
+ change->target_element == EL_SOKOBAN_FIELD_PLAYER)
+ change->target_element = EL_PLAYER_1;
+ }
+
+ // try to detect and fix "Zelda" style levels, which are broken with 3.2.5
+ if (level->game_version < VERSION_IDENT(3,2,5,0))
+ {
+ /* This is needed to fix a problem that was caused by a bugfix in function
+ game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
+ corrects the behaviour when a custom element changes to another custom
+ element with a higher element number that has change actions defined.
+ Normally, only one change per frame is allowed for custom elements.
+ Therefore, it is checked if a custom element already changed in the
+ current frame; if it did, subsequent changes are suppressed.
+ Unfortunately, this is only checked for element changes, but not for
+ change actions, which are still executed. As the function above loops
+ through all custom elements from lower to higher, an element change
+ resulting in a lower CE number won't be checked again, while a target
+ element with a higher number will also be checked, and potential change
+ actions will get executed for this CE, too (which is wrong), while
+ further changes are ignored (which is correct). As this bugfix breaks
+ Zelda II (and introduces graphical bugs to Zelda I, and also breaks a
+ few other levels like Alan Bond's "FMV"), allow the previous, incorrect
+ behaviour for existing levels and tapes that make use of this bug */
+
+ level->use_action_after_change_bug = TRUE;
+ }
+
+ // not centering level after relocating player was default only in 3.2.3
+ if (level->game_version == VERSION_IDENT(3,2,3,0)) // (no pre-releases)
+ level->shifted_relocation = TRUE;
+
+ // EM style elements always chain-exploded in R'n'D engine before 3.2.6
+ if (level->game_version < VERSION_IDENT(3,2,6,0))
+ level->em_explodes_by_fire = TRUE;
+
+ // levels were solved by the first player entering an exit up to 4.1.0.0
+ if (level->game_version <= VERSION_IDENT(4,1,0,0))
+ level->solved_by_one_player = TRUE;
+
+ // game logic of "game of life" and "biomaze" was buggy before 4.1.1.1
+ if (level->game_version < VERSION_IDENT(4,1,1,1))
+ level->use_life_bugs = TRUE;
+
+ // only Sokoban fields (but not objects) had to be solved before 4.1.1.1
+ if (level->game_version < VERSION_IDENT(4,1,1,1))
+ level->sb_objects_needed = FALSE;
+
+ // CE actions were triggered by unfinished digging/collecting up to 4.2.2.0
+ if (level->game_version <= VERSION_IDENT(4,2,2,0))
+ level->finish_dig_collect = FALSE;
+
+ // CE changing to player was kept under the player if walkable up to 4.2.3.1
+ if (level->game_version <= VERSION_IDENT(4,2,3,1))
+ level->keep_walkable_ce = TRUE;
+}
+
+static void LoadLevel_InitSettings_SB(struct LevelInfo *level)
+{
+ boolean is_sokoban_level = TRUE; // unless non-Sokoban elements found
+ int x, y;
+
+ // check if this level is (not) a Sokoban level
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ if (!IS_SB_ELEMENT(Tile[x][y]))
+ is_sokoban_level = FALSE;
+
+ if (is_sokoban_level)
+ {
+ // set special level settings for Sokoban levels
+ SetLevelSettings_SB(level);
+ }
+}
+
+static void LoadLevel_InitSettings(struct LevelInfo *level)
+{
+ // adjust level settings for (non-native) Sokoban-style levels
+ LoadLevel_InitSettings_SB(level);
+
+ // rename levels with title "nameless level" or if renaming is forced
+ if (leveldir_current->empty_level_name != NULL &&
+ (strEqual(level->name, NAMELESS_LEVEL_NAME) ||
+ leveldir_current->force_level_name))
+ snprintf(level->name, MAX_LEVEL_NAME_LEN + 1,
+ leveldir_current->empty_level_name, level_nr);
+}
+
+static void LoadLevel_InitStandardElements(struct LevelInfo *level)
+{
+ int i, x, y;
+
+ // map elements that have changed in newer versions
+ level->amoeba_content = getMappedElementByVersion(level->amoeba_content,
+ level->game_version);
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (x = 0; x < 3; x++)
+ for (y = 0; y < 3; y++)
+ level->yamyam_content[i].e[x][y] =
+ getMappedElementByVersion(level->yamyam_content[i].e[x][y],
+ level->game_version);
+
+}
+
+static void LoadLevel_InitCustomElements(struct LevelInfo *level)
+{
+ int i, j;
+
+ // map custom element change events that have changed in newer versions
+ // (these following values were accidentally changed in version 3.0.1)
+ // (this seems to be needed only for 'ab_levelset3' and 'ab_levelset4')
+ if (level->game_version <= VERSION_IDENT(3,0,0,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ // order of checking and copying events to be mapped is important
+ // (do not change the start and end value -- they are constant)
+ for (j = CE_BY_OTHER_ACTION; j >= CE_VALUE_GETS_ZERO; j--)
+ {
+ if (HAS_CHANGE_EVENT(element, j - 2))
+ {
+ SET_CHANGE_EVENT(element, j - 2, FALSE);
+ SET_CHANGE_EVENT(element, j, TRUE);
+ }
+ }
+
+ // order of checking and copying events to be mapped is important
+ // (do not change the start and end value -- they are constant)
+ for (j = CE_PLAYER_COLLECTS_X; j >= CE_HITTING_SOMETHING; j--)
+ {
+ if (HAS_CHANGE_EVENT(element, j - 1))
+ {
+ SET_CHANGE_EVENT(element, j - 1, FALSE);
+ SET_CHANGE_EVENT(element, j, TRUE);
+ }
+ }
+ }
+ }
+
+ // initialize "can_change" field for old levels with only one change page
+ if (level->game_version <= VERSION_IDENT(3,0,2,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (CAN_CHANGE(element))
+ element_info[element].change->can_change = TRUE;
+ }
+ }
+
+ // correct custom element values (for old levels without these options)
+ if (level->game_version < VERSION_IDENT(3,1,1,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ if (ei->access_direction == MV_NO_DIRECTION)
+ ei->access_direction = MV_ALL_DIRECTIONS;
+ }
+ }
+
+ // correct custom element values (fix invalid values for all versions)
+ if (1)
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ for (j = 0; j < ei->num_change_pages; j++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[j];
+
+ if (change->trigger_player == CH_PLAYER_NONE)
+ change->trigger_player = CH_PLAYER_ANY;
+
+ if (change->trigger_side == CH_SIDE_NONE)
+ change->trigger_side = CH_SIDE_ANY;
+ }
+ }
+ }
+
+ // initialize "can_explode" field for old levels which did not store this
+ // !!! CHECK THIS -- "<= 3,1,0,0" IS PROBABLY WRONG !!!
+ if (level->game_version <= VERSION_IDENT(3,1,0,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (EXPLODES_1X1_OLD(element))
+ element_info[element].explosion_type = EXPLODES_1X1;
+
+ SET_PROPERTY(element, EP_CAN_EXPLODE, (EXPLODES_BY_FIRE(element) ||
+ EXPLODES_SMASHED(element) ||
+ EXPLODES_IMPACT(element)));
+ }
+ }
+
+ // correct previously hard-coded move delay values for maze runner style
+ if (level->game_version < VERSION_IDENT(3,1,1,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (element_info[element].move_pattern & MV_MAZE_RUNNER_STYLE)
+ {
+ // previously hard-coded and therefore ignored
+ element_info[element].move_delay_fixed = 9;
+ element_info[element].move_delay_random = 0;
+ }
+ }
+ }
+
+ // set some other uninitialized values of custom elements in older levels
+ if (level->game_version < VERSION_IDENT(3,1,0,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ element_info[element].access_direction = MV_ALL_DIRECTIONS;
+
+ element_info[element].explosion_delay = 17;
+ element_info[element].ignition_delay = 8;
+ }
+ }
+
+ // set mouse click change events to work for left/middle/right mouse button
+ if (level->game_version < VERSION_IDENT(4,2,3,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ for (j = 0; j < ei->num_change_pages; j++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[j];
+
+ if (change->has_event[CE_CLICKED_BY_MOUSE] ||
+ change->has_event[CE_PRESSED_BY_MOUSE] ||
+ change->has_event[CE_MOUSE_CLICKED_ON_X] ||
+ change->has_event[CE_MOUSE_PRESSED_ON_X])
+ change->trigger_side = CH_SIDE_ANY;
+ }
+ }
+ }
+}
+
+static void LoadLevel_InitElements(struct LevelInfo *level)
+{
+ LoadLevel_InitStandardElements(level);
+
+ if (level->file_has_custom_elements)
+ LoadLevel_InitCustomElements(level);
+
+ // initialize element properties for level editor etc.
+ InitElementPropertiesEngine(level->game_version);
+ InitElementPropertiesGfxElement();
+}
+
+static void LoadLevel_InitPlayfield(struct LevelInfo *level)
+{
+ int x, y;
+
+ // map elements that have changed in newer versions
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ level->field[x][y] = getMappedElementByVersion(level->field[x][y],
+ level->game_version);
+
+ // clear unused playfield data (nicer if level gets resized in editor)
+ for (x = 0; x < MAX_LEV_FIELDX; x++)
+ for (y = 0; y < MAX_LEV_FIELDY; y++)
+ if (x >= level->fieldx || y >= level->fieldy)
+ level->field[x][y] = EL_EMPTY;
+
+ // copy elements to runtime playfield array
+ for (x = 0; x < MAX_LEV_FIELDX; x++)
+ for (y = 0; y < MAX_LEV_FIELDY; y++)
+ Tile[x][y] = level->field[x][y];
+
+ // initialize level size variables for faster access
+ lev_fieldx = level->fieldx;
+ lev_fieldy = level->fieldy;
+
+ // determine border element for this level
+ if (level->file_info.type == LEVEL_FILE_TYPE_DC)
+ BorderElement = EL_EMPTY; // (in editor, SetBorderElement() is used)
+ else
+ SetBorderElement();
+}
+
+static void LoadLevel_InitNativeEngines(struct LevelInfo *level)
+{
+ struct LevelFileInfo *level_file_info = &level->file_info;
+
+ if (level_file_info->type == LEVEL_FILE_TYPE_RND)
+ CopyNativeLevel_RND_to_Native(level);
+}
+
+static void LoadLevelTemplate_LoadAndInit(void)
+{
+ LoadLevelFromFileInfo(&level_template, &level_template.file_info, FALSE);
+
+ LoadLevel_InitVersion(&level_template);
+ LoadLevel_InitElements(&level_template);
+ LoadLevel_InitSettings(&level_template);
+
+ ActivateLevelTemplate();
+}
+
+void LoadLevelTemplate(int nr)
+{
+ if (!fileExists(getGlobalLevelTemplateFilename()))
+ {
+ Warn("no level template found for this level");
+
+ return;
+ }
+
+ setLevelFileInfo(&level_template.file_info, nr);
+
+ LoadLevelTemplate_LoadAndInit();
+}
+
+static void LoadNetworkLevelTemplate(struct NetworkLevelInfo *network_level)
+{
+ copyLevelFileInfo(&network_level->tmpl_info, &level_template.file_info);
+
+ LoadLevelTemplate_LoadAndInit();
+}
+
+static void LoadLevel_LoadAndInit(struct NetworkLevelInfo *network_level)
+{
+ LoadLevelFromFileInfo(&level, &level.file_info, FALSE);
+
+ if (level.use_custom_template)
+ {
+ if (network_level != NULL)
+ LoadNetworkLevelTemplate(network_level);
+ else
+ LoadLevelTemplate(-1);
+ }
+
+ LoadLevel_InitVersion(&level);
+ LoadLevel_InitElements(&level);
+ LoadLevel_InitPlayfield(&level);
+ LoadLevel_InitSettings(&level);
+
+ LoadLevel_InitNativeEngines(&level);
+}
+
+void LoadLevel(int nr)
+{
+ SetLevelSetInfo(leveldir_current->identifier, nr);
+
+ setLevelFileInfo(&level.file_info, nr);
+
+ LoadLevel_LoadAndInit(NULL);
+}
+
+void LoadLevelInfoOnly(int nr)
+{
+ setLevelFileInfo(&level.file_info, nr);
+
+ LoadLevelFromFileInfo(&level, &level.file_info, TRUE);
+}
+
+void LoadNetworkLevel(struct NetworkLevelInfo *network_level)
+{
+ SetLevelSetInfo(network_level->leveldir_identifier,
+ network_level->file_info.nr);
+
+ copyLevelFileInfo(&network_level->file_info, &level.file_info);
+
+ LoadLevel_LoadAndInit(network_level);
+}
+
+static int SaveLevel_VERS(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+
+ chunk_size += putFileVersion(file, level->file_version);
+ chunk_size += putFileVersion(file, level->game_version);
+
+ return chunk_size;
+}
+
+static int SaveLevel_DATE(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+
+ chunk_size += putFile16BitBE(file, level->creation_date.year);
+ chunk_size += putFile8Bit(file, level->creation_date.month);
+ chunk_size += putFile8Bit(file, level->creation_date.day);
+
+ return chunk_size;
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ putFile8Bit(file, level->fieldx);
+ putFile8Bit(file, level->fieldy);
+
+ putFile16BitBE(file, level->time);
+ putFile16BitBE(file, level->gems_needed);
+
+ for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
+ putFile8Bit(file, level->name[i]);
+
+ for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
+ putFile8Bit(file, level->score[i]);
+
+ for (i = 0; i < STD_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile8Bit(file, (level->encoding_16bit_yamyam ? EL_EMPTY :
+ level->yamyam_content[i].e[x][y]));
+ putFile8Bit(file, level->amoeba_speed);
+ putFile8Bit(file, level->time_magic_wall);
+ putFile8Bit(file, level->time_wheel);
+ putFile8Bit(file, (level->encoding_16bit_amoeba ? EL_EMPTY :
+ level->amoeba_content));
+ putFile8Bit(file, (level->initial_player_stepsize == STEPSIZE_FAST ? 1 : 0));
+ putFile8Bit(file, (level->initial_gravity ? 1 : 0));
+ putFile8Bit(file, (level->encoding_16bit_field ? 1 : 0));
+ putFile8Bit(file, (level->em_slippery_gems ? 1 : 0));
+
+ putFile8Bit(file, (level->use_custom_template ? 1 : 0));
+
+ putFile8Bit(file, (level->block_last_field ? 1 : 0));
+ putFile8Bit(file, (level->sp_block_last_field ? 1 : 0));
+ putFile32BitBE(file, level->can_move_into_acid_bits);
+ putFile8Bit(file, level->dont_collide_with_bits);
+
+ putFile8Bit(file, (level->use_spring_bug ? 1 : 0));
+ putFile8Bit(file, (level->use_step_counter ? 1 : 0));
+
+ putFile8Bit(file, (level->instant_relocation ? 1 : 0));
+ putFile8Bit(file, (level->can_pass_to_walkable ? 1 : 0));
+ putFile8Bit(file, (level->grow_into_diggable ? 1 : 0));
+
+ putFile8Bit(file, level->game_engine_type);
+
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_HEAD_UNUSED);
+}
+#endif
+
+static int SaveLevel_NAME(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int i;
+
+ for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
+ chunk_size += putFile8Bit(file, level->name[i]);
+
+ return chunk_size;
+}
+
+static int SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int i;
+
+ for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
+ chunk_size += putFile8Bit(file, level->author[i]);
+
+ return chunk_size;
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int x, y;
+
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ if (level->encoding_16bit_field)
+ chunk_size += putFile16BitBE(file, level->field[x][y]);
+ else
+ chunk_size += putFile8Bit(file, level->field[x][y]);
+
+ return chunk_size;
+}
+#endif
+
+static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int x, y;
+
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ chunk_size += putFile16BitBE(file, level->field[x][y]);
+
+ return chunk_size;
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ putFile8Bit(file, EL_YAMYAM);
+ putFile8Bit(file, level->num_yamyam_contents);
+ putFile8Bit(file, 0);
+ putFile8Bit(file, 0);
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ if (level->encoding_16bit_field)
+ putFile16BitBE(file, level->yamyam_content[i].e[x][y]);
+ else
+ putFile8Bit(file, level->yamyam_content[i].e[x][y]);
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
+{
+ int i, x, y;
+ int num_contents, content_xsize, content_ysize;
+ int content_array[MAX_ELEMENT_CONTENTS][3][3];
+
+ if (element == EL_YAMYAM)
+ {
+ num_contents = level->num_yamyam_contents;
+ content_xsize = 3;
+ content_ysize = 3;
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ content_array[i][x][y] = level->yamyam_content[i].e[x][y];
+ }
+ else if (element == EL_BD_AMOEBA)
+ {
+ num_contents = 1;
+ content_xsize = 1;
+ content_ysize = 1;
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ content_array[i][x][y] = EL_EMPTY;
+ content_array[0][0][0] = level->amoeba_content;
+ }
+ else
+ {
+ // chunk header already written -- write empty chunk data
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_SIZE);
+
+ Warn("cannot save content for element '%d'", element);
+
+ return;
+ }
+
+ putFile16BitBE(file, element);
+ putFile8Bit(file, num_contents);
+ putFile8Bit(file, content_xsize);
+ putFile8Bit(file, content_ysize);
+
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_UNUSED);
+
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, content_array[i][x][y]);
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
+{
+ int envelope_nr = element - EL_ENVELOPE_1;
+ int envelope_len = strlen(level->envelope_text[envelope_nr]) + 1;
+ int chunk_size = 0;
+ int i;
+
+ chunk_size += putFile16BitBE(file, element);
+ chunk_size += putFile16BitBE(file, envelope_len);
+ chunk_size += putFile8Bit(file, level->envelope_xsize[envelope_nr]);
+ chunk_size += putFile8Bit(file, level->envelope_ysize[envelope_nr]);
+
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT3_UNUSED);
+ chunk_size += LEVEL_CHUNK_CNT3_UNUSED;
+
+ for (i = 0; i < envelope_len; i++)
+ chunk_size += putFile8Bit(file, level->envelope_text[envelope_nr][i]);
+
+ return chunk_size;
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
+ int num_changed_custom_elements)
+{
+ int i, check = 0;
+
+ putFile16BitBE(file, num_changed_custom_elements);
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ struct ElementInfo *ei = &element_info[element];
+
+ if (ei->properties[EP_BITFIELD_BASE_NR] != EP_BITMASK_DEFAULT)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+ putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) // should not happen
+ Warn("inconsistent number of custom element properties");
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
+ int num_changed_custom_elements)
+{
+ int i, check = 0;
+
+ putFile16BitBE(file, num_changed_custom_elements);
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (element_info[element].change->target_element != EL_EMPTY_SPACE)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+ putFile16BitBE(file, element_info[element].change->target_element);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) // should not happen
+ Warn("inconsistent number of custom target elements");
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
+ int num_changed_custom_elements)
+{
+ int i, j, x, y, check = 0;
+
+ putFile16BitBE(file, num_changed_custom_elements);
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ if (ei->modified_settings)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+
+ for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
+ putFile8Bit(file, ei->description[j]);
+
+ putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
+
+ // some free bytes for future properties and padding
+ WriteUnusedBytesToFile(file, 7);
+
+ putFile8Bit(file, ei->use_gfx_element);
+ putFile16BitBE(file, ei->gfx_element_initial);
+
+ putFile8Bit(file, ei->collect_score_initial);
+ putFile8Bit(file, ei->collect_count_initial);
+
+ putFile16BitBE(file, ei->push_delay_fixed);
+ putFile16BitBE(file, ei->push_delay_random);
+ putFile16BitBE(file, ei->move_delay_fixed);
+ putFile16BitBE(file, ei->move_delay_random);
+
+ putFile16BitBE(file, ei->move_pattern);
+ putFile8Bit(file, ei->move_direction_initial);
+ putFile8Bit(file, ei->move_stepsize);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, ei->content.e[x][y]);
+
+ putFile32BitBE(file, ei->change->events);
+
+ putFile16BitBE(file, ei->change->target_element);
+
+ putFile16BitBE(file, ei->change->delay_fixed);
+ putFile16BitBE(file, ei->change->delay_random);
+ putFile16BitBE(file, ei->change->delay_frames);
+
+ putFile16BitBE(file, ei->change->initial_trigger_element);
+
+ putFile8Bit(file, ei->change->explode);
+ putFile8Bit(file, ei->change->use_target_content);
+ putFile8Bit(file, ei->change->only_if_complete);
+ putFile8Bit(file, ei->change->use_random_replace);
+
+ putFile8Bit(file, ei->change->random_percentage);
+ putFile8Bit(file, ei->change->replace_when);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, ei->change->content.e[x][y]);
+
+ putFile8Bit(file, ei->slippery_type);
+
+ // some free bytes for future properties and padding
+ WriteUnusedBytesToFile(file, LEVEL_CPART_CUS3_UNUSED);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) // should not happen
+ Warn("inconsistent number of custom element properties");
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ int i, j, x, y;
+
+ // ---------- custom element base property values (96 bytes) ----------------
+
+ putFile16BitBE(file, element);
+
+ for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
+ putFile8Bit(file, ei->description[i]);
+
+ putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
+
+ WriteUnusedBytesToFile(file, 4); // reserved for more base properties
+
+ putFile8Bit(file, ei->num_change_pages);
+
+ putFile16BitBE(file, ei->ce_value_fixed_initial);
+ putFile16BitBE(file, ei->ce_value_random_initial);
+ putFile8Bit(file, ei->use_last_ce_value);
+
+ putFile8Bit(file, ei->use_gfx_element);
+ putFile16BitBE(file, ei->gfx_element_initial);
+
+ putFile8Bit(file, ei->collect_score_initial);
+ putFile8Bit(file, ei->collect_count_initial);
+
+ putFile8Bit(file, ei->drop_delay_fixed);
+ putFile8Bit(file, ei->push_delay_fixed);
+ putFile8Bit(file, ei->drop_delay_random);
+ putFile8Bit(file, ei->push_delay_random);
+ putFile16BitBE(file, ei->move_delay_fixed);
+ putFile16BitBE(file, ei->move_delay_random);
+
+ // bits 0 - 15 of "move_pattern" ...
+ putFile16BitBE(file, ei->move_pattern & 0xffff);
+ putFile8Bit(file, ei->move_direction_initial);
+ putFile8Bit(file, ei->move_stepsize);
+
+ putFile8Bit(file, ei->slippery_type);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, ei->content.e[x][y]);
+
+ putFile16BitBE(file, ei->move_enter_element);
+ putFile16BitBE(file, ei->move_leave_element);
+ putFile8Bit(file, ei->move_leave_type);
+
+ // ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible)
+ putFile16BitBE(file, (ei->move_pattern >> 16) & 0xffff);
+
+ putFile8Bit(file, ei->access_direction);
+
+ putFile8Bit(file, ei->explosion_delay);
+ putFile8Bit(file, ei->ignition_delay);
+ putFile8Bit(file, ei->explosion_type);
+
+ // some free bytes for future custom property values and padding
+ WriteUnusedBytesToFile(file, 1);
+
+ // ---------- change page property values (48 bytes) ------------------------
+
+ for (i = 0; i < ei->num_change_pages; i++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[i];
+ unsigned int event_bits;
+
+ // bits 0 - 31 of "has_event[]" ...
+ event_bits = 0;
+ for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
+ if (change->has_event[j])
+ event_bits |= (1u << j);
+ putFile32BitBE(file, event_bits);
+
+ putFile16BitBE(file, change->target_element);
+
+ putFile16BitBE(file, change->delay_fixed);
+ putFile16BitBE(file, change->delay_random);
+ putFile16BitBE(file, change->delay_frames);
+
+ putFile16BitBE(file, change->initial_trigger_element);
+
+ putFile8Bit(file, change->explode);
+ putFile8Bit(file, change->use_target_content);
+ putFile8Bit(file, change->only_if_complete);
+ putFile8Bit(file, change->use_random_replace);
+
+ putFile8Bit(file, change->random_percentage);
+ putFile8Bit(file, change->replace_when);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, change->target_content.e[x][y]);
+
+ putFile8Bit(file, change->can_change);
+
+ putFile8Bit(file, change->trigger_side);
+
+ putFile8Bit(file, change->trigger_player);
+ putFile8Bit(file, (change->trigger_page == CH_PAGE_ANY ? CH_PAGE_ANY_FILE :
+ log_2(change->trigger_page)));
+
+ putFile8Bit(file, change->has_action);
+ putFile8Bit(file, change->action_type);
+ putFile8Bit(file, change->action_mode);
+ putFile16BitBE(file, change->action_arg);
+
+ // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible)
+ event_bits = 0;
+ for (j = 32; j < NUM_CHANGE_EVENTS; j++)
+ if (change->has_event[j])
+ event_bits |= (1u << (j - 32));
+ putFile8Bit(file, event_bits);
+ }
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ struct ElementGroupInfo *group = ei->group;
+ int i;
+
+ putFile16BitBE(file, element);
+
+ for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
+ putFile8Bit(file, ei->description[i]);
+
+ putFile8Bit(file, group->num_elements);
+
+ putFile8Bit(file, ei->use_gfx_element);
+ putFile16BitBE(file, ei->gfx_element_initial);
+
+ putFile8Bit(file, group->choice_mode);
+
+ // some free bytes for future values and padding
+ WriteUnusedBytesToFile(file, 3);
+
+ for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
+ putFile16BitBE(file, group->element[i]);
+}
+#endif
+
+static int SaveLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *entry,
+ boolean write_element)
+{
+ int save_type = entry->save_type;
+ int data_type = entry->data_type;
+ int conf_type = entry->conf_type;
+ int byte_mask = conf_type & CONF_MASK_BYTES;
+ int element = entry->element;
+ int default_value = entry->default_value;
+ int num_bytes = 0;
+ boolean modified = FALSE;
+
+ if (byte_mask != CONF_MASK_MULTI_BYTES)
+ {
+ void *value_ptr = entry->value;
+ int value = (data_type == TYPE_BOOLEAN ? *(boolean *)value_ptr :
+ *(int *)value_ptr);
+
+ // check if any settings have been modified before saving them
+ if (value != default_value)
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += (byte_mask == CONF_MASK_1_BYTE ? putFile8Bit (file, value) :
+ byte_mask == CONF_MASK_2_BYTE ? putFile16BitBE(file, value) :
+ byte_mask == CONF_MASK_4_BYTE ? putFile32BitBE(file, value) :
+ 0);
+ }
+ else if (data_type == TYPE_STRING)
+ {
+ char *default_string = entry->default_string;
+ char *string = (char *)(entry->value);
+ int string_length = strlen(string);
+ int i;
+
+ // check if any settings have been modified before saving them
+ if (!strEqual(string, default_string))
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += putFile16BitBE(file, string_length);
+
+ for (i = 0; i < string_length; i++)
+ num_bytes += putFile8Bit(file, string[i]);
+ }
+ else if (data_type == TYPE_ELEMENT_LIST)
+ {
+ int *element_array = (int *)(entry->value);
+ int num_elements = *(int *)(entry->num_entities);
+ int i;
+
+ // check if any settings have been modified before saving them
+ for (i = 0; i < num_elements; i++)
+ if (element_array[i] != default_value)
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += putFile16BitBE(file, num_elements * CONF_ELEMENT_NUM_BYTES);
+
+ for (i = 0; i < num_elements; i++)
+ num_bytes += putFile16BitBE(file, element_array[i]);
+ }
+ else if (data_type == TYPE_CONTENT_LIST)
+ {
+ struct Content *content = (struct Content *)(entry->value);
+ int num_contents = *(int *)(entry->num_entities);
+ int i, x, y;
+
+ // check if any settings have been modified before saving them
+ for (i = 0; i < num_contents; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ if (content[i].e[x][y] != default_value)
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += putFile16BitBE(file, num_contents * CONF_CONTENT_NUM_BYTES);
+
+ for (i = 0; i < num_contents; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ num_bytes += putFile16BitBE(file, content[i].e[x][y]);
+ }
+
+ return num_bytes;
+}
+
+static int SaveLevel_INFO(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int i;
+
+ li = *level; // copy level data into temporary buffer
+
+ for (i = 0; chunk_config_INFO[i].data_type != -1; i++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_INFO[i], FALSE);
+
+ return chunk_size;
+}
+
+static int SaveLevel_ELEM(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int i;
+
+ li = *level; // copy level data into temporary buffer
+
+ for (i = 0; chunk_config_ELEM[i].data_type != -1; i++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_ELEM[i], TRUE);
+
+ return chunk_size;
+}
+
+static int SaveLevel_NOTE(FILE *file, struct LevelInfo *level, int element)
+{
+ int envelope_nr = element - EL_ENVELOPE_1;
+ int chunk_size = 0;
+ int i;
+
+ chunk_size += putFile16BitBE(file, element);
+
+ // copy envelope data into temporary buffer
+ xx_envelope = level->envelope[envelope_nr];
+
+ for (i = 0; chunk_config_NOTE[i].data_type != -1; i++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_NOTE[i], FALSE);
+
+ return chunk_size;
+}
+
+static int SaveLevel_CUSX(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ int chunk_size = 0;
+ int i, j;
+
+ chunk_size += putFile16BitBE(file, element);
+
+ xx_ei = *ei; // copy element data into temporary buffer
+
+ // set default description string for this specific element
+ strcpy(xx_default_description, getDefaultElementDescription(ei));
+
+ for (i = 0; chunk_config_CUSX_base[i].data_type != -1; i++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_base[i], FALSE);
+
+ for (i = 0; i < ei->num_change_pages; i++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[i];
+
+ xx_current_change_page = i;
+
+ xx_change = *change; // copy change data into temporary buffer
+
+ resetEventBits();
+ setEventBitsFromEventFlags(change);
+
+ for (j = 0; chunk_config_CUSX_change[j].data_type != -1; j++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_CUSX_change[j],
+ FALSE);
+ }
+
+ return chunk_size;
+}
+
+static int SaveLevel_GRPX(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ struct ElementGroupInfo *group = ei->group;
+ int chunk_size = 0;
+ int i;
+
+ chunk_size += putFile16BitBE(file, element);
+
+ xx_ei = *ei; // copy element data into temporary buffer
+ xx_group = *group; // copy group data into temporary buffer
+
+ // set default description string for this specific element
+ strcpy(xx_default_description, getDefaultElementDescription(ei));
+
+ for (i = 0; chunk_config_GRPX[i].data_type != -1; i++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_GRPX[i], FALSE);
+
+ return chunk_size;
+}
+
+static int SaveLevel_EMPX(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ int chunk_size = 0;
+ int i;
+
+ chunk_size += putFile16BitBE(file, element);
+
+ xx_ei = *ei; // copy element data into temporary buffer
+
+ for (i = 0; chunk_config_EMPX[i].data_type != -1; i++)
+ chunk_size += SaveLevel_MicroChunk(file, &chunk_config_EMPX[i], FALSE);
+
+ return chunk_size;
+}
+
+static void SaveLevelFromFilename(struct LevelInfo *level, char *filename,
+ boolean save_as_template)
+{
+ int chunk_size;
+ int i;
+ FILE *file;
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Warn("cannot save level file '%s'", filename);
+
+ return;
+ }
+
+ level->file_version = FILE_VERSION_ACTUAL;
+ level->game_version = GAME_VERSION_ACTUAL;
+
+ level->creation_date = getCurrentDate();
+
+ putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
+ putFileChunkBE(file, "CAVE", CHUNK_SIZE_NONE);
+
+ chunk_size = SaveLevel_VERS(NULL, level);
+ putFileChunkBE(file, "VERS", chunk_size);
+ SaveLevel_VERS(file, level);
+
+ chunk_size = SaveLevel_DATE(NULL, level);
+ putFileChunkBE(file, "DATE", chunk_size);
+ SaveLevel_DATE(file, level);
+
+ chunk_size = SaveLevel_NAME(NULL, level);
+ putFileChunkBE(file, "NAME", chunk_size);
+ SaveLevel_NAME(file, level);
+
+ chunk_size = SaveLevel_AUTH(NULL, level);
+ putFileChunkBE(file, "AUTH", chunk_size);
+ SaveLevel_AUTH(file, level);
+
+ chunk_size = SaveLevel_INFO(NULL, level);
+ putFileChunkBE(file, "INFO", chunk_size);
+ SaveLevel_INFO(file, level);
+
+ chunk_size = SaveLevel_BODY(NULL, level);
+ putFileChunkBE(file, "BODY", chunk_size);
+ SaveLevel_BODY(file, level);
+
+ chunk_size = SaveLevel_ELEM(NULL, level);
+ if (chunk_size > LEVEL_CHUNK_ELEM_UNCHANGED) // save if changed
+ {
+ putFileChunkBE(file, "ELEM", chunk_size);
+ SaveLevel_ELEM(file, level);
+ }
+
+ for (i = 0; i < NUM_ENVELOPES; i++)
+ {
+ int element = EL_ENVELOPE_1 + i;
+
+ chunk_size = SaveLevel_NOTE(NULL, level, element);
+ if (chunk_size > LEVEL_CHUNK_NOTE_UNCHANGED) // save if changed
+ {
+ putFileChunkBE(file, "NOTE", chunk_size);
+ SaveLevel_NOTE(file, level, element);
+ }
+ }
+
+ // if not using template level, check for non-default custom/group elements
+ if (!level->use_custom_template || save_as_template)
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ chunk_size = SaveLevel_CUSX(NULL, level, element);
+ if (chunk_size > LEVEL_CHUNK_CUSX_UNCHANGED) // save if changed
+ {
+ putFileChunkBE(file, "CUSX", chunk_size);
+ SaveLevel_CUSX(file, level, element);
+ }
+ }
+
+ for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
+ {
+ int element = EL_GROUP_START + i;
+
+ chunk_size = SaveLevel_GRPX(NULL, level, element);
+ if (chunk_size > LEVEL_CHUNK_GRPX_UNCHANGED) // save if changed
+ {
+ putFileChunkBE(file, "GRPX", chunk_size);
+ SaveLevel_GRPX(file, level, element);
+ }
+ }
+
+ for (i = 0; i < NUM_EMPTY_ELEMENTS_ALL; i++)
+ {
+ int element = GET_EMPTY_ELEMENT(i);
+
+ chunk_size = SaveLevel_EMPX(NULL, level, element);
+ if (chunk_size > LEVEL_CHUNK_EMPX_UNCHANGED) // save if changed
+ {
+ putFileChunkBE(file, "EMPX", chunk_size);
+ SaveLevel_EMPX(file, level, element);
+ }
+ }
+ }
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+}
+
+void SaveLevel(int nr)
+{
+ char *filename = getDefaultLevelFilename(nr);
+
+ SaveLevelFromFilename(&level, filename, FALSE);
+}
+
+void SaveLevelTemplate(void)
+{
+ char *filename = getLocalLevelTemplateFilename();
+
+ SaveLevelFromFilename(&level, filename, TRUE);
+}
+
+boolean SaveLevelChecked(int nr)
+{
+ char *filename = getDefaultLevelFilename(nr);
+ boolean new_level = !fileExists(filename);
+ boolean level_saved = FALSE;
+
+ if (new_level || Request("Save this level and kill the old?", REQ_ASK))
+ {
+ SaveLevel(nr);
+
+ if (new_level)
+ Request("Level saved!", REQ_CONFIRM);
+
+ level_saved = TRUE;
+ }
+
+ return level_saved;
+}
+
+void DumpLevel(struct LevelInfo *level)
+{
+ if (level->no_level_file || level->no_valid_file)
+ {
+ Warn("cannot dump -- no valid level file found");
+
+ return;
+ }
+
+ PrintLine("-", 79);
+ Print("Level xxx (file version %08d, game version %08d)\n",
+ level->file_version, level->game_version);
+ PrintLine("-", 79);
+
+ Print("Level author: '%s'\n", level->author);
+ Print("Level title: '%s'\n", level->name);
+ Print("\n");
+ Print("Playfield size: %d x %d\n", level->fieldx, level->fieldy);
+ Print("\n");
+ Print("Level time: %d seconds\n", level->time);
+ Print("Gems needed: %d\n", level->gems_needed);
+ Print("\n");
+ Print("Time for magic wall: %d seconds\n", level->time_magic_wall);
+ Print("Time for wheel: %d seconds\n", level->time_wheel);
+ Print("Time for light: %d seconds\n", level->time_light);
+ Print("Time for timegate: %d seconds\n", level->time_timegate);
+ Print("\n");
+ Print("Amoeba speed: %d\n", level->amoeba_speed);
+ Print("\n");
+
+ Print("EM style slippery gems: %s\n", (level->em_slippery_gems ? "yes" : "no"));
+ Print("Player blocks last field: %s\n", (level->block_last_field ? "yes" : "no"));
+ Print("SP player blocks last field: %s\n", (level->sp_block_last_field ? "yes" : "no"));
+ Print("use spring bug: %s\n", (level->use_spring_bug ? "yes" : "no"));
+ Print("use step counter: %s\n", (level->use_step_counter ? "yes" : "no"));
+ Print("rate time over score: %s\n", (level->rate_time_over_score ? "yes" : "no"));
+
+ if (options.debug)
+ {
+ int i, j;
+
+ for (i = 0; i < NUM_ENVELOPES; i++)
+ {
+ char *text = level->envelope[i].text;
+ int text_len = strlen(text);
+ boolean has_text = FALSE;
+
+ for (j = 0; j < text_len; j++)
+ if (text[j] != ' ' && text[j] != '\n')
+ has_text = TRUE;
+
+ if (has_text)
+ {
+ Print("\n");
+ Print("Envelope %d:\n'%s'\n", i + 1, text);
+ }
+ }
+ }
+
+ PrintLine("-", 79);
+}
+
+void DumpLevels(void)
+{
+ static LevelDirTree *dumplevel_leveldir = NULL;
+
+ dumplevel_leveldir = getTreeInfoFromIdentifier(leveldir_first,
+ global.dumplevel_leveldir);
+
+ if (dumplevel_leveldir == NULL)
+ Fail("no such level identifier: '%s'", global.dumplevel_leveldir);
+
+ if (global.dumplevel_level_nr < dumplevel_leveldir->first_level ||
+ global.dumplevel_level_nr > dumplevel_leveldir->last_level)
+ Fail("no such level number: %d", global.dumplevel_level_nr);
+
+ leveldir_current = dumplevel_leveldir;
+
+ LoadLevel(global.dumplevel_level_nr);
+ DumpLevel(&level);
+
+ CloseAllAndExit(0);
+}
+
+
+// ============================================================================
+// tape file functions
+// ============================================================================
+
+static void setTapeInfoToDefaults(void)
+{
+ int i;
+
+ // always start with reliable default values (empty tape)
+ TapeErase();
+
+ // default values (also for pre-1.2 tapes) with only the first player
+ tape.player_participates[0] = TRUE;
+ for (i = 1; i < MAX_PLAYERS; i++)
+ tape.player_participates[i] = FALSE;
+
+ // at least one (default: the first) player participates in every tape
+ tape.num_participating_players = 1;
+
+ tape.property_bits = TAPE_PROPERTY_NONE;
+
+ tape.level_nr = level_nr;
+ tape.counter = 0;
+ tape.changed = FALSE;
+ tape.solved = FALSE;
+
+ tape.recording = FALSE;
+ tape.playing = FALSE;
+ tape.pausing = FALSE;
+
+ tape.scr_fieldx = SCR_FIELDX_DEFAULT;
+ tape.scr_fieldy = SCR_FIELDY_DEFAULT;
+
+ tape.no_info_chunk = TRUE;
+ tape.no_valid_file = FALSE;
+}
+
+static int getTapePosSize(struct TapeInfo *tape)
+{
+ int tape_pos_size = 0;
+
+ if (tape->use_key_actions)
+ tape_pos_size += tape->num_participating_players;
+
+ if (tape->use_mouse_actions)
+ tape_pos_size += 3; // x and y position and mouse button mask
+
+ tape_pos_size += 1; // tape action delay value
+
+ return tape_pos_size;
+}
+
+static void setTapeActionFlags(struct TapeInfo *tape, int value)
+{
+ tape->use_key_actions = FALSE;
+ tape->use_mouse_actions = FALSE;
+
+ if (value != TAPE_USE_MOUSE_ACTIONS_ONLY)
+ tape->use_key_actions = TRUE;
+
+ if (value != TAPE_USE_KEY_ACTIONS_ONLY)
+ tape->use_mouse_actions = TRUE;
+}
+
+static int getTapeActionValue(struct TapeInfo *tape)
+{
+ return (tape->use_key_actions &&
+ tape->use_mouse_actions ? TAPE_USE_KEY_AND_MOUSE_ACTIONS :
+ tape->use_key_actions ? TAPE_USE_KEY_ACTIONS_ONLY :
+ tape->use_mouse_actions ? TAPE_USE_MOUSE_ACTIONS_ONLY :
+ TAPE_ACTIONS_DEFAULT);
+}
+
+static int LoadTape_VERS(File *file, int chunk_size, struct TapeInfo *tape)
+{
+ tape->file_version = getFileVersion(file);
+ tape->game_version = getFileVersion(file);
+
+ return chunk_size;
+}
+
+static int LoadTape_HEAD(File *file, int chunk_size, struct TapeInfo *tape)
+{
+ int i;
+
+ tape->random_seed = getFile32BitBE(file);
+ tape->date = getFile32BitBE(file);
+ tape->length = getFile32BitBE(file);
+
+ // read header fields that are new since version 1.2
+ if (tape->file_version >= FILE_VERSION_1_2)
+ {
+ byte store_participating_players = getFile8Bit(file);
+ int engine_version;
+
+ // since version 1.2, tapes store which players participate in the tape
+ tape->num_participating_players = 0;
+ for (i = 0; i < MAX_PLAYERS; i++)
+ {
+ tape->player_participates[i] = FALSE;
+
+ if (store_participating_players & (1 << i))
+ {
+ tape->player_participates[i] = TRUE;
+ tape->num_participating_players++;
+ }
+ }
+
+ setTapeActionFlags(tape, getFile8Bit(file));
+
+ tape->property_bits = getFile8Bit(file);
+ tape->solved = getFile8Bit(file);
+
+ engine_version = getFileVersion(file);
+ if (engine_version > 0)
+ tape->engine_version = engine_version;
+ else
+ tape->engine_version = tape->game_version;
+ }
+
+ return chunk_size;
+}
+
+static int LoadTape_SCRN(File *file, int chunk_size, struct TapeInfo *tape)
+{
+ tape->scr_fieldx = getFile8Bit(file);
+ tape->scr_fieldy = getFile8Bit(file);
+
+ return chunk_size;
+}
+
+static int LoadTape_INFO(File *file, int chunk_size, struct TapeInfo *tape)
+{
+ char *level_identifier = NULL;
+ int level_identifier_size;
+ int i;
+
+ tape->no_info_chunk = FALSE;
+
+ level_identifier_size = getFile16BitBE(file);
+
+ level_identifier = checked_malloc(level_identifier_size);
+
+ for (i = 0; i < level_identifier_size; i++)
+ level_identifier[i] = getFile8Bit(file);
+
+ strncpy(tape->level_identifier, level_identifier, MAX_FILENAME_LEN);
+ tape->level_identifier[MAX_FILENAME_LEN] = '\0';
+
+ checked_free(level_identifier);
+
+ tape->level_nr = getFile16BitBE(file);
+
+ chunk_size = 2 + level_identifier_size + 2;
+
+ return chunk_size;
+}
+
+static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape)
+{
+ int i, j;
+ int tape_pos_size = getTapePosSize(tape);
+ int chunk_size_expected = tape_pos_size * tape->length;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size);
+ return chunk_size_expected;
+ }
+
+ for (i = 0; i < tape->length; i++)
+ {
+ if (i >= MAX_TAPE_LEN)
+ {
+ Warn("tape truncated -- size exceeds maximum tape size %d",
+ MAX_TAPE_LEN);
+
+ // tape too large; read and ignore remaining tape data from this chunk
+ for (;i < tape->length; i++)
+ ReadUnusedBytesFromFile(file, tape_pos_size);
+
+ break;
+ }
+
+ if (tape->use_key_actions)
+ {
+ for (j = 0; j < MAX_PLAYERS; j++)
+ {
+ tape->pos[i].action[j] = MV_NONE;
+
+ if (tape->player_participates[j])
+ tape->pos[i].action[j] = getFile8Bit(file);
+ }
+ }
+
+ if (tape->use_mouse_actions)
+ {
+ tape->pos[i].action[TAPE_ACTION_LX] = getFile8Bit(file);
+ tape->pos[i].action[TAPE_ACTION_LY] = getFile8Bit(file);
+ tape->pos[i].action[TAPE_ACTION_BUTTON] = getFile8Bit(file);
+ }
+
+ tape->pos[i].delay = getFile8Bit(file);
+
+ if (tape->file_version == FILE_VERSION_1_0)
+ {
+ // eliminate possible diagonal moves in old tapes
+ // this is only for backward compatibility
+
+ byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
+ byte action = tape->pos[i].action[0];
+ int k, num_moves = 0;
+
+ for (k = 0; k<4; k++)
+ {
+ if (action & joy_dir[k])
+ {
+ tape->pos[i + num_moves].action[0] = joy_dir[k];
+ if (num_moves > 0)
+ tape->pos[i + num_moves].delay = 0;
+ num_moves++;
+ }
+ }
+
+ if (num_moves > 1)
+ {
+ num_moves--;
+ i += num_moves;
+ tape->length += num_moves;
+ }
+ }
+ else if (tape->file_version < FILE_VERSION_2_0)
+ {
+ // convert pre-2.0 tapes to new tape format
+
+ if (tape->pos[i].delay > 1)
+ {
+ // action part
+ tape->pos[i + 1] = tape->pos[i];
+ tape->pos[i + 1].delay = 1;
+
+ // delay part
+ for (j = 0; j < MAX_PLAYERS; j++)
+ tape->pos[i].action[j] = MV_NONE;
+ tape->pos[i].delay--;
+
+ i++;
+ tape->length++;
+ }
+ }
+
+ if (checkEndOfFile(file))
+ break;
+ }
+
+ if (i != tape->length)
+ chunk_size = tape_pos_size * i;
+
+ return chunk_size;
+}
+
+static void LoadTape_SokobanSolution(char *filename)
+{
+ File *file;
+ int move_delay = TILESIZE / level.initial_player_stepsize[0];
+
+ if (!(file = openFile(filename, MODE_READ)))
+ {
+ tape.no_valid_file = TRUE;
+
+ return;
+ }
+
+ while (!checkEndOfFile(file))
+ {
+ unsigned char c = getByteFromFile(file);
+
+ if (checkEndOfFile(file))
+ break;
+
+ switch (c)
+ {
+ case 'u':
+ case 'U':
+ tape.pos[tape.length].action[0] = MV_UP;
+ tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
+ tape.length++;
+ break;
+
+ case 'd':
+ case 'D':
+ tape.pos[tape.length].action[0] = MV_DOWN;
+ tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
+ tape.length++;
+ break;
+
+ case 'l':
+ case 'L':
+ tape.pos[tape.length].action[0] = MV_LEFT;
+ tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
+ tape.length++;
+ break;
+
+ case 'r':
+ case 'R':
+ tape.pos[tape.length].action[0] = MV_RIGHT;
+ tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0);
+ tape.length++;
+ break;
+
+ case '\n':
+ case '\r':
+ case '\t':
+ case ' ':
+ // ignore white-space characters
+ break;
+
+ default:
+ tape.no_valid_file = TRUE;
+
+ Warn("unsupported Sokoban solution file '%s' ['%d']", filename, c);
+
+ break;
+ }
+ }
+
+ closeFile(file);
+
+ if (tape.no_valid_file)
+ return;
+
+ tape.length_frames = GetTapeLengthFrames();
+ tape.length_seconds = GetTapeLengthSeconds();
+}
+
+void LoadTapeFromFilename(char *filename)
+{
+ char cookie[MAX_LINE_LEN];
+ char chunk_name[CHUNK_ID_LEN + 1];
+ File *file;
+ int chunk_size;
+
+ // always start with reliable default values
+ setTapeInfoToDefaults();
+
+ if (strSuffix(filename, ".sln"))
+ {
+ LoadTape_SokobanSolution(filename);
+
+ return;
+ }
+
+ if (!(file = openFile(filename, MODE_READ)))
+ {
+ tape.no_valid_file = TRUE;
+
+ return;
+ }
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (strEqual(chunk_name, "RND1"))
+ {
+ getFile32BitBE(file); // not used
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (!strEqual(chunk_name, "TAPE"))
+ {
+ tape.no_valid_file = TRUE;
+
+ Warn("unknown format of tape file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+ }
+ else // check for pre-2.0 file format with cookie string
+ {
+ strcpy(cookie, chunk_name);
+ if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
+ cookie[4] = '\0';
+ if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
+ cookie[strlen(cookie) - 1] = '\0';
+
+ if (!checkCookieString(cookie, TAPE_COOKIE_TMPL))
+ {
+ tape.no_valid_file = TRUE;
+
+ Warn("unknown format of tape file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+
+ if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1)
+ {
+ tape.no_valid_file = TRUE;
+
+ Warn("unsupported version of tape file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+
+ // pre-2.0 tape files have no game version, so use file version here
+ tape.game_version = tape.file_version;
+ }
+
+ if (tape.file_version < FILE_VERSION_1_2)
+ {
+ // tape files from versions before 1.2.0 without chunk structure
+ LoadTape_HEAD(file, TAPE_CHUNK_HEAD_SIZE, &tape);
+ LoadTape_BODY(file, 2 * tape.length, &tape);
+ }
+ else
+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(File *, int, struct TapeInfo *);
+ }
+ chunk_info[] =
+ {
+ { "VERS", TAPE_CHUNK_VERS_SIZE, LoadTape_VERS },
+ { "HEAD", TAPE_CHUNK_HEAD_SIZE, LoadTape_HEAD },
+ { "SCRN", TAPE_CHUNK_SCRN_SIZE, LoadTape_SCRN },
+ { "INFO", -1, LoadTape_INFO },
+ { "BODY", -1, LoadTape_BODY },
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunkBE(file, chunk_name, &chunk_size))
+ {
+ int i = 0;
+
+ while (chunk_info[i].name != NULL &&
+ !strEqual(chunk_name, chunk_info[i].name))
+ i++;
+
+ if (chunk_info[i].name == NULL)
+ {
+ Warn("unknown chunk '%s' in tape file '%s'",
+ chunk_name, filename);
+
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else if (chunk_info[i].size != -1 &&
+ chunk_info[i].size != chunk_size)
+ {
+ Warn("wrong size (%d) of chunk '%s' in tape file '%s'",
+ chunk_size, chunk_name, filename);
+
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else
+ {
+ // call function to load this tape chunk
+ int chunk_size_expected =
+ (chunk_info[i].loader)(file, chunk_size, &tape);
+
+ // the size of some chunks cannot be checked before reading other
+ // chunks first (like "HEAD" and "BODY") that contain some header
+ // information, so check them here
+ if (chunk_size_expected != chunk_size)
+ {
+ Warn("wrong size (%d) of chunk '%s' in tape file '%s'",
+ chunk_size, chunk_name, filename);
+ }
+ }
+ }
+ }
+
+ closeFile(file);
+
+ tape.length_frames = GetTapeLengthFrames();
+ tape.length_seconds = GetTapeLengthSeconds();
+
+#if 0
+ Debug("files:LoadTapeFromFilename", "tape file version: %d",
+ tape.file_version);
+ Debug("files:LoadTapeFromFilename", "tape game version: %d",
+ tape.game_version);
+ Debug("files:LoadTapeFromFilename", "tape engine version: %d",
+ tape.engine_version);
+#endif
+}
+
+void LoadTape(int nr)
+{
+ char *filename = getTapeFilename(nr);
+
+ LoadTapeFromFilename(filename);
+}
+
+void LoadSolutionTape(int nr)
+{
+ char *filename = getSolutionTapeFilename(nr);
+
+ LoadTapeFromFilename(filename);
+
+ if (TAPE_IS_EMPTY(tape) &&
+ level.game_engine_type == GAME_ENGINE_TYPE_SP &&
+ level.native_sp_level->demo.is_available)
+ CopyNativeTape_SP_to_RND(&level);
+}
+
+void LoadScoreTape(char *score_tape_basename, int nr)
+{
+ char *filename = getScoreTapeFilename(score_tape_basename, nr);
+
+ LoadTapeFromFilename(filename);
+}
+
+void LoadScoreCacheTape(char *score_tape_basename, int nr)
+{
+ char *filename = getScoreCacheTapeFilename(score_tape_basename, nr);
+
+ LoadTapeFromFilename(filename);
+}
+
+static boolean checkSaveTape_SCRN(struct TapeInfo *tape)
+{
+ // chunk required for team mode tapes with non-default screen size
+ return (tape->num_participating_players > 1 &&
+ (tape->scr_fieldx != SCR_FIELDX_DEFAULT ||
+ tape->scr_fieldy != SCR_FIELDY_DEFAULT));
+}
+
+static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
+{
+ putFileVersion(file, tape->file_version);
+ putFileVersion(file, tape->game_version);
+}
+
+static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
+{
+ int i;
+ byte store_participating_players = 0;
+
+ // set bits for participating players for compact storage
+ for (i = 0; i < MAX_PLAYERS; i++)
+ if (tape->player_participates[i])
+ store_participating_players |= (1 << i);
+
+ putFile32BitBE(file, tape->random_seed);
+ putFile32BitBE(file, tape->date);
+ putFile32BitBE(file, tape->length);
+
+ putFile8Bit(file, store_participating_players);
+
+ putFile8Bit(file, getTapeActionValue(tape));
+
+ putFile8Bit(file, tape->property_bits);
+ putFile8Bit(file, tape->solved);
+
+ putFileVersion(file, tape->engine_version);
+}
+
+static void SaveTape_SCRN(FILE *file, struct TapeInfo *tape)
+{
+ putFile8Bit(file, tape->scr_fieldx);
+ putFile8Bit(file, tape->scr_fieldy);
+}
+
+static void SaveTape_INFO(FILE *file, struct TapeInfo *tape)
+{
+ int level_identifier_size = strlen(tape->level_identifier) + 1;
+ int i;
+
+ putFile16BitBE(file, level_identifier_size);
+
+ for (i = 0; i < level_identifier_size; i++)
+ putFile8Bit(file, tape->level_identifier[i]);
+
+ putFile16BitBE(file, tape->level_nr);
+}
+
+static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
+{
+ int i, j;
+
+ for (i = 0; i < tape->length; i++)
+ {
+ if (tape->use_key_actions)
+ {
+ for (j = 0; j < MAX_PLAYERS; j++)
+ if (tape->player_participates[j])
+ putFile8Bit(file, tape->pos[i].action[j]);
+ }
+
+ if (tape->use_mouse_actions)
+ {
+ putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LX]);
+ putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LY]);
+ putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_BUTTON]);
+ }
+
+ putFile8Bit(file, tape->pos[i].delay);
+ }
+}
+
+void SaveTapeToFilename(char *filename)
+{
+ FILE *file;
+ int tape_pos_size;
+ int info_chunk_size;
+ int body_chunk_size;
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Warn("cannot save level recording file '%s'", filename);
+
+ return;
+ }
+
+ tape_pos_size = getTapePosSize(&tape);
+
+ info_chunk_size = 2 + (strlen(tape.level_identifier) + 1) + 2;
+ body_chunk_size = tape_pos_size * tape.length;
+
+ putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
+ putFileChunkBE(file, "TAPE", CHUNK_SIZE_NONE);
+
+ putFileChunkBE(file, "VERS", TAPE_CHUNK_VERS_SIZE);
+ SaveTape_VERS(file, &tape);
+
+ putFileChunkBE(file, "HEAD", TAPE_CHUNK_HEAD_SIZE);
+ SaveTape_HEAD(file, &tape);
+
+ if (checkSaveTape_SCRN(&tape))
+ {
+ putFileChunkBE(file, "SCRN", TAPE_CHUNK_SCRN_SIZE);
+ SaveTape_SCRN(file, &tape);
+ }
+
+ putFileChunkBE(file, "INFO", info_chunk_size);
+ SaveTape_INFO(file, &tape);
+
+ putFileChunkBE(file, "BODY", body_chunk_size);
+ SaveTape_BODY(file, &tape);
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+}
+
+static void SaveTapeExt(char *filename)
+{
+ int i;
+
+ tape.file_version = FILE_VERSION_ACTUAL;
+ tape.game_version = GAME_VERSION_ACTUAL;
+
+ tape.num_participating_players = 0;
+
+ // count number of participating players
+ for (i = 0; i < MAX_PLAYERS; i++)
+ if (tape.player_participates[i])
+ tape.num_participating_players++;
+
+ SaveTapeToFilename(filename);
+
+ tape.changed = FALSE;
+}
+
+void SaveTape(int nr)
+{
+ char *filename = getTapeFilename(nr);
+
+ InitTapeDirectory(leveldir_current->subdir);
+
+ SaveTapeExt(filename);
+}
+
+void SaveScoreTape(int nr)
+{
+ char *filename = getScoreTapeFilename(tape.score_tape_basename, nr);
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ InitScoreTapeDirectory(levelset.identifier, nr);
+
+ SaveTapeExt(filename);
+}
+
+static boolean SaveTapeCheckedExt(int nr, char *msg_replace, char *msg_saved,
+ unsigned int req_state_added)
+{
+ char *filename = getTapeFilename(nr);
+ boolean new_tape = !fileExists(filename);
+ boolean tape_saved = FALSE;
+
+ if (new_tape || Request(msg_replace, REQ_ASK | req_state_added))
+ {
+ SaveTape(nr);
+
+ if (new_tape)
+ Request(msg_saved, REQ_CONFIRM | req_state_added);
+
+ tape_saved = TRUE;
+ }
+
+ return tape_saved;
+}
+
+boolean SaveTapeChecked(int nr)
+{
+ return SaveTapeCheckedExt(nr, "Replace old tape?", "Tape saved!", 0);
+}
+
+boolean SaveTapeChecked_LevelSolved(int nr)
+{
+ return SaveTapeCheckedExt(nr, "Level solved! Replace old tape?",
+ "Level solved! Tape saved!", REQ_STAY_OPEN);
+}
+
+void DumpTape(struct TapeInfo *tape)
+{
+ int tape_frame_counter;
+ int i, j;
+
+ if (tape->no_valid_file)
+ {
+ Warn("cannot dump -- no valid tape file found");
+
+ return;
+ }
+
+ PrintLine("-", 79);
+
+ Print("Tape of Level %03d (file version %08d, game version %08d)\n",
+ tape->level_nr, tape->file_version, tape->game_version);
+ Print(" (effective engine version %08d)\n",
+ tape->engine_version);
+ Print("Level series identifier: '%s'\n", tape->level_identifier);
+
+ Print("Solution tape: %s\n",
+ tape->solved ? "yes" :
+ tape->game_version < VERSION_IDENT(4,3,2,3) ? "unknown" : "no");
+
+ Print("Special tape properties: ");
+ if (tape->property_bits == TAPE_PROPERTY_NONE)
+ Print("[none]");
+ if (tape->property_bits & TAPE_PROPERTY_EM_RANDOM_BUG)
+ Print("[em_random_bug]");
+ if (tape->property_bits & TAPE_PROPERTY_GAME_SPEED)
+ Print("[game_speed]");
+ if (tape->property_bits & TAPE_PROPERTY_PAUSE_MODE)
+ Print("[pause]");
+ if (tape->property_bits & TAPE_PROPERTY_SINGLE_STEP)
+ Print("[single_step]");
+ if (tape->property_bits & TAPE_PROPERTY_SNAPSHOT)
+ Print("[snapshot]");
+ if (tape->property_bits & TAPE_PROPERTY_REPLAYED)
+ Print("[replayed]");
+ if (tape->property_bits & TAPE_PROPERTY_TAS_KEYS)
+ Print("[tas_keys]");
+ if (tape->property_bits & TAPE_PROPERTY_SMALL_GRAPHICS)
+ Print("[small_graphics]");
+ Print("\n");
+
+ int year2 = tape->date / 10000;
+ int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
+ int month_index_raw = (tape->date / 100) % 100;
+ int month_index = month_index_raw % 12; // prevent invalid index
+ int month = month_index + 1;
+ int day = tape->date % 100;
+
+ Print("Tape date: %04d-%02d-%02d\n", year4, month, day);
+
+ PrintLine("-", 79);
+
+ tape_frame_counter = 0;
+
+ for (i = 0; i < tape->length; i++)
+ {
+ if (i >= MAX_TAPE_LEN)
+ break;
+
+ Print("%04d: ", i);
+
+ for (j = 0; j < MAX_PLAYERS; j++)
+ {
+ if (tape->player_participates[j])
+ {
+ int action = tape->pos[i].action[j];
+
+ Print("%d:%02x ", j, action);
+ Print("[%c%c%c%c|%c%c] - ",
+ (action & JOY_LEFT ? '<' : ' '),
+ (action & JOY_RIGHT ? '>' : ' '),
+ (action & JOY_UP ? '^' : ' '),
+ (action & JOY_DOWN ? 'v' : ' '),
+ (action & JOY_BUTTON_1 ? '1' : ' '),
+ (action & JOY_BUTTON_2 ? '2' : ' '));
+ }
+ }
+
+ Print("(%03d) ", tape->pos[i].delay);
+ Print("[%05d]\n", tape_frame_counter);
+
+ tape_frame_counter += tape->pos[i].delay;
+ }
+
+ PrintLine("-", 79);
+}
+
+void DumpTapes(void)
+{
+ static LevelDirTree *dumptape_leveldir = NULL;
+
+ dumptape_leveldir = getTreeInfoFromIdentifier(leveldir_first,
+ global.dumptape_leveldir);
+
+ if (dumptape_leveldir == NULL)
+ Fail("no such level identifier: '%s'", global.dumptape_leveldir);
+
+ if (global.dumptape_level_nr < dumptape_leveldir->first_level ||
+ global.dumptape_level_nr > dumptape_leveldir->last_level)
+ Fail("no such level number: %d", global.dumptape_level_nr);
+
+ leveldir_current = dumptape_leveldir;
+
+ if (options.mytapes)
+ LoadTape(global.dumptape_level_nr);
+ else
+ LoadSolutionTape(global.dumptape_level_nr);
+
+ DumpTape(&tape);
+
+ CloseAllAndExit(0);
+}
+
+
+// ============================================================================
+// score file functions
+// ============================================================================
+
+static void setScoreInfoToDefaultsExt(struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < MAX_SCORE_ENTRIES; i++)
+ {
+ strcpy(scores->entry[i].tape_basename, UNDEFINED_FILENAME);
+ strcpy(scores->entry[i].name, EMPTY_PLAYER_NAME);
+ scores->entry[i].score = 0;
+ scores->entry[i].time = 0;
+
+ scores->entry[i].id = -1;
+ strcpy(scores->entry[i].tape_date, UNKNOWN_NAME);
+ strcpy(scores->entry[i].platform, UNKNOWN_NAME);
+ strcpy(scores->entry[i].version, UNKNOWN_NAME);
+ strcpy(scores->entry[i].country_name, UNKNOWN_NAME);
+ strcpy(scores->entry[i].country_code, "??");
+ }
+
+ scores->num_entries = 0;
+ scores->last_added = -1;
+ scores->last_added_local = -1;
+
+ scores->updated = FALSE;
+ scores->uploaded = FALSE;
+ scores->tape_downloaded = FALSE;
+ scores->force_last_added = FALSE;
+
+ // The following values are intentionally not reset here:
+ // - last_level_nr
+ // - last_entry_nr
+ // - next_level_nr
+ // - continue_playing
+ // - continue_on_return
+}
+
+static void setScoreInfoToDefaults(void)
+{
+ setScoreInfoToDefaultsExt(&scores);
+}
+
+static void setServerScoreInfoToDefaults(void)
+{
+ setScoreInfoToDefaultsExt(&server_scores);
+}
+
+static void LoadScore_OLD(int nr)
+{
+ int i;
+ char *filename = getScoreFilename(nr);
+ char cookie[MAX_LINE_LEN];
+ char line[MAX_LINE_LEN];
+ char *line_ptr;
+ FILE *file;
+
+ if (!(file = fopen(filename, MODE_READ)))
+ return;
+
+ // check file identifier
+ if (fgets(cookie, MAX_LINE_LEN, file) == NULL)
+ cookie[0] = '\0';
+ if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
+ cookie[strlen(cookie) - 1] = '\0';
+
+ if (!checkCookieString(cookie, SCORE_COOKIE_TMPL))
+ {
+ Warn("unknown format of score file '%s'", filename);
+
+ fclose(file);
+
+ return;
+ }
+
+ for (i = 0; i < MAX_SCORE_ENTRIES; i++)
+ {
+ if (fscanf(file, "%d", &scores.entry[i].score) == EOF)
+ Warn("fscanf() failed; %s", strerror(errno));
+
+ if (fgets(line, MAX_LINE_LEN, file) == NULL)
+ line[0] = '\0';
+
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
+ line[strlen(line) - 1] = '\0';
+
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
+ {
+ strncpy(scores.entry[i].name, line_ptr, MAX_PLAYER_NAME_LEN);
+ scores.entry[i].name[MAX_PLAYER_NAME_LEN] = '\0';
+ break;
+ }
+ }
+ }
+
+ fclose(file);
+}
+
+static void ConvertScore_OLD(void)
+{
+ // only convert score to time for levels that rate playing time over score
+ if (!level.rate_time_over_score)
+ return;
+
+ // convert old score to playing time for score-less levels (like Supaplex)
+ int time_final_max = 999;
+ int i;
+
+ for (i = 0; i < MAX_SCORE_ENTRIES; i++)
+ {
+ int score = scores.entry[i].score;
+
+ if (score > 0 && score < time_final_max)
+ scores.entry[i].time = (time_final_max - score - 1) * FRAMES_PER_SECOND;
+ }
+}
+
+static int LoadScore_VERS(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ scores->file_version = getFileVersion(file);
+ scores->game_version = getFileVersion(file);
+
+ return chunk_size;
+}
+
+static int LoadScore_INFO(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ char *level_identifier = NULL;
+ int level_identifier_size;
+ int i;
+
+ level_identifier_size = getFile16BitBE(file);
+
+ level_identifier = checked_malloc(level_identifier_size);
+
+ for (i = 0; i < level_identifier_size; i++)
+ level_identifier[i] = getFile8Bit(file);
+
+ strncpy(scores->level_identifier, level_identifier, MAX_FILENAME_LEN);
+ scores->level_identifier[MAX_FILENAME_LEN] = '\0';
+
+ checked_free(level_identifier);
+
+ scores->level_nr = getFile16BitBE(file);
+ scores->num_entries = getFile16BitBE(file);
+
+ chunk_size = 2 + level_identifier_size + 2 + 2;
+
+ return chunk_size;
+}
+
+static int LoadScore_NAME(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ int i, j;
+
+ for (i = 0; i < scores->num_entries; i++)
+ {
+ for (j = 0; j < MAX_PLAYER_NAME_LEN; j++)
+ scores->entry[i].name[j] = getFile8Bit(file);
+
+ scores->entry[i].name[MAX_PLAYER_NAME_LEN] = '\0';
+ }
+
+ chunk_size = scores->num_entries * MAX_PLAYER_NAME_LEN;
+
+ return chunk_size;
+}
+
+static int LoadScore_SCOR(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < scores->num_entries; i++)
+ scores->entry[i].score = getFile16BitBE(file);
+
+ chunk_size = scores->num_entries * 2;
+
+ return chunk_size;
+}
+
+static int LoadScore_SC4R(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < scores->num_entries; i++)
+ scores->entry[i].score = getFile32BitBE(file);
+
+ chunk_size = scores->num_entries * 4;
+
+ return chunk_size;
+}
+
+static int LoadScore_TIME(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < scores->num_entries; i++)
+ scores->entry[i].time = getFile32BitBE(file);
+
+ chunk_size = scores->num_entries * 4;
+
+ return chunk_size;
+}
+
+static int LoadScore_TAPE(File *file, int chunk_size, struct ScoreInfo *scores)
+{
+ int i, j;
+
+ for (i = 0; i < scores->num_entries; i++)
+ {
+ for (j = 0; j < MAX_SCORE_TAPE_BASENAME_LEN; j++)
+ scores->entry[i].tape_basename[j] = getFile8Bit(file);
+
+ scores->entry[i].tape_basename[MAX_SCORE_TAPE_BASENAME_LEN] = '\0';
+ }
+
+ chunk_size = scores->num_entries * MAX_SCORE_TAPE_BASENAME_LEN;
+
+ return chunk_size;
+}
+
+void LoadScore(int nr)
+{
+ char *filename = getScoreFilename(nr);
+ char cookie[MAX_LINE_LEN];
+ char chunk_name[CHUNK_ID_LEN + 1];
+ int chunk_size;
+ boolean old_score_file_format = FALSE;
+ File *file;
+
+ // always start with reliable default values
+ setScoreInfoToDefaults();
+
+ if (!(file = openFile(filename, MODE_READ)))
+ return;
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (strEqual(chunk_name, "RND1"))
+ {
+ getFile32BitBE(file); // not used
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (!strEqual(chunk_name, "SCOR"))
+ {
+ Warn("unknown format of score file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+ }
+ else // check for old file format with cookie string
+ {
+ strcpy(cookie, chunk_name);
+ if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL)
+ cookie[4] = '\0';
+ if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
+ cookie[strlen(cookie) - 1] = '\0';
+
+ if (!checkCookieString(cookie, SCORE_COOKIE_TMPL))
+ {
+ Warn("unknown format of score file '%s'", filename);
+
+ closeFile(file);
+
+ return;
+ }
+
+ old_score_file_format = TRUE;
+ }
+
+ if (old_score_file_format)
+ {
+ // score files from versions before 4.2.4.0 without chunk structure
+ LoadScore_OLD(nr);
+
+ // convert score to time, if possible (mainly for Supaplex levels)
+ ConvertScore_OLD();
+ }
+ else
+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(File *, int, struct ScoreInfo *);
+ }
+ chunk_info[] =
+ {
+ { "VERS", SCORE_CHUNK_VERS_SIZE, LoadScore_VERS },
+ { "INFO", -1, LoadScore_INFO },
+ { "NAME", -1, LoadScore_NAME },
+ { "SCOR", -1, LoadScore_SCOR },
+ { "SC4R", -1, LoadScore_SC4R },
+ { "TIME", -1, LoadScore_TIME },
+ { "TAPE", -1, LoadScore_TAPE },
+
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunkBE(file, chunk_name, &chunk_size))
+ {
+ int i = 0;
+
+ while (chunk_info[i].name != NULL &&
+ !strEqual(chunk_name, chunk_info[i].name))
+ i++;
+
+ if (chunk_info[i].name == NULL)
+ {
+ Warn("unknown chunk '%s' in score file '%s'",
+ chunk_name, filename);
+
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else if (chunk_info[i].size != -1 &&
+ chunk_info[i].size != chunk_size)
+ {
+ Warn("wrong size (%d) of chunk '%s' in score file '%s'",
+ chunk_size, chunk_name, filename);
+
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else
+ {
+ // call function to load this score chunk
+ int chunk_size_expected =
+ (chunk_info[i].loader)(file, chunk_size, &scores);
+
+ // the size of some chunks cannot be checked before reading other
+ // chunks first (like "HEAD" and "BODY") that contain some header
+ // information, so check them here
+ if (chunk_size_expected != chunk_size)
+ {
+ Warn("wrong size (%d) of chunk '%s' in score file '%s'",
+ chunk_size, chunk_name, filename);
+ }
+ }
+ }
+ }
+
+ closeFile(file);
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+void SaveScore_OLD(int nr)
+{
+ int i;
+ char *filename = getScoreFilename(nr);
+ FILE *file;
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ InitScoreDirectory(levelset.identifier);
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Warn("cannot save score for level %d", nr);
+
+ return;
+ }
+
+ fprintf(file, "%s\n\n", SCORE_COOKIE);
+
+ for (i = 0; i < MAX_SCORE_ENTRIES; i++)
+ fprintf(file, "%d %s\n", scores.entry[i].score, scores.entry[i].name);
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+}
+#endif
+
+static void SaveScore_VERS(FILE *file, struct ScoreInfo *scores)
+{
+ putFileVersion(file, scores->file_version);
+ putFileVersion(file, scores->game_version);
+}
+
+static void SaveScore_INFO(FILE *file, struct ScoreInfo *scores)
+{
+ int level_identifier_size = strlen(scores->level_identifier) + 1;
+ int i;
+
+ putFile16BitBE(file, level_identifier_size);
+
+ for (i = 0; i < level_identifier_size; i++)
+ putFile8Bit(file, scores->level_identifier[i]);
+
+ putFile16BitBE(file, scores->level_nr);
+ putFile16BitBE(file, scores->num_entries);
+}
+
+static void SaveScore_NAME(FILE *file, struct ScoreInfo *scores)
+{
+ int i, j;
+
+ for (i = 0; i < scores->num_entries; i++)
+ {
+ int name_size = strlen(scores->entry[i].name);
+
+ for (j = 0; j < MAX_PLAYER_NAME_LEN; j++)
+ putFile8Bit(file, (j < name_size ? scores->entry[i].name[j] : 0));
+ }
+}
+
+static void SaveScore_SCOR(FILE *file, struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < scores->num_entries; i++)
+ putFile16BitBE(file, scores->entry[i].score);
+}
+
+static void SaveScore_SC4R(FILE *file, struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < scores->num_entries; i++)
+ putFile32BitBE(file, scores->entry[i].score);
+}
+
+static void SaveScore_TIME(FILE *file, struct ScoreInfo *scores)
+{
+ int i;
+
+ for (i = 0; i < scores->num_entries; i++)
+ putFile32BitBE(file, scores->entry[i].time);
+}
+
+static void SaveScore_TAPE(FILE *file, struct ScoreInfo *scores)
+{
+ int i, j;
+
+ for (i = 0; i < scores->num_entries; i++)
+ {
+ int size = strlen(scores->entry[i].tape_basename);
+
+ for (j = 0; j < MAX_SCORE_TAPE_BASENAME_LEN; j++)
+ putFile8Bit(file, (j < size ? scores->entry[i].tape_basename[j] : 0));
+ }
+}
+
+static void SaveScoreToFilename(char *filename)
+{
+ FILE *file;
+ int info_chunk_size;
+ int name_chunk_size;
+ int scor_chunk_size;
+ int sc4r_chunk_size;
+ int time_chunk_size;
+ int tape_chunk_size;
+ boolean has_large_score_values;
+ int i;
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Warn("cannot save score file '%s'", filename);
+
+ return;
+ }
+
+ info_chunk_size = 2 + (strlen(scores.level_identifier) + 1) + 2 + 2;
+ name_chunk_size = scores.num_entries * MAX_PLAYER_NAME_LEN;
+ scor_chunk_size = scores.num_entries * 2;
+ sc4r_chunk_size = scores.num_entries * 4;
+ time_chunk_size = scores.num_entries * 4;
+ tape_chunk_size = scores.num_entries * MAX_SCORE_TAPE_BASENAME_LEN;
+
+ has_large_score_values = FALSE;
+ for (i = 0; i < scores.num_entries; i++)
+ if (scores.entry[i].score > 0xffff)
+ has_large_score_values = TRUE;
+
+ putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
+ putFileChunkBE(file, "SCOR", CHUNK_SIZE_NONE);
+
+ putFileChunkBE(file, "VERS", SCORE_CHUNK_VERS_SIZE);
+ SaveScore_VERS(file, &scores);
+
+ putFileChunkBE(file, "INFO", info_chunk_size);
+ SaveScore_INFO(file, &scores);
+
+ putFileChunkBE(file, "NAME", name_chunk_size);
+ SaveScore_NAME(file, &scores);
+
+ if (has_large_score_values)
+ {
+ putFileChunkBE(file, "SC4R", sc4r_chunk_size);
+ SaveScore_SC4R(file, &scores);
+ }
+ else
+ {
+ putFileChunkBE(file, "SCOR", scor_chunk_size);
+ SaveScore_SCOR(file, &scores);
+ }
+
+ putFileChunkBE(file, "TIME", time_chunk_size);
+ SaveScore_TIME(file, &scores);
+
+ putFileChunkBE(file, "TAPE", tape_chunk_size);
+ SaveScore_TAPE(file, &scores);
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+}
+
+void SaveScore(int nr)
+{
+ char *filename = getScoreFilename(nr);
+ int i;
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ InitScoreDirectory(levelset.identifier);
+
+ scores.file_version = FILE_VERSION_ACTUAL;
+ scores.game_version = GAME_VERSION_ACTUAL;
+
+ strncpy(scores.level_identifier, levelset.identifier, MAX_FILENAME_LEN);
+ scores.level_identifier[MAX_FILENAME_LEN] = '\0';
+ scores.level_nr = level_nr;
+
+ for (i = 0; i < MAX_SCORE_ENTRIES; i++)
+ if (scores.entry[i].score == 0 &&
+ scores.entry[i].time == 0 &&
+ strEqual(scores.entry[i].name, EMPTY_PLAYER_NAME))
+ break;
+
+ scores.num_entries = i;
+
+ if (scores.num_entries == 0)
+ return;
+
+ SaveScoreToFilename(filename);
+}
+
+static void LoadServerScoreFromCache(int nr)
+{
+ struct ScoreEntry score_entry;
+ struct
+ {
+ void *value;
+ boolean is_string;
+ int string_size;
+ }
+ score_mapping[] =
+ {
+ { &score_entry.score, FALSE, 0 },
+ { &score_entry.time, FALSE, 0 },
+ { score_entry.name, TRUE, MAX_PLAYER_NAME_LEN },
+ { score_entry.tape_basename, TRUE, MAX_FILENAME_LEN },
+ { score_entry.tape_date, TRUE, MAX_ISO_DATE_LEN },
+ { &score_entry.id, FALSE, 0 },
+ { score_entry.platform, TRUE, MAX_PLATFORM_TEXT_LEN },
+ { score_entry.version, TRUE, MAX_VERSION_TEXT_LEN },
+ { score_entry.country_code, TRUE, MAX_COUNTRY_CODE_LEN },
+ { score_entry.country_name, TRUE, MAX_COUNTRY_NAME_LEN },
+
+ { NULL, FALSE, 0 }
+ };
+ char *filename = getScoreCacheFilename(nr);
+ SetupFileHash *score_hash = loadSetupFileHash(filename);
+ int i, j;
+
+ server_scores.num_entries = 0;
+
+ if (score_hash == NULL)
+ return;
+
+ for (i = 0; i < MAX_SCORE_ENTRIES; i++)
+ {
+ score_entry = server_scores.entry[i];
+
+ for (j = 0; score_mapping[j].value != NULL; j++)
+ {
+ char token[10];
+
+ sprintf(token, "%02d.%d", i, j);
+
+ char *value = getHashEntry(score_hash, token);
+
+ if (value == NULL)
+ continue;
+
+ if (score_mapping[j].is_string)
+ {
+ char *score_value = (char *)score_mapping[j].value;
+ int value_size = score_mapping[j].string_size;
+
+ strncpy(score_value, value, value_size);
+ score_value[value_size] = '\0';
+ }
+ else
+ {
+ int *score_value = (int *)score_mapping[j].value;
+
+ *score_value = atoi(value);
+ }
+
+ server_scores.num_entries = i + 1;
+ }
+
+ server_scores.entry[i] = score_entry;
+ }
+
+ freeSetupFileHash(score_hash);
+}
+
+void LoadServerScore(int nr, boolean download_score)
+{
+ if (!setup.use_api_server)
+ return;
+
+ // always start with reliable default values
+ setServerScoreInfoToDefaults();
+
+ // 1st step: load server scores from cache file (which may not exist)
+ // (this should prevent reading it while the thread is writing to it)
+ LoadServerScoreFromCache(nr);
+
+ if (download_score && runtime.use_api_server)
+ {
+ // 2nd step: download server scores from score server to cache file
+ // (as thread, as it might time out if the server is not reachable)
+ ApiGetScoreAsThread(nr);
+ }
+}
+
+void PrepareScoreTapesForUpload(char *leveldir_subdir)
+{
+ MarkTapeDirectoryUploadsAsIncomplete(leveldir_subdir);
+
+ // if score tape not uploaded, ask for uploading missing tapes later
+ if (!setup.has_remaining_tapes)
+ setup.ask_for_remaining_tapes = TRUE;
+
+ setup.provide_uploading_tapes = TRUE;
+ setup.has_remaining_tapes = TRUE;
+
+ SaveSetup_ServerSetup();
+}
+
+void SaveServerScore(int nr, boolean tape_saved)
+{
+ if (!runtime.use_api_server)
+ {
+ PrepareScoreTapesForUpload(leveldir_current->subdir);
+
+ return;
+ }
+
+ ApiAddScoreAsThread(nr, tape_saved, NULL);
+}
+
+void SaveServerScoreFromFile(int nr, boolean tape_saved,
+ char *score_tape_filename)
+{
+ if (!runtime.use_api_server)
+ return;
+
+ ApiAddScoreAsThread(nr, tape_saved, score_tape_filename);
+}
+
+void LoadLocalAndServerScore(int nr, boolean download_score)
+{
+ int last_added_local = scores.last_added_local;
+ boolean force_last_added = scores.force_last_added;
+
+ // needed if only showing server scores
+ setScoreInfoToDefaults();
+
+ if (!strEqual(setup.scores_in_highscore_list, STR_SCORES_TYPE_SERVER_ONLY))
+ LoadScore(nr);
+
+ // restore last added local score entry (before merging server scores)
+ scores.last_added = scores.last_added_local = last_added_local;
+
+ if (setup.use_api_server &&
+ !strEqual(setup.scores_in_highscore_list, STR_SCORES_TYPE_LOCAL_ONLY))
+ {
+ // load server scores from cache file and trigger update from server
+ LoadServerScore(nr, download_score);
+
+ // merge local scores with scores from server
+ MergeServerScore();
+ }
+
+ if (force_last_added)
+ scores.force_last_added = force_last_added;
+}
+
+
+// ============================================================================
+// setup file functions
+// ============================================================================
+
+#define TOKEN_STR_PLAYER_PREFIX "player_"
+
+
+static struct TokenInfo global_setup_tokens[] =
+{
+ {
+ TYPE_STRING,
+ &setup.player_name, "player_name"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.multiple_users, "multiple_users"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.sound, "sound"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.sound_loops, "repeating_sound_loops"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.sound_music, "background_music"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.sound_simple, "simple_sound_effects"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.toons, "toons"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.scroll_delay, "scroll_delay"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.forced_scroll_delay, "forced_scroll_delay"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.scroll_delay_value, "scroll_delay_value"
+ },
+ {
+ TYPE_STRING,
+ &setup.engine_snapshot_mode, "engine_snapshot_mode"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.engine_snapshot_memory, "engine_snapshot_memory"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.fade_screens, "fade_screens"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.autorecord, "automatic_tape_recording"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.autorecord_after_replay, "autorecord_after_replay"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.auto_pause_on_start, "auto_pause_on_start"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.show_titlescreen, "show_titlescreen"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.quick_doors, "quick_doors"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.team_mode, "team_mode"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.handicap, "handicap"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.skip_levels, "skip_levels"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.increment_levels, "increment_levels"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.auto_play_next_level, "auto_play_next_level"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.count_score_after_game, "count_score_after_game"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.show_scores_after_game, "show_scores_after_game"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.time_limit, "time_limit"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.fullscreen, "fullscreen"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.window_scaling_percent, "window_scaling_percent"
+ },
+ {
+ TYPE_STRING,
+ &setup.window_scaling_quality, "window_scaling_quality"
+ },
+ {
+ TYPE_STRING,
+ &setup.screen_rendering_mode, "screen_rendering_mode"
+ },
+ {
+ TYPE_STRING,
+ &setup.vsync_mode, "vsync_mode"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_on_escape, "ask_on_escape"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_on_escape_editor, "ask_on_escape_editor"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_on_game_over, "ask_on_game_over"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_on_quit_game, "ask_on_quit_game"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_on_quit_program, "ask_on_quit_program"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.quick_switch, "quick_player_switch"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.input_on_focus, "input_on_focus"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.prefer_aga_graphics, "prefer_aga_graphics"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.prefer_lowpass_sounds, "prefer_lowpass_sounds"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.prefer_extra_panel_items, "prefer_extra_panel_items"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.game_speed_extended, "game_speed_extended"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.game_frame_delay, "game_frame_delay"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.sp_show_border_elements, "sp_show_border_elements"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.small_game_graphics, "small_game_graphics"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.show_load_save_buttons, "show_load_save_buttons"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.show_undo_redo_buttons, "show_undo_redo_buttons"
+ },
+ {
+ TYPE_STRING,
+ &setup.scores_in_highscore_list, "scores_in_highscore_list"
+ },
+ {
+ TYPE_STRING,
+ &setup.graphics_set, "graphics_set"
+ },
+ {
+ TYPE_STRING,
+ &setup.sounds_set, "sounds_set"
+ },
+ {
+ TYPE_STRING,
+ &setup.music_set, "music_set"
+ },
+ {
+ TYPE_SWITCH3,
+ &setup.override_level_graphics, "override_level_graphics"
+ },
+ {
+ TYPE_SWITCH3,
+ &setup.override_level_sounds, "override_level_sounds"
+ },
+ {
+ TYPE_SWITCH3,
+ &setup.override_level_music, "override_level_music"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.volume_simple, "volume_simple"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.volume_loops, "volume_loops"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.volume_music, "volume_music"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.network_mode, "network_mode"
+ },
+ {
+ TYPE_PLAYER,
+ &setup.network_player_nr, "network_player"
+ },
+ {
+ TYPE_STRING,
+ &setup.network_server_hostname, "network_server_hostname"
+ },
+ {
+ TYPE_STRING,
+ &setup.touch.control_type, "touch.control_type"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.move_distance, "touch.move_distance"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.drop_distance, "touch.drop_distance"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.transparency, "touch.transparency"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.draw_outlined, "touch.draw_outlined"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.draw_pressed, "touch.draw_pressed"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.grid_xsize[0], "touch.virtual_buttons.0.xsize"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.grid_ysize[0], "touch.virtual_buttons.0.ysize"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.grid_xsize[1], "touch.virtual_buttons.1.xsize"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.touch.grid_ysize[1], "touch.virtual_buttons.1.ysize"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.touch.overlay_buttons, "touch.overlay_buttons"
+ },
+};
+
+static struct TokenInfo auto_setup_tokens[] =
+{
+ {
+ TYPE_INTEGER,
+ &setup.auto_setup.editor_zoom_tilesize, "editor.zoom_tilesize"
+ },
+};
+
+static struct TokenInfo server_setup_tokens[] =
+{
+ {
+ TYPE_STRING,
+ &setup.player_uuid, "player_uuid"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.player_version, "player_version"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.use_api_server, TEST_PREFIX "use_api_server"
+ },
+ {
+ TYPE_STRING,
+ &setup.api_server_hostname, TEST_PREFIX "api_server_hostname"
+ },
+ {
+ TYPE_STRING,
+ &setup.api_server_password, TEST_PREFIX "api_server_password"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_for_uploading_tapes, TEST_PREFIX "ask_for_uploading_tapes"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_for_remaining_tapes, TEST_PREFIX "ask_for_remaining_tapes"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.provide_uploading_tapes, TEST_PREFIX "provide_uploading_tapes"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.ask_for_using_api_server,TEST_PREFIX "ask_for_using_api_server"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.has_remaining_tapes, TEST_PREFIX "has_remaining_tapes"
+ },
+};
+
+static struct TokenInfo editor_setup_tokens[] =
+{
+ {
+ TYPE_SWITCH,
+ &setup.editor.el_classic, "editor.el_classic"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor.el_custom, "editor.el_custom"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor.el_user_defined, "editor.el_user_defined"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor.el_dynamic, "editor.el_dynamic"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor.el_headlines, "editor.el_headlines"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor.show_element_token, "editor.show_element_token"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor.show_read_only_warning, "editor.show_read_only_warning"
+ },
+};
+
+static struct TokenInfo editor_cascade_setup_tokens[] =
+{
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_bd, "editor.cascade.el_bd"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_em, "editor.cascade.el_em"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_emc, "editor.cascade.el_emc"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_rnd, "editor.cascade.el_rnd"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_sb, "editor.cascade.el_sb"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_sp, "editor.cascade.el_sp"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_dc, "editor.cascade.el_dc"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_dx, "editor.cascade.el_dx"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_mm, "editor.cascade.el_mm"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_df, "editor.cascade.el_df"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_chars, "editor.cascade.el_chars"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_steel_chars, "editor.cascade.el_steel_chars"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_ce, "editor.cascade.el_ce"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_ge, "editor.cascade.el_ge"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_es, "editor.cascade.el_es"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_ref, "editor.cascade.el_ref"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_user, "editor.cascade.el_user"
+ },
+ {
+ TYPE_SWITCH,
+ &setup.editor_cascade.el_dynamic, "editor.cascade.el_dynamic"
+ },
+};
+
+static struct TokenInfo shortcut_setup_tokens[] =
+{
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.save_game, "shortcut.save_game"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.load_game, "shortcut.load_game"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.restart_game, "shortcut.restart_game"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.pause_before_end, "shortcut.pause_before_end"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.toggle_pause, "shortcut.toggle_pause"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.focus_player[0], "shortcut.focus_player_1"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.focus_player[1], "shortcut.focus_player_2"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.focus_player[2], "shortcut.focus_player_3"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.focus_player[3], "shortcut.focus_player_4"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.focus_player_all, "shortcut.focus_player_all"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.tape_eject, "shortcut.tape_eject"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.tape_extra, "shortcut.tape_extra"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.tape_stop, "shortcut.tape_stop"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.tape_pause, "shortcut.tape_pause"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.tape_record, "shortcut.tape_record"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.tape_play, "shortcut.tape_play"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.sound_simple, "shortcut.sound_simple"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.sound_loops, "shortcut.sound_loops"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.sound_music, "shortcut.sound_music"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.snap_left, "shortcut.snap_left"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.snap_right, "shortcut.snap_right"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.snap_up, "shortcut.snap_up"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.shortcut.snap_down, "shortcut.snap_down"
+ },
+};
+
+static struct SetupInputInfo setup_input;
+static struct TokenInfo player_setup_tokens[] =
+{
+ {
+ TYPE_BOOLEAN,
+ &setup_input.use_joystick, ".use_joystick"
+ },
+ {
+ TYPE_STRING,
+ &setup_input.joy.device_name, ".joy.device_name"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.xleft, ".joy.xleft"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.xmiddle, ".joy.xmiddle"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.xright, ".joy.xright"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.yupper, ".joy.yupper"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.ymiddle, ".joy.ymiddle"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.ylower, ".joy.ylower"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.snap, ".joy.snap_field"
+ },
+ {
+ TYPE_INTEGER,
+ &setup_input.joy.drop, ".joy.place_bomb"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup_input.key.left, ".key.move_left"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup_input.key.right, ".key.move_right"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup_input.key.up, ".key.move_up"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup_input.key.down, ".key.move_down"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup_input.key.snap, ".key.snap_field"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup_input.key.drop, ".key.place_bomb"
+ },
+};
+
+static struct TokenInfo system_setup_tokens[] =
+{
+ {
+ TYPE_STRING,
+ &setup.system.sdl_renderdriver, "system.sdl_renderdriver"
+ },
+ {
+ TYPE_STRING,
+ &setup.system.sdl_videodriver, "system.sdl_videodriver"
+ },
+ {
+ TYPE_STRING,
+ &setup.system.sdl_audiodriver, "system.sdl_audiodriver"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.system.audio_fragment_size, "system.audio_fragment_size"
+ },
+};
+
+static struct TokenInfo internal_setup_tokens[] =
+{
+ {
+ TYPE_STRING,
+ &setup.internal.program_title, "program_title"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_version, "program_version"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_author, "program_author"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_email, "program_email"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_website, "program_website"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_copyright, "program_copyright"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_company, "program_company"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.program_icon_file, "program_icon_file"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.default_graphics_set, "default_graphics_set"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.default_sounds_set, "default_sounds_set"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.default_music_set, "default_music_set"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.fallback_graphics_file, "fallback_graphics_file"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.fallback_sounds_file, "fallback_sounds_file"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.fallback_music_file, "fallback_music_file"
+ },
+ {
+ TYPE_STRING,
+ &setup.internal.default_level_series, "default_level_series"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.internal.default_window_width, "default_window_width"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.internal.default_window_height, "default_window_height"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.choose_from_top_leveldir, "choose_from_top_leveldir"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.show_scaling_in_title, "show_scaling_in_title"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.create_user_levelset, "create_user_levelset"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_screens_from_main, "info_screens_from_main"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_game, "menu_game"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_engines, "menu_engines"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_editor, "menu_editor"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_graphics, "menu_graphics"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_sound, "menu_sound"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_artwork, "menu_artwork"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_input, "menu_input"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_touch, "menu_touch"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_shortcuts, "menu_shortcuts"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_exit, "menu_exit"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_save_and_exit, "menu_save_and_exit"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_shortcuts_various, "menu_shortcuts_various"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_shortcuts_focus, "menu_shortcuts_focus"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_shortcuts_tape, "menu_shortcuts_tape"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_shortcuts_sound, "menu_shortcuts_sound"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.menu_shortcuts_snap, "menu_shortcuts_snap"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_title, "info_title"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_elements, "info_elements"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_music, "info_music"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_credits, "info_credits"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_program, "info_program"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_version, "info_version"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_levelset, "info_levelset"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.internal.info_exit, "info_exit"
+ },
+};
+
+static struct TokenInfo debug_setup_tokens[] =
+{
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[0], "debug.frame_delay_0"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[1], "debug.frame_delay_1"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[2], "debug.frame_delay_2"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[3], "debug.frame_delay_3"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[4], "debug.frame_delay_4"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[5], "debug.frame_delay_5"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[6], "debug.frame_delay_6"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[7], "debug.frame_delay_7"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[8], "debug.frame_delay_8"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.frame_delay[9], "debug.frame_delay_9"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[0], "debug.key.frame_delay_0"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[1], "debug.key.frame_delay_1"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[2], "debug.key.frame_delay_2"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[3], "debug.key.frame_delay_3"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[4], "debug.key.frame_delay_4"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[5], "debug.key.frame_delay_5"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[6], "debug.key.frame_delay_6"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[7], "debug.key.frame_delay_7"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[8], "debug.key.frame_delay_8"
+ },
+ {
+ TYPE_KEY_X11,
+ &setup.debug.frame_delay_key[9], "debug.key.frame_delay_9"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.debug.frame_delay_use_mod_key, "debug.frame_delay.use_mod_key"},
+ {
+ TYPE_BOOLEAN,
+ &setup.debug.frame_delay_game_only, "debug.frame_delay.game_only"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.debug.show_frames_per_second, "debug.show_frames_per_second"
+ },
+ {
+ TYPE_SWITCH3,
+ &setup.debug.xsn_mode, "debug.xsn_mode"
+ },
+ {
+ TYPE_INTEGER,
+ &setup.debug.xsn_percent, "debug.xsn_percent"
+ },
+};
+
+static struct TokenInfo options_setup_tokens[] =
+{
+ {
+ TYPE_BOOLEAN,
+ &setup.options.verbose, "options.verbose"
+ },
+ {
+ TYPE_BOOLEAN,
+ &setup.options.debug, "options.debug"
+ },
+ {
+ TYPE_STRING,
+ &setup.options.debug_mode, "options.debug_mode"
+ },
+};
+
+static void setSetupInfoToDefaults(struct SetupInfo *si)
+{
+ int i;
+
+ si->player_name = getStringCopy(getDefaultUserName(user.nr));
+
+ si->multiple_users = TRUE;
+
+ si->sound = TRUE;
+ si->sound_loops = TRUE;
+ si->sound_music = TRUE;
+ si->sound_simple = TRUE;
+ si->toons = TRUE;
+ si->scroll_delay = TRUE;
+ si->forced_scroll_delay = FALSE;
+ si->scroll_delay_value = STD_SCROLL_DELAY;
+ si->engine_snapshot_mode = getStringCopy(STR_SNAPSHOT_MODE_DEFAULT);
+ si->engine_snapshot_memory = SNAPSHOT_MEMORY_DEFAULT;
+ si->fade_screens = TRUE;
+ si->autorecord = TRUE;
+ si->autorecord_after_replay = TRUE;
+ si->auto_pause_on_start = FALSE;
+ si->show_titlescreen = TRUE;
+ si->quick_doors = FALSE;
+ si->team_mode = FALSE;
+ si->handicap = TRUE;
+ si->skip_levels = TRUE;
+ si->increment_levels = TRUE;
+ si->auto_play_next_level = TRUE;
+ si->count_score_after_game = TRUE;
+ si->show_scores_after_game = TRUE;
+ si->time_limit = TRUE;
+ si->fullscreen = FALSE;
+ si->window_scaling_percent = STD_WINDOW_SCALING_PERCENT;
+ si->window_scaling_quality = getStringCopy(SCALING_QUALITY_DEFAULT);
+ si->screen_rendering_mode = getStringCopy(STR_SPECIAL_RENDERING_DEFAULT);
+ si->vsync_mode = getStringCopy(STR_VSYNC_MODE_DEFAULT);
+ si->ask_on_escape = TRUE;
+ si->ask_on_escape_editor = TRUE;
+ si->ask_on_game_over = TRUE;
+ si->ask_on_quit_game = TRUE;
+ si->ask_on_quit_program = TRUE;
+ si->quick_switch = FALSE;
+ si->input_on_focus = FALSE;
+ si->prefer_aga_graphics = TRUE;
+ si->prefer_lowpass_sounds = FALSE;
+ si->prefer_extra_panel_items = TRUE;
+ si->game_speed_extended = FALSE;
+ si->game_frame_delay = GAME_FRAME_DELAY;
+ si->sp_show_border_elements = FALSE;
+ si->small_game_graphics = FALSE;
+ si->show_load_save_buttons = FALSE;
+ si->show_undo_redo_buttons = FALSE;
+ si->scores_in_highscore_list = getStringCopy(STR_SCORES_TYPE_DEFAULT);