I have some code to load map files into memory. It was working fine for a simple map I made, but when I tried loading a larger more complicated one it crashed. Now I have been looking at it for ages to fix the problem and haven't managed to solve it.
The structure of the map file is like this:
map header
board header
tiles
board information
objects
next board header and so on..
I have narrowed down what is going wrong. The first board is loading fine, the tiles look fine etc. The second board starts off well, the board title (in the board header) is correct, but then there are some tiles missing which then messes everything else up.
Now as the board title on the second board loads ok, it surely meens that everything before that has been read correctly. In other words it meens that the second board is not loading right even though it is being given the right place in the file! So I think the reading of the board header the second time around is going wrong but I can't see how.
Its probably going to be impossible for anyone here to actualy solve my problem because you would have to learn how the zzt files work and stuff but what I could use is suggestions about what to do / check next because I am totaly stuck!
Anyway I might as well paste the relevent code in incase I have written somthing in a bad way that could cause an intermitant problem.
Thanks
Code:
bool load_world(char *file_name)
{
FILE *file_pointer;
char buffer[512];
int i, title_length;
file_pointer = fopen(file_name, "rb");
if (file_pointer == NULL)
{
perror("FAILED opening world file");
system("PAUSE");
return false;
}
else
perror("Opened world file");
for (i = 0; i < 512; i++)
buffer[i] = getc(file_pointer);
/* Get the World Header stuff */
world.board_count = (unsigned short)buffer[2] + buffer[3] * 256;
world.board_count ++;
world.start_ammo = (unsigned short)buffer[4] + buffer[5] * 256;
world.start_gems = (unsigned short)buffer[6] + buffer[7] * 256;
world.blue_key = buffer[8];
world.green_key = buffer[9];
world.cyan_key = buffer[10];
world.red_key = buffer[11];
world.purple_key = buffer[12];
world.yellow_key = buffer[13];
world.white_key = buffer[14];
world.start_health = (unsigned short)buffer[15] + buffer[16] * 256;
world.start_board = (unsigned short)buffer[17] + buffer[18] * 256;
world.start_torches = (unsigned short)buffer[19] + buffer[20] * 256;
world.torch_cycles_left = (unsigned short)buffer[21] + buffer[22] * 256;
world.energiser_cycles_left = (unsigned short)buffer[23] + buffer[24] * 256;
world.start_score = (unsigned short)buffer[27] + buffer[28] * 256;
/* Get the title of the world */
title_length = buffer[29];
world.title = malloc(title_length + 1);
for (i = 0; i < title_length; i++)
world.title[i] = buffer[30 + i];
world.title[i] = '\0';
/* Is the world file a saved game? */
world.saved_game = buffer[264];
/* Load the boards */
world.board = malloc( sizeof(struct Sboard) * world.board_count);
for (i = 0; i < world.board_count; i++)
load_board(file_pointer, &world.board[i]);
/* Close the file and return */
fclose(file_pointer);
return true;
}
void load_board(FILE *file_pointer, struct Sboard *board)
{
char *buffer;
unsigned char title_length;
int i, board_size, message_length;
int current_tile = 0;
/* Read the board header into the buffer */
buffer = malloc(53);
for (i = 0; i < 53; i++)
buffer[i] = getc(file_pointer);
/* Get the board size */
board_size = (unsigned short)buffer[0] + buffer[1] * 256;
/* Get the title */
title_length = buffer[2];
board->title = malloc(title_length + 1);
for (i = 0; i < title_length; i++)
board->title[i] = buffer[3 + i];
board->title[i] = '\0';
free(buffer);
/* Get all the tiles put into the board.tile array */
buffer = malloc(3);
printw("current tile: %d\n", current_tile);
while (current_tile < 1500)
{
for (i = 0; i < 3; i++)
buffer[i] = getc(file_pointer);
for (i = 0; i <= buffer[0]; i++)
{
board->tile[current_tile + i].code = buffer[1];
board->tile[current_tile + i].colour = buffer[2];
}
current_tile += buffer[0];
for (i = 0; i < current_tile; i++)
draw_zzt_char(i % 60, i / 60, board->tile[i].code, board->tile[i].colour);
printw("board title: %s\n", board->title);
getch();
clear();
}
free(buffer);
/* Get the board information */
buffer = malloc(88);
for (i = 0; i < 88; i++)
buffer[i] = getc(file_pointer);
board->maximum_shots_fired = buffer[0];
board->dark = buffer[1];
board->board_north = buffer[2];
board->board_south = buffer[3];
board->board_west = buffer[4];
board->board_east = buffer[5];
board->teleport_when_hurt = buffer[6];
message_length = buffer[7];
board->message = malloc(message_length + 1);
for (i = 0; i < message_length; i++)
board->message[i] = buffer[8 + i];
board->message[i] = '\0';
board->time_limit = (unsigned short)buffer[68] + buffer[69] * 256;
board->object_count = (unsigned short)buffer[86] + buffer[87] * 256;
board->object_count ++;
printw("board title: %s\n", board->title);
printw("Object Count: %d\n", board->object_count);
printw("Object Count: %d\n", (unsigned short)buffer[86+10] + buffer[87+10] * 256);
printw("Dark?: %d\n", buffer[1+10]);
free(buffer);
refresh();
getch();
clear();
/* Load all of the objects */
board->object = malloc(board->object_count);
for (i = 0; i < board->object_count; i++)
load_object(file_pointer, &board->object[i]);
}
void load_object(FILE *file_pointer, struct Sobject *object)
{
int i, code_length;
char *buffer;
/* Load the object's information */
buffer = malloc(33);
for (i = 0; i < 33; i++)
buffer[i] = getc(file_pointer);
object->x_pos = buffer[0] - 1;
object->y_pos = buffer[1] - 1;
object->x_step = (unsigned short)buffer[2] + buffer[3] * 256;
object->y_step = (unsigned short)buffer[4] + buffer[5] * 256;
object->cycle_speed = (unsigned short)buffer[6] + buffer[7] * 256;
object->parameter1 = buffer[8];
object->parameter2 = buffer[9];
object->parameter3 = buffer[10];
object->below_tile_code = buffer[15];
object->below_tile_colour = buffer[16];
object->cursor_instruction = (unsigned short)buffer[21] + buffer[22] * 256;
code_length = (unsigned short)buffer[23] + buffer[24] * 256;
/* Load the object's code if it has some */
if (code_length > 0)
{
object->code = malloc(code_length + 1);
for (i = 0; i < code_length; i++)
object->code[i] = getc(file_pointer);
object->code[i] = '\0';
}
free(buffer);
}