Re: [CODE] Coordinate World Map Problems

From: Thomas Arp (t_arp@stofanet.dk)
Date: 05/13/03


Ack, that message was poorly formatted. I've tried correcting it a bit.

From: "Tristan Haynes"

I assume world_map_num is a 2-dimensional int array:

int world_map_num[MAP_HEIGHT+1][MAP_WIDTH+1];


>  void load_world_map()
>  {
>    int x, y, size, coord, coord_num = 0;
>    FILE *mapfile;
> //  int file_coord = 0, file_sector = 0;
> //  determine actual coordinate numbers
>    for (x = 1; x <= MAP_HEIGHT; x++) {
>      for (y = 1; y <= MAP_WIDTH; y++) {
>        world_map_num[x][y] = coord_num++;
>      }
>    }
>
>    // create the coordinate data structure
>    CREATE(map_proto, struct world_map, coord_num);
>    size = sizeof(struct world_map) * coord_num;
>    log("   %d coords, %d bytes.", coord_num, size);
>
>    // open world map file, if problem then shutdown
>    if (!(mapfile = fopen("../lib/world.map", "r"))) {
>      log("SYSERR: cannot open world map file");
>      exit(1);
>    }
>
>    /***** PROBLEM CODE HERE *****/
>    // load the map coord data from file to structure
>    fscanf(mapfile, "%d %d\n", &file_coord, &file_sector);
>    for (coord = 0; coord < coord_num; coord++) {
>      map_proto[file_coord].sector_type = file_sector;
>      if (coord > coord_num)
>        break;
>      fscanf(mapfile, "%d %d\n", &file_coord, &file_sector);
>    }
>    /***** PROBLEM CODE HERE *****/
>    fclose(mapfile);
>  }
> If anyone could help me with this it would be greatly appreciated
> so I can continue my work.

Your problem code assumes, that:
1. there is a line with two numbers for each coordinate in the
   grid. (that means it will expect there to be 10.000 lines in a
   100x100 grid).
2. the first number is within the predefined range.

Here's how I would write that part:

   // load the map coord data from file to structure
   char line[128];
   int num_lines = 0;
   while (get_line(mapfile, line)) {
     if (sscanf(line, "%d %d", &file_coord, &file_sector) != 2)
       break;
     if (file_coord < 0 || file_coord > coord_num)
       break;
     num_lines++;
     map_proto[file_coord].sector_type = file_sector;
   }
   if (num_lines != coord_num) {
     log("load_world_map: sector info count mismatch"
         " (%d found)", num_lines);
     exit(1);
   }

This code assumes it gets a file, no more, no less. If the info
in the file isn't correct, it bails out and gives an error.
It uses the same file format as your other example, and I think
you will regret it on the long run. Reading from file in this way
is timeconsuming, and error-prone. It's hard to get a good idea
of the layout of your world, if it saved in this format. Might
I suggest using another method, similar to how pictures are saved,
perhaps even in a viewable format (bmp, pcx, avs(.x)) ?

Welcor

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/26/03 PDT